blob: a3b4661e0b5567c45f5345b00435b3e0e294e7bf [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
Daniel Veillard75eb1ad2003-07-07 14:42:44 +000030#ifdef HAVE_SYS_TYPES_H
31#include <sys/types.h>
32#endif
Owen Taylor3473f882001-02-23 17:55:21 +000033#ifdef HAVE_SYS_SOCKET_H
34#include <sys/socket.h>
35#endif
36#ifdef HAVE_NETINET_IN_H
37#include <netinet/in.h>
38#endif
39#ifdef HAVE_ARPA_INET_H
40#include <arpa/inet.h>
41#endif
42#ifdef HAVE_NETDB_H
43#include <netdb.h>
44#endif
Daniel Veillardd85f4f42002-03-25 10:48:46 +000045#ifdef HAVE_RESOLV_H
Daniel Veillard9b731d72002-04-14 12:56:08 +000046#ifdef HAVE_ARPA_NAMESER_H
47#include <arpa/nameser.h>
48#endif
Daniel Veillardd85f4f42002-03-25 10:48:46 +000049#include <resolv.h>
50#endif
Owen Taylor3473f882001-02-23 17:55:21 +000051#ifdef HAVE_FCNTL_H
52#include <fcntl.h>
53#endif
54#ifdef HAVE_ERRNO_H
55#include <errno.h>
56#endif
57#ifdef HAVE_SYS_TIME_H
58#include <sys/time.h>
59#endif
60#ifdef HAVE_SYS_SELECT_H
61#include <sys/select.h>
62#endif
63#ifdef HAVE_STRINGS_H
64#include <strings.h>
65#endif
66#ifdef SUPPORT_IP6
67#include <resolv.h>
68#endif
69
70#ifdef VMS
71#include <stropts>
72#define SOCKLEN_T unsigned int
73#define SOCKET int
74#endif
75
Daniel Veillardd0463562001-10-13 09:15:48 +000076#include <libxml/globals.h>
Daniel Veillardf012a642001-07-23 19:10:52 +000077#include <libxml/xmlerror.h>
Owen Taylor3473f882001-02-23 17:55:21 +000078#include <libxml/xmlmemory.h>
79#include <libxml/parser.h> /* for xmlStr(n)casecmp() */
80#include <libxml/nanohttp.h>
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000081#include <libxml/globals.h>
Daniel Veillard8efff672002-12-04 11:44:48 +000082#include <libxml/uri.h>
Owen Taylor3473f882001-02-23 17:55:21 +000083
84/**
85 * A couple portability macros
86 */
87#ifndef _WINSOCKAPI_
88#define closesocket(s) close(s)
89#define SOCKET int
90#endif
91
Daniel Veillard75be0132002-03-13 10:03:35 +000092#ifndef SOCKLEN_T
93#define SOCKLEN_T unsigned int
94#endif
95#ifndef SOCKET
96#define SOCKET int
97#endif
Daniel Veillardf012a642001-07-23 19:10:52 +000098
Owen Taylor3473f882001-02-23 17:55:21 +000099#ifdef STANDALONE
100#define DEBUG_HTTP
101#define xmlStrncasecmp(a, b, n) strncasecmp((char *)a, (char *)b, n)
102#define xmlStrcasecmpi(a, b) strcasecmp((char *)a, (char *)b)
103#endif
104
105#define XML_NANO_HTTP_MAX_REDIR 10
106
107#define XML_NANO_HTTP_CHUNK 4096
108
109#define XML_NANO_HTTP_CLOSED 0
110#define XML_NANO_HTTP_WRITE 1
111#define XML_NANO_HTTP_READ 2
112#define XML_NANO_HTTP_NONE 4
113
114typedef struct xmlNanoHTTPCtxt {
115 char *protocol; /* the protocol name */
116 char *hostname; /* the host name */
117 int port; /* the port */
118 char *path; /* the path within the URL */
119 SOCKET fd; /* the file descriptor for the socket */
120 int state; /* WRITE / READ / CLOSED */
121 char *out; /* buffer sent (zero terminated) */
122 char *outptr; /* index within the buffer sent */
123 char *in; /* the receiving buffer */
124 char *content; /* the start of the content */
125 char *inptr; /* the next byte to read from network */
126 char *inrptr; /* the next byte to give back to the client */
127 int inlen; /* len of the input buffer */
128 int last; /* return code for last operation */
129 int returnValue; /* the protocol return value */
Daniel Veillardf012a642001-07-23 19:10:52 +0000130 int ContentLength; /* specified content length from HTTP header */
Owen Taylor3473f882001-02-23 17:55:21 +0000131 char *contentType; /* the MIME type for the input */
132 char *location; /* the new URL in case of redirect */
133 char *authHeader; /* contents of {WWW,Proxy}-Authenticate header */
134} xmlNanoHTTPCtxt, *xmlNanoHTTPCtxtPtr;
135
136static int initialized = 0;
137static char *proxy = NULL; /* the proxy name if any */
138static int proxyPort; /* the proxy port if any */
139static unsigned int timeout = 60;/* the select() timeout in seconds */
140
Daniel Veillardf012a642001-07-23 19:10:52 +0000141int xmlNanoHTTPFetchContent( void * ctx, char ** ptr, int * len );
142int xmlNanoHTTPContentLength( void * ctx );
143
Owen Taylor3473f882001-02-23 17:55:21 +0000144/**
145 * A portability function
146 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000147static int socket_errno(void) {
Owen Taylor3473f882001-02-23 17:55:21 +0000148#ifdef _WINSOCKAPI_
149 return(WSAGetLastError());
150#else
151 return(errno);
152#endif
153}
154
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000155#ifdef SUPPORT_IP6
Daniel Veillard2db8c122003-07-08 12:16:59 +0000156static
157int have_ipv6(void) {
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000158 int s;
159
160 s = socket (AF_INET6, SOCK_STREAM, 0);
161 if (s != -1) {
162 close (s);
163 return (1);
164 }
165 return (0);
166}
167#endif
168
Owen Taylor3473f882001-02-23 17:55:21 +0000169/**
170 * xmlNanoHTTPInit:
171 *
172 * Initialize the HTTP protocol layer.
173 * Currently it just checks for proxy informations
174 */
175
176void
177xmlNanoHTTPInit(void) {
178 const char *env;
179#ifdef _WINSOCKAPI_
180 WSADATA wsaData;
181#endif
182
183 if (initialized)
184 return;
185
186#ifdef _WINSOCKAPI_
187 if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0)
188 return;
189#endif
190
191 if (proxy == NULL) {
192 proxyPort = 80;
193 env = getenv("no_proxy");
194 if (env != NULL)
195 goto done;
196 env = getenv("http_proxy");
197 if (env != NULL) {
198 xmlNanoHTTPScanProxy(env);
199 goto done;
200 }
201 env = getenv("HTTP_PROXY");
202 if (env != NULL) {
203 xmlNanoHTTPScanProxy(env);
204 goto done;
205 }
206 }
207done:
208 initialized = 1;
209}
210
211/**
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000212 * xmlNanoHTTPCleanup:
Owen Taylor3473f882001-02-23 17:55:21 +0000213 *
214 * Cleanup the HTTP protocol layer.
215 */
216
217void
218xmlNanoHTTPCleanup(void) {
219 if (proxy != NULL)
220 xmlFree(proxy);
221#ifdef _WINSOCKAPI_
222 if (initialized)
223 WSACleanup();
224#endif
225 initialized = 0;
226 return;
227}
228
229/**
Owen Taylor3473f882001-02-23 17:55:21 +0000230 * xmlNanoHTTPScanURL:
231 * @ctxt: an HTTP context
232 * @URL: The URL used to initialize the context
233 *
234 * (Re)Initialize an HTTP context by parsing the URL and finding
235 * the protocol host port and path it indicates.
236 */
237
238static void
239xmlNanoHTTPScanURL(xmlNanoHTTPCtxtPtr ctxt, const char *URL) {
240 const char *cur = URL;
241 char buf[4096];
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000242 int indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000243 int port = 0;
244
245 if (ctxt->protocol != NULL) {
246 xmlFree(ctxt->protocol);
247 ctxt->protocol = NULL;
248 }
249 if (ctxt->hostname != NULL) {
250 xmlFree(ctxt->hostname);
251 ctxt->hostname = NULL;
252 }
253 if (ctxt->path != NULL) {
254 xmlFree(ctxt->path);
255 ctxt->path = NULL;
256 }
257 if (URL == NULL) return;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000258 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000259 while (*cur != 0) {
260 if ((cur[0] == ':') && (cur[1] == '/') && (cur[2] == '/')) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000261 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000262 ctxt->protocol = xmlMemStrdup(buf);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000263 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000264 cur += 3;
265 break;
266 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000267 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000268 }
269 if (*cur == 0) return;
270
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000271 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000272 while (1) {
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000273 if ((strchr (cur, '[') && !strchr (cur, ']')) ||
274 (!strchr (cur, '[') && strchr (cur, ']'))) {
275 xmlGenericError (xmlGenericErrorContext, "\nxmlNanoHTTPScanURL: %s",
276 "Syntax Error\n");
277 return;
278 }
279
280 if (cur[0] == '[') {
281 cur++;
282 while (cur[0] != ']')
283 buf[indx++] = *cur++;
284
285 if (!strchr (buf, ':')) {
286 xmlGenericError (xmlGenericErrorContext, "\nxmlNanoHTTPScanURL: %s",
287 "Use [IPv6]/IPv4 format\n");
288 return;
289 }
290
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000291 buf[indx] = 0;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000292 ctxt->hostname = xmlMemStrdup (buf);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000293 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000294 cur += 1;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000295 if (cur[0] == ':') {
Owen Taylor3473f882001-02-23 17:55:21 +0000296 cur++;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000297 while (*cur >= '0' && *cur <= '9') {
298 port *= 10;
299 port += *cur - '0';
300 cur++;
301 }
302
303 if (port != 0) ctxt->port = port;
304 while ((cur[0] != '/') && (*cur != 0))
305 cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000306 }
Owen Taylor3473f882001-02-23 17:55:21 +0000307 break;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000308 }
309 else {
310 if (cur[0] == ':') {
311 buf[indx] = 0;
312 ctxt->hostname = xmlMemStrdup (buf);
313 indx = 0;
314 cur += 1;
315 while ((*cur >= '0') && (*cur <= '9')) {
316 port *= 10;
317 port += *cur - '0';
318 cur++;
319 }
320 if (port != 0) ctxt->port = port;
321 while ((cur[0] != '/') && (*cur != 0))
322 cur++;
323 break;
324 }
325 if ((*cur == '/') || (*cur == 0)) {
326 buf[indx] = 0;
327 ctxt->hostname = xmlMemStrdup (buf);
328 indx = 0;
329 break;
330 }
Owen Taylor3473f882001-02-23 17:55:21 +0000331 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000332 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000333 }
334 if (*cur == 0)
335 ctxt->path = xmlMemStrdup("/");
336 else {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000337 indx = 0;
338 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000339 while (*cur != 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000340 buf[indx++] = *cur++;
341 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000342 ctxt->path = xmlMemStrdup(buf);
343 }
344}
345
346/**
347 * xmlNanoHTTPScanProxy:
348 * @URL: The proxy URL used to initialize the proxy context
349 *
350 * (Re)Initialize the HTTP Proxy context by parsing the URL and finding
351 * the protocol host port it indicates.
352 * Should be like http://myproxy/ or http://myproxy:3128/
353 * A NULL URL cleans up proxy informations.
354 */
355
356void
357xmlNanoHTTPScanProxy(const char *URL) {
358 const char *cur = URL;
359 char buf[4096];
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000360 int indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000361 int port = 0;
362
363 if (proxy != NULL) {
364 xmlFree(proxy);
365 proxy = NULL;
366 }
367 if (proxyPort != 0) {
368 proxyPort = 0;
369 }
370#ifdef DEBUG_HTTP
371 if (URL == NULL)
372 xmlGenericError(xmlGenericErrorContext,
373 "Removing HTTP proxy info\n");
374 else
375 xmlGenericError(xmlGenericErrorContext,
376 "Using HTTP proxy %s\n", URL);
377#endif
378 if (URL == NULL) return;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000379 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000380 while (*cur != 0) {
381 if ((cur[0] == ':') && (cur[1] == '/') && (cur[2] == '/')) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000382 buf[indx] = 0;
383 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000384 cur += 3;
385 break;
386 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000387 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000388 }
389 if (*cur == 0) return;
390
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000391 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000392 while (1) {
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000393 if ((strchr (cur, '[') && !strchr (cur, ']')) ||
394 (!strchr (cur, '[') && strchr (cur, ']'))) {
395 xmlGenericError (xmlGenericErrorContext, "\nxmlNanoHTTPScanProxy: %s",
396 "Syntax error\n");
397 return;
398 }
399
400 if (cur[0] == '[') {
401 cur++;
402 while (cur[0] != ']')
403 buf[indx++] = *cur++;
404
405 if (!strchr (buf, ':')) {
406 xmlGenericError (xmlGenericErrorContext, "\nxmlNanoHTTPScanProxy: %s",
407 "Use [IPv6]/IPv4 format\n");
408 return;
409 }
410
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000411 buf[indx] = 0;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000412 proxy = xmlMemStrdup (buf);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000413 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000414 cur += 1;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000415 if (cur[0] == ':') {
Owen Taylor3473f882001-02-23 17:55:21 +0000416 cur++;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000417 while (*cur >= '0' && *cur <= '9') {
418 port *= 10;
419 port += *cur - '0';
420 cur++;
421 }
422
423 if (port != 0) proxyPort = port;
424 while ((cur[0] != '/') && (*cur != 0))
425 cur ++;
426 }
Owen Taylor3473f882001-02-23 17:55:21 +0000427 break;
428 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000429 else {
430 if (cur[0] == ':') {
431 buf[indx] = 0;
432 proxy = xmlMemStrdup (buf);
433 indx = 0;
434 cur += 1;
435 while ((*cur >= '0') && (*cur <= '9')) {
436 port *= 10;
437 port += *cur - '0';
438 cur++;
439 }
440 if (port != 0) proxyPort = port;
441 while ((cur[0] != '/') && (*cur != 0))
442 cur++;
443 break;
444 }
445 if ((*cur == '/') || (*cur == 0)) {
446 buf[indx] = 0;
447 proxy = xmlMemStrdup (buf);
448 indx = 0;
449 break;
450 }
Owen Taylor3473f882001-02-23 17:55:21 +0000451 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000452 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000453 }
454}
455
456/**
457 * xmlNanoHTTPNewCtxt:
458 * @URL: The URL used to initialize the context
459 *
460 * Allocate and initialize a new HTTP context.
461 *
462 * Returns an HTTP context or NULL in case of error.
463 */
464
465static xmlNanoHTTPCtxtPtr
466xmlNanoHTTPNewCtxt(const char *URL) {
467 xmlNanoHTTPCtxtPtr ret;
468
469 ret = (xmlNanoHTTPCtxtPtr) xmlMalloc(sizeof(xmlNanoHTTPCtxt));
470 if (ret == NULL) return(NULL);
471
472 memset(ret, 0, sizeof(xmlNanoHTTPCtxt));
473 ret->port = 80;
474 ret->returnValue = 0;
475 ret->fd = -1;
Daniel Veillardf012a642001-07-23 19:10:52 +0000476 ret->ContentLength = -1;
Owen Taylor3473f882001-02-23 17:55:21 +0000477
Daniel Veillardcacbe5d2003-01-10 16:09:51 +0000478 xmlNanoHTTPScanURL(ret, URL);
Owen Taylor3473f882001-02-23 17:55:21 +0000479
480 return(ret);
481}
482
483/**
484 * xmlNanoHTTPFreeCtxt:
485 * @ctxt: an HTTP context
486 *
487 * Frees the context after closing the connection.
488 */
489
490static void
491xmlNanoHTTPFreeCtxt(xmlNanoHTTPCtxtPtr ctxt) {
492 if (ctxt == NULL) return;
493 if (ctxt->hostname != NULL) xmlFree(ctxt->hostname);
494 if (ctxt->protocol != NULL) xmlFree(ctxt->protocol);
495 if (ctxt->path != NULL) xmlFree(ctxt->path);
496 if (ctxt->out != NULL) xmlFree(ctxt->out);
497 if (ctxt->in != NULL) xmlFree(ctxt->in);
498 if (ctxt->contentType != NULL) xmlFree(ctxt->contentType);
499 if (ctxt->location != NULL) xmlFree(ctxt->location);
500 if (ctxt->authHeader != NULL) xmlFree(ctxt->authHeader);
501 ctxt->state = XML_NANO_HTTP_NONE;
502 if (ctxt->fd >= 0) closesocket(ctxt->fd);
503 ctxt->fd = -1;
504 xmlFree(ctxt);
505}
506
507/**
508 * xmlNanoHTTPSend:
509 * @ctxt: an HTTP context
510 *
511 * Send the input needed to initiate the processing on the server side
Daniel Veillardf012a642001-07-23 19:10:52 +0000512 * Returns number of bytes sent or -1 on error.
Owen Taylor3473f882001-02-23 17:55:21 +0000513 */
514
Daniel Veillardf012a642001-07-23 19:10:52 +0000515static int
516xmlNanoHTTPSend(xmlNanoHTTPCtxtPtr ctxt, const char * xmt_ptr, int outlen) {
517
518 int total_sent = 0;
519
520 if ( (ctxt->state & XML_NANO_HTTP_WRITE) && (xmt_ptr != NULL ) ) {
521 while (total_sent < outlen) {
522 int nsent = send(ctxt->fd, xmt_ptr + total_sent,
523 outlen - total_sent, 0);
Owen Taylor3473f882001-02-23 17:55:21 +0000524 if (nsent>0)
525 total_sent += nsent;
Daniel Veillardf012a642001-07-23 19:10:52 +0000526 else if ( ( nsent == -1 ) &&
Daniel Veillardba6db032001-07-31 16:25:45 +0000527#if defined(EAGAIN) && EAGAIN != EWOULDBLOCK
Daniel Veillardf012a642001-07-23 19:10:52 +0000528 ( socket_errno( ) != EAGAIN ) &&
Daniel Veillardba6db032001-07-31 16:25:45 +0000529#endif
Daniel Veillardf012a642001-07-23 19:10:52 +0000530 ( socket_errno( ) != EWOULDBLOCK ) ) {
531 xmlGenericError( xmlGenericErrorContext,
532 "xmlNanoHTTPSend error: %s",
533 strerror( socket_errno( ) ) );
534
535 if ( total_sent == 0 )
536 total_sent = -1;
537 break;
538 }
539 else {
540 /*
541 ** No data sent
542 ** Since non-blocking sockets are used, wait for
543 ** socket to be writable or default timeout prior
544 ** to retrying.
545 */
546
547 struct timeval tv;
548 fd_set wfd;
549
550 tv.tv_sec = timeout;
551 tv.tv_usec = 0;
552 FD_ZERO( &wfd );
553 FD_SET( ctxt->fd, &wfd );
554 (void)select( ctxt->fd + 1, NULL, &wfd, NULL, &tv );
555 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000556 }
Owen Taylor3473f882001-02-23 17:55:21 +0000557 }
Daniel Veillardf012a642001-07-23 19:10:52 +0000558
559 return total_sent;
Owen Taylor3473f882001-02-23 17:55:21 +0000560}
561
562/**
563 * xmlNanoHTTPRecv:
564 * @ctxt: an HTTP context
565 *
566 * Read information coming from the HTTP connection.
567 * This is a blocking call (but it blocks in select(), not read()).
568 *
569 * Returns the number of byte read or -1 in case of error.
570 */
571
572static int
573xmlNanoHTTPRecv(xmlNanoHTTPCtxtPtr ctxt) {
574 fd_set rfd;
575 struct timeval tv;
576
577
578 while (ctxt->state & XML_NANO_HTTP_READ) {
579 if (ctxt->in == NULL) {
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000580 ctxt->in = (char *) xmlMallocAtomic(65000 * sizeof(char));
Owen Taylor3473f882001-02-23 17:55:21 +0000581 if (ctxt->in == NULL) {
582 ctxt->last = -1;
Daniel Veillardf012a642001-07-23 19:10:52 +0000583 xmlGenericError( xmlGenericErrorContext,
584 "xmlNanoHTTPRecv: Error allocating input memory." );
Owen Taylor3473f882001-02-23 17:55:21 +0000585 return(-1);
586 }
587 ctxt->inlen = 65000;
588 ctxt->inptr = ctxt->content = ctxt->inrptr = ctxt->in;
589 }
590 if (ctxt->inrptr > ctxt->in + XML_NANO_HTTP_CHUNK) {
591 int delta = ctxt->inrptr - ctxt->in;
592 int len = ctxt->inptr - ctxt->inrptr;
593
594 memmove(ctxt->in, ctxt->inrptr, len);
595 ctxt->inrptr -= delta;
596 ctxt->content -= delta;
597 ctxt->inptr -= delta;
598 }
599 if ((ctxt->in + ctxt->inlen) < (ctxt->inptr + XML_NANO_HTTP_CHUNK)) {
600 int d_inptr = ctxt->inptr - ctxt->in;
601 int d_content = ctxt->content - ctxt->in;
602 int d_inrptr = ctxt->inrptr - ctxt->in;
Daniel Veillardf012a642001-07-23 19:10:52 +0000603 char * tmp_ptr = ctxt->in;
Owen Taylor3473f882001-02-23 17:55:21 +0000604
605 ctxt->inlen *= 2;
Daniel Veillardf012a642001-07-23 19:10:52 +0000606 ctxt->in = (char *) xmlRealloc(tmp_ptr, ctxt->inlen);
Owen Taylor3473f882001-02-23 17:55:21 +0000607 if (ctxt->in == NULL) {
Daniel Veillardf012a642001-07-23 19:10:52 +0000608 xmlGenericError( xmlGenericErrorContext,
609 "xmlNanoHTTPRecv: %s %d bytes.",
610 "Failed to realloc input buffer to",
611 ctxt->inlen );
612 xmlFree( tmp_ptr );
Owen Taylor3473f882001-02-23 17:55:21 +0000613 ctxt->last = -1;
614 return(-1);
615 }
616 ctxt->inptr = ctxt->in + d_inptr;
617 ctxt->content = ctxt->in + d_content;
618 ctxt->inrptr = ctxt->in + d_inrptr;
619 }
620 ctxt->last = recv(ctxt->fd, ctxt->inptr, XML_NANO_HTTP_CHUNK, 0);
621 if (ctxt->last > 0) {
622 ctxt->inptr += ctxt->last;
623 return(ctxt->last);
624 }
625 if (ctxt->last == 0) {
626 return(0);
627 }
628 if (ctxt->last == -1) {
629 switch (socket_errno()) {
630 case EINPROGRESS:
631 case EWOULDBLOCK:
632#if defined(EAGAIN) && EAGAIN != EWOULDBLOCK
633 case EAGAIN:
634#endif
635 break;
Daniel Veillardf012a642001-07-23 19:10:52 +0000636
637 case ECONNRESET:
638 case ESHUTDOWN:
639 return ( 0 );
640
Owen Taylor3473f882001-02-23 17:55:21 +0000641 default:
Daniel Veillardf012a642001-07-23 19:10:52 +0000642 xmlGenericError( xmlGenericErrorContext,
643 "xmlNanoHTTPRecv: recv( ) failure - %s",
644 strerror( socket_errno( ) ) );
645 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +0000646 }
647 }
648
649 tv.tv_sec = timeout;
650 tv.tv_usec = 0;
651 FD_ZERO(&rfd);
652 FD_SET(ctxt->fd, &rfd);
653
Daniel Veillard50f34372001-08-03 12:06:36 +0000654 if ( (select(ctxt->fd+1, &rfd, NULL, NULL, &tv)<1)
655#if defined(EINTR)
656 && (errno != EINTR)
657#endif
658 )
Owen Taylor3473f882001-02-23 17:55:21 +0000659 return(0);
660 }
661 return(0);
662}
663
664/**
665 * xmlNanoHTTPReadLine:
666 * @ctxt: an HTTP context
667 *
668 * Read one line in the HTTP server output, usually for extracting
669 * the HTTP protocol informations from the answer header.
670 *
671 * Returns a newly allocated string with a copy of the line, or NULL
672 * which indicate the end of the input.
673 */
674
675static char *
676xmlNanoHTTPReadLine(xmlNanoHTTPCtxtPtr ctxt) {
677 char buf[4096];
678 char *bp = buf;
Daniel Veillardf012a642001-07-23 19:10:52 +0000679 int rc;
Owen Taylor3473f882001-02-23 17:55:21 +0000680
681 while (bp - buf < 4095) {
682 if (ctxt->inrptr == ctxt->inptr) {
Daniel Veillardf012a642001-07-23 19:10:52 +0000683 if ( (rc = xmlNanoHTTPRecv(ctxt)) == 0) {
Owen Taylor3473f882001-02-23 17:55:21 +0000684 if (bp == buf)
685 return(NULL);
686 else
687 *bp = 0;
688 return(xmlMemStrdup(buf));
689 }
Daniel Veillardf012a642001-07-23 19:10:52 +0000690 else if ( rc == -1 ) {
691 return ( NULL );
692 }
Owen Taylor3473f882001-02-23 17:55:21 +0000693 }
694 *bp = *ctxt->inrptr++;
695 if (*bp == '\n') {
696 *bp = 0;
697 return(xmlMemStrdup(buf));
698 }
699 if (*bp != '\r')
700 bp++;
701 }
702 buf[4095] = 0;
703 return(xmlMemStrdup(buf));
704}
705
706
707/**
708 * xmlNanoHTTPScanAnswer:
709 * @ctxt: an HTTP context
710 * @line: an HTTP header line
711 *
712 * Try to extract useful informations from the server answer.
713 * We currently parse and process:
714 * - The HTTP revision/ return code
715 * - The Content-Type
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000716 * - The Location for redirect processing.
Owen Taylor3473f882001-02-23 17:55:21 +0000717 *
718 * Returns -1 in case of failure, the file descriptor number otherwise
719 */
720
721static void
722xmlNanoHTTPScanAnswer(xmlNanoHTTPCtxtPtr ctxt, const char *line) {
723 const char *cur = line;
724
725 if (line == NULL) return;
726
727 if (!strncmp(line, "HTTP/", 5)) {
728 int version = 0;
729 int ret = 0;
730
731 cur += 5;
732 while ((*cur >= '0') && (*cur <= '9')) {
733 version *= 10;
734 version += *cur - '0';
735 cur++;
736 }
737 if (*cur == '.') {
738 cur++;
739 if ((*cur >= '0') && (*cur <= '9')) {
740 version *= 10;
741 version += *cur - '0';
742 cur++;
743 }
744 while ((*cur >= '0') && (*cur <= '9'))
745 cur++;
746 } else
747 version *= 10;
748 if ((*cur != ' ') && (*cur != '\t')) return;
749 while ((*cur == ' ') || (*cur == '\t')) cur++;
750 if ((*cur < '0') || (*cur > '9')) return;
751 while ((*cur >= '0') && (*cur <= '9')) {
752 ret *= 10;
753 ret += *cur - '0';
754 cur++;
755 }
756 if ((*cur != 0) && (*cur != ' ') && (*cur != '\t')) return;
757 ctxt->returnValue = ret;
758 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Content-Type:", 13)) {
759 cur += 13;
760 while ((*cur == ' ') || (*cur == '\t')) cur++;
761 if (ctxt->contentType != NULL)
762 xmlFree(ctxt->contentType);
763 ctxt->contentType = xmlMemStrdup(cur);
764 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"ContentType:", 12)) {
765 cur += 12;
766 if (ctxt->contentType != NULL) return;
767 while ((*cur == ' ') || (*cur == '\t')) cur++;
768 ctxt->contentType = xmlMemStrdup(cur);
769 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Location:", 9)) {
770 cur += 9;
771 while ((*cur == ' ') || (*cur == '\t')) cur++;
772 if (ctxt->location != NULL)
773 xmlFree(ctxt->location);
774 ctxt->location = xmlMemStrdup(cur);
775 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"WWW-Authenticate:", 17)) {
776 cur += 17;
777 while ((*cur == ' ') || (*cur == '\t')) cur++;
778 if (ctxt->authHeader != NULL)
779 xmlFree(ctxt->authHeader);
780 ctxt->authHeader = xmlMemStrdup(cur);
781 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Proxy-Authenticate:", 19)) {
782 cur += 19;
783 while ((*cur == ' ') || (*cur == '\t')) cur++;
784 if (ctxt->authHeader != NULL)
785 xmlFree(ctxt->authHeader);
786 ctxt->authHeader = xmlMemStrdup(cur);
Daniel Veillardf012a642001-07-23 19:10:52 +0000787 } else if ( !xmlStrncasecmp( BAD_CAST line, BAD_CAST"Content-Length:", 15) ) {
788 cur += 15;
789 ctxt->ContentLength = strtol( cur, NULL, 10 );
Owen Taylor3473f882001-02-23 17:55:21 +0000790 }
791}
792
793/**
794 * xmlNanoHTTPConnectAttempt:
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000795 * @addr: a socket address structure
Owen Taylor3473f882001-02-23 17:55:21 +0000796 *
797 * Attempt a connection to the given IP:port endpoint. It forces
798 * non-blocking semantic on the socket, and allow 60 seconds for
799 * the host to answer.
800 *
801 * Returns -1 in case of failure, the file descriptor number otherwise
802 */
803
804static int
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000805xmlNanoHTTPConnectAttempt(struct sockaddr *addr)
Owen Taylor3473f882001-02-23 17:55:21 +0000806{
Owen Taylor3473f882001-02-23 17:55:21 +0000807 fd_set wfd;
808 struct timeval tv;
809 int status;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000810 int addrlen;
811 SOCKET s;
Owen Taylor3473f882001-02-23 17:55:21 +0000812
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000813#ifdef SUPPORT_IP6
814 if (addr->sa_family == AF_INET6) {
815 s = socket (PF_INET6, SOCK_STREAM, IPPROTO_TCP);
816 addrlen = sizeof (struct sockaddr_in6);
817 }
818 else
819#endif
820 {
821 s = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
822 addrlen = sizeof (struct sockaddr_in);
823 }
Owen Taylor3473f882001-02-23 17:55:21 +0000824 if (s==-1) {
825#ifdef DEBUG_HTTP
826 perror("socket");
827#endif
Daniel Veillardf012a642001-07-23 19:10:52 +0000828 xmlGenericError( xmlGenericErrorContext,
829 "xmlNanoHTTPConnectAttempt: %s - %s",
830 "socket creation failure",
831 strerror( socket_errno( ) ) );
Owen Taylor3473f882001-02-23 17:55:21 +0000832 return(-1);
833 }
834
835#ifdef _WINSOCKAPI_
836 {
837 u_long one = 1;
838
839 status = ioctlsocket(s, FIONBIO, &one) == SOCKET_ERROR ? -1 : 0;
840 }
841#else /* _WINSOCKAPI_ */
842#if defined(VMS)
843 {
844 int enable = 1;
845 status = ioctl(s, FIONBIO, &enable);
846 }
847#else /* VMS */
848 if ((status = fcntl(s, F_GETFL, 0)) != -1) {
849#ifdef O_NONBLOCK
850 status |= O_NONBLOCK;
851#else /* O_NONBLOCK */
852#ifdef F_NDELAY
853 status |= F_NDELAY;
854#endif /* F_NDELAY */
855#endif /* !O_NONBLOCK */
856 status = fcntl(s, F_SETFL, status);
857 }
858 if (status < 0) {
859#ifdef DEBUG_HTTP
860 perror("nonblocking");
861#endif
Daniel Veillardf012a642001-07-23 19:10:52 +0000862 xmlGenericError( xmlGenericErrorContext,
863 "xmlNanoHTTPConnectAttempt: %s - %s",
864 "error setting non-blocking IO",
865 strerror( socket_errno( ) ) );
Owen Taylor3473f882001-02-23 17:55:21 +0000866 closesocket(s);
867 return(-1);
868 }
869#endif /* !VMS */
870#endif /* !_WINSOCKAPI_ */
871
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000872 if (connect (s, addr, addrlen) == -1) {
Owen Taylor3473f882001-02-23 17:55:21 +0000873 switch (socket_errno()) {
874 case EINPROGRESS:
875 case EWOULDBLOCK:
876 break;
877 default:
Daniel Veillardf012a642001-07-23 19:10:52 +0000878 xmlGenericError( xmlGenericErrorContext,
879 "xmlNanoHTTPConnectAttempt: %s - %s",
880 "error connecting to HTTP server",
881 strerror( socket_errno( ) ) );
Owen Taylor3473f882001-02-23 17:55:21 +0000882 closesocket(s);
883 return(-1);
884 }
885 }
886
887 tv.tv_sec = timeout;
888 tv.tv_usec = 0;
889
890 FD_ZERO(&wfd);
891 FD_SET(s, &wfd);
892
893 switch(select(s+1, NULL, &wfd, NULL, &tv))
894 {
895 case 0:
896 /* Time out */
Daniel Veillardf012a642001-07-23 19:10:52 +0000897 xmlGenericError( xmlGenericErrorContext,
898 "xmlNanoHTTPConnectAttempt: %s",
899 "Connect attempt timed out." );
Owen Taylor3473f882001-02-23 17:55:21 +0000900 closesocket(s);
901 return(-1);
902 case -1:
903 /* Ermm.. ?? */
Daniel Veillardf012a642001-07-23 19:10:52 +0000904 xmlGenericError( xmlGenericErrorContext,
905 "xmlNanoHTTPConnectAttempt: %s - %s",
906 "Error connecting to host",
907 strerror( socket_errno( ) ) );
Owen Taylor3473f882001-02-23 17:55:21 +0000908 closesocket(s);
909 return(-1);
910 }
911
912 if ( FD_ISSET(s, &wfd) ) {
913 SOCKLEN_T len;
914 len = sizeof(status);
915 if (getsockopt(s, SOL_SOCKET, SO_ERROR, (char*)&status, &len) < 0 ) {
916 /* Solaris error code */
Daniel Veillardf012a642001-07-23 19:10:52 +0000917 xmlGenericError( xmlGenericErrorContext,
918 "xmlNanoHTTPConnectAttempt: %s - %s",
919 "Error retrieving pending socket errors",
920 strerror( socket_errno( ) ) );
Owen Taylor3473f882001-02-23 17:55:21 +0000921 return (-1);
922 }
923 if ( status ) {
924 closesocket(s);
925 errno = status;
Daniel Veillardf012a642001-07-23 19:10:52 +0000926 xmlGenericError( xmlGenericErrorContext,
927 "xmlNanoHTTPConnectAttempt: %s - %s",
928 "Error connecting to remote host",
929 strerror( status ) );
Owen Taylor3473f882001-02-23 17:55:21 +0000930 return (-1);
931 }
932 } else {
933 /* pbm */
Daniel Veillardf012a642001-07-23 19:10:52 +0000934 xmlGenericError( xmlGenericErrorContext,
935 "xmlNanoHTTPConnectAttempt: %s\n",
936 "Select returned, but descriptor not set for connection.\n" );
937 closesocket(s);
Owen Taylor3473f882001-02-23 17:55:21 +0000938 return (-1);
939 }
940
941 return(s);
942}
943
944/**
945 * xmlNanoHTTPConnectHost:
946 * @host: the host name
947 * @port: the port number
948 *
949 * Attempt a connection to the given host:port endpoint. It tries
950 * the multiple IP provided by the DNS if available.
951 *
952 * Returns -1 in case of failure, the file descriptor number otherwise
953 */
954
955static int
956xmlNanoHTTPConnectHost(const char *host, int port)
957{
958 struct hostent *h;
Daniel Veillard2db8c122003-07-08 12:16:59 +0000959 struct sockaddr *addr = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +0000960 struct in_addr ia;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000961 struct sockaddr_in sockin;
Daniel Veillard5c396542002-03-15 07:57:50 +0000962
Owen Taylor3473f882001-02-23 17:55:21 +0000963#ifdef SUPPORT_IP6
964 struct in6_addr ia6;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000965 struct sockaddr_in6 sockin6;
Owen Taylor3473f882001-02-23 17:55:21 +0000966#endif
967 int i;
968 int s;
Daniel Veillard5c396542002-03-15 07:57:50 +0000969
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000970 memset (&sockin, 0, sizeof(sockin));
971#ifdef SUPPORT_IP6
972 memset (&sockin6, 0, sizeof(sockin6));
973 if (have_ipv6 ())
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000974#if !defined(HAVE_GETADDRINFO) && defined(RES_USE_INET6)
Daniel Veillard560c2a42003-07-06 21:13:49 +0000975 {
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000976 if (!(_res.options & RES_INIT))
977 res_init();
978 _res.options |= RES_USE_INET6;
979 }
980#elif defined(HAVE_GETADDRINFO)
Daniel Veillard560c2a42003-07-06 21:13:49 +0000981 {
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000982 int status;
983 struct addrinfo hints, *res, *result;
984
985 result = NULL;
986 memset (&hints, 0,sizeof(hints));
987 hints.ai_socktype = SOCK_STREAM;
988
989 status = getaddrinfo (host, NULL, &hints, &result);
990 if (status) {
991 xmlGenericError (xmlGenericErrorContext,
992 "xmlNanoHTTPConnectHost: %s '%s' - %s",
993 "Failed to resolve host", host, gai_strerror (status));
994
995 return (-1);
996 }
997
998 for (res = result; res; res = res->ai_next) {
Daniel Veillard3dc93a42003-07-10 14:04:33 +0000999 if (res->ai_family == AF_INET || res->ai_family == AF_INET6) {
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001000 if (res->ai_family == AF_INET6) {
1001 memcpy (&sockin6, res->ai_addr, res->ai_addrlen);
1002 sockin6.sin6_port = htons (port);
1003 addr = (struct sockaddr *)&sockin6;
1004 }
Daniel Veillard3dc93a42003-07-10 14:04:33 +00001005 else {
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001006 memcpy (&sockin, res->ai_addr, res->ai_addrlen);
1007 sockin.sin_port = htons (port);
1008 addr = (struct sockaddr *)&sockin;
1009 }
1010
1011 s = xmlNanoHTTPConnectAttempt (addr);
1012 if (s != -1) {
1013 freeaddrinfo (result);
1014 return (s);
1015 }
1016 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001017 }
Daniel Veillard3dc93a42003-07-10 14:04:33 +00001018 if (result)
1019 freeaddrinfo (result);
1020 return (-1);
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001021 } else
Owen Taylor3473f882001-02-23 17:55:21 +00001022#endif
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001023#endif
1024 {
1025 h = gethostbyname (host);
1026 if (h == NULL) {
Daniel Veillard56b2db72002-03-25 16:35:28 +00001027
1028/*
1029 * Okay, I got fed up by the non-portability of this error message
1030 * extraction code. it work on Linux, if it work on your platform
1031 * and one want to enable it, send me the defined(foobar) needed
1032 */
1033#if defined(HAVE_NETDB_H) && defined(HOST_NOT_FOUND) && defined(linux)
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001034 const char *h_err_txt = "";
Daniel Veillardf012a642001-07-23 19:10:52 +00001035
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001036 switch (h_errno) {
1037 case HOST_NOT_FOUND:
1038 h_err_txt = "Authoritive host not found";
1039 break;
Daniel Veillardf012a642001-07-23 19:10:52 +00001040
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001041 case TRY_AGAIN:
1042 h_err_txt =
1043 "Non-authoritive host not found or server failure.";
1044 break;
Daniel Veillardf012a642001-07-23 19:10:52 +00001045
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001046 case NO_RECOVERY:
1047 h_err_txt =
1048 "Non-recoverable errors: FORMERR, REFUSED, or NOTIMP.";
1049 break;
Daniel Veillard5c396542002-03-15 07:57:50 +00001050
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001051 case NO_ADDRESS:
1052 h_err_txt =
1053 "Valid name, no data record of requested type.";
1054 break;
Daniel Veillard5c396542002-03-15 07:57:50 +00001055
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001056 default:
1057 h_err_txt = "No error text defined.";
1058 break;
1059 }
1060 xmlGenericError (xmlGenericErrorContext,
1061 "xmlNanoHTTPConnectHost: %s '%s' - %s",
1062 "Failed to resolve host", host, h_err_txt);
Daniel Veillard5c396542002-03-15 07:57:50 +00001063#else
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001064 xmlGenericError (xmlGenericErrorContext,
1065 "xmlNanoHTTPConnectHost: %s '%s'",
1066 "Failed to resolve host", host);
Owen Taylor3473f882001-02-23 17:55:21 +00001067#endif
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001068 return (-1);
1069 }
Daniel Veillard5c396542002-03-15 07:57:50 +00001070
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001071 for (i = 0; h->h_addr_list[i]; i++) {
1072 if (h->h_addrtype == AF_INET) {
1073 /* A records (IPv4) */
1074 memcpy (&ia, h->h_addr_list[i], h->h_length);
1075 sockin.sin_family = h->h_addrtype;
1076 sockin.sin_addr = ia;
1077 sockin.sin_port = htons (port);
1078 addr = (struct sockaddr *) &sockin;
Daniel Veillard5c396542002-03-15 07:57:50 +00001079#ifdef SUPPORT_IP6
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001080 } else if (have_ipv6 () && (h->h_addrtype == AF_INET6)) {
1081 /* AAAA records (IPv6) */
1082 memcpy (&ia6, h->h_addr_list[i], h->h_length);
1083 sockin6.sin6_family = h->h_addrtype;
1084 sockin6.sin6_addr = ia6;
1085 sockin6.sin6_port = htons (port);
1086 addr = (struct sockaddr *) &sockin6;
Daniel Veillard5c396542002-03-15 07:57:50 +00001087#endif
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001088 } else
1089 break; /* for */
Daniel Veillard5c396542002-03-15 07:57:50 +00001090
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001091 s = xmlNanoHTTPConnectAttempt (addr);
1092 if (s != -1)
1093 return (s);
1094 }
Owen Taylor3473f882001-02-23 17:55:21 +00001095 }
Owen Taylor3473f882001-02-23 17:55:21 +00001096#ifdef DEBUG_HTTP
1097 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard5c396542002-03-15 07:57:50 +00001098 "xmlNanoHTTPConnectHost: unable to connect to '%s'.\n",
1099 host);
Owen Taylor3473f882001-02-23 17:55:21 +00001100#endif
Daniel Veillard5c396542002-03-15 07:57:50 +00001101 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001102}
1103
1104
1105/**
1106 * xmlNanoHTTPOpen:
1107 * @URL: The URL to load
1108 * @contentType: if available the Content-Type information will be
1109 * returned at that location
1110 *
1111 * This function try to open a connection to the indicated resource
1112 * via HTTP GET.
1113 *
1114 * Returns NULL in case of failure, otherwise a request handler.
1115 * The contentType, if provided must be freed by the caller
1116 */
1117
1118void*
1119xmlNanoHTTPOpen(const char *URL, char **contentType) {
1120 if (contentType != NULL) *contentType = NULL;
Daniel Veillardf012a642001-07-23 19:10:52 +00001121 return(xmlNanoHTTPMethod(URL, NULL, NULL, contentType, NULL, 0));
Daniel Veillard9403a042001-05-28 11:00:53 +00001122}
1123
1124/**
1125 * xmlNanoHTTPOpenRedir:
1126 * @URL: The URL to load
1127 * @contentType: if available the Content-Type information will be
1128 * returned at that location
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001129 * @redir: if available the redirected URL will be returned
Daniel Veillard9403a042001-05-28 11:00:53 +00001130 *
1131 * This function try to open a connection to the indicated resource
1132 * via HTTP GET.
1133 *
1134 * Returns NULL in case of failure, otherwise a request handler.
1135 * The contentType, if provided must be freed by the caller
1136 */
1137
1138void*
1139xmlNanoHTTPOpenRedir(const char *URL, char **contentType, char **redir) {
1140 if (contentType != NULL) *contentType = NULL;
1141 if (redir != NULL) *redir = NULL;
Daniel Veillardf012a642001-07-23 19:10:52 +00001142 return(xmlNanoHTTPMethodRedir(URL, NULL, NULL, contentType, redir, NULL,0));
Owen Taylor3473f882001-02-23 17:55:21 +00001143}
1144
1145/**
1146 * xmlNanoHTTPRead:
1147 * @ctx: the HTTP context
1148 * @dest: a buffer
1149 * @len: the buffer length
1150 *
1151 * This function tries to read @len bytes from the existing HTTP connection
1152 * and saves them in @dest. This is a blocking call.
1153 *
1154 * Returns the number of byte read. 0 is an indication of an end of connection.
1155 * -1 indicates a parameter error.
1156 */
1157int
1158xmlNanoHTTPRead(void *ctx, void *dest, int len) {
1159 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1160
1161 if (ctx == NULL) return(-1);
1162 if (dest == NULL) return(-1);
1163 if (len <= 0) return(0);
1164
1165 while (ctxt->inptr - ctxt->inrptr < len) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001166 if (xmlNanoHTTPRecv(ctxt) <= 0) break;
Owen Taylor3473f882001-02-23 17:55:21 +00001167 }
1168 if (ctxt->inptr - ctxt->inrptr < len)
1169 len = ctxt->inptr - ctxt->inrptr;
1170 memcpy(dest, ctxt->inrptr, len);
1171 ctxt->inrptr += len;
1172 return(len);
1173}
1174
1175/**
1176 * xmlNanoHTTPClose:
1177 * @ctx: the HTTP context
1178 *
1179 * This function closes an HTTP context, it ends up the connection and
1180 * free all data related to it.
1181 */
1182void
1183xmlNanoHTTPClose(void *ctx) {
1184 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1185
1186 if (ctx == NULL) return;
1187
1188 xmlNanoHTTPFreeCtxt(ctxt);
1189}
1190
1191/**
Daniel Veillard9403a042001-05-28 11:00:53 +00001192 * xmlNanoHTTPMethodRedir:
Owen Taylor3473f882001-02-23 17:55:21 +00001193 * @URL: The URL to load
1194 * @method: the HTTP method to use
1195 * @input: the input string if any
1196 * @contentType: the Content-Type information IN and OUT
Daniel Veillard9403a042001-05-28 11:00:53 +00001197 * @redir: the redirected URL OUT
Owen Taylor3473f882001-02-23 17:55:21 +00001198 * @headers: the extra headers
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001199 * @ilen: input length
Owen Taylor3473f882001-02-23 17:55:21 +00001200 *
1201 * This function try to open a connection to the indicated resource
1202 * via HTTP using the given @method, adding the given extra headers
1203 * and the input buffer for the request content.
1204 *
1205 * Returns NULL in case of failure, otherwise a request handler.
Daniel Veillard9403a042001-05-28 11:00:53 +00001206 * The contentType, or redir, if provided must be freed by the caller
Owen Taylor3473f882001-02-23 17:55:21 +00001207 */
1208
1209void*
Daniel Veillard9403a042001-05-28 11:00:53 +00001210xmlNanoHTTPMethodRedir(const char *URL, const char *method, const char *input,
Daniel Veillardf012a642001-07-23 19:10:52 +00001211 char **contentType, char **redir,
1212 const char *headers, int ilen ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001213 xmlNanoHTTPCtxtPtr ctxt;
1214 char *bp, *p;
Daniel Veillardf012a642001-07-23 19:10:52 +00001215 int blen, ret;
Owen Taylor3473f882001-02-23 17:55:21 +00001216 int head;
1217 int nbRedirects = 0;
1218 char *redirURL = NULL;
William M. Brack78637da2003-07-31 14:47:38 +00001219#ifdef DEBUG_HTTP
1220 int xmt_bytes;
1221#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001222
1223 if (URL == NULL) return(NULL);
1224 if (method == NULL) method = "GET";
1225 xmlNanoHTTPInit();
1226
1227retry:
1228 if (redirURL == NULL)
1229 ctxt = xmlNanoHTTPNewCtxt(URL);
1230 else {
1231 ctxt = xmlNanoHTTPNewCtxt(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001232 }
1233
Daniel Veillardf012a642001-07-23 19:10:52 +00001234 if ( ctxt == NULL ) {
1235 xmlGenericError( xmlGenericErrorContext,
1236 "xmlNanoHTTPMethodRedir: %s %s.",
1237 "Unable to allocate HTTP context to URI",
1238 ( ( redirURL == NULL ) ? URL : redirURL ) );
1239 return ( NULL );
1240 }
1241
Owen Taylor3473f882001-02-23 17:55:21 +00001242 if ((ctxt->protocol == NULL) || (strcmp(ctxt->protocol, "http"))) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001243 xmlGenericError( xmlGenericErrorContext,
1244 "xmlNanoHTTPMethodRedir: %s - %s.",
1245 "Not a valid HTTP URI",
1246 ( ( redirURL == NULL ) ? URL : redirURL ) );
Owen Taylor3473f882001-02-23 17:55:21 +00001247 xmlNanoHTTPFreeCtxt(ctxt);
1248 if (redirURL != NULL) xmlFree(redirURL);
1249 return(NULL);
1250 }
1251 if (ctxt->hostname == NULL) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001252 xmlGenericError( xmlGenericErrorContext,
1253 "xmlNanoHTTPMethodRedir: %s - %s",
1254 "Failed to identify host in URI",
1255 ( ( redirURL == NULL ) ? URL : redirURL ) );
Owen Taylor3473f882001-02-23 17:55:21 +00001256 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001257 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001258 return(NULL);
1259 }
1260 if (proxy) {
1261 blen = strlen(ctxt->hostname) * 2 + 16;
1262 ret = xmlNanoHTTPConnectHost(proxy, proxyPort);
1263 }
1264 else {
1265 blen = strlen(ctxt->hostname);
1266 ret = xmlNanoHTTPConnectHost(ctxt->hostname, ctxt->port);
1267 }
1268 if (ret < 0) {
1269 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001270 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001271 return(NULL);
1272 }
1273 ctxt->fd = ret;
1274
Daniel Veillardf012a642001-07-23 19:10:52 +00001275 if (input == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00001276 ilen = 0;
Daniel Veillardf012a642001-07-23 19:10:52 +00001277 else
1278 blen += 36;
1279
Owen Taylor3473f882001-02-23 17:55:21 +00001280 if (headers != NULL)
Daniel Veillardf012a642001-07-23 19:10:52 +00001281 blen += strlen(headers) + 2;
Owen Taylor3473f882001-02-23 17:55:21 +00001282 if (contentType && *contentType)
1283 blen += strlen(*contentType) + 16;
Daniel Veillardf012a642001-07-23 19:10:52 +00001284 blen += strlen(method) + strlen(ctxt->path) + 24;
Daniel Veillard3c908dc2003-04-19 00:07:51 +00001285 bp = xmlMallocAtomic(blen);
Daniel Veillardf012a642001-07-23 19:10:52 +00001286 if ( bp == NULL ) {
1287 xmlNanoHTTPFreeCtxt( ctxt );
1288 xmlGenericError( xmlGenericErrorContext,
1289 "xmlNanoHTTPMethodRedir: %s",
1290 "Error allocating HTTP header buffer." );
1291 return ( NULL );
1292 }
1293
1294 p = bp;
1295
Owen Taylor3473f882001-02-23 17:55:21 +00001296 if (proxy) {
1297 if (ctxt->port != 80) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001298 p += snprintf( p, blen - (p - bp), "%s http://%s:%d%s",
1299 method, ctxt->hostname,
Daniel Veillardf012a642001-07-23 19:10:52 +00001300 ctxt->port, ctxt->path );
Owen Taylor3473f882001-02-23 17:55:21 +00001301 }
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001302 else
1303 p += snprintf( p, blen - (p - bp), "%s http://%s%s", method,
Daniel Veillardf012a642001-07-23 19:10:52 +00001304 ctxt->hostname, ctxt->path);
Owen Taylor3473f882001-02-23 17:55:21 +00001305 }
1306 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001307 p += snprintf( p, blen - (p - bp), "%s %s", method, ctxt->path);
Daniel Veillardf012a642001-07-23 19:10:52 +00001308
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001309 p += snprintf( p, blen - (p - bp), " HTTP/1.0\r\nHost: %s\r\n",
1310 ctxt->hostname);
Daniel Veillardf012a642001-07-23 19:10:52 +00001311
1312 if (contentType != NULL && *contentType)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001313 p += snprintf(p, blen - (p - bp), "Content-Type: %s\r\n", *contentType);
Daniel Veillardf012a642001-07-23 19:10:52 +00001314
1315 if (headers != NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001316 p += snprintf( p, blen - (p - bp), "%s", headers );
Daniel Veillardf012a642001-07-23 19:10:52 +00001317
Owen Taylor3473f882001-02-23 17:55:21 +00001318 if (input != NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001319 snprintf(p, blen - (p - bp), "Content-Length: %d\r\n\r\n", ilen );
Owen Taylor3473f882001-02-23 17:55:21 +00001320 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001321 snprintf(p, blen - (p - bp), "\r\n");
Daniel Veillardf012a642001-07-23 19:10:52 +00001322
Owen Taylor3473f882001-02-23 17:55:21 +00001323#ifdef DEBUG_HTTP
1324 xmlGenericError(xmlGenericErrorContext,
1325 "-> %s%s", proxy? "(Proxy) " : "", bp);
1326 if ((blen -= strlen(bp)+1) < 0)
1327 xmlGenericError(xmlGenericErrorContext,
1328 "ERROR: overflowed buffer by %d bytes\n", -blen);
1329#endif
1330 ctxt->outptr = ctxt->out = bp;
1331 ctxt->state = XML_NANO_HTTP_WRITE;
Daniel Veillardf012a642001-07-23 19:10:52 +00001332 blen = strlen( ctxt->out );
Daniel Veillardf012a642001-07-23 19:10:52 +00001333#ifdef DEBUG_HTTP
William M. Brack78637da2003-07-31 14:47:38 +00001334 xmt_bytes = xmlNanoHTTPSend(ctxt, ctxt->out, blen );
Daniel Veillardf012a642001-07-23 19:10:52 +00001335 if ( xmt_bytes != blen )
1336 xmlGenericError( xmlGenericErrorContext,
1337 "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
1338 xmt_bytes, blen,
1339 "bytes of HTTP headers sent to host",
1340 ctxt->hostname );
William M. Brack78637da2003-07-31 14:47:38 +00001341#else
1342 xmlNanoHTTPSend(ctxt, ctxt->out, blen );
Daniel Veillardf012a642001-07-23 19:10:52 +00001343#endif
1344
1345 if ( input != NULL ) {
William M. Brack78637da2003-07-31 14:47:38 +00001346#ifdef DEBUG_HTTP
Daniel Veillardf012a642001-07-23 19:10:52 +00001347 xmt_bytes = xmlNanoHTTPSend( ctxt, input, ilen );
1348
Daniel Veillardf012a642001-07-23 19:10:52 +00001349 if ( xmt_bytes != ilen )
1350 xmlGenericError( xmlGenericErrorContext,
1351 "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
1352 xmt_bytes, ilen,
1353 "bytes of HTTP content sent to host",
1354 ctxt->hostname );
William M. Brack78637da2003-07-31 14:47:38 +00001355#else
1356 xmlNanoHTTPSend( ctxt, input, ilen );
Daniel Veillardf012a642001-07-23 19:10:52 +00001357#endif
1358 }
1359
Owen Taylor3473f882001-02-23 17:55:21 +00001360 ctxt->state = XML_NANO_HTTP_READ;
1361 head = 1;
1362
1363 while ((p = xmlNanoHTTPReadLine(ctxt)) != NULL) {
1364 if (head && (*p == 0)) {
1365 head = 0;
1366 ctxt->content = ctxt->inrptr;
1367 xmlFree(p);
1368 break;
1369 }
1370 xmlNanoHTTPScanAnswer(ctxt, p);
1371
1372#ifdef DEBUG_HTTP
1373 xmlGenericError(xmlGenericErrorContext, "<- %s\n", p);
1374#endif
1375 xmlFree(p);
1376 }
1377
1378 if ((ctxt->location != NULL) && (ctxt->returnValue >= 300) &&
1379 (ctxt->returnValue < 400)) {
1380#ifdef DEBUG_HTTP
1381 xmlGenericError(xmlGenericErrorContext,
1382 "\nRedirect to: %s\n", ctxt->location);
1383#endif
Daniel Veillardf012a642001-07-23 19:10:52 +00001384 while ( xmlNanoHTTPRecv(ctxt) > 0 ) ;
Owen Taylor3473f882001-02-23 17:55:21 +00001385 if (nbRedirects < XML_NANO_HTTP_MAX_REDIR) {
1386 nbRedirects++;
Daniel Veillard9403a042001-05-28 11:00:53 +00001387 if (redirURL != NULL)
1388 xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001389 redirURL = xmlMemStrdup(ctxt->location);
1390 xmlNanoHTTPFreeCtxt(ctxt);
1391 goto retry;
1392 }
1393 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001394 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001395#ifdef DEBUG_HTTP
1396 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardf012a642001-07-23 19:10:52 +00001397 "xmlNanoHTTPMethodRedir: Too many redirects, aborting ...\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001398#endif
1399 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001400 }
1401
1402 if (contentType != NULL) {
1403 if (ctxt->contentType != NULL)
1404 *contentType = xmlMemStrdup(ctxt->contentType);
1405 else
1406 *contentType = NULL;
1407 }
1408
Daniel Veillard9403a042001-05-28 11:00:53 +00001409 if ((redir != NULL) && (redirURL != NULL)) {
1410 *redir = redirURL;
1411 } else {
1412 if (redirURL != NULL)
1413 xmlFree(redirURL);
1414 if (redir != NULL)
1415 *redir = NULL;
1416 }
1417
Owen Taylor3473f882001-02-23 17:55:21 +00001418#ifdef DEBUG_HTTP
1419 if (ctxt->contentType != NULL)
1420 xmlGenericError(xmlGenericErrorContext,
1421 "\nCode %d, content-type '%s'\n\n",
1422 ctxt->returnValue, ctxt->contentType);
1423 else
1424 xmlGenericError(xmlGenericErrorContext,
1425 "\nCode %d, no content-type\n\n",
1426 ctxt->returnValue);
1427#endif
1428
1429 return((void *) ctxt);
1430}
1431
1432/**
Daniel Veillard9403a042001-05-28 11:00:53 +00001433 * xmlNanoHTTPMethod:
1434 * @URL: The URL to load
1435 * @method: the HTTP method to use
1436 * @input: the input string if any
1437 * @contentType: the Content-Type information IN and OUT
1438 * @headers: the extra headers
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001439 * @ilen: input length
Daniel Veillard9403a042001-05-28 11:00:53 +00001440 *
1441 * This function try to open a connection to the indicated resource
1442 * via HTTP using the given @method, adding the given extra headers
1443 * and the input buffer for the request content.
1444 *
1445 * Returns NULL in case of failure, otherwise a request handler.
1446 * The contentType, if provided must be freed by the caller
1447 */
1448
1449void*
1450xmlNanoHTTPMethod(const char *URL, const char *method, const char *input,
Daniel Veillardf012a642001-07-23 19:10:52 +00001451 char **contentType, const char *headers, int ilen) {
Daniel Veillard9403a042001-05-28 11:00:53 +00001452 return(xmlNanoHTTPMethodRedir(URL, method, input, contentType,
Daniel Veillardf012a642001-07-23 19:10:52 +00001453 NULL, headers, ilen));
Daniel Veillard9403a042001-05-28 11:00:53 +00001454}
1455
1456/**
Owen Taylor3473f882001-02-23 17:55:21 +00001457 * xmlNanoHTTPFetch:
1458 * @URL: The URL to load
1459 * @filename: the filename where the content should be saved
1460 * @contentType: if available the Content-Type information will be
1461 * returned at that location
1462 *
1463 * This function try to fetch the indicated resource via HTTP GET
1464 * and save it's content in the file.
1465 *
1466 * Returns -1 in case of failure, 0 incase of success. The contentType,
1467 * if provided must be freed by the caller
1468 */
1469int
1470xmlNanoHTTPFetch(const char *URL, const char *filename, char **contentType) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001471 void *ctxt = NULL;
1472 char *buf = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001473 int fd;
1474 int len;
1475
1476 ctxt = xmlNanoHTTPOpen(URL, contentType);
1477 if (ctxt == NULL) return(-1);
1478
1479 if (!strcmp(filename, "-"))
1480 fd = 0;
1481 else {
1482 fd = open(filename, O_CREAT | O_WRONLY, 00644);
1483 if (fd < 0) {
1484 xmlNanoHTTPClose(ctxt);
1485 if ((contentType != NULL) && (*contentType != NULL)) {
1486 xmlFree(*contentType);
1487 *contentType = NULL;
1488 }
1489 return(-1);
1490 }
1491 }
1492
Daniel Veillardf012a642001-07-23 19:10:52 +00001493 xmlNanoHTTPFetchContent( ctxt, &buf, &len );
1494 if ( len > 0 ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001495 write(fd, buf, len);
1496 }
1497
1498 xmlNanoHTTPClose(ctxt);
1499 close(fd);
1500 return(0);
1501}
1502
1503/**
1504 * xmlNanoHTTPSave:
1505 * @ctxt: the HTTP context
1506 * @filename: the filename where the content should be saved
1507 *
1508 * This function saves the output of the HTTP transaction to a file
1509 * It closes and free the context at the end
1510 *
1511 * Returns -1 in case of failure, 0 incase of success.
1512 */
1513int
1514xmlNanoHTTPSave(void *ctxt, const char *filename) {
Daniel Veillarde3924972001-07-25 20:25:21 +00001515 char *buf = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001516 int fd;
1517 int len;
1518
1519 if (ctxt == NULL) return(-1);
1520
1521 if (!strcmp(filename, "-"))
1522 fd = 0;
1523 else {
1524 fd = open(filename, O_CREAT | O_WRONLY);
1525 if (fd < 0) {
1526 xmlNanoHTTPClose(ctxt);
1527 return(-1);
1528 }
1529 }
1530
Daniel Veillardf012a642001-07-23 19:10:52 +00001531 xmlNanoHTTPFetchContent( ctxt, &buf, &len );
1532 if ( len > 0 ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001533 write(fd, buf, len);
1534 }
1535
1536 xmlNanoHTTPClose(ctxt);
1537 return(0);
1538}
1539
1540/**
1541 * xmlNanoHTTPReturnCode:
1542 * @ctx: the HTTP context
1543 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001544 * Get the latest HTTP return code received
1545 *
Owen Taylor3473f882001-02-23 17:55:21 +00001546 * Returns the HTTP return code for the request.
1547 */
1548int
1549xmlNanoHTTPReturnCode(void *ctx) {
1550 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1551
1552 if (ctxt == NULL) return(-1);
1553
1554 return(ctxt->returnValue);
1555}
1556
1557/**
1558 * xmlNanoHTTPAuthHeader:
1559 * @ctx: the HTTP context
1560 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001561 * Get the authentication header of an HTTP context
1562 *
Owen Taylor3473f882001-02-23 17:55:21 +00001563 * Returns the stashed value of the WWW-Authenticate or Proxy-Authenticate
1564 * header.
1565 */
1566const char *
1567xmlNanoHTTPAuthHeader(void *ctx) {
1568 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1569
1570 if (ctxt == NULL) return(NULL);
1571
1572 return(ctxt->authHeader);
1573}
1574
Daniel Veillardf012a642001-07-23 19:10:52 +00001575/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00001576 * xmlNanoHTTPContentLength:
Daniel Veillardf012a642001-07-23 19:10:52 +00001577 * @ctx: the HTTP context
1578 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001579 * Provides the specified content length from the HTTP header.
1580 *
Daniel Veillardf012a642001-07-23 19:10:52 +00001581 * Return the specified content length from the HTTP header. Note that
1582 * a value of -1 indicates that the content length element was not included in
1583 * the response header.
1584 */
1585int
1586xmlNanoHTTPContentLength( void * ctx ) {
1587 xmlNanoHTTPCtxtPtr ctxt = ctx;
1588
1589 return ( ( ctxt == NULL ) ? -1 : ctxt->ContentLength );
1590}
1591
1592/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00001593 * xmlNanoHTTPFetchContent:
Daniel Veillardf012a642001-07-23 19:10:52 +00001594 * @ctx: the HTTP context
1595 * @ptr: pointer to set to the content buffer.
1596 * @len: integer pointer to hold the length of the content
1597 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001598 * Check if all the content was read
1599 *
Daniel Veillardf012a642001-07-23 19:10:52 +00001600 * Returns 0 if all the content was read and available, returns
1601 * -1 if received content length was less than specified or an error
1602 * occurred.
1603 */
1604int
1605xmlNanoHTTPFetchContent( void * ctx, char ** ptr, int * len ) {
1606 xmlNanoHTTPCtxtPtr ctxt = ctx;
1607
1608 int rc = 0;
1609 int cur_lgth;
1610 int rcvd_lgth;
1611 int dummy_int;
1612 char * dummy_ptr = NULL;
1613
1614 /* Dummy up return input parameters if not provided */
1615
1616 if ( len == NULL )
1617 len = &dummy_int;
1618
1619 if ( ptr == NULL )
1620 ptr = &dummy_ptr;
1621
1622 /* But can't work without the context pointer */
1623
1624 if ( ( ctxt == NULL ) || ( ctxt->content == NULL ) ) {
1625 *len = 0;
1626 *ptr = NULL;
1627 return ( -1 );
1628 }
1629
1630 rcvd_lgth = ctxt->inptr - ctxt->content;
1631
1632 while ( (cur_lgth = xmlNanoHTTPRecv( ctxt )) > 0 ) {
1633
1634 rcvd_lgth += cur_lgth;
1635 if ( (ctxt->ContentLength > 0) && (rcvd_lgth >= ctxt->ContentLength) )
1636 break;
1637 }
1638
1639 *ptr = ctxt->content;
1640 *len = rcvd_lgth;
1641
1642 if ( ( ctxt->ContentLength > 0 ) && ( rcvd_lgth < ctxt->ContentLength ) )
1643 rc = -1;
1644 else if ( rcvd_lgth == 0 )
1645 rc = -1;
1646
1647 return ( rc );
1648}
1649
Owen Taylor3473f882001-02-23 17:55:21 +00001650#ifdef STANDALONE
1651int main(int argc, char **argv) {
1652 char *contentType = NULL;
1653
1654 if (argv[1] != NULL) {
1655 if (argv[2] != NULL)
1656 xmlNanoHTTPFetch(argv[1], argv[2], &contentType);
1657 else
1658 xmlNanoHTTPFetch(argv[1], "-", &contentType);
1659 if (contentType != NULL) xmlFree(contentType);
1660 } else {
1661 xmlGenericError(xmlGenericErrorContext,
1662 "%s: minimal HTTP GET implementation\n", argv[0]);
1663 xmlGenericError(xmlGenericErrorContext,
1664 "\tusage %s [ URL [ filename ] ]\n", argv[0]);
1665 }
1666 xmlNanoHTTPCleanup();
1667 xmlMemoryDump();
1668 return(0);
1669}
1670#endif /* STANDALONE */
1671#else /* !LIBXML_HTTP_ENABLED */
1672#ifdef STANDALONE
1673#include <stdio.h>
1674int main(int argc, char **argv) {
1675 xmlGenericError(xmlGenericErrorContext,
1676 "%s : HTTP support not compiled in\n", argv[0]);
1677 return(0);
1678}
1679#endif /* STANDALONE */
1680#endif /* LIBXML_HTTP_ENABLED */