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