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