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