blob: 542c4eac943aa6088ae432b60e38903d687e6e9c [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 Veillard13cee4e2009-09-05 14:52:55 +0200153 int version; /* the protocol version */
Daniel Veillardf012a642001-07-23 19:10:52 +0000154 int ContentLength; /* specified content length from HTTP header */
Owen Taylor3473f882001-02-23 17:55:21 +0000155 char *contentType; /* the MIME type for the input */
156 char *location; /* the new URL in case of redirect */
157 char *authHeader; /* contents of {WWW,Proxy}-Authenticate header */
Daniel Veillard847332a2003-10-18 11:29:40 +0000158 char *encoding; /* encoding extracted from the contentType */
Daniel Veillarda840b692003-10-19 13:35:37 +0000159 char *mimeType; /* Mime-Type extracted from the contentType */
Daniel Veillard9a2724d2005-12-15 11:12:26 +0000160#ifdef HAVE_ZLIB_H
161 z_stream *strm; /* Zlib stream object */
162 int usesGzip; /* "Content-Encoding: gzip" was detected */
163#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000164} xmlNanoHTTPCtxt, *xmlNanoHTTPCtxtPtr;
165
166static int initialized = 0;
167static char *proxy = NULL; /* the proxy name if any */
168static int proxyPort; /* the proxy port if any */
169static unsigned int timeout = 60;/* the select() timeout in seconds */
170
Daniel Veillarda2351322004-06-27 12:08:10 +0000171static int xmlNanoHTTPFetchContent( void * ctx, char ** ptr, int * len );
Daniel Veillardf012a642001-07-23 19:10:52 +0000172
Owen Taylor3473f882001-02-23 17:55:21 +0000173/**
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000174 * xmlHTTPErrMemory:
175 * @extra: extra informations
176 *
177 * Handle an out of memory condition
178 */
179static void
180xmlHTTPErrMemory(const char *extra)
181{
182 __xmlSimpleError(XML_FROM_HTTP, XML_ERR_NO_MEMORY, NULL, NULL, extra);
183}
184
185/**
Owen Taylor3473f882001-02-23 17:55:21 +0000186 * A portability function
187 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000188static int socket_errno(void) {
Owen Taylor3473f882001-02-23 17:55:21 +0000189#ifdef _WINSOCKAPI_
190 return(WSAGetLastError());
191#else
192 return(errno);
193#endif
194}
195
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000196#ifdef SUPPORT_IP6
Daniel Veillard2db8c122003-07-08 12:16:59 +0000197static
198int have_ipv6(void) {
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000199 int s;
200
201 s = socket (AF_INET6, SOCK_STREAM, 0);
202 if (s != -1) {
203 close (s);
204 return (1);
205 }
206 return (0);
207}
208#endif
209
Owen Taylor3473f882001-02-23 17:55:21 +0000210/**
211 * xmlNanoHTTPInit:
212 *
213 * Initialize the HTTP protocol layer.
214 * Currently it just checks for proxy informations
215 */
216
217void
218xmlNanoHTTPInit(void) {
219 const char *env;
220#ifdef _WINSOCKAPI_
221 WSADATA wsaData;
222#endif
223
224 if (initialized)
225 return;
226
227#ifdef _WINSOCKAPI_
228 if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0)
229 return;
230#endif
231
232 if (proxy == NULL) {
233 proxyPort = 80;
234 env = getenv("no_proxy");
Daniel Veillard29b17482004-08-16 00:39:03 +0000235 if (env && ((env[0] == '*') && (env[1] == 0)))
Owen Taylor3473f882001-02-23 17:55:21 +0000236 goto done;
237 env = getenv("http_proxy");
238 if (env != NULL) {
239 xmlNanoHTTPScanProxy(env);
240 goto done;
241 }
242 env = getenv("HTTP_PROXY");
243 if (env != NULL) {
244 xmlNanoHTTPScanProxy(env);
245 goto done;
246 }
247 }
248done:
249 initialized = 1;
250}
251
252/**
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000253 * xmlNanoHTTPCleanup:
Owen Taylor3473f882001-02-23 17:55:21 +0000254 *
255 * Cleanup the HTTP protocol layer.
256 */
257
258void
259xmlNanoHTTPCleanup(void) {
Daniel Veillard744acff2005-07-12 15:09:53 +0000260 if (proxy != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +0000261 xmlFree(proxy);
Daniel Veillard744acff2005-07-12 15:09:53 +0000262 proxy = NULL;
263 }
Owen Taylor3473f882001-02-23 17:55:21 +0000264#ifdef _WINSOCKAPI_
265 if (initialized)
266 WSACleanup();
267#endif
268 initialized = 0;
269 return;
270}
271
272/**
Owen Taylor3473f882001-02-23 17:55:21 +0000273 * xmlNanoHTTPScanURL:
274 * @ctxt: an HTTP context
275 * @URL: The URL used to initialize the context
276 *
277 * (Re)Initialize an HTTP context by parsing the URL and finding
278 * the protocol host port and path it indicates.
279 */
280
281static void
282xmlNanoHTTPScanURL(xmlNanoHTTPCtxtPtr ctxt, const char *URL) {
William M. Brack015ccb22005-02-13 08:18:52 +0000283 xmlURIPtr uri;
284 /*
285 * Clear any existing data from the context
286 */
Owen Taylor3473f882001-02-23 17:55:21 +0000287 if (ctxt->protocol != NULL) {
288 xmlFree(ctxt->protocol);
289 ctxt->protocol = NULL;
290 }
291 if (ctxt->hostname != NULL) {
292 xmlFree(ctxt->hostname);
293 ctxt->hostname = NULL;
294 }
295 if (ctxt->path != NULL) {
296 xmlFree(ctxt->path);
297 ctxt->path = NULL;
298 }
Daniel Veillard351f2d62005-04-13 02:55:12 +0000299 if (ctxt->query != NULL) {
300 xmlFree(ctxt->query);
301 ctxt->query = NULL;
302 }
Owen Taylor3473f882001-02-23 17:55:21 +0000303 if (URL == NULL) return;
William M. Brack015ccb22005-02-13 08:18:52 +0000304
Daniel Veillard336a8e12005-08-07 10:46:19 +0000305 uri = xmlParseURIRaw(URL, 1);
William M. Brack015ccb22005-02-13 08:18:52 +0000306 if (uri == NULL)
307 return;
308
309 if ((uri->scheme == NULL) || (uri->server == NULL)) {
310 xmlFreeURI(uri);
311 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000312 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000313
William M. Brack015ccb22005-02-13 08:18:52 +0000314 ctxt->protocol = xmlMemStrdup(uri->scheme);
315 ctxt->hostname = xmlMemStrdup(uri->server);
316 if (uri->path != NULL)
317 ctxt->path = xmlMemStrdup(uri->path);
318 else
319 ctxt->path = xmlMemStrdup("/");
Daniel Veillard351f2d62005-04-13 02:55:12 +0000320 if (uri->query != NULL)
321 ctxt->query = xmlMemStrdup(uri->query);
William M. Brack015ccb22005-02-13 08:18:52 +0000322 if (uri->port != 0)
323 ctxt->port = uri->port;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000324
William M. Brack015ccb22005-02-13 08:18:52 +0000325 xmlFreeURI(uri);
Owen Taylor3473f882001-02-23 17:55:21 +0000326}
327
328/**
329 * xmlNanoHTTPScanProxy:
330 * @URL: The proxy URL used to initialize the proxy context
331 *
332 * (Re)Initialize the HTTP Proxy context by parsing the URL and finding
333 * the protocol host port it indicates.
334 * Should be like http://myproxy/ or http://myproxy:3128/
335 * A NULL URL cleans up proxy informations.
336 */
337
338void
339xmlNanoHTTPScanProxy(const char *URL) {
William M. Brack015ccb22005-02-13 08:18:52 +0000340 xmlURIPtr uri;
Owen Taylor3473f882001-02-23 17:55:21 +0000341
342 if (proxy != NULL) {
343 xmlFree(proxy);
344 proxy = NULL;
345 }
William M. Brack015ccb22005-02-13 08:18:52 +0000346 proxyPort = 0;
347
Owen Taylor3473f882001-02-23 17:55:21 +0000348#ifdef DEBUG_HTTP
349 if (URL == NULL)
350 xmlGenericError(xmlGenericErrorContext,
351 "Removing HTTP proxy info\n");
352 else
353 xmlGenericError(xmlGenericErrorContext,
354 "Using HTTP proxy %s\n", URL);
355#endif
356 if (URL == NULL) return;
William M. Brack015ccb22005-02-13 08:18:52 +0000357
Daniel Veillard336a8e12005-08-07 10:46:19 +0000358 uri = xmlParseURIRaw(URL, 1);
William M. Brack015ccb22005-02-13 08:18:52 +0000359 if ((uri == NULL) || (uri->scheme == NULL) ||
360 (strcmp(uri->scheme, "http")) || (uri->server == NULL)) {
361 __xmlIOErr(XML_FROM_HTTP, XML_HTTP_URL_SYNTAX, "Syntax Error\n");
362 if (uri != NULL)
363 xmlFreeURI(uri);
364 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000365 }
William M. Brack015ccb22005-02-13 08:18:52 +0000366
367 proxy = xmlMemStrdup(uri->server);
368 if (uri->port != 0)
369 proxyPort = uri->port;
Owen Taylor3473f882001-02-23 17:55:21 +0000370
William M. Brack015ccb22005-02-13 08:18:52 +0000371 xmlFreeURI(uri);
Owen Taylor3473f882001-02-23 17:55:21 +0000372}
373
374/**
375 * xmlNanoHTTPNewCtxt:
376 * @URL: The URL used to initialize the context
377 *
378 * Allocate and initialize a new HTTP context.
379 *
380 * Returns an HTTP context or NULL in case of error.
381 */
382
383static xmlNanoHTTPCtxtPtr
384xmlNanoHTTPNewCtxt(const char *URL) {
385 xmlNanoHTTPCtxtPtr ret;
386
387 ret = (xmlNanoHTTPCtxtPtr) xmlMalloc(sizeof(xmlNanoHTTPCtxt));
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000388 if (ret == NULL) {
389 xmlHTTPErrMemory("allocating context");
390 return(NULL);
391 }
Owen Taylor3473f882001-02-23 17:55:21 +0000392
393 memset(ret, 0, sizeof(xmlNanoHTTPCtxt));
394 ret->port = 80;
395 ret->returnValue = 0;
396 ret->fd = -1;
Daniel Veillardf012a642001-07-23 19:10:52 +0000397 ret->ContentLength = -1;
Owen Taylor3473f882001-02-23 17:55:21 +0000398
Daniel Veillardcacbe5d2003-01-10 16:09:51 +0000399 xmlNanoHTTPScanURL(ret, URL);
Owen Taylor3473f882001-02-23 17:55:21 +0000400
401 return(ret);
402}
403
404/**
405 * xmlNanoHTTPFreeCtxt:
406 * @ctxt: an HTTP context
407 *
408 * Frees the context after closing the connection.
409 */
410
411static void
412xmlNanoHTTPFreeCtxt(xmlNanoHTTPCtxtPtr ctxt) {
413 if (ctxt == NULL) return;
414 if (ctxt->hostname != NULL) xmlFree(ctxt->hostname);
415 if (ctxt->protocol != NULL) xmlFree(ctxt->protocol);
416 if (ctxt->path != NULL) xmlFree(ctxt->path);
Daniel Veillard351f2d62005-04-13 02:55:12 +0000417 if (ctxt->query != NULL) xmlFree(ctxt->query);
Owen Taylor3473f882001-02-23 17:55:21 +0000418 if (ctxt->out != NULL) xmlFree(ctxt->out);
419 if (ctxt->in != NULL) xmlFree(ctxt->in);
420 if (ctxt->contentType != NULL) xmlFree(ctxt->contentType);
Daniel Veillard847332a2003-10-18 11:29:40 +0000421 if (ctxt->encoding != NULL) xmlFree(ctxt->encoding);
Daniel Veillarda840b692003-10-19 13:35:37 +0000422 if (ctxt->mimeType != NULL) xmlFree(ctxt->mimeType);
Owen Taylor3473f882001-02-23 17:55:21 +0000423 if (ctxt->location != NULL) xmlFree(ctxt->location);
424 if (ctxt->authHeader != NULL) xmlFree(ctxt->authHeader);
Daniel Veillard9a2724d2005-12-15 11:12:26 +0000425#ifdef HAVE_ZLIB_H
426 if (ctxt->strm != NULL) {
427 inflateEnd(ctxt->strm);
428 xmlFree(ctxt->strm);
429 }
430#endif
431
Owen Taylor3473f882001-02-23 17:55:21 +0000432 ctxt->state = XML_NANO_HTTP_NONE;
433 if (ctxt->fd >= 0) closesocket(ctxt->fd);
434 ctxt->fd = -1;
435 xmlFree(ctxt);
436}
437
438/**
439 * xmlNanoHTTPSend:
440 * @ctxt: an HTTP context
441 *
442 * Send the input needed to initiate the processing on the server side
Daniel Veillardf012a642001-07-23 19:10:52 +0000443 * Returns number of bytes sent or -1 on error.
Owen Taylor3473f882001-02-23 17:55:21 +0000444 */
445
Daniel Veillardf012a642001-07-23 19:10:52 +0000446static int
Raphael Prevost48b60c32009-08-23 13:11:01 +0200447xmlNanoHTTPSend(xmlNanoHTTPCtxtPtr ctxt, const char *xmt_ptr, int outlen)
448{
449 int total_sent = 0;
450#ifdef HAVE_POLL_H
451 struct pollfd p;
452#else
453 struct timeval tv;
454 fd_set wfd;
455#endif
Daniel Veillardf012a642001-07-23 19:10:52 +0000456
Raphael Prevost48b60c32009-08-23 13:11:01 +0200457 if ((ctxt->state & XML_NANO_HTTP_WRITE) && (xmt_ptr != NULL)) {
Daniel Veillardf012a642001-07-23 19:10:52 +0000458 while (total_sent < outlen) {
459 int nsent = send(ctxt->fd, xmt_ptr + total_sent,
Raphael Prevost48b60c32009-08-23 13:11:01 +0200460 outlen - total_sent, 0);
461
462 if (nsent > 0)
Owen Taylor3473f882001-02-23 17:55:21 +0000463 total_sent += nsent;
Raphael Prevost48b60c32009-08-23 13:11:01 +0200464 else if ((nsent == -1) &&
Daniel Veillardba6db032001-07-31 16:25:45 +0000465#if defined(EAGAIN) && EAGAIN != EWOULDBLOCK
Raphael Prevost48b60c32009-08-23 13:11:01 +0200466 (socket_errno() != EAGAIN) &&
Daniel Veillardba6db032001-07-31 16:25:45 +0000467#endif
Raphael Prevost48b60c32009-08-23 13:11:01 +0200468 (socket_errno() != EWOULDBLOCK)) {
469 __xmlIOErr(XML_FROM_HTTP, 0, "send failed\n");
470 if (total_sent == 0)
471 total_sent = -1;
472 break;
473 } else {
474 /*
475 * No data sent
476 * Since non-blocking sockets are used, wait for
477 * socket to be writable or default timeout prior
478 * to retrying.
479 */
480#ifndef HAVE_POLL_H
spadixd29a5c82009-10-19 14:03:25 +0200481#ifndef _WINSOCKAPI_
Raphael Prevost48b60c32009-08-23 13:11:01 +0200482 if (ctxt->fd > FD_SETSIZE)
483 return -1;
spadixd29a5c82009-10-19 14:03:25 +0200484#endif
Daniel Veillardf012a642001-07-23 19:10:52 +0000485
Raphael Prevost48b60c32009-08-23 13:11:01 +0200486 tv.tv_sec = timeout;
487 tv.tv_usec = 0;
488 FD_ZERO(&wfd);
Daniel Veillard9e2110b2005-08-08 20:33:54 +0000489#ifdef _MSC_VER
490#pragma warning(push)
491#pragma warning(disable: 4018)
492#endif
Raphael Prevost48b60c32009-08-23 13:11:01 +0200493 FD_SET(ctxt->fd, &wfd);
Daniel Veillard9e2110b2005-08-08 20:33:54 +0000494#ifdef _MSC_VER
495#pragma warning(pop)
496#endif
Raphael Prevost48b60c32009-08-23 13:11:01 +0200497 (void) select(ctxt->fd + 1, NULL, &wfd, NULL, &tv);
498#else
499 p.fd = ctxt->fd;
500 p.events = POLLOUT;
501 (void) poll(&p, 1, timeout * 1000);
502#endif /* !HAVE_POLL_H */
503 }
504 }
Owen Taylor3473f882001-02-23 17:55:21 +0000505 }
Daniel Veillardf012a642001-07-23 19:10:52 +0000506
507 return total_sent;
Owen Taylor3473f882001-02-23 17:55:21 +0000508}
509
510/**
511 * xmlNanoHTTPRecv:
512 * @ctxt: an HTTP context
513 *
514 * Read information coming from the HTTP connection.
515 * This is a blocking call (but it blocks in select(), not read()).
516 *
517 * Returns the number of byte read or -1 in case of error.
518 */
519
520static int
Raphael Prevost48b60c32009-08-23 13:11:01 +0200521xmlNanoHTTPRecv(xmlNanoHTTPCtxtPtr ctxt)
522{
523#ifdef HAVE_POLL_H
524 struct pollfd p;
525#else
Owen Taylor3473f882001-02-23 17:55:21 +0000526 fd_set rfd;
527 struct timeval tv;
Raphael Prevost48b60c32009-08-23 13:11:01 +0200528#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000529
530
531 while (ctxt->state & XML_NANO_HTTP_READ) {
Raphael Prevost48b60c32009-08-23 13:11:01 +0200532 if (ctxt->in == NULL) {
533 ctxt->in = (char *) xmlMallocAtomic(65000 * sizeof(char));
534 if (ctxt->in == NULL) {
535 xmlHTTPErrMemory("allocating input");
536 ctxt->last = -1;
537 return (-1);
538 }
539 ctxt->inlen = 65000;
540 ctxt->inptr = ctxt->content = ctxt->inrptr = ctxt->in;
541 }
542 if (ctxt->inrptr > ctxt->in + XML_NANO_HTTP_CHUNK) {
543 int delta = ctxt->inrptr - ctxt->in;
544 int len = ctxt->inptr - ctxt->inrptr;
Owen Taylor3473f882001-02-23 17:55:21 +0000545
Raphael Prevost48b60c32009-08-23 13:11:01 +0200546 memmove(ctxt->in, ctxt->inrptr, len);
547 ctxt->inrptr -= delta;
548 ctxt->content -= delta;
549 ctxt->inptr -= delta;
550 }
551 if ((ctxt->in + ctxt->inlen) < (ctxt->inptr + XML_NANO_HTTP_CHUNK)) {
552 int d_inptr = ctxt->inptr - ctxt->in;
553 int d_content = ctxt->content - ctxt->in;
554 int d_inrptr = ctxt->inrptr - ctxt->in;
555 char *tmp_ptr = ctxt->in;
556
557 ctxt->inlen *= 2;
Daniel Veillardf012a642001-07-23 19:10:52 +0000558 ctxt->in = (char *) xmlRealloc(tmp_ptr, ctxt->inlen);
Raphael Prevost48b60c32009-08-23 13:11:01 +0200559 if (ctxt->in == NULL) {
560 xmlHTTPErrMemory("allocating input buffer");
561 xmlFree(tmp_ptr);
562 ctxt->last = -1;
563 return (-1);
564 }
Owen Taylor3473f882001-02-23 17:55:21 +0000565 ctxt->inptr = ctxt->in + d_inptr;
566 ctxt->content = ctxt->in + d_content;
567 ctxt->inrptr = ctxt->in + d_inrptr;
Raphael Prevost48b60c32009-08-23 13:11:01 +0200568 }
569 ctxt->last = recv(ctxt->fd, ctxt->inptr, XML_NANO_HTTP_CHUNK, 0);
570 if (ctxt->last > 0) {
571 ctxt->inptr += ctxt->last;
572 return (ctxt->last);
573 }
574 if (ctxt->last == 0) {
575 return (0);
576 }
577 if (ctxt->last == -1) {
578 switch (socket_errno()) {
579 case EINPROGRESS:
580 case EWOULDBLOCK:
Owen Taylor3473f882001-02-23 17:55:21 +0000581#if defined(EAGAIN) && EAGAIN != EWOULDBLOCK
Raphael Prevost48b60c32009-08-23 13:11:01 +0200582 case EAGAIN:
Owen Taylor3473f882001-02-23 17:55:21 +0000583#endif
Raphael Prevost48b60c32009-08-23 13:11:01 +0200584 break;
Daniel Veillardf012a642001-07-23 19:10:52 +0000585
Raphael Prevost48b60c32009-08-23 13:11:01 +0200586 case ECONNRESET:
587 case ESHUTDOWN:
588 return (0);
Daniel Veillardf012a642001-07-23 19:10:52 +0000589
Raphael Prevost48b60c32009-08-23 13:11:01 +0200590 default:
591 __xmlIOErr(XML_FROM_HTTP, 0, "recv failed\n");
592 return (-1);
593 }
594 }
595#ifdef HAVE_POLL_H
596 p.fd = ctxt->fd;
597 p.events = POLLIN;
598 if ((poll(&p, 1, timeout * 1000) < 1)
599#if defined(EINTR)
600 && (errno != EINTR)
601#endif
602 )
603 return (0);
604#else /* !HAVE_POLL_H */
spadixd29a5c82009-10-19 14:03:25 +0200605#ifndef _WINSOCKAPI_
Raphael Prevost48b60c32009-08-23 13:11:01 +0200606 if (ctxt->fd > FD_SETSIZE)
607 return 0;
spadixd29a5c82009-10-19 14:03:25 +0200608#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000609
Raphael Prevost48b60c32009-08-23 13:11:01 +0200610 tv.tv_sec = timeout;
611 tv.tv_usec = 0;
612 FD_ZERO(&rfd);
613
Daniel Veillard9e2110b2005-08-08 20:33:54 +0000614#ifdef _MSC_VER
615#pragma warning(push)
616#pragma warning(disable: 4018)
617#endif
Raphael Prevost48b60c32009-08-23 13:11:01 +0200618
619 FD_SET(ctxt->fd, &rfd);
620
Daniel Veillard9e2110b2005-08-08 20:33:54 +0000621#ifdef _MSC_VER
622#pragma warning(pop)
623#endif
Raphael Prevost48b60c32009-08-23 13:11:01 +0200624
625 if ((select(ctxt->fd + 1, &rfd, NULL, NULL, &tv) < 1)
Daniel Veillard50f34372001-08-03 12:06:36 +0000626#if defined(EINTR)
Raphael Prevost48b60c32009-08-23 13:11:01 +0200627 && (errno != EINTR)
Daniel Veillard50f34372001-08-03 12:06:36 +0000628#endif
Raphael Prevost48b60c32009-08-23 13:11:01 +0200629 )
630 return (0);
631#endif /* !HAVE_POLL_H */
Owen Taylor3473f882001-02-23 17:55:21 +0000632 }
Raphael Prevost48b60c32009-08-23 13:11:01 +0200633 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +0000634}
635
636/**
637 * xmlNanoHTTPReadLine:
638 * @ctxt: an HTTP context
639 *
640 * Read one line in the HTTP server output, usually for extracting
641 * the HTTP protocol informations from the answer header.
642 *
643 * Returns a newly allocated string with a copy of the line, or NULL
644 * which indicate the end of the input.
645 */
646
647static char *
648xmlNanoHTTPReadLine(xmlNanoHTTPCtxtPtr ctxt) {
649 char buf[4096];
650 char *bp = buf;
Daniel Veillardf012a642001-07-23 19:10:52 +0000651 int rc;
Owen Taylor3473f882001-02-23 17:55:21 +0000652
653 while (bp - buf < 4095) {
654 if (ctxt->inrptr == ctxt->inptr) {
Daniel Veillardf012a642001-07-23 19:10:52 +0000655 if ( (rc = xmlNanoHTTPRecv(ctxt)) == 0) {
Owen Taylor3473f882001-02-23 17:55:21 +0000656 if (bp == buf)
657 return(NULL);
658 else
659 *bp = 0;
660 return(xmlMemStrdup(buf));
661 }
Daniel Veillardf012a642001-07-23 19:10:52 +0000662 else if ( rc == -1 ) {
663 return ( NULL );
664 }
Owen Taylor3473f882001-02-23 17:55:21 +0000665 }
666 *bp = *ctxt->inrptr++;
667 if (*bp == '\n') {
668 *bp = 0;
669 return(xmlMemStrdup(buf));
670 }
671 if (*bp != '\r')
672 bp++;
673 }
674 buf[4095] = 0;
675 return(xmlMemStrdup(buf));
676}
677
678
679/**
680 * xmlNanoHTTPScanAnswer:
681 * @ctxt: an HTTP context
682 * @line: an HTTP header line
683 *
684 * Try to extract useful informations from the server answer.
685 * We currently parse and process:
686 * - The HTTP revision/ return code
Daniel Veillarda840b692003-10-19 13:35:37 +0000687 * - The Content-Type, Mime-Type and charset used
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000688 * - The Location for redirect processing.
Owen Taylor3473f882001-02-23 17:55:21 +0000689 *
690 * Returns -1 in case of failure, the file descriptor number otherwise
691 */
692
693static void
694xmlNanoHTTPScanAnswer(xmlNanoHTTPCtxtPtr ctxt, const char *line) {
695 const char *cur = line;
696
697 if (line == NULL) return;
698
699 if (!strncmp(line, "HTTP/", 5)) {
700 int version = 0;
701 int ret = 0;
702
703 cur += 5;
704 while ((*cur >= '0') && (*cur <= '9')) {
705 version *= 10;
706 version += *cur - '0';
707 cur++;
708 }
709 if (*cur == '.') {
710 cur++;
711 if ((*cur >= '0') && (*cur <= '9')) {
712 version *= 10;
713 version += *cur - '0';
714 cur++;
715 }
716 while ((*cur >= '0') && (*cur <= '9'))
717 cur++;
718 } else
719 version *= 10;
720 if ((*cur != ' ') && (*cur != '\t')) return;
721 while ((*cur == ' ') || (*cur == '\t')) cur++;
722 if ((*cur < '0') || (*cur > '9')) return;
723 while ((*cur >= '0') && (*cur <= '9')) {
724 ret *= 10;
725 ret += *cur - '0';
726 cur++;
727 }
728 if ((*cur != 0) && (*cur != ' ') && (*cur != '\t')) return;
729 ctxt->returnValue = ret;
Daniel Veillard13cee4e2009-09-05 14:52:55 +0200730 ctxt->version = version;
Owen Taylor3473f882001-02-23 17:55:21 +0000731 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Content-Type:", 13)) {
Daniel Veillarda840b692003-10-19 13:35:37 +0000732 const xmlChar *charset, *last, *mime;
Owen Taylor3473f882001-02-23 17:55:21 +0000733 cur += 13;
734 while ((*cur == ' ') || (*cur == '\t')) cur++;
735 if (ctxt->contentType != NULL)
736 xmlFree(ctxt->contentType);
737 ctxt->contentType = xmlMemStrdup(cur);
Daniel Veillarda840b692003-10-19 13:35:37 +0000738 mime = (const xmlChar *) cur;
739 last = mime;
740 while ((*last != 0) && (*last != ' ') && (*last != '\t') &&
741 (*last != ';') && (*last != ','))
742 last++;
743 if (ctxt->mimeType != NULL)
744 xmlFree(ctxt->mimeType);
745 ctxt->mimeType = (char *) xmlStrndup(mime, last - mime);
746 charset = xmlStrstr(BAD_CAST ctxt->contentType, BAD_CAST "charset=");
747 if (charset != NULL) {
748 charset += 8;
749 last = charset;
750 while ((*last != 0) && (*last != ' ') && (*last != '\t') &&
751 (*last != ';') && (*last != ','))
752 last++;
753 if (ctxt->encoding != NULL)
754 xmlFree(ctxt->encoding);
755 ctxt->encoding = (char *) xmlStrndup(charset, last - charset);
756 }
Owen Taylor3473f882001-02-23 17:55:21 +0000757 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"ContentType:", 12)) {
Daniel Veillarda840b692003-10-19 13:35:37 +0000758 const xmlChar *charset, *last, *mime;
Owen Taylor3473f882001-02-23 17:55:21 +0000759 cur += 12;
760 if (ctxt->contentType != NULL) return;
761 while ((*cur == ' ') || (*cur == '\t')) cur++;
762 ctxt->contentType = xmlMemStrdup(cur);
Daniel Veillarda840b692003-10-19 13:35:37 +0000763 mime = (const xmlChar *) cur;
764 last = mime;
765 while ((*last != 0) && (*last != ' ') && (*last != '\t') &&
766 (*last != ';') && (*last != ','))
767 last++;
768 if (ctxt->mimeType != NULL)
769 xmlFree(ctxt->mimeType);
770 ctxt->mimeType = (char *) xmlStrndup(mime, last - mime);
771 charset = xmlStrstr(BAD_CAST ctxt->contentType, BAD_CAST "charset=");
772 if (charset != NULL) {
773 charset += 8;
774 last = charset;
775 while ((*last != 0) && (*last != ' ') && (*last != '\t') &&
776 (*last != ';') && (*last != ','))
777 last++;
778 if (ctxt->encoding != NULL)
779 xmlFree(ctxt->encoding);
780 ctxt->encoding = (char *) xmlStrndup(charset, last - charset);
781 }
Owen Taylor3473f882001-02-23 17:55:21 +0000782 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Location:", 9)) {
783 cur += 9;
784 while ((*cur == ' ') || (*cur == '\t')) cur++;
785 if (ctxt->location != NULL)
786 xmlFree(ctxt->location);
William M. Brack7e29c0a2004-04-02 09:07:22 +0000787 if (*cur == '/') {
788 xmlChar *tmp_http = xmlStrdup(BAD_CAST "http://");
789 xmlChar *tmp_loc =
790 xmlStrcat(tmp_http, (const xmlChar *) ctxt->hostname);
791 ctxt->location =
792 (char *) xmlStrcat (tmp_loc, (const xmlChar *) cur);
793 } else {
794 ctxt->location = xmlMemStrdup(cur);
795 }
Owen Taylor3473f882001-02-23 17:55:21 +0000796 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"WWW-Authenticate:", 17)) {
797 cur += 17;
798 while ((*cur == ' ') || (*cur == '\t')) cur++;
799 if (ctxt->authHeader != NULL)
800 xmlFree(ctxt->authHeader);
801 ctxt->authHeader = xmlMemStrdup(cur);
802 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Proxy-Authenticate:", 19)) {
803 cur += 19;
804 while ((*cur == ' ') || (*cur == '\t')) cur++;
805 if (ctxt->authHeader != NULL)
806 xmlFree(ctxt->authHeader);
807 ctxt->authHeader = xmlMemStrdup(cur);
Daniel Veillard9a2724d2005-12-15 11:12:26 +0000808#ifdef HAVE_ZLIB_H
809 } else if ( !xmlStrncasecmp( BAD_CAST line, BAD_CAST"Content-Encoding:", 17) ) {
810 cur += 17;
811 while ((*cur == ' ') || (*cur == '\t')) cur++;
812 if ( !xmlStrncasecmp( BAD_CAST cur, BAD_CAST"gzip", 4) ) {
813 ctxt->usesGzip = 1;
814
815 ctxt->strm = xmlMalloc(sizeof(z_stream));
816
817 if (ctxt->strm != NULL) {
818 ctxt->strm->zalloc = Z_NULL;
819 ctxt->strm->zfree = Z_NULL;
820 ctxt->strm->opaque = Z_NULL;
821 ctxt->strm->avail_in = 0;
822 ctxt->strm->next_in = Z_NULL;
823
824 inflateInit2( ctxt->strm, 31 );
825 }
826 }
827#endif
Daniel Veillardf012a642001-07-23 19:10:52 +0000828 } else if ( !xmlStrncasecmp( BAD_CAST line, BAD_CAST"Content-Length:", 15) ) {
829 cur += 15;
830 ctxt->ContentLength = strtol( cur, NULL, 10 );
Owen Taylor3473f882001-02-23 17:55:21 +0000831 }
832}
833
834/**
835 * xmlNanoHTTPConnectAttempt:
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000836 * @addr: a socket address structure
Owen Taylor3473f882001-02-23 17:55:21 +0000837 *
838 * Attempt a connection to the given IP:port endpoint. It forces
839 * non-blocking semantic on the socket, and allow 60 seconds for
840 * the host to answer.
841 *
842 * Returns -1 in case of failure, the file descriptor number otherwise
843 */
844
845static int
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000846xmlNanoHTTPConnectAttempt(struct sockaddr *addr)
Owen Taylor3473f882001-02-23 17:55:21 +0000847{
Raphael Prevost48b60c32009-08-23 13:11:01 +0200848#ifndef HAVE_POLL_H
Owen Taylor3473f882001-02-23 17:55:21 +0000849 fd_set wfd;
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000850#ifdef _WINSOCKAPI_
851 fd_set xfd;
852#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000853 struct timeval tv;
Raphael Prevost48b60c32009-08-23 13:11:01 +0200854#else /* !HAVE_POLL_H */
855 struct pollfd p;
856#endif /* !HAVE_POLL_H */
Owen Taylor3473f882001-02-23 17:55:21 +0000857 int status;
Raphael Prevost48b60c32009-08-23 13:11:01 +0200858
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000859 int addrlen;
Raphael Prevost48b60c32009-08-23 13:11:01 +0200860
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000861 SOCKET s;
Raphael Prevost48b60c32009-08-23 13:11:01 +0200862
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000863#ifdef SUPPORT_IP6
864 if (addr->sa_family == AF_INET6) {
Raphael Prevost48b60c32009-08-23 13:11:01 +0200865 s = socket(PF_INET6, SOCK_STREAM, IPPROTO_TCP);
866 addrlen = sizeof(struct sockaddr_in6);
867 } else
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000868#endif
869 {
Raphael Prevost48b60c32009-08-23 13:11:01 +0200870 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
871 addrlen = sizeof(struct sockaddr_in);
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000872 }
Raphael Prevost48b60c32009-08-23 13:11:01 +0200873 if (s == -1) {
Owen Taylor3473f882001-02-23 17:55:21 +0000874#ifdef DEBUG_HTTP
Raphael Prevost48b60c32009-08-23 13:11:01 +0200875 perror("socket");
Owen Taylor3473f882001-02-23 17:55:21 +0000876#endif
Raphael Prevost48b60c32009-08-23 13:11:01 +0200877 __xmlIOErr(XML_FROM_HTTP, 0, "socket failed\n");
878 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +0000879 }
Owen Taylor3473f882001-02-23 17:55:21 +0000880#ifdef _WINSOCKAPI_
881 {
Raphael Prevost48b60c32009-08-23 13:11:01 +0200882 u_long one = 1;
Owen Taylor3473f882001-02-23 17:55:21 +0000883
Raphael Prevost48b60c32009-08-23 13:11:01 +0200884 status = ioctlsocket(s, FIONBIO, &one) == SOCKET_ERROR ? -1 : 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000885 }
886#else /* _WINSOCKAPI_ */
887#if defined(VMS)
888 {
Raphael Prevost48b60c32009-08-23 13:11:01 +0200889 int enable = 1;
890
891 status = ioctl(s, FIONBIO, &enable);
Owen Taylor3473f882001-02-23 17:55:21 +0000892 }
893#else /* VMS */
Daniel Veillardcba68392008-08-29 12:43:40 +0000894#if defined(__BEOS__) && !defined(__HAIKU__)
Raphael Prevost48b60c32009-08-23 13:11:01 +0200895 {
896 bool noblock = true;
897
898 status =
899 setsockopt(s, SOL_SOCKET, SO_NONBLOCK, &noblock,
900 sizeof(noblock));
901 }
Daniel Veillard254b1262003-11-01 17:04:58 +0000902#else /* __BEOS__ */
Owen Taylor3473f882001-02-23 17:55:21 +0000903 if ((status = fcntl(s, F_GETFL, 0)) != -1) {
904#ifdef O_NONBLOCK
Raphael Prevost48b60c32009-08-23 13:11:01 +0200905 status |= O_NONBLOCK;
Owen Taylor3473f882001-02-23 17:55:21 +0000906#else /* O_NONBLOCK */
907#ifdef F_NDELAY
Raphael Prevost48b60c32009-08-23 13:11:01 +0200908 status |= F_NDELAY;
Owen Taylor3473f882001-02-23 17:55:21 +0000909#endif /* F_NDELAY */
910#endif /* !O_NONBLOCK */
Raphael Prevost48b60c32009-08-23 13:11:01 +0200911 status = fcntl(s, F_SETFL, status);
Owen Taylor3473f882001-02-23 17:55:21 +0000912 }
913 if (status < 0) {
914#ifdef DEBUG_HTTP
Raphael Prevost48b60c32009-08-23 13:11:01 +0200915 perror("nonblocking");
Owen Taylor3473f882001-02-23 17:55:21 +0000916#endif
Raphael Prevost48b60c32009-08-23 13:11:01 +0200917 __xmlIOErr(XML_FROM_HTTP, 0, "error setting non-blocking IO\n");
918 closesocket(s);
919 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +0000920 }
Daniel Veillard254b1262003-11-01 17:04:58 +0000921#endif /* !__BEOS__ */
Owen Taylor3473f882001-02-23 17:55:21 +0000922#endif /* !VMS */
923#endif /* !_WINSOCKAPI_ */
924
Raphael Prevost48b60c32009-08-23 13:11:01 +0200925 if (connect(s, addr, addrlen) == -1) {
926 switch (socket_errno()) {
927 case EINPROGRESS:
928 case EWOULDBLOCK:
929 break;
930 default:
931 __xmlIOErr(XML_FROM_HTTP, 0,
932 "error connecting to HTTP server");
933 closesocket(s);
934 return (-1);
935 }
936 }
937#ifndef HAVE_POLL_H
Owen Taylor3473f882001-02-23 17:55:21 +0000938 tv.tv_sec = timeout;
939 tv.tv_usec = 0;
Daniel Veillard9e2110b2005-08-08 20:33:54 +0000940
941#ifdef _MSC_VER
942#pragma warning(push)
943#pragma warning(disable: 4018)
944#endif
spadixd29a5c82009-10-19 14:03:25 +0200945#ifndef _WINSOCKAPI_
Raphael Prevost48b60c32009-08-23 13:11:01 +0200946 if (s > FD_SETSIZE)
947 return -1;
spadixd29a5c82009-10-19 14:03:25 +0200948#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000949 FD_ZERO(&wfd);
950 FD_SET(s, &wfd);
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000951
Raphael Prevost48b60c32009-08-23 13:11:01 +0200952#ifdef _WINSOCKAPI_
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000953 FD_ZERO(&xfd);
954 FD_SET(s, &xfd);
Raphael Prevost48b60c32009-08-23 13:11:01 +0200955
956 switch (select(s + 1, NULL, &wfd, &xfd, &tv))
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000957#else
Raphael Prevost48b60c32009-08-23 13:11:01 +0200958 switch (select(s + 1, NULL, &wfd, NULL, &tv))
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000959#endif
Daniel Veillard9e2110b2005-08-08 20:33:54 +0000960#ifdef _MSC_VER
961#pragma warning(pop)
962#endif
Raphael Prevost48b60c32009-08-23 13:11:01 +0200963
964#else /* !HAVE_POLL_H */
965 p.fd = s;
966 p.events = POLLOUT;
967 switch (poll(&p, 1, timeout * 1000))
968#endif /* !HAVE_POLL_H */
969
Owen Taylor3473f882001-02-23 17:55:21 +0000970 {
Raphael Prevost48b60c32009-08-23 13:11:01 +0200971 case 0:
972 /* Time out */
973 __xmlIOErr(XML_FROM_HTTP, 0, "Connect attempt timed out");
974 closesocket(s);
975 return (-1);
976 case -1:
977 /* Ermm.. ?? */
978 __xmlIOErr(XML_FROM_HTTP, 0, "Connect failed");
979 closesocket(s);
980 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +0000981 }
982
Raphael Prevost48b60c32009-08-23 13:11:01 +0200983#ifndef HAVE_POLL_H
984 if (FD_ISSET(s, &wfd)
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000985#ifdef _WINSOCKAPI_
Raphael Prevost48b60c32009-08-23 13:11:01 +0200986 || FD_ISSET(s, &xfd)
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000987#endif
Raphael Prevost48b60c32009-08-23 13:11:01 +0200988 )
989#else /* !HAVE_POLL_H */
990 if (p.revents == POLLOUT)
991#endif /* !HAVE_POLL_H */
992 {
993 XML_SOCKLEN_T len;
994
995 len = sizeof(status);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000996#ifdef SO_ERROR
Raphael Prevost48b60c32009-08-23 13:11:01 +0200997 if (getsockopt(s, SOL_SOCKET, SO_ERROR, (char *) &status, &len) <
998 0) {
999 /* Solaris error code */
1000 __xmlIOErr(XML_FROM_HTTP, 0, "getsockopt failed\n");
1001 return (-1);
1002 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001003#endif
Raphael Prevost48b60c32009-08-23 13:11:01 +02001004 if (status) {
1005 __xmlIOErr(XML_FROM_HTTP, 0,
1006 "Error connecting to remote host");
1007 closesocket(s);
1008 errno = status;
1009 return (-1);
1010 }
Owen Taylor3473f882001-02-23 17:55:21 +00001011 } else {
Raphael Prevost48b60c32009-08-23 13:11:01 +02001012 /* pbm */
1013 __xmlIOErr(XML_FROM_HTTP, 0, "select failed\n");
1014 closesocket(s);
1015 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001016 }
Raphael Prevost48b60c32009-08-23 13:11:01 +02001017
1018 return (s);
Owen Taylor3473f882001-02-23 17:55:21 +00001019}
Raphael Prevost48b60c32009-08-23 13:11:01 +02001020
Owen Taylor3473f882001-02-23 17:55:21 +00001021/**
1022 * xmlNanoHTTPConnectHost:
1023 * @host: the host name
1024 * @port: the port number
1025 *
1026 * Attempt a connection to the given host:port endpoint. It tries
1027 * the multiple IP provided by the DNS if available.
1028 *
1029 * Returns -1 in case of failure, the file descriptor number otherwise
1030 */
1031
1032static int
1033xmlNanoHTTPConnectHost(const char *host, int port)
1034{
1035 struct hostent *h;
Daniel Veillard2db8c122003-07-08 12:16:59 +00001036 struct sockaddr *addr = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001037 struct in_addr ia;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001038 struct sockaddr_in sockin;
Daniel Veillard5c396542002-03-15 07:57:50 +00001039
Owen Taylor3473f882001-02-23 17:55:21 +00001040#ifdef SUPPORT_IP6
1041 struct in6_addr ia6;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001042 struct sockaddr_in6 sockin6;
Owen Taylor3473f882001-02-23 17:55:21 +00001043#endif
1044 int i;
1045 int s;
Daniel Veillard5c396542002-03-15 07:57:50 +00001046
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001047 memset (&sockin, 0, sizeof(sockin));
1048#ifdef SUPPORT_IP6
1049 memset (&sockin6, 0, sizeof(sockin6));
Rob Richardscb418de2005-10-13 23:12:42 +00001050#endif
1051
1052#if !defined(HAVE_GETADDRINFO) && defined(SUPPORT_IP6) && defined(RES_USE_INET6)
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001053 if (have_ipv6 ())
Daniel Veillard560c2a42003-07-06 21:13:49 +00001054 {
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001055 if (!(_res.options & RES_INIT))
1056 res_init();
1057 _res.options |= RES_USE_INET6;
1058 }
Rob Richardscb418de2005-10-13 23:12:42 +00001059#endif
1060
1061#if defined(HAVE_GETADDRINFO) && defined(SUPPORT_IP6) && !defined(_WIN32)
1062 if (have_ipv6 ())
1063#endif
1064#if defined(HAVE_GETADDRINFO) && (defined(SUPPORT_IP6) || defined(_WIN32))
Daniel Veillard560c2a42003-07-06 21:13:49 +00001065 {
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001066 int status;
1067 struct addrinfo hints, *res, *result;
1068
1069 result = NULL;
1070 memset (&hints, 0,sizeof(hints));
1071 hints.ai_socktype = SOCK_STREAM;
1072
1073 status = getaddrinfo (host, NULL, &hints, &result);
1074 if (status) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001075 __xmlIOErr(XML_FROM_HTTP, 0, "getaddrinfo failed\n");
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001076 return (-1);
1077 }
1078
1079 for (res = result; res; res = res->ai_next) {
Rob Richardscb418de2005-10-13 23:12:42 +00001080 if (res->ai_family == AF_INET) {
1081 if (res->ai_addrlen > sizeof(sockin)) {
1082 __xmlIOErr(XML_FROM_HTTP, 0, "address size mismatch\n");
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001083 freeaddrinfo (result);
Rob Richardscb418de2005-10-13 23:12:42 +00001084 return (-1);
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001085 }
Rob Richardscb418de2005-10-13 23:12:42 +00001086 memcpy (&sockin, res->ai_addr, res->ai_addrlen);
1087 sockin.sin_port = htons (port);
1088 addr = (struct sockaddr *)&sockin;
1089#ifdef SUPPORT_IP6
1090 } else if (have_ipv6 () && (res->ai_family == AF_INET6)) {
1091 if (res->ai_addrlen > sizeof(sockin6)) {
1092 __xmlIOErr(XML_FROM_HTTP, 0, "address size mismatch\n");
1093 freeaddrinfo (result);
1094 return (-1);
1095 }
1096 memcpy (&sockin6, res->ai_addr, res->ai_addrlen);
1097 sockin6.sin6_port = htons (port);
1098 addr = (struct sockaddr *)&sockin6;
1099#endif
1100 } else
1101 continue; /* for */
1102
1103 s = xmlNanoHTTPConnectAttempt (addr);
1104 if (s != -1) {
1105 freeaddrinfo (result);
1106 return (s);
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001107 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001108 }
Rob Richardscb418de2005-10-13 23:12:42 +00001109
Daniel Veillard3dc93a42003-07-10 14:04:33 +00001110 if (result)
1111 freeaddrinfo (result);
Rob Richardscb418de2005-10-13 23:12:42 +00001112 }
Owen Taylor3473f882001-02-23 17:55:21 +00001113#endif
Rob Richardscb418de2005-10-13 23:12:42 +00001114#if defined(HAVE_GETADDRINFO) && defined(SUPPORT_IP6) && !defined(_WIN32)
1115 else
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001116#endif
Rob Richardscb418de2005-10-13 23:12:42 +00001117#if !defined(HAVE_GETADDRINFO) || !defined(_WIN32)
1118 {
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001119 h = gethostbyname (host);
1120 if (h == NULL) {
Daniel Veillard56b2db72002-03-25 16:35:28 +00001121
1122/*
1123 * Okay, I got fed up by the non-portability of this error message
1124 * extraction code. it work on Linux, if it work on your platform
1125 * and one want to enable it, send me the defined(foobar) needed
1126 */
1127#if defined(HAVE_NETDB_H) && defined(HOST_NOT_FOUND) && defined(linux)
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001128 const char *h_err_txt = "";
Daniel Veillardf012a642001-07-23 19:10:52 +00001129
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001130 switch (h_errno) {
1131 case HOST_NOT_FOUND:
1132 h_err_txt = "Authoritive host not found";
1133 break;
Daniel Veillardf012a642001-07-23 19:10:52 +00001134
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001135 case TRY_AGAIN:
1136 h_err_txt =
1137 "Non-authoritive host not found or server failure.";
1138 break;
Daniel Veillardf012a642001-07-23 19:10:52 +00001139
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001140 case NO_RECOVERY:
1141 h_err_txt =
1142 "Non-recoverable errors: FORMERR, REFUSED, or NOTIMP.";
1143 break;
Daniel Veillard5c396542002-03-15 07:57:50 +00001144
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001145 case NO_ADDRESS:
1146 h_err_txt =
1147 "Valid name, no data record of requested type.";
1148 break;
Daniel Veillard5c396542002-03-15 07:57:50 +00001149
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001150 default:
1151 h_err_txt = "No error text defined.";
1152 break;
1153 }
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001154 __xmlIOErr(XML_FROM_HTTP, 0, h_err_txt);
Daniel Veillard5c396542002-03-15 07:57:50 +00001155#else
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001156 __xmlIOErr(XML_FROM_HTTP, 0, "Failed to resolve host");
Owen Taylor3473f882001-02-23 17:55:21 +00001157#endif
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001158 return (-1);
1159 }
Daniel Veillard5c396542002-03-15 07:57:50 +00001160
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001161 for (i = 0; h->h_addr_list[i]; i++) {
1162 if (h->h_addrtype == AF_INET) {
1163 /* A records (IPv4) */
Daniel Veillard8e2c9792004-10-27 09:39:50 +00001164 if ((unsigned int) h->h_length > sizeof(ia)) {
1165 __xmlIOErr(XML_FROM_HTTP, 0, "address size mismatch\n");
1166 return (-1);
1167 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001168 memcpy (&ia, h->h_addr_list[i], h->h_length);
1169 sockin.sin_family = h->h_addrtype;
1170 sockin.sin_addr = ia;
Daniel Veillard9e2110b2005-08-08 20:33:54 +00001171 sockin.sin_port = (u_short)htons ((unsigned short)port);
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001172 addr = (struct sockaddr *) &sockin;
Daniel Veillard5c396542002-03-15 07:57:50 +00001173#ifdef SUPPORT_IP6
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001174 } else if (have_ipv6 () && (h->h_addrtype == AF_INET6)) {
1175 /* AAAA records (IPv6) */
Daniel Veillard8e2c9792004-10-27 09:39:50 +00001176 if ((unsigned int) h->h_length > sizeof(ia6)) {
1177 __xmlIOErr(XML_FROM_HTTP, 0, "address size mismatch\n");
1178 return (-1);
1179 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001180 memcpy (&ia6, h->h_addr_list[i], h->h_length);
1181 sockin6.sin6_family = h->h_addrtype;
1182 sockin6.sin6_addr = ia6;
1183 sockin6.sin6_port = htons (port);
1184 addr = (struct sockaddr *) &sockin6;
Daniel Veillard5c396542002-03-15 07:57:50 +00001185#endif
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001186 } else
1187 break; /* for */
Daniel Veillard5c396542002-03-15 07:57:50 +00001188
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001189 s = xmlNanoHTTPConnectAttempt (addr);
1190 if (s != -1)
1191 return (s);
1192 }
Owen Taylor3473f882001-02-23 17:55:21 +00001193 }
Rob Richardscb418de2005-10-13 23:12:42 +00001194#endif
1195
Owen Taylor3473f882001-02-23 17:55:21 +00001196#ifdef DEBUG_HTTP
1197 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard5c396542002-03-15 07:57:50 +00001198 "xmlNanoHTTPConnectHost: unable to connect to '%s'.\n",
1199 host);
Owen Taylor3473f882001-02-23 17:55:21 +00001200#endif
Daniel Veillard5c396542002-03-15 07:57:50 +00001201 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001202}
1203
1204
1205/**
1206 * xmlNanoHTTPOpen:
1207 * @URL: The URL to load
1208 * @contentType: if available the Content-Type information will be
1209 * returned at that location
1210 *
1211 * This function try to open a connection to the indicated resource
1212 * via HTTP GET.
1213 *
1214 * Returns NULL in case of failure, otherwise a request handler.
1215 * The contentType, if provided must be freed by the caller
1216 */
1217
1218void*
1219xmlNanoHTTPOpen(const char *URL, char **contentType) {
1220 if (contentType != NULL) *contentType = NULL;
Daniel Veillardf012a642001-07-23 19:10:52 +00001221 return(xmlNanoHTTPMethod(URL, NULL, NULL, contentType, NULL, 0));
Daniel Veillard9403a042001-05-28 11:00:53 +00001222}
1223
1224/**
1225 * xmlNanoHTTPOpenRedir:
1226 * @URL: The URL to load
1227 * @contentType: if available the Content-Type information will be
1228 * returned at that location
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001229 * @redir: if available the redirected URL will be returned
Daniel Veillard9403a042001-05-28 11:00:53 +00001230 *
1231 * This function try to open a connection to the indicated resource
1232 * via HTTP GET.
1233 *
1234 * Returns NULL in case of failure, otherwise a request handler.
1235 * The contentType, if provided must be freed by the caller
1236 */
1237
1238void*
1239xmlNanoHTTPOpenRedir(const char *URL, char **contentType, char **redir) {
1240 if (contentType != NULL) *contentType = NULL;
1241 if (redir != NULL) *redir = NULL;
Daniel Veillardf012a642001-07-23 19:10:52 +00001242 return(xmlNanoHTTPMethodRedir(URL, NULL, NULL, contentType, redir, NULL,0));
Owen Taylor3473f882001-02-23 17:55:21 +00001243}
1244
1245/**
1246 * xmlNanoHTTPRead:
1247 * @ctx: the HTTP context
1248 * @dest: a buffer
1249 * @len: the buffer length
1250 *
1251 * This function tries to read @len bytes from the existing HTTP connection
1252 * and saves them in @dest. This is a blocking call.
1253 *
1254 * Returns the number of byte read. 0 is an indication of an end of connection.
1255 * -1 indicates a parameter error.
1256 */
1257int
1258xmlNanoHTTPRead(void *ctx, void *dest, int len) {
1259 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
Daniel Veillard9a2724d2005-12-15 11:12:26 +00001260#ifdef HAVE_ZLIB_H
1261 int bytes_read = 0;
1262 int orig_avail_in;
1263 int z_ret;
1264#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001265
1266 if (ctx == NULL) return(-1);
1267 if (dest == NULL) return(-1);
1268 if (len <= 0) return(0);
1269
Daniel Veillard9a2724d2005-12-15 11:12:26 +00001270#ifdef HAVE_ZLIB_H
1271 if (ctxt->usesGzip == 1) {
1272 if (ctxt->strm == NULL) return(0);
1273
1274 ctxt->strm->next_out = dest;
1275 ctxt->strm->avail_out = len;
William M. Bracke8827652007-05-16 05:19:13 +00001276 ctxt->strm->avail_in = ctxt->inptr - ctxt->inrptr;
Daniel Veillard9a2724d2005-12-15 11:12:26 +00001277
William M. Bracke8827652007-05-16 05:19:13 +00001278 while (ctxt->strm->avail_out > 0 &&
1279 (ctxt->strm->avail_in > 0 || xmlNanoHTTPRecv(ctxt) > 0)) {
William M. Brackd2f682a2007-05-15 19:42:08 +00001280 orig_avail_in = ctxt->strm->avail_in =
1281 ctxt->inptr - ctxt->inrptr - bytes_read;
Daniel Veillard9a2724d2005-12-15 11:12:26 +00001282 ctxt->strm->next_in = BAD_CAST (ctxt->inrptr + bytes_read);
1283
1284 z_ret = inflate(ctxt->strm, Z_NO_FLUSH);
1285 bytes_read += orig_avail_in - ctxt->strm->avail_in;
1286
1287 if (z_ret != Z_OK) break;
William M. Brackd2f682a2007-05-15 19:42:08 +00001288 }
Daniel Veillard9a2724d2005-12-15 11:12:26 +00001289
1290 ctxt->inrptr += bytes_read;
1291 return(len - ctxt->strm->avail_out);
1292 }
1293#endif
1294
Owen Taylor3473f882001-02-23 17:55:21 +00001295 while (ctxt->inptr - ctxt->inrptr < len) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001296 if (xmlNanoHTTPRecv(ctxt) <= 0) break;
Owen Taylor3473f882001-02-23 17:55:21 +00001297 }
1298 if (ctxt->inptr - ctxt->inrptr < len)
1299 len = ctxt->inptr - ctxt->inrptr;
1300 memcpy(dest, ctxt->inrptr, len);
1301 ctxt->inrptr += len;
1302 return(len);
1303}
1304
1305/**
1306 * xmlNanoHTTPClose:
1307 * @ctx: the HTTP context
1308 *
1309 * This function closes an HTTP context, it ends up the connection and
1310 * free all data related to it.
1311 */
1312void
1313xmlNanoHTTPClose(void *ctx) {
1314 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1315
1316 if (ctx == NULL) return;
1317
1318 xmlNanoHTTPFreeCtxt(ctxt);
1319}
1320
1321/**
Daniel Veillard9403a042001-05-28 11:00:53 +00001322 * xmlNanoHTTPMethodRedir:
Owen Taylor3473f882001-02-23 17:55:21 +00001323 * @URL: The URL to load
1324 * @method: the HTTP method to use
1325 * @input: the input string if any
1326 * @contentType: the Content-Type information IN and OUT
Daniel Veillard9403a042001-05-28 11:00:53 +00001327 * @redir: the redirected URL OUT
Owen Taylor3473f882001-02-23 17:55:21 +00001328 * @headers: the extra headers
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001329 * @ilen: input length
Owen Taylor3473f882001-02-23 17:55:21 +00001330 *
1331 * This function try to open a connection to the indicated resource
1332 * via HTTP using the given @method, adding the given extra headers
1333 * and the input buffer for the request content.
1334 *
1335 * Returns NULL in case of failure, otherwise a request handler.
Daniel Veillard9403a042001-05-28 11:00:53 +00001336 * The contentType, or redir, if provided must be freed by the caller
Owen Taylor3473f882001-02-23 17:55:21 +00001337 */
1338
1339void*
Daniel Veillard9403a042001-05-28 11:00:53 +00001340xmlNanoHTTPMethodRedir(const char *URL, const char *method, const char *input,
Daniel Veillardf012a642001-07-23 19:10:52 +00001341 char **contentType, char **redir,
1342 const char *headers, int ilen ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001343 xmlNanoHTTPCtxtPtr ctxt;
1344 char *bp, *p;
Daniel Veillardf012a642001-07-23 19:10:52 +00001345 int blen, ret;
Owen Taylor3473f882001-02-23 17:55:21 +00001346 int nbRedirects = 0;
1347 char *redirURL = NULL;
William M. Brack78637da2003-07-31 14:47:38 +00001348#ifdef DEBUG_HTTP
1349 int xmt_bytes;
1350#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001351
1352 if (URL == NULL) return(NULL);
1353 if (method == NULL) method = "GET";
1354 xmlNanoHTTPInit();
1355
1356retry:
1357 if (redirURL == NULL)
1358 ctxt = xmlNanoHTTPNewCtxt(URL);
1359 else {
1360 ctxt = xmlNanoHTTPNewCtxt(redirURL);
Daniel Veillarda840b692003-10-19 13:35:37 +00001361 ctxt->location = xmlMemStrdup(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001362 }
1363
Daniel Veillardf012a642001-07-23 19:10:52 +00001364 if ( ctxt == NULL ) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001365 return ( NULL );
1366 }
1367
Owen Taylor3473f882001-02-23 17:55:21 +00001368 if ((ctxt->protocol == NULL) || (strcmp(ctxt->protocol, "http"))) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001369 __xmlIOErr(XML_FROM_HTTP, XML_HTTP_URL_SYNTAX, "Not a valid HTTP URI");
Owen Taylor3473f882001-02-23 17:55:21 +00001370 xmlNanoHTTPFreeCtxt(ctxt);
1371 if (redirURL != NULL) xmlFree(redirURL);
1372 return(NULL);
1373 }
1374 if (ctxt->hostname == NULL) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001375 __xmlIOErr(XML_FROM_HTTP, XML_HTTP_UNKNOWN_HOST,
1376 "Failed to identify host in URI");
Owen Taylor3473f882001-02-23 17:55:21 +00001377 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001378 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001379 return(NULL);
1380 }
1381 if (proxy) {
1382 blen = strlen(ctxt->hostname) * 2 + 16;
1383 ret = xmlNanoHTTPConnectHost(proxy, proxyPort);
1384 }
1385 else {
1386 blen = strlen(ctxt->hostname);
1387 ret = xmlNanoHTTPConnectHost(ctxt->hostname, ctxt->port);
1388 }
1389 if (ret < 0) {
1390 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001391 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001392 return(NULL);
1393 }
1394 ctxt->fd = ret;
1395
Daniel Veillardf012a642001-07-23 19:10:52 +00001396 if (input == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00001397 ilen = 0;
Daniel Veillardf012a642001-07-23 19:10:52 +00001398 else
1399 blen += 36;
1400
Owen Taylor3473f882001-02-23 17:55:21 +00001401 if (headers != NULL)
Daniel Veillardf012a642001-07-23 19:10:52 +00001402 blen += strlen(headers) + 2;
Owen Taylor3473f882001-02-23 17:55:21 +00001403 if (contentType && *contentType)
William M. Brackead35832008-02-06 04:12:46 +00001404 /* reserve for string plus 'Content-Type: \r\n" */
Owen Taylor3473f882001-02-23 17:55:21 +00001405 blen += strlen(*contentType) + 16;
Daniel Veillard351f2d62005-04-13 02:55:12 +00001406 if (ctxt->query != NULL)
William M. Brackead35832008-02-06 04:12:46 +00001407 /* 1 for '?' */
Daniel Veillard351f2d62005-04-13 02:55:12 +00001408 blen += strlen(ctxt->query) + 1;
Daniel Veillardf012a642001-07-23 19:10:52 +00001409 blen += strlen(method) + strlen(ctxt->path) + 24;
Daniel Veillard9a2724d2005-12-15 11:12:26 +00001410#ifdef HAVE_ZLIB_H
William M. Brackead35832008-02-06 04:12:46 +00001411 /* reserve for possible 'Accept-Encoding: gzip' string */
Daniel Veillard9a2724d2005-12-15 11:12:26 +00001412 blen += 23;
1413#endif
William M. Brackead35832008-02-06 04:12:46 +00001414 if (ctxt->port != 80) {
1415 /* reserve space for ':xxxxx', incl. potential proxy */
1416 if (proxy)
1417 blen += 12;
1418 else
1419 blen += 6;
1420 }
Daniel Veillard82cb3192003-10-29 13:39:15 +00001421 bp = (char*)xmlMallocAtomic(blen);
Daniel Veillardf012a642001-07-23 19:10:52 +00001422 if ( bp == NULL ) {
1423 xmlNanoHTTPFreeCtxt( ctxt );
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001424 xmlHTTPErrMemory("allocating header buffer");
Daniel Veillardf012a642001-07-23 19:10:52 +00001425 return ( NULL );
1426 }
1427
1428 p = bp;
1429
Owen Taylor3473f882001-02-23 17:55:21 +00001430 if (proxy) {
1431 if (ctxt->port != 80) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001432 p += snprintf( p, blen - (p - bp), "%s http://%s:%d%s",
1433 method, ctxt->hostname,
Daniel Veillardf012a642001-07-23 19:10:52 +00001434 ctxt->port, ctxt->path );
Owen Taylor3473f882001-02-23 17:55:21 +00001435 }
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001436 else
1437 p += snprintf( p, blen - (p - bp), "%s http://%s%s", method,
Daniel Veillardf012a642001-07-23 19:10:52 +00001438 ctxt->hostname, ctxt->path);
Owen Taylor3473f882001-02-23 17:55:21 +00001439 }
1440 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001441 p += snprintf( p, blen - (p - bp), "%s %s", method, ctxt->path);
Daniel Veillardf012a642001-07-23 19:10:52 +00001442
Daniel Veillard351f2d62005-04-13 02:55:12 +00001443 if (ctxt->query != NULL)
1444 p += snprintf( p, blen - (p - bp), "?%s", ctxt->query);
1445
William M. Brackec720082007-08-24 02:57:38 +00001446 if (ctxt->port == 80) {
1447 p += snprintf( p, blen - (p - bp), " HTTP/1.0\r\nHost: %s\r\n",
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001448 ctxt->hostname);
William M. Brackec720082007-08-24 02:57:38 +00001449 } else {
1450 p += snprintf( p, blen - (p - bp), " HTTP/1.0\r\nHost: %s:%d\r\n",
1451 ctxt->hostname, ctxt->port);
1452 }
Daniel Veillardf012a642001-07-23 19:10:52 +00001453
Daniel Veillard9a2724d2005-12-15 11:12:26 +00001454#ifdef HAVE_ZLIB_H
1455 p += snprintf(p, blen - (p - bp), "Accept-Encoding: gzip\r\n");
1456#endif
1457
Daniel Veillardf012a642001-07-23 19:10:52 +00001458 if (contentType != NULL && *contentType)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001459 p += snprintf(p, blen - (p - bp), "Content-Type: %s\r\n", *contentType);
Daniel Veillardf012a642001-07-23 19:10:52 +00001460
1461 if (headers != NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001462 p += snprintf( p, blen - (p - bp), "%s", headers );
Daniel Veillardf012a642001-07-23 19:10:52 +00001463
Owen Taylor3473f882001-02-23 17:55:21 +00001464 if (input != NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001465 snprintf(p, blen - (p - bp), "Content-Length: %d\r\n\r\n", ilen );
Owen Taylor3473f882001-02-23 17:55:21 +00001466 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001467 snprintf(p, blen - (p - bp), "\r\n");
Daniel Veillardf012a642001-07-23 19:10:52 +00001468
Owen Taylor3473f882001-02-23 17:55:21 +00001469#ifdef DEBUG_HTTP
1470 xmlGenericError(xmlGenericErrorContext,
1471 "-> %s%s", proxy? "(Proxy) " : "", bp);
1472 if ((blen -= strlen(bp)+1) < 0)
1473 xmlGenericError(xmlGenericErrorContext,
1474 "ERROR: overflowed buffer by %d bytes\n", -blen);
1475#endif
1476 ctxt->outptr = ctxt->out = bp;
1477 ctxt->state = XML_NANO_HTTP_WRITE;
Daniel Veillardf012a642001-07-23 19:10:52 +00001478 blen = strlen( ctxt->out );
Daniel Veillardf012a642001-07-23 19:10:52 +00001479#ifdef DEBUG_HTTP
William M. Brack78637da2003-07-31 14:47:38 +00001480 xmt_bytes = xmlNanoHTTPSend(ctxt, ctxt->out, blen );
Daniel Veillardf012a642001-07-23 19:10:52 +00001481 if ( xmt_bytes != blen )
1482 xmlGenericError( xmlGenericErrorContext,
1483 "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
1484 xmt_bytes, blen,
1485 "bytes of HTTP headers sent to host",
1486 ctxt->hostname );
William M. Brack78637da2003-07-31 14:47:38 +00001487#else
1488 xmlNanoHTTPSend(ctxt, ctxt->out, blen );
Daniel Veillardf012a642001-07-23 19:10:52 +00001489#endif
1490
1491 if ( input != NULL ) {
William M. Brack78637da2003-07-31 14:47:38 +00001492#ifdef DEBUG_HTTP
Daniel Veillardf012a642001-07-23 19:10:52 +00001493 xmt_bytes = xmlNanoHTTPSend( ctxt, input, ilen );
1494
Daniel Veillardf012a642001-07-23 19:10:52 +00001495 if ( xmt_bytes != ilen )
1496 xmlGenericError( xmlGenericErrorContext,
1497 "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
1498 xmt_bytes, ilen,
1499 "bytes of HTTP content sent to host",
1500 ctxt->hostname );
William M. Brack78637da2003-07-31 14:47:38 +00001501#else
1502 xmlNanoHTTPSend( ctxt, input, ilen );
Daniel Veillardf012a642001-07-23 19:10:52 +00001503#endif
1504 }
1505
Owen Taylor3473f882001-02-23 17:55:21 +00001506 ctxt->state = XML_NANO_HTTP_READ;
Owen Taylor3473f882001-02-23 17:55:21 +00001507
1508 while ((p = xmlNanoHTTPReadLine(ctxt)) != NULL) {
Daniel Veillard594e5df2009-09-07 14:58:47 +02001509 if (*p == 0) {
Owen Taylor3473f882001-02-23 17:55:21 +00001510 ctxt->content = ctxt->inrptr;
1511 xmlFree(p);
1512 break;
1513 }
1514 xmlNanoHTTPScanAnswer(ctxt, p);
1515
1516#ifdef DEBUG_HTTP
1517 xmlGenericError(xmlGenericErrorContext, "<- %s\n", p);
1518#endif
1519 xmlFree(p);
1520 }
1521
1522 if ((ctxt->location != NULL) && (ctxt->returnValue >= 300) &&
1523 (ctxt->returnValue < 400)) {
1524#ifdef DEBUG_HTTP
1525 xmlGenericError(xmlGenericErrorContext,
1526 "\nRedirect to: %s\n", ctxt->location);
1527#endif
Daniel Veillardf012a642001-07-23 19:10:52 +00001528 while ( xmlNanoHTTPRecv(ctxt) > 0 ) ;
Owen Taylor3473f882001-02-23 17:55:21 +00001529 if (nbRedirects < XML_NANO_HTTP_MAX_REDIR) {
1530 nbRedirects++;
Daniel Veillard9403a042001-05-28 11:00:53 +00001531 if (redirURL != NULL)
1532 xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001533 redirURL = xmlMemStrdup(ctxt->location);
1534 xmlNanoHTTPFreeCtxt(ctxt);
1535 goto retry;
1536 }
1537 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001538 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001539#ifdef DEBUG_HTTP
1540 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardf012a642001-07-23 19:10:52 +00001541 "xmlNanoHTTPMethodRedir: Too many redirects, aborting ...\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001542#endif
1543 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001544 }
1545
1546 if (contentType != NULL) {
1547 if (ctxt->contentType != NULL)
1548 *contentType = xmlMemStrdup(ctxt->contentType);
1549 else
1550 *contentType = NULL;
1551 }
1552
Daniel Veillard9403a042001-05-28 11:00:53 +00001553 if ((redir != NULL) && (redirURL != NULL)) {
1554 *redir = redirURL;
1555 } else {
1556 if (redirURL != NULL)
1557 xmlFree(redirURL);
1558 if (redir != NULL)
1559 *redir = NULL;
1560 }
1561
Owen Taylor3473f882001-02-23 17:55:21 +00001562#ifdef DEBUG_HTTP
1563 if (ctxt->contentType != NULL)
1564 xmlGenericError(xmlGenericErrorContext,
1565 "\nCode %d, content-type '%s'\n\n",
1566 ctxt->returnValue, ctxt->contentType);
1567 else
1568 xmlGenericError(xmlGenericErrorContext,
1569 "\nCode %d, no content-type\n\n",
1570 ctxt->returnValue);
1571#endif
1572
1573 return((void *) ctxt);
1574}
1575
1576/**
Daniel Veillard9403a042001-05-28 11:00:53 +00001577 * xmlNanoHTTPMethod:
1578 * @URL: The URL to load
1579 * @method: the HTTP method to use
1580 * @input: the input string if any
1581 * @contentType: the Content-Type information IN and OUT
1582 * @headers: the extra headers
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001583 * @ilen: input length
Daniel Veillard9403a042001-05-28 11:00:53 +00001584 *
1585 * This function try to open a connection to the indicated resource
1586 * via HTTP using the given @method, adding the given extra headers
1587 * and the input buffer for the request content.
1588 *
1589 * Returns NULL in case of failure, otherwise a request handler.
1590 * The contentType, if provided must be freed by the caller
1591 */
1592
1593void*
1594xmlNanoHTTPMethod(const char *URL, const char *method, const char *input,
Daniel Veillardf012a642001-07-23 19:10:52 +00001595 char **contentType, const char *headers, int ilen) {
Daniel Veillard9403a042001-05-28 11:00:53 +00001596 return(xmlNanoHTTPMethodRedir(URL, method, input, contentType,
Daniel Veillardf012a642001-07-23 19:10:52 +00001597 NULL, headers, ilen));
Daniel Veillard9403a042001-05-28 11:00:53 +00001598}
1599
1600/**
Owen Taylor3473f882001-02-23 17:55:21 +00001601 * xmlNanoHTTPFetch:
1602 * @URL: The URL to load
1603 * @filename: the filename where the content should be saved
1604 * @contentType: if available the Content-Type information will be
1605 * returned at that location
1606 *
1607 * This function try to fetch the indicated resource via HTTP GET
1608 * and save it's content in the file.
1609 *
1610 * Returns -1 in case of failure, 0 incase of success. The contentType,
1611 * if provided must be freed by the caller
1612 */
1613int
1614xmlNanoHTTPFetch(const char *URL, const char *filename, char **contentType) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001615 void *ctxt = NULL;
1616 char *buf = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001617 int fd;
1618 int len;
1619
William M. Brack015ccb22005-02-13 08:18:52 +00001620 if (filename == NULL) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001621 ctxt = xmlNanoHTTPOpen(URL, contentType);
1622 if (ctxt == NULL) return(-1);
1623
1624 if (!strcmp(filename, "-"))
1625 fd = 0;
1626 else {
1627 fd = open(filename, O_CREAT | O_WRONLY, 00644);
1628 if (fd < 0) {
1629 xmlNanoHTTPClose(ctxt);
1630 if ((contentType != NULL) && (*contentType != NULL)) {
1631 xmlFree(*contentType);
1632 *contentType = NULL;
1633 }
1634 return(-1);
1635 }
1636 }
1637
Daniel Veillardf012a642001-07-23 19:10:52 +00001638 xmlNanoHTTPFetchContent( ctxt, &buf, &len );
1639 if ( len > 0 ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001640 write(fd, buf, len);
1641 }
1642
1643 xmlNanoHTTPClose(ctxt);
1644 close(fd);
1645 return(0);
1646}
1647
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001648#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001649/**
1650 * xmlNanoHTTPSave:
1651 * @ctxt: the HTTP context
1652 * @filename: the filename where the content should be saved
1653 *
1654 * This function saves the output of the HTTP transaction to a file
1655 * It closes and free the context at the end
1656 *
1657 * Returns -1 in case of failure, 0 incase of success.
1658 */
1659int
1660xmlNanoHTTPSave(void *ctxt, const char *filename) {
Daniel Veillarde3924972001-07-25 20:25:21 +00001661 char *buf = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001662 int fd;
1663 int len;
1664
William M. Brack015ccb22005-02-13 08:18:52 +00001665 if ((ctxt == NULL) || (filename == NULL)) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001666
1667 if (!strcmp(filename, "-"))
1668 fd = 0;
1669 else {
Daniel Veillardcd2ebab2007-08-23 20:47:33 +00001670 fd = open(filename, O_CREAT | O_WRONLY, 0666);
Owen Taylor3473f882001-02-23 17:55:21 +00001671 if (fd < 0) {
1672 xmlNanoHTTPClose(ctxt);
1673 return(-1);
1674 }
1675 }
1676
Daniel Veillardf012a642001-07-23 19:10:52 +00001677 xmlNanoHTTPFetchContent( ctxt, &buf, &len );
1678 if ( len > 0 ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001679 write(fd, buf, len);
1680 }
1681
1682 xmlNanoHTTPClose(ctxt);
William M. Brack20d82362004-03-17 08:44:46 +00001683 close(fd);
Owen Taylor3473f882001-02-23 17:55:21 +00001684 return(0);
1685}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001686#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001687
1688/**
1689 * xmlNanoHTTPReturnCode:
1690 * @ctx: the HTTP context
1691 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001692 * Get the latest HTTP return code received
1693 *
Owen Taylor3473f882001-02-23 17:55:21 +00001694 * Returns the HTTP return code for the request.
1695 */
1696int
1697xmlNanoHTTPReturnCode(void *ctx) {
1698 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1699
1700 if (ctxt == NULL) return(-1);
1701
1702 return(ctxt->returnValue);
1703}
1704
1705/**
1706 * xmlNanoHTTPAuthHeader:
1707 * @ctx: the HTTP context
1708 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001709 * Get the authentication header of an HTTP context
1710 *
Owen Taylor3473f882001-02-23 17:55:21 +00001711 * Returns the stashed value of the WWW-Authenticate or Proxy-Authenticate
1712 * header.
1713 */
1714const char *
1715xmlNanoHTTPAuthHeader(void *ctx) {
1716 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1717
1718 if (ctxt == NULL) return(NULL);
1719
1720 return(ctxt->authHeader);
1721}
1722
Daniel Veillardf012a642001-07-23 19:10:52 +00001723/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00001724 * xmlNanoHTTPContentLength:
Daniel Veillardf012a642001-07-23 19:10:52 +00001725 * @ctx: the HTTP context
1726 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001727 * Provides the specified content length from the HTTP header.
1728 *
Daniel Veillardf012a642001-07-23 19:10:52 +00001729 * Return the specified content length from the HTTP header. Note that
1730 * a value of -1 indicates that the content length element was not included in
1731 * the response header.
1732 */
1733int
1734xmlNanoHTTPContentLength( void * ctx ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001735 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillardf012a642001-07-23 19:10:52 +00001736
1737 return ( ( ctxt == NULL ) ? -1 : ctxt->ContentLength );
1738}
1739
1740/**
Daniel Veillard847332a2003-10-18 11:29:40 +00001741 * xmlNanoHTTPRedir:
1742 * @ctx: the HTTP context
1743 *
1744 * Provides the specified redirection URL if available from the HTTP header.
1745 *
1746 * Return the specified redirection URL or NULL if not redirected.
1747 */
1748const char *
1749xmlNanoHTTPRedir( void * ctx ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001750 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillard847332a2003-10-18 11:29:40 +00001751
1752 return ( ( ctxt == NULL ) ? NULL : ctxt->location );
1753}
1754
1755/**
1756 * xmlNanoHTTPEncoding:
1757 * @ctx: the HTTP context
1758 *
1759 * Provides the specified encoding if specified in the HTTP headers.
1760 *
1761 * Return the specified encoding or NULL if not available
1762 */
1763const char *
1764xmlNanoHTTPEncoding( void * ctx ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001765 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillard847332a2003-10-18 11:29:40 +00001766
1767 return ( ( ctxt == NULL ) ? NULL : ctxt->encoding );
1768}
1769
1770/**
Daniel Veillarda840b692003-10-19 13:35:37 +00001771 * xmlNanoHTTPMimeType:
1772 * @ctx: the HTTP context
1773 *
1774 * Provides the specified Mime-Type if specified in the HTTP headers.
1775 *
1776 * Return the specified Mime-Type or NULL if not available
1777 */
1778const char *
1779xmlNanoHTTPMimeType( void * ctx ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001780 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillarda840b692003-10-19 13:35:37 +00001781
1782 return ( ( ctxt == NULL ) ? NULL : ctxt->mimeType );
1783}
1784
1785/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00001786 * xmlNanoHTTPFetchContent:
Daniel Veillardf012a642001-07-23 19:10:52 +00001787 * @ctx: the HTTP context
1788 * @ptr: pointer to set to the content buffer.
1789 * @len: integer pointer to hold the length of the content
1790 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001791 * Check if all the content was read
1792 *
Daniel Veillardf012a642001-07-23 19:10:52 +00001793 * Returns 0 if all the content was read and available, returns
1794 * -1 if received content length was less than specified or an error
1795 * occurred.
1796 */
Daniel Veillarda2351322004-06-27 12:08:10 +00001797static int
Daniel Veillardf012a642001-07-23 19:10:52 +00001798xmlNanoHTTPFetchContent( void * ctx, char ** ptr, int * len ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001799 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillardf012a642001-07-23 19:10:52 +00001800
1801 int rc = 0;
1802 int cur_lgth;
1803 int rcvd_lgth;
1804 int dummy_int;
1805 char * dummy_ptr = NULL;
1806
1807 /* Dummy up return input parameters if not provided */
1808
1809 if ( len == NULL )
1810 len = &dummy_int;
1811
1812 if ( ptr == NULL )
1813 ptr = &dummy_ptr;
1814
1815 /* But can't work without the context pointer */
1816
1817 if ( ( ctxt == NULL ) || ( ctxt->content == NULL ) ) {
1818 *len = 0;
1819 *ptr = NULL;
1820 return ( -1 );
1821 }
1822
1823 rcvd_lgth = ctxt->inptr - ctxt->content;
1824
1825 while ( (cur_lgth = xmlNanoHTTPRecv( ctxt )) > 0 ) {
1826
1827 rcvd_lgth += cur_lgth;
1828 if ( (ctxt->ContentLength > 0) && (rcvd_lgth >= ctxt->ContentLength) )
1829 break;
1830 }
1831
1832 *ptr = ctxt->content;
1833 *len = rcvd_lgth;
1834
1835 if ( ( ctxt->ContentLength > 0 ) && ( rcvd_lgth < ctxt->ContentLength ) )
1836 rc = -1;
1837 else if ( rcvd_lgth == 0 )
1838 rc = -1;
1839
1840 return ( rc );
1841}
1842
Owen Taylor3473f882001-02-23 17:55:21 +00001843#ifdef STANDALONE
1844int main(int argc, char **argv) {
1845 char *contentType = NULL;
1846
1847 if (argv[1] != NULL) {
1848 if (argv[2] != NULL)
1849 xmlNanoHTTPFetch(argv[1], argv[2], &contentType);
1850 else
1851 xmlNanoHTTPFetch(argv[1], "-", &contentType);
1852 if (contentType != NULL) xmlFree(contentType);
1853 } else {
1854 xmlGenericError(xmlGenericErrorContext,
1855 "%s: minimal HTTP GET implementation\n", argv[0]);
1856 xmlGenericError(xmlGenericErrorContext,
1857 "\tusage %s [ URL [ filename ] ]\n", argv[0]);
1858 }
1859 xmlNanoHTTPCleanup();
1860 xmlMemoryDump();
1861 return(0);
1862}
1863#endif /* STANDALONE */
1864#else /* !LIBXML_HTTP_ENABLED */
1865#ifdef STANDALONE
1866#include <stdio.h>
1867int main(int argc, char **argv) {
1868 xmlGenericError(xmlGenericErrorContext,
1869 "%s : HTTP support not compiled in\n", argv[0]);
1870 return(0);
1871}
1872#endif /* STANDALONE */
1873#endif /* LIBXML_HTTP_ENABLED */
Daniel Veillard5d4644e2005-04-01 13:11:58 +00001874#define bottom_nanohttp
1875#include "elfgcchack.h"