blob: 8fade163530e5ed700184ae168bacaf84f8765c6 [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 Veillardf012a642001-07-23 19:10:52 +000066#include <libxml/xmlerror.h>
Owen Taylor3473f882001-02-23 17:55:21 +000067#include <libxml/xmlmemory.h>
68#include <libxml/parser.h> /* for xmlStr(n)casecmp() */
69#include <libxml/nanohttp.h>
70
71/**
72 * A couple portability macros
73 */
74#ifndef _WINSOCKAPI_
75#define closesocket(s) close(s)
76#define SOCKET int
77#endif
78
Daniel Veillardf012a642001-07-23 19:10:52 +000079
Owen Taylor3473f882001-02-23 17:55:21 +000080#ifdef STANDALONE
81#define DEBUG_HTTP
82#define xmlStrncasecmp(a, b, n) strncasecmp((char *)a, (char *)b, n)
83#define xmlStrcasecmpi(a, b) strcasecmp((char *)a, (char *)b)
84#endif
85
86#define XML_NANO_HTTP_MAX_REDIR 10
87
88#define XML_NANO_HTTP_CHUNK 4096
89
90#define XML_NANO_HTTP_CLOSED 0
91#define XML_NANO_HTTP_WRITE 1
92#define XML_NANO_HTTP_READ 2
93#define XML_NANO_HTTP_NONE 4
94
95typedef struct xmlNanoHTTPCtxt {
96 char *protocol; /* the protocol name */
97 char *hostname; /* the host name */
98 int port; /* the port */
99 char *path; /* the path within the URL */
100 SOCKET fd; /* the file descriptor for the socket */
101 int state; /* WRITE / READ / CLOSED */
102 char *out; /* buffer sent (zero terminated) */
103 char *outptr; /* index within the buffer sent */
104 char *in; /* the receiving buffer */
105 char *content; /* the start of the content */
106 char *inptr; /* the next byte to read from network */
107 char *inrptr; /* the next byte to give back to the client */
108 int inlen; /* len of the input buffer */
109 int last; /* return code for last operation */
110 int returnValue; /* the protocol return value */
Daniel Veillardf012a642001-07-23 19:10:52 +0000111 int ContentLength; /* specified content length from HTTP header */
Owen Taylor3473f882001-02-23 17:55:21 +0000112 char *contentType; /* the MIME type for the input */
113 char *location; /* the new URL in case of redirect */
114 char *authHeader; /* contents of {WWW,Proxy}-Authenticate header */
115} xmlNanoHTTPCtxt, *xmlNanoHTTPCtxtPtr;
116
117static int initialized = 0;
118static char *proxy = NULL; /* the proxy name if any */
119static int proxyPort; /* the proxy port if any */
120static unsigned int timeout = 60;/* the select() timeout in seconds */
121
Daniel Veillardf012a642001-07-23 19:10:52 +0000122int xmlNanoHTTPFetchContent( void * ctx, char ** ptr, int * len );
123int xmlNanoHTTPContentLength( void * ctx );
124
Owen Taylor3473f882001-02-23 17:55:21 +0000125/**
126 * A portability function
127 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000128static int socket_errno(void) {
Owen Taylor3473f882001-02-23 17:55:21 +0000129#ifdef _WINSOCKAPI_
130 return(WSAGetLastError());
131#else
132 return(errno);
133#endif
134}
135
136/**
137 * xmlNanoHTTPInit:
138 *
139 * Initialize the HTTP protocol layer.
140 * Currently it just checks for proxy informations
141 */
142
143void
144xmlNanoHTTPInit(void) {
145 const char *env;
146#ifdef _WINSOCKAPI_
147 WSADATA wsaData;
148#endif
149
150 if (initialized)
151 return;
152
153#ifdef _WINSOCKAPI_
154 if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0)
155 return;
156#endif
157
158 if (proxy == NULL) {
159 proxyPort = 80;
160 env = getenv("no_proxy");
161 if (env != NULL)
162 goto done;
163 env = getenv("http_proxy");
164 if (env != NULL) {
165 xmlNanoHTTPScanProxy(env);
166 goto done;
167 }
168 env = getenv("HTTP_PROXY");
169 if (env != NULL) {
170 xmlNanoHTTPScanProxy(env);
171 goto done;
172 }
173 }
174done:
175 initialized = 1;
176}
177
178/**
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000179 * xmlNanoHTTPCleanup:
Owen Taylor3473f882001-02-23 17:55:21 +0000180 *
181 * Cleanup the HTTP protocol layer.
182 */
183
184void
185xmlNanoHTTPCleanup(void) {
186 if (proxy != NULL)
187 xmlFree(proxy);
188#ifdef _WINSOCKAPI_
189 if (initialized)
190 WSACleanup();
191#endif
192 initialized = 0;
193 return;
194}
195
196/**
Owen Taylor3473f882001-02-23 17:55:21 +0000197 * xmlNanoHTTPScanURL:
198 * @ctxt: an HTTP context
199 * @URL: The URL used to initialize the context
200 *
201 * (Re)Initialize an HTTP context by parsing the URL and finding
202 * the protocol host port and path it indicates.
203 */
204
205static void
206xmlNanoHTTPScanURL(xmlNanoHTTPCtxtPtr ctxt, const char *URL) {
207 const char *cur = URL;
208 char buf[4096];
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000209 int indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000210 int port = 0;
211
212 if (ctxt->protocol != NULL) {
213 xmlFree(ctxt->protocol);
214 ctxt->protocol = NULL;
215 }
216 if (ctxt->hostname != NULL) {
217 xmlFree(ctxt->hostname);
218 ctxt->hostname = NULL;
219 }
220 if (ctxt->path != NULL) {
221 xmlFree(ctxt->path);
222 ctxt->path = NULL;
223 }
224 if (URL == NULL) return;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000225 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000226 while (*cur != 0) {
227 if ((cur[0] == ':') && (cur[1] == '/') && (cur[2] == '/')) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000228 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000229 ctxt->protocol = xmlMemStrdup(buf);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000230 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000231 cur += 3;
232 break;
233 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000234 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000235 }
236 if (*cur == 0) return;
237
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000238 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000239 while (1) {
240 if (cur[0] == ':') {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000241 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000242 ctxt->hostname = xmlMemStrdup(buf);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000243 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000244 cur += 1;
245 while ((*cur >= '0') && (*cur <= '9')) {
246 port *= 10;
247 port += *cur - '0';
248 cur++;
249 }
250 if (port != 0) ctxt->port = port;
251 while ((cur[0] != '/') && (*cur != 0))
252 cur++;
253 break;
254 }
255 if ((*cur == '/') || (*cur == 0)) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000256 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000257 ctxt->hostname = xmlMemStrdup(buf);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000258 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000259 break;
260 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000261 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000262 }
263 if (*cur == 0)
264 ctxt->path = xmlMemStrdup("/");
265 else {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000266 indx = 0;
267 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000268 while (*cur != 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000269 buf[indx++] = *cur++;
270 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000271 ctxt->path = xmlMemStrdup(buf);
272 }
273}
274
275/**
276 * xmlNanoHTTPScanProxy:
277 * @URL: The proxy URL used to initialize the proxy context
278 *
279 * (Re)Initialize the HTTP Proxy context by parsing the URL and finding
280 * the protocol host port it indicates.
281 * Should be like http://myproxy/ or http://myproxy:3128/
282 * A NULL URL cleans up proxy informations.
283 */
284
285void
286xmlNanoHTTPScanProxy(const char *URL) {
287 const char *cur = URL;
288 char buf[4096];
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000289 int indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000290 int port = 0;
291
292 if (proxy != NULL) {
293 xmlFree(proxy);
294 proxy = NULL;
295 }
296 if (proxyPort != 0) {
297 proxyPort = 0;
298 }
299#ifdef DEBUG_HTTP
300 if (URL == NULL)
301 xmlGenericError(xmlGenericErrorContext,
302 "Removing HTTP proxy info\n");
303 else
304 xmlGenericError(xmlGenericErrorContext,
305 "Using HTTP proxy %s\n", URL);
306#endif
307 if (URL == NULL) return;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000308 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000309 while (*cur != 0) {
310 if ((cur[0] == ':') && (cur[1] == '/') && (cur[2] == '/')) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000311 buf[indx] = 0;
312 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000313 cur += 3;
314 break;
315 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000316 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000317 }
318 if (*cur == 0) return;
319
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000320 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000321 while (1) {
322 if (cur[0] == ':') {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000323 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000324 proxy = xmlMemStrdup(buf);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000325 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000326 cur += 1;
327 while ((*cur >= '0') && (*cur <= '9')) {
328 port *= 10;
329 port += *cur - '0';
330 cur++;
331 }
332 if (port != 0) proxyPort = port;
333 while ((cur[0] != '/') && (*cur != 0))
334 cur++;
335 break;
336 }
337 if ((*cur == '/') || (*cur == 0)) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000338 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000339 proxy = xmlMemStrdup(buf);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000340 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000341 break;
342 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000343 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000344 }
345}
346
347/**
348 * xmlNanoHTTPNewCtxt:
349 * @URL: The URL used to initialize the context
350 *
351 * Allocate and initialize a new HTTP context.
352 *
353 * Returns an HTTP context or NULL in case of error.
354 */
355
356static xmlNanoHTTPCtxtPtr
357xmlNanoHTTPNewCtxt(const char *URL) {
358 xmlNanoHTTPCtxtPtr ret;
359
360 ret = (xmlNanoHTTPCtxtPtr) xmlMalloc(sizeof(xmlNanoHTTPCtxt));
361 if (ret == NULL) return(NULL);
362
363 memset(ret, 0, sizeof(xmlNanoHTTPCtxt));
364 ret->port = 80;
365 ret->returnValue = 0;
366 ret->fd = -1;
Daniel Veillardf012a642001-07-23 19:10:52 +0000367 ret->ContentLength = -1;
Owen Taylor3473f882001-02-23 17:55:21 +0000368
369 xmlNanoHTTPScanURL(ret, URL);
370
371 return(ret);
372}
373
374/**
375 * xmlNanoHTTPFreeCtxt:
376 * @ctxt: an HTTP context
377 *
378 * Frees the context after closing the connection.
379 */
380
381static void
382xmlNanoHTTPFreeCtxt(xmlNanoHTTPCtxtPtr ctxt) {
383 if (ctxt == NULL) return;
384 if (ctxt->hostname != NULL) xmlFree(ctxt->hostname);
385 if (ctxt->protocol != NULL) xmlFree(ctxt->protocol);
386 if (ctxt->path != NULL) xmlFree(ctxt->path);
387 if (ctxt->out != NULL) xmlFree(ctxt->out);
388 if (ctxt->in != NULL) xmlFree(ctxt->in);
389 if (ctxt->contentType != NULL) xmlFree(ctxt->contentType);
390 if (ctxt->location != NULL) xmlFree(ctxt->location);
391 if (ctxt->authHeader != NULL) xmlFree(ctxt->authHeader);
392 ctxt->state = XML_NANO_HTTP_NONE;
393 if (ctxt->fd >= 0) closesocket(ctxt->fd);
394 ctxt->fd = -1;
395 xmlFree(ctxt);
396}
397
398/**
399 * xmlNanoHTTPSend:
400 * @ctxt: an HTTP context
401 *
402 * Send the input needed to initiate the processing on the server side
Daniel Veillardf012a642001-07-23 19:10:52 +0000403 * Returns number of bytes sent or -1 on error.
Owen Taylor3473f882001-02-23 17:55:21 +0000404 */
405
Daniel Veillardf012a642001-07-23 19:10:52 +0000406static int
407xmlNanoHTTPSend(xmlNanoHTTPCtxtPtr ctxt, const char * xmt_ptr, int outlen) {
408
409 int total_sent = 0;
410
411 if ( (ctxt->state & XML_NANO_HTTP_WRITE) && (xmt_ptr != NULL ) ) {
412 while (total_sent < outlen) {
413 int nsent = send(ctxt->fd, xmt_ptr + total_sent,
414 outlen - total_sent, 0);
Owen Taylor3473f882001-02-23 17:55:21 +0000415 if (nsent>0)
416 total_sent += nsent;
Daniel Veillardf012a642001-07-23 19:10:52 +0000417 else if ( ( nsent == -1 ) &&
Daniel Veillardba6db032001-07-31 16:25:45 +0000418#if defined(EAGAIN) && EAGAIN != EWOULDBLOCK
Daniel Veillardf012a642001-07-23 19:10:52 +0000419 ( socket_errno( ) != EAGAIN ) &&
Daniel Veillardba6db032001-07-31 16:25:45 +0000420#endif
Daniel Veillardf012a642001-07-23 19:10:52 +0000421 ( socket_errno( ) != EWOULDBLOCK ) ) {
422 xmlGenericError( xmlGenericErrorContext,
423 "xmlNanoHTTPSend error: %s",
424 strerror( socket_errno( ) ) );
425
426 if ( total_sent == 0 )
427 total_sent = -1;
428 break;
429 }
430 else {
431 /*
432 ** No data sent
433 ** Since non-blocking sockets are used, wait for
434 ** socket to be writable or default timeout prior
435 ** to retrying.
436 */
437
438 struct timeval tv;
439 fd_set wfd;
440
441 tv.tv_sec = timeout;
442 tv.tv_usec = 0;
443 FD_ZERO( &wfd );
444 FD_SET( ctxt->fd, &wfd );
445 (void)select( ctxt->fd + 1, NULL, &wfd, NULL, &tv );
446 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000447 }
Owen Taylor3473f882001-02-23 17:55:21 +0000448 }
Daniel Veillardf012a642001-07-23 19:10:52 +0000449
450 return total_sent;
Owen Taylor3473f882001-02-23 17:55:21 +0000451}
452
453/**
454 * xmlNanoHTTPRecv:
455 * @ctxt: an HTTP context
456 *
457 * Read information coming from the HTTP connection.
458 * This is a blocking call (but it blocks in select(), not read()).
459 *
460 * Returns the number of byte read or -1 in case of error.
461 */
462
463static int
464xmlNanoHTTPRecv(xmlNanoHTTPCtxtPtr ctxt) {
465 fd_set rfd;
466 struct timeval tv;
467
468
469 while (ctxt->state & XML_NANO_HTTP_READ) {
470 if (ctxt->in == NULL) {
471 ctxt->in = (char *) xmlMalloc(65000 * sizeof(char));
472 if (ctxt->in == NULL) {
473 ctxt->last = -1;
Daniel Veillardf012a642001-07-23 19:10:52 +0000474 xmlGenericError( xmlGenericErrorContext,
475 "xmlNanoHTTPRecv: Error allocating input memory." );
Owen Taylor3473f882001-02-23 17:55:21 +0000476 return(-1);
477 }
478 ctxt->inlen = 65000;
479 ctxt->inptr = ctxt->content = ctxt->inrptr = ctxt->in;
480 }
481 if (ctxt->inrptr > ctxt->in + XML_NANO_HTTP_CHUNK) {
482 int delta = ctxt->inrptr - ctxt->in;
483 int len = ctxt->inptr - ctxt->inrptr;
484
485 memmove(ctxt->in, ctxt->inrptr, len);
486 ctxt->inrptr -= delta;
487 ctxt->content -= delta;
488 ctxt->inptr -= delta;
489 }
490 if ((ctxt->in + ctxt->inlen) < (ctxt->inptr + XML_NANO_HTTP_CHUNK)) {
491 int d_inptr = ctxt->inptr - ctxt->in;
492 int d_content = ctxt->content - ctxt->in;
493 int d_inrptr = ctxt->inrptr - ctxt->in;
Daniel Veillardf012a642001-07-23 19:10:52 +0000494 char * tmp_ptr = ctxt->in;
Owen Taylor3473f882001-02-23 17:55:21 +0000495
496 ctxt->inlen *= 2;
Daniel Veillardf012a642001-07-23 19:10:52 +0000497 ctxt->in = (char *) xmlRealloc(tmp_ptr, ctxt->inlen);
Owen Taylor3473f882001-02-23 17:55:21 +0000498 if (ctxt->in == NULL) {
Daniel Veillardf012a642001-07-23 19:10:52 +0000499 xmlGenericError( xmlGenericErrorContext,
500 "xmlNanoHTTPRecv: %s %d bytes.",
501 "Failed to realloc input buffer to",
502 ctxt->inlen );
503 xmlFree( tmp_ptr );
Owen Taylor3473f882001-02-23 17:55:21 +0000504 ctxt->last = -1;
505 return(-1);
506 }
507 ctxt->inptr = ctxt->in + d_inptr;
508 ctxt->content = ctxt->in + d_content;
509 ctxt->inrptr = ctxt->in + d_inrptr;
510 }
511 ctxt->last = recv(ctxt->fd, ctxt->inptr, XML_NANO_HTTP_CHUNK, 0);
512 if (ctxt->last > 0) {
513 ctxt->inptr += ctxt->last;
514 return(ctxt->last);
515 }
516 if (ctxt->last == 0) {
517 return(0);
518 }
519 if (ctxt->last == -1) {
520 switch (socket_errno()) {
521 case EINPROGRESS:
522 case EWOULDBLOCK:
523#if defined(EAGAIN) && EAGAIN != EWOULDBLOCK
524 case EAGAIN:
525#endif
526 break;
Daniel Veillardf012a642001-07-23 19:10:52 +0000527
528 case ECONNRESET:
529 case ESHUTDOWN:
530 return ( 0 );
531
Owen Taylor3473f882001-02-23 17:55:21 +0000532 default:
Daniel Veillardf012a642001-07-23 19:10:52 +0000533 xmlGenericError( xmlGenericErrorContext,
534 "xmlNanoHTTPRecv: recv( ) failure - %s",
535 strerror( socket_errno( ) ) );
536 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +0000537 }
538 }
539
540 tv.tv_sec = timeout;
541 tv.tv_usec = 0;
542 FD_ZERO(&rfd);
543 FD_SET(ctxt->fd, &rfd);
544
Daniel Veillardf012a642001-07-23 19:10:52 +0000545 if ( (select(ctxt->fd+1, &rfd, NULL, NULL, &tv)<1) && (errno != EINTR) )
Owen Taylor3473f882001-02-23 17:55:21 +0000546 return(0);
547 }
548 return(0);
549}
550
551/**
552 * xmlNanoHTTPReadLine:
553 * @ctxt: an HTTP context
554 *
555 * Read one line in the HTTP server output, usually for extracting
556 * the HTTP protocol informations from the answer header.
557 *
558 * Returns a newly allocated string with a copy of the line, or NULL
559 * which indicate the end of the input.
560 */
561
562static char *
563xmlNanoHTTPReadLine(xmlNanoHTTPCtxtPtr ctxt) {
564 char buf[4096];
565 char *bp = buf;
Daniel Veillardf012a642001-07-23 19:10:52 +0000566 int rc;
Owen Taylor3473f882001-02-23 17:55:21 +0000567
568 while (bp - buf < 4095) {
569 if (ctxt->inrptr == ctxt->inptr) {
Daniel Veillardf012a642001-07-23 19:10:52 +0000570 if ( (rc = xmlNanoHTTPRecv(ctxt)) == 0) {
Owen Taylor3473f882001-02-23 17:55:21 +0000571 if (bp == buf)
572 return(NULL);
573 else
574 *bp = 0;
575 return(xmlMemStrdup(buf));
576 }
Daniel Veillardf012a642001-07-23 19:10:52 +0000577 else if ( rc == -1 ) {
578 return ( NULL );
579 }
Owen Taylor3473f882001-02-23 17:55:21 +0000580 }
581 *bp = *ctxt->inrptr++;
582 if (*bp == '\n') {
583 *bp = 0;
584 return(xmlMemStrdup(buf));
585 }
586 if (*bp != '\r')
587 bp++;
588 }
589 buf[4095] = 0;
590 return(xmlMemStrdup(buf));
591}
592
593
594/**
595 * xmlNanoHTTPScanAnswer:
596 * @ctxt: an HTTP context
597 * @line: an HTTP header line
598 *
599 * Try to extract useful informations from the server answer.
600 * We currently parse and process:
601 * - The HTTP revision/ return code
602 * - The Content-Type
603 * - The Location for redirrect processing.
604 *
605 * Returns -1 in case of failure, the file descriptor number otherwise
606 */
607
608static void
609xmlNanoHTTPScanAnswer(xmlNanoHTTPCtxtPtr ctxt, const char *line) {
610 const char *cur = line;
611
612 if (line == NULL) return;
613
614 if (!strncmp(line, "HTTP/", 5)) {
615 int version = 0;
616 int ret = 0;
617
618 cur += 5;
619 while ((*cur >= '0') && (*cur <= '9')) {
620 version *= 10;
621 version += *cur - '0';
622 cur++;
623 }
624 if (*cur == '.') {
625 cur++;
626 if ((*cur >= '0') && (*cur <= '9')) {
627 version *= 10;
628 version += *cur - '0';
629 cur++;
630 }
631 while ((*cur >= '0') && (*cur <= '9'))
632 cur++;
633 } else
634 version *= 10;
635 if ((*cur != ' ') && (*cur != '\t')) return;
636 while ((*cur == ' ') || (*cur == '\t')) cur++;
637 if ((*cur < '0') || (*cur > '9')) return;
638 while ((*cur >= '0') && (*cur <= '9')) {
639 ret *= 10;
640 ret += *cur - '0';
641 cur++;
642 }
643 if ((*cur != 0) && (*cur != ' ') && (*cur != '\t')) return;
644 ctxt->returnValue = ret;
645 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Content-Type:", 13)) {
646 cur += 13;
647 while ((*cur == ' ') || (*cur == '\t')) cur++;
648 if (ctxt->contentType != NULL)
649 xmlFree(ctxt->contentType);
650 ctxt->contentType = xmlMemStrdup(cur);
651 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"ContentType:", 12)) {
652 cur += 12;
653 if (ctxt->contentType != NULL) return;
654 while ((*cur == ' ') || (*cur == '\t')) cur++;
655 ctxt->contentType = xmlMemStrdup(cur);
656 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Location:", 9)) {
657 cur += 9;
658 while ((*cur == ' ') || (*cur == '\t')) cur++;
659 if (ctxt->location != NULL)
660 xmlFree(ctxt->location);
661 ctxt->location = xmlMemStrdup(cur);
662 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"WWW-Authenticate:", 17)) {
663 cur += 17;
664 while ((*cur == ' ') || (*cur == '\t')) cur++;
665 if (ctxt->authHeader != NULL)
666 xmlFree(ctxt->authHeader);
667 ctxt->authHeader = xmlMemStrdup(cur);
668 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Proxy-Authenticate:", 19)) {
669 cur += 19;
670 while ((*cur == ' ') || (*cur == '\t')) cur++;
671 if (ctxt->authHeader != NULL)
672 xmlFree(ctxt->authHeader);
673 ctxt->authHeader = xmlMemStrdup(cur);
Daniel Veillardf012a642001-07-23 19:10:52 +0000674 } else if ( !xmlStrncasecmp( BAD_CAST line, BAD_CAST"Content-Length:", 15) ) {
675 cur += 15;
676 ctxt->ContentLength = strtol( cur, NULL, 10 );
Owen Taylor3473f882001-02-23 17:55:21 +0000677 }
678}
679
680/**
681 * xmlNanoHTTPConnectAttempt:
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000682 * @addr: a socket adress structure
Owen Taylor3473f882001-02-23 17:55:21 +0000683 *
684 * Attempt a connection to the given IP:port endpoint. It forces
685 * non-blocking semantic on the socket, and allow 60 seconds for
686 * the host to answer.
687 *
688 * Returns -1 in case of failure, the file descriptor number otherwise
689 */
690
691static int
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000692xmlNanoHTTPConnectAttempt(struct sockaddr *addr)
Owen Taylor3473f882001-02-23 17:55:21 +0000693{
694 SOCKET s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
695 fd_set wfd;
696 struct timeval tv;
697 int status;
698
699 if (s==-1) {
700#ifdef DEBUG_HTTP
701 perror("socket");
702#endif
Daniel Veillardf012a642001-07-23 19:10:52 +0000703 xmlGenericError( xmlGenericErrorContext,
704 "xmlNanoHTTPConnectAttempt: %s - %s",
705 "socket creation failure",
706 strerror( socket_errno( ) ) );
Owen Taylor3473f882001-02-23 17:55:21 +0000707 return(-1);
708 }
709
710#ifdef _WINSOCKAPI_
711 {
712 u_long one = 1;
713
714 status = ioctlsocket(s, FIONBIO, &one) == SOCKET_ERROR ? -1 : 0;
715 }
716#else /* _WINSOCKAPI_ */
717#if defined(VMS)
718 {
719 int enable = 1;
720 status = ioctl(s, FIONBIO, &enable);
721 }
722#else /* VMS */
723 if ((status = fcntl(s, F_GETFL, 0)) != -1) {
724#ifdef O_NONBLOCK
725 status |= O_NONBLOCK;
726#else /* O_NONBLOCK */
727#ifdef F_NDELAY
728 status |= F_NDELAY;
729#endif /* F_NDELAY */
730#endif /* !O_NONBLOCK */
731 status = fcntl(s, F_SETFL, status);
732 }
733 if (status < 0) {
734#ifdef DEBUG_HTTP
735 perror("nonblocking");
736#endif
Daniel Veillardf012a642001-07-23 19:10:52 +0000737 xmlGenericError( xmlGenericErrorContext,
738 "xmlNanoHTTPConnectAttempt: %s - %s",
739 "error setting non-blocking IO",
740 strerror( socket_errno( ) ) );
Owen Taylor3473f882001-02-23 17:55:21 +0000741 closesocket(s);
742 return(-1);
743 }
744#endif /* !VMS */
745#endif /* !_WINSOCKAPI_ */
746
Owen Taylor3473f882001-02-23 17:55:21 +0000747 if ((connect(s, addr, sizeof(*addr))==-1)) {
748 switch (socket_errno()) {
749 case EINPROGRESS:
750 case EWOULDBLOCK:
751 break;
752 default:
Daniel Veillardf012a642001-07-23 19:10:52 +0000753 xmlGenericError( xmlGenericErrorContext,
754 "xmlNanoHTTPConnectAttempt: %s - %s",
755 "error connecting to HTTP server",
756 strerror( socket_errno( ) ) );
Owen Taylor3473f882001-02-23 17:55:21 +0000757 closesocket(s);
758 return(-1);
759 }
760 }
761
762 tv.tv_sec = timeout;
763 tv.tv_usec = 0;
764
765 FD_ZERO(&wfd);
766 FD_SET(s, &wfd);
767
768 switch(select(s+1, NULL, &wfd, NULL, &tv))
769 {
770 case 0:
771 /* Time out */
Daniel Veillardf012a642001-07-23 19:10:52 +0000772 xmlGenericError( xmlGenericErrorContext,
773 "xmlNanoHTTPConnectAttempt: %s",
774 "Connect attempt timed out." );
Owen Taylor3473f882001-02-23 17:55:21 +0000775 closesocket(s);
776 return(-1);
777 case -1:
778 /* Ermm.. ?? */
Daniel Veillardf012a642001-07-23 19:10:52 +0000779 xmlGenericError( xmlGenericErrorContext,
780 "xmlNanoHTTPConnectAttempt: %s - %s",
781 "Error connecting to host",
782 strerror( socket_errno( ) ) );
Owen Taylor3473f882001-02-23 17:55:21 +0000783 closesocket(s);
784 return(-1);
785 }
786
787 if ( FD_ISSET(s, &wfd) ) {
788 SOCKLEN_T len;
789 len = sizeof(status);
790 if (getsockopt(s, SOL_SOCKET, SO_ERROR, (char*)&status, &len) < 0 ) {
791 /* Solaris error code */
Daniel Veillardf012a642001-07-23 19:10:52 +0000792 xmlGenericError( xmlGenericErrorContext,
793 "xmlNanoHTTPConnectAttempt: %s - %s",
794 "Error retrieving pending socket errors",
795 strerror( socket_errno( ) ) );
Owen Taylor3473f882001-02-23 17:55:21 +0000796 return (-1);
797 }
798 if ( status ) {
799 closesocket(s);
800 errno = status;
Daniel Veillardf012a642001-07-23 19:10:52 +0000801 xmlGenericError( xmlGenericErrorContext,
802 "xmlNanoHTTPConnectAttempt: %s - %s",
803 "Error connecting to remote host",
804 strerror( status ) );
Owen Taylor3473f882001-02-23 17:55:21 +0000805 return (-1);
806 }
807 } else {
808 /* pbm */
Daniel Veillardf012a642001-07-23 19:10:52 +0000809 xmlGenericError( xmlGenericErrorContext,
810 "xmlNanoHTTPConnectAttempt: %s\n",
811 "Select returned, but descriptor not set for connection.\n" );
812 closesocket(s);
Owen Taylor3473f882001-02-23 17:55:21 +0000813 return (-1);
814 }
815
816 return(s);
817}
818
819/**
820 * xmlNanoHTTPConnectHost:
821 * @host: the host name
822 * @port: the port number
823 *
824 * Attempt a connection to the given host:port endpoint. It tries
825 * the multiple IP provided by the DNS if available.
826 *
827 * Returns -1 in case of failure, the file descriptor number otherwise
828 */
829
830static int
831xmlNanoHTTPConnectHost(const char *host, int port)
832{
833 struct hostent *h;
834 struct sockaddr *addr;
835 struct in_addr ia;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000836 struct sockaddr_in sockin;
Owen Taylor3473f882001-02-23 17:55:21 +0000837#ifdef SUPPORT_IP6
838 struct in6_addr ia6;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000839 struct sockaddr_in6 sockin6;
Owen Taylor3473f882001-02-23 17:55:21 +0000840#endif
841 int i;
842 int s;
843
844#if defined(SUPPORT_IP6) && defined(RES_USE_INET6)
845 if (!(_res.options & RES_INIT))
846 res_init();
847 _res.options |= RES_USE_INET6;
848#endif
849 h=gethostbyname(host);
850 if (h==NULL)
851 {
Daniel Veillardf012a642001-07-23 19:10:52 +0000852 const char * h_err_txt = "";
853 switch ( h_errno )
854 {
855 case HOST_NOT_FOUND:
856 h_err_txt = "Authoritive host not found";
857 break;
858
859 case TRY_AGAIN:
860 h_err_txt =
861 "Non-authoritive host not found or server failure.";
862 break;
863
864 case NO_RECOVERY:
865 h_err_txt =
866 "Non-recoverable errors: FORMERR, REFUSED, or NOTIMP.";
867 break;
868
869 case NO_ADDRESS:
870 h_err_txt = "Valid name, no data record of requested type.";
871 break;
872
873 default:
874 h_err_txt = "No error text defined.";
875 break;
876 }
877 xmlGenericError( xmlGenericErrorContext,
878 "xmlNanoHTTPConnectHost: %s '%s' - %s",
879 "Failed to resolve host", host, h_err_txt );
Owen Taylor3473f882001-02-23 17:55:21 +0000880 return(-1);
881 }
882
883 for(i=0; h->h_addr_list[i]; i++)
884 {
885 if (h->h_addrtype == AF_INET) {
886 /* A records (IPv4) */
887 memcpy(&ia, h->h_addr_list[i], h->h_length);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000888 sockin.sin_family = h->h_addrtype;
889 sockin.sin_addr = ia;
890 sockin.sin_port = htons(port);
891 addr = (struct sockaddr *)&sockin;
Owen Taylor3473f882001-02-23 17:55:21 +0000892#ifdef SUPPORT_IP6
893 } else if (h->h_addrtype == AF_INET6) {
894 /* AAAA records (IPv6) */
895 memcpy(&ia6, h->h_addr_list[i], h->h_length);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000896 sockin6.sin_family = h->h_addrtype;
897 sockin6.sin_addr = ia6;
898 sockin6.sin_port = htons(port);
899 addr = (struct sockaddr *)&sockin6;
Owen Taylor3473f882001-02-23 17:55:21 +0000900#endif
901 } else
902 break; /* for */
903
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000904 s = xmlNanoHTTPConnectAttempt(addr);
Owen Taylor3473f882001-02-23 17:55:21 +0000905 if (s != -1)
906 return(s);
907 }
908
909#ifdef DEBUG_HTTP
910 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardf012a642001-07-23 19:10:52 +0000911 "xmlNanoHTTPConnectHost: unable to connect to '%s'.\n", host);
Owen Taylor3473f882001-02-23 17:55:21 +0000912#endif
913 return(-1);
914}
915
916
917/**
918 * xmlNanoHTTPOpen:
919 * @URL: The URL to load
920 * @contentType: if available the Content-Type information will be
921 * returned at that location
922 *
923 * This function try to open a connection to the indicated resource
924 * via HTTP GET.
925 *
926 * Returns NULL in case of failure, otherwise a request handler.
927 * The contentType, if provided must be freed by the caller
928 */
929
930void*
931xmlNanoHTTPOpen(const char *URL, char **contentType) {
932 if (contentType != NULL) *contentType = NULL;
Daniel Veillardf012a642001-07-23 19:10:52 +0000933 return(xmlNanoHTTPMethod(URL, NULL, NULL, contentType, NULL, 0));
Daniel Veillard9403a042001-05-28 11:00:53 +0000934}
935
936/**
937 * xmlNanoHTTPOpenRedir:
938 * @URL: The URL to load
939 * @contentType: if available the Content-Type information will be
940 * returned at that location
941 * @redir: if availble the redirected URL will be returned
942 *
943 * This function try to open a connection to the indicated resource
944 * via HTTP GET.
945 *
946 * Returns NULL in case of failure, otherwise a request handler.
947 * The contentType, if provided must be freed by the caller
948 */
949
950void*
951xmlNanoHTTPOpenRedir(const char *URL, char **contentType, char **redir) {
952 if (contentType != NULL) *contentType = NULL;
953 if (redir != NULL) *redir = NULL;
Daniel Veillardf012a642001-07-23 19:10:52 +0000954 return(xmlNanoHTTPMethodRedir(URL, NULL, NULL, contentType, redir, NULL,0));
Owen Taylor3473f882001-02-23 17:55:21 +0000955}
956
957/**
958 * xmlNanoHTTPRead:
959 * @ctx: the HTTP context
960 * @dest: a buffer
961 * @len: the buffer length
962 *
963 * This function tries to read @len bytes from the existing HTTP connection
964 * and saves them in @dest. This is a blocking call.
965 *
966 * Returns the number of byte read. 0 is an indication of an end of connection.
967 * -1 indicates a parameter error.
968 */
969int
970xmlNanoHTTPRead(void *ctx, void *dest, int len) {
971 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
972
973 if (ctx == NULL) return(-1);
974 if (dest == NULL) return(-1);
975 if (len <= 0) return(0);
976
977 while (ctxt->inptr - ctxt->inrptr < len) {
Daniel Veillardf012a642001-07-23 19:10:52 +0000978 if (xmlNanoHTTPRecv(ctxt) <= 0) break;
Owen Taylor3473f882001-02-23 17:55:21 +0000979 }
980 if (ctxt->inptr - ctxt->inrptr < len)
981 len = ctxt->inptr - ctxt->inrptr;
982 memcpy(dest, ctxt->inrptr, len);
983 ctxt->inrptr += len;
984 return(len);
985}
986
987/**
988 * xmlNanoHTTPClose:
989 * @ctx: the HTTP context
990 *
991 * This function closes an HTTP context, it ends up the connection and
992 * free all data related to it.
993 */
994void
995xmlNanoHTTPClose(void *ctx) {
996 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
997
998 if (ctx == NULL) return;
999
1000 xmlNanoHTTPFreeCtxt(ctxt);
1001}
1002
1003/**
Daniel Veillard9403a042001-05-28 11:00:53 +00001004 * xmlNanoHTTPMethodRedir:
Owen Taylor3473f882001-02-23 17:55:21 +00001005 * @URL: The URL to load
1006 * @method: the HTTP method to use
1007 * @input: the input string if any
1008 * @contentType: the Content-Type information IN and OUT
Daniel Veillard9403a042001-05-28 11:00:53 +00001009 * @redir: the redirected URL OUT
Owen Taylor3473f882001-02-23 17:55:21 +00001010 * @headers: the extra headers
1011 *
1012 * This function try to open a connection to the indicated resource
1013 * via HTTP using the given @method, adding the given extra headers
1014 * and the input buffer for the request content.
1015 *
1016 * Returns NULL in case of failure, otherwise a request handler.
Daniel Veillard9403a042001-05-28 11:00:53 +00001017 * The contentType, or redir, if provided must be freed by the caller
Owen Taylor3473f882001-02-23 17:55:21 +00001018 */
1019
1020void*
Daniel Veillard9403a042001-05-28 11:00:53 +00001021xmlNanoHTTPMethodRedir(const char *URL, const char *method, const char *input,
Daniel Veillardf012a642001-07-23 19:10:52 +00001022 char **contentType, char **redir,
1023 const char *headers, int ilen ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001024 xmlNanoHTTPCtxtPtr ctxt;
1025 char *bp, *p;
Daniel Veillardf012a642001-07-23 19:10:52 +00001026 int blen, ret;
Owen Taylor3473f882001-02-23 17:55:21 +00001027 int head;
Daniel Veillardf012a642001-07-23 19:10:52 +00001028 int xmt_bytes;
Owen Taylor3473f882001-02-23 17:55:21 +00001029 int nbRedirects = 0;
1030 char *redirURL = NULL;
1031
1032 if (URL == NULL) return(NULL);
1033 if (method == NULL) method = "GET";
1034 xmlNanoHTTPInit();
1035
1036retry:
1037 if (redirURL == NULL)
1038 ctxt = xmlNanoHTTPNewCtxt(URL);
1039 else {
1040 ctxt = xmlNanoHTTPNewCtxt(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001041 }
1042
Daniel Veillardf012a642001-07-23 19:10:52 +00001043 if ( ctxt == NULL ) {
1044 xmlGenericError( xmlGenericErrorContext,
1045 "xmlNanoHTTPMethodRedir: %s %s.",
1046 "Unable to allocate HTTP context to URI",
1047 ( ( redirURL == NULL ) ? URL : redirURL ) );
1048 return ( NULL );
1049 }
1050
Owen Taylor3473f882001-02-23 17:55:21 +00001051 if ((ctxt->protocol == NULL) || (strcmp(ctxt->protocol, "http"))) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001052 xmlGenericError( xmlGenericErrorContext,
1053 "xmlNanoHTTPMethodRedir: %s - %s.",
1054 "Not a valid HTTP URI",
1055 ( ( redirURL == NULL ) ? URL : redirURL ) );
Owen Taylor3473f882001-02-23 17:55:21 +00001056 xmlNanoHTTPFreeCtxt(ctxt);
1057 if (redirURL != NULL) xmlFree(redirURL);
1058 return(NULL);
1059 }
1060 if (ctxt->hostname == NULL) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001061 xmlGenericError( xmlGenericErrorContext,
1062 "xmlNanoHTTPMethodRedir: %s - %s",
1063 "Failed to identify host in URI",
1064 ( ( redirURL == NULL ) ? URL : redirURL ) );
Owen Taylor3473f882001-02-23 17:55:21 +00001065 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001066 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001067 return(NULL);
1068 }
1069 if (proxy) {
1070 blen = strlen(ctxt->hostname) * 2 + 16;
1071 ret = xmlNanoHTTPConnectHost(proxy, proxyPort);
1072 }
1073 else {
1074 blen = strlen(ctxt->hostname);
1075 ret = xmlNanoHTTPConnectHost(ctxt->hostname, ctxt->port);
1076 }
1077 if (ret < 0) {
1078 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001079 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001080 return(NULL);
1081 }
1082 ctxt->fd = ret;
1083
Daniel Veillardf012a642001-07-23 19:10:52 +00001084 if (input == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00001085 ilen = 0;
Daniel Veillardf012a642001-07-23 19:10:52 +00001086 else
1087 blen += 36;
1088
Owen Taylor3473f882001-02-23 17:55:21 +00001089 if (headers != NULL)
Daniel Veillardf012a642001-07-23 19:10:52 +00001090 blen += strlen(headers) + 2;
Owen Taylor3473f882001-02-23 17:55:21 +00001091 if (contentType && *contentType)
1092 blen += strlen(*contentType) + 16;
Daniel Veillardf012a642001-07-23 19:10:52 +00001093 blen += strlen(method) + strlen(ctxt->path) + 24;
Owen Taylor3473f882001-02-23 17:55:21 +00001094 bp = xmlMalloc(blen);
Daniel Veillardf012a642001-07-23 19:10:52 +00001095 if ( bp == NULL ) {
1096 xmlNanoHTTPFreeCtxt( ctxt );
1097 xmlGenericError( xmlGenericErrorContext,
1098 "xmlNanoHTTPMethodRedir: %s",
1099 "Error allocating HTTP header buffer." );
1100 return ( NULL );
1101 }
1102
1103 p = bp;
1104
Owen Taylor3473f882001-02-23 17:55:21 +00001105 if (proxy) {
1106 if (ctxt->port != 80) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001107 p += sprintf( p, "%s http://%s:%d%s", method, ctxt->hostname,
1108 ctxt->port, ctxt->path );
Owen Taylor3473f882001-02-23 17:55:21 +00001109 }
1110 else
Daniel Veillardf012a642001-07-23 19:10:52 +00001111 p += sprintf( p, "%s http://%s%s", method,
1112 ctxt->hostname, ctxt->path);
Owen Taylor3473f882001-02-23 17:55:21 +00001113 }
1114 else
Daniel Veillardf012a642001-07-23 19:10:52 +00001115 p += sprintf( p, "%s %s", method, ctxt->path);
1116
1117 p += sprintf(p, " HTTP/1.0\r\nHost: %s\r\n", ctxt->hostname);
1118
1119 if (contentType != NULL && *contentType)
1120 p += sprintf(p, "Content-Type: %s\r\n", *contentType);
1121
1122 if (headers != NULL)
1123 p += sprintf( p, "%s", headers );
1124
Owen Taylor3473f882001-02-23 17:55:21 +00001125 if (input != NULL)
Daniel Veillardf012a642001-07-23 19:10:52 +00001126 sprintf(p, "Content-Length: %d\r\n\r\n", ilen );
Owen Taylor3473f882001-02-23 17:55:21 +00001127 else
1128 strcpy(p, "\r\n");
Daniel Veillardf012a642001-07-23 19:10:52 +00001129
Owen Taylor3473f882001-02-23 17:55:21 +00001130#ifdef DEBUG_HTTP
1131 xmlGenericError(xmlGenericErrorContext,
1132 "-> %s%s", proxy? "(Proxy) " : "", bp);
1133 if ((blen -= strlen(bp)+1) < 0)
1134 xmlGenericError(xmlGenericErrorContext,
1135 "ERROR: overflowed buffer by %d bytes\n", -blen);
1136#endif
1137 ctxt->outptr = ctxt->out = bp;
1138 ctxt->state = XML_NANO_HTTP_WRITE;
Daniel Veillardf012a642001-07-23 19:10:52 +00001139 blen = strlen( ctxt->out );
1140 xmt_bytes = xmlNanoHTTPSend(ctxt, ctxt->out, blen );
1141#ifdef DEBUG_HTTP
1142 if ( xmt_bytes != blen )
1143 xmlGenericError( xmlGenericErrorContext,
1144 "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
1145 xmt_bytes, blen,
1146 "bytes of HTTP headers sent to host",
1147 ctxt->hostname );
1148#endif
1149
1150 if ( input != NULL ) {
1151 xmt_bytes = xmlNanoHTTPSend( ctxt, input, ilen );
1152
1153#ifdef DEBUG_HTTP
1154 if ( xmt_bytes != ilen )
1155 xmlGenericError( xmlGenericErrorContext,
1156 "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
1157 xmt_bytes, ilen,
1158 "bytes of HTTP content sent to host",
1159 ctxt->hostname );
1160#endif
1161 }
1162
Owen Taylor3473f882001-02-23 17:55:21 +00001163 ctxt->state = XML_NANO_HTTP_READ;
1164 head = 1;
1165
1166 while ((p = xmlNanoHTTPReadLine(ctxt)) != NULL) {
1167 if (head && (*p == 0)) {
1168 head = 0;
1169 ctxt->content = ctxt->inrptr;
1170 xmlFree(p);
1171 break;
1172 }
1173 xmlNanoHTTPScanAnswer(ctxt, p);
1174
1175#ifdef DEBUG_HTTP
1176 xmlGenericError(xmlGenericErrorContext, "<- %s\n", p);
1177#endif
1178 xmlFree(p);
1179 }
1180
1181 if ((ctxt->location != NULL) && (ctxt->returnValue >= 300) &&
1182 (ctxt->returnValue < 400)) {
1183#ifdef DEBUG_HTTP
1184 xmlGenericError(xmlGenericErrorContext,
1185 "\nRedirect to: %s\n", ctxt->location);
1186#endif
Daniel Veillardf012a642001-07-23 19:10:52 +00001187 while ( xmlNanoHTTPRecv(ctxt) > 0 ) ;
Owen Taylor3473f882001-02-23 17:55:21 +00001188 if (nbRedirects < XML_NANO_HTTP_MAX_REDIR) {
1189 nbRedirects++;
Daniel Veillard9403a042001-05-28 11:00:53 +00001190 if (redirURL != NULL)
1191 xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001192 redirURL = xmlMemStrdup(ctxt->location);
1193 xmlNanoHTTPFreeCtxt(ctxt);
1194 goto retry;
1195 }
1196 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001197 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001198#ifdef DEBUG_HTTP
1199 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardf012a642001-07-23 19:10:52 +00001200 "xmlNanoHTTPMethodRedir: Too many redirects, aborting ...\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001201#endif
1202 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001203 }
1204
1205 if (contentType != NULL) {
1206 if (ctxt->contentType != NULL)
1207 *contentType = xmlMemStrdup(ctxt->contentType);
1208 else
1209 *contentType = NULL;
1210 }
1211
Daniel Veillard9403a042001-05-28 11:00:53 +00001212 if ((redir != NULL) && (redirURL != NULL)) {
1213 *redir = redirURL;
1214 } else {
1215 if (redirURL != NULL)
1216 xmlFree(redirURL);
1217 if (redir != NULL)
1218 *redir = NULL;
1219 }
1220
Owen Taylor3473f882001-02-23 17:55:21 +00001221#ifdef DEBUG_HTTP
1222 if (ctxt->contentType != NULL)
1223 xmlGenericError(xmlGenericErrorContext,
1224 "\nCode %d, content-type '%s'\n\n",
1225 ctxt->returnValue, ctxt->contentType);
1226 else
1227 xmlGenericError(xmlGenericErrorContext,
1228 "\nCode %d, no content-type\n\n",
1229 ctxt->returnValue);
1230#endif
1231
1232 return((void *) ctxt);
1233}
1234
1235/**
Daniel Veillard9403a042001-05-28 11:00:53 +00001236 * xmlNanoHTTPMethod:
1237 * @URL: The URL to load
1238 * @method: the HTTP method to use
1239 * @input: the input string if any
1240 * @contentType: the Content-Type information IN and OUT
1241 * @headers: the extra headers
1242 *
1243 * This function try to open a connection to the indicated resource
1244 * via HTTP using the given @method, adding the given extra headers
1245 * and the input buffer for the request content.
1246 *
1247 * Returns NULL in case of failure, otherwise a request handler.
1248 * The contentType, if provided must be freed by the caller
1249 */
1250
1251void*
1252xmlNanoHTTPMethod(const char *URL, const char *method, const char *input,
Daniel Veillardf012a642001-07-23 19:10:52 +00001253 char **contentType, const char *headers, int ilen) {
Daniel Veillard9403a042001-05-28 11:00:53 +00001254 return(xmlNanoHTTPMethodRedir(URL, method, input, contentType,
Daniel Veillardf012a642001-07-23 19:10:52 +00001255 NULL, headers, ilen));
Daniel Veillard9403a042001-05-28 11:00:53 +00001256}
1257
1258/**
Owen Taylor3473f882001-02-23 17:55:21 +00001259 * xmlNanoHTTPFetch:
1260 * @URL: The URL to load
1261 * @filename: the filename where the content should be saved
1262 * @contentType: if available the Content-Type information will be
1263 * returned at that location
1264 *
1265 * This function try to fetch the indicated resource via HTTP GET
1266 * and save it's content in the file.
1267 *
1268 * Returns -1 in case of failure, 0 incase of success. The contentType,
1269 * if provided must be freed by the caller
1270 */
1271int
1272xmlNanoHTTPFetch(const char *URL, const char *filename, char **contentType) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001273 void *ctxt = NULL;
1274 char *buf = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001275 int fd;
1276 int len;
1277
1278 ctxt = xmlNanoHTTPOpen(URL, contentType);
1279 if (ctxt == NULL) return(-1);
1280
1281 if (!strcmp(filename, "-"))
1282 fd = 0;
1283 else {
1284 fd = open(filename, O_CREAT | O_WRONLY, 00644);
1285 if (fd < 0) {
1286 xmlNanoHTTPClose(ctxt);
1287 if ((contentType != NULL) && (*contentType != NULL)) {
1288 xmlFree(*contentType);
1289 *contentType = NULL;
1290 }
1291 return(-1);
1292 }
1293 }
1294
Daniel Veillardf012a642001-07-23 19:10:52 +00001295 xmlNanoHTTPFetchContent( ctxt, &buf, &len );
1296 if ( len > 0 ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001297 write(fd, buf, len);
1298 }
1299
1300 xmlNanoHTTPClose(ctxt);
1301 close(fd);
1302 return(0);
1303}
1304
1305/**
1306 * xmlNanoHTTPSave:
1307 * @ctxt: the HTTP context
1308 * @filename: the filename where the content should be saved
1309 *
1310 * This function saves the output of the HTTP transaction to a file
1311 * It closes and free the context at the end
1312 *
1313 * Returns -1 in case of failure, 0 incase of success.
1314 */
1315int
1316xmlNanoHTTPSave(void *ctxt, const char *filename) {
Daniel Veillarde3924972001-07-25 20:25:21 +00001317 char *buf = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001318 int fd;
1319 int len;
1320
1321 if (ctxt == NULL) return(-1);
1322
1323 if (!strcmp(filename, "-"))
1324 fd = 0;
1325 else {
1326 fd = open(filename, O_CREAT | O_WRONLY);
1327 if (fd < 0) {
1328 xmlNanoHTTPClose(ctxt);
1329 return(-1);
1330 }
1331 }
1332
Daniel Veillardf012a642001-07-23 19:10:52 +00001333 xmlNanoHTTPFetchContent( ctxt, &buf, &len );
1334 if ( len > 0 ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001335 write(fd, buf, len);
1336 }
1337
1338 xmlNanoHTTPClose(ctxt);
1339 return(0);
1340}
1341
1342/**
1343 * xmlNanoHTTPReturnCode:
1344 * @ctx: the HTTP context
1345 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001346 * Get the latest HTTP return code received
1347 *
Owen Taylor3473f882001-02-23 17:55:21 +00001348 * Returns the HTTP return code for the request.
1349 */
1350int
1351xmlNanoHTTPReturnCode(void *ctx) {
1352 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1353
1354 if (ctxt == NULL) return(-1);
1355
1356 return(ctxt->returnValue);
1357}
1358
1359/**
1360 * xmlNanoHTTPAuthHeader:
1361 * @ctx: the HTTP context
1362 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001363 * Get the authentication header of an HTTP context
1364 *
Owen Taylor3473f882001-02-23 17:55:21 +00001365 * Returns the stashed value of the WWW-Authenticate or Proxy-Authenticate
1366 * header.
1367 */
1368const char *
1369xmlNanoHTTPAuthHeader(void *ctx) {
1370 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1371
1372 if (ctxt == NULL) return(NULL);
1373
1374 return(ctxt->authHeader);
1375}
1376
Daniel Veillardf012a642001-07-23 19:10:52 +00001377/**
1378 * xmlNanoHTTPContentLength
1379 * @ctx: the HTTP context
1380 *
1381 * Return the specified content length from the HTTP header. Note that
1382 * a value of -1 indicates that the content length element was not included in
1383 * the response header.
1384 */
1385int
1386xmlNanoHTTPContentLength( void * ctx ) {
1387 xmlNanoHTTPCtxtPtr ctxt = ctx;
1388
1389 return ( ( ctxt == NULL ) ? -1 : ctxt->ContentLength );
1390}
1391
1392/**
1393 * xmlNanoHTTPFetchContent
1394 * @ctx: the HTTP context
1395 * @ptr: pointer to set to the content buffer.
1396 * @len: integer pointer to hold the length of the content
1397 *
1398 * Returns 0 if all the content was read and available, returns
1399 * -1 if received content length was less than specified or an error
1400 * occurred.
1401 */
1402int
1403xmlNanoHTTPFetchContent( void * ctx, char ** ptr, int * len ) {
1404 xmlNanoHTTPCtxtPtr ctxt = ctx;
1405
1406 int rc = 0;
1407 int cur_lgth;
1408 int rcvd_lgth;
1409 int dummy_int;
1410 char * dummy_ptr = NULL;
1411
1412 /* Dummy up return input parameters if not provided */
1413
1414 if ( len == NULL )
1415 len = &dummy_int;
1416
1417 if ( ptr == NULL )
1418 ptr = &dummy_ptr;
1419
1420 /* But can't work without the context pointer */
1421
1422 if ( ( ctxt == NULL ) || ( ctxt->content == NULL ) ) {
1423 *len = 0;
1424 *ptr = NULL;
1425 return ( -1 );
1426 }
1427
1428 rcvd_lgth = ctxt->inptr - ctxt->content;
1429
1430 while ( (cur_lgth = xmlNanoHTTPRecv( ctxt )) > 0 ) {
1431
1432 rcvd_lgth += cur_lgth;
1433 if ( (ctxt->ContentLength > 0) && (rcvd_lgth >= ctxt->ContentLength) )
1434 break;
1435 }
1436
1437 *ptr = ctxt->content;
1438 *len = rcvd_lgth;
1439
1440 if ( ( ctxt->ContentLength > 0 ) && ( rcvd_lgth < ctxt->ContentLength ) )
1441 rc = -1;
1442 else if ( rcvd_lgth == 0 )
1443 rc = -1;
1444
1445 return ( rc );
1446}
1447
Owen Taylor3473f882001-02-23 17:55:21 +00001448#ifdef STANDALONE
1449int main(int argc, char **argv) {
1450 char *contentType = NULL;
1451
1452 if (argv[1] != NULL) {
1453 if (argv[2] != NULL)
1454 xmlNanoHTTPFetch(argv[1], argv[2], &contentType);
1455 else
1456 xmlNanoHTTPFetch(argv[1], "-", &contentType);
1457 if (contentType != NULL) xmlFree(contentType);
1458 } else {
1459 xmlGenericError(xmlGenericErrorContext,
1460 "%s: minimal HTTP GET implementation\n", argv[0]);
1461 xmlGenericError(xmlGenericErrorContext,
1462 "\tusage %s [ URL [ filename ] ]\n", argv[0]);
1463 }
1464 xmlNanoHTTPCleanup();
1465 xmlMemoryDump();
1466 return(0);
1467}
1468#endif /* STANDALONE */
1469#else /* !LIBXML_HTTP_ENABLED */
1470#ifdef STANDALONE
1471#include <stdio.h>
1472int main(int argc, char **argv) {
1473 xmlGenericError(xmlGenericErrorContext,
1474 "%s : HTTP support not compiled in\n", argv[0]);
1475 return(0);
1476}
1477#endif /* STANDALONE */
1478#endif /* LIBXML_HTTP_ENABLED */