blob: 7a1941e2902bb2ffc7da5b8bf25ec9c559703cbc [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>
Daniel Veillardc284c642005-03-31 10:24:24 +000072#define XML_SOCKLEN_T unsigned int
Owen Taylor3473f882001-02-23 17:55:21 +000073#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>
Daniel Veillardc284c642005-03-31 10:24:24 +000081#undef XML_SOCKLEN_T
82#define XML_SOCKLEN_T unsigned int
Daniel Veillard1638a472003-08-14 01:23:25 +000083#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 Veillardc284c642005-03-31 10:24:24 +0000110#ifndef XML_SOCKLEN_T
111#define XML_SOCKLEN_T unsigned int
Daniel Veillard75be0132002-03-13 10:03:35 +0000112#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 */
Daniel Veillard351f2d62005-04-13 02:55:12 +0000137 char *query; /* the query string */
Owen Taylor3473f882001-02-23 17:55:21 +0000138 SOCKET fd; /* the file descriptor for the socket */
139 int state; /* WRITE / READ / CLOSED */
140 char *out; /* buffer sent (zero terminated) */
141 char *outptr; /* index within the buffer sent */
142 char *in; /* the receiving buffer */
143 char *content; /* the start of the content */
144 char *inptr; /* the next byte to read from network */
145 char *inrptr; /* the next byte to give back to the client */
146 int inlen; /* len of the input buffer */
147 int last; /* return code for last operation */
148 int returnValue; /* the protocol return value */
Daniel Veillardf012a642001-07-23 19:10:52 +0000149 int ContentLength; /* specified content length from HTTP header */
Owen Taylor3473f882001-02-23 17:55:21 +0000150 char *contentType; /* the MIME type for the input */
151 char *location; /* the new URL in case of redirect */
152 char *authHeader; /* contents of {WWW,Proxy}-Authenticate header */
Daniel Veillard847332a2003-10-18 11:29:40 +0000153 char *encoding; /* encoding extracted from the contentType */
Daniel Veillarda840b692003-10-19 13:35:37 +0000154 char *mimeType; /* Mime-Type extracted from the contentType */
Owen Taylor3473f882001-02-23 17:55:21 +0000155} xmlNanoHTTPCtxt, *xmlNanoHTTPCtxtPtr;
156
157static int initialized = 0;
158static char *proxy = NULL; /* the proxy name if any */
159static int proxyPort; /* the proxy port if any */
160static unsigned int timeout = 60;/* the select() timeout in seconds */
161
Daniel Veillarda2351322004-06-27 12:08:10 +0000162static int xmlNanoHTTPFetchContent( void * ctx, char ** ptr, int * len );
Daniel Veillardf012a642001-07-23 19:10:52 +0000163
Owen Taylor3473f882001-02-23 17:55:21 +0000164/**
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000165 * xmlHTTPErrMemory:
166 * @extra: extra informations
167 *
168 * Handle an out of memory condition
169 */
170static void
171xmlHTTPErrMemory(const char *extra)
172{
173 __xmlSimpleError(XML_FROM_HTTP, XML_ERR_NO_MEMORY, NULL, NULL, extra);
174}
175
176/**
Owen Taylor3473f882001-02-23 17:55:21 +0000177 * A portability function
178 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000179static int socket_errno(void) {
Owen Taylor3473f882001-02-23 17:55:21 +0000180#ifdef _WINSOCKAPI_
181 return(WSAGetLastError());
182#else
183 return(errno);
184#endif
185}
186
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000187#ifdef SUPPORT_IP6
Daniel Veillard2db8c122003-07-08 12:16:59 +0000188static
189int have_ipv6(void) {
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000190 int s;
191
192 s = socket (AF_INET6, SOCK_STREAM, 0);
193 if (s != -1) {
194 close (s);
195 return (1);
196 }
197 return (0);
198}
199#endif
200
Owen Taylor3473f882001-02-23 17:55:21 +0000201/**
202 * xmlNanoHTTPInit:
203 *
204 * Initialize the HTTP protocol layer.
205 * Currently it just checks for proxy informations
206 */
207
208void
209xmlNanoHTTPInit(void) {
210 const char *env;
211#ifdef _WINSOCKAPI_
212 WSADATA wsaData;
213#endif
214
215 if (initialized)
216 return;
217
218#ifdef _WINSOCKAPI_
219 if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0)
220 return;
221#endif
222
223 if (proxy == NULL) {
224 proxyPort = 80;
225 env = getenv("no_proxy");
Daniel Veillard29b17482004-08-16 00:39:03 +0000226 if (env && ((env[0] == '*') && (env[1] == 0)))
Owen Taylor3473f882001-02-23 17:55:21 +0000227 goto done;
228 env = getenv("http_proxy");
229 if (env != NULL) {
230 xmlNanoHTTPScanProxy(env);
231 goto done;
232 }
233 env = getenv("HTTP_PROXY");
234 if (env != NULL) {
235 xmlNanoHTTPScanProxy(env);
236 goto done;
237 }
238 }
239done:
240 initialized = 1;
241}
242
243/**
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000244 * xmlNanoHTTPCleanup:
Owen Taylor3473f882001-02-23 17:55:21 +0000245 *
246 * Cleanup the HTTP protocol layer.
247 */
248
249void
250xmlNanoHTTPCleanup(void) {
Daniel Veillard744acff2005-07-12 15:09:53 +0000251 if (proxy != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +0000252 xmlFree(proxy);
Daniel Veillard744acff2005-07-12 15:09:53 +0000253 proxy = NULL;
254 }
Owen Taylor3473f882001-02-23 17:55:21 +0000255#ifdef _WINSOCKAPI_
256 if (initialized)
257 WSACleanup();
258#endif
259 initialized = 0;
260 return;
261}
262
263/**
Owen Taylor3473f882001-02-23 17:55:21 +0000264 * xmlNanoHTTPScanURL:
265 * @ctxt: an HTTP context
266 * @URL: The URL used to initialize the context
267 *
268 * (Re)Initialize an HTTP context by parsing the URL and finding
269 * the protocol host port and path it indicates.
270 */
271
272static void
273xmlNanoHTTPScanURL(xmlNanoHTTPCtxtPtr ctxt, const char *URL) {
William M. Brack015ccb22005-02-13 08:18:52 +0000274 xmlURIPtr uri;
275 /*
276 * Clear any existing data from the context
277 */
Owen Taylor3473f882001-02-23 17:55:21 +0000278 if (ctxt->protocol != NULL) {
279 xmlFree(ctxt->protocol);
280 ctxt->protocol = NULL;
281 }
282 if (ctxt->hostname != NULL) {
283 xmlFree(ctxt->hostname);
284 ctxt->hostname = NULL;
285 }
286 if (ctxt->path != NULL) {
287 xmlFree(ctxt->path);
288 ctxt->path = NULL;
289 }
Daniel Veillard351f2d62005-04-13 02:55:12 +0000290 if (ctxt->query != NULL) {
291 xmlFree(ctxt->query);
292 ctxt->query = NULL;
293 }
Owen Taylor3473f882001-02-23 17:55:21 +0000294 if (URL == NULL) return;
William M. Brack015ccb22005-02-13 08:18:52 +0000295
Daniel Veillard336a8e12005-08-07 10:46:19 +0000296 uri = xmlParseURIRaw(URL, 1);
William M. Brack015ccb22005-02-13 08:18:52 +0000297 if (uri == NULL)
298 return;
299
300 if ((uri->scheme == NULL) || (uri->server == NULL)) {
301 xmlFreeURI(uri);
302 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000303 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000304
William M. Brack015ccb22005-02-13 08:18:52 +0000305 ctxt->protocol = xmlMemStrdup(uri->scheme);
306 ctxt->hostname = xmlMemStrdup(uri->server);
307 if (uri->path != NULL)
308 ctxt->path = xmlMemStrdup(uri->path);
309 else
310 ctxt->path = xmlMemStrdup("/");
Daniel Veillard351f2d62005-04-13 02:55:12 +0000311 if (uri->query != NULL)
312 ctxt->query = xmlMemStrdup(uri->query);
William M. Brack015ccb22005-02-13 08:18:52 +0000313 if (uri->port != 0)
314 ctxt->port = uri->port;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000315
William M. Brack015ccb22005-02-13 08:18:52 +0000316 xmlFreeURI(uri);
Owen Taylor3473f882001-02-23 17:55:21 +0000317}
318
319/**
320 * xmlNanoHTTPScanProxy:
321 * @URL: The proxy URL used to initialize the proxy context
322 *
323 * (Re)Initialize the HTTP Proxy context by parsing the URL and finding
324 * the protocol host port it indicates.
325 * Should be like http://myproxy/ or http://myproxy:3128/
326 * A NULL URL cleans up proxy informations.
327 */
328
329void
330xmlNanoHTTPScanProxy(const char *URL) {
William M. Brack015ccb22005-02-13 08:18:52 +0000331 xmlURIPtr uri;
Owen Taylor3473f882001-02-23 17:55:21 +0000332
333 if (proxy != NULL) {
334 xmlFree(proxy);
335 proxy = NULL;
336 }
William M. Brack015ccb22005-02-13 08:18:52 +0000337 proxyPort = 0;
338
Owen Taylor3473f882001-02-23 17:55:21 +0000339#ifdef DEBUG_HTTP
340 if (URL == NULL)
341 xmlGenericError(xmlGenericErrorContext,
342 "Removing HTTP proxy info\n");
343 else
344 xmlGenericError(xmlGenericErrorContext,
345 "Using HTTP proxy %s\n", URL);
346#endif
347 if (URL == NULL) return;
William M. Brack015ccb22005-02-13 08:18:52 +0000348
Daniel Veillard336a8e12005-08-07 10:46:19 +0000349 uri = xmlParseURIRaw(URL, 1);
William M. Brack015ccb22005-02-13 08:18:52 +0000350 if ((uri == NULL) || (uri->scheme == NULL) ||
351 (strcmp(uri->scheme, "http")) || (uri->server == NULL)) {
352 __xmlIOErr(XML_FROM_HTTP, XML_HTTP_URL_SYNTAX, "Syntax Error\n");
353 if (uri != NULL)
354 xmlFreeURI(uri);
355 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000356 }
William M. Brack015ccb22005-02-13 08:18:52 +0000357
358 proxy = xmlMemStrdup(uri->server);
359 if (uri->port != 0)
360 proxyPort = uri->port;
Owen Taylor3473f882001-02-23 17:55:21 +0000361
William M. Brack015ccb22005-02-13 08:18:52 +0000362 xmlFreeURI(uri);
Owen Taylor3473f882001-02-23 17:55:21 +0000363}
364
365/**
366 * xmlNanoHTTPNewCtxt:
367 * @URL: The URL used to initialize the context
368 *
369 * Allocate and initialize a new HTTP context.
370 *
371 * Returns an HTTP context or NULL in case of error.
372 */
373
374static xmlNanoHTTPCtxtPtr
375xmlNanoHTTPNewCtxt(const char *URL) {
376 xmlNanoHTTPCtxtPtr ret;
377
378 ret = (xmlNanoHTTPCtxtPtr) xmlMalloc(sizeof(xmlNanoHTTPCtxt));
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000379 if (ret == NULL) {
380 xmlHTTPErrMemory("allocating context");
381 return(NULL);
382 }
Owen Taylor3473f882001-02-23 17:55:21 +0000383
384 memset(ret, 0, sizeof(xmlNanoHTTPCtxt));
385 ret->port = 80;
386 ret->returnValue = 0;
387 ret->fd = -1;
Daniel Veillardf012a642001-07-23 19:10:52 +0000388 ret->ContentLength = -1;
Owen Taylor3473f882001-02-23 17:55:21 +0000389
Daniel Veillardcacbe5d2003-01-10 16:09:51 +0000390 xmlNanoHTTPScanURL(ret, URL);
Owen Taylor3473f882001-02-23 17:55:21 +0000391
392 return(ret);
393}
394
395/**
396 * xmlNanoHTTPFreeCtxt:
397 * @ctxt: an HTTP context
398 *
399 * Frees the context after closing the connection.
400 */
401
402static void
403xmlNanoHTTPFreeCtxt(xmlNanoHTTPCtxtPtr ctxt) {
404 if (ctxt == NULL) return;
405 if (ctxt->hostname != NULL) xmlFree(ctxt->hostname);
406 if (ctxt->protocol != NULL) xmlFree(ctxt->protocol);
407 if (ctxt->path != NULL) xmlFree(ctxt->path);
Daniel Veillard351f2d62005-04-13 02:55:12 +0000408 if (ctxt->query != NULL) xmlFree(ctxt->query);
Owen Taylor3473f882001-02-23 17:55:21 +0000409 if (ctxt->out != NULL) xmlFree(ctxt->out);
410 if (ctxt->in != NULL) xmlFree(ctxt->in);
411 if (ctxt->contentType != NULL) xmlFree(ctxt->contentType);
Daniel Veillard847332a2003-10-18 11:29:40 +0000412 if (ctxt->encoding != NULL) xmlFree(ctxt->encoding);
Daniel Veillarda840b692003-10-19 13:35:37 +0000413 if (ctxt->mimeType != NULL) xmlFree(ctxt->mimeType);
Owen Taylor3473f882001-02-23 17:55:21 +0000414 if (ctxt->location != NULL) xmlFree(ctxt->location);
415 if (ctxt->authHeader != NULL) xmlFree(ctxt->authHeader);
416 ctxt->state = XML_NANO_HTTP_NONE;
417 if (ctxt->fd >= 0) closesocket(ctxt->fd);
418 ctxt->fd = -1;
419 xmlFree(ctxt);
420}
421
422/**
423 * xmlNanoHTTPSend:
424 * @ctxt: an HTTP context
425 *
426 * Send the input needed to initiate the processing on the server side
Daniel Veillardf012a642001-07-23 19:10:52 +0000427 * Returns number of bytes sent or -1 on error.
Owen Taylor3473f882001-02-23 17:55:21 +0000428 */
429
Daniel Veillardf012a642001-07-23 19:10:52 +0000430static int
431xmlNanoHTTPSend(xmlNanoHTTPCtxtPtr ctxt, const char * xmt_ptr, int outlen) {
432
433 int total_sent = 0;
434
435 if ( (ctxt->state & XML_NANO_HTTP_WRITE) && (xmt_ptr != NULL ) ) {
436 while (total_sent < outlen) {
437 int nsent = send(ctxt->fd, xmt_ptr + total_sent,
438 outlen - total_sent, 0);
Owen Taylor3473f882001-02-23 17:55:21 +0000439 if (nsent>0)
440 total_sent += nsent;
Daniel Veillardf012a642001-07-23 19:10:52 +0000441 else if ( ( nsent == -1 ) &&
Daniel Veillardba6db032001-07-31 16:25:45 +0000442#if defined(EAGAIN) && EAGAIN != EWOULDBLOCK
Daniel Veillardf012a642001-07-23 19:10:52 +0000443 ( socket_errno( ) != EAGAIN ) &&
Daniel Veillardba6db032001-07-31 16:25:45 +0000444#endif
Daniel Veillardf012a642001-07-23 19:10:52 +0000445 ( socket_errno( ) != EWOULDBLOCK ) ) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000446 __xmlIOErr(XML_FROM_HTTP, 0, "send failed\n");
Daniel Veillardf012a642001-07-23 19:10:52 +0000447 if ( total_sent == 0 )
448 total_sent = -1;
449 break;
450 }
451 else {
452 /*
453 ** No data sent
454 ** Since non-blocking sockets are used, wait for
455 ** socket to be writable or default timeout prior
456 ** to retrying.
457 */
458
459 struct timeval tv;
460 fd_set wfd;
461
462 tv.tv_sec = timeout;
463 tv.tv_usec = 0;
464 FD_ZERO( &wfd );
Daniel Veillard9e2110b2005-08-08 20:33:54 +0000465#ifdef _MSC_VER
466#pragma warning(push)
467#pragma warning(disable: 4018)
468#endif
Daniel Veillardf012a642001-07-23 19:10:52 +0000469 FD_SET( ctxt->fd, &wfd );
Daniel Veillard9e2110b2005-08-08 20:33:54 +0000470#ifdef _MSC_VER
471#pragma warning(pop)
472#endif
Daniel Veillardf012a642001-07-23 19:10:52 +0000473 (void)select( ctxt->fd + 1, NULL, &wfd, NULL, &tv );
474 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000475 }
Owen Taylor3473f882001-02-23 17:55:21 +0000476 }
Daniel Veillardf012a642001-07-23 19:10:52 +0000477
478 return total_sent;
Owen Taylor3473f882001-02-23 17:55:21 +0000479}
480
481/**
482 * xmlNanoHTTPRecv:
483 * @ctxt: an HTTP context
484 *
485 * Read information coming from the HTTP connection.
486 * This is a blocking call (but it blocks in select(), not read()).
487 *
488 * Returns the number of byte read or -1 in case of error.
489 */
490
491static int
492xmlNanoHTTPRecv(xmlNanoHTTPCtxtPtr ctxt) {
493 fd_set rfd;
494 struct timeval tv;
495
496
497 while (ctxt->state & XML_NANO_HTTP_READ) {
498 if (ctxt->in == NULL) {
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000499 ctxt->in = (char *) xmlMallocAtomic(65000 * sizeof(char));
Owen Taylor3473f882001-02-23 17:55:21 +0000500 if (ctxt->in == NULL) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000501 xmlHTTPErrMemory("allocating input");
Owen Taylor3473f882001-02-23 17:55:21 +0000502 ctxt->last = -1;
503 return(-1);
504 }
505 ctxt->inlen = 65000;
506 ctxt->inptr = ctxt->content = ctxt->inrptr = ctxt->in;
507 }
508 if (ctxt->inrptr > ctxt->in + XML_NANO_HTTP_CHUNK) {
509 int delta = ctxt->inrptr - ctxt->in;
510 int len = ctxt->inptr - ctxt->inrptr;
511
512 memmove(ctxt->in, ctxt->inrptr, len);
513 ctxt->inrptr -= delta;
514 ctxt->content -= delta;
515 ctxt->inptr -= delta;
516 }
517 if ((ctxt->in + ctxt->inlen) < (ctxt->inptr + XML_NANO_HTTP_CHUNK)) {
518 int d_inptr = ctxt->inptr - ctxt->in;
519 int d_content = ctxt->content - ctxt->in;
520 int d_inrptr = ctxt->inrptr - ctxt->in;
Daniel Veillardf012a642001-07-23 19:10:52 +0000521 char * tmp_ptr = ctxt->in;
Owen Taylor3473f882001-02-23 17:55:21 +0000522
523 ctxt->inlen *= 2;
Daniel Veillardf012a642001-07-23 19:10:52 +0000524 ctxt->in = (char *) xmlRealloc(tmp_ptr, ctxt->inlen);
Owen Taylor3473f882001-02-23 17:55:21 +0000525 if (ctxt->in == NULL) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000526 xmlHTTPErrMemory("allocating input buffer");
Daniel Veillardf012a642001-07-23 19:10:52 +0000527 xmlFree( tmp_ptr );
Owen Taylor3473f882001-02-23 17:55:21 +0000528 ctxt->last = -1;
529 return(-1);
530 }
531 ctxt->inptr = ctxt->in + d_inptr;
532 ctxt->content = ctxt->in + d_content;
533 ctxt->inrptr = ctxt->in + d_inrptr;
534 }
535 ctxt->last = recv(ctxt->fd, ctxt->inptr, XML_NANO_HTTP_CHUNK, 0);
536 if (ctxt->last > 0) {
537 ctxt->inptr += ctxt->last;
538 return(ctxt->last);
539 }
540 if (ctxt->last == 0) {
541 return(0);
542 }
543 if (ctxt->last == -1) {
544 switch (socket_errno()) {
545 case EINPROGRESS:
546 case EWOULDBLOCK:
547#if defined(EAGAIN) && EAGAIN != EWOULDBLOCK
548 case EAGAIN:
549#endif
550 break;
Daniel Veillardf012a642001-07-23 19:10:52 +0000551
552 case ECONNRESET:
553 case ESHUTDOWN:
554 return ( 0 );
555
Owen Taylor3473f882001-02-23 17:55:21 +0000556 default:
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000557 __xmlIOErr(XML_FROM_HTTP, 0, "recv failed\n");
Daniel Veillardf012a642001-07-23 19:10:52 +0000558 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +0000559 }
560 }
561
562 tv.tv_sec = timeout;
563 tv.tv_usec = 0;
564 FD_ZERO(&rfd);
Daniel Veillard9e2110b2005-08-08 20:33:54 +0000565#ifdef _MSC_VER
566#pragma warning(push)
567#pragma warning(disable: 4018)
568#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000569 FD_SET(ctxt->fd, &rfd);
Daniel Veillard9e2110b2005-08-08 20:33:54 +0000570#ifdef _MSC_VER
571#pragma warning(pop)
572#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000573
Daniel Veillard50f34372001-08-03 12:06:36 +0000574 if ( (select(ctxt->fd+1, &rfd, NULL, NULL, &tv)<1)
575#if defined(EINTR)
576 && (errno != EINTR)
577#endif
578 )
Owen Taylor3473f882001-02-23 17:55:21 +0000579 return(0);
580 }
581 return(0);
582}
583
584/**
585 * xmlNanoHTTPReadLine:
586 * @ctxt: an HTTP context
587 *
588 * Read one line in the HTTP server output, usually for extracting
589 * the HTTP protocol informations from the answer header.
590 *
591 * Returns a newly allocated string with a copy of the line, or NULL
592 * which indicate the end of the input.
593 */
594
595static char *
596xmlNanoHTTPReadLine(xmlNanoHTTPCtxtPtr ctxt) {
597 char buf[4096];
598 char *bp = buf;
Daniel Veillardf012a642001-07-23 19:10:52 +0000599 int rc;
Owen Taylor3473f882001-02-23 17:55:21 +0000600
601 while (bp - buf < 4095) {
602 if (ctxt->inrptr == ctxt->inptr) {
Daniel Veillardf012a642001-07-23 19:10:52 +0000603 if ( (rc = xmlNanoHTTPRecv(ctxt)) == 0) {
Owen Taylor3473f882001-02-23 17:55:21 +0000604 if (bp == buf)
605 return(NULL);
606 else
607 *bp = 0;
608 return(xmlMemStrdup(buf));
609 }
Daniel Veillardf012a642001-07-23 19:10:52 +0000610 else if ( rc == -1 ) {
611 return ( NULL );
612 }
Owen Taylor3473f882001-02-23 17:55:21 +0000613 }
614 *bp = *ctxt->inrptr++;
615 if (*bp == '\n') {
616 *bp = 0;
617 return(xmlMemStrdup(buf));
618 }
619 if (*bp != '\r')
620 bp++;
621 }
622 buf[4095] = 0;
623 return(xmlMemStrdup(buf));
624}
625
626
627/**
628 * xmlNanoHTTPScanAnswer:
629 * @ctxt: an HTTP context
630 * @line: an HTTP header line
631 *
632 * Try to extract useful informations from the server answer.
633 * We currently parse and process:
634 * - The HTTP revision/ return code
Daniel Veillarda840b692003-10-19 13:35:37 +0000635 * - The Content-Type, Mime-Type and charset used
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000636 * - The Location for redirect processing.
Owen Taylor3473f882001-02-23 17:55:21 +0000637 *
638 * Returns -1 in case of failure, the file descriptor number otherwise
639 */
640
641static void
642xmlNanoHTTPScanAnswer(xmlNanoHTTPCtxtPtr ctxt, const char *line) {
643 const char *cur = line;
644
645 if (line == NULL) return;
646
647 if (!strncmp(line, "HTTP/", 5)) {
648 int version = 0;
649 int ret = 0;
650
651 cur += 5;
652 while ((*cur >= '0') && (*cur <= '9')) {
653 version *= 10;
654 version += *cur - '0';
655 cur++;
656 }
657 if (*cur == '.') {
658 cur++;
659 if ((*cur >= '0') && (*cur <= '9')) {
660 version *= 10;
661 version += *cur - '0';
662 cur++;
663 }
664 while ((*cur >= '0') && (*cur <= '9'))
665 cur++;
666 } else
667 version *= 10;
668 if ((*cur != ' ') && (*cur != '\t')) return;
669 while ((*cur == ' ') || (*cur == '\t')) cur++;
670 if ((*cur < '0') || (*cur > '9')) return;
671 while ((*cur >= '0') && (*cur <= '9')) {
672 ret *= 10;
673 ret += *cur - '0';
674 cur++;
675 }
676 if ((*cur != 0) && (*cur != ' ') && (*cur != '\t')) return;
677 ctxt->returnValue = ret;
678 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Content-Type:", 13)) {
Daniel Veillarda840b692003-10-19 13:35:37 +0000679 const xmlChar *charset, *last, *mime;
Owen Taylor3473f882001-02-23 17:55:21 +0000680 cur += 13;
681 while ((*cur == ' ') || (*cur == '\t')) cur++;
682 if (ctxt->contentType != NULL)
683 xmlFree(ctxt->contentType);
684 ctxt->contentType = xmlMemStrdup(cur);
Daniel Veillarda840b692003-10-19 13:35:37 +0000685 mime = (const xmlChar *) cur;
686 last = mime;
687 while ((*last != 0) && (*last != ' ') && (*last != '\t') &&
688 (*last != ';') && (*last != ','))
689 last++;
690 if (ctxt->mimeType != NULL)
691 xmlFree(ctxt->mimeType);
692 ctxt->mimeType = (char *) xmlStrndup(mime, last - mime);
693 charset = xmlStrstr(BAD_CAST ctxt->contentType, BAD_CAST "charset=");
694 if (charset != NULL) {
695 charset += 8;
696 last = charset;
697 while ((*last != 0) && (*last != ' ') && (*last != '\t') &&
698 (*last != ';') && (*last != ','))
699 last++;
700 if (ctxt->encoding != NULL)
701 xmlFree(ctxt->encoding);
702 ctxt->encoding = (char *) xmlStrndup(charset, last - charset);
703 }
Owen Taylor3473f882001-02-23 17:55:21 +0000704 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"ContentType:", 12)) {
Daniel Veillarda840b692003-10-19 13:35:37 +0000705 const xmlChar *charset, *last, *mime;
Owen Taylor3473f882001-02-23 17:55:21 +0000706 cur += 12;
707 if (ctxt->contentType != NULL) return;
708 while ((*cur == ' ') || (*cur == '\t')) cur++;
709 ctxt->contentType = xmlMemStrdup(cur);
Daniel Veillarda840b692003-10-19 13:35:37 +0000710 mime = (const xmlChar *) cur;
711 last = mime;
712 while ((*last != 0) && (*last != ' ') && (*last != '\t') &&
713 (*last != ';') && (*last != ','))
714 last++;
715 if (ctxt->mimeType != NULL)
716 xmlFree(ctxt->mimeType);
717 ctxt->mimeType = (char *) xmlStrndup(mime, last - mime);
718 charset = xmlStrstr(BAD_CAST ctxt->contentType, BAD_CAST "charset=");
719 if (charset != NULL) {
720 charset += 8;
721 last = charset;
722 while ((*last != 0) && (*last != ' ') && (*last != '\t') &&
723 (*last != ';') && (*last != ','))
724 last++;
725 if (ctxt->encoding != NULL)
726 xmlFree(ctxt->encoding);
727 ctxt->encoding = (char *) xmlStrndup(charset, last - charset);
728 }
Owen Taylor3473f882001-02-23 17:55:21 +0000729 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Location:", 9)) {
730 cur += 9;
731 while ((*cur == ' ') || (*cur == '\t')) cur++;
732 if (ctxt->location != NULL)
733 xmlFree(ctxt->location);
William M. Brack7e29c0a2004-04-02 09:07:22 +0000734 if (*cur == '/') {
735 xmlChar *tmp_http = xmlStrdup(BAD_CAST "http://");
736 xmlChar *tmp_loc =
737 xmlStrcat(tmp_http, (const xmlChar *) ctxt->hostname);
738 ctxt->location =
739 (char *) xmlStrcat (tmp_loc, (const xmlChar *) cur);
740 } else {
741 ctxt->location = xmlMemStrdup(cur);
742 }
Owen Taylor3473f882001-02-23 17:55:21 +0000743 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"WWW-Authenticate:", 17)) {
744 cur += 17;
745 while ((*cur == ' ') || (*cur == '\t')) cur++;
746 if (ctxt->authHeader != NULL)
747 xmlFree(ctxt->authHeader);
748 ctxt->authHeader = xmlMemStrdup(cur);
749 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Proxy-Authenticate:", 19)) {
750 cur += 19;
751 while ((*cur == ' ') || (*cur == '\t')) cur++;
752 if (ctxt->authHeader != NULL)
753 xmlFree(ctxt->authHeader);
754 ctxt->authHeader = xmlMemStrdup(cur);
Daniel Veillardf012a642001-07-23 19:10:52 +0000755 } else if ( !xmlStrncasecmp( BAD_CAST line, BAD_CAST"Content-Length:", 15) ) {
756 cur += 15;
757 ctxt->ContentLength = strtol( cur, NULL, 10 );
Owen Taylor3473f882001-02-23 17:55:21 +0000758 }
759}
760
761/**
762 * xmlNanoHTTPConnectAttempt:
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000763 * @addr: a socket address structure
Owen Taylor3473f882001-02-23 17:55:21 +0000764 *
765 * Attempt a connection to the given IP:port endpoint. It forces
766 * non-blocking semantic on the socket, and allow 60 seconds for
767 * the host to answer.
768 *
769 * Returns -1 in case of failure, the file descriptor number otherwise
770 */
771
772static int
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000773xmlNanoHTTPConnectAttempt(struct sockaddr *addr)
Owen Taylor3473f882001-02-23 17:55:21 +0000774{
Owen Taylor3473f882001-02-23 17:55:21 +0000775 fd_set wfd;
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000776#ifdef _WINSOCKAPI_
777 fd_set xfd;
778#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000779 struct timeval tv;
780 int status;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000781 int addrlen;
782 SOCKET s;
Owen Taylor3473f882001-02-23 17:55:21 +0000783
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000784#ifdef SUPPORT_IP6
785 if (addr->sa_family == AF_INET6) {
786 s = socket (PF_INET6, SOCK_STREAM, IPPROTO_TCP);
787 addrlen = sizeof (struct sockaddr_in6);
788 }
789 else
790#endif
791 {
792 s = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
793 addrlen = sizeof (struct sockaddr_in);
794 }
Owen Taylor3473f882001-02-23 17:55:21 +0000795 if (s==-1) {
796#ifdef DEBUG_HTTP
797 perror("socket");
798#endif
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000799 __xmlIOErr(XML_FROM_HTTP, 0, "socket failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000800 return(-1);
801 }
802
803#ifdef _WINSOCKAPI_
804 {
805 u_long one = 1;
806
807 status = ioctlsocket(s, FIONBIO, &one) == SOCKET_ERROR ? -1 : 0;
808 }
809#else /* _WINSOCKAPI_ */
810#if defined(VMS)
811 {
812 int enable = 1;
813 status = ioctl(s, FIONBIO, &enable);
814 }
815#else /* VMS */
Daniel Veillard254b1262003-11-01 17:04:58 +0000816#if defined(__BEOS__)
817 {
818 bool noblock = true;
819 status = setsockopt(s, SOL_SOCKET, SO_NONBLOCK, &noblock, sizeof(noblock));
820 }
821#else /* __BEOS__ */
Owen Taylor3473f882001-02-23 17:55:21 +0000822 if ((status = fcntl(s, F_GETFL, 0)) != -1) {
823#ifdef O_NONBLOCK
824 status |= O_NONBLOCK;
825#else /* O_NONBLOCK */
826#ifdef F_NDELAY
827 status |= F_NDELAY;
828#endif /* F_NDELAY */
829#endif /* !O_NONBLOCK */
830 status = fcntl(s, F_SETFL, status);
831 }
832 if (status < 0) {
833#ifdef DEBUG_HTTP
834 perror("nonblocking");
835#endif
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000836 __xmlIOErr(XML_FROM_HTTP, 0, "error setting non-blocking IO\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000837 closesocket(s);
838 return(-1);
839 }
Daniel Veillard254b1262003-11-01 17:04:58 +0000840#endif /* !__BEOS__ */
Owen Taylor3473f882001-02-23 17:55:21 +0000841#endif /* !VMS */
842#endif /* !_WINSOCKAPI_ */
843
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000844 if (connect (s, addr, addrlen) == -1) {
Owen Taylor3473f882001-02-23 17:55:21 +0000845 switch (socket_errno()) {
846 case EINPROGRESS:
847 case EWOULDBLOCK:
848 break;
849 default:
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000850 __xmlIOErr(XML_FROM_HTTP, 0, "error connecting to HTTP server");
Owen Taylor3473f882001-02-23 17:55:21 +0000851 closesocket(s);
852 return(-1);
853 }
854 }
855
856 tv.tv_sec = timeout;
857 tv.tv_usec = 0;
Daniel Veillard9e2110b2005-08-08 20:33:54 +0000858
859#ifdef _MSC_VER
860#pragma warning(push)
861#pragma warning(disable: 4018)
862#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000863 FD_ZERO(&wfd);
864 FD_SET(s, &wfd);
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000865
866#ifdef _WINSOCKAPI_
867 FD_ZERO(&xfd);
868 FD_SET(s, &xfd);
Owen Taylor3473f882001-02-23 17:55:21 +0000869
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000870 switch(select(s+1, NULL, &wfd, &xfd, &tv))
871#else
Owen Taylor3473f882001-02-23 17:55:21 +0000872 switch(select(s+1, NULL, &wfd, NULL, &tv))
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000873#endif
Daniel Veillard9e2110b2005-08-08 20:33:54 +0000874#ifdef _MSC_VER
875#pragma warning(pop)
876#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000877 {
878 case 0:
879 /* Time out */
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000880 __xmlIOErr(XML_FROM_HTTP, 0, "Connect attempt timed out");
Owen Taylor3473f882001-02-23 17:55:21 +0000881 closesocket(s);
882 return(-1);
883 case -1:
884 /* Ermm.. ?? */
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000885 __xmlIOErr(XML_FROM_HTTP, 0, "Connect failed");
Owen Taylor3473f882001-02-23 17:55:21 +0000886 closesocket(s);
887 return(-1);
888 }
889
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000890 if ( FD_ISSET(s, &wfd)
891#ifdef _WINSOCKAPI_
892 || FD_ISSET(s, &xfd)
893#endif
894 ) {
Daniel Veillardc284c642005-03-31 10:24:24 +0000895 XML_SOCKLEN_T len;
Owen Taylor3473f882001-02-23 17:55:21 +0000896 len = sizeof(status);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000897#ifdef SO_ERROR
Owen Taylor3473f882001-02-23 17:55:21 +0000898 if (getsockopt(s, SOL_SOCKET, SO_ERROR, (char*)&status, &len) < 0 ) {
899 /* Solaris error code */
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000900 __xmlIOErr(XML_FROM_HTTP, 0, "getsockopt failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000901 return (-1);
902 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000903#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000904 if ( status ) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000905 __xmlIOErr(XML_FROM_HTTP, 0, "Error connecting to remote host");
Owen Taylor3473f882001-02-23 17:55:21 +0000906 closesocket(s);
907 errno = status;
908 return (-1);
909 }
910 } else {
911 /* pbm */
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000912 __xmlIOErr(XML_FROM_HTTP, 0, "select failed\n");
Daniel Veillardf012a642001-07-23 19:10:52 +0000913 closesocket(s);
Owen Taylor3473f882001-02-23 17:55:21 +0000914 return (-1);
915 }
916
917 return(s);
918}
919
920/**
921 * xmlNanoHTTPConnectHost:
922 * @host: the host name
923 * @port: the port number
924 *
925 * Attempt a connection to the given host:port endpoint. It tries
926 * the multiple IP provided by the DNS if available.
927 *
928 * Returns -1 in case of failure, the file descriptor number otherwise
929 */
930
931static int
932xmlNanoHTTPConnectHost(const char *host, int port)
933{
934 struct hostent *h;
Daniel Veillard2db8c122003-07-08 12:16:59 +0000935 struct sockaddr *addr = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +0000936 struct in_addr ia;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000937 struct sockaddr_in sockin;
Daniel Veillard5c396542002-03-15 07:57:50 +0000938
Owen Taylor3473f882001-02-23 17:55:21 +0000939#ifdef SUPPORT_IP6
940 struct in6_addr ia6;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000941 struct sockaddr_in6 sockin6;
Owen Taylor3473f882001-02-23 17:55:21 +0000942#endif
943 int i;
944 int s;
Daniel Veillard5c396542002-03-15 07:57:50 +0000945
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000946 memset (&sockin, 0, sizeof(sockin));
947#ifdef SUPPORT_IP6
948 memset (&sockin6, 0, sizeof(sockin6));
Rob Richardscb418de2005-10-13 23:12:42 +0000949#endif
950
951#if !defined(HAVE_GETADDRINFO) && defined(SUPPORT_IP6) && defined(RES_USE_INET6)
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000952 if (have_ipv6 ())
Daniel Veillard560c2a42003-07-06 21:13:49 +0000953 {
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000954 if (!(_res.options & RES_INIT))
955 res_init();
956 _res.options |= RES_USE_INET6;
957 }
Rob Richardscb418de2005-10-13 23:12:42 +0000958#endif
959
960#if defined(HAVE_GETADDRINFO) && defined(SUPPORT_IP6) && !defined(_WIN32)
961 if (have_ipv6 ())
962#endif
963#if defined(HAVE_GETADDRINFO) && (defined(SUPPORT_IP6) || defined(_WIN32))
Daniel Veillard560c2a42003-07-06 21:13:49 +0000964 {
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000965 int status;
966 struct addrinfo hints, *res, *result;
967
968 result = NULL;
969 memset (&hints, 0,sizeof(hints));
970 hints.ai_socktype = SOCK_STREAM;
971
972 status = getaddrinfo (host, NULL, &hints, &result);
973 if (status) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000974 __xmlIOErr(XML_FROM_HTTP, 0, "getaddrinfo failed\n");
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000975 return (-1);
976 }
977
978 for (res = result; res; res = res->ai_next) {
Rob Richardscb418de2005-10-13 23:12:42 +0000979 if (res->ai_family == AF_INET) {
980 if (res->ai_addrlen > sizeof(sockin)) {
981 __xmlIOErr(XML_FROM_HTTP, 0, "address size mismatch\n");
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000982 freeaddrinfo (result);
Rob Richardscb418de2005-10-13 23:12:42 +0000983 return (-1);
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000984 }
Rob Richardscb418de2005-10-13 23:12:42 +0000985 memcpy (&sockin, res->ai_addr, res->ai_addrlen);
986 sockin.sin_port = htons (port);
987 addr = (struct sockaddr *)&sockin;
988#ifdef SUPPORT_IP6
989 } else if (have_ipv6 () && (res->ai_family == AF_INET6)) {
990 if (res->ai_addrlen > sizeof(sockin6)) {
991 __xmlIOErr(XML_FROM_HTTP, 0, "address size mismatch\n");
992 freeaddrinfo (result);
993 return (-1);
994 }
995 memcpy (&sockin6, res->ai_addr, res->ai_addrlen);
996 sockin6.sin6_port = htons (port);
997 addr = (struct sockaddr *)&sockin6;
998#endif
999 } else
1000 continue; /* for */
1001
1002 s = xmlNanoHTTPConnectAttempt (addr);
1003 if (s != -1) {
1004 freeaddrinfo (result);
1005 return (s);
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001006 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001007 }
Rob Richardscb418de2005-10-13 23:12:42 +00001008
Daniel Veillard3dc93a42003-07-10 14:04:33 +00001009 if (result)
1010 freeaddrinfo (result);
Rob Richardscb418de2005-10-13 23:12:42 +00001011 }
Owen Taylor3473f882001-02-23 17:55:21 +00001012#endif
Rob Richardscb418de2005-10-13 23:12:42 +00001013#if defined(HAVE_GETADDRINFO) && defined(SUPPORT_IP6) && !defined(_WIN32)
1014 else
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001015#endif
Rob Richardscb418de2005-10-13 23:12:42 +00001016#if !defined(HAVE_GETADDRINFO) || !defined(_WIN32)
1017 {
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001018 h = gethostbyname (host);
1019 if (h == NULL) {
Daniel Veillard56b2db72002-03-25 16:35:28 +00001020
1021/*
1022 * Okay, I got fed up by the non-portability of this error message
1023 * extraction code. it work on Linux, if it work on your platform
1024 * and one want to enable it, send me the defined(foobar) needed
1025 */
1026#if defined(HAVE_NETDB_H) && defined(HOST_NOT_FOUND) && defined(linux)
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001027 const char *h_err_txt = "";
Daniel Veillardf012a642001-07-23 19:10:52 +00001028
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001029 switch (h_errno) {
1030 case HOST_NOT_FOUND:
1031 h_err_txt = "Authoritive host not found";
1032 break;
Daniel Veillardf012a642001-07-23 19:10:52 +00001033
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001034 case TRY_AGAIN:
1035 h_err_txt =
1036 "Non-authoritive host not found or server failure.";
1037 break;
Daniel Veillardf012a642001-07-23 19:10:52 +00001038
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001039 case NO_RECOVERY:
1040 h_err_txt =
1041 "Non-recoverable errors: FORMERR, REFUSED, or NOTIMP.";
1042 break;
Daniel Veillard5c396542002-03-15 07:57:50 +00001043
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001044 case NO_ADDRESS:
1045 h_err_txt =
1046 "Valid name, no data record of requested type.";
1047 break;
Daniel Veillard5c396542002-03-15 07:57:50 +00001048
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001049 default:
1050 h_err_txt = "No error text defined.";
1051 break;
1052 }
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001053 __xmlIOErr(XML_FROM_HTTP, 0, h_err_txt);
Daniel Veillard5c396542002-03-15 07:57:50 +00001054#else
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001055 __xmlIOErr(XML_FROM_HTTP, 0, "Failed to resolve host");
Owen Taylor3473f882001-02-23 17:55:21 +00001056#endif
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001057 return (-1);
1058 }
Daniel Veillard5c396542002-03-15 07:57:50 +00001059
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001060 for (i = 0; h->h_addr_list[i]; i++) {
1061 if (h->h_addrtype == AF_INET) {
1062 /* A records (IPv4) */
Daniel Veillard8e2c9792004-10-27 09:39:50 +00001063 if ((unsigned int) h->h_length > sizeof(ia)) {
1064 __xmlIOErr(XML_FROM_HTTP, 0, "address size mismatch\n");
1065 return (-1);
1066 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001067 memcpy (&ia, h->h_addr_list[i], h->h_length);
1068 sockin.sin_family = h->h_addrtype;
1069 sockin.sin_addr = ia;
Daniel Veillard9e2110b2005-08-08 20:33:54 +00001070 sockin.sin_port = (u_short)htons ((unsigned short)port);
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001071 addr = (struct sockaddr *) &sockin;
Daniel Veillard5c396542002-03-15 07:57:50 +00001072#ifdef SUPPORT_IP6
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001073 } else if (have_ipv6 () && (h->h_addrtype == AF_INET6)) {
1074 /* AAAA records (IPv6) */
Daniel Veillard8e2c9792004-10-27 09:39:50 +00001075 if ((unsigned int) h->h_length > sizeof(ia6)) {
1076 __xmlIOErr(XML_FROM_HTTP, 0, "address size mismatch\n");
1077 return (-1);
1078 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001079 memcpy (&ia6, h->h_addr_list[i], h->h_length);
1080 sockin6.sin6_family = h->h_addrtype;
1081 sockin6.sin6_addr = ia6;
1082 sockin6.sin6_port = htons (port);
1083 addr = (struct sockaddr *) &sockin6;
Daniel Veillard5c396542002-03-15 07:57:50 +00001084#endif
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001085 } else
1086 break; /* for */
Daniel Veillard5c396542002-03-15 07:57:50 +00001087
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001088 s = xmlNanoHTTPConnectAttempt (addr);
1089 if (s != -1)
1090 return (s);
1091 }
Owen Taylor3473f882001-02-23 17:55:21 +00001092 }
Rob Richardscb418de2005-10-13 23:12:42 +00001093#endif
1094
Owen Taylor3473f882001-02-23 17:55:21 +00001095#ifdef DEBUG_HTTP
1096 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard5c396542002-03-15 07:57:50 +00001097 "xmlNanoHTTPConnectHost: unable to connect to '%s'.\n",
1098 host);
Owen Taylor3473f882001-02-23 17:55:21 +00001099#endif
Daniel Veillard5c396542002-03-15 07:57:50 +00001100 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001101}
1102
1103
1104/**
1105 * xmlNanoHTTPOpen:
1106 * @URL: The URL to load
1107 * @contentType: if available the Content-Type information will be
1108 * returned at that location
1109 *
1110 * This function try to open a connection to the indicated resource
1111 * via HTTP GET.
1112 *
1113 * Returns NULL in case of failure, otherwise a request handler.
1114 * The contentType, if provided must be freed by the caller
1115 */
1116
1117void*
1118xmlNanoHTTPOpen(const char *URL, char **contentType) {
1119 if (contentType != NULL) *contentType = NULL;
Daniel Veillardf012a642001-07-23 19:10:52 +00001120 return(xmlNanoHTTPMethod(URL, NULL, NULL, contentType, NULL, 0));
Daniel Veillard9403a042001-05-28 11:00:53 +00001121}
1122
1123/**
1124 * xmlNanoHTTPOpenRedir:
1125 * @URL: The URL to load
1126 * @contentType: if available the Content-Type information will be
1127 * returned at that location
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001128 * @redir: if available the redirected URL will be returned
Daniel Veillard9403a042001-05-28 11:00:53 +00001129 *
1130 * This function try to open a connection to the indicated resource
1131 * via HTTP GET.
1132 *
1133 * Returns NULL in case of failure, otherwise a request handler.
1134 * The contentType, if provided must be freed by the caller
1135 */
1136
1137void*
1138xmlNanoHTTPOpenRedir(const char *URL, char **contentType, char **redir) {
1139 if (contentType != NULL) *contentType = NULL;
1140 if (redir != NULL) *redir = NULL;
Daniel Veillardf012a642001-07-23 19:10:52 +00001141 return(xmlNanoHTTPMethodRedir(URL, NULL, NULL, contentType, redir, NULL,0));
Owen Taylor3473f882001-02-23 17:55:21 +00001142}
1143
1144/**
1145 * xmlNanoHTTPRead:
1146 * @ctx: the HTTP context
1147 * @dest: a buffer
1148 * @len: the buffer length
1149 *
1150 * This function tries to read @len bytes from the existing HTTP connection
1151 * and saves them in @dest. This is a blocking call.
1152 *
1153 * Returns the number of byte read. 0 is an indication of an end of connection.
1154 * -1 indicates a parameter error.
1155 */
1156int
1157xmlNanoHTTPRead(void *ctx, void *dest, int len) {
1158 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1159
1160 if (ctx == NULL) return(-1);
1161 if (dest == NULL) return(-1);
1162 if (len <= 0) return(0);
1163
1164 while (ctxt->inptr - ctxt->inrptr < len) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001165 if (xmlNanoHTTPRecv(ctxt) <= 0) break;
Owen Taylor3473f882001-02-23 17:55:21 +00001166 }
1167 if (ctxt->inptr - ctxt->inrptr < len)
1168 len = ctxt->inptr - ctxt->inrptr;
1169 memcpy(dest, ctxt->inrptr, len);
1170 ctxt->inrptr += len;
1171 return(len);
1172}
1173
1174/**
1175 * xmlNanoHTTPClose:
1176 * @ctx: the HTTP context
1177 *
1178 * This function closes an HTTP context, it ends up the connection and
1179 * free all data related to it.
1180 */
1181void
1182xmlNanoHTTPClose(void *ctx) {
1183 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1184
1185 if (ctx == NULL) return;
1186
1187 xmlNanoHTTPFreeCtxt(ctxt);
1188}
1189
1190/**
Daniel Veillard9403a042001-05-28 11:00:53 +00001191 * xmlNanoHTTPMethodRedir:
Owen Taylor3473f882001-02-23 17:55:21 +00001192 * @URL: The URL to load
1193 * @method: the HTTP method to use
1194 * @input: the input string if any
1195 * @contentType: the Content-Type information IN and OUT
Daniel Veillard9403a042001-05-28 11:00:53 +00001196 * @redir: the redirected URL OUT
Owen Taylor3473f882001-02-23 17:55:21 +00001197 * @headers: the extra headers
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001198 * @ilen: input length
Owen Taylor3473f882001-02-23 17:55:21 +00001199 *
1200 * This function try to open a connection to the indicated resource
1201 * via HTTP using the given @method, adding the given extra headers
1202 * and the input buffer for the request content.
1203 *
1204 * Returns NULL in case of failure, otherwise a request handler.
Daniel Veillard9403a042001-05-28 11:00:53 +00001205 * The contentType, or redir, if provided must be freed by the caller
Owen Taylor3473f882001-02-23 17:55:21 +00001206 */
1207
1208void*
Daniel Veillard9403a042001-05-28 11:00:53 +00001209xmlNanoHTTPMethodRedir(const char *URL, const char *method, const char *input,
Daniel Veillardf012a642001-07-23 19:10:52 +00001210 char **contentType, char **redir,
1211 const char *headers, int ilen ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001212 xmlNanoHTTPCtxtPtr ctxt;
1213 char *bp, *p;
Daniel Veillardf012a642001-07-23 19:10:52 +00001214 int blen, ret;
Owen Taylor3473f882001-02-23 17:55:21 +00001215 int head;
1216 int nbRedirects = 0;
1217 char *redirURL = NULL;
William M. Brack78637da2003-07-31 14:47:38 +00001218#ifdef DEBUG_HTTP
1219 int xmt_bytes;
1220#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001221
1222 if (URL == NULL) return(NULL);
1223 if (method == NULL) method = "GET";
1224 xmlNanoHTTPInit();
1225
1226retry:
1227 if (redirURL == NULL)
1228 ctxt = xmlNanoHTTPNewCtxt(URL);
1229 else {
1230 ctxt = xmlNanoHTTPNewCtxt(redirURL);
Daniel Veillarda840b692003-10-19 13:35:37 +00001231 ctxt->location = xmlMemStrdup(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001232 }
1233
Daniel Veillardf012a642001-07-23 19:10:52 +00001234 if ( ctxt == NULL ) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001235 return ( NULL );
1236 }
1237
Owen Taylor3473f882001-02-23 17:55:21 +00001238 if ((ctxt->protocol == NULL) || (strcmp(ctxt->protocol, "http"))) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001239 __xmlIOErr(XML_FROM_HTTP, XML_HTTP_URL_SYNTAX, "Not a valid HTTP URI");
Owen Taylor3473f882001-02-23 17:55:21 +00001240 xmlNanoHTTPFreeCtxt(ctxt);
1241 if (redirURL != NULL) xmlFree(redirURL);
1242 return(NULL);
1243 }
1244 if (ctxt->hostname == NULL) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001245 __xmlIOErr(XML_FROM_HTTP, XML_HTTP_UNKNOWN_HOST,
1246 "Failed to identify host in URI");
Owen Taylor3473f882001-02-23 17:55:21 +00001247 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001248 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001249 return(NULL);
1250 }
1251 if (proxy) {
1252 blen = strlen(ctxt->hostname) * 2 + 16;
1253 ret = xmlNanoHTTPConnectHost(proxy, proxyPort);
1254 }
1255 else {
1256 blen = strlen(ctxt->hostname);
1257 ret = xmlNanoHTTPConnectHost(ctxt->hostname, ctxt->port);
1258 }
1259 if (ret < 0) {
1260 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001261 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001262 return(NULL);
1263 }
1264 ctxt->fd = ret;
1265
Daniel Veillardf012a642001-07-23 19:10:52 +00001266 if (input == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00001267 ilen = 0;
Daniel Veillardf012a642001-07-23 19:10:52 +00001268 else
1269 blen += 36;
1270
Owen Taylor3473f882001-02-23 17:55:21 +00001271 if (headers != NULL)
Daniel Veillardf012a642001-07-23 19:10:52 +00001272 blen += strlen(headers) + 2;
Owen Taylor3473f882001-02-23 17:55:21 +00001273 if (contentType && *contentType)
1274 blen += strlen(*contentType) + 16;
Daniel Veillard351f2d62005-04-13 02:55:12 +00001275 if (ctxt->query != NULL)
1276 blen += strlen(ctxt->query) + 1;
Daniel Veillardf012a642001-07-23 19:10:52 +00001277 blen += strlen(method) + strlen(ctxt->path) + 24;
Daniel Veillard82cb3192003-10-29 13:39:15 +00001278 bp = (char*)xmlMallocAtomic(blen);
Daniel Veillardf012a642001-07-23 19:10:52 +00001279 if ( bp == NULL ) {
1280 xmlNanoHTTPFreeCtxt( ctxt );
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001281 xmlHTTPErrMemory("allocating header buffer");
Daniel Veillardf012a642001-07-23 19:10:52 +00001282 return ( NULL );
1283 }
1284
1285 p = bp;
1286
Owen Taylor3473f882001-02-23 17:55:21 +00001287 if (proxy) {
1288 if (ctxt->port != 80) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001289 p += snprintf( p, blen - (p - bp), "%s http://%s:%d%s",
1290 method, ctxt->hostname,
Daniel Veillardf012a642001-07-23 19:10:52 +00001291 ctxt->port, ctxt->path );
Owen Taylor3473f882001-02-23 17:55:21 +00001292 }
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001293 else
1294 p += snprintf( p, blen - (p - bp), "%s http://%s%s", method,
Daniel Veillardf012a642001-07-23 19:10:52 +00001295 ctxt->hostname, ctxt->path);
Owen Taylor3473f882001-02-23 17:55:21 +00001296 }
1297 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001298 p += snprintf( p, blen - (p - bp), "%s %s", method, ctxt->path);
Daniel Veillardf012a642001-07-23 19:10:52 +00001299
Daniel Veillard351f2d62005-04-13 02:55:12 +00001300 if (ctxt->query != NULL)
1301 p += snprintf( p, blen - (p - bp), "?%s", ctxt->query);
1302
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001303 p += snprintf( p, blen - (p - bp), " HTTP/1.0\r\nHost: %s\r\n",
1304 ctxt->hostname);
Daniel Veillardf012a642001-07-23 19:10:52 +00001305
1306 if (contentType != NULL && *contentType)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001307 p += snprintf(p, blen - (p - bp), "Content-Type: %s\r\n", *contentType);
Daniel Veillardf012a642001-07-23 19:10:52 +00001308
1309 if (headers != NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001310 p += snprintf( p, blen - (p - bp), "%s", headers );
Daniel Veillardf012a642001-07-23 19:10:52 +00001311
Owen Taylor3473f882001-02-23 17:55:21 +00001312 if (input != NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001313 snprintf(p, blen - (p - bp), "Content-Length: %d\r\n\r\n", ilen );
Owen Taylor3473f882001-02-23 17:55:21 +00001314 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001315 snprintf(p, blen - (p - bp), "\r\n");
Daniel Veillardf012a642001-07-23 19:10:52 +00001316
Owen Taylor3473f882001-02-23 17:55:21 +00001317#ifdef DEBUG_HTTP
1318 xmlGenericError(xmlGenericErrorContext,
1319 "-> %s%s", proxy? "(Proxy) " : "", bp);
1320 if ((blen -= strlen(bp)+1) < 0)
1321 xmlGenericError(xmlGenericErrorContext,
1322 "ERROR: overflowed buffer by %d bytes\n", -blen);
1323#endif
1324 ctxt->outptr = ctxt->out = bp;
1325 ctxt->state = XML_NANO_HTTP_WRITE;
Daniel Veillardf012a642001-07-23 19:10:52 +00001326 blen = strlen( ctxt->out );
Daniel Veillardf012a642001-07-23 19:10:52 +00001327#ifdef DEBUG_HTTP
William M. Brack78637da2003-07-31 14:47:38 +00001328 xmt_bytes = xmlNanoHTTPSend(ctxt, ctxt->out, blen );
Daniel Veillardf012a642001-07-23 19:10:52 +00001329 if ( xmt_bytes != blen )
1330 xmlGenericError( xmlGenericErrorContext,
1331 "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
1332 xmt_bytes, blen,
1333 "bytes of HTTP headers sent to host",
1334 ctxt->hostname );
William M. Brack78637da2003-07-31 14:47:38 +00001335#else
1336 xmlNanoHTTPSend(ctxt, ctxt->out, blen );
Daniel Veillardf012a642001-07-23 19:10:52 +00001337#endif
1338
1339 if ( input != NULL ) {
William M. Brack78637da2003-07-31 14:47:38 +00001340#ifdef DEBUG_HTTP
Daniel Veillardf012a642001-07-23 19:10:52 +00001341 xmt_bytes = xmlNanoHTTPSend( ctxt, input, ilen );
1342
Daniel Veillardf012a642001-07-23 19:10:52 +00001343 if ( xmt_bytes != ilen )
1344 xmlGenericError( xmlGenericErrorContext,
1345 "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
1346 xmt_bytes, ilen,
1347 "bytes of HTTP content sent to host",
1348 ctxt->hostname );
William M. Brack78637da2003-07-31 14:47:38 +00001349#else
1350 xmlNanoHTTPSend( ctxt, input, ilen );
Daniel Veillardf012a642001-07-23 19:10:52 +00001351#endif
1352 }
1353
Owen Taylor3473f882001-02-23 17:55:21 +00001354 ctxt->state = XML_NANO_HTTP_READ;
1355 head = 1;
1356
1357 while ((p = xmlNanoHTTPReadLine(ctxt)) != NULL) {
1358 if (head && (*p == 0)) {
1359 head = 0;
1360 ctxt->content = ctxt->inrptr;
1361 xmlFree(p);
1362 break;
1363 }
1364 xmlNanoHTTPScanAnswer(ctxt, p);
1365
1366#ifdef DEBUG_HTTP
1367 xmlGenericError(xmlGenericErrorContext, "<- %s\n", p);
1368#endif
1369 xmlFree(p);
1370 }
1371
1372 if ((ctxt->location != NULL) && (ctxt->returnValue >= 300) &&
1373 (ctxt->returnValue < 400)) {
1374#ifdef DEBUG_HTTP
1375 xmlGenericError(xmlGenericErrorContext,
1376 "\nRedirect to: %s\n", ctxt->location);
1377#endif
Daniel Veillardf012a642001-07-23 19:10:52 +00001378 while ( xmlNanoHTTPRecv(ctxt) > 0 ) ;
Owen Taylor3473f882001-02-23 17:55:21 +00001379 if (nbRedirects < XML_NANO_HTTP_MAX_REDIR) {
1380 nbRedirects++;
Daniel Veillard9403a042001-05-28 11:00:53 +00001381 if (redirURL != NULL)
1382 xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001383 redirURL = xmlMemStrdup(ctxt->location);
1384 xmlNanoHTTPFreeCtxt(ctxt);
1385 goto retry;
1386 }
1387 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001388 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001389#ifdef DEBUG_HTTP
1390 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardf012a642001-07-23 19:10:52 +00001391 "xmlNanoHTTPMethodRedir: Too many redirects, aborting ...\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001392#endif
1393 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001394 }
1395
1396 if (contentType != NULL) {
1397 if (ctxt->contentType != NULL)
1398 *contentType = xmlMemStrdup(ctxt->contentType);
1399 else
1400 *contentType = NULL;
1401 }
1402
Daniel Veillard9403a042001-05-28 11:00:53 +00001403 if ((redir != NULL) && (redirURL != NULL)) {
1404 *redir = redirURL;
1405 } else {
1406 if (redirURL != NULL)
1407 xmlFree(redirURL);
1408 if (redir != NULL)
1409 *redir = NULL;
1410 }
1411
Owen Taylor3473f882001-02-23 17:55:21 +00001412#ifdef DEBUG_HTTP
1413 if (ctxt->contentType != NULL)
1414 xmlGenericError(xmlGenericErrorContext,
1415 "\nCode %d, content-type '%s'\n\n",
1416 ctxt->returnValue, ctxt->contentType);
1417 else
1418 xmlGenericError(xmlGenericErrorContext,
1419 "\nCode %d, no content-type\n\n",
1420 ctxt->returnValue);
1421#endif
1422
1423 return((void *) ctxt);
1424}
1425
1426/**
Daniel Veillard9403a042001-05-28 11:00:53 +00001427 * xmlNanoHTTPMethod:
1428 * @URL: The URL to load
1429 * @method: the HTTP method to use
1430 * @input: the input string if any
1431 * @contentType: the Content-Type information IN and OUT
1432 * @headers: the extra headers
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001433 * @ilen: input length
Daniel Veillard9403a042001-05-28 11:00:53 +00001434 *
1435 * This function try to open a connection to the indicated resource
1436 * via HTTP using the given @method, adding the given extra headers
1437 * and the input buffer for the request content.
1438 *
1439 * Returns NULL in case of failure, otherwise a request handler.
1440 * The contentType, if provided must be freed by the caller
1441 */
1442
1443void*
1444xmlNanoHTTPMethod(const char *URL, const char *method, const char *input,
Daniel Veillardf012a642001-07-23 19:10:52 +00001445 char **contentType, const char *headers, int ilen) {
Daniel Veillard9403a042001-05-28 11:00:53 +00001446 return(xmlNanoHTTPMethodRedir(URL, method, input, contentType,
Daniel Veillardf012a642001-07-23 19:10:52 +00001447 NULL, headers, ilen));
Daniel Veillard9403a042001-05-28 11:00:53 +00001448}
1449
1450/**
Owen Taylor3473f882001-02-23 17:55:21 +00001451 * xmlNanoHTTPFetch:
1452 * @URL: The URL to load
1453 * @filename: the filename where the content should be saved
1454 * @contentType: if available the Content-Type information will be
1455 * returned at that location
1456 *
1457 * This function try to fetch the indicated resource via HTTP GET
1458 * and save it's content in the file.
1459 *
1460 * Returns -1 in case of failure, 0 incase of success. The contentType,
1461 * if provided must be freed by the caller
1462 */
1463int
1464xmlNanoHTTPFetch(const char *URL, const char *filename, char **contentType) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001465 void *ctxt = NULL;
1466 char *buf = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001467 int fd;
1468 int len;
1469
William M. Brack015ccb22005-02-13 08:18:52 +00001470 if (filename == NULL) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001471 ctxt = xmlNanoHTTPOpen(URL, contentType);
1472 if (ctxt == NULL) return(-1);
1473
1474 if (!strcmp(filename, "-"))
1475 fd = 0;
1476 else {
1477 fd = open(filename, O_CREAT | O_WRONLY, 00644);
1478 if (fd < 0) {
1479 xmlNanoHTTPClose(ctxt);
1480 if ((contentType != NULL) && (*contentType != NULL)) {
1481 xmlFree(*contentType);
1482 *contentType = NULL;
1483 }
1484 return(-1);
1485 }
1486 }
1487
Daniel Veillardf012a642001-07-23 19:10:52 +00001488 xmlNanoHTTPFetchContent( ctxt, &buf, &len );
1489 if ( len > 0 ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001490 write(fd, buf, len);
1491 }
1492
1493 xmlNanoHTTPClose(ctxt);
1494 close(fd);
1495 return(0);
1496}
1497
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001498#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001499/**
1500 * xmlNanoHTTPSave:
1501 * @ctxt: the HTTP context
1502 * @filename: the filename where the content should be saved
1503 *
1504 * This function saves the output of the HTTP transaction to a file
1505 * It closes and free the context at the end
1506 *
1507 * Returns -1 in case of failure, 0 incase of success.
1508 */
1509int
1510xmlNanoHTTPSave(void *ctxt, const char *filename) {
Daniel Veillarde3924972001-07-25 20:25:21 +00001511 char *buf = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001512 int fd;
1513 int len;
1514
William M. Brack015ccb22005-02-13 08:18:52 +00001515 if ((ctxt == NULL) || (filename == NULL)) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001516
1517 if (!strcmp(filename, "-"))
1518 fd = 0;
1519 else {
1520 fd = open(filename, O_CREAT | O_WRONLY);
1521 if (fd < 0) {
1522 xmlNanoHTTPClose(ctxt);
1523 return(-1);
1524 }
1525 }
1526
Daniel Veillardf012a642001-07-23 19:10:52 +00001527 xmlNanoHTTPFetchContent( ctxt, &buf, &len );
1528 if ( len > 0 ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001529 write(fd, buf, len);
1530 }
1531
1532 xmlNanoHTTPClose(ctxt);
William M. Brack20d82362004-03-17 08:44:46 +00001533 close(fd);
Owen Taylor3473f882001-02-23 17:55:21 +00001534 return(0);
1535}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001536#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001537
1538/**
1539 * xmlNanoHTTPReturnCode:
1540 * @ctx: the HTTP context
1541 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001542 * Get the latest HTTP return code received
1543 *
Owen Taylor3473f882001-02-23 17:55:21 +00001544 * Returns the HTTP return code for the request.
1545 */
1546int
1547xmlNanoHTTPReturnCode(void *ctx) {
1548 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1549
1550 if (ctxt == NULL) return(-1);
1551
1552 return(ctxt->returnValue);
1553}
1554
1555/**
1556 * xmlNanoHTTPAuthHeader:
1557 * @ctx: the HTTP context
1558 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001559 * Get the authentication header of an HTTP context
1560 *
Owen Taylor3473f882001-02-23 17:55:21 +00001561 * Returns the stashed value of the WWW-Authenticate or Proxy-Authenticate
1562 * header.
1563 */
1564const char *
1565xmlNanoHTTPAuthHeader(void *ctx) {
1566 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1567
1568 if (ctxt == NULL) return(NULL);
1569
1570 return(ctxt->authHeader);
1571}
1572
Daniel Veillardf012a642001-07-23 19:10:52 +00001573/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00001574 * xmlNanoHTTPContentLength:
Daniel Veillardf012a642001-07-23 19:10:52 +00001575 * @ctx: the HTTP context
1576 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001577 * Provides the specified content length from the HTTP header.
1578 *
Daniel Veillardf012a642001-07-23 19:10:52 +00001579 * Return the specified content length from the HTTP header. Note that
1580 * a value of -1 indicates that the content length element was not included in
1581 * the response header.
1582 */
1583int
1584xmlNanoHTTPContentLength( void * ctx ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001585 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillardf012a642001-07-23 19:10:52 +00001586
1587 return ( ( ctxt == NULL ) ? -1 : ctxt->ContentLength );
1588}
1589
1590/**
Daniel Veillard847332a2003-10-18 11:29:40 +00001591 * xmlNanoHTTPRedir:
1592 * @ctx: the HTTP context
1593 *
1594 * Provides the specified redirection URL if available from the HTTP header.
1595 *
1596 * Return the specified redirection URL or NULL if not redirected.
1597 */
1598const char *
1599xmlNanoHTTPRedir( void * ctx ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001600 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillard847332a2003-10-18 11:29:40 +00001601
1602 return ( ( ctxt == NULL ) ? NULL : ctxt->location );
1603}
1604
1605/**
1606 * xmlNanoHTTPEncoding:
1607 * @ctx: the HTTP context
1608 *
1609 * Provides the specified encoding if specified in the HTTP headers.
1610 *
1611 * Return the specified encoding or NULL if not available
1612 */
1613const char *
1614xmlNanoHTTPEncoding( void * ctx ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001615 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillard847332a2003-10-18 11:29:40 +00001616
1617 return ( ( ctxt == NULL ) ? NULL : ctxt->encoding );
1618}
1619
1620/**
Daniel Veillarda840b692003-10-19 13:35:37 +00001621 * xmlNanoHTTPMimeType:
1622 * @ctx: the HTTP context
1623 *
1624 * Provides the specified Mime-Type if specified in the HTTP headers.
1625 *
1626 * Return the specified Mime-Type or NULL if not available
1627 */
1628const char *
1629xmlNanoHTTPMimeType( void * ctx ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001630 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillarda840b692003-10-19 13:35:37 +00001631
1632 return ( ( ctxt == NULL ) ? NULL : ctxt->mimeType );
1633}
1634
1635/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00001636 * xmlNanoHTTPFetchContent:
Daniel Veillardf012a642001-07-23 19:10:52 +00001637 * @ctx: the HTTP context
1638 * @ptr: pointer to set to the content buffer.
1639 * @len: integer pointer to hold the length of the content
1640 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001641 * Check if all the content was read
1642 *
Daniel Veillardf012a642001-07-23 19:10:52 +00001643 * Returns 0 if all the content was read and available, returns
1644 * -1 if received content length was less than specified or an error
1645 * occurred.
1646 */
Daniel Veillarda2351322004-06-27 12:08:10 +00001647static int
Daniel Veillardf012a642001-07-23 19:10:52 +00001648xmlNanoHTTPFetchContent( void * ctx, char ** ptr, int * len ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001649 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillardf012a642001-07-23 19:10:52 +00001650
1651 int rc = 0;
1652 int cur_lgth;
1653 int rcvd_lgth;
1654 int dummy_int;
1655 char * dummy_ptr = NULL;
1656
1657 /* Dummy up return input parameters if not provided */
1658
1659 if ( len == NULL )
1660 len = &dummy_int;
1661
1662 if ( ptr == NULL )
1663 ptr = &dummy_ptr;
1664
1665 /* But can't work without the context pointer */
1666
1667 if ( ( ctxt == NULL ) || ( ctxt->content == NULL ) ) {
1668 *len = 0;
1669 *ptr = NULL;
1670 return ( -1 );
1671 }
1672
1673 rcvd_lgth = ctxt->inptr - ctxt->content;
1674
1675 while ( (cur_lgth = xmlNanoHTTPRecv( ctxt )) > 0 ) {
1676
1677 rcvd_lgth += cur_lgth;
1678 if ( (ctxt->ContentLength > 0) && (rcvd_lgth >= ctxt->ContentLength) )
1679 break;
1680 }
1681
1682 *ptr = ctxt->content;
1683 *len = rcvd_lgth;
1684
1685 if ( ( ctxt->ContentLength > 0 ) && ( rcvd_lgth < ctxt->ContentLength ) )
1686 rc = -1;
1687 else if ( rcvd_lgth == 0 )
1688 rc = -1;
1689
1690 return ( rc );
1691}
1692
Owen Taylor3473f882001-02-23 17:55:21 +00001693#ifdef STANDALONE
1694int main(int argc, char **argv) {
1695 char *contentType = NULL;
1696
1697 if (argv[1] != NULL) {
1698 if (argv[2] != NULL)
1699 xmlNanoHTTPFetch(argv[1], argv[2], &contentType);
1700 else
1701 xmlNanoHTTPFetch(argv[1], "-", &contentType);
1702 if (contentType != NULL) xmlFree(contentType);
1703 } else {
1704 xmlGenericError(xmlGenericErrorContext,
1705 "%s: minimal HTTP GET implementation\n", argv[0]);
1706 xmlGenericError(xmlGenericErrorContext,
1707 "\tusage %s [ URL [ filename ] ]\n", argv[0]);
1708 }
1709 xmlNanoHTTPCleanup();
1710 xmlMemoryDump();
1711 return(0);
1712}
1713#endif /* STANDALONE */
1714#else /* !LIBXML_HTTP_ENABLED */
1715#ifdef STANDALONE
1716#include <stdio.h>
1717int main(int argc, char **argv) {
1718 xmlGenericError(xmlGenericErrorContext,
1719 "%s : HTTP support not compiled in\n", argv[0]);
1720 return(0);
1721}
1722#endif /* STANDALONE */
1723#endif /* LIBXML_HTTP_ENABLED */
Daniel Veillard5d4644e2005-04-01 13:11:58 +00001724#define bottom_nanohttp
1725#include "elfgcchack.h"