blob: e6459dfa9dfc4a03d1a130fc2415c00a0633c377 [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#endif
79
Daniel Veillard59d3ed82007-04-17 12:44:58 +000080#if defined(__MINGW32__) || defined(_WIN32_WCE)
Ozkan Sezerf99d2222010-11-04 12:08:08 +010081#ifndef _WINSOCKAPI_
Daniel Veillard1638a472003-08-14 01:23:25 +000082#define _WINSOCKAPI_
Ozkan Sezerf99d2222010-11-04 12:08:08 +010083#endif
Daniel Veillard1638a472003-08-14 01:23:25 +000084#include <wsockcompat.h>
85#include <winsock2.h>
Daniel Veillardc284c642005-03-31 10:24:24 +000086#undef XML_SOCKLEN_T
87#define XML_SOCKLEN_T unsigned int
Daniel Veillard1638a472003-08-14 01:23:25 +000088#endif
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
Ozkan Sezerf99d2222010-11-04 12:08:08 +0100106#define INVALID_SOCKET (-1)
Owen Taylor3473f882001-02-23 17:55:21 +0000107#endif
108
Daniel Veillard89f7f272003-09-29 13:29:09 +0000109#ifdef __BEOS__
110#ifndef PF_INET
111#define PF_INET AF_INET
112#endif
113#endif
114
Daniel Veillardc284c642005-03-31 10:24:24 +0000115#ifndef XML_SOCKLEN_T
116#define XML_SOCKLEN_T unsigned int
Daniel Veillard75be0132002-03-13 10:03:35 +0000117#endif
Daniel Veillardf012a642001-07-23 19:10:52 +0000118
Owen Taylor3473f882001-02-23 17:55:21 +0000119#ifdef STANDALONE
120#define DEBUG_HTTP
121#define xmlStrncasecmp(a, b, n) strncasecmp((char *)a, (char *)b, n)
122#define xmlStrcasecmpi(a, b) strcasecmp((char *)a, (char *)b)
123#endif
124
125#define XML_NANO_HTTP_MAX_REDIR 10
126
127#define XML_NANO_HTTP_CHUNK 4096
128
129#define XML_NANO_HTTP_CLOSED 0
130#define XML_NANO_HTTP_WRITE 1
131#define XML_NANO_HTTP_READ 2
132#define XML_NANO_HTTP_NONE 4
133
134typedef struct xmlNanoHTTPCtxt {
135 char *protocol; /* the protocol name */
136 char *hostname; /* the host name */
137 int port; /* the port */
138 char *path; /* the path within the URL */
Daniel Veillard351f2d62005-04-13 02:55:12 +0000139 char *query; /* the query string */
Owen Taylor3473f882001-02-23 17:55:21 +0000140 SOCKET fd; /* the file descriptor for the socket */
141 int state; /* WRITE / READ / CLOSED */
142 char *out; /* buffer sent (zero terminated) */
143 char *outptr; /* index within the buffer sent */
144 char *in; /* the receiving buffer */
145 char *content; /* the start of the content */
146 char *inptr; /* the next byte to read from network */
147 char *inrptr; /* the next byte to give back to the client */
148 int inlen; /* len of the input buffer */
149 int last; /* return code for last operation */
150 int returnValue; /* the protocol return value */
Daniel Veillard13cee4e2009-09-05 14:52:55 +0200151 int version; /* the protocol version */
Daniel Veillardf012a642001-07-23 19:10:52 +0000152 int ContentLength; /* specified content length from HTTP header */
Owen Taylor3473f882001-02-23 17:55:21 +0000153 char *contentType; /* the MIME type for the input */
154 char *location; /* the new URL in case of redirect */
155 char *authHeader; /* contents of {WWW,Proxy}-Authenticate header */
Daniel Veillard847332a2003-10-18 11:29:40 +0000156 char *encoding; /* encoding extracted from the contentType */
Daniel Veillarda840b692003-10-19 13:35:37 +0000157 char *mimeType; /* Mime-Type extracted from the contentType */
Daniel Veillard9a2724d2005-12-15 11:12:26 +0000158#ifdef HAVE_ZLIB_H
159 z_stream *strm; /* Zlib stream object */
160 int usesGzip; /* "Content-Encoding: gzip" was detected */
161#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000162} xmlNanoHTTPCtxt, *xmlNanoHTTPCtxtPtr;
163
164static int initialized = 0;
165static char *proxy = NULL; /* the proxy name if any */
166static int proxyPort; /* the proxy port if any */
167static unsigned int timeout = 60;/* the select() timeout in seconds */
168
Daniel Veillarda2351322004-06-27 12:08:10 +0000169static int xmlNanoHTTPFetchContent( void * ctx, char ** ptr, int * len );
Daniel Veillardf012a642001-07-23 19:10:52 +0000170
Owen Taylor3473f882001-02-23 17:55:21 +0000171/**
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000172 * xmlHTTPErrMemory:
173 * @extra: extra informations
174 *
175 * Handle an out of memory condition
176 */
177static void
178xmlHTTPErrMemory(const char *extra)
179{
180 __xmlSimpleError(XML_FROM_HTTP, XML_ERR_NO_MEMORY, NULL, NULL, extra);
181}
182
183/**
Owen Taylor3473f882001-02-23 17:55:21 +0000184 * A portability function
185 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000186static int socket_errno(void) {
Owen Taylor3473f882001-02-23 17:55:21 +0000187#ifdef _WINSOCKAPI_
188 return(WSAGetLastError());
189#else
190 return(errno);
191#endif
192}
193
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000194#ifdef SUPPORT_IP6
Daniel Veillard2db8c122003-07-08 12:16:59 +0000195static
196int have_ipv6(void) {
Ozkan Sezerf99d2222010-11-04 12:08:08 +0100197 SOCKET s;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000198
199 s = socket (AF_INET6, SOCK_STREAM, 0);
Ozkan Sezerf99d2222010-11-04 12:08:08 +0100200 if (s != INVALID_SOCKET) {
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000201 close (s);
202 return (1);
203 }
204 return (0);
205}
206#endif
207
Owen Taylor3473f882001-02-23 17:55:21 +0000208/**
209 * xmlNanoHTTPInit:
210 *
211 * Initialize the HTTP protocol layer.
212 * Currently it just checks for proxy informations
213 */
214
215void
216xmlNanoHTTPInit(void) {
217 const char *env;
218#ifdef _WINSOCKAPI_
219 WSADATA wsaData;
220#endif
221
222 if (initialized)
223 return;
224
225#ifdef _WINSOCKAPI_
226 if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0)
227 return;
228#endif
229
230 if (proxy == NULL) {
231 proxyPort = 80;
232 env = getenv("no_proxy");
Daniel Veillard29b17482004-08-16 00:39:03 +0000233 if (env && ((env[0] == '*') && (env[1] == 0)))
Owen Taylor3473f882001-02-23 17:55:21 +0000234 goto done;
235 env = getenv("http_proxy");
236 if (env != NULL) {
237 xmlNanoHTTPScanProxy(env);
238 goto done;
239 }
240 env = getenv("HTTP_PROXY");
241 if (env != NULL) {
242 xmlNanoHTTPScanProxy(env);
243 goto done;
244 }
245 }
246done:
247 initialized = 1;
248}
249
250/**
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000251 * xmlNanoHTTPCleanup:
Owen Taylor3473f882001-02-23 17:55:21 +0000252 *
253 * Cleanup the HTTP protocol layer.
254 */
255
256void
257xmlNanoHTTPCleanup(void) {
Daniel Veillard744acff2005-07-12 15:09:53 +0000258 if (proxy != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +0000259 xmlFree(proxy);
Daniel Veillard744acff2005-07-12 15:09:53 +0000260 proxy = NULL;
261 }
Owen Taylor3473f882001-02-23 17:55:21 +0000262#ifdef _WINSOCKAPI_
263 if (initialized)
264 WSACleanup();
265#endif
266 initialized = 0;
267 return;
268}
269
270/**
Owen Taylor3473f882001-02-23 17:55:21 +0000271 * xmlNanoHTTPScanURL:
272 * @ctxt: an HTTP context
273 * @URL: The URL used to initialize the context
274 *
275 * (Re)Initialize an HTTP context by parsing the URL and finding
276 * the protocol host port and path it indicates.
277 */
278
279static void
280xmlNanoHTTPScanURL(xmlNanoHTTPCtxtPtr ctxt, const char *URL) {
William M. Brack015ccb22005-02-13 08:18:52 +0000281 xmlURIPtr uri;
282 /*
283 * Clear any existing data from the context
284 */
Owen Taylor3473f882001-02-23 17:55:21 +0000285 if (ctxt->protocol != NULL) {
286 xmlFree(ctxt->protocol);
287 ctxt->protocol = NULL;
288 }
289 if (ctxt->hostname != NULL) {
290 xmlFree(ctxt->hostname);
291 ctxt->hostname = NULL;
292 }
293 if (ctxt->path != NULL) {
294 xmlFree(ctxt->path);
295 ctxt->path = NULL;
296 }
Daniel Veillard351f2d62005-04-13 02:55:12 +0000297 if (ctxt->query != NULL) {
298 xmlFree(ctxt->query);
299 ctxt->query = NULL;
300 }
Owen Taylor3473f882001-02-23 17:55:21 +0000301 if (URL == NULL) return;
William M. Brack015ccb22005-02-13 08:18:52 +0000302
Daniel Veillard336a8e12005-08-07 10:46:19 +0000303 uri = xmlParseURIRaw(URL, 1);
William M. Brack015ccb22005-02-13 08:18:52 +0000304 if (uri == NULL)
305 return;
306
307 if ((uri->scheme == NULL) || (uri->server == NULL)) {
308 xmlFreeURI(uri);
309 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000310 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000311
William M. Brack015ccb22005-02-13 08:18:52 +0000312 ctxt->protocol = xmlMemStrdup(uri->scheme);
313 ctxt->hostname = xmlMemStrdup(uri->server);
314 if (uri->path != NULL)
315 ctxt->path = xmlMemStrdup(uri->path);
316 else
317 ctxt->path = xmlMemStrdup("/");
Daniel Veillard351f2d62005-04-13 02:55:12 +0000318 if (uri->query != NULL)
319 ctxt->query = xmlMemStrdup(uri->query);
William M. Brack015ccb22005-02-13 08:18:52 +0000320 if (uri->port != 0)
321 ctxt->port = uri->port;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000322
William M. Brack015ccb22005-02-13 08:18:52 +0000323 xmlFreeURI(uri);
Owen Taylor3473f882001-02-23 17:55:21 +0000324}
325
326/**
327 * xmlNanoHTTPScanProxy:
328 * @URL: The proxy URL used to initialize the proxy context
329 *
330 * (Re)Initialize the HTTP Proxy context by parsing the URL and finding
331 * the protocol host port it indicates.
332 * Should be like http://myproxy/ or http://myproxy:3128/
333 * A NULL URL cleans up proxy informations.
334 */
335
336void
337xmlNanoHTTPScanProxy(const char *URL) {
William M. Brack015ccb22005-02-13 08:18:52 +0000338 xmlURIPtr uri;
Owen Taylor3473f882001-02-23 17:55:21 +0000339
340 if (proxy != NULL) {
341 xmlFree(proxy);
342 proxy = NULL;
343 }
William M. Brack015ccb22005-02-13 08:18:52 +0000344 proxyPort = 0;
345
Owen Taylor3473f882001-02-23 17:55:21 +0000346#ifdef DEBUG_HTTP
347 if (URL == NULL)
348 xmlGenericError(xmlGenericErrorContext,
349 "Removing HTTP proxy info\n");
350 else
351 xmlGenericError(xmlGenericErrorContext,
352 "Using HTTP proxy %s\n", URL);
353#endif
354 if (URL == NULL) return;
William M. Brack015ccb22005-02-13 08:18:52 +0000355
Daniel Veillard336a8e12005-08-07 10:46:19 +0000356 uri = xmlParseURIRaw(URL, 1);
William M. Brack015ccb22005-02-13 08:18:52 +0000357 if ((uri == NULL) || (uri->scheme == NULL) ||
358 (strcmp(uri->scheme, "http")) || (uri->server == NULL)) {
359 __xmlIOErr(XML_FROM_HTTP, XML_HTTP_URL_SYNTAX, "Syntax Error\n");
360 if (uri != NULL)
361 xmlFreeURI(uri);
362 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000363 }
William M. Brack015ccb22005-02-13 08:18:52 +0000364
365 proxy = xmlMemStrdup(uri->server);
366 if (uri->port != 0)
367 proxyPort = uri->port;
Owen Taylor3473f882001-02-23 17:55:21 +0000368
William M. Brack015ccb22005-02-13 08:18:52 +0000369 xmlFreeURI(uri);
Owen Taylor3473f882001-02-23 17:55:21 +0000370}
371
372/**
373 * xmlNanoHTTPNewCtxt:
374 * @URL: The URL used to initialize the context
375 *
376 * Allocate and initialize a new HTTP context.
377 *
378 * Returns an HTTP context or NULL in case of error.
379 */
380
381static xmlNanoHTTPCtxtPtr
382xmlNanoHTTPNewCtxt(const char *URL) {
383 xmlNanoHTTPCtxtPtr ret;
384
385 ret = (xmlNanoHTTPCtxtPtr) xmlMalloc(sizeof(xmlNanoHTTPCtxt));
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000386 if (ret == NULL) {
387 xmlHTTPErrMemory("allocating context");
388 return(NULL);
389 }
Owen Taylor3473f882001-02-23 17:55:21 +0000390
391 memset(ret, 0, sizeof(xmlNanoHTTPCtxt));
392 ret->port = 80;
393 ret->returnValue = 0;
Ozkan Sezerf99d2222010-11-04 12:08:08 +0100394 ret->fd = INVALID_SOCKET;
Daniel Veillardf012a642001-07-23 19:10:52 +0000395 ret->ContentLength = -1;
Owen Taylor3473f882001-02-23 17:55:21 +0000396
Daniel Veillardcacbe5d2003-01-10 16:09:51 +0000397 xmlNanoHTTPScanURL(ret, URL);
Owen Taylor3473f882001-02-23 17:55:21 +0000398
399 return(ret);
400}
401
402/**
403 * xmlNanoHTTPFreeCtxt:
404 * @ctxt: an HTTP context
405 *
406 * Frees the context after closing the connection.
407 */
408
409static void
410xmlNanoHTTPFreeCtxt(xmlNanoHTTPCtxtPtr ctxt) {
411 if (ctxt == NULL) return;
412 if (ctxt->hostname != NULL) xmlFree(ctxt->hostname);
413 if (ctxt->protocol != NULL) xmlFree(ctxt->protocol);
414 if (ctxt->path != NULL) xmlFree(ctxt->path);
Daniel Veillard351f2d62005-04-13 02:55:12 +0000415 if (ctxt->query != NULL) xmlFree(ctxt->query);
Owen Taylor3473f882001-02-23 17:55:21 +0000416 if (ctxt->out != NULL) xmlFree(ctxt->out);
417 if (ctxt->in != NULL) xmlFree(ctxt->in);
418 if (ctxt->contentType != NULL) xmlFree(ctxt->contentType);
Daniel Veillard847332a2003-10-18 11:29:40 +0000419 if (ctxt->encoding != NULL) xmlFree(ctxt->encoding);
Daniel Veillarda840b692003-10-19 13:35:37 +0000420 if (ctxt->mimeType != NULL) xmlFree(ctxt->mimeType);
Owen Taylor3473f882001-02-23 17:55:21 +0000421 if (ctxt->location != NULL) xmlFree(ctxt->location);
422 if (ctxt->authHeader != NULL) xmlFree(ctxt->authHeader);
Daniel Veillard9a2724d2005-12-15 11:12:26 +0000423#ifdef HAVE_ZLIB_H
424 if (ctxt->strm != NULL) {
425 inflateEnd(ctxt->strm);
426 xmlFree(ctxt->strm);
427 }
428#endif
429
Owen Taylor3473f882001-02-23 17:55:21 +0000430 ctxt->state = XML_NANO_HTTP_NONE;
Ozkan Sezerf99d2222010-11-04 12:08:08 +0100431 if (ctxt->fd != INVALID_SOCKET) closesocket(ctxt->fd);
432 ctxt->fd = INVALID_SOCKET;
Owen Taylor3473f882001-02-23 17:55:21 +0000433 xmlFree(ctxt);
434}
435
436/**
437 * xmlNanoHTTPSend:
438 * @ctxt: an HTTP context
439 *
440 * Send the input needed to initiate the processing on the server side
Daniel Veillardf012a642001-07-23 19:10:52 +0000441 * Returns number of bytes sent or -1 on error.
Owen Taylor3473f882001-02-23 17:55:21 +0000442 */
443
Daniel Veillardf012a642001-07-23 19:10:52 +0000444static int
Raphael Prevost48b60c32009-08-23 13:11:01 +0200445xmlNanoHTTPSend(xmlNanoHTTPCtxtPtr ctxt, const char *xmt_ptr, int outlen)
446{
447 int total_sent = 0;
448#ifdef HAVE_POLL_H
449 struct pollfd p;
450#else
451 struct timeval tv;
452 fd_set wfd;
453#endif
Daniel Veillardf012a642001-07-23 19:10:52 +0000454
Raphael Prevost48b60c32009-08-23 13:11:01 +0200455 if ((ctxt->state & XML_NANO_HTTP_WRITE) && (xmt_ptr != NULL)) {
Daniel Veillardf012a642001-07-23 19:10:52 +0000456 while (total_sent < outlen) {
457 int nsent = send(ctxt->fd, xmt_ptr + total_sent,
Raphael Prevost48b60c32009-08-23 13:11:01 +0200458 outlen - total_sent, 0);
459
460 if (nsent > 0)
Owen Taylor3473f882001-02-23 17:55:21 +0000461 total_sent += nsent;
Raphael Prevost48b60c32009-08-23 13:11:01 +0200462 else if ((nsent == -1) &&
Daniel Veillardba6db032001-07-31 16:25:45 +0000463#if defined(EAGAIN) && EAGAIN != EWOULDBLOCK
Raphael Prevost48b60c32009-08-23 13:11:01 +0200464 (socket_errno() != EAGAIN) &&
Daniel Veillardba6db032001-07-31 16:25:45 +0000465#endif
Raphael Prevost48b60c32009-08-23 13:11:01 +0200466 (socket_errno() != EWOULDBLOCK)) {
467 __xmlIOErr(XML_FROM_HTTP, 0, "send failed\n");
468 if (total_sent == 0)
469 total_sent = -1;
470 break;
471 } else {
472 /*
473 * No data sent
474 * Since non-blocking sockets are used, wait for
475 * socket to be writable or default timeout prior
476 * to retrying.
477 */
478#ifndef HAVE_POLL_H
spadixd29a5c82009-10-19 14:03:25 +0200479#ifndef _WINSOCKAPI_
Raphael Prevost48b60c32009-08-23 13:11:01 +0200480 if (ctxt->fd > FD_SETSIZE)
481 return -1;
spadixd29a5c82009-10-19 14:03:25 +0200482#endif
Daniel Veillardf012a642001-07-23 19:10:52 +0000483
Raphael Prevost48b60c32009-08-23 13:11:01 +0200484 tv.tv_sec = timeout;
485 tv.tv_usec = 0;
486 FD_ZERO(&wfd);
Daniel Veillard9e2110b2005-08-08 20:33:54 +0000487#ifdef _MSC_VER
488#pragma warning(push)
489#pragma warning(disable: 4018)
490#endif
Raphael Prevost48b60c32009-08-23 13:11:01 +0200491 FD_SET(ctxt->fd, &wfd);
Daniel Veillard9e2110b2005-08-08 20:33:54 +0000492#ifdef _MSC_VER
493#pragma warning(pop)
494#endif
Raphael Prevost48b60c32009-08-23 13:11:01 +0200495 (void) select(ctxt->fd + 1, NULL, &wfd, NULL, &tv);
496#else
497 p.fd = ctxt->fd;
498 p.events = POLLOUT;
499 (void) poll(&p, 1, timeout * 1000);
500#endif /* !HAVE_POLL_H */
501 }
502 }
Owen Taylor3473f882001-02-23 17:55:21 +0000503 }
Daniel Veillardf012a642001-07-23 19:10:52 +0000504
505 return total_sent;
Owen Taylor3473f882001-02-23 17:55:21 +0000506}
507
508/**
509 * xmlNanoHTTPRecv:
510 * @ctxt: an HTTP context
511 *
512 * Read information coming from the HTTP connection.
513 * This is a blocking call (but it blocks in select(), not read()).
514 *
515 * Returns the number of byte read or -1 in case of error.
516 */
517
518static int
Raphael Prevost48b60c32009-08-23 13:11:01 +0200519xmlNanoHTTPRecv(xmlNanoHTTPCtxtPtr ctxt)
520{
521#ifdef HAVE_POLL_H
522 struct pollfd p;
523#else
Owen Taylor3473f882001-02-23 17:55:21 +0000524 fd_set rfd;
525 struct timeval tv;
Raphael Prevost48b60c32009-08-23 13:11:01 +0200526#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000527
528
529 while (ctxt->state & XML_NANO_HTTP_READ) {
Raphael Prevost48b60c32009-08-23 13:11:01 +0200530 if (ctxt->in == NULL) {
531 ctxt->in = (char *) xmlMallocAtomic(65000 * sizeof(char));
532 if (ctxt->in == NULL) {
533 xmlHTTPErrMemory("allocating input");
534 ctxt->last = -1;
535 return (-1);
536 }
537 ctxt->inlen = 65000;
538 ctxt->inptr = ctxt->content = ctxt->inrptr = ctxt->in;
539 }
540 if (ctxt->inrptr > ctxt->in + XML_NANO_HTTP_CHUNK) {
541 int delta = ctxt->inrptr - ctxt->in;
542 int len = ctxt->inptr - ctxt->inrptr;
Owen Taylor3473f882001-02-23 17:55:21 +0000543
Raphael Prevost48b60c32009-08-23 13:11:01 +0200544 memmove(ctxt->in, ctxt->inrptr, len);
545 ctxt->inrptr -= delta;
546 ctxt->content -= delta;
547 ctxt->inptr -= delta;
548 }
549 if ((ctxt->in + ctxt->inlen) < (ctxt->inptr + XML_NANO_HTTP_CHUNK)) {
550 int d_inptr = ctxt->inptr - ctxt->in;
551 int d_content = ctxt->content - ctxt->in;
552 int d_inrptr = ctxt->inrptr - ctxt->in;
553 char *tmp_ptr = ctxt->in;
554
555 ctxt->inlen *= 2;
Daniel Veillardf012a642001-07-23 19:10:52 +0000556 ctxt->in = (char *) xmlRealloc(tmp_ptr, ctxt->inlen);
Raphael Prevost48b60c32009-08-23 13:11:01 +0200557 if (ctxt->in == NULL) {
558 xmlHTTPErrMemory("allocating input buffer");
559 xmlFree(tmp_ptr);
560 ctxt->last = -1;
561 return (-1);
562 }
Owen Taylor3473f882001-02-23 17:55:21 +0000563 ctxt->inptr = ctxt->in + d_inptr;
564 ctxt->content = ctxt->in + d_content;
565 ctxt->inrptr = ctxt->in + d_inrptr;
Raphael Prevost48b60c32009-08-23 13:11:01 +0200566 }
567 ctxt->last = recv(ctxt->fd, ctxt->inptr, XML_NANO_HTTP_CHUNK, 0);
568 if (ctxt->last > 0) {
569 ctxt->inptr += ctxt->last;
570 return (ctxt->last);
571 }
572 if (ctxt->last == 0) {
573 return (0);
574 }
575 if (ctxt->last == -1) {
576 switch (socket_errno()) {
577 case EINPROGRESS:
578 case EWOULDBLOCK:
Owen Taylor3473f882001-02-23 17:55:21 +0000579#if defined(EAGAIN) && EAGAIN != EWOULDBLOCK
Raphael Prevost48b60c32009-08-23 13:11:01 +0200580 case EAGAIN:
Owen Taylor3473f882001-02-23 17:55:21 +0000581#endif
Raphael Prevost48b60c32009-08-23 13:11:01 +0200582 break;
Daniel Veillardf012a642001-07-23 19:10:52 +0000583
Raphael Prevost48b60c32009-08-23 13:11:01 +0200584 case ECONNRESET:
585 case ESHUTDOWN:
586 return (0);
Daniel Veillardf012a642001-07-23 19:10:52 +0000587
Raphael Prevost48b60c32009-08-23 13:11:01 +0200588 default:
589 __xmlIOErr(XML_FROM_HTTP, 0, "recv failed\n");
590 return (-1);
591 }
592 }
593#ifdef HAVE_POLL_H
594 p.fd = ctxt->fd;
595 p.events = POLLIN;
596 if ((poll(&p, 1, timeout * 1000) < 1)
597#if defined(EINTR)
598 && (errno != EINTR)
599#endif
600 )
601 return (0);
602#else /* !HAVE_POLL_H */
spadixd29a5c82009-10-19 14:03:25 +0200603#ifndef _WINSOCKAPI_
Raphael Prevost48b60c32009-08-23 13:11:01 +0200604 if (ctxt->fd > FD_SETSIZE)
605 return 0;
spadixd29a5c82009-10-19 14:03:25 +0200606#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000607
Raphael Prevost48b60c32009-08-23 13:11:01 +0200608 tv.tv_sec = timeout;
609 tv.tv_usec = 0;
610 FD_ZERO(&rfd);
611
Daniel Veillard9e2110b2005-08-08 20:33:54 +0000612#ifdef _MSC_VER
613#pragma warning(push)
614#pragma warning(disable: 4018)
615#endif
Raphael Prevost48b60c32009-08-23 13:11:01 +0200616
617 FD_SET(ctxt->fd, &rfd);
618
Daniel Veillard9e2110b2005-08-08 20:33:54 +0000619#ifdef _MSC_VER
620#pragma warning(pop)
621#endif
Raphael Prevost48b60c32009-08-23 13:11:01 +0200622
623 if ((select(ctxt->fd + 1, &rfd, NULL, NULL, &tv) < 1)
Daniel Veillard50f34372001-08-03 12:06:36 +0000624#if defined(EINTR)
Raphael Prevost48b60c32009-08-23 13:11:01 +0200625 && (errno != EINTR)
Daniel Veillard50f34372001-08-03 12:06:36 +0000626#endif
Raphael Prevost48b60c32009-08-23 13:11:01 +0200627 )
628 return (0);
629#endif /* !HAVE_POLL_H */
Owen Taylor3473f882001-02-23 17:55:21 +0000630 }
Raphael Prevost48b60c32009-08-23 13:11:01 +0200631 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +0000632}
633
634/**
635 * xmlNanoHTTPReadLine:
636 * @ctxt: an HTTP context
637 *
638 * Read one line in the HTTP server output, usually for extracting
639 * the HTTP protocol informations from the answer header.
640 *
641 * Returns a newly allocated string with a copy of the line, or NULL
642 * which indicate the end of the input.
643 */
644
645static char *
646xmlNanoHTTPReadLine(xmlNanoHTTPCtxtPtr ctxt) {
647 char buf[4096];
648 char *bp = buf;
Daniel Veillardf012a642001-07-23 19:10:52 +0000649 int rc;
Owen Taylor3473f882001-02-23 17:55:21 +0000650
651 while (bp - buf < 4095) {
652 if (ctxt->inrptr == ctxt->inptr) {
Daniel Veillardf012a642001-07-23 19:10:52 +0000653 if ( (rc = xmlNanoHTTPRecv(ctxt)) == 0) {
Owen Taylor3473f882001-02-23 17:55:21 +0000654 if (bp == buf)
655 return(NULL);
656 else
657 *bp = 0;
658 return(xmlMemStrdup(buf));
659 }
Daniel Veillardf012a642001-07-23 19:10:52 +0000660 else if ( rc == -1 ) {
661 return ( NULL );
662 }
Owen Taylor3473f882001-02-23 17:55:21 +0000663 }
664 *bp = *ctxt->inrptr++;
665 if (*bp == '\n') {
666 *bp = 0;
667 return(xmlMemStrdup(buf));
668 }
669 if (*bp != '\r')
670 bp++;
671 }
672 buf[4095] = 0;
673 return(xmlMemStrdup(buf));
674}
675
676
677/**
678 * xmlNanoHTTPScanAnswer:
679 * @ctxt: an HTTP context
680 * @line: an HTTP header line
681 *
682 * Try to extract useful informations from the server answer.
683 * We currently parse and process:
684 * - The HTTP revision/ return code
Daniel Veillarda840b692003-10-19 13:35:37 +0000685 * - The Content-Type, Mime-Type and charset used
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000686 * - The Location for redirect processing.
Owen Taylor3473f882001-02-23 17:55:21 +0000687 *
688 * Returns -1 in case of failure, the file descriptor number otherwise
689 */
690
691static void
692xmlNanoHTTPScanAnswer(xmlNanoHTTPCtxtPtr ctxt, const char *line) {
693 const char *cur = line;
694
695 if (line == NULL) return;
696
697 if (!strncmp(line, "HTTP/", 5)) {
698 int version = 0;
699 int ret = 0;
700
701 cur += 5;
702 while ((*cur >= '0') && (*cur <= '9')) {
703 version *= 10;
704 version += *cur - '0';
705 cur++;
706 }
707 if (*cur == '.') {
708 cur++;
709 if ((*cur >= '0') && (*cur <= '9')) {
710 version *= 10;
711 version += *cur - '0';
712 cur++;
713 }
714 while ((*cur >= '0') && (*cur <= '9'))
715 cur++;
716 } else
717 version *= 10;
718 if ((*cur != ' ') && (*cur != '\t')) return;
719 while ((*cur == ' ') || (*cur == '\t')) cur++;
720 if ((*cur < '0') || (*cur > '9')) return;
721 while ((*cur >= '0') && (*cur <= '9')) {
722 ret *= 10;
723 ret += *cur - '0';
724 cur++;
725 }
726 if ((*cur != 0) && (*cur != ' ') && (*cur != '\t')) return;
727 ctxt->returnValue = ret;
Daniel Veillard13cee4e2009-09-05 14:52:55 +0200728 ctxt->version = version;
Owen Taylor3473f882001-02-23 17:55:21 +0000729 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Content-Type:", 13)) {
Daniel Veillarda840b692003-10-19 13:35:37 +0000730 const xmlChar *charset, *last, *mime;
Owen Taylor3473f882001-02-23 17:55:21 +0000731 cur += 13;
732 while ((*cur == ' ') || (*cur == '\t')) cur++;
733 if (ctxt->contentType != NULL)
734 xmlFree(ctxt->contentType);
735 ctxt->contentType = xmlMemStrdup(cur);
Daniel Veillarda840b692003-10-19 13:35:37 +0000736 mime = (const xmlChar *) cur;
737 last = mime;
738 while ((*last != 0) && (*last != ' ') && (*last != '\t') &&
739 (*last != ';') && (*last != ','))
740 last++;
741 if (ctxt->mimeType != NULL)
742 xmlFree(ctxt->mimeType);
743 ctxt->mimeType = (char *) xmlStrndup(mime, last - mime);
744 charset = xmlStrstr(BAD_CAST ctxt->contentType, BAD_CAST "charset=");
745 if (charset != NULL) {
746 charset += 8;
747 last = charset;
748 while ((*last != 0) && (*last != ' ') && (*last != '\t') &&
749 (*last != ';') && (*last != ','))
750 last++;
751 if (ctxt->encoding != NULL)
752 xmlFree(ctxt->encoding);
753 ctxt->encoding = (char *) xmlStrndup(charset, last - charset);
754 }
Owen Taylor3473f882001-02-23 17:55:21 +0000755 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"ContentType:", 12)) {
Daniel Veillarda840b692003-10-19 13:35:37 +0000756 const xmlChar *charset, *last, *mime;
Owen Taylor3473f882001-02-23 17:55:21 +0000757 cur += 12;
758 if (ctxt->contentType != NULL) return;
759 while ((*cur == ' ') || (*cur == '\t')) cur++;
760 ctxt->contentType = xmlMemStrdup(cur);
Daniel Veillarda840b692003-10-19 13:35:37 +0000761 mime = (const xmlChar *) cur;
762 last = mime;
763 while ((*last != 0) && (*last != ' ') && (*last != '\t') &&
764 (*last != ';') && (*last != ','))
765 last++;
766 if (ctxt->mimeType != NULL)
767 xmlFree(ctxt->mimeType);
768 ctxt->mimeType = (char *) xmlStrndup(mime, last - mime);
769 charset = xmlStrstr(BAD_CAST ctxt->contentType, BAD_CAST "charset=");
770 if (charset != NULL) {
771 charset += 8;
772 last = charset;
773 while ((*last != 0) && (*last != ' ') && (*last != '\t') &&
774 (*last != ';') && (*last != ','))
775 last++;
776 if (ctxt->encoding != NULL)
777 xmlFree(ctxt->encoding);
778 ctxt->encoding = (char *) xmlStrndup(charset, last - charset);
779 }
Owen Taylor3473f882001-02-23 17:55:21 +0000780 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Location:", 9)) {
781 cur += 9;
782 while ((*cur == ' ') || (*cur == '\t')) cur++;
783 if (ctxt->location != NULL)
784 xmlFree(ctxt->location);
William M. Brack7e29c0a2004-04-02 09:07:22 +0000785 if (*cur == '/') {
786 xmlChar *tmp_http = xmlStrdup(BAD_CAST "http://");
787 xmlChar *tmp_loc =
788 xmlStrcat(tmp_http, (const xmlChar *) ctxt->hostname);
789 ctxt->location =
790 (char *) xmlStrcat (tmp_loc, (const xmlChar *) cur);
791 } else {
792 ctxt->location = xmlMemStrdup(cur);
793 }
Owen Taylor3473f882001-02-23 17:55:21 +0000794 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"WWW-Authenticate:", 17)) {
795 cur += 17;
796 while ((*cur == ' ') || (*cur == '\t')) cur++;
797 if (ctxt->authHeader != NULL)
798 xmlFree(ctxt->authHeader);
799 ctxt->authHeader = xmlMemStrdup(cur);
800 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Proxy-Authenticate:", 19)) {
801 cur += 19;
802 while ((*cur == ' ') || (*cur == '\t')) cur++;
803 if (ctxt->authHeader != NULL)
804 xmlFree(ctxt->authHeader);
805 ctxt->authHeader = xmlMemStrdup(cur);
Daniel Veillard9a2724d2005-12-15 11:12:26 +0000806#ifdef HAVE_ZLIB_H
807 } else if ( !xmlStrncasecmp( BAD_CAST line, BAD_CAST"Content-Encoding:", 17) ) {
808 cur += 17;
809 while ((*cur == ' ') || (*cur == '\t')) cur++;
810 if ( !xmlStrncasecmp( BAD_CAST cur, BAD_CAST"gzip", 4) ) {
811 ctxt->usesGzip = 1;
812
813 ctxt->strm = xmlMalloc(sizeof(z_stream));
814
815 if (ctxt->strm != NULL) {
816 ctxt->strm->zalloc = Z_NULL;
817 ctxt->strm->zfree = Z_NULL;
818 ctxt->strm->opaque = Z_NULL;
819 ctxt->strm->avail_in = 0;
820 ctxt->strm->next_in = Z_NULL;
821
822 inflateInit2( ctxt->strm, 31 );
823 }
824 }
825#endif
Daniel Veillardf012a642001-07-23 19:10:52 +0000826 } else if ( !xmlStrncasecmp( BAD_CAST line, BAD_CAST"Content-Length:", 15) ) {
827 cur += 15;
828 ctxt->ContentLength = strtol( cur, NULL, 10 );
Owen Taylor3473f882001-02-23 17:55:21 +0000829 }
830}
831
832/**
833 * xmlNanoHTTPConnectAttempt:
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000834 * @addr: a socket address structure
Owen Taylor3473f882001-02-23 17:55:21 +0000835 *
836 * Attempt a connection to the given IP:port endpoint. It forces
837 * non-blocking semantic on the socket, and allow 60 seconds for
838 * the host to answer.
839 *
840 * Returns -1 in case of failure, the file descriptor number otherwise
841 */
842
Ozkan Sezerf99d2222010-11-04 12:08:08 +0100843static SOCKET
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000844xmlNanoHTTPConnectAttempt(struct sockaddr *addr)
Owen Taylor3473f882001-02-23 17:55:21 +0000845{
Raphael Prevost48b60c32009-08-23 13:11:01 +0200846#ifndef HAVE_POLL_H
Owen Taylor3473f882001-02-23 17:55:21 +0000847 fd_set wfd;
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000848#ifdef _WINSOCKAPI_
849 fd_set xfd;
850#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000851 struct timeval tv;
Raphael Prevost48b60c32009-08-23 13:11:01 +0200852#else /* !HAVE_POLL_H */
853 struct pollfd p;
854#endif /* !HAVE_POLL_H */
Owen Taylor3473f882001-02-23 17:55:21 +0000855 int status;
Raphael Prevost48b60c32009-08-23 13:11:01 +0200856
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000857 int addrlen;
Raphael Prevost48b60c32009-08-23 13:11:01 +0200858
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000859 SOCKET s;
Raphael Prevost48b60c32009-08-23 13:11:01 +0200860
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000861#ifdef SUPPORT_IP6
862 if (addr->sa_family == AF_INET6) {
Raphael Prevost48b60c32009-08-23 13:11:01 +0200863 s = socket(PF_INET6, SOCK_STREAM, IPPROTO_TCP);
864 addrlen = sizeof(struct sockaddr_in6);
865 } else
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000866#endif
867 {
Raphael Prevost48b60c32009-08-23 13:11:01 +0200868 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
869 addrlen = sizeof(struct sockaddr_in);
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000870 }
Ozkan Sezerf99d2222010-11-04 12:08:08 +0100871 if (s == INVALID_SOCKET) {
Owen Taylor3473f882001-02-23 17:55:21 +0000872#ifdef DEBUG_HTTP
Raphael Prevost48b60c32009-08-23 13:11:01 +0200873 perror("socket");
Owen Taylor3473f882001-02-23 17:55:21 +0000874#endif
Raphael Prevost48b60c32009-08-23 13:11:01 +0200875 __xmlIOErr(XML_FROM_HTTP, 0, "socket failed\n");
Ozkan Sezerf99d2222010-11-04 12:08:08 +0100876 return INVALID_SOCKET;
Owen Taylor3473f882001-02-23 17:55:21 +0000877 }
Owen Taylor3473f882001-02-23 17:55:21 +0000878#ifdef _WINSOCKAPI_
879 {
Raphael Prevost48b60c32009-08-23 13:11:01 +0200880 u_long one = 1;
Owen Taylor3473f882001-02-23 17:55:21 +0000881
Raphael Prevost48b60c32009-08-23 13:11:01 +0200882 status = ioctlsocket(s, FIONBIO, &one) == SOCKET_ERROR ? -1 : 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000883 }
884#else /* _WINSOCKAPI_ */
885#if defined(VMS)
886 {
Raphael Prevost48b60c32009-08-23 13:11:01 +0200887 int enable = 1;
888
889 status = ioctl(s, FIONBIO, &enable);
Owen Taylor3473f882001-02-23 17:55:21 +0000890 }
891#else /* VMS */
Daniel Veillardcba68392008-08-29 12:43:40 +0000892#if defined(__BEOS__) && !defined(__HAIKU__)
Raphael Prevost48b60c32009-08-23 13:11:01 +0200893 {
894 bool noblock = true;
895
896 status =
897 setsockopt(s, SOL_SOCKET, SO_NONBLOCK, &noblock,
898 sizeof(noblock));
899 }
Daniel Veillard254b1262003-11-01 17:04:58 +0000900#else /* __BEOS__ */
Owen Taylor3473f882001-02-23 17:55:21 +0000901 if ((status = fcntl(s, F_GETFL, 0)) != -1) {
902#ifdef O_NONBLOCK
Raphael Prevost48b60c32009-08-23 13:11:01 +0200903 status |= O_NONBLOCK;
Owen Taylor3473f882001-02-23 17:55:21 +0000904#else /* O_NONBLOCK */
905#ifdef F_NDELAY
Raphael Prevost48b60c32009-08-23 13:11:01 +0200906 status |= F_NDELAY;
Owen Taylor3473f882001-02-23 17:55:21 +0000907#endif /* F_NDELAY */
908#endif /* !O_NONBLOCK */
Raphael Prevost48b60c32009-08-23 13:11:01 +0200909 status = fcntl(s, F_SETFL, status);
Owen Taylor3473f882001-02-23 17:55:21 +0000910 }
911 if (status < 0) {
912#ifdef DEBUG_HTTP
Raphael Prevost48b60c32009-08-23 13:11:01 +0200913 perror("nonblocking");
Owen Taylor3473f882001-02-23 17:55:21 +0000914#endif
Raphael Prevost48b60c32009-08-23 13:11:01 +0200915 __xmlIOErr(XML_FROM_HTTP, 0, "error setting non-blocking IO\n");
916 closesocket(s);
Ozkan Sezerf99d2222010-11-04 12:08:08 +0100917 return INVALID_SOCKET;
Owen Taylor3473f882001-02-23 17:55:21 +0000918 }
Daniel Veillard254b1262003-11-01 17:04:58 +0000919#endif /* !__BEOS__ */
Owen Taylor3473f882001-02-23 17:55:21 +0000920#endif /* !VMS */
921#endif /* !_WINSOCKAPI_ */
922
Raphael Prevost48b60c32009-08-23 13:11:01 +0200923 if (connect(s, addr, addrlen) == -1) {
924 switch (socket_errno()) {
925 case EINPROGRESS:
926 case EWOULDBLOCK:
927 break;
928 default:
929 __xmlIOErr(XML_FROM_HTTP, 0,
930 "error connecting to HTTP server");
931 closesocket(s);
Ozkan Sezerf99d2222010-11-04 12:08:08 +0100932 return INVALID_SOCKET;
Raphael Prevost48b60c32009-08-23 13:11:01 +0200933 }
934 }
935#ifndef HAVE_POLL_H
Owen Taylor3473f882001-02-23 17:55:21 +0000936 tv.tv_sec = timeout;
937 tv.tv_usec = 0;
Daniel Veillard9e2110b2005-08-08 20:33:54 +0000938
939#ifdef _MSC_VER
940#pragma warning(push)
941#pragma warning(disable: 4018)
942#endif
spadixd29a5c82009-10-19 14:03:25 +0200943#ifndef _WINSOCKAPI_
Raphael Prevost48b60c32009-08-23 13:11:01 +0200944 if (s > FD_SETSIZE)
Ozkan Sezerf99d2222010-11-04 12:08:08 +0100945 return INVALID_SOCKET;
spadixd29a5c82009-10-19 14:03:25 +0200946#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000947 FD_ZERO(&wfd);
948 FD_SET(s, &wfd);
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000949
Raphael Prevost48b60c32009-08-23 13:11:01 +0200950#ifdef _WINSOCKAPI_
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000951 FD_ZERO(&xfd);
952 FD_SET(s, &xfd);
Raphael Prevost48b60c32009-08-23 13:11:01 +0200953
954 switch (select(s + 1, NULL, &wfd, &xfd, &tv))
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000955#else
Raphael Prevost48b60c32009-08-23 13:11:01 +0200956 switch (select(s + 1, NULL, &wfd, NULL, &tv))
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000957#endif
Daniel Veillard9e2110b2005-08-08 20:33:54 +0000958#ifdef _MSC_VER
959#pragma warning(pop)
960#endif
Raphael Prevost48b60c32009-08-23 13:11:01 +0200961
962#else /* !HAVE_POLL_H */
963 p.fd = s;
964 p.events = POLLOUT;
965 switch (poll(&p, 1, timeout * 1000))
966#endif /* !HAVE_POLL_H */
967
Owen Taylor3473f882001-02-23 17:55:21 +0000968 {
Raphael Prevost48b60c32009-08-23 13:11:01 +0200969 case 0:
970 /* Time out */
971 __xmlIOErr(XML_FROM_HTTP, 0, "Connect attempt timed out");
972 closesocket(s);
Ozkan Sezerf99d2222010-11-04 12:08:08 +0100973 return INVALID_SOCKET;
Raphael Prevost48b60c32009-08-23 13:11:01 +0200974 case -1:
975 /* Ermm.. ?? */
976 __xmlIOErr(XML_FROM_HTTP, 0, "Connect failed");
977 closesocket(s);
Ozkan Sezerf99d2222010-11-04 12:08:08 +0100978 return INVALID_SOCKET;
Owen Taylor3473f882001-02-23 17:55:21 +0000979 }
980
Raphael Prevost48b60c32009-08-23 13:11:01 +0200981#ifndef HAVE_POLL_H
982 if (FD_ISSET(s, &wfd)
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000983#ifdef _WINSOCKAPI_
Raphael Prevost48b60c32009-08-23 13:11:01 +0200984 || FD_ISSET(s, &xfd)
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000985#endif
Raphael Prevost48b60c32009-08-23 13:11:01 +0200986 )
987#else /* !HAVE_POLL_H */
988 if (p.revents == POLLOUT)
989#endif /* !HAVE_POLL_H */
990 {
991 XML_SOCKLEN_T len;
992
993 len = sizeof(status);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000994#ifdef SO_ERROR
Raphael Prevost48b60c32009-08-23 13:11:01 +0200995 if (getsockopt(s, SOL_SOCKET, SO_ERROR, (char *) &status, &len) <
996 0) {
997 /* Solaris error code */
998 __xmlIOErr(XML_FROM_HTTP, 0, "getsockopt failed\n");
Ozkan Sezerf99d2222010-11-04 12:08:08 +0100999 return INVALID_SOCKET;
Raphael Prevost48b60c32009-08-23 13:11:01 +02001000 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001001#endif
Raphael Prevost48b60c32009-08-23 13:11:01 +02001002 if (status) {
1003 __xmlIOErr(XML_FROM_HTTP, 0,
1004 "Error connecting to remote host");
1005 closesocket(s);
1006 errno = status;
Ozkan Sezerf99d2222010-11-04 12:08:08 +01001007 return INVALID_SOCKET;
Raphael Prevost48b60c32009-08-23 13:11:01 +02001008 }
Owen Taylor3473f882001-02-23 17:55:21 +00001009 } else {
Raphael Prevost48b60c32009-08-23 13:11:01 +02001010 /* pbm */
1011 __xmlIOErr(XML_FROM_HTTP, 0, "select failed\n");
1012 closesocket(s);
Ozkan Sezerf99d2222010-11-04 12:08:08 +01001013 return INVALID_SOCKET;
Owen Taylor3473f882001-02-23 17:55:21 +00001014 }
Raphael Prevost48b60c32009-08-23 13:11:01 +02001015
1016 return (s);
Owen Taylor3473f882001-02-23 17:55:21 +00001017}
Raphael Prevost48b60c32009-08-23 13:11:01 +02001018
Owen Taylor3473f882001-02-23 17:55:21 +00001019/**
1020 * xmlNanoHTTPConnectHost:
1021 * @host: the host name
1022 * @port: the port number
1023 *
1024 * Attempt a connection to the given host:port endpoint. It tries
1025 * the multiple IP provided by the DNS if available.
1026 *
1027 * Returns -1 in case of failure, the file descriptor number otherwise
1028 */
1029
Ozkan Sezerf99d2222010-11-04 12:08:08 +01001030static SOCKET
Owen Taylor3473f882001-02-23 17:55:21 +00001031xmlNanoHTTPConnectHost(const char *host, int port)
1032{
1033 struct hostent *h;
Daniel Veillard2db8c122003-07-08 12:16:59 +00001034 struct sockaddr *addr = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001035 struct in_addr ia;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001036 struct sockaddr_in sockin;
Daniel Veillard5c396542002-03-15 07:57:50 +00001037
Owen Taylor3473f882001-02-23 17:55:21 +00001038#ifdef SUPPORT_IP6
1039 struct in6_addr ia6;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001040 struct sockaddr_in6 sockin6;
Owen Taylor3473f882001-02-23 17:55:21 +00001041#endif
1042 int i;
Ozkan Sezerf99d2222010-11-04 12:08:08 +01001043 SOCKET s;
Daniel Veillard5c396542002-03-15 07:57:50 +00001044
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001045 memset (&sockin, 0, sizeof(sockin));
1046#ifdef SUPPORT_IP6
1047 memset (&sockin6, 0, sizeof(sockin6));
Rob Richardscb418de2005-10-13 23:12:42 +00001048#endif
1049
1050#if !defined(HAVE_GETADDRINFO) && defined(SUPPORT_IP6) && defined(RES_USE_INET6)
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001051 if (have_ipv6 ())
Daniel Veillard560c2a42003-07-06 21:13:49 +00001052 {
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001053 if (!(_res.options & RES_INIT))
1054 res_init();
1055 _res.options |= RES_USE_INET6;
1056 }
Rob Richardscb418de2005-10-13 23:12:42 +00001057#endif
1058
1059#if defined(HAVE_GETADDRINFO) && defined(SUPPORT_IP6) && !defined(_WIN32)
1060 if (have_ipv6 ())
1061#endif
1062#if defined(HAVE_GETADDRINFO) && (defined(SUPPORT_IP6) || defined(_WIN32))
Daniel Veillard560c2a42003-07-06 21:13:49 +00001063 {
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001064 int status;
1065 struct addrinfo hints, *res, *result;
1066
1067 result = NULL;
1068 memset (&hints, 0,sizeof(hints));
1069 hints.ai_socktype = SOCK_STREAM;
1070
1071 status = getaddrinfo (host, NULL, &hints, &result);
1072 if (status) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001073 __xmlIOErr(XML_FROM_HTTP, 0, "getaddrinfo failed\n");
Ozkan Sezerf99d2222010-11-04 12:08:08 +01001074 return INVALID_SOCKET;
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001075 }
1076
1077 for (res = result; res; res = res->ai_next) {
Rob Richardscb418de2005-10-13 23:12:42 +00001078 if (res->ai_family == AF_INET) {
1079 if (res->ai_addrlen > sizeof(sockin)) {
1080 __xmlIOErr(XML_FROM_HTTP, 0, "address size mismatch\n");
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001081 freeaddrinfo (result);
Ozkan Sezerf99d2222010-11-04 12:08:08 +01001082 return INVALID_SOCKET;
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001083 }
Rob Richardscb418de2005-10-13 23:12:42 +00001084 memcpy (&sockin, res->ai_addr, res->ai_addrlen);
1085 sockin.sin_port = htons (port);
1086 addr = (struct sockaddr *)&sockin;
1087#ifdef SUPPORT_IP6
1088 } else if (have_ipv6 () && (res->ai_family == AF_INET6)) {
1089 if (res->ai_addrlen > sizeof(sockin6)) {
1090 __xmlIOErr(XML_FROM_HTTP, 0, "address size mismatch\n");
1091 freeaddrinfo (result);
Ozkan Sezerf99d2222010-11-04 12:08:08 +01001092 return INVALID_SOCKET;
Rob Richardscb418de2005-10-13 23:12:42 +00001093 }
1094 memcpy (&sockin6, res->ai_addr, res->ai_addrlen);
1095 sockin6.sin6_port = htons (port);
1096 addr = (struct sockaddr *)&sockin6;
1097#endif
1098 } else
1099 continue; /* for */
1100
1101 s = xmlNanoHTTPConnectAttempt (addr);
Ozkan Sezerf99d2222010-11-04 12:08:08 +01001102 if (s != INVALID_SOCKET) {
Rob Richardscb418de2005-10-13 23:12:42 +00001103 freeaddrinfo (result);
1104 return (s);
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001105 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001106 }
Rob Richardscb418de2005-10-13 23:12:42 +00001107
Daniel Veillard3dc93a42003-07-10 14:04:33 +00001108 if (result)
1109 freeaddrinfo (result);
Rob Richardscb418de2005-10-13 23:12:42 +00001110 }
Owen Taylor3473f882001-02-23 17:55:21 +00001111#endif
Rob Richardscb418de2005-10-13 23:12:42 +00001112#if defined(HAVE_GETADDRINFO) && defined(SUPPORT_IP6) && !defined(_WIN32)
1113 else
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001114#endif
Rob Richardscb418de2005-10-13 23:12:42 +00001115#if !defined(HAVE_GETADDRINFO) || !defined(_WIN32)
1116 {
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001117 h = gethostbyname (host);
1118 if (h == NULL) {
Daniel Veillard56b2db72002-03-25 16:35:28 +00001119
1120/*
1121 * Okay, I got fed up by the non-portability of this error message
1122 * extraction code. it work on Linux, if it work on your platform
1123 * and one want to enable it, send me the defined(foobar) needed
1124 */
1125#if defined(HAVE_NETDB_H) && defined(HOST_NOT_FOUND) && defined(linux)
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001126 const char *h_err_txt = "";
Daniel Veillardf012a642001-07-23 19:10:52 +00001127
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001128 switch (h_errno) {
1129 case HOST_NOT_FOUND:
1130 h_err_txt = "Authoritive host not found";
1131 break;
Daniel Veillardf012a642001-07-23 19:10:52 +00001132
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001133 case TRY_AGAIN:
1134 h_err_txt =
1135 "Non-authoritive host not found or server failure.";
1136 break;
Daniel Veillardf012a642001-07-23 19:10:52 +00001137
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001138 case NO_RECOVERY:
1139 h_err_txt =
1140 "Non-recoverable errors: FORMERR, REFUSED, or NOTIMP.";
1141 break;
Daniel Veillard5c396542002-03-15 07:57:50 +00001142
Daniel Veillardd95b6892012-04-02 17:48:53 +08001143#ifdef NO_ADDRESS
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001144 case NO_ADDRESS:
1145 h_err_txt =
1146 "Valid name, no data record of requested type.";
1147 break;
Daniel Veillardd95b6892012-04-02 17:48:53 +08001148#endif
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
Ozkan Sezerf99d2222010-11-04 12:08:08 +01001158 return INVALID_SOCKET;
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001159 }
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");
Ozkan Sezerf99d2222010-11-04 12:08:08 +01001166 return INVALID_SOCKET;
Daniel Veillard8e2c9792004-10-27 09:39:50 +00001167 }
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 Veillardac17e592012-04-02 15:45:13 +08001171 sockin.sin_port = (unsigned 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");
Ozkan Sezerf99d2222010-11-04 12:08:08 +01001178 return INVALID_SOCKET;
Daniel Veillard8e2c9792004-10-27 09:39:50 +00001179 }
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);
Ozkan Sezerf99d2222010-11-04 12:08:08 +01001190 if (s != INVALID_SOCKET)
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001191 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
Ozkan Sezerf99d2222010-11-04 12:08:08 +01001201 return INVALID_SOCKET;
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;
Ozkan Sezerf99d2222010-11-04 12:08:08 +01001345 int blen;
1346 SOCKET ret;
Owen Taylor3473f882001-02-23 17:55:21 +00001347 int nbRedirects = 0;
1348 char *redirURL = NULL;
William M. Brack78637da2003-07-31 14:47:38 +00001349#ifdef DEBUG_HTTP
1350 int xmt_bytes;
1351#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001352
1353 if (URL == NULL) return(NULL);
1354 if (method == NULL) method = "GET";
1355 xmlNanoHTTPInit();
1356
1357retry:
1358 if (redirURL == NULL)
1359 ctxt = xmlNanoHTTPNewCtxt(URL);
1360 else {
1361 ctxt = xmlNanoHTTPNewCtxt(redirURL);
Daniel Veillarda840b692003-10-19 13:35:37 +00001362 ctxt->location = xmlMemStrdup(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001363 }
1364
Daniel Veillardf012a642001-07-23 19:10:52 +00001365 if ( ctxt == NULL ) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001366 return ( NULL );
1367 }
1368
Owen Taylor3473f882001-02-23 17:55:21 +00001369 if ((ctxt->protocol == NULL) || (strcmp(ctxt->protocol, "http"))) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001370 __xmlIOErr(XML_FROM_HTTP, XML_HTTP_URL_SYNTAX, "Not a valid HTTP URI");
Owen Taylor3473f882001-02-23 17:55:21 +00001371 xmlNanoHTTPFreeCtxt(ctxt);
1372 if (redirURL != NULL) xmlFree(redirURL);
1373 return(NULL);
1374 }
1375 if (ctxt->hostname == NULL) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001376 __xmlIOErr(XML_FROM_HTTP, XML_HTTP_UNKNOWN_HOST,
1377 "Failed to identify host in URI");
Owen Taylor3473f882001-02-23 17:55:21 +00001378 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001379 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001380 return(NULL);
1381 }
1382 if (proxy) {
1383 blen = strlen(ctxt->hostname) * 2 + 16;
1384 ret = xmlNanoHTTPConnectHost(proxy, proxyPort);
1385 }
1386 else {
1387 blen = strlen(ctxt->hostname);
1388 ret = xmlNanoHTTPConnectHost(ctxt->hostname, ctxt->port);
1389 }
Ozkan Sezerf99d2222010-11-04 12:08:08 +01001390 if (ret == INVALID_SOCKET) {
Owen Taylor3473f882001-02-23 17:55:21 +00001391 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001392 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001393 return(NULL);
1394 }
1395 ctxt->fd = ret;
1396
Daniel Veillardf012a642001-07-23 19:10:52 +00001397 if (input == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00001398 ilen = 0;
Daniel Veillardf012a642001-07-23 19:10:52 +00001399 else
1400 blen += 36;
1401
Owen Taylor3473f882001-02-23 17:55:21 +00001402 if (headers != NULL)
Daniel Veillardf012a642001-07-23 19:10:52 +00001403 blen += strlen(headers) + 2;
Owen Taylor3473f882001-02-23 17:55:21 +00001404 if (contentType && *contentType)
William M. Brackead35832008-02-06 04:12:46 +00001405 /* reserve for string plus 'Content-Type: \r\n" */
Owen Taylor3473f882001-02-23 17:55:21 +00001406 blen += strlen(*contentType) + 16;
Daniel Veillard351f2d62005-04-13 02:55:12 +00001407 if (ctxt->query != NULL)
William M. Brackead35832008-02-06 04:12:46 +00001408 /* 1 for '?' */
Daniel Veillard351f2d62005-04-13 02:55:12 +00001409 blen += strlen(ctxt->query) + 1;
Daniel Veillardf012a642001-07-23 19:10:52 +00001410 blen += strlen(method) + strlen(ctxt->path) + 24;
Daniel Veillard9a2724d2005-12-15 11:12:26 +00001411#ifdef HAVE_ZLIB_H
William M. Brackead35832008-02-06 04:12:46 +00001412 /* reserve for possible 'Accept-Encoding: gzip' string */
Daniel Veillard9a2724d2005-12-15 11:12:26 +00001413 blen += 23;
1414#endif
William M. Brackead35832008-02-06 04:12:46 +00001415 if (ctxt->port != 80) {
1416 /* reserve space for ':xxxxx', incl. potential proxy */
1417 if (proxy)
1418 blen += 12;
1419 else
1420 blen += 6;
1421 }
Daniel Veillard82cb3192003-10-29 13:39:15 +00001422 bp = (char*)xmlMallocAtomic(blen);
Daniel Veillardf012a642001-07-23 19:10:52 +00001423 if ( bp == NULL ) {
1424 xmlNanoHTTPFreeCtxt( ctxt );
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001425 xmlHTTPErrMemory("allocating header buffer");
Daniel Veillardf012a642001-07-23 19:10:52 +00001426 return ( NULL );
1427 }
1428
1429 p = bp;
1430
Owen Taylor3473f882001-02-23 17:55:21 +00001431 if (proxy) {
1432 if (ctxt->port != 80) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001433 p += snprintf( p, blen - (p - bp), "%s http://%s:%d%s",
1434 method, ctxt->hostname,
Daniel Veillardf012a642001-07-23 19:10:52 +00001435 ctxt->port, ctxt->path );
Owen Taylor3473f882001-02-23 17:55:21 +00001436 }
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001437 else
1438 p += snprintf( p, blen - (p - bp), "%s http://%s%s", method,
Daniel Veillardf012a642001-07-23 19:10:52 +00001439 ctxt->hostname, ctxt->path);
Owen Taylor3473f882001-02-23 17:55:21 +00001440 }
1441 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001442 p += snprintf( p, blen - (p - bp), "%s %s", method, ctxt->path);
Daniel Veillardf012a642001-07-23 19:10:52 +00001443
Daniel Veillard351f2d62005-04-13 02:55:12 +00001444 if (ctxt->query != NULL)
1445 p += snprintf( p, blen - (p - bp), "?%s", ctxt->query);
1446
William M. Brackec720082007-08-24 02:57:38 +00001447 if (ctxt->port == 80) {
1448 p += snprintf( p, blen - (p - bp), " HTTP/1.0\r\nHost: %s\r\n",
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001449 ctxt->hostname);
William M. Brackec720082007-08-24 02:57:38 +00001450 } else {
1451 p += snprintf( p, blen - (p - bp), " HTTP/1.0\r\nHost: %s:%d\r\n",
1452 ctxt->hostname, ctxt->port);
1453 }
Daniel Veillardf012a642001-07-23 19:10:52 +00001454
Daniel Veillard9a2724d2005-12-15 11:12:26 +00001455#ifdef HAVE_ZLIB_H
1456 p += snprintf(p, blen - (p - bp), "Accept-Encoding: gzip\r\n");
1457#endif
1458
Daniel Veillardf012a642001-07-23 19:10:52 +00001459 if (contentType != NULL && *contentType)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001460 p += snprintf(p, blen - (p - bp), "Content-Type: %s\r\n", *contentType);
Daniel Veillardf012a642001-07-23 19:10:52 +00001461
1462 if (headers != NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001463 p += snprintf( p, blen - (p - bp), "%s", headers );
Daniel Veillardf012a642001-07-23 19:10:52 +00001464
Owen Taylor3473f882001-02-23 17:55:21 +00001465 if (input != NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001466 snprintf(p, blen - (p - bp), "Content-Length: %d\r\n\r\n", ilen );
Owen Taylor3473f882001-02-23 17:55:21 +00001467 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001468 snprintf(p, blen - (p - bp), "\r\n");
Daniel Veillardf012a642001-07-23 19:10:52 +00001469
Owen Taylor3473f882001-02-23 17:55:21 +00001470#ifdef DEBUG_HTTP
1471 xmlGenericError(xmlGenericErrorContext,
1472 "-> %s%s", proxy? "(Proxy) " : "", bp);
1473 if ((blen -= strlen(bp)+1) < 0)
1474 xmlGenericError(xmlGenericErrorContext,
1475 "ERROR: overflowed buffer by %d bytes\n", -blen);
1476#endif
1477 ctxt->outptr = ctxt->out = bp;
1478 ctxt->state = XML_NANO_HTTP_WRITE;
Daniel Veillardf012a642001-07-23 19:10:52 +00001479 blen = strlen( ctxt->out );
Daniel Veillardf012a642001-07-23 19:10:52 +00001480#ifdef DEBUG_HTTP
William M. Brack78637da2003-07-31 14:47:38 +00001481 xmt_bytes = xmlNanoHTTPSend(ctxt, ctxt->out, blen );
Daniel Veillardf012a642001-07-23 19:10:52 +00001482 if ( xmt_bytes != blen )
1483 xmlGenericError( xmlGenericErrorContext,
1484 "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
1485 xmt_bytes, blen,
1486 "bytes of HTTP headers sent to host",
1487 ctxt->hostname );
William M. Brack78637da2003-07-31 14:47:38 +00001488#else
1489 xmlNanoHTTPSend(ctxt, ctxt->out, blen );
Daniel Veillardf012a642001-07-23 19:10:52 +00001490#endif
1491
1492 if ( input != NULL ) {
William M. Brack78637da2003-07-31 14:47:38 +00001493#ifdef DEBUG_HTTP
Daniel Veillardf012a642001-07-23 19:10:52 +00001494 xmt_bytes = xmlNanoHTTPSend( ctxt, input, ilen );
1495
Daniel Veillardf012a642001-07-23 19:10:52 +00001496 if ( xmt_bytes != ilen )
1497 xmlGenericError( xmlGenericErrorContext,
1498 "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
1499 xmt_bytes, ilen,
1500 "bytes of HTTP content sent to host",
1501 ctxt->hostname );
William M. Brack78637da2003-07-31 14:47:38 +00001502#else
1503 xmlNanoHTTPSend( ctxt, input, ilen );
Daniel Veillardf012a642001-07-23 19:10:52 +00001504#endif
1505 }
1506
Owen Taylor3473f882001-02-23 17:55:21 +00001507 ctxt->state = XML_NANO_HTTP_READ;
Owen Taylor3473f882001-02-23 17:55:21 +00001508
1509 while ((p = xmlNanoHTTPReadLine(ctxt)) != NULL) {
Daniel Veillard594e5df2009-09-07 14:58:47 +02001510 if (*p == 0) {
Owen Taylor3473f882001-02-23 17:55:21 +00001511 ctxt->content = ctxt->inrptr;
1512 xmlFree(p);
1513 break;
1514 }
1515 xmlNanoHTTPScanAnswer(ctxt, p);
1516
1517#ifdef DEBUG_HTTP
1518 xmlGenericError(xmlGenericErrorContext, "<- %s\n", p);
1519#endif
1520 xmlFree(p);
1521 }
1522
1523 if ((ctxt->location != NULL) && (ctxt->returnValue >= 300) &&
1524 (ctxt->returnValue < 400)) {
1525#ifdef DEBUG_HTTP
1526 xmlGenericError(xmlGenericErrorContext,
1527 "\nRedirect to: %s\n", ctxt->location);
1528#endif
Daniel Veillardf012a642001-07-23 19:10:52 +00001529 while ( xmlNanoHTTPRecv(ctxt) > 0 ) ;
Owen Taylor3473f882001-02-23 17:55:21 +00001530 if (nbRedirects < XML_NANO_HTTP_MAX_REDIR) {
1531 nbRedirects++;
Daniel Veillard9403a042001-05-28 11:00:53 +00001532 if (redirURL != NULL)
1533 xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001534 redirURL = xmlMemStrdup(ctxt->location);
1535 xmlNanoHTTPFreeCtxt(ctxt);
1536 goto retry;
1537 }
1538 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001539 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001540#ifdef DEBUG_HTTP
1541 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardf012a642001-07-23 19:10:52 +00001542 "xmlNanoHTTPMethodRedir: Too many redirects, aborting ...\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001543#endif
1544 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001545 }
1546
1547 if (contentType != NULL) {
1548 if (ctxt->contentType != NULL)
1549 *contentType = xmlMemStrdup(ctxt->contentType);
1550 else
1551 *contentType = NULL;
1552 }
1553
Daniel Veillard9403a042001-05-28 11:00:53 +00001554 if ((redir != NULL) && (redirURL != NULL)) {
1555 *redir = redirURL;
1556 } else {
1557 if (redirURL != NULL)
1558 xmlFree(redirURL);
1559 if (redir != NULL)
1560 *redir = NULL;
1561 }
1562
Owen Taylor3473f882001-02-23 17:55:21 +00001563#ifdef DEBUG_HTTP
1564 if (ctxt->contentType != NULL)
1565 xmlGenericError(xmlGenericErrorContext,
1566 "\nCode %d, content-type '%s'\n\n",
1567 ctxt->returnValue, ctxt->contentType);
1568 else
1569 xmlGenericError(xmlGenericErrorContext,
1570 "\nCode %d, no content-type\n\n",
1571 ctxt->returnValue);
1572#endif
1573
1574 return((void *) ctxt);
1575}
1576
1577/**
Daniel Veillard9403a042001-05-28 11:00:53 +00001578 * xmlNanoHTTPMethod:
1579 * @URL: The URL to load
1580 * @method: the HTTP method to use
1581 * @input: the input string if any
1582 * @contentType: the Content-Type information IN and OUT
1583 * @headers: the extra headers
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001584 * @ilen: input length
Daniel Veillard9403a042001-05-28 11:00:53 +00001585 *
1586 * This function try to open a connection to the indicated resource
1587 * via HTTP using the given @method, adding the given extra headers
1588 * and the input buffer for the request content.
1589 *
1590 * Returns NULL in case of failure, otherwise a request handler.
1591 * The contentType, if provided must be freed by the caller
1592 */
1593
1594void*
1595xmlNanoHTTPMethod(const char *URL, const char *method, const char *input,
Daniel Veillardf012a642001-07-23 19:10:52 +00001596 char **contentType, const char *headers, int ilen) {
Daniel Veillard9403a042001-05-28 11:00:53 +00001597 return(xmlNanoHTTPMethodRedir(URL, method, input, contentType,
Daniel Veillardf012a642001-07-23 19:10:52 +00001598 NULL, headers, ilen));
Daniel Veillard9403a042001-05-28 11:00:53 +00001599}
1600
1601/**
Owen Taylor3473f882001-02-23 17:55:21 +00001602 * xmlNanoHTTPFetch:
1603 * @URL: The URL to load
1604 * @filename: the filename where the content should be saved
1605 * @contentType: if available the Content-Type information will be
1606 * returned at that location
1607 *
1608 * This function try to fetch the indicated resource via HTTP GET
1609 * and save it's content in the file.
1610 *
1611 * Returns -1 in case of failure, 0 incase of success. The contentType,
1612 * if provided must be freed by the caller
1613 */
1614int
1615xmlNanoHTTPFetch(const char *URL, const char *filename, char **contentType) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001616 void *ctxt = NULL;
1617 char *buf = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001618 int fd;
1619 int len;
Stefan Kostdff8d0f2011-05-09 12:14:59 +03001620 int ret = 0;
1621
William M. Brack015ccb22005-02-13 08:18:52 +00001622 if (filename == NULL) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001623 ctxt = xmlNanoHTTPOpen(URL, contentType);
1624 if (ctxt == NULL) return(-1);
1625
1626 if (!strcmp(filename, "-"))
1627 fd = 0;
1628 else {
1629 fd = open(filename, O_CREAT | O_WRONLY, 00644);
1630 if (fd < 0) {
1631 xmlNanoHTTPClose(ctxt);
1632 if ((contentType != NULL) && (*contentType != NULL)) {
1633 xmlFree(*contentType);
1634 *contentType = NULL;
1635 }
1636 return(-1);
1637 }
1638 }
1639
Daniel Veillardf012a642001-07-23 19:10:52 +00001640 xmlNanoHTTPFetchContent( ctxt, &buf, &len );
1641 if ( len > 0 ) {
Stefan Kostdff8d0f2011-05-09 12:14:59 +03001642 if (write(fd, buf, len) == -1) {
1643 ret = -1;
1644 }
Owen Taylor3473f882001-02-23 17:55:21 +00001645 }
1646
1647 xmlNanoHTTPClose(ctxt);
1648 close(fd);
Stefan Kostdff8d0f2011-05-09 12:14:59 +03001649 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +00001650}
1651
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001652#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001653/**
1654 * xmlNanoHTTPSave:
1655 * @ctxt: the HTTP context
1656 * @filename: the filename where the content should be saved
1657 *
1658 * This function saves the output of the HTTP transaction to a file
1659 * It closes and free the context at the end
1660 *
1661 * Returns -1 in case of failure, 0 incase of success.
1662 */
1663int
1664xmlNanoHTTPSave(void *ctxt, const char *filename) {
Daniel Veillarde3924972001-07-25 20:25:21 +00001665 char *buf = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001666 int fd;
1667 int len;
Stefan Kostdff8d0f2011-05-09 12:14:59 +03001668 int ret = 0;
1669
William M. Brack015ccb22005-02-13 08:18:52 +00001670 if ((ctxt == NULL) || (filename == NULL)) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001671
1672 if (!strcmp(filename, "-"))
1673 fd = 0;
1674 else {
Daniel Veillardcd2ebab2007-08-23 20:47:33 +00001675 fd = open(filename, O_CREAT | O_WRONLY, 0666);
Owen Taylor3473f882001-02-23 17:55:21 +00001676 if (fd < 0) {
1677 xmlNanoHTTPClose(ctxt);
1678 return(-1);
1679 }
1680 }
1681
Daniel Veillardf012a642001-07-23 19:10:52 +00001682 xmlNanoHTTPFetchContent( ctxt, &buf, &len );
1683 if ( len > 0 ) {
Stefan Kostdff8d0f2011-05-09 12:14:59 +03001684 if (write(fd, buf, len) == -1) {
1685 ret = -1;
1686 }
Owen Taylor3473f882001-02-23 17:55:21 +00001687 }
1688
1689 xmlNanoHTTPClose(ctxt);
William M. Brack20d82362004-03-17 08:44:46 +00001690 close(fd);
Stefan Kostdff8d0f2011-05-09 12:14:59 +03001691 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +00001692}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001693#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001694
1695/**
1696 * xmlNanoHTTPReturnCode:
1697 * @ctx: the HTTP context
1698 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001699 * Get the latest HTTP return code received
1700 *
Owen Taylor3473f882001-02-23 17:55:21 +00001701 * Returns the HTTP return code for the request.
1702 */
1703int
1704xmlNanoHTTPReturnCode(void *ctx) {
1705 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1706
1707 if (ctxt == NULL) return(-1);
1708
1709 return(ctxt->returnValue);
1710}
1711
1712/**
1713 * xmlNanoHTTPAuthHeader:
1714 * @ctx: the HTTP context
1715 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001716 * Get the authentication header of an HTTP context
1717 *
Owen Taylor3473f882001-02-23 17:55:21 +00001718 * Returns the stashed value of the WWW-Authenticate or Proxy-Authenticate
1719 * header.
1720 */
1721const char *
1722xmlNanoHTTPAuthHeader(void *ctx) {
1723 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1724
1725 if (ctxt == NULL) return(NULL);
1726
1727 return(ctxt->authHeader);
1728}
1729
Daniel Veillardf012a642001-07-23 19:10:52 +00001730/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00001731 * xmlNanoHTTPContentLength:
Daniel Veillardf012a642001-07-23 19:10:52 +00001732 * @ctx: the HTTP context
1733 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001734 * Provides the specified content length from the HTTP header.
1735 *
Daniel Veillardf012a642001-07-23 19:10:52 +00001736 * Return the specified content length from the HTTP header. Note that
1737 * a value of -1 indicates that the content length element was not included in
1738 * the response header.
1739 */
1740int
1741xmlNanoHTTPContentLength( void * ctx ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001742 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillardf012a642001-07-23 19:10:52 +00001743
1744 return ( ( ctxt == NULL ) ? -1 : ctxt->ContentLength );
1745}
1746
1747/**
Daniel Veillard847332a2003-10-18 11:29:40 +00001748 * xmlNanoHTTPRedir:
1749 * @ctx: the HTTP context
1750 *
1751 * Provides the specified redirection URL if available from the HTTP header.
1752 *
1753 * Return the specified redirection URL or NULL if not redirected.
1754 */
1755const char *
1756xmlNanoHTTPRedir( void * ctx ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001757 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillard847332a2003-10-18 11:29:40 +00001758
1759 return ( ( ctxt == NULL ) ? NULL : ctxt->location );
1760}
1761
1762/**
1763 * xmlNanoHTTPEncoding:
1764 * @ctx: the HTTP context
1765 *
1766 * Provides the specified encoding if specified in the HTTP headers.
1767 *
1768 * Return the specified encoding or NULL if not available
1769 */
1770const char *
1771xmlNanoHTTPEncoding( void * ctx ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001772 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillard847332a2003-10-18 11:29:40 +00001773
1774 return ( ( ctxt == NULL ) ? NULL : ctxt->encoding );
1775}
1776
1777/**
Daniel Veillarda840b692003-10-19 13:35:37 +00001778 * xmlNanoHTTPMimeType:
1779 * @ctx: the HTTP context
1780 *
1781 * Provides the specified Mime-Type if specified in the HTTP headers.
1782 *
1783 * Return the specified Mime-Type or NULL if not available
1784 */
1785const char *
1786xmlNanoHTTPMimeType( void * ctx ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001787 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillarda840b692003-10-19 13:35:37 +00001788
1789 return ( ( ctxt == NULL ) ? NULL : ctxt->mimeType );
1790}
1791
1792/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00001793 * xmlNanoHTTPFetchContent:
Daniel Veillardf012a642001-07-23 19:10:52 +00001794 * @ctx: the HTTP context
1795 * @ptr: pointer to set to the content buffer.
1796 * @len: integer pointer to hold the length of the content
1797 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001798 * Check if all the content was read
1799 *
Daniel Veillardf012a642001-07-23 19:10:52 +00001800 * Returns 0 if all the content was read and available, returns
1801 * -1 if received content length was less than specified or an error
1802 * occurred.
1803 */
Daniel Veillarda2351322004-06-27 12:08:10 +00001804static int
Daniel Veillardf012a642001-07-23 19:10:52 +00001805xmlNanoHTTPFetchContent( void * ctx, char ** ptr, int * len ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001806 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillardf012a642001-07-23 19:10:52 +00001807
1808 int rc = 0;
1809 int cur_lgth;
1810 int rcvd_lgth;
1811 int dummy_int;
1812 char * dummy_ptr = NULL;
1813
1814 /* Dummy up return input parameters if not provided */
1815
1816 if ( len == NULL )
1817 len = &dummy_int;
1818
1819 if ( ptr == NULL )
1820 ptr = &dummy_ptr;
1821
1822 /* But can't work without the context pointer */
1823
1824 if ( ( ctxt == NULL ) || ( ctxt->content == NULL ) ) {
1825 *len = 0;
1826 *ptr = NULL;
1827 return ( -1 );
1828 }
1829
1830 rcvd_lgth = ctxt->inptr - ctxt->content;
1831
1832 while ( (cur_lgth = xmlNanoHTTPRecv( ctxt )) > 0 ) {
1833
1834 rcvd_lgth += cur_lgth;
1835 if ( (ctxt->ContentLength > 0) && (rcvd_lgth >= ctxt->ContentLength) )
1836 break;
1837 }
1838
1839 *ptr = ctxt->content;
1840 *len = rcvd_lgth;
1841
1842 if ( ( ctxt->ContentLength > 0 ) && ( rcvd_lgth < ctxt->ContentLength ) )
1843 rc = -1;
1844 else if ( rcvd_lgth == 0 )
1845 rc = -1;
1846
1847 return ( rc );
1848}
1849
Owen Taylor3473f882001-02-23 17:55:21 +00001850#ifdef STANDALONE
1851int main(int argc, char **argv) {
1852 char *contentType = NULL;
1853
1854 if (argv[1] != NULL) {
1855 if (argv[2] != NULL)
1856 xmlNanoHTTPFetch(argv[1], argv[2], &contentType);
1857 else
1858 xmlNanoHTTPFetch(argv[1], "-", &contentType);
1859 if (contentType != NULL) xmlFree(contentType);
1860 } else {
1861 xmlGenericError(xmlGenericErrorContext,
1862 "%s: minimal HTTP GET implementation\n", argv[0]);
1863 xmlGenericError(xmlGenericErrorContext,
1864 "\tusage %s [ URL [ filename ] ]\n", argv[0]);
1865 }
1866 xmlNanoHTTPCleanup();
1867 xmlMemoryDump();
1868 return(0);
1869}
1870#endif /* STANDALONE */
1871#else /* !LIBXML_HTTP_ENABLED */
1872#ifdef STANDALONE
1873#include <stdio.h>
1874int main(int argc, char **argv) {
1875 xmlGenericError(xmlGenericErrorContext,
1876 "%s : HTTP support not compiled in\n", argv[0]);
1877 return(0);
1878}
1879#endif /* STANDALONE */
1880#endif /* LIBXML_HTTP_ENABLED */
Daniel Veillard5d4644e2005-04-01 13:11:58 +00001881#define bottom_nanohttp
1882#include "elfgcchack.h"