Annotation of XML/nanohttp.c, revision 1.34
1.1 daniel 1: /*
1.5 daniel 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/
1.1 daniel 8: *
9: * See Copyright for the status of this software.
10: *
11: * Daniel.Veillard@w3.org
12: */
13:
1.5 daniel 14: /* TODO add compression support, Send the Accept- , and decompress on the
15: fly with ZLIB if found at compile-time */
16:
1.9 daniel 17: #ifdef WIN32
1.11 daniel 18: #define INCLUDE_WINSOCK
1.9 daniel 19: #include "win32config.h"
20: #else
1.4 daniel 21: #include "config.h"
22: #endif
1.9 daniel 23:
1.25 veillard 24: #include <libxml/xmlversion.h>
1.4 daniel 25:
1.17 daniel 26: #ifdef LIBXML_HTTP_ENABLED
1.1 daniel 27: #include <stdio.h>
28: #include <string.h>
1.4 daniel 29:
30: #ifdef HAVE_STDLIB_H
1.1 daniel 31: #include <stdlib.h>
1.4 daniel 32: #endif
33: #ifdef HAVE_UNISTD_H
1.1 daniel 34: #include <unistd.h>
1.4 daniel 35: #endif
36: #ifdef HAVE_SYS_SOCKET_H
1.1 daniel 37: #include <sys/socket.h>
1.4 daniel 38: #endif
39: #ifdef HAVE_NETINET_IN_H
1.1 daniel 40: #include <netinet/in.h>
1.4 daniel 41: #endif
42: #ifdef HAVE_ARPA_INET_H
1.1 daniel 43: #include <arpa/inet.h>
1.4 daniel 44: #endif
45: #ifdef HAVE_NETDB_H
1.1 daniel 46: #include <netdb.h>
1.4 daniel 47: #endif
48: #ifdef HAVE_FCNTL_H
1.1 daniel 49: #include <fcntl.h>
1.4 daniel 50: #endif
51: #ifdef HAVE_ERRNO_H
1.1 daniel 52: #include <errno.h>
1.4 daniel 53: #endif
54: #ifdef HAVE_SYS_TIME_H
1.1 daniel 55: #include <sys/time.h>
1.4 daniel 56: #endif
57: #ifdef HAVE_SYS_SELECT_H
1.1 daniel 58: #include <sys/select.h>
1.4 daniel 59: #endif
1.15 daniel 60: #ifdef HAVE_STRINGS_H
61: #include <strings.h>
62: #endif
1.1 daniel 63:
1.17 daniel 64: #include <libxml/xmlmemory.h>
1.23 veillard 65: #include <libxml/parser.h> /* for xmlStr(n)casecmp() */
1.17 daniel 66: #include <libxml/nanohttp.h>
1.7 daniel 67:
1.30 veillard 68: /**
69: * A couple portability macros
70: */
71: #ifndef _WINSOCKAPI_
72: #define closesocket(s) close(s)
73: #define SOCKET int
74: #endif
75:
1.5 daniel 76: #ifdef STANDALONE
77: #define DEBUG_HTTP
1.23 veillard 78: #define xmlStrncasecmp(a, b, n) strncasecmp((char *)a, (char *)b, n)
79: #define xmlStrcasecmpi(a, b) strcasecmp((char *)a, (char *)b)
1.5 daniel 80: #endif
81:
1.1 daniel 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 */
1.30 veillard 96: SOCKET fd; /* the file descriptor for the socket */
1.1 daniel 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:
1.12 daniel 111: static int initialized = 0;
1.28 veillard 112: static char *proxy = NULL; /* the proxy name if any */
1.12 daniel 113: static int proxyPort; /* the proxy port if any */
1.28 veillard 114: static unsigned int timeout = 60;/* the select() timeout in seconds */
1.12 daniel 115:
116: /**
1.30 veillard 117: * A portability function
1.26 veillard 118: */
119: int socket_errno(void) {
120: #ifdef _WINSOCKAPI_
121: return(WSAGetLastError());
122: #else
123: return(errno);
124: #endif
125: }
126:
127: /**
1.12 daniel 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;
1.30 veillard 137: #ifdef _WINSOCKAPI_
138: WSADATA wsaData;
139: #endif
1.12 daniel 140:
141: if (initialized)
142: return;
143:
1.26 veillard 144: #ifdef _WINSOCKAPI_
1.30 veillard 145: if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0)
146: return;
1.26 veillard 147: #endif
148:
1.13 daniel 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: }
1.12 daniel 164: }
1.13 daniel 165: done:
1.12 daniel 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);
1.26 veillard 179: #ifdef _WINSOCKAPI_
1.30 veillard 180: if (initialized)
181: WSACleanup();
1.26 veillard 182: #endif
1.30 veillard 183: initialized = 0;
1.12 daniel 184: return;
185: }
186:
1.5 daniel 187: /**
1.28 veillard 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: /**
1.5 daniel 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) {
1.1 daniel 211: const char *cur = URL;
212: char buf[4096];
213: int index = 0;
214: int port = 0;
215:
216: if (ctxt->protocol != NULL) {
1.7 daniel 217: xmlFree(ctxt->protocol);
1.1 daniel 218: ctxt->protocol = NULL;
219: }
220: if (ctxt->hostname != NULL) {
1.7 daniel 221: xmlFree(ctxt->hostname);
1.1 daniel 222: ctxt->hostname = NULL;
223: }
224: if (ctxt->path != NULL) {
1.7 daniel 225: xmlFree(ctxt->path);
1.1 daniel 226: ctxt->path = NULL;
227: }
1.12 daniel 228: if (URL == NULL) return;
1.1 daniel 229: buf[index] = 0;
230: while (*cur != 0) {
231: if ((cur[0] == ':') && (cur[1] == '/') && (cur[2] == '/')) {
232: buf[index] = 0;
1.7 daniel 233: ctxt->protocol = xmlMemStrdup(buf);
1.1 daniel 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;
1.7 daniel 246: ctxt->hostname = xmlMemStrdup(buf);
1.1 daniel 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;
1.7 daniel 261: ctxt->hostname = xmlMemStrdup(buf);
1.1 daniel 262: index = 0;
263: break;
264: }
265: buf[index++] = *cur++;
266: }
267: if (*cur == 0)
1.7 daniel 268: ctxt->path = xmlMemStrdup("/");
1.5 daniel 269: else {
1.14 daniel 270: index = 0;
1.5 daniel 271: buf[index] = 0;
1.14 daniel 272: while (*cur != 0)
1.5 daniel 273: buf[index++] = *cur++;
274: buf[index] = 0;
1.7 daniel 275: ctxt->path = xmlMemStrdup(buf);
1.5 daniel 276: }
1.1 daniel 277: }
278:
1.5 daniel 279: /**
1.12 daniel 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)
1.34 ! veillard 305: xmlGenericError(xmlGenericErrorContext,
! 306: "Removing HTTP proxy info\n");
1.12 daniel 307: else
1.34 ! veillard 308: xmlGenericError(xmlGenericErrorContext,
! 309: "Using HTTP proxy %s\n", URL);
1.12 daniel 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: /**
1.5 daniel 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) {
1.1 daniel 362: xmlNanoHTTPCtxtPtr ret;
363:
1.7 daniel 364: ret = (xmlNanoHTTPCtxtPtr) xmlMalloc(sizeof(xmlNanoHTTPCtxt));
1.1 daniel 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:
1.5 daniel 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;
1.7 daniel 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);
1.1 daniel 393: ctxt->state = XML_NANO_HTTP_NONE;
1.26 veillard 394: if (ctxt->fd >= 0) closesocket(ctxt->fd);
1.1 daniel 395: ctxt->fd = -1;
1.7 daniel 396: xmlFree(ctxt);
1.1 daniel 397: }
398:
1.5 daniel 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) {
1.31 veillard 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: }
1.1 daniel 419: }
420:
1.5 daniel 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) {
1.1 daniel 433: fd_set rfd;
434: struct timeval tv;
435:
436:
437: while (ctxt->state & XML_NANO_HTTP_READ) {
438: if (ctxt->in == NULL) {
1.7 daniel 439: ctxt->in = (char *) xmlMalloc(65000 * sizeof(char));
1.1 daniel 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;
1.7 daniel 462: ctxt->in = (char *) xmlRealloc(ctxt->in, ctxt->inlen);
1.1 daniel 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: }
1.26 veillard 471: ctxt->last = recv(ctxt->fd, ctxt->inptr, XML_NANO_HTTP_CHUNK, 0);
1.1 daniel 472: if (ctxt->last > 0) {
473: ctxt->inptr += ctxt->last;
474: return(ctxt->last);
475: }
476: if (ctxt->last == 0) {
477: return(0);
478: }
1.26 veillard 479: if (ctxt->last == -1) {
480: switch (socket_errno()) {
481: case EINPROGRESS:
482: case EWOULDBLOCK:
483: #if defined(EAGAIN) && EAGAIN != EWOULDBLOCK
484: case EAGAIN:
485: #endif
486: break;
487: default:
488: return(0);
489: }
1.1 daniel 490: }
1.26 veillard 491:
1.28 veillard 492: tv.tv_sec = timeout;
1.26 veillard 493: tv.tv_usec = 0;
1.1 daniel 494: FD_ZERO(&rfd);
495: FD_SET(ctxt->fd, &rfd);
496:
1.26 veillard 497: if (select(ctxt->fd+1, &rfd, NULL, NULL, &tv)<1)
1.5 daniel 498: return(0);
1.1 daniel 499: }
500: return(0);
501: }
502:
1.5 daniel 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];
1.26 veillard 517: char *bp = buf;
1.1 daniel 518:
1.26 veillard 519: while (bp - buf < 4095) {
520: if (ctxt->inrptr == ctxt->inptr) {
1.1 daniel 521: if (xmlNanoHTTPRecv(ctxt) == 0) {
522: if (bp == buf)
1.5 daniel 523: return(NULL);
1.1 daniel 524: else
525: *bp = 0;
1.7 daniel 526: return(xmlMemStrdup(buf));
1.1 daniel 527: }
528: }
529: *bp = *ctxt->inrptr++;
1.26 veillard 530: if (*bp == '\n') {
1.1 daniel 531: *bp = 0;
1.7 daniel 532: return(xmlMemStrdup(buf));
1.1 daniel 533: }
1.26 veillard 534: if (*bp != '\r')
1.1 daniel 535: bp++;
536: }
537: buf[4095] = 0;
1.7 daniel 538: return(xmlMemStrdup(buf));
1.1 daniel 539: }
540:
1.5 daniel 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) {
1.1 daniel 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;
1.23 veillard 593: } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Content-Type:", 13)) {
1.1 daniel 594: cur += 13;
595: while ((*cur == ' ') || (*cur == '\t')) cur++;
596: if (ctxt->contentType != NULL)
1.7 daniel 597: xmlFree(ctxt->contentType);
598: ctxt->contentType = xmlMemStrdup(cur);
1.23 veillard 599: } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"ContentType:", 12)) {
1.1 daniel 600: cur += 12;
601: if (ctxt->contentType != NULL) return;
602: while ((*cur == ' ') || (*cur == '\t')) cur++;
1.7 daniel 603: ctxt->contentType = xmlMemStrdup(cur);
1.23 veillard 604: } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Location:", 9)) {
1.1 daniel 605: cur += 9;
606: while ((*cur == ' ') || (*cur == '\t')) cur++;
607: if (ctxt->location != NULL)
1.7 daniel 608: xmlFree(ctxt->location);
609: ctxt->location = xmlMemStrdup(cur);
1.1 daniel 610: }
611: }
612:
1.5 daniel 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)
1.1 daniel 627: {
1.30 veillard 628: SOCKET s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
1.1 daniel 629: struct sockaddr_in sin;
630: fd_set wfd;
631: struct timeval tv;
1.2 daniel 632: int status;
1.1 daniel 633:
1.26 veillard 634: if (s==-1) {
1.5 daniel 635: #ifdef DEBUG_HTTP
1.1 daniel 636: perror("socket");
1.5 daniel 637: #endif
1.1 daniel 638: return(-1);
639: }
640:
1.2 daniel 641: #ifdef _WINSOCKAPI_
642: {
643: u_long one = 1;
644:
1.3 daniel 645: status = ioctlsocket(s, FIONBIO, &one) == SOCKET_ERROR ? -1 : 0;
1.2 daniel 646: }
647: #else /* _WINSOCKAPI_ */
648: #if defined(VMS)
649: {
650: int enable = 1;
1.3 daniel 651: status = IOCTL(s, FIONBIO, &enable);
1.2 daniel 652: }
653: #else /* VMS */
1.26 veillard 654: if ((status = fcntl(s, F_GETFL, 0)) != -1) {
1.2 daniel 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 */
1.3 daniel 662: status = fcntl(s, F_SETFL, status);
1.2 daniel 663: }
1.26 veillard 664: if (status < 0) {
1.5 daniel 665: #ifdef DEBUG_HTTP
1.1 daniel 666: perror("nonblocking");
1.5 daniel 667: #endif
1.26 veillard 668: closesocket(s);
1.1 daniel 669: return(-1);
670: }
1.2 daniel 671: #endif /* !VMS */
672: #endif /* !_WINSOCKAPI_ */
673:
1.1 daniel 674:
675: sin.sin_family = AF_INET;
676: sin.sin_addr = ia;
677: sin.sin_port = htons(port);
678:
1.30 veillard 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: }
1.1 daniel 689: }
690:
1.28 veillard 691: tv.tv_sec = timeout;
1.1 daniel 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 */
1.26 veillard 701: closesocket(s);
1.1 daniel 702: return(-1);
703: case -1:
704: /* Ermm.. ?? */
1.5 daniel 705: #ifdef DEBUG_HTTP
1.1 daniel 706: perror("select");
1.5 daniel 707: #endif
1.26 veillard 708: closesocket(s);
1.1 daniel 709: return(-1);
710: }
1.19 daniel 711:
712: if ( FD_ISSET(s, &wfd) ) {
1.27 veillard 713: SOCKLEN_T len;
1.19 daniel 714: len = sizeof(status);
1.30 veillard 715: if (getsockopt(s, SOL_SOCKET, SO_ERROR, (char*)&status, &len) < 0 ) {
1.19 daniel 716: /* Solaris error code */
717: return (-1);
718: }
719: if ( status ) {
1.26 veillard 720: closesocket(s);
1.19 daniel 721: errno = status;
722: return (-1);
723: }
724: } else {
725: /* pbm */
726: return (-1);
727: }
1.1 daniel 728:
1.5 daniel 729: return(s);
1.1 daniel 730: }
731:
1.5 daniel 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)
1.1 daniel 745: {
746: struct hostent *h;
747: int i;
748: int s;
749:
750: h=gethostbyname(host);
1.26 veillard 751: if (h==NULL)
1.1 daniel 752: {
1.5 daniel 753: #ifdef DEBUG_HTTP
1.34 ! veillard 754: xmlGenericError(xmlGenericErrorContext,"unable to resolve '%s'.\n", host);
1.5 daniel 755: #endif
1.1 daniel 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);
1.26 veillard 764: if (s != -1)
1.5 daniel 765: return(s);
1.1 daniel 766: }
1.5 daniel 767:
768: #ifdef DEBUG_HTTP
1.34 ! veillard 769: xmlGenericError(xmlGenericErrorContext,
! 770: "unable to connect to '%s'.\n", host);
1.5 daniel 771: #endif
1.1 daniel 772: return(-1);
773: }
774:
775:
1.5 daniel 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: *
1.6 daniel 785: * Returns NULL in case of failure, otherwise a request handler.
786: * The contentType, if provided must be freed by the caller
1.5 daniel 787: */
1.1 daniel 788:
1.18 daniel 789: void*
1.1 daniel 790: xmlNanoHTTPOpen(const char *URL, char **contentType) {
1.5 daniel 791: if (contentType != NULL) *contentType = NULL;
1.33 veillard 792: return xmlNanoHTTPMethod(URL, NULL, NULL, contentType, NULL);
1.1 daniel 793: }
794:
1.5 daniel 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: */
1.1 daniel 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:
1.5 daniel 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: */
1.1 daniel 832: void
833: xmlNanoHTTPClose(void *ctx) {
834: xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
835:
836: if (ctx == NULL) return;
837:
838: xmlNanoHTTPFreeCtxt(ctxt);
839: }
840:
1.5 daniel 841: /**
1.6 daniel 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:
1.18 daniel 857: void*
1.6 daniel 858: xmlNanoHTTPMethod(const char *URL, const char *method, const char *input,
859: char **contentType, const char *headers) {
860: xmlNanoHTTPCtxtPtr ctxt;
1.33 veillard 861: char *bp, *p;
862: int blen, ilen, ret;
1.6 daniel 863: int head;
864: int nbRedirects = 0;
865: char *redirURL = NULL;
866:
867: if (URL == NULL) return(NULL);
868: if (method == NULL) method = "GET";
1.33 veillard 869: xmlNanoHTTPInit();
1.6 daniel 870:
871: retry:
872: if (redirURL == NULL)
873: ctxt = xmlNanoHTTPNewCtxt(URL);
874: else {
875: ctxt = xmlNanoHTTPNewCtxt(redirURL);
1.7 daniel 876: xmlFree(redirURL);
1.6 daniel 877: redirURL = NULL;
878: }
879:
880: if ((ctxt->protocol == NULL) || (strcmp(ctxt->protocol, "http"))) {
881: xmlNanoHTTPFreeCtxt(ctxt);
1.7 daniel 882: if (redirURL != NULL) xmlFree(redirURL);
1.6 daniel 883: return(NULL);
884: }
885: if (ctxt->hostname == NULL) {
886: xmlNanoHTTPFreeCtxt(ctxt);
887: return(NULL);
888: }
1.33 veillard 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: }
1.6 daniel 897: if (ret < 0) {
898: xmlNanoHTTPFreeCtxt(ctxt);
899: return(NULL);
900: }
901: ctxt->fd = ret;
902:
1.33 veillard 903: if (input != NULL) {
904: ilen = strlen(input);
905: blen += ilen + 32;
906: }
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);
1.6 daniel 919: }
1.33 veillard 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);
1.6 daniel 935: }
1.33 veillard 936: if (input != NULL)
937: sprintf(p, "Content-Length: %d\r\n\r\n%s", ilen, input);
938: else
939: strcpy(p, "\r\n");
1.6 daniel 940: #ifdef DEBUG_HTTP
1.34 ! veillard 941: xmlGenericError(xmlGenericErrorContext,
! 942: "-> %s%s", proxy? "(Proxy) " : "", bp);
1.33 veillard 943: if ((blen -= strlen(bp)+1) < 0)
1.34 ! veillard 944: xmlGenericError(xmlGenericErrorContext,
! 945: "ERROR: overflowed buffer by %d bytes\n", -blen);
1.6 daniel 946: #endif
1.33 veillard 947: ctxt->outptr = ctxt->out = bp;
1.6 daniel 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;
1.33 veillard 957: xmlFree(p);
1.6 daniel 958: break;
959: }
960: xmlNanoHTTPScanAnswer(ctxt, p);
961:
962: #ifdef DEBUG_HTTP
1.34 ! veillard 963: xmlGenericError(xmlGenericErrorContext, "<- %s\n", p);
1.6 daniel 964: #endif
1.33 veillard 965: xmlFree(p);
1.6 daniel 966: }
967:
968: if ((ctxt->location != NULL) && (ctxt->returnValue >= 300) &&
969: (ctxt->returnValue < 400)) {
970: #ifdef DEBUG_HTTP
1.34 ! veillard 971: xmlGenericError(xmlGenericErrorContext,
! 972: "\nRedirect to: %s\n", ctxt->location);
1.6 daniel 973: #endif
974: while (xmlNanoHTTPRecv(ctxt)) ;
975: if (nbRedirects < XML_NANO_HTTP_MAX_REDIR) {
976: nbRedirects++;
1.7 daniel 977: redirURL = xmlMemStrdup(ctxt->location);
1.6 daniel 978: xmlNanoHTTPFreeCtxt(ctxt);
979: goto retry;
980: }
981: xmlNanoHTTPFreeCtxt(ctxt);
982: #ifdef DEBUG_HTTP
1.34 ! veillard 983: xmlGenericError(xmlGenericErrorContext,
! 984: "Too many redirects, aborting ...\n");
1.6 daniel 985: #endif
986: return(NULL);
987:
988: }
989:
1.33 veillard 990: if (contentType != NULL) {
991: if (ctxt->contentType != NULL)
992: *contentType = xmlMemStrdup(ctxt->contentType);
993: else
994: *contentType = NULL;
995: }
1.6 daniel 996:
997: #ifdef DEBUG_HTTP
998: if (ctxt->contentType != NULL)
1.34 ! veillard 999: xmlGenericError(xmlGenericErrorContext,
! 1000: "\nCode %d, content-type '%s'\n\n",
1.6 daniel 1001: ctxt->returnValue, ctxt->contentType);
1002: else
1.34 ! veillard 1003: xmlGenericError(xmlGenericErrorContext,
! 1004: "\nCode %d, no content-type\n\n",
1.6 daniel 1005: ctxt->returnValue);
1006: #endif
1007:
1008: return((void *) ctxt);
1009: }
1010:
1011: /**
1.5 daniel 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) {
1.1 daniel 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 {
1.13 daniel 1037: fd = open(filename, O_CREAT | O_WRONLY, 00644);
1.1 daniel 1038: if (fd < 0) {
1039: xmlNanoHTTPClose(ctxt);
1.5 daniel 1040: if ((contentType != NULL) && (*contentType != NULL)) {
1.7 daniel 1041: xmlFree(*contentType);
1.5 daniel 1042: *contentType = NULL;
1043: }
1.1 daniel 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);
1.13 daniel 1053: close(fd);
1.1 daniel 1054: return(0);
1.6 daniel 1055: }
1056:
1057: /**
1058: * xmlNanoHTTPSave:
1.8 daniel 1059: * @ctxt: the HTTP context
1.6 daniel 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);
1.1 daniel 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);
1.7 daniel 1117: if (contentType != NULL) xmlFree(contentType);
1.1 daniel 1118: } else {
1.34 ! veillard 1119: xmlGenericError(xmlGenericErrorContext,
! 1120: "%s: minimal HTTP GET implementation\n", argv[0]);
! 1121: xmlGenericError(xmlGenericErrorContext,
! 1122: "\tusage %s [ URL [ filename ] ]\n", argv[0]);
1.1 daniel 1123: }
1.12 daniel 1124: xmlNanoHTTPCleanup();
1125: xmlMemoryDump();
1.1 daniel 1126: return(0);
1127: }
1128: #endif /* STANDALONE */
1.17 daniel 1129: #else /* !LIBXML_HTTP_ENABLED */
1130: #ifdef STANDALONE
1131: #include <stdio.h>
1132: int main(int argc, char **argv) {
1.34 ! veillard 1133: xmlGenericError(xmlGenericErrorContext,
! 1134: "%s : HTTP support not compiled in\n", argv[0]);
1.17 daniel 1135: return(0);
1136: }
1137: #endif /* STANDALONE */
1138: #endif /* LIBXML_HTTP_ENABLED */
Webmaster