blob: 2879c141079856d8baad3e7ca21290681e5d9521 [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
Daniel Veillard9b731d72002-04-14 12:56:08 +000043#ifdef HAVE_ARPA_NAMESER_H
44#include <arpa/nameser.h>
45#endif
Daniel Veillardd85f4f42002-03-25 10:48:46 +000046#include <resolv.h>
47#endif
Owen Taylor3473f882001-02-23 17:55:21 +000048#ifdef HAVE_FCNTL_H
49#include <fcntl.h>
50#endif
51#ifdef HAVE_ERRNO_H
52#include <errno.h>
53#endif
54#ifdef HAVE_SYS_TIME_H
55#include <sys/time.h>
56#endif
57#ifdef HAVE_SYS_SELECT_H
58#include <sys/select.h>
59#endif
60#ifdef HAVE_STRINGS_H
61#include <strings.h>
62#endif
63#ifdef SUPPORT_IP6
64#include <resolv.h>
65#endif
66
67#ifdef VMS
68#include <stropts>
69#define SOCKLEN_T unsigned int
70#define SOCKET int
71#endif
72
Daniel Veillardd0463562001-10-13 09:15:48 +000073#include <libxml/globals.h>
Daniel Veillardf012a642001-07-23 19:10:52 +000074#include <libxml/xmlerror.h>
Owen Taylor3473f882001-02-23 17:55:21 +000075#include <libxml/xmlmemory.h>
76#include <libxml/parser.h> /* for xmlStr(n)casecmp() */
77#include <libxml/nanohttp.h>
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000078#include <libxml/globals.h>
Daniel Veillard8efff672002-12-04 11:44:48 +000079#include <libxml/uri.h>
Daniel Veillardde2a67b2003-06-21 14:20:04 +000080#include <config.h>
Owen Taylor3473f882001-02-23 17:55:21 +000081
82/**
83 * A couple portability macros
84 */
85#ifndef _WINSOCKAPI_
86#define closesocket(s) close(s)
87#define SOCKET int
88#endif
89
Daniel Veillard75be0132002-03-13 10:03:35 +000090#ifndef SOCKLEN_T
91#define SOCKLEN_T unsigned int
92#endif
93#ifndef SOCKET
94#define SOCKET int
95#endif
Daniel Veillardf012a642001-07-23 19:10:52 +000096
Owen Taylor3473f882001-02-23 17:55:21 +000097#ifdef STANDALONE
98#define DEBUG_HTTP
99#define xmlStrncasecmp(a, b, n) strncasecmp((char *)a, (char *)b, n)
100#define xmlStrcasecmpi(a, b) strcasecmp((char *)a, (char *)b)
101#endif
102
103#define XML_NANO_HTTP_MAX_REDIR 10
104
105#define XML_NANO_HTTP_CHUNK 4096
106
107#define XML_NANO_HTTP_CLOSED 0
108#define XML_NANO_HTTP_WRITE 1
109#define XML_NANO_HTTP_READ 2
110#define XML_NANO_HTTP_NONE 4
111
112typedef struct xmlNanoHTTPCtxt {
113 char *protocol; /* the protocol name */
114 char *hostname; /* the host name */
115 int port; /* the port */
116 char *path; /* the path within the URL */
117 SOCKET fd; /* the file descriptor for the socket */
118 int state; /* WRITE / READ / CLOSED */
119 char *out; /* buffer sent (zero terminated) */
120 char *outptr; /* index within the buffer sent */
121 char *in; /* the receiving buffer */
122 char *content; /* the start of the content */
123 char *inptr; /* the next byte to read from network */
124 char *inrptr; /* the next byte to give back to the client */
125 int inlen; /* len of the input buffer */
126 int last; /* return code for last operation */
127 int returnValue; /* the protocol return value */
Daniel Veillardf012a642001-07-23 19:10:52 +0000128 int ContentLength; /* specified content length from HTTP header */
Owen Taylor3473f882001-02-23 17:55:21 +0000129 char *contentType; /* the MIME type for the input */
130 char *location; /* the new URL in case of redirect */
131 char *authHeader; /* contents of {WWW,Proxy}-Authenticate header */
132} xmlNanoHTTPCtxt, *xmlNanoHTTPCtxtPtr;
133
134static int initialized = 0;
135static char *proxy = NULL; /* the proxy name if any */
136static int proxyPort; /* the proxy port if any */
137static unsigned int timeout = 60;/* the select() timeout in seconds */
138
Daniel Veillardf012a642001-07-23 19:10:52 +0000139int xmlNanoHTTPFetchContent( void * ctx, char ** ptr, int * len );
140int xmlNanoHTTPContentLength( void * ctx );
141
Owen Taylor3473f882001-02-23 17:55:21 +0000142/**
143 * A portability function
144 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000145static int socket_errno(void) {
Owen Taylor3473f882001-02-23 17:55:21 +0000146#ifdef _WINSOCKAPI_
147 return(WSAGetLastError());
148#else
149 return(errno);
150#endif
151}
152
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000153#ifdef SUPPORT_IP6
154static int have_ipv6() {
155 int s;
156
157 s = socket (AF_INET6, SOCK_STREAM, 0);
158 if (s != -1) {
159 close (s);
160 return (1);
161 }
162 return (0);
163}
164#endif
165
Owen Taylor3473f882001-02-23 17:55:21 +0000166/**
167 * xmlNanoHTTPInit:
168 *
169 * Initialize the HTTP protocol layer.
170 * Currently it just checks for proxy informations
171 */
172
173void
174xmlNanoHTTPInit(void) {
175 const char *env;
176#ifdef _WINSOCKAPI_
177 WSADATA wsaData;
178#endif
179
180 if (initialized)
181 return;
182
183#ifdef _WINSOCKAPI_
184 if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0)
185 return;
186#endif
187
188 if (proxy == NULL) {
189 proxyPort = 80;
190 env = getenv("no_proxy");
191 if (env != NULL)
192 goto done;
193 env = getenv("http_proxy");
194 if (env != NULL) {
195 xmlNanoHTTPScanProxy(env);
196 goto done;
197 }
198 env = getenv("HTTP_PROXY");
199 if (env != NULL) {
200 xmlNanoHTTPScanProxy(env);
201 goto done;
202 }
203 }
204done:
205 initialized = 1;
206}
207
208/**
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000209 * xmlNanoHTTPCleanup:
Owen Taylor3473f882001-02-23 17:55:21 +0000210 *
211 * Cleanup the HTTP protocol layer.
212 */
213
214void
215xmlNanoHTTPCleanup(void) {
216 if (proxy != NULL)
217 xmlFree(proxy);
218#ifdef _WINSOCKAPI_
219 if (initialized)
220 WSACleanup();
221#endif
222 initialized = 0;
223 return;
224}
225
226/**
Owen Taylor3473f882001-02-23 17:55:21 +0000227 * xmlNanoHTTPScanURL:
228 * @ctxt: an HTTP context
229 * @URL: The URL used to initialize the context
230 *
231 * (Re)Initialize an HTTP context by parsing the URL and finding
232 * the protocol host port and path it indicates.
233 */
234
235static void
236xmlNanoHTTPScanURL(xmlNanoHTTPCtxtPtr ctxt, const char *URL) {
237 const char *cur = URL;
238 char buf[4096];
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000239 int indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000240 int port = 0;
241
242 if (ctxt->protocol != NULL) {
243 xmlFree(ctxt->protocol);
244 ctxt->protocol = NULL;
245 }
246 if (ctxt->hostname != NULL) {
247 xmlFree(ctxt->hostname);
248 ctxt->hostname = NULL;
249 }
250 if (ctxt->path != NULL) {
251 xmlFree(ctxt->path);
252 ctxt->path = NULL;
253 }
254 if (URL == NULL) return;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000255 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000256 while (*cur != 0) {
257 if ((cur[0] == ':') && (cur[1] == '/') && (cur[2] == '/')) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000258 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000259 ctxt->protocol = xmlMemStrdup(buf);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000260 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000261 cur += 3;
262 break;
263 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000264 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000265 }
266 if (*cur == 0) return;
267
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000268 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000269 while (1) {
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000270 if ((strchr (cur, '[') && !strchr (cur, ']')) ||
271 (!strchr (cur, '[') && strchr (cur, ']'))) {
272 xmlGenericError (xmlGenericErrorContext, "\nxmlNanoHTTPScanURL: %s",
273 "Syntax Error\n");
274 return;
275 }
276
277 if (cur[0] == '[') {
278 cur++;
279 while (cur[0] != ']')
280 buf[indx++] = *cur++;
281
282 if (!strchr (buf, ':')) {
283 xmlGenericError (xmlGenericErrorContext, "\nxmlNanoHTTPScanURL: %s",
284 "Use [IPv6]/IPv4 format\n");
285 return;
286 }
287
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000288 buf[indx] = 0;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000289 ctxt->hostname = xmlMemStrdup (buf);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000290 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000291 cur += 1;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000292 if (cur[0] == ':') {
Owen Taylor3473f882001-02-23 17:55:21 +0000293 cur++;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000294 while (*cur >= '0' && *cur <= '9') {
295 port *= 10;
296 port += *cur - '0';
297 cur++;
298 }
299
300 if (port != 0) ctxt->port = port;
301 while ((cur[0] != '/') && (*cur != 0))
302 cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000303 }
Owen Taylor3473f882001-02-23 17:55:21 +0000304 break;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000305 }
306 else {
307 if (cur[0] == ':') {
308 buf[indx] = 0;
309 ctxt->hostname = xmlMemStrdup (buf);
310 indx = 0;
311 cur += 1;
312 while ((*cur >= '0') && (*cur <= '9')) {
313 port *= 10;
314 port += *cur - '0';
315 cur++;
316 }
317 if (port != 0) ctxt->port = port;
318 while ((cur[0] != '/') && (*cur != 0))
319 cur++;
320 break;
321 }
322 if ((*cur == '/') || (*cur == 0)) {
323 buf[indx] = 0;
324 ctxt->hostname = xmlMemStrdup (buf);
325 indx = 0;
326 break;
327 }
Owen Taylor3473f882001-02-23 17:55:21 +0000328 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000329 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000330 }
331 if (*cur == 0)
332 ctxt->path = xmlMemStrdup("/");
333 else {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000334 indx = 0;
335 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000336 while (*cur != 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000337 buf[indx++] = *cur++;
338 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000339 ctxt->path = xmlMemStrdup(buf);
340 }
341}
342
343/**
344 * xmlNanoHTTPScanProxy:
345 * @URL: The proxy URL used to initialize the proxy context
346 *
347 * (Re)Initialize the HTTP Proxy context by parsing the URL and finding
348 * the protocol host port it indicates.
349 * Should be like http://myproxy/ or http://myproxy:3128/
350 * A NULL URL cleans up proxy informations.
351 */
352
353void
354xmlNanoHTTPScanProxy(const char *URL) {
355 const char *cur = URL;
356 char buf[4096];
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000357 int indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000358 int port = 0;
359
360 if (proxy != NULL) {
361 xmlFree(proxy);
362 proxy = NULL;
363 }
364 if (proxyPort != 0) {
365 proxyPort = 0;
366 }
367#ifdef DEBUG_HTTP
368 if (URL == NULL)
369 xmlGenericError(xmlGenericErrorContext,
370 "Removing HTTP proxy info\n");
371 else
372 xmlGenericError(xmlGenericErrorContext,
373 "Using HTTP proxy %s\n", URL);
374#endif
375 if (URL == NULL) return;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000376 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000377 while (*cur != 0) {
378 if ((cur[0] == ':') && (cur[1] == '/') && (cur[2] == '/')) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000379 buf[indx] = 0;
380 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000381 cur += 3;
382 break;
383 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000384 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000385 }
386 if (*cur == 0) return;
387
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000388 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000389 while (1) {
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000390 if ((strchr (cur, '[') && !strchr (cur, ']')) ||
391 (!strchr (cur, '[') && strchr (cur, ']'))) {
392 xmlGenericError (xmlGenericErrorContext, "\nxmlNanoHTTPScanProxy: %s",
393 "Syntax error\n");
394 return;
395 }
396
397 if (cur[0] == '[') {
398 cur++;
399 while (cur[0] != ']')
400 buf[indx++] = *cur++;
401
402 if (!strchr (buf, ':')) {
403 xmlGenericError (xmlGenericErrorContext, "\nxmlNanoHTTPScanProxy: %s",
404 "Use [IPv6]/IPv4 format\n");
405 return;
406 }
407
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000408 buf[indx] = 0;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000409 proxy = xmlMemStrdup (buf);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000410 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000411 cur += 1;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000412 if (cur[0] == ':') {
Owen Taylor3473f882001-02-23 17:55:21 +0000413 cur++;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000414 while (*cur >= '0' && *cur <= '9') {
415 port *= 10;
416 port += *cur - '0';
417 cur++;
418 }
419
420 if (port != 0) proxyPort = port;
421 while ((cur[0] != '/') && (*cur != 0))
422 cur ++;
423 }
Owen Taylor3473f882001-02-23 17:55:21 +0000424 break;
425 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000426 else {
427 if (cur[0] == ':') {
428 buf[indx] = 0;
429 proxy = xmlMemStrdup (buf);
430 indx = 0;
431 cur += 1;
432 while ((*cur >= '0') && (*cur <= '9')) {
433 port *= 10;
434 port += *cur - '0';
435 cur++;
436 }
437 if (port != 0) proxyPort = port;
438 while ((cur[0] != '/') && (*cur != 0))
439 cur++;
440 break;
441 }
442 if ((*cur == '/') || (*cur == 0)) {
443 buf[indx] = 0;
444 proxy = xmlMemStrdup (buf);
445 indx = 0;
446 break;
447 }
Owen Taylor3473f882001-02-23 17:55:21 +0000448 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000449 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000450 }
451}
452
453/**
454 * xmlNanoHTTPNewCtxt:
455 * @URL: The URL used to initialize the context
456 *
457 * Allocate and initialize a new HTTP context.
458 *
459 * Returns an HTTP context or NULL in case of error.
460 */
461
462static xmlNanoHTTPCtxtPtr
463xmlNanoHTTPNewCtxt(const char *URL) {
464 xmlNanoHTTPCtxtPtr ret;
465
466 ret = (xmlNanoHTTPCtxtPtr) xmlMalloc(sizeof(xmlNanoHTTPCtxt));
467 if (ret == NULL) return(NULL);
468
469 memset(ret, 0, sizeof(xmlNanoHTTPCtxt));
470 ret->port = 80;
471 ret->returnValue = 0;
472 ret->fd = -1;
Daniel Veillardf012a642001-07-23 19:10:52 +0000473 ret->ContentLength = -1;
Owen Taylor3473f882001-02-23 17:55:21 +0000474
Daniel Veillardcacbe5d2003-01-10 16:09:51 +0000475 xmlNanoHTTPScanURL(ret, URL);
Owen Taylor3473f882001-02-23 17:55:21 +0000476
477 return(ret);
478}
479
480/**
481 * xmlNanoHTTPFreeCtxt:
482 * @ctxt: an HTTP context
483 *
484 * Frees the context after closing the connection.
485 */
486
487static void
488xmlNanoHTTPFreeCtxt(xmlNanoHTTPCtxtPtr ctxt) {
489 if (ctxt == NULL) return;
490 if (ctxt->hostname != NULL) xmlFree(ctxt->hostname);
491 if (ctxt->protocol != NULL) xmlFree(ctxt->protocol);
492 if (ctxt->path != NULL) xmlFree(ctxt->path);
493 if (ctxt->out != NULL) xmlFree(ctxt->out);
494 if (ctxt->in != NULL) xmlFree(ctxt->in);
495 if (ctxt->contentType != NULL) xmlFree(ctxt->contentType);
496 if (ctxt->location != NULL) xmlFree(ctxt->location);
497 if (ctxt->authHeader != NULL) xmlFree(ctxt->authHeader);
498 ctxt->state = XML_NANO_HTTP_NONE;
499 if (ctxt->fd >= 0) closesocket(ctxt->fd);
500 ctxt->fd = -1;
501 xmlFree(ctxt);
502}
503
504/**
505 * xmlNanoHTTPSend:
506 * @ctxt: an HTTP context
507 *
508 * Send the input needed to initiate the processing on the server side
Daniel Veillardf012a642001-07-23 19:10:52 +0000509 * Returns number of bytes sent or -1 on error.
Owen Taylor3473f882001-02-23 17:55:21 +0000510 */
511
Daniel Veillardf012a642001-07-23 19:10:52 +0000512static int
513xmlNanoHTTPSend(xmlNanoHTTPCtxtPtr ctxt, const char * xmt_ptr, int outlen) {
514
515 int total_sent = 0;
516
517 if ( (ctxt->state & XML_NANO_HTTP_WRITE) && (xmt_ptr != NULL ) ) {
518 while (total_sent < outlen) {
519 int nsent = send(ctxt->fd, xmt_ptr + total_sent,
520 outlen - total_sent, 0);
Owen Taylor3473f882001-02-23 17:55:21 +0000521 if (nsent>0)
522 total_sent += nsent;
Daniel Veillardf012a642001-07-23 19:10:52 +0000523 else if ( ( nsent == -1 ) &&
Daniel Veillardba6db032001-07-31 16:25:45 +0000524#if defined(EAGAIN) && EAGAIN != EWOULDBLOCK
Daniel Veillardf012a642001-07-23 19:10:52 +0000525 ( socket_errno( ) != EAGAIN ) &&
Daniel Veillardba6db032001-07-31 16:25:45 +0000526#endif
Daniel Veillardf012a642001-07-23 19:10:52 +0000527 ( socket_errno( ) != EWOULDBLOCK ) ) {
528 xmlGenericError( xmlGenericErrorContext,
529 "xmlNanoHTTPSend error: %s",
530 strerror( socket_errno( ) ) );
531
532 if ( total_sent == 0 )
533 total_sent = -1;
534 break;
535 }
536 else {
537 /*
538 ** No data sent
539 ** Since non-blocking sockets are used, wait for
540 ** socket to be writable or default timeout prior
541 ** to retrying.
542 */
543
544 struct timeval tv;
545 fd_set wfd;
546
547 tv.tv_sec = timeout;
548 tv.tv_usec = 0;
549 FD_ZERO( &wfd );
550 FD_SET( ctxt->fd, &wfd );
551 (void)select( ctxt->fd + 1, NULL, &wfd, NULL, &tv );
552 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000553 }
Owen Taylor3473f882001-02-23 17:55:21 +0000554 }
Daniel Veillardf012a642001-07-23 19:10:52 +0000555
556 return total_sent;
Owen Taylor3473f882001-02-23 17:55:21 +0000557}
558
559/**
560 * xmlNanoHTTPRecv:
561 * @ctxt: an HTTP context
562 *
563 * Read information coming from the HTTP connection.
564 * This is a blocking call (but it blocks in select(), not read()).
565 *
566 * Returns the number of byte read or -1 in case of error.
567 */
568
569static int
570xmlNanoHTTPRecv(xmlNanoHTTPCtxtPtr ctxt) {
571 fd_set rfd;
572 struct timeval tv;
573
574
575 while (ctxt->state & XML_NANO_HTTP_READ) {
576 if (ctxt->in == NULL) {
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000577 ctxt->in = (char *) xmlMallocAtomic(65000 * sizeof(char));
Owen Taylor3473f882001-02-23 17:55:21 +0000578 if (ctxt->in == NULL) {
579 ctxt->last = -1;
Daniel Veillardf012a642001-07-23 19:10:52 +0000580 xmlGenericError( xmlGenericErrorContext,
581 "xmlNanoHTTPRecv: Error allocating input memory." );
Owen Taylor3473f882001-02-23 17:55:21 +0000582 return(-1);
583 }
584 ctxt->inlen = 65000;
585 ctxt->inptr = ctxt->content = ctxt->inrptr = ctxt->in;
586 }
587 if (ctxt->inrptr > ctxt->in + XML_NANO_HTTP_CHUNK) {
588 int delta = ctxt->inrptr - ctxt->in;
589 int len = ctxt->inptr - ctxt->inrptr;
590
591 memmove(ctxt->in, ctxt->inrptr, len);
592 ctxt->inrptr -= delta;
593 ctxt->content -= delta;
594 ctxt->inptr -= delta;
595 }
596 if ((ctxt->in + ctxt->inlen) < (ctxt->inptr + XML_NANO_HTTP_CHUNK)) {
597 int d_inptr = ctxt->inptr - ctxt->in;
598 int d_content = ctxt->content - ctxt->in;
599 int d_inrptr = ctxt->inrptr - ctxt->in;
Daniel Veillardf012a642001-07-23 19:10:52 +0000600 char * tmp_ptr = ctxt->in;
Owen Taylor3473f882001-02-23 17:55:21 +0000601
602 ctxt->inlen *= 2;
Daniel Veillardf012a642001-07-23 19:10:52 +0000603 ctxt->in = (char *) xmlRealloc(tmp_ptr, ctxt->inlen);
Owen Taylor3473f882001-02-23 17:55:21 +0000604 if (ctxt->in == NULL) {
Daniel Veillardf012a642001-07-23 19:10:52 +0000605 xmlGenericError( xmlGenericErrorContext,
606 "xmlNanoHTTPRecv: %s %d bytes.",
607 "Failed to realloc input buffer to",
608 ctxt->inlen );
609 xmlFree( tmp_ptr );
Owen Taylor3473f882001-02-23 17:55:21 +0000610 ctxt->last = -1;
611 return(-1);
612 }
613 ctxt->inptr = ctxt->in + d_inptr;
614 ctxt->content = ctxt->in + d_content;
615 ctxt->inrptr = ctxt->in + d_inrptr;
616 }
617 ctxt->last = recv(ctxt->fd, ctxt->inptr, XML_NANO_HTTP_CHUNK, 0);
618 if (ctxt->last > 0) {
619 ctxt->inptr += ctxt->last;
620 return(ctxt->last);
621 }
622 if (ctxt->last == 0) {
623 return(0);
624 }
625 if (ctxt->last == -1) {
626 switch (socket_errno()) {
627 case EINPROGRESS:
628 case EWOULDBLOCK:
629#if defined(EAGAIN) && EAGAIN != EWOULDBLOCK
630 case EAGAIN:
631#endif
632 break;
Daniel Veillardf012a642001-07-23 19:10:52 +0000633
634 case ECONNRESET:
635 case ESHUTDOWN:
636 return ( 0 );
637
Owen Taylor3473f882001-02-23 17:55:21 +0000638 default:
Daniel Veillardf012a642001-07-23 19:10:52 +0000639 xmlGenericError( xmlGenericErrorContext,
640 "xmlNanoHTTPRecv: recv( ) failure - %s",
641 strerror( socket_errno( ) ) );
642 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +0000643 }
644 }
645
646 tv.tv_sec = timeout;
647 tv.tv_usec = 0;
648 FD_ZERO(&rfd);
649 FD_SET(ctxt->fd, &rfd);
650
Daniel Veillard50f34372001-08-03 12:06:36 +0000651 if ( (select(ctxt->fd+1, &rfd, NULL, NULL, &tv)<1)
652#if defined(EINTR)
653 && (errno != EINTR)
654#endif
655 )
Owen Taylor3473f882001-02-23 17:55:21 +0000656 return(0);
657 }
658 return(0);
659}
660
661/**
662 * xmlNanoHTTPReadLine:
663 * @ctxt: an HTTP context
664 *
665 * Read one line in the HTTP server output, usually for extracting
666 * the HTTP protocol informations from the answer header.
667 *
668 * Returns a newly allocated string with a copy of the line, or NULL
669 * which indicate the end of the input.
670 */
671
672static char *
673xmlNanoHTTPReadLine(xmlNanoHTTPCtxtPtr ctxt) {
674 char buf[4096];
675 char *bp = buf;
Daniel Veillardf012a642001-07-23 19:10:52 +0000676 int rc;
Owen Taylor3473f882001-02-23 17:55:21 +0000677
678 while (bp - buf < 4095) {
679 if (ctxt->inrptr == ctxt->inptr) {
Daniel Veillardf012a642001-07-23 19:10:52 +0000680 if ( (rc = xmlNanoHTTPRecv(ctxt)) == 0) {
Owen Taylor3473f882001-02-23 17:55:21 +0000681 if (bp == buf)
682 return(NULL);
683 else
684 *bp = 0;
685 return(xmlMemStrdup(buf));
686 }
Daniel Veillardf012a642001-07-23 19:10:52 +0000687 else if ( rc == -1 ) {
688 return ( NULL );
689 }
Owen Taylor3473f882001-02-23 17:55:21 +0000690 }
691 *bp = *ctxt->inrptr++;
692 if (*bp == '\n') {
693 *bp = 0;
694 return(xmlMemStrdup(buf));
695 }
696 if (*bp != '\r')
697 bp++;
698 }
699 buf[4095] = 0;
700 return(xmlMemStrdup(buf));
701}
702
703
704/**
705 * xmlNanoHTTPScanAnswer:
706 * @ctxt: an HTTP context
707 * @line: an HTTP header line
708 *
709 * Try to extract useful informations from the server answer.
710 * We currently parse and process:
711 * - The HTTP revision/ return code
712 * - The Content-Type
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000713 * - The Location for redirect processing.
Owen Taylor3473f882001-02-23 17:55:21 +0000714 *
715 * Returns -1 in case of failure, the file descriptor number otherwise
716 */
717
718static void
719xmlNanoHTTPScanAnswer(xmlNanoHTTPCtxtPtr ctxt, const char *line) {
720 const char *cur = line;
721
722 if (line == NULL) return;
723
724 if (!strncmp(line, "HTTP/", 5)) {
725 int version = 0;
726 int ret = 0;
727
728 cur += 5;
729 while ((*cur >= '0') && (*cur <= '9')) {
730 version *= 10;
731 version += *cur - '0';
732 cur++;
733 }
734 if (*cur == '.') {
735 cur++;
736 if ((*cur >= '0') && (*cur <= '9')) {
737 version *= 10;
738 version += *cur - '0';
739 cur++;
740 }
741 while ((*cur >= '0') && (*cur <= '9'))
742 cur++;
743 } else
744 version *= 10;
745 if ((*cur != ' ') && (*cur != '\t')) return;
746 while ((*cur == ' ') || (*cur == '\t')) cur++;
747 if ((*cur < '0') || (*cur > '9')) return;
748 while ((*cur >= '0') && (*cur <= '9')) {
749 ret *= 10;
750 ret += *cur - '0';
751 cur++;
752 }
753 if ((*cur != 0) && (*cur != ' ') && (*cur != '\t')) return;
754 ctxt->returnValue = ret;
755 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Content-Type:", 13)) {
756 cur += 13;
757 while ((*cur == ' ') || (*cur == '\t')) cur++;
758 if (ctxt->contentType != NULL)
759 xmlFree(ctxt->contentType);
760 ctxt->contentType = xmlMemStrdup(cur);
761 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"ContentType:", 12)) {
762 cur += 12;
763 if (ctxt->contentType != NULL) return;
764 while ((*cur == ' ') || (*cur == '\t')) cur++;
765 ctxt->contentType = xmlMemStrdup(cur);
766 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Location:", 9)) {
767 cur += 9;
768 while ((*cur == ' ') || (*cur == '\t')) cur++;
769 if (ctxt->location != NULL)
770 xmlFree(ctxt->location);
771 ctxt->location = xmlMemStrdup(cur);
772 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"WWW-Authenticate:", 17)) {
773 cur += 17;
774 while ((*cur == ' ') || (*cur == '\t')) cur++;
775 if (ctxt->authHeader != NULL)
776 xmlFree(ctxt->authHeader);
777 ctxt->authHeader = xmlMemStrdup(cur);
778 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Proxy-Authenticate:", 19)) {
779 cur += 19;
780 while ((*cur == ' ') || (*cur == '\t')) cur++;
781 if (ctxt->authHeader != NULL)
782 xmlFree(ctxt->authHeader);
783 ctxt->authHeader = xmlMemStrdup(cur);
Daniel Veillardf012a642001-07-23 19:10:52 +0000784 } else if ( !xmlStrncasecmp( BAD_CAST line, BAD_CAST"Content-Length:", 15) ) {
785 cur += 15;
786 ctxt->ContentLength = strtol( cur, NULL, 10 );
Owen Taylor3473f882001-02-23 17:55:21 +0000787 }
788}
789
790/**
791 * xmlNanoHTTPConnectAttempt:
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000792 * @addr: a socket address structure
Owen Taylor3473f882001-02-23 17:55:21 +0000793 *
794 * Attempt a connection to the given IP:port endpoint. It forces
795 * non-blocking semantic on the socket, and allow 60 seconds for
796 * the host to answer.
797 *
798 * Returns -1 in case of failure, the file descriptor number otherwise
799 */
800
801static int
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000802xmlNanoHTTPConnectAttempt(struct sockaddr *addr)
Owen Taylor3473f882001-02-23 17:55:21 +0000803{
Owen Taylor3473f882001-02-23 17:55:21 +0000804 fd_set wfd;
805 struct timeval tv;
806 int status;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000807 int addrlen;
808 SOCKET s;
Owen Taylor3473f882001-02-23 17:55:21 +0000809
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000810#ifdef SUPPORT_IP6
811 if (addr->sa_family == AF_INET6) {
812 s = socket (PF_INET6, SOCK_STREAM, IPPROTO_TCP);
813 addrlen = sizeof (struct sockaddr_in6);
814 }
815 else
816#endif
817 {
818 s = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
819 addrlen = sizeof (struct sockaddr_in);
820 }
Owen Taylor3473f882001-02-23 17:55:21 +0000821 if (s==-1) {
822#ifdef DEBUG_HTTP
823 perror("socket");
824#endif
Daniel Veillardf012a642001-07-23 19:10:52 +0000825 xmlGenericError( xmlGenericErrorContext,
826 "xmlNanoHTTPConnectAttempt: %s - %s",
827 "socket creation failure",
828 strerror( socket_errno( ) ) );
Owen Taylor3473f882001-02-23 17:55:21 +0000829 return(-1);
830 }
831
832#ifdef _WINSOCKAPI_
833 {
834 u_long one = 1;
835
836 status = ioctlsocket(s, FIONBIO, &one) == SOCKET_ERROR ? -1 : 0;
837 }
838#else /* _WINSOCKAPI_ */
839#if defined(VMS)
840 {
841 int enable = 1;
842 status = ioctl(s, FIONBIO, &enable);
843 }
844#else /* VMS */
845 if ((status = fcntl(s, F_GETFL, 0)) != -1) {
846#ifdef O_NONBLOCK
847 status |= O_NONBLOCK;
848#else /* O_NONBLOCK */
849#ifdef F_NDELAY
850 status |= F_NDELAY;
851#endif /* F_NDELAY */
852#endif /* !O_NONBLOCK */
853 status = fcntl(s, F_SETFL, status);
854 }
855 if (status < 0) {
856#ifdef DEBUG_HTTP
857 perror("nonblocking");
858#endif
Daniel Veillardf012a642001-07-23 19:10:52 +0000859 xmlGenericError( xmlGenericErrorContext,
860 "xmlNanoHTTPConnectAttempt: %s - %s",
861 "error setting non-blocking IO",
862 strerror( socket_errno( ) ) );
Owen Taylor3473f882001-02-23 17:55:21 +0000863 closesocket(s);
864 return(-1);
865 }
866#endif /* !VMS */
867#endif /* !_WINSOCKAPI_ */
868
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000869 if (connect (s, addr, addrlen) == -1) {
Owen Taylor3473f882001-02-23 17:55:21 +0000870 switch (socket_errno()) {
871 case EINPROGRESS:
872 case EWOULDBLOCK:
873 break;
874 default:
Daniel Veillardf012a642001-07-23 19:10:52 +0000875 xmlGenericError( xmlGenericErrorContext,
876 "xmlNanoHTTPConnectAttempt: %s - %s",
877 "error connecting to HTTP server",
878 strerror( socket_errno( ) ) );
Owen Taylor3473f882001-02-23 17:55:21 +0000879 closesocket(s);
880 return(-1);
881 }
882 }
883
884 tv.tv_sec = timeout;
885 tv.tv_usec = 0;
886
887 FD_ZERO(&wfd);
888 FD_SET(s, &wfd);
889
890 switch(select(s+1, NULL, &wfd, NULL, &tv))
891 {
892 case 0:
893 /* Time out */
Daniel Veillardf012a642001-07-23 19:10:52 +0000894 xmlGenericError( xmlGenericErrorContext,
895 "xmlNanoHTTPConnectAttempt: %s",
896 "Connect attempt timed out." );
Owen Taylor3473f882001-02-23 17:55:21 +0000897 closesocket(s);
898 return(-1);
899 case -1:
900 /* Ermm.. ?? */
Daniel Veillardf012a642001-07-23 19:10:52 +0000901 xmlGenericError( xmlGenericErrorContext,
902 "xmlNanoHTTPConnectAttempt: %s - %s",
903 "Error connecting to host",
904 strerror( socket_errno( ) ) );
Owen Taylor3473f882001-02-23 17:55:21 +0000905 closesocket(s);
906 return(-1);
907 }
908
909 if ( FD_ISSET(s, &wfd) ) {
910 SOCKLEN_T len;
911 len = sizeof(status);
912 if (getsockopt(s, SOL_SOCKET, SO_ERROR, (char*)&status, &len) < 0 ) {
913 /* Solaris error code */
Daniel Veillardf012a642001-07-23 19:10:52 +0000914 xmlGenericError( xmlGenericErrorContext,
915 "xmlNanoHTTPConnectAttempt: %s - %s",
916 "Error retrieving pending socket errors",
917 strerror( socket_errno( ) ) );
Owen Taylor3473f882001-02-23 17:55:21 +0000918 return (-1);
919 }
920 if ( status ) {
921 closesocket(s);
922 errno = status;
Daniel Veillardf012a642001-07-23 19:10:52 +0000923 xmlGenericError( xmlGenericErrorContext,
924 "xmlNanoHTTPConnectAttempt: %s - %s",
925 "Error connecting to remote host",
926 strerror( status ) );
Owen Taylor3473f882001-02-23 17:55:21 +0000927 return (-1);
928 }
929 } else {
930 /* pbm */
Daniel Veillardf012a642001-07-23 19:10:52 +0000931 xmlGenericError( xmlGenericErrorContext,
932 "xmlNanoHTTPConnectAttempt: %s\n",
933 "Select returned, but descriptor not set for connection.\n" );
934 closesocket(s);
Owen Taylor3473f882001-02-23 17:55:21 +0000935 return (-1);
936 }
937
938 return(s);
939}
940
941/**
942 * xmlNanoHTTPConnectHost:
943 * @host: the host name
944 * @port: the port number
945 *
946 * Attempt a connection to the given host:port endpoint. It tries
947 * the multiple IP provided by the DNS if available.
948 *
949 * Returns -1 in case of failure, the file descriptor number otherwise
950 */
951
952static int
953xmlNanoHTTPConnectHost(const char *host, int port)
954{
955 struct hostent *h;
956 struct sockaddr *addr;
957 struct in_addr ia;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000958 struct sockaddr_in sockin;
Daniel Veillard5c396542002-03-15 07:57:50 +0000959
Owen Taylor3473f882001-02-23 17:55:21 +0000960#ifdef SUPPORT_IP6
961 struct in6_addr ia6;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000962 struct sockaddr_in6 sockin6;
Owen Taylor3473f882001-02-23 17:55:21 +0000963#endif
964 int i;
965 int s;
Daniel Veillard5c396542002-03-15 07:57:50 +0000966
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000967 memset (&sockin, 0, sizeof(sockin));
968#ifdef SUPPORT_IP6
969 memset (&sockin6, 0, sizeof(sockin6));
970 if (have_ipv6 ())
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000971#if !defined(HAVE_GETADDRINFO) && defined(RES_USE_INET6)
Daniel Veillard560c2a42003-07-06 21:13:49 +0000972 {
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000973 if (!(_res.options & RES_INIT))
974 res_init();
975 _res.options |= RES_USE_INET6;
976 }
977#elif defined(HAVE_GETADDRINFO)
Daniel Veillard560c2a42003-07-06 21:13:49 +0000978 {
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000979 int status;
980 struct addrinfo hints, *res, *result;
981
982 result = NULL;
983 memset (&hints, 0,sizeof(hints));
984 hints.ai_socktype = SOCK_STREAM;
985
986 status = getaddrinfo (host, NULL, &hints, &result);
987 if (status) {
988 xmlGenericError (xmlGenericErrorContext,
989 "xmlNanoHTTPConnectHost: %s '%s' - %s",
990 "Failed to resolve host", host, gai_strerror (status));
991
992 return (-1);
993 }
994
995 for (res = result; res; res = res->ai_next) {
996 if (res) {
997 if (res->ai_family == AF_INET6) {
998 memcpy (&sockin6, res->ai_addr, res->ai_addrlen);
999 sockin6.sin6_port = htons (port);
1000 addr = (struct sockaddr *)&sockin6;
1001 }
1002
1003 if (res->ai_family == AF_INET) {
1004 memcpy (&sockin, res->ai_addr, res->ai_addrlen);
1005 sockin.sin_port = htons (port);
1006 addr = (struct sockaddr *)&sockin;
1007 }
1008
1009 s = xmlNanoHTTPConnectAttempt (addr);
1010 if (s != -1) {
1011 freeaddrinfo (result);
1012 return (s);
1013 }
1014 }
1015 else {
1016 freeaddrinfo (result);
1017 return (-1);
1018 }
1019 }
1020 } else
Owen Taylor3473f882001-02-23 17:55:21 +00001021#endif
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001022#endif
1023 {
1024 h = gethostbyname (host);
1025 if (h == NULL) {
Daniel Veillard56b2db72002-03-25 16:35:28 +00001026
1027/*
1028 * Okay, I got fed up by the non-portability of this error message
1029 * extraction code. it work on Linux, if it work on your platform
1030 * and one want to enable it, send me the defined(foobar) needed
1031 */
1032#if defined(HAVE_NETDB_H) && defined(HOST_NOT_FOUND) && defined(linux)
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001033 const char *h_err_txt = "";
Daniel Veillardf012a642001-07-23 19:10:52 +00001034
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001035 switch (h_errno) {
1036 case HOST_NOT_FOUND:
1037 h_err_txt = "Authoritive host not found";
1038 break;
Daniel Veillardf012a642001-07-23 19:10:52 +00001039
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001040 case TRY_AGAIN:
1041 h_err_txt =
1042 "Non-authoritive host not found or server failure.";
1043 break;
Daniel Veillardf012a642001-07-23 19:10:52 +00001044
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001045 case NO_RECOVERY:
1046 h_err_txt =
1047 "Non-recoverable errors: FORMERR, REFUSED, or NOTIMP.";
1048 break;
Daniel Veillard5c396542002-03-15 07:57:50 +00001049
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001050 case NO_ADDRESS:
1051 h_err_txt =
1052 "Valid name, no data record of requested type.";
1053 break;
Daniel Veillard5c396542002-03-15 07:57:50 +00001054
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001055 default:
1056 h_err_txt = "No error text defined.";
1057 break;
1058 }
1059 xmlGenericError (xmlGenericErrorContext,
1060 "xmlNanoHTTPConnectHost: %s '%s' - %s",
1061 "Failed to resolve host", host, h_err_txt);
Daniel Veillard5c396542002-03-15 07:57:50 +00001062#else
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001063 xmlGenericError (xmlGenericErrorContext,
1064 "xmlNanoHTTPConnectHost: %s '%s'",
1065 "Failed to resolve host", host);
Owen Taylor3473f882001-02-23 17:55:21 +00001066#endif
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001067 return (-1);
1068 }
Daniel Veillard5c396542002-03-15 07:57:50 +00001069
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001070 for (i = 0; h->h_addr_list[i]; i++) {
1071 if (h->h_addrtype == AF_INET) {
1072 /* A records (IPv4) */
1073 memcpy (&ia, h->h_addr_list[i], h->h_length);
1074 sockin.sin_family = h->h_addrtype;
1075 sockin.sin_addr = ia;
1076 sockin.sin_port = htons (port);
1077 addr = (struct sockaddr *) &sockin;
Daniel Veillard5c396542002-03-15 07:57:50 +00001078#ifdef SUPPORT_IP6
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001079 } else if (have_ipv6 () && (h->h_addrtype == AF_INET6)) {
1080 /* AAAA records (IPv6) */
1081 memcpy (&ia6, h->h_addr_list[i], h->h_length);
1082 sockin6.sin6_family = h->h_addrtype;
1083 sockin6.sin6_addr = ia6;
1084 sockin6.sin6_port = htons (port);
1085 addr = (struct sockaddr *) &sockin6;
Daniel Veillard5c396542002-03-15 07:57:50 +00001086#endif
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001087 } else
1088 break; /* for */
Daniel Veillard5c396542002-03-15 07:57:50 +00001089
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001090 s = xmlNanoHTTPConnectAttempt (addr);
1091 if (s != -1)
1092 return (s);
1093 }
Owen Taylor3473f882001-02-23 17:55:21 +00001094 }
Owen Taylor3473f882001-02-23 17:55:21 +00001095#ifdef DEBUG_HTTP
1096 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard5c396542002-03-15 07:57:50 +00001097 "xmlNanoHTTPConnectHost: unable to connect to '%s'.\n",
1098 host);
Owen Taylor3473f882001-02-23 17:55:21 +00001099#endif
Daniel Veillard5c396542002-03-15 07:57:50 +00001100 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001101}
1102
1103
1104/**
1105 * xmlNanoHTTPOpen:
1106 * @URL: The URL to load
1107 * @contentType: if available the Content-Type information will be
1108 * returned at that location
1109 *
1110 * This function try to open a connection to the indicated resource
1111 * via HTTP GET.
1112 *
1113 * Returns NULL in case of failure, otherwise a request handler.
1114 * The contentType, if provided must be freed by the caller
1115 */
1116
1117void*
1118xmlNanoHTTPOpen(const char *URL, char **contentType) {
1119 if (contentType != NULL) *contentType = NULL;
Daniel Veillardf012a642001-07-23 19:10:52 +00001120 return(xmlNanoHTTPMethod(URL, NULL, NULL, contentType, NULL, 0));
Daniel Veillard9403a042001-05-28 11:00:53 +00001121}
1122
1123/**
1124 * xmlNanoHTTPOpenRedir:
1125 * @URL: The URL to load
1126 * @contentType: if available the Content-Type information will be
1127 * returned at that location
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001128 * @redir: if available the redirected URL will be returned
Daniel Veillard9403a042001-05-28 11:00:53 +00001129 *
1130 * This function try to open a connection to the indicated resource
1131 * via HTTP GET.
1132 *
1133 * Returns NULL in case of failure, otherwise a request handler.
1134 * The contentType, if provided must be freed by the caller
1135 */
1136
1137void*
1138xmlNanoHTTPOpenRedir(const char *URL, char **contentType, char **redir) {
1139 if (contentType != NULL) *contentType = NULL;
1140 if (redir != NULL) *redir = NULL;
Daniel Veillardf012a642001-07-23 19:10:52 +00001141 return(xmlNanoHTTPMethodRedir(URL, NULL, NULL, contentType, redir, NULL,0));
Owen Taylor3473f882001-02-23 17:55:21 +00001142}
1143
1144/**
1145 * xmlNanoHTTPRead:
1146 * @ctx: the HTTP context
1147 * @dest: a buffer
1148 * @len: the buffer length
1149 *
1150 * This function tries to read @len bytes from the existing HTTP connection
1151 * and saves them in @dest. This is a blocking call.
1152 *
1153 * Returns the number of byte read. 0 is an indication of an end of connection.
1154 * -1 indicates a parameter error.
1155 */
1156int
1157xmlNanoHTTPRead(void *ctx, void *dest, int len) {
1158 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1159
1160 if (ctx == NULL) return(-1);
1161 if (dest == NULL) return(-1);
1162 if (len <= 0) return(0);
1163
1164 while (ctxt->inptr - ctxt->inrptr < len) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001165 if (xmlNanoHTTPRecv(ctxt) <= 0) break;
Owen Taylor3473f882001-02-23 17:55:21 +00001166 }
1167 if (ctxt->inptr - ctxt->inrptr < len)
1168 len = ctxt->inptr - ctxt->inrptr;
1169 memcpy(dest, ctxt->inrptr, len);
1170 ctxt->inrptr += len;
1171 return(len);
1172}
1173
1174/**
1175 * xmlNanoHTTPClose:
1176 * @ctx: the HTTP context
1177 *
1178 * This function closes an HTTP context, it ends up the connection and
1179 * free all data related to it.
1180 */
1181void
1182xmlNanoHTTPClose(void *ctx) {
1183 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1184
1185 if (ctx == NULL) return;
1186
1187 xmlNanoHTTPFreeCtxt(ctxt);
1188}
1189
1190/**
Daniel Veillard9403a042001-05-28 11:00:53 +00001191 * xmlNanoHTTPMethodRedir:
Owen Taylor3473f882001-02-23 17:55:21 +00001192 * @URL: The URL to load
1193 * @method: the HTTP method to use
1194 * @input: the input string if any
1195 * @contentType: the Content-Type information IN and OUT
Daniel Veillard9403a042001-05-28 11:00:53 +00001196 * @redir: the redirected URL OUT
Owen Taylor3473f882001-02-23 17:55:21 +00001197 * @headers: the extra headers
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001198 * @ilen: input length
Owen Taylor3473f882001-02-23 17:55:21 +00001199 *
1200 * This function try to open a connection to the indicated resource
1201 * via HTTP using the given @method, adding the given extra headers
1202 * and the input buffer for the request content.
1203 *
1204 * Returns NULL in case of failure, otherwise a request handler.
Daniel Veillard9403a042001-05-28 11:00:53 +00001205 * The contentType, or redir, if provided must be freed by the caller
Owen Taylor3473f882001-02-23 17:55:21 +00001206 */
1207
1208void*
Daniel Veillard9403a042001-05-28 11:00:53 +00001209xmlNanoHTTPMethodRedir(const char *URL, const char *method, const char *input,
Daniel Veillardf012a642001-07-23 19:10:52 +00001210 char **contentType, char **redir,
1211 const char *headers, int ilen ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001212 xmlNanoHTTPCtxtPtr ctxt;
1213 char *bp, *p;
Daniel Veillardf012a642001-07-23 19:10:52 +00001214 int blen, ret;
Owen Taylor3473f882001-02-23 17:55:21 +00001215 int head;
Daniel Veillardf012a642001-07-23 19:10:52 +00001216 int xmt_bytes;
Owen Taylor3473f882001-02-23 17:55:21 +00001217 int nbRedirects = 0;
1218 char *redirURL = NULL;
1219
1220 if (URL == NULL) return(NULL);
1221 if (method == NULL) method = "GET";
1222 xmlNanoHTTPInit();
1223
1224retry:
1225 if (redirURL == NULL)
1226 ctxt = xmlNanoHTTPNewCtxt(URL);
1227 else {
1228 ctxt = xmlNanoHTTPNewCtxt(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001229 }
1230
Daniel Veillardf012a642001-07-23 19:10:52 +00001231 if ( ctxt == NULL ) {
1232 xmlGenericError( xmlGenericErrorContext,
1233 "xmlNanoHTTPMethodRedir: %s %s.",
1234 "Unable to allocate HTTP context to URI",
1235 ( ( redirURL == NULL ) ? URL : redirURL ) );
1236 return ( NULL );
1237 }
1238
Owen Taylor3473f882001-02-23 17:55:21 +00001239 if ((ctxt->protocol == NULL) || (strcmp(ctxt->protocol, "http"))) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001240 xmlGenericError( xmlGenericErrorContext,
1241 "xmlNanoHTTPMethodRedir: %s - %s.",
1242 "Not a valid HTTP URI",
1243 ( ( redirURL == NULL ) ? URL : redirURL ) );
Owen Taylor3473f882001-02-23 17:55:21 +00001244 xmlNanoHTTPFreeCtxt(ctxt);
1245 if (redirURL != NULL) xmlFree(redirURL);
1246 return(NULL);
1247 }
1248 if (ctxt->hostname == NULL) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001249 xmlGenericError( xmlGenericErrorContext,
1250 "xmlNanoHTTPMethodRedir: %s - %s",
1251 "Failed to identify host in URI",
1252 ( ( redirURL == NULL ) ? URL : redirURL ) );
Owen Taylor3473f882001-02-23 17:55:21 +00001253 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001254 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001255 return(NULL);
1256 }
1257 if (proxy) {
1258 blen = strlen(ctxt->hostname) * 2 + 16;
1259 ret = xmlNanoHTTPConnectHost(proxy, proxyPort);
1260 }
1261 else {
1262 blen = strlen(ctxt->hostname);
1263 ret = xmlNanoHTTPConnectHost(ctxt->hostname, ctxt->port);
1264 }
1265 if (ret < 0) {
1266 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001267 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001268 return(NULL);
1269 }
1270 ctxt->fd = ret;
1271
Daniel Veillardf012a642001-07-23 19:10:52 +00001272 if (input == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00001273 ilen = 0;
Daniel Veillardf012a642001-07-23 19:10:52 +00001274 else
1275 blen += 36;
1276
Owen Taylor3473f882001-02-23 17:55:21 +00001277 if (headers != NULL)
Daniel Veillardf012a642001-07-23 19:10:52 +00001278 blen += strlen(headers) + 2;
Owen Taylor3473f882001-02-23 17:55:21 +00001279 if (contentType && *contentType)
1280 blen += strlen(*contentType) + 16;
Daniel Veillardf012a642001-07-23 19:10:52 +00001281 blen += strlen(method) + strlen(ctxt->path) + 24;
Daniel Veillard3c908dc2003-04-19 00:07:51 +00001282 bp = xmlMallocAtomic(blen);
Daniel Veillardf012a642001-07-23 19:10:52 +00001283 if ( bp == NULL ) {
1284 xmlNanoHTTPFreeCtxt( ctxt );
1285 xmlGenericError( xmlGenericErrorContext,
1286 "xmlNanoHTTPMethodRedir: %s",
1287 "Error allocating HTTP header buffer." );
1288 return ( NULL );
1289 }
1290
1291 p = bp;
1292
Owen Taylor3473f882001-02-23 17:55:21 +00001293 if (proxy) {
1294 if (ctxt->port != 80) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001295 p += snprintf( p, blen - (p - bp), "%s http://%s:%d%s",
1296 method, ctxt->hostname,
Daniel Veillardf012a642001-07-23 19:10:52 +00001297 ctxt->port, ctxt->path );
Owen Taylor3473f882001-02-23 17:55:21 +00001298 }
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001299 else
1300 p += snprintf( p, blen - (p - bp), "%s http://%s%s", method,
Daniel Veillardf012a642001-07-23 19:10:52 +00001301 ctxt->hostname, ctxt->path);
Owen Taylor3473f882001-02-23 17:55:21 +00001302 }
1303 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001304 p += snprintf( p, blen - (p - bp), "%s %s", method, ctxt->path);
Daniel Veillardf012a642001-07-23 19:10:52 +00001305
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001306 p += snprintf( p, blen - (p - bp), " HTTP/1.0\r\nHost: %s\r\n",
1307 ctxt->hostname);
Daniel Veillardf012a642001-07-23 19:10:52 +00001308
1309 if (contentType != NULL && *contentType)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001310 p += snprintf(p, blen - (p - bp), "Content-Type: %s\r\n", *contentType);
Daniel Veillardf012a642001-07-23 19:10:52 +00001311
1312 if (headers != NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001313 p += snprintf( p, blen - (p - bp), "%s", headers );
Daniel Veillardf012a642001-07-23 19:10:52 +00001314
Owen Taylor3473f882001-02-23 17:55:21 +00001315 if (input != NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001316 snprintf(p, blen - (p - bp), "Content-Length: %d\r\n\r\n", ilen );
Owen Taylor3473f882001-02-23 17:55:21 +00001317 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001318 snprintf(p, blen - (p - bp), "\r\n");
Daniel Veillardf012a642001-07-23 19:10:52 +00001319
Owen Taylor3473f882001-02-23 17:55:21 +00001320#ifdef DEBUG_HTTP
1321 xmlGenericError(xmlGenericErrorContext,
1322 "-> %s%s", proxy? "(Proxy) " : "", bp);
1323 if ((blen -= strlen(bp)+1) < 0)
1324 xmlGenericError(xmlGenericErrorContext,
1325 "ERROR: overflowed buffer by %d bytes\n", -blen);
1326#endif
1327 ctxt->outptr = ctxt->out = bp;
1328 ctxt->state = XML_NANO_HTTP_WRITE;
Daniel Veillardf012a642001-07-23 19:10:52 +00001329 blen = strlen( ctxt->out );
1330 xmt_bytes = xmlNanoHTTPSend(ctxt, ctxt->out, blen );
1331#ifdef DEBUG_HTTP
1332 if ( xmt_bytes != blen )
1333 xmlGenericError( xmlGenericErrorContext,
1334 "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
1335 xmt_bytes, blen,
1336 "bytes of HTTP headers sent to host",
1337 ctxt->hostname );
1338#endif
1339
1340 if ( input != NULL ) {
1341 xmt_bytes = xmlNanoHTTPSend( ctxt, input, ilen );
1342
1343#ifdef DEBUG_HTTP
1344 if ( xmt_bytes != ilen )
1345 xmlGenericError( xmlGenericErrorContext,
1346 "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
1347 xmt_bytes, ilen,
1348 "bytes of HTTP content sent to host",
1349 ctxt->hostname );
1350#endif
1351 }
1352
Owen Taylor3473f882001-02-23 17:55:21 +00001353 ctxt->state = XML_NANO_HTTP_READ;
1354 head = 1;
1355
1356 while ((p = xmlNanoHTTPReadLine(ctxt)) != NULL) {
1357 if (head && (*p == 0)) {
1358 head = 0;
1359 ctxt->content = ctxt->inrptr;
1360 xmlFree(p);
1361 break;
1362 }
1363 xmlNanoHTTPScanAnswer(ctxt, p);
1364
1365#ifdef DEBUG_HTTP
1366 xmlGenericError(xmlGenericErrorContext, "<- %s\n", p);
1367#endif
1368 xmlFree(p);
1369 }
1370
1371 if ((ctxt->location != NULL) && (ctxt->returnValue >= 300) &&
1372 (ctxt->returnValue < 400)) {
1373#ifdef DEBUG_HTTP
1374 xmlGenericError(xmlGenericErrorContext,
1375 "\nRedirect to: %s\n", ctxt->location);
1376#endif
Daniel Veillardf012a642001-07-23 19:10:52 +00001377 while ( xmlNanoHTTPRecv(ctxt) > 0 ) ;
Owen Taylor3473f882001-02-23 17:55:21 +00001378 if (nbRedirects < XML_NANO_HTTP_MAX_REDIR) {
1379 nbRedirects++;
Daniel Veillard9403a042001-05-28 11:00:53 +00001380 if (redirURL != NULL)
1381 xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001382 redirURL = xmlMemStrdup(ctxt->location);
1383 xmlNanoHTTPFreeCtxt(ctxt);
1384 goto retry;
1385 }
1386 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001387 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001388#ifdef DEBUG_HTTP
1389 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardf012a642001-07-23 19:10:52 +00001390 "xmlNanoHTTPMethodRedir: Too many redirects, aborting ...\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001391#endif
1392 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001393 }
1394
1395 if (contentType != NULL) {
1396 if (ctxt->contentType != NULL)
1397 *contentType = xmlMemStrdup(ctxt->contentType);
1398 else
1399 *contentType = NULL;
1400 }
1401
Daniel Veillard9403a042001-05-28 11:00:53 +00001402 if ((redir != NULL) && (redirURL != NULL)) {
1403 *redir = redirURL;
1404 } else {
1405 if (redirURL != NULL)
1406 xmlFree(redirURL);
1407 if (redir != NULL)
1408 *redir = NULL;
1409 }
1410
Owen Taylor3473f882001-02-23 17:55:21 +00001411#ifdef DEBUG_HTTP
1412 if (ctxt->contentType != NULL)
1413 xmlGenericError(xmlGenericErrorContext,
1414 "\nCode %d, content-type '%s'\n\n",
1415 ctxt->returnValue, ctxt->contentType);
1416 else
1417 xmlGenericError(xmlGenericErrorContext,
1418 "\nCode %d, no content-type\n\n",
1419 ctxt->returnValue);
1420#endif
1421
1422 return((void *) ctxt);
1423}
1424
1425/**
Daniel Veillard9403a042001-05-28 11:00:53 +00001426 * xmlNanoHTTPMethod:
1427 * @URL: The URL to load
1428 * @method: the HTTP method to use
1429 * @input: the input string if any
1430 * @contentType: the Content-Type information IN and OUT
1431 * @headers: the extra headers
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001432 * @ilen: input length
Daniel Veillard9403a042001-05-28 11:00:53 +00001433 *
1434 * This function try to open a connection to the indicated resource
1435 * via HTTP using the given @method, adding the given extra headers
1436 * and the input buffer for the request content.
1437 *
1438 * Returns NULL in case of failure, otherwise a request handler.
1439 * The contentType, if provided must be freed by the caller
1440 */
1441
1442void*
1443xmlNanoHTTPMethod(const char *URL, const char *method, const char *input,
Daniel Veillardf012a642001-07-23 19:10:52 +00001444 char **contentType, const char *headers, int ilen) {
Daniel Veillard9403a042001-05-28 11:00:53 +00001445 return(xmlNanoHTTPMethodRedir(URL, method, input, contentType,
Daniel Veillardf012a642001-07-23 19:10:52 +00001446 NULL, headers, ilen));
Daniel Veillard9403a042001-05-28 11:00:53 +00001447}
1448
1449/**
Owen Taylor3473f882001-02-23 17:55:21 +00001450 * xmlNanoHTTPFetch:
1451 * @URL: The URL to load
1452 * @filename: the filename where the content should be saved
1453 * @contentType: if available the Content-Type information will be
1454 * returned at that location
1455 *
1456 * This function try to fetch the indicated resource via HTTP GET
1457 * and save it's content in the file.
1458 *
1459 * Returns -1 in case of failure, 0 incase of success. The contentType,
1460 * if provided must be freed by the caller
1461 */
1462int
1463xmlNanoHTTPFetch(const char *URL, const char *filename, char **contentType) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001464 void *ctxt = NULL;
1465 char *buf = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001466 int fd;
1467 int len;
1468
1469 ctxt = xmlNanoHTTPOpen(URL, contentType);
1470 if (ctxt == NULL) return(-1);
1471
1472 if (!strcmp(filename, "-"))
1473 fd = 0;
1474 else {
1475 fd = open(filename, O_CREAT | O_WRONLY, 00644);
1476 if (fd < 0) {
1477 xmlNanoHTTPClose(ctxt);
1478 if ((contentType != NULL) && (*contentType != NULL)) {
1479 xmlFree(*contentType);
1480 *contentType = NULL;
1481 }
1482 return(-1);
1483 }
1484 }
1485
Daniel Veillardf012a642001-07-23 19:10:52 +00001486 xmlNanoHTTPFetchContent( ctxt, &buf, &len );
1487 if ( len > 0 ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001488 write(fd, buf, len);
1489 }
1490
1491 xmlNanoHTTPClose(ctxt);
1492 close(fd);
1493 return(0);
1494}
1495
1496/**
1497 * xmlNanoHTTPSave:
1498 * @ctxt: the HTTP context
1499 * @filename: the filename where the content should be saved
1500 *
1501 * This function saves the output of the HTTP transaction to a file
1502 * It closes and free the context at the end
1503 *
1504 * Returns -1 in case of failure, 0 incase of success.
1505 */
1506int
1507xmlNanoHTTPSave(void *ctxt, const char *filename) {
Daniel Veillarde3924972001-07-25 20:25:21 +00001508 char *buf = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001509 int fd;
1510 int len;
1511
1512 if (ctxt == NULL) return(-1);
1513
1514 if (!strcmp(filename, "-"))
1515 fd = 0;
1516 else {
1517 fd = open(filename, O_CREAT | O_WRONLY);
1518 if (fd < 0) {
1519 xmlNanoHTTPClose(ctxt);
1520 return(-1);
1521 }
1522 }
1523
Daniel Veillardf012a642001-07-23 19:10:52 +00001524 xmlNanoHTTPFetchContent( ctxt, &buf, &len );
1525 if ( len > 0 ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001526 write(fd, buf, len);
1527 }
1528
1529 xmlNanoHTTPClose(ctxt);
1530 return(0);
1531}
1532
1533/**
1534 * xmlNanoHTTPReturnCode:
1535 * @ctx: the HTTP context
1536 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001537 * Get the latest HTTP return code received
1538 *
Owen Taylor3473f882001-02-23 17:55:21 +00001539 * Returns the HTTP return code for the request.
1540 */
1541int
1542xmlNanoHTTPReturnCode(void *ctx) {
1543 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1544
1545 if (ctxt == NULL) return(-1);
1546
1547 return(ctxt->returnValue);
1548}
1549
1550/**
1551 * xmlNanoHTTPAuthHeader:
1552 * @ctx: the HTTP context
1553 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001554 * Get the authentication header of an HTTP context
1555 *
Owen Taylor3473f882001-02-23 17:55:21 +00001556 * Returns the stashed value of the WWW-Authenticate or Proxy-Authenticate
1557 * header.
1558 */
1559const char *
1560xmlNanoHTTPAuthHeader(void *ctx) {
1561 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1562
1563 if (ctxt == NULL) return(NULL);
1564
1565 return(ctxt->authHeader);
1566}
1567
Daniel Veillardf012a642001-07-23 19:10:52 +00001568/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00001569 * xmlNanoHTTPContentLength:
Daniel Veillardf012a642001-07-23 19:10:52 +00001570 * @ctx: the HTTP context
1571 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001572 * Provides the specified content length from the HTTP header.
1573 *
Daniel Veillardf012a642001-07-23 19:10:52 +00001574 * Return the specified content length from the HTTP header. Note that
1575 * a value of -1 indicates that the content length element was not included in
1576 * the response header.
1577 */
1578int
1579xmlNanoHTTPContentLength( void * ctx ) {
1580 xmlNanoHTTPCtxtPtr ctxt = ctx;
1581
1582 return ( ( ctxt == NULL ) ? -1 : ctxt->ContentLength );
1583}
1584
1585/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00001586 * xmlNanoHTTPFetchContent:
Daniel Veillardf012a642001-07-23 19:10:52 +00001587 * @ctx: the HTTP context
1588 * @ptr: pointer to set to the content buffer.
1589 * @len: integer pointer to hold the length of the content
1590 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001591 * Check if all the content was read
1592 *
Daniel Veillardf012a642001-07-23 19:10:52 +00001593 * Returns 0 if all the content was read and available, returns
1594 * -1 if received content length was less than specified or an error
1595 * occurred.
1596 */
1597int
1598xmlNanoHTTPFetchContent( void * ctx, char ** ptr, int * len ) {
1599 xmlNanoHTTPCtxtPtr ctxt = ctx;
1600
1601 int rc = 0;
1602 int cur_lgth;
1603 int rcvd_lgth;
1604 int dummy_int;
1605 char * dummy_ptr = NULL;
1606
1607 /* Dummy up return input parameters if not provided */
1608
1609 if ( len == NULL )
1610 len = &dummy_int;
1611
1612 if ( ptr == NULL )
1613 ptr = &dummy_ptr;
1614
1615 /* But can't work without the context pointer */
1616
1617 if ( ( ctxt == NULL ) || ( ctxt->content == NULL ) ) {
1618 *len = 0;
1619 *ptr = NULL;
1620 return ( -1 );
1621 }
1622
1623 rcvd_lgth = ctxt->inptr - ctxt->content;
1624
1625 while ( (cur_lgth = xmlNanoHTTPRecv( ctxt )) > 0 ) {
1626
1627 rcvd_lgth += cur_lgth;
1628 if ( (ctxt->ContentLength > 0) && (rcvd_lgth >= ctxt->ContentLength) )
1629 break;
1630 }
1631
1632 *ptr = ctxt->content;
1633 *len = rcvd_lgth;
1634
1635 if ( ( ctxt->ContentLength > 0 ) && ( rcvd_lgth < ctxt->ContentLength ) )
1636 rc = -1;
1637 else if ( rcvd_lgth == 0 )
1638 rc = -1;
1639
1640 return ( rc );
1641}
1642
Owen Taylor3473f882001-02-23 17:55:21 +00001643#ifdef STANDALONE
1644int main(int argc, char **argv) {
1645 char *contentType = NULL;
1646
1647 if (argv[1] != NULL) {
1648 if (argv[2] != NULL)
1649 xmlNanoHTTPFetch(argv[1], argv[2], &contentType);
1650 else
1651 xmlNanoHTTPFetch(argv[1], "-", &contentType);
1652 if (contentType != NULL) xmlFree(contentType);
1653 } else {
1654 xmlGenericError(xmlGenericErrorContext,
1655 "%s: minimal HTTP GET implementation\n", argv[0]);
1656 xmlGenericError(xmlGenericErrorContext,
1657 "\tusage %s [ URL [ filename ] ]\n", argv[0]);
1658 }
1659 xmlNanoHTTPCleanup();
1660 xmlMemoryDump();
1661 return(0);
1662}
1663#endif /* STANDALONE */
1664#else /* !LIBXML_HTTP_ENABLED */
1665#ifdef STANDALONE
1666#include <stdio.h>
1667int main(int argc, char **argv) {
1668 xmlGenericError(xmlGenericErrorContext,
1669 "%s : HTTP support not compiled in\n", argv[0]);
1670 return(0);
1671}
1672#endif /* STANDALONE */
1673#endif /* LIBXML_HTTP_ENABLED */