blob: be92b32070c0bf6a971cd4e04236da92813e6f05 [file] [log] [blame]
Owen Taylor3473f882001-02-23 17:55:21 +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 *
Daniel Veillardc5d64342001-06-24 12:13:24 +000011 * daniel@veillard.com
Owen Taylor3473f882001-02-23 17:55:21 +000012 */
13
14/* TODO add compression support, Send the Accept- , and decompress on the
15 fly with ZLIB if found at compile-time */
16
Daniel Veillardf3afa7d2001-06-09 13:52:58 +000017#define NEED_SOCKETS
Bjorn Reese70a9da52001-04-21 16:57:29 +000018#include "libxml.h"
Owen Taylor3473f882001-02-23 17:55:21 +000019
20#ifdef LIBXML_HTTP_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +000021#include <string.h>
22
23#ifdef HAVE_STDLIB_H
24#include <stdlib.h>
25#endif
26#ifdef HAVE_UNISTD_H
27#include <unistd.h>
28#endif
29#ifdef HAVE_SYS_SOCKET_H
30#include <sys/socket.h>
31#endif
32#ifdef HAVE_NETINET_IN_H
33#include <netinet/in.h>
34#endif
35#ifdef HAVE_ARPA_INET_H
36#include <arpa/inet.h>
37#endif
38#ifdef HAVE_NETDB_H
39#include <netdb.h>
40#endif
41#ifdef HAVE_FCNTL_H
42#include <fcntl.h>
43#endif
44#ifdef HAVE_ERRNO_H
45#include <errno.h>
46#endif
47#ifdef HAVE_SYS_TIME_H
48#include <sys/time.h>
49#endif
50#ifdef HAVE_SYS_SELECT_H
51#include <sys/select.h>
52#endif
53#ifdef HAVE_STRINGS_H
54#include <strings.h>
55#endif
56#ifdef SUPPORT_IP6
57#include <resolv.h>
58#endif
59
60#ifdef VMS
61#include <stropts>
62#define SOCKLEN_T unsigned int
63#define SOCKET int
64#endif
65
Daniel Veillardd0463562001-10-13 09:15:48 +000066#include <libxml/globals.h>
Daniel Veillardf012a642001-07-23 19:10:52 +000067#include <libxml/xmlerror.h>
Owen Taylor3473f882001-02-23 17:55:21 +000068#include <libxml/xmlmemory.h>
69#include <libxml/parser.h> /* for xmlStr(n)casecmp() */
70#include <libxml/nanohttp.h>
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000071#include <libxml/globals.h>
Owen Taylor3473f882001-02-23 17:55:21 +000072
73/**
74 * A couple portability macros
75 */
76#ifndef _WINSOCKAPI_
77#define closesocket(s) close(s)
78#define SOCKET int
79#endif
80
Daniel Veillardf012a642001-07-23 19:10:52 +000081
Owen Taylor3473f882001-02-23 17:55:21 +000082#ifdef STANDALONE
83#define DEBUG_HTTP
84#define xmlStrncasecmp(a, b, n) strncasecmp((char *)a, (char *)b, n)
85#define xmlStrcasecmpi(a, b) strcasecmp((char *)a, (char *)b)
86#endif
87
88#define XML_NANO_HTTP_MAX_REDIR 10
89
90#define XML_NANO_HTTP_CHUNK 4096
91
92#define XML_NANO_HTTP_CLOSED 0
93#define XML_NANO_HTTP_WRITE 1
94#define XML_NANO_HTTP_READ 2
95#define XML_NANO_HTTP_NONE 4
96
97typedef struct xmlNanoHTTPCtxt {
98 char *protocol; /* the protocol name */
99 char *hostname; /* the host name */
100 int port; /* the port */
101 char *path; /* the path within the URL */
102 SOCKET fd; /* the file descriptor for the socket */
103 int state; /* WRITE / READ / CLOSED */
104 char *out; /* buffer sent (zero terminated) */
105 char *outptr; /* index within the buffer sent */
106 char *in; /* the receiving buffer */
107 char *content; /* the start of the content */
108 char *inptr; /* the next byte to read from network */
109 char *inrptr; /* the next byte to give back to the client */
110 int inlen; /* len of the input buffer */
111 int last; /* return code for last operation */
112 int returnValue; /* the protocol return value */
Daniel Veillardf012a642001-07-23 19:10:52 +0000113 int ContentLength; /* specified content length from HTTP header */
Owen Taylor3473f882001-02-23 17:55:21 +0000114 char *contentType; /* the MIME type for the input */
115 char *location; /* the new URL in case of redirect */
116 char *authHeader; /* contents of {WWW,Proxy}-Authenticate header */
117} xmlNanoHTTPCtxt, *xmlNanoHTTPCtxtPtr;
118
119static int initialized = 0;
120static char *proxy = NULL; /* the proxy name if any */
121static int proxyPort; /* the proxy port if any */
122static unsigned int timeout = 60;/* the select() timeout in seconds */
123
Daniel Veillardf012a642001-07-23 19:10:52 +0000124int xmlNanoHTTPFetchContent( void * ctx, char ** ptr, int * len );
125int xmlNanoHTTPContentLength( void * ctx );
126
Owen Taylor3473f882001-02-23 17:55:21 +0000127/**
128 * A portability function
129 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000130static int socket_errno(void) {
Owen Taylor3473f882001-02-23 17:55:21 +0000131#ifdef _WINSOCKAPI_
132 return(WSAGetLastError());
133#else
134 return(errno);
135#endif
136}
137
138/**
139 * xmlNanoHTTPInit:
140 *
141 * Initialize the HTTP protocol layer.
142 * Currently it just checks for proxy informations
143 */
144
145void
146xmlNanoHTTPInit(void) {
147 const char *env;
148#ifdef _WINSOCKAPI_
149 WSADATA wsaData;
150#endif
151
152 if (initialized)
153 return;
154
155#ifdef _WINSOCKAPI_
156 if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0)
157 return;
158#endif
159
160 if (proxy == NULL) {
161 proxyPort = 80;
162 env = getenv("no_proxy");
163 if (env != NULL)
164 goto done;
165 env = getenv("http_proxy");
166 if (env != NULL) {
167 xmlNanoHTTPScanProxy(env);
168 goto done;
169 }
170 env = getenv("HTTP_PROXY");
171 if (env != NULL) {
172 xmlNanoHTTPScanProxy(env);
173 goto done;
174 }
175 }
176done:
177 initialized = 1;
178}
179
180/**
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000181 * xmlNanoHTTPCleanup:
Owen Taylor3473f882001-02-23 17:55:21 +0000182 *
183 * Cleanup the HTTP protocol layer.
184 */
185
186void
187xmlNanoHTTPCleanup(void) {
188 if (proxy != NULL)
189 xmlFree(proxy);
190#ifdef _WINSOCKAPI_
191 if (initialized)
192 WSACleanup();
193#endif
194 initialized = 0;
195 return;
196}
197
198/**
Owen Taylor3473f882001-02-23 17:55:21 +0000199 * xmlNanoHTTPScanURL:
200 * @ctxt: an HTTP context
201 * @URL: The URL used to initialize the context
202 *
203 * (Re)Initialize an HTTP context by parsing the URL and finding
204 * the protocol host port and path it indicates.
205 */
206
207static void
208xmlNanoHTTPScanURL(xmlNanoHTTPCtxtPtr ctxt, const char *URL) {
209 const char *cur = URL;
210 char buf[4096];
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000211 int indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000212 int port = 0;
213
214 if (ctxt->protocol != NULL) {
215 xmlFree(ctxt->protocol);
216 ctxt->protocol = NULL;
217 }
218 if (ctxt->hostname != NULL) {
219 xmlFree(ctxt->hostname);
220 ctxt->hostname = NULL;
221 }
222 if (ctxt->path != NULL) {
223 xmlFree(ctxt->path);
224 ctxt->path = NULL;
225 }
226 if (URL == NULL) return;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000227 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000228 while (*cur != 0) {
229 if ((cur[0] == ':') && (cur[1] == '/') && (cur[2] == '/')) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000230 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000231 ctxt->protocol = xmlMemStrdup(buf);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000232 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000233 cur += 3;
234 break;
235 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000236 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000237 }
238 if (*cur == 0) return;
239
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000240 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000241 while (1) {
242 if (cur[0] == ':') {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000243 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000244 ctxt->hostname = xmlMemStrdup(buf);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000245 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000246 cur += 1;
247 while ((*cur >= '0') && (*cur <= '9')) {
248 port *= 10;
249 port += *cur - '0';
250 cur++;
251 }
252 if (port != 0) ctxt->port = port;
253 while ((cur[0] != '/') && (*cur != 0))
254 cur++;
255 break;
256 }
257 if ((*cur == '/') || (*cur == 0)) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000258 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000259 ctxt->hostname = xmlMemStrdup(buf);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000260 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000261 break;
262 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000263 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000264 }
265 if (*cur == 0)
266 ctxt->path = xmlMemStrdup("/");
267 else {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000268 indx = 0;
269 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000270 while (*cur != 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000271 buf[indx++] = *cur++;
272 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000273 ctxt->path = xmlMemStrdup(buf);
274 }
275}
276
277/**
278 * xmlNanoHTTPScanProxy:
279 * @URL: The proxy URL used to initialize the proxy context
280 *
281 * (Re)Initialize the HTTP Proxy context by parsing the URL and finding
282 * the protocol host port it indicates.
283 * Should be like http://myproxy/ or http://myproxy:3128/
284 * A NULL URL cleans up proxy informations.
285 */
286
287void
288xmlNanoHTTPScanProxy(const char *URL) {
289 const char *cur = URL;
290 char buf[4096];
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000291 int indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000292 int port = 0;
293
294 if (proxy != NULL) {
295 xmlFree(proxy);
296 proxy = NULL;
297 }
298 if (proxyPort != 0) {
299 proxyPort = 0;
300 }
301#ifdef DEBUG_HTTP
302 if (URL == NULL)
303 xmlGenericError(xmlGenericErrorContext,
304 "Removing HTTP proxy info\n");
305 else
306 xmlGenericError(xmlGenericErrorContext,
307 "Using HTTP proxy %s\n", URL);
308#endif
309 if (URL == NULL) return;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000310 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000311 while (*cur != 0) {
312 if ((cur[0] == ':') && (cur[1] == '/') && (cur[2] == '/')) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000313 buf[indx] = 0;
314 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000315 cur += 3;
316 break;
317 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000318 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000319 }
320 if (*cur == 0) return;
321
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000322 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000323 while (1) {
324 if (cur[0] == ':') {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000325 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000326 proxy = xmlMemStrdup(buf);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000327 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000328 cur += 1;
329 while ((*cur >= '0') && (*cur <= '9')) {
330 port *= 10;
331 port += *cur - '0';
332 cur++;
333 }
334 if (port != 0) proxyPort = port;
335 while ((cur[0] != '/') && (*cur != 0))
336 cur++;
337 break;
338 }
339 if ((*cur == '/') || (*cur == 0)) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000340 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000341 proxy = xmlMemStrdup(buf);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000342 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000343 break;
344 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000345 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000346 }
347}
348
349/**
350 * xmlNanoHTTPNewCtxt:
351 * @URL: The URL used to initialize the context
352 *
353 * Allocate and initialize a new HTTP context.
354 *
355 * Returns an HTTP context or NULL in case of error.
356 */
357
358static xmlNanoHTTPCtxtPtr
359xmlNanoHTTPNewCtxt(const char *URL) {
360 xmlNanoHTTPCtxtPtr ret;
361
362 ret = (xmlNanoHTTPCtxtPtr) xmlMalloc(sizeof(xmlNanoHTTPCtxt));
363 if (ret == NULL) return(NULL);
364
365 memset(ret, 0, sizeof(xmlNanoHTTPCtxt));
366 ret->port = 80;
367 ret->returnValue = 0;
368 ret->fd = -1;
Daniel Veillardf012a642001-07-23 19:10:52 +0000369 ret->ContentLength = -1;
Owen Taylor3473f882001-02-23 17:55:21 +0000370
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 if (ctxt->authHeader != NULL) xmlFree(ctxt->authHeader);
394 ctxt->state = XML_NANO_HTTP_NONE;
395 if (ctxt->fd >= 0) closesocket(ctxt->fd);
396 ctxt->fd = -1;
397 xmlFree(ctxt);
398}
399
400/**
401 * xmlNanoHTTPSend:
402 * @ctxt: an HTTP context
403 *
404 * Send the input needed to initiate the processing on the server side
Daniel Veillardf012a642001-07-23 19:10:52 +0000405 * Returns number of bytes sent or -1 on error.
Owen Taylor3473f882001-02-23 17:55:21 +0000406 */
407
Daniel Veillardf012a642001-07-23 19:10:52 +0000408static int
409xmlNanoHTTPSend(xmlNanoHTTPCtxtPtr ctxt, const char * xmt_ptr, int outlen) {
410
411 int total_sent = 0;
412
413 if ( (ctxt->state & XML_NANO_HTTP_WRITE) && (xmt_ptr != NULL ) ) {
414 while (total_sent < outlen) {
415 int nsent = send(ctxt->fd, xmt_ptr + total_sent,
416 outlen - total_sent, 0);
Owen Taylor3473f882001-02-23 17:55:21 +0000417 if (nsent>0)
418 total_sent += nsent;
Daniel Veillardf012a642001-07-23 19:10:52 +0000419 else if ( ( nsent == -1 ) &&
Daniel Veillardba6db032001-07-31 16:25:45 +0000420#if defined(EAGAIN) && EAGAIN != EWOULDBLOCK
Daniel Veillardf012a642001-07-23 19:10:52 +0000421 ( socket_errno( ) != EAGAIN ) &&
Daniel Veillardba6db032001-07-31 16:25:45 +0000422#endif
Daniel Veillardf012a642001-07-23 19:10:52 +0000423 ( socket_errno( ) != EWOULDBLOCK ) ) {
424 xmlGenericError( xmlGenericErrorContext,
425 "xmlNanoHTTPSend error: %s",
426 strerror( socket_errno( ) ) );
427
428 if ( total_sent == 0 )
429 total_sent = -1;
430 break;
431 }
432 else {
433 /*
434 ** No data sent
435 ** Since non-blocking sockets are used, wait for
436 ** socket to be writable or default timeout prior
437 ** to retrying.
438 */
439
440 struct timeval tv;
441 fd_set wfd;
442
443 tv.tv_sec = timeout;
444 tv.tv_usec = 0;
445 FD_ZERO( &wfd );
446 FD_SET( ctxt->fd, &wfd );
447 (void)select( ctxt->fd + 1, NULL, &wfd, NULL, &tv );
448 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000449 }
Owen Taylor3473f882001-02-23 17:55:21 +0000450 }
Daniel Veillardf012a642001-07-23 19:10:52 +0000451
452 return total_sent;
Owen Taylor3473f882001-02-23 17:55:21 +0000453}
454
455/**
456 * xmlNanoHTTPRecv:
457 * @ctxt: an HTTP context
458 *
459 * Read information coming from the HTTP connection.
460 * This is a blocking call (but it blocks in select(), not read()).
461 *
462 * Returns the number of byte read or -1 in case of error.
463 */
464
465static int
466xmlNanoHTTPRecv(xmlNanoHTTPCtxtPtr ctxt) {
467 fd_set rfd;
468 struct timeval tv;
469
470
471 while (ctxt->state & XML_NANO_HTTP_READ) {
472 if (ctxt->in == NULL) {
473 ctxt->in = (char *) xmlMalloc(65000 * sizeof(char));
474 if (ctxt->in == NULL) {
475 ctxt->last = -1;
Daniel Veillardf012a642001-07-23 19:10:52 +0000476 xmlGenericError( xmlGenericErrorContext,
477 "xmlNanoHTTPRecv: Error allocating input memory." );
Owen Taylor3473f882001-02-23 17:55:21 +0000478 return(-1);
479 }
480 ctxt->inlen = 65000;
481 ctxt->inptr = ctxt->content = ctxt->inrptr = ctxt->in;
482 }
483 if (ctxt->inrptr > ctxt->in + XML_NANO_HTTP_CHUNK) {
484 int delta = ctxt->inrptr - ctxt->in;
485 int len = ctxt->inptr - ctxt->inrptr;
486
487 memmove(ctxt->in, ctxt->inrptr, len);
488 ctxt->inrptr -= delta;
489 ctxt->content -= delta;
490 ctxt->inptr -= delta;
491 }
492 if ((ctxt->in + ctxt->inlen) < (ctxt->inptr + XML_NANO_HTTP_CHUNK)) {
493 int d_inptr = ctxt->inptr - ctxt->in;
494 int d_content = ctxt->content - ctxt->in;
495 int d_inrptr = ctxt->inrptr - ctxt->in;
Daniel Veillardf012a642001-07-23 19:10:52 +0000496 char * tmp_ptr = ctxt->in;
Owen Taylor3473f882001-02-23 17:55:21 +0000497
498 ctxt->inlen *= 2;
Daniel Veillardf012a642001-07-23 19:10:52 +0000499 ctxt->in = (char *) xmlRealloc(tmp_ptr, ctxt->inlen);
Owen Taylor3473f882001-02-23 17:55:21 +0000500 if (ctxt->in == NULL) {
Daniel Veillardf012a642001-07-23 19:10:52 +0000501 xmlGenericError( xmlGenericErrorContext,
502 "xmlNanoHTTPRecv: %s %d bytes.",
503 "Failed to realloc input buffer to",
504 ctxt->inlen );
505 xmlFree( tmp_ptr );
Owen Taylor3473f882001-02-23 17:55:21 +0000506 ctxt->last = -1;
507 return(-1);
508 }
509 ctxt->inptr = ctxt->in + d_inptr;
510 ctxt->content = ctxt->in + d_content;
511 ctxt->inrptr = ctxt->in + d_inrptr;
512 }
513 ctxt->last = recv(ctxt->fd, ctxt->inptr, XML_NANO_HTTP_CHUNK, 0);
514 if (ctxt->last > 0) {
515 ctxt->inptr += ctxt->last;
516 return(ctxt->last);
517 }
518 if (ctxt->last == 0) {
519 return(0);
520 }
521 if (ctxt->last == -1) {
522 switch (socket_errno()) {
523 case EINPROGRESS:
524 case EWOULDBLOCK:
525#if defined(EAGAIN) && EAGAIN != EWOULDBLOCK
526 case EAGAIN:
527#endif
528 break;
Daniel Veillardf012a642001-07-23 19:10:52 +0000529
530 case ECONNRESET:
531 case ESHUTDOWN:
532 return ( 0 );
533
Owen Taylor3473f882001-02-23 17:55:21 +0000534 default:
Daniel Veillardf012a642001-07-23 19:10:52 +0000535 xmlGenericError( xmlGenericErrorContext,
536 "xmlNanoHTTPRecv: recv( ) failure - %s",
537 strerror( socket_errno( ) ) );
538 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +0000539 }
540 }
541
542 tv.tv_sec = timeout;
543 tv.tv_usec = 0;
544 FD_ZERO(&rfd);
545 FD_SET(ctxt->fd, &rfd);
546
Daniel Veillard50f34372001-08-03 12:06:36 +0000547 if ( (select(ctxt->fd+1, &rfd, NULL, NULL, &tv)<1)
548#if defined(EINTR)
549 && (errno != EINTR)
550#endif
551 )
Owen Taylor3473f882001-02-23 17:55:21 +0000552 return(0);
553 }
554 return(0);
555}
556
557/**
558 * xmlNanoHTTPReadLine:
559 * @ctxt: an HTTP context
560 *
561 * Read one line in the HTTP server output, usually for extracting
562 * the HTTP protocol informations from the answer header.
563 *
564 * Returns a newly allocated string with a copy of the line, or NULL
565 * which indicate the end of the input.
566 */
567
568static char *
569xmlNanoHTTPReadLine(xmlNanoHTTPCtxtPtr ctxt) {
570 char buf[4096];
571 char *bp = buf;
Daniel Veillardf012a642001-07-23 19:10:52 +0000572 int rc;
Owen Taylor3473f882001-02-23 17:55:21 +0000573
574 while (bp - buf < 4095) {
575 if (ctxt->inrptr == ctxt->inptr) {
Daniel Veillardf012a642001-07-23 19:10:52 +0000576 if ( (rc = xmlNanoHTTPRecv(ctxt)) == 0) {
Owen Taylor3473f882001-02-23 17:55:21 +0000577 if (bp == buf)
578 return(NULL);
579 else
580 *bp = 0;
581 return(xmlMemStrdup(buf));
582 }
Daniel Veillardf012a642001-07-23 19:10:52 +0000583 else if ( rc == -1 ) {
584 return ( NULL );
585 }
Owen Taylor3473f882001-02-23 17:55:21 +0000586 }
587 *bp = *ctxt->inrptr++;
588 if (*bp == '\n') {
589 *bp = 0;
590 return(xmlMemStrdup(buf));
591 }
592 if (*bp != '\r')
593 bp++;
594 }
595 buf[4095] = 0;
596 return(xmlMemStrdup(buf));
597}
598
599
600/**
601 * xmlNanoHTTPScanAnswer:
602 * @ctxt: an HTTP context
603 * @line: an HTTP header line
604 *
605 * Try to extract useful informations from the server answer.
606 * We currently parse and process:
607 * - The HTTP revision/ return code
608 * - The Content-Type
609 * - The Location for redirrect processing.
610 *
611 * Returns -1 in case of failure, the file descriptor number otherwise
612 */
613
614static void
615xmlNanoHTTPScanAnswer(xmlNanoHTTPCtxtPtr ctxt, const char *line) {
616 const char *cur = line;
617
618 if (line == NULL) return;
619
620 if (!strncmp(line, "HTTP/", 5)) {
621 int version = 0;
622 int ret = 0;
623
624 cur += 5;
625 while ((*cur >= '0') && (*cur <= '9')) {
626 version *= 10;
627 version += *cur - '0';
628 cur++;
629 }
630 if (*cur == '.') {
631 cur++;
632 if ((*cur >= '0') && (*cur <= '9')) {
633 version *= 10;
634 version += *cur - '0';
635 cur++;
636 }
637 while ((*cur >= '0') && (*cur <= '9'))
638 cur++;
639 } else
640 version *= 10;
641 if ((*cur != ' ') && (*cur != '\t')) return;
642 while ((*cur == ' ') || (*cur == '\t')) cur++;
643 if ((*cur < '0') || (*cur > '9')) return;
644 while ((*cur >= '0') && (*cur <= '9')) {
645 ret *= 10;
646 ret += *cur - '0';
647 cur++;
648 }
649 if ((*cur != 0) && (*cur != ' ') && (*cur != '\t')) return;
650 ctxt->returnValue = ret;
651 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Content-Type:", 13)) {
652 cur += 13;
653 while ((*cur == ' ') || (*cur == '\t')) cur++;
654 if (ctxt->contentType != NULL)
655 xmlFree(ctxt->contentType);
656 ctxt->contentType = xmlMemStrdup(cur);
657 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"ContentType:", 12)) {
658 cur += 12;
659 if (ctxt->contentType != NULL) return;
660 while ((*cur == ' ') || (*cur == '\t')) cur++;
661 ctxt->contentType = xmlMemStrdup(cur);
662 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Location:", 9)) {
663 cur += 9;
664 while ((*cur == ' ') || (*cur == '\t')) cur++;
665 if (ctxt->location != NULL)
666 xmlFree(ctxt->location);
667 ctxt->location = xmlMemStrdup(cur);
668 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"WWW-Authenticate:", 17)) {
669 cur += 17;
670 while ((*cur == ' ') || (*cur == '\t')) cur++;
671 if (ctxt->authHeader != NULL)
672 xmlFree(ctxt->authHeader);
673 ctxt->authHeader = xmlMemStrdup(cur);
674 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Proxy-Authenticate:", 19)) {
675 cur += 19;
676 while ((*cur == ' ') || (*cur == '\t')) cur++;
677 if (ctxt->authHeader != NULL)
678 xmlFree(ctxt->authHeader);
679 ctxt->authHeader = xmlMemStrdup(cur);
Daniel Veillardf012a642001-07-23 19:10:52 +0000680 } else if ( !xmlStrncasecmp( BAD_CAST line, BAD_CAST"Content-Length:", 15) ) {
681 cur += 15;
682 ctxt->ContentLength = strtol( cur, NULL, 10 );
Owen Taylor3473f882001-02-23 17:55:21 +0000683 }
684}
685
686/**
687 * xmlNanoHTTPConnectAttempt:
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000688 * @addr: a socket adress structure
Owen Taylor3473f882001-02-23 17:55:21 +0000689 *
690 * Attempt a connection to the given IP:port endpoint. It forces
691 * non-blocking semantic on the socket, and allow 60 seconds for
692 * the host to answer.
693 *
694 * Returns -1 in case of failure, the file descriptor number otherwise
695 */
696
697static int
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000698xmlNanoHTTPConnectAttempt(struct sockaddr *addr)
Owen Taylor3473f882001-02-23 17:55:21 +0000699{
700 SOCKET s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
701 fd_set wfd;
702 struct timeval tv;
703 int status;
704
705 if (s==-1) {
706#ifdef DEBUG_HTTP
707 perror("socket");
708#endif
Daniel Veillardf012a642001-07-23 19:10:52 +0000709 xmlGenericError( xmlGenericErrorContext,
710 "xmlNanoHTTPConnectAttempt: %s - %s",
711 "socket creation failure",
712 strerror( socket_errno( ) ) );
Owen Taylor3473f882001-02-23 17:55:21 +0000713 return(-1);
714 }
715
716#ifdef _WINSOCKAPI_
717 {
718 u_long one = 1;
719
720 status = ioctlsocket(s, FIONBIO, &one) == SOCKET_ERROR ? -1 : 0;
721 }
722#else /* _WINSOCKAPI_ */
723#if defined(VMS)
724 {
725 int enable = 1;
726 status = ioctl(s, FIONBIO, &enable);
727 }
728#else /* VMS */
729 if ((status = fcntl(s, F_GETFL, 0)) != -1) {
730#ifdef O_NONBLOCK
731 status |= O_NONBLOCK;
732#else /* O_NONBLOCK */
733#ifdef F_NDELAY
734 status |= F_NDELAY;
735#endif /* F_NDELAY */
736#endif /* !O_NONBLOCK */
737 status = fcntl(s, F_SETFL, status);
738 }
739 if (status < 0) {
740#ifdef DEBUG_HTTP
741 perror("nonblocking");
742#endif
Daniel Veillardf012a642001-07-23 19:10:52 +0000743 xmlGenericError( xmlGenericErrorContext,
744 "xmlNanoHTTPConnectAttempt: %s - %s",
745 "error setting non-blocking IO",
746 strerror( socket_errno( ) ) );
Owen Taylor3473f882001-02-23 17:55:21 +0000747 closesocket(s);
748 return(-1);
749 }
750#endif /* !VMS */
751#endif /* !_WINSOCKAPI_ */
752
Owen Taylor3473f882001-02-23 17:55:21 +0000753 if ((connect(s, addr, sizeof(*addr))==-1)) {
754 switch (socket_errno()) {
755 case EINPROGRESS:
756 case EWOULDBLOCK:
757 break;
758 default:
Daniel Veillardf012a642001-07-23 19:10:52 +0000759 xmlGenericError( xmlGenericErrorContext,
760 "xmlNanoHTTPConnectAttempt: %s - %s",
761 "error connecting to HTTP server",
762 strerror( socket_errno( ) ) );
Owen Taylor3473f882001-02-23 17:55:21 +0000763 closesocket(s);
764 return(-1);
765 }
766 }
767
768 tv.tv_sec = timeout;
769 tv.tv_usec = 0;
770
771 FD_ZERO(&wfd);
772 FD_SET(s, &wfd);
773
774 switch(select(s+1, NULL, &wfd, NULL, &tv))
775 {
776 case 0:
777 /* Time out */
Daniel Veillardf012a642001-07-23 19:10:52 +0000778 xmlGenericError( xmlGenericErrorContext,
779 "xmlNanoHTTPConnectAttempt: %s",
780 "Connect attempt timed out." );
Owen Taylor3473f882001-02-23 17:55:21 +0000781 closesocket(s);
782 return(-1);
783 case -1:
784 /* Ermm.. ?? */
Daniel Veillardf012a642001-07-23 19:10:52 +0000785 xmlGenericError( xmlGenericErrorContext,
786 "xmlNanoHTTPConnectAttempt: %s - %s",
787 "Error connecting to host",
788 strerror( socket_errno( ) ) );
Owen Taylor3473f882001-02-23 17:55:21 +0000789 closesocket(s);
790 return(-1);
791 }
792
793 if ( FD_ISSET(s, &wfd) ) {
794 SOCKLEN_T len;
795 len = sizeof(status);
796 if (getsockopt(s, SOL_SOCKET, SO_ERROR, (char*)&status, &len) < 0 ) {
797 /* Solaris error code */
Daniel Veillardf012a642001-07-23 19:10:52 +0000798 xmlGenericError( xmlGenericErrorContext,
799 "xmlNanoHTTPConnectAttempt: %s - %s",
800 "Error retrieving pending socket errors",
801 strerror( socket_errno( ) ) );
Owen Taylor3473f882001-02-23 17:55:21 +0000802 return (-1);
803 }
804 if ( status ) {
805 closesocket(s);
806 errno = status;
Daniel Veillardf012a642001-07-23 19:10:52 +0000807 xmlGenericError( xmlGenericErrorContext,
808 "xmlNanoHTTPConnectAttempt: %s - %s",
809 "Error connecting to remote host",
810 strerror( status ) );
Owen Taylor3473f882001-02-23 17:55:21 +0000811 return (-1);
812 }
813 } else {
814 /* pbm */
Daniel Veillardf012a642001-07-23 19:10:52 +0000815 xmlGenericError( xmlGenericErrorContext,
816 "xmlNanoHTTPConnectAttempt: %s\n",
817 "Select returned, but descriptor not set for connection.\n" );
818 closesocket(s);
Owen Taylor3473f882001-02-23 17:55:21 +0000819 return (-1);
820 }
821
822 return(s);
823}
824
825/**
826 * xmlNanoHTTPConnectHost:
827 * @host: the host name
828 * @port: the port number
829 *
830 * Attempt a connection to the given host:port endpoint. It tries
831 * the multiple IP provided by the DNS if available.
832 *
833 * Returns -1 in case of failure, the file descriptor number otherwise
834 */
835
836static int
837xmlNanoHTTPConnectHost(const char *host, int port)
838{
839 struct hostent *h;
840 struct sockaddr *addr;
841 struct in_addr ia;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000842 struct sockaddr_in sockin;
Owen Taylor3473f882001-02-23 17:55:21 +0000843#ifdef SUPPORT_IP6
844 struct in6_addr ia6;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000845 struct sockaddr_in6 sockin6;
Owen Taylor3473f882001-02-23 17:55:21 +0000846#endif
847 int i;
848 int s;
849
850#if defined(SUPPORT_IP6) && defined(RES_USE_INET6)
851 if (!(_res.options & RES_INIT))
852 res_init();
853 _res.options |= RES_USE_INET6;
854#endif
855 h=gethostbyname(host);
856 if (h==NULL)
857 {
Daniel Veillardf012a642001-07-23 19:10:52 +0000858 const char * h_err_txt = "";
859 switch ( h_errno )
860 {
861 case HOST_NOT_FOUND:
862 h_err_txt = "Authoritive host not found";
863 break;
864
865 case TRY_AGAIN:
866 h_err_txt =
867 "Non-authoritive host not found or server failure.";
868 break;
869
870 case NO_RECOVERY:
871 h_err_txt =
872 "Non-recoverable errors: FORMERR, REFUSED, or NOTIMP.";
873 break;
874
875 case NO_ADDRESS:
876 h_err_txt = "Valid name, no data record of requested type.";
877 break;
878
879 default:
880 h_err_txt = "No error text defined.";
881 break;
882 }
883 xmlGenericError( xmlGenericErrorContext,
884 "xmlNanoHTTPConnectHost: %s '%s' - %s",
885 "Failed to resolve host", host, h_err_txt );
Owen Taylor3473f882001-02-23 17:55:21 +0000886 return(-1);
887 }
888
889 for(i=0; h->h_addr_list[i]; i++)
890 {
891 if (h->h_addrtype == AF_INET) {
892 /* A records (IPv4) */
893 memcpy(&ia, h->h_addr_list[i], h->h_length);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000894 sockin.sin_family = h->h_addrtype;
895 sockin.sin_addr = ia;
896 sockin.sin_port = htons(port);
897 addr = (struct sockaddr *)&sockin;
Owen Taylor3473f882001-02-23 17:55:21 +0000898#ifdef SUPPORT_IP6
899 } else if (h->h_addrtype == AF_INET6) {
900 /* AAAA records (IPv6) */
901 memcpy(&ia6, h->h_addr_list[i], h->h_length);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000902 sockin6.sin_family = h->h_addrtype;
903 sockin6.sin_addr = ia6;
904 sockin6.sin_port = htons(port);
905 addr = (struct sockaddr *)&sockin6;
Owen Taylor3473f882001-02-23 17:55:21 +0000906#endif
907 } else
908 break; /* for */
909
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000910 s = xmlNanoHTTPConnectAttempt(addr);
Owen Taylor3473f882001-02-23 17:55:21 +0000911 if (s != -1)
912 return(s);
913 }
914
915#ifdef DEBUG_HTTP
916 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardf012a642001-07-23 19:10:52 +0000917 "xmlNanoHTTPConnectHost: unable to connect to '%s'.\n", host);
Owen Taylor3473f882001-02-23 17:55:21 +0000918#endif
919 return(-1);
920}
921
922
923/**
924 * xmlNanoHTTPOpen:
925 * @URL: The URL to load
926 * @contentType: if available the Content-Type information will be
927 * returned at that location
928 *
929 * This function try to open a connection to the indicated resource
930 * via HTTP GET.
931 *
932 * Returns NULL in case of failure, otherwise a request handler.
933 * The contentType, if provided must be freed by the caller
934 */
935
936void*
937xmlNanoHTTPOpen(const char *URL, char **contentType) {
938 if (contentType != NULL) *contentType = NULL;
Daniel Veillardf012a642001-07-23 19:10:52 +0000939 return(xmlNanoHTTPMethod(URL, NULL, NULL, contentType, NULL, 0));
Daniel Veillard9403a042001-05-28 11:00:53 +0000940}
941
942/**
943 * xmlNanoHTTPOpenRedir:
944 * @URL: The URL to load
945 * @contentType: if available the Content-Type information will be
946 * returned at that location
947 * @redir: if availble the redirected URL will be returned
948 *
949 * This function try to open a connection to the indicated resource
950 * via HTTP GET.
951 *
952 * Returns NULL in case of failure, otherwise a request handler.
953 * The contentType, if provided must be freed by the caller
954 */
955
956void*
957xmlNanoHTTPOpenRedir(const char *URL, char **contentType, char **redir) {
958 if (contentType != NULL) *contentType = NULL;
959 if (redir != NULL) *redir = NULL;
Daniel Veillardf012a642001-07-23 19:10:52 +0000960 return(xmlNanoHTTPMethodRedir(URL, NULL, NULL, contentType, redir, NULL,0));
Owen Taylor3473f882001-02-23 17:55:21 +0000961}
962
963/**
964 * xmlNanoHTTPRead:
965 * @ctx: the HTTP context
966 * @dest: a buffer
967 * @len: the buffer length
968 *
969 * This function tries to read @len bytes from the existing HTTP connection
970 * and saves them in @dest. This is a blocking call.
971 *
972 * Returns the number of byte read. 0 is an indication of an end of connection.
973 * -1 indicates a parameter error.
974 */
975int
976xmlNanoHTTPRead(void *ctx, void *dest, int len) {
977 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
978
979 if (ctx == NULL) return(-1);
980 if (dest == NULL) return(-1);
981 if (len <= 0) return(0);
982
983 while (ctxt->inptr - ctxt->inrptr < len) {
Daniel Veillardf012a642001-07-23 19:10:52 +0000984 if (xmlNanoHTTPRecv(ctxt) <= 0) break;
Owen Taylor3473f882001-02-23 17:55:21 +0000985 }
986 if (ctxt->inptr - ctxt->inrptr < len)
987 len = ctxt->inptr - ctxt->inrptr;
988 memcpy(dest, ctxt->inrptr, len);
989 ctxt->inrptr += len;
990 return(len);
991}
992
993/**
994 * xmlNanoHTTPClose:
995 * @ctx: the HTTP context
996 *
997 * This function closes an HTTP context, it ends up the connection and
998 * free all data related to it.
999 */
1000void
1001xmlNanoHTTPClose(void *ctx) {
1002 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1003
1004 if (ctx == NULL) return;
1005
1006 xmlNanoHTTPFreeCtxt(ctxt);
1007}
1008
1009/**
Daniel Veillard9403a042001-05-28 11:00:53 +00001010 * xmlNanoHTTPMethodRedir:
Owen Taylor3473f882001-02-23 17:55:21 +00001011 * @URL: The URL to load
1012 * @method: the HTTP method to use
1013 * @input: the input string if any
1014 * @contentType: the Content-Type information IN and OUT
Daniel Veillard9403a042001-05-28 11:00:53 +00001015 * @redir: the redirected URL OUT
Owen Taylor3473f882001-02-23 17:55:21 +00001016 * @headers: the extra headers
1017 *
1018 * This function try to open a connection to the indicated resource
1019 * via HTTP using the given @method, adding the given extra headers
1020 * and the input buffer for the request content.
1021 *
1022 * Returns NULL in case of failure, otherwise a request handler.
Daniel Veillard9403a042001-05-28 11:00:53 +00001023 * The contentType, or redir, if provided must be freed by the caller
Owen Taylor3473f882001-02-23 17:55:21 +00001024 */
1025
1026void*
Daniel Veillard9403a042001-05-28 11:00:53 +00001027xmlNanoHTTPMethodRedir(const char *URL, const char *method, const char *input,
Daniel Veillardf012a642001-07-23 19:10:52 +00001028 char **contentType, char **redir,
1029 const char *headers, int ilen ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001030 xmlNanoHTTPCtxtPtr ctxt;
1031 char *bp, *p;
Daniel Veillardf012a642001-07-23 19:10:52 +00001032 int blen, ret;
Owen Taylor3473f882001-02-23 17:55:21 +00001033 int head;
Daniel Veillardf012a642001-07-23 19:10:52 +00001034 int xmt_bytes;
Owen Taylor3473f882001-02-23 17:55:21 +00001035 int nbRedirects = 0;
1036 char *redirURL = NULL;
1037
1038 if (URL == NULL) return(NULL);
1039 if (method == NULL) method = "GET";
1040 xmlNanoHTTPInit();
1041
1042retry:
1043 if (redirURL == NULL)
1044 ctxt = xmlNanoHTTPNewCtxt(URL);
1045 else {
1046 ctxt = xmlNanoHTTPNewCtxt(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001047 }
1048
Daniel Veillardf012a642001-07-23 19:10:52 +00001049 if ( ctxt == NULL ) {
1050 xmlGenericError( xmlGenericErrorContext,
1051 "xmlNanoHTTPMethodRedir: %s %s.",
1052 "Unable to allocate HTTP context to URI",
1053 ( ( redirURL == NULL ) ? URL : redirURL ) );
1054 return ( NULL );
1055 }
1056
Owen Taylor3473f882001-02-23 17:55:21 +00001057 if ((ctxt->protocol == NULL) || (strcmp(ctxt->protocol, "http"))) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001058 xmlGenericError( xmlGenericErrorContext,
1059 "xmlNanoHTTPMethodRedir: %s - %s.",
1060 "Not a valid HTTP URI",
1061 ( ( redirURL == NULL ) ? URL : redirURL ) );
Owen Taylor3473f882001-02-23 17:55:21 +00001062 xmlNanoHTTPFreeCtxt(ctxt);
1063 if (redirURL != NULL) xmlFree(redirURL);
1064 return(NULL);
1065 }
1066 if (ctxt->hostname == NULL) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001067 xmlGenericError( xmlGenericErrorContext,
1068 "xmlNanoHTTPMethodRedir: %s - %s",
1069 "Failed to identify host in URI",
1070 ( ( redirURL == NULL ) ? URL : redirURL ) );
Owen Taylor3473f882001-02-23 17:55:21 +00001071 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001072 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001073 return(NULL);
1074 }
1075 if (proxy) {
1076 blen = strlen(ctxt->hostname) * 2 + 16;
1077 ret = xmlNanoHTTPConnectHost(proxy, proxyPort);
1078 }
1079 else {
1080 blen = strlen(ctxt->hostname);
1081 ret = xmlNanoHTTPConnectHost(ctxt->hostname, ctxt->port);
1082 }
1083 if (ret < 0) {
1084 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001085 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001086 return(NULL);
1087 }
1088 ctxt->fd = ret;
1089
Daniel Veillardf012a642001-07-23 19:10:52 +00001090 if (input == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00001091 ilen = 0;
Daniel Veillardf012a642001-07-23 19:10:52 +00001092 else
1093 blen += 36;
1094
Owen Taylor3473f882001-02-23 17:55:21 +00001095 if (headers != NULL)
Daniel Veillardf012a642001-07-23 19:10:52 +00001096 blen += strlen(headers) + 2;
Owen Taylor3473f882001-02-23 17:55:21 +00001097 if (contentType && *contentType)
1098 blen += strlen(*contentType) + 16;
Daniel Veillardf012a642001-07-23 19:10:52 +00001099 blen += strlen(method) + strlen(ctxt->path) + 24;
Owen Taylor3473f882001-02-23 17:55:21 +00001100 bp = xmlMalloc(blen);
Daniel Veillardf012a642001-07-23 19:10:52 +00001101 if ( bp == NULL ) {
1102 xmlNanoHTTPFreeCtxt( ctxt );
1103 xmlGenericError( xmlGenericErrorContext,
1104 "xmlNanoHTTPMethodRedir: %s",
1105 "Error allocating HTTP header buffer." );
1106 return ( NULL );
1107 }
1108
1109 p = bp;
1110
Owen Taylor3473f882001-02-23 17:55:21 +00001111 if (proxy) {
1112 if (ctxt->port != 80) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001113 p += sprintf( p, "%s http://%s:%d%s", method, ctxt->hostname,
1114 ctxt->port, ctxt->path );
Owen Taylor3473f882001-02-23 17:55:21 +00001115 }
1116 else
Daniel Veillardf012a642001-07-23 19:10:52 +00001117 p += sprintf( p, "%s http://%s%s", method,
1118 ctxt->hostname, ctxt->path);
Owen Taylor3473f882001-02-23 17:55:21 +00001119 }
1120 else
Daniel Veillardf012a642001-07-23 19:10:52 +00001121 p += sprintf( p, "%s %s", method, ctxt->path);
1122
1123 p += sprintf(p, " HTTP/1.0\r\nHost: %s\r\n", ctxt->hostname);
1124
1125 if (contentType != NULL && *contentType)
1126 p += sprintf(p, "Content-Type: %s\r\n", *contentType);
1127
1128 if (headers != NULL)
1129 p += sprintf( p, "%s", headers );
1130
Owen Taylor3473f882001-02-23 17:55:21 +00001131 if (input != NULL)
Daniel Veillardf012a642001-07-23 19:10:52 +00001132 sprintf(p, "Content-Length: %d\r\n\r\n", ilen );
Owen Taylor3473f882001-02-23 17:55:21 +00001133 else
1134 strcpy(p, "\r\n");
Daniel Veillardf012a642001-07-23 19:10:52 +00001135
Owen Taylor3473f882001-02-23 17:55:21 +00001136#ifdef DEBUG_HTTP
1137 xmlGenericError(xmlGenericErrorContext,
1138 "-> %s%s", proxy? "(Proxy) " : "", bp);
1139 if ((blen -= strlen(bp)+1) < 0)
1140 xmlGenericError(xmlGenericErrorContext,
1141 "ERROR: overflowed buffer by %d bytes\n", -blen);
1142#endif
1143 ctxt->outptr = ctxt->out = bp;
1144 ctxt->state = XML_NANO_HTTP_WRITE;
Daniel Veillardf012a642001-07-23 19:10:52 +00001145 blen = strlen( ctxt->out );
1146 xmt_bytes = xmlNanoHTTPSend(ctxt, ctxt->out, blen );
1147#ifdef DEBUG_HTTP
1148 if ( xmt_bytes != blen )
1149 xmlGenericError( xmlGenericErrorContext,
1150 "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
1151 xmt_bytes, blen,
1152 "bytes of HTTP headers sent to host",
1153 ctxt->hostname );
1154#endif
1155
1156 if ( input != NULL ) {
1157 xmt_bytes = xmlNanoHTTPSend( ctxt, input, ilen );
1158
1159#ifdef DEBUG_HTTP
1160 if ( xmt_bytes != ilen )
1161 xmlGenericError( xmlGenericErrorContext,
1162 "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
1163 xmt_bytes, ilen,
1164 "bytes of HTTP content sent to host",
1165 ctxt->hostname );
1166#endif
1167 }
1168
Owen Taylor3473f882001-02-23 17:55:21 +00001169 ctxt->state = XML_NANO_HTTP_READ;
1170 head = 1;
1171
1172 while ((p = xmlNanoHTTPReadLine(ctxt)) != NULL) {
1173 if (head && (*p == 0)) {
1174 head = 0;
1175 ctxt->content = ctxt->inrptr;
1176 xmlFree(p);
1177 break;
1178 }
1179 xmlNanoHTTPScanAnswer(ctxt, p);
1180
1181#ifdef DEBUG_HTTP
1182 xmlGenericError(xmlGenericErrorContext, "<- %s\n", p);
1183#endif
1184 xmlFree(p);
1185 }
1186
1187 if ((ctxt->location != NULL) && (ctxt->returnValue >= 300) &&
1188 (ctxt->returnValue < 400)) {
1189#ifdef DEBUG_HTTP
1190 xmlGenericError(xmlGenericErrorContext,
1191 "\nRedirect to: %s\n", ctxt->location);
1192#endif
Daniel Veillardf012a642001-07-23 19:10:52 +00001193 while ( xmlNanoHTTPRecv(ctxt) > 0 ) ;
Owen Taylor3473f882001-02-23 17:55:21 +00001194 if (nbRedirects < XML_NANO_HTTP_MAX_REDIR) {
1195 nbRedirects++;
Daniel Veillard9403a042001-05-28 11:00:53 +00001196 if (redirURL != NULL)
1197 xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001198 redirURL = xmlMemStrdup(ctxt->location);
1199 xmlNanoHTTPFreeCtxt(ctxt);
1200 goto retry;
1201 }
1202 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001203 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001204#ifdef DEBUG_HTTP
1205 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardf012a642001-07-23 19:10:52 +00001206 "xmlNanoHTTPMethodRedir: Too many redirects, aborting ...\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001207#endif
1208 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001209 }
1210
1211 if (contentType != NULL) {
1212 if (ctxt->contentType != NULL)
1213 *contentType = xmlMemStrdup(ctxt->contentType);
1214 else
1215 *contentType = NULL;
1216 }
1217
Daniel Veillard9403a042001-05-28 11:00:53 +00001218 if ((redir != NULL) && (redirURL != NULL)) {
1219 *redir = redirURL;
1220 } else {
1221 if (redirURL != NULL)
1222 xmlFree(redirURL);
1223 if (redir != NULL)
1224 *redir = NULL;
1225 }
1226
Owen Taylor3473f882001-02-23 17:55:21 +00001227#ifdef DEBUG_HTTP
1228 if (ctxt->contentType != NULL)
1229 xmlGenericError(xmlGenericErrorContext,
1230 "\nCode %d, content-type '%s'\n\n",
1231 ctxt->returnValue, ctxt->contentType);
1232 else
1233 xmlGenericError(xmlGenericErrorContext,
1234 "\nCode %d, no content-type\n\n",
1235 ctxt->returnValue);
1236#endif
1237
1238 return((void *) ctxt);
1239}
1240
1241/**
Daniel Veillard9403a042001-05-28 11:00:53 +00001242 * xmlNanoHTTPMethod:
1243 * @URL: The URL to load
1244 * @method: the HTTP method to use
1245 * @input: the input string if any
1246 * @contentType: the Content-Type information IN and OUT
1247 * @headers: the extra headers
1248 *
1249 * This function try to open a connection to the indicated resource
1250 * via HTTP using the given @method, adding the given extra headers
1251 * and the input buffer for the request content.
1252 *
1253 * Returns NULL in case of failure, otherwise a request handler.
1254 * The contentType, if provided must be freed by the caller
1255 */
1256
1257void*
1258xmlNanoHTTPMethod(const char *URL, const char *method, const char *input,
Daniel Veillardf012a642001-07-23 19:10:52 +00001259 char **contentType, const char *headers, int ilen) {
Daniel Veillard9403a042001-05-28 11:00:53 +00001260 return(xmlNanoHTTPMethodRedir(URL, method, input, contentType,
Daniel Veillardf012a642001-07-23 19:10:52 +00001261 NULL, headers, ilen));
Daniel Veillard9403a042001-05-28 11:00:53 +00001262}
1263
1264/**
Owen Taylor3473f882001-02-23 17:55:21 +00001265 * xmlNanoHTTPFetch:
1266 * @URL: The URL to load
1267 * @filename: the filename where the content should be saved
1268 * @contentType: if available the Content-Type information will be
1269 * returned at that location
1270 *
1271 * This function try to fetch the indicated resource via HTTP GET
1272 * and save it's content in the file.
1273 *
1274 * Returns -1 in case of failure, 0 incase of success. The contentType,
1275 * if provided must be freed by the caller
1276 */
1277int
1278xmlNanoHTTPFetch(const char *URL, const char *filename, char **contentType) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001279 void *ctxt = NULL;
1280 char *buf = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001281 int fd;
1282 int len;
1283
1284 ctxt = xmlNanoHTTPOpen(URL, contentType);
1285 if (ctxt == NULL) return(-1);
1286
1287 if (!strcmp(filename, "-"))
1288 fd = 0;
1289 else {
1290 fd = open(filename, O_CREAT | O_WRONLY, 00644);
1291 if (fd < 0) {
1292 xmlNanoHTTPClose(ctxt);
1293 if ((contentType != NULL) && (*contentType != NULL)) {
1294 xmlFree(*contentType);
1295 *contentType = NULL;
1296 }
1297 return(-1);
1298 }
1299 }
1300
Daniel Veillardf012a642001-07-23 19:10:52 +00001301 xmlNanoHTTPFetchContent( ctxt, &buf, &len );
1302 if ( len > 0 ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001303 write(fd, buf, len);
1304 }
1305
1306 xmlNanoHTTPClose(ctxt);
1307 close(fd);
1308 return(0);
1309}
1310
1311/**
1312 * xmlNanoHTTPSave:
1313 * @ctxt: the HTTP context
1314 * @filename: the filename where the content should be saved
1315 *
1316 * This function saves the output of the HTTP transaction to a file
1317 * It closes and free the context at the end
1318 *
1319 * Returns -1 in case of failure, 0 incase of success.
1320 */
1321int
1322xmlNanoHTTPSave(void *ctxt, const char *filename) {
Daniel Veillarde3924972001-07-25 20:25:21 +00001323 char *buf = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001324 int fd;
1325 int len;
1326
1327 if (ctxt == NULL) return(-1);
1328
1329 if (!strcmp(filename, "-"))
1330 fd = 0;
1331 else {
1332 fd = open(filename, O_CREAT | O_WRONLY);
1333 if (fd < 0) {
1334 xmlNanoHTTPClose(ctxt);
1335 return(-1);
1336 }
1337 }
1338
Daniel Veillardf012a642001-07-23 19:10:52 +00001339 xmlNanoHTTPFetchContent( ctxt, &buf, &len );
1340 if ( len > 0 ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001341 write(fd, buf, len);
1342 }
1343
1344 xmlNanoHTTPClose(ctxt);
1345 return(0);
1346}
1347
1348/**
1349 * xmlNanoHTTPReturnCode:
1350 * @ctx: the HTTP context
1351 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001352 * Get the latest HTTP return code received
1353 *
Owen Taylor3473f882001-02-23 17:55:21 +00001354 * Returns the HTTP return code for the request.
1355 */
1356int
1357xmlNanoHTTPReturnCode(void *ctx) {
1358 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1359
1360 if (ctxt == NULL) return(-1);
1361
1362 return(ctxt->returnValue);
1363}
1364
1365/**
1366 * xmlNanoHTTPAuthHeader:
1367 * @ctx: the HTTP context
1368 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001369 * Get the authentication header of an HTTP context
1370 *
Owen Taylor3473f882001-02-23 17:55:21 +00001371 * Returns the stashed value of the WWW-Authenticate or Proxy-Authenticate
1372 * header.
1373 */
1374const char *
1375xmlNanoHTTPAuthHeader(void *ctx) {
1376 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1377
1378 if (ctxt == NULL) return(NULL);
1379
1380 return(ctxt->authHeader);
1381}
1382
Daniel Veillardf012a642001-07-23 19:10:52 +00001383/**
1384 * xmlNanoHTTPContentLength
1385 * @ctx: the HTTP context
1386 *
1387 * Return the specified content length from the HTTP header. Note that
1388 * a value of -1 indicates that the content length element was not included in
1389 * the response header.
1390 */
1391int
1392xmlNanoHTTPContentLength( void * ctx ) {
1393 xmlNanoHTTPCtxtPtr ctxt = ctx;
1394
1395 return ( ( ctxt == NULL ) ? -1 : ctxt->ContentLength );
1396}
1397
1398/**
1399 * xmlNanoHTTPFetchContent
1400 * @ctx: the HTTP context
1401 * @ptr: pointer to set to the content buffer.
1402 * @len: integer pointer to hold the length of the content
1403 *
1404 * Returns 0 if all the content was read and available, returns
1405 * -1 if received content length was less than specified or an error
1406 * occurred.
1407 */
1408int
1409xmlNanoHTTPFetchContent( void * ctx, char ** ptr, int * len ) {
1410 xmlNanoHTTPCtxtPtr ctxt = ctx;
1411
1412 int rc = 0;
1413 int cur_lgth;
1414 int rcvd_lgth;
1415 int dummy_int;
1416 char * dummy_ptr = NULL;
1417
1418 /* Dummy up return input parameters if not provided */
1419
1420 if ( len == NULL )
1421 len = &dummy_int;
1422
1423 if ( ptr == NULL )
1424 ptr = &dummy_ptr;
1425
1426 /* But can't work without the context pointer */
1427
1428 if ( ( ctxt == NULL ) || ( ctxt->content == NULL ) ) {
1429 *len = 0;
1430 *ptr = NULL;
1431 return ( -1 );
1432 }
1433
1434 rcvd_lgth = ctxt->inptr - ctxt->content;
1435
1436 while ( (cur_lgth = xmlNanoHTTPRecv( ctxt )) > 0 ) {
1437
1438 rcvd_lgth += cur_lgth;
1439 if ( (ctxt->ContentLength > 0) && (rcvd_lgth >= ctxt->ContentLength) )
1440 break;
1441 }
1442
1443 *ptr = ctxt->content;
1444 *len = rcvd_lgth;
1445
1446 if ( ( ctxt->ContentLength > 0 ) && ( rcvd_lgth < ctxt->ContentLength ) )
1447 rc = -1;
1448 else if ( rcvd_lgth == 0 )
1449 rc = -1;
1450
1451 return ( rc );
1452}
1453
Owen Taylor3473f882001-02-23 17:55:21 +00001454#ifdef STANDALONE
1455int main(int argc, char **argv) {
1456 char *contentType = NULL;
1457
1458 if (argv[1] != NULL) {
1459 if (argv[2] != NULL)
1460 xmlNanoHTTPFetch(argv[1], argv[2], &contentType);
1461 else
1462 xmlNanoHTTPFetch(argv[1], "-", &contentType);
1463 if (contentType != NULL) xmlFree(contentType);
1464 } else {
1465 xmlGenericError(xmlGenericErrorContext,
1466 "%s: minimal HTTP GET implementation\n", argv[0]);
1467 xmlGenericError(xmlGenericErrorContext,
1468 "\tusage %s [ URL [ filename ] ]\n", argv[0]);
1469 }
1470 xmlNanoHTTPCleanup();
1471 xmlMemoryDump();
1472 return(0);
1473}
1474#endif /* STANDALONE */
1475#else /* !LIBXML_HTTP_ENABLED */
1476#ifdef STANDALONE
1477#include <stdio.h>
1478int main(int argc, char **argv) {
1479 xmlGenericError(xmlGenericErrorContext,
1480 "%s : HTTP support not compiled in\n", argv[0]);
1481 return(0);
1482}
1483#endif /* STANDALONE */
1484#endif /* LIBXML_HTTP_ENABLED */