blob: 9ae35740f0aa41e4fedef049dba4c575004a1c0a [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
Raphael Prevost48b60c32009-08-23 13:11:01 +020057#ifndef HAVE_POLL_H
Owen Taylor3473f882001-02-23 17:55:21 +000058#ifdef HAVE_SYS_SELECT_H
59#include <sys/select.h>
60#endif
Raphael Prevost48b60c32009-08-23 13:11:01 +020061#else
62#include <poll.h>
63#endif
Owen Taylor3473f882001-02-23 17:55:21 +000064#ifdef HAVE_STRINGS_H
65#include <strings.h>
66#endif
67#ifdef SUPPORT_IP6
68#include <resolv.h>
69#endif
Daniel Veillard9a2724d2005-12-15 11:12:26 +000070#ifdef HAVE_ZLIB_H
71#include <zlib.h>
72#endif
73
Owen Taylor3473f882001-02-23 17:55:21 +000074
75#ifdef VMS
76#include <stropts>
Daniel Veillardc284c642005-03-31 10:24:24 +000077#define XML_SOCKLEN_T unsigned int
Owen Taylor3473f882001-02-23 17:55:21 +000078#define SOCKET int
79#endif
80
Daniel Veillard59d3ed82007-04-17 12:44:58 +000081#if defined(__MINGW32__) || defined(_WIN32_WCE)
Daniel Veillard1638a472003-08-14 01:23:25 +000082#define _WINSOCKAPI_
83#include <wsockcompat.h>
84#include <winsock2.h>
Daniel Veillardc284c642005-03-31 10:24:24 +000085#undef XML_SOCKLEN_T
86#define XML_SOCKLEN_T unsigned int
Daniel Veillard1638a472003-08-14 01:23:25 +000087#endif
88
89
Daniel Veillardd0463562001-10-13 09:15:48 +000090#include <libxml/globals.h>
Daniel Veillardf012a642001-07-23 19:10:52 +000091#include <libxml/xmlerror.h>
Owen Taylor3473f882001-02-23 17:55:21 +000092#include <libxml/xmlmemory.h>
93#include <libxml/parser.h> /* for xmlStr(n)casecmp() */
94#include <libxml/nanohttp.h>
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000095#include <libxml/globals.h>
Daniel Veillard8efff672002-12-04 11:44:48 +000096#include <libxml/uri.h>
Owen Taylor3473f882001-02-23 17:55:21 +000097
98/**
99 * A couple portability macros
100 */
101#ifndef _WINSOCKAPI_
Daniel Veillardcba68392008-08-29 12:43:40 +0000102#if !defined(__BEOS__) || defined(__HAIKU__)
Owen Taylor3473f882001-02-23 17:55:21 +0000103#define closesocket(s) close(s)
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000104#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000105#define SOCKET int
106#endif
107
Daniel Veillard89f7f272003-09-29 13:29:09 +0000108#ifdef __BEOS__
109#ifndef PF_INET
110#define PF_INET AF_INET
111#endif
112#endif
113
Daniel Veillardc284c642005-03-31 10:24:24 +0000114#ifndef XML_SOCKLEN_T
115#define XML_SOCKLEN_T unsigned int
Daniel Veillard75be0132002-03-13 10:03:35 +0000116#endif
117#ifndef SOCKET
118#define SOCKET int
119#endif
Daniel Veillardf012a642001-07-23 19:10:52 +0000120
Owen Taylor3473f882001-02-23 17:55:21 +0000121#ifdef STANDALONE
122#define DEBUG_HTTP
123#define xmlStrncasecmp(a, b, n) strncasecmp((char *)a, (char *)b, n)
124#define xmlStrcasecmpi(a, b) strcasecmp((char *)a, (char *)b)
125#endif
126
127#define XML_NANO_HTTP_MAX_REDIR 10
128
129#define XML_NANO_HTTP_CHUNK 4096
130
131#define XML_NANO_HTTP_CLOSED 0
132#define XML_NANO_HTTP_WRITE 1
133#define XML_NANO_HTTP_READ 2
134#define XML_NANO_HTTP_NONE 4
135
136typedef struct xmlNanoHTTPCtxt {
137 char *protocol; /* the protocol name */
138 char *hostname; /* the host name */
139 int port; /* the port */
140 char *path; /* the path within the URL */
Daniel Veillard351f2d62005-04-13 02:55:12 +0000141 char *query; /* the query string */
Owen Taylor3473f882001-02-23 17:55:21 +0000142 SOCKET fd; /* the file descriptor for the socket */
143 int state; /* WRITE / READ / CLOSED */
144 char *out; /* buffer sent (zero terminated) */
145 char *outptr; /* index within the buffer sent */
146 char *in; /* the receiving buffer */
147 char *content; /* the start of the content */
148 char *inptr; /* the next byte to read from network */
149 char *inrptr; /* the next byte to give back to the client */
150 int inlen; /* len of the input buffer */
151 int last; /* return code for last operation */
152 int returnValue; /* the protocol return value */
Daniel Veillardf012a642001-07-23 19:10:52 +0000153 int ContentLength; /* specified content length from HTTP header */
Owen Taylor3473f882001-02-23 17:55:21 +0000154 char *contentType; /* the MIME type for the input */
155 char *location; /* the new URL in case of redirect */
156 char *authHeader; /* contents of {WWW,Proxy}-Authenticate header */
Daniel Veillard847332a2003-10-18 11:29:40 +0000157 char *encoding; /* encoding extracted from the contentType */
Daniel Veillarda840b692003-10-19 13:35:37 +0000158 char *mimeType; /* Mime-Type extracted from the contentType */
Daniel Veillard9a2724d2005-12-15 11:12:26 +0000159#ifdef HAVE_ZLIB_H
160 z_stream *strm; /* Zlib stream object */
161 int usesGzip; /* "Content-Encoding: gzip" was detected */
162#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000163} xmlNanoHTTPCtxt, *xmlNanoHTTPCtxtPtr;
164
165static int initialized = 0;
166static char *proxy = NULL; /* the proxy name if any */
167static int proxyPort; /* the proxy port if any */
168static unsigned int timeout = 60;/* the select() timeout in seconds */
169
Daniel Veillarda2351322004-06-27 12:08:10 +0000170static int xmlNanoHTTPFetchContent( void * ctx, char ** ptr, int * len );
Daniel Veillardf012a642001-07-23 19:10:52 +0000171
Owen Taylor3473f882001-02-23 17:55:21 +0000172/**
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000173 * xmlHTTPErrMemory:
174 * @extra: extra informations
175 *
176 * Handle an out of memory condition
177 */
178static void
179xmlHTTPErrMemory(const char *extra)
180{
181 __xmlSimpleError(XML_FROM_HTTP, XML_ERR_NO_MEMORY, NULL, NULL, extra);
182}
183
184/**
Owen Taylor3473f882001-02-23 17:55:21 +0000185 * A portability function
186 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000187static int socket_errno(void) {
Owen Taylor3473f882001-02-23 17:55:21 +0000188#ifdef _WINSOCKAPI_
189 return(WSAGetLastError());
190#else
191 return(errno);
192#endif
193}
194
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000195#ifdef SUPPORT_IP6
Daniel Veillard2db8c122003-07-08 12:16:59 +0000196static
197int have_ipv6(void) {
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000198 int s;
199
200 s = socket (AF_INET6, SOCK_STREAM, 0);
201 if (s != -1) {
202 close (s);
203 return (1);
204 }
205 return (0);
206}
207#endif
208
Owen Taylor3473f882001-02-23 17:55:21 +0000209/**
210 * xmlNanoHTTPInit:
211 *
212 * Initialize the HTTP protocol layer.
213 * Currently it just checks for proxy informations
214 */
215
216void
217xmlNanoHTTPInit(void) {
218 const char *env;
219#ifdef _WINSOCKAPI_
220 WSADATA wsaData;
221#endif
222
223 if (initialized)
224 return;
225
226#ifdef _WINSOCKAPI_
227 if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0)
228 return;
229#endif
230
231 if (proxy == NULL) {
232 proxyPort = 80;
233 env = getenv("no_proxy");
Daniel Veillard29b17482004-08-16 00:39:03 +0000234 if (env && ((env[0] == '*') && (env[1] == 0)))
Owen Taylor3473f882001-02-23 17:55:21 +0000235 goto done;
236 env = getenv("http_proxy");
237 if (env != NULL) {
238 xmlNanoHTTPScanProxy(env);
239 goto done;
240 }
241 env = getenv("HTTP_PROXY");
242 if (env != NULL) {
243 xmlNanoHTTPScanProxy(env);
244 goto done;
245 }
246 }
247done:
248 initialized = 1;
249}
250
251/**
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000252 * xmlNanoHTTPCleanup:
Owen Taylor3473f882001-02-23 17:55:21 +0000253 *
254 * Cleanup the HTTP protocol layer.
255 */
256
257void
258xmlNanoHTTPCleanup(void) {
Daniel Veillard744acff2005-07-12 15:09:53 +0000259 if (proxy != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +0000260 xmlFree(proxy);
Daniel Veillard744acff2005-07-12 15:09:53 +0000261 proxy = NULL;
262 }
Owen Taylor3473f882001-02-23 17:55:21 +0000263#ifdef _WINSOCKAPI_
264 if (initialized)
265 WSACleanup();
266#endif
267 initialized = 0;
268 return;
269}
270
271/**
Owen Taylor3473f882001-02-23 17:55:21 +0000272 * xmlNanoHTTPScanURL:
273 * @ctxt: an HTTP context
274 * @URL: The URL used to initialize the context
275 *
276 * (Re)Initialize an HTTP context by parsing the URL and finding
277 * the protocol host port and path it indicates.
278 */
279
280static void
281xmlNanoHTTPScanURL(xmlNanoHTTPCtxtPtr ctxt, const char *URL) {
William M. Brack015ccb22005-02-13 08:18:52 +0000282 xmlURIPtr uri;
283 /*
284 * Clear any existing data from the context
285 */
Owen Taylor3473f882001-02-23 17:55:21 +0000286 if (ctxt->protocol != NULL) {
287 xmlFree(ctxt->protocol);
288 ctxt->protocol = NULL;
289 }
290 if (ctxt->hostname != NULL) {
291 xmlFree(ctxt->hostname);
292 ctxt->hostname = NULL;
293 }
294 if (ctxt->path != NULL) {
295 xmlFree(ctxt->path);
296 ctxt->path = NULL;
297 }
Daniel Veillard351f2d62005-04-13 02:55:12 +0000298 if (ctxt->query != NULL) {
299 xmlFree(ctxt->query);
300 ctxt->query = NULL;
301 }
Owen Taylor3473f882001-02-23 17:55:21 +0000302 if (URL == NULL) return;
William M. Brack015ccb22005-02-13 08:18:52 +0000303
Daniel Veillard336a8e12005-08-07 10:46:19 +0000304 uri = xmlParseURIRaw(URL, 1);
William M. Brack015ccb22005-02-13 08:18:52 +0000305 if (uri == NULL)
306 return;
307
308 if ((uri->scheme == NULL) || (uri->server == NULL)) {
309 xmlFreeURI(uri);
310 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000311 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000312
William M. Brack015ccb22005-02-13 08:18:52 +0000313 ctxt->protocol = xmlMemStrdup(uri->scheme);
314 ctxt->hostname = xmlMemStrdup(uri->server);
315 if (uri->path != NULL)
316 ctxt->path = xmlMemStrdup(uri->path);
317 else
318 ctxt->path = xmlMemStrdup("/");
Daniel Veillard351f2d62005-04-13 02:55:12 +0000319 if (uri->query != NULL)
320 ctxt->query = xmlMemStrdup(uri->query);
William M. Brack015ccb22005-02-13 08:18:52 +0000321 if (uri->port != 0)
322 ctxt->port = uri->port;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000323
William M. Brack015ccb22005-02-13 08:18:52 +0000324 xmlFreeURI(uri);
Owen Taylor3473f882001-02-23 17:55:21 +0000325}
326
327/**
328 * xmlNanoHTTPScanProxy:
329 * @URL: The proxy URL used to initialize the proxy context
330 *
331 * (Re)Initialize the HTTP Proxy context by parsing the URL and finding
332 * the protocol host port it indicates.
333 * Should be like http://myproxy/ or http://myproxy:3128/
334 * A NULL URL cleans up proxy informations.
335 */
336
337void
338xmlNanoHTTPScanProxy(const char *URL) {
William M. Brack015ccb22005-02-13 08:18:52 +0000339 xmlURIPtr uri;
Owen Taylor3473f882001-02-23 17:55:21 +0000340
341 if (proxy != NULL) {
342 xmlFree(proxy);
343 proxy = NULL;
344 }
William M. Brack015ccb22005-02-13 08:18:52 +0000345 proxyPort = 0;
346
Owen Taylor3473f882001-02-23 17:55:21 +0000347#ifdef DEBUG_HTTP
348 if (URL == NULL)
349 xmlGenericError(xmlGenericErrorContext,
350 "Removing HTTP proxy info\n");
351 else
352 xmlGenericError(xmlGenericErrorContext,
353 "Using HTTP proxy %s\n", URL);
354#endif
355 if (URL == NULL) return;
William M. Brack015ccb22005-02-13 08:18:52 +0000356
Daniel Veillard336a8e12005-08-07 10:46:19 +0000357 uri = xmlParseURIRaw(URL, 1);
William M. Brack015ccb22005-02-13 08:18:52 +0000358 if ((uri == NULL) || (uri->scheme == NULL) ||
359 (strcmp(uri->scheme, "http")) || (uri->server == NULL)) {
360 __xmlIOErr(XML_FROM_HTTP, XML_HTTP_URL_SYNTAX, "Syntax Error\n");
361 if (uri != NULL)
362 xmlFreeURI(uri);
363 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000364 }
William M. Brack015ccb22005-02-13 08:18:52 +0000365
366 proxy = xmlMemStrdup(uri->server);
367 if (uri->port != 0)
368 proxyPort = uri->port;
Owen Taylor3473f882001-02-23 17:55:21 +0000369
William M. Brack015ccb22005-02-13 08:18:52 +0000370 xmlFreeURI(uri);
Owen Taylor3473f882001-02-23 17:55:21 +0000371}
372
373/**
374 * xmlNanoHTTPNewCtxt:
375 * @URL: The URL used to initialize the context
376 *
377 * Allocate and initialize a new HTTP context.
378 *
379 * Returns an HTTP context or NULL in case of error.
380 */
381
382static xmlNanoHTTPCtxtPtr
383xmlNanoHTTPNewCtxt(const char *URL) {
384 xmlNanoHTTPCtxtPtr ret;
385
386 ret = (xmlNanoHTTPCtxtPtr) xmlMalloc(sizeof(xmlNanoHTTPCtxt));
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000387 if (ret == NULL) {
388 xmlHTTPErrMemory("allocating context");
389 return(NULL);
390 }
Owen Taylor3473f882001-02-23 17:55:21 +0000391
392 memset(ret, 0, sizeof(xmlNanoHTTPCtxt));
393 ret->port = 80;
394 ret->returnValue = 0;
395 ret->fd = -1;
Daniel Veillardf012a642001-07-23 19:10:52 +0000396 ret->ContentLength = -1;
Owen Taylor3473f882001-02-23 17:55:21 +0000397
Daniel Veillardcacbe5d2003-01-10 16:09:51 +0000398 xmlNanoHTTPScanURL(ret, URL);
Owen Taylor3473f882001-02-23 17:55:21 +0000399
400 return(ret);
401}
402
403/**
404 * xmlNanoHTTPFreeCtxt:
405 * @ctxt: an HTTP context
406 *
407 * Frees the context after closing the connection.
408 */
409
410static void
411xmlNanoHTTPFreeCtxt(xmlNanoHTTPCtxtPtr ctxt) {
412 if (ctxt == NULL) return;
413 if (ctxt->hostname != NULL) xmlFree(ctxt->hostname);
414 if (ctxt->protocol != NULL) xmlFree(ctxt->protocol);
415 if (ctxt->path != NULL) xmlFree(ctxt->path);
Daniel Veillard351f2d62005-04-13 02:55:12 +0000416 if (ctxt->query != NULL) xmlFree(ctxt->query);
Owen Taylor3473f882001-02-23 17:55:21 +0000417 if (ctxt->out != NULL) xmlFree(ctxt->out);
418 if (ctxt->in != NULL) xmlFree(ctxt->in);
419 if (ctxt->contentType != NULL) xmlFree(ctxt->contentType);
Daniel Veillard847332a2003-10-18 11:29:40 +0000420 if (ctxt->encoding != NULL) xmlFree(ctxt->encoding);
Daniel Veillarda840b692003-10-19 13:35:37 +0000421 if (ctxt->mimeType != NULL) xmlFree(ctxt->mimeType);
Owen Taylor3473f882001-02-23 17:55:21 +0000422 if (ctxt->location != NULL) xmlFree(ctxt->location);
423 if (ctxt->authHeader != NULL) xmlFree(ctxt->authHeader);
Daniel Veillard9a2724d2005-12-15 11:12:26 +0000424#ifdef HAVE_ZLIB_H
425 if (ctxt->strm != NULL) {
426 inflateEnd(ctxt->strm);
427 xmlFree(ctxt->strm);
428 }
429#endif
430
Owen Taylor3473f882001-02-23 17:55:21 +0000431 ctxt->state = XML_NANO_HTTP_NONE;
432 if (ctxt->fd >= 0) closesocket(ctxt->fd);
433 ctxt->fd = -1;
434 xmlFree(ctxt);
435}
436
437/**
438 * xmlNanoHTTPSend:
439 * @ctxt: an HTTP context
440 *
441 * Send the input needed to initiate the processing on the server side
Daniel Veillardf012a642001-07-23 19:10:52 +0000442 * Returns number of bytes sent or -1 on error.
Owen Taylor3473f882001-02-23 17:55:21 +0000443 */
444
Daniel Veillardf012a642001-07-23 19:10:52 +0000445static int
Raphael Prevost48b60c32009-08-23 13:11:01 +0200446xmlNanoHTTPSend(xmlNanoHTTPCtxtPtr ctxt, const char *xmt_ptr, int outlen)
447{
448 int total_sent = 0;
449#ifdef HAVE_POLL_H
450 struct pollfd p;
451#else
452 struct timeval tv;
453 fd_set wfd;
454#endif
Daniel Veillardf012a642001-07-23 19:10:52 +0000455
Raphael Prevost48b60c32009-08-23 13:11:01 +0200456 if ((ctxt->state & XML_NANO_HTTP_WRITE) && (xmt_ptr != NULL)) {
Daniel Veillardf012a642001-07-23 19:10:52 +0000457 while (total_sent < outlen) {
458 int nsent = send(ctxt->fd, xmt_ptr + total_sent,
Raphael Prevost48b60c32009-08-23 13:11:01 +0200459 outlen - total_sent, 0);
460
461 if (nsent > 0)
Owen Taylor3473f882001-02-23 17:55:21 +0000462 total_sent += nsent;
Raphael Prevost48b60c32009-08-23 13:11:01 +0200463 else if ((nsent == -1) &&
Daniel Veillardba6db032001-07-31 16:25:45 +0000464#if defined(EAGAIN) && EAGAIN != EWOULDBLOCK
Raphael Prevost48b60c32009-08-23 13:11:01 +0200465 (socket_errno() != EAGAIN) &&
Daniel Veillardba6db032001-07-31 16:25:45 +0000466#endif
Raphael Prevost48b60c32009-08-23 13:11:01 +0200467 (socket_errno() != EWOULDBLOCK)) {
468 __xmlIOErr(XML_FROM_HTTP, 0, "send failed\n");
469 if (total_sent == 0)
470 total_sent = -1;
471 break;
472 } else {
473 /*
474 * No data sent
475 * Since non-blocking sockets are used, wait for
476 * socket to be writable or default timeout prior
477 * to retrying.
478 */
479#ifndef HAVE_POLL_H
480 if (ctxt->fd > FD_SETSIZE)
481 return -1;
Daniel Veillardf012a642001-07-23 19:10:52 +0000482
Raphael Prevost48b60c32009-08-23 13:11:01 +0200483 tv.tv_sec = timeout;
484 tv.tv_usec = 0;
485 FD_ZERO(&wfd);
Daniel Veillard9e2110b2005-08-08 20:33:54 +0000486#ifdef _MSC_VER
487#pragma warning(push)
488#pragma warning(disable: 4018)
489#endif
Raphael Prevost48b60c32009-08-23 13:11:01 +0200490 FD_SET(ctxt->fd, &wfd);
Daniel Veillard9e2110b2005-08-08 20:33:54 +0000491#ifdef _MSC_VER
492#pragma warning(pop)
493#endif
Raphael Prevost48b60c32009-08-23 13:11:01 +0200494 (void) select(ctxt->fd + 1, NULL, &wfd, NULL, &tv);
495#else
496 p.fd = ctxt->fd;
497 p.events = POLLOUT;
498 (void) poll(&p, 1, timeout * 1000);
499#endif /* !HAVE_POLL_H */
500 }
501 }
Owen Taylor3473f882001-02-23 17:55:21 +0000502 }
Daniel Veillardf012a642001-07-23 19:10:52 +0000503
504 return total_sent;
Owen Taylor3473f882001-02-23 17:55:21 +0000505}
506
507/**
508 * xmlNanoHTTPRecv:
509 * @ctxt: an HTTP context
510 *
511 * Read information coming from the HTTP connection.
512 * This is a blocking call (but it blocks in select(), not read()).
513 *
514 * Returns the number of byte read or -1 in case of error.
515 */
516
517static int
Raphael Prevost48b60c32009-08-23 13:11:01 +0200518xmlNanoHTTPRecv(xmlNanoHTTPCtxtPtr ctxt)
519{
520#ifdef HAVE_POLL_H
521 struct pollfd p;
522#else
Owen Taylor3473f882001-02-23 17:55:21 +0000523 fd_set rfd;
524 struct timeval tv;
Raphael Prevost48b60c32009-08-23 13:11:01 +0200525#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000526
527
528 while (ctxt->state & XML_NANO_HTTP_READ) {
Raphael Prevost48b60c32009-08-23 13:11:01 +0200529 if (ctxt->in == NULL) {
530 ctxt->in = (char *) xmlMallocAtomic(65000 * sizeof(char));
531 if (ctxt->in == NULL) {
532 xmlHTTPErrMemory("allocating input");
533 ctxt->last = -1;
534 return (-1);
535 }
536 ctxt->inlen = 65000;
537 ctxt->inptr = ctxt->content = ctxt->inrptr = ctxt->in;
538 }
539 if (ctxt->inrptr > ctxt->in + XML_NANO_HTTP_CHUNK) {
540 int delta = ctxt->inrptr - ctxt->in;
541 int len = ctxt->inptr - ctxt->inrptr;
Owen Taylor3473f882001-02-23 17:55:21 +0000542
Raphael Prevost48b60c32009-08-23 13:11:01 +0200543 memmove(ctxt->in, ctxt->inrptr, len);
544 ctxt->inrptr -= delta;
545 ctxt->content -= delta;
546 ctxt->inptr -= delta;
547 }
548 if ((ctxt->in + ctxt->inlen) < (ctxt->inptr + XML_NANO_HTTP_CHUNK)) {
549 int d_inptr = ctxt->inptr - ctxt->in;
550 int d_content = ctxt->content - ctxt->in;
551 int d_inrptr = ctxt->inrptr - ctxt->in;
552 char *tmp_ptr = ctxt->in;
553
554 ctxt->inlen *= 2;
Daniel Veillardf012a642001-07-23 19:10:52 +0000555 ctxt->in = (char *) xmlRealloc(tmp_ptr, ctxt->inlen);
Raphael Prevost48b60c32009-08-23 13:11:01 +0200556 if (ctxt->in == NULL) {
557 xmlHTTPErrMemory("allocating input buffer");
558 xmlFree(tmp_ptr);
559 ctxt->last = -1;
560 return (-1);
561 }
Owen Taylor3473f882001-02-23 17:55:21 +0000562 ctxt->inptr = ctxt->in + d_inptr;
563 ctxt->content = ctxt->in + d_content;
564 ctxt->inrptr = ctxt->in + d_inrptr;
Raphael Prevost48b60c32009-08-23 13:11:01 +0200565 }
566 ctxt->last = recv(ctxt->fd, ctxt->inptr, XML_NANO_HTTP_CHUNK, 0);
567 if (ctxt->last > 0) {
568 ctxt->inptr += ctxt->last;
569 return (ctxt->last);
570 }
571 if (ctxt->last == 0) {
572 return (0);
573 }
574 if (ctxt->last == -1) {
575 switch (socket_errno()) {
576 case EINPROGRESS:
577 case EWOULDBLOCK:
Owen Taylor3473f882001-02-23 17:55:21 +0000578#if defined(EAGAIN) && EAGAIN != EWOULDBLOCK
Raphael Prevost48b60c32009-08-23 13:11:01 +0200579 case EAGAIN:
Owen Taylor3473f882001-02-23 17:55:21 +0000580#endif
Raphael Prevost48b60c32009-08-23 13:11:01 +0200581 break;
Daniel Veillardf012a642001-07-23 19:10:52 +0000582
Raphael Prevost48b60c32009-08-23 13:11:01 +0200583 case ECONNRESET:
584 case ESHUTDOWN:
585 return (0);
Daniel Veillardf012a642001-07-23 19:10:52 +0000586
Raphael Prevost48b60c32009-08-23 13:11:01 +0200587 default:
588 __xmlIOErr(XML_FROM_HTTP, 0, "recv failed\n");
589 return (-1);
590 }
591 }
592#ifdef HAVE_POLL_H
593 p.fd = ctxt->fd;
594 p.events = POLLIN;
595 if ((poll(&p, 1, timeout * 1000) < 1)
596#if defined(EINTR)
597 && (errno != EINTR)
598#endif
599 )
600 return (0);
601#else /* !HAVE_POLL_H */
602 if (ctxt->fd > FD_SETSIZE)
603 return 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000604
Raphael Prevost48b60c32009-08-23 13:11:01 +0200605 tv.tv_sec = timeout;
606 tv.tv_usec = 0;
607 FD_ZERO(&rfd);
608
Daniel Veillard9e2110b2005-08-08 20:33:54 +0000609#ifdef _MSC_VER
610#pragma warning(push)
611#pragma warning(disable: 4018)
612#endif
Raphael Prevost48b60c32009-08-23 13:11:01 +0200613
614 FD_SET(ctxt->fd, &rfd);
615
Daniel Veillard9e2110b2005-08-08 20:33:54 +0000616#ifdef _MSC_VER
617#pragma warning(pop)
618#endif
Raphael Prevost48b60c32009-08-23 13:11:01 +0200619
620 if ((select(ctxt->fd + 1, &rfd, NULL, NULL, &tv) < 1)
Daniel Veillard50f34372001-08-03 12:06:36 +0000621#if defined(EINTR)
Raphael Prevost48b60c32009-08-23 13:11:01 +0200622 && (errno != EINTR)
Daniel Veillard50f34372001-08-03 12:06:36 +0000623#endif
Raphael Prevost48b60c32009-08-23 13:11:01 +0200624 )
625 return (0);
626#endif /* !HAVE_POLL_H */
Owen Taylor3473f882001-02-23 17:55:21 +0000627 }
Raphael Prevost48b60c32009-08-23 13:11:01 +0200628 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +0000629}
630
631/**
632 * xmlNanoHTTPReadLine:
633 * @ctxt: an HTTP context
634 *
635 * Read one line in the HTTP server output, usually for extracting
636 * the HTTP protocol informations from the answer header.
637 *
638 * Returns a newly allocated string with a copy of the line, or NULL
639 * which indicate the end of the input.
640 */
641
642static char *
643xmlNanoHTTPReadLine(xmlNanoHTTPCtxtPtr ctxt) {
644 char buf[4096];
645 char *bp = buf;
Daniel Veillardf012a642001-07-23 19:10:52 +0000646 int rc;
Owen Taylor3473f882001-02-23 17:55:21 +0000647
648 while (bp - buf < 4095) {
649 if (ctxt->inrptr == ctxt->inptr) {
Daniel Veillardf012a642001-07-23 19:10:52 +0000650 if ( (rc = xmlNanoHTTPRecv(ctxt)) == 0) {
Owen Taylor3473f882001-02-23 17:55:21 +0000651 if (bp == buf)
652 return(NULL);
653 else
654 *bp = 0;
655 return(xmlMemStrdup(buf));
656 }
Daniel Veillardf012a642001-07-23 19:10:52 +0000657 else if ( rc == -1 ) {
658 return ( NULL );
659 }
Owen Taylor3473f882001-02-23 17:55:21 +0000660 }
661 *bp = *ctxt->inrptr++;
662 if (*bp == '\n') {
663 *bp = 0;
664 return(xmlMemStrdup(buf));
665 }
666 if (*bp != '\r')
667 bp++;
668 }
669 buf[4095] = 0;
670 return(xmlMemStrdup(buf));
671}
672
673
674/**
675 * xmlNanoHTTPScanAnswer:
676 * @ctxt: an HTTP context
677 * @line: an HTTP header line
678 *
679 * Try to extract useful informations from the server answer.
680 * We currently parse and process:
681 * - The HTTP revision/ return code
Daniel Veillarda840b692003-10-19 13:35:37 +0000682 * - The Content-Type, Mime-Type and charset used
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000683 * - The Location for redirect processing.
Owen Taylor3473f882001-02-23 17:55:21 +0000684 *
685 * Returns -1 in case of failure, the file descriptor number otherwise
686 */
687
688static void
689xmlNanoHTTPScanAnswer(xmlNanoHTTPCtxtPtr ctxt, const char *line) {
690 const char *cur = line;
691
692 if (line == NULL) return;
693
694 if (!strncmp(line, "HTTP/", 5)) {
695 int version = 0;
696 int ret = 0;
697
698 cur += 5;
699 while ((*cur >= '0') && (*cur <= '9')) {
700 version *= 10;
701 version += *cur - '0';
702 cur++;
703 }
704 if (*cur == '.') {
705 cur++;
706 if ((*cur >= '0') && (*cur <= '9')) {
707 version *= 10;
708 version += *cur - '0';
709 cur++;
710 }
711 while ((*cur >= '0') && (*cur <= '9'))
712 cur++;
713 } else
714 version *= 10;
715 if ((*cur != ' ') && (*cur != '\t')) return;
716 while ((*cur == ' ') || (*cur == '\t')) cur++;
717 if ((*cur < '0') || (*cur > '9')) return;
718 while ((*cur >= '0') && (*cur <= '9')) {
719 ret *= 10;
720 ret += *cur - '0';
721 cur++;
722 }
723 if ((*cur != 0) && (*cur != ' ') && (*cur != '\t')) return;
724 ctxt->returnValue = ret;
725 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Content-Type:", 13)) {
Daniel Veillarda840b692003-10-19 13:35:37 +0000726 const xmlChar *charset, *last, *mime;
Owen Taylor3473f882001-02-23 17:55:21 +0000727 cur += 13;
728 while ((*cur == ' ') || (*cur == '\t')) cur++;
729 if (ctxt->contentType != NULL)
730 xmlFree(ctxt->contentType);
731 ctxt->contentType = xmlMemStrdup(cur);
Daniel Veillarda840b692003-10-19 13:35:37 +0000732 mime = (const xmlChar *) cur;
733 last = mime;
734 while ((*last != 0) && (*last != ' ') && (*last != '\t') &&
735 (*last != ';') && (*last != ','))
736 last++;
737 if (ctxt->mimeType != NULL)
738 xmlFree(ctxt->mimeType);
739 ctxt->mimeType = (char *) xmlStrndup(mime, last - mime);
740 charset = xmlStrstr(BAD_CAST ctxt->contentType, BAD_CAST "charset=");
741 if (charset != NULL) {
742 charset += 8;
743 last = charset;
744 while ((*last != 0) && (*last != ' ') && (*last != '\t') &&
745 (*last != ';') && (*last != ','))
746 last++;
747 if (ctxt->encoding != NULL)
748 xmlFree(ctxt->encoding);
749 ctxt->encoding = (char *) xmlStrndup(charset, last - charset);
750 }
Owen Taylor3473f882001-02-23 17:55:21 +0000751 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"ContentType:", 12)) {
Daniel Veillarda840b692003-10-19 13:35:37 +0000752 const xmlChar *charset, *last, *mime;
Owen Taylor3473f882001-02-23 17:55:21 +0000753 cur += 12;
754 if (ctxt->contentType != NULL) return;
755 while ((*cur == ' ') || (*cur == '\t')) cur++;
756 ctxt->contentType = xmlMemStrdup(cur);
Daniel Veillarda840b692003-10-19 13:35:37 +0000757 mime = (const xmlChar *) cur;
758 last = mime;
759 while ((*last != 0) && (*last != ' ') && (*last != '\t') &&
760 (*last != ';') && (*last != ','))
761 last++;
762 if (ctxt->mimeType != NULL)
763 xmlFree(ctxt->mimeType);
764 ctxt->mimeType = (char *) xmlStrndup(mime, last - mime);
765 charset = xmlStrstr(BAD_CAST ctxt->contentType, BAD_CAST "charset=");
766 if (charset != NULL) {
767 charset += 8;
768 last = charset;
769 while ((*last != 0) && (*last != ' ') && (*last != '\t') &&
770 (*last != ';') && (*last != ','))
771 last++;
772 if (ctxt->encoding != NULL)
773 xmlFree(ctxt->encoding);
774 ctxt->encoding = (char *) xmlStrndup(charset, last - charset);
775 }
Owen Taylor3473f882001-02-23 17:55:21 +0000776 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Location:", 9)) {
777 cur += 9;
778 while ((*cur == ' ') || (*cur == '\t')) cur++;
779 if (ctxt->location != NULL)
780 xmlFree(ctxt->location);
William M. Brack7e29c0a2004-04-02 09:07:22 +0000781 if (*cur == '/') {
782 xmlChar *tmp_http = xmlStrdup(BAD_CAST "http://");
783 xmlChar *tmp_loc =
784 xmlStrcat(tmp_http, (const xmlChar *) ctxt->hostname);
785 ctxt->location =
786 (char *) xmlStrcat (tmp_loc, (const xmlChar *) cur);
787 } else {
788 ctxt->location = xmlMemStrdup(cur);
789 }
Owen Taylor3473f882001-02-23 17:55:21 +0000790 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"WWW-Authenticate:", 17)) {
791 cur += 17;
792 while ((*cur == ' ') || (*cur == '\t')) cur++;
793 if (ctxt->authHeader != NULL)
794 xmlFree(ctxt->authHeader);
795 ctxt->authHeader = xmlMemStrdup(cur);
796 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Proxy-Authenticate:", 19)) {
797 cur += 19;
798 while ((*cur == ' ') || (*cur == '\t')) cur++;
799 if (ctxt->authHeader != NULL)
800 xmlFree(ctxt->authHeader);
801 ctxt->authHeader = xmlMemStrdup(cur);
Daniel Veillard9a2724d2005-12-15 11:12:26 +0000802#ifdef HAVE_ZLIB_H
803 } else if ( !xmlStrncasecmp( BAD_CAST line, BAD_CAST"Content-Encoding:", 17) ) {
804 cur += 17;
805 while ((*cur == ' ') || (*cur == '\t')) cur++;
806 if ( !xmlStrncasecmp( BAD_CAST cur, BAD_CAST"gzip", 4) ) {
807 ctxt->usesGzip = 1;
808
809 ctxt->strm = xmlMalloc(sizeof(z_stream));
810
811 if (ctxt->strm != NULL) {
812 ctxt->strm->zalloc = Z_NULL;
813 ctxt->strm->zfree = Z_NULL;
814 ctxt->strm->opaque = Z_NULL;
815 ctxt->strm->avail_in = 0;
816 ctxt->strm->next_in = Z_NULL;
817
818 inflateInit2( ctxt->strm, 31 );
819 }
820 }
821#endif
Daniel Veillardf012a642001-07-23 19:10:52 +0000822 } else if ( !xmlStrncasecmp( BAD_CAST line, BAD_CAST"Content-Length:", 15) ) {
823 cur += 15;
824 ctxt->ContentLength = strtol( cur, NULL, 10 );
Owen Taylor3473f882001-02-23 17:55:21 +0000825 }
826}
827
828/**
829 * xmlNanoHTTPConnectAttempt:
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000830 * @addr: a socket address structure
Owen Taylor3473f882001-02-23 17:55:21 +0000831 *
832 * Attempt a connection to the given IP:port endpoint. It forces
833 * non-blocking semantic on the socket, and allow 60 seconds for
834 * the host to answer.
835 *
836 * Returns -1 in case of failure, the file descriptor number otherwise
837 */
838
839static int
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000840xmlNanoHTTPConnectAttempt(struct sockaddr *addr)
Owen Taylor3473f882001-02-23 17:55:21 +0000841{
Raphael Prevost48b60c32009-08-23 13:11:01 +0200842#ifndef HAVE_POLL_H
Owen Taylor3473f882001-02-23 17:55:21 +0000843 fd_set wfd;
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000844#ifdef _WINSOCKAPI_
845 fd_set xfd;
846#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000847 struct timeval tv;
Raphael Prevost48b60c32009-08-23 13:11:01 +0200848#else /* !HAVE_POLL_H */
849 struct pollfd p;
850#endif /* !HAVE_POLL_H */
Owen Taylor3473f882001-02-23 17:55:21 +0000851 int status;
Raphael Prevost48b60c32009-08-23 13:11:01 +0200852
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000853 int addrlen;
Raphael Prevost48b60c32009-08-23 13:11:01 +0200854
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000855 SOCKET s;
Raphael Prevost48b60c32009-08-23 13:11:01 +0200856
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000857#ifdef SUPPORT_IP6
858 if (addr->sa_family == AF_INET6) {
Raphael Prevost48b60c32009-08-23 13:11:01 +0200859 s = socket(PF_INET6, SOCK_STREAM, IPPROTO_TCP);
860 addrlen = sizeof(struct sockaddr_in6);
861 } else
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000862#endif
863 {
Raphael Prevost48b60c32009-08-23 13:11:01 +0200864 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
865 addrlen = sizeof(struct sockaddr_in);
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000866 }
Raphael Prevost48b60c32009-08-23 13:11:01 +0200867 if (s == -1) {
Owen Taylor3473f882001-02-23 17:55:21 +0000868#ifdef DEBUG_HTTP
Raphael Prevost48b60c32009-08-23 13:11:01 +0200869 perror("socket");
Owen Taylor3473f882001-02-23 17:55:21 +0000870#endif
Raphael Prevost48b60c32009-08-23 13:11:01 +0200871 __xmlIOErr(XML_FROM_HTTP, 0, "socket failed\n");
872 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +0000873 }
Owen Taylor3473f882001-02-23 17:55:21 +0000874#ifdef _WINSOCKAPI_
875 {
Raphael Prevost48b60c32009-08-23 13:11:01 +0200876 u_long one = 1;
Owen Taylor3473f882001-02-23 17:55:21 +0000877
Raphael Prevost48b60c32009-08-23 13:11:01 +0200878 status = ioctlsocket(s, FIONBIO, &one) == SOCKET_ERROR ? -1 : 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000879 }
880#else /* _WINSOCKAPI_ */
881#if defined(VMS)
882 {
Raphael Prevost48b60c32009-08-23 13:11:01 +0200883 int enable = 1;
884
885 status = ioctl(s, FIONBIO, &enable);
Owen Taylor3473f882001-02-23 17:55:21 +0000886 }
887#else /* VMS */
Daniel Veillardcba68392008-08-29 12:43:40 +0000888#if defined(__BEOS__) && !defined(__HAIKU__)
Raphael Prevost48b60c32009-08-23 13:11:01 +0200889 {
890 bool noblock = true;
891
892 status =
893 setsockopt(s, SOL_SOCKET, SO_NONBLOCK, &noblock,
894 sizeof(noblock));
895 }
Daniel Veillard254b1262003-11-01 17:04:58 +0000896#else /* __BEOS__ */
Owen Taylor3473f882001-02-23 17:55:21 +0000897 if ((status = fcntl(s, F_GETFL, 0)) != -1) {
898#ifdef O_NONBLOCK
Raphael Prevost48b60c32009-08-23 13:11:01 +0200899 status |= O_NONBLOCK;
Owen Taylor3473f882001-02-23 17:55:21 +0000900#else /* O_NONBLOCK */
901#ifdef F_NDELAY
Raphael Prevost48b60c32009-08-23 13:11:01 +0200902 status |= F_NDELAY;
Owen Taylor3473f882001-02-23 17:55:21 +0000903#endif /* F_NDELAY */
904#endif /* !O_NONBLOCK */
Raphael Prevost48b60c32009-08-23 13:11:01 +0200905 status = fcntl(s, F_SETFL, status);
Owen Taylor3473f882001-02-23 17:55:21 +0000906 }
907 if (status < 0) {
908#ifdef DEBUG_HTTP
Raphael Prevost48b60c32009-08-23 13:11:01 +0200909 perror("nonblocking");
Owen Taylor3473f882001-02-23 17:55:21 +0000910#endif
Raphael Prevost48b60c32009-08-23 13:11:01 +0200911 __xmlIOErr(XML_FROM_HTTP, 0, "error setting non-blocking IO\n");
912 closesocket(s);
913 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +0000914 }
Daniel Veillard254b1262003-11-01 17:04:58 +0000915#endif /* !__BEOS__ */
Owen Taylor3473f882001-02-23 17:55:21 +0000916#endif /* !VMS */
917#endif /* !_WINSOCKAPI_ */
918
Raphael Prevost48b60c32009-08-23 13:11:01 +0200919 if (connect(s, addr, addrlen) == -1) {
920 switch (socket_errno()) {
921 case EINPROGRESS:
922 case EWOULDBLOCK:
923 break;
924 default:
925 __xmlIOErr(XML_FROM_HTTP, 0,
926 "error connecting to HTTP server");
927 closesocket(s);
928 return (-1);
929 }
930 }
931#ifndef HAVE_POLL_H
Owen Taylor3473f882001-02-23 17:55:21 +0000932 tv.tv_sec = timeout;
933 tv.tv_usec = 0;
Daniel Veillard9e2110b2005-08-08 20:33:54 +0000934
935#ifdef _MSC_VER
936#pragma warning(push)
937#pragma warning(disable: 4018)
938#endif
Raphael Prevost48b60c32009-08-23 13:11:01 +0200939 if (s > FD_SETSIZE)
940 return -1;
Owen Taylor3473f882001-02-23 17:55:21 +0000941 FD_ZERO(&wfd);
942 FD_SET(s, &wfd);
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000943
Raphael Prevost48b60c32009-08-23 13:11:01 +0200944#ifdef _WINSOCKAPI_
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000945 FD_ZERO(&xfd);
946 FD_SET(s, &xfd);
Raphael Prevost48b60c32009-08-23 13:11:01 +0200947
948 switch (select(s + 1, NULL, &wfd, &xfd, &tv))
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000949#else
Raphael Prevost48b60c32009-08-23 13:11:01 +0200950 switch (select(s + 1, NULL, &wfd, NULL, &tv))
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000951#endif
Daniel Veillard9e2110b2005-08-08 20:33:54 +0000952#ifdef _MSC_VER
953#pragma warning(pop)
954#endif
Raphael Prevost48b60c32009-08-23 13:11:01 +0200955
956#else /* !HAVE_POLL_H */
957 p.fd = s;
958 p.events = POLLOUT;
959 switch (poll(&p, 1, timeout * 1000))
960#endif /* !HAVE_POLL_H */
961
Owen Taylor3473f882001-02-23 17:55:21 +0000962 {
Raphael Prevost48b60c32009-08-23 13:11:01 +0200963 case 0:
964 /* Time out */
965 __xmlIOErr(XML_FROM_HTTP, 0, "Connect attempt timed out");
966 closesocket(s);
967 return (-1);
968 case -1:
969 /* Ermm.. ?? */
970 __xmlIOErr(XML_FROM_HTTP, 0, "Connect failed");
971 closesocket(s);
972 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +0000973 }
974
Raphael Prevost48b60c32009-08-23 13:11:01 +0200975#ifndef HAVE_POLL_H
976 if (FD_ISSET(s, &wfd)
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000977#ifdef _WINSOCKAPI_
Raphael Prevost48b60c32009-08-23 13:11:01 +0200978 || FD_ISSET(s, &xfd)
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000979#endif
Raphael Prevost48b60c32009-08-23 13:11:01 +0200980 )
981#else /* !HAVE_POLL_H */
982 if (p.revents == POLLOUT)
983#endif /* !HAVE_POLL_H */
984 {
985 XML_SOCKLEN_T len;
986
987 len = sizeof(status);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000988#ifdef SO_ERROR
Raphael Prevost48b60c32009-08-23 13:11:01 +0200989 if (getsockopt(s, SOL_SOCKET, SO_ERROR, (char *) &status, &len) <
990 0) {
991 /* Solaris error code */
992 __xmlIOErr(XML_FROM_HTTP, 0, "getsockopt failed\n");
993 return (-1);
994 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000995#endif
Raphael Prevost48b60c32009-08-23 13:11:01 +0200996 if (status) {
997 __xmlIOErr(XML_FROM_HTTP, 0,
998 "Error connecting to remote host");
999 closesocket(s);
1000 errno = status;
1001 return (-1);
1002 }
Owen Taylor3473f882001-02-23 17:55:21 +00001003 } else {
Raphael Prevost48b60c32009-08-23 13:11:01 +02001004 /* pbm */
1005 __xmlIOErr(XML_FROM_HTTP, 0, "select failed\n");
1006 closesocket(s);
1007 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001008 }
Raphael Prevost48b60c32009-08-23 13:11:01 +02001009
1010 return (s);
Owen Taylor3473f882001-02-23 17:55:21 +00001011}
Raphael Prevost48b60c32009-08-23 13:11:01 +02001012
Owen Taylor3473f882001-02-23 17:55:21 +00001013/**
1014 * xmlNanoHTTPConnectHost:
1015 * @host: the host name
1016 * @port: the port number
1017 *
1018 * Attempt a connection to the given host:port endpoint. It tries
1019 * the multiple IP provided by the DNS if available.
1020 *
1021 * Returns -1 in case of failure, the file descriptor number otherwise
1022 */
1023
1024static int
1025xmlNanoHTTPConnectHost(const char *host, int port)
1026{
1027 struct hostent *h;
Daniel Veillard2db8c122003-07-08 12:16:59 +00001028 struct sockaddr *addr = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001029 struct in_addr ia;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001030 struct sockaddr_in sockin;
Daniel Veillard5c396542002-03-15 07:57:50 +00001031
Owen Taylor3473f882001-02-23 17:55:21 +00001032#ifdef SUPPORT_IP6
1033 struct in6_addr ia6;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001034 struct sockaddr_in6 sockin6;
Owen Taylor3473f882001-02-23 17:55:21 +00001035#endif
1036 int i;
1037 int s;
Daniel Veillard5c396542002-03-15 07:57:50 +00001038
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001039 memset (&sockin, 0, sizeof(sockin));
1040#ifdef SUPPORT_IP6
1041 memset (&sockin6, 0, sizeof(sockin6));
Rob Richardscb418de2005-10-13 23:12:42 +00001042#endif
1043
1044#if !defined(HAVE_GETADDRINFO) && defined(SUPPORT_IP6) && defined(RES_USE_INET6)
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001045 if (have_ipv6 ())
Daniel Veillard560c2a42003-07-06 21:13:49 +00001046 {
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001047 if (!(_res.options & RES_INIT))
1048 res_init();
1049 _res.options |= RES_USE_INET6;
1050 }
Rob Richardscb418de2005-10-13 23:12:42 +00001051#endif
1052
1053#if defined(HAVE_GETADDRINFO) && defined(SUPPORT_IP6) && !defined(_WIN32)
1054 if (have_ipv6 ())
1055#endif
1056#if defined(HAVE_GETADDRINFO) && (defined(SUPPORT_IP6) || defined(_WIN32))
Daniel Veillard560c2a42003-07-06 21:13:49 +00001057 {
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001058 int status;
1059 struct addrinfo hints, *res, *result;
1060
1061 result = NULL;
1062 memset (&hints, 0,sizeof(hints));
1063 hints.ai_socktype = SOCK_STREAM;
1064
1065 status = getaddrinfo (host, NULL, &hints, &result);
1066 if (status) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001067 __xmlIOErr(XML_FROM_HTTP, 0, "getaddrinfo failed\n");
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001068 return (-1);
1069 }
1070
1071 for (res = result; res; res = res->ai_next) {
Rob Richardscb418de2005-10-13 23:12:42 +00001072 if (res->ai_family == AF_INET) {
1073 if (res->ai_addrlen > sizeof(sockin)) {
1074 __xmlIOErr(XML_FROM_HTTP, 0, "address size mismatch\n");
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001075 freeaddrinfo (result);
Rob Richardscb418de2005-10-13 23:12:42 +00001076 return (-1);
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001077 }
Rob Richardscb418de2005-10-13 23:12:42 +00001078 memcpy (&sockin, res->ai_addr, res->ai_addrlen);
1079 sockin.sin_port = htons (port);
1080 addr = (struct sockaddr *)&sockin;
1081#ifdef SUPPORT_IP6
1082 } else if (have_ipv6 () && (res->ai_family == AF_INET6)) {
1083 if (res->ai_addrlen > sizeof(sockin6)) {
1084 __xmlIOErr(XML_FROM_HTTP, 0, "address size mismatch\n");
1085 freeaddrinfo (result);
1086 return (-1);
1087 }
1088 memcpy (&sockin6, res->ai_addr, res->ai_addrlen);
1089 sockin6.sin6_port = htons (port);
1090 addr = (struct sockaddr *)&sockin6;
1091#endif
1092 } else
1093 continue; /* for */
1094
1095 s = xmlNanoHTTPConnectAttempt (addr);
1096 if (s != -1) {
1097 freeaddrinfo (result);
1098 return (s);
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001099 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001100 }
Rob Richardscb418de2005-10-13 23:12:42 +00001101
Daniel Veillard3dc93a42003-07-10 14:04:33 +00001102 if (result)
1103 freeaddrinfo (result);
Rob Richardscb418de2005-10-13 23:12:42 +00001104 }
Owen Taylor3473f882001-02-23 17:55:21 +00001105#endif
Rob Richardscb418de2005-10-13 23:12:42 +00001106#if defined(HAVE_GETADDRINFO) && defined(SUPPORT_IP6) && !defined(_WIN32)
1107 else
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001108#endif
Rob Richardscb418de2005-10-13 23:12:42 +00001109#if !defined(HAVE_GETADDRINFO) || !defined(_WIN32)
1110 {
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001111 h = gethostbyname (host);
1112 if (h == NULL) {
Daniel Veillard56b2db72002-03-25 16:35:28 +00001113
1114/*
1115 * Okay, I got fed up by the non-portability of this error message
1116 * extraction code. it work on Linux, if it work on your platform
1117 * and one want to enable it, send me the defined(foobar) needed
1118 */
1119#if defined(HAVE_NETDB_H) && defined(HOST_NOT_FOUND) && defined(linux)
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001120 const char *h_err_txt = "";
Daniel Veillardf012a642001-07-23 19:10:52 +00001121
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001122 switch (h_errno) {
1123 case HOST_NOT_FOUND:
1124 h_err_txt = "Authoritive host not found";
1125 break;
Daniel Veillardf012a642001-07-23 19:10:52 +00001126
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001127 case TRY_AGAIN:
1128 h_err_txt =
1129 "Non-authoritive host not found or server failure.";
1130 break;
Daniel Veillardf012a642001-07-23 19:10:52 +00001131
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001132 case NO_RECOVERY:
1133 h_err_txt =
1134 "Non-recoverable errors: FORMERR, REFUSED, or NOTIMP.";
1135 break;
Daniel Veillard5c396542002-03-15 07:57:50 +00001136
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001137 case NO_ADDRESS:
1138 h_err_txt =
1139 "Valid name, no data record of requested type.";
1140 break;
Daniel Veillard5c396542002-03-15 07:57:50 +00001141
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001142 default:
1143 h_err_txt = "No error text defined.";
1144 break;
1145 }
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001146 __xmlIOErr(XML_FROM_HTTP, 0, h_err_txt);
Daniel Veillard5c396542002-03-15 07:57:50 +00001147#else
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001148 __xmlIOErr(XML_FROM_HTTP, 0, "Failed to resolve host");
Owen Taylor3473f882001-02-23 17:55:21 +00001149#endif
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001150 return (-1);
1151 }
Daniel Veillard5c396542002-03-15 07:57:50 +00001152
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001153 for (i = 0; h->h_addr_list[i]; i++) {
1154 if (h->h_addrtype == AF_INET) {
1155 /* A records (IPv4) */
Daniel Veillard8e2c9792004-10-27 09:39:50 +00001156 if ((unsigned int) h->h_length > sizeof(ia)) {
1157 __xmlIOErr(XML_FROM_HTTP, 0, "address size mismatch\n");
1158 return (-1);
1159 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001160 memcpy (&ia, h->h_addr_list[i], h->h_length);
1161 sockin.sin_family = h->h_addrtype;
1162 sockin.sin_addr = ia;
Daniel Veillard9e2110b2005-08-08 20:33:54 +00001163 sockin.sin_port = (u_short)htons ((unsigned short)port);
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001164 addr = (struct sockaddr *) &sockin;
Daniel Veillard5c396542002-03-15 07:57:50 +00001165#ifdef SUPPORT_IP6
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001166 } else if (have_ipv6 () && (h->h_addrtype == AF_INET6)) {
1167 /* AAAA records (IPv6) */
Daniel Veillard8e2c9792004-10-27 09:39:50 +00001168 if ((unsigned int) h->h_length > sizeof(ia6)) {
1169 __xmlIOErr(XML_FROM_HTTP, 0, "address size mismatch\n");
1170 return (-1);
1171 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001172 memcpy (&ia6, h->h_addr_list[i], h->h_length);
1173 sockin6.sin6_family = h->h_addrtype;
1174 sockin6.sin6_addr = ia6;
1175 sockin6.sin6_port = htons (port);
1176 addr = (struct sockaddr *) &sockin6;
Daniel Veillard5c396542002-03-15 07:57:50 +00001177#endif
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001178 } else
1179 break; /* for */
Daniel Veillard5c396542002-03-15 07:57:50 +00001180
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001181 s = xmlNanoHTTPConnectAttempt (addr);
1182 if (s != -1)
1183 return (s);
1184 }
Owen Taylor3473f882001-02-23 17:55:21 +00001185 }
Rob Richardscb418de2005-10-13 23:12:42 +00001186#endif
1187
Owen Taylor3473f882001-02-23 17:55:21 +00001188#ifdef DEBUG_HTTP
1189 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard5c396542002-03-15 07:57:50 +00001190 "xmlNanoHTTPConnectHost: unable to connect to '%s'.\n",
1191 host);
Owen Taylor3473f882001-02-23 17:55:21 +00001192#endif
Daniel Veillard5c396542002-03-15 07:57:50 +00001193 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001194}
1195
1196
1197/**
1198 * xmlNanoHTTPOpen:
1199 * @URL: The URL to load
1200 * @contentType: if available the Content-Type information will be
1201 * returned at that location
1202 *
1203 * This function try to open a connection to the indicated resource
1204 * via HTTP GET.
1205 *
1206 * Returns NULL in case of failure, otherwise a request handler.
1207 * The contentType, if provided must be freed by the caller
1208 */
1209
1210void*
1211xmlNanoHTTPOpen(const char *URL, char **contentType) {
1212 if (contentType != NULL) *contentType = NULL;
Daniel Veillardf012a642001-07-23 19:10:52 +00001213 return(xmlNanoHTTPMethod(URL, NULL, NULL, contentType, NULL, 0));
Daniel Veillard9403a042001-05-28 11:00:53 +00001214}
1215
1216/**
1217 * xmlNanoHTTPOpenRedir:
1218 * @URL: The URL to load
1219 * @contentType: if available the Content-Type information will be
1220 * returned at that location
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001221 * @redir: if available the redirected URL will be returned
Daniel Veillard9403a042001-05-28 11:00:53 +00001222 *
1223 * This function try to open a connection to the indicated resource
1224 * via HTTP GET.
1225 *
1226 * Returns NULL in case of failure, otherwise a request handler.
1227 * The contentType, if provided must be freed by the caller
1228 */
1229
1230void*
1231xmlNanoHTTPOpenRedir(const char *URL, char **contentType, char **redir) {
1232 if (contentType != NULL) *contentType = NULL;
1233 if (redir != NULL) *redir = NULL;
Daniel Veillardf012a642001-07-23 19:10:52 +00001234 return(xmlNanoHTTPMethodRedir(URL, NULL, NULL, contentType, redir, NULL,0));
Owen Taylor3473f882001-02-23 17:55:21 +00001235}
1236
1237/**
1238 * xmlNanoHTTPRead:
1239 * @ctx: the HTTP context
1240 * @dest: a buffer
1241 * @len: the buffer length
1242 *
1243 * This function tries to read @len bytes from the existing HTTP connection
1244 * and saves them in @dest. This is a blocking call.
1245 *
1246 * Returns the number of byte read. 0 is an indication of an end of connection.
1247 * -1 indicates a parameter error.
1248 */
1249int
1250xmlNanoHTTPRead(void *ctx, void *dest, int len) {
1251 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
Daniel Veillard9a2724d2005-12-15 11:12:26 +00001252#ifdef HAVE_ZLIB_H
1253 int bytes_read = 0;
1254 int orig_avail_in;
1255 int z_ret;
1256#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001257
1258 if (ctx == NULL) return(-1);
1259 if (dest == NULL) return(-1);
1260 if (len <= 0) return(0);
1261
Daniel Veillard9a2724d2005-12-15 11:12:26 +00001262#ifdef HAVE_ZLIB_H
1263 if (ctxt->usesGzip == 1) {
1264 if (ctxt->strm == NULL) return(0);
1265
1266 ctxt->strm->next_out = dest;
1267 ctxt->strm->avail_out = len;
William M. Bracke8827652007-05-16 05:19:13 +00001268 ctxt->strm->avail_in = ctxt->inptr - ctxt->inrptr;
Daniel Veillard9a2724d2005-12-15 11:12:26 +00001269
William M. Bracke8827652007-05-16 05:19:13 +00001270 while (ctxt->strm->avail_out > 0 &&
1271 (ctxt->strm->avail_in > 0 || xmlNanoHTTPRecv(ctxt) > 0)) {
William M. Brackd2f682a2007-05-15 19:42:08 +00001272 orig_avail_in = ctxt->strm->avail_in =
1273 ctxt->inptr - ctxt->inrptr - bytes_read;
Daniel Veillard9a2724d2005-12-15 11:12:26 +00001274 ctxt->strm->next_in = BAD_CAST (ctxt->inrptr + bytes_read);
1275
1276 z_ret = inflate(ctxt->strm, Z_NO_FLUSH);
1277 bytes_read += orig_avail_in - ctxt->strm->avail_in;
1278
1279 if (z_ret != Z_OK) break;
William M. Brackd2f682a2007-05-15 19:42:08 +00001280 }
Daniel Veillard9a2724d2005-12-15 11:12:26 +00001281
1282 ctxt->inrptr += bytes_read;
1283 return(len - ctxt->strm->avail_out);
1284 }
1285#endif
1286
Owen Taylor3473f882001-02-23 17:55:21 +00001287 while (ctxt->inptr - ctxt->inrptr < len) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001288 if (xmlNanoHTTPRecv(ctxt) <= 0) break;
Owen Taylor3473f882001-02-23 17:55:21 +00001289 }
1290 if (ctxt->inptr - ctxt->inrptr < len)
1291 len = ctxt->inptr - ctxt->inrptr;
1292 memcpy(dest, ctxt->inrptr, len);
1293 ctxt->inrptr += len;
1294 return(len);
1295}
1296
1297/**
1298 * xmlNanoHTTPClose:
1299 * @ctx: the HTTP context
1300 *
1301 * This function closes an HTTP context, it ends up the connection and
1302 * free all data related to it.
1303 */
1304void
1305xmlNanoHTTPClose(void *ctx) {
1306 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1307
1308 if (ctx == NULL) return;
1309
1310 xmlNanoHTTPFreeCtxt(ctxt);
1311}
1312
1313/**
Daniel Veillard9403a042001-05-28 11:00:53 +00001314 * xmlNanoHTTPMethodRedir:
Owen Taylor3473f882001-02-23 17:55:21 +00001315 * @URL: The URL to load
1316 * @method: the HTTP method to use
1317 * @input: the input string if any
1318 * @contentType: the Content-Type information IN and OUT
Daniel Veillard9403a042001-05-28 11:00:53 +00001319 * @redir: the redirected URL OUT
Owen Taylor3473f882001-02-23 17:55:21 +00001320 * @headers: the extra headers
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001321 * @ilen: input length
Owen Taylor3473f882001-02-23 17:55:21 +00001322 *
1323 * This function try to open a connection to the indicated resource
1324 * via HTTP using the given @method, adding the given extra headers
1325 * and the input buffer for the request content.
1326 *
1327 * Returns NULL in case of failure, otherwise a request handler.
Daniel Veillard9403a042001-05-28 11:00:53 +00001328 * The contentType, or redir, if provided must be freed by the caller
Owen Taylor3473f882001-02-23 17:55:21 +00001329 */
1330
1331void*
Daniel Veillard9403a042001-05-28 11:00:53 +00001332xmlNanoHTTPMethodRedir(const char *URL, const char *method, const char *input,
Daniel Veillardf012a642001-07-23 19:10:52 +00001333 char **contentType, char **redir,
1334 const char *headers, int ilen ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001335 xmlNanoHTTPCtxtPtr ctxt;
1336 char *bp, *p;
Daniel Veillardf012a642001-07-23 19:10:52 +00001337 int blen, ret;
Owen Taylor3473f882001-02-23 17:55:21 +00001338 int head;
1339 int nbRedirects = 0;
1340 char *redirURL = NULL;
William M. Brack78637da2003-07-31 14:47:38 +00001341#ifdef DEBUG_HTTP
1342 int xmt_bytes;
1343#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001344
1345 if (URL == NULL) return(NULL);
1346 if (method == NULL) method = "GET";
1347 xmlNanoHTTPInit();
1348
1349retry:
1350 if (redirURL == NULL)
1351 ctxt = xmlNanoHTTPNewCtxt(URL);
1352 else {
1353 ctxt = xmlNanoHTTPNewCtxt(redirURL);
Daniel Veillarda840b692003-10-19 13:35:37 +00001354 ctxt->location = xmlMemStrdup(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001355 }
1356
Daniel Veillardf012a642001-07-23 19:10:52 +00001357 if ( ctxt == NULL ) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001358 return ( NULL );
1359 }
1360
Owen Taylor3473f882001-02-23 17:55:21 +00001361 if ((ctxt->protocol == NULL) || (strcmp(ctxt->protocol, "http"))) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001362 __xmlIOErr(XML_FROM_HTTP, XML_HTTP_URL_SYNTAX, "Not a valid HTTP URI");
Owen Taylor3473f882001-02-23 17:55:21 +00001363 xmlNanoHTTPFreeCtxt(ctxt);
1364 if (redirURL != NULL) xmlFree(redirURL);
1365 return(NULL);
1366 }
1367 if (ctxt->hostname == NULL) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001368 __xmlIOErr(XML_FROM_HTTP, XML_HTTP_UNKNOWN_HOST,
1369 "Failed to identify host in URI");
Owen Taylor3473f882001-02-23 17:55:21 +00001370 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001371 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001372 return(NULL);
1373 }
1374 if (proxy) {
1375 blen = strlen(ctxt->hostname) * 2 + 16;
1376 ret = xmlNanoHTTPConnectHost(proxy, proxyPort);
1377 }
1378 else {
1379 blen = strlen(ctxt->hostname);
1380 ret = xmlNanoHTTPConnectHost(ctxt->hostname, ctxt->port);
1381 }
1382 if (ret < 0) {
1383 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001384 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001385 return(NULL);
1386 }
1387 ctxt->fd = ret;
1388
Daniel Veillardf012a642001-07-23 19:10:52 +00001389 if (input == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00001390 ilen = 0;
Daniel Veillardf012a642001-07-23 19:10:52 +00001391 else
1392 blen += 36;
1393
Owen Taylor3473f882001-02-23 17:55:21 +00001394 if (headers != NULL)
Daniel Veillardf012a642001-07-23 19:10:52 +00001395 blen += strlen(headers) + 2;
Owen Taylor3473f882001-02-23 17:55:21 +00001396 if (contentType && *contentType)
William M. Brackead35832008-02-06 04:12:46 +00001397 /* reserve for string plus 'Content-Type: \r\n" */
Owen Taylor3473f882001-02-23 17:55:21 +00001398 blen += strlen(*contentType) + 16;
Daniel Veillard351f2d62005-04-13 02:55:12 +00001399 if (ctxt->query != NULL)
William M. Brackead35832008-02-06 04:12:46 +00001400 /* 1 for '?' */
Daniel Veillard351f2d62005-04-13 02:55:12 +00001401 blen += strlen(ctxt->query) + 1;
Daniel Veillardf012a642001-07-23 19:10:52 +00001402 blen += strlen(method) + strlen(ctxt->path) + 24;
Daniel Veillard9a2724d2005-12-15 11:12:26 +00001403#ifdef HAVE_ZLIB_H
William M. Brackead35832008-02-06 04:12:46 +00001404 /* reserve for possible 'Accept-Encoding: gzip' string */
Daniel Veillard9a2724d2005-12-15 11:12:26 +00001405 blen += 23;
1406#endif
William M. Brackead35832008-02-06 04:12:46 +00001407 if (ctxt->port != 80) {
1408 /* reserve space for ':xxxxx', incl. potential proxy */
1409 if (proxy)
1410 blen += 12;
1411 else
1412 blen += 6;
1413 }
Daniel Veillard82cb3192003-10-29 13:39:15 +00001414 bp = (char*)xmlMallocAtomic(blen);
Daniel Veillardf012a642001-07-23 19:10:52 +00001415 if ( bp == NULL ) {
1416 xmlNanoHTTPFreeCtxt( ctxt );
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001417 xmlHTTPErrMemory("allocating header buffer");
Daniel Veillardf012a642001-07-23 19:10:52 +00001418 return ( NULL );
1419 }
1420
1421 p = bp;
1422
Owen Taylor3473f882001-02-23 17:55:21 +00001423 if (proxy) {
1424 if (ctxt->port != 80) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001425 p += snprintf( p, blen - (p - bp), "%s http://%s:%d%s",
1426 method, ctxt->hostname,
Daniel Veillardf012a642001-07-23 19:10:52 +00001427 ctxt->port, ctxt->path );
Owen Taylor3473f882001-02-23 17:55:21 +00001428 }
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001429 else
1430 p += snprintf( p, blen - (p - bp), "%s http://%s%s", method,
Daniel Veillardf012a642001-07-23 19:10:52 +00001431 ctxt->hostname, ctxt->path);
Owen Taylor3473f882001-02-23 17:55:21 +00001432 }
1433 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001434 p += snprintf( p, blen - (p - bp), "%s %s", method, ctxt->path);
Daniel Veillardf012a642001-07-23 19:10:52 +00001435
Daniel Veillard351f2d62005-04-13 02:55:12 +00001436 if (ctxt->query != NULL)
1437 p += snprintf( p, blen - (p - bp), "?%s", ctxt->query);
1438
William M. Brackec720082007-08-24 02:57:38 +00001439 if (ctxt->port == 80) {
1440 p += snprintf( p, blen - (p - bp), " HTTP/1.0\r\nHost: %s\r\n",
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001441 ctxt->hostname);
William M. Brackec720082007-08-24 02:57:38 +00001442 } else {
1443 p += snprintf( p, blen - (p - bp), " HTTP/1.0\r\nHost: %s:%d\r\n",
1444 ctxt->hostname, ctxt->port);
1445 }
Daniel Veillardf012a642001-07-23 19:10:52 +00001446
Daniel Veillard9a2724d2005-12-15 11:12:26 +00001447#ifdef HAVE_ZLIB_H
1448 p += snprintf(p, blen - (p - bp), "Accept-Encoding: gzip\r\n");
1449#endif
1450
Daniel Veillardf012a642001-07-23 19:10:52 +00001451 if (contentType != NULL && *contentType)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001452 p += snprintf(p, blen - (p - bp), "Content-Type: %s\r\n", *contentType);
Daniel Veillardf012a642001-07-23 19:10:52 +00001453
1454 if (headers != NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001455 p += snprintf( p, blen - (p - bp), "%s", headers );
Daniel Veillardf012a642001-07-23 19:10:52 +00001456
Owen Taylor3473f882001-02-23 17:55:21 +00001457 if (input != NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001458 snprintf(p, blen - (p - bp), "Content-Length: %d\r\n\r\n", ilen );
Owen Taylor3473f882001-02-23 17:55:21 +00001459 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001460 snprintf(p, blen - (p - bp), "\r\n");
Daniel Veillardf012a642001-07-23 19:10:52 +00001461
Owen Taylor3473f882001-02-23 17:55:21 +00001462#ifdef DEBUG_HTTP
1463 xmlGenericError(xmlGenericErrorContext,
1464 "-> %s%s", proxy? "(Proxy) " : "", bp);
1465 if ((blen -= strlen(bp)+1) < 0)
1466 xmlGenericError(xmlGenericErrorContext,
1467 "ERROR: overflowed buffer by %d bytes\n", -blen);
1468#endif
1469 ctxt->outptr = ctxt->out = bp;
1470 ctxt->state = XML_NANO_HTTP_WRITE;
Daniel Veillardf012a642001-07-23 19:10:52 +00001471 blen = strlen( ctxt->out );
Daniel Veillardf012a642001-07-23 19:10:52 +00001472#ifdef DEBUG_HTTP
William M. Brack78637da2003-07-31 14:47:38 +00001473 xmt_bytes = xmlNanoHTTPSend(ctxt, ctxt->out, blen );
Daniel Veillardf012a642001-07-23 19:10:52 +00001474 if ( xmt_bytes != blen )
1475 xmlGenericError( xmlGenericErrorContext,
1476 "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
1477 xmt_bytes, blen,
1478 "bytes of HTTP headers sent to host",
1479 ctxt->hostname );
William M. Brack78637da2003-07-31 14:47:38 +00001480#else
1481 xmlNanoHTTPSend(ctxt, ctxt->out, blen );
Daniel Veillardf012a642001-07-23 19:10:52 +00001482#endif
1483
1484 if ( input != NULL ) {
William M. Brack78637da2003-07-31 14:47:38 +00001485#ifdef DEBUG_HTTP
Daniel Veillardf012a642001-07-23 19:10:52 +00001486 xmt_bytes = xmlNanoHTTPSend( ctxt, input, ilen );
1487
Daniel Veillardf012a642001-07-23 19:10:52 +00001488 if ( xmt_bytes != ilen )
1489 xmlGenericError( xmlGenericErrorContext,
1490 "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
1491 xmt_bytes, ilen,
1492 "bytes of HTTP content sent to host",
1493 ctxt->hostname );
William M. Brack78637da2003-07-31 14:47:38 +00001494#else
1495 xmlNanoHTTPSend( ctxt, input, ilen );
Daniel Veillardf012a642001-07-23 19:10:52 +00001496#endif
1497 }
1498
Owen Taylor3473f882001-02-23 17:55:21 +00001499 ctxt->state = XML_NANO_HTTP_READ;
1500 head = 1;
1501
1502 while ((p = xmlNanoHTTPReadLine(ctxt)) != NULL) {
1503 if (head && (*p == 0)) {
1504 head = 0;
1505 ctxt->content = ctxt->inrptr;
1506 xmlFree(p);
1507 break;
1508 }
1509 xmlNanoHTTPScanAnswer(ctxt, p);
1510
1511#ifdef DEBUG_HTTP
1512 xmlGenericError(xmlGenericErrorContext, "<- %s\n", p);
1513#endif
1514 xmlFree(p);
1515 }
1516
1517 if ((ctxt->location != NULL) && (ctxt->returnValue >= 300) &&
1518 (ctxt->returnValue < 400)) {
1519#ifdef DEBUG_HTTP
1520 xmlGenericError(xmlGenericErrorContext,
1521 "\nRedirect to: %s\n", ctxt->location);
1522#endif
Daniel Veillardf012a642001-07-23 19:10:52 +00001523 while ( xmlNanoHTTPRecv(ctxt) > 0 ) ;
Owen Taylor3473f882001-02-23 17:55:21 +00001524 if (nbRedirects < XML_NANO_HTTP_MAX_REDIR) {
1525 nbRedirects++;
Daniel Veillard9403a042001-05-28 11:00:53 +00001526 if (redirURL != NULL)
1527 xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001528 redirURL = xmlMemStrdup(ctxt->location);
1529 xmlNanoHTTPFreeCtxt(ctxt);
1530 goto retry;
1531 }
1532 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001533 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001534#ifdef DEBUG_HTTP
1535 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardf012a642001-07-23 19:10:52 +00001536 "xmlNanoHTTPMethodRedir: Too many redirects, aborting ...\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001537#endif
1538 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001539 }
1540
1541 if (contentType != NULL) {
1542 if (ctxt->contentType != NULL)
1543 *contentType = xmlMemStrdup(ctxt->contentType);
1544 else
1545 *contentType = NULL;
1546 }
1547
Daniel Veillard9403a042001-05-28 11:00:53 +00001548 if ((redir != NULL) && (redirURL != NULL)) {
1549 *redir = redirURL;
1550 } else {
1551 if (redirURL != NULL)
1552 xmlFree(redirURL);
1553 if (redir != NULL)
1554 *redir = NULL;
1555 }
1556
Owen Taylor3473f882001-02-23 17:55:21 +00001557#ifdef DEBUG_HTTP
1558 if (ctxt->contentType != NULL)
1559 xmlGenericError(xmlGenericErrorContext,
1560 "\nCode %d, content-type '%s'\n\n",
1561 ctxt->returnValue, ctxt->contentType);
1562 else
1563 xmlGenericError(xmlGenericErrorContext,
1564 "\nCode %d, no content-type\n\n",
1565 ctxt->returnValue);
1566#endif
1567
1568 return((void *) ctxt);
1569}
1570
1571/**
Daniel Veillard9403a042001-05-28 11:00:53 +00001572 * xmlNanoHTTPMethod:
1573 * @URL: The URL to load
1574 * @method: the HTTP method to use
1575 * @input: the input string if any
1576 * @contentType: the Content-Type information IN and OUT
1577 * @headers: the extra headers
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001578 * @ilen: input length
Daniel Veillard9403a042001-05-28 11:00:53 +00001579 *
1580 * This function try to open a connection to the indicated resource
1581 * via HTTP using the given @method, adding the given extra headers
1582 * and the input buffer for the request content.
1583 *
1584 * Returns NULL in case of failure, otherwise a request handler.
1585 * The contentType, if provided must be freed by the caller
1586 */
1587
1588void*
1589xmlNanoHTTPMethod(const char *URL, const char *method, const char *input,
Daniel Veillardf012a642001-07-23 19:10:52 +00001590 char **contentType, const char *headers, int ilen) {
Daniel Veillard9403a042001-05-28 11:00:53 +00001591 return(xmlNanoHTTPMethodRedir(URL, method, input, contentType,
Daniel Veillardf012a642001-07-23 19:10:52 +00001592 NULL, headers, ilen));
Daniel Veillard9403a042001-05-28 11:00:53 +00001593}
1594
1595/**
Owen Taylor3473f882001-02-23 17:55:21 +00001596 * xmlNanoHTTPFetch:
1597 * @URL: The URL to load
1598 * @filename: the filename where the content should be saved
1599 * @contentType: if available the Content-Type information will be
1600 * returned at that location
1601 *
1602 * This function try to fetch the indicated resource via HTTP GET
1603 * and save it's content in the file.
1604 *
1605 * Returns -1 in case of failure, 0 incase of success. The contentType,
1606 * if provided must be freed by the caller
1607 */
1608int
1609xmlNanoHTTPFetch(const char *URL, const char *filename, char **contentType) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001610 void *ctxt = NULL;
1611 char *buf = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001612 int fd;
1613 int len;
1614
William M. Brack015ccb22005-02-13 08:18:52 +00001615 if (filename == NULL) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001616 ctxt = xmlNanoHTTPOpen(URL, contentType);
1617 if (ctxt == NULL) return(-1);
1618
1619 if (!strcmp(filename, "-"))
1620 fd = 0;
1621 else {
1622 fd = open(filename, O_CREAT | O_WRONLY, 00644);
1623 if (fd < 0) {
1624 xmlNanoHTTPClose(ctxt);
1625 if ((contentType != NULL) && (*contentType != NULL)) {
1626 xmlFree(*contentType);
1627 *contentType = NULL;
1628 }
1629 return(-1);
1630 }
1631 }
1632
Daniel Veillardf012a642001-07-23 19:10:52 +00001633 xmlNanoHTTPFetchContent( ctxt, &buf, &len );
1634 if ( len > 0 ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001635 write(fd, buf, len);
1636 }
1637
1638 xmlNanoHTTPClose(ctxt);
1639 close(fd);
1640 return(0);
1641}
1642
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001643#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001644/**
1645 * xmlNanoHTTPSave:
1646 * @ctxt: the HTTP context
1647 * @filename: the filename where the content should be saved
1648 *
1649 * This function saves the output of the HTTP transaction to a file
1650 * It closes and free the context at the end
1651 *
1652 * Returns -1 in case of failure, 0 incase of success.
1653 */
1654int
1655xmlNanoHTTPSave(void *ctxt, const char *filename) {
Daniel Veillarde3924972001-07-25 20:25:21 +00001656 char *buf = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001657 int fd;
1658 int len;
1659
William M. Brack015ccb22005-02-13 08:18:52 +00001660 if ((ctxt == NULL) || (filename == NULL)) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001661
1662 if (!strcmp(filename, "-"))
1663 fd = 0;
1664 else {
Daniel Veillardcd2ebab2007-08-23 20:47:33 +00001665 fd = open(filename, O_CREAT | O_WRONLY, 0666);
Owen Taylor3473f882001-02-23 17:55:21 +00001666 if (fd < 0) {
1667 xmlNanoHTTPClose(ctxt);
1668 return(-1);
1669 }
1670 }
1671
Daniel Veillardf012a642001-07-23 19:10:52 +00001672 xmlNanoHTTPFetchContent( ctxt, &buf, &len );
1673 if ( len > 0 ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001674 write(fd, buf, len);
1675 }
1676
1677 xmlNanoHTTPClose(ctxt);
William M. Brack20d82362004-03-17 08:44:46 +00001678 close(fd);
Owen Taylor3473f882001-02-23 17:55:21 +00001679 return(0);
1680}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001681#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001682
1683/**
1684 * xmlNanoHTTPReturnCode:
1685 * @ctx: the HTTP context
1686 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001687 * Get the latest HTTP return code received
1688 *
Owen Taylor3473f882001-02-23 17:55:21 +00001689 * Returns the HTTP return code for the request.
1690 */
1691int
1692xmlNanoHTTPReturnCode(void *ctx) {
1693 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1694
1695 if (ctxt == NULL) return(-1);
1696
1697 return(ctxt->returnValue);
1698}
1699
1700/**
1701 * xmlNanoHTTPAuthHeader:
1702 * @ctx: the HTTP context
1703 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001704 * Get the authentication header of an HTTP context
1705 *
Owen Taylor3473f882001-02-23 17:55:21 +00001706 * Returns the stashed value of the WWW-Authenticate or Proxy-Authenticate
1707 * header.
1708 */
1709const char *
1710xmlNanoHTTPAuthHeader(void *ctx) {
1711 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1712
1713 if (ctxt == NULL) return(NULL);
1714
1715 return(ctxt->authHeader);
1716}
1717
Daniel Veillardf012a642001-07-23 19:10:52 +00001718/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00001719 * xmlNanoHTTPContentLength:
Daniel Veillardf012a642001-07-23 19:10:52 +00001720 * @ctx: the HTTP context
1721 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001722 * Provides the specified content length from the HTTP header.
1723 *
Daniel Veillardf012a642001-07-23 19:10:52 +00001724 * Return the specified content length from the HTTP header. Note that
1725 * a value of -1 indicates that the content length element was not included in
1726 * the response header.
1727 */
1728int
1729xmlNanoHTTPContentLength( void * ctx ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001730 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillardf012a642001-07-23 19:10:52 +00001731
1732 return ( ( ctxt == NULL ) ? -1 : ctxt->ContentLength );
1733}
1734
1735/**
Daniel Veillard847332a2003-10-18 11:29:40 +00001736 * xmlNanoHTTPRedir:
1737 * @ctx: the HTTP context
1738 *
1739 * Provides the specified redirection URL if available from the HTTP header.
1740 *
1741 * Return the specified redirection URL or NULL if not redirected.
1742 */
1743const char *
1744xmlNanoHTTPRedir( void * ctx ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001745 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillard847332a2003-10-18 11:29:40 +00001746
1747 return ( ( ctxt == NULL ) ? NULL : ctxt->location );
1748}
1749
1750/**
1751 * xmlNanoHTTPEncoding:
1752 * @ctx: the HTTP context
1753 *
1754 * Provides the specified encoding if specified in the HTTP headers.
1755 *
1756 * Return the specified encoding or NULL if not available
1757 */
1758const char *
1759xmlNanoHTTPEncoding( void * ctx ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001760 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillard847332a2003-10-18 11:29:40 +00001761
1762 return ( ( ctxt == NULL ) ? NULL : ctxt->encoding );
1763}
1764
1765/**
Daniel Veillarda840b692003-10-19 13:35:37 +00001766 * xmlNanoHTTPMimeType:
1767 * @ctx: the HTTP context
1768 *
1769 * Provides the specified Mime-Type if specified in the HTTP headers.
1770 *
1771 * Return the specified Mime-Type or NULL if not available
1772 */
1773const char *
1774xmlNanoHTTPMimeType( void * ctx ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001775 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillarda840b692003-10-19 13:35:37 +00001776
1777 return ( ( ctxt == NULL ) ? NULL : ctxt->mimeType );
1778}
1779
1780/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00001781 * xmlNanoHTTPFetchContent:
Daniel Veillardf012a642001-07-23 19:10:52 +00001782 * @ctx: the HTTP context
1783 * @ptr: pointer to set to the content buffer.
1784 * @len: integer pointer to hold the length of the content
1785 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001786 * Check if all the content was read
1787 *
Daniel Veillardf012a642001-07-23 19:10:52 +00001788 * Returns 0 if all the content was read and available, returns
1789 * -1 if received content length was less than specified or an error
1790 * occurred.
1791 */
Daniel Veillarda2351322004-06-27 12:08:10 +00001792static int
Daniel Veillardf012a642001-07-23 19:10:52 +00001793xmlNanoHTTPFetchContent( void * ctx, char ** ptr, int * len ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001794 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillardf012a642001-07-23 19:10:52 +00001795
1796 int rc = 0;
1797 int cur_lgth;
1798 int rcvd_lgth;
1799 int dummy_int;
1800 char * dummy_ptr = NULL;
1801
1802 /* Dummy up return input parameters if not provided */
1803
1804 if ( len == NULL )
1805 len = &dummy_int;
1806
1807 if ( ptr == NULL )
1808 ptr = &dummy_ptr;
1809
1810 /* But can't work without the context pointer */
1811
1812 if ( ( ctxt == NULL ) || ( ctxt->content == NULL ) ) {
1813 *len = 0;
1814 *ptr = NULL;
1815 return ( -1 );
1816 }
1817
1818 rcvd_lgth = ctxt->inptr - ctxt->content;
1819
1820 while ( (cur_lgth = xmlNanoHTTPRecv( ctxt )) > 0 ) {
1821
1822 rcvd_lgth += cur_lgth;
1823 if ( (ctxt->ContentLength > 0) && (rcvd_lgth >= ctxt->ContentLength) )
1824 break;
1825 }
1826
1827 *ptr = ctxt->content;
1828 *len = rcvd_lgth;
1829
1830 if ( ( ctxt->ContentLength > 0 ) && ( rcvd_lgth < ctxt->ContentLength ) )
1831 rc = -1;
1832 else if ( rcvd_lgth == 0 )
1833 rc = -1;
1834
1835 return ( rc );
1836}
1837
Owen Taylor3473f882001-02-23 17:55:21 +00001838#ifdef STANDALONE
1839int main(int argc, char **argv) {
1840 char *contentType = NULL;
1841
1842 if (argv[1] != NULL) {
1843 if (argv[2] != NULL)
1844 xmlNanoHTTPFetch(argv[1], argv[2], &contentType);
1845 else
1846 xmlNanoHTTPFetch(argv[1], "-", &contentType);
1847 if (contentType != NULL) xmlFree(contentType);
1848 } else {
1849 xmlGenericError(xmlGenericErrorContext,
1850 "%s: minimal HTTP GET implementation\n", argv[0]);
1851 xmlGenericError(xmlGenericErrorContext,
1852 "\tusage %s [ URL [ filename ] ]\n", argv[0]);
1853 }
1854 xmlNanoHTTPCleanup();
1855 xmlMemoryDump();
1856 return(0);
1857}
1858#endif /* STANDALONE */
1859#else /* !LIBXML_HTTP_ENABLED */
1860#ifdef STANDALONE
1861#include <stdio.h>
1862int main(int argc, char **argv) {
1863 xmlGenericError(xmlGenericErrorContext,
1864 "%s : HTTP support not compiled in\n", argv[0]);
1865 return(0);
1866}
1867#endif /* STANDALONE */
1868#endif /* LIBXML_HTTP_ENABLED */
Daniel Veillard5d4644e2005-04-01 13:11:58 +00001869#define bottom_nanohttp
1870#include "elfgcchack.h"