Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +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 | * |
| 11 | * Daniel.Veillard@w3.org |
| 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 | 3c558c3 | 1999-12-22 11:30:41 +0000 | [diff] [blame] | 17 | #ifdef WIN32 |
Daniel Veillard | 0142b84 | 2000-01-14 14:45:24 +0000 | [diff] [blame] | 18 | #define INCLUDE_WINSOCK |
Daniel Veillard | 3c558c3 | 1999-12-22 11:30:41 +0000 | [diff] [blame] | 19 | #include "win32config.h" |
| 20 | #else |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 21 | #include "config.h" |
| 22 | #endif |
| 23 | |
Daniel Veillard | b71379b | 2000-10-09 12:30:39 +0000 | [diff] [blame] | 24 | #include <libxml/xmlversion.h> |
Daniel Veillard | 3c558c3 | 1999-12-22 11:30:41 +0000 | [diff] [blame] | 25 | |
Daniel Veillard | 361d845 | 2000-04-03 19:48:13 +0000 | [diff] [blame] | 26 | #ifdef LIBXML_HTTP_ENABLED |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 27 | #include <stdio.h> |
| 28 | #include <string.h> |
| 29 | |
| 30 | #ifdef HAVE_STDLIB_H |
| 31 | #include <stdlib.h> |
| 32 | #endif |
| 33 | #ifdef HAVE_UNISTD_H |
| 34 | #include <unistd.h> |
| 35 | #endif |
| 36 | #ifdef HAVE_SYS_SOCKET_H |
| 37 | #include <sys/socket.h> |
| 38 | #endif |
| 39 | #ifdef HAVE_NETINET_IN_H |
| 40 | #include <netinet/in.h> |
| 41 | #endif |
| 42 | #ifdef HAVE_ARPA_INET_H |
| 43 | #include <arpa/inet.h> |
| 44 | #endif |
| 45 | #ifdef HAVE_NETDB_H |
| 46 | #include <netdb.h> |
| 47 | #endif |
| 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 |
Daniel Veillard | 5feb849 | 2000-02-02 17:15:36 +0000 | [diff] [blame] | 60 | #ifdef HAVE_STRINGS_H |
| 61 | #include <strings.h> |
| 62 | #endif |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 63 | |
Daniel Veillard | 361d845 | 2000-04-03 19:48:13 +0000 | [diff] [blame] | 64 | #include <libxml/xmlmemory.h> |
Daniel Veillard | b656ebe | 2000-09-22 13:51:48 +0000 | [diff] [blame] | 65 | #include <libxml/parser.h> /* for xmlStr(n)casecmp() */ |
Daniel Veillard | 361d845 | 2000-04-03 19:48:13 +0000 | [diff] [blame] | 66 | #include <libxml/nanohttp.h> |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 67 | |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 68 | /** |
| 69 | * A couple portability macros |
| 70 | */ |
| 71 | #ifndef _WINSOCKAPI_ |
| 72 | #define closesocket(s) close(s) |
| 73 | #define SOCKET int |
| 74 | #endif |
| 75 | |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 76 | #ifdef STANDALONE |
| 77 | #define DEBUG_HTTP |
Daniel Veillard | b656ebe | 2000-09-22 13:51:48 +0000 | [diff] [blame] | 78 | #define xmlStrncasecmp(a, b, n) strncasecmp((char *)a, (char *)b, n) |
Daniel Veillard | 8ddb5a7 | 2000-09-23 10:28:52 +0000 | [diff] [blame] | 79 | #define xmlStrcasecmpi(a, b) strcasecmp((char *)a, (char *)b) |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 80 | #endif |
| 81 | |
| 82 | #define XML_NANO_HTTP_MAX_REDIR 10 |
| 83 | |
| 84 | #define XML_NANO_HTTP_CHUNK 4096 |
| 85 | |
| 86 | #define XML_NANO_HTTP_CLOSED 0 |
| 87 | #define XML_NANO_HTTP_WRITE 1 |
| 88 | #define XML_NANO_HTTP_READ 2 |
| 89 | #define XML_NANO_HTTP_NONE 4 |
| 90 | |
| 91 | typedef struct xmlNanoHTTPCtxt { |
| 92 | char *protocol; /* the protocol name */ |
| 93 | char *hostname; /* the host name */ |
| 94 | int port; /* the port */ |
| 95 | char *path; /* the path within the URL */ |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 96 | SOCKET fd; /* the file descriptor for the socket */ |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 97 | int state; /* WRITE / READ / CLOSED */ |
| 98 | char *out; /* buffer sent (zero terminated) */ |
| 99 | char *outptr; /* index within the buffer sent */ |
| 100 | char *in; /* the receiving buffer */ |
| 101 | char *content; /* the start of the content */ |
| 102 | char *inptr; /* the next byte to read from network */ |
| 103 | char *inrptr; /* the next byte to give back to the client */ |
| 104 | int inlen; /* len of the input buffer */ |
| 105 | int last; /* return code for last operation */ |
| 106 | int returnValue; /* the protocol return value */ |
| 107 | char *contentType; /* the MIME type for the input */ |
| 108 | char *location; /* the new URL in case of redirect */ |
| 109 | } xmlNanoHTTPCtxt, *xmlNanoHTTPCtxtPtr; |
| 110 | |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 111 | static int initialized = 0; |
Daniel Veillard | 19d6111 | 2000-10-11 23:50:35 +0000 | [diff] [blame] | 112 | static char *proxy = NULL; /* the proxy name if any */ |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 113 | static int proxyPort; /* the proxy port if any */ |
Daniel Veillard | 19d6111 | 2000-10-11 23:50:35 +0000 | [diff] [blame] | 114 | static unsigned int timeout = 60;/* the select() timeout in seconds */ |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 115 | |
| 116 | /** |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 117 | * A portability function |
Daniel Veillard | e8282ed | 2000-10-10 23:01:31 +0000 | [diff] [blame] | 118 | */ |
Daniel Veillard | e8282ed | 2000-10-10 23:01:31 +0000 | [diff] [blame] | 119 | int socket_errno(void) { |
| 120 | #ifdef _WINSOCKAPI_ |
| 121 | return(WSAGetLastError()); |
| 122 | #else |
| 123 | return(errno); |
| 124 | #endif |
| 125 | } |
| 126 | |
| 127 | /** |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 128 | * xmlNanoHTTPInit: |
| 129 | * |
| 130 | * Initialize the HTTP protocol layer. |
| 131 | * Currently it just checks for proxy informations |
| 132 | */ |
| 133 | |
| 134 | void |
| 135 | xmlNanoHTTPInit(void) { |
| 136 | const char *env; |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 137 | #ifdef _WINSOCKAPI_ |
| 138 | WSADATA wsaData; |
| 139 | #endif |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 140 | |
| 141 | if (initialized) |
| 142 | return; |
| 143 | |
Daniel Veillard | e8282ed | 2000-10-10 23:01:31 +0000 | [diff] [blame] | 144 | #ifdef _WINSOCKAPI_ |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 145 | if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) |
| 146 | return; |
Daniel Veillard | e8282ed | 2000-10-10 23:01:31 +0000 | [diff] [blame] | 147 | #endif |
| 148 | |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 149 | if (proxy == NULL) { |
| 150 | proxyPort = 80; |
| 151 | env = getenv("no_proxy"); |
| 152 | if (env != NULL) |
| 153 | goto done; |
| 154 | env = getenv("http_proxy"); |
| 155 | if (env != NULL) { |
| 156 | xmlNanoHTTPScanProxy(env); |
| 157 | goto done; |
| 158 | } |
| 159 | env = getenv("HTTP_PROXY"); |
| 160 | if (env != NULL) { |
| 161 | xmlNanoHTTPScanProxy(env); |
| 162 | goto done; |
| 163 | } |
| 164 | } |
| 165 | done: |
| 166 | initialized = 1; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * xmlNanoHTTPClenup: |
| 171 | * |
| 172 | * Cleanup the HTTP protocol layer. |
| 173 | */ |
| 174 | |
| 175 | void |
| 176 | xmlNanoHTTPCleanup(void) { |
| 177 | if (proxy != NULL) |
| 178 | xmlFree(proxy); |
Daniel Veillard | e8282ed | 2000-10-10 23:01:31 +0000 | [diff] [blame] | 179 | #ifdef _WINSOCKAPI_ |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 180 | if (initialized) |
| 181 | WSACleanup(); |
Daniel Veillard | e8282ed | 2000-10-10 23:01:31 +0000 | [diff] [blame] | 182 | #endif |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 183 | initialized = 0; |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 184 | return; |
| 185 | } |
| 186 | |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 187 | /** |
Daniel Veillard | 19d6111 | 2000-10-11 23:50:35 +0000 | [diff] [blame] | 188 | * xmlNanoHTTPTimeout: |
| 189 | * @delay: the delay in seconds |
| 190 | * |
| 191 | * Set the HTTP timeout, (default is 60secs). 0 means immediate |
| 192 | * return, while -1 infinite. |
| 193 | */ |
| 194 | |
| 195 | void |
| 196 | xmlNanoHTTPTimeout(int delay) { |
| 197 | timeout = (unsigned int) delay; |
| 198 | } |
| 199 | |
| 200 | /** |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 201 | * xmlNanoHTTPScanURL: |
| 202 | * @ctxt: an HTTP context |
| 203 | * @URL: The URL used to initialize the context |
| 204 | * |
| 205 | * (Re)Initialize an HTTP context by parsing the URL and finding |
| 206 | * the protocol host port and path it indicates. |
| 207 | */ |
| 208 | |
| 209 | static void |
| 210 | xmlNanoHTTPScanURL(xmlNanoHTTPCtxtPtr ctxt, const char *URL) { |
| 211 | const char *cur = URL; |
| 212 | char buf[4096]; |
| 213 | int index = 0; |
| 214 | int port = 0; |
| 215 | |
| 216 | if (ctxt->protocol != NULL) { |
| 217 | xmlFree(ctxt->protocol); |
| 218 | ctxt->protocol = NULL; |
| 219 | } |
| 220 | if (ctxt->hostname != NULL) { |
| 221 | xmlFree(ctxt->hostname); |
| 222 | ctxt->hostname = NULL; |
| 223 | } |
| 224 | if (ctxt->path != NULL) { |
| 225 | xmlFree(ctxt->path); |
| 226 | ctxt->path = NULL; |
| 227 | } |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 228 | if (URL == NULL) return; |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 229 | buf[index] = 0; |
| 230 | while (*cur != 0) { |
| 231 | if ((cur[0] == ':') && (cur[1] == '/') && (cur[2] == '/')) { |
| 232 | buf[index] = 0; |
| 233 | ctxt->protocol = xmlMemStrdup(buf); |
| 234 | index = 0; |
| 235 | cur += 3; |
| 236 | break; |
| 237 | } |
| 238 | buf[index++] = *cur++; |
| 239 | } |
| 240 | if (*cur == 0) return; |
| 241 | |
| 242 | buf[index] = 0; |
| 243 | while (1) { |
| 244 | if (cur[0] == ':') { |
| 245 | buf[index] = 0; |
| 246 | ctxt->hostname = xmlMemStrdup(buf); |
| 247 | index = 0; |
| 248 | cur += 1; |
| 249 | while ((*cur >= '0') && (*cur <= '9')) { |
| 250 | port *= 10; |
| 251 | port += *cur - '0'; |
| 252 | cur++; |
| 253 | } |
| 254 | if (port != 0) ctxt->port = port; |
| 255 | while ((cur[0] != '/') && (*cur != 0)) |
| 256 | cur++; |
| 257 | break; |
| 258 | } |
| 259 | if ((*cur == '/') || (*cur == 0)) { |
| 260 | buf[index] = 0; |
| 261 | ctxt->hostname = xmlMemStrdup(buf); |
| 262 | index = 0; |
| 263 | break; |
| 264 | } |
| 265 | buf[index++] = *cur++; |
| 266 | } |
| 267 | if (*cur == 0) |
| 268 | ctxt->path = xmlMemStrdup("/"); |
| 269 | else { |
Daniel Veillard | 726e879 | 2000-01-30 20:04:29 +0000 | [diff] [blame] | 270 | index = 0; |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 271 | buf[index] = 0; |
Daniel Veillard | 726e879 | 2000-01-30 20:04:29 +0000 | [diff] [blame] | 272 | while (*cur != 0) |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 273 | buf[index++] = *cur++; |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 274 | buf[index] = 0; |
| 275 | ctxt->path = xmlMemStrdup(buf); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | /** |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 280 | * xmlNanoHTTPScanProxy: |
| 281 | * @URL: The proxy URL used to initialize the proxy context |
| 282 | * |
| 283 | * (Re)Initialize the HTTP Proxy context by parsing the URL and finding |
| 284 | * the protocol host port it indicates. |
| 285 | * Should be like http://myproxy/ or http://myproxy:3128/ |
| 286 | * A NULL URL cleans up proxy informations. |
| 287 | */ |
| 288 | |
| 289 | void |
| 290 | xmlNanoHTTPScanProxy(const char *URL) { |
| 291 | const char *cur = URL; |
| 292 | char buf[4096]; |
| 293 | int index = 0; |
| 294 | int port = 0; |
| 295 | |
| 296 | if (proxy != NULL) { |
| 297 | xmlFree(proxy); |
| 298 | proxy = NULL; |
| 299 | } |
| 300 | if (proxyPort != 0) { |
| 301 | proxyPort = 0; |
| 302 | } |
| 303 | #ifdef DEBUG_HTTP |
| 304 | if (URL == NULL) |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 305 | xmlGenericError(xmlGenericErrorContext, |
| 306 | "Removing HTTP proxy info\n"); |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 307 | else |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 308 | xmlGenericError(xmlGenericErrorContext, |
| 309 | "Using HTTP proxy %s\n", URL); |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 310 | #endif |
| 311 | if (URL == NULL) return; |
| 312 | buf[index] = 0; |
| 313 | while (*cur != 0) { |
| 314 | if ((cur[0] == ':') && (cur[1] == '/') && (cur[2] == '/')) { |
| 315 | buf[index] = 0; |
| 316 | index = 0; |
| 317 | cur += 3; |
| 318 | break; |
| 319 | } |
| 320 | buf[index++] = *cur++; |
| 321 | } |
| 322 | if (*cur == 0) return; |
| 323 | |
| 324 | buf[index] = 0; |
| 325 | while (1) { |
| 326 | if (cur[0] == ':') { |
| 327 | buf[index] = 0; |
| 328 | proxy = xmlMemStrdup(buf); |
| 329 | index = 0; |
| 330 | cur += 1; |
| 331 | while ((*cur >= '0') && (*cur <= '9')) { |
| 332 | port *= 10; |
| 333 | port += *cur - '0'; |
| 334 | cur++; |
| 335 | } |
| 336 | if (port != 0) proxyPort = port; |
| 337 | while ((cur[0] != '/') && (*cur != 0)) |
| 338 | cur++; |
| 339 | break; |
| 340 | } |
| 341 | if ((*cur == '/') || (*cur == 0)) { |
| 342 | buf[index] = 0; |
| 343 | proxy = xmlMemStrdup(buf); |
| 344 | index = 0; |
| 345 | break; |
| 346 | } |
| 347 | buf[index++] = *cur++; |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | /** |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 352 | * xmlNanoHTTPNewCtxt: |
| 353 | * @URL: The URL used to initialize the context |
| 354 | * |
| 355 | * Allocate and initialize a new HTTP context. |
| 356 | * |
| 357 | * Returns an HTTP context or NULL in case of error. |
| 358 | */ |
| 359 | |
| 360 | static xmlNanoHTTPCtxtPtr |
| 361 | xmlNanoHTTPNewCtxt(const char *URL) { |
| 362 | xmlNanoHTTPCtxtPtr ret; |
| 363 | |
| 364 | ret = (xmlNanoHTTPCtxtPtr) xmlMalloc(sizeof(xmlNanoHTTPCtxt)); |
| 365 | if (ret == NULL) return(NULL); |
| 366 | |
| 367 | memset(ret, 0, sizeof(xmlNanoHTTPCtxt)); |
| 368 | ret->port = 80; |
| 369 | ret->returnValue = 0; |
| 370 | |
| 371 | xmlNanoHTTPScanURL(ret, URL); |
| 372 | |
| 373 | return(ret); |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * xmlNanoHTTPFreeCtxt: |
| 378 | * @ctxt: an HTTP context |
| 379 | * |
| 380 | * Frees the context after closing the connection. |
| 381 | */ |
| 382 | |
| 383 | static void |
| 384 | xmlNanoHTTPFreeCtxt(xmlNanoHTTPCtxtPtr ctxt) { |
| 385 | if (ctxt == NULL) return; |
| 386 | if (ctxt->hostname != NULL) xmlFree(ctxt->hostname); |
| 387 | if (ctxt->protocol != NULL) xmlFree(ctxt->protocol); |
| 388 | if (ctxt->path != NULL) xmlFree(ctxt->path); |
| 389 | if (ctxt->out != NULL) xmlFree(ctxt->out); |
| 390 | if (ctxt->in != NULL) xmlFree(ctxt->in); |
| 391 | if (ctxt->contentType != NULL) xmlFree(ctxt->contentType); |
| 392 | if (ctxt->location != NULL) xmlFree(ctxt->location); |
| 393 | ctxt->state = XML_NANO_HTTP_NONE; |
Daniel Veillard | e8282ed | 2000-10-10 23:01:31 +0000 | [diff] [blame] | 394 | if (ctxt->fd >= 0) closesocket(ctxt->fd); |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 395 | ctxt->fd = -1; |
| 396 | xmlFree(ctxt); |
| 397 | } |
| 398 | |
| 399 | /** |
| 400 | * xmlNanoHTTPSend: |
| 401 | * @ctxt: an HTTP context |
| 402 | * |
| 403 | * Send the input needed to initiate the processing on the server side |
| 404 | */ |
| 405 | |
| 406 | static void |
| 407 | xmlNanoHTTPSend(xmlNanoHTTPCtxtPtr ctxt) { |
Daniel Veillard | 683cb02 | 2000-10-22 12:04:13 +0000 | [diff] [blame] | 408 | if (ctxt->state & XML_NANO_HTTP_WRITE) { |
| 409 | int total_sent = 0; |
| 410 | while (total_sent <strlen(ctxt->outptr)) { |
| 411 | int nsent = send(ctxt->fd, ctxt->outptr+total_sent, |
| 412 | strlen(ctxt->outptr)-total_sent, 0); |
| 413 | if (nsent>0) |
| 414 | total_sent += nsent; |
| 415 | } |
| 416 | |
| 417 | ctxt->last = total_sent; |
| 418 | } |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | /** |
| 422 | * xmlNanoHTTPRecv: |
| 423 | * @ctxt: an HTTP context |
| 424 | * |
| 425 | * Read information coming from the HTTP connection. |
| 426 | * This is a blocking call (but it blocks in select(), not read()). |
| 427 | * |
| 428 | * Returns the number of byte read or -1 in case of error. |
| 429 | */ |
| 430 | |
| 431 | static int |
| 432 | xmlNanoHTTPRecv(xmlNanoHTTPCtxtPtr ctxt) { |
| 433 | fd_set rfd; |
| 434 | struct timeval tv; |
| 435 | |
| 436 | |
| 437 | while (ctxt->state & XML_NANO_HTTP_READ) { |
| 438 | if (ctxt->in == NULL) { |
| 439 | ctxt->in = (char *) xmlMalloc(65000 * sizeof(char)); |
| 440 | if (ctxt->in == NULL) { |
| 441 | ctxt->last = -1; |
| 442 | return(-1); |
| 443 | } |
| 444 | ctxt->inlen = 65000; |
| 445 | ctxt->inptr = ctxt->content = ctxt->inrptr = ctxt->in; |
| 446 | } |
| 447 | if (ctxt->inrptr > ctxt->in + XML_NANO_HTTP_CHUNK) { |
| 448 | int delta = ctxt->inrptr - ctxt->in; |
| 449 | int len = ctxt->inptr - ctxt->inrptr; |
| 450 | |
| 451 | memmove(ctxt->in, ctxt->inrptr, len); |
| 452 | ctxt->inrptr -= delta; |
| 453 | ctxt->content -= delta; |
| 454 | ctxt->inptr -= delta; |
| 455 | } |
| 456 | if ((ctxt->in + ctxt->inlen) < (ctxt->inptr + XML_NANO_HTTP_CHUNK)) { |
| 457 | int d_inptr = ctxt->inptr - ctxt->in; |
| 458 | int d_content = ctxt->content - ctxt->in; |
| 459 | int d_inrptr = ctxt->inrptr - ctxt->in; |
| 460 | |
| 461 | ctxt->inlen *= 2; |
| 462 | ctxt->in = (char *) xmlRealloc(ctxt->in, ctxt->inlen); |
| 463 | if (ctxt->in == NULL) { |
| 464 | ctxt->last = -1; |
| 465 | return(-1); |
| 466 | } |
| 467 | ctxt->inptr = ctxt->in + d_inptr; |
| 468 | ctxt->content = ctxt->in + d_content; |
| 469 | ctxt->inrptr = ctxt->in + d_inrptr; |
| 470 | } |
Daniel Veillard | e8282ed | 2000-10-10 23:01:31 +0000 | [diff] [blame] | 471 | ctxt->last = recv(ctxt->fd, ctxt->inptr, XML_NANO_HTTP_CHUNK, 0); |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 472 | if (ctxt->last > 0) { |
| 473 | ctxt->inptr += ctxt->last; |
| 474 | return(ctxt->last); |
| 475 | } |
| 476 | if (ctxt->last == 0) { |
| 477 | return(0); |
| 478 | } |
Daniel Veillard | e8282ed | 2000-10-10 23:01:31 +0000 | [diff] [blame] | 479 | if (ctxt->last == -1) { |
| 480 | switch (socket_errno()) { |
| 481 | case EINPROGRESS: |
| 482 | case EWOULDBLOCK: |
| 483 | #if defined(EAGAIN) && EAGAIN != EWOULDBLOCK |
| 484 | case EAGAIN: |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 485 | #endif |
Daniel Veillard | e8282ed | 2000-10-10 23:01:31 +0000 | [diff] [blame] | 486 | break; |
| 487 | default: |
| 488 | return(0); |
| 489 | } |
| 490 | } |
| 491 | |
Daniel Veillard | 19d6111 | 2000-10-11 23:50:35 +0000 | [diff] [blame] | 492 | tv.tv_sec = timeout; |
Daniel Veillard | e8282ed | 2000-10-10 23:01:31 +0000 | [diff] [blame] | 493 | tv.tv_usec = 0; |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 494 | FD_ZERO(&rfd); |
| 495 | FD_SET(ctxt->fd, &rfd); |
| 496 | |
Daniel Veillard | e8282ed | 2000-10-10 23:01:31 +0000 | [diff] [blame] | 497 | if (select(ctxt->fd+1, &rfd, NULL, NULL, &tv)<1) |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 498 | return(0); |
| 499 | } |
| 500 | return(0); |
| 501 | } |
| 502 | |
| 503 | /** |
| 504 | * xmlNanoHTTPReadLine: |
| 505 | * @ctxt: an HTTP context |
| 506 | * |
| 507 | * Read one line in the HTTP server output, usually for extracting |
| 508 | * the HTTP protocol informations from the answer header. |
| 509 | * |
| 510 | * Returns a newly allocated string with a copy of the line, or NULL |
| 511 | * which indicate the end of the input. |
| 512 | */ |
| 513 | |
| 514 | static char * |
| 515 | xmlNanoHTTPReadLine(xmlNanoHTTPCtxtPtr ctxt) { |
| 516 | char buf[4096]; |
Daniel Veillard | e8282ed | 2000-10-10 23:01:31 +0000 | [diff] [blame] | 517 | char *bp = buf; |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 518 | |
Daniel Veillard | e8282ed | 2000-10-10 23:01:31 +0000 | [diff] [blame] | 519 | while (bp - buf < 4095) { |
| 520 | if (ctxt->inrptr == ctxt->inptr) { |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 521 | if (xmlNanoHTTPRecv(ctxt) == 0) { |
| 522 | if (bp == buf) |
| 523 | return(NULL); |
| 524 | else |
| 525 | *bp = 0; |
| 526 | return(xmlMemStrdup(buf)); |
| 527 | } |
| 528 | } |
| 529 | *bp = *ctxt->inrptr++; |
Daniel Veillard | e8282ed | 2000-10-10 23:01:31 +0000 | [diff] [blame] | 530 | if (*bp == '\n') { |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 531 | *bp = 0; |
| 532 | return(xmlMemStrdup(buf)); |
| 533 | } |
Daniel Veillard | e8282ed | 2000-10-10 23:01:31 +0000 | [diff] [blame] | 534 | if (*bp != '\r') |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 535 | bp++; |
| 536 | } |
| 537 | buf[4095] = 0; |
| 538 | return(xmlMemStrdup(buf)); |
| 539 | } |
| 540 | |
| 541 | |
| 542 | /** |
| 543 | * xmlNanoHTTPScanAnswer: |
| 544 | * @ctxt: an HTTP context |
| 545 | * @line: an HTTP header line |
| 546 | * |
| 547 | * Try to extract useful informations from the server answer. |
| 548 | * We currently parse and process: |
| 549 | * - The HTTP revision/ return code |
| 550 | * - The Content-Type |
| 551 | * - The Location for redirrect processing. |
| 552 | * |
| 553 | * Returns -1 in case of failure, the file descriptor number otherwise |
| 554 | */ |
| 555 | |
| 556 | static void |
| 557 | xmlNanoHTTPScanAnswer(xmlNanoHTTPCtxtPtr ctxt, const char *line) { |
| 558 | const char *cur = line; |
| 559 | |
| 560 | if (line == NULL) return; |
| 561 | |
| 562 | if (!strncmp(line, "HTTP/", 5)) { |
| 563 | int version = 0; |
| 564 | int ret = 0; |
| 565 | |
| 566 | cur += 5; |
| 567 | while ((*cur >= '0') && (*cur <= '9')) { |
| 568 | version *= 10; |
| 569 | version += *cur - '0'; |
| 570 | cur++; |
| 571 | } |
| 572 | if (*cur == '.') { |
| 573 | cur++; |
| 574 | if ((*cur >= '0') && (*cur <= '9')) { |
| 575 | version *= 10; |
| 576 | version += *cur - '0'; |
| 577 | cur++; |
| 578 | } |
| 579 | while ((*cur >= '0') && (*cur <= '9')) |
| 580 | cur++; |
| 581 | } else |
| 582 | version *= 10; |
| 583 | if ((*cur != ' ') && (*cur != '\t')) return; |
| 584 | while ((*cur == ' ') || (*cur == '\t')) cur++; |
| 585 | if ((*cur < '0') || (*cur > '9')) return; |
| 586 | while ((*cur >= '0') && (*cur <= '9')) { |
| 587 | ret *= 10; |
| 588 | ret += *cur - '0'; |
| 589 | cur++; |
| 590 | } |
| 591 | if ((*cur != 0) && (*cur != ' ') && (*cur != '\t')) return; |
| 592 | ctxt->returnValue = ret; |
Daniel Veillard | b656ebe | 2000-09-22 13:51:48 +0000 | [diff] [blame] | 593 | } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Content-Type:", 13)) { |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 594 | cur += 13; |
| 595 | while ((*cur == ' ') || (*cur == '\t')) cur++; |
| 596 | if (ctxt->contentType != NULL) |
| 597 | xmlFree(ctxt->contentType); |
| 598 | ctxt->contentType = xmlMemStrdup(cur); |
Daniel Veillard | b656ebe | 2000-09-22 13:51:48 +0000 | [diff] [blame] | 599 | } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"ContentType:", 12)) { |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 600 | cur += 12; |
| 601 | if (ctxt->contentType != NULL) return; |
| 602 | while ((*cur == ' ') || (*cur == '\t')) cur++; |
| 603 | ctxt->contentType = xmlMemStrdup(cur); |
Daniel Veillard | b656ebe | 2000-09-22 13:51:48 +0000 | [diff] [blame] | 604 | } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Location:", 9)) { |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 605 | cur += 9; |
| 606 | while ((*cur == ' ') || (*cur == '\t')) cur++; |
| 607 | if (ctxt->location != NULL) |
| 608 | xmlFree(ctxt->location); |
| 609 | ctxt->location = xmlMemStrdup(cur); |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 610 | } |
| 611 | } |
| 612 | |
| 613 | /** |
| 614 | * xmlNanoHTTPConnectAttempt: |
| 615 | * @ia: an internet adress structure |
| 616 | * @port: the port number |
| 617 | * |
| 618 | * Attempt a connection to the given IP:port endpoint. It forces |
| 619 | * non-blocking semantic on the socket, and allow 60 seconds for |
| 620 | * the host to answer. |
| 621 | * |
| 622 | * Returns -1 in case of failure, the file descriptor number otherwise |
| 623 | */ |
| 624 | |
| 625 | static int |
| 626 | xmlNanoHTTPConnectAttempt(struct in_addr ia, int port) |
| 627 | { |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 628 | SOCKET s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 629 | struct sockaddr_in sin; |
| 630 | fd_set wfd; |
| 631 | struct timeval tv; |
| 632 | int status; |
| 633 | |
Daniel Veillard | e8282ed | 2000-10-10 23:01:31 +0000 | [diff] [blame] | 634 | if (s==-1) { |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 635 | #ifdef DEBUG_HTTP |
| 636 | perror("socket"); |
| 637 | #endif |
| 638 | return(-1); |
| 639 | } |
| 640 | |
| 641 | #ifdef _WINSOCKAPI_ |
| 642 | { |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 643 | u_long one = 1; |
| 644 | |
| 645 | status = ioctlsocket(s, FIONBIO, &one) == SOCKET_ERROR ? -1 : 0; |
| 646 | } |
| 647 | #else /* _WINSOCKAPI_ */ |
| 648 | #if defined(VMS) |
| 649 | { |
| 650 | int enable = 1; |
| 651 | status = IOCTL(s, FIONBIO, &enable); |
| 652 | } |
| 653 | #else /* VMS */ |
Daniel Veillard | e8282ed | 2000-10-10 23:01:31 +0000 | [diff] [blame] | 654 | if ((status = fcntl(s, F_GETFL, 0)) != -1) { |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 655 | #ifdef O_NONBLOCK |
| 656 | status |= O_NONBLOCK; |
| 657 | #else /* O_NONBLOCK */ |
| 658 | #ifdef F_NDELAY |
| 659 | status |= F_NDELAY; |
| 660 | #endif /* F_NDELAY */ |
| 661 | #endif /* !O_NONBLOCK */ |
| 662 | status = fcntl(s, F_SETFL, status); |
| 663 | } |
Daniel Veillard | e8282ed | 2000-10-10 23:01:31 +0000 | [diff] [blame] | 664 | if (status < 0) { |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 665 | #ifdef DEBUG_HTTP |
| 666 | perror("nonblocking"); |
| 667 | #endif |
Daniel Veillard | e8282ed | 2000-10-10 23:01:31 +0000 | [diff] [blame] | 668 | closesocket(s); |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 669 | return(-1); |
| 670 | } |
| 671 | #endif /* !VMS */ |
| 672 | #endif /* !_WINSOCKAPI_ */ |
| 673 | |
| 674 | |
| 675 | sin.sin_family = AF_INET; |
| 676 | sin.sin_addr = ia; |
| 677 | sin.sin_port = htons(port); |
| 678 | |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 679 | if ((connect(s, (struct sockaddr *)&sin, sizeof(sin))==-1)) { |
| 680 | switch (socket_errno()) { |
| 681 | case EINPROGRESS: |
| 682 | case EWOULDBLOCK: |
| 683 | break; |
| 684 | default: |
| 685 | perror("connect"); |
| 686 | closesocket(s); |
| 687 | return(-1); |
| 688 | } |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 689 | } |
| 690 | |
Daniel Veillard | 19d6111 | 2000-10-11 23:50:35 +0000 | [diff] [blame] | 691 | tv.tv_sec = timeout; |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 692 | tv.tv_usec = 0; |
| 693 | |
| 694 | FD_ZERO(&wfd); |
| 695 | FD_SET(s, &wfd); |
| 696 | |
| 697 | switch(select(s+1, NULL, &wfd, NULL, &tv)) |
| 698 | { |
| 699 | case 0: |
| 700 | /* Time out */ |
Daniel Veillard | e8282ed | 2000-10-10 23:01:31 +0000 | [diff] [blame] | 701 | closesocket(s); |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 702 | return(-1); |
| 703 | case -1: |
| 704 | /* Ermm.. ?? */ |
| 705 | #ifdef DEBUG_HTTP |
| 706 | perror("select"); |
| 707 | #endif |
Daniel Veillard | e8282ed | 2000-10-10 23:01:31 +0000 | [diff] [blame] | 708 | closesocket(s); |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 709 | return(-1); |
| 710 | } |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 711 | |
| 712 | if ( FD_ISSET(s, &wfd) ) { |
Daniel Veillard | b0426ca | 2000-10-11 23:39:43 +0000 | [diff] [blame] | 713 | SOCKLEN_T len; |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 714 | len = sizeof(status); |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 715 | if (getsockopt(s, SOL_SOCKET, SO_ERROR, (char*)&status, &len) < 0 ) { |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 716 | /* Solaris error code */ |
| 717 | return (-1); |
| 718 | } |
| 719 | if ( status ) { |
Daniel Veillard | e8282ed | 2000-10-10 23:01:31 +0000 | [diff] [blame] | 720 | closesocket(s); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 721 | errno = status; |
| 722 | return (-1); |
| 723 | } |
| 724 | } else { |
| 725 | /* pbm */ |
| 726 | return (-1); |
| 727 | } |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 728 | |
| 729 | return(s); |
| 730 | } |
| 731 | |
| 732 | /** |
| 733 | * xmlNanoHTTPConnectHost: |
| 734 | * @host: the host name |
| 735 | * @port: the port number |
| 736 | * |
| 737 | * Attempt a connection to the given host:port endpoint. It tries |
| 738 | * the multiple IP provided by the DNS if available. |
| 739 | * |
| 740 | * Returns -1 in case of failure, the file descriptor number otherwise |
| 741 | */ |
| 742 | |
| 743 | static int |
| 744 | xmlNanoHTTPConnectHost(const char *host, int port) |
| 745 | { |
| 746 | struct hostent *h; |
| 747 | int i; |
| 748 | int s; |
| 749 | |
| 750 | h=gethostbyname(host); |
Daniel Veillard | e8282ed | 2000-10-10 23:01:31 +0000 | [diff] [blame] | 751 | if (h==NULL) |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 752 | { |
| 753 | #ifdef DEBUG_HTTP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 754 | xmlGenericError(xmlGenericErrorContext,"unable to resolve '%s'.\n", host); |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 755 | #endif |
| 756 | return(-1); |
| 757 | } |
| 758 | |
| 759 | for(i=0; h->h_addr_list[i]; i++) |
| 760 | { |
| 761 | struct in_addr ia; |
| 762 | memcpy(&ia, h->h_addr_list[i],4); |
| 763 | s = xmlNanoHTTPConnectAttempt(ia, port); |
Daniel Veillard | e8282ed | 2000-10-10 23:01:31 +0000 | [diff] [blame] | 764 | if (s != -1) |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 765 | return(s); |
| 766 | } |
| 767 | |
| 768 | #ifdef DEBUG_HTTP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 769 | xmlGenericError(xmlGenericErrorContext, |
| 770 | "unable to connect to '%s'.\n", host); |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 771 | #endif |
| 772 | return(-1); |
| 773 | } |
| 774 | |
| 775 | |
| 776 | /** |
| 777 | * xmlNanoHTTPOpen: |
| 778 | * @URL: The URL to load |
| 779 | * @contentType: if available the Content-Type information will be |
| 780 | * returned at that location |
| 781 | * |
| 782 | * This function try to open a connection to the indicated resource |
| 783 | * via HTTP GET. |
| 784 | * |
| 785 | * Returns NULL in case of failure, otherwise a request handler. |
| 786 | * The contentType, if provided must be freed by the caller |
| 787 | */ |
| 788 | |
Daniel Veillard | 0604743 | 2000-04-24 11:33:38 +0000 | [diff] [blame] | 789 | void* |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 790 | xmlNanoHTTPOpen(const char *URL, char **contentType) { |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 791 | if (contentType != NULL) *contentType = NULL; |
Daniel Veillard | be9ec4b | 2000-10-25 11:01:53 +0000 | [diff] [blame] | 792 | return xmlNanoHTTPMethod(URL, NULL, NULL, contentType, NULL); |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | /** |
| 796 | * xmlNanoHTTPRead: |
| 797 | * @ctx: the HTTP context |
| 798 | * @dest: a buffer |
| 799 | * @len: the buffer length |
| 800 | * |
| 801 | * This function tries to read @len bytes from the existing HTTP connection |
| 802 | * and saves them in @dest. This is a blocking call. |
| 803 | * |
| 804 | * Returns the number of byte read. 0 is an indication of an end of connection. |
| 805 | * -1 indicates a parameter error. |
| 806 | */ |
| 807 | int |
| 808 | xmlNanoHTTPRead(void *ctx, void *dest, int len) { |
| 809 | xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx; |
| 810 | |
| 811 | if (ctx == NULL) return(-1); |
| 812 | if (dest == NULL) return(-1); |
| 813 | if (len <= 0) return(0); |
| 814 | |
| 815 | while (ctxt->inptr - ctxt->inrptr < len) { |
| 816 | if (xmlNanoHTTPRecv(ctxt) == 0) break; |
| 817 | } |
| 818 | if (ctxt->inptr - ctxt->inrptr < len) |
| 819 | len = ctxt->inptr - ctxt->inrptr; |
| 820 | memcpy(dest, ctxt->inrptr, len); |
| 821 | ctxt->inrptr += len; |
| 822 | return(len); |
| 823 | } |
| 824 | |
| 825 | /** |
| 826 | * xmlNanoHTTPClose: |
| 827 | * @ctx: the HTTP context |
| 828 | * |
| 829 | * This function closes an HTTP context, it ends up the connection and |
| 830 | * free all data related to it. |
| 831 | */ |
| 832 | void |
| 833 | xmlNanoHTTPClose(void *ctx) { |
| 834 | xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx; |
| 835 | |
| 836 | if (ctx == NULL) return; |
| 837 | |
| 838 | xmlNanoHTTPFreeCtxt(ctxt); |
| 839 | } |
| 840 | |
| 841 | /** |
| 842 | * xmlNanoHTTPMethod: |
| 843 | * @URL: The URL to load |
| 844 | * @method: the HTTP method to use |
| 845 | * @input: the input string if any |
| 846 | * @contentType: the Content-Type information IN and OUT |
| 847 | * @headers: the extra headers |
| 848 | * |
| 849 | * This function try to open a connection to the indicated resource |
| 850 | * via HTTP using the given @method, adding the given extra headers |
| 851 | * and the input buffer for the request content. |
| 852 | * |
| 853 | * Returns NULL in case of failure, otherwise a request handler. |
| 854 | * The contentType, if provided must be freed by the caller |
| 855 | */ |
| 856 | |
Daniel Veillard | 0604743 | 2000-04-24 11:33:38 +0000 | [diff] [blame] | 857 | void* |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 858 | xmlNanoHTTPMethod(const char *URL, const char *method, const char *input, |
| 859 | char **contentType, const char *headers) { |
| 860 | xmlNanoHTTPCtxtPtr ctxt; |
Daniel Veillard | be9ec4b | 2000-10-25 11:01:53 +0000 | [diff] [blame] | 861 | char *bp, *p; |
| 862 | int blen, ilen, ret; |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 863 | int head; |
| 864 | int nbRedirects = 0; |
| 865 | char *redirURL = NULL; |
| 866 | |
| 867 | if (URL == NULL) return(NULL); |
| 868 | if (method == NULL) method = "GET"; |
Daniel Veillard | be9ec4b | 2000-10-25 11:01:53 +0000 | [diff] [blame] | 869 | xmlNanoHTTPInit(); |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 870 | |
| 871 | retry: |
| 872 | if (redirURL == NULL) |
| 873 | ctxt = xmlNanoHTTPNewCtxt(URL); |
| 874 | else { |
| 875 | ctxt = xmlNanoHTTPNewCtxt(redirURL); |
| 876 | xmlFree(redirURL); |
| 877 | redirURL = NULL; |
| 878 | } |
| 879 | |
| 880 | if ((ctxt->protocol == NULL) || (strcmp(ctxt->protocol, "http"))) { |
| 881 | xmlNanoHTTPFreeCtxt(ctxt); |
| 882 | if (redirURL != NULL) xmlFree(redirURL); |
| 883 | return(NULL); |
| 884 | } |
| 885 | if (ctxt->hostname == NULL) { |
| 886 | xmlNanoHTTPFreeCtxt(ctxt); |
| 887 | return(NULL); |
| 888 | } |
Daniel Veillard | be9ec4b | 2000-10-25 11:01:53 +0000 | [diff] [blame] | 889 | if (proxy) { |
| 890 | blen = strlen(ctxt->hostname) * 2 + 16; |
| 891 | ret = xmlNanoHTTPConnectHost(proxy, proxyPort); |
| 892 | } |
| 893 | else { |
| 894 | blen = strlen(ctxt->hostname); |
| 895 | ret = xmlNanoHTTPConnectHost(ctxt->hostname, ctxt->port); |
| 896 | } |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 897 | if (ret < 0) { |
| 898 | xmlNanoHTTPFreeCtxt(ctxt); |
| 899 | return(NULL); |
| 900 | } |
| 901 | ctxt->fd = ret; |
| 902 | |
Daniel Veillard | be9ec4b | 2000-10-25 11:01:53 +0000 | [diff] [blame] | 903 | if (input != NULL) { |
| 904 | ilen = strlen(input); |
| 905 | blen += ilen + 32; |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 906 | } |
Daniel Veillard | be9ec4b | 2000-10-25 11:01:53 +0000 | [diff] [blame] | 907 | else |
| 908 | ilen = 0; |
| 909 | if (headers != NULL) |
| 910 | blen += strlen(headers); |
| 911 | if (contentType && *contentType) |
| 912 | blen += strlen(*contentType) + 16; |
| 913 | blen += strlen(method) + strlen(ctxt->path) + 23; |
| 914 | bp = xmlMalloc(blen); |
| 915 | if (proxy) { |
| 916 | if (ctxt->port != 80) { |
| 917 | sprintf(bp, "%s http://%s:%d%s", method, ctxt->hostname, |
| 918 | ctxt->port, ctxt->path); |
| 919 | } |
| 920 | else |
| 921 | sprintf(bp, "%s http://%s%s", method, ctxt->hostname, ctxt->path); |
| 922 | } |
| 923 | else |
| 924 | sprintf(bp, "%s %s", method, ctxt->path); |
| 925 | p = bp + strlen(bp); |
| 926 | sprintf(p, " HTTP/1.0\r\nHost: %s\r\n", ctxt->hostname); |
| 927 | p += strlen(p); |
| 928 | if (contentType != NULL && *contentType) { |
| 929 | sprintf(p, "Content-Type: %s\r\n", *contentType); |
| 930 | p += strlen(p); |
| 931 | } |
| 932 | if (headers != NULL) { |
| 933 | strcpy(p, headers); |
| 934 | p += strlen(p); |
| 935 | } |
| 936 | if (input != NULL) |
| 937 | sprintf(p, "Content-Length: %d\r\n\r\n%s", ilen, input); |
| 938 | else |
| 939 | strcpy(p, "\r\n"); |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 940 | #ifdef DEBUG_HTTP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 941 | xmlGenericError(xmlGenericErrorContext, |
| 942 | "-> %s%s", proxy? "(Proxy) " : "", bp); |
Daniel Veillard | be9ec4b | 2000-10-25 11:01:53 +0000 | [diff] [blame] | 943 | if ((blen -= strlen(bp)+1) < 0) |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 944 | xmlGenericError(xmlGenericErrorContext, |
| 945 | "ERROR: overflowed buffer by %d bytes\n", -blen); |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 946 | #endif |
Daniel Veillard | be9ec4b | 2000-10-25 11:01:53 +0000 | [diff] [blame] | 947 | ctxt->outptr = ctxt->out = bp; |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 948 | ctxt->state = XML_NANO_HTTP_WRITE; |
| 949 | xmlNanoHTTPSend(ctxt); |
| 950 | ctxt->state = XML_NANO_HTTP_READ; |
| 951 | head = 1; |
| 952 | |
| 953 | while ((p = xmlNanoHTTPReadLine(ctxt)) != NULL) { |
| 954 | if (head && (*p == 0)) { |
| 955 | head = 0; |
| 956 | ctxt->content = ctxt->inrptr; |
Daniel Veillard | be9ec4b | 2000-10-25 11:01:53 +0000 | [diff] [blame] | 957 | xmlFree(p); |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 958 | break; |
| 959 | } |
| 960 | xmlNanoHTTPScanAnswer(ctxt, p); |
| 961 | |
| 962 | #ifdef DEBUG_HTTP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 963 | xmlGenericError(xmlGenericErrorContext, "<- %s\n", p); |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 964 | #endif |
Daniel Veillard | be9ec4b | 2000-10-25 11:01:53 +0000 | [diff] [blame] | 965 | xmlFree(p); |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | if ((ctxt->location != NULL) && (ctxt->returnValue >= 300) && |
| 969 | (ctxt->returnValue < 400)) { |
| 970 | #ifdef DEBUG_HTTP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 971 | xmlGenericError(xmlGenericErrorContext, |
| 972 | "\nRedirect to: %s\n", ctxt->location); |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 973 | #endif |
| 974 | while (xmlNanoHTTPRecv(ctxt)) ; |
| 975 | if (nbRedirects < XML_NANO_HTTP_MAX_REDIR) { |
| 976 | nbRedirects++; |
| 977 | redirURL = xmlMemStrdup(ctxt->location); |
| 978 | xmlNanoHTTPFreeCtxt(ctxt); |
| 979 | goto retry; |
| 980 | } |
| 981 | xmlNanoHTTPFreeCtxt(ctxt); |
| 982 | #ifdef DEBUG_HTTP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 983 | xmlGenericError(xmlGenericErrorContext, |
| 984 | "Too many redirects, aborting ...\n"); |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 985 | #endif |
| 986 | return(NULL); |
| 987 | |
| 988 | } |
| 989 | |
Daniel Veillard | be9ec4b | 2000-10-25 11:01:53 +0000 | [diff] [blame] | 990 | if (contentType != NULL) { |
| 991 | if (ctxt->contentType != NULL) |
| 992 | *contentType = xmlMemStrdup(ctxt->contentType); |
| 993 | else |
| 994 | *contentType = NULL; |
| 995 | } |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 996 | |
| 997 | #ifdef DEBUG_HTTP |
| 998 | if (ctxt->contentType != NULL) |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 999 | xmlGenericError(xmlGenericErrorContext, |
| 1000 | "\nCode %d, content-type '%s'\n\n", |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 1001 | ctxt->returnValue, ctxt->contentType); |
| 1002 | else |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1003 | xmlGenericError(xmlGenericErrorContext, |
| 1004 | "\nCode %d, no content-type\n\n", |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 1005 | ctxt->returnValue); |
| 1006 | #endif |
| 1007 | |
| 1008 | return((void *) ctxt); |
| 1009 | } |
| 1010 | |
| 1011 | /** |
| 1012 | * xmlNanoHTTPFetch: |
| 1013 | * @URL: The URL to load |
| 1014 | * @filename: the filename where the content should be saved |
| 1015 | * @contentType: if available the Content-Type information will be |
| 1016 | * returned at that location |
| 1017 | * |
| 1018 | * This function try to fetch the indicated resource via HTTP GET |
| 1019 | * and save it's content in the file. |
| 1020 | * |
| 1021 | * Returns -1 in case of failure, 0 incase of success. The contentType, |
| 1022 | * if provided must be freed by the caller |
| 1023 | */ |
| 1024 | int |
| 1025 | xmlNanoHTTPFetch(const char *URL, const char *filename, char **contentType) { |
| 1026 | void *ctxt; |
| 1027 | char buf[4096]; |
| 1028 | int fd; |
| 1029 | int len; |
| 1030 | |
| 1031 | ctxt = xmlNanoHTTPOpen(URL, contentType); |
| 1032 | if (ctxt == NULL) return(-1); |
| 1033 | |
| 1034 | if (!strcmp(filename, "-")) |
| 1035 | fd = 0; |
| 1036 | else { |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 1037 | fd = open(filename, O_CREAT | O_WRONLY, 00644); |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 1038 | if (fd < 0) { |
| 1039 | xmlNanoHTTPClose(ctxt); |
| 1040 | if ((contentType != NULL) && (*contentType != NULL)) { |
| 1041 | xmlFree(*contentType); |
| 1042 | *contentType = NULL; |
| 1043 | } |
| 1044 | return(-1); |
| 1045 | } |
| 1046 | } |
| 1047 | |
| 1048 | while ((len = xmlNanoHTTPRead(ctxt, buf, sizeof(buf))) > 0) { |
| 1049 | write(fd, buf, len); |
| 1050 | } |
| 1051 | |
| 1052 | xmlNanoHTTPClose(ctxt); |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 1053 | close(fd); |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 1054 | return(0); |
| 1055 | } |
| 1056 | |
| 1057 | /** |
| 1058 | * xmlNanoHTTPSave: |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 1059 | * @ctxt: the HTTP context |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 1060 | * @filename: the filename where the content should be saved |
| 1061 | * |
| 1062 | * This function saves the output of the HTTP transaction to a file |
| 1063 | * It closes and free the context at the end |
| 1064 | * |
| 1065 | * Returns -1 in case of failure, 0 incase of success. |
| 1066 | */ |
| 1067 | int |
| 1068 | xmlNanoHTTPSave(void *ctxt, const char *filename) { |
| 1069 | char buf[4096]; |
| 1070 | int fd; |
| 1071 | int len; |
| 1072 | |
| 1073 | if (ctxt == NULL) return(-1); |
| 1074 | |
| 1075 | if (!strcmp(filename, "-")) |
| 1076 | fd = 0; |
| 1077 | else { |
| 1078 | fd = open(filename, O_CREAT | O_WRONLY); |
| 1079 | if (fd < 0) { |
| 1080 | xmlNanoHTTPClose(ctxt); |
| 1081 | return(-1); |
| 1082 | } |
| 1083 | } |
| 1084 | |
| 1085 | while ((len = xmlNanoHTTPRead(ctxt, buf, sizeof(buf))) > 0) { |
| 1086 | write(fd, buf, len); |
| 1087 | } |
| 1088 | |
| 1089 | xmlNanoHTTPClose(ctxt); |
| 1090 | return(0); |
| 1091 | } |
| 1092 | |
| 1093 | /** |
| 1094 | * xmlNanoHTTPReturnCode: |
| 1095 | * @ctx: the HTTP context |
| 1096 | * |
| 1097 | * Returns the HTTP return code for the request. |
| 1098 | */ |
| 1099 | int |
| 1100 | xmlNanoHTTPReturnCode(void *ctx) { |
| 1101 | xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx; |
| 1102 | |
| 1103 | if (ctxt == NULL) return(-1); |
| 1104 | |
| 1105 | return(ctxt->returnValue); |
| 1106 | } |
| 1107 | |
| 1108 | #ifdef STANDALONE |
| 1109 | int main(int argc, char **argv) { |
| 1110 | char *contentType = NULL; |
| 1111 | |
| 1112 | if (argv[1] != NULL) { |
| 1113 | if (argv[2] != NULL) |
| 1114 | xmlNanoHTTPFetch(argv[1], argv[2], &contentType); |
| 1115 | else |
| 1116 | xmlNanoHTTPFetch(argv[1], "-", &contentType); |
| 1117 | if (contentType != NULL) xmlFree(contentType); |
| 1118 | } else { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1119 | xmlGenericError(xmlGenericErrorContext, |
| 1120 | "%s: minimal HTTP GET implementation\n", argv[0]); |
| 1121 | xmlGenericError(xmlGenericErrorContext, |
| 1122 | "\tusage %s [ URL [ filename ] ]\n", argv[0]); |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 1123 | } |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 1124 | xmlNanoHTTPCleanup(); |
| 1125 | xmlMemoryDump(); |
Daniel Veillard | 4ecf39f | 1999-09-22 12:14:03 +0000 | [diff] [blame] | 1126 | return(0); |
| 1127 | } |
| 1128 | #endif /* STANDALONE */ |
Daniel Veillard | 361d845 | 2000-04-03 19:48:13 +0000 | [diff] [blame] | 1129 | #else /* !LIBXML_HTTP_ENABLED */ |
| 1130 | #ifdef STANDALONE |
| 1131 | #include <stdio.h> |
| 1132 | int main(int argc, char **argv) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1133 | xmlGenericError(xmlGenericErrorContext, |
| 1134 | "%s : HTTP support not compiled in\n", argv[0]); |
Daniel Veillard | 361d845 | 2000-04-03 19:48:13 +0000 | [diff] [blame] | 1135 | return(0); |
| 1136 | } |
| 1137 | #endif /* STANDALONE */ |
| 1138 | #endif /* LIBXML_HTTP_ENABLED */ |