blob: d94f6d924b3d004bc55a0c6d33be74f8afd1f9a2 [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 ())
971 {
972#if !defined(HAVE_GETADDRINFO) && defined(RES_USE_INET6)
973 if (!(_res.options & RES_INIT))
974 res_init();
975 _res.options |= RES_USE_INET6;
976 }
977#elif defined(HAVE_GETADDRINFO)
978 int status;
979 struct addrinfo hints, *res, *result;
980
981 result = NULL;
982 memset (&hints, 0,sizeof(hints));
983 hints.ai_socktype = SOCK_STREAM;
984
985 status = getaddrinfo (host, NULL, &hints, &result);
986 if (status) {
987 xmlGenericError (xmlGenericErrorContext,
988 "xmlNanoHTTPConnectHost: %s '%s' - %s",
989 "Failed to resolve host", host, gai_strerror (status));
990
991 return (-1);
992 }
993
994 for (res = result; res; res = res->ai_next) {
995 if (res) {
996 if (res->ai_family == AF_INET6) {
997 memcpy (&sockin6, res->ai_addr, res->ai_addrlen);
998 sockin6.sin6_port = htons (port);
999 addr = (struct sockaddr *)&sockin6;
1000 }
1001
1002 if (res->ai_family == AF_INET) {
1003 memcpy (&sockin, res->ai_addr, res->ai_addrlen);
1004 sockin.sin_port = htons (port);
1005 addr = (struct sockaddr *)&sockin;
1006 }
1007
1008 s = xmlNanoHTTPConnectAttempt (addr);
1009 if (s != -1) {
1010 freeaddrinfo (result);
1011 return (s);
1012 }
1013 }
1014 else {
1015 freeaddrinfo (result);
1016 return (-1);
1017 }
1018 }
1019 } else
Owen Taylor3473f882001-02-23 17:55:21 +00001020#endif
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001021#endif
1022 {
1023 h = gethostbyname (host);
1024 if (h == NULL) {
Daniel Veillard56b2db72002-03-25 16:35:28 +00001025
1026/*
1027 * Okay, I got fed up by the non-portability of this error message
1028 * extraction code. it work on Linux, if it work on your platform
1029 * and one want to enable it, send me the defined(foobar) needed
1030 */
1031#if defined(HAVE_NETDB_H) && defined(HOST_NOT_FOUND) && defined(linux)
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001032 const char *h_err_txt = "";
Daniel Veillardf012a642001-07-23 19:10:52 +00001033
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001034 switch (h_errno) {
1035 case HOST_NOT_FOUND:
1036 h_err_txt = "Authoritive host not found";
1037 break;
Daniel Veillardf012a642001-07-23 19:10:52 +00001038
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001039 case TRY_AGAIN:
1040 h_err_txt =
1041 "Non-authoritive host not found or server failure.";
1042 break;
Daniel Veillardf012a642001-07-23 19:10:52 +00001043
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001044 case NO_RECOVERY:
1045 h_err_txt =
1046 "Non-recoverable errors: FORMERR, REFUSED, or NOTIMP.";
1047 break;
Daniel Veillard5c396542002-03-15 07:57:50 +00001048
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001049 case NO_ADDRESS:
1050 h_err_txt =
1051 "Valid name, no data record of requested type.";
1052 break;
Daniel Veillard5c396542002-03-15 07:57:50 +00001053
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001054 default:
1055 h_err_txt = "No error text defined.";
1056 break;
1057 }
1058 xmlGenericError (xmlGenericErrorContext,
1059 "xmlNanoHTTPConnectHost: %s '%s' - %s",
1060 "Failed to resolve host", host, h_err_txt);
Daniel Veillard5c396542002-03-15 07:57:50 +00001061#else
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001062 xmlGenericError (xmlGenericErrorContext,
1063 "xmlNanoHTTPConnectHost: %s '%s'",
1064 "Failed to resolve host", host);
Owen Taylor3473f882001-02-23 17:55:21 +00001065#endif
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001066 return (-1);
1067 }
Daniel Veillard5c396542002-03-15 07:57:50 +00001068
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001069 for (i = 0; h->h_addr_list[i]; i++) {
1070 if (h->h_addrtype == AF_INET) {
1071 /* A records (IPv4) */
1072 memcpy (&ia, h->h_addr_list[i], h->h_length);
1073 sockin.sin_family = h->h_addrtype;
1074 sockin.sin_addr = ia;
1075 sockin.sin_port = htons (port);
1076 addr = (struct sockaddr *) &sockin;
Daniel Veillard5c396542002-03-15 07:57:50 +00001077#ifdef SUPPORT_IP6
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001078 } else if (have_ipv6 () && (h->h_addrtype == AF_INET6)) {
1079 /* AAAA records (IPv6) */
1080 memcpy (&ia6, h->h_addr_list[i], h->h_length);
1081 sockin6.sin6_family = h->h_addrtype;
1082 sockin6.sin6_addr = ia6;
1083 sockin6.sin6_port = htons (port);
1084 addr = (struct sockaddr *) &sockin6;
Daniel Veillard5c396542002-03-15 07:57:50 +00001085#endif
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001086 } else
1087 break; /* for */
Daniel Veillard5c396542002-03-15 07:57:50 +00001088
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001089 s = xmlNanoHTTPConnectAttempt (addr);
1090 if (s != -1)
1091 return (s);
1092 }
Owen Taylor3473f882001-02-23 17:55:21 +00001093 }
Owen Taylor3473f882001-02-23 17:55:21 +00001094#ifdef DEBUG_HTTP
1095 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard5c396542002-03-15 07:57:50 +00001096 "xmlNanoHTTPConnectHost: unable to connect to '%s'.\n",
1097 host);
Owen Taylor3473f882001-02-23 17:55:21 +00001098#endif
Daniel Veillard5c396542002-03-15 07:57:50 +00001099 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001100}
1101
1102
1103/**
1104 * xmlNanoHTTPOpen:
1105 * @URL: The URL to load
1106 * @contentType: if available the Content-Type information will be
1107 * returned at that location
1108 *
1109 * This function try to open a connection to the indicated resource
1110 * via HTTP GET.
1111 *
1112 * Returns NULL in case of failure, otherwise a request handler.
1113 * The contentType, if provided must be freed by the caller
1114 */
1115
1116void*
1117xmlNanoHTTPOpen(const char *URL, char **contentType) {
1118 if (contentType != NULL) *contentType = NULL;
Daniel Veillardf012a642001-07-23 19:10:52 +00001119 return(xmlNanoHTTPMethod(URL, NULL, NULL, contentType, NULL, 0));
Daniel Veillard9403a042001-05-28 11:00:53 +00001120}
1121
1122/**
1123 * xmlNanoHTTPOpenRedir:
1124 * @URL: The URL to load
1125 * @contentType: if available the Content-Type information will be
1126 * returned at that location
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001127 * @redir: if available the redirected URL will be returned
Daniel Veillard9403a042001-05-28 11:00:53 +00001128 *
1129 * This function try to open a connection to the indicated resource
1130 * via HTTP GET.
1131 *
1132 * Returns NULL in case of failure, otherwise a request handler.
1133 * The contentType, if provided must be freed by the caller
1134 */
1135
1136void*
1137xmlNanoHTTPOpenRedir(const char *URL, char **contentType, char **redir) {
1138 if (contentType != NULL) *contentType = NULL;
1139 if (redir != NULL) *redir = NULL;
Daniel Veillardf012a642001-07-23 19:10:52 +00001140 return(xmlNanoHTTPMethodRedir(URL, NULL, NULL, contentType, redir, NULL,0));
Owen Taylor3473f882001-02-23 17:55:21 +00001141}
1142
1143/**
1144 * xmlNanoHTTPRead:
1145 * @ctx: the HTTP context
1146 * @dest: a buffer
1147 * @len: the buffer length
1148 *
1149 * This function tries to read @len bytes from the existing HTTP connection
1150 * and saves them in @dest. This is a blocking call.
1151 *
1152 * Returns the number of byte read. 0 is an indication of an end of connection.
1153 * -1 indicates a parameter error.
1154 */
1155int
1156xmlNanoHTTPRead(void *ctx, void *dest, int len) {
1157 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1158
1159 if (ctx == NULL) return(-1);
1160 if (dest == NULL) return(-1);
1161 if (len <= 0) return(0);
1162
1163 while (ctxt->inptr - ctxt->inrptr < len) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001164 if (xmlNanoHTTPRecv(ctxt) <= 0) break;
Owen Taylor3473f882001-02-23 17:55:21 +00001165 }
1166 if (ctxt->inptr - ctxt->inrptr < len)
1167 len = ctxt->inptr - ctxt->inrptr;
1168 memcpy(dest, ctxt->inrptr, len);
1169 ctxt->inrptr += len;
1170 return(len);
1171}
1172
1173/**
1174 * xmlNanoHTTPClose:
1175 * @ctx: the HTTP context
1176 *
1177 * This function closes an HTTP context, it ends up the connection and
1178 * free all data related to it.
1179 */
1180void
1181xmlNanoHTTPClose(void *ctx) {
1182 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1183
1184 if (ctx == NULL) return;
1185
1186 xmlNanoHTTPFreeCtxt(ctxt);
1187}
1188
1189/**
Daniel Veillard9403a042001-05-28 11:00:53 +00001190 * xmlNanoHTTPMethodRedir:
Owen Taylor3473f882001-02-23 17:55:21 +00001191 * @URL: The URL to load
1192 * @method: the HTTP method to use
1193 * @input: the input string if any
1194 * @contentType: the Content-Type information IN and OUT
Daniel Veillard9403a042001-05-28 11:00:53 +00001195 * @redir: the redirected URL OUT
Owen Taylor3473f882001-02-23 17:55:21 +00001196 * @headers: the extra headers
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001197 * @ilen: input length
Owen Taylor3473f882001-02-23 17:55:21 +00001198 *
1199 * This function try to open a connection to the indicated resource
1200 * via HTTP using the given @method, adding the given extra headers
1201 * and the input buffer for the request content.
1202 *
1203 * Returns NULL in case of failure, otherwise a request handler.
Daniel Veillard9403a042001-05-28 11:00:53 +00001204 * The contentType, or redir, if provided must be freed by the caller
Owen Taylor3473f882001-02-23 17:55:21 +00001205 */
1206
1207void*
Daniel Veillard9403a042001-05-28 11:00:53 +00001208xmlNanoHTTPMethodRedir(const char *URL, const char *method, const char *input,
Daniel Veillardf012a642001-07-23 19:10:52 +00001209 char **contentType, char **redir,
1210 const char *headers, int ilen ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001211 xmlNanoHTTPCtxtPtr ctxt;
1212 char *bp, *p;
Daniel Veillardf012a642001-07-23 19:10:52 +00001213 int blen, ret;
Owen Taylor3473f882001-02-23 17:55:21 +00001214 int head;
Daniel Veillardf012a642001-07-23 19:10:52 +00001215 int xmt_bytes;
Owen Taylor3473f882001-02-23 17:55:21 +00001216 int nbRedirects = 0;
1217 char *redirURL = NULL;
1218
1219 if (URL == NULL) return(NULL);
1220 if (method == NULL) method = "GET";
1221 xmlNanoHTTPInit();
1222
1223retry:
1224 if (redirURL == NULL)
1225 ctxt = xmlNanoHTTPNewCtxt(URL);
1226 else {
1227 ctxt = xmlNanoHTTPNewCtxt(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001228 }
1229
Daniel Veillardf012a642001-07-23 19:10:52 +00001230 if ( ctxt == NULL ) {
1231 xmlGenericError( xmlGenericErrorContext,
1232 "xmlNanoHTTPMethodRedir: %s %s.",
1233 "Unable to allocate HTTP context to URI",
1234 ( ( redirURL == NULL ) ? URL : redirURL ) );
1235 return ( NULL );
1236 }
1237
Owen Taylor3473f882001-02-23 17:55:21 +00001238 if ((ctxt->protocol == NULL) || (strcmp(ctxt->protocol, "http"))) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001239 xmlGenericError( xmlGenericErrorContext,
1240 "xmlNanoHTTPMethodRedir: %s - %s.",
1241 "Not a valid HTTP URI",
1242 ( ( redirURL == NULL ) ? URL : redirURL ) );
Owen Taylor3473f882001-02-23 17:55:21 +00001243 xmlNanoHTTPFreeCtxt(ctxt);
1244 if (redirURL != NULL) xmlFree(redirURL);
1245 return(NULL);
1246 }
1247 if (ctxt->hostname == NULL) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001248 xmlGenericError( xmlGenericErrorContext,
1249 "xmlNanoHTTPMethodRedir: %s - %s",
1250 "Failed to identify host in URI",
1251 ( ( redirURL == NULL ) ? URL : redirURL ) );
Owen Taylor3473f882001-02-23 17:55:21 +00001252 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001253 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001254 return(NULL);
1255 }
1256 if (proxy) {
1257 blen = strlen(ctxt->hostname) * 2 + 16;
1258 ret = xmlNanoHTTPConnectHost(proxy, proxyPort);
1259 }
1260 else {
1261 blen = strlen(ctxt->hostname);
1262 ret = xmlNanoHTTPConnectHost(ctxt->hostname, ctxt->port);
1263 }
1264 if (ret < 0) {
1265 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001266 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001267 return(NULL);
1268 }
1269 ctxt->fd = ret;
1270
Daniel Veillardf012a642001-07-23 19:10:52 +00001271 if (input == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00001272 ilen = 0;
Daniel Veillardf012a642001-07-23 19:10:52 +00001273 else
1274 blen += 36;
1275
Owen Taylor3473f882001-02-23 17:55:21 +00001276 if (headers != NULL)
Daniel Veillardf012a642001-07-23 19:10:52 +00001277 blen += strlen(headers) + 2;
Owen Taylor3473f882001-02-23 17:55:21 +00001278 if (contentType && *contentType)
1279 blen += strlen(*contentType) + 16;
Daniel Veillardf012a642001-07-23 19:10:52 +00001280 blen += strlen(method) + strlen(ctxt->path) + 24;
Daniel Veillard3c908dc2003-04-19 00:07:51 +00001281 bp = xmlMallocAtomic(blen);
Daniel Veillardf012a642001-07-23 19:10:52 +00001282 if ( bp == NULL ) {
1283 xmlNanoHTTPFreeCtxt( ctxt );
1284 xmlGenericError( xmlGenericErrorContext,
1285 "xmlNanoHTTPMethodRedir: %s",
1286 "Error allocating HTTP header buffer." );
1287 return ( NULL );
1288 }
1289
1290 p = bp;
1291
Owen Taylor3473f882001-02-23 17:55:21 +00001292 if (proxy) {
1293 if (ctxt->port != 80) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001294 p += snprintf( p, blen - (p - bp), "%s http://%s:%d%s",
1295 method, ctxt->hostname,
Daniel Veillardf012a642001-07-23 19:10:52 +00001296 ctxt->port, ctxt->path );
Owen Taylor3473f882001-02-23 17:55:21 +00001297 }
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001298 else
1299 p += snprintf( p, blen - (p - bp), "%s http://%s%s", method,
Daniel Veillardf012a642001-07-23 19:10:52 +00001300 ctxt->hostname, ctxt->path);
Owen Taylor3473f882001-02-23 17:55:21 +00001301 }
1302 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001303 p += snprintf( p, blen - (p - bp), "%s %s", method, ctxt->path);
Daniel Veillardf012a642001-07-23 19:10:52 +00001304
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001305 p += snprintf( p, blen - (p - bp), " HTTP/1.0\r\nHost: %s\r\n",
1306 ctxt->hostname);
Daniel Veillardf012a642001-07-23 19:10:52 +00001307
1308 if (contentType != NULL && *contentType)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001309 p += snprintf(p, blen - (p - bp), "Content-Type: %s\r\n", *contentType);
Daniel Veillardf012a642001-07-23 19:10:52 +00001310
1311 if (headers != NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001312 p += snprintf( p, blen - (p - bp), "%s", headers );
Daniel Veillardf012a642001-07-23 19:10:52 +00001313
Owen Taylor3473f882001-02-23 17:55:21 +00001314 if (input != NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001315 snprintf(p, blen - (p - bp), "Content-Length: %d\r\n\r\n", ilen );
Owen Taylor3473f882001-02-23 17:55:21 +00001316 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001317 snprintf(p, blen - (p - bp), "\r\n");
Daniel Veillardf012a642001-07-23 19:10:52 +00001318
Owen Taylor3473f882001-02-23 17:55:21 +00001319#ifdef DEBUG_HTTP
1320 xmlGenericError(xmlGenericErrorContext,
1321 "-> %s%s", proxy? "(Proxy) " : "", bp);
1322 if ((blen -= strlen(bp)+1) < 0)
1323 xmlGenericError(xmlGenericErrorContext,
1324 "ERROR: overflowed buffer by %d bytes\n", -blen);
1325#endif
1326 ctxt->outptr = ctxt->out = bp;
1327 ctxt->state = XML_NANO_HTTP_WRITE;
Daniel Veillardf012a642001-07-23 19:10:52 +00001328 blen = strlen( ctxt->out );
1329 xmt_bytes = xmlNanoHTTPSend(ctxt, ctxt->out, blen );
1330#ifdef DEBUG_HTTP
1331 if ( xmt_bytes != blen )
1332 xmlGenericError( xmlGenericErrorContext,
1333 "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
1334 xmt_bytes, blen,
1335 "bytes of HTTP headers sent to host",
1336 ctxt->hostname );
1337#endif
1338
1339 if ( input != NULL ) {
1340 xmt_bytes = xmlNanoHTTPSend( ctxt, input, ilen );
1341
1342#ifdef DEBUG_HTTP
1343 if ( xmt_bytes != ilen )
1344 xmlGenericError( xmlGenericErrorContext,
1345 "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
1346 xmt_bytes, ilen,
1347 "bytes of HTTP content sent to host",
1348 ctxt->hostname );
1349#endif
1350 }
1351
Owen Taylor3473f882001-02-23 17:55:21 +00001352 ctxt->state = XML_NANO_HTTP_READ;
1353 head = 1;
1354
1355 while ((p = xmlNanoHTTPReadLine(ctxt)) != NULL) {
1356 if (head && (*p == 0)) {
1357 head = 0;
1358 ctxt->content = ctxt->inrptr;
1359 xmlFree(p);
1360 break;
1361 }
1362 xmlNanoHTTPScanAnswer(ctxt, p);
1363
1364#ifdef DEBUG_HTTP
1365 xmlGenericError(xmlGenericErrorContext, "<- %s\n", p);
1366#endif
1367 xmlFree(p);
1368 }
1369
1370 if ((ctxt->location != NULL) && (ctxt->returnValue >= 300) &&
1371 (ctxt->returnValue < 400)) {
1372#ifdef DEBUG_HTTP
1373 xmlGenericError(xmlGenericErrorContext,
1374 "\nRedirect to: %s\n", ctxt->location);
1375#endif
Daniel Veillardf012a642001-07-23 19:10:52 +00001376 while ( xmlNanoHTTPRecv(ctxt) > 0 ) ;
Owen Taylor3473f882001-02-23 17:55:21 +00001377 if (nbRedirects < XML_NANO_HTTP_MAX_REDIR) {
1378 nbRedirects++;
Daniel Veillard9403a042001-05-28 11:00:53 +00001379 if (redirURL != NULL)
1380 xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001381 redirURL = xmlMemStrdup(ctxt->location);
1382 xmlNanoHTTPFreeCtxt(ctxt);
1383 goto retry;
1384 }
1385 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001386 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001387#ifdef DEBUG_HTTP
1388 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardf012a642001-07-23 19:10:52 +00001389 "xmlNanoHTTPMethodRedir: Too many redirects, aborting ...\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001390#endif
1391 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001392 }
1393
1394 if (contentType != NULL) {
1395 if (ctxt->contentType != NULL)
1396 *contentType = xmlMemStrdup(ctxt->contentType);
1397 else
1398 *contentType = NULL;
1399 }
1400
Daniel Veillard9403a042001-05-28 11:00:53 +00001401 if ((redir != NULL) && (redirURL != NULL)) {
1402 *redir = redirURL;
1403 } else {
1404 if (redirURL != NULL)
1405 xmlFree(redirURL);
1406 if (redir != NULL)
1407 *redir = NULL;
1408 }
1409
Owen Taylor3473f882001-02-23 17:55:21 +00001410#ifdef DEBUG_HTTP
1411 if (ctxt->contentType != NULL)
1412 xmlGenericError(xmlGenericErrorContext,
1413 "\nCode %d, content-type '%s'\n\n",
1414 ctxt->returnValue, ctxt->contentType);
1415 else
1416 xmlGenericError(xmlGenericErrorContext,
1417 "\nCode %d, no content-type\n\n",
1418 ctxt->returnValue);
1419#endif
1420
1421 return((void *) ctxt);
1422}
1423
1424/**
Daniel Veillard9403a042001-05-28 11:00:53 +00001425 * xmlNanoHTTPMethod:
1426 * @URL: The URL to load
1427 * @method: the HTTP method to use
1428 * @input: the input string if any
1429 * @contentType: the Content-Type information IN and OUT
1430 * @headers: the extra headers
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001431 * @ilen: input length
Daniel Veillard9403a042001-05-28 11:00:53 +00001432 *
1433 * This function try to open a connection to the indicated resource
1434 * via HTTP using the given @method, adding the given extra headers
1435 * and the input buffer for the request content.
1436 *
1437 * Returns NULL in case of failure, otherwise a request handler.
1438 * The contentType, if provided must be freed by the caller
1439 */
1440
1441void*
1442xmlNanoHTTPMethod(const char *URL, const char *method, const char *input,
Daniel Veillardf012a642001-07-23 19:10:52 +00001443 char **contentType, const char *headers, int ilen) {
Daniel Veillard9403a042001-05-28 11:00:53 +00001444 return(xmlNanoHTTPMethodRedir(URL, method, input, contentType,
Daniel Veillardf012a642001-07-23 19:10:52 +00001445 NULL, headers, ilen));
Daniel Veillard9403a042001-05-28 11:00:53 +00001446}
1447
1448/**
Owen Taylor3473f882001-02-23 17:55:21 +00001449 * xmlNanoHTTPFetch:
1450 * @URL: The URL to load
1451 * @filename: the filename where the content should be saved
1452 * @contentType: if available the Content-Type information will be
1453 * returned at that location
1454 *
1455 * This function try to fetch the indicated resource via HTTP GET
1456 * and save it's content in the file.
1457 *
1458 * Returns -1 in case of failure, 0 incase of success. The contentType,
1459 * if provided must be freed by the caller
1460 */
1461int
1462xmlNanoHTTPFetch(const char *URL, const char *filename, char **contentType) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001463 void *ctxt = NULL;
1464 char *buf = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001465 int fd;
1466 int len;
1467
1468 ctxt = xmlNanoHTTPOpen(URL, contentType);
1469 if (ctxt == NULL) return(-1);
1470
1471 if (!strcmp(filename, "-"))
1472 fd = 0;
1473 else {
1474 fd = open(filename, O_CREAT | O_WRONLY, 00644);
1475 if (fd < 0) {
1476 xmlNanoHTTPClose(ctxt);
1477 if ((contentType != NULL) && (*contentType != NULL)) {
1478 xmlFree(*contentType);
1479 *contentType = NULL;
1480 }
1481 return(-1);
1482 }
1483 }
1484
Daniel Veillardf012a642001-07-23 19:10:52 +00001485 xmlNanoHTTPFetchContent( ctxt, &buf, &len );
1486 if ( len > 0 ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001487 write(fd, buf, len);
1488 }
1489
1490 xmlNanoHTTPClose(ctxt);
1491 close(fd);
1492 return(0);
1493}
1494
1495/**
1496 * xmlNanoHTTPSave:
1497 * @ctxt: the HTTP context
1498 * @filename: the filename where the content should be saved
1499 *
1500 * This function saves the output of the HTTP transaction to a file
1501 * It closes and free the context at the end
1502 *
1503 * Returns -1 in case of failure, 0 incase of success.
1504 */
1505int
1506xmlNanoHTTPSave(void *ctxt, const char *filename) {
Daniel Veillarde3924972001-07-25 20:25:21 +00001507 char *buf = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001508 int fd;
1509 int len;
1510
1511 if (ctxt == NULL) return(-1);
1512
1513 if (!strcmp(filename, "-"))
1514 fd = 0;
1515 else {
1516 fd = open(filename, O_CREAT | O_WRONLY);
1517 if (fd < 0) {
1518 xmlNanoHTTPClose(ctxt);
1519 return(-1);
1520 }
1521 }
1522
Daniel Veillardf012a642001-07-23 19:10:52 +00001523 xmlNanoHTTPFetchContent( ctxt, &buf, &len );
1524 if ( len > 0 ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001525 write(fd, buf, len);
1526 }
1527
1528 xmlNanoHTTPClose(ctxt);
1529 return(0);
1530}
1531
1532/**
1533 * xmlNanoHTTPReturnCode:
1534 * @ctx: the HTTP context
1535 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001536 * Get the latest HTTP return code received
1537 *
Owen Taylor3473f882001-02-23 17:55:21 +00001538 * Returns the HTTP return code for the request.
1539 */
1540int
1541xmlNanoHTTPReturnCode(void *ctx) {
1542 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1543
1544 if (ctxt == NULL) return(-1);
1545
1546 return(ctxt->returnValue);
1547}
1548
1549/**
1550 * xmlNanoHTTPAuthHeader:
1551 * @ctx: the HTTP context
1552 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001553 * Get the authentication header of an HTTP context
1554 *
Owen Taylor3473f882001-02-23 17:55:21 +00001555 * Returns the stashed value of the WWW-Authenticate or Proxy-Authenticate
1556 * header.
1557 */
1558const char *
1559xmlNanoHTTPAuthHeader(void *ctx) {
1560 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1561
1562 if (ctxt == NULL) return(NULL);
1563
1564 return(ctxt->authHeader);
1565}
1566
Daniel Veillardf012a642001-07-23 19:10:52 +00001567/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00001568 * xmlNanoHTTPContentLength:
Daniel Veillardf012a642001-07-23 19:10:52 +00001569 * @ctx: the HTTP context
1570 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001571 * Provides the specified content length from the HTTP header.
1572 *
Daniel Veillardf012a642001-07-23 19:10:52 +00001573 * Return the specified content length from the HTTP header. Note that
1574 * a value of -1 indicates that the content length element was not included in
1575 * the response header.
1576 */
1577int
1578xmlNanoHTTPContentLength( void * ctx ) {
1579 xmlNanoHTTPCtxtPtr ctxt = ctx;
1580
1581 return ( ( ctxt == NULL ) ? -1 : ctxt->ContentLength );
1582}
1583
1584/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00001585 * xmlNanoHTTPFetchContent:
Daniel Veillardf012a642001-07-23 19:10:52 +00001586 * @ctx: the HTTP context
1587 * @ptr: pointer to set to the content buffer.
1588 * @len: integer pointer to hold the length of the content
1589 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001590 * Check if all the content was read
1591 *
Daniel Veillardf012a642001-07-23 19:10:52 +00001592 * Returns 0 if all the content was read and available, returns
1593 * -1 if received content length was less than specified or an error
1594 * occurred.
1595 */
1596int
1597xmlNanoHTTPFetchContent( void * ctx, char ** ptr, int * len ) {
1598 xmlNanoHTTPCtxtPtr ctxt = ctx;
1599
1600 int rc = 0;
1601 int cur_lgth;
1602 int rcvd_lgth;
1603 int dummy_int;
1604 char * dummy_ptr = NULL;
1605
1606 /* Dummy up return input parameters if not provided */
1607
1608 if ( len == NULL )
1609 len = &dummy_int;
1610
1611 if ( ptr == NULL )
1612 ptr = &dummy_ptr;
1613
1614 /* But can't work without the context pointer */
1615
1616 if ( ( ctxt == NULL ) || ( ctxt->content == NULL ) ) {
1617 *len = 0;
1618 *ptr = NULL;
1619 return ( -1 );
1620 }
1621
1622 rcvd_lgth = ctxt->inptr - ctxt->content;
1623
1624 while ( (cur_lgth = xmlNanoHTTPRecv( ctxt )) > 0 ) {
1625
1626 rcvd_lgth += cur_lgth;
1627 if ( (ctxt->ContentLength > 0) && (rcvd_lgth >= ctxt->ContentLength) )
1628 break;
1629 }
1630
1631 *ptr = ctxt->content;
1632 *len = rcvd_lgth;
1633
1634 if ( ( ctxt->ContentLength > 0 ) && ( rcvd_lgth < ctxt->ContentLength ) )
1635 rc = -1;
1636 else if ( rcvd_lgth == 0 )
1637 rc = -1;
1638
1639 return ( rc );
1640}
1641
Owen Taylor3473f882001-02-23 17:55:21 +00001642#ifdef STANDALONE
1643int main(int argc, char **argv) {
1644 char *contentType = NULL;
1645
1646 if (argv[1] != NULL) {
1647 if (argv[2] != NULL)
1648 xmlNanoHTTPFetch(argv[1], argv[2], &contentType);
1649 else
1650 xmlNanoHTTPFetch(argv[1], "-", &contentType);
1651 if (contentType != NULL) xmlFree(contentType);
1652 } else {
1653 xmlGenericError(xmlGenericErrorContext,
1654 "%s: minimal HTTP GET implementation\n", argv[0]);
1655 xmlGenericError(xmlGenericErrorContext,
1656 "\tusage %s [ URL [ filename ] ]\n", argv[0]);
1657 }
1658 xmlNanoHTTPCleanup();
1659 xmlMemoryDump();
1660 return(0);
1661}
1662#endif /* STANDALONE */
1663#else /* !LIBXML_HTTP_ENABLED */
1664#ifdef STANDALONE
1665#include <stdio.h>
1666int main(int argc, char **argv) {
1667 xmlGenericError(xmlGenericErrorContext,
1668 "%s : HTTP support not compiled in\n", argv[0]);
1669 return(0);
1670}
1671#endif /* STANDALONE */
1672#endif /* LIBXML_HTTP_ENABLED */