blob: c02136a8d1276028383d5c7dae3ed0028f1b1e32 [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>
72#define SOCKLEN_T unsigned int
73#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>
81#undef SOCKLEN_T
82#define SOCKLEN_T unsigned int
83#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 Veillard75be0132002-03-13 10:03:35 +0000110#ifndef SOCKLEN_T
111#define SOCKLEN_T unsigned int
112#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 */
137 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 Veillardf012a642001-07-23 19:10:52 +0000148 int ContentLength; /* specified content length from HTTP header */
Owen Taylor3473f882001-02-23 17:55:21 +0000149 char *contentType; /* the MIME type for the input */
150 char *location; /* the new URL in case of redirect */
151 char *authHeader; /* contents of {WWW,Proxy}-Authenticate header */
152} xmlNanoHTTPCtxt, *xmlNanoHTTPCtxtPtr;
153
154static int initialized = 0;
155static char *proxy = NULL; /* the proxy name if any */
156static int proxyPort; /* the proxy port if any */
157static unsigned int timeout = 60;/* the select() timeout in seconds */
158
Daniel Veillardf012a642001-07-23 19:10:52 +0000159int xmlNanoHTTPFetchContent( void * ctx, char ** ptr, int * len );
160int xmlNanoHTTPContentLength( void * ctx );
161
Owen Taylor3473f882001-02-23 17:55:21 +0000162/**
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000163 * xmlHTTPErrMemory:
164 * @extra: extra informations
165 *
166 * Handle an out of memory condition
167 */
168static void
169xmlHTTPErrMemory(const char *extra)
170{
171 __xmlSimpleError(XML_FROM_HTTP, XML_ERR_NO_MEMORY, NULL, NULL, extra);
172}
173
174/**
Owen Taylor3473f882001-02-23 17:55:21 +0000175 * A portability function
176 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000177static int socket_errno(void) {
Owen Taylor3473f882001-02-23 17:55:21 +0000178#ifdef _WINSOCKAPI_
179 return(WSAGetLastError());
180#else
181 return(errno);
182#endif
183}
184
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000185#ifdef SUPPORT_IP6
Daniel Veillard2db8c122003-07-08 12:16:59 +0000186static
187int have_ipv6(void) {
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000188 int s;
189
190 s = socket (AF_INET6, SOCK_STREAM, 0);
191 if (s != -1) {
192 close (s);
193 return (1);
194 }
195 return (0);
196}
197#endif
198
Owen Taylor3473f882001-02-23 17:55:21 +0000199/**
200 * xmlNanoHTTPInit:
201 *
202 * Initialize the HTTP protocol layer.
203 * Currently it just checks for proxy informations
204 */
205
206void
207xmlNanoHTTPInit(void) {
208 const char *env;
209#ifdef _WINSOCKAPI_
210 WSADATA wsaData;
211#endif
212
213 if (initialized)
214 return;
215
216#ifdef _WINSOCKAPI_
217 if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0)
218 return;
219#endif
220
221 if (proxy == NULL) {
222 proxyPort = 80;
223 env = getenv("no_proxy");
224 if (env != NULL)
225 goto done;
226 env = getenv("http_proxy");
227 if (env != NULL) {
228 xmlNanoHTTPScanProxy(env);
229 goto done;
230 }
231 env = getenv("HTTP_PROXY");
232 if (env != NULL) {
233 xmlNanoHTTPScanProxy(env);
234 goto done;
235 }
236 }
237done:
238 initialized = 1;
239}
240
241/**
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000242 * xmlNanoHTTPCleanup:
Owen Taylor3473f882001-02-23 17:55:21 +0000243 *
244 * Cleanup the HTTP protocol layer.
245 */
246
247void
248xmlNanoHTTPCleanup(void) {
249 if (proxy != NULL)
250 xmlFree(proxy);
251#ifdef _WINSOCKAPI_
252 if (initialized)
253 WSACleanup();
254#endif
255 initialized = 0;
256 return;
257}
258
259/**
Owen Taylor3473f882001-02-23 17:55:21 +0000260 * xmlNanoHTTPScanURL:
261 * @ctxt: an HTTP context
262 * @URL: The URL used to initialize the context
263 *
264 * (Re)Initialize an HTTP context by parsing the URL and finding
265 * the protocol host port and path it indicates.
266 */
267
268static void
269xmlNanoHTTPScanURL(xmlNanoHTTPCtxtPtr ctxt, const char *URL) {
270 const char *cur = URL;
271 char buf[4096];
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000272 int indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000273 int port = 0;
274
275 if (ctxt->protocol != NULL) {
276 xmlFree(ctxt->protocol);
277 ctxt->protocol = NULL;
278 }
279 if (ctxt->hostname != NULL) {
280 xmlFree(ctxt->hostname);
281 ctxt->hostname = NULL;
282 }
283 if (ctxt->path != NULL) {
284 xmlFree(ctxt->path);
285 ctxt->path = NULL;
286 }
287 if (URL == NULL) return;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000288 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000289 while (*cur != 0) {
290 if ((cur[0] == ':') && (cur[1] == '/') && (cur[2] == '/')) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000291 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000292 ctxt->protocol = xmlMemStrdup(buf);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000293 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000294 cur += 3;
295 break;
296 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000297 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000298 }
299 if (*cur == 0) return;
300
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000301 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000302 while (1) {
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000303 if ((strchr (cur, '[') && !strchr (cur, ']')) ||
304 (!strchr (cur, '[') && strchr (cur, ']'))) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000305 __xmlIOErr(XML_FROM_HTTP, XML_HTTP_URL_SYNTAX,
306 "Syntax Error\n");
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000307 return;
308 }
309
310 if (cur[0] == '[') {
311 cur++;
312 while (cur[0] != ']')
313 buf[indx++] = *cur++;
314
315 if (!strchr (buf, ':')) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000316 __xmlIOErr(XML_FROM_HTTP, XML_HTTP_USE_IP,
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000317 "Use [IPv6]/IPv4 format\n");
318 return;
319 }
320
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000321 buf[indx] = 0;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000322 ctxt->hostname = xmlMemStrdup (buf);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000323 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000324 cur += 1;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000325 if (cur[0] == ':') {
Owen Taylor3473f882001-02-23 17:55:21 +0000326 cur++;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000327 while (*cur >= '0' && *cur <= '9') {
328 port *= 10;
329 port += *cur - '0';
330 cur++;
331 }
332
333 if (port != 0) ctxt->port = port;
334 while ((cur[0] != '/') && (*cur != 0))
335 cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000336 }
Owen Taylor3473f882001-02-23 17:55:21 +0000337 break;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000338 }
339 else {
340 if (cur[0] == ':') {
341 buf[indx] = 0;
342 ctxt->hostname = xmlMemStrdup (buf);
343 indx = 0;
344 cur += 1;
345 while ((*cur >= '0') && (*cur <= '9')) {
346 port *= 10;
347 port += *cur - '0';
348 cur++;
349 }
350 if (port != 0) ctxt->port = port;
351 while ((cur[0] != '/') && (*cur != 0))
352 cur++;
353 break;
354 }
355 if ((*cur == '/') || (*cur == 0)) {
356 buf[indx] = 0;
357 ctxt->hostname = xmlMemStrdup (buf);
358 indx = 0;
359 break;
360 }
Owen Taylor3473f882001-02-23 17:55:21 +0000361 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000362 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000363 }
364 if (*cur == 0)
365 ctxt->path = xmlMemStrdup("/");
366 else {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000367 indx = 0;
368 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000369 while (*cur != 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000370 buf[indx++] = *cur++;
371 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000372 ctxt->path = xmlMemStrdup(buf);
373 }
374}
375
376/**
377 * xmlNanoHTTPScanProxy:
378 * @URL: The proxy URL used to initialize the proxy context
379 *
380 * (Re)Initialize the HTTP Proxy context by parsing the URL and finding
381 * the protocol host port it indicates.
382 * Should be like http://myproxy/ or http://myproxy:3128/
383 * A NULL URL cleans up proxy informations.
384 */
385
386void
387xmlNanoHTTPScanProxy(const char *URL) {
388 const char *cur = URL;
389 char buf[4096];
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000390 int indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000391 int port = 0;
392
393 if (proxy != NULL) {
394 xmlFree(proxy);
395 proxy = NULL;
396 }
397 if (proxyPort != 0) {
398 proxyPort = 0;
399 }
400#ifdef DEBUG_HTTP
401 if (URL == NULL)
402 xmlGenericError(xmlGenericErrorContext,
403 "Removing HTTP proxy info\n");
404 else
405 xmlGenericError(xmlGenericErrorContext,
406 "Using HTTP proxy %s\n", URL);
407#endif
408 if (URL == NULL) return;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000409 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000410 while (*cur != 0) {
411 if ((cur[0] == ':') && (cur[1] == '/') && (cur[2] == '/')) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000412 buf[indx] = 0;
413 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000414 cur += 3;
415 break;
416 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000417 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000418 }
419 if (*cur == 0) return;
420
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000421 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000422 while (1) {
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000423 if ((strchr (cur, '[') && !strchr (cur, ']')) ||
424 (!strchr (cur, '[') && strchr (cur, ']'))) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000425 __xmlIOErr(XML_FROM_HTTP, XML_HTTP_URL_SYNTAX, "Syntax Error\n");
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000426 return;
427 }
428
429 if (cur[0] == '[') {
430 cur++;
431 while (cur[0] != ']')
432 buf[indx++] = *cur++;
433
434 if (!strchr (buf, ':')) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000435 __xmlIOErr(XML_FROM_HTTP, XML_HTTP_USE_IP,
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000436 "Use [IPv6]/IPv4 format\n");
437 return;
438 }
439
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000440 buf[indx] = 0;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000441 proxy = xmlMemStrdup (buf);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000442 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000443 cur += 1;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000444 if (cur[0] == ':') {
Owen Taylor3473f882001-02-23 17:55:21 +0000445 cur++;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000446 while (*cur >= '0' && *cur <= '9') {
447 port *= 10;
448 port += *cur - '0';
449 cur++;
450 }
451
452 if (port != 0) proxyPort = port;
453 while ((cur[0] != '/') && (*cur != 0))
454 cur ++;
455 }
Owen Taylor3473f882001-02-23 17:55:21 +0000456 break;
457 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000458 else {
459 if (cur[0] == ':') {
460 buf[indx] = 0;
461 proxy = xmlMemStrdup (buf);
462 indx = 0;
463 cur += 1;
464 while ((*cur >= '0') && (*cur <= '9')) {
465 port *= 10;
466 port += *cur - '0';
467 cur++;
468 }
469 if (port != 0) proxyPort = port;
470 while ((cur[0] != '/') && (*cur != 0))
471 cur++;
472 break;
473 }
474 if ((*cur == '/') || (*cur == 0)) {
475 buf[indx] = 0;
476 proxy = xmlMemStrdup (buf);
477 indx = 0;
478 break;
479 }
Owen Taylor3473f882001-02-23 17:55:21 +0000480 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000481 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000482 }
483}
484
485/**
486 * xmlNanoHTTPNewCtxt:
487 * @URL: The URL used to initialize the context
488 *
489 * Allocate and initialize a new HTTP context.
490 *
491 * Returns an HTTP context or NULL in case of error.
492 */
493
494static xmlNanoHTTPCtxtPtr
495xmlNanoHTTPNewCtxt(const char *URL) {
496 xmlNanoHTTPCtxtPtr ret;
497
498 ret = (xmlNanoHTTPCtxtPtr) xmlMalloc(sizeof(xmlNanoHTTPCtxt));
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000499 if (ret == NULL) {
500 xmlHTTPErrMemory("allocating context");
501 return(NULL);
502 }
Owen Taylor3473f882001-02-23 17:55:21 +0000503
504 memset(ret, 0, sizeof(xmlNanoHTTPCtxt));
505 ret->port = 80;
506 ret->returnValue = 0;
507 ret->fd = -1;
Daniel Veillardf012a642001-07-23 19:10:52 +0000508 ret->ContentLength = -1;
Owen Taylor3473f882001-02-23 17:55:21 +0000509
Daniel Veillardcacbe5d2003-01-10 16:09:51 +0000510 xmlNanoHTTPScanURL(ret, URL);
Owen Taylor3473f882001-02-23 17:55:21 +0000511
512 return(ret);
513}
514
515/**
516 * xmlNanoHTTPFreeCtxt:
517 * @ctxt: an HTTP context
518 *
519 * Frees the context after closing the connection.
520 */
521
522static void
523xmlNanoHTTPFreeCtxt(xmlNanoHTTPCtxtPtr ctxt) {
524 if (ctxt == NULL) return;
525 if (ctxt->hostname != NULL) xmlFree(ctxt->hostname);
526 if (ctxt->protocol != NULL) xmlFree(ctxt->protocol);
527 if (ctxt->path != NULL) xmlFree(ctxt->path);
528 if (ctxt->out != NULL) xmlFree(ctxt->out);
529 if (ctxt->in != NULL) xmlFree(ctxt->in);
530 if (ctxt->contentType != NULL) xmlFree(ctxt->contentType);
531 if (ctxt->location != NULL) xmlFree(ctxt->location);
532 if (ctxt->authHeader != NULL) xmlFree(ctxt->authHeader);
533 ctxt->state = XML_NANO_HTTP_NONE;
534 if (ctxt->fd >= 0) closesocket(ctxt->fd);
535 ctxt->fd = -1;
536 xmlFree(ctxt);
537}
538
539/**
540 * xmlNanoHTTPSend:
541 * @ctxt: an HTTP context
542 *
543 * Send the input needed to initiate the processing on the server side
Daniel Veillardf012a642001-07-23 19:10:52 +0000544 * Returns number of bytes sent or -1 on error.
Owen Taylor3473f882001-02-23 17:55:21 +0000545 */
546
Daniel Veillardf012a642001-07-23 19:10:52 +0000547static int
548xmlNanoHTTPSend(xmlNanoHTTPCtxtPtr ctxt, const char * xmt_ptr, int outlen) {
549
550 int total_sent = 0;
551
552 if ( (ctxt->state & XML_NANO_HTTP_WRITE) && (xmt_ptr != NULL ) ) {
553 while (total_sent < outlen) {
554 int nsent = send(ctxt->fd, xmt_ptr + total_sent,
555 outlen - total_sent, 0);
Owen Taylor3473f882001-02-23 17:55:21 +0000556 if (nsent>0)
557 total_sent += nsent;
Daniel Veillardf012a642001-07-23 19:10:52 +0000558 else if ( ( nsent == -1 ) &&
Daniel Veillardba6db032001-07-31 16:25:45 +0000559#if defined(EAGAIN) && EAGAIN != EWOULDBLOCK
Daniel Veillardf012a642001-07-23 19:10:52 +0000560 ( socket_errno( ) != EAGAIN ) &&
Daniel Veillardba6db032001-07-31 16:25:45 +0000561#endif
Daniel Veillardf012a642001-07-23 19:10:52 +0000562 ( socket_errno( ) != EWOULDBLOCK ) ) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000563 __xmlIOErr(XML_FROM_HTTP, 0, "send failed\n");
Daniel Veillardf012a642001-07-23 19:10:52 +0000564 if ( total_sent == 0 )
565 total_sent = -1;
566 break;
567 }
568 else {
569 /*
570 ** No data sent
571 ** Since non-blocking sockets are used, wait for
572 ** socket to be writable or default timeout prior
573 ** to retrying.
574 */
575
576 struct timeval tv;
577 fd_set wfd;
578
579 tv.tv_sec = timeout;
580 tv.tv_usec = 0;
581 FD_ZERO( &wfd );
582 FD_SET( ctxt->fd, &wfd );
583 (void)select( ctxt->fd + 1, NULL, &wfd, NULL, &tv );
584 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000585 }
Owen Taylor3473f882001-02-23 17:55:21 +0000586 }
Daniel Veillardf012a642001-07-23 19:10:52 +0000587
588 return total_sent;
Owen Taylor3473f882001-02-23 17:55:21 +0000589}
590
591/**
592 * xmlNanoHTTPRecv:
593 * @ctxt: an HTTP context
594 *
595 * Read information coming from the HTTP connection.
596 * This is a blocking call (but it blocks in select(), not read()).
597 *
598 * Returns the number of byte read or -1 in case of error.
599 */
600
601static int
602xmlNanoHTTPRecv(xmlNanoHTTPCtxtPtr ctxt) {
603 fd_set rfd;
604 struct timeval tv;
605
606
607 while (ctxt->state & XML_NANO_HTTP_READ) {
608 if (ctxt->in == NULL) {
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000609 ctxt->in = (char *) xmlMallocAtomic(65000 * sizeof(char));
Owen Taylor3473f882001-02-23 17:55:21 +0000610 if (ctxt->in == NULL) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000611 xmlHTTPErrMemory("allocating input");
Owen Taylor3473f882001-02-23 17:55:21 +0000612 ctxt->last = -1;
613 return(-1);
614 }
615 ctxt->inlen = 65000;
616 ctxt->inptr = ctxt->content = ctxt->inrptr = ctxt->in;
617 }
618 if (ctxt->inrptr > ctxt->in + XML_NANO_HTTP_CHUNK) {
619 int delta = ctxt->inrptr - ctxt->in;
620 int len = ctxt->inptr - ctxt->inrptr;
621
622 memmove(ctxt->in, ctxt->inrptr, len);
623 ctxt->inrptr -= delta;
624 ctxt->content -= delta;
625 ctxt->inptr -= delta;
626 }
627 if ((ctxt->in + ctxt->inlen) < (ctxt->inptr + XML_NANO_HTTP_CHUNK)) {
628 int d_inptr = ctxt->inptr - ctxt->in;
629 int d_content = ctxt->content - ctxt->in;
630 int d_inrptr = ctxt->inrptr - ctxt->in;
Daniel Veillardf012a642001-07-23 19:10:52 +0000631 char * tmp_ptr = ctxt->in;
Owen Taylor3473f882001-02-23 17:55:21 +0000632
633 ctxt->inlen *= 2;
Daniel Veillardf012a642001-07-23 19:10:52 +0000634 ctxt->in = (char *) xmlRealloc(tmp_ptr, ctxt->inlen);
Owen Taylor3473f882001-02-23 17:55:21 +0000635 if (ctxt->in == NULL) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000636 xmlHTTPErrMemory("allocating input buffer");
Daniel Veillardf012a642001-07-23 19:10:52 +0000637 xmlFree( tmp_ptr );
Owen Taylor3473f882001-02-23 17:55:21 +0000638 ctxt->last = -1;
639 return(-1);
640 }
641 ctxt->inptr = ctxt->in + d_inptr;
642 ctxt->content = ctxt->in + d_content;
643 ctxt->inrptr = ctxt->in + d_inrptr;
644 }
645 ctxt->last = recv(ctxt->fd, ctxt->inptr, XML_NANO_HTTP_CHUNK, 0);
646 if (ctxt->last > 0) {
647 ctxt->inptr += ctxt->last;
648 return(ctxt->last);
649 }
650 if (ctxt->last == 0) {
651 return(0);
652 }
653 if (ctxt->last == -1) {
654 switch (socket_errno()) {
655 case EINPROGRESS:
656 case EWOULDBLOCK:
657#if defined(EAGAIN) && EAGAIN != EWOULDBLOCK
658 case EAGAIN:
659#endif
660 break;
Daniel Veillardf012a642001-07-23 19:10:52 +0000661
662 case ECONNRESET:
663 case ESHUTDOWN:
664 return ( 0 );
665
Owen Taylor3473f882001-02-23 17:55:21 +0000666 default:
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000667 __xmlIOErr(XML_FROM_HTTP, 0, "recv failed\n");
Daniel Veillardf012a642001-07-23 19:10:52 +0000668 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +0000669 }
670 }
671
672 tv.tv_sec = timeout;
673 tv.tv_usec = 0;
674 FD_ZERO(&rfd);
675 FD_SET(ctxt->fd, &rfd);
676
Daniel Veillard50f34372001-08-03 12:06:36 +0000677 if ( (select(ctxt->fd+1, &rfd, NULL, NULL, &tv)<1)
678#if defined(EINTR)
679 && (errno != EINTR)
680#endif
681 )
Owen Taylor3473f882001-02-23 17:55:21 +0000682 return(0);
683 }
684 return(0);
685}
686
687/**
688 * xmlNanoHTTPReadLine:
689 * @ctxt: an HTTP context
690 *
691 * Read one line in the HTTP server output, usually for extracting
692 * the HTTP protocol informations from the answer header.
693 *
694 * Returns a newly allocated string with a copy of the line, or NULL
695 * which indicate the end of the input.
696 */
697
698static char *
699xmlNanoHTTPReadLine(xmlNanoHTTPCtxtPtr ctxt) {
700 char buf[4096];
701 char *bp = buf;
Daniel Veillardf012a642001-07-23 19:10:52 +0000702 int rc;
Owen Taylor3473f882001-02-23 17:55:21 +0000703
704 while (bp - buf < 4095) {
705 if (ctxt->inrptr == ctxt->inptr) {
Daniel Veillardf012a642001-07-23 19:10:52 +0000706 if ( (rc = xmlNanoHTTPRecv(ctxt)) == 0) {
Owen Taylor3473f882001-02-23 17:55:21 +0000707 if (bp == buf)
708 return(NULL);
709 else
710 *bp = 0;
711 return(xmlMemStrdup(buf));
712 }
Daniel Veillardf012a642001-07-23 19:10:52 +0000713 else if ( rc == -1 ) {
714 return ( NULL );
715 }
Owen Taylor3473f882001-02-23 17:55:21 +0000716 }
717 *bp = *ctxt->inrptr++;
718 if (*bp == '\n') {
719 *bp = 0;
720 return(xmlMemStrdup(buf));
721 }
722 if (*bp != '\r')
723 bp++;
724 }
725 buf[4095] = 0;
726 return(xmlMemStrdup(buf));
727}
728
729
730/**
731 * xmlNanoHTTPScanAnswer:
732 * @ctxt: an HTTP context
733 * @line: an HTTP header line
734 *
735 * Try to extract useful informations from the server answer.
736 * We currently parse and process:
737 * - The HTTP revision/ return code
738 * - The Content-Type
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000739 * - The Location for redirect processing.
Owen Taylor3473f882001-02-23 17:55:21 +0000740 *
741 * Returns -1 in case of failure, the file descriptor number otherwise
742 */
743
744static void
745xmlNanoHTTPScanAnswer(xmlNanoHTTPCtxtPtr ctxt, const char *line) {
746 const char *cur = line;
747
748 if (line == NULL) return;
749
750 if (!strncmp(line, "HTTP/", 5)) {
751 int version = 0;
752 int ret = 0;
753
754 cur += 5;
755 while ((*cur >= '0') && (*cur <= '9')) {
756 version *= 10;
757 version += *cur - '0';
758 cur++;
759 }
760 if (*cur == '.') {
761 cur++;
762 if ((*cur >= '0') && (*cur <= '9')) {
763 version *= 10;
764 version += *cur - '0';
765 cur++;
766 }
767 while ((*cur >= '0') && (*cur <= '9'))
768 cur++;
769 } else
770 version *= 10;
771 if ((*cur != ' ') && (*cur != '\t')) return;
772 while ((*cur == ' ') || (*cur == '\t')) cur++;
773 if ((*cur < '0') || (*cur > '9')) return;
774 while ((*cur >= '0') && (*cur <= '9')) {
775 ret *= 10;
776 ret += *cur - '0';
777 cur++;
778 }
779 if ((*cur != 0) && (*cur != ' ') && (*cur != '\t')) return;
780 ctxt->returnValue = ret;
781 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Content-Type:", 13)) {
782 cur += 13;
783 while ((*cur == ' ') || (*cur == '\t')) cur++;
784 if (ctxt->contentType != NULL)
785 xmlFree(ctxt->contentType);
786 ctxt->contentType = xmlMemStrdup(cur);
787 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"ContentType:", 12)) {
788 cur += 12;
789 if (ctxt->contentType != NULL) return;
790 while ((*cur == ' ') || (*cur == '\t')) cur++;
791 ctxt->contentType = xmlMemStrdup(cur);
792 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Location:", 9)) {
793 cur += 9;
794 while ((*cur == ' ') || (*cur == '\t')) cur++;
795 if (ctxt->location != NULL)
796 xmlFree(ctxt->location);
797 ctxt->location = xmlMemStrdup(cur);
798 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"WWW-Authenticate:", 17)) {
799 cur += 17;
800 while ((*cur == ' ') || (*cur == '\t')) cur++;
801 if (ctxt->authHeader != NULL)
802 xmlFree(ctxt->authHeader);
803 ctxt->authHeader = xmlMemStrdup(cur);
804 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Proxy-Authenticate:", 19)) {
805 cur += 19;
806 while ((*cur == ' ') || (*cur == '\t')) cur++;
807 if (ctxt->authHeader != NULL)
808 xmlFree(ctxt->authHeader);
809 ctxt->authHeader = xmlMemStrdup(cur);
Daniel Veillardf012a642001-07-23 19:10:52 +0000810 } else if ( !xmlStrncasecmp( BAD_CAST line, BAD_CAST"Content-Length:", 15) ) {
811 cur += 15;
812 ctxt->ContentLength = strtol( cur, NULL, 10 );
Owen Taylor3473f882001-02-23 17:55:21 +0000813 }
814}
815
816/**
817 * xmlNanoHTTPConnectAttempt:
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000818 * @addr: a socket address structure
Owen Taylor3473f882001-02-23 17:55:21 +0000819 *
820 * Attempt a connection to the given IP:port endpoint. It forces
821 * non-blocking semantic on the socket, and allow 60 seconds for
822 * the host to answer.
823 *
824 * Returns -1 in case of failure, the file descriptor number otherwise
825 */
826
827static int
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000828xmlNanoHTTPConnectAttempt(struct sockaddr *addr)
Owen Taylor3473f882001-02-23 17:55:21 +0000829{
Owen Taylor3473f882001-02-23 17:55:21 +0000830 fd_set wfd;
831 struct timeval tv;
832 int status;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000833 int addrlen;
834 SOCKET s;
Owen Taylor3473f882001-02-23 17:55:21 +0000835
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000836#ifdef SUPPORT_IP6
837 if (addr->sa_family == AF_INET6) {
838 s = socket (PF_INET6, SOCK_STREAM, IPPROTO_TCP);
839 addrlen = sizeof (struct sockaddr_in6);
840 }
841 else
842#endif
843 {
844 s = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
845 addrlen = sizeof (struct sockaddr_in);
846 }
Owen Taylor3473f882001-02-23 17:55:21 +0000847 if (s==-1) {
848#ifdef DEBUG_HTTP
849 perror("socket");
850#endif
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000851 __xmlIOErr(XML_FROM_HTTP, 0, "socket failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000852 return(-1);
853 }
854
855#ifdef _WINSOCKAPI_
856 {
857 u_long one = 1;
858
859 status = ioctlsocket(s, FIONBIO, &one) == SOCKET_ERROR ? -1 : 0;
860 }
861#else /* _WINSOCKAPI_ */
862#if defined(VMS)
863 {
864 int enable = 1;
865 status = ioctl(s, FIONBIO, &enable);
866 }
867#else /* VMS */
868 if ((status = fcntl(s, F_GETFL, 0)) != -1) {
869#ifdef O_NONBLOCK
870 status |= O_NONBLOCK;
871#else /* O_NONBLOCK */
872#ifdef F_NDELAY
873 status |= F_NDELAY;
874#endif /* F_NDELAY */
875#endif /* !O_NONBLOCK */
876 status = fcntl(s, F_SETFL, status);
877 }
878 if (status < 0) {
879#ifdef DEBUG_HTTP
880 perror("nonblocking");
881#endif
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000882 __xmlIOErr(XML_FROM_HTTP, 0, "error setting non-blocking IO\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000883 closesocket(s);
884 return(-1);
885 }
886#endif /* !VMS */
887#endif /* !_WINSOCKAPI_ */
888
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000889 if (connect (s, addr, addrlen) == -1) {
Owen Taylor3473f882001-02-23 17:55:21 +0000890 switch (socket_errno()) {
891 case EINPROGRESS:
892 case EWOULDBLOCK:
893 break;
894 default:
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000895 __xmlIOErr(XML_FROM_HTTP, 0, "error connecting to HTTP server");
Owen Taylor3473f882001-02-23 17:55:21 +0000896 closesocket(s);
897 return(-1);
898 }
899 }
900
901 tv.tv_sec = timeout;
902 tv.tv_usec = 0;
903
904 FD_ZERO(&wfd);
905 FD_SET(s, &wfd);
906
907 switch(select(s+1, NULL, &wfd, NULL, &tv))
908 {
909 case 0:
910 /* Time out */
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000911 __xmlIOErr(XML_FROM_HTTP, 0, "Connect attempt timed out");
Owen Taylor3473f882001-02-23 17:55:21 +0000912 closesocket(s);
913 return(-1);
914 case -1:
915 /* Ermm.. ?? */
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000916 __xmlIOErr(XML_FROM_HTTP, 0, "Connect failed");
Owen Taylor3473f882001-02-23 17:55:21 +0000917 closesocket(s);
918 return(-1);
919 }
920
921 if ( FD_ISSET(s, &wfd) ) {
922 SOCKLEN_T len;
923 len = sizeof(status);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000924#ifdef SO_ERROR
Owen Taylor3473f882001-02-23 17:55:21 +0000925 if (getsockopt(s, SOL_SOCKET, SO_ERROR, (char*)&status, &len) < 0 ) {
926 /* Solaris error code */
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000927 __xmlIOErr(XML_FROM_HTTP, 0, "getsockopt failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000928 return (-1);
929 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000930#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000931 if ( status ) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000932 __xmlIOErr(XML_FROM_HTTP, 0, "Error connecting to remote host");
Owen Taylor3473f882001-02-23 17:55:21 +0000933 closesocket(s);
934 errno = status;
935 return (-1);
936 }
937 } else {
938 /* pbm */
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000939 __xmlIOErr(XML_FROM_HTTP, 0, "select failed\n");
Daniel Veillardf012a642001-07-23 19:10:52 +0000940 closesocket(s);
Owen Taylor3473f882001-02-23 17:55:21 +0000941 return (-1);
942 }
943
944 return(s);
945}
946
947/**
948 * xmlNanoHTTPConnectHost:
949 * @host: the host name
950 * @port: the port number
951 *
952 * Attempt a connection to the given host:port endpoint. It tries
953 * the multiple IP provided by the DNS if available.
954 *
955 * Returns -1 in case of failure, the file descriptor number otherwise
956 */
957
958static int
959xmlNanoHTTPConnectHost(const char *host, int port)
960{
961 struct hostent *h;
Daniel Veillard2db8c122003-07-08 12:16:59 +0000962 struct sockaddr *addr = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +0000963 struct in_addr ia;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000964 struct sockaddr_in sockin;
Daniel Veillard5c396542002-03-15 07:57:50 +0000965
Owen Taylor3473f882001-02-23 17:55:21 +0000966#ifdef SUPPORT_IP6
967 struct in6_addr ia6;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000968 struct sockaddr_in6 sockin6;
Owen Taylor3473f882001-02-23 17:55:21 +0000969#endif
970 int i;
971 int s;
Daniel Veillard5c396542002-03-15 07:57:50 +0000972
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000973 memset (&sockin, 0, sizeof(sockin));
974#ifdef SUPPORT_IP6
975 memset (&sockin6, 0, sizeof(sockin6));
976 if (have_ipv6 ())
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000977#if !defined(HAVE_GETADDRINFO) && defined(RES_USE_INET6)
Daniel Veillard560c2a42003-07-06 21:13:49 +0000978 {
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000979 if (!(_res.options & RES_INIT))
980 res_init();
981 _res.options |= RES_USE_INET6;
982 }
983#elif defined(HAVE_GETADDRINFO)
Daniel Veillard560c2a42003-07-06 21:13:49 +0000984 {
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000985 int status;
986 struct addrinfo hints, *res, *result;
987
988 result = NULL;
989 memset (&hints, 0,sizeof(hints));
990 hints.ai_socktype = SOCK_STREAM;
991
992 status = getaddrinfo (host, NULL, &hints, &result);
993 if (status) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000994 __xmlIOErr(XML_FROM_HTTP, 0, "getaddrinfo failed\n");
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000995 return (-1);
996 }
997
998 for (res = result; res; res = res->ai_next) {
Daniel Veillard3dc93a42003-07-10 14:04:33 +0000999 if (res->ai_family == AF_INET || res->ai_family == AF_INET6) {
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001000 if (res->ai_family == AF_INET6) {
1001 memcpy (&sockin6, res->ai_addr, res->ai_addrlen);
1002 sockin6.sin6_port = htons (port);
1003 addr = (struct sockaddr *)&sockin6;
1004 }
Daniel Veillard3dc93a42003-07-10 14:04:33 +00001005 else {
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001006 memcpy (&sockin, res->ai_addr, res->ai_addrlen);
1007 sockin.sin_port = htons (port);
1008 addr = (struct sockaddr *)&sockin;
1009 }
1010
1011 s = xmlNanoHTTPConnectAttempt (addr);
1012 if (s != -1) {
1013 freeaddrinfo (result);
1014 return (s);
1015 }
1016 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001017 }
Daniel Veillard3dc93a42003-07-10 14:04:33 +00001018 if (result)
1019 freeaddrinfo (result);
1020 return (-1);
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001021 } else
Owen Taylor3473f882001-02-23 17:55:21 +00001022#endif
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001023#endif
1024 {
1025 h = gethostbyname (host);
1026 if (h == NULL) {
Daniel Veillard56b2db72002-03-25 16:35:28 +00001027
1028/*
1029 * Okay, I got fed up by the non-portability of this error message
1030 * extraction code. it work on Linux, if it work on your platform
1031 * and one want to enable it, send me the defined(foobar) needed
1032 */
1033#if defined(HAVE_NETDB_H) && defined(HOST_NOT_FOUND) && defined(linux)
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001034 const char *h_err_txt = "";
Daniel Veillardf012a642001-07-23 19:10:52 +00001035
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001036 switch (h_errno) {
1037 case HOST_NOT_FOUND:
1038 h_err_txt = "Authoritive host not found";
1039 break;
Daniel Veillardf012a642001-07-23 19:10:52 +00001040
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001041 case TRY_AGAIN:
1042 h_err_txt =
1043 "Non-authoritive host not found or server failure.";
1044 break;
Daniel Veillardf012a642001-07-23 19:10:52 +00001045
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001046 case NO_RECOVERY:
1047 h_err_txt =
1048 "Non-recoverable errors: FORMERR, REFUSED, or NOTIMP.";
1049 break;
Daniel Veillard5c396542002-03-15 07:57:50 +00001050
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001051 case NO_ADDRESS:
1052 h_err_txt =
1053 "Valid name, no data record of requested type.";
1054 break;
Daniel Veillard5c396542002-03-15 07:57:50 +00001055
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001056 default:
1057 h_err_txt = "No error text defined.";
1058 break;
1059 }
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001060 __xmlIOErr(XML_FROM_HTTP, 0, h_err_txt);
Daniel Veillard5c396542002-03-15 07:57:50 +00001061#else
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001062 __xmlIOErr(XML_FROM_HTTP, 0, "Failed to resolve host");
Owen Taylor3473f882001-02-23 17:55:21 +00001063#endif
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001064 return (-1);
1065 }
Daniel Veillard5c396542002-03-15 07:57:50 +00001066
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001067 for (i = 0; h->h_addr_list[i]; i++) {
1068 if (h->h_addrtype == AF_INET) {
1069 /* A records (IPv4) */
1070 memcpy (&ia, h->h_addr_list[i], h->h_length);
1071 sockin.sin_family = h->h_addrtype;
1072 sockin.sin_addr = ia;
1073 sockin.sin_port = htons (port);
1074 addr = (struct sockaddr *) &sockin;
Daniel Veillard5c396542002-03-15 07:57:50 +00001075#ifdef SUPPORT_IP6
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001076 } else if (have_ipv6 () && (h->h_addrtype == AF_INET6)) {
1077 /* AAAA records (IPv6) */
1078 memcpy (&ia6, h->h_addr_list[i], h->h_length);
1079 sockin6.sin6_family = h->h_addrtype;
1080 sockin6.sin6_addr = ia6;
1081 sockin6.sin6_port = htons (port);
1082 addr = (struct sockaddr *) &sockin6;
Daniel Veillard5c396542002-03-15 07:57:50 +00001083#endif
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001084 } else
1085 break; /* for */
Daniel Veillard5c396542002-03-15 07:57:50 +00001086
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001087 s = xmlNanoHTTPConnectAttempt (addr);
1088 if (s != -1)
1089 return (s);
1090 }
Owen Taylor3473f882001-02-23 17:55:21 +00001091 }
Owen Taylor3473f882001-02-23 17:55:21 +00001092#ifdef DEBUG_HTTP
1093 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard5c396542002-03-15 07:57:50 +00001094 "xmlNanoHTTPConnectHost: unable to connect to '%s'.\n",
1095 host);
Owen Taylor3473f882001-02-23 17:55:21 +00001096#endif
Daniel Veillard5c396542002-03-15 07:57:50 +00001097 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001098}
1099
1100
1101/**
1102 * xmlNanoHTTPOpen:
1103 * @URL: The URL to load
1104 * @contentType: if available the Content-Type information will be
1105 * returned at that location
1106 *
1107 * This function try to open a connection to the indicated resource
1108 * via HTTP GET.
1109 *
1110 * Returns NULL in case of failure, otherwise a request handler.
1111 * The contentType, if provided must be freed by the caller
1112 */
1113
1114void*
1115xmlNanoHTTPOpen(const char *URL, char **contentType) {
1116 if (contentType != NULL) *contentType = NULL;
Daniel Veillardf012a642001-07-23 19:10:52 +00001117 return(xmlNanoHTTPMethod(URL, NULL, NULL, contentType, NULL, 0));
Daniel Veillard9403a042001-05-28 11:00:53 +00001118}
1119
1120/**
1121 * xmlNanoHTTPOpenRedir:
1122 * @URL: The URL to load
1123 * @contentType: if available the Content-Type information will be
1124 * returned at that location
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001125 * @redir: if available the redirected URL will be returned
Daniel Veillard9403a042001-05-28 11:00:53 +00001126 *
1127 * This function try to open a connection to the indicated resource
1128 * via HTTP GET.
1129 *
1130 * Returns NULL in case of failure, otherwise a request handler.
1131 * The contentType, if provided must be freed by the caller
1132 */
1133
1134void*
1135xmlNanoHTTPOpenRedir(const char *URL, char **contentType, char **redir) {
1136 if (contentType != NULL) *contentType = NULL;
1137 if (redir != NULL) *redir = NULL;
Daniel Veillardf012a642001-07-23 19:10:52 +00001138 return(xmlNanoHTTPMethodRedir(URL, NULL, NULL, contentType, redir, NULL,0));
Owen Taylor3473f882001-02-23 17:55:21 +00001139}
1140
1141/**
1142 * xmlNanoHTTPRead:
1143 * @ctx: the HTTP context
1144 * @dest: a buffer
1145 * @len: the buffer length
1146 *
1147 * This function tries to read @len bytes from the existing HTTP connection
1148 * and saves them in @dest. This is a blocking call.
1149 *
1150 * Returns the number of byte read. 0 is an indication of an end of connection.
1151 * -1 indicates a parameter error.
1152 */
1153int
1154xmlNanoHTTPRead(void *ctx, void *dest, int len) {
1155 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1156
1157 if (ctx == NULL) return(-1);
1158 if (dest == NULL) return(-1);
1159 if (len <= 0) return(0);
1160
1161 while (ctxt->inptr - ctxt->inrptr < len) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001162 if (xmlNanoHTTPRecv(ctxt) <= 0) break;
Owen Taylor3473f882001-02-23 17:55:21 +00001163 }
1164 if (ctxt->inptr - ctxt->inrptr < len)
1165 len = ctxt->inptr - ctxt->inrptr;
1166 memcpy(dest, ctxt->inrptr, len);
1167 ctxt->inrptr += len;
1168 return(len);
1169}
1170
1171/**
1172 * xmlNanoHTTPClose:
1173 * @ctx: the HTTP context
1174 *
1175 * This function closes an HTTP context, it ends up the connection and
1176 * free all data related to it.
1177 */
1178void
1179xmlNanoHTTPClose(void *ctx) {
1180 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1181
1182 if (ctx == NULL) return;
1183
1184 xmlNanoHTTPFreeCtxt(ctxt);
1185}
1186
1187/**
Daniel Veillard9403a042001-05-28 11:00:53 +00001188 * xmlNanoHTTPMethodRedir:
Owen Taylor3473f882001-02-23 17:55:21 +00001189 * @URL: The URL to load
1190 * @method: the HTTP method to use
1191 * @input: the input string if any
1192 * @contentType: the Content-Type information IN and OUT
Daniel Veillard9403a042001-05-28 11:00:53 +00001193 * @redir: the redirected URL OUT
Owen Taylor3473f882001-02-23 17:55:21 +00001194 * @headers: the extra headers
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001195 * @ilen: input length
Owen Taylor3473f882001-02-23 17:55:21 +00001196 *
1197 * This function try to open a connection to the indicated resource
1198 * via HTTP using the given @method, adding the given extra headers
1199 * and the input buffer for the request content.
1200 *
1201 * Returns NULL in case of failure, otherwise a request handler.
Daniel Veillard9403a042001-05-28 11:00:53 +00001202 * The contentType, or redir, if provided must be freed by the caller
Owen Taylor3473f882001-02-23 17:55:21 +00001203 */
1204
1205void*
Daniel Veillard9403a042001-05-28 11:00:53 +00001206xmlNanoHTTPMethodRedir(const char *URL, const char *method, const char *input,
Daniel Veillardf012a642001-07-23 19:10:52 +00001207 char **contentType, char **redir,
1208 const char *headers, int ilen ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001209 xmlNanoHTTPCtxtPtr ctxt;
1210 char *bp, *p;
Daniel Veillardf012a642001-07-23 19:10:52 +00001211 int blen, ret;
Owen Taylor3473f882001-02-23 17:55:21 +00001212 int head;
1213 int nbRedirects = 0;
1214 char *redirURL = NULL;
William M. Brack78637da2003-07-31 14:47:38 +00001215#ifdef DEBUG_HTTP
1216 int xmt_bytes;
1217#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001218
1219 if (URL == NULL) return(NULL);
1220 if (method == NULL) method = "GET";
1221 xmlNanoHTTPInit();
1222
1223retry:
1224 if (redirURL == NULL)
1225 ctxt = xmlNanoHTTPNewCtxt(URL);
1226 else {
1227 ctxt = xmlNanoHTTPNewCtxt(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001228 }
1229
Daniel Veillardf012a642001-07-23 19:10:52 +00001230 if ( ctxt == NULL ) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001231 return ( NULL );
1232 }
1233
Owen Taylor3473f882001-02-23 17:55:21 +00001234 if ((ctxt->protocol == NULL) || (strcmp(ctxt->protocol, "http"))) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001235 __xmlIOErr(XML_FROM_HTTP, XML_HTTP_URL_SYNTAX, "Not a valid HTTP URI");
Owen Taylor3473f882001-02-23 17:55:21 +00001236 xmlNanoHTTPFreeCtxt(ctxt);
1237 if (redirURL != NULL) xmlFree(redirURL);
1238 return(NULL);
1239 }
1240 if (ctxt->hostname == NULL) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001241 __xmlIOErr(XML_FROM_HTTP, XML_HTTP_UNKNOWN_HOST,
1242 "Failed to identify host in URI");
Owen Taylor3473f882001-02-23 17:55:21 +00001243 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001244 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001245 return(NULL);
1246 }
1247 if (proxy) {
1248 blen = strlen(ctxt->hostname) * 2 + 16;
1249 ret = xmlNanoHTTPConnectHost(proxy, proxyPort);
1250 }
1251 else {
1252 blen = strlen(ctxt->hostname);
1253 ret = xmlNanoHTTPConnectHost(ctxt->hostname, ctxt->port);
1254 }
1255 if (ret < 0) {
1256 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001257 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001258 return(NULL);
1259 }
1260 ctxt->fd = ret;
1261
Daniel Veillardf012a642001-07-23 19:10:52 +00001262 if (input == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00001263 ilen = 0;
Daniel Veillardf012a642001-07-23 19:10:52 +00001264 else
1265 blen += 36;
1266
Owen Taylor3473f882001-02-23 17:55:21 +00001267 if (headers != NULL)
Daniel Veillardf012a642001-07-23 19:10:52 +00001268 blen += strlen(headers) + 2;
Owen Taylor3473f882001-02-23 17:55:21 +00001269 if (contentType && *contentType)
1270 blen += strlen(*contentType) + 16;
Daniel Veillardf012a642001-07-23 19:10:52 +00001271 blen += strlen(method) + strlen(ctxt->path) + 24;
Daniel Veillard3c908dc2003-04-19 00:07:51 +00001272 bp = xmlMallocAtomic(blen);
Daniel Veillardf012a642001-07-23 19:10:52 +00001273 if ( bp == NULL ) {
1274 xmlNanoHTTPFreeCtxt( ctxt );
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001275 xmlHTTPErrMemory("allocating header buffer");
Daniel Veillardf012a642001-07-23 19:10:52 +00001276 return ( NULL );
1277 }
1278
1279 p = bp;
1280
Owen Taylor3473f882001-02-23 17:55:21 +00001281 if (proxy) {
1282 if (ctxt->port != 80) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001283 p += snprintf( p, blen - (p - bp), "%s http://%s:%d%s",
1284 method, ctxt->hostname,
Daniel Veillardf012a642001-07-23 19:10:52 +00001285 ctxt->port, ctxt->path );
Owen Taylor3473f882001-02-23 17:55:21 +00001286 }
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001287 else
1288 p += snprintf( p, blen - (p - bp), "%s http://%s%s", method,
Daniel Veillardf012a642001-07-23 19:10:52 +00001289 ctxt->hostname, ctxt->path);
Owen Taylor3473f882001-02-23 17:55:21 +00001290 }
1291 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001292 p += snprintf( p, blen - (p - bp), "%s %s", method, ctxt->path);
Daniel Veillardf012a642001-07-23 19:10:52 +00001293
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001294 p += snprintf( p, blen - (p - bp), " HTTP/1.0\r\nHost: %s\r\n",
1295 ctxt->hostname);
Daniel Veillardf012a642001-07-23 19:10:52 +00001296
1297 if (contentType != NULL && *contentType)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001298 p += snprintf(p, blen - (p - bp), "Content-Type: %s\r\n", *contentType);
Daniel Veillardf012a642001-07-23 19:10:52 +00001299
1300 if (headers != NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001301 p += snprintf( p, blen - (p - bp), "%s", headers );
Daniel Veillardf012a642001-07-23 19:10:52 +00001302
Owen Taylor3473f882001-02-23 17:55:21 +00001303 if (input != NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001304 snprintf(p, blen - (p - bp), "Content-Length: %d\r\n\r\n", ilen );
Owen Taylor3473f882001-02-23 17:55:21 +00001305 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001306 snprintf(p, blen - (p - bp), "\r\n");
Daniel Veillardf012a642001-07-23 19:10:52 +00001307
Owen Taylor3473f882001-02-23 17:55:21 +00001308#ifdef DEBUG_HTTP
1309 xmlGenericError(xmlGenericErrorContext,
1310 "-> %s%s", proxy? "(Proxy) " : "", bp);
1311 if ((blen -= strlen(bp)+1) < 0)
1312 xmlGenericError(xmlGenericErrorContext,
1313 "ERROR: overflowed buffer by %d bytes\n", -blen);
1314#endif
1315 ctxt->outptr = ctxt->out = bp;
1316 ctxt->state = XML_NANO_HTTP_WRITE;
Daniel Veillardf012a642001-07-23 19:10:52 +00001317 blen = strlen( ctxt->out );
Daniel Veillardf012a642001-07-23 19:10:52 +00001318#ifdef DEBUG_HTTP
William M. Brack78637da2003-07-31 14:47:38 +00001319 xmt_bytes = xmlNanoHTTPSend(ctxt, ctxt->out, blen );
Daniel Veillardf012a642001-07-23 19:10:52 +00001320 if ( xmt_bytes != blen )
1321 xmlGenericError( xmlGenericErrorContext,
1322 "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
1323 xmt_bytes, blen,
1324 "bytes of HTTP headers sent to host",
1325 ctxt->hostname );
William M. Brack78637da2003-07-31 14:47:38 +00001326#else
1327 xmlNanoHTTPSend(ctxt, ctxt->out, blen );
Daniel Veillardf012a642001-07-23 19:10:52 +00001328#endif
1329
1330 if ( input != NULL ) {
William M. Brack78637da2003-07-31 14:47:38 +00001331#ifdef DEBUG_HTTP
Daniel Veillardf012a642001-07-23 19:10:52 +00001332 xmt_bytes = xmlNanoHTTPSend( ctxt, input, ilen );
1333
Daniel Veillardf012a642001-07-23 19:10:52 +00001334 if ( xmt_bytes != ilen )
1335 xmlGenericError( xmlGenericErrorContext,
1336 "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
1337 xmt_bytes, ilen,
1338 "bytes of HTTP content sent to host",
1339 ctxt->hostname );
William M. Brack78637da2003-07-31 14:47:38 +00001340#else
1341 xmlNanoHTTPSend( ctxt, input, ilen );
Daniel Veillardf012a642001-07-23 19:10:52 +00001342#endif
1343 }
1344
Owen Taylor3473f882001-02-23 17:55:21 +00001345 ctxt->state = XML_NANO_HTTP_READ;
1346 head = 1;
1347
1348 while ((p = xmlNanoHTTPReadLine(ctxt)) != NULL) {
1349 if (head && (*p == 0)) {
1350 head = 0;
1351 ctxt->content = ctxt->inrptr;
1352 xmlFree(p);
1353 break;
1354 }
1355 xmlNanoHTTPScanAnswer(ctxt, p);
1356
1357#ifdef DEBUG_HTTP
1358 xmlGenericError(xmlGenericErrorContext, "<- %s\n", p);
1359#endif
1360 xmlFree(p);
1361 }
1362
1363 if ((ctxt->location != NULL) && (ctxt->returnValue >= 300) &&
1364 (ctxt->returnValue < 400)) {
1365#ifdef DEBUG_HTTP
1366 xmlGenericError(xmlGenericErrorContext,
1367 "\nRedirect to: %s\n", ctxt->location);
1368#endif
Daniel Veillardf012a642001-07-23 19:10:52 +00001369 while ( xmlNanoHTTPRecv(ctxt) > 0 ) ;
Owen Taylor3473f882001-02-23 17:55:21 +00001370 if (nbRedirects < XML_NANO_HTTP_MAX_REDIR) {
1371 nbRedirects++;
Daniel Veillard9403a042001-05-28 11:00:53 +00001372 if (redirURL != NULL)
1373 xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001374 redirURL = xmlMemStrdup(ctxt->location);
1375 xmlNanoHTTPFreeCtxt(ctxt);
1376 goto retry;
1377 }
1378 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001379 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001380#ifdef DEBUG_HTTP
1381 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardf012a642001-07-23 19:10:52 +00001382 "xmlNanoHTTPMethodRedir: Too many redirects, aborting ...\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001383#endif
1384 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001385 }
1386
1387 if (contentType != NULL) {
1388 if (ctxt->contentType != NULL)
1389 *contentType = xmlMemStrdup(ctxt->contentType);
1390 else
1391 *contentType = NULL;
1392 }
1393
Daniel Veillard9403a042001-05-28 11:00:53 +00001394 if ((redir != NULL) && (redirURL != NULL)) {
1395 *redir = redirURL;
1396 } else {
1397 if (redirURL != NULL)
1398 xmlFree(redirURL);
1399 if (redir != NULL)
1400 *redir = NULL;
1401 }
1402
Owen Taylor3473f882001-02-23 17:55:21 +00001403#ifdef DEBUG_HTTP
1404 if (ctxt->contentType != NULL)
1405 xmlGenericError(xmlGenericErrorContext,
1406 "\nCode %d, content-type '%s'\n\n",
1407 ctxt->returnValue, ctxt->contentType);
1408 else
1409 xmlGenericError(xmlGenericErrorContext,
1410 "\nCode %d, no content-type\n\n",
1411 ctxt->returnValue);
1412#endif
1413
1414 return((void *) ctxt);
1415}
1416
1417/**
Daniel Veillard9403a042001-05-28 11:00:53 +00001418 * xmlNanoHTTPMethod:
1419 * @URL: The URL to load
1420 * @method: the HTTP method to use
1421 * @input: the input string if any
1422 * @contentType: the Content-Type information IN and OUT
1423 * @headers: the extra headers
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001424 * @ilen: input length
Daniel Veillard9403a042001-05-28 11:00:53 +00001425 *
1426 * This function try to open a connection to the indicated resource
1427 * via HTTP using the given @method, adding the given extra headers
1428 * and the input buffer for the request content.
1429 *
1430 * Returns NULL in case of failure, otherwise a request handler.
1431 * The contentType, if provided must be freed by the caller
1432 */
1433
1434void*
1435xmlNanoHTTPMethod(const char *URL, const char *method, const char *input,
Daniel Veillardf012a642001-07-23 19:10:52 +00001436 char **contentType, const char *headers, int ilen) {
Daniel Veillard9403a042001-05-28 11:00:53 +00001437 return(xmlNanoHTTPMethodRedir(URL, method, input, contentType,
Daniel Veillardf012a642001-07-23 19:10:52 +00001438 NULL, headers, ilen));
Daniel Veillard9403a042001-05-28 11:00:53 +00001439}
1440
1441/**
Owen Taylor3473f882001-02-23 17:55:21 +00001442 * xmlNanoHTTPFetch:
1443 * @URL: The URL to load
1444 * @filename: the filename where the content should be saved
1445 * @contentType: if available the Content-Type information will be
1446 * returned at that location
1447 *
1448 * This function try to fetch the indicated resource via HTTP GET
1449 * and save it's content in the file.
1450 *
1451 * Returns -1 in case of failure, 0 incase of success. The contentType,
1452 * if provided must be freed by the caller
1453 */
1454int
1455xmlNanoHTTPFetch(const char *URL, const char *filename, char **contentType) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001456 void *ctxt = NULL;
1457 char *buf = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001458 int fd;
1459 int len;
1460
1461 ctxt = xmlNanoHTTPOpen(URL, contentType);
1462 if (ctxt == NULL) return(-1);
1463
1464 if (!strcmp(filename, "-"))
1465 fd = 0;
1466 else {
1467 fd = open(filename, O_CREAT | O_WRONLY, 00644);
1468 if (fd < 0) {
1469 xmlNanoHTTPClose(ctxt);
1470 if ((contentType != NULL) && (*contentType != NULL)) {
1471 xmlFree(*contentType);
1472 *contentType = NULL;
1473 }
1474 return(-1);
1475 }
1476 }
1477
Daniel Veillardf012a642001-07-23 19:10:52 +00001478 xmlNanoHTTPFetchContent( ctxt, &buf, &len );
1479 if ( len > 0 ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001480 write(fd, buf, len);
1481 }
1482
1483 xmlNanoHTTPClose(ctxt);
1484 close(fd);
1485 return(0);
1486}
1487
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001488#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001489/**
1490 * xmlNanoHTTPSave:
1491 * @ctxt: the HTTP context
1492 * @filename: the filename where the content should be saved
1493 *
1494 * This function saves the output of the HTTP transaction to a file
1495 * It closes and free the context at the end
1496 *
1497 * Returns -1 in case of failure, 0 incase of success.
1498 */
1499int
1500xmlNanoHTTPSave(void *ctxt, const char *filename) {
Daniel Veillarde3924972001-07-25 20:25:21 +00001501 char *buf = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001502 int fd;
1503 int len;
1504
1505 if (ctxt == NULL) return(-1);
1506
1507 if (!strcmp(filename, "-"))
1508 fd = 0;
1509 else {
1510 fd = open(filename, O_CREAT | O_WRONLY);
1511 if (fd < 0) {
1512 xmlNanoHTTPClose(ctxt);
1513 return(-1);
1514 }
1515 }
1516
Daniel Veillardf012a642001-07-23 19:10:52 +00001517 xmlNanoHTTPFetchContent( ctxt, &buf, &len );
1518 if ( len > 0 ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001519 write(fd, buf, len);
1520 }
1521
1522 xmlNanoHTTPClose(ctxt);
1523 return(0);
1524}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001525#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001526
1527/**
1528 * xmlNanoHTTPReturnCode:
1529 * @ctx: the HTTP context
1530 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001531 * Get the latest HTTP return code received
1532 *
Owen Taylor3473f882001-02-23 17:55:21 +00001533 * Returns the HTTP return code for the request.
1534 */
1535int
1536xmlNanoHTTPReturnCode(void *ctx) {
1537 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1538
1539 if (ctxt == NULL) return(-1);
1540
1541 return(ctxt->returnValue);
1542}
1543
1544/**
1545 * xmlNanoHTTPAuthHeader:
1546 * @ctx: the HTTP context
1547 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001548 * Get the authentication header of an HTTP context
1549 *
Owen Taylor3473f882001-02-23 17:55:21 +00001550 * Returns the stashed value of the WWW-Authenticate or Proxy-Authenticate
1551 * header.
1552 */
1553const char *
1554xmlNanoHTTPAuthHeader(void *ctx) {
1555 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1556
1557 if (ctxt == NULL) return(NULL);
1558
1559 return(ctxt->authHeader);
1560}
1561
Daniel Veillardf012a642001-07-23 19:10:52 +00001562/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00001563 * xmlNanoHTTPContentLength:
Daniel Veillardf012a642001-07-23 19:10:52 +00001564 * @ctx: the HTTP context
1565 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001566 * Provides the specified content length from the HTTP header.
1567 *
Daniel Veillardf012a642001-07-23 19:10:52 +00001568 * Return the specified content length from the HTTP header. Note that
1569 * a value of -1 indicates that the content length element was not included in
1570 * the response header.
1571 */
1572int
1573xmlNanoHTTPContentLength( void * ctx ) {
1574 xmlNanoHTTPCtxtPtr ctxt = ctx;
1575
1576 return ( ( ctxt == NULL ) ? -1 : ctxt->ContentLength );
1577}
1578
1579/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00001580 * xmlNanoHTTPFetchContent:
Daniel Veillardf012a642001-07-23 19:10:52 +00001581 * @ctx: the HTTP context
1582 * @ptr: pointer to set to the content buffer.
1583 * @len: integer pointer to hold the length of the content
1584 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001585 * Check if all the content was read
1586 *
Daniel Veillardf012a642001-07-23 19:10:52 +00001587 * Returns 0 if all the content was read and available, returns
1588 * -1 if received content length was less than specified or an error
1589 * occurred.
1590 */
1591int
1592xmlNanoHTTPFetchContent( void * ctx, char ** ptr, int * len ) {
1593 xmlNanoHTTPCtxtPtr ctxt = ctx;
1594
1595 int rc = 0;
1596 int cur_lgth;
1597 int rcvd_lgth;
1598 int dummy_int;
1599 char * dummy_ptr = NULL;
1600
1601 /* Dummy up return input parameters if not provided */
1602
1603 if ( len == NULL )
1604 len = &dummy_int;
1605
1606 if ( ptr == NULL )
1607 ptr = &dummy_ptr;
1608
1609 /* But can't work without the context pointer */
1610
1611 if ( ( ctxt == NULL ) || ( ctxt->content == NULL ) ) {
1612 *len = 0;
1613 *ptr = NULL;
1614 return ( -1 );
1615 }
1616
1617 rcvd_lgth = ctxt->inptr - ctxt->content;
1618
1619 while ( (cur_lgth = xmlNanoHTTPRecv( ctxt )) > 0 ) {
1620
1621 rcvd_lgth += cur_lgth;
1622 if ( (ctxt->ContentLength > 0) && (rcvd_lgth >= ctxt->ContentLength) )
1623 break;
1624 }
1625
1626 *ptr = ctxt->content;
1627 *len = rcvd_lgth;
1628
1629 if ( ( ctxt->ContentLength > 0 ) && ( rcvd_lgth < ctxt->ContentLength ) )
1630 rc = -1;
1631 else if ( rcvd_lgth == 0 )
1632 rc = -1;
1633
1634 return ( rc );
1635}
1636
Owen Taylor3473f882001-02-23 17:55:21 +00001637#ifdef STANDALONE
1638int main(int argc, char **argv) {
1639 char *contentType = NULL;
1640
1641 if (argv[1] != NULL) {
1642 if (argv[2] != NULL)
1643 xmlNanoHTTPFetch(argv[1], argv[2], &contentType);
1644 else
1645 xmlNanoHTTPFetch(argv[1], "-", &contentType);
1646 if (contentType != NULL) xmlFree(contentType);
1647 } else {
1648 xmlGenericError(xmlGenericErrorContext,
1649 "%s: minimal HTTP GET implementation\n", argv[0]);
1650 xmlGenericError(xmlGenericErrorContext,
1651 "\tusage %s [ URL [ filename ] ]\n", argv[0]);
1652 }
1653 xmlNanoHTTPCleanup();
1654 xmlMemoryDump();
1655 return(0);
1656}
1657#endif /* STANDALONE */
1658#else /* !LIBXML_HTTP_ENABLED */
1659#ifdef STANDALONE
1660#include <stdio.h>
1661int main(int argc, char **argv) {
1662 xmlGenericError(xmlGenericErrorContext,
1663 "%s : HTTP support not compiled in\n", argv[0]);
1664 return(0);
1665}
1666#endif /* STANDALONE */
1667#endif /* LIBXML_HTTP_ENABLED */