blob: f0d3062671db05c0129f68b4a7a77f8c7b241c9b [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
Daniel Veillardd85f4f42002-03-25 10:48:46 +000042#ifdef HAVE_RESOLV_H
43#include <resolv.h>
44#endif
Owen Taylor3473f882001-02-23 17:55:21 +000045#ifdef HAVE_FCNTL_H
46#include <fcntl.h>
47#endif
48#ifdef HAVE_ERRNO_H
49#include <errno.h>
50#endif
51#ifdef HAVE_SYS_TIME_H
52#include <sys/time.h>
53#endif
54#ifdef HAVE_SYS_SELECT_H
55#include <sys/select.h>
56#endif
57#ifdef HAVE_STRINGS_H
58#include <strings.h>
59#endif
60#ifdef SUPPORT_IP6
61#include <resolv.h>
62#endif
63
64#ifdef VMS
65#include <stropts>
66#define SOCKLEN_T unsigned int
67#define SOCKET int
68#endif
69
Daniel Veillardd0463562001-10-13 09:15:48 +000070#include <libxml/globals.h>
Daniel Veillardf012a642001-07-23 19:10:52 +000071#include <libxml/xmlerror.h>
Owen Taylor3473f882001-02-23 17:55:21 +000072#include <libxml/xmlmemory.h>
73#include <libxml/parser.h> /* for xmlStr(n)casecmp() */
74#include <libxml/nanohttp.h>
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000075#include <libxml/globals.h>
Owen Taylor3473f882001-02-23 17:55:21 +000076
77/**
78 * A couple portability macros
79 */
80#ifndef _WINSOCKAPI_
81#define closesocket(s) close(s)
82#define SOCKET int
83#endif
84
Daniel Veillard75be0132002-03-13 10:03:35 +000085#ifndef SOCKLEN_T
86#define SOCKLEN_T unsigned int
87#endif
88#ifndef SOCKET
89#define SOCKET int
90#endif
Daniel Veillardf012a642001-07-23 19:10:52 +000091
Owen Taylor3473f882001-02-23 17:55:21 +000092#ifdef STANDALONE
93#define DEBUG_HTTP
94#define xmlStrncasecmp(a, b, n) strncasecmp((char *)a, (char *)b, n)
95#define xmlStrcasecmpi(a, b) strcasecmp((char *)a, (char *)b)
96#endif
97
98#define XML_NANO_HTTP_MAX_REDIR 10
99
100#define XML_NANO_HTTP_CHUNK 4096
101
102#define XML_NANO_HTTP_CLOSED 0
103#define XML_NANO_HTTP_WRITE 1
104#define XML_NANO_HTTP_READ 2
105#define XML_NANO_HTTP_NONE 4
106
107typedef struct xmlNanoHTTPCtxt {
108 char *protocol; /* the protocol name */
109 char *hostname; /* the host name */
110 int port; /* the port */
111 char *path; /* the path within the URL */
112 SOCKET fd; /* the file descriptor for the socket */
113 int state; /* WRITE / READ / CLOSED */
114 char *out; /* buffer sent (zero terminated) */
115 char *outptr; /* index within the buffer sent */
116 char *in; /* the receiving buffer */
117 char *content; /* the start of the content */
118 char *inptr; /* the next byte to read from network */
119 char *inrptr; /* the next byte to give back to the client */
120 int inlen; /* len of the input buffer */
121 int last; /* return code for last operation */
122 int returnValue; /* the protocol return value */
Daniel Veillardf012a642001-07-23 19:10:52 +0000123 int ContentLength; /* specified content length from HTTP header */
Owen Taylor3473f882001-02-23 17:55:21 +0000124 char *contentType; /* the MIME type for the input */
125 char *location; /* the new URL in case of redirect */
126 char *authHeader; /* contents of {WWW,Proxy}-Authenticate header */
127} xmlNanoHTTPCtxt, *xmlNanoHTTPCtxtPtr;
128
129static int initialized = 0;
130static char *proxy = NULL; /* the proxy name if any */
131static int proxyPort; /* the proxy port if any */
132static unsigned int timeout = 60;/* the select() timeout in seconds */
133
Daniel Veillardf012a642001-07-23 19:10:52 +0000134int xmlNanoHTTPFetchContent( void * ctx, char ** ptr, int * len );
135int xmlNanoHTTPContentLength( void * ctx );
136
Owen Taylor3473f882001-02-23 17:55:21 +0000137/**
138 * A portability function
139 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000140static int socket_errno(void) {
Owen Taylor3473f882001-02-23 17:55:21 +0000141#ifdef _WINSOCKAPI_
142 return(WSAGetLastError());
143#else
144 return(errno);
145#endif
146}
147
148/**
149 * xmlNanoHTTPInit:
150 *
151 * Initialize the HTTP protocol layer.
152 * Currently it just checks for proxy informations
153 */
154
155void
156xmlNanoHTTPInit(void) {
157 const char *env;
158#ifdef _WINSOCKAPI_
159 WSADATA wsaData;
160#endif
161
162 if (initialized)
163 return;
164
165#ifdef _WINSOCKAPI_
166 if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0)
167 return;
168#endif
169
170 if (proxy == NULL) {
171 proxyPort = 80;
172 env = getenv("no_proxy");
173 if (env != NULL)
174 goto done;
175 env = getenv("http_proxy");
176 if (env != NULL) {
177 xmlNanoHTTPScanProxy(env);
178 goto done;
179 }
180 env = getenv("HTTP_PROXY");
181 if (env != NULL) {
182 xmlNanoHTTPScanProxy(env);
183 goto done;
184 }
185 }
186done:
187 initialized = 1;
188}
189
190/**
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000191 * xmlNanoHTTPCleanup:
Owen Taylor3473f882001-02-23 17:55:21 +0000192 *
193 * Cleanup the HTTP protocol layer.
194 */
195
196void
197xmlNanoHTTPCleanup(void) {
198 if (proxy != NULL)
199 xmlFree(proxy);
200#ifdef _WINSOCKAPI_
201 if (initialized)
202 WSACleanup();
203#endif
204 initialized = 0;
205 return;
206}
207
208/**
Owen Taylor3473f882001-02-23 17:55:21 +0000209 * xmlNanoHTTPScanURL:
210 * @ctxt: an HTTP context
211 * @URL: The URL used to initialize the context
212 *
213 * (Re)Initialize an HTTP context by parsing the URL and finding
214 * the protocol host port and path it indicates.
215 */
216
217static void
218xmlNanoHTTPScanURL(xmlNanoHTTPCtxtPtr ctxt, const char *URL) {
219 const char *cur = URL;
220 char buf[4096];
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000221 int indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000222 int port = 0;
223
224 if (ctxt->protocol != NULL) {
225 xmlFree(ctxt->protocol);
226 ctxt->protocol = NULL;
227 }
228 if (ctxt->hostname != NULL) {
229 xmlFree(ctxt->hostname);
230 ctxt->hostname = NULL;
231 }
232 if (ctxt->path != NULL) {
233 xmlFree(ctxt->path);
234 ctxt->path = NULL;
235 }
236 if (URL == NULL) return;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000237 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000238 while (*cur != 0) {
239 if ((cur[0] == ':') && (cur[1] == '/') && (cur[2] == '/')) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000240 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000241 ctxt->protocol = xmlMemStrdup(buf);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000242 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000243 cur += 3;
244 break;
245 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000246 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000247 }
248 if (*cur == 0) return;
249
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000250 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000251 while (1) {
252 if (cur[0] == ':') {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000253 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000254 ctxt->hostname = xmlMemStrdup(buf);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000255 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000256 cur += 1;
257 while ((*cur >= '0') && (*cur <= '9')) {
258 port *= 10;
259 port += *cur - '0';
260 cur++;
261 }
262 if (port != 0) ctxt->port = port;
263 while ((cur[0] != '/') && (*cur != 0))
264 cur++;
265 break;
266 }
267 if ((*cur == '/') || (*cur == 0)) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000268 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000269 ctxt->hostname = xmlMemStrdup(buf);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000270 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000271 break;
272 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000273 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000274 }
275 if (*cur == 0)
276 ctxt->path = xmlMemStrdup("/");
277 else {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000278 indx = 0;
279 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000280 while (*cur != 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000281 buf[indx++] = *cur++;
282 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000283 ctxt->path = xmlMemStrdup(buf);
284 }
285}
286
287/**
288 * xmlNanoHTTPScanProxy:
289 * @URL: The proxy URL used to initialize the proxy context
290 *
291 * (Re)Initialize the HTTP Proxy context by parsing the URL and finding
292 * the protocol host port it indicates.
293 * Should be like http://myproxy/ or http://myproxy:3128/
294 * A NULL URL cleans up proxy informations.
295 */
296
297void
298xmlNanoHTTPScanProxy(const char *URL) {
299 const char *cur = URL;
300 char buf[4096];
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000301 int indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000302 int port = 0;
303
304 if (proxy != NULL) {
305 xmlFree(proxy);
306 proxy = NULL;
307 }
308 if (proxyPort != 0) {
309 proxyPort = 0;
310 }
311#ifdef DEBUG_HTTP
312 if (URL == NULL)
313 xmlGenericError(xmlGenericErrorContext,
314 "Removing HTTP proxy info\n");
315 else
316 xmlGenericError(xmlGenericErrorContext,
317 "Using HTTP proxy %s\n", URL);
318#endif
319 if (URL == NULL) return;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000320 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000321 while (*cur != 0) {
322 if ((cur[0] == ':') && (cur[1] == '/') && (cur[2] == '/')) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000323 buf[indx] = 0;
324 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000325 cur += 3;
326 break;
327 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000328 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000329 }
330 if (*cur == 0) return;
331
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000332 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000333 while (1) {
334 if (cur[0] == ':') {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000335 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000336 proxy = xmlMemStrdup(buf);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000337 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000338 cur += 1;
339 while ((*cur >= '0') && (*cur <= '9')) {
340 port *= 10;
341 port += *cur - '0';
342 cur++;
343 }
344 if (port != 0) proxyPort = port;
345 while ((cur[0] != '/') && (*cur != 0))
346 cur++;
347 break;
348 }
349 if ((*cur == '/') || (*cur == 0)) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000350 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000351 proxy = xmlMemStrdup(buf);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000352 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000353 break;
354 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000355 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000356 }
357}
358
359/**
360 * xmlNanoHTTPNewCtxt:
361 * @URL: The URL used to initialize the context
362 *
363 * Allocate and initialize a new HTTP context.
364 *
365 * Returns an HTTP context or NULL in case of error.
366 */
367
368static xmlNanoHTTPCtxtPtr
369xmlNanoHTTPNewCtxt(const char *URL) {
370 xmlNanoHTTPCtxtPtr ret;
371
372 ret = (xmlNanoHTTPCtxtPtr) xmlMalloc(sizeof(xmlNanoHTTPCtxt));
373 if (ret == NULL) return(NULL);
374
375 memset(ret, 0, sizeof(xmlNanoHTTPCtxt));
376 ret->port = 80;
377 ret->returnValue = 0;
378 ret->fd = -1;
Daniel Veillardf012a642001-07-23 19:10:52 +0000379 ret->ContentLength = -1;
Owen Taylor3473f882001-02-23 17:55:21 +0000380
381 xmlNanoHTTPScanURL(ret, URL);
382
383 return(ret);
384}
385
386/**
387 * xmlNanoHTTPFreeCtxt:
388 * @ctxt: an HTTP context
389 *
390 * Frees the context after closing the connection.
391 */
392
393static void
394xmlNanoHTTPFreeCtxt(xmlNanoHTTPCtxtPtr ctxt) {
395 if (ctxt == NULL) return;
396 if (ctxt->hostname != NULL) xmlFree(ctxt->hostname);
397 if (ctxt->protocol != NULL) xmlFree(ctxt->protocol);
398 if (ctxt->path != NULL) xmlFree(ctxt->path);
399 if (ctxt->out != NULL) xmlFree(ctxt->out);
400 if (ctxt->in != NULL) xmlFree(ctxt->in);
401 if (ctxt->contentType != NULL) xmlFree(ctxt->contentType);
402 if (ctxt->location != NULL) xmlFree(ctxt->location);
403 if (ctxt->authHeader != NULL) xmlFree(ctxt->authHeader);
404 ctxt->state = XML_NANO_HTTP_NONE;
405 if (ctxt->fd >= 0) closesocket(ctxt->fd);
406 ctxt->fd = -1;
407 xmlFree(ctxt);
408}
409
410/**
411 * xmlNanoHTTPSend:
412 * @ctxt: an HTTP context
413 *
414 * Send the input needed to initiate the processing on the server side
Daniel Veillardf012a642001-07-23 19:10:52 +0000415 * Returns number of bytes sent or -1 on error.
Owen Taylor3473f882001-02-23 17:55:21 +0000416 */
417
Daniel Veillardf012a642001-07-23 19:10:52 +0000418static int
419xmlNanoHTTPSend(xmlNanoHTTPCtxtPtr ctxt, const char * xmt_ptr, int outlen) {
420
421 int total_sent = 0;
422
423 if ( (ctxt->state & XML_NANO_HTTP_WRITE) && (xmt_ptr != NULL ) ) {
424 while (total_sent < outlen) {
425 int nsent = send(ctxt->fd, xmt_ptr + total_sent,
426 outlen - total_sent, 0);
Owen Taylor3473f882001-02-23 17:55:21 +0000427 if (nsent>0)
428 total_sent += nsent;
Daniel Veillardf012a642001-07-23 19:10:52 +0000429 else if ( ( nsent == -1 ) &&
Daniel Veillardba6db032001-07-31 16:25:45 +0000430#if defined(EAGAIN) && EAGAIN != EWOULDBLOCK
Daniel Veillardf012a642001-07-23 19:10:52 +0000431 ( socket_errno( ) != EAGAIN ) &&
Daniel Veillardba6db032001-07-31 16:25:45 +0000432#endif
Daniel Veillardf012a642001-07-23 19:10:52 +0000433 ( socket_errno( ) != EWOULDBLOCK ) ) {
434 xmlGenericError( xmlGenericErrorContext,
435 "xmlNanoHTTPSend error: %s",
436 strerror( socket_errno( ) ) );
437
438 if ( total_sent == 0 )
439 total_sent = -1;
440 break;
441 }
442 else {
443 /*
444 ** No data sent
445 ** Since non-blocking sockets are used, wait for
446 ** socket to be writable or default timeout prior
447 ** to retrying.
448 */
449
450 struct timeval tv;
451 fd_set wfd;
452
453 tv.tv_sec = timeout;
454 tv.tv_usec = 0;
455 FD_ZERO( &wfd );
456 FD_SET( ctxt->fd, &wfd );
457 (void)select( ctxt->fd + 1, NULL, &wfd, NULL, &tv );
458 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000459 }
Owen Taylor3473f882001-02-23 17:55:21 +0000460 }
Daniel Veillardf012a642001-07-23 19:10:52 +0000461
462 return total_sent;
Owen Taylor3473f882001-02-23 17:55:21 +0000463}
464
465/**
466 * xmlNanoHTTPRecv:
467 * @ctxt: an HTTP context
468 *
469 * Read information coming from the HTTP connection.
470 * This is a blocking call (but it blocks in select(), not read()).
471 *
472 * Returns the number of byte read or -1 in case of error.
473 */
474
475static int
476xmlNanoHTTPRecv(xmlNanoHTTPCtxtPtr ctxt) {
477 fd_set rfd;
478 struct timeval tv;
479
480
481 while (ctxt->state & XML_NANO_HTTP_READ) {
482 if (ctxt->in == NULL) {
483 ctxt->in = (char *) xmlMalloc(65000 * sizeof(char));
484 if (ctxt->in == NULL) {
485 ctxt->last = -1;
Daniel Veillardf012a642001-07-23 19:10:52 +0000486 xmlGenericError( xmlGenericErrorContext,
487 "xmlNanoHTTPRecv: Error allocating input memory." );
Owen Taylor3473f882001-02-23 17:55:21 +0000488 return(-1);
489 }
490 ctxt->inlen = 65000;
491 ctxt->inptr = ctxt->content = ctxt->inrptr = ctxt->in;
492 }
493 if (ctxt->inrptr > ctxt->in + XML_NANO_HTTP_CHUNK) {
494 int delta = ctxt->inrptr - ctxt->in;
495 int len = ctxt->inptr - ctxt->inrptr;
496
497 memmove(ctxt->in, ctxt->inrptr, len);
498 ctxt->inrptr -= delta;
499 ctxt->content -= delta;
500 ctxt->inptr -= delta;
501 }
502 if ((ctxt->in + ctxt->inlen) < (ctxt->inptr + XML_NANO_HTTP_CHUNK)) {
503 int d_inptr = ctxt->inptr - ctxt->in;
504 int d_content = ctxt->content - ctxt->in;
505 int d_inrptr = ctxt->inrptr - ctxt->in;
Daniel Veillardf012a642001-07-23 19:10:52 +0000506 char * tmp_ptr = ctxt->in;
Owen Taylor3473f882001-02-23 17:55:21 +0000507
508 ctxt->inlen *= 2;
Daniel Veillardf012a642001-07-23 19:10:52 +0000509 ctxt->in = (char *) xmlRealloc(tmp_ptr, ctxt->inlen);
Owen Taylor3473f882001-02-23 17:55:21 +0000510 if (ctxt->in == NULL) {
Daniel Veillardf012a642001-07-23 19:10:52 +0000511 xmlGenericError( xmlGenericErrorContext,
512 "xmlNanoHTTPRecv: %s %d bytes.",
513 "Failed to realloc input buffer to",
514 ctxt->inlen );
515 xmlFree( tmp_ptr );
Owen Taylor3473f882001-02-23 17:55:21 +0000516 ctxt->last = -1;
517 return(-1);
518 }
519 ctxt->inptr = ctxt->in + d_inptr;
520 ctxt->content = ctxt->in + d_content;
521 ctxt->inrptr = ctxt->in + d_inrptr;
522 }
523 ctxt->last = recv(ctxt->fd, ctxt->inptr, XML_NANO_HTTP_CHUNK, 0);
524 if (ctxt->last > 0) {
525 ctxt->inptr += ctxt->last;
526 return(ctxt->last);
527 }
528 if (ctxt->last == 0) {
529 return(0);
530 }
531 if (ctxt->last == -1) {
532 switch (socket_errno()) {
533 case EINPROGRESS:
534 case EWOULDBLOCK:
535#if defined(EAGAIN) && EAGAIN != EWOULDBLOCK
536 case EAGAIN:
537#endif
538 break;
Daniel Veillardf012a642001-07-23 19:10:52 +0000539
540 case ECONNRESET:
541 case ESHUTDOWN:
542 return ( 0 );
543
Owen Taylor3473f882001-02-23 17:55:21 +0000544 default:
Daniel Veillardf012a642001-07-23 19:10:52 +0000545 xmlGenericError( xmlGenericErrorContext,
546 "xmlNanoHTTPRecv: recv( ) failure - %s",
547 strerror( socket_errno( ) ) );
548 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +0000549 }
550 }
551
552 tv.tv_sec = timeout;
553 tv.tv_usec = 0;
554 FD_ZERO(&rfd);
555 FD_SET(ctxt->fd, &rfd);
556
Daniel Veillard50f34372001-08-03 12:06:36 +0000557 if ( (select(ctxt->fd+1, &rfd, NULL, NULL, &tv)<1)
558#if defined(EINTR)
559 && (errno != EINTR)
560#endif
561 )
Owen Taylor3473f882001-02-23 17:55:21 +0000562 return(0);
563 }
564 return(0);
565}
566
567/**
568 * xmlNanoHTTPReadLine:
569 * @ctxt: an HTTP context
570 *
571 * Read one line in the HTTP server output, usually for extracting
572 * the HTTP protocol informations from the answer header.
573 *
574 * Returns a newly allocated string with a copy of the line, or NULL
575 * which indicate the end of the input.
576 */
577
578static char *
579xmlNanoHTTPReadLine(xmlNanoHTTPCtxtPtr ctxt) {
580 char buf[4096];
581 char *bp = buf;
Daniel Veillardf012a642001-07-23 19:10:52 +0000582 int rc;
Owen Taylor3473f882001-02-23 17:55:21 +0000583
584 while (bp - buf < 4095) {
585 if (ctxt->inrptr == ctxt->inptr) {
Daniel Veillardf012a642001-07-23 19:10:52 +0000586 if ( (rc = xmlNanoHTTPRecv(ctxt)) == 0) {
Owen Taylor3473f882001-02-23 17:55:21 +0000587 if (bp == buf)
588 return(NULL);
589 else
590 *bp = 0;
591 return(xmlMemStrdup(buf));
592 }
Daniel Veillardf012a642001-07-23 19:10:52 +0000593 else if ( rc == -1 ) {
594 return ( NULL );
595 }
Owen Taylor3473f882001-02-23 17:55:21 +0000596 }
597 *bp = *ctxt->inrptr++;
598 if (*bp == '\n') {
599 *bp = 0;
600 return(xmlMemStrdup(buf));
601 }
602 if (*bp != '\r')
603 bp++;
604 }
605 buf[4095] = 0;
606 return(xmlMemStrdup(buf));
607}
608
609
610/**
611 * xmlNanoHTTPScanAnswer:
612 * @ctxt: an HTTP context
613 * @line: an HTTP header line
614 *
615 * Try to extract useful informations from the server answer.
616 * We currently parse and process:
617 * - The HTTP revision/ return code
618 * - The Content-Type
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000619 * - The Location for redirect processing.
Owen Taylor3473f882001-02-23 17:55:21 +0000620 *
621 * Returns -1 in case of failure, the file descriptor number otherwise
622 */
623
624static void
625xmlNanoHTTPScanAnswer(xmlNanoHTTPCtxtPtr ctxt, const char *line) {
626 const char *cur = line;
627
628 if (line == NULL) return;
629
630 if (!strncmp(line, "HTTP/", 5)) {
631 int version = 0;
632 int ret = 0;
633
634 cur += 5;
635 while ((*cur >= '0') && (*cur <= '9')) {
636 version *= 10;
637 version += *cur - '0';
638 cur++;
639 }
640 if (*cur == '.') {
641 cur++;
642 if ((*cur >= '0') && (*cur <= '9')) {
643 version *= 10;
644 version += *cur - '0';
645 cur++;
646 }
647 while ((*cur >= '0') && (*cur <= '9'))
648 cur++;
649 } else
650 version *= 10;
651 if ((*cur != ' ') && (*cur != '\t')) return;
652 while ((*cur == ' ') || (*cur == '\t')) cur++;
653 if ((*cur < '0') || (*cur > '9')) return;
654 while ((*cur >= '0') && (*cur <= '9')) {
655 ret *= 10;
656 ret += *cur - '0';
657 cur++;
658 }
659 if ((*cur != 0) && (*cur != ' ') && (*cur != '\t')) return;
660 ctxt->returnValue = ret;
661 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Content-Type:", 13)) {
662 cur += 13;
663 while ((*cur == ' ') || (*cur == '\t')) cur++;
664 if (ctxt->contentType != NULL)
665 xmlFree(ctxt->contentType);
666 ctxt->contentType = xmlMemStrdup(cur);
667 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"ContentType:", 12)) {
668 cur += 12;
669 if (ctxt->contentType != NULL) return;
670 while ((*cur == ' ') || (*cur == '\t')) cur++;
671 ctxt->contentType = xmlMemStrdup(cur);
672 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Location:", 9)) {
673 cur += 9;
674 while ((*cur == ' ') || (*cur == '\t')) cur++;
675 if (ctxt->location != NULL)
676 xmlFree(ctxt->location);
677 ctxt->location = xmlMemStrdup(cur);
678 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"WWW-Authenticate:", 17)) {
679 cur += 17;
680 while ((*cur == ' ') || (*cur == '\t')) cur++;
681 if (ctxt->authHeader != NULL)
682 xmlFree(ctxt->authHeader);
683 ctxt->authHeader = xmlMemStrdup(cur);
684 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Proxy-Authenticate:", 19)) {
685 cur += 19;
686 while ((*cur == ' ') || (*cur == '\t')) cur++;
687 if (ctxt->authHeader != NULL)
688 xmlFree(ctxt->authHeader);
689 ctxt->authHeader = xmlMemStrdup(cur);
Daniel Veillardf012a642001-07-23 19:10:52 +0000690 } else if ( !xmlStrncasecmp( BAD_CAST line, BAD_CAST"Content-Length:", 15) ) {
691 cur += 15;
692 ctxt->ContentLength = strtol( cur, NULL, 10 );
Owen Taylor3473f882001-02-23 17:55:21 +0000693 }
694}
695
696/**
697 * xmlNanoHTTPConnectAttempt:
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000698 * @addr: a socket address structure
Owen Taylor3473f882001-02-23 17:55:21 +0000699 *
700 * Attempt a connection to the given IP:port endpoint. It forces
701 * non-blocking semantic on the socket, and allow 60 seconds for
702 * the host to answer.
703 *
704 * Returns -1 in case of failure, the file descriptor number otherwise
705 */
706
707static int
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000708xmlNanoHTTPConnectAttempt(struct sockaddr *addr)
Owen Taylor3473f882001-02-23 17:55:21 +0000709{
710 SOCKET s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
711 fd_set wfd;
712 struct timeval tv;
713 int status;
714
715 if (s==-1) {
716#ifdef DEBUG_HTTP
717 perror("socket");
718#endif
Daniel Veillardf012a642001-07-23 19:10:52 +0000719 xmlGenericError( xmlGenericErrorContext,
720 "xmlNanoHTTPConnectAttempt: %s - %s",
721 "socket creation failure",
722 strerror( socket_errno( ) ) );
Owen Taylor3473f882001-02-23 17:55:21 +0000723 return(-1);
724 }
725
726#ifdef _WINSOCKAPI_
727 {
728 u_long one = 1;
729
730 status = ioctlsocket(s, FIONBIO, &one) == SOCKET_ERROR ? -1 : 0;
731 }
732#else /* _WINSOCKAPI_ */
733#if defined(VMS)
734 {
735 int enable = 1;
736 status = ioctl(s, FIONBIO, &enable);
737 }
738#else /* VMS */
739 if ((status = fcntl(s, F_GETFL, 0)) != -1) {
740#ifdef O_NONBLOCK
741 status |= O_NONBLOCK;
742#else /* O_NONBLOCK */
743#ifdef F_NDELAY
744 status |= F_NDELAY;
745#endif /* F_NDELAY */
746#endif /* !O_NONBLOCK */
747 status = fcntl(s, F_SETFL, status);
748 }
749 if (status < 0) {
750#ifdef DEBUG_HTTP
751 perror("nonblocking");
752#endif
Daniel Veillardf012a642001-07-23 19:10:52 +0000753 xmlGenericError( xmlGenericErrorContext,
754 "xmlNanoHTTPConnectAttempt: %s - %s",
755 "error setting non-blocking IO",
756 strerror( socket_errno( ) ) );
Owen Taylor3473f882001-02-23 17:55:21 +0000757 closesocket(s);
758 return(-1);
759 }
760#endif /* !VMS */
761#endif /* !_WINSOCKAPI_ */
762
Owen Taylor3473f882001-02-23 17:55:21 +0000763 if ((connect(s, addr, sizeof(*addr))==-1)) {
764 switch (socket_errno()) {
765 case EINPROGRESS:
766 case EWOULDBLOCK:
767 break;
768 default:
Daniel Veillardf012a642001-07-23 19:10:52 +0000769 xmlGenericError( xmlGenericErrorContext,
770 "xmlNanoHTTPConnectAttempt: %s - %s",
771 "error connecting to HTTP server",
772 strerror( socket_errno( ) ) );
Owen Taylor3473f882001-02-23 17:55:21 +0000773 closesocket(s);
774 return(-1);
775 }
776 }
777
778 tv.tv_sec = timeout;
779 tv.tv_usec = 0;
780
781 FD_ZERO(&wfd);
782 FD_SET(s, &wfd);
783
784 switch(select(s+1, NULL, &wfd, NULL, &tv))
785 {
786 case 0:
787 /* Time out */
Daniel Veillardf012a642001-07-23 19:10:52 +0000788 xmlGenericError( xmlGenericErrorContext,
789 "xmlNanoHTTPConnectAttempt: %s",
790 "Connect attempt timed out." );
Owen Taylor3473f882001-02-23 17:55:21 +0000791 closesocket(s);
792 return(-1);
793 case -1:
794 /* Ermm.. ?? */
Daniel Veillardf012a642001-07-23 19:10:52 +0000795 xmlGenericError( xmlGenericErrorContext,
796 "xmlNanoHTTPConnectAttempt: %s - %s",
797 "Error connecting to host",
798 strerror( socket_errno( ) ) );
Owen Taylor3473f882001-02-23 17:55:21 +0000799 closesocket(s);
800 return(-1);
801 }
802
803 if ( FD_ISSET(s, &wfd) ) {
804 SOCKLEN_T len;
805 len = sizeof(status);
806 if (getsockopt(s, SOL_SOCKET, SO_ERROR, (char*)&status, &len) < 0 ) {
807 /* Solaris error code */
Daniel Veillardf012a642001-07-23 19:10:52 +0000808 xmlGenericError( xmlGenericErrorContext,
809 "xmlNanoHTTPConnectAttempt: %s - %s",
810 "Error retrieving pending socket errors",
811 strerror( socket_errno( ) ) );
Owen Taylor3473f882001-02-23 17:55:21 +0000812 return (-1);
813 }
814 if ( status ) {
815 closesocket(s);
816 errno = status;
Daniel Veillardf012a642001-07-23 19:10:52 +0000817 xmlGenericError( xmlGenericErrorContext,
818 "xmlNanoHTTPConnectAttempt: %s - %s",
819 "Error connecting to remote host",
820 strerror( status ) );
Owen Taylor3473f882001-02-23 17:55:21 +0000821 return (-1);
822 }
823 } else {
824 /* pbm */
Daniel Veillardf012a642001-07-23 19:10:52 +0000825 xmlGenericError( xmlGenericErrorContext,
826 "xmlNanoHTTPConnectAttempt: %s\n",
827 "Select returned, but descriptor not set for connection.\n" );
828 closesocket(s);
Owen Taylor3473f882001-02-23 17:55:21 +0000829 return (-1);
830 }
831
832 return(s);
833}
834
835/**
836 * xmlNanoHTTPConnectHost:
837 * @host: the host name
838 * @port: the port number
839 *
840 * Attempt a connection to the given host:port endpoint. It tries
841 * the multiple IP provided by the DNS if available.
842 *
843 * Returns -1 in case of failure, the file descriptor number otherwise
844 */
845
846static int
847xmlNanoHTTPConnectHost(const char *host, int port)
848{
849 struct hostent *h;
850 struct sockaddr *addr;
851 struct in_addr ia;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000852 struct sockaddr_in sockin;
Daniel Veillard5c396542002-03-15 07:57:50 +0000853
Owen Taylor3473f882001-02-23 17:55:21 +0000854#ifdef SUPPORT_IP6
855 struct in6_addr ia6;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000856 struct sockaddr_in6 sockin6;
Owen Taylor3473f882001-02-23 17:55:21 +0000857#endif
858 int i;
859 int s;
Daniel Veillard5c396542002-03-15 07:57:50 +0000860
Owen Taylor3473f882001-02-23 17:55:21 +0000861#if defined(SUPPORT_IP6) && defined(RES_USE_INET6)
862 if (!(_res.options & RES_INIT))
Daniel Veillard5c396542002-03-15 07:57:50 +0000863 res_init();
Owen Taylor3473f882001-02-23 17:55:21 +0000864 _res.options |= RES_USE_INET6;
865#endif
Daniel Veillard5c396542002-03-15 07:57:50 +0000866 h = gethostbyname(host);
867 if (h == NULL) {
868#if defined(HAVE_NETDB_H) && defined(HOST_NOT_FOUND)
869 const char *h_err_txt = "";
Daniel Veillardf012a642001-07-23 19:10:52 +0000870
Daniel Veillard5c396542002-03-15 07:57:50 +0000871 switch (h_errno) {
872 case HOST_NOT_FOUND:
873 h_err_txt = "Authoritive host not found";
874 break;
Daniel Veillardf012a642001-07-23 19:10:52 +0000875
Daniel Veillard5c396542002-03-15 07:57:50 +0000876 case TRY_AGAIN:
877 h_err_txt =
878 "Non-authoritive host not found or server failure.";
879 break;
Daniel Veillardf012a642001-07-23 19:10:52 +0000880
Daniel Veillard5c396542002-03-15 07:57:50 +0000881 case NO_RECOVERY:
882 h_err_txt =
883 "Non-recoverable errors: FORMERR, REFUSED, or NOTIMP.";
884 break;
885
886 case NO_ADDRESS:
887 h_err_txt =
888 "Valid name, no data record of requested type.";
889 break;
890
891 default:
892 h_err_txt = "No error text defined.";
893 break;
894 }
895 xmlGenericError(xmlGenericErrorContext,
896 "xmlNanoHTTPConnectHost: %s '%s' - %s",
897 "Failed to resolve host", host, h_err_txt);
898#else
899 xmlGenericError(xmlGenericErrorContext,
900 "xmlNanoHTTPConnectHost: %s '%s'",
901 "Failed to resolve host", host);
Owen Taylor3473f882001-02-23 17:55:21 +0000902#endif
Daniel Veillard5c396542002-03-15 07:57:50 +0000903 return (-1);
904 }
905
906 for (i = 0; h->h_addr_list[i]; i++) {
907 if (h->h_addrtype == AF_INET) {
908 /* A records (IPv4) */
909 memcpy(&ia, h->h_addr_list[i], h->h_length);
910 sockin.sin_family = h->h_addrtype;
911 sockin.sin_addr = ia;
912 sockin.sin_port = htons(port);
913 addr = (struct sockaddr *) &sockin;
914#ifdef SUPPORT_IP6
915 } else if (h->h_addrtype == AF_INET6) {
916 /* AAAA records (IPv6) */
917 memcpy(&ia6, h->h_addr_list[i], h->h_length);
918 sockin6.sin_family = h->h_addrtype;
919 sockin6.sin_addr = ia6;
920 sockin6.sin_port = htons(port);
921 addr = (struct sockaddr *) &sockin6;
922#endif
923 } else
924 break; /* for */
925
926 s = xmlNanoHTTPConnectAttempt(addr);
927 if (s != -1)
928 return (s);
Owen Taylor3473f882001-02-23 17:55:21 +0000929 }
930
931#ifdef DEBUG_HTTP
932 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard5c396542002-03-15 07:57:50 +0000933 "xmlNanoHTTPConnectHost: unable to connect to '%s'.\n",
934 host);
Owen Taylor3473f882001-02-23 17:55:21 +0000935#endif
Daniel Veillard5c396542002-03-15 07:57:50 +0000936 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +0000937}
938
939
940/**
941 * xmlNanoHTTPOpen:
942 * @URL: The URL to load
943 * @contentType: if available the Content-Type information will be
944 * returned at that location
945 *
946 * This function try to open a connection to the indicated resource
947 * via HTTP GET.
948 *
949 * Returns NULL in case of failure, otherwise a request handler.
950 * The contentType, if provided must be freed by the caller
951 */
952
953void*
954xmlNanoHTTPOpen(const char *URL, char **contentType) {
955 if (contentType != NULL) *contentType = NULL;
Daniel Veillardf012a642001-07-23 19:10:52 +0000956 return(xmlNanoHTTPMethod(URL, NULL, NULL, contentType, NULL, 0));
Daniel Veillard9403a042001-05-28 11:00:53 +0000957}
958
959/**
960 * xmlNanoHTTPOpenRedir:
961 * @URL: The URL to load
962 * @contentType: if available the Content-Type information will be
963 * returned at that location
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000964 * @redir: if available the redirected URL will be returned
Daniel Veillard9403a042001-05-28 11:00:53 +0000965 *
966 * This function try to open a connection to the indicated resource
967 * via HTTP GET.
968 *
969 * Returns NULL in case of failure, otherwise a request handler.
970 * The contentType, if provided must be freed by the caller
971 */
972
973void*
974xmlNanoHTTPOpenRedir(const char *URL, char **contentType, char **redir) {
975 if (contentType != NULL) *contentType = NULL;
976 if (redir != NULL) *redir = NULL;
Daniel Veillardf012a642001-07-23 19:10:52 +0000977 return(xmlNanoHTTPMethodRedir(URL, NULL, NULL, contentType, redir, NULL,0));
Owen Taylor3473f882001-02-23 17:55:21 +0000978}
979
980/**
981 * xmlNanoHTTPRead:
982 * @ctx: the HTTP context
983 * @dest: a buffer
984 * @len: the buffer length
985 *
986 * This function tries to read @len bytes from the existing HTTP connection
987 * and saves them in @dest. This is a blocking call.
988 *
989 * Returns the number of byte read. 0 is an indication of an end of connection.
990 * -1 indicates a parameter error.
991 */
992int
993xmlNanoHTTPRead(void *ctx, void *dest, int len) {
994 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
995
996 if (ctx == NULL) return(-1);
997 if (dest == NULL) return(-1);
998 if (len <= 0) return(0);
999
1000 while (ctxt->inptr - ctxt->inrptr < len) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001001 if (xmlNanoHTTPRecv(ctxt) <= 0) break;
Owen Taylor3473f882001-02-23 17:55:21 +00001002 }
1003 if (ctxt->inptr - ctxt->inrptr < len)
1004 len = ctxt->inptr - ctxt->inrptr;
1005 memcpy(dest, ctxt->inrptr, len);
1006 ctxt->inrptr += len;
1007 return(len);
1008}
1009
1010/**
1011 * xmlNanoHTTPClose:
1012 * @ctx: the HTTP context
1013 *
1014 * This function closes an HTTP context, it ends up the connection and
1015 * free all data related to it.
1016 */
1017void
1018xmlNanoHTTPClose(void *ctx) {
1019 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1020
1021 if (ctx == NULL) return;
1022
1023 xmlNanoHTTPFreeCtxt(ctxt);
1024}
1025
1026/**
Daniel Veillard9403a042001-05-28 11:00:53 +00001027 * xmlNanoHTTPMethodRedir:
Owen Taylor3473f882001-02-23 17:55:21 +00001028 * @URL: The URL to load
1029 * @method: the HTTP method to use
1030 * @input: the input string if any
1031 * @contentType: the Content-Type information IN and OUT
Daniel Veillard9403a042001-05-28 11:00:53 +00001032 * @redir: the redirected URL OUT
Owen Taylor3473f882001-02-23 17:55:21 +00001033 * @headers: the extra headers
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001034 * @ilen: input length
Owen Taylor3473f882001-02-23 17:55:21 +00001035 *
1036 * This function try to open a connection to the indicated resource
1037 * via HTTP using the given @method, adding the given extra headers
1038 * and the input buffer for the request content.
1039 *
1040 * Returns NULL in case of failure, otherwise a request handler.
Daniel Veillard9403a042001-05-28 11:00:53 +00001041 * The contentType, or redir, if provided must be freed by the caller
Owen Taylor3473f882001-02-23 17:55:21 +00001042 */
1043
1044void*
Daniel Veillard9403a042001-05-28 11:00:53 +00001045xmlNanoHTTPMethodRedir(const char *URL, const char *method, const char *input,
Daniel Veillardf012a642001-07-23 19:10:52 +00001046 char **contentType, char **redir,
1047 const char *headers, int ilen ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001048 xmlNanoHTTPCtxtPtr ctxt;
1049 char *bp, *p;
Daniel Veillardf012a642001-07-23 19:10:52 +00001050 int blen, ret;
Owen Taylor3473f882001-02-23 17:55:21 +00001051 int head;
Daniel Veillardf012a642001-07-23 19:10:52 +00001052 int xmt_bytes;
Owen Taylor3473f882001-02-23 17:55:21 +00001053 int nbRedirects = 0;
1054 char *redirURL = NULL;
1055
1056 if (URL == NULL) return(NULL);
1057 if (method == NULL) method = "GET";
1058 xmlNanoHTTPInit();
1059
1060retry:
1061 if (redirURL == NULL)
1062 ctxt = xmlNanoHTTPNewCtxt(URL);
1063 else {
1064 ctxt = xmlNanoHTTPNewCtxt(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001065 }
1066
Daniel Veillardf012a642001-07-23 19:10:52 +00001067 if ( ctxt == NULL ) {
1068 xmlGenericError( xmlGenericErrorContext,
1069 "xmlNanoHTTPMethodRedir: %s %s.",
1070 "Unable to allocate HTTP context to URI",
1071 ( ( redirURL == NULL ) ? URL : redirURL ) );
1072 return ( NULL );
1073 }
1074
Owen Taylor3473f882001-02-23 17:55:21 +00001075 if ((ctxt->protocol == NULL) || (strcmp(ctxt->protocol, "http"))) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001076 xmlGenericError( xmlGenericErrorContext,
1077 "xmlNanoHTTPMethodRedir: %s - %s.",
1078 "Not a valid HTTP URI",
1079 ( ( redirURL == NULL ) ? URL : redirURL ) );
Owen Taylor3473f882001-02-23 17:55:21 +00001080 xmlNanoHTTPFreeCtxt(ctxt);
1081 if (redirURL != NULL) xmlFree(redirURL);
1082 return(NULL);
1083 }
1084 if (ctxt->hostname == NULL) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001085 xmlGenericError( xmlGenericErrorContext,
1086 "xmlNanoHTTPMethodRedir: %s - %s",
1087 "Failed to identify host in URI",
1088 ( ( redirURL == NULL ) ? URL : redirURL ) );
Owen Taylor3473f882001-02-23 17:55:21 +00001089 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001090 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001091 return(NULL);
1092 }
1093 if (proxy) {
1094 blen = strlen(ctxt->hostname) * 2 + 16;
1095 ret = xmlNanoHTTPConnectHost(proxy, proxyPort);
1096 }
1097 else {
1098 blen = strlen(ctxt->hostname);
1099 ret = xmlNanoHTTPConnectHost(ctxt->hostname, ctxt->port);
1100 }
1101 if (ret < 0) {
1102 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001103 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001104 return(NULL);
1105 }
1106 ctxt->fd = ret;
1107
Daniel Veillardf012a642001-07-23 19:10:52 +00001108 if (input == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00001109 ilen = 0;
Daniel Veillardf012a642001-07-23 19:10:52 +00001110 else
1111 blen += 36;
1112
Owen Taylor3473f882001-02-23 17:55:21 +00001113 if (headers != NULL)
Daniel Veillardf012a642001-07-23 19:10:52 +00001114 blen += strlen(headers) + 2;
Owen Taylor3473f882001-02-23 17:55:21 +00001115 if (contentType && *contentType)
1116 blen += strlen(*contentType) + 16;
Daniel Veillardf012a642001-07-23 19:10:52 +00001117 blen += strlen(method) + strlen(ctxt->path) + 24;
Owen Taylor3473f882001-02-23 17:55:21 +00001118 bp = xmlMalloc(blen);
Daniel Veillardf012a642001-07-23 19:10:52 +00001119 if ( bp == NULL ) {
1120 xmlNanoHTTPFreeCtxt( ctxt );
1121 xmlGenericError( xmlGenericErrorContext,
1122 "xmlNanoHTTPMethodRedir: %s",
1123 "Error allocating HTTP header buffer." );
1124 return ( NULL );
1125 }
1126
1127 p = bp;
1128
Owen Taylor3473f882001-02-23 17:55:21 +00001129 if (proxy) {
1130 if (ctxt->port != 80) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001131 p += sprintf( p, "%s http://%s:%d%s", method, ctxt->hostname,
1132 ctxt->port, ctxt->path );
Owen Taylor3473f882001-02-23 17:55:21 +00001133 }
1134 else
Daniel Veillardf012a642001-07-23 19:10:52 +00001135 p += sprintf( p, "%s http://%s%s", method,
1136 ctxt->hostname, ctxt->path);
Owen Taylor3473f882001-02-23 17:55:21 +00001137 }
1138 else
Daniel Veillardf012a642001-07-23 19:10:52 +00001139 p += sprintf( p, "%s %s", method, ctxt->path);
1140
1141 p += sprintf(p, " HTTP/1.0\r\nHost: %s\r\n", ctxt->hostname);
1142
1143 if (contentType != NULL && *contentType)
1144 p += sprintf(p, "Content-Type: %s\r\n", *contentType);
1145
1146 if (headers != NULL)
1147 p += sprintf( p, "%s", headers );
1148
Owen Taylor3473f882001-02-23 17:55:21 +00001149 if (input != NULL)
Daniel Veillardf012a642001-07-23 19:10:52 +00001150 sprintf(p, "Content-Length: %d\r\n\r\n", ilen );
Owen Taylor3473f882001-02-23 17:55:21 +00001151 else
1152 strcpy(p, "\r\n");
Daniel Veillardf012a642001-07-23 19:10:52 +00001153
Owen Taylor3473f882001-02-23 17:55:21 +00001154#ifdef DEBUG_HTTP
1155 xmlGenericError(xmlGenericErrorContext,
1156 "-> %s%s", proxy? "(Proxy) " : "", bp);
1157 if ((blen -= strlen(bp)+1) < 0)
1158 xmlGenericError(xmlGenericErrorContext,
1159 "ERROR: overflowed buffer by %d bytes\n", -blen);
1160#endif
1161 ctxt->outptr = ctxt->out = bp;
1162 ctxt->state = XML_NANO_HTTP_WRITE;
Daniel Veillardf012a642001-07-23 19:10:52 +00001163 blen = strlen( ctxt->out );
1164 xmt_bytes = xmlNanoHTTPSend(ctxt, ctxt->out, blen );
1165#ifdef DEBUG_HTTP
1166 if ( xmt_bytes != blen )
1167 xmlGenericError( xmlGenericErrorContext,
1168 "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
1169 xmt_bytes, blen,
1170 "bytes of HTTP headers sent to host",
1171 ctxt->hostname );
1172#endif
1173
1174 if ( input != NULL ) {
1175 xmt_bytes = xmlNanoHTTPSend( ctxt, input, ilen );
1176
1177#ifdef DEBUG_HTTP
1178 if ( xmt_bytes != ilen )
1179 xmlGenericError( xmlGenericErrorContext,
1180 "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
1181 xmt_bytes, ilen,
1182 "bytes of HTTP content sent to host",
1183 ctxt->hostname );
1184#endif
1185 }
1186
Owen Taylor3473f882001-02-23 17:55:21 +00001187 ctxt->state = XML_NANO_HTTP_READ;
1188 head = 1;
1189
1190 while ((p = xmlNanoHTTPReadLine(ctxt)) != NULL) {
1191 if (head && (*p == 0)) {
1192 head = 0;
1193 ctxt->content = ctxt->inrptr;
1194 xmlFree(p);
1195 break;
1196 }
1197 xmlNanoHTTPScanAnswer(ctxt, p);
1198
1199#ifdef DEBUG_HTTP
1200 xmlGenericError(xmlGenericErrorContext, "<- %s\n", p);
1201#endif
1202 xmlFree(p);
1203 }
1204
1205 if ((ctxt->location != NULL) && (ctxt->returnValue >= 300) &&
1206 (ctxt->returnValue < 400)) {
1207#ifdef DEBUG_HTTP
1208 xmlGenericError(xmlGenericErrorContext,
1209 "\nRedirect to: %s\n", ctxt->location);
1210#endif
Daniel Veillardf012a642001-07-23 19:10:52 +00001211 while ( xmlNanoHTTPRecv(ctxt) > 0 ) ;
Owen Taylor3473f882001-02-23 17:55:21 +00001212 if (nbRedirects < XML_NANO_HTTP_MAX_REDIR) {
1213 nbRedirects++;
Daniel Veillard9403a042001-05-28 11:00:53 +00001214 if (redirURL != NULL)
1215 xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001216 redirURL = xmlMemStrdup(ctxt->location);
1217 xmlNanoHTTPFreeCtxt(ctxt);
1218 goto retry;
1219 }
1220 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001221 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001222#ifdef DEBUG_HTTP
1223 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardf012a642001-07-23 19:10:52 +00001224 "xmlNanoHTTPMethodRedir: Too many redirects, aborting ...\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001225#endif
1226 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001227 }
1228
1229 if (contentType != NULL) {
1230 if (ctxt->contentType != NULL)
1231 *contentType = xmlMemStrdup(ctxt->contentType);
1232 else
1233 *contentType = NULL;
1234 }
1235
Daniel Veillard9403a042001-05-28 11:00:53 +00001236 if ((redir != NULL) && (redirURL != NULL)) {
1237 *redir = redirURL;
1238 } else {
1239 if (redirURL != NULL)
1240 xmlFree(redirURL);
1241 if (redir != NULL)
1242 *redir = NULL;
1243 }
1244
Owen Taylor3473f882001-02-23 17:55:21 +00001245#ifdef DEBUG_HTTP
1246 if (ctxt->contentType != NULL)
1247 xmlGenericError(xmlGenericErrorContext,
1248 "\nCode %d, content-type '%s'\n\n",
1249 ctxt->returnValue, ctxt->contentType);
1250 else
1251 xmlGenericError(xmlGenericErrorContext,
1252 "\nCode %d, no content-type\n\n",
1253 ctxt->returnValue);
1254#endif
1255
1256 return((void *) ctxt);
1257}
1258
1259/**
Daniel Veillard9403a042001-05-28 11:00:53 +00001260 * xmlNanoHTTPMethod:
1261 * @URL: The URL to load
1262 * @method: the HTTP method to use
1263 * @input: the input string if any
1264 * @contentType: the Content-Type information IN and OUT
1265 * @headers: the extra headers
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001266 * @ilen: input length
Daniel Veillard9403a042001-05-28 11:00:53 +00001267 *
1268 * This function try to open a connection to the indicated resource
1269 * via HTTP using the given @method, adding the given extra headers
1270 * and the input buffer for the request content.
1271 *
1272 * Returns NULL in case of failure, otherwise a request handler.
1273 * The contentType, if provided must be freed by the caller
1274 */
1275
1276void*
1277xmlNanoHTTPMethod(const char *URL, const char *method, const char *input,
Daniel Veillardf012a642001-07-23 19:10:52 +00001278 char **contentType, const char *headers, int ilen) {
Daniel Veillard9403a042001-05-28 11:00:53 +00001279 return(xmlNanoHTTPMethodRedir(URL, method, input, contentType,
Daniel Veillardf012a642001-07-23 19:10:52 +00001280 NULL, headers, ilen));
Daniel Veillard9403a042001-05-28 11:00:53 +00001281}
1282
1283/**
Owen Taylor3473f882001-02-23 17:55:21 +00001284 * xmlNanoHTTPFetch:
1285 * @URL: The URL to load
1286 * @filename: the filename where the content should be saved
1287 * @contentType: if available the Content-Type information will be
1288 * returned at that location
1289 *
1290 * This function try to fetch the indicated resource via HTTP GET
1291 * and save it's content in the file.
1292 *
1293 * Returns -1 in case of failure, 0 incase of success. The contentType,
1294 * if provided must be freed by the caller
1295 */
1296int
1297xmlNanoHTTPFetch(const char *URL, const char *filename, char **contentType) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001298 void *ctxt = NULL;
1299 char *buf = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001300 int fd;
1301 int len;
1302
1303 ctxt = xmlNanoHTTPOpen(URL, contentType);
1304 if (ctxt == NULL) return(-1);
1305
1306 if (!strcmp(filename, "-"))
1307 fd = 0;
1308 else {
1309 fd = open(filename, O_CREAT | O_WRONLY, 00644);
1310 if (fd < 0) {
1311 xmlNanoHTTPClose(ctxt);
1312 if ((contentType != NULL) && (*contentType != NULL)) {
1313 xmlFree(*contentType);
1314 *contentType = NULL;
1315 }
1316 return(-1);
1317 }
1318 }
1319
Daniel Veillardf012a642001-07-23 19:10:52 +00001320 xmlNanoHTTPFetchContent( ctxt, &buf, &len );
1321 if ( len > 0 ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001322 write(fd, buf, len);
1323 }
1324
1325 xmlNanoHTTPClose(ctxt);
1326 close(fd);
1327 return(0);
1328}
1329
1330/**
1331 * xmlNanoHTTPSave:
1332 * @ctxt: the HTTP context
1333 * @filename: the filename where the content should be saved
1334 *
1335 * This function saves the output of the HTTP transaction to a file
1336 * It closes and free the context at the end
1337 *
1338 * Returns -1 in case of failure, 0 incase of success.
1339 */
1340int
1341xmlNanoHTTPSave(void *ctxt, const char *filename) {
Daniel Veillarde3924972001-07-25 20:25:21 +00001342 char *buf = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001343 int fd;
1344 int len;
1345
1346 if (ctxt == NULL) return(-1);
1347
1348 if (!strcmp(filename, "-"))
1349 fd = 0;
1350 else {
1351 fd = open(filename, O_CREAT | O_WRONLY);
1352 if (fd < 0) {
1353 xmlNanoHTTPClose(ctxt);
1354 return(-1);
1355 }
1356 }
1357
Daniel Veillardf012a642001-07-23 19:10:52 +00001358 xmlNanoHTTPFetchContent( ctxt, &buf, &len );
1359 if ( len > 0 ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001360 write(fd, buf, len);
1361 }
1362
1363 xmlNanoHTTPClose(ctxt);
1364 return(0);
1365}
1366
1367/**
1368 * xmlNanoHTTPReturnCode:
1369 * @ctx: the HTTP context
1370 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001371 * Get the latest HTTP return code received
1372 *
Owen Taylor3473f882001-02-23 17:55:21 +00001373 * Returns the HTTP return code for the request.
1374 */
1375int
1376xmlNanoHTTPReturnCode(void *ctx) {
1377 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1378
1379 if (ctxt == NULL) return(-1);
1380
1381 return(ctxt->returnValue);
1382}
1383
1384/**
1385 * xmlNanoHTTPAuthHeader:
1386 * @ctx: the HTTP context
1387 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001388 * Get the authentication header of an HTTP context
1389 *
Owen Taylor3473f882001-02-23 17:55:21 +00001390 * Returns the stashed value of the WWW-Authenticate or Proxy-Authenticate
1391 * header.
1392 */
1393const char *
1394xmlNanoHTTPAuthHeader(void *ctx) {
1395 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1396
1397 if (ctxt == NULL) return(NULL);
1398
1399 return(ctxt->authHeader);
1400}
1401
Daniel Veillardf012a642001-07-23 19:10:52 +00001402/**
1403 * xmlNanoHTTPContentLength
1404 * @ctx: the HTTP context
1405 *
1406 * Return the specified content length from the HTTP header. Note that
1407 * a value of -1 indicates that the content length element was not included in
1408 * the response header.
1409 */
1410int
1411xmlNanoHTTPContentLength( void * ctx ) {
1412 xmlNanoHTTPCtxtPtr ctxt = ctx;
1413
1414 return ( ( ctxt == NULL ) ? -1 : ctxt->ContentLength );
1415}
1416
1417/**
1418 * xmlNanoHTTPFetchContent
1419 * @ctx: the HTTP context
1420 * @ptr: pointer to set to the content buffer.
1421 * @len: integer pointer to hold the length of the content
1422 *
1423 * Returns 0 if all the content was read and available, returns
1424 * -1 if received content length was less than specified or an error
1425 * occurred.
1426 */
1427int
1428xmlNanoHTTPFetchContent( void * ctx, char ** ptr, int * len ) {
1429 xmlNanoHTTPCtxtPtr ctxt = ctx;
1430
1431 int rc = 0;
1432 int cur_lgth;
1433 int rcvd_lgth;
1434 int dummy_int;
1435 char * dummy_ptr = NULL;
1436
1437 /* Dummy up return input parameters if not provided */
1438
1439 if ( len == NULL )
1440 len = &dummy_int;
1441
1442 if ( ptr == NULL )
1443 ptr = &dummy_ptr;
1444
1445 /* But can't work without the context pointer */
1446
1447 if ( ( ctxt == NULL ) || ( ctxt->content == NULL ) ) {
1448 *len = 0;
1449 *ptr = NULL;
1450 return ( -1 );
1451 }
1452
1453 rcvd_lgth = ctxt->inptr - ctxt->content;
1454
1455 while ( (cur_lgth = xmlNanoHTTPRecv( ctxt )) > 0 ) {
1456
1457 rcvd_lgth += cur_lgth;
1458 if ( (ctxt->ContentLength > 0) && (rcvd_lgth >= ctxt->ContentLength) )
1459 break;
1460 }
1461
1462 *ptr = ctxt->content;
1463 *len = rcvd_lgth;
1464
1465 if ( ( ctxt->ContentLength > 0 ) && ( rcvd_lgth < ctxt->ContentLength ) )
1466 rc = -1;
1467 else if ( rcvd_lgth == 0 )
1468 rc = -1;
1469
1470 return ( rc );
1471}
1472
Owen Taylor3473f882001-02-23 17:55:21 +00001473#ifdef STANDALONE
1474int main(int argc, char **argv) {
1475 char *contentType = NULL;
1476
1477 if (argv[1] != NULL) {
1478 if (argv[2] != NULL)
1479 xmlNanoHTTPFetch(argv[1], argv[2], &contentType);
1480 else
1481 xmlNanoHTTPFetch(argv[1], "-", &contentType);
1482 if (contentType != NULL) xmlFree(contentType);
1483 } else {
1484 xmlGenericError(xmlGenericErrorContext,
1485 "%s: minimal HTTP GET implementation\n", argv[0]);
1486 xmlGenericError(xmlGenericErrorContext,
1487 "\tusage %s [ URL [ filename ] ]\n", argv[0]);
1488 }
1489 xmlNanoHTTPCleanup();
1490 xmlMemoryDump();
1491 return(0);
1492}
1493#endif /* STANDALONE */
1494#else /* !LIBXML_HTTP_ENABLED */
1495#ifdef STANDALONE
1496#include <stdio.h>
1497int main(int argc, char **argv) {
1498 xmlGenericError(xmlGenericErrorContext,
1499 "%s : HTTP support not compiled in\n", argv[0]);
1500 return(0);
1501}
1502#endif /* STANDALONE */
1503#endif /* LIBXML_HTTP_ENABLED */