blob: de89ab788ce362f47991dc078c10a830d0010028 [file] [log] [blame]
Owen Taylor3473f882001-02-23 17:55:21 +00001/*
2 * nanohttp.c: minimalist HTTP GET implementation to fetch external subsets.
3 * focuses on size, streamability, reentrancy and portability
4 *
5 * This is clearly not a general purpose HTTP implementation
6 * If you look for one, check:
7 * http://www.w3.org/Library/
8 *
9 * See Copyright for the status of this software.
10 *
Daniel Veillardc5d64342001-06-24 12:13:24 +000011 * daniel@veillard.com
Owen Taylor3473f882001-02-23 17:55:21 +000012 */
13
14/* TODO add compression support, Send the Accept- , and decompress on the
15 fly with ZLIB if found at compile-time */
16
Daniel Veillardf3afa7d2001-06-09 13:52:58 +000017#define NEED_SOCKETS
Daniel Veillard34ce8be2002-03-18 19:37:11 +000018#define IN_LIBXML
Bjorn Reese70a9da52001-04-21 16:57:29 +000019#include "libxml.h"
Owen Taylor3473f882001-02-23 17:55:21 +000020
21#ifdef LIBXML_HTTP_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +000022#include <string.h>
23
24#ifdef HAVE_STDLIB_H
25#include <stdlib.h>
26#endif
27#ifdef HAVE_UNISTD_H
28#include <unistd.h>
29#endif
Daniel Veillard75eb1ad2003-07-07 14:42:44 +000030#ifdef HAVE_SYS_TYPES_H
31#include <sys/types.h>
32#endif
Owen Taylor3473f882001-02-23 17:55:21 +000033#ifdef HAVE_SYS_SOCKET_H
34#include <sys/socket.h>
35#endif
36#ifdef HAVE_NETINET_IN_H
37#include <netinet/in.h>
38#endif
39#ifdef HAVE_ARPA_INET_H
40#include <arpa/inet.h>
41#endif
42#ifdef HAVE_NETDB_H
43#include <netdb.h>
44#endif
Daniel Veillardd85f4f42002-03-25 10:48:46 +000045#ifdef HAVE_RESOLV_H
Daniel Veillard9b731d72002-04-14 12:56:08 +000046#ifdef HAVE_ARPA_NAMESER_H
47#include <arpa/nameser.h>
48#endif
Daniel Veillardd85f4f42002-03-25 10:48:46 +000049#include <resolv.h>
50#endif
Owen Taylor3473f882001-02-23 17:55:21 +000051#ifdef HAVE_FCNTL_H
52#include <fcntl.h>
53#endif
54#ifdef HAVE_ERRNO_H
55#include <errno.h>
56#endif
57#ifdef HAVE_SYS_TIME_H
58#include <sys/time.h>
59#endif
60#ifdef HAVE_SYS_SELECT_H
61#include <sys/select.h>
62#endif
63#ifdef HAVE_STRINGS_H
64#include <strings.h>
65#endif
66#ifdef SUPPORT_IP6
67#include <resolv.h>
68#endif
69
70#ifdef VMS
71#include <stropts>
Daniel Veillardc284c642005-03-31 10:24:24 +000072#define XML_SOCKLEN_T unsigned int
Owen Taylor3473f882001-02-23 17:55:21 +000073#define SOCKET int
74#endif
75
Daniel Veillard1638a472003-08-14 01:23:25 +000076
77#ifdef __MINGW32__
78#define _WINSOCKAPI_
79#include <wsockcompat.h>
80#include <winsock2.h>
Daniel Veillardc284c642005-03-31 10:24:24 +000081#undef XML_SOCKLEN_T
82#define XML_SOCKLEN_T unsigned int
Daniel Veillard1638a472003-08-14 01:23:25 +000083#endif
84
85
Daniel Veillardd0463562001-10-13 09:15:48 +000086#include <libxml/globals.h>
Daniel Veillardf012a642001-07-23 19:10:52 +000087#include <libxml/xmlerror.h>
Owen Taylor3473f882001-02-23 17:55:21 +000088#include <libxml/xmlmemory.h>
89#include <libxml/parser.h> /* for xmlStr(n)casecmp() */
90#include <libxml/nanohttp.h>
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000091#include <libxml/globals.h>
Daniel Veillard8efff672002-12-04 11:44:48 +000092#include <libxml/uri.h>
Owen Taylor3473f882001-02-23 17:55:21 +000093
94/**
95 * A couple portability macros
96 */
97#ifndef _WINSOCKAPI_
Daniel Veillarda9cce9c2003-09-29 13:20:24 +000098#ifndef __BEOS__
Owen Taylor3473f882001-02-23 17:55:21 +000099#define closesocket(s) close(s)
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000100#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000101#define SOCKET int
102#endif
103
Daniel Veillard89f7f272003-09-29 13:29:09 +0000104#ifdef __BEOS__
105#ifndef PF_INET
106#define PF_INET AF_INET
107#endif
108#endif
109
Daniel Veillardc284c642005-03-31 10:24:24 +0000110#ifndef XML_SOCKLEN_T
111#define XML_SOCKLEN_T unsigned int
Daniel Veillard75be0132002-03-13 10:03:35 +0000112#endif
113#ifndef SOCKET
114#define SOCKET int
115#endif
Daniel Veillardf012a642001-07-23 19:10:52 +0000116
Owen Taylor3473f882001-02-23 17:55:21 +0000117#ifdef STANDALONE
118#define DEBUG_HTTP
119#define xmlStrncasecmp(a, b, n) strncasecmp((char *)a, (char *)b, n)
120#define xmlStrcasecmpi(a, b) strcasecmp((char *)a, (char *)b)
121#endif
122
123#define XML_NANO_HTTP_MAX_REDIR 10
124
125#define XML_NANO_HTTP_CHUNK 4096
126
127#define XML_NANO_HTTP_CLOSED 0
128#define XML_NANO_HTTP_WRITE 1
129#define XML_NANO_HTTP_READ 2
130#define XML_NANO_HTTP_NONE 4
131
132typedef struct xmlNanoHTTPCtxt {
133 char *protocol; /* the protocol name */
134 char *hostname; /* the host name */
135 int port; /* the port */
136 char *path; /* the path within the URL */
Daniel Veillard351f2d62005-04-13 02:55:12 +0000137 char *query; /* the query string */
Owen Taylor3473f882001-02-23 17:55:21 +0000138 SOCKET fd; /* the file descriptor for the socket */
139 int state; /* WRITE / READ / CLOSED */
140 char *out; /* buffer sent (zero terminated) */
141 char *outptr; /* index within the buffer sent */
142 char *in; /* the receiving buffer */
143 char *content; /* the start of the content */
144 char *inptr; /* the next byte to read from network */
145 char *inrptr; /* the next byte to give back to the client */
146 int inlen; /* len of the input buffer */
147 int last; /* return code for last operation */
148 int returnValue; /* the protocol return value */
Daniel Veillardf012a642001-07-23 19:10:52 +0000149 int ContentLength; /* specified content length from HTTP header */
Owen Taylor3473f882001-02-23 17:55:21 +0000150 char *contentType; /* the MIME type for the input */
151 char *location; /* the new URL in case of redirect */
152 char *authHeader; /* contents of {WWW,Proxy}-Authenticate header */
Daniel Veillard847332a2003-10-18 11:29:40 +0000153 char *encoding; /* encoding extracted from the contentType */
Daniel Veillarda840b692003-10-19 13:35:37 +0000154 char *mimeType; /* Mime-Type extracted from the contentType */
Owen Taylor3473f882001-02-23 17:55:21 +0000155} xmlNanoHTTPCtxt, *xmlNanoHTTPCtxtPtr;
156
157static int initialized = 0;
158static char *proxy = NULL; /* the proxy name if any */
159static int proxyPort; /* the proxy port if any */
160static unsigned int timeout = 60;/* the select() timeout in seconds */
161
Daniel Veillarda2351322004-06-27 12:08:10 +0000162static int xmlNanoHTTPFetchContent( void * ctx, char ** ptr, int * len );
Daniel Veillardf012a642001-07-23 19:10:52 +0000163
Owen Taylor3473f882001-02-23 17:55:21 +0000164/**
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000165 * xmlHTTPErrMemory:
166 * @extra: extra informations
167 *
168 * Handle an out of memory condition
169 */
170static void
171xmlHTTPErrMemory(const char *extra)
172{
173 __xmlSimpleError(XML_FROM_HTTP, XML_ERR_NO_MEMORY, NULL, NULL, extra);
174}
175
176/**
Owen Taylor3473f882001-02-23 17:55:21 +0000177 * A portability function
178 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000179static int socket_errno(void) {
Owen Taylor3473f882001-02-23 17:55:21 +0000180#ifdef _WINSOCKAPI_
181 return(WSAGetLastError());
182#else
183 return(errno);
184#endif
185}
186
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000187#ifdef SUPPORT_IP6
Daniel Veillard2db8c122003-07-08 12:16:59 +0000188static
189int have_ipv6(void) {
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000190 int s;
191
192 s = socket (AF_INET6, SOCK_STREAM, 0);
193 if (s != -1) {
194 close (s);
195 return (1);
196 }
197 return (0);
198}
199#endif
200
Owen Taylor3473f882001-02-23 17:55:21 +0000201/**
202 * xmlNanoHTTPInit:
203 *
204 * Initialize the HTTP protocol layer.
205 * Currently it just checks for proxy informations
206 */
207
208void
209xmlNanoHTTPInit(void) {
210 const char *env;
211#ifdef _WINSOCKAPI_
212 WSADATA wsaData;
213#endif
214
215 if (initialized)
216 return;
217
218#ifdef _WINSOCKAPI_
219 if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0)
220 return;
221#endif
222
223 if (proxy == NULL) {
224 proxyPort = 80;
225 env = getenv("no_proxy");
Daniel Veillard29b17482004-08-16 00:39:03 +0000226 if (env && ((env[0] == '*') && (env[1] == 0)))
Owen Taylor3473f882001-02-23 17:55:21 +0000227 goto done;
228 env = getenv("http_proxy");
229 if (env != NULL) {
230 xmlNanoHTTPScanProxy(env);
231 goto done;
232 }
233 env = getenv("HTTP_PROXY");
234 if (env != NULL) {
235 xmlNanoHTTPScanProxy(env);
236 goto done;
237 }
238 }
239done:
240 initialized = 1;
241}
242
243/**
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000244 * xmlNanoHTTPCleanup:
Owen Taylor3473f882001-02-23 17:55:21 +0000245 *
246 * Cleanup the HTTP protocol layer.
247 */
248
249void
250xmlNanoHTTPCleanup(void) {
251 if (proxy != NULL)
252 xmlFree(proxy);
253#ifdef _WINSOCKAPI_
254 if (initialized)
255 WSACleanup();
256#endif
257 initialized = 0;
258 return;
259}
260
261/**
Owen Taylor3473f882001-02-23 17:55:21 +0000262 * xmlNanoHTTPScanURL:
263 * @ctxt: an HTTP context
264 * @URL: The URL used to initialize the context
265 *
266 * (Re)Initialize an HTTP context by parsing the URL and finding
267 * the protocol host port and path it indicates.
268 */
269
270static void
271xmlNanoHTTPScanURL(xmlNanoHTTPCtxtPtr ctxt, const char *URL) {
William M. Brack015ccb22005-02-13 08:18:52 +0000272 xmlURIPtr uri;
273 /*
274 * Clear any existing data from the context
275 */
Owen Taylor3473f882001-02-23 17:55:21 +0000276 if (ctxt->protocol != NULL) {
277 xmlFree(ctxt->protocol);
278 ctxt->protocol = NULL;
279 }
280 if (ctxt->hostname != NULL) {
281 xmlFree(ctxt->hostname);
282 ctxt->hostname = NULL;
283 }
284 if (ctxt->path != NULL) {
285 xmlFree(ctxt->path);
286 ctxt->path = NULL;
287 }
Daniel Veillard351f2d62005-04-13 02:55:12 +0000288 if (ctxt->query != NULL) {
289 xmlFree(ctxt->query);
290 ctxt->query = NULL;
291 }
Owen Taylor3473f882001-02-23 17:55:21 +0000292 if (URL == NULL) return;
William M. Brack015ccb22005-02-13 08:18:52 +0000293
294 uri = xmlParseURI(URL);
295 if (uri == NULL)
296 return;
297
298 if ((uri->scheme == NULL) || (uri->server == NULL)) {
299 xmlFreeURI(uri);
300 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000301 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000302
William M. Brack015ccb22005-02-13 08:18:52 +0000303 ctxt->protocol = xmlMemStrdup(uri->scheme);
304 ctxt->hostname = xmlMemStrdup(uri->server);
305 if (uri->path != NULL)
306 ctxt->path = xmlMemStrdup(uri->path);
307 else
308 ctxt->path = xmlMemStrdup("/");
Daniel Veillard351f2d62005-04-13 02:55:12 +0000309 if (uri->query != NULL)
310 ctxt->query = xmlMemStrdup(uri->query);
William M. Brack015ccb22005-02-13 08:18:52 +0000311 if (uri->port != 0)
312 ctxt->port = uri->port;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000313
William M. Brack015ccb22005-02-13 08:18:52 +0000314 xmlFreeURI(uri);
Owen Taylor3473f882001-02-23 17:55:21 +0000315}
316
317/**
318 * xmlNanoHTTPScanProxy:
319 * @URL: The proxy URL used to initialize the proxy context
320 *
321 * (Re)Initialize the HTTP Proxy context by parsing the URL and finding
322 * the protocol host port it indicates.
323 * Should be like http://myproxy/ or http://myproxy:3128/
324 * A NULL URL cleans up proxy informations.
325 */
326
327void
328xmlNanoHTTPScanProxy(const char *URL) {
William M. Brack015ccb22005-02-13 08:18:52 +0000329 xmlURIPtr uri;
Owen Taylor3473f882001-02-23 17:55:21 +0000330
331 if (proxy != NULL) {
332 xmlFree(proxy);
333 proxy = NULL;
334 }
William M. Brack015ccb22005-02-13 08:18:52 +0000335 proxyPort = 0;
336
Owen Taylor3473f882001-02-23 17:55:21 +0000337#ifdef DEBUG_HTTP
338 if (URL == NULL)
339 xmlGenericError(xmlGenericErrorContext,
340 "Removing HTTP proxy info\n");
341 else
342 xmlGenericError(xmlGenericErrorContext,
343 "Using HTTP proxy %s\n", URL);
344#endif
345 if (URL == NULL) return;
William M. Brack015ccb22005-02-13 08:18:52 +0000346
347 uri = xmlParseURI(URL);
348 if ((uri == NULL) || (uri->scheme == NULL) ||
349 (strcmp(uri->scheme, "http")) || (uri->server == NULL)) {
350 __xmlIOErr(XML_FROM_HTTP, XML_HTTP_URL_SYNTAX, "Syntax Error\n");
351 if (uri != NULL)
352 xmlFreeURI(uri);
353 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000354 }
William M. Brack015ccb22005-02-13 08:18:52 +0000355
356 proxy = xmlMemStrdup(uri->server);
357 if (uri->port != 0)
358 proxyPort = uri->port;
Owen Taylor3473f882001-02-23 17:55:21 +0000359
William M. Brack015ccb22005-02-13 08:18:52 +0000360 xmlFreeURI(uri);
Owen Taylor3473f882001-02-23 17:55:21 +0000361}
362
363/**
364 * xmlNanoHTTPNewCtxt:
365 * @URL: The URL used to initialize the context
366 *
367 * Allocate and initialize a new HTTP context.
368 *
369 * Returns an HTTP context or NULL in case of error.
370 */
371
372static xmlNanoHTTPCtxtPtr
373xmlNanoHTTPNewCtxt(const char *URL) {
374 xmlNanoHTTPCtxtPtr ret;
375
376 ret = (xmlNanoHTTPCtxtPtr) xmlMalloc(sizeof(xmlNanoHTTPCtxt));
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000377 if (ret == NULL) {
378 xmlHTTPErrMemory("allocating context");
379 return(NULL);
380 }
Owen Taylor3473f882001-02-23 17:55:21 +0000381
382 memset(ret, 0, sizeof(xmlNanoHTTPCtxt));
383 ret->port = 80;
384 ret->returnValue = 0;
385 ret->fd = -1;
Daniel Veillardf012a642001-07-23 19:10:52 +0000386 ret->ContentLength = -1;
Owen Taylor3473f882001-02-23 17:55:21 +0000387
Daniel Veillardcacbe5d2003-01-10 16:09:51 +0000388 xmlNanoHTTPScanURL(ret, URL);
Owen Taylor3473f882001-02-23 17:55:21 +0000389
390 return(ret);
391}
392
393/**
394 * xmlNanoHTTPFreeCtxt:
395 * @ctxt: an HTTP context
396 *
397 * Frees the context after closing the connection.
398 */
399
400static void
401xmlNanoHTTPFreeCtxt(xmlNanoHTTPCtxtPtr ctxt) {
402 if (ctxt == NULL) return;
403 if (ctxt->hostname != NULL) xmlFree(ctxt->hostname);
404 if (ctxt->protocol != NULL) xmlFree(ctxt->protocol);
405 if (ctxt->path != NULL) xmlFree(ctxt->path);
Daniel Veillard351f2d62005-04-13 02:55:12 +0000406 if (ctxt->query != NULL) xmlFree(ctxt->query);
Owen Taylor3473f882001-02-23 17:55:21 +0000407 if (ctxt->out != NULL) xmlFree(ctxt->out);
408 if (ctxt->in != NULL) xmlFree(ctxt->in);
409 if (ctxt->contentType != NULL) xmlFree(ctxt->contentType);
Daniel Veillard847332a2003-10-18 11:29:40 +0000410 if (ctxt->encoding != NULL) xmlFree(ctxt->encoding);
Daniel Veillarda840b692003-10-19 13:35:37 +0000411 if (ctxt->mimeType != NULL) xmlFree(ctxt->mimeType);
Owen Taylor3473f882001-02-23 17:55:21 +0000412 if (ctxt->location != NULL) xmlFree(ctxt->location);
413 if (ctxt->authHeader != NULL) xmlFree(ctxt->authHeader);
414 ctxt->state = XML_NANO_HTTP_NONE;
415 if (ctxt->fd >= 0) closesocket(ctxt->fd);
416 ctxt->fd = -1;
417 xmlFree(ctxt);
418}
419
420/**
421 * xmlNanoHTTPSend:
422 * @ctxt: an HTTP context
423 *
424 * Send the input needed to initiate the processing on the server side
Daniel Veillardf012a642001-07-23 19:10:52 +0000425 * Returns number of bytes sent or -1 on error.
Owen Taylor3473f882001-02-23 17:55:21 +0000426 */
427
Daniel Veillardf012a642001-07-23 19:10:52 +0000428static int
429xmlNanoHTTPSend(xmlNanoHTTPCtxtPtr ctxt, const char * xmt_ptr, int outlen) {
430
431 int total_sent = 0;
432
433 if ( (ctxt->state & XML_NANO_HTTP_WRITE) && (xmt_ptr != NULL ) ) {
434 while (total_sent < outlen) {
435 int nsent = send(ctxt->fd, xmt_ptr + total_sent,
436 outlen - total_sent, 0);
Owen Taylor3473f882001-02-23 17:55:21 +0000437 if (nsent>0)
438 total_sent += nsent;
Daniel Veillardf012a642001-07-23 19:10:52 +0000439 else if ( ( nsent == -1 ) &&
Daniel Veillardba6db032001-07-31 16:25:45 +0000440#if defined(EAGAIN) && EAGAIN != EWOULDBLOCK
Daniel Veillardf012a642001-07-23 19:10:52 +0000441 ( socket_errno( ) != EAGAIN ) &&
Daniel Veillardba6db032001-07-31 16:25:45 +0000442#endif
Daniel Veillardf012a642001-07-23 19:10:52 +0000443 ( socket_errno( ) != EWOULDBLOCK ) ) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000444 __xmlIOErr(XML_FROM_HTTP, 0, "send failed\n");
Daniel Veillardf012a642001-07-23 19:10:52 +0000445 if ( total_sent == 0 )
446 total_sent = -1;
447 break;
448 }
449 else {
450 /*
451 ** No data sent
452 ** Since non-blocking sockets are used, wait for
453 ** socket to be writable or default timeout prior
454 ** to retrying.
455 */
456
457 struct timeval tv;
458 fd_set wfd;
459
460 tv.tv_sec = timeout;
461 tv.tv_usec = 0;
462 FD_ZERO( &wfd );
463 FD_SET( ctxt->fd, &wfd );
464 (void)select( ctxt->fd + 1, NULL, &wfd, NULL, &tv );
465 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000466 }
Owen Taylor3473f882001-02-23 17:55:21 +0000467 }
Daniel Veillardf012a642001-07-23 19:10:52 +0000468
469 return total_sent;
Owen Taylor3473f882001-02-23 17:55:21 +0000470}
471
472/**
473 * xmlNanoHTTPRecv:
474 * @ctxt: an HTTP context
475 *
476 * Read information coming from the HTTP connection.
477 * This is a blocking call (but it blocks in select(), not read()).
478 *
479 * Returns the number of byte read or -1 in case of error.
480 */
481
482static int
483xmlNanoHTTPRecv(xmlNanoHTTPCtxtPtr ctxt) {
484 fd_set rfd;
485 struct timeval tv;
486
487
488 while (ctxt->state & XML_NANO_HTTP_READ) {
489 if (ctxt->in == NULL) {
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000490 ctxt->in = (char *) xmlMallocAtomic(65000 * sizeof(char));
Owen Taylor3473f882001-02-23 17:55:21 +0000491 if (ctxt->in == NULL) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000492 xmlHTTPErrMemory("allocating input");
Owen Taylor3473f882001-02-23 17:55:21 +0000493 ctxt->last = -1;
494 return(-1);
495 }
496 ctxt->inlen = 65000;
497 ctxt->inptr = ctxt->content = ctxt->inrptr = ctxt->in;
498 }
499 if (ctxt->inrptr > ctxt->in + XML_NANO_HTTP_CHUNK) {
500 int delta = ctxt->inrptr - ctxt->in;
501 int len = ctxt->inptr - ctxt->inrptr;
502
503 memmove(ctxt->in, ctxt->inrptr, len);
504 ctxt->inrptr -= delta;
505 ctxt->content -= delta;
506 ctxt->inptr -= delta;
507 }
508 if ((ctxt->in + ctxt->inlen) < (ctxt->inptr + XML_NANO_HTTP_CHUNK)) {
509 int d_inptr = ctxt->inptr - ctxt->in;
510 int d_content = ctxt->content - ctxt->in;
511 int d_inrptr = ctxt->inrptr - ctxt->in;
Daniel Veillardf012a642001-07-23 19:10:52 +0000512 char * tmp_ptr = ctxt->in;
Owen Taylor3473f882001-02-23 17:55:21 +0000513
514 ctxt->inlen *= 2;
Daniel Veillardf012a642001-07-23 19:10:52 +0000515 ctxt->in = (char *) xmlRealloc(tmp_ptr, ctxt->inlen);
Owen Taylor3473f882001-02-23 17:55:21 +0000516 if (ctxt->in == NULL) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000517 xmlHTTPErrMemory("allocating input buffer");
Daniel Veillardf012a642001-07-23 19:10:52 +0000518 xmlFree( tmp_ptr );
Owen Taylor3473f882001-02-23 17:55:21 +0000519 ctxt->last = -1;
520 return(-1);
521 }
522 ctxt->inptr = ctxt->in + d_inptr;
523 ctxt->content = ctxt->in + d_content;
524 ctxt->inrptr = ctxt->in + d_inrptr;
525 }
526 ctxt->last = recv(ctxt->fd, ctxt->inptr, XML_NANO_HTTP_CHUNK, 0);
527 if (ctxt->last > 0) {
528 ctxt->inptr += ctxt->last;
529 return(ctxt->last);
530 }
531 if (ctxt->last == 0) {
532 return(0);
533 }
534 if (ctxt->last == -1) {
535 switch (socket_errno()) {
536 case EINPROGRESS:
537 case EWOULDBLOCK:
538#if defined(EAGAIN) && EAGAIN != EWOULDBLOCK
539 case EAGAIN:
540#endif
541 break;
Daniel Veillardf012a642001-07-23 19:10:52 +0000542
543 case ECONNRESET:
544 case ESHUTDOWN:
545 return ( 0 );
546
Owen Taylor3473f882001-02-23 17:55:21 +0000547 default:
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000548 __xmlIOErr(XML_FROM_HTTP, 0, "recv failed\n");
Daniel Veillardf012a642001-07-23 19:10:52 +0000549 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +0000550 }
551 }
552
553 tv.tv_sec = timeout;
554 tv.tv_usec = 0;
555 FD_ZERO(&rfd);
556 FD_SET(ctxt->fd, &rfd);
557
Daniel Veillard50f34372001-08-03 12:06:36 +0000558 if ( (select(ctxt->fd+1, &rfd, NULL, NULL, &tv)<1)
559#if defined(EINTR)
560 && (errno != EINTR)
561#endif
562 )
Owen Taylor3473f882001-02-23 17:55:21 +0000563 return(0);
564 }
565 return(0);
566}
567
568/**
569 * xmlNanoHTTPReadLine:
570 * @ctxt: an HTTP context
571 *
572 * Read one line in the HTTP server output, usually for extracting
573 * the HTTP protocol informations from the answer header.
574 *
575 * Returns a newly allocated string with a copy of the line, or NULL
576 * which indicate the end of the input.
577 */
578
579static char *
580xmlNanoHTTPReadLine(xmlNanoHTTPCtxtPtr ctxt) {
581 char buf[4096];
582 char *bp = buf;
Daniel Veillardf012a642001-07-23 19:10:52 +0000583 int rc;
Owen Taylor3473f882001-02-23 17:55:21 +0000584
585 while (bp - buf < 4095) {
586 if (ctxt->inrptr == ctxt->inptr) {
Daniel Veillardf012a642001-07-23 19:10:52 +0000587 if ( (rc = xmlNanoHTTPRecv(ctxt)) == 0) {
Owen Taylor3473f882001-02-23 17:55:21 +0000588 if (bp == buf)
589 return(NULL);
590 else
591 *bp = 0;
592 return(xmlMemStrdup(buf));
593 }
Daniel Veillardf012a642001-07-23 19:10:52 +0000594 else if ( rc == -1 ) {
595 return ( NULL );
596 }
Owen Taylor3473f882001-02-23 17:55:21 +0000597 }
598 *bp = *ctxt->inrptr++;
599 if (*bp == '\n') {
600 *bp = 0;
601 return(xmlMemStrdup(buf));
602 }
603 if (*bp != '\r')
604 bp++;
605 }
606 buf[4095] = 0;
607 return(xmlMemStrdup(buf));
608}
609
610
611/**
612 * xmlNanoHTTPScanAnswer:
613 * @ctxt: an HTTP context
614 * @line: an HTTP header line
615 *
616 * Try to extract useful informations from the server answer.
617 * We currently parse and process:
618 * - The HTTP revision/ return code
Daniel Veillarda840b692003-10-19 13:35:37 +0000619 * - The Content-Type, Mime-Type and charset used
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000620 * - The Location for redirect processing.
Owen Taylor3473f882001-02-23 17:55:21 +0000621 *
622 * Returns -1 in case of failure, the file descriptor number otherwise
623 */
624
625static void
626xmlNanoHTTPScanAnswer(xmlNanoHTTPCtxtPtr ctxt, const char *line) {
627 const char *cur = line;
628
629 if (line == NULL) return;
630
631 if (!strncmp(line, "HTTP/", 5)) {
632 int version = 0;
633 int ret = 0;
634
635 cur += 5;
636 while ((*cur >= '0') && (*cur <= '9')) {
637 version *= 10;
638 version += *cur - '0';
639 cur++;
640 }
641 if (*cur == '.') {
642 cur++;
643 if ((*cur >= '0') && (*cur <= '9')) {
644 version *= 10;
645 version += *cur - '0';
646 cur++;
647 }
648 while ((*cur >= '0') && (*cur <= '9'))
649 cur++;
650 } else
651 version *= 10;
652 if ((*cur != ' ') && (*cur != '\t')) return;
653 while ((*cur == ' ') || (*cur == '\t')) cur++;
654 if ((*cur < '0') || (*cur > '9')) return;
655 while ((*cur >= '0') && (*cur <= '9')) {
656 ret *= 10;
657 ret += *cur - '0';
658 cur++;
659 }
660 if ((*cur != 0) && (*cur != ' ') && (*cur != '\t')) return;
661 ctxt->returnValue = ret;
662 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Content-Type:", 13)) {
Daniel Veillarda840b692003-10-19 13:35:37 +0000663 const xmlChar *charset, *last, *mime;
Owen Taylor3473f882001-02-23 17:55:21 +0000664 cur += 13;
665 while ((*cur == ' ') || (*cur == '\t')) cur++;
666 if (ctxt->contentType != NULL)
667 xmlFree(ctxt->contentType);
668 ctxt->contentType = xmlMemStrdup(cur);
Daniel Veillarda840b692003-10-19 13:35:37 +0000669 mime = (const xmlChar *) cur;
670 last = mime;
671 while ((*last != 0) && (*last != ' ') && (*last != '\t') &&
672 (*last != ';') && (*last != ','))
673 last++;
674 if (ctxt->mimeType != NULL)
675 xmlFree(ctxt->mimeType);
676 ctxt->mimeType = (char *) xmlStrndup(mime, last - mime);
677 charset = xmlStrstr(BAD_CAST ctxt->contentType, BAD_CAST "charset=");
678 if (charset != NULL) {
679 charset += 8;
680 last = charset;
681 while ((*last != 0) && (*last != ' ') && (*last != '\t') &&
682 (*last != ';') && (*last != ','))
683 last++;
684 if (ctxt->encoding != NULL)
685 xmlFree(ctxt->encoding);
686 ctxt->encoding = (char *) xmlStrndup(charset, last - charset);
687 }
Owen Taylor3473f882001-02-23 17:55:21 +0000688 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"ContentType:", 12)) {
Daniel Veillarda840b692003-10-19 13:35:37 +0000689 const xmlChar *charset, *last, *mime;
Owen Taylor3473f882001-02-23 17:55:21 +0000690 cur += 12;
691 if (ctxt->contentType != NULL) return;
692 while ((*cur == ' ') || (*cur == '\t')) cur++;
693 ctxt->contentType = xmlMemStrdup(cur);
Daniel Veillarda840b692003-10-19 13:35:37 +0000694 mime = (const xmlChar *) cur;
695 last = mime;
696 while ((*last != 0) && (*last != ' ') && (*last != '\t') &&
697 (*last != ';') && (*last != ','))
698 last++;
699 if (ctxt->mimeType != NULL)
700 xmlFree(ctxt->mimeType);
701 ctxt->mimeType = (char *) xmlStrndup(mime, last - mime);
702 charset = xmlStrstr(BAD_CAST ctxt->contentType, BAD_CAST "charset=");
703 if (charset != NULL) {
704 charset += 8;
705 last = charset;
706 while ((*last != 0) && (*last != ' ') && (*last != '\t') &&
707 (*last != ';') && (*last != ','))
708 last++;
709 if (ctxt->encoding != NULL)
710 xmlFree(ctxt->encoding);
711 ctxt->encoding = (char *) xmlStrndup(charset, last - charset);
712 }
Owen Taylor3473f882001-02-23 17:55:21 +0000713 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Location:", 9)) {
714 cur += 9;
715 while ((*cur == ' ') || (*cur == '\t')) cur++;
716 if (ctxt->location != NULL)
717 xmlFree(ctxt->location);
William M. Brack7e29c0a2004-04-02 09:07:22 +0000718 if (*cur == '/') {
719 xmlChar *tmp_http = xmlStrdup(BAD_CAST "http://");
720 xmlChar *tmp_loc =
721 xmlStrcat(tmp_http, (const xmlChar *) ctxt->hostname);
722 ctxt->location =
723 (char *) xmlStrcat (tmp_loc, (const xmlChar *) cur);
724 } else {
725 ctxt->location = xmlMemStrdup(cur);
726 }
Owen Taylor3473f882001-02-23 17:55:21 +0000727 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"WWW-Authenticate:", 17)) {
728 cur += 17;
729 while ((*cur == ' ') || (*cur == '\t')) cur++;
730 if (ctxt->authHeader != NULL)
731 xmlFree(ctxt->authHeader);
732 ctxt->authHeader = xmlMemStrdup(cur);
733 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Proxy-Authenticate:", 19)) {
734 cur += 19;
735 while ((*cur == ' ') || (*cur == '\t')) cur++;
736 if (ctxt->authHeader != NULL)
737 xmlFree(ctxt->authHeader);
738 ctxt->authHeader = xmlMemStrdup(cur);
Daniel Veillardf012a642001-07-23 19:10:52 +0000739 } else if ( !xmlStrncasecmp( BAD_CAST line, BAD_CAST"Content-Length:", 15) ) {
740 cur += 15;
741 ctxt->ContentLength = strtol( cur, NULL, 10 );
Owen Taylor3473f882001-02-23 17:55:21 +0000742 }
743}
744
745/**
746 * xmlNanoHTTPConnectAttempt:
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000747 * @addr: a socket address structure
Owen Taylor3473f882001-02-23 17:55:21 +0000748 *
749 * Attempt a connection to the given IP:port endpoint. It forces
750 * non-blocking semantic on the socket, and allow 60 seconds for
751 * the host to answer.
752 *
753 * Returns -1 in case of failure, the file descriptor number otherwise
754 */
755
756static int
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000757xmlNanoHTTPConnectAttempt(struct sockaddr *addr)
Owen Taylor3473f882001-02-23 17:55:21 +0000758{
Owen Taylor3473f882001-02-23 17:55:21 +0000759 fd_set wfd;
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000760#ifdef _WINSOCKAPI_
761 fd_set xfd;
762#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000763 struct timeval tv;
764 int status;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000765 int addrlen;
766 SOCKET s;
Owen Taylor3473f882001-02-23 17:55:21 +0000767
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000768#ifdef SUPPORT_IP6
769 if (addr->sa_family == AF_INET6) {
770 s = socket (PF_INET6, SOCK_STREAM, IPPROTO_TCP);
771 addrlen = sizeof (struct sockaddr_in6);
772 }
773 else
774#endif
775 {
776 s = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
777 addrlen = sizeof (struct sockaddr_in);
778 }
Owen Taylor3473f882001-02-23 17:55:21 +0000779 if (s==-1) {
780#ifdef DEBUG_HTTP
781 perror("socket");
782#endif
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000783 __xmlIOErr(XML_FROM_HTTP, 0, "socket failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000784 return(-1);
785 }
786
787#ifdef _WINSOCKAPI_
788 {
789 u_long one = 1;
790
791 status = ioctlsocket(s, FIONBIO, &one) == SOCKET_ERROR ? -1 : 0;
792 }
793#else /* _WINSOCKAPI_ */
794#if defined(VMS)
795 {
796 int enable = 1;
797 status = ioctl(s, FIONBIO, &enable);
798 }
799#else /* VMS */
Daniel Veillard254b1262003-11-01 17:04:58 +0000800#if defined(__BEOS__)
801 {
802 bool noblock = true;
803 status = setsockopt(s, SOL_SOCKET, SO_NONBLOCK, &noblock, sizeof(noblock));
804 }
805#else /* __BEOS__ */
Owen Taylor3473f882001-02-23 17:55:21 +0000806 if ((status = fcntl(s, F_GETFL, 0)) != -1) {
807#ifdef O_NONBLOCK
808 status |= O_NONBLOCK;
809#else /* O_NONBLOCK */
810#ifdef F_NDELAY
811 status |= F_NDELAY;
812#endif /* F_NDELAY */
813#endif /* !O_NONBLOCK */
814 status = fcntl(s, F_SETFL, status);
815 }
816 if (status < 0) {
817#ifdef DEBUG_HTTP
818 perror("nonblocking");
819#endif
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000820 __xmlIOErr(XML_FROM_HTTP, 0, "error setting non-blocking IO\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000821 closesocket(s);
822 return(-1);
823 }
Daniel Veillard254b1262003-11-01 17:04:58 +0000824#endif /* !__BEOS__ */
Owen Taylor3473f882001-02-23 17:55:21 +0000825#endif /* !VMS */
826#endif /* !_WINSOCKAPI_ */
827
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000828 if (connect (s, addr, addrlen) == -1) {
Owen Taylor3473f882001-02-23 17:55:21 +0000829 switch (socket_errno()) {
830 case EINPROGRESS:
831 case EWOULDBLOCK:
832 break;
833 default:
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000834 __xmlIOErr(XML_FROM_HTTP, 0, "error connecting to HTTP server");
Owen Taylor3473f882001-02-23 17:55:21 +0000835 closesocket(s);
836 return(-1);
837 }
838 }
839
840 tv.tv_sec = timeout;
841 tv.tv_usec = 0;
842
843 FD_ZERO(&wfd);
844 FD_SET(s, &wfd);
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000845
846#ifdef _WINSOCKAPI_
847 FD_ZERO(&xfd);
848 FD_SET(s, &xfd);
Owen Taylor3473f882001-02-23 17:55:21 +0000849
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000850 switch(select(s+1, NULL, &wfd, &xfd, &tv))
851#else
Owen Taylor3473f882001-02-23 17:55:21 +0000852 switch(select(s+1, NULL, &wfd, NULL, &tv))
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000853#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000854 {
855 case 0:
856 /* Time out */
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000857 __xmlIOErr(XML_FROM_HTTP, 0, "Connect attempt timed out");
Owen Taylor3473f882001-02-23 17:55:21 +0000858 closesocket(s);
859 return(-1);
860 case -1:
861 /* Ermm.. ?? */
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000862 __xmlIOErr(XML_FROM_HTTP, 0, "Connect failed");
Owen Taylor3473f882001-02-23 17:55:21 +0000863 closesocket(s);
864 return(-1);
865 }
866
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000867 if ( FD_ISSET(s, &wfd)
868#ifdef _WINSOCKAPI_
869 || FD_ISSET(s, &xfd)
870#endif
871 ) {
Daniel Veillardc284c642005-03-31 10:24:24 +0000872 XML_SOCKLEN_T len;
Owen Taylor3473f882001-02-23 17:55:21 +0000873 len = sizeof(status);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000874#ifdef SO_ERROR
Owen Taylor3473f882001-02-23 17:55:21 +0000875 if (getsockopt(s, SOL_SOCKET, SO_ERROR, (char*)&status, &len) < 0 ) {
876 /* Solaris error code */
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000877 __xmlIOErr(XML_FROM_HTTP, 0, "getsockopt failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000878 return (-1);
879 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000880#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000881 if ( status ) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000882 __xmlIOErr(XML_FROM_HTTP, 0, "Error connecting to remote host");
Owen Taylor3473f882001-02-23 17:55:21 +0000883 closesocket(s);
884 errno = status;
885 return (-1);
886 }
887 } else {
888 /* pbm */
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000889 __xmlIOErr(XML_FROM_HTTP, 0, "select failed\n");
Daniel Veillardf012a642001-07-23 19:10:52 +0000890 closesocket(s);
Owen Taylor3473f882001-02-23 17:55:21 +0000891 return (-1);
892 }
893
894 return(s);
895}
896
897/**
898 * xmlNanoHTTPConnectHost:
899 * @host: the host name
900 * @port: the port number
901 *
902 * Attempt a connection to the given host:port endpoint. It tries
903 * the multiple IP provided by the DNS if available.
904 *
905 * Returns -1 in case of failure, the file descriptor number otherwise
906 */
907
908static int
909xmlNanoHTTPConnectHost(const char *host, int port)
910{
911 struct hostent *h;
Daniel Veillard2db8c122003-07-08 12:16:59 +0000912 struct sockaddr *addr = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +0000913 struct in_addr ia;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000914 struct sockaddr_in sockin;
Daniel Veillard5c396542002-03-15 07:57:50 +0000915
Owen Taylor3473f882001-02-23 17:55:21 +0000916#ifdef SUPPORT_IP6
917 struct in6_addr ia6;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000918 struct sockaddr_in6 sockin6;
Owen Taylor3473f882001-02-23 17:55:21 +0000919#endif
920 int i;
921 int s;
Daniel Veillard5c396542002-03-15 07:57:50 +0000922
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000923 memset (&sockin, 0, sizeof(sockin));
924#ifdef SUPPORT_IP6
925 memset (&sockin6, 0, sizeof(sockin6));
926 if (have_ipv6 ())
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000927#if !defined(HAVE_GETADDRINFO) && defined(RES_USE_INET6)
Daniel Veillard560c2a42003-07-06 21:13:49 +0000928 {
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000929 if (!(_res.options & RES_INIT))
930 res_init();
931 _res.options |= RES_USE_INET6;
932 }
933#elif defined(HAVE_GETADDRINFO)
Daniel Veillard560c2a42003-07-06 21:13:49 +0000934 {
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000935 int status;
936 struct addrinfo hints, *res, *result;
937
938 result = NULL;
939 memset (&hints, 0,sizeof(hints));
940 hints.ai_socktype = SOCK_STREAM;
941
942 status = getaddrinfo (host, NULL, &hints, &result);
943 if (status) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000944 __xmlIOErr(XML_FROM_HTTP, 0, "getaddrinfo failed\n");
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000945 return (-1);
946 }
947
948 for (res = result; res; res = res->ai_next) {
Daniel Veillard3dc93a42003-07-10 14:04:33 +0000949 if (res->ai_family == AF_INET || res->ai_family == AF_INET6) {
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000950 if (res->ai_family == AF_INET6) {
Daniel Veillard8e2c9792004-10-27 09:39:50 +0000951 if (res->ai_addrlen > sizeof(sockin6)) {
952 __xmlIOErr(XML_FROM_HTTP, 0, "address size mismatch\n");
953 freeaddrinfo (result);
954 return (-1);
955 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000956 memcpy (&sockin6, res->ai_addr, res->ai_addrlen);
957 sockin6.sin6_port = htons (port);
958 addr = (struct sockaddr *)&sockin6;
959 }
Daniel Veillard3dc93a42003-07-10 14:04:33 +0000960 else {
Daniel Veillard8e2c9792004-10-27 09:39:50 +0000961 if (res->ai_addrlen > sizeof(sockin)) {
962 __xmlIOErr(XML_FROM_HTTP, 0, "address size mismatch\n");
963 freeaddrinfo (result);
964 return (-1);
965 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000966 memcpy (&sockin, res->ai_addr, res->ai_addrlen);
967 sockin.sin_port = htons (port);
968 addr = (struct sockaddr *)&sockin;
969 }
970
971 s = xmlNanoHTTPConnectAttempt (addr);
972 if (s != -1) {
973 freeaddrinfo (result);
974 return (s);
975 }
976 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000977 }
Daniel Veillard3dc93a42003-07-10 14:04:33 +0000978 if (result)
979 freeaddrinfo (result);
980 return (-1);
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000981 } else
Owen Taylor3473f882001-02-23 17:55:21 +0000982#endif
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000983#endif
984 {
985 h = gethostbyname (host);
986 if (h == NULL) {
Daniel Veillard56b2db72002-03-25 16:35:28 +0000987
988/*
989 * Okay, I got fed up by the non-portability of this error message
990 * extraction code. it work on Linux, if it work on your platform
991 * and one want to enable it, send me the defined(foobar) needed
992 */
993#if defined(HAVE_NETDB_H) && defined(HOST_NOT_FOUND) && defined(linux)
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000994 const char *h_err_txt = "";
Daniel Veillardf012a642001-07-23 19:10:52 +0000995
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000996 switch (h_errno) {
997 case HOST_NOT_FOUND:
998 h_err_txt = "Authoritive host not found";
999 break;
Daniel Veillardf012a642001-07-23 19:10:52 +00001000
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001001 case TRY_AGAIN:
1002 h_err_txt =
1003 "Non-authoritive host not found or server failure.";
1004 break;
Daniel Veillardf012a642001-07-23 19:10:52 +00001005
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001006 case NO_RECOVERY:
1007 h_err_txt =
1008 "Non-recoverable errors: FORMERR, REFUSED, or NOTIMP.";
1009 break;
Daniel Veillard5c396542002-03-15 07:57:50 +00001010
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001011 case NO_ADDRESS:
1012 h_err_txt =
1013 "Valid name, no data record of requested type.";
1014 break;
Daniel Veillard5c396542002-03-15 07:57:50 +00001015
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001016 default:
1017 h_err_txt = "No error text defined.";
1018 break;
1019 }
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001020 __xmlIOErr(XML_FROM_HTTP, 0, h_err_txt);
Daniel Veillard5c396542002-03-15 07:57:50 +00001021#else
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001022 __xmlIOErr(XML_FROM_HTTP, 0, "Failed to resolve host");
Owen Taylor3473f882001-02-23 17:55:21 +00001023#endif
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001024 return (-1);
1025 }
Daniel Veillard5c396542002-03-15 07:57:50 +00001026
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001027 for (i = 0; h->h_addr_list[i]; i++) {
1028 if (h->h_addrtype == AF_INET) {
1029 /* A records (IPv4) */
Daniel Veillard8e2c9792004-10-27 09:39:50 +00001030 if ((unsigned int) h->h_length > sizeof(ia)) {
1031 __xmlIOErr(XML_FROM_HTTP, 0, "address size mismatch\n");
1032 return (-1);
1033 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001034 memcpy (&ia, h->h_addr_list[i], h->h_length);
1035 sockin.sin_family = h->h_addrtype;
1036 sockin.sin_addr = ia;
1037 sockin.sin_port = htons (port);
1038 addr = (struct sockaddr *) &sockin;
Daniel Veillard5c396542002-03-15 07:57:50 +00001039#ifdef SUPPORT_IP6
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001040 } else if (have_ipv6 () && (h->h_addrtype == AF_INET6)) {
1041 /* AAAA records (IPv6) */
Daniel Veillard8e2c9792004-10-27 09:39:50 +00001042 if ((unsigned int) h->h_length > sizeof(ia6)) {
1043 __xmlIOErr(XML_FROM_HTTP, 0, "address size mismatch\n");
1044 return (-1);
1045 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001046 memcpy (&ia6, h->h_addr_list[i], h->h_length);
1047 sockin6.sin6_family = h->h_addrtype;
1048 sockin6.sin6_addr = ia6;
1049 sockin6.sin6_port = htons (port);
1050 addr = (struct sockaddr *) &sockin6;
Daniel Veillard5c396542002-03-15 07:57:50 +00001051#endif
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001052 } else
1053 break; /* for */
Daniel Veillard5c396542002-03-15 07:57:50 +00001054
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001055 s = xmlNanoHTTPConnectAttempt (addr);
1056 if (s != -1)
1057 return (s);
1058 }
Owen Taylor3473f882001-02-23 17:55:21 +00001059 }
Owen Taylor3473f882001-02-23 17:55:21 +00001060#ifdef DEBUG_HTTP
1061 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard5c396542002-03-15 07:57:50 +00001062 "xmlNanoHTTPConnectHost: unable to connect to '%s'.\n",
1063 host);
Owen Taylor3473f882001-02-23 17:55:21 +00001064#endif
Daniel Veillard5c396542002-03-15 07:57:50 +00001065 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001066}
1067
1068
1069/**
1070 * xmlNanoHTTPOpen:
1071 * @URL: The URL to load
1072 * @contentType: if available the Content-Type information will be
1073 * returned at that location
1074 *
1075 * This function try to open a connection to the indicated resource
1076 * via HTTP GET.
1077 *
1078 * Returns NULL in case of failure, otherwise a request handler.
1079 * The contentType, if provided must be freed by the caller
1080 */
1081
1082void*
1083xmlNanoHTTPOpen(const char *URL, char **contentType) {
1084 if (contentType != NULL) *contentType = NULL;
Daniel Veillardf012a642001-07-23 19:10:52 +00001085 return(xmlNanoHTTPMethod(URL, NULL, NULL, contentType, NULL, 0));
Daniel Veillard9403a042001-05-28 11:00:53 +00001086}
1087
1088/**
1089 * xmlNanoHTTPOpenRedir:
1090 * @URL: The URL to load
1091 * @contentType: if available the Content-Type information will be
1092 * returned at that location
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001093 * @redir: if available the redirected URL will be returned
Daniel Veillard9403a042001-05-28 11:00:53 +00001094 *
1095 * This function try to open a connection to the indicated resource
1096 * via HTTP GET.
1097 *
1098 * Returns NULL in case of failure, otherwise a request handler.
1099 * The contentType, if provided must be freed by the caller
1100 */
1101
1102void*
1103xmlNanoHTTPOpenRedir(const char *URL, char **contentType, char **redir) {
1104 if (contentType != NULL) *contentType = NULL;
1105 if (redir != NULL) *redir = NULL;
Daniel Veillardf012a642001-07-23 19:10:52 +00001106 return(xmlNanoHTTPMethodRedir(URL, NULL, NULL, contentType, redir, NULL,0));
Owen Taylor3473f882001-02-23 17:55:21 +00001107}
1108
1109/**
1110 * xmlNanoHTTPRead:
1111 * @ctx: the HTTP context
1112 * @dest: a buffer
1113 * @len: the buffer length
1114 *
1115 * This function tries to read @len bytes from the existing HTTP connection
1116 * and saves them in @dest. This is a blocking call.
1117 *
1118 * Returns the number of byte read. 0 is an indication of an end of connection.
1119 * -1 indicates a parameter error.
1120 */
1121int
1122xmlNanoHTTPRead(void *ctx, void *dest, int len) {
1123 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1124
1125 if (ctx == NULL) return(-1);
1126 if (dest == NULL) return(-1);
1127 if (len <= 0) return(0);
1128
1129 while (ctxt->inptr - ctxt->inrptr < len) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001130 if (xmlNanoHTTPRecv(ctxt) <= 0) break;
Owen Taylor3473f882001-02-23 17:55:21 +00001131 }
1132 if (ctxt->inptr - ctxt->inrptr < len)
1133 len = ctxt->inptr - ctxt->inrptr;
1134 memcpy(dest, ctxt->inrptr, len);
1135 ctxt->inrptr += len;
1136 return(len);
1137}
1138
1139/**
1140 * xmlNanoHTTPClose:
1141 * @ctx: the HTTP context
1142 *
1143 * This function closes an HTTP context, it ends up the connection and
1144 * free all data related to it.
1145 */
1146void
1147xmlNanoHTTPClose(void *ctx) {
1148 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1149
1150 if (ctx == NULL) return;
1151
1152 xmlNanoHTTPFreeCtxt(ctxt);
1153}
1154
1155/**
Daniel Veillard9403a042001-05-28 11:00:53 +00001156 * xmlNanoHTTPMethodRedir:
Owen Taylor3473f882001-02-23 17:55:21 +00001157 * @URL: The URL to load
1158 * @method: the HTTP method to use
1159 * @input: the input string if any
1160 * @contentType: the Content-Type information IN and OUT
Daniel Veillard9403a042001-05-28 11:00:53 +00001161 * @redir: the redirected URL OUT
Owen Taylor3473f882001-02-23 17:55:21 +00001162 * @headers: the extra headers
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001163 * @ilen: input length
Owen Taylor3473f882001-02-23 17:55:21 +00001164 *
1165 * This function try to open a connection to the indicated resource
1166 * via HTTP using the given @method, adding the given extra headers
1167 * and the input buffer for the request content.
1168 *
1169 * Returns NULL in case of failure, otherwise a request handler.
Daniel Veillard9403a042001-05-28 11:00:53 +00001170 * The contentType, or redir, if provided must be freed by the caller
Owen Taylor3473f882001-02-23 17:55:21 +00001171 */
1172
1173void*
Daniel Veillard9403a042001-05-28 11:00:53 +00001174xmlNanoHTTPMethodRedir(const char *URL, const char *method, const char *input,
Daniel Veillardf012a642001-07-23 19:10:52 +00001175 char **contentType, char **redir,
1176 const char *headers, int ilen ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001177 xmlNanoHTTPCtxtPtr ctxt;
1178 char *bp, *p;
Daniel Veillardf012a642001-07-23 19:10:52 +00001179 int blen, ret;
Owen Taylor3473f882001-02-23 17:55:21 +00001180 int head;
1181 int nbRedirects = 0;
1182 char *redirURL = NULL;
William M. Brack78637da2003-07-31 14:47:38 +00001183#ifdef DEBUG_HTTP
1184 int xmt_bytes;
1185#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001186
1187 if (URL == NULL) return(NULL);
1188 if (method == NULL) method = "GET";
1189 xmlNanoHTTPInit();
1190
1191retry:
1192 if (redirURL == NULL)
1193 ctxt = xmlNanoHTTPNewCtxt(URL);
1194 else {
1195 ctxt = xmlNanoHTTPNewCtxt(redirURL);
Daniel Veillarda840b692003-10-19 13:35:37 +00001196 ctxt->location = xmlMemStrdup(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001197 }
1198
Daniel Veillardf012a642001-07-23 19:10:52 +00001199 if ( ctxt == NULL ) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001200 return ( NULL );
1201 }
1202
Owen Taylor3473f882001-02-23 17:55:21 +00001203 if ((ctxt->protocol == NULL) || (strcmp(ctxt->protocol, "http"))) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001204 __xmlIOErr(XML_FROM_HTTP, XML_HTTP_URL_SYNTAX, "Not a valid HTTP URI");
Owen Taylor3473f882001-02-23 17:55:21 +00001205 xmlNanoHTTPFreeCtxt(ctxt);
1206 if (redirURL != NULL) xmlFree(redirURL);
1207 return(NULL);
1208 }
1209 if (ctxt->hostname == NULL) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001210 __xmlIOErr(XML_FROM_HTTP, XML_HTTP_UNKNOWN_HOST,
1211 "Failed to identify host in URI");
Owen Taylor3473f882001-02-23 17:55:21 +00001212 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001213 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001214 return(NULL);
1215 }
1216 if (proxy) {
1217 blen = strlen(ctxt->hostname) * 2 + 16;
1218 ret = xmlNanoHTTPConnectHost(proxy, proxyPort);
1219 }
1220 else {
1221 blen = strlen(ctxt->hostname);
1222 ret = xmlNanoHTTPConnectHost(ctxt->hostname, ctxt->port);
1223 }
1224 if (ret < 0) {
1225 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001226 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001227 return(NULL);
1228 }
1229 ctxt->fd = ret;
1230
Daniel Veillardf012a642001-07-23 19:10:52 +00001231 if (input == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00001232 ilen = 0;
Daniel Veillardf012a642001-07-23 19:10:52 +00001233 else
1234 blen += 36;
1235
Owen Taylor3473f882001-02-23 17:55:21 +00001236 if (headers != NULL)
Daniel Veillardf012a642001-07-23 19:10:52 +00001237 blen += strlen(headers) + 2;
Owen Taylor3473f882001-02-23 17:55:21 +00001238 if (contentType && *contentType)
1239 blen += strlen(*contentType) + 16;
Daniel Veillard351f2d62005-04-13 02:55:12 +00001240 if (ctxt->query != NULL)
1241 blen += strlen(ctxt->query) + 1;
Daniel Veillardf012a642001-07-23 19:10:52 +00001242 blen += strlen(method) + strlen(ctxt->path) + 24;
Daniel Veillard82cb3192003-10-29 13:39:15 +00001243 bp = (char*)xmlMallocAtomic(blen);
Daniel Veillardf012a642001-07-23 19:10:52 +00001244 if ( bp == NULL ) {
1245 xmlNanoHTTPFreeCtxt( ctxt );
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001246 xmlHTTPErrMemory("allocating header buffer");
Daniel Veillardf012a642001-07-23 19:10:52 +00001247 return ( NULL );
1248 }
1249
1250 p = bp;
1251
Owen Taylor3473f882001-02-23 17:55:21 +00001252 if (proxy) {
1253 if (ctxt->port != 80) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001254 p += snprintf( p, blen - (p - bp), "%s http://%s:%d%s",
1255 method, ctxt->hostname,
Daniel Veillardf012a642001-07-23 19:10:52 +00001256 ctxt->port, ctxt->path );
Owen Taylor3473f882001-02-23 17:55:21 +00001257 }
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001258 else
1259 p += snprintf( p, blen - (p - bp), "%s http://%s%s", method,
Daniel Veillardf012a642001-07-23 19:10:52 +00001260 ctxt->hostname, ctxt->path);
Owen Taylor3473f882001-02-23 17:55:21 +00001261 }
1262 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001263 p += snprintf( p, blen - (p - bp), "%s %s", method, ctxt->path);
Daniel Veillardf012a642001-07-23 19:10:52 +00001264
Daniel Veillard351f2d62005-04-13 02:55:12 +00001265 if (ctxt->query != NULL)
1266 p += snprintf( p, blen - (p - bp), "?%s", ctxt->query);
1267
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001268 p += snprintf( p, blen - (p - bp), " HTTP/1.0\r\nHost: %s\r\n",
1269 ctxt->hostname);
Daniel Veillardf012a642001-07-23 19:10:52 +00001270
1271 if (contentType != NULL && *contentType)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001272 p += snprintf(p, blen - (p - bp), "Content-Type: %s\r\n", *contentType);
Daniel Veillardf012a642001-07-23 19:10:52 +00001273
1274 if (headers != NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001275 p += snprintf( p, blen - (p - bp), "%s", headers );
Daniel Veillardf012a642001-07-23 19:10:52 +00001276
Owen Taylor3473f882001-02-23 17:55:21 +00001277 if (input != NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001278 snprintf(p, blen - (p - bp), "Content-Length: %d\r\n\r\n", ilen );
Owen Taylor3473f882001-02-23 17:55:21 +00001279 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001280 snprintf(p, blen - (p - bp), "\r\n");
Daniel Veillardf012a642001-07-23 19:10:52 +00001281
Owen Taylor3473f882001-02-23 17:55:21 +00001282#ifdef DEBUG_HTTP
1283 xmlGenericError(xmlGenericErrorContext,
1284 "-> %s%s", proxy? "(Proxy) " : "", bp);
1285 if ((blen -= strlen(bp)+1) < 0)
1286 xmlGenericError(xmlGenericErrorContext,
1287 "ERROR: overflowed buffer by %d bytes\n", -blen);
1288#endif
1289 ctxt->outptr = ctxt->out = bp;
1290 ctxt->state = XML_NANO_HTTP_WRITE;
Daniel Veillardf012a642001-07-23 19:10:52 +00001291 blen = strlen( ctxt->out );
Daniel Veillardf012a642001-07-23 19:10:52 +00001292#ifdef DEBUG_HTTP
William M. Brack78637da2003-07-31 14:47:38 +00001293 xmt_bytes = xmlNanoHTTPSend(ctxt, ctxt->out, blen );
Daniel Veillardf012a642001-07-23 19:10:52 +00001294 if ( xmt_bytes != blen )
1295 xmlGenericError( xmlGenericErrorContext,
1296 "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
1297 xmt_bytes, blen,
1298 "bytes of HTTP headers sent to host",
1299 ctxt->hostname );
William M. Brack78637da2003-07-31 14:47:38 +00001300#else
1301 xmlNanoHTTPSend(ctxt, ctxt->out, blen );
Daniel Veillardf012a642001-07-23 19:10:52 +00001302#endif
1303
1304 if ( input != NULL ) {
William M. Brack78637da2003-07-31 14:47:38 +00001305#ifdef DEBUG_HTTP
Daniel Veillardf012a642001-07-23 19:10:52 +00001306 xmt_bytes = xmlNanoHTTPSend( ctxt, input, ilen );
1307
Daniel Veillardf012a642001-07-23 19:10:52 +00001308 if ( xmt_bytes != ilen )
1309 xmlGenericError( xmlGenericErrorContext,
1310 "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
1311 xmt_bytes, ilen,
1312 "bytes of HTTP content sent to host",
1313 ctxt->hostname );
William M. Brack78637da2003-07-31 14:47:38 +00001314#else
1315 xmlNanoHTTPSend( ctxt, input, ilen );
Daniel Veillardf012a642001-07-23 19:10:52 +00001316#endif
1317 }
1318
Owen Taylor3473f882001-02-23 17:55:21 +00001319 ctxt->state = XML_NANO_HTTP_READ;
1320 head = 1;
1321
1322 while ((p = xmlNanoHTTPReadLine(ctxt)) != NULL) {
1323 if (head && (*p == 0)) {
1324 head = 0;
1325 ctxt->content = ctxt->inrptr;
1326 xmlFree(p);
1327 break;
1328 }
1329 xmlNanoHTTPScanAnswer(ctxt, p);
1330
1331#ifdef DEBUG_HTTP
1332 xmlGenericError(xmlGenericErrorContext, "<- %s\n", p);
1333#endif
1334 xmlFree(p);
1335 }
1336
1337 if ((ctxt->location != NULL) && (ctxt->returnValue >= 300) &&
1338 (ctxt->returnValue < 400)) {
1339#ifdef DEBUG_HTTP
1340 xmlGenericError(xmlGenericErrorContext,
1341 "\nRedirect to: %s\n", ctxt->location);
1342#endif
Daniel Veillardf012a642001-07-23 19:10:52 +00001343 while ( xmlNanoHTTPRecv(ctxt) > 0 ) ;
Owen Taylor3473f882001-02-23 17:55:21 +00001344 if (nbRedirects < XML_NANO_HTTP_MAX_REDIR) {
1345 nbRedirects++;
Daniel Veillard9403a042001-05-28 11:00:53 +00001346 if (redirURL != NULL)
1347 xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001348 redirURL = xmlMemStrdup(ctxt->location);
1349 xmlNanoHTTPFreeCtxt(ctxt);
1350 goto retry;
1351 }
1352 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001353 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001354#ifdef DEBUG_HTTP
1355 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardf012a642001-07-23 19:10:52 +00001356 "xmlNanoHTTPMethodRedir: Too many redirects, aborting ...\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001357#endif
1358 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001359 }
1360
1361 if (contentType != NULL) {
1362 if (ctxt->contentType != NULL)
1363 *contentType = xmlMemStrdup(ctxt->contentType);
1364 else
1365 *contentType = NULL;
1366 }
1367
Daniel Veillard9403a042001-05-28 11:00:53 +00001368 if ((redir != NULL) && (redirURL != NULL)) {
1369 *redir = redirURL;
1370 } else {
1371 if (redirURL != NULL)
1372 xmlFree(redirURL);
1373 if (redir != NULL)
1374 *redir = NULL;
1375 }
1376
Owen Taylor3473f882001-02-23 17:55:21 +00001377#ifdef DEBUG_HTTP
1378 if (ctxt->contentType != NULL)
1379 xmlGenericError(xmlGenericErrorContext,
1380 "\nCode %d, content-type '%s'\n\n",
1381 ctxt->returnValue, ctxt->contentType);
1382 else
1383 xmlGenericError(xmlGenericErrorContext,
1384 "\nCode %d, no content-type\n\n",
1385 ctxt->returnValue);
1386#endif
1387
1388 return((void *) ctxt);
1389}
1390
1391/**
Daniel Veillard9403a042001-05-28 11:00:53 +00001392 * xmlNanoHTTPMethod:
1393 * @URL: The URL to load
1394 * @method: the HTTP method to use
1395 * @input: the input string if any
1396 * @contentType: the Content-Type information IN and OUT
1397 * @headers: the extra headers
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001398 * @ilen: input length
Daniel Veillard9403a042001-05-28 11:00:53 +00001399 *
1400 * This function try to open a connection to the indicated resource
1401 * via HTTP using the given @method, adding the given extra headers
1402 * and the input buffer for the request content.
1403 *
1404 * Returns NULL in case of failure, otherwise a request handler.
1405 * The contentType, if provided must be freed by the caller
1406 */
1407
1408void*
1409xmlNanoHTTPMethod(const char *URL, const char *method, const char *input,
Daniel Veillardf012a642001-07-23 19:10:52 +00001410 char **contentType, const char *headers, int ilen) {
Daniel Veillard9403a042001-05-28 11:00:53 +00001411 return(xmlNanoHTTPMethodRedir(URL, method, input, contentType,
Daniel Veillardf012a642001-07-23 19:10:52 +00001412 NULL, headers, ilen));
Daniel Veillard9403a042001-05-28 11:00:53 +00001413}
1414
1415/**
Owen Taylor3473f882001-02-23 17:55:21 +00001416 * xmlNanoHTTPFetch:
1417 * @URL: The URL to load
1418 * @filename: the filename where the content should be saved
1419 * @contentType: if available the Content-Type information will be
1420 * returned at that location
1421 *
1422 * This function try to fetch the indicated resource via HTTP GET
1423 * and save it's content in the file.
1424 *
1425 * Returns -1 in case of failure, 0 incase of success. The contentType,
1426 * if provided must be freed by the caller
1427 */
1428int
1429xmlNanoHTTPFetch(const char *URL, const char *filename, char **contentType) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001430 void *ctxt = NULL;
1431 char *buf = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001432 int fd;
1433 int len;
1434
William M. Brack015ccb22005-02-13 08:18:52 +00001435 if (filename == NULL) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001436 ctxt = xmlNanoHTTPOpen(URL, contentType);
1437 if (ctxt == NULL) return(-1);
1438
1439 if (!strcmp(filename, "-"))
1440 fd = 0;
1441 else {
1442 fd = open(filename, O_CREAT | O_WRONLY, 00644);
1443 if (fd < 0) {
1444 xmlNanoHTTPClose(ctxt);
1445 if ((contentType != NULL) && (*contentType != NULL)) {
1446 xmlFree(*contentType);
1447 *contentType = NULL;
1448 }
1449 return(-1);
1450 }
1451 }
1452
Daniel Veillardf012a642001-07-23 19:10:52 +00001453 xmlNanoHTTPFetchContent( ctxt, &buf, &len );
1454 if ( len > 0 ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001455 write(fd, buf, len);
1456 }
1457
1458 xmlNanoHTTPClose(ctxt);
1459 close(fd);
1460 return(0);
1461}
1462
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001463#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001464/**
1465 * xmlNanoHTTPSave:
1466 * @ctxt: the HTTP context
1467 * @filename: the filename where the content should be saved
1468 *
1469 * This function saves the output of the HTTP transaction to a file
1470 * It closes and free the context at the end
1471 *
1472 * Returns -1 in case of failure, 0 incase of success.
1473 */
1474int
1475xmlNanoHTTPSave(void *ctxt, const char *filename) {
Daniel Veillarde3924972001-07-25 20:25:21 +00001476 char *buf = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001477 int fd;
1478 int len;
1479
William M. Brack015ccb22005-02-13 08:18:52 +00001480 if ((ctxt == NULL) || (filename == NULL)) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001481
1482 if (!strcmp(filename, "-"))
1483 fd = 0;
1484 else {
1485 fd = open(filename, O_CREAT | O_WRONLY);
1486 if (fd < 0) {
1487 xmlNanoHTTPClose(ctxt);
1488 return(-1);
1489 }
1490 }
1491
Daniel Veillardf012a642001-07-23 19:10:52 +00001492 xmlNanoHTTPFetchContent( ctxt, &buf, &len );
1493 if ( len > 0 ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001494 write(fd, buf, len);
1495 }
1496
1497 xmlNanoHTTPClose(ctxt);
William M. Brack20d82362004-03-17 08:44:46 +00001498 close(fd);
Owen Taylor3473f882001-02-23 17:55:21 +00001499 return(0);
1500}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001501#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001502
1503/**
1504 * xmlNanoHTTPReturnCode:
1505 * @ctx: the HTTP context
1506 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001507 * Get the latest HTTP return code received
1508 *
Owen Taylor3473f882001-02-23 17:55:21 +00001509 * Returns the HTTP return code for the request.
1510 */
1511int
1512xmlNanoHTTPReturnCode(void *ctx) {
1513 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1514
1515 if (ctxt == NULL) return(-1);
1516
1517 return(ctxt->returnValue);
1518}
1519
1520/**
1521 * xmlNanoHTTPAuthHeader:
1522 * @ctx: the HTTP context
1523 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001524 * Get the authentication header of an HTTP context
1525 *
Owen Taylor3473f882001-02-23 17:55:21 +00001526 * Returns the stashed value of the WWW-Authenticate or Proxy-Authenticate
1527 * header.
1528 */
1529const char *
1530xmlNanoHTTPAuthHeader(void *ctx) {
1531 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1532
1533 if (ctxt == NULL) return(NULL);
1534
1535 return(ctxt->authHeader);
1536}
1537
Daniel Veillardf012a642001-07-23 19:10:52 +00001538/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00001539 * xmlNanoHTTPContentLength:
Daniel Veillardf012a642001-07-23 19:10:52 +00001540 * @ctx: the HTTP context
1541 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001542 * Provides the specified content length from the HTTP header.
1543 *
Daniel Veillardf012a642001-07-23 19:10:52 +00001544 * Return the specified content length from the HTTP header. Note that
1545 * a value of -1 indicates that the content length element was not included in
1546 * the response header.
1547 */
1548int
1549xmlNanoHTTPContentLength( void * ctx ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001550 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillardf012a642001-07-23 19:10:52 +00001551
1552 return ( ( ctxt == NULL ) ? -1 : ctxt->ContentLength );
1553}
1554
1555/**
Daniel Veillard847332a2003-10-18 11:29:40 +00001556 * xmlNanoHTTPRedir:
1557 * @ctx: the HTTP context
1558 *
1559 * Provides the specified redirection URL if available from the HTTP header.
1560 *
1561 * Return the specified redirection URL or NULL if not redirected.
1562 */
1563const char *
1564xmlNanoHTTPRedir( void * ctx ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001565 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillard847332a2003-10-18 11:29:40 +00001566
1567 return ( ( ctxt == NULL ) ? NULL : ctxt->location );
1568}
1569
1570/**
1571 * xmlNanoHTTPEncoding:
1572 * @ctx: the HTTP context
1573 *
1574 * Provides the specified encoding if specified in the HTTP headers.
1575 *
1576 * Return the specified encoding or NULL if not available
1577 */
1578const char *
1579xmlNanoHTTPEncoding( void * ctx ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001580 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillard847332a2003-10-18 11:29:40 +00001581
1582 return ( ( ctxt == NULL ) ? NULL : ctxt->encoding );
1583}
1584
1585/**
Daniel Veillarda840b692003-10-19 13:35:37 +00001586 * xmlNanoHTTPMimeType:
1587 * @ctx: the HTTP context
1588 *
1589 * Provides the specified Mime-Type if specified in the HTTP headers.
1590 *
1591 * Return the specified Mime-Type or NULL if not available
1592 */
1593const char *
1594xmlNanoHTTPMimeType( void * ctx ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001595 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillarda840b692003-10-19 13:35:37 +00001596
1597 return ( ( ctxt == NULL ) ? NULL : ctxt->mimeType );
1598}
1599
1600/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00001601 * xmlNanoHTTPFetchContent:
Daniel Veillardf012a642001-07-23 19:10:52 +00001602 * @ctx: the HTTP context
1603 * @ptr: pointer to set to the content buffer.
1604 * @len: integer pointer to hold the length of the content
1605 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001606 * Check if all the content was read
1607 *
Daniel Veillardf012a642001-07-23 19:10:52 +00001608 * Returns 0 if all the content was read and available, returns
1609 * -1 if received content length was less than specified or an error
1610 * occurred.
1611 */
Daniel Veillarda2351322004-06-27 12:08:10 +00001612static int
Daniel Veillardf012a642001-07-23 19:10:52 +00001613xmlNanoHTTPFetchContent( void * ctx, char ** ptr, int * len ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001614 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillardf012a642001-07-23 19:10:52 +00001615
1616 int rc = 0;
1617 int cur_lgth;
1618 int rcvd_lgth;
1619 int dummy_int;
1620 char * dummy_ptr = NULL;
1621
1622 /* Dummy up return input parameters if not provided */
1623
1624 if ( len == NULL )
1625 len = &dummy_int;
1626
1627 if ( ptr == NULL )
1628 ptr = &dummy_ptr;
1629
1630 /* But can't work without the context pointer */
1631
1632 if ( ( ctxt == NULL ) || ( ctxt->content == NULL ) ) {
1633 *len = 0;
1634 *ptr = NULL;
1635 return ( -1 );
1636 }
1637
1638 rcvd_lgth = ctxt->inptr - ctxt->content;
1639
1640 while ( (cur_lgth = xmlNanoHTTPRecv( ctxt )) > 0 ) {
1641
1642 rcvd_lgth += cur_lgth;
1643 if ( (ctxt->ContentLength > 0) && (rcvd_lgth >= ctxt->ContentLength) )
1644 break;
1645 }
1646
1647 *ptr = ctxt->content;
1648 *len = rcvd_lgth;
1649
1650 if ( ( ctxt->ContentLength > 0 ) && ( rcvd_lgth < ctxt->ContentLength ) )
1651 rc = -1;
1652 else if ( rcvd_lgth == 0 )
1653 rc = -1;
1654
1655 return ( rc );
1656}
1657
Owen Taylor3473f882001-02-23 17:55:21 +00001658#ifdef STANDALONE
1659int main(int argc, char **argv) {
1660 char *contentType = NULL;
1661
1662 if (argv[1] != NULL) {
1663 if (argv[2] != NULL)
1664 xmlNanoHTTPFetch(argv[1], argv[2], &contentType);
1665 else
1666 xmlNanoHTTPFetch(argv[1], "-", &contentType);
1667 if (contentType != NULL) xmlFree(contentType);
1668 } else {
1669 xmlGenericError(xmlGenericErrorContext,
1670 "%s: minimal HTTP GET implementation\n", argv[0]);
1671 xmlGenericError(xmlGenericErrorContext,
1672 "\tusage %s [ URL [ filename ] ]\n", argv[0]);
1673 }
1674 xmlNanoHTTPCleanup();
1675 xmlMemoryDump();
1676 return(0);
1677}
1678#endif /* STANDALONE */
1679#else /* !LIBXML_HTTP_ENABLED */
1680#ifdef STANDALONE
1681#include <stdio.h>
1682int main(int argc, char **argv) {
1683 xmlGenericError(xmlGenericErrorContext,
1684 "%s : HTTP support not compiled in\n", argv[0]);
1685 return(0);
1686}
1687#endif /* STANDALONE */
1688#endif /* LIBXML_HTTP_ENABLED */
Daniel Veillard5d4644e2005-04-01 13:11:58 +00001689#define bottom_nanohttp
1690#include "elfgcchack.h"