blob: 673b893e058603fdce2ab614d4bb2d31db9f7dad [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 */
Daniel Veillard847332a2003-10-18 11:29:40 +0000152 char *encoding; /* encoding extracted from the contentType */
Daniel Veillarda840b692003-10-19 13:35:37 +0000153 char *mimeType; /* Mime-Type extracted from the contentType */
Owen Taylor3473f882001-02-23 17:55:21 +0000154} xmlNanoHTTPCtxt, *xmlNanoHTTPCtxtPtr;
155
156static int initialized = 0;
157static char *proxy = NULL; /* the proxy name if any */
158static int proxyPort; /* the proxy port if any */
159static unsigned int timeout = 60;/* the select() timeout in seconds */
160
Daniel Veillardf012a642001-07-23 19:10:52 +0000161int xmlNanoHTTPFetchContent( void * ctx, char ** ptr, int * len );
162int xmlNanoHTTPContentLength( void * ctx );
163
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");
226 if (env != NULL)
227 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) {
272 const char *cur = URL;
273 char buf[4096];
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000274 int indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000275 int port = 0;
276
277 if (ctxt->protocol != NULL) {
278 xmlFree(ctxt->protocol);
279 ctxt->protocol = NULL;
280 }
281 if (ctxt->hostname != NULL) {
282 xmlFree(ctxt->hostname);
283 ctxt->hostname = NULL;
284 }
285 if (ctxt->path != NULL) {
286 xmlFree(ctxt->path);
287 ctxt->path = NULL;
288 }
289 if (URL == NULL) return;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000290 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000291 while (*cur != 0) {
292 if ((cur[0] == ':') && (cur[1] == '/') && (cur[2] == '/')) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000293 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000294 ctxt->protocol = xmlMemStrdup(buf);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000295 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000296 cur += 3;
297 break;
298 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000299 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000300 }
301 if (*cur == 0) return;
302
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000303 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000304 while (1) {
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000305 if ((strchr (cur, '[') && !strchr (cur, ']')) ||
306 (!strchr (cur, '[') && strchr (cur, ']'))) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000307 __xmlIOErr(XML_FROM_HTTP, XML_HTTP_URL_SYNTAX,
308 "Syntax Error\n");
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000309 return;
310 }
311
312 if (cur[0] == '[') {
313 cur++;
314 while (cur[0] != ']')
315 buf[indx++] = *cur++;
316
317 if (!strchr (buf, ':')) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000318 __xmlIOErr(XML_FROM_HTTP, XML_HTTP_USE_IP,
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000319 "Use [IPv6]/IPv4 format\n");
320 return;
321 }
322
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000323 buf[indx] = 0;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000324 ctxt->hostname = xmlMemStrdup (buf);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000325 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000326 cur += 1;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000327 if (cur[0] == ':') {
Owen Taylor3473f882001-02-23 17:55:21 +0000328 cur++;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000329 while (*cur >= '0' && *cur <= '9') {
330 port *= 10;
331 port += *cur - '0';
332 cur++;
333 }
334
335 if (port != 0) ctxt->port = port;
336 while ((cur[0] != '/') && (*cur != 0))
337 cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000338 }
Owen Taylor3473f882001-02-23 17:55:21 +0000339 break;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000340 }
341 else {
342 if (cur[0] == ':') {
343 buf[indx] = 0;
344 ctxt->hostname = xmlMemStrdup (buf);
345 indx = 0;
346 cur += 1;
347 while ((*cur >= '0') && (*cur <= '9')) {
348 port *= 10;
349 port += *cur - '0';
350 cur++;
351 }
352 if (port != 0) ctxt->port = port;
353 while ((cur[0] != '/') && (*cur != 0))
354 cur++;
355 break;
356 }
357 if ((*cur == '/') || (*cur == 0)) {
358 buf[indx] = 0;
359 ctxt->hostname = xmlMemStrdup (buf);
360 indx = 0;
361 break;
362 }
Owen Taylor3473f882001-02-23 17:55:21 +0000363 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000364 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000365 }
366 if (*cur == 0)
367 ctxt->path = xmlMemStrdup("/");
368 else {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000369 indx = 0;
370 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000371 while (*cur != 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000372 buf[indx++] = *cur++;
373 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000374 ctxt->path = xmlMemStrdup(buf);
375 }
376}
377
378/**
379 * xmlNanoHTTPScanProxy:
380 * @URL: The proxy URL used to initialize the proxy context
381 *
382 * (Re)Initialize the HTTP Proxy context by parsing the URL and finding
383 * the protocol host port it indicates.
384 * Should be like http://myproxy/ or http://myproxy:3128/
385 * A NULL URL cleans up proxy informations.
386 */
387
388void
389xmlNanoHTTPScanProxy(const char *URL) {
390 const char *cur = URL;
391 char buf[4096];
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000392 int indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000393 int port = 0;
394
395 if (proxy != NULL) {
396 xmlFree(proxy);
397 proxy = NULL;
398 }
399 if (proxyPort != 0) {
400 proxyPort = 0;
401 }
402#ifdef DEBUG_HTTP
403 if (URL == NULL)
404 xmlGenericError(xmlGenericErrorContext,
405 "Removing HTTP proxy info\n");
406 else
407 xmlGenericError(xmlGenericErrorContext,
408 "Using HTTP proxy %s\n", URL);
409#endif
410 if (URL == NULL) return;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000411 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000412 while (*cur != 0) {
413 if ((cur[0] == ':') && (cur[1] == '/') && (cur[2] == '/')) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000414 buf[indx] = 0;
415 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000416 cur += 3;
417 break;
418 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000419 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000420 }
421 if (*cur == 0) return;
422
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000423 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000424 while (1) {
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000425 if ((strchr (cur, '[') && !strchr (cur, ']')) ||
426 (!strchr (cur, '[') && strchr (cur, ']'))) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000427 __xmlIOErr(XML_FROM_HTTP, XML_HTTP_URL_SYNTAX, "Syntax Error\n");
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000428 return;
429 }
430
431 if (cur[0] == '[') {
432 cur++;
433 while (cur[0] != ']')
434 buf[indx++] = *cur++;
435
436 if (!strchr (buf, ':')) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000437 __xmlIOErr(XML_FROM_HTTP, XML_HTTP_USE_IP,
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000438 "Use [IPv6]/IPv4 format\n");
439 return;
440 }
441
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000442 buf[indx] = 0;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000443 proxy = xmlMemStrdup (buf);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000444 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000445 cur += 1;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000446 if (cur[0] == ':') {
Owen Taylor3473f882001-02-23 17:55:21 +0000447 cur++;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000448 while (*cur >= '0' && *cur <= '9') {
449 port *= 10;
450 port += *cur - '0';
451 cur++;
452 }
453
454 if (port != 0) proxyPort = port;
455 while ((cur[0] != '/') && (*cur != 0))
456 cur ++;
457 }
Owen Taylor3473f882001-02-23 17:55:21 +0000458 break;
459 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000460 else {
461 if (cur[0] == ':') {
462 buf[indx] = 0;
463 proxy = xmlMemStrdup (buf);
464 indx = 0;
465 cur += 1;
466 while ((*cur >= '0') && (*cur <= '9')) {
467 port *= 10;
468 port += *cur - '0';
469 cur++;
470 }
471 if (port != 0) proxyPort = port;
472 while ((cur[0] != '/') && (*cur != 0))
473 cur++;
474 break;
475 }
476 if ((*cur == '/') || (*cur == 0)) {
477 buf[indx] = 0;
478 proxy = xmlMemStrdup (buf);
479 indx = 0;
480 break;
481 }
Owen Taylor3473f882001-02-23 17:55:21 +0000482 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000483 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000484 }
485}
486
487/**
488 * xmlNanoHTTPNewCtxt:
489 * @URL: The URL used to initialize the context
490 *
491 * Allocate and initialize a new HTTP context.
492 *
493 * Returns an HTTP context or NULL in case of error.
494 */
495
496static xmlNanoHTTPCtxtPtr
497xmlNanoHTTPNewCtxt(const char *URL) {
498 xmlNanoHTTPCtxtPtr ret;
499
500 ret = (xmlNanoHTTPCtxtPtr) xmlMalloc(sizeof(xmlNanoHTTPCtxt));
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000501 if (ret == NULL) {
502 xmlHTTPErrMemory("allocating context");
503 return(NULL);
504 }
Owen Taylor3473f882001-02-23 17:55:21 +0000505
506 memset(ret, 0, sizeof(xmlNanoHTTPCtxt));
507 ret->port = 80;
508 ret->returnValue = 0;
509 ret->fd = -1;
Daniel Veillardf012a642001-07-23 19:10:52 +0000510 ret->ContentLength = -1;
Owen Taylor3473f882001-02-23 17:55:21 +0000511
Daniel Veillardcacbe5d2003-01-10 16:09:51 +0000512 xmlNanoHTTPScanURL(ret, URL);
Owen Taylor3473f882001-02-23 17:55:21 +0000513
514 return(ret);
515}
516
517/**
518 * xmlNanoHTTPFreeCtxt:
519 * @ctxt: an HTTP context
520 *
521 * Frees the context after closing the connection.
522 */
523
524static void
525xmlNanoHTTPFreeCtxt(xmlNanoHTTPCtxtPtr ctxt) {
526 if (ctxt == NULL) return;
527 if (ctxt->hostname != NULL) xmlFree(ctxt->hostname);
528 if (ctxt->protocol != NULL) xmlFree(ctxt->protocol);
529 if (ctxt->path != NULL) xmlFree(ctxt->path);
530 if (ctxt->out != NULL) xmlFree(ctxt->out);
531 if (ctxt->in != NULL) xmlFree(ctxt->in);
532 if (ctxt->contentType != NULL) xmlFree(ctxt->contentType);
Daniel Veillard847332a2003-10-18 11:29:40 +0000533 if (ctxt->encoding != NULL) xmlFree(ctxt->encoding);
Daniel Veillarda840b692003-10-19 13:35:37 +0000534 if (ctxt->mimeType != NULL) xmlFree(ctxt->mimeType);
Owen Taylor3473f882001-02-23 17:55:21 +0000535 if (ctxt->location != NULL) xmlFree(ctxt->location);
536 if (ctxt->authHeader != NULL) xmlFree(ctxt->authHeader);
537 ctxt->state = XML_NANO_HTTP_NONE;
538 if (ctxt->fd >= 0) closesocket(ctxt->fd);
539 ctxt->fd = -1;
540 xmlFree(ctxt);
541}
542
543/**
544 * xmlNanoHTTPSend:
545 * @ctxt: an HTTP context
546 *
547 * Send the input needed to initiate the processing on the server side
Daniel Veillardf012a642001-07-23 19:10:52 +0000548 * Returns number of bytes sent or -1 on error.
Owen Taylor3473f882001-02-23 17:55:21 +0000549 */
550
Daniel Veillardf012a642001-07-23 19:10:52 +0000551static int
552xmlNanoHTTPSend(xmlNanoHTTPCtxtPtr ctxt, const char * xmt_ptr, int outlen) {
553
554 int total_sent = 0;
555
556 if ( (ctxt->state & XML_NANO_HTTP_WRITE) && (xmt_ptr != NULL ) ) {
557 while (total_sent < outlen) {
558 int nsent = send(ctxt->fd, xmt_ptr + total_sent,
559 outlen - total_sent, 0);
Owen Taylor3473f882001-02-23 17:55:21 +0000560 if (nsent>0)
561 total_sent += nsent;
Daniel Veillardf012a642001-07-23 19:10:52 +0000562 else if ( ( nsent == -1 ) &&
Daniel Veillardba6db032001-07-31 16:25:45 +0000563#if defined(EAGAIN) && EAGAIN != EWOULDBLOCK
Daniel Veillardf012a642001-07-23 19:10:52 +0000564 ( socket_errno( ) != EAGAIN ) &&
Daniel Veillardba6db032001-07-31 16:25:45 +0000565#endif
Daniel Veillardf012a642001-07-23 19:10:52 +0000566 ( socket_errno( ) != EWOULDBLOCK ) ) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000567 __xmlIOErr(XML_FROM_HTTP, 0, "send failed\n");
Daniel Veillardf012a642001-07-23 19:10:52 +0000568 if ( total_sent == 0 )
569 total_sent = -1;
570 break;
571 }
572 else {
573 /*
574 ** No data sent
575 ** Since non-blocking sockets are used, wait for
576 ** socket to be writable or default timeout prior
577 ** to retrying.
578 */
579
580 struct timeval tv;
581 fd_set wfd;
582
583 tv.tv_sec = timeout;
584 tv.tv_usec = 0;
585 FD_ZERO( &wfd );
586 FD_SET( ctxt->fd, &wfd );
587 (void)select( ctxt->fd + 1, NULL, &wfd, NULL, &tv );
588 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000589 }
Owen Taylor3473f882001-02-23 17:55:21 +0000590 }
Daniel Veillardf012a642001-07-23 19:10:52 +0000591
592 return total_sent;
Owen Taylor3473f882001-02-23 17:55:21 +0000593}
594
595/**
596 * xmlNanoHTTPRecv:
597 * @ctxt: an HTTP context
598 *
599 * Read information coming from the HTTP connection.
600 * This is a blocking call (but it blocks in select(), not read()).
601 *
602 * Returns the number of byte read or -1 in case of error.
603 */
604
605static int
606xmlNanoHTTPRecv(xmlNanoHTTPCtxtPtr ctxt) {
607 fd_set rfd;
608 struct timeval tv;
609
610
611 while (ctxt->state & XML_NANO_HTTP_READ) {
612 if (ctxt->in == NULL) {
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000613 ctxt->in = (char *) xmlMallocAtomic(65000 * sizeof(char));
Owen Taylor3473f882001-02-23 17:55:21 +0000614 if (ctxt->in == NULL) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000615 xmlHTTPErrMemory("allocating input");
Owen Taylor3473f882001-02-23 17:55:21 +0000616 ctxt->last = -1;
617 return(-1);
618 }
619 ctxt->inlen = 65000;
620 ctxt->inptr = ctxt->content = ctxt->inrptr = ctxt->in;
621 }
622 if (ctxt->inrptr > ctxt->in + XML_NANO_HTTP_CHUNK) {
623 int delta = ctxt->inrptr - ctxt->in;
624 int len = ctxt->inptr - ctxt->inrptr;
625
626 memmove(ctxt->in, ctxt->inrptr, len);
627 ctxt->inrptr -= delta;
628 ctxt->content -= delta;
629 ctxt->inptr -= delta;
630 }
631 if ((ctxt->in + ctxt->inlen) < (ctxt->inptr + XML_NANO_HTTP_CHUNK)) {
632 int d_inptr = ctxt->inptr - ctxt->in;
633 int d_content = ctxt->content - ctxt->in;
634 int d_inrptr = ctxt->inrptr - ctxt->in;
Daniel Veillardf012a642001-07-23 19:10:52 +0000635 char * tmp_ptr = ctxt->in;
Owen Taylor3473f882001-02-23 17:55:21 +0000636
637 ctxt->inlen *= 2;
Daniel Veillardf012a642001-07-23 19:10:52 +0000638 ctxt->in = (char *) xmlRealloc(tmp_ptr, ctxt->inlen);
Owen Taylor3473f882001-02-23 17:55:21 +0000639 if (ctxt->in == NULL) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000640 xmlHTTPErrMemory("allocating input buffer");
Daniel Veillardf012a642001-07-23 19:10:52 +0000641 xmlFree( tmp_ptr );
Owen Taylor3473f882001-02-23 17:55:21 +0000642 ctxt->last = -1;
643 return(-1);
644 }
645 ctxt->inptr = ctxt->in + d_inptr;
646 ctxt->content = ctxt->in + d_content;
647 ctxt->inrptr = ctxt->in + d_inrptr;
648 }
649 ctxt->last = recv(ctxt->fd, ctxt->inptr, XML_NANO_HTTP_CHUNK, 0);
650 if (ctxt->last > 0) {
651 ctxt->inptr += ctxt->last;
652 return(ctxt->last);
653 }
654 if (ctxt->last == 0) {
655 return(0);
656 }
657 if (ctxt->last == -1) {
658 switch (socket_errno()) {
659 case EINPROGRESS:
660 case EWOULDBLOCK:
661#if defined(EAGAIN) && EAGAIN != EWOULDBLOCK
662 case EAGAIN:
663#endif
664 break;
Daniel Veillardf012a642001-07-23 19:10:52 +0000665
666 case ECONNRESET:
667 case ESHUTDOWN:
668 return ( 0 );
669
Owen Taylor3473f882001-02-23 17:55:21 +0000670 default:
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000671 __xmlIOErr(XML_FROM_HTTP, 0, "recv failed\n");
Daniel Veillardf012a642001-07-23 19:10:52 +0000672 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +0000673 }
674 }
675
676 tv.tv_sec = timeout;
677 tv.tv_usec = 0;
678 FD_ZERO(&rfd);
679 FD_SET(ctxt->fd, &rfd);
680
Daniel Veillard50f34372001-08-03 12:06:36 +0000681 if ( (select(ctxt->fd+1, &rfd, NULL, NULL, &tv)<1)
682#if defined(EINTR)
683 && (errno != EINTR)
684#endif
685 )
Owen Taylor3473f882001-02-23 17:55:21 +0000686 return(0);
687 }
688 return(0);
689}
690
691/**
692 * xmlNanoHTTPReadLine:
693 * @ctxt: an HTTP context
694 *
695 * Read one line in the HTTP server output, usually for extracting
696 * the HTTP protocol informations from the answer header.
697 *
698 * Returns a newly allocated string with a copy of the line, or NULL
699 * which indicate the end of the input.
700 */
701
702static char *
703xmlNanoHTTPReadLine(xmlNanoHTTPCtxtPtr ctxt) {
704 char buf[4096];
705 char *bp = buf;
Daniel Veillardf012a642001-07-23 19:10:52 +0000706 int rc;
Owen Taylor3473f882001-02-23 17:55:21 +0000707
708 while (bp - buf < 4095) {
709 if (ctxt->inrptr == ctxt->inptr) {
Daniel Veillardf012a642001-07-23 19:10:52 +0000710 if ( (rc = xmlNanoHTTPRecv(ctxt)) == 0) {
Owen Taylor3473f882001-02-23 17:55:21 +0000711 if (bp == buf)
712 return(NULL);
713 else
714 *bp = 0;
715 return(xmlMemStrdup(buf));
716 }
Daniel Veillardf012a642001-07-23 19:10:52 +0000717 else if ( rc == -1 ) {
718 return ( NULL );
719 }
Owen Taylor3473f882001-02-23 17:55:21 +0000720 }
721 *bp = *ctxt->inrptr++;
722 if (*bp == '\n') {
723 *bp = 0;
724 return(xmlMemStrdup(buf));
725 }
726 if (*bp != '\r')
727 bp++;
728 }
729 buf[4095] = 0;
730 return(xmlMemStrdup(buf));
731}
732
733
734/**
735 * xmlNanoHTTPScanAnswer:
736 * @ctxt: an HTTP context
737 * @line: an HTTP header line
738 *
739 * Try to extract useful informations from the server answer.
740 * We currently parse and process:
741 * - The HTTP revision/ return code
Daniel Veillarda840b692003-10-19 13:35:37 +0000742 * - The Content-Type, Mime-Type and charset used
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000743 * - The Location for redirect processing.
Owen Taylor3473f882001-02-23 17:55:21 +0000744 *
745 * Returns -1 in case of failure, the file descriptor number otherwise
746 */
747
748static void
749xmlNanoHTTPScanAnswer(xmlNanoHTTPCtxtPtr ctxt, const char *line) {
750 const char *cur = line;
751
752 if (line == NULL) return;
753
754 if (!strncmp(line, "HTTP/", 5)) {
755 int version = 0;
756 int ret = 0;
757
758 cur += 5;
759 while ((*cur >= '0') && (*cur <= '9')) {
760 version *= 10;
761 version += *cur - '0';
762 cur++;
763 }
764 if (*cur == '.') {
765 cur++;
766 if ((*cur >= '0') && (*cur <= '9')) {
767 version *= 10;
768 version += *cur - '0';
769 cur++;
770 }
771 while ((*cur >= '0') && (*cur <= '9'))
772 cur++;
773 } else
774 version *= 10;
775 if ((*cur != ' ') && (*cur != '\t')) return;
776 while ((*cur == ' ') || (*cur == '\t')) cur++;
777 if ((*cur < '0') || (*cur > '9')) return;
778 while ((*cur >= '0') && (*cur <= '9')) {
779 ret *= 10;
780 ret += *cur - '0';
781 cur++;
782 }
783 if ((*cur != 0) && (*cur != ' ') && (*cur != '\t')) return;
784 ctxt->returnValue = ret;
785 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Content-Type:", 13)) {
Daniel Veillarda840b692003-10-19 13:35:37 +0000786 const xmlChar *charset, *last, *mime;
Owen Taylor3473f882001-02-23 17:55:21 +0000787 cur += 13;
788 while ((*cur == ' ') || (*cur == '\t')) cur++;
789 if (ctxt->contentType != NULL)
790 xmlFree(ctxt->contentType);
791 ctxt->contentType = xmlMemStrdup(cur);
Daniel Veillarda840b692003-10-19 13:35:37 +0000792 mime = (const xmlChar *) cur;
793 last = mime;
794 while ((*last != 0) && (*last != ' ') && (*last != '\t') &&
795 (*last != ';') && (*last != ','))
796 last++;
797 if (ctxt->mimeType != NULL)
798 xmlFree(ctxt->mimeType);
799 ctxt->mimeType = (char *) xmlStrndup(mime, last - mime);
800 charset = xmlStrstr(BAD_CAST ctxt->contentType, BAD_CAST "charset=");
801 if (charset != NULL) {
802 charset += 8;
803 last = charset;
804 while ((*last != 0) && (*last != ' ') && (*last != '\t') &&
805 (*last != ';') && (*last != ','))
806 last++;
807 if (ctxt->encoding != NULL)
808 xmlFree(ctxt->encoding);
809 ctxt->encoding = (char *) xmlStrndup(charset, last - charset);
810 }
Owen Taylor3473f882001-02-23 17:55:21 +0000811 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"ContentType:", 12)) {
Daniel Veillarda840b692003-10-19 13:35:37 +0000812 const xmlChar *charset, *last, *mime;
Owen Taylor3473f882001-02-23 17:55:21 +0000813 cur += 12;
814 if (ctxt->contentType != NULL) return;
815 while ((*cur == ' ') || (*cur == '\t')) cur++;
816 ctxt->contentType = xmlMemStrdup(cur);
Daniel Veillarda840b692003-10-19 13:35:37 +0000817 mime = (const xmlChar *) cur;
818 last = mime;
819 while ((*last != 0) && (*last != ' ') && (*last != '\t') &&
820 (*last != ';') && (*last != ','))
821 last++;
822 if (ctxt->mimeType != NULL)
823 xmlFree(ctxt->mimeType);
824 ctxt->mimeType = (char *) xmlStrndup(mime, last - mime);
825 charset = xmlStrstr(BAD_CAST ctxt->contentType, BAD_CAST "charset=");
826 if (charset != NULL) {
827 charset += 8;
828 last = charset;
829 while ((*last != 0) && (*last != ' ') && (*last != '\t') &&
830 (*last != ';') && (*last != ','))
831 last++;
832 if (ctxt->encoding != NULL)
833 xmlFree(ctxt->encoding);
834 ctxt->encoding = (char *) xmlStrndup(charset, last - charset);
835 }
Owen Taylor3473f882001-02-23 17:55:21 +0000836 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Location:", 9)) {
837 cur += 9;
838 while ((*cur == ' ') || (*cur == '\t')) cur++;
839 if (ctxt->location != NULL)
840 xmlFree(ctxt->location);
841 ctxt->location = xmlMemStrdup(cur);
842 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"WWW-Authenticate:", 17)) {
843 cur += 17;
844 while ((*cur == ' ') || (*cur == '\t')) cur++;
845 if (ctxt->authHeader != NULL)
846 xmlFree(ctxt->authHeader);
847 ctxt->authHeader = xmlMemStrdup(cur);
848 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Proxy-Authenticate:", 19)) {
849 cur += 19;
850 while ((*cur == ' ') || (*cur == '\t')) cur++;
851 if (ctxt->authHeader != NULL)
852 xmlFree(ctxt->authHeader);
853 ctxt->authHeader = xmlMemStrdup(cur);
Daniel Veillardf012a642001-07-23 19:10:52 +0000854 } else if ( !xmlStrncasecmp( BAD_CAST line, BAD_CAST"Content-Length:", 15) ) {
855 cur += 15;
856 ctxt->ContentLength = strtol( cur, NULL, 10 );
Owen Taylor3473f882001-02-23 17:55:21 +0000857 }
858}
859
860/**
861 * xmlNanoHTTPConnectAttempt:
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000862 * @addr: a socket address structure
Owen Taylor3473f882001-02-23 17:55:21 +0000863 *
864 * Attempt a connection to the given IP:port endpoint. It forces
865 * non-blocking semantic on the socket, and allow 60 seconds for
866 * the host to answer.
867 *
868 * Returns -1 in case of failure, the file descriptor number otherwise
869 */
870
871static int
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000872xmlNanoHTTPConnectAttempt(struct sockaddr *addr)
Owen Taylor3473f882001-02-23 17:55:21 +0000873{
Owen Taylor3473f882001-02-23 17:55:21 +0000874 fd_set wfd;
875 struct timeval tv;
876 int status;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000877 int addrlen;
878 SOCKET s;
Owen Taylor3473f882001-02-23 17:55:21 +0000879
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000880#ifdef SUPPORT_IP6
881 if (addr->sa_family == AF_INET6) {
882 s = socket (PF_INET6, SOCK_STREAM, IPPROTO_TCP);
883 addrlen = sizeof (struct sockaddr_in6);
884 }
885 else
886#endif
887 {
888 s = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
889 addrlen = sizeof (struct sockaddr_in);
890 }
Owen Taylor3473f882001-02-23 17:55:21 +0000891 if (s==-1) {
892#ifdef DEBUG_HTTP
893 perror("socket");
894#endif
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000895 __xmlIOErr(XML_FROM_HTTP, 0, "socket failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000896 return(-1);
897 }
898
899#ifdef _WINSOCKAPI_
900 {
901 u_long one = 1;
902
903 status = ioctlsocket(s, FIONBIO, &one) == SOCKET_ERROR ? -1 : 0;
904 }
905#else /* _WINSOCKAPI_ */
906#if defined(VMS)
907 {
908 int enable = 1;
909 status = ioctl(s, FIONBIO, &enable);
910 }
911#else /* VMS */
Daniel Veillard254b1262003-11-01 17:04:58 +0000912#if defined(__BEOS__)
913 {
914 bool noblock = true;
915 status = setsockopt(s, SOL_SOCKET, SO_NONBLOCK, &noblock, sizeof(noblock));
916 }
917#else /* __BEOS__ */
Owen Taylor3473f882001-02-23 17:55:21 +0000918 if ((status = fcntl(s, F_GETFL, 0)) != -1) {
919#ifdef O_NONBLOCK
920 status |= O_NONBLOCK;
921#else /* O_NONBLOCK */
922#ifdef F_NDELAY
923 status |= F_NDELAY;
924#endif /* F_NDELAY */
925#endif /* !O_NONBLOCK */
926 status = fcntl(s, F_SETFL, status);
927 }
928 if (status < 0) {
929#ifdef DEBUG_HTTP
930 perror("nonblocking");
931#endif
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000932 __xmlIOErr(XML_FROM_HTTP, 0, "error setting non-blocking IO\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000933 closesocket(s);
934 return(-1);
935 }
Daniel Veillard254b1262003-11-01 17:04:58 +0000936#endif /* !__BEOS__ */
Owen Taylor3473f882001-02-23 17:55:21 +0000937#endif /* !VMS */
938#endif /* !_WINSOCKAPI_ */
939
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000940 if (connect (s, addr, addrlen) == -1) {
Owen Taylor3473f882001-02-23 17:55:21 +0000941 switch (socket_errno()) {
942 case EINPROGRESS:
943 case EWOULDBLOCK:
944 break;
945 default:
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000946 __xmlIOErr(XML_FROM_HTTP, 0, "error connecting to HTTP server");
Owen Taylor3473f882001-02-23 17:55:21 +0000947 closesocket(s);
948 return(-1);
949 }
950 }
951
952 tv.tv_sec = timeout;
953 tv.tv_usec = 0;
954
955 FD_ZERO(&wfd);
956 FD_SET(s, &wfd);
957
958 switch(select(s+1, NULL, &wfd, NULL, &tv))
959 {
960 case 0:
961 /* Time out */
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000962 __xmlIOErr(XML_FROM_HTTP, 0, "Connect attempt timed out");
Owen Taylor3473f882001-02-23 17:55:21 +0000963 closesocket(s);
964 return(-1);
965 case -1:
966 /* Ermm.. ?? */
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000967 __xmlIOErr(XML_FROM_HTTP, 0, "Connect failed");
Owen Taylor3473f882001-02-23 17:55:21 +0000968 closesocket(s);
969 return(-1);
970 }
971
972 if ( FD_ISSET(s, &wfd) ) {
973 SOCKLEN_T len;
974 len = sizeof(status);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000975#ifdef SO_ERROR
Owen Taylor3473f882001-02-23 17:55:21 +0000976 if (getsockopt(s, SOL_SOCKET, SO_ERROR, (char*)&status, &len) < 0 ) {
977 /* Solaris error code */
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000978 __xmlIOErr(XML_FROM_HTTP, 0, "getsockopt failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000979 return (-1);
980 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000981#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000982 if ( status ) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000983 __xmlIOErr(XML_FROM_HTTP, 0, "Error connecting to remote host");
Owen Taylor3473f882001-02-23 17:55:21 +0000984 closesocket(s);
985 errno = status;
986 return (-1);
987 }
988 } else {
989 /* pbm */
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000990 __xmlIOErr(XML_FROM_HTTP, 0, "select failed\n");
Daniel Veillardf012a642001-07-23 19:10:52 +0000991 closesocket(s);
Owen Taylor3473f882001-02-23 17:55:21 +0000992 return (-1);
993 }
994
995 return(s);
996}
997
998/**
999 * xmlNanoHTTPConnectHost:
1000 * @host: the host name
1001 * @port: the port number
1002 *
1003 * Attempt a connection to the given host:port endpoint. It tries
1004 * the multiple IP provided by the DNS if available.
1005 *
1006 * Returns -1 in case of failure, the file descriptor number otherwise
1007 */
1008
1009static int
1010xmlNanoHTTPConnectHost(const char *host, int port)
1011{
1012 struct hostent *h;
Daniel Veillard2db8c122003-07-08 12:16:59 +00001013 struct sockaddr *addr = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001014 struct in_addr ia;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001015 struct sockaddr_in sockin;
Daniel Veillard5c396542002-03-15 07:57:50 +00001016
Owen Taylor3473f882001-02-23 17:55:21 +00001017#ifdef SUPPORT_IP6
1018 struct in6_addr ia6;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001019 struct sockaddr_in6 sockin6;
Owen Taylor3473f882001-02-23 17:55:21 +00001020#endif
1021 int i;
1022 int s;
Daniel Veillard5c396542002-03-15 07:57:50 +00001023
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001024 memset (&sockin, 0, sizeof(sockin));
1025#ifdef SUPPORT_IP6
1026 memset (&sockin6, 0, sizeof(sockin6));
1027 if (have_ipv6 ())
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001028#if !defined(HAVE_GETADDRINFO) && defined(RES_USE_INET6)
Daniel Veillard560c2a42003-07-06 21:13:49 +00001029 {
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001030 if (!(_res.options & RES_INIT))
1031 res_init();
1032 _res.options |= RES_USE_INET6;
1033 }
1034#elif defined(HAVE_GETADDRINFO)
Daniel Veillard560c2a42003-07-06 21:13:49 +00001035 {
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001036 int status;
1037 struct addrinfo hints, *res, *result;
1038
1039 result = NULL;
1040 memset (&hints, 0,sizeof(hints));
1041 hints.ai_socktype = SOCK_STREAM;
1042
1043 status = getaddrinfo (host, NULL, &hints, &result);
1044 if (status) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001045 __xmlIOErr(XML_FROM_HTTP, 0, "getaddrinfo failed\n");
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001046 return (-1);
1047 }
1048
1049 for (res = result; res; res = res->ai_next) {
Daniel Veillard3dc93a42003-07-10 14:04:33 +00001050 if (res->ai_family == AF_INET || res->ai_family == AF_INET6) {
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001051 if (res->ai_family == AF_INET6) {
1052 memcpy (&sockin6, res->ai_addr, res->ai_addrlen);
1053 sockin6.sin6_port = htons (port);
1054 addr = (struct sockaddr *)&sockin6;
1055 }
Daniel Veillard3dc93a42003-07-10 14:04:33 +00001056 else {
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001057 memcpy (&sockin, res->ai_addr, res->ai_addrlen);
1058 sockin.sin_port = htons (port);
1059 addr = (struct sockaddr *)&sockin;
1060 }
1061
1062 s = xmlNanoHTTPConnectAttempt (addr);
1063 if (s != -1) {
1064 freeaddrinfo (result);
1065 return (s);
1066 }
1067 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001068 }
Daniel Veillard3dc93a42003-07-10 14:04:33 +00001069 if (result)
1070 freeaddrinfo (result);
1071 return (-1);
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001072 } else
Owen Taylor3473f882001-02-23 17:55:21 +00001073#endif
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001074#endif
1075 {
1076 h = gethostbyname (host);
1077 if (h == NULL) {
Daniel Veillard56b2db72002-03-25 16:35:28 +00001078
1079/*
1080 * Okay, I got fed up by the non-portability of this error message
1081 * extraction code. it work on Linux, if it work on your platform
1082 * and one want to enable it, send me the defined(foobar) needed
1083 */
1084#if defined(HAVE_NETDB_H) && defined(HOST_NOT_FOUND) && defined(linux)
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001085 const char *h_err_txt = "";
Daniel Veillardf012a642001-07-23 19:10:52 +00001086
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001087 switch (h_errno) {
1088 case HOST_NOT_FOUND:
1089 h_err_txt = "Authoritive host not found";
1090 break;
Daniel Veillardf012a642001-07-23 19:10:52 +00001091
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001092 case TRY_AGAIN:
1093 h_err_txt =
1094 "Non-authoritive host not found or server failure.";
1095 break;
Daniel Veillardf012a642001-07-23 19:10:52 +00001096
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001097 case NO_RECOVERY:
1098 h_err_txt =
1099 "Non-recoverable errors: FORMERR, REFUSED, or NOTIMP.";
1100 break;
Daniel Veillard5c396542002-03-15 07:57:50 +00001101
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001102 case NO_ADDRESS:
1103 h_err_txt =
1104 "Valid name, no data record of requested type.";
1105 break;
Daniel Veillard5c396542002-03-15 07:57:50 +00001106
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001107 default:
1108 h_err_txt = "No error text defined.";
1109 break;
1110 }
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001111 __xmlIOErr(XML_FROM_HTTP, 0, h_err_txt);
Daniel Veillard5c396542002-03-15 07:57:50 +00001112#else
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001113 __xmlIOErr(XML_FROM_HTTP, 0, "Failed to resolve host");
Owen Taylor3473f882001-02-23 17:55:21 +00001114#endif
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001115 return (-1);
1116 }
Daniel Veillard5c396542002-03-15 07:57:50 +00001117
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001118 for (i = 0; h->h_addr_list[i]; i++) {
1119 if (h->h_addrtype == AF_INET) {
1120 /* A records (IPv4) */
1121 memcpy (&ia, h->h_addr_list[i], h->h_length);
1122 sockin.sin_family = h->h_addrtype;
1123 sockin.sin_addr = ia;
1124 sockin.sin_port = htons (port);
1125 addr = (struct sockaddr *) &sockin;
Daniel Veillard5c396542002-03-15 07:57:50 +00001126#ifdef SUPPORT_IP6
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001127 } else if (have_ipv6 () && (h->h_addrtype == AF_INET6)) {
1128 /* AAAA records (IPv6) */
1129 memcpy (&ia6, h->h_addr_list[i], h->h_length);
1130 sockin6.sin6_family = h->h_addrtype;
1131 sockin6.sin6_addr = ia6;
1132 sockin6.sin6_port = htons (port);
1133 addr = (struct sockaddr *) &sockin6;
Daniel Veillard5c396542002-03-15 07:57:50 +00001134#endif
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001135 } else
1136 break; /* for */
Daniel Veillard5c396542002-03-15 07:57:50 +00001137
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001138 s = xmlNanoHTTPConnectAttempt (addr);
1139 if (s != -1)
1140 return (s);
1141 }
Owen Taylor3473f882001-02-23 17:55:21 +00001142 }
Owen Taylor3473f882001-02-23 17:55:21 +00001143#ifdef DEBUG_HTTP
1144 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard5c396542002-03-15 07:57:50 +00001145 "xmlNanoHTTPConnectHost: unable to connect to '%s'.\n",
1146 host);
Owen Taylor3473f882001-02-23 17:55:21 +00001147#endif
Daniel Veillard5c396542002-03-15 07:57:50 +00001148 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001149}
1150
1151
1152/**
1153 * xmlNanoHTTPOpen:
1154 * @URL: The URL to load
1155 * @contentType: if available the Content-Type information will be
1156 * returned at that location
1157 *
1158 * This function try to open a connection to the indicated resource
1159 * via HTTP GET.
1160 *
1161 * Returns NULL in case of failure, otherwise a request handler.
1162 * The contentType, if provided must be freed by the caller
1163 */
1164
1165void*
1166xmlNanoHTTPOpen(const char *URL, char **contentType) {
1167 if (contentType != NULL) *contentType = NULL;
Daniel Veillardf012a642001-07-23 19:10:52 +00001168 return(xmlNanoHTTPMethod(URL, NULL, NULL, contentType, NULL, 0));
Daniel Veillard9403a042001-05-28 11:00:53 +00001169}
1170
1171/**
1172 * xmlNanoHTTPOpenRedir:
1173 * @URL: The URL to load
1174 * @contentType: if available the Content-Type information will be
1175 * returned at that location
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001176 * @redir: if available the redirected URL will be returned
Daniel Veillard9403a042001-05-28 11:00:53 +00001177 *
1178 * This function try to open a connection to the indicated resource
1179 * via HTTP GET.
1180 *
1181 * Returns NULL in case of failure, otherwise a request handler.
1182 * The contentType, if provided must be freed by the caller
1183 */
1184
1185void*
1186xmlNanoHTTPOpenRedir(const char *URL, char **contentType, char **redir) {
1187 if (contentType != NULL) *contentType = NULL;
1188 if (redir != NULL) *redir = NULL;
Daniel Veillardf012a642001-07-23 19:10:52 +00001189 return(xmlNanoHTTPMethodRedir(URL, NULL, NULL, contentType, redir, NULL,0));
Owen Taylor3473f882001-02-23 17:55:21 +00001190}
1191
1192/**
1193 * xmlNanoHTTPRead:
1194 * @ctx: the HTTP context
1195 * @dest: a buffer
1196 * @len: the buffer length
1197 *
1198 * This function tries to read @len bytes from the existing HTTP connection
1199 * and saves them in @dest. This is a blocking call.
1200 *
1201 * Returns the number of byte read. 0 is an indication of an end of connection.
1202 * -1 indicates a parameter error.
1203 */
1204int
1205xmlNanoHTTPRead(void *ctx, void *dest, int len) {
1206 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1207
1208 if (ctx == NULL) return(-1);
1209 if (dest == NULL) return(-1);
1210 if (len <= 0) return(0);
1211
1212 while (ctxt->inptr - ctxt->inrptr < len) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001213 if (xmlNanoHTTPRecv(ctxt) <= 0) break;
Owen Taylor3473f882001-02-23 17:55:21 +00001214 }
1215 if (ctxt->inptr - ctxt->inrptr < len)
1216 len = ctxt->inptr - ctxt->inrptr;
1217 memcpy(dest, ctxt->inrptr, len);
1218 ctxt->inrptr += len;
1219 return(len);
1220}
1221
1222/**
1223 * xmlNanoHTTPClose:
1224 * @ctx: the HTTP context
1225 *
1226 * This function closes an HTTP context, it ends up the connection and
1227 * free all data related to it.
1228 */
1229void
1230xmlNanoHTTPClose(void *ctx) {
1231 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1232
1233 if (ctx == NULL) return;
1234
1235 xmlNanoHTTPFreeCtxt(ctxt);
1236}
1237
1238/**
Daniel Veillard9403a042001-05-28 11:00:53 +00001239 * xmlNanoHTTPMethodRedir:
Owen Taylor3473f882001-02-23 17:55:21 +00001240 * @URL: The URL to load
1241 * @method: the HTTP method to use
1242 * @input: the input string if any
1243 * @contentType: the Content-Type information IN and OUT
Daniel Veillard9403a042001-05-28 11:00:53 +00001244 * @redir: the redirected URL OUT
Owen Taylor3473f882001-02-23 17:55:21 +00001245 * @headers: the extra headers
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001246 * @ilen: input length
Owen Taylor3473f882001-02-23 17:55:21 +00001247 *
1248 * This function try to open a connection to the indicated resource
1249 * via HTTP using the given @method, adding the given extra headers
1250 * and the input buffer for the request content.
1251 *
1252 * Returns NULL in case of failure, otherwise a request handler.
Daniel Veillard9403a042001-05-28 11:00:53 +00001253 * The contentType, or redir, if provided must be freed by the caller
Owen Taylor3473f882001-02-23 17:55:21 +00001254 */
1255
1256void*
Daniel Veillard9403a042001-05-28 11:00:53 +00001257xmlNanoHTTPMethodRedir(const char *URL, const char *method, const char *input,
Daniel Veillardf012a642001-07-23 19:10:52 +00001258 char **contentType, char **redir,
1259 const char *headers, int ilen ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001260 xmlNanoHTTPCtxtPtr ctxt;
1261 char *bp, *p;
Daniel Veillardf012a642001-07-23 19:10:52 +00001262 int blen, ret;
Owen Taylor3473f882001-02-23 17:55:21 +00001263 int head;
1264 int nbRedirects = 0;
1265 char *redirURL = NULL;
William M. Brack78637da2003-07-31 14:47:38 +00001266#ifdef DEBUG_HTTP
1267 int xmt_bytes;
1268#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001269
1270 if (URL == NULL) return(NULL);
1271 if (method == NULL) method = "GET";
1272 xmlNanoHTTPInit();
1273
1274retry:
1275 if (redirURL == NULL)
1276 ctxt = xmlNanoHTTPNewCtxt(URL);
1277 else {
1278 ctxt = xmlNanoHTTPNewCtxt(redirURL);
Daniel Veillarda840b692003-10-19 13:35:37 +00001279 ctxt->location = xmlMemStrdup(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001280 }
1281
Daniel Veillardf012a642001-07-23 19:10:52 +00001282 if ( ctxt == NULL ) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001283 return ( NULL );
1284 }
1285
Owen Taylor3473f882001-02-23 17:55:21 +00001286 if ((ctxt->protocol == NULL) || (strcmp(ctxt->protocol, "http"))) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001287 __xmlIOErr(XML_FROM_HTTP, XML_HTTP_URL_SYNTAX, "Not a valid HTTP URI");
Owen Taylor3473f882001-02-23 17:55:21 +00001288 xmlNanoHTTPFreeCtxt(ctxt);
1289 if (redirURL != NULL) xmlFree(redirURL);
1290 return(NULL);
1291 }
1292 if (ctxt->hostname == NULL) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001293 __xmlIOErr(XML_FROM_HTTP, XML_HTTP_UNKNOWN_HOST,
1294 "Failed to identify host in URI");
Owen Taylor3473f882001-02-23 17:55:21 +00001295 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001296 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001297 return(NULL);
1298 }
1299 if (proxy) {
1300 blen = strlen(ctxt->hostname) * 2 + 16;
1301 ret = xmlNanoHTTPConnectHost(proxy, proxyPort);
1302 }
1303 else {
1304 blen = strlen(ctxt->hostname);
1305 ret = xmlNanoHTTPConnectHost(ctxt->hostname, ctxt->port);
1306 }
1307 if (ret < 0) {
1308 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001309 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001310 return(NULL);
1311 }
1312 ctxt->fd = ret;
1313
Daniel Veillardf012a642001-07-23 19:10:52 +00001314 if (input == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00001315 ilen = 0;
Daniel Veillardf012a642001-07-23 19:10:52 +00001316 else
1317 blen += 36;
1318
Owen Taylor3473f882001-02-23 17:55:21 +00001319 if (headers != NULL)
Daniel Veillardf012a642001-07-23 19:10:52 +00001320 blen += strlen(headers) + 2;
Owen Taylor3473f882001-02-23 17:55:21 +00001321 if (contentType && *contentType)
1322 blen += strlen(*contentType) + 16;
Daniel Veillardf012a642001-07-23 19:10:52 +00001323 blen += strlen(method) + strlen(ctxt->path) + 24;
Daniel Veillard82cb3192003-10-29 13:39:15 +00001324 bp = (char*)xmlMallocAtomic(blen);
Daniel Veillardf012a642001-07-23 19:10:52 +00001325 if ( bp == NULL ) {
1326 xmlNanoHTTPFreeCtxt( ctxt );
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001327 xmlHTTPErrMemory("allocating header buffer");
Daniel Veillardf012a642001-07-23 19:10:52 +00001328 return ( NULL );
1329 }
1330
1331 p = bp;
1332
Owen Taylor3473f882001-02-23 17:55:21 +00001333 if (proxy) {
1334 if (ctxt->port != 80) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001335 p += snprintf( p, blen - (p - bp), "%s http://%s:%d%s",
1336 method, ctxt->hostname,
Daniel Veillardf012a642001-07-23 19:10:52 +00001337 ctxt->port, ctxt->path );
Owen Taylor3473f882001-02-23 17:55:21 +00001338 }
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001339 else
1340 p += snprintf( p, blen - (p - bp), "%s http://%s%s", method,
Daniel Veillardf012a642001-07-23 19:10:52 +00001341 ctxt->hostname, ctxt->path);
Owen Taylor3473f882001-02-23 17:55:21 +00001342 }
1343 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001344 p += snprintf( p, blen - (p - bp), "%s %s", method, ctxt->path);
Daniel Veillardf012a642001-07-23 19:10:52 +00001345
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001346 p += snprintf( p, blen - (p - bp), " HTTP/1.0\r\nHost: %s\r\n",
1347 ctxt->hostname);
Daniel Veillardf012a642001-07-23 19:10:52 +00001348
1349 if (contentType != NULL && *contentType)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001350 p += snprintf(p, blen - (p - bp), "Content-Type: %s\r\n", *contentType);
Daniel Veillardf012a642001-07-23 19:10:52 +00001351
1352 if (headers != NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001353 p += snprintf( p, blen - (p - bp), "%s", headers );
Daniel Veillardf012a642001-07-23 19:10:52 +00001354
Owen Taylor3473f882001-02-23 17:55:21 +00001355 if (input != NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001356 snprintf(p, blen - (p - bp), "Content-Length: %d\r\n\r\n", ilen );
Owen Taylor3473f882001-02-23 17:55:21 +00001357 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001358 snprintf(p, blen - (p - bp), "\r\n");
Daniel Veillardf012a642001-07-23 19:10:52 +00001359
Owen Taylor3473f882001-02-23 17:55:21 +00001360#ifdef DEBUG_HTTP
1361 xmlGenericError(xmlGenericErrorContext,
1362 "-> %s%s", proxy? "(Proxy) " : "", bp);
1363 if ((blen -= strlen(bp)+1) < 0)
1364 xmlGenericError(xmlGenericErrorContext,
1365 "ERROR: overflowed buffer by %d bytes\n", -blen);
1366#endif
1367 ctxt->outptr = ctxt->out = bp;
1368 ctxt->state = XML_NANO_HTTP_WRITE;
Daniel Veillardf012a642001-07-23 19:10:52 +00001369 blen = strlen( ctxt->out );
Daniel Veillardf012a642001-07-23 19:10:52 +00001370#ifdef DEBUG_HTTP
William M. Brack78637da2003-07-31 14:47:38 +00001371 xmt_bytes = xmlNanoHTTPSend(ctxt, ctxt->out, blen );
Daniel Veillardf012a642001-07-23 19:10:52 +00001372 if ( xmt_bytes != blen )
1373 xmlGenericError( xmlGenericErrorContext,
1374 "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
1375 xmt_bytes, blen,
1376 "bytes of HTTP headers sent to host",
1377 ctxt->hostname );
William M. Brack78637da2003-07-31 14:47:38 +00001378#else
1379 xmlNanoHTTPSend(ctxt, ctxt->out, blen );
Daniel Veillardf012a642001-07-23 19:10:52 +00001380#endif
1381
1382 if ( input != NULL ) {
William M. Brack78637da2003-07-31 14:47:38 +00001383#ifdef DEBUG_HTTP
Daniel Veillardf012a642001-07-23 19:10:52 +00001384 xmt_bytes = xmlNanoHTTPSend( ctxt, input, ilen );
1385
Daniel Veillardf012a642001-07-23 19:10:52 +00001386 if ( xmt_bytes != ilen )
1387 xmlGenericError( xmlGenericErrorContext,
1388 "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
1389 xmt_bytes, ilen,
1390 "bytes of HTTP content sent to host",
1391 ctxt->hostname );
William M. Brack78637da2003-07-31 14:47:38 +00001392#else
1393 xmlNanoHTTPSend( ctxt, input, ilen );
Daniel Veillardf012a642001-07-23 19:10:52 +00001394#endif
1395 }
1396
Owen Taylor3473f882001-02-23 17:55:21 +00001397 ctxt->state = XML_NANO_HTTP_READ;
1398 head = 1;
1399
1400 while ((p = xmlNanoHTTPReadLine(ctxt)) != NULL) {
1401 if (head && (*p == 0)) {
1402 head = 0;
1403 ctxt->content = ctxt->inrptr;
1404 xmlFree(p);
1405 break;
1406 }
1407 xmlNanoHTTPScanAnswer(ctxt, p);
1408
1409#ifdef DEBUG_HTTP
1410 xmlGenericError(xmlGenericErrorContext, "<- %s\n", p);
1411#endif
1412 xmlFree(p);
1413 }
1414
1415 if ((ctxt->location != NULL) && (ctxt->returnValue >= 300) &&
1416 (ctxt->returnValue < 400)) {
1417#ifdef DEBUG_HTTP
1418 xmlGenericError(xmlGenericErrorContext,
1419 "\nRedirect to: %s\n", ctxt->location);
1420#endif
Daniel Veillardf012a642001-07-23 19:10:52 +00001421 while ( xmlNanoHTTPRecv(ctxt) > 0 ) ;
Owen Taylor3473f882001-02-23 17:55:21 +00001422 if (nbRedirects < XML_NANO_HTTP_MAX_REDIR) {
1423 nbRedirects++;
Daniel Veillard9403a042001-05-28 11:00:53 +00001424 if (redirURL != NULL)
1425 xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001426 redirURL = xmlMemStrdup(ctxt->location);
1427 xmlNanoHTTPFreeCtxt(ctxt);
1428 goto retry;
1429 }
1430 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001431 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001432#ifdef DEBUG_HTTP
1433 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardf012a642001-07-23 19:10:52 +00001434 "xmlNanoHTTPMethodRedir: Too many redirects, aborting ...\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001435#endif
1436 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001437 }
1438
1439 if (contentType != NULL) {
1440 if (ctxt->contentType != NULL)
1441 *contentType = xmlMemStrdup(ctxt->contentType);
1442 else
1443 *contentType = NULL;
1444 }
1445
Daniel Veillard9403a042001-05-28 11:00:53 +00001446 if ((redir != NULL) && (redirURL != NULL)) {
1447 *redir = redirURL;
1448 } else {
1449 if (redirURL != NULL)
1450 xmlFree(redirURL);
1451 if (redir != NULL)
1452 *redir = NULL;
1453 }
1454
Owen Taylor3473f882001-02-23 17:55:21 +00001455#ifdef DEBUG_HTTP
1456 if (ctxt->contentType != NULL)
1457 xmlGenericError(xmlGenericErrorContext,
1458 "\nCode %d, content-type '%s'\n\n",
1459 ctxt->returnValue, ctxt->contentType);
1460 else
1461 xmlGenericError(xmlGenericErrorContext,
1462 "\nCode %d, no content-type\n\n",
1463 ctxt->returnValue);
1464#endif
1465
1466 return((void *) ctxt);
1467}
1468
1469/**
Daniel Veillard9403a042001-05-28 11:00:53 +00001470 * xmlNanoHTTPMethod:
1471 * @URL: The URL to load
1472 * @method: the HTTP method to use
1473 * @input: the input string if any
1474 * @contentType: the Content-Type information IN and OUT
1475 * @headers: the extra headers
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001476 * @ilen: input length
Daniel Veillard9403a042001-05-28 11:00:53 +00001477 *
1478 * This function try to open a connection to the indicated resource
1479 * via HTTP using the given @method, adding the given extra headers
1480 * and the input buffer for the request content.
1481 *
1482 * Returns NULL in case of failure, otherwise a request handler.
1483 * The contentType, if provided must be freed by the caller
1484 */
1485
1486void*
1487xmlNanoHTTPMethod(const char *URL, const char *method, const char *input,
Daniel Veillardf012a642001-07-23 19:10:52 +00001488 char **contentType, const char *headers, int ilen) {
Daniel Veillard9403a042001-05-28 11:00:53 +00001489 return(xmlNanoHTTPMethodRedir(URL, method, input, contentType,
Daniel Veillardf012a642001-07-23 19:10:52 +00001490 NULL, headers, ilen));
Daniel Veillard9403a042001-05-28 11:00:53 +00001491}
1492
1493/**
Owen Taylor3473f882001-02-23 17:55:21 +00001494 * xmlNanoHTTPFetch:
1495 * @URL: The URL to load
1496 * @filename: the filename where the content should be saved
1497 * @contentType: if available the Content-Type information will be
1498 * returned at that location
1499 *
1500 * This function try to fetch the indicated resource via HTTP GET
1501 * and save it's content in the file.
1502 *
1503 * Returns -1 in case of failure, 0 incase of success. The contentType,
1504 * if provided must be freed by the caller
1505 */
1506int
1507xmlNanoHTTPFetch(const char *URL, const char *filename, char **contentType) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001508 void *ctxt = NULL;
1509 char *buf = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001510 int fd;
1511 int len;
1512
1513 ctxt = xmlNanoHTTPOpen(URL, contentType);
1514 if (ctxt == NULL) return(-1);
1515
1516 if (!strcmp(filename, "-"))
1517 fd = 0;
1518 else {
1519 fd = open(filename, O_CREAT | O_WRONLY, 00644);
1520 if (fd < 0) {
1521 xmlNanoHTTPClose(ctxt);
1522 if ((contentType != NULL) && (*contentType != NULL)) {
1523 xmlFree(*contentType);
1524 *contentType = NULL;
1525 }
1526 return(-1);
1527 }
1528 }
1529
Daniel Veillardf012a642001-07-23 19:10:52 +00001530 xmlNanoHTTPFetchContent( ctxt, &buf, &len );
1531 if ( len > 0 ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001532 write(fd, buf, len);
1533 }
1534
1535 xmlNanoHTTPClose(ctxt);
1536 close(fd);
1537 return(0);
1538}
1539
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001540#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001541/**
1542 * xmlNanoHTTPSave:
1543 * @ctxt: the HTTP context
1544 * @filename: the filename where the content should be saved
1545 *
1546 * This function saves the output of the HTTP transaction to a file
1547 * It closes and free the context at the end
1548 *
1549 * Returns -1 in case of failure, 0 incase of success.
1550 */
1551int
1552xmlNanoHTTPSave(void *ctxt, const char *filename) {
Daniel Veillarde3924972001-07-25 20:25:21 +00001553 char *buf = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001554 int fd;
1555 int len;
1556
1557 if (ctxt == NULL) return(-1);
1558
1559 if (!strcmp(filename, "-"))
1560 fd = 0;
1561 else {
1562 fd = open(filename, O_CREAT | O_WRONLY);
1563 if (fd < 0) {
1564 xmlNanoHTTPClose(ctxt);
1565 return(-1);
1566 }
1567 }
1568
Daniel Veillardf012a642001-07-23 19:10:52 +00001569 xmlNanoHTTPFetchContent( ctxt, &buf, &len );
1570 if ( len > 0 ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001571 write(fd, buf, len);
1572 }
1573
1574 xmlNanoHTTPClose(ctxt);
1575 return(0);
1576}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001577#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001578
1579/**
1580 * xmlNanoHTTPReturnCode:
1581 * @ctx: the HTTP context
1582 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001583 * Get the latest HTTP return code received
1584 *
Owen Taylor3473f882001-02-23 17:55:21 +00001585 * Returns the HTTP return code for the request.
1586 */
1587int
1588xmlNanoHTTPReturnCode(void *ctx) {
1589 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1590
1591 if (ctxt == NULL) return(-1);
1592
1593 return(ctxt->returnValue);
1594}
1595
1596/**
1597 * xmlNanoHTTPAuthHeader:
1598 * @ctx: the HTTP context
1599 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001600 * Get the authentication header of an HTTP context
1601 *
Owen Taylor3473f882001-02-23 17:55:21 +00001602 * Returns the stashed value of the WWW-Authenticate or Proxy-Authenticate
1603 * header.
1604 */
1605const char *
1606xmlNanoHTTPAuthHeader(void *ctx) {
1607 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1608
1609 if (ctxt == NULL) return(NULL);
1610
1611 return(ctxt->authHeader);
1612}
1613
Daniel Veillardf012a642001-07-23 19:10:52 +00001614/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00001615 * xmlNanoHTTPContentLength:
Daniel Veillardf012a642001-07-23 19:10:52 +00001616 * @ctx: the HTTP context
1617 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001618 * Provides the specified content length from the HTTP header.
1619 *
Daniel Veillardf012a642001-07-23 19:10:52 +00001620 * Return the specified content length from the HTTP header. Note that
1621 * a value of -1 indicates that the content length element was not included in
1622 * the response header.
1623 */
1624int
1625xmlNanoHTTPContentLength( void * ctx ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001626 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillardf012a642001-07-23 19:10:52 +00001627
1628 return ( ( ctxt == NULL ) ? -1 : ctxt->ContentLength );
1629}
1630
1631/**
Daniel Veillard847332a2003-10-18 11:29:40 +00001632 * xmlNanoHTTPRedir:
1633 * @ctx: the HTTP context
1634 *
1635 * Provides the specified redirection URL if available from the HTTP header.
1636 *
1637 * Return the specified redirection URL or NULL if not redirected.
1638 */
1639const char *
1640xmlNanoHTTPRedir( void * ctx ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001641 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillard847332a2003-10-18 11:29:40 +00001642
1643 return ( ( ctxt == NULL ) ? NULL : ctxt->location );
1644}
1645
1646/**
1647 * xmlNanoHTTPEncoding:
1648 * @ctx: the HTTP context
1649 *
1650 * Provides the specified encoding if specified in the HTTP headers.
1651 *
1652 * Return the specified encoding or NULL if not available
1653 */
1654const char *
1655xmlNanoHTTPEncoding( void * ctx ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001656 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillard847332a2003-10-18 11:29:40 +00001657
1658 return ( ( ctxt == NULL ) ? NULL : ctxt->encoding );
1659}
1660
1661/**
Daniel Veillarda840b692003-10-19 13:35:37 +00001662 * xmlNanoHTTPMimeType:
1663 * @ctx: the HTTP context
1664 *
1665 * Provides the specified Mime-Type if specified in the HTTP headers.
1666 *
1667 * Return the specified Mime-Type or NULL if not available
1668 */
1669const char *
1670xmlNanoHTTPMimeType( void * ctx ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001671 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillarda840b692003-10-19 13:35:37 +00001672
1673 return ( ( ctxt == NULL ) ? NULL : ctxt->mimeType );
1674}
1675
1676/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00001677 * xmlNanoHTTPFetchContent:
Daniel Veillardf012a642001-07-23 19:10:52 +00001678 * @ctx: the HTTP context
1679 * @ptr: pointer to set to the content buffer.
1680 * @len: integer pointer to hold the length of the content
1681 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001682 * Check if all the content was read
1683 *
Daniel Veillardf012a642001-07-23 19:10:52 +00001684 * Returns 0 if all the content was read and available, returns
1685 * -1 if received content length was less than specified or an error
1686 * occurred.
1687 */
1688int
1689xmlNanoHTTPFetchContent( void * ctx, char ** ptr, int * len ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001690 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillardf012a642001-07-23 19:10:52 +00001691
1692 int rc = 0;
1693 int cur_lgth;
1694 int rcvd_lgth;
1695 int dummy_int;
1696 char * dummy_ptr = NULL;
1697
1698 /* Dummy up return input parameters if not provided */
1699
1700 if ( len == NULL )
1701 len = &dummy_int;
1702
1703 if ( ptr == NULL )
1704 ptr = &dummy_ptr;
1705
1706 /* But can't work without the context pointer */
1707
1708 if ( ( ctxt == NULL ) || ( ctxt->content == NULL ) ) {
1709 *len = 0;
1710 *ptr = NULL;
1711 return ( -1 );
1712 }
1713
1714 rcvd_lgth = ctxt->inptr - ctxt->content;
1715
1716 while ( (cur_lgth = xmlNanoHTTPRecv( ctxt )) > 0 ) {
1717
1718 rcvd_lgth += cur_lgth;
1719 if ( (ctxt->ContentLength > 0) && (rcvd_lgth >= ctxt->ContentLength) )
1720 break;
1721 }
1722
1723 *ptr = ctxt->content;
1724 *len = rcvd_lgth;
1725
1726 if ( ( ctxt->ContentLength > 0 ) && ( rcvd_lgth < ctxt->ContentLength ) )
1727 rc = -1;
1728 else if ( rcvd_lgth == 0 )
1729 rc = -1;
1730
1731 return ( rc );
1732}
1733
Owen Taylor3473f882001-02-23 17:55:21 +00001734#ifdef STANDALONE
1735int main(int argc, char **argv) {
1736 char *contentType = NULL;
1737
1738 if (argv[1] != NULL) {
1739 if (argv[2] != NULL)
1740 xmlNanoHTTPFetch(argv[1], argv[2], &contentType);
1741 else
1742 xmlNanoHTTPFetch(argv[1], "-", &contentType);
1743 if (contentType != NULL) xmlFree(contentType);
1744 } else {
1745 xmlGenericError(xmlGenericErrorContext,
1746 "%s: minimal HTTP GET implementation\n", argv[0]);
1747 xmlGenericError(xmlGenericErrorContext,
1748 "\tusage %s [ URL [ filename ] ]\n", argv[0]);
1749 }
1750 xmlNanoHTTPCleanup();
1751 xmlMemoryDump();
1752 return(0);
1753}
1754#endif /* STANDALONE */
1755#else /* !LIBXML_HTTP_ENABLED */
1756#ifdef STANDALONE
1757#include <stdio.h>
1758int main(int argc, char **argv) {
1759 xmlGenericError(xmlGenericErrorContext,
1760 "%s : HTTP support not compiled in\n", argv[0]);
1761 return(0);
1762}
1763#endif /* STANDALONE */
1764#endif /* LIBXML_HTTP_ENABLED */