blob: c2c4d30841db781da4fb0e8929b8065cf87ea36a [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 Veillard1638a472003-08-14 01:23:25 +000076
77#ifdef __MINGW32__
78#define _WINSOCKAPI_
79#include <wsockcompat.h>
80#include <winsock2.h>
81#undef SOCKLEN_T
82#define SOCKLEN_T unsigned int
83#endif
84
85
Daniel Veillardd0463562001-10-13 09:15:48 +000086#include <libxml/globals.h>
Daniel Veillardf012a642001-07-23 19:10:52 +000087#include <libxml/xmlerror.h>
Owen Taylor3473f882001-02-23 17:55:21 +000088#include <libxml/xmlmemory.h>
89#include <libxml/parser.h> /* for xmlStr(n)casecmp() */
90#include <libxml/nanohttp.h>
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000091#include <libxml/globals.h>
Daniel Veillard8efff672002-12-04 11:44:48 +000092#include <libxml/uri.h>
Owen Taylor3473f882001-02-23 17:55:21 +000093
94/**
95 * A couple portability macros
96 */
97#ifndef _WINSOCKAPI_
Daniel Veillarda9cce9c2003-09-29 13:20:24 +000098#ifndef __BEOS__
Owen Taylor3473f882001-02-23 17:55:21 +000099#define closesocket(s) close(s)
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000100#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000101#define SOCKET int
102#endif
103
Daniel Veillard89f7f272003-09-29 13:29:09 +0000104#ifdef __BEOS__
105#ifndef PF_INET
106#define PF_INET AF_INET
107#endif
108#endif
109
Daniel Veillard75be0132002-03-13 10:03:35 +0000110#ifndef SOCKLEN_T
111#define SOCKLEN_T unsigned int
112#endif
113#ifndef SOCKET
114#define SOCKET int
115#endif
Daniel Veillardf012a642001-07-23 19:10:52 +0000116
Owen Taylor3473f882001-02-23 17:55:21 +0000117#ifdef STANDALONE
118#define DEBUG_HTTP
119#define xmlStrncasecmp(a, b, n) strncasecmp((char *)a, (char *)b, n)
120#define xmlStrcasecmpi(a, b) strcasecmp((char *)a, (char *)b)
121#endif
122
123#define XML_NANO_HTTP_MAX_REDIR 10
124
125#define XML_NANO_HTTP_CHUNK 4096
126
127#define XML_NANO_HTTP_CLOSED 0
128#define XML_NANO_HTTP_WRITE 1
129#define XML_NANO_HTTP_READ 2
130#define XML_NANO_HTTP_NONE 4
131
132typedef struct xmlNanoHTTPCtxt {
133 char *protocol; /* the protocol name */
134 char *hostname; /* the host name */
135 int port; /* the port */
136 char *path; /* the path within the URL */
137 SOCKET fd; /* the file descriptor for the socket */
138 int state; /* WRITE / READ / CLOSED */
139 char *out; /* buffer sent (zero terminated) */
140 char *outptr; /* index within the buffer sent */
141 char *in; /* the receiving buffer */
142 char *content; /* the start of the content */
143 char *inptr; /* the next byte to read from network */
144 char *inrptr; /* the next byte to give back to the client */
145 int inlen; /* len of the input buffer */
146 int last; /* return code for last operation */
147 int returnValue; /* the protocol return value */
Daniel Veillardf012a642001-07-23 19:10:52 +0000148 int ContentLength; /* specified content length from HTTP header */
Owen Taylor3473f882001-02-23 17:55:21 +0000149 char *contentType; /* the MIME type for the input */
150 char *location; /* the new URL in case of redirect */
151 char *authHeader; /* contents of {WWW,Proxy}-Authenticate header */
152} xmlNanoHTTPCtxt, *xmlNanoHTTPCtxtPtr;
153
154static int initialized = 0;
155static char *proxy = NULL; /* the proxy name if any */
156static int proxyPort; /* the proxy port if any */
157static unsigned int timeout = 60;/* the select() timeout in seconds */
158
Daniel Veillardf012a642001-07-23 19:10:52 +0000159int xmlNanoHTTPFetchContent( void * ctx, char ** ptr, int * len );
160int xmlNanoHTTPContentLength( void * ctx );
161
Owen Taylor3473f882001-02-23 17:55:21 +0000162/**
163 * A portability function
164 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000165static int socket_errno(void) {
Owen Taylor3473f882001-02-23 17:55:21 +0000166#ifdef _WINSOCKAPI_
167 return(WSAGetLastError());
168#else
169 return(errno);
170#endif
171}
172
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000173#ifdef SUPPORT_IP6
Daniel Veillard2db8c122003-07-08 12:16:59 +0000174static
175int have_ipv6(void) {
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000176 int s;
177
178 s = socket (AF_INET6, SOCK_STREAM, 0);
179 if (s != -1) {
180 close (s);
181 return (1);
182 }
183 return (0);
184}
185#endif
186
Owen Taylor3473f882001-02-23 17:55:21 +0000187/**
188 * xmlNanoHTTPInit:
189 *
190 * Initialize the HTTP protocol layer.
191 * Currently it just checks for proxy informations
192 */
193
194void
195xmlNanoHTTPInit(void) {
196 const char *env;
197#ifdef _WINSOCKAPI_
198 WSADATA wsaData;
199#endif
200
201 if (initialized)
202 return;
203
204#ifdef _WINSOCKAPI_
205 if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0)
206 return;
207#endif
208
209 if (proxy == NULL) {
210 proxyPort = 80;
211 env = getenv("no_proxy");
212 if (env != NULL)
213 goto done;
214 env = getenv("http_proxy");
215 if (env != NULL) {
216 xmlNanoHTTPScanProxy(env);
217 goto done;
218 }
219 env = getenv("HTTP_PROXY");
220 if (env != NULL) {
221 xmlNanoHTTPScanProxy(env);
222 goto done;
223 }
224 }
225done:
226 initialized = 1;
227}
228
229/**
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000230 * xmlNanoHTTPCleanup:
Owen Taylor3473f882001-02-23 17:55:21 +0000231 *
232 * Cleanup the HTTP protocol layer.
233 */
234
235void
236xmlNanoHTTPCleanup(void) {
237 if (proxy != NULL)
238 xmlFree(proxy);
239#ifdef _WINSOCKAPI_
240 if (initialized)
241 WSACleanup();
242#endif
243 initialized = 0;
244 return;
245}
246
247/**
Owen Taylor3473f882001-02-23 17:55:21 +0000248 * xmlNanoHTTPScanURL:
249 * @ctxt: an HTTP context
250 * @URL: The URL used to initialize the context
251 *
252 * (Re)Initialize an HTTP context by parsing the URL and finding
253 * the protocol host port and path it indicates.
254 */
255
256static void
257xmlNanoHTTPScanURL(xmlNanoHTTPCtxtPtr ctxt, const char *URL) {
258 const char *cur = URL;
259 char buf[4096];
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000260 int indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000261 int port = 0;
262
263 if (ctxt->protocol != NULL) {
264 xmlFree(ctxt->protocol);
265 ctxt->protocol = NULL;
266 }
267 if (ctxt->hostname != NULL) {
268 xmlFree(ctxt->hostname);
269 ctxt->hostname = NULL;
270 }
271 if (ctxt->path != NULL) {
272 xmlFree(ctxt->path);
273 ctxt->path = NULL;
274 }
275 if (URL == NULL) return;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000276 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000277 while (*cur != 0) {
278 if ((cur[0] == ':') && (cur[1] == '/') && (cur[2] == '/')) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000279 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000280 ctxt->protocol = xmlMemStrdup(buf);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000281 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000282 cur += 3;
283 break;
284 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000285 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000286 }
287 if (*cur == 0) return;
288
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000289 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000290 while (1) {
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000291 if ((strchr (cur, '[') && !strchr (cur, ']')) ||
292 (!strchr (cur, '[') && strchr (cur, ']'))) {
293 xmlGenericError (xmlGenericErrorContext, "\nxmlNanoHTTPScanURL: %s",
294 "Syntax Error\n");
295 return;
296 }
297
298 if (cur[0] == '[') {
299 cur++;
300 while (cur[0] != ']')
301 buf[indx++] = *cur++;
302
303 if (!strchr (buf, ':')) {
304 xmlGenericError (xmlGenericErrorContext, "\nxmlNanoHTTPScanURL: %s",
305 "Use [IPv6]/IPv4 format\n");
306 return;
307 }
308
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000309 buf[indx] = 0;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000310 ctxt->hostname = xmlMemStrdup (buf);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000311 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000312 cur += 1;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000313 if (cur[0] == ':') {
Owen Taylor3473f882001-02-23 17:55:21 +0000314 cur++;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000315 while (*cur >= '0' && *cur <= '9') {
316 port *= 10;
317 port += *cur - '0';
318 cur++;
319 }
320
321 if (port != 0) ctxt->port = port;
322 while ((cur[0] != '/') && (*cur != 0))
323 cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000324 }
Owen Taylor3473f882001-02-23 17:55:21 +0000325 break;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000326 }
327 else {
328 if (cur[0] == ':') {
329 buf[indx] = 0;
330 ctxt->hostname = xmlMemStrdup (buf);
331 indx = 0;
332 cur += 1;
333 while ((*cur >= '0') && (*cur <= '9')) {
334 port *= 10;
335 port += *cur - '0';
336 cur++;
337 }
338 if (port != 0) ctxt->port = port;
339 while ((cur[0] != '/') && (*cur != 0))
340 cur++;
341 break;
342 }
343 if ((*cur == '/') || (*cur == 0)) {
344 buf[indx] = 0;
345 ctxt->hostname = xmlMemStrdup (buf);
346 indx = 0;
347 break;
348 }
Owen Taylor3473f882001-02-23 17:55:21 +0000349 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000350 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000351 }
352 if (*cur == 0)
353 ctxt->path = xmlMemStrdup("/");
354 else {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000355 indx = 0;
356 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000357 while (*cur != 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000358 buf[indx++] = *cur++;
359 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000360 ctxt->path = xmlMemStrdup(buf);
361 }
362}
363
364/**
365 * xmlNanoHTTPScanProxy:
366 * @URL: The proxy URL used to initialize the proxy context
367 *
368 * (Re)Initialize the HTTP Proxy context by parsing the URL and finding
369 * the protocol host port it indicates.
370 * Should be like http://myproxy/ or http://myproxy:3128/
371 * A NULL URL cleans up proxy informations.
372 */
373
374void
375xmlNanoHTTPScanProxy(const char *URL) {
376 const char *cur = URL;
377 char buf[4096];
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000378 int indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000379 int port = 0;
380
381 if (proxy != NULL) {
382 xmlFree(proxy);
383 proxy = NULL;
384 }
385 if (proxyPort != 0) {
386 proxyPort = 0;
387 }
388#ifdef DEBUG_HTTP
389 if (URL == NULL)
390 xmlGenericError(xmlGenericErrorContext,
391 "Removing HTTP proxy info\n");
392 else
393 xmlGenericError(xmlGenericErrorContext,
394 "Using HTTP proxy %s\n", URL);
395#endif
396 if (URL == NULL) return;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000397 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000398 while (*cur != 0) {
399 if ((cur[0] == ':') && (cur[1] == '/') && (cur[2] == '/')) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000400 buf[indx] = 0;
401 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000402 cur += 3;
403 break;
404 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000405 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000406 }
407 if (*cur == 0) return;
408
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000409 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000410 while (1) {
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000411 if ((strchr (cur, '[') && !strchr (cur, ']')) ||
412 (!strchr (cur, '[') && strchr (cur, ']'))) {
413 xmlGenericError (xmlGenericErrorContext, "\nxmlNanoHTTPScanProxy: %s",
414 "Syntax error\n");
415 return;
416 }
417
418 if (cur[0] == '[') {
419 cur++;
420 while (cur[0] != ']')
421 buf[indx++] = *cur++;
422
423 if (!strchr (buf, ':')) {
424 xmlGenericError (xmlGenericErrorContext, "\nxmlNanoHTTPScanProxy: %s",
425 "Use [IPv6]/IPv4 format\n");
426 return;
427 }
428
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000429 buf[indx] = 0;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000430 proxy = xmlMemStrdup (buf);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000431 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000432 cur += 1;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000433 if (cur[0] == ':') {
Owen Taylor3473f882001-02-23 17:55:21 +0000434 cur++;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000435 while (*cur >= '0' && *cur <= '9') {
436 port *= 10;
437 port += *cur - '0';
438 cur++;
439 }
440
441 if (port != 0) proxyPort = port;
442 while ((cur[0] != '/') && (*cur != 0))
443 cur ++;
444 }
Owen Taylor3473f882001-02-23 17:55:21 +0000445 break;
446 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000447 else {
448 if (cur[0] == ':') {
449 buf[indx] = 0;
450 proxy = xmlMemStrdup (buf);
451 indx = 0;
452 cur += 1;
453 while ((*cur >= '0') && (*cur <= '9')) {
454 port *= 10;
455 port += *cur - '0';
456 cur++;
457 }
458 if (port != 0) proxyPort = port;
459 while ((cur[0] != '/') && (*cur != 0))
460 cur++;
461 break;
462 }
463 if ((*cur == '/') || (*cur == 0)) {
464 buf[indx] = 0;
465 proxy = xmlMemStrdup (buf);
466 indx = 0;
467 break;
468 }
Owen Taylor3473f882001-02-23 17:55:21 +0000469 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000470 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000471 }
472}
473
474/**
475 * xmlNanoHTTPNewCtxt:
476 * @URL: The URL used to initialize the context
477 *
478 * Allocate and initialize a new HTTP context.
479 *
480 * Returns an HTTP context or NULL in case of error.
481 */
482
483static xmlNanoHTTPCtxtPtr
484xmlNanoHTTPNewCtxt(const char *URL) {
485 xmlNanoHTTPCtxtPtr ret;
486
487 ret = (xmlNanoHTTPCtxtPtr) xmlMalloc(sizeof(xmlNanoHTTPCtxt));
488 if (ret == NULL) return(NULL);
489
490 memset(ret, 0, sizeof(xmlNanoHTTPCtxt));
491 ret->port = 80;
492 ret->returnValue = 0;
493 ret->fd = -1;
Daniel Veillardf012a642001-07-23 19:10:52 +0000494 ret->ContentLength = -1;
Owen Taylor3473f882001-02-23 17:55:21 +0000495
Daniel Veillardcacbe5d2003-01-10 16:09:51 +0000496 xmlNanoHTTPScanURL(ret, URL);
Owen Taylor3473f882001-02-23 17:55:21 +0000497
498 return(ret);
499}
500
501/**
502 * xmlNanoHTTPFreeCtxt:
503 * @ctxt: an HTTP context
504 *
505 * Frees the context after closing the connection.
506 */
507
508static void
509xmlNanoHTTPFreeCtxt(xmlNanoHTTPCtxtPtr ctxt) {
510 if (ctxt == NULL) return;
511 if (ctxt->hostname != NULL) xmlFree(ctxt->hostname);
512 if (ctxt->protocol != NULL) xmlFree(ctxt->protocol);
513 if (ctxt->path != NULL) xmlFree(ctxt->path);
514 if (ctxt->out != NULL) xmlFree(ctxt->out);
515 if (ctxt->in != NULL) xmlFree(ctxt->in);
516 if (ctxt->contentType != NULL) xmlFree(ctxt->contentType);
517 if (ctxt->location != NULL) xmlFree(ctxt->location);
518 if (ctxt->authHeader != NULL) xmlFree(ctxt->authHeader);
519 ctxt->state = XML_NANO_HTTP_NONE;
520 if (ctxt->fd >= 0) closesocket(ctxt->fd);
521 ctxt->fd = -1;
522 xmlFree(ctxt);
523}
524
525/**
526 * xmlNanoHTTPSend:
527 * @ctxt: an HTTP context
528 *
529 * Send the input needed to initiate the processing on the server side
Daniel Veillardf012a642001-07-23 19:10:52 +0000530 * Returns number of bytes sent or -1 on error.
Owen Taylor3473f882001-02-23 17:55:21 +0000531 */
532
Daniel Veillardf012a642001-07-23 19:10:52 +0000533static int
534xmlNanoHTTPSend(xmlNanoHTTPCtxtPtr ctxt, const char * xmt_ptr, int outlen) {
535
536 int total_sent = 0;
537
538 if ( (ctxt->state & XML_NANO_HTTP_WRITE) && (xmt_ptr != NULL ) ) {
539 while (total_sent < outlen) {
540 int nsent = send(ctxt->fd, xmt_ptr + total_sent,
541 outlen - total_sent, 0);
Owen Taylor3473f882001-02-23 17:55:21 +0000542 if (nsent>0)
543 total_sent += nsent;
Daniel Veillardf012a642001-07-23 19:10:52 +0000544 else if ( ( nsent == -1 ) &&
Daniel Veillardba6db032001-07-31 16:25:45 +0000545#if defined(EAGAIN) && EAGAIN != EWOULDBLOCK
Daniel Veillardf012a642001-07-23 19:10:52 +0000546 ( socket_errno( ) != EAGAIN ) &&
Daniel Veillardba6db032001-07-31 16:25:45 +0000547#endif
Daniel Veillardf012a642001-07-23 19:10:52 +0000548 ( socket_errno( ) != EWOULDBLOCK ) ) {
549 xmlGenericError( xmlGenericErrorContext,
550 "xmlNanoHTTPSend error: %s",
551 strerror( socket_errno( ) ) );
552
553 if ( total_sent == 0 )
554 total_sent = -1;
555 break;
556 }
557 else {
558 /*
559 ** No data sent
560 ** Since non-blocking sockets are used, wait for
561 ** socket to be writable or default timeout prior
562 ** to retrying.
563 */
564
565 struct timeval tv;
566 fd_set wfd;
567
568 tv.tv_sec = timeout;
569 tv.tv_usec = 0;
570 FD_ZERO( &wfd );
571 FD_SET( ctxt->fd, &wfd );
572 (void)select( ctxt->fd + 1, NULL, &wfd, NULL, &tv );
573 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000574 }
Owen Taylor3473f882001-02-23 17:55:21 +0000575 }
Daniel Veillardf012a642001-07-23 19:10:52 +0000576
577 return total_sent;
Owen Taylor3473f882001-02-23 17:55:21 +0000578}
579
580/**
581 * xmlNanoHTTPRecv:
582 * @ctxt: an HTTP context
583 *
584 * Read information coming from the HTTP connection.
585 * This is a blocking call (but it blocks in select(), not read()).
586 *
587 * Returns the number of byte read or -1 in case of error.
588 */
589
590static int
591xmlNanoHTTPRecv(xmlNanoHTTPCtxtPtr ctxt) {
592 fd_set rfd;
593 struct timeval tv;
594
595
596 while (ctxt->state & XML_NANO_HTTP_READ) {
597 if (ctxt->in == NULL) {
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000598 ctxt->in = (char *) xmlMallocAtomic(65000 * sizeof(char));
Owen Taylor3473f882001-02-23 17:55:21 +0000599 if (ctxt->in == NULL) {
600 ctxt->last = -1;
Daniel Veillardf012a642001-07-23 19:10:52 +0000601 xmlGenericError( xmlGenericErrorContext,
602 "xmlNanoHTTPRecv: Error allocating input memory." );
Owen Taylor3473f882001-02-23 17:55:21 +0000603 return(-1);
604 }
605 ctxt->inlen = 65000;
606 ctxt->inptr = ctxt->content = ctxt->inrptr = ctxt->in;
607 }
608 if (ctxt->inrptr > ctxt->in + XML_NANO_HTTP_CHUNK) {
609 int delta = ctxt->inrptr - ctxt->in;
610 int len = ctxt->inptr - ctxt->inrptr;
611
612 memmove(ctxt->in, ctxt->inrptr, len);
613 ctxt->inrptr -= delta;
614 ctxt->content -= delta;
615 ctxt->inptr -= delta;
616 }
617 if ((ctxt->in + ctxt->inlen) < (ctxt->inptr + XML_NANO_HTTP_CHUNK)) {
618 int d_inptr = ctxt->inptr - ctxt->in;
619 int d_content = ctxt->content - ctxt->in;
620 int d_inrptr = ctxt->inrptr - ctxt->in;
Daniel Veillardf012a642001-07-23 19:10:52 +0000621 char * tmp_ptr = ctxt->in;
Owen Taylor3473f882001-02-23 17:55:21 +0000622
623 ctxt->inlen *= 2;
Daniel Veillardf012a642001-07-23 19:10:52 +0000624 ctxt->in = (char *) xmlRealloc(tmp_ptr, ctxt->inlen);
Owen Taylor3473f882001-02-23 17:55:21 +0000625 if (ctxt->in == NULL) {
Daniel Veillardf012a642001-07-23 19:10:52 +0000626 xmlGenericError( xmlGenericErrorContext,
627 "xmlNanoHTTPRecv: %s %d bytes.",
628 "Failed to realloc input buffer to",
629 ctxt->inlen );
630 xmlFree( tmp_ptr );
Owen Taylor3473f882001-02-23 17:55:21 +0000631 ctxt->last = -1;
632 return(-1);
633 }
634 ctxt->inptr = ctxt->in + d_inptr;
635 ctxt->content = ctxt->in + d_content;
636 ctxt->inrptr = ctxt->in + d_inrptr;
637 }
638 ctxt->last = recv(ctxt->fd, ctxt->inptr, XML_NANO_HTTP_CHUNK, 0);
639 if (ctxt->last > 0) {
640 ctxt->inptr += ctxt->last;
641 return(ctxt->last);
642 }
643 if (ctxt->last == 0) {
644 return(0);
645 }
646 if (ctxt->last == -1) {
647 switch (socket_errno()) {
648 case EINPROGRESS:
649 case EWOULDBLOCK:
650#if defined(EAGAIN) && EAGAIN != EWOULDBLOCK
651 case EAGAIN:
652#endif
653 break;
Daniel Veillardf012a642001-07-23 19:10:52 +0000654
655 case ECONNRESET:
656 case ESHUTDOWN:
657 return ( 0 );
658
Owen Taylor3473f882001-02-23 17:55:21 +0000659 default:
Daniel Veillardf012a642001-07-23 19:10:52 +0000660 xmlGenericError( xmlGenericErrorContext,
661 "xmlNanoHTTPRecv: recv( ) failure - %s",
662 strerror( socket_errno( ) ) );
663 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +0000664 }
665 }
666
667 tv.tv_sec = timeout;
668 tv.tv_usec = 0;
669 FD_ZERO(&rfd);
670 FD_SET(ctxt->fd, &rfd);
671
Daniel Veillard50f34372001-08-03 12:06:36 +0000672 if ( (select(ctxt->fd+1, &rfd, NULL, NULL, &tv)<1)
673#if defined(EINTR)
674 && (errno != EINTR)
675#endif
676 )
Owen Taylor3473f882001-02-23 17:55:21 +0000677 return(0);
678 }
679 return(0);
680}
681
682/**
683 * xmlNanoHTTPReadLine:
684 * @ctxt: an HTTP context
685 *
686 * Read one line in the HTTP server output, usually for extracting
687 * the HTTP protocol informations from the answer header.
688 *
689 * Returns a newly allocated string with a copy of the line, or NULL
690 * which indicate the end of the input.
691 */
692
693static char *
694xmlNanoHTTPReadLine(xmlNanoHTTPCtxtPtr ctxt) {
695 char buf[4096];
696 char *bp = buf;
Daniel Veillardf012a642001-07-23 19:10:52 +0000697 int rc;
Owen Taylor3473f882001-02-23 17:55:21 +0000698
699 while (bp - buf < 4095) {
700 if (ctxt->inrptr == ctxt->inptr) {
Daniel Veillardf012a642001-07-23 19:10:52 +0000701 if ( (rc = xmlNanoHTTPRecv(ctxt)) == 0) {
Owen Taylor3473f882001-02-23 17:55:21 +0000702 if (bp == buf)
703 return(NULL);
704 else
705 *bp = 0;
706 return(xmlMemStrdup(buf));
707 }
Daniel Veillardf012a642001-07-23 19:10:52 +0000708 else if ( rc == -1 ) {
709 return ( NULL );
710 }
Owen Taylor3473f882001-02-23 17:55:21 +0000711 }
712 *bp = *ctxt->inrptr++;
713 if (*bp == '\n') {
714 *bp = 0;
715 return(xmlMemStrdup(buf));
716 }
717 if (*bp != '\r')
718 bp++;
719 }
720 buf[4095] = 0;
721 return(xmlMemStrdup(buf));
722}
723
724
725/**
726 * xmlNanoHTTPScanAnswer:
727 * @ctxt: an HTTP context
728 * @line: an HTTP header line
729 *
730 * Try to extract useful informations from the server answer.
731 * We currently parse and process:
732 * - The HTTP revision/ return code
733 * - The Content-Type
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000734 * - The Location for redirect processing.
Owen Taylor3473f882001-02-23 17:55:21 +0000735 *
736 * Returns -1 in case of failure, the file descriptor number otherwise
737 */
738
739static void
740xmlNanoHTTPScanAnswer(xmlNanoHTTPCtxtPtr ctxt, const char *line) {
741 const char *cur = line;
742
743 if (line == NULL) return;
744
745 if (!strncmp(line, "HTTP/", 5)) {
746 int version = 0;
747 int ret = 0;
748
749 cur += 5;
750 while ((*cur >= '0') && (*cur <= '9')) {
751 version *= 10;
752 version += *cur - '0';
753 cur++;
754 }
755 if (*cur == '.') {
756 cur++;
757 if ((*cur >= '0') && (*cur <= '9')) {
758 version *= 10;
759 version += *cur - '0';
760 cur++;
761 }
762 while ((*cur >= '0') && (*cur <= '9'))
763 cur++;
764 } else
765 version *= 10;
766 if ((*cur != ' ') && (*cur != '\t')) return;
767 while ((*cur == ' ') || (*cur == '\t')) cur++;
768 if ((*cur < '0') || (*cur > '9')) return;
769 while ((*cur >= '0') && (*cur <= '9')) {
770 ret *= 10;
771 ret += *cur - '0';
772 cur++;
773 }
774 if ((*cur != 0) && (*cur != ' ') && (*cur != '\t')) return;
775 ctxt->returnValue = ret;
776 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Content-Type:", 13)) {
777 cur += 13;
778 while ((*cur == ' ') || (*cur == '\t')) cur++;
779 if (ctxt->contentType != NULL)
780 xmlFree(ctxt->contentType);
781 ctxt->contentType = xmlMemStrdup(cur);
782 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"ContentType:", 12)) {
783 cur += 12;
784 if (ctxt->contentType != NULL) return;
785 while ((*cur == ' ') || (*cur == '\t')) cur++;
786 ctxt->contentType = xmlMemStrdup(cur);
787 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Location:", 9)) {
788 cur += 9;
789 while ((*cur == ' ') || (*cur == '\t')) cur++;
790 if (ctxt->location != NULL)
791 xmlFree(ctxt->location);
792 ctxt->location = xmlMemStrdup(cur);
793 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"WWW-Authenticate:", 17)) {
794 cur += 17;
795 while ((*cur == ' ') || (*cur == '\t')) cur++;
796 if (ctxt->authHeader != NULL)
797 xmlFree(ctxt->authHeader);
798 ctxt->authHeader = xmlMemStrdup(cur);
799 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Proxy-Authenticate:", 19)) {
800 cur += 19;
801 while ((*cur == ' ') || (*cur == '\t')) cur++;
802 if (ctxt->authHeader != NULL)
803 xmlFree(ctxt->authHeader);
804 ctxt->authHeader = xmlMemStrdup(cur);
Daniel Veillardf012a642001-07-23 19:10:52 +0000805 } else if ( !xmlStrncasecmp( BAD_CAST line, BAD_CAST"Content-Length:", 15) ) {
806 cur += 15;
807 ctxt->ContentLength = strtol( cur, NULL, 10 );
Owen Taylor3473f882001-02-23 17:55:21 +0000808 }
809}
810
811/**
812 * xmlNanoHTTPConnectAttempt:
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000813 * @addr: a socket address structure
Owen Taylor3473f882001-02-23 17:55:21 +0000814 *
815 * Attempt a connection to the given IP:port endpoint. It forces
816 * non-blocking semantic on the socket, and allow 60 seconds for
817 * the host to answer.
818 *
819 * Returns -1 in case of failure, the file descriptor number otherwise
820 */
821
822static int
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000823xmlNanoHTTPConnectAttempt(struct sockaddr *addr)
Owen Taylor3473f882001-02-23 17:55:21 +0000824{
Owen Taylor3473f882001-02-23 17:55:21 +0000825 fd_set wfd;
826 struct timeval tv;
827 int status;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000828 int addrlen;
829 SOCKET s;
Owen Taylor3473f882001-02-23 17:55:21 +0000830
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000831#ifdef SUPPORT_IP6
832 if (addr->sa_family == AF_INET6) {
833 s = socket (PF_INET6, SOCK_STREAM, IPPROTO_TCP);
834 addrlen = sizeof (struct sockaddr_in6);
835 }
836 else
837#endif
838 {
839 s = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
840 addrlen = sizeof (struct sockaddr_in);
841 }
Owen Taylor3473f882001-02-23 17:55:21 +0000842 if (s==-1) {
843#ifdef DEBUG_HTTP
844 perror("socket");
845#endif
Daniel Veillardf012a642001-07-23 19:10:52 +0000846 xmlGenericError( xmlGenericErrorContext,
847 "xmlNanoHTTPConnectAttempt: %s - %s",
848 "socket creation failure",
849 strerror( socket_errno( ) ) );
Owen Taylor3473f882001-02-23 17:55:21 +0000850 return(-1);
851 }
852
853#ifdef _WINSOCKAPI_
854 {
855 u_long one = 1;
856
857 status = ioctlsocket(s, FIONBIO, &one) == SOCKET_ERROR ? -1 : 0;
858 }
859#else /* _WINSOCKAPI_ */
860#if defined(VMS)
861 {
862 int enable = 1;
863 status = ioctl(s, FIONBIO, &enable);
864 }
865#else /* VMS */
866 if ((status = fcntl(s, F_GETFL, 0)) != -1) {
867#ifdef O_NONBLOCK
868 status |= O_NONBLOCK;
869#else /* O_NONBLOCK */
870#ifdef F_NDELAY
871 status |= F_NDELAY;
872#endif /* F_NDELAY */
873#endif /* !O_NONBLOCK */
874 status = fcntl(s, F_SETFL, status);
875 }
876 if (status < 0) {
877#ifdef DEBUG_HTTP
878 perror("nonblocking");
879#endif
Daniel Veillardf012a642001-07-23 19:10:52 +0000880 xmlGenericError( xmlGenericErrorContext,
881 "xmlNanoHTTPConnectAttempt: %s - %s",
882 "error setting non-blocking IO",
883 strerror( socket_errno( ) ) );
Owen Taylor3473f882001-02-23 17:55:21 +0000884 closesocket(s);
885 return(-1);
886 }
887#endif /* !VMS */
888#endif /* !_WINSOCKAPI_ */
889
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000890 if (connect (s, addr, addrlen) == -1) {
Owen Taylor3473f882001-02-23 17:55:21 +0000891 switch (socket_errno()) {
892 case EINPROGRESS:
893 case EWOULDBLOCK:
894 break;
895 default:
Daniel Veillardf012a642001-07-23 19:10:52 +0000896 xmlGenericError( xmlGenericErrorContext,
897 "xmlNanoHTTPConnectAttempt: %s - %s",
898 "error connecting to HTTP server",
899 strerror( socket_errno( ) ) );
Owen Taylor3473f882001-02-23 17:55:21 +0000900 closesocket(s);
901 return(-1);
902 }
903 }
904
905 tv.tv_sec = timeout;
906 tv.tv_usec = 0;
907
908 FD_ZERO(&wfd);
909 FD_SET(s, &wfd);
910
911 switch(select(s+1, NULL, &wfd, NULL, &tv))
912 {
913 case 0:
914 /* Time out */
Daniel Veillardf012a642001-07-23 19:10:52 +0000915 xmlGenericError( xmlGenericErrorContext,
916 "xmlNanoHTTPConnectAttempt: %s",
917 "Connect attempt timed out." );
Owen Taylor3473f882001-02-23 17:55:21 +0000918 closesocket(s);
919 return(-1);
920 case -1:
921 /* Ermm.. ?? */
Daniel Veillardf012a642001-07-23 19:10:52 +0000922 xmlGenericError( xmlGenericErrorContext,
923 "xmlNanoHTTPConnectAttempt: %s - %s",
924 "Error connecting to host",
925 strerror( socket_errno( ) ) );
Owen Taylor3473f882001-02-23 17:55:21 +0000926 closesocket(s);
927 return(-1);
928 }
929
930 if ( FD_ISSET(s, &wfd) ) {
931 SOCKLEN_T len;
932 len = sizeof(status);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000933#ifdef SO_ERROR
Owen Taylor3473f882001-02-23 17:55:21 +0000934 if (getsockopt(s, SOL_SOCKET, SO_ERROR, (char*)&status, &len) < 0 ) {
935 /* Solaris error code */
Daniel Veillardf012a642001-07-23 19:10:52 +0000936 xmlGenericError( xmlGenericErrorContext,
937 "xmlNanoHTTPConnectAttempt: %s - %s",
938 "Error retrieving pending socket errors",
939 strerror( socket_errno( ) ) );
Owen Taylor3473f882001-02-23 17:55:21 +0000940 return (-1);
941 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000942#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000943 if ( status ) {
944 closesocket(s);
945 errno = status;
Daniel Veillardf012a642001-07-23 19:10:52 +0000946 xmlGenericError( xmlGenericErrorContext,
947 "xmlNanoHTTPConnectAttempt: %s - %s",
948 "Error connecting to remote host",
949 strerror( status ) );
Owen Taylor3473f882001-02-23 17:55:21 +0000950 return (-1);
951 }
952 } else {
953 /* pbm */
Daniel Veillardf012a642001-07-23 19:10:52 +0000954 xmlGenericError( xmlGenericErrorContext,
955 "xmlNanoHTTPConnectAttempt: %s\n",
956 "Select returned, but descriptor not set for connection.\n" );
957 closesocket(s);
Owen Taylor3473f882001-02-23 17:55:21 +0000958 return (-1);
959 }
960
961 return(s);
962}
963
964/**
965 * xmlNanoHTTPConnectHost:
966 * @host: the host name
967 * @port: the port number
968 *
969 * Attempt a connection to the given host:port endpoint. It tries
970 * the multiple IP provided by the DNS if available.
971 *
972 * Returns -1 in case of failure, the file descriptor number otherwise
973 */
974
975static int
976xmlNanoHTTPConnectHost(const char *host, int port)
977{
978 struct hostent *h;
Daniel Veillard2db8c122003-07-08 12:16:59 +0000979 struct sockaddr *addr = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +0000980 struct in_addr ia;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000981 struct sockaddr_in sockin;
Daniel Veillard5c396542002-03-15 07:57:50 +0000982
Owen Taylor3473f882001-02-23 17:55:21 +0000983#ifdef SUPPORT_IP6
984 struct in6_addr ia6;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000985 struct sockaddr_in6 sockin6;
Owen Taylor3473f882001-02-23 17:55:21 +0000986#endif
987 int i;
988 int s;
Daniel Veillard5c396542002-03-15 07:57:50 +0000989
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000990 memset (&sockin, 0, sizeof(sockin));
991#ifdef SUPPORT_IP6
992 memset (&sockin6, 0, sizeof(sockin6));
993 if (have_ipv6 ())
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000994#if !defined(HAVE_GETADDRINFO) && defined(RES_USE_INET6)
Daniel Veillard560c2a42003-07-06 21:13:49 +0000995 {
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000996 if (!(_res.options & RES_INIT))
997 res_init();
998 _res.options |= RES_USE_INET6;
999 }
1000#elif defined(HAVE_GETADDRINFO)
Daniel Veillard560c2a42003-07-06 21:13:49 +00001001 {
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001002 int status;
1003 struct addrinfo hints, *res, *result;
1004
1005 result = NULL;
1006 memset (&hints, 0,sizeof(hints));
1007 hints.ai_socktype = SOCK_STREAM;
1008
1009 status = getaddrinfo (host, NULL, &hints, &result);
1010 if (status) {
1011 xmlGenericError (xmlGenericErrorContext,
1012 "xmlNanoHTTPConnectHost: %s '%s' - %s",
1013 "Failed to resolve host", host, gai_strerror (status));
1014
1015 return (-1);
1016 }
1017
1018 for (res = result; res; res = res->ai_next) {
Daniel Veillard3dc93a42003-07-10 14:04:33 +00001019 if (res->ai_family == AF_INET || res->ai_family == AF_INET6) {
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001020 if (res->ai_family == AF_INET6) {
1021 memcpy (&sockin6, res->ai_addr, res->ai_addrlen);
1022 sockin6.sin6_port = htons (port);
1023 addr = (struct sockaddr *)&sockin6;
1024 }
Daniel Veillard3dc93a42003-07-10 14:04:33 +00001025 else {
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001026 memcpy (&sockin, res->ai_addr, res->ai_addrlen);
1027 sockin.sin_port = htons (port);
1028 addr = (struct sockaddr *)&sockin;
1029 }
1030
1031 s = xmlNanoHTTPConnectAttempt (addr);
1032 if (s != -1) {
1033 freeaddrinfo (result);
1034 return (s);
1035 }
1036 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001037 }
Daniel Veillard3dc93a42003-07-10 14:04:33 +00001038 if (result)
1039 freeaddrinfo (result);
1040 return (-1);
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001041 } else
Owen Taylor3473f882001-02-23 17:55:21 +00001042#endif
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001043#endif
1044 {
1045 h = gethostbyname (host);
1046 if (h == NULL) {
Daniel Veillard56b2db72002-03-25 16:35:28 +00001047
1048/*
1049 * Okay, I got fed up by the non-portability of this error message
1050 * extraction code. it work on Linux, if it work on your platform
1051 * and one want to enable it, send me the defined(foobar) needed
1052 */
1053#if defined(HAVE_NETDB_H) && defined(HOST_NOT_FOUND) && defined(linux)
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001054 const char *h_err_txt = "";
Daniel Veillardf012a642001-07-23 19:10:52 +00001055
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001056 switch (h_errno) {
1057 case HOST_NOT_FOUND:
1058 h_err_txt = "Authoritive host not found";
1059 break;
Daniel Veillardf012a642001-07-23 19:10:52 +00001060
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001061 case TRY_AGAIN:
1062 h_err_txt =
1063 "Non-authoritive host not found or server failure.";
1064 break;
Daniel Veillardf012a642001-07-23 19:10:52 +00001065
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001066 case NO_RECOVERY:
1067 h_err_txt =
1068 "Non-recoverable errors: FORMERR, REFUSED, or NOTIMP.";
1069 break;
Daniel Veillard5c396542002-03-15 07:57:50 +00001070
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001071 case NO_ADDRESS:
1072 h_err_txt =
1073 "Valid name, no data record of requested type.";
1074 break;
Daniel Veillard5c396542002-03-15 07:57:50 +00001075
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001076 default:
1077 h_err_txt = "No error text defined.";
1078 break;
1079 }
1080 xmlGenericError (xmlGenericErrorContext,
1081 "xmlNanoHTTPConnectHost: %s '%s' - %s",
1082 "Failed to resolve host", host, h_err_txt);
Daniel Veillard5c396542002-03-15 07:57:50 +00001083#else
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001084 xmlGenericError (xmlGenericErrorContext,
1085 "xmlNanoHTTPConnectHost: %s '%s'",
1086 "Failed to resolve host", host);
Owen Taylor3473f882001-02-23 17:55:21 +00001087#endif
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001088 return (-1);
1089 }
Daniel Veillard5c396542002-03-15 07:57:50 +00001090
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001091 for (i = 0; h->h_addr_list[i]; i++) {
1092 if (h->h_addrtype == AF_INET) {
1093 /* A records (IPv4) */
1094 memcpy (&ia, h->h_addr_list[i], h->h_length);
1095 sockin.sin_family = h->h_addrtype;
1096 sockin.sin_addr = ia;
1097 sockin.sin_port = htons (port);
1098 addr = (struct sockaddr *) &sockin;
Daniel Veillard5c396542002-03-15 07:57:50 +00001099#ifdef SUPPORT_IP6
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001100 } else if (have_ipv6 () && (h->h_addrtype == AF_INET6)) {
1101 /* AAAA records (IPv6) */
1102 memcpy (&ia6, h->h_addr_list[i], h->h_length);
1103 sockin6.sin6_family = h->h_addrtype;
1104 sockin6.sin6_addr = ia6;
1105 sockin6.sin6_port = htons (port);
1106 addr = (struct sockaddr *) &sockin6;
Daniel Veillard5c396542002-03-15 07:57:50 +00001107#endif
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001108 } else
1109 break; /* for */
Daniel Veillard5c396542002-03-15 07:57:50 +00001110
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001111 s = xmlNanoHTTPConnectAttempt (addr);
1112 if (s != -1)
1113 return (s);
1114 }
Owen Taylor3473f882001-02-23 17:55:21 +00001115 }
Owen Taylor3473f882001-02-23 17:55:21 +00001116#ifdef DEBUG_HTTP
1117 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard5c396542002-03-15 07:57:50 +00001118 "xmlNanoHTTPConnectHost: unable to connect to '%s'.\n",
1119 host);
Owen Taylor3473f882001-02-23 17:55:21 +00001120#endif
Daniel Veillard5c396542002-03-15 07:57:50 +00001121 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001122}
1123
1124
1125/**
1126 * xmlNanoHTTPOpen:
1127 * @URL: The URL to load
1128 * @contentType: if available the Content-Type information will be
1129 * returned at that location
1130 *
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*
1139xmlNanoHTTPOpen(const char *URL, char **contentType) {
1140 if (contentType != NULL) *contentType = NULL;
Daniel Veillardf012a642001-07-23 19:10:52 +00001141 return(xmlNanoHTTPMethod(URL, NULL, NULL, contentType, NULL, 0));
Daniel Veillard9403a042001-05-28 11:00:53 +00001142}
1143
1144/**
1145 * xmlNanoHTTPOpenRedir:
1146 * @URL: The URL to load
1147 * @contentType: if available the Content-Type information will be
1148 * returned at that location
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001149 * @redir: if available the redirected URL will be returned
Daniel Veillard9403a042001-05-28 11:00:53 +00001150 *
1151 * This function try to open a connection to the indicated resource
1152 * via HTTP GET.
1153 *
1154 * Returns NULL in case of failure, otherwise a request handler.
1155 * The contentType, if provided must be freed by the caller
1156 */
1157
1158void*
1159xmlNanoHTTPOpenRedir(const char *URL, char **contentType, char **redir) {
1160 if (contentType != NULL) *contentType = NULL;
1161 if (redir != NULL) *redir = NULL;
Daniel Veillardf012a642001-07-23 19:10:52 +00001162 return(xmlNanoHTTPMethodRedir(URL, NULL, NULL, contentType, redir, NULL,0));
Owen Taylor3473f882001-02-23 17:55:21 +00001163}
1164
1165/**
1166 * xmlNanoHTTPRead:
1167 * @ctx: the HTTP context
1168 * @dest: a buffer
1169 * @len: the buffer length
1170 *
1171 * This function tries to read @len bytes from the existing HTTP connection
1172 * and saves them in @dest. This is a blocking call.
1173 *
1174 * Returns the number of byte read. 0 is an indication of an end of connection.
1175 * -1 indicates a parameter error.
1176 */
1177int
1178xmlNanoHTTPRead(void *ctx, void *dest, int len) {
1179 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1180
1181 if (ctx == NULL) return(-1);
1182 if (dest == NULL) return(-1);
1183 if (len <= 0) return(0);
1184
1185 while (ctxt->inptr - ctxt->inrptr < len) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001186 if (xmlNanoHTTPRecv(ctxt) <= 0) break;
Owen Taylor3473f882001-02-23 17:55:21 +00001187 }
1188 if (ctxt->inptr - ctxt->inrptr < len)
1189 len = ctxt->inptr - ctxt->inrptr;
1190 memcpy(dest, ctxt->inrptr, len);
1191 ctxt->inrptr += len;
1192 return(len);
1193}
1194
1195/**
1196 * xmlNanoHTTPClose:
1197 * @ctx: the HTTP context
1198 *
1199 * This function closes an HTTP context, it ends up the connection and
1200 * free all data related to it.
1201 */
1202void
1203xmlNanoHTTPClose(void *ctx) {
1204 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1205
1206 if (ctx == NULL) return;
1207
1208 xmlNanoHTTPFreeCtxt(ctxt);
1209}
1210
1211/**
Daniel Veillard9403a042001-05-28 11:00:53 +00001212 * xmlNanoHTTPMethodRedir:
Owen Taylor3473f882001-02-23 17:55:21 +00001213 * @URL: The URL to load
1214 * @method: the HTTP method to use
1215 * @input: the input string if any
1216 * @contentType: the Content-Type information IN and OUT
Daniel Veillard9403a042001-05-28 11:00:53 +00001217 * @redir: the redirected URL OUT
Owen Taylor3473f882001-02-23 17:55:21 +00001218 * @headers: the extra headers
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001219 * @ilen: input length
Owen Taylor3473f882001-02-23 17:55:21 +00001220 *
1221 * This function try to open a connection to the indicated resource
1222 * via HTTP using the given @method, adding the given extra headers
1223 * and the input buffer for the request content.
1224 *
1225 * Returns NULL in case of failure, otherwise a request handler.
Daniel Veillard9403a042001-05-28 11:00:53 +00001226 * The contentType, or redir, if provided must be freed by the caller
Owen Taylor3473f882001-02-23 17:55:21 +00001227 */
1228
1229void*
Daniel Veillard9403a042001-05-28 11:00:53 +00001230xmlNanoHTTPMethodRedir(const char *URL, const char *method, const char *input,
Daniel Veillardf012a642001-07-23 19:10:52 +00001231 char **contentType, char **redir,
1232 const char *headers, int ilen ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001233 xmlNanoHTTPCtxtPtr ctxt;
1234 char *bp, *p;
Daniel Veillardf012a642001-07-23 19:10:52 +00001235 int blen, ret;
Owen Taylor3473f882001-02-23 17:55:21 +00001236 int head;
1237 int nbRedirects = 0;
1238 char *redirURL = NULL;
William M. Brack78637da2003-07-31 14:47:38 +00001239#ifdef DEBUG_HTTP
1240 int xmt_bytes;
1241#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001242
1243 if (URL == NULL) return(NULL);
1244 if (method == NULL) method = "GET";
1245 xmlNanoHTTPInit();
1246
1247retry:
1248 if (redirURL == NULL)
1249 ctxt = xmlNanoHTTPNewCtxt(URL);
1250 else {
1251 ctxt = xmlNanoHTTPNewCtxt(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001252 }
1253
Daniel Veillardf012a642001-07-23 19:10:52 +00001254 if ( ctxt == NULL ) {
1255 xmlGenericError( xmlGenericErrorContext,
1256 "xmlNanoHTTPMethodRedir: %s %s.",
1257 "Unable to allocate HTTP context to URI",
1258 ( ( redirURL == NULL ) ? URL : redirURL ) );
1259 return ( NULL );
1260 }
1261
Owen Taylor3473f882001-02-23 17:55:21 +00001262 if ((ctxt->protocol == NULL) || (strcmp(ctxt->protocol, "http"))) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001263 xmlGenericError( xmlGenericErrorContext,
1264 "xmlNanoHTTPMethodRedir: %s - %s.",
1265 "Not a valid HTTP URI",
1266 ( ( redirURL == NULL ) ? URL : redirURL ) );
Owen Taylor3473f882001-02-23 17:55:21 +00001267 xmlNanoHTTPFreeCtxt(ctxt);
1268 if (redirURL != NULL) xmlFree(redirURL);
1269 return(NULL);
1270 }
1271 if (ctxt->hostname == NULL) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001272 xmlGenericError( xmlGenericErrorContext,
1273 "xmlNanoHTTPMethodRedir: %s - %s",
1274 "Failed to identify host in URI",
1275 ( ( redirURL == NULL ) ? URL : redirURL ) );
Owen Taylor3473f882001-02-23 17:55:21 +00001276 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001277 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001278 return(NULL);
1279 }
1280 if (proxy) {
1281 blen = strlen(ctxt->hostname) * 2 + 16;
1282 ret = xmlNanoHTTPConnectHost(proxy, proxyPort);
1283 }
1284 else {
1285 blen = strlen(ctxt->hostname);
1286 ret = xmlNanoHTTPConnectHost(ctxt->hostname, ctxt->port);
1287 }
1288 if (ret < 0) {
1289 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001290 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001291 return(NULL);
1292 }
1293 ctxt->fd = ret;
1294
Daniel Veillardf012a642001-07-23 19:10:52 +00001295 if (input == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00001296 ilen = 0;
Daniel Veillardf012a642001-07-23 19:10:52 +00001297 else
1298 blen += 36;
1299
Owen Taylor3473f882001-02-23 17:55:21 +00001300 if (headers != NULL)
Daniel Veillardf012a642001-07-23 19:10:52 +00001301 blen += strlen(headers) + 2;
Owen Taylor3473f882001-02-23 17:55:21 +00001302 if (contentType && *contentType)
1303 blen += strlen(*contentType) + 16;
Daniel Veillardf012a642001-07-23 19:10:52 +00001304 blen += strlen(method) + strlen(ctxt->path) + 24;
Daniel Veillard3c908dc2003-04-19 00:07:51 +00001305 bp = xmlMallocAtomic(blen);
Daniel Veillardf012a642001-07-23 19:10:52 +00001306 if ( bp == NULL ) {
1307 xmlNanoHTTPFreeCtxt( ctxt );
1308 xmlGenericError( xmlGenericErrorContext,
1309 "xmlNanoHTTPMethodRedir: %s",
1310 "Error allocating HTTP header buffer." );
1311 return ( NULL );
1312 }
1313
1314 p = bp;
1315
Owen Taylor3473f882001-02-23 17:55:21 +00001316 if (proxy) {
1317 if (ctxt->port != 80) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001318 p += snprintf( p, blen - (p - bp), "%s http://%s:%d%s",
1319 method, ctxt->hostname,
Daniel Veillardf012a642001-07-23 19:10:52 +00001320 ctxt->port, ctxt->path );
Owen Taylor3473f882001-02-23 17:55:21 +00001321 }
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001322 else
1323 p += snprintf( p, blen - (p - bp), "%s http://%s%s", method,
Daniel Veillardf012a642001-07-23 19:10:52 +00001324 ctxt->hostname, ctxt->path);
Owen Taylor3473f882001-02-23 17:55:21 +00001325 }
1326 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001327 p += snprintf( p, blen - (p - bp), "%s %s", method, ctxt->path);
Daniel Veillardf012a642001-07-23 19:10:52 +00001328
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001329 p += snprintf( p, blen - (p - bp), " HTTP/1.0\r\nHost: %s\r\n",
1330 ctxt->hostname);
Daniel Veillardf012a642001-07-23 19:10:52 +00001331
1332 if (contentType != NULL && *contentType)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001333 p += snprintf(p, blen - (p - bp), "Content-Type: %s\r\n", *contentType);
Daniel Veillardf012a642001-07-23 19:10:52 +00001334
1335 if (headers != NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001336 p += snprintf( p, blen - (p - bp), "%s", headers );
Daniel Veillardf012a642001-07-23 19:10:52 +00001337
Owen Taylor3473f882001-02-23 17:55:21 +00001338 if (input != NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001339 snprintf(p, blen - (p - bp), "Content-Length: %d\r\n\r\n", ilen );
Owen Taylor3473f882001-02-23 17:55:21 +00001340 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001341 snprintf(p, blen - (p - bp), "\r\n");
Daniel Veillardf012a642001-07-23 19:10:52 +00001342
Owen Taylor3473f882001-02-23 17:55:21 +00001343#ifdef DEBUG_HTTP
1344 xmlGenericError(xmlGenericErrorContext,
1345 "-> %s%s", proxy? "(Proxy) " : "", bp);
1346 if ((blen -= strlen(bp)+1) < 0)
1347 xmlGenericError(xmlGenericErrorContext,
1348 "ERROR: overflowed buffer by %d bytes\n", -blen);
1349#endif
1350 ctxt->outptr = ctxt->out = bp;
1351 ctxt->state = XML_NANO_HTTP_WRITE;
Daniel Veillardf012a642001-07-23 19:10:52 +00001352 blen = strlen( ctxt->out );
Daniel Veillardf012a642001-07-23 19:10:52 +00001353#ifdef DEBUG_HTTP
William M. Brack78637da2003-07-31 14:47:38 +00001354 xmt_bytes = xmlNanoHTTPSend(ctxt, ctxt->out, blen );
Daniel Veillardf012a642001-07-23 19:10:52 +00001355 if ( xmt_bytes != blen )
1356 xmlGenericError( xmlGenericErrorContext,
1357 "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
1358 xmt_bytes, blen,
1359 "bytes of HTTP headers sent to host",
1360 ctxt->hostname );
William M. Brack78637da2003-07-31 14:47:38 +00001361#else
1362 xmlNanoHTTPSend(ctxt, ctxt->out, blen );
Daniel Veillardf012a642001-07-23 19:10:52 +00001363#endif
1364
1365 if ( input != NULL ) {
William M. Brack78637da2003-07-31 14:47:38 +00001366#ifdef DEBUG_HTTP
Daniel Veillardf012a642001-07-23 19:10:52 +00001367 xmt_bytes = xmlNanoHTTPSend( ctxt, input, ilen );
1368
Daniel Veillardf012a642001-07-23 19:10:52 +00001369 if ( xmt_bytes != ilen )
1370 xmlGenericError( xmlGenericErrorContext,
1371 "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
1372 xmt_bytes, ilen,
1373 "bytes of HTTP content sent to host",
1374 ctxt->hostname );
William M. Brack78637da2003-07-31 14:47:38 +00001375#else
1376 xmlNanoHTTPSend( ctxt, input, ilen );
Daniel Veillardf012a642001-07-23 19:10:52 +00001377#endif
1378 }
1379
Owen Taylor3473f882001-02-23 17:55:21 +00001380 ctxt->state = XML_NANO_HTTP_READ;
1381 head = 1;
1382
1383 while ((p = xmlNanoHTTPReadLine(ctxt)) != NULL) {
1384 if (head && (*p == 0)) {
1385 head = 0;
1386 ctxt->content = ctxt->inrptr;
1387 xmlFree(p);
1388 break;
1389 }
1390 xmlNanoHTTPScanAnswer(ctxt, p);
1391
1392#ifdef DEBUG_HTTP
1393 xmlGenericError(xmlGenericErrorContext, "<- %s\n", p);
1394#endif
1395 xmlFree(p);
1396 }
1397
1398 if ((ctxt->location != NULL) && (ctxt->returnValue >= 300) &&
1399 (ctxt->returnValue < 400)) {
1400#ifdef DEBUG_HTTP
1401 xmlGenericError(xmlGenericErrorContext,
1402 "\nRedirect to: %s\n", ctxt->location);
1403#endif
Daniel Veillardf012a642001-07-23 19:10:52 +00001404 while ( xmlNanoHTTPRecv(ctxt) > 0 ) ;
Owen Taylor3473f882001-02-23 17:55:21 +00001405 if (nbRedirects < XML_NANO_HTTP_MAX_REDIR) {
1406 nbRedirects++;
Daniel Veillard9403a042001-05-28 11:00:53 +00001407 if (redirURL != NULL)
1408 xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001409 redirURL = xmlMemStrdup(ctxt->location);
1410 xmlNanoHTTPFreeCtxt(ctxt);
1411 goto retry;
1412 }
1413 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001414 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001415#ifdef DEBUG_HTTP
1416 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardf012a642001-07-23 19:10:52 +00001417 "xmlNanoHTTPMethodRedir: Too many redirects, aborting ...\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001418#endif
1419 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001420 }
1421
1422 if (contentType != NULL) {
1423 if (ctxt->contentType != NULL)
1424 *contentType = xmlMemStrdup(ctxt->contentType);
1425 else
1426 *contentType = NULL;
1427 }
1428
Daniel Veillard9403a042001-05-28 11:00:53 +00001429 if ((redir != NULL) && (redirURL != NULL)) {
1430 *redir = redirURL;
1431 } else {
1432 if (redirURL != NULL)
1433 xmlFree(redirURL);
1434 if (redir != NULL)
1435 *redir = NULL;
1436 }
1437
Owen Taylor3473f882001-02-23 17:55:21 +00001438#ifdef DEBUG_HTTP
1439 if (ctxt->contentType != NULL)
1440 xmlGenericError(xmlGenericErrorContext,
1441 "\nCode %d, content-type '%s'\n\n",
1442 ctxt->returnValue, ctxt->contentType);
1443 else
1444 xmlGenericError(xmlGenericErrorContext,
1445 "\nCode %d, no content-type\n\n",
1446 ctxt->returnValue);
1447#endif
1448
1449 return((void *) ctxt);
1450}
1451
1452/**
Daniel Veillard9403a042001-05-28 11:00:53 +00001453 * xmlNanoHTTPMethod:
1454 * @URL: The URL to load
1455 * @method: the HTTP method to use
1456 * @input: the input string if any
1457 * @contentType: the Content-Type information IN and OUT
1458 * @headers: the extra headers
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001459 * @ilen: input length
Daniel Veillard9403a042001-05-28 11:00:53 +00001460 *
1461 * This function try to open a connection to the indicated resource
1462 * via HTTP using the given @method, adding the given extra headers
1463 * and the input buffer for the request content.
1464 *
1465 * Returns NULL in case of failure, otherwise a request handler.
1466 * The contentType, if provided must be freed by the caller
1467 */
1468
1469void*
1470xmlNanoHTTPMethod(const char *URL, const char *method, const char *input,
Daniel Veillardf012a642001-07-23 19:10:52 +00001471 char **contentType, const char *headers, int ilen) {
Daniel Veillard9403a042001-05-28 11:00:53 +00001472 return(xmlNanoHTTPMethodRedir(URL, method, input, contentType,
Daniel Veillardf012a642001-07-23 19:10:52 +00001473 NULL, headers, ilen));
Daniel Veillard9403a042001-05-28 11:00:53 +00001474}
1475
1476/**
Owen Taylor3473f882001-02-23 17:55:21 +00001477 * xmlNanoHTTPFetch:
1478 * @URL: The URL to load
1479 * @filename: the filename where the content should be saved
1480 * @contentType: if available the Content-Type information will be
1481 * returned at that location
1482 *
1483 * This function try to fetch the indicated resource via HTTP GET
1484 * and save it's content in the file.
1485 *
1486 * Returns -1 in case of failure, 0 incase of success. The contentType,
1487 * if provided must be freed by the caller
1488 */
1489int
1490xmlNanoHTTPFetch(const char *URL, const char *filename, char **contentType) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001491 void *ctxt = NULL;
1492 char *buf = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001493 int fd;
1494 int len;
1495
1496 ctxt = xmlNanoHTTPOpen(URL, contentType);
1497 if (ctxt == NULL) return(-1);
1498
1499 if (!strcmp(filename, "-"))
1500 fd = 0;
1501 else {
1502 fd = open(filename, O_CREAT | O_WRONLY, 00644);
1503 if (fd < 0) {
1504 xmlNanoHTTPClose(ctxt);
1505 if ((contentType != NULL) && (*contentType != NULL)) {
1506 xmlFree(*contentType);
1507 *contentType = NULL;
1508 }
1509 return(-1);
1510 }
1511 }
1512
Daniel Veillardf012a642001-07-23 19:10:52 +00001513 xmlNanoHTTPFetchContent( ctxt, &buf, &len );
1514 if ( len > 0 ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001515 write(fd, buf, len);
1516 }
1517
1518 xmlNanoHTTPClose(ctxt);
1519 close(fd);
1520 return(0);
1521}
1522
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001523#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001524/**
1525 * xmlNanoHTTPSave:
1526 * @ctxt: the HTTP context
1527 * @filename: the filename where the content should be saved
1528 *
1529 * This function saves the output of the HTTP transaction to a file
1530 * It closes and free the context at the end
1531 *
1532 * Returns -1 in case of failure, 0 incase of success.
1533 */
1534int
1535xmlNanoHTTPSave(void *ctxt, const char *filename) {
Daniel Veillarde3924972001-07-25 20:25:21 +00001536 char *buf = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001537 int fd;
1538 int len;
1539
1540 if (ctxt == NULL) return(-1);
1541
1542 if (!strcmp(filename, "-"))
1543 fd = 0;
1544 else {
1545 fd = open(filename, O_CREAT | O_WRONLY);
1546 if (fd < 0) {
1547 xmlNanoHTTPClose(ctxt);
1548 return(-1);
1549 }
1550 }
1551
Daniel Veillardf012a642001-07-23 19:10:52 +00001552 xmlNanoHTTPFetchContent( ctxt, &buf, &len );
1553 if ( len > 0 ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001554 write(fd, buf, len);
1555 }
1556
1557 xmlNanoHTTPClose(ctxt);
1558 return(0);
1559}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001560#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001561
1562/**
1563 * xmlNanoHTTPReturnCode:
1564 * @ctx: the HTTP context
1565 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001566 * Get the latest HTTP return code received
1567 *
Owen Taylor3473f882001-02-23 17:55:21 +00001568 * Returns the HTTP return code for the request.
1569 */
1570int
1571xmlNanoHTTPReturnCode(void *ctx) {
1572 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1573
1574 if (ctxt == NULL) return(-1);
1575
1576 return(ctxt->returnValue);
1577}
1578
1579/**
1580 * xmlNanoHTTPAuthHeader:
1581 * @ctx: the HTTP context
1582 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001583 * Get the authentication header of an HTTP context
1584 *
Owen Taylor3473f882001-02-23 17:55:21 +00001585 * Returns the stashed value of the WWW-Authenticate or Proxy-Authenticate
1586 * header.
1587 */
1588const char *
1589xmlNanoHTTPAuthHeader(void *ctx) {
1590 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1591
1592 if (ctxt == NULL) return(NULL);
1593
1594 return(ctxt->authHeader);
1595}
1596
Daniel Veillardf012a642001-07-23 19:10:52 +00001597/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00001598 * xmlNanoHTTPContentLength:
Daniel Veillardf012a642001-07-23 19:10:52 +00001599 * @ctx: the HTTP context
1600 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001601 * Provides the specified content length from the HTTP header.
1602 *
Daniel Veillardf012a642001-07-23 19:10:52 +00001603 * Return the specified content length from the HTTP header. Note that
1604 * a value of -1 indicates that the content length element was not included in
1605 * the response header.
1606 */
1607int
1608xmlNanoHTTPContentLength( void * ctx ) {
1609 xmlNanoHTTPCtxtPtr ctxt = ctx;
1610
1611 return ( ( ctxt == NULL ) ? -1 : ctxt->ContentLength );
1612}
1613
1614/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00001615 * xmlNanoHTTPFetchContent:
Daniel Veillardf012a642001-07-23 19:10:52 +00001616 * @ctx: the HTTP context
1617 * @ptr: pointer to set to the content buffer.
1618 * @len: integer pointer to hold the length of the content
1619 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001620 * Check if all the content was read
1621 *
Daniel Veillardf012a642001-07-23 19:10:52 +00001622 * Returns 0 if all the content was read and available, returns
1623 * -1 if received content length was less than specified or an error
1624 * occurred.
1625 */
1626int
1627xmlNanoHTTPFetchContent( void * ctx, char ** ptr, int * len ) {
1628 xmlNanoHTTPCtxtPtr ctxt = ctx;
1629
1630 int rc = 0;
1631 int cur_lgth;
1632 int rcvd_lgth;
1633 int dummy_int;
1634 char * dummy_ptr = NULL;
1635
1636 /* Dummy up return input parameters if not provided */
1637
1638 if ( len == NULL )
1639 len = &dummy_int;
1640
1641 if ( ptr == NULL )
1642 ptr = &dummy_ptr;
1643
1644 /* But can't work without the context pointer */
1645
1646 if ( ( ctxt == NULL ) || ( ctxt->content == NULL ) ) {
1647 *len = 0;
1648 *ptr = NULL;
1649 return ( -1 );
1650 }
1651
1652 rcvd_lgth = ctxt->inptr - ctxt->content;
1653
1654 while ( (cur_lgth = xmlNanoHTTPRecv( ctxt )) > 0 ) {
1655
1656 rcvd_lgth += cur_lgth;
1657 if ( (ctxt->ContentLength > 0) && (rcvd_lgth >= ctxt->ContentLength) )
1658 break;
1659 }
1660
1661 *ptr = ctxt->content;
1662 *len = rcvd_lgth;
1663
1664 if ( ( ctxt->ContentLength > 0 ) && ( rcvd_lgth < ctxt->ContentLength ) )
1665 rc = -1;
1666 else if ( rcvd_lgth == 0 )
1667 rc = -1;
1668
1669 return ( rc );
1670}
1671
Owen Taylor3473f882001-02-23 17:55:21 +00001672#ifdef STANDALONE
1673int main(int argc, char **argv) {
1674 char *contentType = NULL;
1675
1676 if (argv[1] != NULL) {
1677 if (argv[2] != NULL)
1678 xmlNanoHTTPFetch(argv[1], argv[2], &contentType);
1679 else
1680 xmlNanoHTTPFetch(argv[1], "-", &contentType);
1681 if (contentType != NULL) xmlFree(contentType);
1682 } else {
1683 xmlGenericError(xmlGenericErrorContext,
1684 "%s: minimal HTTP GET implementation\n", argv[0]);
1685 xmlGenericError(xmlGenericErrorContext,
1686 "\tusage %s [ URL [ filename ] ]\n", argv[0]);
1687 }
1688 xmlNanoHTTPCleanup();
1689 xmlMemoryDump();
1690 return(0);
1691}
1692#endif /* STANDALONE */
1693#else /* !LIBXML_HTTP_ENABLED */
1694#ifdef STANDALONE
1695#include <stdio.h>
1696int main(int argc, char **argv) {
1697 xmlGenericError(xmlGenericErrorContext,
1698 "%s : HTTP support not compiled in\n", argv[0]);
1699 return(0);
1700}
1701#endif /* STANDALONE */
1702#endif /* LIBXML_HTTP_ENABLED */