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