blob: ae7923f143e6ed918587102e402971b304dbb81c [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
Daniel Veillardf3afa7d2001-06-09 13:52:58 +000014#define NEED_SOCKETS
Daniel Veillard34ce8be2002-03-18 19:37:11 +000015#define IN_LIBXML
Bjorn Reese70a9da52001-04-21 16:57:29 +000016#include "libxml.h"
Owen Taylor3473f882001-02-23 17:55:21 +000017
18#ifdef LIBXML_HTTP_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +000019#include <string.h>
20
21#ifdef HAVE_STDLIB_H
22#include <stdlib.h>
23#endif
24#ifdef HAVE_UNISTD_H
25#include <unistd.h>
26#endif
Daniel Veillard75eb1ad2003-07-07 14:42:44 +000027#ifdef HAVE_SYS_TYPES_H
28#include <sys/types.h>
29#endif
Owen Taylor3473f882001-02-23 17:55:21 +000030#ifdef HAVE_SYS_SOCKET_H
31#include <sys/socket.h>
32#endif
33#ifdef HAVE_NETINET_IN_H
34#include <netinet/in.h>
35#endif
36#ifdef HAVE_ARPA_INET_H
37#include <arpa/inet.h>
38#endif
39#ifdef HAVE_NETDB_H
40#include <netdb.h>
41#endif
Daniel Veillardd85f4f42002-03-25 10:48:46 +000042#ifdef HAVE_RESOLV_H
Daniel Veillard9b731d72002-04-14 12:56:08 +000043#ifdef HAVE_ARPA_NAMESER_H
44#include <arpa/nameser.h>
45#endif
Daniel Veillardd85f4f42002-03-25 10:48:46 +000046#include <resolv.h>
47#endif
Owen Taylor3473f882001-02-23 17:55:21 +000048#ifdef HAVE_FCNTL_H
49#include <fcntl.h>
50#endif
51#ifdef HAVE_ERRNO_H
52#include <errno.h>
53#endif
54#ifdef HAVE_SYS_TIME_H
55#include <sys/time.h>
56#endif
57#ifdef HAVE_SYS_SELECT_H
58#include <sys/select.h>
59#endif
60#ifdef HAVE_STRINGS_H
61#include <strings.h>
62#endif
63#ifdef SUPPORT_IP6
64#include <resolv.h>
65#endif
Daniel Veillard9a2724d2005-12-15 11:12:26 +000066#ifdef HAVE_ZLIB_H
67#include <zlib.h>
68#endif
69
Owen Taylor3473f882001-02-23 17:55:21 +000070
71#ifdef VMS
72#include <stropts>
Daniel Veillardc284c642005-03-31 10:24:24 +000073#define XML_SOCKLEN_T unsigned int
Owen Taylor3473f882001-02-23 17:55:21 +000074#define SOCKET int
75#endif
76
Daniel Veillard1638a472003-08-14 01:23:25 +000077
78#ifdef __MINGW32__
79#define _WINSOCKAPI_
80#include <wsockcompat.h>
81#include <winsock2.h>
Daniel Veillardc284c642005-03-31 10:24:24 +000082#undef XML_SOCKLEN_T
83#define XML_SOCKLEN_T unsigned int
Daniel Veillard1638a472003-08-14 01:23:25 +000084#endif
85
86
Daniel Veillardd0463562001-10-13 09:15:48 +000087#include <libxml/globals.h>
Daniel Veillardf012a642001-07-23 19:10:52 +000088#include <libxml/xmlerror.h>
Owen Taylor3473f882001-02-23 17:55:21 +000089#include <libxml/xmlmemory.h>
90#include <libxml/parser.h> /* for xmlStr(n)casecmp() */
91#include <libxml/nanohttp.h>
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000092#include <libxml/globals.h>
Daniel Veillard8efff672002-12-04 11:44:48 +000093#include <libxml/uri.h>
Owen Taylor3473f882001-02-23 17:55:21 +000094
95/**
96 * A couple portability macros
97 */
98#ifndef _WINSOCKAPI_
Daniel Veillarda9cce9c2003-09-29 13:20:24 +000099#ifndef __BEOS__
Owen Taylor3473f882001-02-23 17:55:21 +0000100#define closesocket(s) close(s)
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000101#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000102#define SOCKET int
103#endif
104
Daniel Veillard89f7f272003-09-29 13:29:09 +0000105#ifdef __BEOS__
106#ifndef PF_INET
107#define PF_INET AF_INET
108#endif
109#endif
110
Daniel Veillardc284c642005-03-31 10:24:24 +0000111#ifndef XML_SOCKLEN_T
112#define XML_SOCKLEN_T unsigned int
Daniel Veillard75be0132002-03-13 10:03:35 +0000113#endif
114#ifndef SOCKET
115#define SOCKET int
116#endif
Daniel Veillardf012a642001-07-23 19:10:52 +0000117
Owen Taylor3473f882001-02-23 17:55:21 +0000118#ifdef STANDALONE
119#define DEBUG_HTTP
120#define xmlStrncasecmp(a, b, n) strncasecmp((char *)a, (char *)b, n)
121#define xmlStrcasecmpi(a, b) strcasecmp((char *)a, (char *)b)
122#endif
123
124#define XML_NANO_HTTP_MAX_REDIR 10
125
126#define XML_NANO_HTTP_CHUNK 4096
127
128#define XML_NANO_HTTP_CLOSED 0
129#define XML_NANO_HTTP_WRITE 1
130#define XML_NANO_HTTP_READ 2
131#define XML_NANO_HTTP_NONE 4
132
133typedef struct xmlNanoHTTPCtxt {
134 char *protocol; /* the protocol name */
135 char *hostname; /* the host name */
136 int port; /* the port */
137 char *path; /* the path within the URL */
Daniel Veillard351f2d62005-04-13 02:55:12 +0000138 char *query; /* the query string */
Owen Taylor3473f882001-02-23 17:55:21 +0000139 SOCKET fd; /* the file descriptor for the socket */
140 int state; /* WRITE / READ / CLOSED */
141 char *out; /* buffer sent (zero terminated) */
142 char *outptr; /* index within the buffer sent */
143 char *in; /* the receiving buffer */
144 char *content; /* the start of the content */
145 char *inptr; /* the next byte to read from network */
146 char *inrptr; /* the next byte to give back to the client */
147 int inlen; /* len of the input buffer */
148 int last; /* return code for last operation */
149 int returnValue; /* the protocol return value */
Daniel Veillardf012a642001-07-23 19:10:52 +0000150 int ContentLength; /* specified content length from HTTP header */
Owen Taylor3473f882001-02-23 17:55:21 +0000151 char *contentType; /* the MIME type for the input */
152 char *location; /* the new URL in case of redirect */
153 char *authHeader; /* contents of {WWW,Proxy}-Authenticate header */
Daniel Veillard847332a2003-10-18 11:29:40 +0000154 char *encoding; /* encoding extracted from the contentType */
Daniel Veillarda840b692003-10-19 13:35:37 +0000155 char *mimeType; /* Mime-Type extracted from the contentType */
Daniel Veillard9a2724d2005-12-15 11:12:26 +0000156#ifdef HAVE_ZLIB_H
157 z_stream *strm; /* Zlib stream object */
158 int usesGzip; /* "Content-Encoding: gzip" was detected */
159#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000160} xmlNanoHTTPCtxt, *xmlNanoHTTPCtxtPtr;
161
162static int initialized = 0;
163static char *proxy = NULL; /* the proxy name if any */
164static int proxyPort; /* the proxy port if any */
165static unsigned int timeout = 60;/* the select() timeout in seconds */
166
Daniel Veillarda2351322004-06-27 12:08:10 +0000167static int xmlNanoHTTPFetchContent( void * ctx, char ** ptr, int * len );
Daniel Veillardf012a642001-07-23 19:10:52 +0000168
Owen Taylor3473f882001-02-23 17:55:21 +0000169/**
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000170 * xmlHTTPErrMemory:
171 * @extra: extra informations
172 *
173 * Handle an out of memory condition
174 */
175static void
176xmlHTTPErrMemory(const char *extra)
177{
178 __xmlSimpleError(XML_FROM_HTTP, XML_ERR_NO_MEMORY, NULL, NULL, extra);
179}
180
181/**
Owen Taylor3473f882001-02-23 17:55:21 +0000182 * A portability function
183 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000184static int socket_errno(void) {
Owen Taylor3473f882001-02-23 17:55:21 +0000185#ifdef _WINSOCKAPI_
186 return(WSAGetLastError());
187#else
188 return(errno);
189#endif
190}
191
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000192#ifdef SUPPORT_IP6
Daniel Veillard2db8c122003-07-08 12:16:59 +0000193static
194int have_ipv6(void) {
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000195 int s;
196
197 s = socket (AF_INET6, SOCK_STREAM, 0);
198 if (s != -1) {
199 close (s);
200 return (1);
201 }
202 return (0);
203}
204#endif
205
Owen Taylor3473f882001-02-23 17:55:21 +0000206/**
207 * xmlNanoHTTPInit:
208 *
209 * Initialize the HTTP protocol layer.
210 * Currently it just checks for proxy informations
211 */
212
213void
214xmlNanoHTTPInit(void) {
215 const char *env;
216#ifdef _WINSOCKAPI_
217 WSADATA wsaData;
218#endif
219
220 if (initialized)
221 return;
222
223#ifdef _WINSOCKAPI_
224 if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0)
225 return;
226#endif
227
228 if (proxy == NULL) {
229 proxyPort = 80;
230 env = getenv("no_proxy");
Daniel Veillard29b17482004-08-16 00:39:03 +0000231 if (env && ((env[0] == '*') && (env[1] == 0)))
Owen Taylor3473f882001-02-23 17:55:21 +0000232 goto done;
233 env = getenv("http_proxy");
234 if (env != NULL) {
235 xmlNanoHTTPScanProxy(env);
236 goto done;
237 }
238 env = getenv("HTTP_PROXY");
239 if (env != NULL) {
240 xmlNanoHTTPScanProxy(env);
241 goto done;
242 }
243 }
244done:
245 initialized = 1;
246}
247
248/**
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000249 * xmlNanoHTTPCleanup:
Owen Taylor3473f882001-02-23 17:55:21 +0000250 *
251 * Cleanup the HTTP protocol layer.
252 */
253
254void
255xmlNanoHTTPCleanup(void) {
Daniel Veillard744acff2005-07-12 15:09:53 +0000256 if (proxy != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +0000257 xmlFree(proxy);
Daniel Veillard744acff2005-07-12 15:09:53 +0000258 proxy = NULL;
259 }
Owen Taylor3473f882001-02-23 17:55:21 +0000260#ifdef _WINSOCKAPI_
261 if (initialized)
262 WSACleanup();
263#endif
264 initialized = 0;
265 return;
266}
267
268/**
Owen Taylor3473f882001-02-23 17:55:21 +0000269 * xmlNanoHTTPScanURL:
270 * @ctxt: an HTTP context
271 * @URL: The URL used to initialize the context
272 *
273 * (Re)Initialize an HTTP context by parsing the URL and finding
274 * the protocol host port and path it indicates.
275 */
276
277static void
278xmlNanoHTTPScanURL(xmlNanoHTTPCtxtPtr ctxt, const char *URL) {
William M. Brack015ccb22005-02-13 08:18:52 +0000279 xmlURIPtr uri;
280 /*
281 * Clear any existing data from the context
282 */
Owen Taylor3473f882001-02-23 17:55:21 +0000283 if (ctxt->protocol != NULL) {
284 xmlFree(ctxt->protocol);
285 ctxt->protocol = NULL;
286 }
287 if (ctxt->hostname != NULL) {
288 xmlFree(ctxt->hostname);
289 ctxt->hostname = NULL;
290 }
291 if (ctxt->path != NULL) {
292 xmlFree(ctxt->path);
293 ctxt->path = NULL;
294 }
Daniel Veillard351f2d62005-04-13 02:55:12 +0000295 if (ctxt->query != NULL) {
296 xmlFree(ctxt->query);
297 ctxt->query = NULL;
298 }
Owen Taylor3473f882001-02-23 17:55:21 +0000299 if (URL == NULL) return;
William M. Brack015ccb22005-02-13 08:18:52 +0000300
Daniel Veillard336a8e12005-08-07 10:46:19 +0000301 uri = xmlParseURIRaw(URL, 1);
William M. Brack015ccb22005-02-13 08:18:52 +0000302 if (uri == NULL)
303 return;
304
305 if ((uri->scheme == NULL) || (uri->server == NULL)) {
306 xmlFreeURI(uri);
307 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000308 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000309
William M. Brack015ccb22005-02-13 08:18:52 +0000310 ctxt->protocol = xmlMemStrdup(uri->scheme);
311 ctxt->hostname = xmlMemStrdup(uri->server);
312 if (uri->path != NULL)
313 ctxt->path = xmlMemStrdup(uri->path);
314 else
315 ctxt->path = xmlMemStrdup("/");
Daniel Veillard351f2d62005-04-13 02:55:12 +0000316 if (uri->query != NULL)
317 ctxt->query = xmlMemStrdup(uri->query);
William M. Brack015ccb22005-02-13 08:18:52 +0000318 if (uri->port != 0)
319 ctxt->port = uri->port;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000320
William M. Brack015ccb22005-02-13 08:18:52 +0000321 xmlFreeURI(uri);
Owen Taylor3473f882001-02-23 17:55:21 +0000322}
323
324/**
325 * xmlNanoHTTPScanProxy:
326 * @URL: The proxy URL used to initialize the proxy context
327 *
328 * (Re)Initialize the HTTP Proxy context by parsing the URL and finding
329 * the protocol host port it indicates.
330 * Should be like http://myproxy/ or http://myproxy:3128/
331 * A NULL URL cleans up proxy informations.
332 */
333
334void
335xmlNanoHTTPScanProxy(const char *URL) {
William M. Brack015ccb22005-02-13 08:18:52 +0000336 xmlURIPtr uri;
Owen Taylor3473f882001-02-23 17:55:21 +0000337
338 if (proxy != NULL) {
339 xmlFree(proxy);
340 proxy = NULL;
341 }
William M. Brack015ccb22005-02-13 08:18:52 +0000342 proxyPort = 0;
343
Owen Taylor3473f882001-02-23 17:55:21 +0000344#ifdef DEBUG_HTTP
345 if (URL == NULL)
346 xmlGenericError(xmlGenericErrorContext,
347 "Removing HTTP proxy info\n");
348 else
349 xmlGenericError(xmlGenericErrorContext,
350 "Using HTTP proxy %s\n", URL);
351#endif
352 if (URL == NULL) return;
William M. Brack015ccb22005-02-13 08:18:52 +0000353
Daniel Veillard336a8e12005-08-07 10:46:19 +0000354 uri = xmlParseURIRaw(URL, 1);
William M. Brack015ccb22005-02-13 08:18:52 +0000355 if ((uri == NULL) || (uri->scheme == NULL) ||
356 (strcmp(uri->scheme, "http")) || (uri->server == NULL)) {
357 __xmlIOErr(XML_FROM_HTTP, XML_HTTP_URL_SYNTAX, "Syntax Error\n");
358 if (uri != NULL)
359 xmlFreeURI(uri);
360 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000361 }
William M. Brack015ccb22005-02-13 08:18:52 +0000362
363 proxy = xmlMemStrdup(uri->server);
364 if (uri->port != 0)
365 proxyPort = uri->port;
Owen Taylor3473f882001-02-23 17:55:21 +0000366
William M. Brack015ccb22005-02-13 08:18:52 +0000367 xmlFreeURI(uri);
Owen Taylor3473f882001-02-23 17:55:21 +0000368}
369
370/**
371 * xmlNanoHTTPNewCtxt:
372 * @URL: The URL used to initialize the context
373 *
374 * Allocate and initialize a new HTTP context.
375 *
376 * Returns an HTTP context or NULL in case of error.
377 */
378
379static xmlNanoHTTPCtxtPtr
380xmlNanoHTTPNewCtxt(const char *URL) {
381 xmlNanoHTTPCtxtPtr ret;
382
383 ret = (xmlNanoHTTPCtxtPtr) xmlMalloc(sizeof(xmlNanoHTTPCtxt));
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000384 if (ret == NULL) {
385 xmlHTTPErrMemory("allocating context");
386 return(NULL);
387 }
Owen Taylor3473f882001-02-23 17:55:21 +0000388
389 memset(ret, 0, sizeof(xmlNanoHTTPCtxt));
390 ret->port = 80;
391 ret->returnValue = 0;
392 ret->fd = -1;
Daniel Veillardf012a642001-07-23 19:10:52 +0000393 ret->ContentLength = -1;
Owen Taylor3473f882001-02-23 17:55:21 +0000394
Daniel Veillardcacbe5d2003-01-10 16:09:51 +0000395 xmlNanoHTTPScanURL(ret, URL);
Owen Taylor3473f882001-02-23 17:55:21 +0000396
397 return(ret);
398}
399
400/**
401 * xmlNanoHTTPFreeCtxt:
402 * @ctxt: an HTTP context
403 *
404 * Frees the context after closing the connection.
405 */
406
407static void
408xmlNanoHTTPFreeCtxt(xmlNanoHTTPCtxtPtr ctxt) {
409 if (ctxt == NULL) return;
410 if (ctxt->hostname != NULL) xmlFree(ctxt->hostname);
411 if (ctxt->protocol != NULL) xmlFree(ctxt->protocol);
412 if (ctxt->path != NULL) xmlFree(ctxt->path);
Daniel Veillard351f2d62005-04-13 02:55:12 +0000413 if (ctxt->query != NULL) xmlFree(ctxt->query);
Owen Taylor3473f882001-02-23 17:55:21 +0000414 if (ctxt->out != NULL) xmlFree(ctxt->out);
415 if (ctxt->in != NULL) xmlFree(ctxt->in);
416 if (ctxt->contentType != NULL) xmlFree(ctxt->contentType);
Daniel Veillard847332a2003-10-18 11:29:40 +0000417 if (ctxt->encoding != NULL) xmlFree(ctxt->encoding);
Daniel Veillarda840b692003-10-19 13:35:37 +0000418 if (ctxt->mimeType != NULL) xmlFree(ctxt->mimeType);
Owen Taylor3473f882001-02-23 17:55:21 +0000419 if (ctxt->location != NULL) xmlFree(ctxt->location);
420 if (ctxt->authHeader != NULL) xmlFree(ctxt->authHeader);
Daniel Veillard9a2724d2005-12-15 11:12:26 +0000421#ifdef HAVE_ZLIB_H
422 if (ctxt->strm != NULL) {
423 inflateEnd(ctxt->strm);
424 xmlFree(ctxt->strm);
425 }
426#endif
427
Owen Taylor3473f882001-02-23 17:55:21 +0000428 ctxt->state = XML_NANO_HTTP_NONE;
429 if (ctxt->fd >= 0) closesocket(ctxt->fd);
430 ctxt->fd = -1;
431 xmlFree(ctxt);
432}
433
434/**
435 * xmlNanoHTTPSend:
436 * @ctxt: an HTTP context
437 *
438 * Send the input needed to initiate the processing on the server side
Daniel Veillardf012a642001-07-23 19:10:52 +0000439 * Returns number of bytes sent or -1 on error.
Owen Taylor3473f882001-02-23 17:55:21 +0000440 */
441
Daniel Veillardf012a642001-07-23 19:10:52 +0000442static int
443xmlNanoHTTPSend(xmlNanoHTTPCtxtPtr ctxt, const char * xmt_ptr, int outlen) {
444
445 int total_sent = 0;
446
447 if ( (ctxt->state & XML_NANO_HTTP_WRITE) && (xmt_ptr != NULL ) ) {
448 while (total_sent < outlen) {
449 int nsent = send(ctxt->fd, xmt_ptr + total_sent,
450 outlen - total_sent, 0);
Owen Taylor3473f882001-02-23 17:55:21 +0000451 if (nsent>0)
452 total_sent += nsent;
Daniel Veillardf012a642001-07-23 19:10:52 +0000453 else if ( ( nsent == -1 ) &&
Daniel Veillardba6db032001-07-31 16:25:45 +0000454#if defined(EAGAIN) && EAGAIN != EWOULDBLOCK
Daniel Veillardf012a642001-07-23 19:10:52 +0000455 ( socket_errno( ) != EAGAIN ) &&
Daniel Veillardba6db032001-07-31 16:25:45 +0000456#endif
Daniel Veillardf012a642001-07-23 19:10:52 +0000457 ( socket_errno( ) != EWOULDBLOCK ) ) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000458 __xmlIOErr(XML_FROM_HTTP, 0, "send failed\n");
Daniel Veillardf012a642001-07-23 19:10:52 +0000459 if ( total_sent == 0 )
460 total_sent = -1;
461 break;
462 }
463 else {
464 /*
465 ** No data sent
466 ** Since non-blocking sockets are used, wait for
467 ** socket to be writable or default timeout prior
468 ** to retrying.
469 */
470
471 struct timeval tv;
472 fd_set wfd;
473
474 tv.tv_sec = timeout;
475 tv.tv_usec = 0;
476 FD_ZERO( &wfd );
Daniel Veillard9e2110b2005-08-08 20:33:54 +0000477#ifdef _MSC_VER
478#pragma warning(push)
479#pragma warning(disable: 4018)
480#endif
Daniel Veillardf012a642001-07-23 19:10:52 +0000481 FD_SET( ctxt->fd, &wfd );
Daniel Veillard9e2110b2005-08-08 20:33:54 +0000482#ifdef _MSC_VER
483#pragma warning(pop)
484#endif
Daniel Veillardf012a642001-07-23 19:10:52 +0000485 (void)select( ctxt->fd + 1, NULL, &wfd, NULL, &tv );
486 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000487 }
Owen Taylor3473f882001-02-23 17:55:21 +0000488 }
Daniel Veillardf012a642001-07-23 19:10:52 +0000489
490 return total_sent;
Owen Taylor3473f882001-02-23 17:55:21 +0000491}
492
493/**
494 * xmlNanoHTTPRecv:
495 * @ctxt: an HTTP context
496 *
497 * Read information coming from the HTTP connection.
498 * This is a blocking call (but it blocks in select(), not read()).
499 *
500 * Returns the number of byte read or -1 in case of error.
501 */
502
503static int
504xmlNanoHTTPRecv(xmlNanoHTTPCtxtPtr ctxt) {
505 fd_set rfd;
506 struct timeval tv;
507
508
509 while (ctxt->state & XML_NANO_HTTP_READ) {
510 if (ctxt->in == NULL) {
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000511 ctxt->in = (char *) xmlMallocAtomic(65000 * sizeof(char));
Owen Taylor3473f882001-02-23 17:55:21 +0000512 if (ctxt->in == NULL) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000513 xmlHTTPErrMemory("allocating input");
Owen Taylor3473f882001-02-23 17:55:21 +0000514 ctxt->last = -1;
515 return(-1);
516 }
517 ctxt->inlen = 65000;
518 ctxt->inptr = ctxt->content = ctxt->inrptr = ctxt->in;
519 }
520 if (ctxt->inrptr > ctxt->in + XML_NANO_HTTP_CHUNK) {
521 int delta = ctxt->inrptr - ctxt->in;
522 int len = ctxt->inptr - ctxt->inrptr;
523
524 memmove(ctxt->in, ctxt->inrptr, len);
525 ctxt->inrptr -= delta;
526 ctxt->content -= delta;
527 ctxt->inptr -= delta;
528 }
529 if ((ctxt->in + ctxt->inlen) < (ctxt->inptr + XML_NANO_HTTP_CHUNK)) {
530 int d_inptr = ctxt->inptr - ctxt->in;
531 int d_content = ctxt->content - ctxt->in;
532 int d_inrptr = ctxt->inrptr - ctxt->in;
Daniel Veillardf012a642001-07-23 19:10:52 +0000533 char * tmp_ptr = ctxt->in;
Owen Taylor3473f882001-02-23 17:55:21 +0000534
535 ctxt->inlen *= 2;
Daniel Veillardf012a642001-07-23 19:10:52 +0000536 ctxt->in = (char *) xmlRealloc(tmp_ptr, ctxt->inlen);
Owen Taylor3473f882001-02-23 17:55:21 +0000537 if (ctxt->in == NULL) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000538 xmlHTTPErrMemory("allocating input buffer");
Daniel Veillardf012a642001-07-23 19:10:52 +0000539 xmlFree( tmp_ptr );
Owen Taylor3473f882001-02-23 17:55:21 +0000540 ctxt->last = -1;
541 return(-1);
542 }
543 ctxt->inptr = ctxt->in + d_inptr;
544 ctxt->content = ctxt->in + d_content;
545 ctxt->inrptr = ctxt->in + d_inrptr;
546 }
547 ctxt->last = recv(ctxt->fd, ctxt->inptr, XML_NANO_HTTP_CHUNK, 0);
548 if (ctxt->last > 0) {
549 ctxt->inptr += ctxt->last;
550 return(ctxt->last);
551 }
552 if (ctxt->last == 0) {
553 return(0);
554 }
555 if (ctxt->last == -1) {
556 switch (socket_errno()) {
557 case EINPROGRESS:
558 case EWOULDBLOCK:
559#if defined(EAGAIN) && EAGAIN != EWOULDBLOCK
560 case EAGAIN:
561#endif
562 break;
Daniel Veillardf012a642001-07-23 19:10:52 +0000563
564 case ECONNRESET:
565 case ESHUTDOWN:
566 return ( 0 );
567
Owen Taylor3473f882001-02-23 17:55:21 +0000568 default:
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000569 __xmlIOErr(XML_FROM_HTTP, 0, "recv failed\n");
Daniel Veillardf012a642001-07-23 19:10:52 +0000570 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +0000571 }
572 }
573
574 tv.tv_sec = timeout;
575 tv.tv_usec = 0;
576 FD_ZERO(&rfd);
Daniel Veillard9e2110b2005-08-08 20:33:54 +0000577#ifdef _MSC_VER
578#pragma warning(push)
579#pragma warning(disable: 4018)
580#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000581 FD_SET(ctxt->fd, &rfd);
Daniel Veillard9e2110b2005-08-08 20:33:54 +0000582#ifdef _MSC_VER
583#pragma warning(pop)
584#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000585
Daniel Veillard50f34372001-08-03 12:06:36 +0000586 if ( (select(ctxt->fd+1, &rfd, NULL, NULL, &tv)<1)
587#if defined(EINTR)
588 && (errno != EINTR)
589#endif
590 )
Owen Taylor3473f882001-02-23 17:55:21 +0000591 return(0);
592 }
593 return(0);
594}
595
596/**
597 * xmlNanoHTTPReadLine:
598 * @ctxt: an HTTP context
599 *
600 * Read one line in the HTTP server output, usually for extracting
601 * the HTTP protocol informations from the answer header.
602 *
603 * Returns a newly allocated string with a copy of the line, or NULL
604 * which indicate the end of the input.
605 */
606
607static char *
608xmlNanoHTTPReadLine(xmlNanoHTTPCtxtPtr ctxt) {
609 char buf[4096];
610 char *bp = buf;
Daniel Veillardf012a642001-07-23 19:10:52 +0000611 int rc;
Owen Taylor3473f882001-02-23 17:55:21 +0000612
613 while (bp - buf < 4095) {
614 if (ctxt->inrptr == ctxt->inptr) {
Daniel Veillardf012a642001-07-23 19:10:52 +0000615 if ( (rc = xmlNanoHTTPRecv(ctxt)) == 0) {
Owen Taylor3473f882001-02-23 17:55:21 +0000616 if (bp == buf)
617 return(NULL);
618 else
619 *bp = 0;
620 return(xmlMemStrdup(buf));
621 }
Daniel Veillardf012a642001-07-23 19:10:52 +0000622 else if ( rc == -1 ) {
623 return ( NULL );
624 }
Owen Taylor3473f882001-02-23 17:55:21 +0000625 }
626 *bp = *ctxt->inrptr++;
627 if (*bp == '\n') {
628 *bp = 0;
629 return(xmlMemStrdup(buf));
630 }
631 if (*bp != '\r')
632 bp++;
633 }
634 buf[4095] = 0;
635 return(xmlMemStrdup(buf));
636}
637
638
639/**
640 * xmlNanoHTTPScanAnswer:
641 * @ctxt: an HTTP context
642 * @line: an HTTP header line
643 *
644 * Try to extract useful informations from the server answer.
645 * We currently parse and process:
646 * - The HTTP revision/ return code
Daniel Veillarda840b692003-10-19 13:35:37 +0000647 * - The Content-Type, Mime-Type and charset used
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000648 * - The Location for redirect processing.
Owen Taylor3473f882001-02-23 17:55:21 +0000649 *
650 * Returns -1 in case of failure, the file descriptor number otherwise
651 */
652
653static void
654xmlNanoHTTPScanAnswer(xmlNanoHTTPCtxtPtr ctxt, const char *line) {
655 const char *cur = line;
656
657 if (line == NULL) return;
658
659 if (!strncmp(line, "HTTP/", 5)) {
660 int version = 0;
661 int ret = 0;
662
663 cur += 5;
664 while ((*cur >= '0') && (*cur <= '9')) {
665 version *= 10;
666 version += *cur - '0';
667 cur++;
668 }
669 if (*cur == '.') {
670 cur++;
671 if ((*cur >= '0') && (*cur <= '9')) {
672 version *= 10;
673 version += *cur - '0';
674 cur++;
675 }
676 while ((*cur >= '0') && (*cur <= '9'))
677 cur++;
678 } else
679 version *= 10;
680 if ((*cur != ' ') && (*cur != '\t')) return;
681 while ((*cur == ' ') || (*cur == '\t')) cur++;
682 if ((*cur < '0') || (*cur > '9')) return;
683 while ((*cur >= '0') && (*cur <= '9')) {
684 ret *= 10;
685 ret += *cur - '0';
686 cur++;
687 }
688 if ((*cur != 0) && (*cur != ' ') && (*cur != '\t')) return;
689 ctxt->returnValue = ret;
690 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Content-Type:", 13)) {
Daniel Veillarda840b692003-10-19 13:35:37 +0000691 const xmlChar *charset, *last, *mime;
Owen Taylor3473f882001-02-23 17:55:21 +0000692 cur += 13;
693 while ((*cur == ' ') || (*cur == '\t')) cur++;
694 if (ctxt->contentType != NULL)
695 xmlFree(ctxt->contentType);
696 ctxt->contentType = xmlMemStrdup(cur);
Daniel Veillarda840b692003-10-19 13:35:37 +0000697 mime = (const xmlChar *) cur;
698 last = mime;
699 while ((*last != 0) && (*last != ' ') && (*last != '\t') &&
700 (*last != ';') && (*last != ','))
701 last++;
702 if (ctxt->mimeType != NULL)
703 xmlFree(ctxt->mimeType);
704 ctxt->mimeType = (char *) xmlStrndup(mime, last - mime);
705 charset = xmlStrstr(BAD_CAST ctxt->contentType, BAD_CAST "charset=");
706 if (charset != NULL) {
707 charset += 8;
708 last = charset;
709 while ((*last != 0) && (*last != ' ') && (*last != '\t') &&
710 (*last != ';') && (*last != ','))
711 last++;
712 if (ctxt->encoding != NULL)
713 xmlFree(ctxt->encoding);
714 ctxt->encoding = (char *) xmlStrndup(charset, last - charset);
715 }
Owen Taylor3473f882001-02-23 17:55:21 +0000716 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"ContentType:", 12)) {
Daniel Veillarda840b692003-10-19 13:35:37 +0000717 const xmlChar *charset, *last, *mime;
Owen Taylor3473f882001-02-23 17:55:21 +0000718 cur += 12;
719 if (ctxt->contentType != NULL) return;
720 while ((*cur == ' ') || (*cur == '\t')) cur++;
721 ctxt->contentType = xmlMemStrdup(cur);
Daniel Veillarda840b692003-10-19 13:35:37 +0000722 mime = (const xmlChar *) cur;
723 last = mime;
724 while ((*last != 0) && (*last != ' ') && (*last != '\t') &&
725 (*last != ';') && (*last != ','))
726 last++;
727 if (ctxt->mimeType != NULL)
728 xmlFree(ctxt->mimeType);
729 ctxt->mimeType = (char *) xmlStrndup(mime, last - mime);
730 charset = xmlStrstr(BAD_CAST ctxt->contentType, BAD_CAST "charset=");
731 if (charset != NULL) {
732 charset += 8;
733 last = charset;
734 while ((*last != 0) && (*last != ' ') && (*last != '\t') &&
735 (*last != ';') && (*last != ','))
736 last++;
737 if (ctxt->encoding != NULL)
738 xmlFree(ctxt->encoding);
739 ctxt->encoding = (char *) xmlStrndup(charset, last - charset);
740 }
Owen Taylor3473f882001-02-23 17:55:21 +0000741 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Location:", 9)) {
742 cur += 9;
743 while ((*cur == ' ') || (*cur == '\t')) cur++;
744 if (ctxt->location != NULL)
745 xmlFree(ctxt->location);
William M. Brack7e29c0a2004-04-02 09:07:22 +0000746 if (*cur == '/') {
747 xmlChar *tmp_http = xmlStrdup(BAD_CAST "http://");
748 xmlChar *tmp_loc =
749 xmlStrcat(tmp_http, (const xmlChar *) ctxt->hostname);
750 ctxt->location =
751 (char *) xmlStrcat (tmp_loc, (const xmlChar *) cur);
752 } else {
753 ctxt->location = xmlMemStrdup(cur);
754 }
Owen Taylor3473f882001-02-23 17:55:21 +0000755 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"WWW-Authenticate:", 17)) {
756 cur += 17;
757 while ((*cur == ' ') || (*cur == '\t')) cur++;
758 if (ctxt->authHeader != NULL)
759 xmlFree(ctxt->authHeader);
760 ctxt->authHeader = xmlMemStrdup(cur);
761 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Proxy-Authenticate:", 19)) {
762 cur += 19;
763 while ((*cur == ' ') || (*cur == '\t')) cur++;
764 if (ctxt->authHeader != NULL)
765 xmlFree(ctxt->authHeader);
766 ctxt->authHeader = xmlMemStrdup(cur);
Daniel Veillard9a2724d2005-12-15 11:12:26 +0000767#ifdef HAVE_ZLIB_H
768 } else if ( !xmlStrncasecmp( BAD_CAST line, BAD_CAST"Content-Encoding:", 17) ) {
769 cur += 17;
770 while ((*cur == ' ') || (*cur == '\t')) cur++;
771 if ( !xmlStrncasecmp( BAD_CAST cur, BAD_CAST"gzip", 4) ) {
772 ctxt->usesGzip = 1;
773
774 ctxt->strm = xmlMalloc(sizeof(z_stream));
775
776 if (ctxt->strm != NULL) {
777 ctxt->strm->zalloc = Z_NULL;
778 ctxt->strm->zfree = Z_NULL;
779 ctxt->strm->opaque = Z_NULL;
780 ctxt->strm->avail_in = 0;
781 ctxt->strm->next_in = Z_NULL;
782
783 inflateInit2( ctxt->strm, 31 );
784 }
785 }
786#endif
Daniel Veillardf012a642001-07-23 19:10:52 +0000787 } else if ( !xmlStrncasecmp( BAD_CAST line, BAD_CAST"Content-Length:", 15) ) {
788 cur += 15;
789 ctxt->ContentLength = strtol( cur, NULL, 10 );
Owen Taylor3473f882001-02-23 17:55:21 +0000790 }
791}
792
793/**
794 * xmlNanoHTTPConnectAttempt:
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000795 * @addr: a socket address structure
Owen Taylor3473f882001-02-23 17:55:21 +0000796 *
797 * Attempt a connection to the given IP:port endpoint. It forces
798 * non-blocking semantic on the socket, and allow 60 seconds for
799 * the host to answer.
800 *
801 * Returns -1 in case of failure, the file descriptor number otherwise
802 */
803
804static int
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000805xmlNanoHTTPConnectAttempt(struct sockaddr *addr)
Owen Taylor3473f882001-02-23 17:55:21 +0000806{
Owen Taylor3473f882001-02-23 17:55:21 +0000807 fd_set wfd;
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000808#ifdef _WINSOCKAPI_
809 fd_set xfd;
810#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000811 struct timeval tv;
812 int status;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000813 int addrlen;
814 SOCKET s;
Owen Taylor3473f882001-02-23 17:55:21 +0000815
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000816#ifdef SUPPORT_IP6
817 if (addr->sa_family == AF_INET6) {
818 s = socket (PF_INET6, SOCK_STREAM, IPPROTO_TCP);
819 addrlen = sizeof (struct sockaddr_in6);
820 }
821 else
822#endif
823 {
824 s = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
825 addrlen = sizeof (struct sockaddr_in);
826 }
Owen Taylor3473f882001-02-23 17:55:21 +0000827 if (s==-1) {
828#ifdef DEBUG_HTTP
829 perror("socket");
830#endif
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000831 __xmlIOErr(XML_FROM_HTTP, 0, "socket failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000832 return(-1);
833 }
834
835#ifdef _WINSOCKAPI_
836 {
837 u_long one = 1;
838
839 status = ioctlsocket(s, FIONBIO, &one) == SOCKET_ERROR ? -1 : 0;
840 }
841#else /* _WINSOCKAPI_ */
842#if defined(VMS)
843 {
844 int enable = 1;
845 status = ioctl(s, FIONBIO, &enable);
846 }
847#else /* VMS */
Daniel Veillard254b1262003-11-01 17:04:58 +0000848#if defined(__BEOS__)
849 {
850 bool noblock = true;
851 status = setsockopt(s, SOL_SOCKET, SO_NONBLOCK, &noblock, sizeof(noblock));
852 }
853#else /* __BEOS__ */
Owen Taylor3473f882001-02-23 17:55:21 +0000854 if ((status = fcntl(s, F_GETFL, 0)) != -1) {
855#ifdef O_NONBLOCK
856 status |= O_NONBLOCK;
857#else /* O_NONBLOCK */
858#ifdef F_NDELAY
859 status |= F_NDELAY;
860#endif /* F_NDELAY */
861#endif /* !O_NONBLOCK */
862 status = fcntl(s, F_SETFL, status);
863 }
864 if (status < 0) {
865#ifdef DEBUG_HTTP
866 perror("nonblocking");
867#endif
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000868 __xmlIOErr(XML_FROM_HTTP, 0, "error setting non-blocking IO\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000869 closesocket(s);
870 return(-1);
871 }
Daniel Veillard254b1262003-11-01 17:04:58 +0000872#endif /* !__BEOS__ */
Owen Taylor3473f882001-02-23 17:55:21 +0000873#endif /* !VMS */
874#endif /* !_WINSOCKAPI_ */
875
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000876 if (connect (s, addr, addrlen) == -1) {
Owen Taylor3473f882001-02-23 17:55:21 +0000877 switch (socket_errno()) {
878 case EINPROGRESS:
879 case EWOULDBLOCK:
880 break;
881 default:
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000882 __xmlIOErr(XML_FROM_HTTP, 0, "error connecting to HTTP server");
Owen Taylor3473f882001-02-23 17:55:21 +0000883 closesocket(s);
884 return(-1);
885 }
886 }
887
888 tv.tv_sec = timeout;
889 tv.tv_usec = 0;
Daniel Veillard9e2110b2005-08-08 20:33:54 +0000890
891#ifdef _MSC_VER
892#pragma warning(push)
893#pragma warning(disable: 4018)
894#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000895 FD_ZERO(&wfd);
896 FD_SET(s, &wfd);
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000897
898#ifdef _WINSOCKAPI_
899 FD_ZERO(&xfd);
900 FD_SET(s, &xfd);
Owen Taylor3473f882001-02-23 17:55:21 +0000901
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000902 switch(select(s+1, NULL, &wfd, &xfd, &tv))
903#else
Owen Taylor3473f882001-02-23 17:55:21 +0000904 switch(select(s+1, NULL, &wfd, NULL, &tv))
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000905#endif
Daniel Veillard9e2110b2005-08-08 20:33:54 +0000906#ifdef _MSC_VER
907#pragma warning(pop)
908#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000909 {
910 case 0:
911 /* Time out */
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000912 __xmlIOErr(XML_FROM_HTTP, 0, "Connect attempt timed out");
Owen Taylor3473f882001-02-23 17:55:21 +0000913 closesocket(s);
914 return(-1);
915 case -1:
916 /* Ermm.. ?? */
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000917 __xmlIOErr(XML_FROM_HTTP, 0, "Connect failed");
Owen Taylor3473f882001-02-23 17:55:21 +0000918 closesocket(s);
919 return(-1);
920 }
921
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000922 if ( FD_ISSET(s, &wfd)
923#ifdef _WINSOCKAPI_
924 || FD_ISSET(s, &xfd)
925#endif
926 ) {
Daniel Veillardc284c642005-03-31 10:24:24 +0000927 XML_SOCKLEN_T len;
Owen Taylor3473f882001-02-23 17:55:21 +0000928 len = sizeof(status);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000929#ifdef SO_ERROR
Owen Taylor3473f882001-02-23 17:55:21 +0000930 if (getsockopt(s, SOL_SOCKET, SO_ERROR, (char*)&status, &len) < 0 ) {
931 /* Solaris error code */
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000932 __xmlIOErr(XML_FROM_HTTP, 0, "getsockopt failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000933 return (-1);
934 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000935#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000936 if ( status ) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000937 __xmlIOErr(XML_FROM_HTTP, 0, "Error connecting to remote host");
Owen Taylor3473f882001-02-23 17:55:21 +0000938 closesocket(s);
939 errno = status;
940 return (-1);
941 }
942 } else {
943 /* pbm */
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000944 __xmlIOErr(XML_FROM_HTTP, 0, "select failed\n");
Daniel Veillardf012a642001-07-23 19:10:52 +0000945 closesocket(s);
Owen Taylor3473f882001-02-23 17:55:21 +0000946 return (-1);
947 }
948
949 return(s);
950}
951
952/**
953 * xmlNanoHTTPConnectHost:
954 * @host: the host name
955 * @port: the port number
956 *
957 * Attempt a connection to the given host:port endpoint. It tries
958 * the multiple IP provided by the DNS if available.
959 *
960 * Returns -1 in case of failure, the file descriptor number otherwise
961 */
962
963static int
964xmlNanoHTTPConnectHost(const char *host, int port)
965{
966 struct hostent *h;
Daniel Veillard2db8c122003-07-08 12:16:59 +0000967 struct sockaddr *addr = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +0000968 struct in_addr ia;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000969 struct sockaddr_in sockin;
Daniel Veillard5c396542002-03-15 07:57:50 +0000970
Owen Taylor3473f882001-02-23 17:55:21 +0000971#ifdef SUPPORT_IP6
972 struct in6_addr ia6;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000973 struct sockaddr_in6 sockin6;
Owen Taylor3473f882001-02-23 17:55:21 +0000974#endif
975 int i;
976 int s;
Daniel Veillard5c396542002-03-15 07:57:50 +0000977
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000978 memset (&sockin, 0, sizeof(sockin));
979#ifdef SUPPORT_IP6
980 memset (&sockin6, 0, sizeof(sockin6));
Rob Richardscb418de2005-10-13 23:12:42 +0000981#endif
982
983#if !defined(HAVE_GETADDRINFO) && defined(SUPPORT_IP6) && defined(RES_USE_INET6)
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000984 if (have_ipv6 ())
Daniel Veillard560c2a42003-07-06 21:13:49 +0000985 {
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000986 if (!(_res.options & RES_INIT))
987 res_init();
988 _res.options |= RES_USE_INET6;
989 }
Rob Richardscb418de2005-10-13 23:12:42 +0000990#endif
991
992#if defined(HAVE_GETADDRINFO) && defined(SUPPORT_IP6) && !defined(_WIN32)
993 if (have_ipv6 ())
994#endif
995#if defined(HAVE_GETADDRINFO) && (defined(SUPPORT_IP6) || defined(_WIN32))
Daniel Veillard560c2a42003-07-06 21:13:49 +0000996 {
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000997 int status;
998 struct addrinfo hints, *res, *result;
999
1000 result = NULL;
1001 memset (&hints, 0,sizeof(hints));
1002 hints.ai_socktype = SOCK_STREAM;
1003
1004 status = getaddrinfo (host, NULL, &hints, &result);
1005 if (status) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001006 __xmlIOErr(XML_FROM_HTTP, 0, "getaddrinfo failed\n");
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001007 return (-1);
1008 }
1009
1010 for (res = result; res; res = res->ai_next) {
Rob Richardscb418de2005-10-13 23:12:42 +00001011 if (res->ai_family == AF_INET) {
1012 if (res->ai_addrlen > sizeof(sockin)) {
1013 __xmlIOErr(XML_FROM_HTTP, 0, "address size mismatch\n");
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001014 freeaddrinfo (result);
Rob Richardscb418de2005-10-13 23:12:42 +00001015 return (-1);
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001016 }
Rob Richardscb418de2005-10-13 23:12:42 +00001017 memcpy (&sockin, res->ai_addr, res->ai_addrlen);
1018 sockin.sin_port = htons (port);
1019 addr = (struct sockaddr *)&sockin;
1020#ifdef SUPPORT_IP6
1021 } else if (have_ipv6 () && (res->ai_family == AF_INET6)) {
1022 if (res->ai_addrlen > sizeof(sockin6)) {
1023 __xmlIOErr(XML_FROM_HTTP, 0, "address size mismatch\n");
1024 freeaddrinfo (result);
1025 return (-1);
1026 }
1027 memcpy (&sockin6, res->ai_addr, res->ai_addrlen);
1028 sockin6.sin6_port = htons (port);
1029 addr = (struct sockaddr *)&sockin6;
1030#endif
1031 } else
1032 continue; /* for */
1033
1034 s = xmlNanoHTTPConnectAttempt (addr);
1035 if (s != -1) {
1036 freeaddrinfo (result);
1037 return (s);
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001038 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001039 }
Rob Richardscb418de2005-10-13 23:12:42 +00001040
Daniel Veillard3dc93a42003-07-10 14:04:33 +00001041 if (result)
1042 freeaddrinfo (result);
Rob Richardscb418de2005-10-13 23:12:42 +00001043 }
Owen Taylor3473f882001-02-23 17:55:21 +00001044#endif
Rob Richardscb418de2005-10-13 23:12:42 +00001045#if defined(HAVE_GETADDRINFO) && defined(SUPPORT_IP6) && !defined(_WIN32)
1046 else
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001047#endif
Rob Richardscb418de2005-10-13 23:12:42 +00001048#if !defined(HAVE_GETADDRINFO) || !defined(_WIN32)
1049 {
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001050 h = gethostbyname (host);
1051 if (h == NULL) {
Daniel Veillard56b2db72002-03-25 16:35:28 +00001052
1053/*
1054 * Okay, I got fed up by the non-portability of this error message
1055 * extraction code. it work on Linux, if it work on your platform
1056 * and one want to enable it, send me the defined(foobar) needed
1057 */
1058#if defined(HAVE_NETDB_H) && defined(HOST_NOT_FOUND) && defined(linux)
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001059 const char *h_err_txt = "";
Daniel Veillardf012a642001-07-23 19:10:52 +00001060
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001061 switch (h_errno) {
1062 case HOST_NOT_FOUND:
1063 h_err_txt = "Authoritive host not found";
1064 break;
Daniel Veillardf012a642001-07-23 19:10:52 +00001065
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001066 case TRY_AGAIN:
1067 h_err_txt =
1068 "Non-authoritive host not found or server failure.";
1069 break;
Daniel Veillardf012a642001-07-23 19:10:52 +00001070
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001071 case NO_RECOVERY:
1072 h_err_txt =
1073 "Non-recoverable errors: FORMERR, REFUSED, or NOTIMP.";
1074 break;
Daniel Veillard5c396542002-03-15 07:57:50 +00001075
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001076 case NO_ADDRESS:
1077 h_err_txt =
1078 "Valid name, no data record of requested type.";
1079 break;
Daniel Veillard5c396542002-03-15 07:57:50 +00001080
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001081 default:
1082 h_err_txt = "No error text defined.";
1083 break;
1084 }
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001085 __xmlIOErr(XML_FROM_HTTP, 0, h_err_txt);
Daniel Veillard5c396542002-03-15 07:57:50 +00001086#else
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001087 __xmlIOErr(XML_FROM_HTTP, 0, "Failed to resolve host");
Owen Taylor3473f882001-02-23 17:55:21 +00001088#endif
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001089 return (-1);
1090 }
Daniel Veillard5c396542002-03-15 07:57:50 +00001091
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001092 for (i = 0; h->h_addr_list[i]; i++) {
1093 if (h->h_addrtype == AF_INET) {
1094 /* A records (IPv4) */
Daniel Veillard8e2c9792004-10-27 09:39:50 +00001095 if ((unsigned int) h->h_length > sizeof(ia)) {
1096 __xmlIOErr(XML_FROM_HTTP, 0, "address size mismatch\n");
1097 return (-1);
1098 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001099 memcpy (&ia, h->h_addr_list[i], h->h_length);
1100 sockin.sin_family = h->h_addrtype;
1101 sockin.sin_addr = ia;
Daniel Veillard9e2110b2005-08-08 20:33:54 +00001102 sockin.sin_port = (u_short)htons ((unsigned short)port);
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001103 addr = (struct sockaddr *) &sockin;
Daniel Veillard5c396542002-03-15 07:57:50 +00001104#ifdef SUPPORT_IP6
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001105 } else if (have_ipv6 () && (h->h_addrtype == AF_INET6)) {
1106 /* AAAA records (IPv6) */
Daniel Veillard8e2c9792004-10-27 09:39:50 +00001107 if ((unsigned int) h->h_length > sizeof(ia6)) {
1108 __xmlIOErr(XML_FROM_HTTP, 0, "address size mismatch\n");
1109 return (-1);
1110 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001111 memcpy (&ia6, h->h_addr_list[i], h->h_length);
1112 sockin6.sin6_family = h->h_addrtype;
1113 sockin6.sin6_addr = ia6;
1114 sockin6.sin6_port = htons (port);
1115 addr = (struct sockaddr *) &sockin6;
Daniel Veillard5c396542002-03-15 07:57:50 +00001116#endif
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001117 } else
1118 break; /* for */
Daniel Veillard5c396542002-03-15 07:57:50 +00001119
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001120 s = xmlNanoHTTPConnectAttempt (addr);
1121 if (s != -1)
1122 return (s);
1123 }
Owen Taylor3473f882001-02-23 17:55:21 +00001124 }
Rob Richardscb418de2005-10-13 23:12:42 +00001125#endif
1126
Owen Taylor3473f882001-02-23 17:55:21 +00001127#ifdef DEBUG_HTTP
1128 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard5c396542002-03-15 07:57:50 +00001129 "xmlNanoHTTPConnectHost: unable to connect to '%s'.\n",
1130 host);
Owen Taylor3473f882001-02-23 17:55:21 +00001131#endif
Daniel Veillard5c396542002-03-15 07:57:50 +00001132 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001133}
1134
1135
1136/**
1137 * xmlNanoHTTPOpen:
1138 * @URL: The URL to load
1139 * @contentType: if available the Content-Type information will be
1140 * returned at that location
1141 *
1142 * This function try to open a connection to the indicated resource
1143 * via HTTP GET.
1144 *
1145 * Returns NULL in case of failure, otherwise a request handler.
1146 * The contentType, if provided must be freed by the caller
1147 */
1148
1149void*
1150xmlNanoHTTPOpen(const char *URL, char **contentType) {
1151 if (contentType != NULL) *contentType = NULL;
Daniel Veillardf012a642001-07-23 19:10:52 +00001152 return(xmlNanoHTTPMethod(URL, NULL, NULL, contentType, NULL, 0));
Daniel Veillard9403a042001-05-28 11:00:53 +00001153}
1154
1155/**
1156 * xmlNanoHTTPOpenRedir:
1157 * @URL: The URL to load
1158 * @contentType: if available the Content-Type information will be
1159 * returned at that location
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001160 * @redir: if available the redirected URL will be returned
Daniel Veillard9403a042001-05-28 11:00:53 +00001161 *
1162 * This function try to open a connection to the indicated resource
1163 * via HTTP GET.
1164 *
1165 * Returns NULL in case of failure, otherwise a request handler.
1166 * The contentType, if provided must be freed by the caller
1167 */
1168
1169void*
1170xmlNanoHTTPOpenRedir(const char *URL, char **contentType, char **redir) {
1171 if (contentType != NULL) *contentType = NULL;
1172 if (redir != NULL) *redir = NULL;
Daniel Veillardf012a642001-07-23 19:10:52 +00001173 return(xmlNanoHTTPMethodRedir(URL, NULL, NULL, contentType, redir, NULL,0));
Owen Taylor3473f882001-02-23 17:55:21 +00001174}
1175
1176/**
1177 * xmlNanoHTTPRead:
1178 * @ctx: the HTTP context
1179 * @dest: a buffer
1180 * @len: the buffer length
1181 *
1182 * This function tries to read @len bytes from the existing HTTP connection
1183 * and saves them in @dest. This is a blocking call.
1184 *
1185 * Returns the number of byte read. 0 is an indication of an end of connection.
1186 * -1 indicates a parameter error.
1187 */
1188int
1189xmlNanoHTTPRead(void *ctx, void *dest, int len) {
1190 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
Daniel Veillard9a2724d2005-12-15 11:12:26 +00001191#ifdef HAVE_ZLIB_H
1192 int bytes_read = 0;
1193 int orig_avail_in;
1194 int z_ret;
1195#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001196
1197 if (ctx == NULL) return(-1);
1198 if (dest == NULL) return(-1);
1199 if (len <= 0) return(0);
1200
Daniel Veillard9a2724d2005-12-15 11:12:26 +00001201#ifdef HAVE_ZLIB_H
1202 if (ctxt->usesGzip == 1) {
1203 if (ctxt->strm == NULL) return(0);
1204
1205 ctxt->strm->next_out = dest;
1206 ctxt->strm->avail_out = len;
1207
1208 do {
1209 orig_avail_in = ctxt->strm->avail_in = ctxt->inptr - ctxt->inrptr - bytes_read;
1210 ctxt->strm->next_in = BAD_CAST (ctxt->inrptr + bytes_read);
1211
1212 z_ret = inflate(ctxt->strm, Z_NO_FLUSH);
1213 bytes_read += orig_avail_in - ctxt->strm->avail_in;
1214
1215 if (z_ret != Z_OK) break;
1216 } while (ctxt->strm->avail_out > 0 && xmlNanoHTTPRecv(ctxt) > 0);
1217
1218 ctxt->inrptr += bytes_read;
1219 return(len - ctxt->strm->avail_out);
1220 }
1221#endif
1222
Owen Taylor3473f882001-02-23 17:55:21 +00001223 while (ctxt->inptr - ctxt->inrptr < len) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001224 if (xmlNanoHTTPRecv(ctxt) <= 0) break;
Owen Taylor3473f882001-02-23 17:55:21 +00001225 }
1226 if (ctxt->inptr - ctxt->inrptr < len)
1227 len = ctxt->inptr - ctxt->inrptr;
1228 memcpy(dest, ctxt->inrptr, len);
1229 ctxt->inrptr += len;
1230 return(len);
1231}
1232
1233/**
1234 * xmlNanoHTTPClose:
1235 * @ctx: the HTTP context
1236 *
1237 * This function closes an HTTP context, it ends up the connection and
1238 * free all data related to it.
1239 */
1240void
1241xmlNanoHTTPClose(void *ctx) {
1242 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1243
1244 if (ctx == NULL) return;
1245
1246 xmlNanoHTTPFreeCtxt(ctxt);
1247}
1248
1249/**
Daniel Veillard9403a042001-05-28 11:00:53 +00001250 * xmlNanoHTTPMethodRedir:
Owen Taylor3473f882001-02-23 17:55:21 +00001251 * @URL: The URL to load
1252 * @method: the HTTP method to use
1253 * @input: the input string if any
1254 * @contentType: the Content-Type information IN and OUT
Daniel Veillard9403a042001-05-28 11:00:53 +00001255 * @redir: the redirected URL OUT
Owen Taylor3473f882001-02-23 17:55:21 +00001256 * @headers: the extra headers
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001257 * @ilen: input length
Owen Taylor3473f882001-02-23 17:55:21 +00001258 *
1259 * This function try to open a connection to the indicated resource
1260 * via HTTP using the given @method, adding the given extra headers
1261 * and the input buffer for the request content.
1262 *
1263 * Returns NULL in case of failure, otherwise a request handler.
Daniel Veillard9403a042001-05-28 11:00:53 +00001264 * The contentType, or redir, if provided must be freed by the caller
Owen Taylor3473f882001-02-23 17:55:21 +00001265 */
1266
1267void*
Daniel Veillard9403a042001-05-28 11:00:53 +00001268xmlNanoHTTPMethodRedir(const char *URL, const char *method, const char *input,
Daniel Veillardf012a642001-07-23 19:10:52 +00001269 char **contentType, char **redir,
1270 const char *headers, int ilen ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001271 xmlNanoHTTPCtxtPtr ctxt;
1272 char *bp, *p;
Daniel Veillardf012a642001-07-23 19:10:52 +00001273 int blen, ret;
Owen Taylor3473f882001-02-23 17:55:21 +00001274 int head;
1275 int nbRedirects = 0;
1276 char *redirURL = NULL;
William M. Brack78637da2003-07-31 14:47:38 +00001277#ifdef DEBUG_HTTP
1278 int xmt_bytes;
1279#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001280
1281 if (URL == NULL) return(NULL);
1282 if (method == NULL) method = "GET";
1283 xmlNanoHTTPInit();
1284
1285retry:
1286 if (redirURL == NULL)
1287 ctxt = xmlNanoHTTPNewCtxt(URL);
1288 else {
1289 ctxt = xmlNanoHTTPNewCtxt(redirURL);
Daniel Veillarda840b692003-10-19 13:35:37 +00001290 ctxt->location = xmlMemStrdup(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001291 }
1292
Daniel Veillardf012a642001-07-23 19:10:52 +00001293 if ( ctxt == NULL ) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001294 return ( NULL );
1295 }
1296
Owen Taylor3473f882001-02-23 17:55:21 +00001297 if ((ctxt->protocol == NULL) || (strcmp(ctxt->protocol, "http"))) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001298 __xmlIOErr(XML_FROM_HTTP, XML_HTTP_URL_SYNTAX, "Not a valid HTTP URI");
Owen Taylor3473f882001-02-23 17:55:21 +00001299 xmlNanoHTTPFreeCtxt(ctxt);
1300 if (redirURL != NULL) xmlFree(redirURL);
1301 return(NULL);
1302 }
1303 if (ctxt->hostname == NULL) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001304 __xmlIOErr(XML_FROM_HTTP, XML_HTTP_UNKNOWN_HOST,
1305 "Failed to identify host in URI");
Owen Taylor3473f882001-02-23 17:55:21 +00001306 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001307 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001308 return(NULL);
1309 }
1310 if (proxy) {
1311 blen = strlen(ctxt->hostname) * 2 + 16;
1312 ret = xmlNanoHTTPConnectHost(proxy, proxyPort);
1313 }
1314 else {
1315 blen = strlen(ctxt->hostname);
1316 ret = xmlNanoHTTPConnectHost(ctxt->hostname, ctxt->port);
1317 }
1318 if (ret < 0) {
1319 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001320 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001321 return(NULL);
1322 }
1323 ctxt->fd = ret;
1324
Daniel Veillardf012a642001-07-23 19:10:52 +00001325 if (input == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00001326 ilen = 0;
Daniel Veillardf012a642001-07-23 19:10:52 +00001327 else
1328 blen += 36;
1329
Owen Taylor3473f882001-02-23 17:55:21 +00001330 if (headers != NULL)
Daniel Veillardf012a642001-07-23 19:10:52 +00001331 blen += strlen(headers) + 2;
Owen Taylor3473f882001-02-23 17:55:21 +00001332 if (contentType && *contentType)
1333 blen += strlen(*contentType) + 16;
Daniel Veillard351f2d62005-04-13 02:55:12 +00001334 if (ctxt->query != NULL)
1335 blen += strlen(ctxt->query) + 1;
Daniel Veillardf012a642001-07-23 19:10:52 +00001336 blen += strlen(method) + strlen(ctxt->path) + 24;
Daniel Veillard9a2724d2005-12-15 11:12:26 +00001337#ifdef HAVE_ZLIB_H
1338 blen += 23;
1339#endif
Daniel Veillard82cb3192003-10-29 13:39:15 +00001340 bp = (char*)xmlMallocAtomic(blen);
Daniel Veillardf012a642001-07-23 19:10:52 +00001341 if ( bp == NULL ) {
1342 xmlNanoHTTPFreeCtxt( ctxt );
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001343 xmlHTTPErrMemory("allocating header buffer");
Daniel Veillardf012a642001-07-23 19:10:52 +00001344 return ( NULL );
1345 }
1346
1347 p = bp;
1348
Owen Taylor3473f882001-02-23 17:55:21 +00001349 if (proxy) {
1350 if (ctxt->port != 80) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001351 p += snprintf( p, blen - (p - bp), "%s http://%s:%d%s",
1352 method, ctxt->hostname,
Daniel Veillardf012a642001-07-23 19:10:52 +00001353 ctxt->port, ctxt->path );
Owen Taylor3473f882001-02-23 17:55:21 +00001354 }
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001355 else
1356 p += snprintf( p, blen - (p - bp), "%s http://%s%s", method,
Daniel Veillardf012a642001-07-23 19:10:52 +00001357 ctxt->hostname, ctxt->path);
Owen Taylor3473f882001-02-23 17:55:21 +00001358 }
1359 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001360 p += snprintf( p, blen - (p - bp), "%s %s", method, ctxt->path);
Daniel Veillardf012a642001-07-23 19:10:52 +00001361
Daniel Veillard351f2d62005-04-13 02:55:12 +00001362 if (ctxt->query != NULL)
1363 p += snprintf( p, blen - (p - bp), "?%s", ctxt->query);
1364
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001365 p += snprintf( p, blen - (p - bp), " HTTP/1.0\r\nHost: %s\r\n",
1366 ctxt->hostname);
Daniel Veillardf012a642001-07-23 19:10:52 +00001367
Daniel Veillard9a2724d2005-12-15 11:12:26 +00001368#ifdef HAVE_ZLIB_H
1369 p += snprintf(p, blen - (p - bp), "Accept-Encoding: gzip\r\n");
1370#endif
1371
Daniel Veillardf012a642001-07-23 19:10:52 +00001372 if (contentType != NULL && *contentType)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001373 p += snprintf(p, blen - (p - bp), "Content-Type: %s\r\n", *contentType);
Daniel Veillardf012a642001-07-23 19:10:52 +00001374
1375 if (headers != NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001376 p += snprintf( p, blen - (p - bp), "%s", headers );
Daniel Veillardf012a642001-07-23 19:10:52 +00001377
Owen Taylor3473f882001-02-23 17:55:21 +00001378 if (input != NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001379 snprintf(p, blen - (p - bp), "Content-Length: %d\r\n\r\n", ilen );
Owen Taylor3473f882001-02-23 17:55:21 +00001380 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001381 snprintf(p, blen - (p - bp), "\r\n");
Daniel Veillardf012a642001-07-23 19:10:52 +00001382
Owen Taylor3473f882001-02-23 17:55:21 +00001383#ifdef DEBUG_HTTP
1384 xmlGenericError(xmlGenericErrorContext,
1385 "-> %s%s", proxy? "(Proxy) " : "", bp);
1386 if ((blen -= strlen(bp)+1) < 0)
1387 xmlGenericError(xmlGenericErrorContext,
1388 "ERROR: overflowed buffer by %d bytes\n", -blen);
1389#endif
1390 ctxt->outptr = ctxt->out = bp;
1391 ctxt->state = XML_NANO_HTTP_WRITE;
Daniel Veillardf012a642001-07-23 19:10:52 +00001392 blen = strlen( ctxt->out );
Daniel Veillardf012a642001-07-23 19:10:52 +00001393#ifdef DEBUG_HTTP
William M. Brack78637da2003-07-31 14:47:38 +00001394 xmt_bytes = xmlNanoHTTPSend(ctxt, ctxt->out, blen );
Daniel Veillardf012a642001-07-23 19:10:52 +00001395 if ( xmt_bytes != blen )
1396 xmlGenericError( xmlGenericErrorContext,
1397 "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
1398 xmt_bytes, blen,
1399 "bytes of HTTP headers sent to host",
1400 ctxt->hostname );
William M. Brack78637da2003-07-31 14:47:38 +00001401#else
1402 xmlNanoHTTPSend(ctxt, ctxt->out, blen );
Daniel Veillardf012a642001-07-23 19:10:52 +00001403#endif
1404
1405 if ( input != NULL ) {
William M. Brack78637da2003-07-31 14:47:38 +00001406#ifdef DEBUG_HTTP
Daniel Veillardf012a642001-07-23 19:10:52 +00001407 xmt_bytes = xmlNanoHTTPSend( ctxt, input, ilen );
1408
Daniel Veillardf012a642001-07-23 19:10:52 +00001409 if ( xmt_bytes != ilen )
1410 xmlGenericError( xmlGenericErrorContext,
1411 "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
1412 xmt_bytes, ilen,
1413 "bytes of HTTP content sent to host",
1414 ctxt->hostname );
William M. Brack78637da2003-07-31 14:47:38 +00001415#else
1416 xmlNanoHTTPSend( ctxt, input, ilen );
Daniel Veillardf012a642001-07-23 19:10:52 +00001417#endif
1418 }
1419
Owen Taylor3473f882001-02-23 17:55:21 +00001420 ctxt->state = XML_NANO_HTTP_READ;
1421 head = 1;
1422
1423 while ((p = xmlNanoHTTPReadLine(ctxt)) != NULL) {
1424 if (head && (*p == 0)) {
1425 head = 0;
1426 ctxt->content = ctxt->inrptr;
1427 xmlFree(p);
1428 break;
1429 }
1430 xmlNanoHTTPScanAnswer(ctxt, p);
1431
1432#ifdef DEBUG_HTTP
1433 xmlGenericError(xmlGenericErrorContext, "<- %s\n", p);
1434#endif
1435 xmlFree(p);
1436 }
1437
1438 if ((ctxt->location != NULL) && (ctxt->returnValue >= 300) &&
1439 (ctxt->returnValue < 400)) {
1440#ifdef DEBUG_HTTP
1441 xmlGenericError(xmlGenericErrorContext,
1442 "\nRedirect to: %s\n", ctxt->location);
1443#endif
Daniel Veillardf012a642001-07-23 19:10:52 +00001444 while ( xmlNanoHTTPRecv(ctxt) > 0 ) ;
Owen Taylor3473f882001-02-23 17:55:21 +00001445 if (nbRedirects < XML_NANO_HTTP_MAX_REDIR) {
1446 nbRedirects++;
Daniel Veillard9403a042001-05-28 11:00:53 +00001447 if (redirURL != NULL)
1448 xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001449 redirURL = xmlMemStrdup(ctxt->location);
1450 xmlNanoHTTPFreeCtxt(ctxt);
1451 goto retry;
1452 }
1453 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001454 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001455#ifdef DEBUG_HTTP
1456 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardf012a642001-07-23 19:10:52 +00001457 "xmlNanoHTTPMethodRedir: Too many redirects, aborting ...\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001458#endif
1459 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001460 }
1461
1462 if (contentType != NULL) {
1463 if (ctxt->contentType != NULL)
1464 *contentType = xmlMemStrdup(ctxt->contentType);
1465 else
1466 *contentType = NULL;
1467 }
1468
Daniel Veillard9403a042001-05-28 11:00:53 +00001469 if ((redir != NULL) && (redirURL != NULL)) {
1470 *redir = redirURL;
1471 } else {
1472 if (redirURL != NULL)
1473 xmlFree(redirURL);
1474 if (redir != NULL)
1475 *redir = NULL;
1476 }
1477
Owen Taylor3473f882001-02-23 17:55:21 +00001478#ifdef DEBUG_HTTP
1479 if (ctxt->contentType != NULL)
1480 xmlGenericError(xmlGenericErrorContext,
1481 "\nCode %d, content-type '%s'\n\n",
1482 ctxt->returnValue, ctxt->contentType);
1483 else
1484 xmlGenericError(xmlGenericErrorContext,
1485 "\nCode %d, no content-type\n\n",
1486 ctxt->returnValue);
1487#endif
1488
1489 return((void *) ctxt);
1490}
1491
1492/**
Daniel Veillard9403a042001-05-28 11:00:53 +00001493 * xmlNanoHTTPMethod:
1494 * @URL: The URL to load
1495 * @method: the HTTP method to use
1496 * @input: the input string if any
1497 * @contentType: the Content-Type information IN and OUT
1498 * @headers: the extra headers
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001499 * @ilen: input length
Daniel Veillard9403a042001-05-28 11:00:53 +00001500 *
1501 * This function try to open a connection to the indicated resource
1502 * via HTTP using the given @method, adding the given extra headers
1503 * and the input buffer for the request content.
1504 *
1505 * Returns NULL in case of failure, otherwise a request handler.
1506 * The contentType, if provided must be freed by the caller
1507 */
1508
1509void*
1510xmlNanoHTTPMethod(const char *URL, const char *method, const char *input,
Daniel Veillardf012a642001-07-23 19:10:52 +00001511 char **contentType, const char *headers, int ilen) {
Daniel Veillard9403a042001-05-28 11:00:53 +00001512 return(xmlNanoHTTPMethodRedir(URL, method, input, contentType,
Daniel Veillardf012a642001-07-23 19:10:52 +00001513 NULL, headers, ilen));
Daniel Veillard9403a042001-05-28 11:00:53 +00001514}
1515
1516/**
Owen Taylor3473f882001-02-23 17:55:21 +00001517 * xmlNanoHTTPFetch:
1518 * @URL: The URL to load
1519 * @filename: the filename where the content should be saved
1520 * @contentType: if available the Content-Type information will be
1521 * returned at that location
1522 *
1523 * This function try to fetch the indicated resource via HTTP GET
1524 * and save it's content in the file.
1525 *
1526 * Returns -1 in case of failure, 0 incase of success. The contentType,
1527 * if provided must be freed by the caller
1528 */
1529int
1530xmlNanoHTTPFetch(const char *URL, const char *filename, char **contentType) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001531 void *ctxt = NULL;
1532 char *buf = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001533 int fd;
1534 int len;
1535
William M. Brack015ccb22005-02-13 08:18:52 +00001536 if (filename == NULL) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001537 ctxt = xmlNanoHTTPOpen(URL, contentType);
1538 if (ctxt == NULL) return(-1);
1539
1540 if (!strcmp(filename, "-"))
1541 fd = 0;
1542 else {
1543 fd = open(filename, O_CREAT | O_WRONLY, 00644);
1544 if (fd < 0) {
1545 xmlNanoHTTPClose(ctxt);
1546 if ((contentType != NULL) && (*contentType != NULL)) {
1547 xmlFree(*contentType);
1548 *contentType = NULL;
1549 }
1550 return(-1);
1551 }
1552 }
1553
Daniel Veillardf012a642001-07-23 19:10:52 +00001554 xmlNanoHTTPFetchContent( ctxt, &buf, &len );
1555 if ( len > 0 ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001556 write(fd, buf, len);
1557 }
1558
1559 xmlNanoHTTPClose(ctxt);
1560 close(fd);
1561 return(0);
1562}
1563
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001564#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001565/**
1566 * xmlNanoHTTPSave:
1567 * @ctxt: the HTTP context
1568 * @filename: the filename where the content should be saved
1569 *
1570 * This function saves the output of the HTTP transaction to a file
1571 * It closes and free the context at the end
1572 *
1573 * Returns -1 in case of failure, 0 incase of success.
1574 */
1575int
1576xmlNanoHTTPSave(void *ctxt, const char *filename) {
Daniel Veillarde3924972001-07-25 20:25:21 +00001577 char *buf = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001578 int fd;
1579 int len;
1580
William M. Brack015ccb22005-02-13 08:18:52 +00001581 if ((ctxt == NULL) || (filename == NULL)) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001582
1583 if (!strcmp(filename, "-"))
1584 fd = 0;
1585 else {
1586 fd = open(filename, O_CREAT | O_WRONLY);
1587 if (fd < 0) {
1588 xmlNanoHTTPClose(ctxt);
1589 return(-1);
1590 }
1591 }
1592
Daniel Veillardf012a642001-07-23 19:10:52 +00001593 xmlNanoHTTPFetchContent( ctxt, &buf, &len );
1594 if ( len > 0 ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001595 write(fd, buf, len);
1596 }
1597
1598 xmlNanoHTTPClose(ctxt);
William M. Brack20d82362004-03-17 08:44:46 +00001599 close(fd);
Owen Taylor3473f882001-02-23 17:55:21 +00001600 return(0);
1601}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001602#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001603
1604/**
1605 * xmlNanoHTTPReturnCode:
1606 * @ctx: the HTTP context
1607 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001608 * Get the latest HTTP return code received
1609 *
Owen Taylor3473f882001-02-23 17:55:21 +00001610 * Returns the HTTP return code for the request.
1611 */
1612int
1613xmlNanoHTTPReturnCode(void *ctx) {
1614 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1615
1616 if (ctxt == NULL) return(-1);
1617
1618 return(ctxt->returnValue);
1619}
1620
1621/**
1622 * xmlNanoHTTPAuthHeader:
1623 * @ctx: the HTTP context
1624 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001625 * Get the authentication header of an HTTP context
1626 *
Owen Taylor3473f882001-02-23 17:55:21 +00001627 * Returns the stashed value of the WWW-Authenticate or Proxy-Authenticate
1628 * header.
1629 */
1630const char *
1631xmlNanoHTTPAuthHeader(void *ctx) {
1632 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1633
1634 if (ctxt == NULL) return(NULL);
1635
1636 return(ctxt->authHeader);
1637}
1638
Daniel Veillardf012a642001-07-23 19:10:52 +00001639/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00001640 * xmlNanoHTTPContentLength:
Daniel Veillardf012a642001-07-23 19:10:52 +00001641 * @ctx: the HTTP context
1642 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001643 * Provides the specified content length from the HTTP header.
1644 *
Daniel Veillardf012a642001-07-23 19:10:52 +00001645 * Return the specified content length from the HTTP header. Note that
1646 * a value of -1 indicates that the content length element was not included in
1647 * the response header.
1648 */
1649int
1650xmlNanoHTTPContentLength( void * ctx ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001651 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillardf012a642001-07-23 19:10:52 +00001652
1653 return ( ( ctxt == NULL ) ? -1 : ctxt->ContentLength );
1654}
1655
1656/**
Daniel Veillard847332a2003-10-18 11:29:40 +00001657 * xmlNanoHTTPRedir:
1658 * @ctx: the HTTP context
1659 *
1660 * Provides the specified redirection URL if available from the HTTP header.
1661 *
1662 * Return the specified redirection URL or NULL if not redirected.
1663 */
1664const char *
1665xmlNanoHTTPRedir( void * ctx ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001666 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillard847332a2003-10-18 11:29:40 +00001667
1668 return ( ( ctxt == NULL ) ? NULL : ctxt->location );
1669}
1670
1671/**
1672 * xmlNanoHTTPEncoding:
1673 * @ctx: the HTTP context
1674 *
1675 * Provides the specified encoding if specified in the HTTP headers.
1676 *
1677 * Return the specified encoding or NULL if not available
1678 */
1679const char *
1680xmlNanoHTTPEncoding( void * ctx ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001681 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillard847332a2003-10-18 11:29:40 +00001682
1683 return ( ( ctxt == NULL ) ? NULL : ctxt->encoding );
1684}
1685
1686/**
Daniel Veillarda840b692003-10-19 13:35:37 +00001687 * xmlNanoHTTPMimeType:
1688 * @ctx: the HTTP context
1689 *
1690 * Provides the specified Mime-Type if specified in the HTTP headers.
1691 *
1692 * Return the specified Mime-Type or NULL if not available
1693 */
1694const char *
1695xmlNanoHTTPMimeType( void * ctx ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001696 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillarda840b692003-10-19 13:35:37 +00001697
1698 return ( ( ctxt == NULL ) ? NULL : ctxt->mimeType );
1699}
1700
1701/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00001702 * xmlNanoHTTPFetchContent:
Daniel Veillardf012a642001-07-23 19:10:52 +00001703 * @ctx: the HTTP context
1704 * @ptr: pointer to set to the content buffer.
1705 * @len: integer pointer to hold the length of the content
1706 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001707 * Check if all the content was read
1708 *
Daniel Veillardf012a642001-07-23 19:10:52 +00001709 * Returns 0 if all the content was read and available, returns
1710 * -1 if received content length was less than specified or an error
1711 * occurred.
1712 */
Daniel Veillarda2351322004-06-27 12:08:10 +00001713static int
Daniel Veillardf012a642001-07-23 19:10:52 +00001714xmlNanoHTTPFetchContent( void * ctx, char ** ptr, int * len ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001715 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillardf012a642001-07-23 19:10:52 +00001716
1717 int rc = 0;
1718 int cur_lgth;
1719 int rcvd_lgth;
1720 int dummy_int;
1721 char * dummy_ptr = NULL;
1722
1723 /* Dummy up return input parameters if not provided */
1724
1725 if ( len == NULL )
1726 len = &dummy_int;
1727
1728 if ( ptr == NULL )
1729 ptr = &dummy_ptr;
1730
1731 /* But can't work without the context pointer */
1732
1733 if ( ( ctxt == NULL ) || ( ctxt->content == NULL ) ) {
1734 *len = 0;
1735 *ptr = NULL;
1736 return ( -1 );
1737 }
1738
1739 rcvd_lgth = ctxt->inptr - ctxt->content;
1740
1741 while ( (cur_lgth = xmlNanoHTTPRecv( ctxt )) > 0 ) {
1742
1743 rcvd_lgth += cur_lgth;
1744 if ( (ctxt->ContentLength > 0) && (rcvd_lgth >= ctxt->ContentLength) )
1745 break;
1746 }
1747
1748 *ptr = ctxt->content;
1749 *len = rcvd_lgth;
1750
1751 if ( ( ctxt->ContentLength > 0 ) && ( rcvd_lgth < ctxt->ContentLength ) )
1752 rc = -1;
1753 else if ( rcvd_lgth == 0 )
1754 rc = -1;
1755
1756 return ( rc );
1757}
1758
Owen Taylor3473f882001-02-23 17:55:21 +00001759#ifdef STANDALONE
1760int main(int argc, char **argv) {
1761 char *contentType = NULL;
1762
1763 if (argv[1] != NULL) {
1764 if (argv[2] != NULL)
1765 xmlNanoHTTPFetch(argv[1], argv[2], &contentType);
1766 else
1767 xmlNanoHTTPFetch(argv[1], "-", &contentType);
1768 if (contentType != NULL) xmlFree(contentType);
1769 } else {
1770 xmlGenericError(xmlGenericErrorContext,
1771 "%s: minimal HTTP GET implementation\n", argv[0]);
1772 xmlGenericError(xmlGenericErrorContext,
1773 "\tusage %s [ URL [ filename ] ]\n", argv[0]);
1774 }
1775 xmlNanoHTTPCleanup();
1776 xmlMemoryDump();
1777 return(0);
1778}
1779#endif /* STANDALONE */
1780#else /* !LIBXML_HTTP_ENABLED */
1781#ifdef STANDALONE
1782#include <stdio.h>
1783int main(int argc, char **argv) {
1784 xmlGenericError(xmlGenericErrorContext,
1785 "%s : HTTP support not compiled in\n", argv[0]);
1786 return(0);
1787}
1788#endif /* STANDALONE */
1789#endif /* LIBXML_HTTP_ENABLED */
Daniel Veillard5d4644e2005-04-01 13:11:58 +00001790#define bottom_nanohttp
1791#include "elfgcchack.h"