Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * nanohttp.c: minimalist HTTP GET implementation to fetch external subsets. |
| 3 | * focuses on size, streamability, reentrancy and portability |
| 4 | * |
| 5 | * This is clearly not a general purpose HTTP implementation |
| 6 | * If you look for one, check: |
| 7 | * http://www.w3.org/Library/ |
| 8 | * |
| 9 | * See Copyright for the status of this software. |
| 10 | * |
Daniel Veillard | c5d6434 | 2001-06-24 12:13:24 +0000 | [diff] [blame] | 11 | * daniel@veillard.com |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12 | */ |
| 13 | |
Daniel Veillard | f3afa7d | 2001-06-09 13:52:58 +0000 | [diff] [blame] | 14 | #define NEED_SOCKETS |
Daniel Veillard | 34ce8be | 2002-03-18 19:37:11 +0000 | [diff] [blame] | 15 | #define IN_LIBXML |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 16 | #include "libxml.h" |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 17 | |
| 18 | #ifdef LIBXML_HTTP_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 19 | #include <string.h> |
| 20 | |
| 21 | #ifdef HAVE_STDLIB_H |
| 22 | #include <stdlib.h> |
| 23 | #endif |
| 24 | #ifdef HAVE_UNISTD_H |
| 25 | #include <unistd.h> |
| 26 | #endif |
Daniel Veillard | 75eb1ad | 2003-07-07 14:42:44 +0000 | [diff] [blame] | 27 | #ifdef HAVE_SYS_TYPES_H |
| 28 | #include <sys/types.h> |
| 29 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 30 | #ifdef HAVE_SYS_SOCKET_H |
| 31 | #include <sys/socket.h> |
| 32 | #endif |
| 33 | #ifdef HAVE_NETINET_IN_H |
| 34 | #include <netinet/in.h> |
| 35 | #endif |
| 36 | #ifdef HAVE_ARPA_INET_H |
| 37 | #include <arpa/inet.h> |
| 38 | #endif |
| 39 | #ifdef HAVE_NETDB_H |
| 40 | #include <netdb.h> |
| 41 | #endif |
Daniel Veillard | d85f4f4 | 2002-03-25 10:48:46 +0000 | [diff] [blame] | 42 | #ifdef HAVE_RESOLV_H |
Daniel Veillard | 9b731d7 | 2002-04-14 12:56:08 +0000 | [diff] [blame] | 43 | #ifdef HAVE_ARPA_NAMESER_H |
| 44 | #include <arpa/nameser.h> |
| 45 | #endif |
Daniel Veillard | d85f4f4 | 2002-03-25 10:48:46 +0000 | [diff] [blame] | 46 | #include <resolv.h> |
| 47 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 48 | #ifdef HAVE_FCNTL_H |
| 49 | #include <fcntl.h> |
| 50 | #endif |
| 51 | #ifdef HAVE_ERRNO_H |
| 52 | #include <errno.h> |
| 53 | #endif |
| 54 | #ifdef HAVE_SYS_TIME_H |
| 55 | #include <sys/time.h> |
| 56 | #endif |
| 57 | #ifdef HAVE_SYS_SELECT_H |
| 58 | #include <sys/select.h> |
| 59 | #endif |
| 60 | #ifdef HAVE_STRINGS_H |
| 61 | #include <strings.h> |
| 62 | #endif |
| 63 | #ifdef SUPPORT_IP6 |
| 64 | #include <resolv.h> |
| 65 | #endif |
Daniel Veillard | 9a2724d | 2005-12-15 11:12:26 +0000 | [diff] [blame] | 66 | #ifdef HAVE_ZLIB_H |
| 67 | #include <zlib.h> |
| 68 | #endif |
| 69 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 70 | |
| 71 | #ifdef VMS |
| 72 | #include <stropts> |
Daniel Veillard | c284c64 | 2005-03-31 10:24:24 +0000 | [diff] [blame] | 73 | #define XML_SOCKLEN_T unsigned int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 74 | #define SOCKET int |
| 75 | #endif |
| 76 | |
Daniel Veillard | 59d3ed8 | 2007-04-17 12:44:58 +0000 | [diff] [blame] | 77 | #if defined(__MINGW32__) || defined(_WIN32_WCE) |
Daniel Veillard | 1638a47 | 2003-08-14 01:23:25 +0000 | [diff] [blame] | 78 | #define _WINSOCKAPI_ |
| 79 | #include <wsockcompat.h> |
| 80 | #include <winsock2.h> |
Daniel Veillard | c284c64 | 2005-03-31 10:24:24 +0000 | [diff] [blame] | 81 | #undef XML_SOCKLEN_T |
| 82 | #define XML_SOCKLEN_T unsigned int |
Daniel Veillard | 1638a47 | 2003-08-14 01:23:25 +0000 | [diff] [blame] | 83 | #endif |
| 84 | |
| 85 | |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 86 | #include <libxml/globals.h> |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 87 | #include <libxml/xmlerror.h> |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 88 | #include <libxml/xmlmemory.h> |
| 89 | #include <libxml/parser.h> /* for xmlStr(n)casecmp() */ |
| 90 | #include <libxml/nanohttp.h> |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 91 | #include <libxml/globals.h> |
Daniel Veillard | 8efff67 | 2002-12-04 11:44:48 +0000 | [diff] [blame] | 92 | #include <libxml/uri.h> |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 93 | |
| 94 | /** |
| 95 | * A couple portability macros |
| 96 | */ |
| 97 | #ifndef _WINSOCKAPI_ |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 98 | #ifndef __BEOS__ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 99 | #define closesocket(s) close(s) |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 100 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 101 | #define SOCKET int |
| 102 | #endif |
| 103 | |
Daniel Veillard | 89f7f27 | 2003-09-29 13:29:09 +0000 | [diff] [blame] | 104 | #ifdef __BEOS__ |
| 105 | #ifndef PF_INET |
| 106 | #define PF_INET AF_INET |
| 107 | #endif |
| 108 | #endif |
| 109 | |
Daniel Veillard | c284c64 | 2005-03-31 10:24:24 +0000 | [diff] [blame] | 110 | #ifndef XML_SOCKLEN_T |
| 111 | #define XML_SOCKLEN_T unsigned int |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 112 | #endif |
| 113 | #ifndef SOCKET |
| 114 | #define SOCKET int |
| 115 | #endif |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 116 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 117 | #ifdef STANDALONE |
| 118 | #define DEBUG_HTTP |
| 119 | #define xmlStrncasecmp(a, b, n) strncasecmp((char *)a, (char *)b, n) |
| 120 | #define xmlStrcasecmpi(a, b) strcasecmp((char *)a, (char *)b) |
| 121 | #endif |
| 122 | |
| 123 | #define XML_NANO_HTTP_MAX_REDIR 10 |
| 124 | |
| 125 | #define XML_NANO_HTTP_CHUNK 4096 |
| 126 | |
| 127 | #define XML_NANO_HTTP_CLOSED 0 |
| 128 | #define XML_NANO_HTTP_WRITE 1 |
| 129 | #define XML_NANO_HTTP_READ 2 |
| 130 | #define XML_NANO_HTTP_NONE 4 |
| 131 | |
| 132 | typedef struct xmlNanoHTTPCtxt { |
| 133 | char *protocol; /* the protocol name */ |
| 134 | char *hostname; /* the host name */ |
| 135 | int port; /* the port */ |
| 136 | char *path; /* the path within the URL */ |
Daniel Veillard | 351f2d6 | 2005-04-13 02:55:12 +0000 | [diff] [blame] | 137 | char *query; /* the query string */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 138 | SOCKET fd; /* the file descriptor for the socket */ |
| 139 | int state; /* WRITE / READ / CLOSED */ |
| 140 | char *out; /* buffer sent (zero terminated) */ |
| 141 | char *outptr; /* index within the buffer sent */ |
| 142 | char *in; /* the receiving buffer */ |
| 143 | char *content; /* the start of the content */ |
| 144 | char *inptr; /* the next byte to read from network */ |
| 145 | char *inrptr; /* the next byte to give back to the client */ |
| 146 | int inlen; /* len of the input buffer */ |
| 147 | int last; /* return code for last operation */ |
| 148 | int returnValue; /* the protocol return value */ |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 149 | int ContentLength; /* specified content length from HTTP header */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 150 | char *contentType; /* the MIME type for the input */ |
| 151 | char *location; /* the new URL in case of redirect */ |
| 152 | char *authHeader; /* contents of {WWW,Proxy}-Authenticate header */ |
Daniel Veillard | 847332a | 2003-10-18 11:29:40 +0000 | [diff] [blame] | 153 | char *encoding; /* encoding extracted from the contentType */ |
Daniel Veillard | a840b69 | 2003-10-19 13:35:37 +0000 | [diff] [blame] | 154 | char *mimeType; /* Mime-Type extracted from the contentType */ |
Daniel Veillard | 9a2724d | 2005-12-15 11:12:26 +0000 | [diff] [blame] | 155 | #ifdef HAVE_ZLIB_H |
| 156 | z_stream *strm; /* Zlib stream object */ |
| 157 | int usesGzip; /* "Content-Encoding: gzip" was detected */ |
| 158 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 159 | } xmlNanoHTTPCtxt, *xmlNanoHTTPCtxtPtr; |
| 160 | |
| 161 | static int initialized = 0; |
| 162 | static char *proxy = NULL; /* the proxy name if any */ |
| 163 | static int proxyPort; /* the proxy port if any */ |
| 164 | static unsigned int timeout = 60;/* the select() timeout in seconds */ |
| 165 | |
Daniel Veillard | a235132 | 2004-06-27 12:08:10 +0000 | [diff] [blame] | 166 | static int xmlNanoHTTPFetchContent( void * ctx, char ** ptr, int * len ); |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 167 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 168 | /** |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 169 | * xmlHTTPErrMemory: |
| 170 | * @extra: extra informations |
| 171 | * |
| 172 | * Handle an out of memory condition |
| 173 | */ |
| 174 | static void |
| 175 | xmlHTTPErrMemory(const char *extra) |
| 176 | { |
| 177 | __xmlSimpleError(XML_FROM_HTTP, XML_ERR_NO_MEMORY, NULL, NULL, extra); |
| 178 | } |
| 179 | |
| 180 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 181 | * A portability function |
| 182 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 183 | static int socket_errno(void) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 184 | #ifdef _WINSOCKAPI_ |
| 185 | return(WSAGetLastError()); |
| 186 | #else |
| 187 | return(errno); |
| 188 | #endif |
| 189 | } |
| 190 | |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 191 | #ifdef SUPPORT_IP6 |
Daniel Veillard | 2db8c12 | 2003-07-08 12:16:59 +0000 | [diff] [blame] | 192 | static |
| 193 | int have_ipv6(void) { |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 194 | int s; |
| 195 | |
| 196 | s = socket (AF_INET6, SOCK_STREAM, 0); |
| 197 | if (s != -1) { |
| 198 | close (s); |
| 199 | return (1); |
| 200 | } |
| 201 | return (0); |
| 202 | } |
| 203 | #endif |
| 204 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 205 | /** |
| 206 | * xmlNanoHTTPInit: |
| 207 | * |
| 208 | * Initialize the HTTP protocol layer. |
| 209 | * Currently it just checks for proxy informations |
| 210 | */ |
| 211 | |
| 212 | void |
| 213 | xmlNanoHTTPInit(void) { |
| 214 | const char *env; |
| 215 | #ifdef _WINSOCKAPI_ |
| 216 | WSADATA wsaData; |
| 217 | #endif |
| 218 | |
| 219 | if (initialized) |
| 220 | return; |
| 221 | |
| 222 | #ifdef _WINSOCKAPI_ |
| 223 | if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) |
| 224 | return; |
| 225 | #endif |
| 226 | |
| 227 | if (proxy == NULL) { |
| 228 | proxyPort = 80; |
| 229 | env = getenv("no_proxy"); |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 230 | if (env && ((env[0] == '*') && (env[1] == 0))) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 231 | goto done; |
| 232 | env = getenv("http_proxy"); |
| 233 | if (env != NULL) { |
| 234 | xmlNanoHTTPScanProxy(env); |
| 235 | goto done; |
| 236 | } |
| 237 | env = getenv("HTTP_PROXY"); |
| 238 | if (env != NULL) { |
| 239 | xmlNanoHTTPScanProxy(env); |
| 240 | goto done; |
| 241 | } |
| 242 | } |
| 243 | done: |
| 244 | initialized = 1; |
| 245 | } |
| 246 | |
| 247 | /** |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 248 | * xmlNanoHTTPCleanup: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 249 | * |
| 250 | * Cleanup the HTTP protocol layer. |
| 251 | */ |
| 252 | |
| 253 | void |
| 254 | xmlNanoHTTPCleanup(void) { |
Daniel Veillard | 744acff | 2005-07-12 15:09:53 +0000 | [diff] [blame] | 255 | if (proxy != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 256 | xmlFree(proxy); |
Daniel Veillard | 744acff | 2005-07-12 15:09:53 +0000 | [diff] [blame] | 257 | proxy = NULL; |
| 258 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 259 | #ifdef _WINSOCKAPI_ |
| 260 | if (initialized) |
| 261 | WSACleanup(); |
| 262 | #endif |
| 263 | initialized = 0; |
| 264 | return; |
| 265 | } |
| 266 | |
| 267 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 268 | * xmlNanoHTTPScanURL: |
| 269 | * @ctxt: an HTTP context |
| 270 | * @URL: The URL used to initialize the context |
| 271 | * |
| 272 | * (Re)Initialize an HTTP context by parsing the URL and finding |
| 273 | * the protocol host port and path it indicates. |
| 274 | */ |
| 275 | |
| 276 | static void |
| 277 | xmlNanoHTTPScanURL(xmlNanoHTTPCtxtPtr ctxt, const char *URL) { |
William M. Brack | 015ccb2 | 2005-02-13 08:18:52 +0000 | [diff] [blame] | 278 | xmlURIPtr uri; |
| 279 | /* |
| 280 | * Clear any existing data from the context |
| 281 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 282 | if (ctxt->protocol != NULL) { |
| 283 | xmlFree(ctxt->protocol); |
| 284 | ctxt->protocol = NULL; |
| 285 | } |
| 286 | if (ctxt->hostname != NULL) { |
| 287 | xmlFree(ctxt->hostname); |
| 288 | ctxt->hostname = NULL; |
| 289 | } |
| 290 | if (ctxt->path != NULL) { |
| 291 | xmlFree(ctxt->path); |
| 292 | ctxt->path = NULL; |
| 293 | } |
Daniel Veillard | 351f2d6 | 2005-04-13 02:55:12 +0000 | [diff] [blame] | 294 | if (ctxt->query != NULL) { |
| 295 | xmlFree(ctxt->query); |
| 296 | ctxt->query = NULL; |
| 297 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 298 | if (URL == NULL) return; |
William M. Brack | 015ccb2 | 2005-02-13 08:18:52 +0000 | [diff] [blame] | 299 | |
Daniel Veillard | 336a8e1 | 2005-08-07 10:46:19 +0000 | [diff] [blame] | 300 | uri = xmlParseURIRaw(URL, 1); |
William M. Brack | 015ccb2 | 2005-02-13 08:18:52 +0000 | [diff] [blame] | 301 | if (uri == NULL) |
| 302 | return; |
| 303 | |
| 304 | if ((uri->scheme == NULL) || (uri->server == NULL)) { |
| 305 | xmlFreeURI(uri); |
| 306 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 307 | } |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 308 | |
William M. Brack | 015ccb2 | 2005-02-13 08:18:52 +0000 | [diff] [blame] | 309 | ctxt->protocol = xmlMemStrdup(uri->scheme); |
| 310 | ctxt->hostname = xmlMemStrdup(uri->server); |
| 311 | if (uri->path != NULL) |
| 312 | ctxt->path = xmlMemStrdup(uri->path); |
| 313 | else |
| 314 | ctxt->path = xmlMemStrdup("/"); |
Daniel Veillard | 351f2d6 | 2005-04-13 02:55:12 +0000 | [diff] [blame] | 315 | if (uri->query != NULL) |
| 316 | ctxt->query = xmlMemStrdup(uri->query); |
William M. Brack | 015ccb2 | 2005-02-13 08:18:52 +0000 | [diff] [blame] | 317 | if (uri->port != 0) |
| 318 | ctxt->port = uri->port; |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 319 | |
William M. Brack | 015ccb2 | 2005-02-13 08:18:52 +0000 | [diff] [blame] | 320 | xmlFreeURI(uri); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | /** |
| 324 | * xmlNanoHTTPScanProxy: |
| 325 | * @URL: The proxy URL used to initialize the proxy context |
| 326 | * |
| 327 | * (Re)Initialize the HTTP Proxy context by parsing the URL and finding |
| 328 | * the protocol host port it indicates. |
| 329 | * Should be like http://myproxy/ or http://myproxy:3128/ |
| 330 | * A NULL URL cleans up proxy informations. |
| 331 | */ |
| 332 | |
| 333 | void |
| 334 | xmlNanoHTTPScanProxy(const char *URL) { |
William M. Brack | 015ccb2 | 2005-02-13 08:18:52 +0000 | [diff] [blame] | 335 | xmlURIPtr uri; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 336 | |
| 337 | if (proxy != NULL) { |
| 338 | xmlFree(proxy); |
| 339 | proxy = NULL; |
| 340 | } |
William M. Brack | 015ccb2 | 2005-02-13 08:18:52 +0000 | [diff] [blame] | 341 | proxyPort = 0; |
| 342 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 343 | #ifdef DEBUG_HTTP |
| 344 | if (URL == NULL) |
| 345 | xmlGenericError(xmlGenericErrorContext, |
| 346 | "Removing HTTP proxy info\n"); |
| 347 | else |
| 348 | xmlGenericError(xmlGenericErrorContext, |
| 349 | "Using HTTP proxy %s\n", URL); |
| 350 | #endif |
| 351 | if (URL == NULL) return; |
William M. Brack | 015ccb2 | 2005-02-13 08:18:52 +0000 | [diff] [blame] | 352 | |
Daniel Veillard | 336a8e1 | 2005-08-07 10:46:19 +0000 | [diff] [blame] | 353 | uri = xmlParseURIRaw(URL, 1); |
William M. Brack | 015ccb2 | 2005-02-13 08:18:52 +0000 | [diff] [blame] | 354 | if ((uri == NULL) || (uri->scheme == NULL) || |
| 355 | (strcmp(uri->scheme, "http")) || (uri->server == NULL)) { |
| 356 | __xmlIOErr(XML_FROM_HTTP, XML_HTTP_URL_SYNTAX, "Syntax Error\n"); |
| 357 | if (uri != NULL) |
| 358 | xmlFreeURI(uri); |
| 359 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 360 | } |
William M. Brack | 015ccb2 | 2005-02-13 08:18:52 +0000 | [diff] [blame] | 361 | |
| 362 | proxy = xmlMemStrdup(uri->server); |
| 363 | if (uri->port != 0) |
| 364 | proxyPort = uri->port; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 365 | |
William M. Brack | 015ccb2 | 2005-02-13 08:18:52 +0000 | [diff] [blame] | 366 | xmlFreeURI(uri); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | /** |
| 370 | * xmlNanoHTTPNewCtxt: |
| 371 | * @URL: The URL used to initialize the context |
| 372 | * |
| 373 | * Allocate and initialize a new HTTP context. |
| 374 | * |
| 375 | * Returns an HTTP context or NULL in case of error. |
| 376 | */ |
| 377 | |
| 378 | static xmlNanoHTTPCtxtPtr |
| 379 | xmlNanoHTTPNewCtxt(const char *URL) { |
| 380 | xmlNanoHTTPCtxtPtr ret; |
| 381 | |
| 382 | ret = (xmlNanoHTTPCtxtPtr) xmlMalloc(sizeof(xmlNanoHTTPCtxt)); |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 383 | if (ret == NULL) { |
| 384 | xmlHTTPErrMemory("allocating context"); |
| 385 | return(NULL); |
| 386 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 387 | |
| 388 | memset(ret, 0, sizeof(xmlNanoHTTPCtxt)); |
| 389 | ret->port = 80; |
| 390 | ret->returnValue = 0; |
| 391 | ret->fd = -1; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 392 | ret->ContentLength = -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 393 | |
Daniel Veillard | cacbe5d | 2003-01-10 16:09:51 +0000 | [diff] [blame] | 394 | xmlNanoHTTPScanURL(ret, URL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 395 | |
| 396 | return(ret); |
| 397 | } |
| 398 | |
| 399 | /** |
| 400 | * xmlNanoHTTPFreeCtxt: |
| 401 | * @ctxt: an HTTP context |
| 402 | * |
| 403 | * Frees the context after closing the connection. |
| 404 | */ |
| 405 | |
| 406 | static void |
| 407 | xmlNanoHTTPFreeCtxt(xmlNanoHTTPCtxtPtr ctxt) { |
| 408 | if (ctxt == NULL) return; |
| 409 | if (ctxt->hostname != NULL) xmlFree(ctxt->hostname); |
| 410 | if (ctxt->protocol != NULL) xmlFree(ctxt->protocol); |
| 411 | if (ctxt->path != NULL) xmlFree(ctxt->path); |
Daniel Veillard | 351f2d6 | 2005-04-13 02:55:12 +0000 | [diff] [blame] | 412 | if (ctxt->query != NULL) xmlFree(ctxt->query); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 413 | if (ctxt->out != NULL) xmlFree(ctxt->out); |
| 414 | if (ctxt->in != NULL) xmlFree(ctxt->in); |
| 415 | if (ctxt->contentType != NULL) xmlFree(ctxt->contentType); |
Daniel Veillard | 847332a | 2003-10-18 11:29:40 +0000 | [diff] [blame] | 416 | if (ctxt->encoding != NULL) xmlFree(ctxt->encoding); |
Daniel Veillard | a840b69 | 2003-10-19 13:35:37 +0000 | [diff] [blame] | 417 | if (ctxt->mimeType != NULL) xmlFree(ctxt->mimeType); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 418 | if (ctxt->location != NULL) xmlFree(ctxt->location); |
| 419 | if (ctxt->authHeader != NULL) xmlFree(ctxt->authHeader); |
Daniel Veillard | 9a2724d | 2005-12-15 11:12:26 +0000 | [diff] [blame] | 420 | #ifdef HAVE_ZLIB_H |
| 421 | if (ctxt->strm != NULL) { |
| 422 | inflateEnd(ctxt->strm); |
| 423 | xmlFree(ctxt->strm); |
| 424 | } |
| 425 | #endif |
| 426 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 427 | ctxt->state = XML_NANO_HTTP_NONE; |
| 428 | if (ctxt->fd >= 0) closesocket(ctxt->fd); |
| 429 | ctxt->fd = -1; |
| 430 | xmlFree(ctxt); |
| 431 | } |
| 432 | |
| 433 | /** |
| 434 | * xmlNanoHTTPSend: |
| 435 | * @ctxt: an HTTP context |
| 436 | * |
| 437 | * Send the input needed to initiate the processing on the server side |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 438 | * Returns number of bytes sent or -1 on error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 439 | */ |
| 440 | |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 441 | static int |
| 442 | xmlNanoHTTPSend(xmlNanoHTTPCtxtPtr ctxt, const char * xmt_ptr, int outlen) { |
| 443 | |
| 444 | int total_sent = 0; |
| 445 | |
| 446 | if ( (ctxt->state & XML_NANO_HTTP_WRITE) && (xmt_ptr != NULL ) ) { |
| 447 | while (total_sent < outlen) { |
| 448 | int nsent = send(ctxt->fd, xmt_ptr + total_sent, |
| 449 | outlen - total_sent, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 450 | if (nsent>0) |
| 451 | total_sent += nsent; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 452 | else if ( ( nsent == -1 ) && |
Daniel Veillard | ba6db03 | 2001-07-31 16:25:45 +0000 | [diff] [blame] | 453 | #if defined(EAGAIN) && EAGAIN != EWOULDBLOCK |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 454 | ( socket_errno( ) != EAGAIN ) && |
Daniel Veillard | ba6db03 | 2001-07-31 16:25:45 +0000 | [diff] [blame] | 455 | #endif |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 456 | ( socket_errno( ) != EWOULDBLOCK ) ) { |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 457 | __xmlIOErr(XML_FROM_HTTP, 0, "send failed\n"); |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 458 | if ( total_sent == 0 ) |
| 459 | total_sent = -1; |
| 460 | break; |
| 461 | } |
| 462 | else { |
| 463 | /* |
| 464 | ** No data sent |
| 465 | ** Since non-blocking sockets are used, wait for |
| 466 | ** socket to be writable or default timeout prior |
| 467 | ** to retrying. |
| 468 | */ |
| 469 | |
| 470 | struct timeval tv; |
| 471 | fd_set wfd; |
| 472 | |
| 473 | tv.tv_sec = timeout; |
| 474 | tv.tv_usec = 0; |
| 475 | FD_ZERO( &wfd ); |
Daniel Veillard | 9e2110b | 2005-08-08 20:33:54 +0000 | [diff] [blame] | 476 | #ifdef _MSC_VER |
| 477 | #pragma warning(push) |
| 478 | #pragma warning(disable: 4018) |
| 479 | #endif |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 480 | FD_SET( ctxt->fd, &wfd ); |
Daniel Veillard | 9e2110b | 2005-08-08 20:33:54 +0000 | [diff] [blame] | 481 | #ifdef _MSC_VER |
| 482 | #pragma warning(pop) |
| 483 | #endif |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 484 | (void)select( ctxt->fd + 1, NULL, &wfd, NULL, &tv ); |
| 485 | } |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 486 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 487 | } |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 488 | |
| 489 | return total_sent; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | /** |
| 493 | * xmlNanoHTTPRecv: |
| 494 | * @ctxt: an HTTP context |
| 495 | * |
| 496 | * Read information coming from the HTTP connection. |
| 497 | * This is a blocking call (but it blocks in select(), not read()). |
| 498 | * |
| 499 | * Returns the number of byte read or -1 in case of error. |
| 500 | */ |
| 501 | |
| 502 | static int |
| 503 | xmlNanoHTTPRecv(xmlNanoHTTPCtxtPtr ctxt) { |
| 504 | fd_set rfd; |
| 505 | struct timeval tv; |
| 506 | |
| 507 | |
| 508 | while (ctxt->state & XML_NANO_HTTP_READ) { |
| 509 | if (ctxt->in == NULL) { |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 510 | ctxt->in = (char *) xmlMallocAtomic(65000 * sizeof(char)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 511 | if (ctxt->in == NULL) { |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 512 | xmlHTTPErrMemory("allocating input"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 513 | ctxt->last = -1; |
| 514 | return(-1); |
| 515 | } |
| 516 | ctxt->inlen = 65000; |
| 517 | ctxt->inptr = ctxt->content = ctxt->inrptr = ctxt->in; |
| 518 | } |
| 519 | if (ctxt->inrptr > ctxt->in + XML_NANO_HTTP_CHUNK) { |
| 520 | int delta = ctxt->inrptr - ctxt->in; |
| 521 | int len = ctxt->inptr - ctxt->inrptr; |
| 522 | |
| 523 | memmove(ctxt->in, ctxt->inrptr, len); |
| 524 | ctxt->inrptr -= delta; |
| 525 | ctxt->content -= delta; |
| 526 | ctxt->inptr -= delta; |
| 527 | } |
| 528 | if ((ctxt->in + ctxt->inlen) < (ctxt->inptr + XML_NANO_HTTP_CHUNK)) { |
| 529 | int d_inptr = ctxt->inptr - ctxt->in; |
| 530 | int d_content = ctxt->content - ctxt->in; |
| 531 | int d_inrptr = ctxt->inrptr - ctxt->in; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 532 | char * tmp_ptr = ctxt->in; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 533 | |
| 534 | ctxt->inlen *= 2; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 535 | ctxt->in = (char *) xmlRealloc(tmp_ptr, ctxt->inlen); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 536 | if (ctxt->in == NULL) { |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 537 | xmlHTTPErrMemory("allocating input buffer"); |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 538 | xmlFree( tmp_ptr ); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 539 | ctxt->last = -1; |
| 540 | return(-1); |
| 541 | } |
| 542 | ctxt->inptr = ctxt->in + d_inptr; |
| 543 | ctxt->content = ctxt->in + d_content; |
| 544 | ctxt->inrptr = ctxt->in + d_inrptr; |
| 545 | } |
| 546 | ctxt->last = recv(ctxt->fd, ctxt->inptr, XML_NANO_HTTP_CHUNK, 0); |
| 547 | if (ctxt->last > 0) { |
| 548 | ctxt->inptr += ctxt->last; |
| 549 | return(ctxt->last); |
| 550 | } |
| 551 | if (ctxt->last == 0) { |
| 552 | return(0); |
| 553 | } |
| 554 | if (ctxt->last == -1) { |
| 555 | switch (socket_errno()) { |
| 556 | case EINPROGRESS: |
| 557 | case EWOULDBLOCK: |
| 558 | #if defined(EAGAIN) && EAGAIN != EWOULDBLOCK |
| 559 | case EAGAIN: |
| 560 | #endif |
| 561 | break; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 562 | |
| 563 | case ECONNRESET: |
| 564 | case ESHUTDOWN: |
| 565 | return ( 0 ); |
| 566 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 567 | default: |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 568 | __xmlIOErr(XML_FROM_HTTP, 0, "recv failed\n"); |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 569 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 570 | } |
| 571 | } |
| 572 | |
| 573 | tv.tv_sec = timeout; |
| 574 | tv.tv_usec = 0; |
| 575 | FD_ZERO(&rfd); |
Daniel Veillard | 9e2110b | 2005-08-08 20:33:54 +0000 | [diff] [blame] | 576 | #ifdef _MSC_VER |
| 577 | #pragma warning(push) |
| 578 | #pragma warning(disable: 4018) |
| 579 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 580 | FD_SET(ctxt->fd, &rfd); |
Daniel Veillard | 9e2110b | 2005-08-08 20:33:54 +0000 | [diff] [blame] | 581 | #ifdef _MSC_VER |
| 582 | #pragma warning(pop) |
| 583 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 584 | |
Daniel Veillard | 50f3437 | 2001-08-03 12:06:36 +0000 | [diff] [blame] | 585 | if ( (select(ctxt->fd+1, &rfd, NULL, NULL, &tv)<1) |
| 586 | #if defined(EINTR) |
| 587 | && (errno != EINTR) |
| 588 | #endif |
| 589 | ) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 590 | return(0); |
| 591 | } |
| 592 | return(0); |
| 593 | } |
| 594 | |
| 595 | /** |
| 596 | * xmlNanoHTTPReadLine: |
| 597 | * @ctxt: an HTTP context |
| 598 | * |
| 599 | * Read one line in the HTTP server output, usually for extracting |
| 600 | * the HTTP protocol informations from the answer header. |
| 601 | * |
| 602 | * Returns a newly allocated string with a copy of the line, or NULL |
| 603 | * which indicate the end of the input. |
| 604 | */ |
| 605 | |
| 606 | static char * |
| 607 | xmlNanoHTTPReadLine(xmlNanoHTTPCtxtPtr ctxt) { |
| 608 | char buf[4096]; |
| 609 | char *bp = buf; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 610 | int rc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 611 | |
| 612 | while (bp - buf < 4095) { |
| 613 | if (ctxt->inrptr == ctxt->inptr) { |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 614 | if ( (rc = xmlNanoHTTPRecv(ctxt)) == 0) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 615 | if (bp == buf) |
| 616 | return(NULL); |
| 617 | else |
| 618 | *bp = 0; |
| 619 | return(xmlMemStrdup(buf)); |
| 620 | } |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 621 | else if ( rc == -1 ) { |
| 622 | return ( NULL ); |
| 623 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 624 | } |
| 625 | *bp = *ctxt->inrptr++; |
| 626 | if (*bp == '\n') { |
| 627 | *bp = 0; |
| 628 | return(xmlMemStrdup(buf)); |
| 629 | } |
| 630 | if (*bp != '\r') |
| 631 | bp++; |
| 632 | } |
| 633 | buf[4095] = 0; |
| 634 | return(xmlMemStrdup(buf)); |
| 635 | } |
| 636 | |
| 637 | |
| 638 | /** |
| 639 | * xmlNanoHTTPScanAnswer: |
| 640 | * @ctxt: an HTTP context |
| 641 | * @line: an HTTP header line |
| 642 | * |
| 643 | * Try to extract useful informations from the server answer. |
| 644 | * We currently parse and process: |
| 645 | * - The HTTP revision/ return code |
Daniel Veillard | a840b69 | 2003-10-19 13:35:37 +0000 | [diff] [blame] | 646 | * - The Content-Type, Mime-Type and charset used |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 647 | * - The Location for redirect processing. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 648 | * |
| 649 | * Returns -1 in case of failure, the file descriptor number otherwise |
| 650 | */ |
| 651 | |
| 652 | static void |
| 653 | xmlNanoHTTPScanAnswer(xmlNanoHTTPCtxtPtr ctxt, const char *line) { |
| 654 | const char *cur = line; |
| 655 | |
| 656 | if (line == NULL) return; |
| 657 | |
| 658 | if (!strncmp(line, "HTTP/", 5)) { |
| 659 | int version = 0; |
| 660 | int ret = 0; |
| 661 | |
| 662 | cur += 5; |
| 663 | while ((*cur >= '0') && (*cur <= '9')) { |
| 664 | version *= 10; |
| 665 | version += *cur - '0'; |
| 666 | cur++; |
| 667 | } |
| 668 | if (*cur == '.') { |
| 669 | cur++; |
| 670 | if ((*cur >= '0') && (*cur <= '9')) { |
| 671 | version *= 10; |
| 672 | version += *cur - '0'; |
| 673 | cur++; |
| 674 | } |
| 675 | while ((*cur >= '0') && (*cur <= '9')) |
| 676 | cur++; |
| 677 | } else |
| 678 | version *= 10; |
| 679 | if ((*cur != ' ') && (*cur != '\t')) return; |
| 680 | while ((*cur == ' ') || (*cur == '\t')) cur++; |
| 681 | if ((*cur < '0') || (*cur > '9')) return; |
| 682 | while ((*cur >= '0') && (*cur <= '9')) { |
| 683 | ret *= 10; |
| 684 | ret += *cur - '0'; |
| 685 | cur++; |
| 686 | } |
| 687 | if ((*cur != 0) && (*cur != ' ') && (*cur != '\t')) return; |
| 688 | ctxt->returnValue = ret; |
| 689 | } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Content-Type:", 13)) { |
Daniel Veillard | a840b69 | 2003-10-19 13:35:37 +0000 | [diff] [blame] | 690 | const xmlChar *charset, *last, *mime; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 691 | cur += 13; |
| 692 | while ((*cur == ' ') || (*cur == '\t')) cur++; |
| 693 | if (ctxt->contentType != NULL) |
| 694 | xmlFree(ctxt->contentType); |
| 695 | ctxt->contentType = xmlMemStrdup(cur); |
Daniel Veillard | a840b69 | 2003-10-19 13:35:37 +0000 | [diff] [blame] | 696 | mime = (const xmlChar *) cur; |
| 697 | last = mime; |
| 698 | while ((*last != 0) && (*last != ' ') && (*last != '\t') && |
| 699 | (*last != ';') && (*last != ',')) |
| 700 | last++; |
| 701 | if (ctxt->mimeType != NULL) |
| 702 | xmlFree(ctxt->mimeType); |
| 703 | ctxt->mimeType = (char *) xmlStrndup(mime, last - mime); |
| 704 | charset = xmlStrstr(BAD_CAST ctxt->contentType, BAD_CAST "charset="); |
| 705 | if (charset != NULL) { |
| 706 | charset += 8; |
| 707 | last = charset; |
| 708 | while ((*last != 0) && (*last != ' ') && (*last != '\t') && |
| 709 | (*last != ';') && (*last != ',')) |
| 710 | last++; |
| 711 | if (ctxt->encoding != NULL) |
| 712 | xmlFree(ctxt->encoding); |
| 713 | ctxt->encoding = (char *) xmlStrndup(charset, last - charset); |
| 714 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 715 | } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"ContentType:", 12)) { |
Daniel Veillard | a840b69 | 2003-10-19 13:35:37 +0000 | [diff] [blame] | 716 | const xmlChar *charset, *last, *mime; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 717 | cur += 12; |
| 718 | if (ctxt->contentType != NULL) return; |
| 719 | while ((*cur == ' ') || (*cur == '\t')) cur++; |
| 720 | ctxt->contentType = xmlMemStrdup(cur); |
Daniel Veillard | a840b69 | 2003-10-19 13:35:37 +0000 | [diff] [blame] | 721 | mime = (const xmlChar *) cur; |
| 722 | last = mime; |
| 723 | while ((*last != 0) && (*last != ' ') && (*last != '\t') && |
| 724 | (*last != ';') && (*last != ',')) |
| 725 | last++; |
| 726 | if (ctxt->mimeType != NULL) |
| 727 | xmlFree(ctxt->mimeType); |
| 728 | ctxt->mimeType = (char *) xmlStrndup(mime, last - mime); |
| 729 | charset = xmlStrstr(BAD_CAST ctxt->contentType, BAD_CAST "charset="); |
| 730 | if (charset != NULL) { |
| 731 | charset += 8; |
| 732 | last = charset; |
| 733 | while ((*last != 0) && (*last != ' ') && (*last != '\t') && |
| 734 | (*last != ';') && (*last != ',')) |
| 735 | last++; |
| 736 | if (ctxt->encoding != NULL) |
| 737 | xmlFree(ctxt->encoding); |
| 738 | ctxt->encoding = (char *) xmlStrndup(charset, last - charset); |
| 739 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 740 | } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Location:", 9)) { |
| 741 | cur += 9; |
| 742 | while ((*cur == ' ') || (*cur == '\t')) cur++; |
| 743 | if (ctxt->location != NULL) |
| 744 | xmlFree(ctxt->location); |
William M. Brack | 7e29c0a | 2004-04-02 09:07:22 +0000 | [diff] [blame] | 745 | if (*cur == '/') { |
| 746 | xmlChar *tmp_http = xmlStrdup(BAD_CAST "http://"); |
| 747 | xmlChar *tmp_loc = |
| 748 | xmlStrcat(tmp_http, (const xmlChar *) ctxt->hostname); |
| 749 | ctxt->location = |
| 750 | (char *) xmlStrcat (tmp_loc, (const xmlChar *) cur); |
| 751 | } else { |
| 752 | ctxt->location = xmlMemStrdup(cur); |
| 753 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 754 | } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"WWW-Authenticate:", 17)) { |
| 755 | cur += 17; |
| 756 | while ((*cur == ' ') || (*cur == '\t')) cur++; |
| 757 | if (ctxt->authHeader != NULL) |
| 758 | xmlFree(ctxt->authHeader); |
| 759 | ctxt->authHeader = xmlMemStrdup(cur); |
| 760 | } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Proxy-Authenticate:", 19)) { |
| 761 | cur += 19; |
| 762 | while ((*cur == ' ') || (*cur == '\t')) cur++; |
| 763 | if (ctxt->authHeader != NULL) |
| 764 | xmlFree(ctxt->authHeader); |
| 765 | ctxt->authHeader = xmlMemStrdup(cur); |
Daniel Veillard | 9a2724d | 2005-12-15 11:12:26 +0000 | [diff] [blame] | 766 | #ifdef HAVE_ZLIB_H |
| 767 | } else if ( !xmlStrncasecmp( BAD_CAST line, BAD_CAST"Content-Encoding:", 17) ) { |
| 768 | cur += 17; |
| 769 | while ((*cur == ' ') || (*cur == '\t')) cur++; |
| 770 | if ( !xmlStrncasecmp( BAD_CAST cur, BAD_CAST"gzip", 4) ) { |
| 771 | ctxt->usesGzip = 1; |
| 772 | |
| 773 | ctxt->strm = xmlMalloc(sizeof(z_stream)); |
| 774 | |
| 775 | if (ctxt->strm != NULL) { |
| 776 | ctxt->strm->zalloc = Z_NULL; |
| 777 | ctxt->strm->zfree = Z_NULL; |
| 778 | ctxt->strm->opaque = Z_NULL; |
| 779 | ctxt->strm->avail_in = 0; |
| 780 | ctxt->strm->next_in = Z_NULL; |
| 781 | |
| 782 | inflateInit2( ctxt->strm, 31 ); |
| 783 | } |
| 784 | } |
| 785 | #endif |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 786 | } else if ( !xmlStrncasecmp( BAD_CAST line, BAD_CAST"Content-Length:", 15) ) { |
| 787 | cur += 15; |
| 788 | ctxt->ContentLength = strtol( cur, NULL, 10 ); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 789 | } |
| 790 | } |
| 791 | |
| 792 | /** |
| 793 | * xmlNanoHTTPConnectAttempt: |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 794 | * @addr: a socket address structure |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 795 | * |
| 796 | * Attempt a connection to the given IP:port endpoint. It forces |
| 797 | * non-blocking semantic on the socket, and allow 60 seconds for |
| 798 | * the host to answer. |
| 799 | * |
| 800 | * Returns -1 in case of failure, the file descriptor number otherwise |
| 801 | */ |
| 802 | |
| 803 | static int |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 804 | xmlNanoHTTPConnectAttempt(struct sockaddr *addr) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 805 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 806 | fd_set wfd; |
Daniel Veillard | 5bb9ccd | 2004-02-09 12:39:02 +0000 | [diff] [blame] | 807 | #ifdef _WINSOCKAPI_ |
| 808 | fd_set xfd; |
| 809 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 810 | struct timeval tv; |
| 811 | int status; |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 812 | int addrlen; |
| 813 | SOCKET s; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 814 | |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 815 | #ifdef SUPPORT_IP6 |
| 816 | if (addr->sa_family == AF_INET6) { |
| 817 | s = socket (PF_INET6, SOCK_STREAM, IPPROTO_TCP); |
| 818 | addrlen = sizeof (struct sockaddr_in6); |
| 819 | } |
| 820 | else |
| 821 | #endif |
| 822 | { |
| 823 | s = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 824 | addrlen = sizeof (struct sockaddr_in); |
| 825 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 826 | if (s==-1) { |
| 827 | #ifdef DEBUG_HTTP |
| 828 | perror("socket"); |
| 829 | #endif |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 830 | __xmlIOErr(XML_FROM_HTTP, 0, "socket failed\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 831 | return(-1); |
| 832 | } |
| 833 | |
| 834 | #ifdef _WINSOCKAPI_ |
| 835 | { |
| 836 | u_long one = 1; |
| 837 | |
| 838 | status = ioctlsocket(s, FIONBIO, &one) == SOCKET_ERROR ? -1 : 0; |
| 839 | } |
| 840 | #else /* _WINSOCKAPI_ */ |
| 841 | #if defined(VMS) |
| 842 | { |
| 843 | int enable = 1; |
| 844 | status = ioctl(s, FIONBIO, &enable); |
| 845 | } |
| 846 | #else /* VMS */ |
Daniel Veillard | 254b126 | 2003-11-01 17:04:58 +0000 | [diff] [blame] | 847 | #if defined(__BEOS__) |
| 848 | { |
| 849 | bool noblock = true; |
| 850 | status = setsockopt(s, SOL_SOCKET, SO_NONBLOCK, &noblock, sizeof(noblock)); |
| 851 | } |
| 852 | #else /* __BEOS__ */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 853 | if ((status = fcntl(s, F_GETFL, 0)) != -1) { |
| 854 | #ifdef O_NONBLOCK |
| 855 | status |= O_NONBLOCK; |
| 856 | #else /* O_NONBLOCK */ |
| 857 | #ifdef F_NDELAY |
| 858 | status |= F_NDELAY; |
| 859 | #endif /* F_NDELAY */ |
| 860 | #endif /* !O_NONBLOCK */ |
| 861 | status = fcntl(s, F_SETFL, status); |
| 862 | } |
| 863 | if (status < 0) { |
| 864 | #ifdef DEBUG_HTTP |
| 865 | perror("nonblocking"); |
| 866 | #endif |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 867 | __xmlIOErr(XML_FROM_HTTP, 0, "error setting non-blocking IO\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 868 | closesocket(s); |
| 869 | return(-1); |
| 870 | } |
Daniel Veillard | 254b126 | 2003-11-01 17:04:58 +0000 | [diff] [blame] | 871 | #endif /* !__BEOS__ */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 872 | #endif /* !VMS */ |
| 873 | #endif /* !_WINSOCKAPI_ */ |
| 874 | |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 875 | if (connect (s, addr, addrlen) == -1) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 876 | switch (socket_errno()) { |
| 877 | case EINPROGRESS: |
| 878 | case EWOULDBLOCK: |
| 879 | break; |
| 880 | default: |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 881 | __xmlIOErr(XML_FROM_HTTP, 0, "error connecting to HTTP server"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 882 | closesocket(s); |
| 883 | return(-1); |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | tv.tv_sec = timeout; |
| 888 | tv.tv_usec = 0; |
Daniel Veillard | 9e2110b | 2005-08-08 20:33:54 +0000 | [diff] [blame] | 889 | |
| 890 | #ifdef _MSC_VER |
| 891 | #pragma warning(push) |
| 892 | #pragma warning(disable: 4018) |
| 893 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 894 | FD_ZERO(&wfd); |
| 895 | FD_SET(s, &wfd); |
Daniel Veillard | 5bb9ccd | 2004-02-09 12:39:02 +0000 | [diff] [blame] | 896 | |
| 897 | #ifdef _WINSOCKAPI_ |
| 898 | FD_ZERO(&xfd); |
| 899 | FD_SET(s, &xfd); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 900 | |
Daniel Veillard | 5bb9ccd | 2004-02-09 12:39:02 +0000 | [diff] [blame] | 901 | switch(select(s+1, NULL, &wfd, &xfd, &tv)) |
| 902 | #else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 903 | switch(select(s+1, NULL, &wfd, NULL, &tv)) |
Daniel Veillard | 5bb9ccd | 2004-02-09 12:39:02 +0000 | [diff] [blame] | 904 | #endif |
Daniel Veillard | 9e2110b | 2005-08-08 20:33:54 +0000 | [diff] [blame] | 905 | #ifdef _MSC_VER |
| 906 | #pragma warning(pop) |
| 907 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 908 | { |
| 909 | case 0: |
| 910 | /* Time out */ |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 911 | __xmlIOErr(XML_FROM_HTTP, 0, "Connect attempt timed out"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 912 | closesocket(s); |
| 913 | return(-1); |
| 914 | case -1: |
| 915 | /* Ermm.. ?? */ |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 916 | __xmlIOErr(XML_FROM_HTTP, 0, "Connect failed"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 917 | closesocket(s); |
| 918 | return(-1); |
| 919 | } |
| 920 | |
Daniel Veillard | 5bb9ccd | 2004-02-09 12:39:02 +0000 | [diff] [blame] | 921 | if ( FD_ISSET(s, &wfd) |
| 922 | #ifdef _WINSOCKAPI_ |
| 923 | || FD_ISSET(s, &xfd) |
| 924 | #endif |
| 925 | ) { |
Daniel Veillard | c284c64 | 2005-03-31 10:24:24 +0000 | [diff] [blame] | 926 | XML_SOCKLEN_T len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 927 | len = sizeof(status); |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 928 | #ifdef SO_ERROR |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 929 | if (getsockopt(s, SOL_SOCKET, SO_ERROR, (char*)&status, &len) < 0 ) { |
| 930 | /* Solaris error code */ |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 931 | __xmlIOErr(XML_FROM_HTTP, 0, "getsockopt failed\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 932 | return (-1); |
| 933 | } |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 934 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 935 | if ( status ) { |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 936 | __xmlIOErr(XML_FROM_HTTP, 0, "Error connecting to remote host"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 937 | closesocket(s); |
| 938 | errno = status; |
| 939 | return (-1); |
| 940 | } |
| 941 | } else { |
| 942 | /* pbm */ |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 943 | __xmlIOErr(XML_FROM_HTTP, 0, "select failed\n"); |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 944 | closesocket(s); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 945 | return (-1); |
| 946 | } |
| 947 | |
| 948 | return(s); |
| 949 | } |
| 950 | |
| 951 | /** |
| 952 | * xmlNanoHTTPConnectHost: |
| 953 | * @host: the host name |
| 954 | * @port: the port number |
| 955 | * |
| 956 | * Attempt a connection to the given host:port endpoint. It tries |
| 957 | * the multiple IP provided by the DNS if available. |
| 958 | * |
| 959 | * Returns -1 in case of failure, the file descriptor number otherwise |
| 960 | */ |
| 961 | |
| 962 | static int |
| 963 | xmlNanoHTTPConnectHost(const char *host, int port) |
| 964 | { |
| 965 | struct hostent *h; |
Daniel Veillard | 2db8c12 | 2003-07-08 12:16:59 +0000 | [diff] [blame] | 966 | struct sockaddr *addr = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 967 | struct in_addr ia; |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 968 | struct sockaddr_in sockin; |
Daniel Veillard | 5c39654 | 2002-03-15 07:57:50 +0000 | [diff] [blame] | 969 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 970 | #ifdef SUPPORT_IP6 |
| 971 | struct in6_addr ia6; |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 972 | struct sockaddr_in6 sockin6; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 973 | #endif |
| 974 | int i; |
| 975 | int s; |
Daniel Veillard | 5c39654 | 2002-03-15 07:57:50 +0000 | [diff] [blame] | 976 | |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 977 | memset (&sockin, 0, sizeof(sockin)); |
| 978 | #ifdef SUPPORT_IP6 |
| 979 | memset (&sockin6, 0, sizeof(sockin6)); |
Rob Richards | cb418de | 2005-10-13 23:12:42 +0000 | [diff] [blame] | 980 | #endif |
| 981 | |
| 982 | #if !defined(HAVE_GETADDRINFO) && defined(SUPPORT_IP6) && defined(RES_USE_INET6) |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 983 | if (have_ipv6 ()) |
Daniel Veillard | 560c2a4 | 2003-07-06 21:13:49 +0000 | [diff] [blame] | 984 | { |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 985 | if (!(_res.options & RES_INIT)) |
| 986 | res_init(); |
| 987 | _res.options |= RES_USE_INET6; |
| 988 | } |
Rob Richards | cb418de | 2005-10-13 23:12:42 +0000 | [diff] [blame] | 989 | #endif |
| 990 | |
| 991 | #if defined(HAVE_GETADDRINFO) && defined(SUPPORT_IP6) && !defined(_WIN32) |
| 992 | if (have_ipv6 ()) |
| 993 | #endif |
| 994 | #if defined(HAVE_GETADDRINFO) && (defined(SUPPORT_IP6) || defined(_WIN32)) |
Daniel Veillard | 560c2a4 | 2003-07-06 21:13:49 +0000 | [diff] [blame] | 995 | { |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 996 | int status; |
| 997 | struct addrinfo hints, *res, *result; |
| 998 | |
| 999 | result = NULL; |
| 1000 | memset (&hints, 0,sizeof(hints)); |
| 1001 | hints.ai_socktype = SOCK_STREAM; |
| 1002 | |
| 1003 | status = getaddrinfo (host, NULL, &hints, &result); |
| 1004 | if (status) { |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 1005 | __xmlIOErr(XML_FROM_HTTP, 0, "getaddrinfo failed\n"); |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1006 | return (-1); |
| 1007 | } |
| 1008 | |
| 1009 | for (res = result; res; res = res->ai_next) { |
Rob Richards | cb418de | 2005-10-13 23:12:42 +0000 | [diff] [blame] | 1010 | if (res->ai_family == AF_INET) { |
| 1011 | if (res->ai_addrlen > sizeof(sockin)) { |
| 1012 | __xmlIOErr(XML_FROM_HTTP, 0, "address size mismatch\n"); |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1013 | freeaddrinfo (result); |
Rob Richards | cb418de | 2005-10-13 23:12:42 +0000 | [diff] [blame] | 1014 | return (-1); |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1015 | } |
Rob Richards | cb418de | 2005-10-13 23:12:42 +0000 | [diff] [blame] | 1016 | memcpy (&sockin, res->ai_addr, res->ai_addrlen); |
| 1017 | sockin.sin_port = htons (port); |
| 1018 | addr = (struct sockaddr *)&sockin; |
| 1019 | #ifdef SUPPORT_IP6 |
| 1020 | } else if (have_ipv6 () && (res->ai_family == AF_INET6)) { |
| 1021 | if (res->ai_addrlen > sizeof(sockin6)) { |
| 1022 | __xmlIOErr(XML_FROM_HTTP, 0, "address size mismatch\n"); |
| 1023 | freeaddrinfo (result); |
| 1024 | return (-1); |
| 1025 | } |
| 1026 | memcpy (&sockin6, res->ai_addr, res->ai_addrlen); |
| 1027 | sockin6.sin6_port = htons (port); |
| 1028 | addr = (struct sockaddr *)&sockin6; |
| 1029 | #endif |
| 1030 | } else |
| 1031 | continue; /* for */ |
| 1032 | |
| 1033 | s = xmlNanoHTTPConnectAttempt (addr); |
| 1034 | if (s != -1) { |
| 1035 | freeaddrinfo (result); |
| 1036 | return (s); |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1037 | } |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1038 | } |
Rob Richards | cb418de | 2005-10-13 23:12:42 +0000 | [diff] [blame] | 1039 | |
Daniel Veillard | 3dc93a4 | 2003-07-10 14:04:33 +0000 | [diff] [blame] | 1040 | if (result) |
| 1041 | freeaddrinfo (result); |
Rob Richards | cb418de | 2005-10-13 23:12:42 +0000 | [diff] [blame] | 1042 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1043 | #endif |
Rob Richards | cb418de | 2005-10-13 23:12:42 +0000 | [diff] [blame] | 1044 | #if defined(HAVE_GETADDRINFO) && defined(SUPPORT_IP6) && !defined(_WIN32) |
| 1045 | else |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1046 | #endif |
Rob Richards | cb418de | 2005-10-13 23:12:42 +0000 | [diff] [blame] | 1047 | #if !defined(HAVE_GETADDRINFO) || !defined(_WIN32) |
| 1048 | { |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1049 | h = gethostbyname (host); |
| 1050 | if (h == NULL) { |
Daniel Veillard | 56b2db7 | 2002-03-25 16:35:28 +0000 | [diff] [blame] | 1051 | |
| 1052 | /* |
| 1053 | * Okay, I got fed up by the non-portability of this error message |
| 1054 | * extraction code. it work on Linux, if it work on your platform |
| 1055 | * and one want to enable it, send me the defined(foobar) needed |
| 1056 | */ |
| 1057 | #if defined(HAVE_NETDB_H) && defined(HOST_NOT_FOUND) && defined(linux) |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1058 | const char *h_err_txt = ""; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1059 | |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1060 | switch (h_errno) { |
| 1061 | case HOST_NOT_FOUND: |
| 1062 | h_err_txt = "Authoritive host not found"; |
| 1063 | break; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1064 | |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1065 | case TRY_AGAIN: |
| 1066 | h_err_txt = |
| 1067 | "Non-authoritive host not found or server failure."; |
| 1068 | break; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1069 | |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1070 | case NO_RECOVERY: |
| 1071 | h_err_txt = |
| 1072 | "Non-recoverable errors: FORMERR, REFUSED, or NOTIMP."; |
| 1073 | break; |
Daniel Veillard | 5c39654 | 2002-03-15 07:57:50 +0000 | [diff] [blame] | 1074 | |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1075 | case NO_ADDRESS: |
| 1076 | h_err_txt = |
| 1077 | "Valid name, no data record of requested type."; |
| 1078 | break; |
Daniel Veillard | 5c39654 | 2002-03-15 07:57:50 +0000 | [diff] [blame] | 1079 | |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1080 | default: |
| 1081 | h_err_txt = "No error text defined."; |
| 1082 | break; |
| 1083 | } |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 1084 | __xmlIOErr(XML_FROM_HTTP, 0, h_err_txt); |
Daniel Veillard | 5c39654 | 2002-03-15 07:57:50 +0000 | [diff] [blame] | 1085 | #else |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 1086 | __xmlIOErr(XML_FROM_HTTP, 0, "Failed to resolve host"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1087 | #endif |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1088 | return (-1); |
| 1089 | } |
Daniel Veillard | 5c39654 | 2002-03-15 07:57:50 +0000 | [diff] [blame] | 1090 | |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1091 | for (i = 0; h->h_addr_list[i]; i++) { |
| 1092 | if (h->h_addrtype == AF_INET) { |
| 1093 | /* A records (IPv4) */ |
Daniel Veillard | 8e2c979 | 2004-10-27 09:39:50 +0000 | [diff] [blame] | 1094 | if ((unsigned int) h->h_length > sizeof(ia)) { |
| 1095 | __xmlIOErr(XML_FROM_HTTP, 0, "address size mismatch\n"); |
| 1096 | return (-1); |
| 1097 | } |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1098 | memcpy (&ia, h->h_addr_list[i], h->h_length); |
| 1099 | sockin.sin_family = h->h_addrtype; |
| 1100 | sockin.sin_addr = ia; |
Daniel Veillard | 9e2110b | 2005-08-08 20:33:54 +0000 | [diff] [blame] | 1101 | sockin.sin_port = (u_short)htons ((unsigned short)port); |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1102 | addr = (struct sockaddr *) &sockin; |
Daniel Veillard | 5c39654 | 2002-03-15 07:57:50 +0000 | [diff] [blame] | 1103 | #ifdef SUPPORT_IP6 |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1104 | } else if (have_ipv6 () && (h->h_addrtype == AF_INET6)) { |
| 1105 | /* AAAA records (IPv6) */ |
Daniel Veillard | 8e2c979 | 2004-10-27 09:39:50 +0000 | [diff] [blame] | 1106 | if ((unsigned int) h->h_length > sizeof(ia6)) { |
| 1107 | __xmlIOErr(XML_FROM_HTTP, 0, "address size mismatch\n"); |
| 1108 | return (-1); |
| 1109 | } |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1110 | memcpy (&ia6, h->h_addr_list[i], h->h_length); |
| 1111 | sockin6.sin6_family = h->h_addrtype; |
| 1112 | sockin6.sin6_addr = ia6; |
| 1113 | sockin6.sin6_port = htons (port); |
| 1114 | addr = (struct sockaddr *) &sockin6; |
Daniel Veillard | 5c39654 | 2002-03-15 07:57:50 +0000 | [diff] [blame] | 1115 | #endif |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1116 | } else |
| 1117 | break; /* for */ |
Daniel Veillard | 5c39654 | 2002-03-15 07:57:50 +0000 | [diff] [blame] | 1118 | |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1119 | s = xmlNanoHTTPConnectAttempt (addr); |
| 1120 | if (s != -1) |
| 1121 | return (s); |
| 1122 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1123 | } |
Rob Richards | cb418de | 2005-10-13 23:12:42 +0000 | [diff] [blame] | 1124 | #endif |
| 1125 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1126 | #ifdef DEBUG_HTTP |
| 1127 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 5c39654 | 2002-03-15 07:57:50 +0000 | [diff] [blame] | 1128 | "xmlNanoHTTPConnectHost: unable to connect to '%s'.\n", |
| 1129 | host); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1130 | #endif |
Daniel Veillard | 5c39654 | 2002-03-15 07:57:50 +0000 | [diff] [blame] | 1131 | return (-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1132 | } |
| 1133 | |
| 1134 | |
| 1135 | /** |
| 1136 | * xmlNanoHTTPOpen: |
| 1137 | * @URL: The URL to load |
| 1138 | * @contentType: if available the Content-Type information will be |
| 1139 | * returned at that location |
| 1140 | * |
| 1141 | * This function try to open a connection to the indicated resource |
| 1142 | * via HTTP GET. |
| 1143 | * |
| 1144 | * Returns NULL in case of failure, otherwise a request handler. |
| 1145 | * The contentType, if provided must be freed by the caller |
| 1146 | */ |
| 1147 | |
| 1148 | void* |
| 1149 | xmlNanoHTTPOpen(const char *URL, char **contentType) { |
| 1150 | if (contentType != NULL) *contentType = NULL; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1151 | return(xmlNanoHTTPMethod(URL, NULL, NULL, contentType, NULL, 0)); |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 1152 | } |
| 1153 | |
| 1154 | /** |
| 1155 | * xmlNanoHTTPOpenRedir: |
| 1156 | * @URL: The URL to load |
| 1157 | * @contentType: if available the Content-Type information will be |
| 1158 | * returned at that location |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1159 | * @redir: if available the redirected URL will be returned |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 1160 | * |
| 1161 | * This function try to open a connection to the indicated resource |
| 1162 | * via HTTP GET. |
| 1163 | * |
| 1164 | * Returns NULL in case of failure, otherwise a request handler. |
| 1165 | * The contentType, if provided must be freed by the caller |
| 1166 | */ |
| 1167 | |
| 1168 | void* |
| 1169 | xmlNanoHTTPOpenRedir(const char *URL, char **contentType, char **redir) { |
| 1170 | if (contentType != NULL) *contentType = NULL; |
| 1171 | if (redir != NULL) *redir = NULL; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1172 | return(xmlNanoHTTPMethodRedir(URL, NULL, NULL, contentType, redir, NULL,0)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1173 | } |
| 1174 | |
| 1175 | /** |
| 1176 | * xmlNanoHTTPRead: |
| 1177 | * @ctx: the HTTP context |
| 1178 | * @dest: a buffer |
| 1179 | * @len: the buffer length |
| 1180 | * |
| 1181 | * This function tries to read @len bytes from the existing HTTP connection |
| 1182 | * and saves them in @dest. This is a blocking call. |
| 1183 | * |
| 1184 | * Returns the number of byte read. 0 is an indication of an end of connection. |
| 1185 | * -1 indicates a parameter error. |
| 1186 | */ |
| 1187 | int |
| 1188 | xmlNanoHTTPRead(void *ctx, void *dest, int len) { |
| 1189 | xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx; |
Daniel Veillard | 9a2724d | 2005-12-15 11:12:26 +0000 | [diff] [blame] | 1190 | #ifdef HAVE_ZLIB_H |
| 1191 | int bytes_read = 0; |
| 1192 | int orig_avail_in; |
| 1193 | int z_ret; |
| 1194 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1195 | |
| 1196 | if (ctx == NULL) return(-1); |
| 1197 | if (dest == NULL) return(-1); |
| 1198 | if (len <= 0) return(0); |
| 1199 | |
Daniel Veillard | 9a2724d | 2005-12-15 11:12:26 +0000 | [diff] [blame] | 1200 | #ifdef HAVE_ZLIB_H |
| 1201 | if (ctxt->usesGzip == 1) { |
| 1202 | if (ctxt->strm == NULL) return(0); |
| 1203 | |
| 1204 | ctxt->strm->next_out = dest; |
| 1205 | ctxt->strm->avail_out = len; |
William M. Brack | e882765 | 2007-05-16 05:19:13 +0000 | [diff] [blame] | 1206 | ctxt->strm->avail_in = ctxt->inptr - ctxt->inrptr; |
Daniel Veillard | 9a2724d | 2005-12-15 11:12:26 +0000 | [diff] [blame] | 1207 | |
William M. Brack | e882765 | 2007-05-16 05:19:13 +0000 | [diff] [blame] | 1208 | while (ctxt->strm->avail_out > 0 && |
| 1209 | (ctxt->strm->avail_in > 0 || xmlNanoHTTPRecv(ctxt) > 0)) { |
William M. Brack | d2f682a | 2007-05-15 19:42:08 +0000 | [diff] [blame] | 1210 | orig_avail_in = ctxt->strm->avail_in = |
| 1211 | ctxt->inptr - ctxt->inrptr - bytes_read; |
Daniel Veillard | 9a2724d | 2005-12-15 11:12:26 +0000 | [diff] [blame] | 1212 | ctxt->strm->next_in = BAD_CAST (ctxt->inrptr + bytes_read); |
| 1213 | |
| 1214 | z_ret = inflate(ctxt->strm, Z_NO_FLUSH); |
| 1215 | bytes_read += orig_avail_in - ctxt->strm->avail_in; |
| 1216 | |
| 1217 | if (z_ret != Z_OK) break; |
William M. Brack | d2f682a | 2007-05-15 19:42:08 +0000 | [diff] [blame] | 1218 | } |
Daniel Veillard | 9a2724d | 2005-12-15 11:12:26 +0000 | [diff] [blame] | 1219 | |
| 1220 | ctxt->inrptr += bytes_read; |
| 1221 | return(len - ctxt->strm->avail_out); |
| 1222 | } |
| 1223 | #endif |
| 1224 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1225 | while (ctxt->inptr - ctxt->inrptr < len) { |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1226 | if (xmlNanoHTTPRecv(ctxt) <= 0) break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1227 | } |
| 1228 | if (ctxt->inptr - ctxt->inrptr < len) |
| 1229 | len = ctxt->inptr - ctxt->inrptr; |
| 1230 | memcpy(dest, ctxt->inrptr, len); |
| 1231 | ctxt->inrptr += len; |
| 1232 | return(len); |
| 1233 | } |
| 1234 | |
| 1235 | /** |
| 1236 | * xmlNanoHTTPClose: |
| 1237 | * @ctx: the HTTP context |
| 1238 | * |
| 1239 | * This function closes an HTTP context, it ends up the connection and |
| 1240 | * free all data related to it. |
| 1241 | */ |
| 1242 | void |
| 1243 | xmlNanoHTTPClose(void *ctx) { |
| 1244 | xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx; |
| 1245 | |
| 1246 | if (ctx == NULL) return; |
| 1247 | |
| 1248 | xmlNanoHTTPFreeCtxt(ctxt); |
| 1249 | } |
| 1250 | |
| 1251 | /** |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 1252 | * xmlNanoHTTPMethodRedir: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1253 | * @URL: The URL to load |
| 1254 | * @method: the HTTP method to use |
| 1255 | * @input: the input string if any |
| 1256 | * @contentType: the Content-Type information IN and OUT |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 1257 | * @redir: the redirected URL OUT |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1258 | * @headers: the extra headers |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1259 | * @ilen: input length |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1260 | * |
| 1261 | * This function try to open a connection to the indicated resource |
| 1262 | * via HTTP using the given @method, adding the given extra headers |
| 1263 | * and the input buffer for the request content. |
| 1264 | * |
| 1265 | * Returns NULL in case of failure, otherwise a request handler. |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 1266 | * The contentType, or redir, if provided must be freed by the caller |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1267 | */ |
| 1268 | |
| 1269 | void* |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 1270 | xmlNanoHTTPMethodRedir(const char *URL, const char *method, const char *input, |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1271 | char **contentType, char **redir, |
| 1272 | const char *headers, int ilen ) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1273 | xmlNanoHTTPCtxtPtr ctxt; |
| 1274 | char *bp, *p; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1275 | int blen, ret; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1276 | int head; |
| 1277 | int nbRedirects = 0; |
| 1278 | char *redirURL = NULL; |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 1279 | #ifdef DEBUG_HTTP |
| 1280 | int xmt_bytes; |
| 1281 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1282 | |
| 1283 | if (URL == NULL) return(NULL); |
| 1284 | if (method == NULL) method = "GET"; |
| 1285 | xmlNanoHTTPInit(); |
| 1286 | |
| 1287 | retry: |
| 1288 | if (redirURL == NULL) |
| 1289 | ctxt = xmlNanoHTTPNewCtxt(URL); |
| 1290 | else { |
| 1291 | ctxt = xmlNanoHTTPNewCtxt(redirURL); |
Daniel Veillard | a840b69 | 2003-10-19 13:35:37 +0000 | [diff] [blame] | 1292 | ctxt->location = xmlMemStrdup(redirURL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1293 | } |
| 1294 | |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1295 | if ( ctxt == NULL ) { |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1296 | return ( NULL ); |
| 1297 | } |
| 1298 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1299 | if ((ctxt->protocol == NULL) || (strcmp(ctxt->protocol, "http"))) { |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 1300 | __xmlIOErr(XML_FROM_HTTP, XML_HTTP_URL_SYNTAX, "Not a valid HTTP URI"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1301 | xmlNanoHTTPFreeCtxt(ctxt); |
| 1302 | if (redirURL != NULL) xmlFree(redirURL); |
| 1303 | return(NULL); |
| 1304 | } |
| 1305 | if (ctxt->hostname == NULL) { |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 1306 | __xmlIOErr(XML_FROM_HTTP, XML_HTTP_UNKNOWN_HOST, |
| 1307 | "Failed to identify host in URI"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1308 | xmlNanoHTTPFreeCtxt(ctxt); |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 1309 | if (redirURL != NULL) xmlFree(redirURL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1310 | return(NULL); |
| 1311 | } |
| 1312 | if (proxy) { |
| 1313 | blen = strlen(ctxt->hostname) * 2 + 16; |
| 1314 | ret = xmlNanoHTTPConnectHost(proxy, proxyPort); |
| 1315 | } |
| 1316 | else { |
| 1317 | blen = strlen(ctxt->hostname); |
| 1318 | ret = xmlNanoHTTPConnectHost(ctxt->hostname, ctxt->port); |
| 1319 | } |
| 1320 | if (ret < 0) { |
| 1321 | xmlNanoHTTPFreeCtxt(ctxt); |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 1322 | if (redirURL != NULL) xmlFree(redirURL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1323 | return(NULL); |
| 1324 | } |
| 1325 | ctxt->fd = ret; |
| 1326 | |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1327 | if (input == NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1328 | ilen = 0; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1329 | else |
| 1330 | blen += 36; |
| 1331 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1332 | if (headers != NULL) |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1333 | blen += strlen(headers) + 2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1334 | if (contentType && *contentType) |
| 1335 | blen += strlen(*contentType) + 16; |
Daniel Veillard | 351f2d6 | 2005-04-13 02:55:12 +0000 | [diff] [blame] | 1336 | if (ctxt->query != NULL) |
| 1337 | blen += strlen(ctxt->query) + 1; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1338 | blen += strlen(method) + strlen(ctxt->path) + 24; |
Daniel Veillard | 9a2724d | 2005-12-15 11:12:26 +0000 | [diff] [blame] | 1339 | #ifdef HAVE_ZLIB_H |
| 1340 | blen += 23; |
| 1341 | #endif |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 1342 | bp = (char*)xmlMallocAtomic(blen); |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1343 | if ( bp == NULL ) { |
| 1344 | xmlNanoHTTPFreeCtxt( ctxt ); |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 1345 | xmlHTTPErrMemory("allocating header buffer"); |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1346 | return ( NULL ); |
| 1347 | } |
| 1348 | |
| 1349 | p = bp; |
| 1350 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1351 | if (proxy) { |
| 1352 | if (ctxt->port != 80) { |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 1353 | p += snprintf( p, blen - (p - bp), "%s http://%s:%d%s", |
| 1354 | method, ctxt->hostname, |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1355 | ctxt->port, ctxt->path ); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1356 | } |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 1357 | else |
| 1358 | p += snprintf( p, blen - (p - bp), "%s http://%s%s", method, |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1359 | ctxt->hostname, ctxt->path); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1360 | } |
| 1361 | else |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 1362 | p += snprintf( p, blen - (p - bp), "%s %s", method, ctxt->path); |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1363 | |
Daniel Veillard | 351f2d6 | 2005-04-13 02:55:12 +0000 | [diff] [blame] | 1364 | if (ctxt->query != NULL) |
| 1365 | p += snprintf( p, blen - (p - bp), "?%s", ctxt->query); |
| 1366 | |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 1367 | p += snprintf( p, blen - (p - bp), " HTTP/1.0\r\nHost: %s\r\n", |
| 1368 | ctxt->hostname); |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1369 | |
Daniel Veillard | 9a2724d | 2005-12-15 11:12:26 +0000 | [diff] [blame] | 1370 | #ifdef HAVE_ZLIB_H |
| 1371 | p += snprintf(p, blen - (p - bp), "Accept-Encoding: gzip\r\n"); |
| 1372 | #endif |
| 1373 | |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1374 | if (contentType != NULL && *contentType) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 1375 | p += snprintf(p, blen - (p - bp), "Content-Type: %s\r\n", *contentType); |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1376 | |
| 1377 | if (headers != NULL) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 1378 | p += snprintf( p, blen - (p - bp), "%s", headers ); |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1379 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1380 | if (input != NULL) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 1381 | snprintf(p, blen - (p - bp), "Content-Length: %d\r\n\r\n", ilen ); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1382 | else |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 1383 | snprintf(p, blen - (p - bp), "\r\n"); |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1384 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1385 | #ifdef DEBUG_HTTP |
| 1386 | xmlGenericError(xmlGenericErrorContext, |
| 1387 | "-> %s%s", proxy? "(Proxy) " : "", bp); |
| 1388 | if ((blen -= strlen(bp)+1) < 0) |
| 1389 | xmlGenericError(xmlGenericErrorContext, |
| 1390 | "ERROR: overflowed buffer by %d bytes\n", -blen); |
| 1391 | #endif |
| 1392 | ctxt->outptr = ctxt->out = bp; |
| 1393 | ctxt->state = XML_NANO_HTTP_WRITE; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1394 | blen = strlen( ctxt->out ); |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1395 | #ifdef DEBUG_HTTP |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 1396 | xmt_bytes = xmlNanoHTTPSend(ctxt, ctxt->out, blen ); |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1397 | if ( xmt_bytes != blen ) |
| 1398 | xmlGenericError( xmlGenericErrorContext, |
| 1399 | "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n", |
| 1400 | xmt_bytes, blen, |
| 1401 | "bytes of HTTP headers sent to host", |
| 1402 | ctxt->hostname ); |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 1403 | #else |
| 1404 | xmlNanoHTTPSend(ctxt, ctxt->out, blen ); |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1405 | #endif |
| 1406 | |
| 1407 | if ( input != NULL ) { |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 1408 | #ifdef DEBUG_HTTP |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1409 | xmt_bytes = xmlNanoHTTPSend( ctxt, input, ilen ); |
| 1410 | |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1411 | if ( xmt_bytes != ilen ) |
| 1412 | xmlGenericError( xmlGenericErrorContext, |
| 1413 | "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n", |
| 1414 | xmt_bytes, ilen, |
| 1415 | "bytes of HTTP content sent to host", |
| 1416 | ctxt->hostname ); |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 1417 | #else |
| 1418 | xmlNanoHTTPSend( ctxt, input, ilen ); |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1419 | #endif |
| 1420 | } |
| 1421 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1422 | ctxt->state = XML_NANO_HTTP_READ; |
| 1423 | head = 1; |
| 1424 | |
| 1425 | while ((p = xmlNanoHTTPReadLine(ctxt)) != NULL) { |
| 1426 | if (head && (*p == 0)) { |
| 1427 | head = 0; |
| 1428 | ctxt->content = ctxt->inrptr; |
| 1429 | xmlFree(p); |
| 1430 | break; |
| 1431 | } |
| 1432 | xmlNanoHTTPScanAnswer(ctxt, p); |
| 1433 | |
| 1434 | #ifdef DEBUG_HTTP |
| 1435 | xmlGenericError(xmlGenericErrorContext, "<- %s\n", p); |
| 1436 | #endif |
| 1437 | xmlFree(p); |
| 1438 | } |
| 1439 | |
| 1440 | if ((ctxt->location != NULL) && (ctxt->returnValue >= 300) && |
| 1441 | (ctxt->returnValue < 400)) { |
| 1442 | #ifdef DEBUG_HTTP |
| 1443 | xmlGenericError(xmlGenericErrorContext, |
| 1444 | "\nRedirect to: %s\n", ctxt->location); |
| 1445 | #endif |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1446 | while ( xmlNanoHTTPRecv(ctxt) > 0 ) ; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1447 | if (nbRedirects < XML_NANO_HTTP_MAX_REDIR) { |
| 1448 | nbRedirects++; |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 1449 | if (redirURL != NULL) |
| 1450 | xmlFree(redirURL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1451 | redirURL = xmlMemStrdup(ctxt->location); |
| 1452 | xmlNanoHTTPFreeCtxt(ctxt); |
| 1453 | goto retry; |
| 1454 | } |
| 1455 | xmlNanoHTTPFreeCtxt(ctxt); |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 1456 | if (redirURL != NULL) xmlFree(redirURL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1457 | #ifdef DEBUG_HTTP |
| 1458 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1459 | "xmlNanoHTTPMethodRedir: Too many redirects, aborting ...\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1460 | #endif |
| 1461 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1462 | } |
| 1463 | |
| 1464 | if (contentType != NULL) { |
| 1465 | if (ctxt->contentType != NULL) |
| 1466 | *contentType = xmlMemStrdup(ctxt->contentType); |
| 1467 | else |
| 1468 | *contentType = NULL; |
| 1469 | } |
| 1470 | |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 1471 | if ((redir != NULL) && (redirURL != NULL)) { |
| 1472 | *redir = redirURL; |
| 1473 | } else { |
| 1474 | if (redirURL != NULL) |
| 1475 | xmlFree(redirURL); |
| 1476 | if (redir != NULL) |
| 1477 | *redir = NULL; |
| 1478 | } |
| 1479 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1480 | #ifdef DEBUG_HTTP |
| 1481 | if (ctxt->contentType != NULL) |
| 1482 | xmlGenericError(xmlGenericErrorContext, |
| 1483 | "\nCode %d, content-type '%s'\n\n", |
| 1484 | ctxt->returnValue, ctxt->contentType); |
| 1485 | else |
| 1486 | xmlGenericError(xmlGenericErrorContext, |
| 1487 | "\nCode %d, no content-type\n\n", |
| 1488 | ctxt->returnValue); |
| 1489 | #endif |
| 1490 | |
| 1491 | return((void *) ctxt); |
| 1492 | } |
| 1493 | |
| 1494 | /** |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 1495 | * xmlNanoHTTPMethod: |
| 1496 | * @URL: The URL to load |
| 1497 | * @method: the HTTP method to use |
| 1498 | * @input: the input string if any |
| 1499 | * @contentType: the Content-Type information IN and OUT |
| 1500 | * @headers: the extra headers |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1501 | * @ilen: input length |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 1502 | * |
| 1503 | * This function try to open a connection to the indicated resource |
| 1504 | * via HTTP using the given @method, adding the given extra headers |
| 1505 | * and the input buffer for the request content. |
| 1506 | * |
| 1507 | * Returns NULL in case of failure, otherwise a request handler. |
| 1508 | * The contentType, if provided must be freed by the caller |
| 1509 | */ |
| 1510 | |
| 1511 | void* |
| 1512 | xmlNanoHTTPMethod(const char *URL, const char *method, const char *input, |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1513 | char **contentType, const char *headers, int ilen) { |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 1514 | return(xmlNanoHTTPMethodRedir(URL, method, input, contentType, |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1515 | NULL, headers, ilen)); |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 1516 | } |
| 1517 | |
| 1518 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1519 | * xmlNanoHTTPFetch: |
| 1520 | * @URL: The URL to load |
| 1521 | * @filename: the filename where the content should be saved |
| 1522 | * @contentType: if available the Content-Type information will be |
| 1523 | * returned at that location |
| 1524 | * |
| 1525 | * This function try to fetch the indicated resource via HTTP GET |
| 1526 | * and save it's content in the file. |
| 1527 | * |
| 1528 | * Returns -1 in case of failure, 0 incase of success. The contentType, |
| 1529 | * if provided must be freed by the caller |
| 1530 | */ |
| 1531 | int |
| 1532 | xmlNanoHTTPFetch(const char *URL, const char *filename, char **contentType) { |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1533 | void *ctxt = NULL; |
| 1534 | char *buf = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1535 | int fd; |
| 1536 | int len; |
| 1537 | |
William M. Brack | 015ccb2 | 2005-02-13 08:18:52 +0000 | [diff] [blame] | 1538 | if (filename == NULL) return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1539 | ctxt = xmlNanoHTTPOpen(URL, contentType); |
| 1540 | if (ctxt == NULL) return(-1); |
| 1541 | |
| 1542 | if (!strcmp(filename, "-")) |
| 1543 | fd = 0; |
| 1544 | else { |
| 1545 | fd = open(filename, O_CREAT | O_WRONLY, 00644); |
| 1546 | if (fd < 0) { |
| 1547 | xmlNanoHTTPClose(ctxt); |
| 1548 | if ((contentType != NULL) && (*contentType != NULL)) { |
| 1549 | xmlFree(*contentType); |
| 1550 | *contentType = NULL; |
| 1551 | } |
| 1552 | return(-1); |
| 1553 | } |
| 1554 | } |
| 1555 | |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1556 | xmlNanoHTTPFetchContent( ctxt, &buf, &len ); |
| 1557 | if ( len > 0 ) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1558 | write(fd, buf, len); |
| 1559 | } |
| 1560 | |
| 1561 | xmlNanoHTTPClose(ctxt); |
| 1562 | close(fd); |
| 1563 | return(0); |
| 1564 | } |
| 1565 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1566 | #ifdef LIBXML_OUTPUT_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1567 | /** |
| 1568 | * xmlNanoHTTPSave: |
| 1569 | * @ctxt: the HTTP context |
| 1570 | * @filename: the filename where the content should be saved |
| 1571 | * |
| 1572 | * This function saves the output of the HTTP transaction to a file |
| 1573 | * It closes and free the context at the end |
| 1574 | * |
| 1575 | * Returns -1 in case of failure, 0 incase of success. |
| 1576 | */ |
| 1577 | int |
| 1578 | xmlNanoHTTPSave(void *ctxt, const char *filename) { |
Daniel Veillard | e392497 | 2001-07-25 20:25:21 +0000 | [diff] [blame] | 1579 | char *buf = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1580 | int fd; |
| 1581 | int len; |
| 1582 | |
William M. Brack | 015ccb2 | 2005-02-13 08:18:52 +0000 | [diff] [blame] | 1583 | if ((ctxt == NULL) || (filename == NULL)) return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1584 | |
| 1585 | if (!strcmp(filename, "-")) |
| 1586 | fd = 0; |
| 1587 | else { |
| 1588 | fd = open(filename, O_CREAT | O_WRONLY); |
| 1589 | if (fd < 0) { |
| 1590 | xmlNanoHTTPClose(ctxt); |
| 1591 | return(-1); |
| 1592 | } |
| 1593 | } |
| 1594 | |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1595 | xmlNanoHTTPFetchContent( ctxt, &buf, &len ); |
| 1596 | if ( len > 0 ) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1597 | write(fd, buf, len); |
| 1598 | } |
| 1599 | |
| 1600 | xmlNanoHTTPClose(ctxt); |
William M. Brack | 20d8236 | 2004-03-17 08:44:46 +0000 | [diff] [blame] | 1601 | close(fd); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1602 | return(0); |
| 1603 | } |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1604 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1605 | |
| 1606 | /** |
| 1607 | * xmlNanoHTTPReturnCode: |
| 1608 | * @ctx: the HTTP context |
| 1609 | * |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1610 | * Get the latest HTTP return code received |
| 1611 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1612 | * Returns the HTTP return code for the request. |
| 1613 | */ |
| 1614 | int |
| 1615 | xmlNanoHTTPReturnCode(void *ctx) { |
| 1616 | xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx; |
| 1617 | |
| 1618 | if (ctxt == NULL) return(-1); |
| 1619 | |
| 1620 | return(ctxt->returnValue); |
| 1621 | } |
| 1622 | |
| 1623 | /** |
| 1624 | * xmlNanoHTTPAuthHeader: |
| 1625 | * @ctx: the HTTP context |
| 1626 | * |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1627 | * Get the authentication header of an HTTP context |
| 1628 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1629 | * Returns the stashed value of the WWW-Authenticate or Proxy-Authenticate |
| 1630 | * header. |
| 1631 | */ |
| 1632 | const char * |
| 1633 | xmlNanoHTTPAuthHeader(void *ctx) { |
| 1634 | xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx; |
| 1635 | |
| 1636 | if (ctxt == NULL) return(NULL); |
| 1637 | |
| 1638 | return(ctxt->authHeader); |
| 1639 | } |
| 1640 | |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1641 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 1642 | * xmlNanoHTTPContentLength: |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1643 | * @ctx: the HTTP context |
| 1644 | * |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 1645 | * Provides the specified content length from the HTTP header. |
| 1646 | * |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1647 | * Return the specified content length from the HTTP header. Note that |
| 1648 | * a value of -1 indicates that the content length element was not included in |
| 1649 | * the response header. |
| 1650 | */ |
| 1651 | int |
| 1652 | xmlNanoHTTPContentLength( void * ctx ) { |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 1653 | xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1654 | |
| 1655 | return ( ( ctxt == NULL ) ? -1 : ctxt->ContentLength ); |
| 1656 | } |
| 1657 | |
| 1658 | /** |
Daniel Veillard | 847332a | 2003-10-18 11:29:40 +0000 | [diff] [blame] | 1659 | * xmlNanoHTTPRedir: |
| 1660 | * @ctx: the HTTP context |
| 1661 | * |
| 1662 | * Provides the specified redirection URL if available from the HTTP header. |
| 1663 | * |
| 1664 | * Return the specified redirection URL or NULL if not redirected. |
| 1665 | */ |
| 1666 | const char * |
| 1667 | xmlNanoHTTPRedir( void * ctx ) { |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 1668 | xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx; |
Daniel Veillard | 847332a | 2003-10-18 11:29:40 +0000 | [diff] [blame] | 1669 | |
| 1670 | return ( ( ctxt == NULL ) ? NULL : ctxt->location ); |
| 1671 | } |
| 1672 | |
| 1673 | /** |
| 1674 | * xmlNanoHTTPEncoding: |
| 1675 | * @ctx: the HTTP context |
| 1676 | * |
| 1677 | * Provides the specified encoding if specified in the HTTP headers. |
| 1678 | * |
| 1679 | * Return the specified encoding or NULL if not available |
| 1680 | */ |
| 1681 | const char * |
| 1682 | xmlNanoHTTPEncoding( void * ctx ) { |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 1683 | xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx; |
Daniel Veillard | 847332a | 2003-10-18 11:29:40 +0000 | [diff] [blame] | 1684 | |
| 1685 | return ( ( ctxt == NULL ) ? NULL : ctxt->encoding ); |
| 1686 | } |
| 1687 | |
| 1688 | /** |
Daniel Veillard | a840b69 | 2003-10-19 13:35:37 +0000 | [diff] [blame] | 1689 | * xmlNanoHTTPMimeType: |
| 1690 | * @ctx: the HTTP context |
| 1691 | * |
| 1692 | * Provides the specified Mime-Type if specified in the HTTP headers. |
| 1693 | * |
| 1694 | * Return the specified Mime-Type or NULL if not available |
| 1695 | */ |
| 1696 | const char * |
| 1697 | xmlNanoHTTPMimeType( void * ctx ) { |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 1698 | xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx; |
Daniel Veillard | a840b69 | 2003-10-19 13:35:37 +0000 | [diff] [blame] | 1699 | |
| 1700 | return ( ( ctxt == NULL ) ? NULL : ctxt->mimeType ); |
| 1701 | } |
| 1702 | |
| 1703 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 1704 | * xmlNanoHTTPFetchContent: |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1705 | * @ctx: the HTTP context |
| 1706 | * @ptr: pointer to set to the content buffer. |
| 1707 | * @len: integer pointer to hold the length of the content |
| 1708 | * |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 1709 | * Check if all the content was read |
| 1710 | * |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1711 | * Returns 0 if all the content was read and available, returns |
| 1712 | * -1 if received content length was less than specified or an error |
| 1713 | * occurred. |
| 1714 | */ |
Daniel Veillard | a235132 | 2004-06-27 12:08:10 +0000 | [diff] [blame] | 1715 | static int |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1716 | xmlNanoHTTPFetchContent( void * ctx, char ** ptr, int * len ) { |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 1717 | xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1718 | |
| 1719 | int rc = 0; |
| 1720 | int cur_lgth; |
| 1721 | int rcvd_lgth; |
| 1722 | int dummy_int; |
| 1723 | char * dummy_ptr = NULL; |
| 1724 | |
| 1725 | /* Dummy up return input parameters if not provided */ |
| 1726 | |
| 1727 | if ( len == NULL ) |
| 1728 | len = &dummy_int; |
| 1729 | |
| 1730 | if ( ptr == NULL ) |
| 1731 | ptr = &dummy_ptr; |
| 1732 | |
| 1733 | /* But can't work without the context pointer */ |
| 1734 | |
| 1735 | if ( ( ctxt == NULL ) || ( ctxt->content == NULL ) ) { |
| 1736 | *len = 0; |
| 1737 | *ptr = NULL; |
| 1738 | return ( -1 ); |
| 1739 | } |
| 1740 | |
| 1741 | rcvd_lgth = ctxt->inptr - ctxt->content; |
| 1742 | |
| 1743 | while ( (cur_lgth = xmlNanoHTTPRecv( ctxt )) > 0 ) { |
| 1744 | |
| 1745 | rcvd_lgth += cur_lgth; |
| 1746 | if ( (ctxt->ContentLength > 0) && (rcvd_lgth >= ctxt->ContentLength) ) |
| 1747 | break; |
| 1748 | } |
| 1749 | |
| 1750 | *ptr = ctxt->content; |
| 1751 | *len = rcvd_lgth; |
| 1752 | |
| 1753 | if ( ( ctxt->ContentLength > 0 ) && ( rcvd_lgth < ctxt->ContentLength ) ) |
| 1754 | rc = -1; |
| 1755 | else if ( rcvd_lgth == 0 ) |
| 1756 | rc = -1; |
| 1757 | |
| 1758 | return ( rc ); |
| 1759 | } |
| 1760 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1761 | #ifdef STANDALONE |
| 1762 | int main(int argc, char **argv) { |
| 1763 | char *contentType = NULL; |
| 1764 | |
| 1765 | if (argv[1] != NULL) { |
| 1766 | if (argv[2] != NULL) |
| 1767 | xmlNanoHTTPFetch(argv[1], argv[2], &contentType); |
| 1768 | else |
| 1769 | xmlNanoHTTPFetch(argv[1], "-", &contentType); |
| 1770 | if (contentType != NULL) xmlFree(contentType); |
| 1771 | } else { |
| 1772 | xmlGenericError(xmlGenericErrorContext, |
| 1773 | "%s: minimal HTTP GET implementation\n", argv[0]); |
| 1774 | xmlGenericError(xmlGenericErrorContext, |
| 1775 | "\tusage %s [ URL [ filename ] ]\n", argv[0]); |
| 1776 | } |
| 1777 | xmlNanoHTTPCleanup(); |
| 1778 | xmlMemoryDump(); |
| 1779 | return(0); |
| 1780 | } |
| 1781 | #endif /* STANDALONE */ |
| 1782 | #else /* !LIBXML_HTTP_ENABLED */ |
| 1783 | #ifdef STANDALONE |
| 1784 | #include <stdio.h> |
| 1785 | int main(int argc, char **argv) { |
| 1786 | xmlGenericError(xmlGenericErrorContext, |
| 1787 | "%s : HTTP support not compiled in\n", argv[0]); |
| 1788 | return(0); |
| 1789 | } |
| 1790 | #endif /* STANDALONE */ |
| 1791 | #endif /* LIBXML_HTTP_ENABLED */ |
Daniel Veillard | 5d4644e | 2005-04-01 13:11:58 +0000 | [diff] [blame] | 1792 | #define bottom_nanohttp |
| 1793 | #include "elfgcchack.h" |