blob: ef88fd08adb403582676da552ed6d7bfdb085303 [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 */
Daniel Veillard847332a2003-10-18 11:29:40 +0000152 char *encoding; /* encoding extracted from the contentType */
Daniel Veillarda840b692003-10-19 13:35:37 +0000153 char *mimeType; /* Mime-Type extracted from the contentType */
Owen Taylor3473f882001-02-23 17:55:21 +0000154} xmlNanoHTTPCtxt, *xmlNanoHTTPCtxtPtr;
155
156static int initialized = 0;
157static char *proxy = NULL; /* the proxy name if any */
158static int proxyPort; /* the proxy port if any */
159static unsigned int timeout = 60;/* the select() timeout in seconds */
160
Daniel Veillarda2351322004-06-27 12:08:10 +0000161static int xmlNanoHTTPFetchContent( void * ctx, char ** ptr, int * len );
Daniel Veillardf012a642001-07-23 19:10:52 +0000162
Owen Taylor3473f882001-02-23 17:55:21 +0000163/**
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000164 * xmlHTTPErrMemory:
165 * @extra: extra informations
166 *
167 * Handle an out of memory condition
168 */
169static void
170xmlHTTPErrMemory(const char *extra)
171{
172 __xmlSimpleError(XML_FROM_HTTP, XML_ERR_NO_MEMORY, NULL, NULL, extra);
173}
174
175/**
Owen Taylor3473f882001-02-23 17:55:21 +0000176 * A portability function
177 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000178static int socket_errno(void) {
Owen Taylor3473f882001-02-23 17:55:21 +0000179#ifdef _WINSOCKAPI_
180 return(WSAGetLastError());
181#else
182 return(errno);
183#endif
184}
185
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000186#ifdef SUPPORT_IP6
Daniel Veillard2db8c122003-07-08 12:16:59 +0000187static
188int have_ipv6(void) {
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000189 int s;
190
191 s = socket (AF_INET6, SOCK_STREAM, 0);
192 if (s != -1) {
193 close (s);
194 return (1);
195 }
196 return (0);
197}
198#endif
199
Owen Taylor3473f882001-02-23 17:55:21 +0000200/**
201 * xmlNanoHTTPInit:
202 *
203 * Initialize the HTTP protocol layer.
204 * Currently it just checks for proxy informations
205 */
206
207void
208xmlNanoHTTPInit(void) {
209 const char *env;
210#ifdef _WINSOCKAPI_
211 WSADATA wsaData;
212#endif
213
214 if (initialized)
215 return;
216
217#ifdef _WINSOCKAPI_
218 if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0)
219 return;
220#endif
221
222 if (proxy == NULL) {
223 proxyPort = 80;
224 env = getenv("no_proxy");
Daniel Veillard29b17482004-08-16 00:39:03 +0000225 if (env && ((env[0] == '*') && (env[1] == 0)))
Owen Taylor3473f882001-02-23 17:55:21 +0000226 goto done;
227 env = getenv("http_proxy");
228 if (env != NULL) {
229 xmlNanoHTTPScanProxy(env);
230 goto done;
231 }
232 env = getenv("HTTP_PROXY");
233 if (env != NULL) {
234 xmlNanoHTTPScanProxy(env);
235 goto done;
236 }
237 }
238done:
239 initialized = 1;
240}
241
242/**
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000243 * xmlNanoHTTPCleanup:
Owen Taylor3473f882001-02-23 17:55:21 +0000244 *
245 * Cleanup the HTTP protocol layer.
246 */
247
248void
249xmlNanoHTTPCleanup(void) {
250 if (proxy != NULL)
251 xmlFree(proxy);
252#ifdef _WINSOCKAPI_
253 if (initialized)
254 WSACleanup();
255#endif
256 initialized = 0;
257 return;
258}
259
260/**
Owen Taylor3473f882001-02-23 17:55:21 +0000261 * xmlNanoHTTPScanURL:
262 * @ctxt: an HTTP context
263 * @URL: The URL used to initialize the context
264 *
265 * (Re)Initialize an HTTP context by parsing the URL and finding
266 * the protocol host port and path it indicates.
267 */
268
269static void
270xmlNanoHTTPScanURL(xmlNanoHTTPCtxtPtr ctxt, const char *URL) {
William M. Brack015ccb22005-02-13 08:18:52 +0000271 xmlURIPtr uri;
272 /*
273 * Clear any existing data from the context
274 */
Owen Taylor3473f882001-02-23 17:55:21 +0000275 if (ctxt->protocol != NULL) {
276 xmlFree(ctxt->protocol);
277 ctxt->protocol = NULL;
278 }
279 if (ctxt->hostname != NULL) {
280 xmlFree(ctxt->hostname);
281 ctxt->hostname = NULL;
282 }
283 if (ctxt->path != NULL) {
284 xmlFree(ctxt->path);
285 ctxt->path = NULL;
286 }
287 if (URL == NULL) return;
William M. Brack015ccb22005-02-13 08:18:52 +0000288
289 uri = xmlParseURI(URL);
290 if (uri == NULL)
291 return;
292
293 if ((uri->scheme == NULL) || (uri->server == NULL)) {
294 xmlFreeURI(uri);
295 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000296 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000297
William M. Brack015ccb22005-02-13 08:18:52 +0000298 ctxt->protocol = xmlMemStrdup(uri->scheme);
299 ctxt->hostname = xmlMemStrdup(uri->server);
300 if (uri->path != NULL)
301 ctxt->path = xmlMemStrdup(uri->path);
302 else
303 ctxt->path = xmlMemStrdup("/");
304 if (uri->port != 0)
305 ctxt->port = uri->port;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000306
William M. Brack015ccb22005-02-13 08:18:52 +0000307 xmlFreeURI(uri);
Owen Taylor3473f882001-02-23 17:55:21 +0000308}
309
310/**
311 * xmlNanoHTTPScanProxy:
312 * @URL: The proxy URL used to initialize the proxy context
313 *
314 * (Re)Initialize the HTTP Proxy context by parsing the URL and finding
315 * the protocol host port it indicates.
316 * Should be like http://myproxy/ or http://myproxy:3128/
317 * A NULL URL cleans up proxy informations.
318 */
319
320void
321xmlNanoHTTPScanProxy(const char *URL) {
William M. Brack015ccb22005-02-13 08:18:52 +0000322 xmlURIPtr uri;
Owen Taylor3473f882001-02-23 17:55:21 +0000323
324 if (proxy != NULL) {
325 xmlFree(proxy);
326 proxy = NULL;
327 }
William M. Brack015ccb22005-02-13 08:18:52 +0000328 proxyPort = 0;
329
Owen Taylor3473f882001-02-23 17:55:21 +0000330#ifdef DEBUG_HTTP
331 if (URL == NULL)
332 xmlGenericError(xmlGenericErrorContext,
333 "Removing HTTP proxy info\n");
334 else
335 xmlGenericError(xmlGenericErrorContext,
336 "Using HTTP proxy %s\n", URL);
337#endif
338 if (URL == NULL) return;
William M. Brack015ccb22005-02-13 08:18:52 +0000339
340 uri = xmlParseURI(URL);
341 if ((uri == NULL) || (uri->scheme == NULL) ||
342 (strcmp(uri->scheme, "http")) || (uri->server == NULL)) {
343 __xmlIOErr(XML_FROM_HTTP, XML_HTTP_URL_SYNTAX, "Syntax Error\n");
344 if (uri != NULL)
345 xmlFreeURI(uri);
346 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000347 }
William M. Brack015ccb22005-02-13 08:18:52 +0000348
349 proxy = xmlMemStrdup(uri->server);
350 if (uri->port != 0)
351 proxyPort = uri->port;
Owen Taylor3473f882001-02-23 17:55:21 +0000352
William M. Brack015ccb22005-02-13 08:18:52 +0000353 xmlFreeURI(uri);
Owen Taylor3473f882001-02-23 17:55:21 +0000354}
355
356/**
357 * xmlNanoHTTPNewCtxt:
358 * @URL: The URL used to initialize the context
359 *
360 * Allocate and initialize a new HTTP context.
361 *
362 * Returns an HTTP context or NULL in case of error.
363 */
364
365static xmlNanoHTTPCtxtPtr
366xmlNanoHTTPNewCtxt(const char *URL) {
367 xmlNanoHTTPCtxtPtr ret;
368
369 ret = (xmlNanoHTTPCtxtPtr) xmlMalloc(sizeof(xmlNanoHTTPCtxt));
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000370 if (ret == NULL) {
371 xmlHTTPErrMemory("allocating context");
372 return(NULL);
373 }
Owen Taylor3473f882001-02-23 17:55:21 +0000374
375 memset(ret, 0, sizeof(xmlNanoHTTPCtxt));
376 ret->port = 80;
377 ret->returnValue = 0;
378 ret->fd = -1;
Daniel Veillardf012a642001-07-23 19:10:52 +0000379 ret->ContentLength = -1;
Owen Taylor3473f882001-02-23 17:55:21 +0000380
Daniel Veillardcacbe5d2003-01-10 16:09:51 +0000381 xmlNanoHTTPScanURL(ret, URL);
Owen Taylor3473f882001-02-23 17:55:21 +0000382
383 return(ret);
384}
385
386/**
387 * xmlNanoHTTPFreeCtxt:
388 * @ctxt: an HTTP context
389 *
390 * Frees the context after closing the connection.
391 */
392
393static void
394xmlNanoHTTPFreeCtxt(xmlNanoHTTPCtxtPtr ctxt) {
395 if (ctxt == NULL) return;
396 if (ctxt->hostname != NULL) xmlFree(ctxt->hostname);
397 if (ctxt->protocol != NULL) xmlFree(ctxt->protocol);
398 if (ctxt->path != NULL) xmlFree(ctxt->path);
399 if (ctxt->out != NULL) xmlFree(ctxt->out);
400 if (ctxt->in != NULL) xmlFree(ctxt->in);
401 if (ctxt->contentType != NULL) xmlFree(ctxt->contentType);
Daniel Veillard847332a2003-10-18 11:29:40 +0000402 if (ctxt->encoding != NULL) xmlFree(ctxt->encoding);
Daniel Veillarda840b692003-10-19 13:35:37 +0000403 if (ctxt->mimeType != NULL) xmlFree(ctxt->mimeType);
Owen Taylor3473f882001-02-23 17:55:21 +0000404 if (ctxt->location != NULL) xmlFree(ctxt->location);
405 if (ctxt->authHeader != NULL) xmlFree(ctxt->authHeader);
406 ctxt->state = XML_NANO_HTTP_NONE;
407 if (ctxt->fd >= 0) closesocket(ctxt->fd);
408 ctxt->fd = -1;
409 xmlFree(ctxt);
410}
411
412/**
413 * xmlNanoHTTPSend:
414 * @ctxt: an HTTP context
415 *
416 * Send the input needed to initiate the processing on the server side
Daniel Veillardf012a642001-07-23 19:10:52 +0000417 * Returns number of bytes sent or -1 on error.
Owen Taylor3473f882001-02-23 17:55:21 +0000418 */
419
Daniel Veillardf012a642001-07-23 19:10:52 +0000420static int
421xmlNanoHTTPSend(xmlNanoHTTPCtxtPtr ctxt, const char * xmt_ptr, int outlen) {
422
423 int total_sent = 0;
424
425 if ( (ctxt->state & XML_NANO_HTTP_WRITE) && (xmt_ptr != NULL ) ) {
426 while (total_sent < outlen) {
427 int nsent = send(ctxt->fd, xmt_ptr + total_sent,
428 outlen - total_sent, 0);
Owen Taylor3473f882001-02-23 17:55:21 +0000429 if (nsent>0)
430 total_sent += nsent;
Daniel Veillardf012a642001-07-23 19:10:52 +0000431 else if ( ( nsent == -1 ) &&
Daniel Veillardba6db032001-07-31 16:25:45 +0000432#if defined(EAGAIN) && EAGAIN != EWOULDBLOCK
Daniel Veillardf012a642001-07-23 19:10:52 +0000433 ( socket_errno( ) != EAGAIN ) &&
Daniel Veillardba6db032001-07-31 16:25:45 +0000434#endif
Daniel Veillardf012a642001-07-23 19:10:52 +0000435 ( socket_errno( ) != EWOULDBLOCK ) ) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000436 __xmlIOErr(XML_FROM_HTTP, 0, "send failed\n");
Daniel Veillardf012a642001-07-23 19:10:52 +0000437 if ( total_sent == 0 )
438 total_sent = -1;
439 break;
440 }
441 else {
442 /*
443 ** No data sent
444 ** Since non-blocking sockets are used, wait for
445 ** socket to be writable or default timeout prior
446 ** to retrying.
447 */
448
449 struct timeval tv;
450 fd_set wfd;
451
452 tv.tv_sec = timeout;
453 tv.tv_usec = 0;
454 FD_ZERO( &wfd );
455 FD_SET( ctxt->fd, &wfd );
456 (void)select( ctxt->fd + 1, NULL, &wfd, NULL, &tv );
457 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000458 }
Owen Taylor3473f882001-02-23 17:55:21 +0000459 }
Daniel Veillardf012a642001-07-23 19:10:52 +0000460
461 return total_sent;
Owen Taylor3473f882001-02-23 17:55:21 +0000462}
463
464/**
465 * xmlNanoHTTPRecv:
466 * @ctxt: an HTTP context
467 *
468 * Read information coming from the HTTP connection.
469 * This is a blocking call (but it blocks in select(), not read()).
470 *
471 * Returns the number of byte read or -1 in case of error.
472 */
473
474static int
475xmlNanoHTTPRecv(xmlNanoHTTPCtxtPtr ctxt) {
476 fd_set rfd;
477 struct timeval tv;
478
479
480 while (ctxt->state & XML_NANO_HTTP_READ) {
481 if (ctxt->in == NULL) {
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000482 ctxt->in = (char *) xmlMallocAtomic(65000 * sizeof(char));
Owen Taylor3473f882001-02-23 17:55:21 +0000483 if (ctxt->in == NULL) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000484 xmlHTTPErrMemory("allocating input");
Owen Taylor3473f882001-02-23 17:55:21 +0000485 ctxt->last = -1;
486 return(-1);
487 }
488 ctxt->inlen = 65000;
489 ctxt->inptr = ctxt->content = ctxt->inrptr = ctxt->in;
490 }
491 if (ctxt->inrptr > ctxt->in + XML_NANO_HTTP_CHUNK) {
492 int delta = ctxt->inrptr - ctxt->in;
493 int len = ctxt->inptr - ctxt->inrptr;
494
495 memmove(ctxt->in, ctxt->inrptr, len);
496 ctxt->inrptr -= delta;
497 ctxt->content -= delta;
498 ctxt->inptr -= delta;
499 }
500 if ((ctxt->in + ctxt->inlen) < (ctxt->inptr + XML_NANO_HTTP_CHUNK)) {
501 int d_inptr = ctxt->inptr - ctxt->in;
502 int d_content = ctxt->content - ctxt->in;
503 int d_inrptr = ctxt->inrptr - ctxt->in;
Daniel Veillardf012a642001-07-23 19:10:52 +0000504 char * tmp_ptr = ctxt->in;
Owen Taylor3473f882001-02-23 17:55:21 +0000505
506 ctxt->inlen *= 2;
Daniel Veillardf012a642001-07-23 19:10:52 +0000507 ctxt->in = (char *) xmlRealloc(tmp_ptr, ctxt->inlen);
Owen Taylor3473f882001-02-23 17:55:21 +0000508 if (ctxt->in == NULL) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000509 xmlHTTPErrMemory("allocating input buffer");
Daniel Veillardf012a642001-07-23 19:10:52 +0000510 xmlFree( tmp_ptr );
Owen Taylor3473f882001-02-23 17:55:21 +0000511 ctxt->last = -1;
512 return(-1);
513 }
514 ctxt->inptr = ctxt->in + d_inptr;
515 ctxt->content = ctxt->in + d_content;
516 ctxt->inrptr = ctxt->in + d_inrptr;
517 }
518 ctxt->last = recv(ctxt->fd, ctxt->inptr, XML_NANO_HTTP_CHUNK, 0);
519 if (ctxt->last > 0) {
520 ctxt->inptr += ctxt->last;
521 return(ctxt->last);
522 }
523 if (ctxt->last == 0) {
524 return(0);
525 }
526 if (ctxt->last == -1) {
527 switch (socket_errno()) {
528 case EINPROGRESS:
529 case EWOULDBLOCK:
530#if defined(EAGAIN) && EAGAIN != EWOULDBLOCK
531 case EAGAIN:
532#endif
533 break;
Daniel Veillardf012a642001-07-23 19:10:52 +0000534
535 case ECONNRESET:
536 case ESHUTDOWN:
537 return ( 0 );
538
Owen Taylor3473f882001-02-23 17:55:21 +0000539 default:
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000540 __xmlIOErr(XML_FROM_HTTP, 0, "recv failed\n");
Daniel Veillardf012a642001-07-23 19:10:52 +0000541 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +0000542 }
543 }
544
545 tv.tv_sec = timeout;
546 tv.tv_usec = 0;
547 FD_ZERO(&rfd);
548 FD_SET(ctxt->fd, &rfd);
549
Daniel Veillard50f34372001-08-03 12:06:36 +0000550 if ( (select(ctxt->fd+1, &rfd, NULL, NULL, &tv)<1)
551#if defined(EINTR)
552 && (errno != EINTR)
553#endif
554 )
Owen Taylor3473f882001-02-23 17:55:21 +0000555 return(0);
556 }
557 return(0);
558}
559
560/**
561 * xmlNanoHTTPReadLine:
562 * @ctxt: an HTTP context
563 *
564 * Read one line in the HTTP server output, usually for extracting
565 * the HTTP protocol informations from the answer header.
566 *
567 * Returns a newly allocated string with a copy of the line, or NULL
568 * which indicate the end of the input.
569 */
570
571static char *
572xmlNanoHTTPReadLine(xmlNanoHTTPCtxtPtr ctxt) {
573 char buf[4096];
574 char *bp = buf;
Daniel Veillardf012a642001-07-23 19:10:52 +0000575 int rc;
Owen Taylor3473f882001-02-23 17:55:21 +0000576
577 while (bp - buf < 4095) {
578 if (ctxt->inrptr == ctxt->inptr) {
Daniel Veillardf012a642001-07-23 19:10:52 +0000579 if ( (rc = xmlNanoHTTPRecv(ctxt)) == 0) {
Owen Taylor3473f882001-02-23 17:55:21 +0000580 if (bp == buf)
581 return(NULL);
582 else
583 *bp = 0;
584 return(xmlMemStrdup(buf));
585 }
Daniel Veillardf012a642001-07-23 19:10:52 +0000586 else if ( rc == -1 ) {
587 return ( NULL );
588 }
Owen Taylor3473f882001-02-23 17:55:21 +0000589 }
590 *bp = *ctxt->inrptr++;
591 if (*bp == '\n') {
592 *bp = 0;
593 return(xmlMemStrdup(buf));
594 }
595 if (*bp != '\r')
596 bp++;
597 }
598 buf[4095] = 0;
599 return(xmlMemStrdup(buf));
600}
601
602
603/**
604 * xmlNanoHTTPScanAnswer:
605 * @ctxt: an HTTP context
606 * @line: an HTTP header line
607 *
608 * Try to extract useful informations from the server answer.
609 * We currently parse and process:
610 * - The HTTP revision/ return code
Daniel Veillarda840b692003-10-19 13:35:37 +0000611 * - The Content-Type, Mime-Type and charset used
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000612 * - The Location for redirect processing.
Owen Taylor3473f882001-02-23 17:55:21 +0000613 *
614 * Returns -1 in case of failure, the file descriptor number otherwise
615 */
616
617static void
618xmlNanoHTTPScanAnswer(xmlNanoHTTPCtxtPtr ctxt, const char *line) {
619 const char *cur = line;
620
621 if (line == NULL) return;
622
623 if (!strncmp(line, "HTTP/", 5)) {
624 int version = 0;
625 int ret = 0;
626
627 cur += 5;
628 while ((*cur >= '0') && (*cur <= '9')) {
629 version *= 10;
630 version += *cur - '0';
631 cur++;
632 }
633 if (*cur == '.') {
634 cur++;
635 if ((*cur >= '0') && (*cur <= '9')) {
636 version *= 10;
637 version += *cur - '0';
638 cur++;
639 }
640 while ((*cur >= '0') && (*cur <= '9'))
641 cur++;
642 } else
643 version *= 10;
644 if ((*cur != ' ') && (*cur != '\t')) return;
645 while ((*cur == ' ') || (*cur == '\t')) cur++;
646 if ((*cur < '0') || (*cur > '9')) return;
647 while ((*cur >= '0') && (*cur <= '9')) {
648 ret *= 10;
649 ret += *cur - '0';
650 cur++;
651 }
652 if ((*cur != 0) && (*cur != ' ') && (*cur != '\t')) return;
653 ctxt->returnValue = ret;
654 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Content-Type:", 13)) {
Daniel Veillarda840b692003-10-19 13:35:37 +0000655 const xmlChar *charset, *last, *mime;
Owen Taylor3473f882001-02-23 17:55:21 +0000656 cur += 13;
657 while ((*cur == ' ') || (*cur == '\t')) cur++;
658 if (ctxt->contentType != NULL)
659 xmlFree(ctxt->contentType);
660 ctxt->contentType = xmlMemStrdup(cur);
Daniel Veillarda840b692003-10-19 13:35:37 +0000661 mime = (const xmlChar *) cur;
662 last = mime;
663 while ((*last != 0) && (*last != ' ') && (*last != '\t') &&
664 (*last != ';') && (*last != ','))
665 last++;
666 if (ctxt->mimeType != NULL)
667 xmlFree(ctxt->mimeType);
668 ctxt->mimeType = (char *) xmlStrndup(mime, last - mime);
669 charset = xmlStrstr(BAD_CAST ctxt->contentType, BAD_CAST "charset=");
670 if (charset != NULL) {
671 charset += 8;
672 last = charset;
673 while ((*last != 0) && (*last != ' ') && (*last != '\t') &&
674 (*last != ';') && (*last != ','))
675 last++;
676 if (ctxt->encoding != NULL)
677 xmlFree(ctxt->encoding);
678 ctxt->encoding = (char *) xmlStrndup(charset, last - charset);
679 }
Owen Taylor3473f882001-02-23 17:55:21 +0000680 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"ContentType:", 12)) {
Daniel Veillarda840b692003-10-19 13:35:37 +0000681 const xmlChar *charset, *last, *mime;
Owen Taylor3473f882001-02-23 17:55:21 +0000682 cur += 12;
683 if (ctxt->contentType != NULL) return;
684 while ((*cur == ' ') || (*cur == '\t')) cur++;
685 ctxt->contentType = xmlMemStrdup(cur);
Daniel Veillarda840b692003-10-19 13:35:37 +0000686 mime = (const xmlChar *) cur;
687 last = mime;
688 while ((*last != 0) && (*last != ' ') && (*last != '\t') &&
689 (*last != ';') && (*last != ','))
690 last++;
691 if (ctxt->mimeType != NULL)
692 xmlFree(ctxt->mimeType);
693 ctxt->mimeType = (char *) xmlStrndup(mime, last - mime);
694 charset = xmlStrstr(BAD_CAST ctxt->contentType, BAD_CAST "charset=");
695 if (charset != NULL) {
696 charset += 8;
697 last = charset;
698 while ((*last != 0) && (*last != ' ') && (*last != '\t') &&
699 (*last != ';') && (*last != ','))
700 last++;
701 if (ctxt->encoding != NULL)
702 xmlFree(ctxt->encoding);
703 ctxt->encoding = (char *) xmlStrndup(charset, last - charset);
704 }
Owen Taylor3473f882001-02-23 17:55:21 +0000705 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Location:", 9)) {
706 cur += 9;
707 while ((*cur == ' ') || (*cur == '\t')) cur++;
708 if (ctxt->location != NULL)
709 xmlFree(ctxt->location);
William M. Brack7e29c0a2004-04-02 09:07:22 +0000710 if (*cur == '/') {
711 xmlChar *tmp_http = xmlStrdup(BAD_CAST "http://");
712 xmlChar *tmp_loc =
713 xmlStrcat(tmp_http, (const xmlChar *) ctxt->hostname);
714 ctxt->location =
715 (char *) xmlStrcat (tmp_loc, (const xmlChar *) cur);
716 } else {
717 ctxt->location = xmlMemStrdup(cur);
718 }
Owen Taylor3473f882001-02-23 17:55:21 +0000719 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"WWW-Authenticate:", 17)) {
720 cur += 17;
721 while ((*cur == ' ') || (*cur == '\t')) cur++;
722 if (ctxt->authHeader != NULL)
723 xmlFree(ctxt->authHeader);
724 ctxt->authHeader = xmlMemStrdup(cur);
725 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Proxy-Authenticate:", 19)) {
726 cur += 19;
727 while ((*cur == ' ') || (*cur == '\t')) cur++;
728 if (ctxt->authHeader != NULL)
729 xmlFree(ctxt->authHeader);
730 ctxt->authHeader = xmlMemStrdup(cur);
Daniel Veillardf012a642001-07-23 19:10:52 +0000731 } else if ( !xmlStrncasecmp( BAD_CAST line, BAD_CAST"Content-Length:", 15) ) {
732 cur += 15;
733 ctxt->ContentLength = strtol( cur, NULL, 10 );
Owen Taylor3473f882001-02-23 17:55:21 +0000734 }
735}
736
737/**
738 * xmlNanoHTTPConnectAttempt:
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000739 * @addr: a socket address structure
Owen Taylor3473f882001-02-23 17:55:21 +0000740 *
741 * Attempt a connection to the given IP:port endpoint. It forces
742 * non-blocking semantic on the socket, and allow 60 seconds for
743 * the host to answer.
744 *
745 * Returns -1 in case of failure, the file descriptor number otherwise
746 */
747
748static int
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000749xmlNanoHTTPConnectAttempt(struct sockaddr *addr)
Owen Taylor3473f882001-02-23 17:55:21 +0000750{
Owen Taylor3473f882001-02-23 17:55:21 +0000751 fd_set wfd;
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000752#ifdef _WINSOCKAPI_
753 fd_set xfd;
754#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000755 struct timeval tv;
756 int status;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000757 int addrlen;
758 SOCKET s;
Owen Taylor3473f882001-02-23 17:55:21 +0000759
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000760#ifdef SUPPORT_IP6
761 if (addr->sa_family == AF_INET6) {
762 s = socket (PF_INET6, SOCK_STREAM, IPPROTO_TCP);
763 addrlen = sizeof (struct sockaddr_in6);
764 }
765 else
766#endif
767 {
768 s = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
769 addrlen = sizeof (struct sockaddr_in);
770 }
Owen Taylor3473f882001-02-23 17:55:21 +0000771 if (s==-1) {
772#ifdef DEBUG_HTTP
773 perror("socket");
774#endif
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000775 __xmlIOErr(XML_FROM_HTTP, 0, "socket failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000776 return(-1);
777 }
778
779#ifdef _WINSOCKAPI_
780 {
781 u_long one = 1;
782
783 status = ioctlsocket(s, FIONBIO, &one) == SOCKET_ERROR ? -1 : 0;
784 }
785#else /* _WINSOCKAPI_ */
786#if defined(VMS)
787 {
788 int enable = 1;
789 status = ioctl(s, FIONBIO, &enable);
790 }
791#else /* VMS */
Daniel Veillard254b1262003-11-01 17:04:58 +0000792#if defined(__BEOS__)
793 {
794 bool noblock = true;
795 status = setsockopt(s, SOL_SOCKET, SO_NONBLOCK, &noblock, sizeof(noblock));
796 }
797#else /* __BEOS__ */
Owen Taylor3473f882001-02-23 17:55:21 +0000798 if ((status = fcntl(s, F_GETFL, 0)) != -1) {
799#ifdef O_NONBLOCK
800 status |= O_NONBLOCK;
801#else /* O_NONBLOCK */
802#ifdef F_NDELAY
803 status |= F_NDELAY;
804#endif /* F_NDELAY */
805#endif /* !O_NONBLOCK */
806 status = fcntl(s, F_SETFL, status);
807 }
808 if (status < 0) {
809#ifdef DEBUG_HTTP
810 perror("nonblocking");
811#endif
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000812 __xmlIOErr(XML_FROM_HTTP, 0, "error setting non-blocking IO\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000813 closesocket(s);
814 return(-1);
815 }
Daniel Veillard254b1262003-11-01 17:04:58 +0000816#endif /* !__BEOS__ */
Owen Taylor3473f882001-02-23 17:55:21 +0000817#endif /* !VMS */
818#endif /* !_WINSOCKAPI_ */
819
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000820 if (connect (s, addr, addrlen) == -1) {
Owen Taylor3473f882001-02-23 17:55:21 +0000821 switch (socket_errno()) {
822 case EINPROGRESS:
823 case EWOULDBLOCK:
824 break;
825 default:
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000826 __xmlIOErr(XML_FROM_HTTP, 0, "error connecting to HTTP server");
Owen Taylor3473f882001-02-23 17:55:21 +0000827 closesocket(s);
828 return(-1);
829 }
830 }
831
832 tv.tv_sec = timeout;
833 tv.tv_usec = 0;
834
835 FD_ZERO(&wfd);
836 FD_SET(s, &wfd);
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000837
838#ifdef _WINSOCKAPI_
839 FD_ZERO(&xfd);
840 FD_SET(s, &xfd);
Owen Taylor3473f882001-02-23 17:55:21 +0000841
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000842 switch(select(s+1, NULL, &wfd, &xfd, &tv))
843#else
Owen Taylor3473f882001-02-23 17:55:21 +0000844 switch(select(s+1, NULL, &wfd, NULL, &tv))
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000845#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000846 {
847 case 0:
848 /* Time out */
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000849 __xmlIOErr(XML_FROM_HTTP, 0, "Connect attempt timed out");
Owen Taylor3473f882001-02-23 17:55:21 +0000850 closesocket(s);
851 return(-1);
852 case -1:
853 /* Ermm.. ?? */
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000854 __xmlIOErr(XML_FROM_HTTP, 0, "Connect failed");
Owen Taylor3473f882001-02-23 17:55:21 +0000855 closesocket(s);
856 return(-1);
857 }
858
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000859 if ( FD_ISSET(s, &wfd)
860#ifdef _WINSOCKAPI_
861 || FD_ISSET(s, &xfd)
862#endif
863 ) {
Owen Taylor3473f882001-02-23 17:55:21 +0000864 SOCKLEN_T len;
865 len = sizeof(status);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000866#ifdef SO_ERROR
Owen Taylor3473f882001-02-23 17:55:21 +0000867 if (getsockopt(s, SOL_SOCKET, SO_ERROR, (char*)&status, &len) < 0 ) {
868 /* Solaris error code */
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000869 __xmlIOErr(XML_FROM_HTTP, 0, "getsockopt failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000870 return (-1);
871 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000872#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000873 if ( status ) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000874 __xmlIOErr(XML_FROM_HTTP, 0, "Error connecting to remote host");
Owen Taylor3473f882001-02-23 17:55:21 +0000875 closesocket(s);
876 errno = status;
877 return (-1);
878 }
879 } else {
880 /* pbm */
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000881 __xmlIOErr(XML_FROM_HTTP, 0, "select failed\n");
Daniel Veillardf012a642001-07-23 19:10:52 +0000882 closesocket(s);
Owen Taylor3473f882001-02-23 17:55:21 +0000883 return (-1);
884 }
885
886 return(s);
887}
888
889/**
890 * xmlNanoHTTPConnectHost:
891 * @host: the host name
892 * @port: the port number
893 *
894 * Attempt a connection to the given host:port endpoint. It tries
895 * the multiple IP provided by the DNS if available.
896 *
897 * Returns -1 in case of failure, the file descriptor number otherwise
898 */
899
900static int
901xmlNanoHTTPConnectHost(const char *host, int port)
902{
903 struct hostent *h;
Daniel Veillard2db8c122003-07-08 12:16:59 +0000904 struct sockaddr *addr = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +0000905 struct in_addr ia;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000906 struct sockaddr_in sockin;
Daniel Veillard5c396542002-03-15 07:57:50 +0000907
Owen Taylor3473f882001-02-23 17:55:21 +0000908#ifdef SUPPORT_IP6
909 struct in6_addr ia6;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000910 struct sockaddr_in6 sockin6;
Owen Taylor3473f882001-02-23 17:55:21 +0000911#endif
912 int i;
913 int s;
Daniel Veillard5c396542002-03-15 07:57:50 +0000914
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000915 memset (&sockin, 0, sizeof(sockin));
916#ifdef SUPPORT_IP6
917 memset (&sockin6, 0, sizeof(sockin6));
918 if (have_ipv6 ())
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000919#if !defined(HAVE_GETADDRINFO) && defined(RES_USE_INET6)
Daniel Veillard560c2a42003-07-06 21:13:49 +0000920 {
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000921 if (!(_res.options & RES_INIT))
922 res_init();
923 _res.options |= RES_USE_INET6;
924 }
925#elif defined(HAVE_GETADDRINFO)
Daniel Veillard560c2a42003-07-06 21:13:49 +0000926 {
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000927 int status;
928 struct addrinfo hints, *res, *result;
929
930 result = NULL;
931 memset (&hints, 0,sizeof(hints));
932 hints.ai_socktype = SOCK_STREAM;
933
934 status = getaddrinfo (host, NULL, &hints, &result);
935 if (status) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000936 __xmlIOErr(XML_FROM_HTTP, 0, "getaddrinfo failed\n");
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000937 return (-1);
938 }
939
940 for (res = result; res; res = res->ai_next) {
Daniel Veillard3dc93a42003-07-10 14:04:33 +0000941 if (res->ai_family == AF_INET || res->ai_family == AF_INET6) {
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000942 if (res->ai_family == AF_INET6) {
Daniel Veillard8e2c9792004-10-27 09:39:50 +0000943 if (res->ai_addrlen > sizeof(sockin6)) {
944 __xmlIOErr(XML_FROM_HTTP, 0, "address size mismatch\n");
945 freeaddrinfo (result);
946 return (-1);
947 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000948 memcpy (&sockin6, res->ai_addr, res->ai_addrlen);
949 sockin6.sin6_port = htons (port);
950 addr = (struct sockaddr *)&sockin6;
951 }
Daniel Veillard3dc93a42003-07-10 14:04:33 +0000952 else {
Daniel Veillard8e2c9792004-10-27 09:39:50 +0000953 if (res->ai_addrlen > sizeof(sockin)) {
954 __xmlIOErr(XML_FROM_HTTP, 0, "address size mismatch\n");
955 freeaddrinfo (result);
956 return (-1);
957 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000958 memcpy (&sockin, res->ai_addr, res->ai_addrlen);
959 sockin.sin_port = htons (port);
960 addr = (struct sockaddr *)&sockin;
961 }
962
963 s = xmlNanoHTTPConnectAttempt (addr);
964 if (s != -1) {
965 freeaddrinfo (result);
966 return (s);
967 }
968 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000969 }
Daniel Veillard3dc93a42003-07-10 14:04:33 +0000970 if (result)
971 freeaddrinfo (result);
972 return (-1);
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000973 } else
Owen Taylor3473f882001-02-23 17:55:21 +0000974#endif
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000975#endif
976 {
977 h = gethostbyname (host);
978 if (h == NULL) {
Daniel Veillard56b2db72002-03-25 16:35:28 +0000979
980/*
981 * Okay, I got fed up by the non-portability of this error message
982 * extraction code. it work on Linux, if it work on your platform
983 * and one want to enable it, send me the defined(foobar) needed
984 */
985#if defined(HAVE_NETDB_H) && defined(HOST_NOT_FOUND) && defined(linux)
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000986 const char *h_err_txt = "";
Daniel Veillardf012a642001-07-23 19:10:52 +0000987
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000988 switch (h_errno) {
989 case HOST_NOT_FOUND:
990 h_err_txt = "Authoritive host not found";
991 break;
Daniel Veillardf012a642001-07-23 19:10:52 +0000992
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000993 case TRY_AGAIN:
994 h_err_txt =
995 "Non-authoritive host not found or server failure.";
996 break;
Daniel Veillardf012a642001-07-23 19:10:52 +0000997
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000998 case NO_RECOVERY:
999 h_err_txt =
1000 "Non-recoverable errors: FORMERR, REFUSED, or NOTIMP.";
1001 break;
Daniel Veillard5c396542002-03-15 07:57:50 +00001002
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001003 case NO_ADDRESS:
1004 h_err_txt =
1005 "Valid name, no data record of requested type.";
1006 break;
Daniel Veillard5c396542002-03-15 07:57:50 +00001007
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001008 default:
1009 h_err_txt = "No error text defined.";
1010 break;
1011 }
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001012 __xmlIOErr(XML_FROM_HTTP, 0, h_err_txt);
Daniel Veillard5c396542002-03-15 07:57:50 +00001013#else
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001014 __xmlIOErr(XML_FROM_HTTP, 0, "Failed to resolve host");
Owen Taylor3473f882001-02-23 17:55:21 +00001015#endif
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001016 return (-1);
1017 }
Daniel Veillard5c396542002-03-15 07:57:50 +00001018
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001019 for (i = 0; h->h_addr_list[i]; i++) {
1020 if (h->h_addrtype == AF_INET) {
1021 /* A records (IPv4) */
Daniel Veillard8e2c9792004-10-27 09:39:50 +00001022 if ((unsigned int) h->h_length > sizeof(ia)) {
1023 __xmlIOErr(XML_FROM_HTTP, 0, "address size mismatch\n");
1024 return (-1);
1025 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001026 memcpy (&ia, h->h_addr_list[i], h->h_length);
1027 sockin.sin_family = h->h_addrtype;
1028 sockin.sin_addr = ia;
1029 sockin.sin_port = htons (port);
1030 addr = (struct sockaddr *) &sockin;
Daniel Veillard5c396542002-03-15 07:57:50 +00001031#ifdef SUPPORT_IP6
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001032 } else if (have_ipv6 () && (h->h_addrtype == AF_INET6)) {
1033 /* AAAA records (IPv6) */
Daniel Veillard8e2c9792004-10-27 09:39:50 +00001034 if ((unsigned int) h->h_length > sizeof(ia6)) {
1035 __xmlIOErr(XML_FROM_HTTP, 0, "address size mismatch\n");
1036 return (-1);
1037 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001038 memcpy (&ia6, h->h_addr_list[i], h->h_length);
1039 sockin6.sin6_family = h->h_addrtype;
1040 sockin6.sin6_addr = ia6;
1041 sockin6.sin6_port = htons (port);
1042 addr = (struct sockaddr *) &sockin6;
Daniel Veillard5c396542002-03-15 07:57:50 +00001043#endif
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001044 } else
1045 break; /* for */
Daniel Veillard5c396542002-03-15 07:57:50 +00001046
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001047 s = xmlNanoHTTPConnectAttempt (addr);
1048 if (s != -1)
1049 return (s);
1050 }
Owen Taylor3473f882001-02-23 17:55:21 +00001051 }
Owen Taylor3473f882001-02-23 17:55:21 +00001052#ifdef DEBUG_HTTP
1053 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard5c396542002-03-15 07:57:50 +00001054 "xmlNanoHTTPConnectHost: unable to connect to '%s'.\n",
1055 host);
Owen Taylor3473f882001-02-23 17:55:21 +00001056#endif
Daniel Veillard5c396542002-03-15 07:57:50 +00001057 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001058}
1059
1060
1061/**
1062 * xmlNanoHTTPOpen:
1063 * @URL: The URL to load
1064 * @contentType: if available the Content-Type information will be
1065 * returned at that location
1066 *
1067 * This function try to open a connection to the indicated resource
1068 * via HTTP GET.
1069 *
1070 * Returns NULL in case of failure, otherwise a request handler.
1071 * The contentType, if provided must be freed by the caller
1072 */
1073
1074void*
1075xmlNanoHTTPOpen(const char *URL, char **contentType) {
1076 if (contentType != NULL) *contentType = NULL;
Daniel Veillardf012a642001-07-23 19:10:52 +00001077 return(xmlNanoHTTPMethod(URL, NULL, NULL, contentType, NULL, 0));
Daniel Veillard9403a042001-05-28 11:00:53 +00001078}
1079
1080/**
1081 * xmlNanoHTTPOpenRedir:
1082 * @URL: The URL to load
1083 * @contentType: if available the Content-Type information will be
1084 * returned at that location
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001085 * @redir: if available the redirected URL will be returned
Daniel Veillard9403a042001-05-28 11:00:53 +00001086 *
1087 * This function try to open a connection to the indicated resource
1088 * via HTTP GET.
1089 *
1090 * Returns NULL in case of failure, otherwise a request handler.
1091 * The contentType, if provided must be freed by the caller
1092 */
1093
1094void*
1095xmlNanoHTTPOpenRedir(const char *URL, char **contentType, char **redir) {
1096 if (contentType != NULL) *contentType = NULL;
1097 if (redir != NULL) *redir = NULL;
Daniel Veillardf012a642001-07-23 19:10:52 +00001098 return(xmlNanoHTTPMethodRedir(URL, NULL, NULL, contentType, redir, NULL,0));
Owen Taylor3473f882001-02-23 17:55:21 +00001099}
1100
1101/**
1102 * xmlNanoHTTPRead:
1103 * @ctx: the HTTP context
1104 * @dest: a buffer
1105 * @len: the buffer length
1106 *
1107 * This function tries to read @len bytes from the existing HTTP connection
1108 * and saves them in @dest. This is a blocking call.
1109 *
1110 * Returns the number of byte read. 0 is an indication of an end of connection.
1111 * -1 indicates a parameter error.
1112 */
1113int
1114xmlNanoHTTPRead(void *ctx, void *dest, int len) {
1115 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1116
1117 if (ctx == NULL) return(-1);
1118 if (dest == NULL) return(-1);
1119 if (len <= 0) return(0);
1120
1121 while (ctxt->inptr - ctxt->inrptr < len) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001122 if (xmlNanoHTTPRecv(ctxt) <= 0) break;
Owen Taylor3473f882001-02-23 17:55:21 +00001123 }
1124 if (ctxt->inptr - ctxt->inrptr < len)
1125 len = ctxt->inptr - ctxt->inrptr;
1126 memcpy(dest, ctxt->inrptr, len);
1127 ctxt->inrptr += len;
1128 return(len);
1129}
1130
1131/**
1132 * xmlNanoHTTPClose:
1133 * @ctx: the HTTP context
1134 *
1135 * This function closes an HTTP context, it ends up the connection and
1136 * free all data related to it.
1137 */
1138void
1139xmlNanoHTTPClose(void *ctx) {
1140 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1141
1142 if (ctx == NULL) return;
1143
1144 xmlNanoHTTPFreeCtxt(ctxt);
1145}
1146
1147/**
Daniel Veillard9403a042001-05-28 11:00:53 +00001148 * xmlNanoHTTPMethodRedir:
Owen Taylor3473f882001-02-23 17:55:21 +00001149 * @URL: The URL to load
1150 * @method: the HTTP method to use
1151 * @input: the input string if any
1152 * @contentType: the Content-Type information IN and OUT
Daniel Veillard9403a042001-05-28 11:00:53 +00001153 * @redir: the redirected URL OUT
Owen Taylor3473f882001-02-23 17:55:21 +00001154 * @headers: the extra headers
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001155 * @ilen: input length
Owen Taylor3473f882001-02-23 17:55:21 +00001156 *
1157 * This function try to open a connection to the indicated resource
1158 * via HTTP using the given @method, adding the given extra headers
1159 * and the input buffer for the request content.
1160 *
1161 * Returns NULL in case of failure, otherwise a request handler.
Daniel Veillard9403a042001-05-28 11:00:53 +00001162 * The contentType, or redir, if provided must be freed by the caller
Owen Taylor3473f882001-02-23 17:55:21 +00001163 */
1164
1165void*
Daniel Veillard9403a042001-05-28 11:00:53 +00001166xmlNanoHTTPMethodRedir(const char *URL, const char *method, const char *input,
Daniel Veillardf012a642001-07-23 19:10:52 +00001167 char **contentType, char **redir,
1168 const char *headers, int ilen ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001169 xmlNanoHTTPCtxtPtr ctxt;
1170 char *bp, *p;
Daniel Veillardf012a642001-07-23 19:10:52 +00001171 int blen, ret;
Owen Taylor3473f882001-02-23 17:55:21 +00001172 int head;
1173 int nbRedirects = 0;
1174 char *redirURL = NULL;
William M. Brack78637da2003-07-31 14:47:38 +00001175#ifdef DEBUG_HTTP
1176 int xmt_bytes;
1177#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001178
1179 if (URL == NULL) return(NULL);
1180 if (method == NULL) method = "GET";
1181 xmlNanoHTTPInit();
1182
1183retry:
1184 if (redirURL == NULL)
1185 ctxt = xmlNanoHTTPNewCtxt(URL);
1186 else {
1187 ctxt = xmlNanoHTTPNewCtxt(redirURL);
Daniel Veillarda840b692003-10-19 13:35:37 +00001188 ctxt->location = xmlMemStrdup(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001189 }
1190
Daniel Veillardf012a642001-07-23 19:10:52 +00001191 if ( ctxt == NULL ) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001192 return ( NULL );
1193 }
1194
Owen Taylor3473f882001-02-23 17:55:21 +00001195 if ((ctxt->protocol == NULL) || (strcmp(ctxt->protocol, "http"))) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001196 __xmlIOErr(XML_FROM_HTTP, XML_HTTP_URL_SYNTAX, "Not a valid HTTP URI");
Owen Taylor3473f882001-02-23 17:55:21 +00001197 xmlNanoHTTPFreeCtxt(ctxt);
1198 if (redirURL != NULL) xmlFree(redirURL);
1199 return(NULL);
1200 }
1201 if (ctxt->hostname == NULL) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001202 __xmlIOErr(XML_FROM_HTTP, XML_HTTP_UNKNOWN_HOST,
1203 "Failed to identify host in URI");
Owen Taylor3473f882001-02-23 17:55:21 +00001204 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001205 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001206 return(NULL);
1207 }
1208 if (proxy) {
1209 blen = strlen(ctxt->hostname) * 2 + 16;
1210 ret = xmlNanoHTTPConnectHost(proxy, proxyPort);
1211 }
1212 else {
1213 blen = strlen(ctxt->hostname);
1214 ret = xmlNanoHTTPConnectHost(ctxt->hostname, ctxt->port);
1215 }
1216 if (ret < 0) {
1217 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001218 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001219 return(NULL);
1220 }
1221 ctxt->fd = ret;
1222
Daniel Veillardf012a642001-07-23 19:10:52 +00001223 if (input == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00001224 ilen = 0;
Daniel Veillardf012a642001-07-23 19:10:52 +00001225 else
1226 blen += 36;
1227
Owen Taylor3473f882001-02-23 17:55:21 +00001228 if (headers != NULL)
Daniel Veillardf012a642001-07-23 19:10:52 +00001229 blen += strlen(headers) + 2;
Owen Taylor3473f882001-02-23 17:55:21 +00001230 if (contentType && *contentType)
1231 blen += strlen(*contentType) + 16;
Daniel Veillardf012a642001-07-23 19:10:52 +00001232 blen += strlen(method) + strlen(ctxt->path) + 24;
Daniel Veillard82cb3192003-10-29 13:39:15 +00001233 bp = (char*)xmlMallocAtomic(blen);
Daniel Veillardf012a642001-07-23 19:10:52 +00001234 if ( bp == NULL ) {
1235 xmlNanoHTTPFreeCtxt( ctxt );
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001236 xmlHTTPErrMemory("allocating header buffer");
Daniel Veillardf012a642001-07-23 19:10:52 +00001237 return ( NULL );
1238 }
1239
1240 p = bp;
1241
Owen Taylor3473f882001-02-23 17:55:21 +00001242 if (proxy) {
1243 if (ctxt->port != 80) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001244 p += snprintf( p, blen - (p - bp), "%s http://%s:%d%s",
1245 method, ctxt->hostname,
Daniel Veillardf012a642001-07-23 19:10:52 +00001246 ctxt->port, ctxt->path );
Owen Taylor3473f882001-02-23 17:55:21 +00001247 }
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001248 else
1249 p += snprintf( p, blen - (p - bp), "%s http://%s%s", method,
Daniel Veillardf012a642001-07-23 19:10:52 +00001250 ctxt->hostname, ctxt->path);
Owen Taylor3473f882001-02-23 17:55:21 +00001251 }
1252 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001253 p += snprintf( p, blen - (p - bp), "%s %s", method, ctxt->path);
Daniel Veillardf012a642001-07-23 19:10:52 +00001254
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001255 p += snprintf( p, blen - (p - bp), " HTTP/1.0\r\nHost: %s\r\n",
1256 ctxt->hostname);
Daniel Veillardf012a642001-07-23 19:10:52 +00001257
1258 if (contentType != NULL && *contentType)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001259 p += snprintf(p, blen - (p - bp), "Content-Type: %s\r\n", *contentType);
Daniel Veillardf012a642001-07-23 19:10:52 +00001260
1261 if (headers != NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001262 p += snprintf( p, blen - (p - bp), "%s", headers );
Daniel Veillardf012a642001-07-23 19:10:52 +00001263
Owen Taylor3473f882001-02-23 17:55:21 +00001264 if (input != NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001265 snprintf(p, blen - (p - bp), "Content-Length: %d\r\n\r\n", ilen );
Owen Taylor3473f882001-02-23 17:55:21 +00001266 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001267 snprintf(p, blen - (p - bp), "\r\n");
Daniel Veillardf012a642001-07-23 19:10:52 +00001268
Owen Taylor3473f882001-02-23 17:55:21 +00001269#ifdef DEBUG_HTTP
1270 xmlGenericError(xmlGenericErrorContext,
1271 "-> %s%s", proxy? "(Proxy) " : "", bp);
1272 if ((blen -= strlen(bp)+1) < 0)
1273 xmlGenericError(xmlGenericErrorContext,
1274 "ERROR: overflowed buffer by %d bytes\n", -blen);
1275#endif
1276 ctxt->outptr = ctxt->out = bp;
1277 ctxt->state = XML_NANO_HTTP_WRITE;
Daniel Veillardf012a642001-07-23 19:10:52 +00001278 blen = strlen( ctxt->out );
Daniel Veillardf012a642001-07-23 19:10:52 +00001279#ifdef DEBUG_HTTP
William M. Brack78637da2003-07-31 14:47:38 +00001280 xmt_bytes = xmlNanoHTTPSend(ctxt, ctxt->out, blen );
Daniel Veillardf012a642001-07-23 19:10:52 +00001281 if ( xmt_bytes != blen )
1282 xmlGenericError( xmlGenericErrorContext,
1283 "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
1284 xmt_bytes, blen,
1285 "bytes of HTTP headers sent to host",
1286 ctxt->hostname );
William M. Brack78637da2003-07-31 14:47:38 +00001287#else
1288 xmlNanoHTTPSend(ctxt, ctxt->out, blen );
Daniel Veillardf012a642001-07-23 19:10:52 +00001289#endif
1290
1291 if ( input != NULL ) {
William M. Brack78637da2003-07-31 14:47:38 +00001292#ifdef DEBUG_HTTP
Daniel Veillardf012a642001-07-23 19:10:52 +00001293 xmt_bytes = xmlNanoHTTPSend( ctxt, input, ilen );
1294
Daniel Veillardf012a642001-07-23 19:10:52 +00001295 if ( xmt_bytes != ilen )
1296 xmlGenericError( xmlGenericErrorContext,
1297 "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
1298 xmt_bytes, ilen,
1299 "bytes of HTTP content sent to host",
1300 ctxt->hostname );
William M. Brack78637da2003-07-31 14:47:38 +00001301#else
1302 xmlNanoHTTPSend( ctxt, input, ilen );
Daniel Veillardf012a642001-07-23 19:10:52 +00001303#endif
1304 }
1305
Owen Taylor3473f882001-02-23 17:55:21 +00001306 ctxt->state = XML_NANO_HTTP_READ;
1307 head = 1;
1308
1309 while ((p = xmlNanoHTTPReadLine(ctxt)) != NULL) {
1310 if (head && (*p == 0)) {
1311 head = 0;
1312 ctxt->content = ctxt->inrptr;
1313 xmlFree(p);
1314 break;
1315 }
1316 xmlNanoHTTPScanAnswer(ctxt, p);
1317
1318#ifdef DEBUG_HTTP
1319 xmlGenericError(xmlGenericErrorContext, "<- %s\n", p);
1320#endif
1321 xmlFree(p);
1322 }
1323
1324 if ((ctxt->location != NULL) && (ctxt->returnValue >= 300) &&
1325 (ctxt->returnValue < 400)) {
1326#ifdef DEBUG_HTTP
1327 xmlGenericError(xmlGenericErrorContext,
1328 "\nRedirect to: %s\n", ctxt->location);
1329#endif
Daniel Veillardf012a642001-07-23 19:10:52 +00001330 while ( xmlNanoHTTPRecv(ctxt) > 0 ) ;
Owen Taylor3473f882001-02-23 17:55:21 +00001331 if (nbRedirects < XML_NANO_HTTP_MAX_REDIR) {
1332 nbRedirects++;
Daniel Veillard9403a042001-05-28 11:00:53 +00001333 if (redirURL != NULL)
1334 xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001335 redirURL = xmlMemStrdup(ctxt->location);
1336 xmlNanoHTTPFreeCtxt(ctxt);
1337 goto retry;
1338 }
1339 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001340 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001341#ifdef DEBUG_HTTP
1342 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardf012a642001-07-23 19:10:52 +00001343 "xmlNanoHTTPMethodRedir: Too many redirects, aborting ...\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001344#endif
1345 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001346 }
1347
1348 if (contentType != NULL) {
1349 if (ctxt->contentType != NULL)
1350 *contentType = xmlMemStrdup(ctxt->contentType);
1351 else
1352 *contentType = NULL;
1353 }
1354
Daniel Veillard9403a042001-05-28 11:00:53 +00001355 if ((redir != NULL) && (redirURL != NULL)) {
1356 *redir = redirURL;
1357 } else {
1358 if (redirURL != NULL)
1359 xmlFree(redirURL);
1360 if (redir != NULL)
1361 *redir = NULL;
1362 }
1363
Owen Taylor3473f882001-02-23 17:55:21 +00001364#ifdef DEBUG_HTTP
1365 if (ctxt->contentType != NULL)
1366 xmlGenericError(xmlGenericErrorContext,
1367 "\nCode %d, content-type '%s'\n\n",
1368 ctxt->returnValue, ctxt->contentType);
1369 else
1370 xmlGenericError(xmlGenericErrorContext,
1371 "\nCode %d, no content-type\n\n",
1372 ctxt->returnValue);
1373#endif
1374
1375 return((void *) ctxt);
1376}
1377
1378/**
Daniel Veillard9403a042001-05-28 11:00:53 +00001379 * xmlNanoHTTPMethod:
1380 * @URL: The URL to load
1381 * @method: the HTTP method to use
1382 * @input: the input string if any
1383 * @contentType: the Content-Type information IN and OUT
1384 * @headers: the extra headers
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001385 * @ilen: input length
Daniel Veillard9403a042001-05-28 11:00:53 +00001386 *
1387 * This function try to open a connection to the indicated resource
1388 * via HTTP using the given @method, adding the given extra headers
1389 * and the input buffer for the request content.
1390 *
1391 * Returns NULL in case of failure, otherwise a request handler.
1392 * The contentType, if provided must be freed by the caller
1393 */
1394
1395void*
1396xmlNanoHTTPMethod(const char *URL, const char *method, const char *input,
Daniel Veillardf012a642001-07-23 19:10:52 +00001397 char **contentType, const char *headers, int ilen) {
Daniel Veillard9403a042001-05-28 11:00:53 +00001398 return(xmlNanoHTTPMethodRedir(URL, method, input, contentType,
Daniel Veillardf012a642001-07-23 19:10:52 +00001399 NULL, headers, ilen));
Daniel Veillard9403a042001-05-28 11:00:53 +00001400}
1401
1402/**
Owen Taylor3473f882001-02-23 17:55:21 +00001403 * xmlNanoHTTPFetch:
1404 * @URL: The URL to load
1405 * @filename: the filename where the content should be saved
1406 * @contentType: if available the Content-Type information will be
1407 * returned at that location
1408 *
1409 * This function try to fetch the indicated resource via HTTP GET
1410 * and save it's content in the file.
1411 *
1412 * Returns -1 in case of failure, 0 incase of success. The contentType,
1413 * if provided must be freed by the caller
1414 */
1415int
1416xmlNanoHTTPFetch(const char *URL, const char *filename, char **contentType) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001417 void *ctxt = NULL;
1418 char *buf = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001419 int fd;
1420 int len;
1421
William M. Brack015ccb22005-02-13 08:18:52 +00001422 if (filename == NULL) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001423 ctxt = xmlNanoHTTPOpen(URL, contentType);
1424 if (ctxt == NULL) return(-1);
1425
1426 if (!strcmp(filename, "-"))
1427 fd = 0;
1428 else {
1429 fd = open(filename, O_CREAT | O_WRONLY, 00644);
1430 if (fd < 0) {
1431 xmlNanoHTTPClose(ctxt);
1432 if ((contentType != NULL) && (*contentType != NULL)) {
1433 xmlFree(*contentType);
1434 *contentType = NULL;
1435 }
1436 return(-1);
1437 }
1438 }
1439
Daniel Veillardf012a642001-07-23 19:10:52 +00001440 xmlNanoHTTPFetchContent( ctxt, &buf, &len );
1441 if ( len > 0 ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001442 write(fd, buf, len);
1443 }
1444
1445 xmlNanoHTTPClose(ctxt);
1446 close(fd);
1447 return(0);
1448}
1449
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001450#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001451/**
1452 * xmlNanoHTTPSave:
1453 * @ctxt: the HTTP context
1454 * @filename: the filename where the content should be saved
1455 *
1456 * This function saves the output of the HTTP transaction to a file
1457 * It closes and free the context at the end
1458 *
1459 * Returns -1 in case of failure, 0 incase of success.
1460 */
1461int
1462xmlNanoHTTPSave(void *ctxt, const char *filename) {
Daniel Veillarde3924972001-07-25 20:25:21 +00001463 char *buf = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001464 int fd;
1465 int len;
1466
William M. Brack015ccb22005-02-13 08:18:52 +00001467 if ((ctxt == NULL) || (filename == NULL)) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001468
1469 if (!strcmp(filename, "-"))
1470 fd = 0;
1471 else {
1472 fd = open(filename, O_CREAT | O_WRONLY);
1473 if (fd < 0) {
1474 xmlNanoHTTPClose(ctxt);
1475 return(-1);
1476 }
1477 }
1478
Daniel Veillardf012a642001-07-23 19:10:52 +00001479 xmlNanoHTTPFetchContent( ctxt, &buf, &len );
1480 if ( len > 0 ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001481 write(fd, buf, len);
1482 }
1483
1484 xmlNanoHTTPClose(ctxt);
William M. Brack20d82362004-03-17 08:44:46 +00001485 close(fd);
Owen Taylor3473f882001-02-23 17:55:21 +00001486 return(0);
1487}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001488#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001489
1490/**
1491 * xmlNanoHTTPReturnCode:
1492 * @ctx: the HTTP context
1493 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001494 * Get the latest HTTP return code received
1495 *
Owen Taylor3473f882001-02-23 17:55:21 +00001496 * Returns the HTTP return code for the request.
1497 */
1498int
1499xmlNanoHTTPReturnCode(void *ctx) {
1500 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1501
1502 if (ctxt == NULL) return(-1);
1503
1504 return(ctxt->returnValue);
1505}
1506
1507/**
1508 * xmlNanoHTTPAuthHeader:
1509 * @ctx: the HTTP context
1510 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001511 * Get the authentication header of an HTTP context
1512 *
Owen Taylor3473f882001-02-23 17:55:21 +00001513 * Returns the stashed value of the WWW-Authenticate or Proxy-Authenticate
1514 * header.
1515 */
1516const char *
1517xmlNanoHTTPAuthHeader(void *ctx) {
1518 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1519
1520 if (ctxt == NULL) return(NULL);
1521
1522 return(ctxt->authHeader);
1523}
1524
Daniel Veillardf012a642001-07-23 19:10:52 +00001525/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00001526 * xmlNanoHTTPContentLength:
Daniel Veillardf012a642001-07-23 19:10:52 +00001527 * @ctx: the HTTP context
1528 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001529 * Provides the specified content length from the HTTP header.
1530 *
Daniel Veillardf012a642001-07-23 19:10:52 +00001531 * Return the specified content length from the HTTP header. Note that
1532 * a value of -1 indicates that the content length element was not included in
1533 * the response header.
1534 */
1535int
1536xmlNanoHTTPContentLength( void * ctx ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001537 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillardf012a642001-07-23 19:10:52 +00001538
1539 return ( ( ctxt == NULL ) ? -1 : ctxt->ContentLength );
1540}
1541
1542/**
Daniel Veillard847332a2003-10-18 11:29:40 +00001543 * xmlNanoHTTPRedir:
1544 * @ctx: the HTTP context
1545 *
1546 * Provides the specified redirection URL if available from the HTTP header.
1547 *
1548 * Return the specified redirection URL or NULL if not redirected.
1549 */
1550const char *
1551xmlNanoHTTPRedir( void * ctx ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001552 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillard847332a2003-10-18 11:29:40 +00001553
1554 return ( ( ctxt == NULL ) ? NULL : ctxt->location );
1555}
1556
1557/**
1558 * xmlNanoHTTPEncoding:
1559 * @ctx: the HTTP context
1560 *
1561 * Provides the specified encoding if specified in the HTTP headers.
1562 *
1563 * Return the specified encoding or NULL if not available
1564 */
1565const char *
1566xmlNanoHTTPEncoding( void * ctx ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001567 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillard847332a2003-10-18 11:29:40 +00001568
1569 return ( ( ctxt == NULL ) ? NULL : ctxt->encoding );
1570}
1571
1572/**
Daniel Veillarda840b692003-10-19 13:35:37 +00001573 * xmlNanoHTTPMimeType:
1574 * @ctx: the HTTP context
1575 *
1576 * Provides the specified Mime-Type if specified in the HTTP headers.
1577 *
1578 * Return the specified Mime-Type or NULL if not available
1579 */
1580const char *
1581xmlNanoHTTPMimeType( void * ctx ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001582 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillarda840b692003-10-19 13:35:37 +00001583
1584 return ( ( ctxt == NULL ) ? NULL : ctxt->mimeType );
1585}
1586
1587/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00001588 * xmlNanoHTTPFetchContent:
Daniel Veillardf012a642001-07-23 19:10:52 +00001589 * @ctx: the HTTP context
1590 * @ptr: pointer to set to the content buffer.
1591 * @len: integer pointer to hold the length of the content
1592 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001593 * Check if all the content was read
1594 *
Daniel Veillardf012a642001-07-23 19:10:52 +00001595 * Returns 0 if all the content was read and available, returns
1596 * -1 if received content length was less than specified or an error
1597 * occurred.
1598 */
Daniel Veillarda2351322004-06-27 12:08:10 +00001599static int
Daniel Veillardf012a642001-07-23 19:10:52 +00001600xmlNanoHTTPFetchContent( void * ctx, char ** ptr, int * len ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001601 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillardf012a642001-07-23 19:10:52 +00001602
1603 int rc = 0;
1604 int cur_lgth;
1605 int rcvd_lgth;
1606 int dummy_int;
1607 char * dummy_ptr = NULL;
1608
1609 /* Dummy up return input parameters if not provided */
1610
1611 if ( len == NULL )
1612 len = &dummy_int;
1613
1614 if ( ptr == NULL )
1615 ptr = &dummy_ptr;
1616
1617 /* But can't work without the context pointer */
1618
1619 if ( ( ctxt == NULL ) || ( ctxt->content == NULL ) ) {
1620 *len = 0;
1621 *ptr = NULL;
1622 return ( -1 );
1623 }
1624
1625 rcvd_lgth = ctxt->inptr - ctxt->content;
1626
1627 while ( (cur_lgth = xmlNanoHTTPRecv( ctxt )) > 0 ) {
1628
1629 rcvd_lgth += cur_lgth;
1630 if ( (ctxt->ContentLength > 0) && (rcvd_lgth >= ctxt->ContentLength) )
1631 break;
1632 }
1633
1634 *ptr = ctxt->content;
1635 *len = rcvd_lgth;
1636
1637 if ( ( ctxt->ContentLength > 0 ) && ( rcvd_lgth < ctxt->ContentLength ) )
1638 rc = -1;
1639 else if ( rcvd_lgth == 0 )
1640 rc = -1;
1641
1642 return ( rc );
1643}
1644
Owen Taylor3473f882001-02-23 17:55:21 +00001645#ifdef STANDALONE
1646int main(int argc, char **argv) {
1647 char *contentType = NULL;
1648
1649 if (argv[1] != NULL) {
1650 if (argv[2] != NULL)
1651 xmlNanoHTTPFetch(argv[1], argv[2], &contentType);
1652 else
1653 xmlNanoHTTPFetch(argv[1], "-", &contentType);
1654 if (contentType != NULL) xmlFree(contentType);
1655 } else {
1656 xmlGenericError(xmlGenericErrorContext,
1657 "%s: minimal HTTP GET implementation\n", argv[0]);
1658 xmlGenericError(xmlGenericErrorContext,
1659 "\tusage %s [ URL [ filename ] ]\n", argv[0]);
1660 }
1661 xmlNanoHTTPCleanup();
1662 xmlMemoryDump();
1663 return(0);
1664}
1665#endif /* STANDALONE */
1666#else /* !LIBXML_HTTP_ENABLED */
1667#ifdef STANDALONE
1668#include <stdio.h>
1669int main(int argc, char **argv) {
1670 xmlGenericError(xmlGenericErrorContext,
1671 "%s : HTTP support not compiled in\n", argv[0]);
1672 return(0);
1673}
1674#endif /* STANDALONE */
1675#endif /* LIBXML_HTTP_ENABLED */