blob: 9c4b84f1cf36301d5fdc0a5859f40c1296f608c1 [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>
71
72/**
73 * A couple portability macros
74 */
75#ifndef _WINSOCKAPI_
76#define closesocket(s) close(s)
77#define SOCKET int
78#endif
79
Daniel Veillardf012a642001-07-23 19:10:52 +000080
Owen Taylor3473f882001-02-23 17:55:21 +000081#ifdef STANDALONE
82#define DEBUG_HTTP
83#define xmlStrncasecmp(a, b, n) strncasecmp((char *)a, (char *)b, n)
84#define xmlStrcasecmpi(a, b) strcasecmp((char *)a, (char *)b)
85#endif
86
87#define XML_NANO_HTTP_MAX_REDIR 10
88
89#define XML_NANO_HTTP_CHUNK 4096
90
91#define XML_NANO_HTTP_CLOSED 0
92#define XML_NANO_HTTP_WRITE 1
93#define XML_NANO_HTTP_READ 2
94#define XML_NANO_HTTP_NONE 4
95
96typedef struct xmlNanoHTTPCtxt {
97 char *protocol; /* the protocol name */
98 char *hostname; /* the host name */
99 int port; /* the port */
100 char *path; /* the path within the URL */
101 SOCKET fd; /* the file descriptor for the socket */
102 int state; /* WRITE / READ / CLOSED */
103 char *out; /* buffer sent (zero terminated) */
104 char *outptr; /* index within the buffer sent */
105 char *in; /* the receiving buffer */
106 char *content; /* the start of the content */
107 char *inptr; /* the next byte to read from network */
108 char *inrptr; /* the next byte to give back to the client */
109 int inlen; /* len of the input buffer */
110 int last; /* return code for last operation */
111 int returnValue; /* the protocol return value */
Daniel Veillardf012a642001-07-23 19:10:52 +0000112 int ContentLength; /* specified content length from HTTP header */
Owen Taylor3473f882001-02-23 17:55:21 +0000113 char *contentType; /* the MIME type for the input */
114 char *location; /* the new URL in case of redirect */
115 char *authHeader; /* contents of {WWW,Proxy}-Authenticate header */
116} xmlNanoHTTPCtxt, *xmlNanoHTTPCtxtPtr;
117
118static int initialized = 0;
119static char *proxy = NULL; /* the proxy name if any */
120static int proxyPort; /* the proxy port if any */
121static unsigned int timeout = 60;/* the select() timeout in seconds */
122
Daniel Veillardf012a642001-07-23 19:10:52 +0000123int xmlNanoHTTPFetchContent( void * ctx, char ** ptr, int * len );
124int xmlNanoHTTPContentLength( void * ctx );
125
Owen Taylor3473f882001-02-23 17:55:21 +0000126/**
127 * A portability function
128 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000129static int socket_errno(void) {
Owen Taylor3473f882001-02-23 17:55:21 +0000130#ifdef _WINSOCKAPI_
131 return(WSAGetLastError());
132#else
133 return(errno);
134#endif
135}
136
137/**
138 * xmlNanoHTTPInit:
139 *
140 * Initialize the HTTP protocol layer.
141 * Currently it just checks for proxy informations
142 */
143
144void
145xmlNanoHTTPInit(void) {
146 const char *env;
147#ifdef _WINSOCKAPI_
148 WSADATA wsaData;
149#endif
150
151 if (initialized)
152 return;
153
154#ifdef _WINSOCKAPI_
155 if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0)
156 return;
157#endif
158
159 if (proxy == NULL) {
160 proxyPort = 80;
161 env = getenv("no_proxy");
162 if (env != NULL)
163 goto done;
164 env = getenv("http_proxy");
165 if (env != NULL) {
166 xmlNanoHTTPScanProxy(env);
167 goto done;
168 }
169 env = getenv("HTTP_PROXY");
170 if (env != NULL) {
171 xmlNanoHTTPScanProxy(env);
172 goto done;
173 }
174 }
175done:
176 initialized = 1;
177}
178
179/**
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000180 * xmlNanoHTTPCleanup:
Owen Taylor3473f882001-02-23 17:55:21 +0000181 *
182 * Cleanup the HTTP protocol layer.
183 */
184
185void
186xmlNanoHTTPCleanup(void) {
187 if (proxy != NULL)
188 xmlFree(proxy);
189#ifdef _WINSOCKAPI_
190 if (initialized)
191 WSACleanup();
192#endif
193 initialized = 0;
194 return;
195}
196
197/**
Owen Taylor3473f882001-02-23 17:55:21 +0000198 * xmlNanoHTTPScanURL:
199 * @ctxt: an HTTP context
200 * @URL: The URL used to initialize the context
201 *
202 * (Re)Initialize an HTTP context by parsing the URL and finding
203 * the protocol host port and path it indicates.
204 */
205
206static void
207xmlNanoHTTPScanURL(xmlNanoHTTPCtxtPtr ctxt, const char *URL) {
208 const char *cur = URL;
209 char buf[4096];
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000210 int indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000211 int port = 0;
212
213 if (ctxt->protocol != NULL) {
214 xmlFree(ctxt->protocol);
215 ctxt->protocol = NULL;
216 }
217 if (ctxt->hostname != NULL) {
218 xmlFree(ctxt->hostname);
219 ctxt->hostname = NULL;
220 }
221 if (ctxt->path != NULL) {
222 xmlFree(ctxt->path);
223 ctxt->path = NULL;
224 }
225 if (URL == NULL) return;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000226 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000227 while (*cur != 0) {
228 if ((cur[0] == ':') && (cur[1] == '/') && (cur[2] == '/')) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000229 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000230 ctxt->protocol = xmlMemStrdup(buf);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000231 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000232 cur += 3;
233 break;
234 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000235 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000236 }
237 if (*cur == 0) return;
238
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000239 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000240 while (1) {
241 if (cur[0] == ':') {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000242 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000243 ctxt->hostname = xmlMemStrdup(buf);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000244 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000245 cur += 1;
246 while ((*cur >= '0') && (*cur <= '9')) {
247 port *= 10;
248 port += *cur - '0';
249 cur++;
250 }
251 if (port != 0) ctxt->port = port;
252 while ((cur[0] != '/') && (*cur != 0))
253 cur++;
254 break;
255 }
256 if ((*cur == '/') || (*cur == 0)) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000257 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000258 ctxt->hostname = xmlMemStrdup(buf);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000259 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000260 break;
261 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000262 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000263 }
264 if (*cur == 0)
265 ctxt->path = xmlMemStrdup("/");
266 else {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000267 indx = 0;
268 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000269 while (*cur != 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000270 buf[indx++] = *cur++;
271 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000272 ctxt->path = xmlMemStrdup(buf);
273 }
274}
275
276/**
277 * xmlNanoHTTPScanProxy:
278 * @URL: The proxy URL used to initialize the proxy context
279 *
280 * (Re)Initialize the HTTP Proxy context by parsing the URL and finding
281 * the protocol host port it indicates.
282 * Should be like http://myproxy/ or http://myproxy:3128/
283 * A NULL URL cleans up proxy informations.
284 */
285
286void
287xmlNanoHTTPScanProxy(const char *URL) {
288 const char *cur = URL;
289 char buf[4096];
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000290 int indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000291 int port = 0;
292
293 if (proxy != NULL) {
294 xmlFree(proxy);
295 proxy = NULL;
296 }
297 if (proxyPort != 0) {
298 proxyPort = 0;
299 }
300#ifdef DEBUG_HTTP
301 if (URL == NULL)
302 xmlGenericError(xmlGenericErrorContext,
303 "Removing HTTP proxy info\n");
304 else
305 xmlGenericError(xmlGenericErrorContext,
306 "Using HTTP proxy %s\n", URL);
307#endif
308 if (URL == NULL) return;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000309 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000310 while (*cur != 0) {
311 if ((cur[0] == ':') && (cur[1] == '/') && (cur[2] == '/')) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000312 buf[indx] = 0;
313 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000314 cur += 3;
315 break;
316 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000317 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000318 }
319 if (*cur == 0) return;
320
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000321 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000322 while (1) {
323 if (cur[0] == ':') {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000324 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000325 proxy = xmlMemStrdup(buf);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000326 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000327 cur += 1;
328 while ((*cur >= '0') && (*cur <= '9')) {
329 port *= 10;
330 port += *cur - '0';
331 cur++;
332 }
333 if (port != 0) proxyPort = port;
334 while ((cur[0] != '/') && (*cur != 0))
335 cur++;
336 break;
337 }
338 if ((*cur == '/') || (*cur == 0)) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000339 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000340 proxy = xmlMemStrdup(buf);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000341 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000342 break;
343 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000344 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000345 }
346}
347
348/**
349 * xmlNanoHTTPNewCtxt:
350 * @URL: The URL used to initialize the context
351 *
352 * Allocate and initialize a new HTTP context.
353 *
354 * Returns an HTTP context or NULL in case of error.
355 */
356
357static xmlNanoHTTPCtxtPtr
358xmlNanoHTTPNewCtxt(const char *URL) {
359 xmlNanoHTTPCtxtPtr ret;
360
361 ret = (xmlNanoHTTPCtxtPtr) xmlMalloc(sizeof(xmlNanoHTTPCtxt));
362 if (ret == NULL) return(NULL);
363
364 memset(ret, 0, sizeof(xmlNanoHTTPCtxt));
365 ret->port = 80;
366 ret->returnValue = 0;
367 ret->fd = -1;
Daniel Veillardf012a642001-07-23 19:10:52 +0000368 ret->ContentLength = -1;
Owen Taylor3473f882001-02-23 17:55:21 +0000369
370 xmlNanoHTTPScanURL(ret, URL);
371
372 return(ret);
373}
374
375/**
376 * xmlNanoHTTPFreeCtxt:
377 * @ctxt: an HTTP context
378 *
379 * Frees the context after closing the connection.
380 */
381
382static void
383xmlNanoHTTPFreeCtxt(xmlNanoHTTPCtxtPtr ctxt) {
384 if (ctxt == NULL) return;
385 if (ctxt->hostname != NULL) xmlFree(ctxt->hostname);
386 if (ctxt->protocol != NULL) xmlFree(ctxt->protocol);
387 if (ctxt->path != NULL) xmlFree(ctxt->path);
388 if (ctxt->out != NULL) xmlFree(ctxt->out);
389 if (ctxt->in != NULL) xmlFree(ctxt->in);
390 if (ctxt->contentType != NULL) xmlFree(ctxt->contentType);
391 if (ctxt->location != NULL) xmlFree(ctxt->location);
392 if (ctxt->authHeader != NULL) xmlFree(ctxt->authHeader);
393 ctxt->state = XML_NANO_HTTP_NONE;
394 if (ctxt->fd >= 0) closesocket(ctxt->fd);
395 ctxt->fd = -1;
396 xmlFree(ctxt);
397}
398
399/**
400 * xmlNanoHTTPSend:
401 * @ctxt: an HTTP context
402 *
403 * Send the input needed to initiate the processing on the server side
Daniel Veillardf012a642001-07-23 19:10:52 +0000404 * Returns number of bytes sent or -1 on error.
Owen Taylor3473f882001-02-23 17:55:21 +0000405 */
406
Daniel Veillardf012a642001-07-23 19:10:52 +0000407static int
408xmlNanoHTTPSend(xmlNanoHTTPCtxtPtr ctxt, const char * xmt_ptr, int outlen) {
409
410 int total_sent = 0;
411
412 if ( (ctxt->state & XML_NANO_HTTP_WRITE) && (xmt_ptr != NULL ) ) {
413 while (total_sent < outlen) {
414 int nsent = send(ctxt->fd, xmt_ptr + total_sent,
415 outlen - total_sent, 0);
Owen Taylor3473f882001-02-23 17:55:21 +0000416 if (nsent>0)
417 total_sent += nsent;
Daniel Veillardf012a642001-07-23 19:10:52 +0000418 else if ( ( nsent == -1 ) &&
Daniel Veillardba6db032001-07-31 16:25:45 +0000419#if defined(EAGAIN) && EAGAIN != EWOULDBLOCK
Daniel Veillardf012a642001-07-23 19:10:52 +0000420 ( socket_errno( ) != EAGAIN ) &&
Daniel Veillardba6db032001-07-31 16:25:45 +0000421#endif
Daniel Veillardf012a642001-07-23 19:10:52 +0000422 ( socket_errno( ) != EWOULDBLOCK ) ) {
423 xmlGenericError( xmlGenericErrorContext,
424 "xmlNanoHTTPSend error: %s",
425 strerror( socket_errno( ) ) );
426
427 if ( total_sent == 0 )
428 total_sent = -1;
429 break;
430 }
431 else {
432 /*
433 ** No data sent
434 ** Since non-blocking sockets are used, wait for
435 ** socket to be writable or default timeout prior
436 ** to retrying.
437 */
438
439 struct timeval tv;
440 fd_set wfd;
441
442 tv.tv_sec = timeout;
443 tv.tv_usec = 0;
444 FD_ZERO( &wfd );
445 FD_SET( ctxt->fd, &wfd );
446 (void)select( ctxt->fd + 1, NULL, &wfd, NULL, &tv );
447 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000448 }
Owen Taylor3473f882001-02-23 17:55:21 +0000449 }
Daniel Veillardf012a642001-07-23 19:10:52 +0000450
451 return total_sent;
Owen Taylor3473f882001-02-23 17:55:21 +0000452}
453
454/**
455 * xmlNanoHTTPRecv:
456 * @ctxt: an HTTP context
457 *
458 * Read information coming from the HTTP connection.
459 * This is a blocking call (but it blocks in select(), not read()).
460 *
461 * Returns the number of byte read or -1 in case of error.
462 */
463
464static int
465xmlNanoHTTPRecv(xmlNanoHTTPCtxtPtr ctxt) {
466 fd_set rfd;
467 struct timeval tv;
468
469
470 while (ctxt->state & XML_NANO_HTTP_READ) {
471 if (ctxt->in == NULL) {
472 ctxt->in = (char *) xmlMalloc(65000 * sizeof(char));
473 if (ctxt->in == NULL) {
474 ctxt->last = -1;
Daniel Veillardf012a642001-07-23 19:10:52 +0000475 xmlGenericError( xmlGenericErrorContext,
476 "xmlNanoHTTPRecv: Error allocating input memory." );
Owen Taylor3473f882001-02-23 17:55:21 +0000477 return(-1);
478 }
479 ctxt->inlen = 65000;
480 ctxt->inptr = ctxt->content = ctxt->inrptr = ctxt->in;
481 }
482 if (ctxt->inrptr > ctxt->in + XML_NANO_HTTP_CHUNK) {
483 int delta = ctxt->inrptr - ctxt->in;
484 int len = ctxt->inptr - ctxt->inrptr;
485
486 memmove(ctxt->in, ctxt->inrptr, len);
487 ctxt->inrptr -= delta;
488 ctxt->content -= delta;
489 ctxt->inptr -= delta;
490 }
491 if ((ctxt->in + ctxt->inlen) < (ctxt->inptr + XML_NANO_HTTP_CHUNK)) {
492 int d_inptr = ctxt->inptr - ctxt->in;
493 int d_content = ctxt->content - ctxt->in;
494 int d_inrptr = ctxt->inrptr - ctxt->in;
Daniel Veillardf012a642001-07-23 19:10:52 +0000495 char * tmp_ptr = ctxt->in;
Owen Taylor3473f882001-02-23 17:55:21 +0000496
497 ctxt->inlen *= 2;
Daniel Veillardf012a642001-07-23 19:10:52 +0000498 ctxt->in = (char *) xmlRealloc(tmp_ptr, ctxt->inlen);
Owen Taylor3473f882001-02-23 17:55:21 +0000499 if (ctxt->in == NULL) {
Daniel Veillardf012a642001-07-23 19:10:52 +0000500 xmlGenericError( xmlGenericErrorContext,
501 "xmlNanoHTTPRecv: %s %d bytes.",
502 "Failed to realloc input buffer to",
503 ctxt->inlen );
504 xmlFree( tmp_ptr );
Owen Taylor3473f882001-02-23 17:55:21 +0000505 ctxt->last = -1;
506 return(-1);
507 }
508 ctxt->inptr = ctxt->in + d_inptr;
509 ctxt->content = ctxt->in + d_content;
510 ctxt->inrptr = ctxt->in + d_inrptr;
511 }
512 ctxt->last = recv(ctxt->fd, ctxt->inptr, XML_NANO_HTTP_CHUNK, 0);
513 if (ctxt->last > 0) {
514 ctxt->inptr += ctxt->last;
515 return(ctxt->last);
516 }
517 if (ctxt->last == 0) {
518 return(0);
519 }
520 if (ctxt->last == -1) {
521 switch (socket_errno()) {
522 case EINPROGRESS:
523 case EWOULDBLOCK:
524#if defined(EAGAIN) && EAGAIN != EWOULDBLOCK
525 case EAGAIN:
526#endif
527 break;
Daniel Veillardf012a642001-07-23 19:10:52 +0000528
529 case ECONNRESET:
530 case ESHUTDOWN:
531 return ( 0 );
532
Owen Taylor3473f882001-02-23 17:55:21 +0000533 default:
Daniel Veillardf012a642001-07-23 19:10:52 +0000534 xmlGenericError( xmlGenericErrorContext,
535 "xmlNanoHTTPRecv: recv( ) failure - %s",
536 strerror( socket_errno( ) ) );
537 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +0000538 }
539 }
540
541 tv.tv_sec = timeout;
542 tv.tv_usec = 0;
543 FD_ZERO(&rfd);
544 FD_SET(ctxt->fd, &rfd);
545
Daniel Veillard50f34372001-08-03 12:06:36 +0000546 if ( (select(ctxt->fd+1, &rfd, NULL, NULL, &tv)<1)
547#if defined(EINTR)
548 && (errno != EINTR)
549#endif
550 )
Owen Taylor3473f882001-02-23 17:55:21 +0000551 return(0);
552 }
553 return(0);
554}
555
556/**
557 * xmlNanoHTTPReadLine:
558 * @ctxt: an HTTP context
559 *
560 * Read one line in the HTTP server output, usually for extracting
561 * the HTTP protocol informations from the answer header.
562 *
563 * Returns a newly allocated string with a copy of the line, or NULL
564 * which indicate the end of the input.
565 */
566
567static char *
568xmlNanoHTTPReadLine(xmlNanoHTTPCtxtPtr ctxt) {
569 char buf[4096];
570 char *bp = buf;
Daniel Veillardf012a642001-07-23 19:10:52 +0000571 int rc;
Owen Taylor3473f882001-02-23 17:55:21 +0000572
573 while (bp - buf < 4095) {
574 if (ctxt->inrptr == ctxt->inptr) {
Daniel Veillardf012a642001-07-23 19:10:52 +0000575 if ( (rc = xmlNanoHTTPRecv(ctxt)) == 0) {
Owen Taylor3473f882001-02-23 17:55:21 +0000576 if (bp == buf)
577 return(NULL);
578 else
579 *bp = 0;
580 return(xmlMemStrdup(buf));
581 }
Daniel Veillardf012a642001-07-23 19:10:52 +0000582 else if ( rc == -1 ) {
583 return ( NULL );
584 }
Owen Taylor3473f882001-02-23 17:55:21 +0000585 }
586 *bp = *ctxt->inrptr++;
587 if (*bp == '\n') {
588 *bp = 0;
589 return(xmlMemStrdup(buf));
590 }
591 if (*bp != '\r')
592 bp++;
593 }
594 buf[4095] = 0;
595 return(xmlMemStrdup(buf));
596}
597
598
599/**
600 * xmlNanoHTTPScanAnswer:
601 * @ctxt: an HTTP context
602 * @line: an HTTP header line
603 *
604 * Try to extract useful informations from the server answer.
605 * We currently parse and process:
606 * - The HTTP revision/ return code
607 * - The Content-Type
608 * - The Location for redirrect processing.
609 *
610 * Returns -1 in case of failure, the file descriptor number otherwise
611 */
612
613static void
614xmlNanoHTTPScanAnswer(xmlNanoHTTPCtxtPtr ctxt, const char *line) {
615 const char *cur = line;
616
617 if (line == NULL) return;
618
619 if (!strncmp(line, "HTTP/", 5)) {
620 int version = 0;
621 int ret = 0;
622
623 cur += 5;
624 while ((*cur >= '0') && (*cur <= '9')) {
625 version *= 10;
626 version += *cur - '0';
627 cur++;
628 }
629 if (*cur == '.') {
630 cur++;
631 if ((*cur >= '0') && (*cur <= '9')) {
632 version *= 10;
633 version += *cur - '0';
634 cur++;
635 }
636 while ((*cur >= '0') && (*cur <= '9'))
637 cur++;
638 } else
639 version *= 10;
640 if ((*cur != ' ') && (*cur != '\t')) return;
641 while ((*cur == ' ') || (*cur == '\t')) cur++;
642 if ((*cur < '0') || (*cur > '9')) return;
643 while ((*cur >= '0') && (*cur <= '9')) {
644 ret *= 10;
645 ret += *cur - '0';
646 cur++;
647 }
648 if ((*cur != 0) && (*cur != ' ') && (*cur != '\t')) return;
649 ctxt->returnValue = ret;
650 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Content-Type:", 13)) {
651 cur += 13;
652 while ((*cur == ' ') || (*cur == '\t')) cur++;
653 if (ctxt->contentType != NULL)
654 xmlFree(ctxt->contentType);
655 ctxt->contentType = xmlMemStrdup(cur);
656 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"ContentType:", 12)) {
657 cur += 12;
658 if (ctxt->contentType != NULL) return;
659 while ((*cur == ' ') || (*cur == '\t')) cur++;
660 ctxt->contentType = xmlMemStrdup(cur);
661 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Location:", 9)) {
662 cur += 9;
663 while ((*cur == ' ') || (*cur == '\t')) cur++;
664 if (ctxt->location != NULL)
665 xmlFree(ctxt->location);
666 ctxt->location = xmlMemStrdup(cur);
667 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"WWW-Authenticate:", 17)) {
668 cur += 17;
669 while ((*cur == ' ') || (*cur == '\t')) cur++;
670 if (ctxt->authHeader != NULL)
671 xmlFree(ctxt->authHeader);
672 ctxt->authHeader = xmlMemStrdup(cur);
673 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Proxy-Authenticate:", 19)) {
674 cur += 19;
675 while ((*cur == ' ') || (*cur == '\t')) cur++;
676 if (ctxt->authHeader != NULL)
677 xmlFree(ctxt->authHeader);
678 ctxt->authHeader = xmlMemStrdup(cur);
Daniel Veillardf012a642001-07-23 19:10:52 +0000679 } else if ( !xmlStrncasecmp( BAD_CAST line, BAD_CAST"Content-Length:", 15) ) {
680 cur += 15;
681 ctxt->ContentLength = strtol( cur, NULL, 10 );
Owen Taylor3473f882001-02-23 17:55:21 +0000682 }
683}
684
685/**
686 * xmlNanoHTTPConnectAttempt:
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000687 * @addr: a socket adress structure
Owen Taylor3473f882001-02-23 17:55:21 +0000688 *
689 * Attempt a connection to the given IP:port endpoint. It forces
690 * non-blocking semantic on the socket, and allow 60 seconds for
691 * the host to answer.
692 *
693 * Returns -1 in case of failure, the file descriptor number otherwise
694 */
695
696static int
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000697xmlNanoHTTPConnectAttempt(struct sockaddr *addr)
Owen Taylor3473f882001-02-23 17:55:21 +0000698{
699 SOCKET s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
700 fd_set wfd;
701 struct timeval tv;
702 int status;
703
704 if (s==-1) {
705#ifdef DEBUG_HTTP
706 perror("socket");
707#endif
Daniel Veillardf012a642001-07-23 19:10:52 +0000708 xmlGenericError( xmlGenericErrorContext,
709 "xmlNanoHTTPConnectAttempt: %s - %s",
710 "socket creation failure",
711 strerror( socket_errno( ) ) );
Owen Taylor3473f882001-02-23 17:55:21 +0000712 return(-1);
713 }
714
715#ifdef _WINSOCKAPI_
716 {
717 u_long one = 1;
718
719 status = ioctlsocket(s, FIONBIO, &one) == SOCKET_ERROR ? -1 : 0;
720 }
721#else /* _WINSOCKAPI_ */
722#if defined(VMS)
723 {
724 int enable = 1;
725 status = ioctl(s, FIONBIO, &enable);
726 }
727#else /* VMS */
728 if ((status = fcntl(s, F_GETFL, 0)) != -1) {
729#ifdef O_NONBLOCK
730 status |= O_NONBLOCK;
731#else /* O_NONBLOCK */
732#ifdef F_NDELAY
733 status |= F_NDELAY;
734#endif /* F_NDELAY */
735#endif /* !O_NONBLOCK */
736 status = fcntl(s, F_SETFL, status);
737 }
738 if (status < 0) {
739#ifdef DEBUG_HTTP
740 perror("nonblocking");
741#endif
Daniel Veillardf012a642001-07-23 19:10:52 +0000742 xmlGenericError( xmlGenericErrorContext,
743 "xmlNanoHTTPConnectAttempt: %s - %s",
744 "error setting non-blocking IO",
745 strerror( socket_errno( ) ) );
Owen Taylor3473f882001-02-23 17:55:21 +0000746 closesocket(s);
747 return(-1);
748 }
749#endif /* !VMS */
750#endif /* !_WINSOCKAPI_ */
751
Owen Taylor3473f882001-02-23 17:55:21 +0000752 if ((connect(s, addr, sizeof(*addr))==-1)) {
753 switch (socket_errno()) {
754 case EINPROGRESS:
755 case EWOULDBLOCK:
756 break;
757 default:
Daniel Veillardf012a642001-07-23 19:10:52 +0000758 xmlGenericError( xmlGenericErrorContext,
759 "xmlNanoHTTPConnectAttempt: %s - %s",
760 "error connecting to HTTP server",
761 strerror( socket_errno( ) ) );
Owen Taylor3473f882001-02-23 17:55:21 +0000762 closesocket(s);
763 return(-1);
764 }
765 }
766
767 tv.tv_sec = timeout;
768 tv.tv_usec = 0;
769
770 FD_ZERO(&wfd);
771 FD_SET(s, &wfd);
772
773 switch(select(s+1, NULL, &wfd, NULL, &tv))
774 {
775 case 0:
776 /* Time out */
Daniel Veillardf012a642001-07-23 19:10:52 +0000777 xmlGenericError( xmlGenericErrorContext,
778 "xmlNanoHTTPConnectAttempt: %s",
779 "Connect attempt timed out." );
Owen Taylor3473f882001-02-23 17:55:21 +0000780 closesocket(s);
781 return(-1);
782 case -1:
783 /* Ermm.. ?? */
Daniel Veillardf012a642001-07-23 19:10:52 +0000784 xmlGenericError( xmlGenericErrorContext,
785 "xmlNanoHTTPConnectAttempt: %s - %s",
786 "Error connecting to host",
787 strerror( socket_errno( ) ) );
Owen Taylor3473f882001-02-23 17:55:21 +0000788 closesocket(s);
789 return(-1);
790 }
791
792 if ( FD_ISSET(s, &wfd) ) {
793 SOCKLEN_T len;
794 len = sizeof(status);
795 if (getsockopt(s, SOL_SOCKET, SO_ERROR, (char*)&status, &len) < 0 ) {
796 /* Solaris error code */
Daniel Veillardf012a642001-07-23 19:10:52 +0000797 xmlGenericError( xmlGenericErrorContext,
798 "xmlNanoHTTPConnectAttempt: %s - %s",
799 "Error retrieving pending socket errors",
800 strerror( socket_errno( ) ) );
Owen Taylor3473f882001-02-23 17:55:21 +0000801 return (-1);
802 }
803 if ( status ) {
804 closesocket(s);
805 errno = status;
Daniel Veillardf012a642001-07-23 19:10:52 +0000806 xmlGenericError( xmlGenericErrorContext,
807 "xmlNanoHTTPConnectAttempt: %s - %s",
808 "Error connecting to remote host",
809 strerror( status ) );
Owen Taylor3473f882001-02-23 17:55:21 +0000810 return (-1);
811 }
812 } else {
813 /* pbm */
Daniel Veillardf012a642001-07-23 19:10:52 +0000814 xmlGenericError( xmlGenericErrorContext,
815 "xmlNanoHTTPConnectAttempt: %s\n",
816 "Select returned, but descriptor not set for connection.\n" );
817 closesocket(s);
Owen Taylor3473f882001-02-23 17:55:21 +0000818 return (-1);
819 }
820
821 return(s);
822}
823
824/**
825 * xmlNanoHTTPConnectHost:
826 * @host: the host name
827 * @port: the port number
828 *
829 * Attempt a connection to the given host:port endpoint. It tries
830 * the multiple IP provided by the DNS if available.
831 *
832 * Returns -1 in case of failure, the file descriptor number otherwise
833 */
834
835static int
836xmlNanoHTTPConnectHost(const char *host, int port)
837{
838 struct hostent *h;
839 struct sockaddr *addr;
840 struct in_addr ia;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000841 struct sockaddr_in sockin;
Owen Taylor3473f882001-02-23 17:55:21 +0000842#ifdef SUPPORT_IP6
843 struct in6_addr ia6;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000844 struct sockaddr_in6 sockin6;
Owen Taylor3473f882001-02-23 17:55:21 +0000845#endif
846 int i;
847 int s;
848
849#if defined(SUPPORT_IP6) && defined(RES_USE_INET6)
850 if (!(_res.options & RES_INIT))
851 res_init();
852 _res.options |= RES_USE_INET6;
853#endif
854 h=gethostbyname(host);
855 if (h==NULL)
856 {
Daniel Veillardf012a642001-07-23 19:10:52 +0000857 const char * h_err_txt = "";
858 switch ( h_errno )
859 {
860 case HOST_NOT_FOUND:
861 h_err_txt = "Authoritive host not found";
862 break;
863
864 case TRY_AGAIN:
865 h_err_txt =
866 "Non-authoritive host not found or server failure.";
867 break;
868
869 case NO_RECOVERY:
870 h_err_txt =
871 "Non-recoverable errors: FORMERR, REFUSED, or NOTIMP.";
872 break;
873
874 case NO_ADDRESS:
875 h_err_txt = "Valid name, no data record of requested type.";
876 break;
877
878 default:
879 h_err_txt = "No error text defined.";
880 break;
881 }
882 xmlGenericError( xmlGenericErrorContext,
883 "xmlNanoHTTPConnectHost: %s '%s' - %s",
884 "Failed to resolve host", host, h_err_txt );
Owen Taylor3473f882001-02-23 17:55:21 +0000885 return(-1);
886 }
887
888 for(i=0; h->h_addr_list[i]; i++)
889 {
890 if (h->h_addrtype == AF_INET) {
891 /* A records (IPv4) */
892 memcpy(&ia, h->h_addr_list[i], h->h_length);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000893 sockin.sin_family = h->h_addrtype;
894 sockin.sin_addr = ia;
895 sockin.sin_port = htons(port);
896 addr = (struct sockaddr *)&sockin;
Owen Taylor3473f882001-02-23 17:55:21 +0000897#ifdef SUPPORT_IP6
898 } else if (h->h_addrtype == AF_INET6) {
899 /* AAAA records (IPv6) */
900 memcpy(&ia6, h->h_addr_list[i], h->h_length);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000901 sockin6.sin_family = h->h_addrtype;
902 sockin6.sin_addr = ia6;
903 sockin6.sin_port = htons(port);
904 addr = (struct sockaddr *)&sockin6;
Owen Taylor3473f882001-02-23 17:55:21 +0000905#endif
906 } else
907 break; /* for */
908
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000909 s = xmlNanoHTTPConnectAttempt(addr);
Owen Taylor3473f882001-02-23 17:55:21 +0000910 if (s != -1)
911 return(s);
912 }
913
914#ifdef DEBUG_HTTP
915 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardf012a642001-07-23 19:10:52 +0000916 "xmlNanoHTTPConnectHost: unable to connect to '%s'.\n", host);
Owen Taylor3473f882001-02-23 17:55:21 +0000917#endif
918 return(-1);
919}
920
921
922/**
923 * xmlNanoHTTPOpen:
924 * @URL: The URL to load
925 * @contentType: if available the Content-Type information will be
926 * returned at that location
927 *
928 * This function try to open a connection to the indicated resource
929 * via HTTP GET.
930 *
931 * Returns NULL in case of failure, otherwise a request handler.
932 * The contentType, if provided must be freed by the caller
933 */
934
935void*
936xmlNanoHTTPOpen(const char *URL, char **contentType) {
937 if (contentType != NULL) *contentType = NULL;
Daniel Veillardf012a642001-07-23 19:10:52 +0000938 return(xmlNanoHTTPMethod(URL, NULL, NULL, contentType, NULL, 0));
Daniel Veillard9403a042001-05-28 11:00:53 +0000939}
940
941/**
942 * xmlNanoHTTPOpenRedir:
943 * @URL: The URL to load
944 * @contentType: if available the Content-Type information will be
945 * returned at that location
946 * @redir: if availble the redirected URL will be returned
947 *
948 * This function try to open a connection to the indicated resource
949 * via HTTP GET.
950 *
951 * Returns NULL in case of failure, otherwise a request handler.
952 * The contentType, if provided must be freed by the caller
953 */
954
955void*
956xmlNanoHTTPOpenRedir(const char *URL, char **contentType, char **redir) {
957 if (contentType != NULL) *contentType = NULL;
958 if (redir != NULL) *redir = NULL;
Daniel Veillardf012a642001-07-23 19:10:52 +0000959 return(xmlNanoHTTPMethodRedir(URL, NULL, NULL, contentType, redir, NULL,0));
Owen Taylor3473f882001-02-23 17:55:21 +0000960}
961
962/**
963 * xmlNanoHTTPRead:
964 * @ctx: the HTTP context
965 * @dest: a buffer
966 * @len: the buffer length
967 *
968 * This function tries to read @len bytes from the existing HTTP connection
969 * and saves them in @dest. This is a blocking call.
970 *
971 * Returns the number of byte read. 0 is an indication of an end of connection.
972 * -1 indicates a parameter error.
973 */
974int
975xmlNanoHTTPRead(void *ctx, void *dest, int len) {
976 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
977
978 if (ctx == NULL) return(-1);
979 if (dest == NULL) return(-1);
980 if (len <= 0) return(0);
981
982 while (ctxt->inptr - ctxt->inrptr < len) {
Daniel Veillardf012a642001-07-23 19:10:52 +0000983 if (xmlNanoHTTPRecv(ctxt) <= 0) break;
Owen Taylor3473f882001-02-23 17:55:21 +0000984 }
985 if (ctxt->inptr - ctxt->inrptr < len)
986 len = ctxt->inptr - ctxt->inrptr;
987 memcpy(dest, ctxt->inrptr, len);
988 ctxt->inrptr += len;
989 return(len);
990}
991
992/**
993 * xmlNanoHTTPClose:
994 * @ctx: the HTTP context
995 *
996 * This function closes an HTTP context, it ends up the connection and
997 * free all data related to it.
998 */
999void
1000xmlNanoHTTPClose(void *ctx) {
1001 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1002
1003 if (ctx == NULL) return;
1004
1005 xmlNanoHTTPFreeCtxt(ctxt);
1006}
1007
1008/**
Daniel Veillard9403a042001-05-28 11:00:53 +00001009 * xmlNanoHTTPMethodRedir:
Owen Taylor3473f882001-02-23 17:55:21 +00001010 * @URL: The URL to load
1011 * @method: the HTTP method to use
1012 * @input: the input string if any
1013 * @contentType: the Content-Type information IN and OUT
Daniel Veillard9403a042001-05-28 11:00:53 +00001014 * @redir: the redirected URL OUT
Owen Taylor3473f882001-02-23 17:55:21 +00001015 * @headers: the extra headers
1016 *
1017 * This function try to open a connection to the indicated resource
1018 * via HTTP using the given @method, adding the given extra headers
1019 * and the input buffer for the request content.
1020 *
1021 * Returns NULL in case of failure, otherwise a request handler.
Daniel Veillard9403a042001-05-28 11:00:53 +00001022 * The contentType, or redir, if provided must be freed by the caller
Owen Taylor3473f882001-02-23 17:55:21 +00001023 */
1024
1025void*
Daniel Veillard9403a042001-05-28 11:00:53 +00001026xmlNanoHTTPMethodRedir(const char *URL, const char *method, const char *input,
Daniel Veillardf012a642001-07-23 19:10:52 +00001027 char **contentType, char **redir,
1028 const char *headers, int ilen ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001029 xmlNanoHTTPCtxtPtr ctxt;
1030 char *bp, *p;
Daniel Veillardf012a642001-07-23 19:10:52 +00001031 int blen, ret;
Owen Taylor3473f882001-02-23 17:55:21 +00001032 int head;
Daniel Veillardf012a642001-07-23 19:10:52 +00001033 int xmt_bytes;
Owen Taylor3473f882001-02-23 17:55:21 +00001034 int nbRedirects = 0;
1035 char *redirURL = NULL;
1036
1037 if (URL == NULL) return(NULL);
1038 if (method == NULL) method = "GET";
1039 xmlNanoHTTPInit();
1040
1041retry:
1042 if (redirURL == NULL)
1043 ctxt = xmlNanoHTTPNewCtxt(URL);
1044 else {
1045 ctxt = xmlNanoHTTPNewCtxt(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001046 }
1047
Daniel Veillardf012a642001-07-23 19:10:52 +00001048 if ( ctxt == NULL ) {
1049 xmlGenericError( xmlGenericErrorContext,
1050 "xmlNanoHTTPMethodRedir: %s %s.",
1051 "Unable to allocate HTTP context to URI",
1052 ( ( redirURL == NULL ) ? URL : redirURL ) );
1053 return ( NULL );
1054 }
1055
Owen Taylor3473f882001-02-23 17:55:21 +00001056 if ((ctxt->protocol == NULL) || (strcmp(ctxt->protocol, "http"))) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001057 xmlGenericError( xmlGenericErrorContext,
1058 "xmlNanoHTTPMethodRedir: %s - %s.",
1059 "Not a valid HTTP URI",
1060 ( ( redirURL == NULL ) ? URL : redirURL ) );
Owen Taylor3473f882001-02-23 17:55:21 +00001061 xmlNanoHTTPFreeCtxt(ctxt);
1062 if (redirURL != NULL) xmlFree(redirURL);
1063 return(NULL);
1064 }
1065 if (ctxt->hostname == NULL) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001066 xmlGenericError( xmlGenericErrorContext,
1067 "xmlNanoHTTPMethodRedir: %s - %s",
1068 "Failed to identify host in URI",
1069 ( ( redirURL == NULL ) ? URL : redirURL ) );
Owen Taylor3473f882001-02-23 17:55:21 +00001070 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001071 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001072 return(NULL);
1073 }
1074 if (proxy) {
1075 blen = strlen(ctxt->hostname) * 2 + 16;
1076 ret = xmlNanoHTTPConnectHost(proxy, proxyPort);
1077 }
1078 else {
1079 blen = strlen(ctxt->hostname);
1080 ret = xmlNanoHTTPConnectHost(ctxt->hostname, ctxt->port);
1081 }
1082 if (ret < 0) {
1083 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001084 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001085 return(NULL);
1086 }
1087 ctxt->fd = ret;
1088
Daniel Veillardf012a642001-07-23 19:10:52 +00001089 if (input == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00001090 ilen = 0;
Daniel Veillardf012a642001-07-23 19:10:52 +00001091 else
1092 blen += 36;
1093
Owen Taylor3473f882001-02-23 17:55:21 +00001094 if (headers != NULL)
Daniel Veillardf012a642001-07-23 19:10:52 +00001095 blen += strlen(headers) + 2;
Owen Taylor3473f882001-02-23 17:55:21 +00001096 if (contentType && *contentType)
1097 blen += strlen(*contentType) + 16;
Daniel Veillardf012a642001-07-23 19:10:52 +00001098 blen += strlen(method) + strlen(ctxt->path) + 24;
Owen Taylor3473f882001-02-23 17:55:21 +00001099 bp = xmlMalloc(blen);
Daniel Veillardf012a642001-07-23 19:10:52 +00001100 if ( bp == NULL ) {
1101 xmlNanoHTTPFreeCtxt( ctxt );
1102 xmlGenericError( xmlGenericErrorContext,
1103 "xmlNanoHTTPMethodRedir: %s",
1104 "Error allocating HTTP header buffer." );
1105 return ( NULL );
1106 }
1107
1108 p = bp;
1109
Owen Taylor3473f882001-02-23 17:55:21 +00001110 if (proxy) {
1111 if (ctxt->port != 80) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001112 p += sprintf( p, "%s http://%s:%d%s", method, ctxt->hostname,
1113 ctxt->port, ctxt->path );
Owen Taylor3473f882001-02-23 17:55:21 +00001114 }
1115 else
Daniel Veillardf012a642001-07-23 19:10:52 +00001116 p += sprintf( p, "%s http://%s%s", method,
1117 ctxt->hostname, ctxt->path);
Owen Taylor3473f882001-02-23 17:55:21 +00001118 }
1119 else
Daniel Veillardf012a642001-07-23 19:10:52 +00001120 p += sprintf( p, "%s %s", method, ctxt->path);
1121
1122 p += sprintf(p, " HTTP/1.0\r\nHost: %s\r\n", ctxt->hostname);
1123
1124 if (contentType != NULL && *contentType)
1125 p += sprintf(p, "Content-Type: %s\r\n", *contentType);
1126
1127 if (headers != NULL)
1128 p += sprintf( p, "%s", headers );
1129
Owen Taylor3473f882001-02-23 17:55:21 +00001130 if (input != NULL)
Daniel Veillardf012a642001-07-23 19:10:52 +00001131 sprintf(p, "Content-Length: %d\r\n\r\n", ilen );
Owen Taylor3473f882001-02-23 17:55:21 +00001132 else
1133 strcpy(p, "\r\n");
Daniel Veillardf012a642001-07-23 19:10:52 +00001134
Owen Taylor3473f882001-02-23 17:55:21 +00001135#ifdef DEBUG_HTTP
1136 xmlGenericError(xmlGenericErrorContext,
1137 "-> %s%s", proxy? "(Proxy) " : "", bp);
1138 if ((blen -= strlen(bp)+1) < 0)
1139 xmlGenericError(xmlGenericErrorContext,
1140 "ERROR: overflowed buffer by %d bytes\n", -blen);
1141#endif
1142 ctxt->outptr = ctxt->out = bp;
1143 ctxt->state = XML_NANO_HTTP_WRITE;
Daniel Veillardf012a642001-07-23 19:10:52 +00001144 blen = strlen( ctxt->out );
1145 xmt_bytes = xmlNanoHTTPSend(ctxt, ctxt->out, blen );
1146#ifdef DEBUG_HTTP
1147 if ( xmt_bytes != blen )
1148 xmlGenericError( xmlGenericErrorContext,
1149 "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
1150 xmt_bytes, blen,
1151 "bytes of HTTP headers sent to host",
1152 ctxt->hostname );
1153#endif
1154
1155 if ( input != NULL ) {
1156 xmt_bytes = xmlNanoHTTPSend( ctxt, input, ilen );
1157
1158#ifdef DEBUG_HTTP
1159 if ( xmt_bytes != ilen )
1160 xmlGenericError( xmlGenericErrorContext,
1161 "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
1162 xmt_bytes, ilen,
1163 "bytes of HTTP content sent to host",
1164 ctxt->hostname );
1165#endif
1166 }
1167
Owen Taylor3473f882001-02-23 17:55:21 +00001168 ctxt->state = XML_NANO_HTTP_READ;
1169 head = 1;
1170
1171 while ((p = xmlNanoHTTPReadLine(ctxt)) != NULL) {
1172 if (head && (*p == 0)) {
1173 head = 0;
1174 ctxt->content = ctxt->inrptr;
1175 xmlFree(p);
1176 break;
1177 }
1178 xmlNanoHTTPScanAnswer(ctxt, p);
1179
1180#ifdef DEBUG_HTTP
1181 xmlGenericError(xmlGenericErrorContext, "<- %s\n", p);
1182#endif
1183 xmlFree(p);
1184 }
1185
1186 if ((ctxt->location != NULL) && (ctxt->returnValue >= 300) &&
1187 (ctxt->returnValue < 400)) {
1188#ifdef DEBUG_HTTP
1189 xmlGenericError(xmlGenericErrorContext,
1190 "\nRedirect to: %s\n", ctxt->location);
1191#endif
Daniel Veillardf012a642001-07-23 19:10:52 +00001192 while ( xmlNanoHTTPRecv(ctxt) > 0 ) ;
Owen Taylor3473f882001-02-23 17:55:21 +00001193 if (nbRedirects < XML_NANO_HTTP_MAX_REDIR) {
1194 nbRedirects++;
Daniel Veillard9403a042001-05-28 11:00:53 +00001195 if (redirURL != NULL)
1196 xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001197 redirURL = xmlMemStrdup(ctxt->location);
1198 xmlNanoHTTPFreeCtxt(ctxt);
1199 goto retry;
1200 }
1201 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001202 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001203#ifdef DEBUG_HTTP
1204 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardf012a642001-07-23 19:10:52 +00001205 "xmlNanoHTTPMethodRedir: Too many redirects, aborting ...\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001206#endif
1207 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001208 }
1209
1210 if (contentType != NULL) {
1211 if (ctxt->contentType != NULL)
1212 *contentType = xmlMemStrdup(ctxt->contentType);
1213 else
1214 *contentType = NULL;
1215 }
1216
Daniel Veillard9403a042001-05-28 11:00:53 +00001217 if ((redir != NULL) && (redirURL != NULL)) {
1218 *redir = redirURL;
1219 } else {
1220 if (redirURL != NULL)
1221 xmlFree(redirURL);
1222 if (redir != NULL)
1223 *redir = NULL;
1224 }
1225
Owen Taylor3473f882001-02-23 17:55:21 +00001226#ifdef DEBUG_HTTP
1227 if (ctxt->contentType != NULL)
1228 xmlGenericError(xmlGenericErrorContext,
1229 "\nCode %d, content-type '%s'\n\n",
1230 ctxt->returnValue, ctxt->contentType);
1231 else
1232 xmlGenericError(xmlGenericErrorContext,
1233 "\nCode %d, no content-type\n\n",
1234 ctxt->returnValue);
1235#endif
1236
1237 return((void *) ctxt);
1238}
1239
1240/**
Daniel Veillard9403a042001-05-28 11:00:53 +00001241 * xmlNanoHTTPMethod:
1242 * @URL: The URL to load
1243 * @method: the HTTP method to use
1244 * @input: the input string if any
1245 * @contentType: the Content-Type information IN and OUT
1246 * @headers: the extra headers
1247 *
1248 * This function try to open a connection to the indicated resource
1249 * via HTTP using the given @method, adding the given extra headers
1250 * and the input buffer for the request content.
1251 *
1252 * Returns NULL in case of failure, otherwise a request handler.
1253 * The contentType, if provided must be freed by the caller
1254 */
1255
1256void*
1257xmlNanoHTTPMethod(const char *URL, const char *method, const char *input,
Daniel Veillardf012a642001-07-23 19:10:52 +00001258 char **contentType, const char *headers, int ilen) {
Daniel Veillard9403a042001-05-28 11:00:53 +00001259 return(xmlNanoHTTPMethodRedir(URL, method, input, contentType,
Daniel Veillardf012a642001-07-23 19:10:52 +00001260 NULL, headers, ilen));
Daniel Veillard9403a042001-05-28 11:00:53 +00001261}
1262
1263/**
Owen Taylor3473f882001-02-23 17:55:21 +00001264 * xmlNanoHTTPFetch:
1265 * @URL: The URL to load
1266 * @filename: the filename where the content should be saved
1267 * @contentType: if available the Content-Type information will be
1268 * returned at that location
1269 *
1270 * This function try to fetch the indicated resource via HTTP GET
1271 * and save it's content in the file.
1272 *
1273 * Returns -1 in case of failure, 0 incase of success. The contentType,
1274 * if provided must be freed by the caller
1275 */
1276int
1277xmlNanoHTTPFetch(const char *URL, const char *filename, char **contentType) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001278 void *ctxt = NULL;
1279 char *buf = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001280 int fd;
1281 int len;
1282
1283 ctxt = xmlNanoHTTPOpen(URL, contentType);
1284 if (ctxt == NULL) return(-1);
1285
1286 if (!strcmp(filename, "-"))
1287 fd = 0;
1288 else {
1289 fd = open(filename, O_CREAT | O_WRONLY, 00644);
1290 if (fd < 0) {
1291 xmlNanoHTTPClose(ctxt);
1292 if ((contentType != NULL) && (*contentType != NULL)) {
1293 xmlFree(*contentType);
1294 *contentType = NULL;
1295 }
1296 return(-1);
1297 }
1298 }
1299
Daniel Veillardf012a642001-07-23 19:10:52 +00001300 xmlNanoHTTPFetchContent( ctxt, &buf, &len );
1301 if ( len > 0 ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001302 write(fd, buf, len);
1303 }
1304
1305 xmlNanoHTTPClose(ctxt);
1306 close(fd);
1307 return(0);
1308}
1309
1310/**
1311 * xmlNanoHTTPSave:
1312 * @ctxt: the HTTP context
1313 * @filename: the filename where the content should be saved
1314 *
1315 * This function saves the output of the HTTP transaction to a file
1316 * It closes and free the context at the end
1317 *
1318 * Returns -1 in case of failure, 0 incase of success.
1319 */
1320int
1321xmlNanoHTTPSave(void *ctxt, const char *filename) {
Daniel Veillarde3924972001-07-25 20:25:21 +00001322 char *buf = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001323 int fd;
1324 int len;
1325
1326 if (ctxt == NULL) return(-1);
1327
1328 if (!strcmp(filename, "-"))
1329 fd = 0;
1330 else {
1331 fd = open(filename, O_CREAT | O_WRONLY);
1332 if (fd < 0) {
1333 xmlNanoHTTPClose(ctxt);
1334 return(-1);
1335 }
1336 }
1337
Daniel Veillardf012a642001-07-23 19:10:52 +00001338 xmlNanoHTTPFetchContent( ctxt, &buf, &len );
1339 if ( len > 0 ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001340 write(fd, buf, len);
1341 }
1342
1343 xmlNanoHTTPClose(ctxt);
1344 return(0);
1345}
1346
1347/**
1348 * xmlNanoHTTPReturnCode:
1349 * @ctx: the HTTP context
1350 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001351 * Get the latest HTTP return code received
1352 *
Owen Taylor3473f882001-02-23 17:55:21 +00001353 * Returns the HTTP return code for the request.
1354 */
1355int
1356xmlNanoHTTPReturnCode(void *ctx) {
1357 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1358
1359 if (ctxt == NULL) return(-1);
1360
1361 return(ctxt->returnValue);
1362}
1363
1364/**
1365 * xmlNanoHTTPAuthHeader:
1366 * @ctx: the HTTP context
1367 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001368 * Get the authentication header of an HTTP context
1369 *
Owen Taylor3473f882001-02-23 17:55:21 +00001370 * Returns the stashed value of the WWW-Authenticate or Proxy-Authenticate
1371 * header.
1372 */
1373const char *
1374xmlNanoHTTPAuthHeader(void *ctx) {
1375 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1376
1377 if (ctxt == NULL) return(NULL);
1378
1379 return(ctxt->authHeader);
1380}
1381
Daniel Veillardf012a642001-07-23 19:10:52 +00001382/**
1383 * xmlNanoHTTPContentLength
1384 * @ctx: the HTTP context
1385 *
1386 * Return the specified content length from the HTTP header. Note that
1387 * a value of -1 indicates that the content length element was not included in
1388 * the response header.
1389 */
1390int
1391xmlNanoHTTPContentLength( void * ctx ) {
1392 xmlNanoHTTPCtxtPtr ctxt = ctx;
1393
1394 return ( ( ctxt == NULL ) ? -1 : ctxt->ContentLength );
1395}
1396
1397/**
1398 * xmlNanoHTTPFetchContent
1399 * @ctx: the HTTP context
1400 * @ptr: pointer to set to the content buffer.
1401 * @len: integer pointer to hold the length of the content
1402 *
1403 * Returns 0 if all the content was read and available, returns
1404 * -1 if received content length was less than specified or an error
1405 * occurred.
1406 */
1407int
1408xmlNanoHTTPFetchContent( void * ctx, char ** ptr, int * len ) {
1409 xmlNanoHTTPCtxtPtr ctxt = ctx;
1410
1411 int rc = 0;
1412 int cur_lgth;
1413 int rcvd_lgth;
1414 int dummy_int;
1415 char * dummy_ptr = NULL;
1416
1417 /* Dummy up return input parameters if not provided */
1418
1419 if ( len == NULL )
1420 len = &dummy_int;
1421
1422 if ( ptr == NULL )
1423 ptr = &dummy_ptr;
1424
1425 /* But can't work without the context pointer */
1426
1427 if ( ( ctxt == NULL ) || ( ctxt->content == NULL ) ) {
1428 *len = 0;
1429 *ptr = NULL;
1430 return ( -1 );
1431 }
1432
1433 rcvd_lgth = ctxt->inptr - ctxt->content;
1434
1435 while ( (cur_lgth = xmlNanoHTTPRecv( ctxt )) > 0 ) {
1436
1437 rcvd_lgth += cur_lgth;
1438 if ( (ctxt->ContentLength > 0) && (rcvd_lgth >= ctxt->ContentLength) )
1439 break;
1440 }
1441
1442 *ptr = ctxt->content;
1443 *len = rcvd_lgth;
1444
1445 if ( ( ctxt->ContentLength > 0 ) && ( rcvd_lgth < ctxt->ContentLength ) )
1446 rc = -1;
1447 else if ( rcvd_lgth == 0 )
1448 rc = -1;
1449
1450 return ( rc );
1451}
1452
Owen Taylor3473f882001-02-23 17:55:21 +00001453#ifdef STANDALONE
1454int main(int argc, char **argv) {
1455 char *contentType = NULL;
1456
1457 if (argv[1] != NULL) {
1458 if (argv[2] != NULL)
1459 xmlNanoHTTPFetch(argv[1], argv[2], &contentType);
1460 else
1461 xmlNanoHTTPFetch(argv[1], "-", &contentType);
1462 if (contentType != NULL) xmlFree(contentType);
1463 } else {
1464 xmlGenericError(xmlGenericErrorContext,
1465 "%s: minimal HTTP GET implementation\n", argv[0]);
1466 xmlGenericError(xmlGenericErrorContext,
1467 "\tusage %s [ URL [ filename ] ]\n", argv[0]);
1468 }
1469 xmlNanoHTTPCleanup();
1470 xmlMemoryDump();
1471 return(0);
1472}
1473#endif /* STANDALONE */
1474#else /* !LIBXML_HTTP_ENABLED */
1475#ifdef STANDALONE
1476#include <stdio.h>
1477int main(int argc, char **argv) {
1478 xmlGenericError(xmlGenericErrorContext,
1479 "%s : HTTP support not compiled in\n", argv[0]);
1480 return(0);
1481}
1482#endif /* STANDALONE */
1483#endif /* LIBXML_HTTP_ENABLED */