blob: a92a9b31ef32877544a72ea20b0d8882afd1ce98 [file] [log] [blame]
Daniel Veillard4ecf39f1999-09-22 12:14:03 +00001/*
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 Veillard3c558c31999-12-22 11:30:41 +000017#ifdef WIN32
Daniel Veillard0142b842000-01-14 14:45:24 +000018#define INCLUDE_WINSOCK
Daniel Veillard3c558c31999-12-22 11:30:41 +000019#include "win32config.h"
20#else
Daniel Veillard4ecf39f1999-09-22 12:14:03 +000021#include "config.h"
22#endif
23
Daniel Veillardb71379b2000-10-09 12:30:39 +000024#include <libxml/xmlversion.h>
Daniel Veillard3c558c31999-12-22 11:30:41 +000025
Daniel Veillard361d8452000-04-03 19:48:13 +000026#ifdef LIBXML_HTTP_ENABLED
Daniel Veillard4ecf39f1999-09-22 12:14:03 +000027#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 Veillard5feb8492000-02-02 17:15:36 +000060#ifdef HAVE_STRINGS_H
61#include <strings.h>
62#endif
Daniel Veillard4ecf39f1999-09-22 12:14:03 +000063
Daniel Veillard361d8452000-04-03 19:48:13 +000064#include <libxml/xmlmemory.h>
Daniel Veillardb656ebe2000-09-22 13:51:48 +000065#include <libxml/parser.h> /* for xmlStr(n)casecmp() */
Daniel Veillard361d8452000-04-03 19:48:13 +000066#include <libxml/nanohttp.h>
Daniel Veillard4ecf39f1999-09-22 12:14:03 +000067
Daniel Veillard2f971a22000-10-12 23:26:32 +000068/**
69 * A couple portability macros
70 */
71#ifndef _WINSOCKAPI_
72#define closesocket(s) close(s)
73#define SOCKET int
74#endif
75
Daniel Veillard4ecf39f1999-09-22 12:14:03 +000076#ifdef STANDALONE
77#define DEBUG_HTTP
Daniel Veillardb656ebe2000-09-22 13:51:48 +000078#define xmlStrncasecmp(a, b, n) strncasecmp((char *)a, (char *)b, n)
Daniel Veillard8ddb5a72000-09-23 10:28:52 +000079#define xmlStrcasecmpi(a, b) strcasecmp((char *)a, (char *)b)
Daniel Veillard4ecf39f1999-09-22 12:14:03 +000080#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
91typedef 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 Veillard2f971a22000-10-12 23:26:32 +000096 SOCKET fd; /* the file descriptor for the socket */
Daniel Veillard4ecf39f1999-09-22 12:14:03 +000097 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 Veillarde41f2b72000-01-30 20:00:07 +0000111static int initialized = 0;
Daniel Veillard19d61112000-10-11 23:50:35 +0000112static char *proxy = NULL; /* the proxy name if any */
Daniel Veillarde41f2b72000-01-30 20:00:07 +0000113static int proxyPort; /* the proxy port if any */
Daniel Veillard19d61112000-10-11 23:50:35 +0000114static unsigned int timeout = 60;/* the select() timeout in seconds */
Daniel Veillarde41f2b72000-01-30 20:00:07 +0000115
116/**
Daniel Veillard2f971a22000-10-12 23:26:32 +0000117 * A portability function
Daniel Veillarde8282ed2000-10-10 23:01:31 +0000118 */
Daniel Veillarde8282ed2000-10-10 23:01:31 +0000119int socket_errno(void) {
120#ifdef _WINSOCKAPI_
121 return(WSAGetLastError());
122#else
123 return(errno);
124#endif
125}
126
127/**
Daniel Veillarde41f2b72000-01-30 20:00:07 +0000128 * xmlNanoHTTPInit:
129 *
130 * Initialize the HTTP protocol layer.
131 * Currently it just checks for proxy informations
132 */
133
134void
135xmlNanoHTTPInit(void) {
136 const char *env;
Daniel Veillard2f971a22000-10-12 23:26:32 +0000137#ifdef _WINSOCKAPI_
138 WSADATA wsaData;
139#endif
Daniel Veillarde41f2b72000-01-30 20:00:07 +0000140
141 if (initialized)
142 return;
143
Daniel Veillarde8282ed2000-10-10 23:01:31 +0000144#ifdef _WINSOCKAPI_
Daniel Veillard2f971a22000-10-12 23:26:32 +0000145 if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0)
146 return;
Daniel Veillarde8282ed2000-10-10 23:01:31 +0000147#endif
148
Daniel Veillarde41f2b72000-01-30 20:00:07 +0000149 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 }
165done:
166 initialized = 1;
167}
168
169/**
170 * xmlNanoHTTPClenup:
171 *
172 * Cleanup the HTTP protocol layer.
173 */
174
175void
176xmlNanoHTTPCleanup(void) {
177 if (proxy != NULL)
178 xmlFree(proxy);
Daniel Veillarde8282ed2000-10-10 23:01:31 +0000179#ifdef _WINSOCKAPI_
Daniel Veillard2f971a22000-10-12 23:26:32 +0000180 if (initialized)
181 WSACleanup();
Daniel Veillarde8282ed2000-10-10 23:01:31 +0000182#endif
Daniel Veillard2f971a22000-10-12 23:26:32 +0000183 initialized = 0;
Daniel Veillarde41f2b72000-01-30 20:00:07 +0000184 return;
185}
186
Daniel Veillard4ecf39f1999-09-22 12:14:03 +0000187/**
Daniel Veillard19d61112000-10-11 23:50:35 +0000188 * 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
195void
196xmlNanoHTTPTimeout(int delay) {
197 timeout = (unsigned int) delay;
198}
199
200/**
Daniel Veillard4ecf39f1999-09-22 12:14:03 +0000201 * 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
209static void
210xmlNanoHTTPScanURL(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 Veillarde41f2b72000-01-30 20:00:07 +0000228 if (URL == NULL) return;
Daniel Veillard4ecf39f1999-09-22 12:14:03 +0000229 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 Veillard726e8792000-01-30 20:04:29 +0000270 index = 0;
Daniel Veillard4ecf39f1999-09-22 12:14:03 +0000271 buf[index] = 0;
Daniel Veillard726e8792000-01-30 20:04:29 +0000272 while (*cur != 0)
Daniel Veillard4ecf39f1999-09-22 12:14:03 +0000273 buf[index++] = *cur++;
Daniel Veillard4ecf39f1999-09-22 12:14:03 +0000274 buf[index] = 0;
275 ctxt->path = xmlMemStrdup(buf);
276 }
277}
278
279/**
Daniel Veillarde41f2b72000-01-30 20:00:07 +0000280 * 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
289void
290xmlNanoHTTPScanProxy(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 Veillardd6d7f7b2000-10-25 19:56:55 +0000305 xmlGenericError(xmlGenericErrorContext,
306 "Removing HTTP proxy info\n");
Daniel Veillarde41f2b72000-01-30 20:00:07 +0000307 else
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000308 xmlGenericError(xmlGenericErrorContext,
309 "Using HTTP proxy %s\n", URL);
Daniel Veillarde41f2b72000-01-30 20:00:07 +0000310#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 Veillard4ecf39f1999-09-22 12:14:03 +0000352 * 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
360static xmlNanoHTTPCtxtPtr
361xmlNanoHTTPNewCtxt(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
383static void
384xmlNanoHTTPFreeCtxt(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 Veillarde8282ed2000-10-10 23:01:31 +0000394 if (ctxt->fd >= 0) closesocket(ctxt->fd);
Daniel Veillard4ecf39f1999-09-22 12:14:03 +0000395 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
406static void
407xmlNanoHTTPSend(xmlNanoHTTPCtxtPtr ctxt) {
Daniel Veillard683cb022000-10-22 12:04:13 +0000408 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 Veillard4ecf39f1999-09-22 12:14:03 +0000419}
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
431static int
432xmlNanoHTTPRecv(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 Veillarde8282ed2000-10-10 23:01:31 +0000471 ctxt->last = recv(ctxt->fd, ctxt->inptr, XML_NANO_HTTP_CHUNK, 0);
Daniel Veillard4ecf39f1999-09-22 12:14:03 +0000472 if (ctxt->last > 0) {
473 ctxt->inptr += ctxt->last;
474 return(ctxt->last);
475 }
476 if (ctxt->last == 0) {
477 return(0);
478 }
Daniel Veillarde8282ed2000-10-10 23:01:31 +0000479 if (ctxt->last == -1) {
480 switch (socket_errno()) {
481 case EINPROGRESS:
482 case EWOULDBLOCK:
483#if defined(EAGAIN) && EAGAIN != EWOULDBLOCK
484 case EAGAIN:
Daniel Veillard4ecf39f1999-09-22 12:14:03 +0000485#endif
Daniel Veillarde8282ed2000-10-10 23:01:31 +0000486 break;
487 default:
488 return(0);
489 }
490 }
491
Daniel Veillard19d61112000-10-11 23:50:35 +0000492 tv.tv_sec = timeout;
Daniel Veillarde8282ed2000-10-10 23:01:31 +0000493 tv.tv_usec = 0;
Daniel Veillard4ecf39f1999-09-22 12:14:03 +0000494 FD_ZERO(&rfd);
495 FD_SET(ctxt->fd, &rfd);
496
Daniel Veillarde8282ed2000-10-10 23:01:31 +0000497 if (select(ctxt->fd+1, &rfd, NULL, NULL, &tv)<1)
Daniel Veillard4ecf39f1999-09-22 12:14:03 +0000498 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
514static char *
515xmlNanoHTTPReadLine(xmlNanoHTTPCtxtPtr ctxt) {
516 char buf[4096];
Daniel Veillarde8282ed2000-10-10 23:01:31 +0000517 char *bp = buf;
Daniel Veillard4ecf39f1999-09-22 12:14:03 +0000518
Daniel Veillarde8282ed2000-10-10 23:01:31 +0000519 while (bp - buf < 4095) {
520 if (ctxt->inrptr == ctxt->inptr) {
Daniel Veillard4ecf39f1999-09-22 12:14:03 +0000521 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 Veillarde8282ed2000-10-10 23:01:31 +0000530 if (*bp == '\n') {
Daniel Veillard4ecf39f1999-09-22 12:14:03 +0000531 *bp = 0;
532 return(xmlMemStrdup(buf));
533 }
Daniel Veillarde8282ed2000-10-10 23:01:31 +0000534 if (*bp != '\r')
Daniel Veillard4ecf39f1999-09-22 12:14:03 +0000535 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
556static void
557xmlNanoHTTPScanAnswer(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 Veillardb656ebe2000-09-22 13:51:48 +0000593 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Content-Type:", 13)) {
Daniel Veillard4ecf39f1999-09-22 12:14:03 +0000594 cur += 13;
595 while ((*cur == ' ') || (*cur == '\t')) cur++;
596 if (ctxt->contentType != NULL)
597 xmlFree(ctxt->contentType);
598 ctxt->contentType = xmlMemStrdup(cur);
Daniel Veillardb656ebe2000-09-22 13:51:48 +0000599 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"ContentType:", 12)) {
Daniel Veillard4ecf39f1999-09-22 12:14:03 +0000600 cur += 12;
601 if (ctxt->contentType != NULL) return;
602 while ((*cur == ' ') || (*cur == '\t')) cur++;
603 ctxt->contentType = xmlMemStrdup(cur);
Daniel Veillardb656ebe2000-09-22 13:51:48 +0000604 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Location:", 9)) {
Daniel Veillard4ecf39f1999-09-22 12:14:03 +0000605 cur += 9;
606 while ((*cur == ' ') || (*cur == '\t')) cur++;
607 if (ctxt->location != NULL)
608 xmlFree(ctxt->location);
609 ctxt->location = xmlMemStrdup(cur);
Daniel Veillard4ecf39f1999-09-22 12:14:03 +0000610 }
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
625static int
626xmlNanoHTTPConnectAttempt(struct in_addr ia, int port)
627{
Daniel Veillard2f971a22000-10-12 23:26:32 +0000628 SOCKET s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
Daniel Veillard4ecf39f1999-09-22 12:14:03 +0000629 struct sockaddr_in sin;
630 fd_set wfd;
631 struct timeval tv;
632 int status;
633
Daniel Veillarde8282ed2000-10-10 23:01:31 +0000634 if (s==-1) {
Daniel Veillard4ecf39f1999-09-22 12:14:03 +0000635#ifdef DEBUG_HTTP
636 perror("socket");
637#endif
638 return(-1);
639 }
640
641#ifdef _WINSOCKAPI_
642 {
Daniel Veillard4ecf39f1999-09-22 12:14:03 +0000643 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 Veillarde8282ed2000-10-10 23:01:31 +0000654 if ((status = fcntl(s, F_GETFL, 0)) != -1) {
Daniel Veillard4ecf39f1999-09-22 12:14:03 +0000655#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 Veillarde8282ed2000-10-10 23:01:31 +0000664 if (status < 0) {
Daniel Veillard4ecf39f1999-09-22 12:14:03 +0000665#ifdef DEBUG_HTTP
666 perror("nonblocking");
667#endif
Daniel Veillarde8282ed2000-10-10 23:01:31 +0000668 closesocket(s);
Daniel Veillard4ecf39f1999-09-22 12:14:03 +0000669 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 Veillard2f971a22000-10-12 23:26:32 +0000679 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 Veillard4ecf39f1999-09-22 12:14:03 +0000689 }
690
Daniel Veillard19d61112000-10-11 23:50:35 +0000691 tv.tv_sec = timeout;
Daniel Veillard4ecf39f1999-09-22 12:14:03 +0000692 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 Veillarde8282ed2000-10-10 23:01:31 +0000701 closesocket(s);
Daniel Veillard4ecf39f1999-09-22 12:14:03 +0000702 return(-1);
703 case -1:
704 /* Ermm.. ?? */
705#ifdef DEBUG_HTTP
706 perror("select");
707#endif
Daniel Veillarde8282ed2000-10-10 23:01:31 +0000708 closesocket(s);
Daniel Veillard4ecf39f1999-09-22 12:14:03 +0000709 return(-1);
710 }
Daniel Veillardbe803962000-06-28 23:40:59 +0000711
712 if ( FD_ISSET(s, &wfd) ) {
Daniel Veillardb0426ca2000-10-11 23:39:43 +0000713 SOCKLEN_T len;
Daniel Veillardbe803962000-06-28 23:40:59 +0000714 len = sizeof(status);
Daniel Veillard2f971a22000-10-12 23:26:32 +0000715 if (getsockopt(s, SOL_SOCKET, SO_ERROR, (char*)&status, &len) < 0 ) {
Daniel Veillardbe803962000-06-28 23:40:59 +0000716 /* Solaris error code */
717 return (-1);
718 }
719 if ( status ) {
Daniel Veillarde8282ed2000-10-10 23:01:31 +0000720 closesocket(s);
Daniel Veillardbe803962000-06-28 23:40:59 +0000721 errno = status;
722 return (-1);
723 }
724 } else {
725 /* pbm */
726 return (-1);
727 }
Daniel Veillard4ecf39f1999-09-22 12:14:03 +0000728
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
743static int
744xmlNanoHTTPConnectHost(const char *host, int port)
745{
746 struct hostent *h;
747 int i;
748 int s;
749
750 h=gethostbyname(host);
Daniel Veillarde8282ed2000-10-10 23:01:31 +0000751 if (h==NULL)
Daniel Veillard4ecf39f1999-09-22 12:14:03 +0000752 {
753#ifdef DEBUG_HTTP
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000754 xmlGenericError(xmlGenericErrorContext,"unable to resolve '%s'.\n", host);
Daniel Veillard4ecf39f1999-09-22 12:14:03 +0000755#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 Veillarde8282ed2000-10-10 23:01:31 +0000764 if (s != -1)
Daniel Veillard4ecf39f1999-09-22 12:14:03 +0000765 return(s);
766 }
767
768#ifdef DEBUG_HTTP
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000769 xmlGenericError(xmlGenericErrorContext,
770 "unable to connect to '%s'.\n", host);
Daniel Veillard4ecf39f1999-09-22 12:14:03 +0000771#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 Veillard06047432000-04-24 11:33:38 +0000789void*
Daniel Veillard4ecf39f1999-09-22 12:14:03 +0000790xmlNanoHTTPOpen(const char *URL, char **contentType) {
Daniel Veillard4ecf39f1999-09-22 12:14:03 +0000791 if (contentType != NULL) *contentType = NULL;
Daniel Veillardbe9ec4b2000-10-25 11:01:53 +0000792 return xmlNanoHTTPMethod(URL, NULL, NULL, contentType, NULL);
Daniel Veillard4ecf39f1999-09-22 12:14:03 +0000793}
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 */
807int
808xmlNanoHTTPRead(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 */
832void
833xmlNanoHTTPClose(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 Veillard06047432000-04-24 11:33:38 +0000857void*
Daniel Veillard4ecf39f1999-09-22 12:14:03 +0000858xmlNanoHTTPMethod(const char *URL, const char *method, const char *input,
859 char **contentType, const char *headers) {
860 xmlNanoHTTPCtxtPtr ctxt;
Daniel Veillardbe9ec4b2000-10-25 11:01:53 +0000861 char *bp, *p;
862 int blen, ilen, ret;
Daniel Veillard4ecf39f1999-09-22 12:14:03 +0000863 int head;
864 int nbRedirects = 0;
865 char *redirURL = NULL;
866
867 if (URL == NULL) return(NULL);
868 if (method == NULL) method = "GET";
Daniel Veillardbe9ec4b2000-10-25 11:01:53 +0000869 xmlNanoHTTPInit();
Daniel Veillard4ecf39f1999-09-22 12:14:03 +0000870
871retry:
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 Veillardbe9ec4b2000-10-25 11:01:53 +0000889 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 Veillard4ecf39f1999-09-22 12:14:03 +0000897 if (ret < 0) {
898 xmlNanoHTTPFreeCtxt(ctxt);
899 return(NULL);
900 }
901 ctxt->fd = ret;
902
Daniel Veillardbe9ec4b2000-10-25 11:01:53 +0000903 if (input != NULL) {
904 ilen = strlen(input);
905 blen += ilen + 32;
Daniel Veillard4ecf39f1999-09-22 12:14:03 +0000906 }
Daniel Veillardbe9ec4b2000-10-25 11:01:53 +0000907 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 Veillard4ecf39f1999-09-22 12:14:03 +0000940#ifdef DEBUG_HTTP
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000941 xmlGenericError(xmlGenericErrorContext,
942 "-> %s%s", proxy? "(Proxy) " : "", bp);
Daniel Veillardbe9ec4b2000-10-25 11:01:53 +0000943 if ((blen -= strlen(bp)+1) < 0)
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000944 xmlGenericError(xmlGenericErrorContext,
945 "ERROR: overflowed buffer by %d bytes\n", -blen);
Daniel Veillard4ecf39f1999-09-22 12:14:03 +0000946#endif
Daniel Veillardbe9ec4b2000-10-25 11:01:53 +0000947 ctxt->outptr = ctxt->out = bp;
Daniel Veillard4ecf39f1999-09-22 12:14:03 +0000948 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 Veillardbe9ec4b2000-10-25 11:01:53 +0000957 xmlFree(p);
Daniel Veillard4ecf39f1999-09-22 12:14:03 +0000958 break;
959 }
960 xmlNanoHTTPScanAnswer(ctxt, p);
961
962#ifdef DEBUG_HTTP
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000963 xmlGenericError(xmlGenericErrorContext, "<- %s\n", p);
Daniel Veillard4ecf39f1999-09-22 12:14:03 +0000964#endif
Daniel Veillardbe9ec4b2000-10-25 11:01:53 +0000965 xmlFree(p);
Daniel Veillard4ecf39f1999-09-22 12:14:03 +0000966 }
967
968 if ((ctxt->location != NULL) && (ctxt->returnValue >= 300) &&
969 (ctxt->returnValue < 400)) {
970#ifdef DEBUG_HTTP
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000971 xmlGenericError(xmlGenericErrorContext,
972 "\nRedirect to: %s\n", ctxt->location);
Daniel Veillard4ecf39f1999-09-22 12:14:03 +0000973#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 Veillardd6d7f7b2000-10-25 19:56:55 +0000983 xmlGenericError(xmlGenericErrorContext,
984 "Too many redirects, aborting ...\n");
Daniel Veillard4ecf39f1999-09-22 12:14:03 +0000985#endif
986 return(NULL);
987
988 }
989
Daniel Veillardbe9ec4b2000-10-25 11:01:53 +0000990 if (contentType != NULL) {
991 if (ctxt->contentType != NULL)
992 *contentType = xmlMemStrdup(ctxt->contentType);
993 else
994 *contentType = NULL;
995 }
Daniel Veillard4ecf39f1999-09-22 12:14:03 +0000996
997#ifdef DEBUG_HTTP
998 if (ctxt->contentType != NULL)
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000999 xmlGenericError(xmlGenericErrorContext,
1000 "\nCode %d, content-type '%s'\n\n",
Daniel Veillard4ecf39f1999-09-22 12:14:03 +00001001 ctxt->returnValue, ctxt->contentType);
1002 else
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001003 xmlGenericError(xmlGenericErrorContext,
1004 "\nCode %d, no content-type\n\n",
Daniel Veillard4ecf39f1999-09-22 12:14:03 +00001005 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 */
1024int
1025xmlNanoHTTPFetch(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 Veillarde41f2b72000-01-30 20:00:07 +00001037 fd = open(filename, O_CREAT | O_WRONLY, 00644);
Daniel Veillard4ecf39f1999-09-22 12:14:03 +00001038 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 Veillarde41f2b72000-01-30 20:00:07 +00001053 close(fd);
Daniel Veillard4ecf39f1999-09-22 12:14:03 +00001054 return(0);
1055}
1056
1057/**
1058 * xmlNanoHTTPSave:
Daniel Veillard00fdf371999-10-08 09:40:39 +00001059 * @ctxt: the HTTP context
Daniel Veillard4ecf39f1999-09-22 12:14:03 +00001060 * @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 */
1067int
1068xmlNanoHTTPSave(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 */
1099int
1100xmlNanoHTTPReturnCode(void *ctx) {
1101 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1102
1103 if (ctxt == NULL) return(-1);
1104
1105 return(ctxt->returnValue);
1106}
1107
1108#ifdef STANDALONE
1109int 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 Veillardd6d7f7b2000-10-25 19:56:55 +00001119 xmlGenericError(xmlGenericErrorContext,
1120 "%s: minimal HTTP GET implementation\n", argv[0]);
1121 xmlGenericError(xmlGenericErrorContext,
1122 "\tusage %s [ URL [ filename ] ]\n", argv[0]);
Daniel Veillard4ecf39f1999-09-22 12:14:03 +00001123 }
Daniel Veillarde41f2b72000-01-30 20:00:07 +00001124 xmlNanoHTTPCleanup();
1125 xmlMemoryDump();
Daniel Veillard4ecf39f1999-09-22 12:14:03 +00001126 return(0);
1127}
1128#endif /* STANDALONE */
Daniel Veillard361d8452000-04-03 19:48:13 +00001129#else /* !LIBXML_HTTP_ENABLED */
1130#ifdef STANDALONE
1131#include <stdio.h>
1132int main(int argc, char **argv) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001133 xmlGenericError(xmlGenericErrorContext,
1134 "%s : HTTP support not compiled in\n", argv[0]);
Daniel Veillard361d8452000-04-03 19:48:13 +00001135 return(0);
1136}
1137#endif /* STANDALONE */
1138#endif /* LIBXML_HTTP_ENABLED */