blob: b843370ea36b0fe9306cfbf77a315a115243f774 [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
Daniel Veillard34ce8be2002-03-18 19:37:11 +000018#define IN_LIBXML
Bjorn Reese70a9da52001-04-21 16:57:29 +000019#include "libxml.h"
Owen Taylor3473f882001-02-23 17:55:21 +000020
21#ifdef LIBXML_HTTP_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +000022#include <string.h>
23
24#ifdef HAVE_STDLIB_H
25#include <stdlib.h>
26#endif
27#ifdef HAVE_UNISTD_H
28#include <unistd.h>
29#endif
30#ifdef HAVE_SYS_SOCKET_H
31#include <sys/socket.h>
32#endif
33#ifdef HAVE_NETINET_IN_H
34#include <netinet/in.h>
35#endif
36#ifdef HAVE_ARPA_INET_H
37#include <arpa/inet.h>
38#endif
39#ifdef HAVE_NETDB_H
40#include <netdb.h>
41#endif
42#ifdef HAVE_FCNTL_H
43#include <fcntl.h>
44#endif
45#ifdef HAVE_ERRNO_H
46#include <errno.h>
47#endif
48#ifdef HAVE_SYS_TIME_H
49#include <sys/time.h>
50#endif
51#ifdef HAVE_SYS_SELECT_H
52#include <sys/select.h>
53#endif
54#ifdef HAVE_STRINGS_H
55#include <strings.h>
56#endif
57#ifdef SUPPORT_IP6
58#include <resolv.h>
59#endif
60
61#ifdef VMS
62#include <stropts>
63#define SOCKLEN_T unsigned int
64#define SOCKET int
65#endif
66
Daniel Veillardd0463562001-10-13 09:15:48 +000067#include <libxml/globals.h>
Daniel Veillardf012a642001-07-23 19:10:52 +000068#include <libxml/xmlerror.h>
Owen Taylor3473f882001-02-23 17:55:21 +000069#include <libxml/xmlmemory.h>
70#include <libxml/parser.h> /* for xmlStr(n)casecmp() */
71#include <libxml/nanohttp.h>
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000072#include <libxml/globals.h>
Owen Taylor3473f882001-02-23 17:55:21 +000073
74/**
75 * A couple portability macros
76 */
77#ifndef _WINSOCKAPI_
78#define closesocket(s) close(s)
79#define SOCKET int
80#endif
81
Daniel Veillard75be0132002-03-13 10:03:35 +000082#ifndef SOCKLEN_T
83#define SOCKLEN_T unsigned int
84#endif
85#ifndef SOCKET
86#define SOCKET int
87#endif
Daniel Veillardf012a642001-07-23 19:10:52 +000088
Owen Taylor3473f882001-02-23 17:55:21 +000089#ifdef STANDALONE
90#define DEBUG_HTTP
91#define xmlStrncasecmp(a, b, n) strncasecmp((char *)a, (char *)b, n)
92#define xmlStrcasecmpi(a, b) strcasecmp((char *)a, (char *)b)
93#endif
94
95#define XML_NANO_HTTP_MAX_REDIR 10
96
97#define XML_NANO_HTTP_CHUNK 4096
98
99#define XML_NANO_HTTP_CLOSED 0
100#define XML_NANO_HTTP_WRITE 1
101#define XML_NANO_HTTP_READ 2
102#define XML_NANO_HTTP_NONE 4
103
104typedef struct xmlNanoHTTPCtxt {
105 char *protocol; /* the protocol name */
106 char *hostname; /* the host name */
107 int port; /* the port */
108 char *path; /* the path within the URL */
109 SOCKET fd; /* the file descriptor for the socket */
110 int state; /* WRITE / READ / CLOSED */
111 char *out; /* buffer sent (zero terminated) */
112 char *outptr; /* index within the buffer sent */
113 char *in; /* the receiving buffer */
114 char *content; /* the start of the content */
115 char *inptr; /* the next byte to read from network */
116 char *inrptr; /* the next byte to give back to the client */
117 int inlen; /* len of the input buffer */
118 int last; /* return code for last operation */
119 int returnValue; /* the protocol return value */
Daniel Veillardf012a642001-07-23 19:10:52 +0000120 int ContentLength; /* specified content length from HTTP header */
Owen Taylor3473f882001-02-23 17:55:21 +0000121 char *contentType; /* the MIME type for the input */
122 char *location; /* the new URL in case of redirect */
123 char *authHeader; /* contents of {WWW,Proxy}-Authenticate header */
124} xmlNanoHTTPCtxt, *xmlNanoHTTPCtxtPtr;
125
126static int initialized = 0;
127static char *proxy = NULL; /* the proxy name if any */
128static int proxyPort; /* the proxy port if any */
129static unsigned int timeout = 60;/* the select() timeout in seconds */
130
Daniel Veillardf012a642001-07-23 19:10:52 +0000131int xmlNanoHTTPFetchContent( void * ctx, char ** ptr, int * len );
132int xmlNanoHTTPContentLength( void * ctx );
133
Owen Taylor3473f882001-02-23 17:55:21 +0000134/**
135 * A portability function
136 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000137static int socket_errno(void) {
Owen Taylor3473f882001-02-23 17:55:21 +0000138#ifdef _WINSOCKAPI_
139 return(WSAGetLastError());
140#else
141 return(errno);
142#endif
143}
144
145/**
146 * xmlNanoHTTPInit:
147 *
148 * Initialize the HTTP protocol layer.
149 * Currently it just checks for proxy informations
150 */
151
152void
153xmlNanoHTTPInit(void) {
154 const char *env;
155#ifdef _WINSOCKAPI_
156 WSADATA wsaData;
157#endif
158
159 if (initialized)
160 return;
161
162#ifdef _WINSOCKAPI_
163 if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0)
164 return;
165#endif
166
167 if (proxy == NULL) {
168 proxyPort = 80;
169 env = getenv("no_proxy");
170 if (env != NULL)
171 goto done;
172 env = getenv("http_proxy");
173 if (env != NULL) {
174 xmlNanoHTTPScanProxy(env);
175 goto done;
176 }
177 env = getenv("HTTP_PROXY");
178 if (env != NULL) {
179 xmlNanoHTTPScanProxy(env);
180 goto done;
181 }
182 }
183done:
184 initialized = 1;
185}
186
187/**
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000188 * xmlNanoHTTPCleanup:
Owen Taylor3473f882001-02-23 17:55:21 +0000189 *
190 * Cleanup the HTTP protocol layer.
191 */
192
193void
194xmlNanoHTTPCleanup(void) {
195 if (proxy != NULL)
196 xmlFree(proxy);
197#ifdef _WINSOCKAPI_
198 if (initialized)
199 WSACleanup();
200#endif
201 initialized = 0;
202 return;
203}
204
205/**
Owen Taylor3473f882001-02-23 17:55:21 +0000206 * xmlNanoHTTPScanURL:
207 * @ctxt: an HTTP context
208 * @URL: The URL used to initialize the context
209 *
210 * (Re)Initialize an HTTP context by parsing the URL and finding
211 * the protocol host port and path it indicates.
212 */
213
214static void
215xmlNanoHTTPScanURL(xmlNanoHTTPCtxtPtr ctxt, const char *URL) {
216 const char *cur = URL;
217 char buf[4096];
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000218 int indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000219 int port = 0;
220
221 if (ctxt->protocol != NULL) {
222 xmlFree(ctxt->protocol);
223 ctxt->protocol = NULL;
224 }
225 if (ctxt->hostname != NULL) {
226 xmlFree(ctxt->hostname);
227 ctxt->hostname = NULL;
228 }
229 if (ctxt->path != NULL) {
230 xmlFree(ctxt->path);
231 ctxt->path = NULL;
232 }
233 if (URL == NULL) return;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000234 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000235 while (*cur != 0) {
236 if ((cur[0] == ':') && (cur[1] == '/') && (cur[2] == '/')) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000237 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000238 ctxt->protocol = xmlMemStrdup(buf);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000239 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000240 cur += 3;
241 break;
242 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000243 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000244 }
245 if (*cur == 0) return;
246
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000247 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000248 while (1) {
249 if (cur[0] == ':') {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000250 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000251 ctxt->hostname = xmlMemStrdup(buf);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000252 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000253 cur += 1;
254 while ((*cur >= '0') && (*cur <= '9')) {
255 port *= 10;
256 port += *cur - '0';
257 cur++;
258 }
259 if (port != 0) ctxt->port = port;
260 while ((cur[0] != '/') && (*cur != 0))
261 cur++;
262 break;
263 }
264 if ((*cur == '/') || (*cur == 0)) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000265 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000266 ctxt->hostname = xmlMemStrdup(buf);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000267 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000268 break;
269 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000270 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000271 }
272 if (*cur == 0)
273 ctxt->path = xmlMemStrdup("/");
274 else {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000275 indx = 0;
276 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000277 while (*cur != 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000278 buf[indx++] = *cur++;
279 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000280 ctxt->path = xmlMemStrdup(buf);
281 }
282}
283
284/**
285 * xmlNanoHTTPScanProxy:
286 * @URL: The proxy URL used to initialize the proxy context
287 *
288 * (Re)Initialize the HTTP Proxy context by parsing the URL and finding
289 * the protocol host port it indicates.
290 * Should be like http://myproxy/ or http://myproxy:3128/
291 * A NULL URL cleans up proxy informations.
292 */
293
294void
295xmlNanoHTTPScanProxy(const char *URL) {
296 const char *cur = URL;
297 char buf[4096];
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000298 int indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000299 int port = 0;
300
301 if (proxy != NULL) {
302 xmlFree(proxy);
303 proxy = NULL;
304 }
305 if (proxyPort != 0) {
306 proxyPort = 0;
307 }
308#ifdef DEBUG_HTTP
309 if (URL == NULL)
310 xmlGenericError(xmlGenericErrorContext,
311 "Removing HTTP proxy info\n");
312 else
313 xmlGenericError(xmlGenericErrorContext,
314 "Using HTTP proxy %s\n", URL);
315#endif
316 if (URL == NULL) return;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000317 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000318 while (*cur != 0) {
319 if ((cur[0] == ':') && (cur[1] == '/') && (cur[2] == '/')) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000320 buf[indx] = 0;
321 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000322 cur += 3;
323 break;
324 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000325 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000326 }
327 if (*cur == 0) return;
328
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000329 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000330 while (1) {
331 if (cur[0] == ':') {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000332 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000333 proxy = xmlMemStrdup(buf);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000334 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000335 cur += 1;
336 while ((*cur >= '0') && (*cur <= '9')) {
337 port *= 10;
338 port += *cur - '0';
339 cur++;
340 }
341 if (port != 0) proxyPort = port;
342 while ((cur[0] != '/') && (*cur != 0))
343 cur++;
344 break;
345 }
346 if ((*cur == '/') || (*cur == 0)) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000347 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000348 proxy = xmlMemStrdup(buf);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000349 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000350 break;
351 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000352 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000353 }
354}
355
356/**
357 * xmlNanoHTTPNewCtxt:
358 * @URL: The URL used to initialize the context
359 *
360 * Allocate and initialize a new HTTP context.
361 *
362 * Returns an HTTP context or NULL in case of error.
363 */
364
365static xmlNanoHTTPCtxtPtr
366xmlNanoHTTPNewCtxt(const char *URL) {
367 xmlNanoHTTPCtxtPtr ret;
368
369 ret = (xmlNanoHTTPCtxtPtr) xmlMalloc(sizeof(xmlNanoHTTPCtxt));
370 if (ret == NULL) return(NULL);
371
372 memset(ret, 0, sizeof(xmlNanoHTTPCtxt));
373 ret->port = 80;
374 ret->returnValue = 0;
375 ret->fd = -1;
Daniel Veillardf012a642001-07-23 19:10:52 +0000376 ret->ContentLength = -1;
Owen Taylor3473f882001-02-23 17:55:21 +0000377
378 xmlNanoHTTPScanURL(ret, URL);
379
380 return(ret);
381}
382
383/**
384 * xmlNanoHTTPFreeCtxt:
385 * @ctxt: an HTTP context
386 *
387 * Frees the context after closing the connection.
388 */
389
390static void
391xmlNanoHTTPFreeCtxt(xmlNanoHTTPCtxtPtr ctxt) {
392 if (ctxt == NULL) return;
393 if (ctxt->hostname != NULL) xmlFree(ctxt->hostname);
394 if (ctxt->protocol != NULL) xmlFree(ctxt->protocol);
395 if (ctxt->path != NULL) xmlFree(ctxt->path);
396 if (ctxt->out != NULL) xmlFree(ctxt->out);
397 if (ctxt->in != NULL) xmlFree(ctxt->in);
398 if (ctxt->contentType != NULL) xmlFree(ctxt->contentType);
399 if (ctxt->location != NULL) xmlFree(ctxt->location);
400 if (ctxt->authHeader != NULL) xmlFree(ctxt->authHeader);
401 ctxt->state = XML_NANO_HTTP_NONE;
402 if (ctxt->fd >= 0) closesocket(ctxt->fd);
403 ctxt->fd = -1;
404 xmlFree(ctxt);
405}
406
407/**
408 * xmlNanoHTTPSend:
409 * @ctxt: an HTTP context
410 *
411 * Send the input needed to initiate the processing on the server side
Daniel Veillardf012a642001-07-23 19:10:52 +0000412 * Returns number of bytes sent or -1 on error.
Owen Taylor3473f882001-02-23 17:55:21 +0000413 */
414
Daniel Veillardf012a642001-07-23 19:10:52 +0000415static int
416xmlNanoHTTPSend(xmlNanoHTTPCtxtPtr ctxt, const char * xmt_ptr, int outlen) {
417
418 int total_sent = 0;
419
420 if ( (ctxt->state & XML_NANO_HTTP_WRITE) && (xmt_ptr != NULL ) ) {
421 while (total_sent < outlen) {
422 int nsent = send(ctxt->fd, xmt_ptr + total_sent,
423 outlen - total_sent, 0);
Owen Taylor3473f882001-02-23 17:55:21 +0000424 if (nsent>0)
425 total_sent += nsent;
Daniel Veillardf012a642001-07-23 19:10:52 +0000426 else if ( ( nsent == -1 ) &&
Daniel Veillardba6db032001-07-31 16:25:45 +0000427#if defined(EAGAIN) && EAGAIN != EWOULDBLOCK
Daniel Veillardf012a642001-07-23 19:10:52 +0000428 ( socket_errno( ) != EAGAIN ) &&
Daniel Veillardba6db032001-07-31 16:25:45 +0000429#endif
Daniel Veillardf012a642001-07-23 19:10:52 +0000430 ( socket_errno( ) != EWOULDBLOCK ) ) {
431 xmlGenericError( xmlGenericErrorContext,
432 "xmlNanoHTTPSend error: %s",
433 strerror( socket_errno( ) ) );
434
435 if ( total_sent == 0 )
436 total_sent = -1;
437 break;
438 }
439 else {
440 /*
441 ** No data sent
442 ** Since non-blocking sockets are used, wait for
443 ** socket to be writable or default timeout prior
444 ** to retrying.
445 */
446
447 struct timeval tv;
448 fd_set wfd;
449
450 tv.tv_sec = timeout;
451 tv.tv_usec = 0;
452 FD_ZERO( &wfd );
453 FD_SET( ctxt->fd, &wfd );
454 (void)select( ctxt->fd + 1, NULL, &wfd, NULL, &tv );
455 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000456 }
Owen Taylor3473f882001-02-23 17:55:21 +0000457 }
Daniel Veillardf012a642001-07-23 19:10:52 +0000458
459 return total_sent;
Owen Taylor3473f882001-02-23 17:55:21 +0000460}
461
462/**
463 * xmlNanoHTTPRecv:
464 * @ctxt: an HTTP context
465 *
466 * Read information coming from the HTTP connection.
467 * This is a blocking call (but it blocks in select(), not read()).
468 *
469 * Returns the number of byte read or -1 in case of error.
470 */
471
472static int
473xmlNanoHTTPRecv(xmlNanoHTTPCtxtPtr ctxt) {
474 fd_set rfd;
475 struct timeval tv;
476
477
478 while (ctxt->state & XML_NANO_HTTP_READ) {
479 if (ctxt->in == NULL) {
480 ctxt->in = (char *) xmlMalloc(65000 * sizeof(char));
481 if (ctxt->in == NULL) {
482 ctxt->last = -1;
Daniel Veillardf012a642001-07-23 19:10:52 +0000483 xmlGenericError( xmlGenericErrorContext,
484 "xmlNanoHTTPRecv: Error allocating input memory." );
Owen Taylor3473f882001-02-23 17:55:21 +0000485 return(-1);
486 }
487 ctxt->inlen = 65000;
488 ctxt->inptr = ctxt->content = ctxt->inrptr = ctxt->in;
489 }
490 if (ctxt->inrptr > ctxt->in + XML_NANO_HTTP_CHUNK) {
491 int delta = ctxt->inrptr - ctxt->in;
492 int len = ctxt->inptr - ctxt->inrptr;
493
494 memmove(ctxt->in, ctxt->inrptr, len);
495 ctxt->inrptr -= delta;
496 ctxt->content -= delta;
497 ctxt->inptr -= delta;
498 }
499 if ((ctxt->in + ctxt->inlen) < (ctxt->inptr + XML_NANO_HTTP_CHUNK)) {
500 int d_inptr = ctxt->inptr - ctxt->in;
501 int d_content = ctxt->content - ctxt->in;
502 int d_inrptr = ctxt->inrptr - ctxt->in;
Daniel Veillardf012a642001-07-23 19:10:52 +0000503 char * tmp_ptr = ctxt->in;
Owen Taylor3473f882001-02-23 17:55:21 +0000504
505 ctxt->inlen *= 2;
Daniel Veillardf012a642001-07-23 19:10:52 +0000506 ctxt->in = (char *) xmlRealloc(tmp_ptr, ctxt->inlen);
Owen Taylor3473f882001-02-23 17:55:21 +0000507 if (ctxt->in == NULL) {
Daniel Veillardf012a642001-07-23 19:10:52 +0000508 xmlGenericError( xmlGenericErrorContext,
509 "xmlNanoHTTPRecv: %s %d bytes.",
510 "Failed to realloc input buffer to",
511 ctxt->inlen );
512 xmlFree( tmp_ptr );
Owen Taylor3473f882001-02-23 17:55:21 +0000513 ctxt->last = -1;
514 return(-1);
515 }
516 ctxt->inptr = ctxt->in + d_inptr;
517 ctxt->content = ctxt->in + d_content;
518 ctxt->inrptr = ctxt->in + d_inrptr;
519 }
520 ctxt->last = recv(ctxt->fd, ctxt->inptr, XML_NANO_HTTP_CHUNK, 0);
521 if (ctxt->last > 0) {
522 ctxt->inptr += ctxt->last;
523 return(ctxt->last);
524 }
525 if (ctxt->last == 0) {
526 return(0);
527 }
528 if (ctxt->last == -1) {
529 switch (socket_errno()) {
530 case EINPROGRESS:
531 case EWOULDBLOCK:
532#if defined(EAGAIN) && EAGAIN != EWOULDBLOCK
533 case EAGAIN:
534#endif
535 break;
Daniel Veillardf012a642001-07-23 19:10:52 +0000536
537 case ECONNRESET:
538 case ESHUTDOWN:
539 return ( 0 );
540
Owen Taylor3473f882001-02-23 17:55:21 +0000541 default:
Daniel Veillardf012a642001-07-23 19:10:52 +0000542 xmlGenericError( xmlGenericErrorContext,
543 "xmlNanoHTTPRecv: recv( ) failure - %s",
544 strerror( socket_errno( ) ) );
545 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +0000546 }
547 }
548
549 tv.tv_sec = timeout;
550 tv.tv_usec = 0;
551 FD_ZERO(&rfd);
552 FD_SET(ctxt->fd, &rfd);
553
Daniel Veillard50f34372001-08-03 12:06:36 +0000554 if ( (select(ctxt->fd+1, &rfd, NULL, NULL, &tv)<1)
555#if defined(EINTR)
556 && (errno != EINTR)
557#endif
558 )
Owen Taylor3473f882001-02-23 17:55:21 +0000559 return(0);
560 }
561 return(0);
562}
563
564/**
565 * xmlNanoHTTPReadLine:
566 * @ctxt: an HTTP context
567 *
568 * Read one line in the HTTP server output, usually for extracting
569 * the HTTP protocol informations from the answer header.
570 *
571 * Returns a newly allocated string with a copy of the line, or NULL
572 * which indicate the end of the input.
573 */
574
575static char *
576xmlNanoHTTPReadLine(xmlNanoHTTPCtxtPtr ctxt) {
577 char buf[4096];
578 char *bp = buf;
Daniel Veillardf012a642001-07-23 19:10:52 +0000579 int rc;
Owen Taylor3473f882001-02-23 17:55:21 +0000580
581 while (bp - buf < 4095) {
582 if (ctxt->inrptr == ctxt->inptr) {
Daniel Veillardf012a642001-07-23 19:10:52 +0000583 if ( (rc = xmlNanoHTTPRecv(ctxt)) == 0) {
Owen Taylor3473f882001-02-23 17:55:21 +0000584 if (bp == buf)
585 return(NULL);
586 else
587 *bp = 0;
588 return(xmlMemStrdup(buf));
589 }
Daniel Veillardf012a642001-07-23 19:10:52 +0000590 else if ( rc == -1 ) {
591 return ( NULL );
592 }
Owen Taylor3473f882001-02-23 17:55:21 +0000593 }
594 *bp = *ctxt->inrptr++;
595 if (*bp == '\n') {
596 *bp = 0;
597 return(xmlMemStrdup(buf));
598 }
599 if (*bp != '\r')
600 bp++;
601 }
602 buf[4095] = 0;
603 return(xmlMemStrdup(buf));
604}
605
606
607/**
608 * xmlNanoHTTPScanAnswer:
609 * @ctxt: an HTTP context
610 * @line: an HTTP header line
611 *
612 * Try to extract useful informations from the server answer.
613 * We currently parse and process:
614 * - The HTTP revision/ return code
615 * - The Content-Type
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000616 * - The Location for redirect processing.
Owen Taylor3473f882001-02-23 17:55:21 +0000617 *
618 * Returns -1 in case of failure, the file descriptor number otherwise
619 */
620
621static void
622xmlNanoHTTPScanAnswer(xmlNanoHTTPCtxtPtr ctxt, const char *line) {
623 const char *cur = line;
624
625 if (line == NULL) return;
626
627 if (!strncmp(line, "HTTP/", 5)) {
628 int version = 0;
629 int ret = 0;
630
631 cur += 5;
632 while ((*cur >= '0') && (*cur <= '9')) {
633 version *= 10;
634 version += *cur - '0';
635 cur++;
636 }
637 if (*cur == '.') {
638 cur++;
639 if ((*cur >= '0') && (*cur <= '9')) {
640 version *= 10;
641 version += *cur - '0';
642 cur++;
643 }
644 while ((*cur >= '0') && (*cur <= '9'))
645 cur++;
646 } else
647 version *= 10;
648 if ((*cur != ' ') && (*cur != '\t')) return;
649 while ((*cur == ' ') || (*cur == '\t')) cur++;
650 if ((*cur < '0') || (*cur > '9')) return;
651 while ((*cur >= '0') && (*cur <= '9')) {
652 ret *= 10;
653 ret += *cur - '0';
654 cur++;
655 }
656 if ((*cur != 0) && (*cur != ' ') && (*cur != '\t')) return;
657 ctxt->returnValue = ret;
658 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Content-Type:", 13)) {
659 cur += 13;
660 while ((*cur == ' ') || (*cur == '\t')) cur++;
661 if (ctxt->contentType != NULL)
662 xmlFree(ctxt->contentType);
663 ctxt->contentType = xmlMemStrdup(cur);
664 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"ContentType:", 12)) {
665 cur += 12;
666 if (ctxt->contentType != NULL) return;
667 while ((*cur == ' ') || (*cur == '\t')) cur++;
668 ctxt->contentType = xmlMemStrdup(cur);
669 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Location:", 9)) {
670 cur += 9;
671 while ((*cur == ' ') || (*cur == '\t')) cur++;
672 if (ctxt->location != NULL)
673 xmlFree(ctxt->location);
674 ctxt->location = xmlMemStrdup(cur);
675 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"WWW-Authenticate:", 17)) {
676 cur += 17;
677 while ((*cur == ' ') || (*cur == '\t')) cur++;
678 if (ctxt->authHeader != NULL)
679 xmlFree(ctxt->authHeader);
680 ctxt->authHeader = xmlMemStrdup(cur);
681 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Proxy-Authenticate:", 19)) {
682 cur += 19;
683 while ((*cur == ' ') || (*cur == '\t')) cur++;
684 if (ctxt->authHeader != NULL)
685 xmlFree(ctxt->authHeader);
686 ctxt->authHeader = xmlMemStrdup(cur);
Daniel Veillardf012a642001-07-23 19:10:52 +0000687 } else if ( !xmlStrncasecmp( BAD_CAST line, BAD_CAST"Content-Length:", 15) ) {
688 cur += 15;
689 ctxt->ContentLength = strtol( cur, NULL, 10 );
Owen Taylor3473f882001-02-23 17:55:21 +0000690 }
691}
692
693/**
694 * xmlNanoHTTPConnectAttempt:
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000695 * @addr: a socket address structure
Owen Taylor3473f882001-02-23 17:55:21 +0000696 *
697 * Attempt a connection to the given IP:port endpoint. It forces
698 * non-blocking semantic on the socket, and allow 60 seconds for
699 * the host to answer.
700 *
701 * Returns -1 in case of failure, the file descriptor number otherwise
702 */
703
704static int
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000705xmlNanoHTTPConnectAttempt(struct sockaddr *addr)
Owen Taylor3473f882001-02-23 17:55:21 +0000706{
707 SOCKET s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
708 fd_set wfd;
709 struct timeval tv;
710 int status;
711
712 if (s==-1) {
713#ifdef DEBUG_HTTP
714 perror("socket");
715#endif
Daniel Veillardf012a642001-07-23 19:10:52 +0000716 xmlGenericError( xmlGenericErrorContext,
717 "xmlNanoHTTPConnectAttempt: %s - %s",
718 "socket creation failure",
719 strerror( socket_errno( ) ) );
Owen Taylor3473f882001-02-23 17:55:21 +0000720 return(-1);
721 }
722
723#ifdef _WINSOCKAPI_
724 {
725 u_long one = 1;
726
727 status = ioctlsocket(s, FIONBIO, &one) == SOCKET_ERROR ? -1 : 0;
728 }
729#else /* _WINSOCKAPI_ */
730#if defined(VMS)
731 {
732 int enable = 1;
733 status = ioctl(s, FIONBIO, &enable);
734 }
735#else /* VMS */
736 if ((status = fcntl(s, F_GETFL, 0)) != -1) {
737#ifdef O_NONBLOCK
738 status |= O_NONBLOCK;
739#else /* O_NONBLOCK */
740#ifdef F_NDELAY
741 status |= F_NDELAY;
742#endif /* F_NDELAY */
743#endif /* !O_NONBLOCK */
744 status = fcntl(s, F_SETFL, status);
745 }
746 if (status < 0) {
747#ifdef DEBUG_HTTP
748 perror("nonblocking");
749#endif
Daniel Veillardf012a642001-07-23 19:10:52 +0000750 xmlGenericError( xmlGenericErrorContext,
751 "xmlNanoHTTPConnectAttempt: %s - %s",
752 "error setting non-blocking IO",
753 strerror( socket_errno( ) ) );
Owen Taylor3473f882001-02-23 17:55:21 +0000754 closesocket(s);
755 return(-1);
756 }
757#endif /* !VMS */
758#endif /* !_WINSOCKAPI_ */
759
Owen Taylor3473f882001-02-23 17:55:21 +0000760 if ((connect(s, addr, sizeof(*addr))==-1)) {
761 switch (socket_errno()) {
762 case EINPROGRESS:
763 case EWOULDBLOCK:
764 break;
765 default:
Daniel Veillardf012a642001-07-23 19:10:52 +0000766 xmlGenericError( xmlGenericErrorContext,
767 "xmlNanoHTTPConnectAttempt: %s - %s",
768 "error connecting to HTTP server",
769 strerror( socket_errno( ) ) );
Owen Taylor3473f882001-02-23 17:55:21 +0000770 closesocket(s);
771 return(-1);
772 }
773 }
774
775 tv.tv_sec = timeout;
776 tv.tv_usec = 0;
777
778 FD_ZERO(&wfd);
779 FD_SET(s, &wfd);
780
781 switch(select(s+1, NULL, &wfd, NULL, &tv))
782 {
783 case 0:
784 /* Time out */
Daniel Veillardf012a642001-07-23 19:10:52 +0000785 xmlGenericError( xmlGenericErrorContext,
786 "xmlNanoHTTPConnectAttempt: %s",
787 "Connect attempt timed out." );
Owen Taylor3473f882001-02-23 17:55:21 +0000788 closesocket(s);
789 return(-1);
790 case -1:
791 /* Ermm.. ?? */
Daniel Veillardf012a642001-07-23 19:10:52 +0000792 xmlGenericError( xmlGenericErrorContext,
793 "xmlNanoHTTPConnectAttempt: %s - %s",
794 "Error connecting to host",
795 strerror( socket_errno( ) ) );
Owen Taylor3473f882001-02-23 17:55:21 +0000796 closesocket(s);
797 return(-1);
798 }
799
800 if ( FD_ISSET(s, &wfd) ) {
801 SOCKLEN_T len;
802 len = sizeof(status);
803 if (getsockopt(s, SOL_SOCKET, SO_ERROR, (char*)&status, &len) < 0 ) {
804 /* Solaris error code */
Daniel Veillardf012a642001-07-23 19:10:52 +0000805 xmlGenericError( xmlGenericErrorContext,
806 "xmlNanoHTTPConnectAttempt: %s - %s",
807 "Error retrieving pending socket errors",
808 strerror( socket_errno( ) ) );
Owen Taylor3473f882001-02-23 17:55:21 +0000809 return (-1);
810 }
811 if ( status ) {
812 closesocket(s);
813 errno = status;
Daniel Veillardf012a642001-07-23 19:10:52 +0000814 xmlGenericError( xmlGenericErrorContext,
815 "xmlNanoHTTPConnectAttempt: %s - %s",
816 "Error connecting to remote host",
817 strerror( status ) );
Owen Taylor3473f882001-02-23 17:55:21 +0000818 return (-1);
819 }
820 } else {
821 /* pbm */
Daniel Veillardf012a642001-07-23 19:10:52 +0000822 xmlGenericError( xmlGenericErrorContext,
823 "xmlNanoHTTPConnectAttempt: %s\n",
824 "Select returned, but descriptor not set for connection.\n" );
825 closesocket(s);
Owen Taylor3473f882001-02-23 17:55:21 +0000826 return (-1);
827 }
828
829 return(s);
830}
831
832/**
833 * xmlNanoHTTPConnectHost:
834 * @host: the host name
835 * @port: the port number
836 *
837 * Attempt a connection to the given host:port endpoint. It tries
838 * the multiple IP provided by the DNS if available.
839 *
840 * Returns -1 in case of failure, the file descriptor number otherwise
841 */
842
843static int
844xmlNanoHTTPConnectHost(const char *host, int port)
845{
846 struct hostent *h;
847 struct sockaddr *addr;
848 struct in_addr ia;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000849 struct sockaddr_in sockin;
Daniel Veillard5c396542002-03-15 07:57:50 +0000850
Owen Taylor3473f882001-02-23 17:55:21 +0000851#ifdef SUPPORT_IP6
852 struct in6_addr ia6;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000853 struct sockaddr_in6 sockin6;
Owen Taylor3473f882001-02-23 17:55:21 +0000854#endif
855 int i;
856 int s;
Daniel Veillard5c396542002-03-15 07:57:50 +0000857
Owen Taylor3473f882001-02-23 17:55:21 +0000858#if defined(SUPPORT_IP6) && defined(RES_USE_INET6)
859 if (!(_res.options & RES_INIT))
Daniel Veillard5c396542002-03-15 07:57:50 +0000860 res_init();
Owen Taylor3473f882001-02-23 17:55:21 +0000861 _res.options |= RES_USE_INET6;
862#endif
Daniel Veillard5c396542002-03-15 07:57:50 +0000863 h = gethostbyname(host);
864 if (h == NULL) {
865#if defined(HAVE_NETDB_H) && defined(HOST_NOT_FOUND)
866 const char *h_err_txt = "";
Daniel Veillardf012a642001-07-23 19:10:52 +0000867
Daniel Veillard5c396542002-03-15 07:57:50 +0000868 switch (h_errno) {
869 case HOST_NOT_FOUND:
870 h_err_txt = "Authoritive host not found";
871 break;
Daniel Veillardf012a642001-07-23 19:10:52 +0000872
Daniel Veillard5c396542002-03-15 07:57:50 +0000873 case TRY_AGAIN:
874 h_err_txt =
875 "Non-authoritive host not found or server failure.";
876 break;
Daniel Veillardf012a642001-07-23 19:10:52 +0000877
Daniel Veillard5c396542002-03-15 07:57:50 +0000878 case NO_RECOVERY:
879 h_err_txt =
880 "Non-recoverable errors: FORMERR, REFUSED, or NOTIMP.";
881 break;
882
883 case NO_ADDRESS:
884 h_err_txt =
885 "Valid name, no data record of requested type.";
886 break;
887
888 default:
889 h_err_txt = "No error text defined.";
890 break;
891 }
892 xmlGenericError(xmlGenericErrorContext,
893 "xmlNanoHTTPConnectHost: %s '%s' - %s",
894 "Failed to resolve host", host, h_err_txt);
895#else
896 xmlGenericError(xmlGenericErrorContext,
897 "xmlNanoHTTPConnectHost: %s '%s'",
898 "Failed to resolve host", host);
Owen Taylor3473f882001-02-23 17:55:21 +0000899#endif
Daniel Veillard5c396542002-03-15 07:57:50 +0000900 return (-1);
901 }
902
903 for (i = 0; h->h_addr_list[i]; i++) {
904 if (h->h_addrtype == AF_INET) {
905 /* A records (IPv4) */
906 memcpy(&ia, h->h_addr_list[i], h->h_length);
907 sockin.sin_family = h->h_addrtype;
908 sockin.sin_addr = ia;
909 sockin.sin_port = htons(port);
910 addr = (struct sockaddr *) &sockin;
911#ifdef SUPPORT_IP6
912 } else if (h->h_addrtype == AF_INET6) {
913 /* AAAA records (IPv6) */
914 memcpy(&ia6, h->h_addr_list[i], h->h_length);
915 sockin6.sin_family = h->h_addrtype;
916 sockin6.sin_addr = ia6;
917 sockin6.sin_port = htons(port);
918 addr = (struct sockaddr *) &sockin6;
919#endif
920 } else
921 break; /* for */
922
923 s = xmlNanoHTTPConnectAttempt(addr);
924 if (s != -1)
925 return (s);
Owen Taylor3473f882001-02-23 17:55:21 +0000926 }
927
928#ifdef DEBUG_HTTP
929 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard5c396542002-03-15 07:57:50 +0000930 "xmlNanoHTTPConnectHost: unable to connect to '%s'.\n",
931 host);
Owen Taylor3473f882001-02-23 17:55:21 +0000932#endif
Daniel Veillard5c396542002-03-15 07:57:50 +0000933 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +0000934}
935
936
937/**
938 * xmlNanoHTTPOpen:
939 * @URL: The URL to load
940 * @contentType: if available the Content-Type information will be
941 * returned at that location
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*
951xmlNanoHTTPOpen(const char *URL, char **contentType) {
952 if (contentType != NULL) *contentType = NULL;
Daniel Veillardf012a642001-07-23 19:10:52 +0000953 return(xmlNanoHTTPMethod(URL, NULL, NULL, contentType, NULL, 0));
Daniel Veillard9403a042001-05-28 11:00:53 +0000954}
955
956/**
957 * xmlNanoHTTPOpenRedir:
958 * @URL: The URL to load
959 * @contentType: if available the Content-Type information will be
960 * returned at that location
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000961 * @redir: if available the redirected URL will be returned
Daniel Veillard9403a042001-05-28 11:00:53 +0000962 *
963 * This function try to open a connection to the indicated resource
964 * via HTTP GET.
965 *
966 * Returns NULL in case of failure, otherwise a request handler.
967 * The contentType, if provided must be freed by the caller
968 */
969
970void*
971xmlNanoHTTPOpenRedir(const char *URL, char **contentType, char **redir) {
972 if (contentType != NULL) *contentType = NULL;
973 if (redir != NULL) *redir = NULL;
Daniel Veillardf012a642001-07-23 19:10:52 +0000974 return(xmlNanoHTTPMethodRedir(URL, NULL, NULL, contentType, redir, NULL,0));
Owen Taylor3473f882001-02-23 17:55:21 +0000975}
976
977/**
978 * xmlNanoHTTPRead:
979 * @ctx: the HTTP context
980 * @dest: a buffer
981 * @len: the buffer length
982 *
983 * This function tries to read @len bytes from the existing HTTP connection
984 * and saves them in @dest. This is a blocking call.
985 *
986 * Returns the number of byte read. 0 is an indication of an end of connection.
987 * -1 indicates a parameter error.
988 */
989int
990xmlNanoHTTPRead(void *ctx, void *dest, int len) {
991 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
992
993 if (ctx == NULL) return(-1);
994 if (dest == NULL) return(-1);
995 if (len <= 0) return(0);
996
997 while (ctxt->inptr - ctxt->inrptr < len) {
Daniel Veillardf012a642001-07-23 19:10:52 +0000998 if (xmlNanoHTTPRecv(ctxt) <= 0) break;
Owen Taylor3473f882001-02-23 17:55:21 +0000999 }
1000 if (ctxt->inptr - ctxt->inrptr < len)
1001 len = ctxt->inptr - ctxt->inrptr;
1002 memcpy(dest, ctxt->inrptr, len);
1003 ctxt->inrptr += len;
1004 return(len);
1005}
1006
1007/**
1008 * xmlNanoHTTPClose:
1009 * @ctx: the HTTP context
1010 *
1011 * This function closes an HTTP context, it ends up the connection and
1012 * free all data related to it.
1013 */
1014void
1015xmlNanoHTTPClose(void *ctx) {
1016 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1017
1018 if (ctx == NULL) return;
1019
1020 xmlNanoHTTPFreeCtxt(ctxt);
1021}
1022
1023/**
Daniel Veillard9403a042001-05-28 11:00:53 +00001024 * xmlNanoHTTPMethodRedir:
Owen Taylor3473f882001-02-23 17:55:21 +00001025 * @URL: The URL to load
1026 * @method: the HTTP method to use
1027 * @input: the input string if any
1028 * @contentType: the Content-Type information IN and OUT
Daniel Veillard9403a042001-05-28 11:00:53 +00001029 * @redir: the redirected URL OUT
Owen Taylor3473f882001-02-23 17:55:21 +00001030 * @headers: the extra headers
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001031 * @ilen: input length
Owen Taylor3473f882001-02-23 17:55:21 +00001032 *
1033 * This function try to open a connection to the indicated resource
1034 * via HTTP using the given @method, adding the given extra headers
1035 * and the input buffer for the request content.
1036 *
1037 * Returns NULL in case of failure, otherwise a request handler.
Daniel Veillard9403a042001-05-28 11:00:53 +00001038 * The contentType, or redir, if provided must be freed by the caller
Owen Taylor3473f882001-02-23 17:55:21 +00001039 */
1040
1041void*
Daniel Veillard9403a042001-05-28 11:00:53 +00001042xmlNanoHTTPMethodRedir(const char *URL, const char *method, const char *input,
Daniel Veillardf012a642001-07-23 19:10:52 +00001043 char **contentType, char **redir,
1044 const char *headers, int ilen ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001045 xmlNanoHTTPCtxtPtr ctxt;
1046 char *bp, *p;
Daniel Veillardf012a642001-07-23 19:10:52 +00001047 int blen, ret;
Owen Taylor3473f882001-02-23 17:55:21 +00001048 int head;
Daniel Veillardf012a642001-07-23 19:10:52 +00001049 int xmt_bytes;
Owen Taylor3473f882001-02-23 17:55:21 +00001050 int nbRedirects = 0;
1051 char *redirURL = NULL;
1052
1053 if (URL == NULL) return(NULL);
1054 if (method == NULL) method = "GET";
1055 xmlNanoHTTPInit();
1056
1057retry:
1058 if (redirURL == NULL)
1059 ctxt = xmlNanoHTTPNewCtxt(URL);
1060 else {
1061 ctxt = xmlNanoHTTPNewCtxt(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001062 }
1063
Daniel Veillardf012a642001-07-23 19:10:52 +00001064 if ( ctxt == NULL ) {
1065 xmlGenericError( xmlGenericErrorContext,
1066 "xmlNanoHTTPMethodRedir: %s %s.",
1067 "Unable to allocate HTTP context to URI",
1068 ( ( redirURL == NULL ) ? URL : redirURL ) );
1069 return ( NULL );
1070 }
1071
Owen Taylor3473f882001-02-23 17:55:21 +00001072 if ((ctxt->protocol == NULL) || (strcmp(ctxt->protocol, "http"))) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001073 xmlGenericError( xmlGenericErrorContext,
1074 "xmlNanoHTTPMethodRedir: %s - %s.",
1075 "Not a valid HTTP URI",
1076 ( ( redirURL == NULL ) ? URL : redirURL ) );
Owen Taylor3473f882001-02-23 17:55:21 +00001077 xmlNanoHTTPFreeCtxt(ctxt);
1078 if (redirURL != NULL) xmlFree(redirURL);
1079 return(NULL);
1080 }
1081 if (ctxt->hostname == NULL) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001082 xmlGenericError( xmlGenericErrorContext,
1083 "xmlNanoHTTPMethodRedir: %s - %s",
1084 "Failed to identify host in URI",
1085 ( ( redirURL == NULL ) ? URL : redirURL ) );
Owen Taylor3473f882001-02-23 17:55:21 +00001086 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001087 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001088 return(NULL);
1089 }
1090 if (proxy) {
1091 blen = strlen(ctxt->hostname) * 2 + 16;
1092 ret = xmlNanoHTTPConnectHost(proxy, proxyPort);
1093 }
1094 else {
1095 blen = strlen(ctxt->hostname);
1096 ret = xmlNanoHTTPConnectHost(ctxt->hostname, ctxt->port);
1097 }
1098 if (ret < 0) {
1099 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001100 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001101 return(NULL);
1102 }
1103 ctxt->fd = ret;
1104
Daniel Veillardf012a642001-07-23 19:10:52 +00001105 if (input == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00001106 ilen = 0;
Daniel Veillardf012a642001-07-23 19:10:52 +00001107 else
1108 blen += 36;
1109
Owen Taylor3473f882001-02-23 17:55:21 +00001110 if (headers != NULL)
Daniel Veillardf012a642001-07-23 19:10:52 +00001111 blen += strlen(headers) + 2;
Owen Taylor3473f882001-02-23 17:55:21 +00001112 if (contentType && *contentType)
1113 blen += strlen(*contentType) + 16;
Daniel Veillardf012a642001-07-23 19:10:52 +00001114 blen += strlen(method) + strlen(ctxt->path) + 24;
Owen Taylor3473f882001-02-23 17:55:21 +00001115 bp = xmlMalloc(blen);
Daniel Veillardf012a642001-07-23 19:10:52 +00001116 if ( bp == NULL ) {
1117 xmlNanoHTTPFreeCtxt( ctxt );
1118 xmlGenericError( xmlGenericErrorContext,
1119 "xmlNanoHTTPMethodRedir: %s",
1120 "Error allocating HTTP header buffer." );
1121 return ( NULL );
1122 }
1123
1124 p = bp;
1125
Owen Taylor3473f882001-02-23 17:55:21 +00001126 if (proxy) {
1127 if (ctxt->port != 80) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001128 p += sprintf( p, "%s http://%s:%d%s", method, ctxt->hostname,
1129 ctxt->port, ctxt->path );
Owen Taylor3473f882001-02-23 17:55:21 +00001130 }
1131 else
Daniel Veillardf012a642001-07-23 19:10:52 +00001132 p += sprintf( p, "%s http://%s%s", method,
1133 ctxt->hostname, ctxt->path);
Owen Taylor3473f882001-02-23 17:55:21 +00001134 }
1135 else
Daniel Veillardf012a642001-07-23 19:10:52 +00001136 p += sprintf( p, "%s %s", method, ctxt->path);
1137
1138 p += sprintf(p, " HTTP/1.0\r\nHost: %s\r\n", ctxt->hostname);
1139
1140 if (contentType != NULL && *contentType)
1141 p += sprintf(p, "Content-Type: %s\r\n", *contentType);
1142
1143 if (headers != NULL)
1144 p += sprintf( p, "%s", headers );
1145
Owen Taylor3473f882001-02-23 17:55:21 +00001146 if (input != NULL)
Daniel Veillardf012a642001-07-23 19:10:52 +00001147 sprintf(p, "Content-Length: %d\r\n\r\n", ilen );
Owen Taylor3473f882001-02-23 17:55:21 +00001148 else
1149 strcpy(p, "\r\n");
Daniel Veillardf012a642001-07-23 19:10:52 +00001150
Owen Taylor3473f882001-02-23 17:55:21 +00001151#ifdef DEBUG_HTTP
1152 xmlGenericError(xmlGenericErrorContext,
1153 "-> %s%s", proxy? "(Proxy) " : "", bp);
1154 if ((blen -= strlen(bp)+1) < 0)
1155 xmlGenericError(xmlGenericErrorContext,
1156 "ERROR: overflowed buffer by %d bytes\n", -blen);
1157#endif
1158 ctxt->outptr = ctxt->out = bp;
1159 ctxt->state = XML_NANO_HTTP_WRITE;
Daniel Veillardf012a642001-07-23 19:10:52 +00001160 blen = strlen( ctxt->out );
1161 xmt_bytes = xmlNanoHTTPSend(ctxt, ctxt->out, blen );
1162#ifdef DEBUG_HTTP
1163 if ( xmt_bytes != blen )
1164 xmlGenericError( xmlGenericErrorContext,
1165 "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
1166 xmt_bytes, blen,
1167 "bytes of HTTP headers sent to host",
1168 ctxt->hostname );
1169#endif
1170
1171 if ( input != NULL ) {
1172 xmt_bytes = xmlNanoHTTPSend( ctxt, input, ilen );
1173
1174#ifdef DEBUG_HTTP
1175 if ( xmt_bytes != ilen )
1176 xmlGenericError( xmlGenericErrorContext,
1177 "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
1178 xmt_bytes, ilen,
1179 "bytes of HTTP content sent to host",
1180 ctxt->hostname );
1181#endif
1182 }
1183
Owen Taylor3473f882001-02-23 17:55:21 +00001184 ctxt->state = XML_NANO_HTTP_READ;
1185 head = 1;
1186
1187 while ((p = xmlNanoHTTPReadLine(ctxt)) != NULL) {
1188 if (head && (*p == 0)) {
1189 head = 0;
1190 ctxt->content = ctxt->inrptr;
1191 xmlFree(p);
1192 break;
1193 }
1194 xmlNanoHTTPScanAnswer(ctxt, p);
1195
1196#ifdef DEBUG_HTTP
1197 xmlGenericError(xmlGenericErrorContext, "<- %s\n", p);
1198#endif
1199 xmlFree(p);
1200 }
1201
1202 if ((ctxt->location != NULL) && (ctxt->returnValue >= 300) &&
1203 (ctxt->returnValue < 400)) {
1204#ifdef DEBUG_HTTP
1205 xmlGenericError(xmlGenericErrorContext,
1206 "\nRedirect to: %s\n", ctxt->location);
1207#endif
Daniel Veillardf012a642001-07-23 19:10:52 +00001208 while ( xmlNanoHTTPRecv(ctxt) > 0 ) ;
Owen Taylor3473f882001-02-23 17:55:21 +00001209 if (nbRedirects < XML_NANO_HTTP_MAX_REDIR) {
1210 nbRedirects++;
Daniel Veillard9403a042001-05-28 11:00:53 +00001211 if (redirURL != NULL)
1212 xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001213 redirURL = xmlMemStrdup(ctxt->location);
1214 xmlNanoHTTPFreeCtxt(ctxt);
1215 goto retry;
1216 }
1217 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001218 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001219#ifdef DEBUG_HTTP
1220 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardf012a642001-07-23 19:10:52 +00001221 "xmlNanoHTTPMethodRedir: Too many redirects, aborting ...\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001222#endif
1223 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001224 }
1225
1226 if (contentType != NULL) {
1227 if (ctxt->contentType != NULL)
1228 *contentType = xmlMemStrdup(ctxt->contentType);
1229 else
1230 *contentType = NULL;
1231 }
1232
Daniel Veillard9403a042001-05-28 11:00:53 +00001233 if ((redir != NULL) && (redirURL != NULL)) {
1234 *redir = redirURL;
1235 } else {
1236 if (redirURL != NULL)
1237 xmlFree(redirURL);
1238 if (redir != NULL)
1239 *redir = NULL;
1240 }
1241
Owen Taylor3473f882001-02-23 17:55:21 +00001242#ifdef DEBUG_HTTP
1243 if (ctxt->contentType != NULL)
1244 xmlGenericError(xmlGenericErrorContext,
1245 "\nCode %d, content-type '%s'\n\n",
1246 ctxt->returnValue, ctxt->contentType);
1247 else
1248 xmlGenericError(xmlGenericErrorContext,
1249 "\nCode %d, no content-type\n\n",
1250 ctxt->returnValue);
1251#endif
1252
1253 return((void *) ctxt);
1254}
1255
1256/**
Daniel Veillard9403a042001-05-28 11:00:53 +00001257 * xmlNanoHTTPMethod:
1258 * @URL: The URL to load
1259 * @method: the HTTP method to use
1260 * @input: the input string if any
1261 * @contentType: the Content-Type information IN and OUT
1262 * @headers: the extra headers
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001263 * @ilen: input length
Daniel Veillard9403a042001-05-28 11:00:53 +00001264 *
1265 * This function try to open a connection to the indicated resource
1266 * via HTTP using the given @method, adding the given extra headers
1267 * and the input buffer for the request content.
1268 *
1269 * Returns NULL in case of failure, otherwise a request handler.
1270 * The contentType, if provided must be freed by the caller
1271 */
1272
1273void*
1274xmlNanoHTTPMethod(const char *URL, const char *method, const char *input,
Daniel Veillardf012a642001-07-23 19:10:52 +00001275 char **contentType, const char *headers, int ilen) {
Daniel Veillard9403a042001-05-28 11:00:53 +00001276 return(xmlNanoHTTPMethodRedir(URL, method, input, contentType,
Daniel Veillardf012a642001-07-23 19:10:52 +00001277 NULL, headers, ilen));
Daniel Veillard9403a042001-05-28 11:00:53 +00001278}
1279
1280/**
Owen Taylor3473f882001-02-23 17:55:21 +00001281 * xmlNanoHTTPFetch:
1282 * @URL: The URL to load
1283 * @filename: the filename where the content should be saved
1284 * @contentType: if available the Content-Type information will be
1285 * returned at that location
1286 *
1287 * This function try to fetch the indicated resource via HTTP GET
1288 * and save it's content in the file.
1289 *
1290 * Returns -1 in case of failure, 0 incase of success. The contentType,
1291 * if provided must be freed by the caller
1292 */
1293int
1294xmlNanoHTTPFetch(const char *URL, const char *filename, char **contentType) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001295 void *ctxt = NULL;
1296 char *buf = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001297 int fd;
1298 int len;
1299
1300 ctxt = xmlNanoHTTPOpen(URL, contentType);
1301 if (ctxt == NULL) return(-1);
1302
1303 if (!strcmp(filename, "-"))
1304 fd = 0;
1305 else {
1306 fd = open(filename, O_CREAT | O_WRONLY, 00644);
1307 if (fd < 0) {
1308 xmlNanoHTTPClose(ctxt);
1309 if ((contentType != NULL) && (*contentType != NULL)) {
1310 xmlFree(*contentType);
1311 *contentType = NULL;
1312 }
1313 return(-1);
1314 }
1315 }
1316
Daniel Veillardf012a642001-07-23 19:10:52 +00001317 xmlNanoHTTPFetchContent( ctxt, &buf, &len );
1318 if ( len > 0 ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001319 write(fd, buf, len);
1320 }
1321
1322 xmlNanoHTTPClose(ctxt);
1323 close(fd);
1324 return(0);
1325}
1326
1327/**
1328 * xmlNanoHTTPSave:
1329 * @ctxt: the HTTP context
1330 * @filename: the filename where the content should be saved
1331 *
1332 * This function saves the output of the HTTP transaction to a file
1333 * It closes and free the context at the end
1334 *
1335 * Returns -1 in case of failure, 0 incase of success.
1336 */
1337int
1338xmlNanoHTTPSave(void *ctxt, const char *filename) {
Daniel Veillarde3924972001-07-25 20:25:21 +00001339 char *buf = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001340 int fd;
1341 int len;
1342
1343 if (ctxt == NULL) return(-1);
1344
1345 if (!strcmp(filename, "-"))
1346 fd = 0;
1347 else {
1348 fd = open(filename, O_CREAT | O_WRONLY);
1349 if (fd < 0) {
1350 xmlNanoHTTPClose(ctxt);
1351 return(-1);
1352 }
1353 }
1354
Daniel Veillardf012a642001-07-23 19:10:52 +00001355 xmlNanoHTTPFetchContent( ctxt, &buf, &len );
1356 if ( len > 0 ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001357 write(fd, buf, len);
1358 }
1359
1360 xmlNanoHTTPClose(ctxt);
1361 return(0);
1362}
1363
1364/**
1365 * xmlNanoHTTPReturnCode:
1366 * @ctx: the HTTP context
1367 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001368 * Get the latest HTTP return code received
1369 *
Owen Taylor3473f882001-02-23 17:55:21 +00001370 * Returns the HTTP return code for the request.
1371 */
1372int
1373xmlNanoHTTPReturnCode(void *ctx) {
1374 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1375
1376 if (ctxt == NULL) return(-1);
1377
1378 return(ctxt->returnValue);
1379}
1380
1381/**
1382 * xmlNanoHTTPAuthHeader:
1383 * @ctx: the HTTP context
1384 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001385 * Get the authentication header of an HTTP context
1386 *
Owen Taylor3473f882001-02-23 17:55:21 +00001387 * Returns the stashed value of the WWW-Authenticate or Proxy-Authenticate
1388 * header.
1389 */
1390const char *
1391xmlNanoHTTPAuthHeader(void *ctx) {
1392 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1393
1394 if (ctxt == NULL) return(NULL);
1395
1396 return(ctxt->authHeader);
1397}
1398
Daniel Veillardf012a642001-07-23 19:10:52 +00001399/**
1400 * xmlNanoHTTPContentLength
1401 * @ctx: the HTTP context
1402 *
1403 * Return the specified content length from the HTTP header. Note that
1404 * a value of -1 indicates that the content length element was not included in
1405 * the response header.
1406 */
1407int
1408xmlNanoHTTPContentLength( void * ctx ) {
1409 xmlNanoHTTPCtxtPtr ctxt = ctx;
1410
1411 return ( ( ctxt == NULL ) ? -1 : ctxt->ContentLength );
1412}
1413
1414/**
1415 * xmlNanoHTTPFetchContent
1416 * @ctx: the HTTP context
1417 * @ptr: pointer to set to the content buffer.
1418 * @len: integer pointer to hold the length of the content
1419 *
1420 * Returns 0 if all the content was read and available, returns
1421 * -1 if received content length was less than specified or an error
1422 * occurred.
1423 */
1424int
1425xmlNanoHTTPFetchContent( void * ctx, char ** ptr, int * len ) {
1426 xmlNanoHTTPCtxtPtr ctxt = ctx;
1427
1428 int rc = 0;
1429 int cur_lgth;
1430 int rcvd_lgth;
1431 int dummy_int;
1432 char * dummy_ptr = NULL;
1433
1434 /* Dummy up return input parameters if not provided */
1435
1436 if ( len == NULL )
1437 len = &dummy_int;
1438
1439 if ( ptr == NULL )
1440 ptr = &dummy_ptr;
1441
1442 /* But can't work without the context pointer */
1443
1444 if ( ( ctxt == NULL ) || ( ctxt->content == NULL ) ) {
1445 *len = 0;
1446 *ptr = NULL;
1447 return ( -1 );
1448 }
1449
1450 rcvd_lgth = ctxt->inptr - ctxt->content;
1451
1452 while ( (cur_lgth = xmlNanoHTTPRecv( ctxt )) > 0 ) {
1453
1454 rcvd_lgth += cur_lgth;
1455 if ( (ctxt->ContentLength > 0) && (rcvd_lgth >= ctxt->ContentLength) )
1456 break;
1457 }
1458
1459 *ptr = ctxt->content;
1460 *len = rcvd_lgth;
1461
1462 if ( ( ctxt->ContentLength > 0 ) && ( rcvd_lgth < ctxt->ContentLength ) )
1463 rc = -1;
1464 else if ( rcvd_lgth == 0 )
1465 rc = -1;
1466
1467 return ( rc );
1468}
1469
Owen Taylor3473f882001-02-23 17:55:21 +00001470#ifdef STANDALONE
1471int main(int argc, char **argv) {
1472 char *contentType = NULL;
1473
1474 if (argv[1] != NULL) {
1475 if (argv[2] != NULL)
1476 xmlNanoHTTPFetch(argv[1], argv[2], &contentType);
1477 else
1478 xmlNanoHTTPFetch(argv[1], "-", &contentType);
1479 if (contentType != NULL) xmlFree(contentType);
1480 } else {
1481 xmlGenericError(xmlGenericErrorContext,
1482 "%s: minimal HTTP GET implementation\n", argv[0]);
1483 xmlGenericError(xmlGenericErrorContext,
1484 "\tusage %s [ URL [ filename ] ]\n", argv[0]);
1485 }
1486 xmlNanoHTTPCleanup();
1487 xmlMemoryDump();
1488 return(0);
1489}
1490#endif /* STANDALONE */
1491#else /* !LIBXML_HTTP_ENABLED */
1492#ifdef STANDALONE
1493#include <stdio.h>
1494int main(int argc, char **argv) {
1495 xmlGenericError(xmlGenericErrorContext,
1496 "%s : HTTP support not compiled in\n", argv[0]);
1497 return(0);
1498}
1499#endif /* STANDALONE */
1500#endif /* LIBXML_HTTP_ENABLED */