blob: ac47ea679956088b5268a452841c6bc6f83073d4 [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 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +080013
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
Daniel Veillardf8e3db02012-09-11 13:26:36 +080049#include <fcntl.h>
Owen Taylor3473f882001-02-23 17:55:21 +000050#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
Daniel Veillard9a2724d2005-12-15 11:12:26 +000067#ifdef HAVE_ZLIB_H
68#include <zlib.h>
69#endif
70
Owen Taylor3473f882001-02-23 17:55:21 +000071
72#ifdef VMS
73#include <stropts>
Daniel Veillardc284c642005-03-31 10:24:24 +000074#define XML_SOCKLEN_T unsigned int
Owen Taylor3473f882001-02-23 17:55:21 +000075#endif
76
Daniel Veillard59d3ed82007-04-17 12:44:58 +000077#if defined(__MINGW32__) || defined(_WIN32_WCE)
Ozkan Sezerf99d2222010-11-04 12:08:08 +010078#ifndef _WINSOCKAPI_
Daniel Veillard1638a472003-08-14 01:23:25 +000079#define _WINSOCKAPI_
Ozkan Sezerf99d2222010-11-04 12:08:08 +010080#endif
Daniel Veillard1638a472003-08-14 01:23:25 +000081#include <wsockcompat.h>
82#include <winsock2.h>
Daniel Veillardc284c642005-03-31 10:24:24 +000083#undef XML_SOCKLEN_T
84#define XML_SOCKLEN_T unsigned int
Daniel Veillard1638a472003-08-14 01:23:25 +000085#endif
86
Daniel Veillardd0463562001-10-13 09:15:48 +000087#include <libxml/globals.h>
Daniel Veillardf012a642001-07-23 19:10:52 +000088#include <libxml/xmlerror.h>
Owen Taylor3473f882001-02-23 17:55:21 +000089#include <libxml/xmlmemory.h>
90#include <libxml/parser.h> /* for xmlStr(n)casecmp() */
91#include <libxml/nanohttp.h>
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000092#include <libxml/globals.h>
Daniel Veillard8efff672002-12-04 11:44:48 +000093#include <libxml/uri.h>
Owen Taylor3473f882001-02-23 17:55:21 +000094
95/**
96 * A couple portability macros
97 */
98#ifndef _WINSOCKAPI_
Daniel Veillardcba68392008-08-29 12:43:40 +000099#if !defined(__BEOS__) || defined(__HAIKU__)
Owen Taylor3473f882001-02-23 17:55:21 +0000100#define closesocket(s) close(s)
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000101#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000102#define SOCKET int
Ozkan Sezerf99d2222010-11-04 12:08:08 +0100103#define INVALID_SOCKET (-1)
Owen Taylor3473f882001-02-23 17:55:21 +0000104#endif
105
Daniel Veillard89f7f272003-09-29 13:29:09 +0000106#ifdef __BEOS__
107#ifndef PF_INET
108#define PF_INET AF_INET
109#endif
110#endif
111
Daniel Veillardc284c642005-03-31 10:24:24 +0000112#ifndef XML_SOCKLEN_T
113#define XML_SOCKLEN_T unsigned int
Daniel Veillard75be0132002-03-13 10:03:35 +0000114#endif
Daniel Veillardf012a642001-07-23 19:10:52 +0000115
Owen Taylor3473f882001-02-23 17:55:21 +0000116#ifdef STANDALONE
117#define DEBUG_HTTP
118#define xmlStrncasecmp(a, b, n) strncasecmp((char *)a, (char *)b, n)
119#define xmlStrcasecmpi(a, b) strcasecmp((char *)a, (char *)b)
120#endif
121
122#define XML_NANO_HTTP_MAX_REDIR 10
123
124#define XML_NANO_HTTP_CHUNK 4096
125
126#define XML_NANO_HTTP_CLOSED 0
127#define XML_NANO_HTTP_WRITE 1
128#define XML_NANO_HTTP_READ 2
129#define XML_NANO_HTTP_NONE 4
130
131typedef struct xmlNanoHTTPCtxt {
132 char *protocol; /* the protocol name */
133 char *hostname; /* the host name */
134 int port; /* the port */
135 char *path; /* the path within the URL */
Daniel Veillard351f2d62005-04-13 02:55:12 +0000136 char *query; /* the query string */
Owen Taylor3473f882001-02-23 17:55:21 +0000137 SOCKET fd; /* the file descriptor for the socket */
138 int state; /* WRITE / READ / CLOSED */
139 char *out; /* buffer sent (zero terminated) */
140 char *outptr; /* index within the buffer sent */
141 char *in; /* the receiving buffer */
142 char *content; /* the start of the content */
143 char *inptr; /* the next byte to read from network */
144 char *inrptr; /* the next byte to give back to the client */
145 int inlen; /* len of the input buffer */
146 int last; /* return code for last operation */
147 int returnValue; /* the protocol return value */
Daniel Veillard13cee4e2009-09-05 14:52:55 +0200148 int version; /* the protocol version */
Daniel Veillardf012a642001-07-23 19:10:52 +0000149 int ContentLength; /* specified content length from HTTP header */
Owen Taylor3473f882001-02-23 17:55:21 +0000150 char *contentType; /* the MIME type for the input */
151 char *location; /* the new URL in case of redirect */
152 char *authHeader; /* contents of {WWW,Proxy}-Authenticate header */
Daniel Veillard847332a2003-10-18 11:29:40 +0000153 char *encoding; /* encoding extracted from the contentType */
Daniel Veillarda840b692003-10-19 13:35:37 +0000154 char *mimeType; /* Mime-Type extracted from the contentType */
Daniel Veillard9a2724d2005-12-15 11:12:26 +0000155#ifdef HAVE_ZLIB_H
156 z_stream *strm; /* Zlib stream object */
157 int usesGzip; /* "Content-Encoding: gzip" was detected */
158#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000159} xmlNanoHTTPCtxt, *xmlNanoHTTPCtxtPtr;
160
161static int initialized = 0;
162static char *proxy = NULL; /* the proxy name if any */
163static int proxyPort; /* the proxy port if any */
164static unsigned int timeout = 60;/* the select() timeout in seconds */
165
Daniel Veillarda2351322004-06-27 12:08:10 +0000166static int xmlNanoHTTPFetchContent( void * ctx, char ** ptr, int * len );
Daniel Veillardf012a642001-07-23 19:10:52 +0000167
Owen Taylor3473f882001-02-23 17:55:21 +0000168/**
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000169 * xmlHTTPErrMemory:
170 * @extra: extra informations
171 *
172 * Handle an out of memory condition
173 */
174static void
175xmlHTTPErrMemory(const char *extra)
176{
177 __xmlSimpleError(XML_FROM_HTTP, XML_ERR_NO_MEMORY, NULL, NULL, extra);
178}
179
180/**
Owen Taylor3473f882001-02-23 17:55:21 +0000181 * A portability function
182 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000183static int socket_errno(void) {
Owen Taylor3473f882001-02-23 17:55:21 +0000184#ifdef _WINSOCKAPI_
185 return(WSAGetLastError());
186#else
187 return(errno);
188#endif
189}
190
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000191#ifdef SUPPORT_IP6
Daniel Veillard2db8c122003-07-08 12:16:59 +0000192static
193int have_ipv6(void) {
Ozkan Sezerf99d2222010-11-04 12:08:08 +0100194 SOCKET s;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000195
196 s = socket (AF_INET6, SOCK_STREAM, 0);
Ozkan Sezerf99d2222010-11-04 12:08:08 +0100197 if (s != INVALID_SOCKET) {
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000198 close (s);
199 return (1);
200 }
201 return (0);
202}
203#endif
204
Owen Taylor3473f882001-02-23 17:55:21 +0000205/**
206 * xmlNanoHTTPInit:
207 *
208 * Initialize the HTTP protocol layer.
209 * Currently it just checks for proxy informations
210 */
211
212void
213xmlNanoHTTPInit(void) {
214 const char *env;
215#ifdef _WINSOCKAPI_
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800216 WSADATA wsaData;
Owen Taylor3473f882001-02-23 17:55:21 +0000217#endif
218
219 if (initialized)
220 return;
221
222#ifdef _WINSOCKAPI_
223 if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0)
224 return;
225#endif
226
227 if (proxy == NULL) {
228 proxyPort = 80;
229 env = getenv("no_proxy");
Daniel Veillard29b17482004-08-16 00:39:03 +0000230 if (env && ((env[0] == '*') && (env[1] == 0)))
Owen Taylor3473f882001-02-23 17:55:21 +0000231 goto done;
232 env = getenv("http_proxy");
233 if (env != NULL) {
234 xmlNanoHTTPScanProxy(env);
235 goto done;
236 }
237 env = getenv("HTTP_PROXY");
238 if (env != NULL) {
239 xmlNanoHTTPScanProxy(env);
240 goto done;
241 }
242 }
243done:
244 initialized = 1;
245}
246
247/**
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000248 * xmlNanoHTTPCleanup:
Owen Taylor3473f882001-02-23 17:55:21 +0000249 *
250 * Cleanup the HTTP protocol layer.
251 */
252
253void
254xmlNanoHTTPCleanup(void) {
Daniel Veillard744acff2005-07-12 15:09:53 +0000255 if (proxy != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +0000256 xmlFree(proxy);
Daniel Veillard744acff2005-07-12 15:09:53 +0000257 proxy = NULL;
258 }
Owen Taylor3473f882001-02-23 17:55:21 +0000259#ifdef _WINSOCKAPI_
260 if (initialized)
261 WSACleanup();
262#endif
263 initialized = 0;
264 return;
265}
266
267/**
Owen Taylor3473f882001-02-23 17:55:21 +0000268 * xmlNanoHTTPScanURL:
269 * @ctxt: an HTTP context
270 * @URL: The URL used to initialize the context
271 *
272 * (Re)Initialize an HTTP context by parsing the URL and finding
273 * the protocol host port and path it indicates.
274 */
275
276static void
277xmlNanoHTTPScanURL(xmlNanoHTTPCtxtPtr ctxt, const char *URL) {
William M. Brack015ccb22005-02-13 08:18:52 +0000278 xmlURIPtr uri;
Steve Wolf19d785b2013-02-28 18:22:46 +0800279 int len;
280
William M. Brack015ccb22005-02-13 08:18:52 +0000281 /*
282 * Clear any existing data from the context
283 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800284 if (ctxt->protocol != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +0000285 xmlFree(ctxt->protocol);
286 ctxt->protocol = NULL;
287 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800288 if (ctxt->hostname != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +0000289 xmlFree(ctxt->hostname);
290 ctxt->hostname = NULL;
291 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800292 if (ctxt->path != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +0000293 xmlFree(ctxt->path);
294 ctxt->path = NULL;
295 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800296 if (ctxt->query != NULL) {
Daniel Veillard351f2d62005-04-13 02:55:12 +0000297 xmlFree(ctxt->query);
298 ctxt->query = NULL;
299 }
Owen Taylor3473f882001-02-23 17:55:21 +0000300 if (URL == NULL) return;
William M. Brack015ccb22005-02-13 08:18:52 +0000301
Daniel Veillard336a8e12005-08-07 10:46:19 +0000302 uri = xmlParseURIRaw(URL, 1);
William M. Brack015ccb22005-02-13 08:18:52 +0000303 if (uri == NULL)
304 return;
305
306 if ((uri->scheme == NULL) || (uri->server == NULL)) {
307 xmlFreeURI(uri);
308 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000309 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800310
William M. Brack015ccb22005-02-13 08:18:52 +0000311 ctxt->protocol = xmlMemStrdup(uri->scheme);
Steve Wolf19d785b2013-02-28 18:22:46 +0800312 /* special case of IPv6 addresses, the [] need to be removed */
313 if ((uri->server != NULL) && (*uri->server == '[')) {
314 len = strlen(uri->server);
315 if ((len > 2) && (uri->server[len - 1] == ']')) {
316 ctxt->hostname = (char *) xmlCharStrndup(uri->server + 1, len -2);
317 } else
318 ctxt->hostname = xmlMemStrdup(uri->server);
319 } else
320 ctxt->hostname = xmlMemStrdup(uri->server);
William M. Brack015ccb22005-02-13 08:18:52 +0000321 if (uri->path != NULL)
322 ctxt->path = xmlMemStrdup(uri->path);
323 else
324 ctxt->path = xmlMemStrdup("/");
Daniel Veillard351f2d62005-04-13 02:55:12 +0000325 if (uri->query != NULL)
326 ctxt->query = xmlMemStrdup(uri->query);
William M. Brack015ccb22005-02-13 08:18:52 +0000327 if (uri->port != 0)
328 ctxt->port = uri->port;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000329
William M. Brack015ccb22005-02-13 08:18:52 +0000330 xmlFreeURI(uri);
Owen Taylor3473f882001-02-23 17:55:21 +0000331}
332
333/**
334 * xmlNanoHTTPScanProxy:
335 * @URL: The proxy URL used to initialize the proxy context
336 *
337 * (Re)Initialize the HTTP Proxy context by parsing the URL and finding
338 * the protocol host port it indicates.
339 * Should be like http://myproxy/ or http://myproxy:3128/
340 * A NULL URL cleans up proxy informations.
341 */
342
343void
344xmlNanoHTTPScanProxy(const char *URL) {
William M. Brack015ccb22005-02-13 08:18:52 +0000345 xmlURIPtr uri;
Owen Taylor3473f882001-02-23 17:55:21 +0000346
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800347 if (proxy != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +0000348 xmlFree(proxy);
349 proxy = NULL;
350 }
William M. Brack015ccb22005-02-13 08:18:52 +0000351 proxyPort = 0;
352
Owen Taylor3473f882001-02-23 17:55:21 +0000353#ifdef DEBUG_HTTP
354 if (URL == NULL)
355 xmlGenericError(xmlGenericErrorContext,
356 "Removing HTTP proxy info\n");
357 else
358 xmlGenericError(xmlGenericErrorContext,
359 "Using HTTP proxy %s\n", URL);
360#endif
361 if (URL == NULL) return;
William M. Brack015ccb22005-02-13 08:18:52 +0000362
Daniel Veillard336a8e12005-08-07 10:46:19 +0000363 uri = xmlParseURIRaw(URL, 1);
William M. Brack015ccb22005-02-13 08:18:52 +0000364 if ((uri == NULL) || (uri->scheme == NULL) ||
365 (strcmp(uri->scheme, "http")) || (uri->server == NULL)) {
366 __xmlIOErr(XML_FROM_HTTP, XML_HTTP_URL_SYNTAX, "Syntax Error\n");
367 if (uri != NULL)
368 xmlFreeURI(uri);
369 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000370 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800371
William M. Brack015ccb22005-02-13 08:18:52 +0000372 proxy = xmlMemStrdup(uri->server);
373 if (uri->port != 0)
374 proxyPort = uri->port;
Owen Taylor3473f882001-02-23 17:55:21 +0000375
William M. Brack015ccb22005-02-13 08:18:52 +0000376 xmlFreeURI(uri);
Owen Taylor3473f882001-02-23 17:55:21 +0000377}
378
379/**
380 * xmlNanoHTTPNewCtxt:
381 * @URL: The URL used to initialize the context
382 *
383 * Allocate and initialize a new HTTP context.
384 *
385 * Returns an HTTP context or NULL in case of error.
386 */
387
388static xmlNanoHTTPCtxtPtr
389xmlNanoHTTPNewCtxt(const char *URL) {
390 xmlNanoHTTPCtxtPtr ret;
391
392 ret = (xmlNanoHTTPCtxtPtr) xmlMalloc(sizeof(xmlNanoHTTPCtxt));
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000393 if (ret == NULL) {
394 xmlHTTPErrMemory("allocating context");
395 return(NULL);
396 }
Owen Taylor3473f882001-02-23 17:55:21 +0000397
398 memset(ret, 0, sizeof(xmlNanoHTTPCtxt));
399 ret->port = 80;
400 ret->returnValue = 0;
Ozkan Sezerf99d2222010-11-04 12:08:08 +0100401 ret->fd = INVALID_SOCKET;
Daniel Veillardf012a642001-07-23 19:10:52 +0000402 ret->ContentLength = -1;
Owen Taylor3473f882001-02-23 17:55:21 +0000403
Daniel Veillardcacbe5d2003-01-10 16:09:51 +0000404 xmlNanoHTTPScanURL(ret, URL);
Owen Taylor3473f882001-02-23 17:55:21 +0000405
406 return(ret);
407}
408
409/**
410 * xmlNanoHTTPFreeCtxt:
411 * @ctxt: an HTTP context
412 *
413 * Frees the context after closing the connection.
414 */
415
416static void
417xmlNanoHTTPFreeCtxt(xmlNanoHTTPCtxtPtr ctxt) {
418 if (ctxt == NULL) return;
419 if (ctxt->hostname != NULL) xmlFree(ctxt->hostname);
420 if (ctxt->protocol != NULL) xmlFree(ctxt->protocol);
421 if (ctxt->path != NULL) xmlFree(ctxt->path);
Daniel Veillard351f2d62005-04-13 02:55:12 +0000422 if (ctxt->query != NULL) xmlFree(ctxt->query);
Owen Taylor3473f882001-02-23 17:55:21 +0000423 if (ctxt->out != NULL) xmlFree(ctxt->out);
424 if (ctxt->in != NULL) xmlFree(ctxt->in);
425 if (ctxt->contentType != NULL) xmlFree(ctxt->contentType);
Daniel Veillard847332a2003-10-18 11:29:40 +0000426 if (ctxt->encoding != NULL) xmlFree(ctxt->encoding);
Daniel Veillarda840b692003-10-19 13:35:37 +0000427 if (ctxt->mimeType != NULL) xmlFree(ctxt->mimeType);
Owen Taylor3473f882001-02-23 17:55:21 +0000428 if (ctxt->location != NULL) xmlFree(ctxt->location);
429 if (ctxt->authHeader != NULL) xmlFree(ctxt->authHeader);
Daniel Veillard9a2724d2005-12-15 11:12:26 +0000430#ifdef HAVE_ZLIB_H
431 if (ctxt->strm != NULL) {
432 inflateEnd(ctxt->strm);
433 xmlFree(ctxt->strm);
434 }
435#endif
436
Owen Taylor3473f882001-02-23 17:55:21 +0000437 ctxt->state = XML_NANO_HTTP_NONE;
Ozkan Sezerf99d2222010-11-04 12:08:08 +0100438 if (ctxt->fd != INVALID_SOCKET) closesocket(ctxt->fd);
439 ctxt->fd = INVALID_SOCKET;
Owen Taylor3473f882001-02-23 17:55:21 +0000440 xmlFree(ctxt);
441}
442
443/**
444 * xmlNanoHTTPSend:
445 * @ctxt: an HTTP context
446 *
447 * Send the input needed to initiate the processing on the server side
Daniel Veillardf012a642001-07-23 19:10:52 +0000448 * Returns number of bytes sent or -1 on error.
Owen Taylor3473f882001-02-23 17:55:21 +0000449 */
450
Daniel Veillardf012a642001-07-23 19:10:52 +0000451static int
Raphael Prevost48b60c32009-08-23 13:11:01 +0200452xmlNanoHTTPSend(xmlNanoHTTPCtxtPtr ctxt, const char *xmt_ptr, int outlen)
453{
454 int total_sent = 0;
455#ifdef HAVE_POLL_H
456 struct pollfd p;
457#else
458 struct timeval tv;
459 fd_set wfd;
460#endif
Daniel Veillardf012a642001-07-23 19:10:52 +0000461
Raphael Prevost48b60c32009-08-23 13:11:01 +0200462 if ((ctxt->state & XML_NANO_HTTP_WRITE) && (xmt_ptr != NULL)) {
Daniel Veillardf012a642001-07-23 19:10:52 +0000463 while (total_sent < outlen) {
464 int nsent = send(ctxt->fd, xmt_ptr + total_sent,
Raphael Prevost48b60c32009-08-23 13:11:01 +0200465 outlen - total_sent, 0);
466
467 if (nsent > 0)
Owen Taylor3473f882001-02-23 17:55:21 +0000468 total_sent += nsent;
Raphael Prevost48b60c32009-08-23 13:11:01 +0200469 else if ((nsent == -1) &&
Daniel Veillardba6db032001-07-31 16:25:45 +0000470#if defined(EAGAIN) && EAGAIN != EWOULDBLOCK
Raphael Prevost48b60c32009-08-23 13:11:01 +0200471 (socket_errno() != EAGAIN) &&
Daniel Veillardba6db032001-07-31 16:25:45 +0000472#endif
Raphael Prevost48b60c32009-08-23 13:11:01 +0200473 (socket_errno() != EWOULDBLOCK)) {
474 __xmlIOErr(XML_FROM_HTTP, 0, "send failed\n");
475 if (total_sent == 0)
476 total_sent = -1;
477 break;
478 } else {
479 /*
480 * No data sent
481 * Since non-blocking sockets are used, wait for
482 * socket to be writable or default timeout prior
483 * to retrying.
484 */
485#ifndef HAVE_POLL_H
spadixd29a5c82009-10-19 14:03:25 +0200486#ifndef _WINSOCKAPI_
Raphael Prevost48b60c32009-08-23 13:11:01 +0200487 if (ctxt->fd > FD_SETSIZE)
488 return -1;
spadixd29a5c82009-10-19 14:03:25 +0200489#endif
Daniel Veillardf012a642001-07-23 19:10:52 +0000490
Raphael Prevost48b60c32009-08-23 13:11:01 +0200491 tv.tv_sec = timeout;
492 tv.tv_usec = 0;
493 FD_ZERO(&wfd);
Daniel Veillard9e2110b2005-08-08 20:33:54 +0000494#ifdef _MSC_VER
495#pragma warning(push)
496#pragma warning(disable: 4018)
497#endif
Raphael Prevost48b60c32009-08-23 13:11:01 +0200498 FD_SET(ctxt->fd, &wfd);
Daniel Veillard9e2110b2005-08-08 20:33:54 +0000499#ifdef _MSC_VER
500#pragma warning(pop)
501#endif
Raphael Prevost48b60c32009-08-23 13:11:01 +0200502 (void) select(ctxt->fd + 1, NULL, &wfd, NULL, &tv);
503#else
504 p.fd = ctxt->fd;
505 p.events = POLLOUT;
506 (void) poll(&p, 1, timeout * 1000);
507#endif /* !HAVE_POLL_H */
508 }
509 }
Owen Taylor3473f882001-02-23 17:55:21 +0000510 }
Daniel Veillardf012a642001-07-23 19:10:52 +0000511
512 return total_sent;
Owen Taylor3473f882001-02-23 17:55:21 +0000513}
514
515/**
516 * xmlNanoHTTPRecv:
517 * @ctxt: an HTTP context
518 *
519 * Read information coming from the HTTP connection.
520 * This is a blocking call (but it blocks in select(), not read()).
521 *
522 * Returns the number of byte read or -1 in case of error.
523 */
524
525static int
Raphael Prevost48b60c32009-08-23 13:11:01 +0200526xmlNanoHTTPRecv(xmlNanoHTTPCtxtPtr ctxt)
527{
528#ifdef HAVE_POLL_H
529 struct pollfd p;
530#else
Owen Taylor3473f882001-02-23 17:55:21 +0000531 fd_set rfd;
532 struct timeval tv;
Raphael Prevost48b60c32009-08-23 13:11:01 +0200533#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000534
535
536 while (ctxt->state & XML_NANO_HTTP_READ) {
Raphael Prevost48b60c32009-08-23 13:11:01 +0200537 if (ctxt->in == NULL) {
538 ctxt->in = (char *) xmlMallocAtomic(65000 * sizeof(char));
539 if (ctxt->in == NULL) {
540 xmlHTTPErrMemory("allocating input");
541 ctxt->last = -1;
542 return (-1);
543 }
544 ctxt->inlen = 65000;
545 ctxt->inptr = ctxt->content = ctxt->inrptr = ctxt->in;
546 }
547 if (ctxt->inrptr > ctxt->in + XML_NANO_HTTP_CHUNK) {
548 int delta = ctxt->inrptr - ctxt->in;
549 int len = ctxt->inptr - ctxt->inrptr;
Owen Taylor3473f882001-02-23 17:55:21 +0000550
Raphael Prevost48b60c32009-08-23 13:11:01 +0200551 memmove(ctxt->in, ctxt->inrptr, len);
552 ctxt->inrptr -= delta;
553 ctxt->content -= delta;
554 ctxt->inptr -= delta;
555 }
556 if ((ctxt->in + ctxt->inlen) < (ctxt->inptr + XML_NANO_HTTP_CHUNK)) {
557 int d_inptr = ctxt->inptr - ctxt->in;
558 int d_content = ctxt->content - ctxt->in;
559 int d_inrptr = ctxt->inrptr - ctxt->in;
560 char *tmp_ptr = ctxt->in;
561
562 ctxt->inlen *= 2;
Daniel Veillardf012a642001-07-23 19:10:52 +0000563 ctxt->in = (char *) xmlRealloc(tmp_ptr, ctxt->inlen);
Raphael Prevost48b60c32009-08-23 13:11:01 +0200564 if (ctxt->in == NULL) {
565 xmlHTTPErrMemory("allocating input buffer");
566 xmlFree(tmp_ptr);
567 ctxt->last = -1;
568 return (-1);
569 }
Owen Taylor3473f882001-02-23 17:55:21 +0000570 ctxt->inptr = ctxt->in + d_inptr;
571 ctxt->content = ctxt->in + d_content;
572 ctxt->inrptr = ctxt->in + d_inrptr;
Raphael Prevost48b60c32009-08-23 13:11:01 +0200573 }
574 ctxt->last = recv(ctxt->fd, ctxt->inptr, XML_NANO_HTTP_CHUNK, 0);
575 if (ctxt->last > 0) {
576 ctxt->inptr += ctxt->last;
577 return (ctxt->last);
578 }
579 if (ctxt->last == 0) {
580 return (0);
581 }
582 if (ctxt->last == -1) {
583 switch (socket_errno()) {
584 case EINPROGRESS:
585 case EWOULDBLOCK:
Owen Taylor3473f882001-02-23 17:55:21 +0000586#if defined(EAGAIN) && EAGAIN != EWOULDBLOCK
Raphael Prevost48b60c32009-08-23 13:11:01 +0200587 case EAGAIN:
Owen Taylor3473f882001-02-23 17:55:21 +0000588#endif
Raphael Prevost48b60c32009-08-23 13:11:01 +0200589 break;
Daniel Veillardf012a642001-07-23 19:10:52 +0000590
Raphael Prevost48b60c32009-08-23 13:11:01 +0200591 case ECONNRESET:
592 case ESHUTDOWN:
593 return (0);
Daniel Veillardf012a642001-07-23 19:10:52 +0000594
Raphael Prevost48b60c32009-08-23 13:11:01 +0200595 default:
596 __xmlIOErr(XML_FROM_HTTP, 0, "recv failed\n");
597 return (-1);
598 }
599 }
600#ifdef HAVE_POLL_H
601 p.fd = ctxt->fd;
602 p.events = POLLIN;
603 if ((poll(&p, 1, timeout * 1000) < 1)
604#if defined(EINTR)
605 && (errno != EINTR)
606#endif
607 )
608 return (0);
609#else /* !HAVE_POLL_H */
spadixd29a5c82009-10-19 14:03:25 +0200610#ifndef _WINSOCKAPI_
Raphael Prevost48b60c32009-08-23 13:11:01 +0200611 if (ctxt->fd > FD_SETSIZE)
612 return 0;
spadixd29a5c82009-10-19 14:03:25 +0200613#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000614
Raphael Prevost48b60c32009-08-23 13:11:01 +0200615 tv.tv_sec = timeout;
616 tv.tv_usec = 0;
617 FD_ZERO(&rfd);
618
Daniel Veillard9e2110b2005-08-08 20:33:54 +0000619#ifdef _MSC_VER
620#pragma warning(push)
621#pragma warning(disable: 4018)
622#endif
Raphael Prevost48b60c32009-08-23 13:11:01 +0200623
624 FD_SET(ctxt->fd, &rfd);
625
Daniel Veillard9e2110b2005-08-08 20:33:54 +0000626#ifdef _MSC_VER
627#pragma warning(pop)
628#endif
Raphael Prevost48b60c32009-08-23 13:11:01 +0200629
630 if ((select(ctxt->fd + 1, &rfd, NULL, NULL, &tv) < 1)
Daniel Veillard50f34372001-08-03 12:06:36 +0000631#if defined(EINTR)
Raphael Prevost48b60c32009-08-23 13:11:01 +0200632 && (errno != EINTR)
Daniel Veillard50f34372001-08-03 12:06:36 +0000633#endif
Raphael Prevost48b60c32009-08-23 13:11:01 +0200634 )
635 return (0);
636#endif /* !HAVE_POLL_H */
Owen Taylor3473f882001-02-23 17:55:21 +0000637 }
Raphael Prevost48b60c32009-08-23 13:11:01 +0200638 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +0000639}
640
641/**
642 * xmlNanoHTTPReadLine:
643 * @ctxt: an HTTP context
644 *
645 * Read one line in the HTTP server output, usually for extracting
646 * the HTTP protocol informations from the answer header.
647 *
648 * Returns a newly allocated string with a copy of the line, or NULL
649 * which indicate the end of the input.
650 */
651
652static char *
653xmlNanoHTTPReadLine(xmlNanoHTTPCtxtPtr ctxt) {
654 char buf[4096];
655 char *bp = buf;
Daniel Veillardf012a642001-07-23 19:10:52 +0000656 int rc;
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800657
Owen Taylor3473f882001-02-23 17:55:21 +0000658 while (bp - buf < 4095) {
659 if (ctxt->inrptr == ctxt->inptr) {
Daniel Veillardf012a642001-07-23 19:10:52 +0000660 if ( (rc = xmlNanoHTTPRecv(ctxt)) == 0) {
Owen Taylor3473f882001-02-23 17:55:21 +0000661 if (bp == buf)
662 return(NULL);
663 else
664 *bp = 0;
665 return(xmlMemStrdup(buf));
666 }
Daniel Veillardf012a642001-07-23 19:10:52 +0000667 else if ( rc == -1 ) {
668 return ( NULL );
669 }
Owen Taylor3473f882001-02-23 17:55:21 +0000670 }
671 *bp = *ctxt->inrptr++;
672 if (*bp == '\n') {
673 *bp = 0;
674 return(xmlMemStrdup(buf));
675 }
676 if (*bp != '\r')
677 bp++;
678 }
679 buf[4095] = 0;
680 return(xmlMemStrdup(buf));
681}
682
683
684/**
685 * xmlNanoHTTPScanAnswer:
686 * @ctxt: an HTTP context
687 * @line: an HTTP header line
688 *
689 * Try to extract useful informations from the server answer.
690 * We currently parse and process:
691 * - The HTTP revision/ return code
Daniel Veillarda840b692003-10-19 13:35:37 +0000692 * - The Content-Type, Mime-Type and charset used
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000693 * - The Location for redirect processing.
Owen Taylor3473f882001-02-23 17:55:21 +0000694 *
695 * Returns -1 in case of failure, the file descriptor number otherwise
696 */
697
698static void
699xmlNanoHTTPScanAnswer(xmlNanoHTTPCtxtPtr ctxt, const char *line) {
700 const char *cur = line;
701
702 if (line == NULL) return;
703
704 if (!strncmp(line, "HTTP/", 5)) {
705 int version = 0;
706 int ret = 0;
707
708 cur += 5;
709 while ((*cur >= '0') && (*cur <= '9')) {
710 version *= 10;
711 version += *cur - '0';
712 cur++;
713 }
714 if (*cur == '.') {
715 cur++;
716 if ((*cur >= '0') && (*cur <= '9')) {
717 version *= 10;
718 version += *cur - '0';
719 cur++;
720 }
721 while ((*cur >= '0') && (*cur <= '9'))
722 cur++;
723 } else
724 version *= 10;
725 if ((*cur != ' ') && (*cur != '\t')) return;
726 while ((*cur == ' ') || (*cur == '\t')) cur++;
727 if ((*cur < '0') || (*cur > '9')) return;
728 while ((*cur >= '0') && (*cur <= '9')) {
729 ret *= 10;
730 ret += *cur - '0';
731 cur++;
732 }
733 if ((*cur != 0) && (*cur != ' ') && (*cur != '\t')) return;
734 ctxt->returnValue = ret;
Daniel Veillard13cee4e2009-09-05 14:52:55 +0200735 ctxt->version = version;
Owen Taylor3473f882001-02-23 17:55:21 +0000736 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Content-Type:", 13)) {
Daniel Veillarda840b692003-10-19 13:35:37 +0000737 const xmlChar *charset, *last, *mime;
Owen Taylor3473f882001-02-23 17:55:21 +0000738 cur += 13;
739 while ((*cur == ' ') || (*cur == '\t')) cur++;
740 if (ctxt->contentType != NULL)
741 xmlFree(ctxt->contentType);
742 ctxt->contentType = xmlMemStrdup(cur);
Daniel Veillarda840b692003-10-19 13:35:37 +0000743 mime = (const xmlChar *) cur;
744 last = mime;
745 while ((*last != 0) && (*last != ' ') && (*last != '\t') &&
746 (*last != ';') && (*last != ','))
747 last++;
748 if (ctxt->mimeType != NULL)
749 xmlFree(ctxt->mimeType);
750 ctxt->mimeType = (char *) xmlStrndup(mime, last - mime);
751 charset = xmlStrstr(BAD_CAST ctxt->contentType, BAD_CAST "charset=");
752 if (charset != NULL) {
753 charset += 8;
754 last = charset;
755 while ((*last != 0) && (*last != ' ') && (*last != '\t') &&
756 (*last != ';') && (*last != ','))
757 last++;
758 if (ctxt->encoding != NULL)
759 xmlFree(ctxt->encoding);
760 ctxt->encoding = (char *) xmlStrndup(charset, last - charset);
761 }
Owen Taylor3473f882001-02-23 17:55:21 +0000762 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"ContentType:", 12)) {
Daniel Veillarda840b692003-10-19 13:35:37 +0000763 const xmlChar *charset, *last, *mime;
Owen Taylor3473f882001-02-23 17:55:21 +0000764 cur += 12;
765 if (ctxt->contentType != NULL) return;
766 while ((*cur == ' ') || (*cur == '\t')) cur++;
767 ctxt->contentType = xmlMemStrdup(cur);
Daniel Veillarda840b692003-10-19 13:35:37 +0000768 mime = (const xmlChar *) cur;
769 last = mime;
770 while ((*last != 0) && (*last != ' ') && (*last != '\t') &&
771 (*last != ';') && (*last != ','))
772 last++;
773 if (ctxt->mimeType != NULL)
774 xmlFree(ctxt->mimeType);
775 ctxt->mimeType = (char *) xmlStrndup(mime, last - mime);
776 charset = xmlStrstr(BAD_CAST ctxt->contentType, BAD_CAST "charset=");
777 if (charset != NULL) {
778 charset += 8;
779 last = charset;
780 while ((*last != 0) && (*last != ' ') && (*last != '\t') &&
781 (*last != ';') && (*last != ','))
782 last++;
783 if (ctxt->encoding != NULL)
784 xmlFree(ctxt->encoding);
785 ctxt->encoding = (char *) xmlStrndup(charset, last - charset);
786 }
Owen Taylor3473f882001-02-23 17:55:21 +0000787 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Location:", 9)) {
788 cur += 9;
789 while ((*cur == ' ') || (*cur == '\t')) cur++;
790 if (ctxt->location != NULL)
791 xmlFree(ctxt->location);
William M. Brack7e29c0a2004-04-02 09:07:22 +0000792 if (*cur == '/') {
793 xmlChar *tmp_http = xmlStrdup(BAD_CAST "http://");
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800794 xmlChar *tmp_loc =
William M. Brack7e29c0a2004-04-02 09:07:22 +0000795 xmlStrcat(tmp_http, (const xmlChar *) ctxt->hostname);
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800796 ctxt->location =
William M. Brack7e29c0a2004-04-02 09:07:22 +0000797 (char *) xmlStrcat (tmp_loc, (const xmlChar *) cur);
798 } else {
799 ctxt->location = xmlMemStrdup(cur);
800 }
Owen Taylor3473f882001-02-23 17:55:21 +0000801 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"WWW-Authenticate:", 17)) {
802 cur += 17;
803 while ((*cur == ' ') || (*cur == '\t')) cur++;
804 if (ctxt->authHeader != NULL)
805 xmlFree(ctxt->authHeader);
806 ctxt->authHeader = xmlMemStrdup(cur);
807 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Proxy-Authenticate:", 19)) {
808 cur += 19;
809 while ((*cur == ' ') || (*cur == '\t')) cur++;
810 if (ctxt->authHeader != NULL)
811 xmlFree(ctxt->authHeader);
812 ctxt->authHeader = xmlMemStrdup(cur);
Daniel Veillard9a2724d2005-12-15 11:12:26 +0000813#ifdef HAVE_ZLIB_H
814 } else if ( !xmlStrncasecmp( BAD_CAST line, BAD_CAST"Content-Encoding:", 17) ) {
815 cur += 17;
816 while ((*cur == ' ') || (*cur == '\t')) cur++;
817 if ( !xmlStrncasecmp( BAD_CAST cur, BAD_CAST"gzip", 4) ) {
818 ctxt->usesGzip = 1;
819
820 ctxt->strm = xmlMalloc(sizeof(z_stream));
821
822 if (ctxt->strm != NULL) {
823 ctxt->strm->zalloc = Z_NULL;
824 ctxt->strm->zfree = Z_NULL;
825 ctxt->strm->opaque = Z_NULL;
826 ctxt->strm->avail_in = 0;
827 ctxt->strm->next_in = Z_NULL;
828
829 inflateInit2( ctxt->strm, 31 );
830 }
831 }
832#endif
Daniel Veillardf012a642001-07-23 19:10:52 +0000833 } else if ( !xmlStrncasecmp( BAD_CAST line, BAD_CAST"Content-Length:", 15) ) {
834 cur += 15;
835 ctxt->ContentLength = strtol( cur, NULL, 10 );
Owen Taylor3473f882001-02-23 17:55:21 +0000836 }
837}
838
839/**
840 * xmlNanoHTTPConnectAttempt:
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000841 * @addr: a socket address structure
Owen Taylor3473f882001-02-23 17:55:21 +0000842 *
843 * Attempt a connection to the given IP:port endpoint. It forces
844 * non-blocking semantic on the socket, and allow 60 seconds for
845 * the host to answer.
846 *
847 * Returns -1 in case of failure, the file descriptor number otherwise
848 */
849
Ozkan Sezerf99d2222010-11-04 12:08:08 +0100850static SOCKET
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000851xmlNanoHTTPConnectAttempt(struct sockaddr *addr)
Owen Taylor3473f882001-02-23 17:55:21 +0000852{
Raphael Prevost48b60c32009-08-23 13:11:01 +0200853#ifndef HAVE_POLL_H
Owen Taylor3473f882001-02-23 17:55:21 +0000854 fd_set wfd;
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000855#ifdef _WINSOCKAPI_
856 fd_set xfd;
857#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000858 struct timeval tv;
Raphael Prevost48b60c32009-08-23 13:11:01 +0200859#else /* !HAVE_POLL_H */
860 struct pollfd p;
861#endif /* !HAVE_POLL_H */
Owen Taylor3473f882001-02-23 17:55:21 +0000862 int status;
Raphael Prevost48b60c32009-08-23 13:11:01 +0200863
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000864 int addrlen;
Raphael Prevost48b60c32009-08-23 13:11:01 +0200865
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000866 SOCKET s;
Raphael Prevost48b60c32009-08-23 13:11:01 +0200867
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000868#ifdef SUPPORT_IP6
869 if (addr->sa_family == AF_INET6) {
Raphael Prevost48b60c32009-08-23 13:11:01 +0200870 s = socket(PF_INET6, SOCK_STREAM, IPPROTO_TCP);
871 addrlen = sizeof(struct sockaddr_in6);
872 } else
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000873#endif
874 {
Raphael Prevost48b60c32009-08-23 13:11:01 +0200875 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
876 addrlen = sizeof(struct sockaddr_in);
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000877 }
Ozkan Sezerf99d2222010-11-04 12:08:08 +0100878 if (s == INVALID_SOCKET) {
Owen Taylor3473f882001-02-23 17:55:21 +0000879#ifdef DEBUG_HTTP
Raphael Prevost48b60c32009-08-23 13:11:01 +0200880 perror("socket");
Owen Taylor3473f882001-02-23 17:55:21 +0000881#endif
Raphael Prevost48b60c32009-08-23 13:11:01 +0200882 __xmlIOErr(XML_FROM_HTTP, 0, "socket failed\n");
Ozkan Sezerf99d2222010-11-04 12:08:08 +0100883 return INVALID_SOCKET;
Owen Taylor3473f882001-02-23 17:55:21 +0000884 }
Owen Taylor3473f882001-02-23 17:55:21 +0000885#ifdef _WINSOCKAPI_
886 {
Raphael Prevost48b60c32009-08-23 13:11:01 +0200887 u_long one = 1;
Owen Taylor3473f882001-02-23 17:55:21 +0000888
Raphael Prevost48b60c32009-08-23 13:11:01 +0200889 status = ioctlsocket(s, FIONBIO, &one) == SOCKET_ERROR ? -1 : 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000890 }
891#else /* _WINSOCKAPI_ */
892#if defined(VMS)
893 {
Raphael Prevost48b60c32009-08-23 13:11:01 +0200894 int enable = 1;
895
896 status = ioctl(s, FIONBIO, &enable);
Owen Taylor3473f882001-02-23 17:55:21 +0000897 }
898#else /* VMS */
Daniel Veillardcba68392008-08-29 12:43:40 +0000899#if defined(__BEOS__) && !defined(__HAIKU__)
Raphael Prevost48b60c32009-08-23 13:11:01 +0200900 {
901 bool noblock = true;
902
903 status =
904 setsockopt(s, SOL_SOCKET, SO_NONBLOCK, &noblock,
905 sizeof(noblock));
906 }
Daniel Veillard254b1262003-11-01 17:04:58 +0000907#else /* __BEOS__ */
Owen Taylor3473f882001-02-23 17:55:21 +0000908 if ((status = fcntl(s, F_GETFL, 0)) != -1) {
909#ifdef O_NONBLOCK
Raphael Prevost48b60c32009-08-23 13:11:01 +0200910 status |= O_NONBLOCK;
Owen Taylor3473f882001-02-23 17:55:21 +0000911#else /* O_NONBLOCK */
912#ifdef F_NDELAY
Raphael Prevost48b60c32009-08-23 13:11:01 +0200913 status |= F_NDELAY;
Owen Taylor3473f882001-02-23 17:55:21 +0000914#endif /* F_NDELAY */
915#endif /* !O_NONBLOCK */
Raphael Prevost48b60c32009-08-23 13:11:01 +0200916 status = fcntl(s, F_SETFL, status);
Owen Taylor3473f882001-02-23 17:55:21 +0000917 }
918 if (status < 0) {
919#ifdef DEBUG_HTTP
Raphael Prevost48b60c32009-08-23 13:11:01 +0200920 perror("nonblocking");
Owen Taylor3473f882001-02-23 17:55:21 +0000921#endif
Raphael Prevost48b60c32009-08-23 13:11:01 +0200922 __xmlIOErr(XML_FROM_HTTP, 0, "error setting non-blocking IO\n");
923 closesocket(s);
Ozkan Sezerf99d2222010-11-04 12:08:08 +0100924 return INVALID_SOCKET;
Owen Taylor3473f882001-02-23 17:55:21 +0000925 }
Daniel Veillard254b1262003-11-01 17:04:58 +0000926#endif /* !__BEOS__ */
Owen Taylor3473f882001-02-23 17:55:21 +0000927#endif /* !VMS */
928#endif /* !_WINSOCKAPI_ */
929
Raphael Prevost48b60c32009-08-23 13:11:01 +0200930 if (connect(s, addr, addrlen) == -1) {
931 switch (socket_errno()) {
932 case EINPROGRESS:
933 case EWOULDBLOCK:
934 break;
935 default:
936 __xmlIOErr(XML_FROM_HTTP, 0,
937 "error connecting to HTTP server");
938 closesocket(s);
Ozkan Sezerf99d2222010-11-04 12:08:08 +0100939 return INVALID_SOCKET;
Raphael Prevost48b60c32009-08-23 13:11:01 +0200940 }
941 }
942#ifndef HAVE_POLL_H
Owen Taylor3473f882001-02-23 17:55:21 +0000943 tv.tv_sec = timeout;
944 tv.tv_usec = 0;
Daniel Veillard9e2110b2005-08-08 20:33:54 +0000945
946#ifdef _MSC_VER
947#pragma warning(push)
948#pragma warning(disable: 4018)
949#endif
spadixd29a5c82009-10-19 14:03:25 +0200950#ifndef _WINSOCKAPI_
Raphael Prevost48b60c32009-08-23 13:11:01 +0200951 if (s > FD_SETSIZE)
Ozkan Sezerf99d2222010-11-04 12:08:08 +0100952 return INVALID_SOCKET;
spadixd29a5c82009-10-19 14:03:25 +0200953#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000954 FD_ZERO(&wfd);
955 FD_SET(s, &wfd);
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000956
Raphael Prevost48b60c32009-08-23 13:11:01 +0200957#ifdef _WINSOCKAPI_
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000958 FD_ZERO(&xfd);
959 FD_SET(s, &xfd);
Raphael Prevost48b60c32009-08-23 13:11:01 +0200960
961 switch (select(s + 1, NULL, &wfd, &xfd, &tv))
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000962#else
Raphael Prevost48b60c32009-08-23 13:11:01 +0200963 switch (select(s + 1, NULL, &wfd, NULL, &tv))
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000964#endif
Daniel Veillard9e2110b2005-08-08 20:33:54 +0000965#ifdef _MSC_VER
966#pragma warning(pop)
967#endif
Raphael Prevost48b60c32009-08-23 13:11:01 +0200968
969#else /* !HAVE_POLL_H */
970 p.fd = s;
971 p.events = POLLOUT;
972 switch (poll(&p, 1, timeout * 1000))
973#endif /* !HAVE_POLL_H */
974
Owen Taylor3473f882001-02-23 17:55:21 +0000975 {
Raphael Prevost48b60c32009-08-23 13:11:01 +0200976 case 0:
977 /* Time out */
978 __xmlIOErr(XML_FROM_HTTP, 0, "Connect attempt timed out");
979 closesocket(s);
Ozkan Sezerf99d2222010-11-04 12:08:08 +0100980 return INVALID_SOCKET;
Raphael Prevost48b60c32009-08-23 13:11:01 +0200981 case -1:
982 /* Ermm.. ?? */
983 __xmlIOErr(XML_FROM_HTTP, 0, "Connect failed");
984 closesocket(s);
Ozkan Sezerf99d2222010-11-04 12:08:08 +0100985 return INVALID_SOCKET;
Owen Taylor3473f882001-02-23 17:55:21 +0000986 }
987
Raphael Prevost48b60c32009-08-23 13:11:01 +0200988#ifndef HAVE_POLL_H
989 if (FD_ISSET(s, &wfd)
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000990#ifdef _WINSOCKAPI_
Raphael Prevost48b60c32009-08-23 13:11:01 +0200991 || FD_ISSET(s, &xfd)
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000992#endif
Raphael Prevost48b60c32009-08-23 13:11:01 +0200993 )
994#else /* !HAVE_POLL_H */
995 if (p.revents == POLLOUT)
996#endif /* !HAVE_POLL_H */
997 {
998 XML_SOCKLEN_T len;
999
1000 len = sizeof(status);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001001#ifdef SO_ERROR
Raphael Prevost48b60c32009-08-23 13:11:01 +02001002 if (getsockopt(s, SOL_SOCKET, SO_ERROR, (char *) &status, &len) <
1003 0) {
1004 /* Solaris error code */
1005 __xmlIOErr(XML_FROM_HTTP, 0, "getsockopt failed\n");
Ozkan Sezerf99d2222010-11-04 12:08:08 +01001006 return INVALID_SOCKET;
Raphael Prevost48b60c32009-08-23 13:11:01 +02001007 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001008#endif
Raphael Prevost48b60c32009-08-23 13:11:01 +02001009 if (status) {
1010 __xmlIOErr(XML_FROM_HTTP, 0,
1011 "Error connecting to remote host");
1012 closesocket(s);
1013 errno = status;
Ozkan Sezerf99d2222010-11-04 12:08:08 +01001014 return INVALID_SOCKET;
Raphael Prevost48b60c32009-08-23 13:11:01 +02001015 }
Owen Taylor3473f882001-02-23 17:55:21 +00001016 } else {
Raphael Prevost48b60c32009-08-23 13:11:01 +02001017 /* pbm */
1018 __xmlIOErr(XML_FROM_HTTP, 0, "select failed\n");
1019 closesocket(s);
Ozkan Sezerf99d2222010-11-04 12:08:08 +01001020 return INVALID_SOCKET;
Owen Taylor3473f882001-02-23 17:55:21 +00001021 }
Raphael Prevost48b60c32009-08-23 13:11:01 +02001022
1023 return (s);
Owen Taylor3473f882001-02-23 17:55:21 +00001024}
Raphael Prevost48b60c32009-08-23 13:11:01 +02001025
Owen Taylor3473f882001-02-23 17:55:21 +00001026/**
1027 * xmlNanoHTTPConnectHost:
1028 * @host: the host name
1029 * @port: the port number
1030 *
1031 * Attempt a connection to the given host:port endpoint. It tries
1032 * the multiple IP provided by the DNS if available.
1033 *
1034 * Returns -1 in case of failure, the file descriptor number otherwise
1035 */
1036
Ozkan Sezerf99d2222010-11-04 12:08:08 +01001037static SOCKET
Owen Taylor3473f882001-02-23 17:55:21 +00001038xmlNanoHTTPConnectHost(const char *host, int port)
1039{
1040 struct hostent *h;
Daniel Veillard2db8c122003-07-08 12:16:59 +00001041 struct sockaddr *addr = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001042 struct in_addr ia;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001043 struct sockaddr_in sockin;
Daniel Veillard5c396542002-03-15 07:57:50 +00001044
Owen Taylor3473f882001-02-23 17:55:21 +00001045#ifdef SUPPORT_IP6
1046 struct in6_addr ia6;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001047 struct sockaddr_in6 sockin6;
Owen Taylor3473f882001-02-23 17:55:21 +00001048#endif
1049 int i;
Ozkan Sezerf99d2222010-11-04 12:08:08 +01001050 SOCKET s;
Daniel Veillard5c396542002-03-15 07:57:50 +00001051
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001052 memset (&sockin, 0, sizeof(sockin));
1053#ifdef SUPPORT_IP6
1054 memset (&sockin6, 0, sizeof(sockin6));
Rob Richardscb418de2005-10-13 23:12:42 +00001055#endif
1056
1057#if !defined(HAVE_GETADDRINFO) && defined(SUPPORT_IP6) && defined(RES_USE_INET6)
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001058 if (have_ipv6 ())
Daniel Veillard560c2a42003-07-06 21:13:49 +00001059 {
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001060 if (!(_res.options & RES_INIT))
1061 res_init();
1062 _res.options |= RES_USE_INET6;
1063 }
Rob Richardscb418de2005-10-13 23:12:42 +00001064#endif
1065
1066#if defined(HAVE_GETADDRINFO) && defined(SUPPORT_IP6) && !defined(_WIN32)
1067 if (have_ipv6 ())
1068#endif
1069#if defined(HAVE_GETADDRINFO) && (defined(SUPPORT_IP6) || defined(_WIN32))
Daniel Veillard560c2a42003-07-06 21:13:49 +00001070 {
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001071 int status;
1072 struct addrinfo hints, *res, *result;
1073
1074 result = NULL;
1075 memset (&hints, 0,sizeof(hints));
1076 hints.ai_socktype = SOCK_STREAM;
1077
1078 status = getaddrinfo (host, NULL, &hints, &result);
1079 if (status) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001080 __xmlIOErr(XML_FROM_HTTP, 0, "getaddrinfo failed\n");
Ozkan Sezerf99d2222010-11-04 12:08:08 +01001081 return INVALID_SOCKET;
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001082 }
1083
1084 for (res = result; res; res = res->ai_next) {
Rob Richardscb418de2005-10-13 23:12:42 +00001085 if (res->ai_family == AF_INET) {
1086 if (res->ai_addrlen > sizeof(sockin)) {
1087 __xmlIOErr(XML_FROM_HTTP, 0, "address size mismatch\n");
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001088 freeaddrinfo (result);
Ozkan Sezerf99d2222010-11-04 12:08:08 +01001089 return INVALID_SOCKET;
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001090 }
Rob Richardscb418de2005-10-13 23:12:42 +00001091 memcpy (&sockin, res->ai_addr, res->ai_addrlen);
1092 sockin.sin_port = htons (port);
1093 addr = (struct sockaddr *)&sockin;
1094#ifdef SUPPORT_IP6
1095 } else if (have_ipv6 () && (res->ai_family == AF_INET6)) {
1096 if (res->ai_addrlen > sizeof(sockin6)) {
1097 __xmlIOErr(XML_FROM_HTTP, 0, "address size mismatch\n");
1098 freeaddrinfo (result);
Ozkan Sezerf99d2222010-11-04 12:08:08 +01001099 return INVALID_SOCKET;
Rob Richardscb418de2005-10-13 23:12:42 +00001100 }
1101 memcpy (&sockin6, res->ai_addr, res->ai_addrlen);
1102 sockin6.sin6_port = htons (port);
1103 addr = (struct sockaddr *)&sockin6;
1104#endif
1105 } else
1106 continue; /* for */
1107
1108 s = xmlNanoHTTPConnectAttempt (addr);
Ozkan Sezerf99d2222010-11-04 12:08:08 +01001109 if (s != INVALID_SOCKET) {
Rob Richardscb418de2005-10-13 23:12:42 +00001110 freeaddrinfo (result);
1111 return (s);
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001112 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001113 }
Rob Richardscb418de2005-10-13 23:12:42 +00001114
Daniel Veillard3dc93a42003-07-10 14:04:33 +00001115 if (result)
1116 freeaddrinfo (result);
Rob Richardscb418de2005-10-13 23:12:42 +00001117 }
Owen Taylor3473f882001-02-23 17:55:21 +00001118#endif
Rob Richardscb418de2005-10-13 23:12:42 +00001119#if defined(HAVE_GETADDRINFO) && defined(SUPPORT_IP6) && !defined(_WIN32)
1120 else
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001121#endif
Rob Richardscb418de2005-10-13 23:12:42 +00001122#if !defined(HAVE_GETADDRINFO) || !defined(_WIN32)
1123 {
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001124 h = gethostbyname (host);
1125 if (h == NULL) {
Daniel Veillard56b2db72002-03-25 16:35:28 +00001126
1127/*
1128 * Okay, I got fed up by the non-portability of this error message
1129 * extraction code. it work on Linux, if it work on your platform
1130 * and one want to enable it, send me the defined(foobar) needed
1131 */
1132#if defined(HAVE_NETDB_H) && defined(HOST_NOT_FOUND) && defined(linux)
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001133 const char *h_err_txt = "";
Daniel Veillardf012a642001-07-23 19:10:52 +00001134
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001135 switch (h_errno) {
1136 case HOST_NOT_FOUND:
1137 h_err_txt = "Authoritive host not found";
1138 break;
Daniel Veillardf012a642001-07-23 19:10:52 +00001139
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001140 case TRY_AGAIN:
1141 h_err_txt =
1142 "Non-authoritive host not found or server failure.";
1143 break;
Daniel Veillardf012a642001-07-23 19:10:52 +00001144
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001145 case NO_RECOVERY:
1146 h_err_txt =
1147 "Non-recoverable errors: FORMERR, REFUSED, or NOTIMP.";
1148 break;
Daniel Veillard5c396542002-03-15 07:57:50 +00001149
Daniel Veillardd95b6892012-04-02 17:48:53 +08001150#ifdef NO_ADDRESS
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001151 case NO_ADDRESS:
1152 h_err_txt =
1153 "Valid name, no data record of requested type.";
1154 break;
Daniel Veillardd95b6892012-04-02 17:48:53 +08001155#endif
Daniel Veillard5c396542002-03-15 07:57:50 +00001156
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001157 default:
1158 h_err_txt = "No error text defined.";
1159 break;
1160 }
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001161 __xmlIOErr(XML_FROM_HTTP, 0, h_err_txt);
Daniel Veillard5c396542002-03-15 07:57:50 +00001162#else
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001163 __xmlIOErr(XML_FROM_HTTP, 0, "Failed to resolve host");
Owen Taylor3473f882001-02-23 17:55:21 +00001164#endif
Ozkan Sezerf99d2222010-11-04 12:08:08 +01001165 return INVALID_SOCKET;
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001166 }
Daniel Veillard5c396542002-03-15 07:57:50 +00001167
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001168 for (i = 0; h->h_addr_list[i]; i++) {
1169 if (h->h_addrtype == AF_INET) {
1170 /* A records (IPv4) */
Daniel Veillard8e2c9792004-10-27 09:39:50 +00001171 if ((unsigned int) h->h_length > sizeof(ia)) {
1172 __xmlIOErr(XML_FROM_HTTP, 0, "address size mismatch\n");
Ozkan Sezerf99d2222010-11-04 12:08:08 +01001173 return INVALID_SOCKET;
Daniel Veillard8e2c9792004-10-27 09:39:50 +00001174 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001175 memcpy (&ia, h->h_addr_list[i], h->h_length);
1176 sockin.sin_family = h->h_addrtype;
1177 sockin.sin_addr = ia;
Daniel Veillardac17e592012-04-02 15:45:13 +08001178 sockin.sin_port = (unsigned short)htons ((unsigned short)port);
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001179 addr = (struct sockaddr *) &sockin;
Daniel Veillard5c396542002-03-15 07:57:50 +00001180#ifdef SUPPORT_IP6
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001181 } else if (have_ipv6 () && (h->h_addrtype == AF_INET6)) {
1182 /* AAAA records (IPv6) */
Daniel Veillard8e2c9792004-10-27 09:39:50 +00001183 if ((unsigned int) h->h_length > sizeof(ia6)) {
1184 __xmlIOErr(XML_FROM_HTTP, 0, "address size mismatch\n");
Ozkan Sezerf99d2222010-11-04 12:08:08 +01001185 return INVALID_SOCKET;
Daniel Veillard8e2c9792004-10-27 09:39:50 +00001186 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001187 memcpy (&ia6, h->h_addr_list[i], h->h_length);
1188 sockin6.sin6_family = h->h_addrtype;
1189 sockin6.sin6_addr = ia6;
1190 sockin6.sin6_port = htons (port);
1191 addr = (struct sockaddr *) &sockin6;
Daniel Veillard5c396542002-03-15 07:57:50 +00001192#endif
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001193 } else
1194 break; /* for */
Daniel Veillard5c396542002-03-15 07:57:50 +00001195
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001196 s = xmlNanoHTTPConnectAttempt (addr);
Ozkan Sezerf99d2222010-11-04 12:08:08 +01001197 if (s != INVALID_SOCKET)
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001198 return (s);
1199 }
Owen Taylor3473f882001-02-23 17:55:21 +00001200 }
Rob Richardscb418de2005-10-13 23:12:42 +00001201#endif
1202
Owen Taylor3473f882001-02-23 17:55:21 +00001203#ifdef DEBUG_HTTP
1204 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard5c396542002-03-15 07:57:50 +00001205 "xmlNanoHTTPConnectHost: unable to connect to '%s'.\n",
1206 host);
Owen Taylor3473f882001-02-23 17:55:21 +00001207#endif
Ozkan Sezerf99d2222010-11-04 12:08:08 +01001208 return INVALID_SOCKET;
Owen Taylor3473f882001-02-23 17:55:21 +00001209}
1210
1211
1212/**
1213 * xmlNanoHTTPOpen:
1214 * @URL: The URL to load
1215 * @contentType: if available the Content-Type information will be
1216 * returned at that location
1217 *
1218 * This function try to open a connection to the indicated resource
1219 * via HTTP GET.
1220 *
1221 * Returns NULL in case of failure, otherwise a request handler.
1222 * The contentType, if provided must be freed by the caller
1223 */
1224
1225void*
1226xmlNanoHTTPOpen(const char *URL, char **contentType) {
1227 if (contentType != NULL) *contentType = NULL;
Daniel Veillardf012a642001-07-23 19:10:52 +00001228 return(xmlNanoHTTPMethod(URL, NULL, NULL, contentType, NULL, 0));
Daniel Veillard9403a042001-05-28 11:00:53 +00001229}
1230
1231/**
1232 * xmlNanoHTTPOpenRedir:
1233 * @URL: The URL to load
1234 * @contentType: if available the Content-Type information will be
1235 * returned at that location
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001236 * @redir: if available the redirected URL will be returned
Daniel Veillard9403a042001-05-28 11:00:53 +00001237 *
1238 * This function try to open a connection to the indicated resource
1239 * via HTTP GET.
1240 *
1241 * Returns NULL in case of failure, otherwise a request handler.
1242 * The contentType, if provided must be freed by the caller
1243 */
1244
1245void*
1246xmlNanoHTTPOpenRedir(const char *URL, char **contentType, char **redir) {
1247 if (contentType != NULL) *contentType = NULL;
1248 if (redir != NULL) *redir = NULL;
Daniel Veillardf012a642001-07-23 19:10:52 +00001249 return(xmlNanoHTTPMethodRedir(URL, NULL, NULL, contentType, redir, NULL,0));
Owen Taylor3473f882001-02-23 17:55:21 +00001250}
1251
1252/**
1253 * xmlNanoHTTPRead:
1254 * @ctx: the HTTP context
1255 * @dest: a buffer
1256 * @len: the buffer length
1257 *
1258 * This function tries to read @len bytes from the existing HTTP connection
1259 * and saves them in @dest. This is a blocking call.
1260 *
1261 * Returns the number of byte read. 0 is an indication of an end of connection.
1262 * -1 indicates a parameter error.
1263 */
1264int
1265xmlNanoHTTPRead(void *ctx, void *dest, int len) {
1266 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
Daniel Veillard9a2724d2005-12-15 11:12:26 +00001267#ifdef HAVE_ZLIB_H
1268 int bytes_read = 0;
1269 int orig_avail_in;
1270 int z_ret;
1271#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001272
1273 if (ctx == NULL) return(-1);
1274 if (dest == NULL) return(-1);
1275 if (len <= 0) return(0);
1276
Daniel Veillard9a2724d2005-12-15 11:12:26 +00001277#ifdef HAVE_ZLIB_H
1278 if (ctxt->usesGzip == 1) {
1279 if (ctxt->strm == NULL) return(0);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001280
Daniel Veillard9a2724d2005-12-15 11:12:26 +00001281 ctxt->strm->next_out = dest;
1282 ctxt->strm->avail_out = len;
William M. Bracke8827652007-05-16 05:19:13 +00001283 ctxt->strm->avail_in = ctxt->inptr - ctxt->inrptr;
Daniel Veillard9a2724d2005-12-15 11:12:26 +00001284
William M. Bracke8827652007-05-16 05:19:13 +00001285 while (ctxt->strm->avail_out > 0 &&
1286 (ctxt->strm->avail_in > 0 || xmlNanoHTTPRecv(ctxt) > 0)) {
William M. Brackd2f682a2007-05-15 19:42:08 +00001287 orig_avail_in = ctxt->strm->avail_in =
1288 ctxt->inptr - ctxt->inrptr - bytes_read;
Daniel Veillard9a2724d2005-12-15 11:12:26 +00001289 ctxt->strm->next_in = BAD_CAST (ctxt->inrptr + bytes_read);
1290
1291 z_ret = inflate(ctxt->strm, Z_NO_FLUSH);
1292 bytes_read += orig_avail_in - ctxt->strm->avail_in;
1293
1294 if (z_ret != Z_OK) break;
William M. Brackd2f682a2007-05-15 19:42:08 +00001295 }
Daniel Veillard9a2724d2005-12-15 11:12:26 +00001296
1297 ctxt->inrptr += bytes_read;
1298 return(len - ctxt->strm->avail_out);
1299 }
1300#endif
1301
Owen Taylor3473f882001-02-23 17:55:21 +00001302 while (ctxt->inptr - ctxt->inrptr < len) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001303 if (xmlNanoHTTPRecv(ctxt) <= 0) break;
Owen Taylor3473f882001-02-23 17:55:21 +00001304 }
1305 if (ctxt->inptr - ctxt->inrptr < len)
1306 len = ctxt->inptr - ctxt->inrptr;
1307 memcpy(dest, ctxt->inrptr, len);
1308 ctxt->inrptr += len;
1309 return(len);
1310}
1311
1312/**
1313 * xmlNanoHTTPClose:
1314 * @ctx: the HTTP context
1315 *
1316 * This function closes an HTTP context, it ends up the connection and
1317 * free all data related to it.
1318 */
1319void
1320xmlNanoHTTPClose(void *ctx) {
1321 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1322
1323 if (ctx == NULL) return;
1324
1325 xmlNanoHTTPFreeCtxt(ctxt);
1326}
1327
1328/**
Daniel Veillard9403a042001-05-28 11:00:53 +00001329 * xmlNanoHTTPMethodRedir:
Owen Taylor3473f882001-02-23 17:55:21 +00001330 * @URL: The URL to load
1331 * @method: the HTTP method to use
1332 * @input: the input string if any
1333 * @contentType: the Content-Type information IN and OUT
Daniel Veillard9403a042001-05-28 11:00:53 +00001334 * @redir: the redirected URL OUT
Owen Taylor3473f882001-02-23 17:55:21 +00001335 * @headers: the extra headers
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001336 * @ilen: input length
Owen Taylor3473f882001-02-23 17:55:21 +00001337 *
1338 * This function try to open a connection to the indicated resource
1339 * via HTTP using the given @method, adding the given extra headers
1340 * and the input buffer for the request content.
1341 *
1342 * Returns NULL in case of failure, otherwise a request handler.
Daniel Veillard9403a042001-05-28 11:00:53 +00001343 * The contentType, or redir, if provided must be freed by the caller
Owen Taylor3473f882001-02-23 17:55:21 +00001344 */
1345
1346void*
Daniel Veillard9403a042001-05-28 11:00:53 +00001347xmlNanoHTTPMethodRedir(const char *URL, const char *method, const char *input,
Daniel Veillardf012a642001-07-23 19:10:52 +00001348 char **contentType, char **redir,
1349 const char *headers, int ilen ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001350 xmlNanoHTTPCtxtPtr ctxt;
1351 char *bp, *p;
Ozkan Sezerf99d2222010-11-04 12:08:08 +01001352 int blen;
1353 SOCKET ret;
Owen Taylor3473f882001-02-23 17:55:21 +00001354 int nbRedirects = 0;
1355 char *redirURL = NULL;
William M. Brack78637da2003-07-31 14:47:38 +00001356#ifdef DEBUG_HTTP
1357 int xmt_bytes;
1358#endif
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001359
Owen Taylor3473f882001-02-23 17:55:21 +00001360 if (URL == NULL) return(NULL);
1361 if (method == NULL) method = "GET";
1362 xmlNanoHTTPInit();
1363
1364retry:
1365 if (redirURL == NULL)
1366 ctxt = xmlNanoHTTPNewCtxt(URL);
1367 else {
1368 ctxt = xmlNanoHTTPNewCtxt(redirURL);
Daniel Veillarda840b692003-10-19 13:35:37 +00001369 ctxt->location = xmlMemStrdup(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001370 }
1371
Daniel Veillardf012a642001-07-23 19:10:52 +00001372 if ( ctxt == NULL ) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001373 return ( NULL );
1374 }
1375
Owen Taylor3473f882001-02-23 17:55:21 +00001376 if ((ctxt->protocol == NULL) || (strcmp(ctxt->protocol, "http"))) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001377 __xmlIOErr(XML_FROM_HTTP, XML_HTTP_URL_SYNTAX, "Not a valid HTTP URI");
Owen Taylor3473f882001-02-23 17:55:21 +00001378 xmlNanoHTTPFreeCtxt(ctxt);
1379 if (redirURL != NULL) xmlFree(redirURL);
1380 return(NULL);
1381 }
1382 if (ctxt->hostname == NULL) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001383 __xmlIOErr(XML_FROM_HTTP, XML_HTTP_UNKNOWN_HOST,
1384 "Failed to identify host in URI");
Owen Taylor3473f882001-02-23 17:55:21 +00001385 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001386 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001387 return(NULL);
1388 }
1389 if (proxy) {
1390 blen = strlen(ctxt->hostname) * 2 + 16;
1391 ret = xmlNanoHTTPConnectHost(proxy, proxyPort);
1392 }
1393 else {
1394 blen = strlen(ctxt->hostname);
1395 ret = xmlNanoHTTPConnectHost(ctxt->hostname, ctxt->port);
1396 }
Ozkan Sezerf99d2222010-11-04 12:08:08 +01001397 if (ret == INVALID_SOCKET) {
Owen Taylor3473f882001-02-23 17:55:21 +00001398 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001399 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001400 return(NULL);
1401 }
1402 ctxt->fd = ret;
1403
Daniel Veillardf012a642001-07-23 19:10:52 +00001404 if (input == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00001405 ilen = 0;
Daniel Veillardf012a642001-07-23 19:10:52 +00001406 else
1407 blen += 36;
1408
Owen Taylor3473f882001-02-23 17:55:21 +00001409 if (headers != NULL)
Daniel Veillardf012a642001-07-23 19:10:52 +00001410 blen += strlen(headers) + 2;
Owen Taylor3473f882001-02-23 17:55:21 +00001411 if (contentType && *contentType)
William M. Brackead35832008-02-06 04:12:46 +00001412 /* reserve for string plus 'Content-Type: \r\n" */
Owen Taylor3473f882001-02-23 17:55:21 +00001413 blen += strlen(*contentType) + 16;
Daniel Veillard351f2d62005-04-13 02:55:12 +00001414 if (ctxt->query != NULL)
William M. Brackead35832008-02-06 04:12:46 +00001415 /* 1 for '?' */
Daniel Veillard351f2d62005-04-13 02:55:12 +00001416 blen += strlen(ctxt->query) + 1;
Daniel Veillardf012a642001-07-23 19:10:52 +00001417 blen += strlen(method) + strlen(ctxt->path) + 24;
Daniel Veillard9a2724d2005-12-15 11:12:26 +00001418#ifdef HAVE_ZLIB_H
William M. Brackead35832008-02-06 04:12:46 +00001419 /* reserve for possible 'Accept-Encoding: gzip' string */
Daniel Veillard9a2724d2005-12-15 11:12:26 +00001420 blen += 23;
1421#endif
William M. Brackead35832008-02-06 04:12:46 +00001422 if (ctxt->port != 80) {
1423 /* reserve space for ':xxxxx', incl. potential proxy */
1424 if (proxy)
1425 blen += 12;
1426 else
1427 blen += 6;
1428 }
Daniel Veillard82cb3192003-10-29 13:39:15 +00001429 bp = (char*)xmlMallocAtomic(blen);
Daniel Veillardf012a642001-07-23 19:10:52 +00001430 if ( bp == NULL ) {
1431 xmlNanoHTTPFreeCtxt( ctxt );
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001432 xmlHTTPErrMemory("allocating header buffer");
Daniel Veillardf012a642001-07-23 19:10:52 +00001433 return ( NULL );
1434 }
1435
1436 p = bp;
1437
Owen Taylor3473f882001-02-23 17:55:21 +00001438 if (proxy) {
1439 if (ctxt->port != 80) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001440 p += snprintf( p, blen - (p - bp), "%s http://%s:%d%s",
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001441 method, ctxt->hostname,
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001442 ctxt->port, ctxt->path );
Owen Taylor3473f882001-02-23 17:55:21 +00001443 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001444 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001445 p += snprintf( p, blen - (p - bp), "%s http://%s%s", method,
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001446 ctxt->hostname, ctxt->path);
Owen Taylor3473f882001-02-23 17:55:21 +00001447 }
1448 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001449 p += snprintf( p, blen - (p - bp), "%s %s", method, ctxt->path);
Daniel Veillardf012a642001-07-23 19:10:52 +00001450
Daniel Veillard351f2d62005-04-13 02:55:12 +00001451 if (ctxt->query != NULL)
1452 p += snprintf( p, blen - (p - bp), "?%s", ctxt->query);
1453
William M. Brackec720082007-08-24 02:57:38 +00001454 if (ctxt->port == 80) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001455 p += snprintf( p, blen - (p - bp), " HTTP/1.0\r\nHost: %s\r\n",
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001456 ctxt->hostname);
William M. Brackec720082007-08-24 02:57:38 +00001457 } else {
1458 p += snprintf( p, blen - (p - bp), " HTTP/1.0\r\nHost: %s:%d\r\n",
1459 ctxt->hostname, ctxt->port);
1460 }
Daniel Veillardf012a642001-07-23 19:10:52 +00001461
Daniel Veillard9a2724d2005-12-15 11:12:26 +00001462#ifdef HAVE_ZLIB_H
1463 p += snprintf(p, blen - (p - bp), "Accept-Encoding: gzip\r\n");
1464#endif
1465
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001466 if (contentType != NULL && *contentType)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001467 p += snprintf(p, blen - (p - bp), "Content-Type: %s\r\n", *contentType);
Daniel Veillardf012a642001-07-23 19:10:52 +00001468
1469 if (headers != NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001470 p += snprintf( p, blen - (p - bp), "%s", headers );
Daniel Veillardf012a642001-07-23 19:10:52 +00001471
Owen Taylor3473f882001-02-23 17:55:21 +00001472 if (input != NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001473 snprintf(p, blen - (p - bp), "Content-Length: %d\r\n\r\n", ilen );
Owen Taylor3473f882001-02-23 17:55:21 +00001474 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001475 snprintf(p, blen - (p - bp), "\r\n");
Daniel Veillardf012a642001-07-23 19:10:52 +00001476
Owen Taylor3473f882001-02-23 17:55:21 +00001477#ifdef DEBUG_HTTP
1478 xmlGenericError(xmlGenericErrorContext,
1479 "-> %s%s", proxy? "(Proxy) " : "", bp);
1480 if ((blen -= strlen(bp)+1) < 0)
1481 xmlGenericError(xmlGenericErrorContext,
1482 "ERROR: overflowed buffer by %d bytes\n", -blen);
1483#endif
1484 ctxt->outptr = ctxt->out = bp;
1485 ctxt->state = XML_NANO_HTTP_WRITE;
Daniel Veillardf012a642001-07-23 19:10:52 +00001486 blen = strlen( ctxt->out );
Daniel Veillardf012a642001-07-23 19:10:52 +00001487#ifdef DEBUG_HTTP
William M. Brack78637da2003-07-31 14:47:38 +00001488 xmt_bytes = xmlNanoHTTPSend(ctxt, ctxt->out, blen );
Daniel Veillardf012a642001-07-23 19:10:52 +00001489 if ( xmt_bytes != blen )
1490 xmlGenericError( xmlGenericErrorContext,
1491 "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
1492 xmt_bytes, blen,
1493 "bytes of HTTP headers sent to host",
1494 ctxt->hostname );
William M. Brack78637da2003-07-31 14:47:38 +00001495#else
1496 xmlNanoHTTPSend(ctxt, ctxt->out, blen );
Daniel Veillardf012a642001-07-23 19:10:52 +00001497#endif
1498
1499 if ( input != NULL ) {
William M. Brack78637da2003-07-31 14:47:38 +00001500#ifdef DEBUG_HTTP
Daniel Veillardf012a642001-07-23 19:10:52 +00001501 xmt_bytes = xmlNanoHTTPSend( ctxt, input, ilen );
1502
Daniel Veillardf012a642001-07-23 19:10:52 +00001503 if ( xmt_bytes != ilen )
1504 xmlGenericError( xmlGenericErrorContext,
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001505 "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
Daniel Veillardf012a642001-07-23 19:10:52 +00001506 xmt_bytes, ilen,
1507 "bytes of HTTP content sent to host",
1508 ctxt->hostname );
William M. Brack78637da2003-07-31 14:47:38 +00001509#else
1510 xmlNanoHTTPSend( ctxt, input, ilen );
Daniel Veillardf012a642001-07-23 19:10:52 +00001511#endif
1512 }
1513
Owen Taylor3473f882001-02-23 17:55:21 +00001514 ctxt->state = XML_NANO_HTTP_READ;
Owen Taylor3473f882001-02-23 17:55:21 +00001515
1516 while ((p = xmlNanoHTTPReadLine(ctxt)) != NULL) {
Daniel Veillard594e5df2009-09-07 14:58:47 +02001517 if (*p == 0) {
Owen Taylor3473f882001-02-23 17:55:21 +00001518 ctxt->content = ctxt->inrptr;
1519 xmlFree(p);
1520 break;
1521 }
1522 xmlNanoHTTPScanAnswer(ctxt, p);
1523
1524#ifdef DEBUG_HTTP
1525 xmlGenericError(xmlGenericErrorContext, "<- %s\n", p);
1526#endif
1527 xmlFree(p);
1528 }
1529
1530 if ((ctxt->location != NULL) && (ctxt->returnValue >= 300) &&
1531 (ctxt->returnValue < 400)) {
1532#ifdef DEBUG_HTTP
1533 xmlGenericError(xmlGenericErrorContext,
1534 "\nRedirect to: %s\n", ctxt->location);
1535#endif
Daniel Veillardf012a642001-07-23 19:10:52 +00001536 while ( xmlNanoHTTPRecv(ctxt) > 0 ) ;
Owen Taylor3473f882001-02-23 17:55:21 +00001537 if (nbRedirects < XML_NANO_HTTP_MAX_REDIR) {
1538 nbRedirects++;
Daniel Veillard9403a042001-05-28 11:00:53 +00001539 if (redirURL != NULL)
1540 xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001541 redirURL = xmlMemStrdup(ctxt->location);
1542 xmlNanoHTTPFreeCtxt(ctxt);
1543 goto retry;
1544 }
1545 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001546 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001547#ifdef DEBUG_HTTP
1548 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardf012a642001-07-23 19:10:52 +00001549 "xmlNanoHTTPMethodRedir: Too many redirects, aborting ...\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001550#endif
1551 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001552 }
1553
1554 if (contentType != NULL) {
1555 if (ctxt->contentType != NULL)
1556 *contentType = xmlMemStrdup(ctxt->contentType);
1557 else
1558 *contentType = NULL;
1559 }
1560
Daniel Veillard9403a042001-05-28 11:00:53 +00001561 if ((redir != NULL) && (redirURL != NULL)) {
1562 *redir = redirURL;
1563 } else {
1564 if (redirURL != NULL)
1565 xmlFree(redirURL);
1566 if (redir != NULL)
1567 *redir = NULL;
1568 }
1569
Owen Taylor3473f882001-02-23 17:55:21 +00001570#ifdef DEBUG_HTTP
1571 if (ctxt->contentType != NULL)
1572 xmlGenericError(xmlGenericErrorContext,
1573 "\nCode %d, content-type '%s'\n\n",
1574 ctxt->returnValue, ctxt->contentType);
1575 else
1576 xmlGenericError(xmlGenericErrorContext,
1577 "\nCode %d, no content-type\n\n",
1578 ctxt->returnValue);
1579#endif
1580
1581 return((void *) ctxt);
1582}
1583
1584/**
Daniel Veillard9403a042001-05-28 11:00:53 +00001585 * xmlNanoHTTPMethod:
1586 * @URL: The URL to load
1587 * @method: the HTTP method to use
1588 * @input: the input string if any
1589 * @contentType: the Content-Type information IN and OUT
1590 * @headers: the extra headers
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001591 * @ilen: input length
Daniel Veillard9403a042001-05-28 11:00:53 +00001592 *
1593 * This function try to open a connection to the indicated resource
1594 * via HTTP using the given @method, adding the given extra headers
1595 * and the input buffer for the request content.
1596 *
1597 * Returns NULL in case of failure, otherwise a request handler.
1598 * The contentType, if provided must be freed by the caller
1599 */
1600
1601void*
1602xmlNanoHTTPMethod(const char *URL, const char *method, const char *input,
Daniel Veillardf012a642001-07-23 19:10:52 +00001603 char **contentType, const char *headers, int ilen) {
Daniel Veillard9403a042001-05-28 11:00:53 +00001604 return(xmlNanoHTTPMethodRedir(URL, method, input, contentType,
Daniel Veillardf012a642001-07-23 19:10:52 +00001605 NULL, headers, ilen));
Daniel Veillard9403a042001-05-28 11:00:53 +00001606}
1607
1608/**
Owen Taylor3473f882001-02-23 17:55:21 +00001609 * xmlNanoHTTPFetch:
1610 * @URL: The URL to load
1611 * @filename: the filename where the content should be saved
1612 * @contentType: if available the Content-Type information will be
1613 * returned at that location
1614 *
1615 * This function try to fetch the indicated resource via HTTP GET
1616 * and save it's content in the file.
1617 *
1618 * Returns -1 in case of failure, 0 incase of success. The contentType,
1619 * if provided must be freed by the caller
1620 */
1621int
1622xmlNanoHTTPFetch(const char *URL, const char *filename, char **contentType) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001623 void *ctxt = NULL;
1624 char *buf = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001625 int fd;
1626 int len;
Stefan Kostdff8d0f2011-05-09 12:14:59 +03001627 int ret = 0;
1628
William M. Brack015ccb22005-02-13 08:18:52 +00001629 if (filename == NULL) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001630 ctxt = xmlNanoHTTPOpen(URL, contentType);
1631 if (ctxt == NULL) return(-1);
1632
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001633 if (!strcmp(filename, "-"))
Owen Taylor3473f882001-02-23 17:55:21 +00001634 fd = 0;
1635 else {
1636 fd = open(filename, O_CREAT | O_WRONLY, 00644);
1637 if (fd < 0) {
1638 xmlNanoHTTPClose(ctxt);
1639 if ((contentType != NULL) && (*contentType != NULL)) {
1640 xmlFree(*contentType);
1641 *contentType = NULL;
1642 }
1643 return(-1);
1644 }
1645 }
1646
Daniel Veillardf012a642001-07-23 19:10:52 +00001647 xmlNanoHTTPFetchContent( ctxt, &buf, &len );
1648 if ( len > 0 ) {
Stefan Kostdff8d0f2011-05-09 12:14:59 +03001649 if (write(fd, buf, len) == -1) {
1650 ret = -1;
1651 }
Owen Taylor3473f882001-02-23 17:55:21 +00001652 }
1653
1654 xmlNanoHTTPClose(ctxt);
1655 close(fd);
Stefan Kostdff8d0f2011-05-09 12:14:59 +03001656 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +00001657}
1658
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001659#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001660/**
1661 * xmlNanoHTTPSave:
1662 * @ctxt: the HTTP context
1663 * @filename: the filename where the content should be saved
1664 *
1665 * This function saves the output of the HTTP transaction to a file
1666 * It closes and free the context at the end
1667 *
1668 * Returns -1 in case of failure, 0 incase of success.
1669 */
1670int
1671xmlNanoHTTPSave(void *ctxt, const char *filename) {
Daniel Veillarde3924972001-07-25 20:25:21 +00001672 char *buf = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001673 int fd;
1674 int len;
Stefan Kostdff8d0f2011-05-09 12:14:59 +03001675 int ret = 0;
1676
William M. Brack015ccb22005-02-13 08:18:52 +00001677 if ((ctxt == NULL) || (filename == NULL)) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001678
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001679 if (!strcmp(filename, "-"))
Owen Taylor3473f882001-02-23 17:55:21 +00001680 fd = 0;
1681 else {
Daniel Veillardcd2ebab2007-08-23 20:47:33 +00001682 fd = open(filename, O_CREAT | O_WRONLY, 0666);
Owen Taylor3473f882001-02-23 17:55:21 +00001683 if (fd < 0) {
1684 xmlNanoHTTPClose(ctxt);
1685 return(-1);
1686 }
1687 }
1688
Daniel Veillardf012a642001-07-23 19:10:52 +00001689 xmlNanoHTTPFetchContent( ctxt, &buf, &len );
1690 if ( len > 0 ) {
Stefan Kostdff8d0f2011-05-09 12:14:59 +03001691 if (write(fd, buf, len) == -1) {
1692 ret = -1;
1693 }
Owen Taylor3473f882001-02-23 17:55:21 +00001694 }
1695
1696 xmlNanoHTTPClose(ctxt);
William M. Brack20d82362004-03-17 08:44:46 +00001697 close(fd);
Stefan Kostdff8d0f2011-05-09 12:14:59 +03001698 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +00001699}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001700#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001701
1702/**
1703 * xmlNanoHTTPReturnCode:
1704 * @ctx: the HTTP context
1705 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001706 * Get the latest HTTP return code received
1707 *
Owen Taylor3473f882001-02-23 17:55:21 +00001708 * Returns the HTTP return code for the request.
1709 */
1710int
1711xmlNanoHTTPReturnCode(void *ctx) {
1712 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1713
1714 if (ctxt == NULL) return(-1);
1715
1716 return(ctxt->returnValue);
1717}
1718
1719/**
1720 * xmlNanoHTTPAuthHeader:
1721 * @ctx: the HTTP context
1722 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001723 * Get the authentication header of an HTTP context
1724 *
Owen Taylor3473f882001-02-23 17:55:21 +00001725 * Returns the stashed value of the WWW-Authenticate or Proxy-Authenticate
1726 * header.
1727 */
1728const char *
1729xmlNanoHTTPAuthHeader(void *ctx) {
1730 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1731
1732 if (ctxt == NULL) return(NULL);
1733
1734 return(ctxt->authHeader);
1735}
1736
Daniel Veillardf012a642001-07-23 19:10:52 +00001737/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00001738 * xmlNanoHTTPContentLength:
Daniel Veillardf012a642001-07-23 19:10:52 +00001739 * @ctx: the HTTP context
1740 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001741 * Provides the specified content length from the HTTP header.
1742 *
Daniel Veillardf012a642001-07-23 19:10:52 +00001743 * Return the specified content length from the HTTP header. Note that
1744 * a value of -1 indicates that the content length element was not included in
1745 * the response header.
1746 */
1747int
1748xmlNanoHTTPContentLength( void * ctx ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001749 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillardf012a642001-07-23 19:10:52 +00001750
1751 return ( ( ctxt == NULL ) ? -1 : ctxt->ContentLength );
1752}
1753
1754/**
Daniel Veillard847332a2003-10-18 11:29:40 +00001755 * xmlNanoHTTPRedir:
1756 * @ctx: the HTTP context
1757 *
1758 * Provides the specified redirection URL if available from the HTTP header.
1759 *
1760 * Return the specified redirection URL or NULL if not redirected.
1761 */
1762const char *
1763xmlNanoHTTPRedir( void * ctx ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001764 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillard847332a2003-10-18 11:29:40 +00001765
1766 return ( ( ctxt == NULL ) ? NULL : ctxt->location );
1767}
1768
1769/**
1770 * xmlNanoHTTPEncoding:
1771 * @ctx: the HTTP context
1772 *
1773 * Provides the specified encoding if specified in the HTTP headers.
1774 *
1775 * Return the specified encoding or NULL if not available
1776 */
1777const char *
1778xmlNanoHTTPEncoding( void * ctx ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001779 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillard847332a2003-10-18 11:29:40 +00001780
1781 return ( ( ctxt == NULL ) ? NULL : ctxt->encoding );
1782}
1783
1784/**
Daniel Veillarda840b692003-10-19 13:35:37 +00001785 * xmlNanoHTTPMimeType:
1786 * @ctx: the HTTP context
1787 *
1788 * Provides the specified Mime-Type if specified in the HTTP headers.
1789 *
1790 * Return the specified Mime-Type or NULL if not available
1791 */
1792const char *
1793xmlNanoHTTPMimeType( void * ctx ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001794 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillarda840b692003-10-19 13:35:37 +00001795
1796 return ( ( ctxt == NULL ) ? NULL : ctxt->mimeType );
1797}
1798
1799/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00001800 * xmlNanoHTTPFetchContent:
Daniel Veillardf012a642001-07-23 19:10:52 +00001801 * @ctx: the HTTP context
1802 * @ptr: pointer to set to the content buffer.
1803 * @len: integer pointer to hold the length of the content
1804 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001805 * Check if all the content was read
1806 *
Daniel Veillardf012a642001-07-23 19:10:52 +00001807 * Returns 0 if all the content was read and available, returns
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001808 * -1 if received content length was less than specified or an error
Daniel Veillardf012a642001-07-23 19:10:52 +00001809 * occurred.
1810 */
Daniel Veillarda2351322004-06-27 12:08:10 +00001811static int
Daniel Veillardf012a642001-07-23 19:10:52 +00001812xmlNanoHTTPFetchContent( void * ctx, char ** ptr, int * len ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001813 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillardf012a642001-07-23 19:10:52 +00001814
1815 int rc = 0;
1816 int cur_lgth;
1817 int rcvd_lgth;
1818 int dummy_int;
1819 char * dummy_ptr = NULL;
1820
1821 /* Dummy up return input parameters if not provided */
1822
1823 if ( len == NULL )
1824 len = &dummy_int;
1825
1826 if ( ptr == NULL )
1827 ptr = &dummy_ptr;
1828
1829 /* But can't work without the context pointer */
1830
1831 if ( ( ctxt == NULL ) || ( ctxt->content == NULL ) ) {
1832 *len = 0;
1833 *ptr = NULL;
1834 return ( -1 );
1835 }
1836
1837 rcvd_lgth = ctxt->inptr - ctxt->content;
1838
1839 while ( (cur_lgth = xmlNanoHTTPRecv( ctxt )) > 0 ) {
1840
1841 rcvd_lgth += cur_lgth;
1842 if ( (ctxt->ContentLength > 0) && (rcvd_lgth >= ctxt->ContentLength) )
1843 break;
1844 }
1845
1846 *ptr = ctxt->content;
1847 *len = rcvd_lgth;
1848
1849 if ( ( ctxt->ContentLength > 0 ) && ( rcvd_lgth < ctxt->ContentLength ) )
1850 rc = -1;
1851 else if ( rcvd_lgth == 0 )
1852 rc = -1;
1853
1854 return ( rc );
1855}
1856
Owen Taylor3473f882001-02-23 17:55:21 +00001857#ifdef STANDALONE
1858int main(int argc, char **argv) {
1859 char *contentType = NULL;
1860
1861 if (argv[1] != NULL) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001862 if (argv[2] != NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00001863 xmlNanoHTTPFetch(argv[1], argv[2], &contentType);
1864 else
1865 xmlNanoHTTPFetch(argv[1], "-", &contentType);
1866 if (contentType != NULL) xmlFree(contentType);
1867 } else {
1868 xmlGenericError(xmlGenericErrorContext,
1869 "%s: minimal HTTP GET implementation\n", argv[0]);
1870 xmlGenericError(xmlGenericErrorContext,
1871 "\tusage %s [ URL [ filename ] ]\n", argv[0]);
1872 }
1873 xmlNanoHTTPCleanup();
1874 xmlMemoryDump();
1875 return(0);
1876}
1877#endif /* STANDALONE */
1878#else /* !LIBXML_HTTP_ENABLED */
1879#ifdef STANDALONE
1880#include <stdio.h>
1881int main(int argc, char **argv) {
1882 xmlGenericError(xmlGenericErrorContext,
1883 "%s : HTTP support not compiled in\n", argv[0]);
1884 return(0);
1885}
1886#endif /* STANDALONE */
1887#endif /* LIBXML_HTTP_ENABLED */
Daniel Veillard5d4644e2005-04-01 13:11:58 +00001888#define bottom_nanohttp
1889#include "elfgcchack.h"