blob: 489f14a68385c022ddea1c02ba602494bf54d391 [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 */
912 if ((status = fcntl(s, F_GETFL, 0)) != -1) {
913#ifdef O_NONBLOCK
914 status |= O_NONBLOCK;
915#else /* O_NONBLOCK */
916#ifdef F_NDELAY
917 status |= F_NDELAY;
918#endif /* F_NDELAY */
919#endif /* !O_NONBLOCK */
920 status = fcntl(s, F_SETFL, status);
921 }
922 if (status < 0) {
923#ifdef DEBUG_HTTP
924 perror("nonblocking");
925#endif
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000926 __xmlIOErr(XML_FROM_HTTP, 0, "error setting non-blocking IO\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000927 closesocket(s);
928 return(-1);
929 }
930#endif /* !VMS */
931#endif /* !_WINSOCKAPI_ */
932
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000933 if (connect (s, addr, addrlen) == -1) {
Owen Taylor3473f882001-02-23 17:55:21 +0000934 switch (socket_errno()) {
935 case EINPROGRESS:
936 case EWOULDBLOCK:
937 break;
938 default:
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000939 __xmlIOErr(XML_FROM_HTTP, 0, "error connecting to HTTP server");
Owen Taylor3473f882001-02-23 17:55:21 +0000940 closesocket(s);
941 return(-1);
942 }
943 }
944
945 tv.tv_sec = timeout;
946 tv.tv_usec = 0;
947
948 FD_ZERO(&wfd);
949 FD_SET(s, &wfd);
950
951 switch(select(s+1, NULL, &wfd, NULL, &tv))
952 {
953 case 0:
954 /* Time out */
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000955 __xmlIOErr(XML_FROM_HTTP, 0, "Connect attempt timed out");
Owen Taylor3473f882001-02-23 17:55:21 +0000956 closesocket(s);
957 return(-1);
958 case -1:
959 /* Ermm.. ?? */
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000960 __xmlIOErr(XML_FROM_HTTP, 0, "Connect failed");
Owen Taylor3473f882001-02-23 17:55:21 +0000961 closesocket(s);
962 return(-1);
963 }
964
965 if ( FD_ISSET(s, &wfd) ) {
966 SOCKLEN_T len;
967 len = sizeof(status);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000968#ifdef SO_ERROR
Owen Taylor3473f882001-02-23 17:55:21 +0000969 if (getsockopt(s, SOL_SOCKET, SO_ERROR, (char*)&status, &len) < 0 ) {
970 /* Solaris error code */
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000971 __xmlIOErr(XML_FROM_HTTP, 0, "getsockopt failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000972 return (-1);
973 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000974#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000975 if ( status ) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000976 __xmlIOErr(XML_FROM_HTTP, 0, "Error connecting to remote host");
Owen Taylor3473f882001-02-23 17:55:21 +0000977 closesocket(s);
978 errno = status;
979 return (-1);
980 }
981 } else {
982 /* pbm */
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000983 __xmlIOErr(XML_FROM_HTTP, 0, "select failed\n");
Daniel Veillardf012a642001-07-23 19:10:52 +0000984 closesocket(s);
Owen Taylor3473f882001-02-23 17:55:21 +0000985 return (-1);
986 }
987
988 return(s);
989}
990
991/**
992 * xmlNanoHTTPConnectHost:
993 * @host: the host name
994 * @port: the port number
995 *
996 * Attempt a connection to the given host:port endpoint. It tries
997 * the multiple IP provided by the DNS if available.
998 *
999 * Returns -1 in case of failure, the file descriptor number otherwise
1000 */
1001
1002static int
1003xmlNanoHTTPConnectHost(const char *host, int port)
1004{
1005 struct hostent *h;
Daniel Veillard2db8c122003-07-08 12:16:59 +00001006 struct sockaddr *addr = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001007 struct in_addr ia;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001008 struct sockaddr_in sockin;
Daniel Veillard5c396542002-03-15 07:57:50 +00001009
Owen Taylor3473f882001-02-23 17:55:21 +00001010#ifdef SUPPORT_IP6
1011 struct in6_addr ia6;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001012 struct sockaddr_in6 sockin6;
Owen Taylor3473f882001-02-23 17:55:21 +00001013#endif
1014 int i;
1015 int s;
Daniel Veillard5c396542002-03-15 07:57:50 +00001016
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001017 memset (&sockin, 0, sizeof(sockin));
1018#ifdef SUPPORT_IP6
1019 memset (&sockin6, 0, sizeof(sockin6));
1020 if (have_ipv6 ())
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001021#if !defined(HAVE_GETADDRINFO) && defined(RES_USE_INET6)
Daniel Veillard560c2a42003-07-06 21:13:49 +00001022 {
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001023 if (!(_res.options & RES_INIT))
1024 res_init();
1025 _res.options |= RES_USE_INET6;
1026 }
1027#elif defined(HAVE_GETADDRINFO)
Daniel Veillard560c2a42003-07-06 21:13:49 +00001028 {
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001029 int status;
1030 struct addrinfo hints, *res, *result;
1031
1032 result = NULL;
1033 memset (&hints, 0,sizeof(hints));
1034 hints.ai_socktype = SOCK_STREAM;
1035
1036 status = getaddrinfo (host, NULL, &hints, &result);
1037 if (status) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001038 __xmlIOErr(XML_FROM_HTTP, 0, "getaddrinfo failed\n");
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001039 return (-1);
1040 }
1041
1042 for (res = result; res; res = res->ai_next) {
Daniel Veillard3dc93a42003-07-10 14:04:33 +00001043 if (res->ai_family == AF_INET || res->ai_family == AF_INET6) {
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001044 if (res->ai_family == AF_INET6) {
1045 memcpy (&sockin6, res->ai_addr, res->ai_addrlen);
1046 sockin6.sin6_port = htons (port);
1047 addr = (struct sockaddr *)&sockin6;
1048 }
Daniel Veillard3dc93a42003-07-10 14:04:33 +00001049 else {
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001050 memcpy (&sockin, res->ai_addr, res->ai_addrlen);
1051 sockin.sin_port = htons (port);
1052 addr = (struct sockaddr *)&sockin;
1053 }
1054
1055 s = xmlNanoHTTPConnectAttempt (addr);
1056 if (s != -1) {
1057 freeaddrinfo (result);
1058 return (s);
1059 }
1060 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001061 }
Daniel Veillard3dc93a42003-07-10 14:04:33 +00001062 if (result)
1063 freeaddrinfo (result);
1064 return (-1);
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001065 } else
Owen Taylor3473f882001-02-23 17:55:21 +00001066#endif
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001067#endif
1068 {
1069 h = gethostbyname (host);
1070 if (h == NULL) {
Daniel Veillard56b2db72002-03-25 16:35:28 +00001071
1072/*
1073 * Okay, I got fed up by the non-portability of this error message
1074 * extraction code. it work on Linux, if it work on your platform
1075 * and one want to enable it, send me the defined(foobar) needed
1076 */
1077#if defined(HAVE_NETDB_H) && defined(HOST_NOT_FOUND) && defined(linux)
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001078 const char *h_err_txt = "";
Daniel Veillardf012a642001-07-23 19:10:52 +00001079
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001080 switch (h_errno) {
1081 case HOST_NOT_FOUND:
1082 h_err_txt = "Authoritive host not found";
1083 break;
Daniel Veillardf012a642001-07-23 19:10:52 +00001084
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001085 case TRY_AGAIN:
1086 h_err_txt =
1087 "Non-authoritive host not found or server failure.";
1088 break;
Daniel Veillardf012a642001-07-23 19:10:52 +00001089
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001090 case NO_RECOVERY:
1091 h_err_txt =
1092 "Non-recoverable errors: FORMERR, REFUSED, or NOTIMP.";
1093 break;
Daniel Veillard5c396542002-03-15 07:57:50 +00001094
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001095 case NO_ADDRESS:
1096 h_err_txt =
1097 "Valid name, no data record of requested type.";
1098 break;
Daniel Veillard5c396542002-03-15 07:57:50 +00001099
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001100 default:
1101 h_err_txt = "No error text defined.";
1102 break;
1103 }
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001104 __xmlIOErr(XML_FROM_HTTP, 0, h_err_txt);
Daniel Veillard5c396542002-03-15 07:57:50 +00001105#else
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001106 __xmlIOErr(XML_FROM_HTTP, 0, "Failed to resolve host");
Owen Taylor3473f882001-02-23 17:55:21 +00001107#endif
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001108 return (-1);
1109 }
Daniel Veillard5c396542002-03-15 07:57:50 +00001110
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001111 for (i = 0; h->h_addr_list[i]; i++) {
1112 if (h->h_addrtype == AF_INET) {
1113 /* A records (IPv4) */
1114 memcpy (&ia, h->h_addr_list[i], h->h_length);
1115 sockin.sin_family = h->h_addrtype;
1116 sockin.sin_addr = ia;
1117 sockin.sin_port = htons (port);
1118 addr = (struct sockaddr *) &sockin;
Daniel Veillard5c396542002-03-15 07:57:50 +00001119#ifdef SUPPORT_IP6
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001120 } else if (have_ipv6 () && (h->h_addrtype == AF_INET6)) {
1121 /* AAAA records (IPv6) */
1122 memcpy (&ia6, h->h_addr_list[i], h->h_length);
1123 sockin6.sin6_family = h->h_addrtype;
1124 sockin6.sin6_addr = ia6;
1125 sockin6.sin6_port = htons (port);
1126 addr = (struct sockaddr *) &sockin6;
Daniel Veillard5c396542002-03-15 07:57:50 +00001127#endif
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001128 } else
1129 break; /* for */
Daniel Veillard5c396542002-03-15 07:57:50 +00001130
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001131 s = xmlNanoHTTPConnectAttempt (addr);
1132 if (s != -1)
1133 return (s);
1134 }
Owen Taylor3473f882001-02-23 17:55:21 +00001135 }
Owen Taylor3473f882001-02-23 17:55:21 +00001136#ifdef DEBUG_HTTP
1137 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard5c396542002-03-15 07:57:50 +00001138 "xmlNanoHTTPConnectHost: unable to connect to '%s'.\n",
1139 host);
Owen Taylor3473f882001-02-23 17:55:21 +00001140#endif
Daniel Veillard5c396542002-03-15 07:57:50 +00001141 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001142}
1143
1144
1145/**
1146 * xmlNanoHTTPOpen:
1147 * @URL: The URL to load
1148 * @contentType: if available the Content-Type information will be
1149 * returned at that location
1150 *
1151 * This function try to open a connection to the indicated resource
1152 * via HTTP GET.
1153 *
1154 * Returns NULL in case of failure, otherwise a request handler.
1155 * The contentType, if provided must be freed by the caller
1156 */
1157
1158void*
1159xmlNanoHTTPOpen(const char *URL, char **contentType) {
1160 if (contentType != NULL) *contentType = NULL;
Daniel Veillardf012a642001-07-23 19:10:52 +00001161 return(xmlNanoHTTPMethod(URL, NULL, NULL, contentType, NULL, 0));
Daniel Veillard9403a042001-05-28 11:00:53 +00001162}
1163
1164/**
1165 * xmlNanoHTTPOpenRedir:
1166 * @URL: The URL to load
1167 * @contentType: if available the Content-Type information will be
1168 * returned at that location
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001169 * @redir: if available the redirected URL will be returned
Daniel Veillard9403a042001-05-28 11:00:53 +00001170 *
1171 * This function try to open a connection to the indicated resource
1172 * via HTTP GET.
1173 *
1174 * Returns NULL in case of failure, otherwise a request handler.
1175 * The contentType, if provided must be freed by the caller
1176 */
1177
1178void*
1179xmlNanoHTTPOpenRedir(const char *URL, char **contentType, char **redir) {
1180 if (contentType != NULL) *contentType = NULL;
1181 if (redir != NULL) *redir = NULL;
Daniel Veillardf012a642001-07-23 19:10:52 +00001182 return(xmlNanoHTTPMethodRedir(URL, NULL, NULL, contentType, redir, NULL,0));
Owen Taylor3473f882001-02-23 17:55:21 +00001183}
1184
1185/**
1186 * xmlNanoHTTPRead:
1187 * @ctx: the HTTP context
1188 * @dest: a buffer
1189 * @len: the buffer length
1190 *
1191 * This function tries to read @len bytes from the existing HTTP connection
1192 * and saves them in @dest. This is a blocking call.
1193 *
1194 * Returns the number of byte read. 0 is an indication of an end of connection.
1195 * -1 indicates a parameter error.
1196 */
1197int
1198xmlNanoHTTPRead(void *ctx, void *dest, int len) {
1199 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1200
1201 if (ctx == NULL) return(-1);
1202 if (dest == NULL) return(-1);
1203 if (len <= 0) return(0);
1204
1205 while (ctxt->inptr - ctxt->inrptr < len) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001206 if (xmlNanoHTTPRecv(ctxt) <= 0) break;
Owen Taylor3473f882001-02-23 17:55:21 +00001207 }
1208 if (ctxt->inptr - ctxt->inrptr < len)
1209 len = ctxt->inptr - ctxt->inrptr;
1210 memcpy(dest, ctxt->inrptr, len);
1211 ctxt->inrptr += len;
1212 return(len);
1213}
1214
1215/**
1216 * xmlNanoHTTPClose:
1217 * @ctx: the HTTP context
1218 *
1219 * This function closes an HTTP context, it ends up the connection and
1220 * free all data related to it.
1221 */
1222void
1223xmlNanoHTTPClose(void *ctx) {
1224 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1225
1226 if (ctx == NULL) return;
1227
1228 xmlNanoHTTPFreeCtxt(ctxt);
1229}
1230
1231/**
Daniel Veillard9403a042001-05-28 11:00:53 +00001232 * xmlNanoHTTPMethodRedir:
Owen Taylor3473f882001-02-23 17:55:21 +00001233 * @URL: The URL to load
1234 * @method: the HTTP method to use
1235 * @input: the input string if any
1236 * @contentType: the Content-Type information IN and OUT
Daniel Veillard9403a042001-05-28 11:00:53 +00001237 * @redir: the redirected URL OUT
Owen Taylor3473f882001-02-23 17:55:21 +00001238 * @headers: the extra headers
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001239 * @ilen: input length
Owen Taylor3473f882001-02-23 17:55:21 +00001240 *
1241 * This function try to open a connection to the indicated resource
1242 * via HTTP using the given @method, adding the given extra headers
1243 * and the input buffer for the request content.
1244 *
1245 * Returns NULL in case of failure, otherwise a request handler.
Daniel Veillard9403a042001-05-28 11:00:53 +00001246 * The contentType, or redir, if provided must be freed by the caller
Owen Taylor3473f882001-02-23 17:55:21 +00001247 */
1248
1249void*
Daniel Veillard9403a042001-05-28 11:00:53 +00001250xmlNanoHTTPMethodRedir(const char *URL, const char *method, const char *input,
Daniel Veillardf012a642001-07-23 19:10:52 +00001251 char **contentType, char **redir,
1252 const char *headers, int ilen ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001253 xmlNanoHTTPCtxtPtr ctxt;
1254 char *bp, *p;
Daniel Veillardf012a642001-07-23 19:10:52 +00001255 int blen, ret;
Owen Taylor3473f882001-02-23 17:55:21 +00001256 int head;
1257 int nbRedirects = 0;
1258 char *redirURL = NULL;
William M. Brack78637da2003-07-31 14:47:38 +00001259#ifdef DEBUG_HTTP
1260 int xmt_bytes;
1261#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001262
1263 if (URL == NULL) return(NULL);
1264 if (method == NULL) method = "GET";
1265 xmlNanoHTTPInit();
1266
1267retry:
1268 if (redirURL == NULL)
1269 ctxt = xmlNanoHTTPNewCtxt(URL);
1270 else {
1271 ctxt = xmlNanoHTTPNewCtxt(redirURL);
Daniel Veillarda840b692003-10-19 13:35:37 +00001272 ctxt->location = xmlMemStrdup(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001273 }
1274
Daniel Veillardf012a642001-07-23 19:10:52 +00001275 if ( ctxt == NULL ) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001276 return ( NULL );
1277 }
1278
Owen Taylor3473f882001-02-23 17:55:21 +00001279 if ((ctxt->protocol == NULL) || (strcmp(ctxt->protocol, "http"))) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001280 __xmlIOErr(XML_FROM_HTTP, XML_HTTP_URL_SYNTAX, "Not a valid HTTP URI");
Owen Taylor3473f882001-02-23 17:55:21 +00001281 xmlNanoHTTPFreeCtxt(ctxt);
1282 if (redirURL != NULL) xmlFree(redirURL);
1283 return(NULL);
1284 }
1285 if (ctxt->hostname == NULL) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001286 __xmlIOErr(XML_FROM_HTTP, XML_HTTP_UNKNOWN_HOST,
1287 "Failed to identify host in URI");
Owen Taylor3473f882001-02-23 17:55:21 +00001288 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001289 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001290 return(NULL);
1291 }
1292 if (proxy) {
1293 blen = strlen(ctxt->hostname) * 2 + 16;
1294 ret = xmlNanoHTTPConnectHost(proxy, proxyPort);
1295 }
1296 else {
1297 blen = strlen(ctxt->hostname);
1298 ret = xmlNanoHTTPConnectHost(ctxt->hostname, ctxt->port);
1299 }
1300 if (ret < 0) {
1301 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001302 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001303 return(NULL);
1304 }
1305 ctxt->fd = ret;
1306
Daniel Veillardf012a642001-07-23 19:10:52 +00001307 if (input == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00001308 ilen = 0;
Daniel Veillardf012a642001-07-23 19:10:52 +00001309 else
1310 blen += 36;
1311
Owen Taylor3473f882001-02-23 17:55:21 +00001312 if (headers != NULL)
Daniel Veillardf012a642001-07-23 19:10:52 +00001313 blen += strlen(headers) + 2;
Owen Taylor3473f882001-02-23 17:55:21 +00001314 if (contentType && *contentType)
1315 blen += strlen(*contentType) + 16;
Daniel Veillardf012a642001-07-23 19:10:52 +00001316 blen += strlen(method) + strlen(ctxt->path) + 24;
Daniel Veillard3c908dc2003-04-19 00:07:51 +00001317 bp = xmlMallocAtomic(blen);
Daniel Veillardf012a642001-07-23 19:10:52 +00001318 if ( bp == NULL ) {
1319 xmlNanoHTTPFreeCtxt( ctxt );
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001320 xmlHTTPErrMemory("allocating header buffer");
Daniel Veillardf012a642001-07-23 19:10:52 +00001321 return ( NULL );
1322 }
1323
1324 p = bp;
1325
Owen Taylor3473f882001-02-23 17:55:21 +00001326 if (proxy) {
1327 if (ctxt->port != 80) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001328 p += snprintf( p, blen - (p - bp), "%s http://%s:%d%s",
1329 method, ctxt->hostname,
Daniel Veillardf012a642001-07-23 19:10:52 +00001330 ctxt->port, ctxt->path );
Owen Taylor3473f882001-02-23 17:55:21 +00001331 }
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001332 else
1333 p += snprintf( p, blen - (p - bp), "%s http://%s%s", method,
Daniel Veillardf012a642001-07-23 19:10:52 +00001334 ctxt->hostname, ctxt->path);
Owen Taylor3473f882001-02-23 17:55:21 +00001335 }
1336 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001337 p += snprintf( p, blen - (p - bp), "%s %s", method, ctxt->path);
Daniel Veillardf012a642001-07-23 19:10:52 +00001338
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001339 p += snprintf( p, blen - (p - bp), " HTTP/1.0\r\nHost: %s\r\n",
1340 ctxt->hostname);
Daniel Veillardf012a642001-07-23 19:10:52 +00001341
1342 if (contentType != NULL && *contentType)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001343 p += snprintf(p, blen - (p - bp), "Content-Type: %s\r\n", *contentType);
Daniel Veillardf012a642001-07-23 19:10:52 +00001344
1345 if (headers != NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001346 p += snprintf( p, blen - (p - bp), "%s", headers );
Daniel Veillardf012a642001-07-23 19:10:52 +00001347
Owen Taylor3473f882001-02-23 17:55:21 +00001348 if (input != NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001349 snprintf(p, blen - (p - bp), "Content-Length: %d\r\n\r\n", ilen );
Owen Taylor3473f882001-02-23 17:55:21 +00001350 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001351 snprintf(p, blen - (p - bp), "\r\n");
Daniel Veillardf012a642001-07-23 19:10:52 +00001352
Owen Taylor3473f882001-02-23 17:55:21 +00001353#ifdef DEBUG_HTTP
1354 xmlGenericError(xmlGenericErrorContext,
1355 "-> %s%s", proxy? "(Proxy) " : "", bp);
1356 if ((blen -= strlen(bp)+1) < 0)
1357 xmlGenericError(xmlGenericErrorContext,
1358 "ERROR: overflowed buffer by %d bytes\n", -blen);
1359#endif
1360 ctxt->outptr = ctxt->out = bp;
1361 ctxt->state = XML_NANO_HTTP_WRITE;
Daniel Veillardf012a642001-07-23 19:10:52 +00001362 blen = strlen( ctxt->out );
Daniel Veillardf012a642001-07-23 19:10:52 +00001363#ifdef DEBUG_HTTP
William M. Brack78637da2003-07-31 14:47:38 +00001364 xmt_bytes = xmlNanoHTTPSend(ctxt, ctxt->out, blen );
Daniel Veillardf012a642001-07-23 19:10:52 +00001365 if ( xmt_bytes != blen )
1366 xmlGenericError( xmlGenericErrorContext,
1367 "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
1368 xmt_bytes, blen,
1369 "bytes of HTTP headers sent to host",
1370 ctxt->hostname );
William M. Brack78637da2003-07-31 14:47:38 +00001371#else
1372 xmlNanoHTTPSend(ctxt, ctxt->out, blen );
Daniel Veillardf012a642001-07-23 19:10:52 +00001373#endif
1374
1375 if ( input != NULL ) {
William M. Brack78637da2003-07-31 14:47:38 +00001376#ifdef DEBUG_HTTP
Daniel Veillardf012a642001-07-23 19:10:52 +00001377 xmt_bytes = xmlNanoHTTPSend( ctxt, input, ilen );
1378
Daniel Veillardf012a642001-07-23 19:10:52 +00001379 if ( xmt_bytes != ilen )
1380 xmlGenericError( xmlGenericErrorContext,
1381 "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
1382 xmt_bytes, ilen,
1383 "bytes of HTTP content sent to host",
1384 ctxt->hostname );
William M. Brack78637da2003-07-31 14:47:38 +00001385#else
1386 xmlNanoHTTPSend( ctxt, input, ilen );
Daniel Veillardf012a642001-07-23 19:10:52 +00001387#endif
1388 }
1389
Owen Taylor3473f882001-02-23 17:55:21 +00001390 ctxt->state = XML_NANO_HTTP_READ;
1391 head = 1;
1392
1393 while ((p = xmlNanoHTTPReadLine(ctxt)) != NULL) {
1394 if (head && (*p == 0)) {
1395 head = 0;
1396 ctxt->content = ctxt->inrptr;
1397 xmlFree(p);
1398 break;
1399 }
1400 xmlNanoHTTPScanAnswer(ctxt, p);
1401
1402#ifdef DEBUG_HTTP
1403 xmlGenericError(xmlGenericErrorContext, "<- %s\n", p);
1404#endif
1405 xmlFree(p);
1406 }
1407
1408 if ((ctxt->location != NULL) && (ctxt->returnValue >= 300) &&
1409 (ctxt->returnValue < 400)) {
1410#ifdef DEBUG_HTTP
1411 xmlGenericError(xmlGenericErrorContext,
1412 "\nRedirect to: %s\n", ctxt->location);
1413#endif
Daniel Veillardf012a642001-07-23 19:10:52 +00001414 while ( xmlNanoHTTPRecv(ctxt) > 0 ) ;
Owen Taylor3473f882001-02-23 17:55:21 +00001415 if (nbRedirects < XML_NANO_HTTP_MAX_REDIR) {
1416 nbRedirects++;
Daniel Veillard9403a042001-05-28 11:00:53 +00001417 if (redirURL != NULL)
1418 xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001419 redirURL = xmlMemStrdup(ctxt->location);
1420 xmlNanoHTTPFreeCtxt(ctxt);
1421 goto retry;
1422 }
1423 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001424 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001425#ifdef DEBUG_HTTP
1426 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardf012a642001-07-23 19:10:52 +00001427 "xmlNanoHTTPMethodRedir: Too many redirects, aborting ...\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001428#endif
1429 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001430 }
1431
1432 if (contentType != NULL) {
1433 if (ctxt->contentType != NULL)
1434 *contentType = xmlMemStrdup(ctxt->contentType);
1435 else
1436 *contentType = NULL;
1437 }
1438
Daniel Veillard9403a042001-05-28 11:00:53 +00001439 if ((redir != NULL) && (redirURL != NULL)) {
1440 *redir = redirURL;
1441 } else {
1442 if (redirURL != NULL)
1443 xmlFree(redirURL);
1444 if (redir != NULL)
1445 *redir = NULL;
1446 }
1447
Owen Taylor3473f882001-02-23 17:55:21 +00001448#ifdef DEBUG_HTTP
1449 if (ctxt->contentType != NULL)
1450 xmlGenericError(xmlGenericErrorContext,
1451 "\nCode %d, content-type '%s'\n\n",
1452 ctxt->returnValue, ctxt->contentType);
1453 else
1454 xmlGenericError(xmlGenericErrorContext,
1455 "\nCode %d, no content-type\n\n",
1456 ctxt->returnValue);
1457#endif
1458
1459 return((void *) ctxt);
1460}
1461
1462/**
Daniel Veillard9403a042001-05-28 11:00:53 +00001463 * xmlNanoHTTPMethod:
1464 * @URL: The URL to load
1465 * @method: the HTTP method to use
1466 * @input: the input string if any
1467 * @contentType: the Content-Type information IN and OUT
1468 * @headers: the extra headers
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001469 * @ilen: input length
Daniel Veillard9403a042001-05-28 11:00:53 +00001470 *
1471 * This function try to open a connection to the indicated resource
1472 * via HTTP using the given @method, adding the given extra headers
1473 * and the input buffer for the request content.
1474 *
1475 * Returns NULL in case of failure, otherwise a request handler.
1476 * The contentType, if provided must be freed by the caller
1477 */
1478
1479void*
1480xmlNanoHTTPMethod(const char *URL, const char *method, const char *input,
Daniel Veillardf012a642001-07-23 19:10:52 +00001481 char **contentType, const char *headers, int ilen) {
Daniel Veillard9403a042001-05-28 11:00:53 +00001482 return(xmlNanoHTTPMethodRedir(URL, method, input, contentType,
Daniel Veillardf012a642001-07-23 19:10:52 +00001483 NULL, headers, ilen));
Daniel Veillard9403a042001-05-28 11:00:53 +00001484}
1485
1486/**
Owen Taylor3473f882001-02-23 17:55:21 +00001487 * xmlNanoHTTPFetch:
1488 * @URL: The URL to load
1489 * @filename: the filename where the content should be saved
1490 * @contentType: if available the Content-Type information will be
1491 * returned at that location
1492 *
1493 * This function try to fetch the indicated resource via HTTP GET
1494 * and save it's content in the file.
1495 *
1496 * Returns -1 in case of failure, 0 incase of success. The contentType,
1497 * if provided must be freed by the caller
1498 */
1499int
1500xmlNanoHTTPFetch(const char *URL, const char *filename, char **contentType) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001501 void *ctxt = NULL;
1502 char *buf = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001503 int fd;
1504 int len;
1505
1506 ctxt = xmlNanoHTTPOpen(URL, contentType);
1507 if (ctxt == NULL) return(-1);
1508
1509 if (!strcmp(filename, "-"))
1510 fd = 0;
1511 else {
1512 fd = open(filename, O_CREAT | O_WRONLY, 00644);
1513 if (fd < 0) {
1514 xmlNanoHTTPClose(ctxt);
1515 if ((contentType != NULL) && (*contentType != NULL)) {
1516 xmlFree(*contentType);
1517 *contentType = NULL;
1518 }
1519 return(-1);
1520 }
1521 }
1522
Daniel Veillardf012a642001-07-23 19:10:52 +00001523 xmlNanoHTTPFetchContent( ctxt, &buf, &len );
1524 if ( len > 0 ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001525 write(fd, buf, len);
1526 }
1527
1528 xmlNanoHTTPClose(ctxt);
1529 close(fd);
1530 return(0);
1531}
1532
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001533#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001534/**
1535 * xmlNanoHTTPSave:
1536 * @ctxt: the HTTP context
1537 * @filename: the filename where the content should be saved
1538 *
1539 * This function saves the output of the HTTP transaction to a file
1540 * It closes and free the context at the end
1541 *
1542 * Returns -1 in case of failure, 0 incase of success.
1543 */
1544int
1545xmlNanoHTTPSave(void *ctxt, const char *filename) {
Daniel Veillarde3924972001-07-25 20:25:21 +00001546 char *buf = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001547 int fd;
1548 int len;
1549
1550 if (ctxt == NULL) return(-1);
1551
1552 if (!strcmp(filename, "-"))
1553 fd = 0;
1554 else {
1555 fd = open(filename, O_CREAT | O_WRONLY);
1556 if (fd < 0) {
1557 xmlNanoHTTPClose(ctxt);
1558 return(-1);
1559 }
1560 }
1561
Daniel Veillardf012a642001-07-23 19:10:52 +00001562 xmlNanoHTTPFetchContent( ctxt, &buf, &len );
1563 if ( len > 0 ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001564 write(fd, buf, len);
1565 }
1566
1567 xmlNanoHTTPClose(ctxt);
1568 return(0);
1569}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001570#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001571
1572/**
1573 * xmlNanoHTTPReturnCode:
1574 * @ctx: the HTTP context
1575 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001576 * Get the latest HTTP return code received
1577 *
Owen Taylor3473f882001-02-23 17:55:21 +00001578 * Returns the HTTP return code for the request.
1579 */
1580int
1581xmlNanoHTTPReturnCode(void *ctx) {
1582 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1583
1584 if (ctxt == NULL) return(-1);
1585
1586 return(ctxt->returnValue);
1587}
1588
1589/**
1590 * xmlNanoHTTPAuthHeader:
1591 * @ctx: the HTTP context
1592 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001593 * Get the authentication header of an HTTP context
1594 *
Owen Taylor3473f882001-02-23 17:55:21 +00001595 * Returns the stashed value of the WWW-Authenticate or Proxy-Authenticate
1596 * header.
1597 */
1598const char *
1599xmlNanoHTTPAuthHeader(void *ctx) {
1600 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1601
1602 if (ctxt == NULL) return(NULL);
1603
1604 return(ctxt->authHeader);
1605}
1606
Daniel Veillardf012a642001-07-23 19:10:52 +00001607/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00001608 * xmlNanoHTTPContentLength:
Daniel Veillardf012a642001-07-23 19:10:52 +00001609 * @ctx: the HTTP context
1610 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001611 * Provides the specified content length from the HTTP header.
1612 *
Daniel Veillardf012a642001-07-23 19:10:52 +00001613 * Return the specified content length from the HTTP header. Note that
1614 * a value of -1 indicates that the content length element was not included in
1615 * the response header.
1616 */
1617int
1618xmlNanoHTTPContentLength( void * ctx ) {
1619 xmlNanoHTTPCtxtPtr ctxt = ctx;
1620
1621 return ( ( ctxt == NULL ) ? -1 : ctxt->ContentLength );
1622}
1623
1624/**
Daniel Veillard847332a2003-10-18 11:29:40 +00001625 * xmlNanoHTTPRedir:
1626 * @ctx: the HTTP context
1627 *
1628 * Provides the specified redirection URL if available from the HTTP header.
1629 *
1630 * Return the specified redirection URL or NULL if not redirected.
1631 */
1632const char *
1633xmlNanoHTTPRedir( void * ctx ) {
1634 xmlNanoHTTPCtxtPtr ctxt = ctx;
1635
1636 return ( ( ctxt == NULL ) ? NULL : ctxt->location );
1637}
1638
1639/**
1640 * xmlNanoHTTPEncoding:
1641 * @ctx: the HTTP context
1642 *
1643 * Provides the specified encoding if specified in the HTTP headers.
1644 *
1645 * Return the specified encoding or NULL if not available
1646 */
1647const char *
1648xmlNanoHTTPEncoding( void * ctx ) {
1649 xmlNanoHTTPCtxtPtr ctxt = ctx;
1650
1651 return ( ( ctxt == NULL ) ? NULL : ctxt->encoding );
1652}
1653
1654/**
Daniel Veillarda840b692003-10-19 13:35:37 +00001655 * xmlNanoHTTPMimeType:
1656 * @ctx: the HTTP context
1657 *
1658 * Provides the specified Mime-Type if specified in the HTTP headers.
1659 *
1660 * Return the specified Mime-Type or NULL if not available
1661 */
1662const char *
1663xmlNanoHTTPMimeType( void * ctx ) {
1664 xmlNanoHTTPCtxtPtr ctxt = ctx;
1665
1666 return ( ( ctxt == NULL ) ? NULL : ctxt->mimeType );
1667}
1668
1669/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00001670 * xmlNanoHTTPFetchContent:
Daniel Veillardf012a642001-07-23 19:10:52 +00001671 * @ctx: the HTTP context
1672 * @ptr: pointer to set to the content buffer.
1673 * @len: integer pointer to hold the length of the content
1674 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001675 * Check if all the content was read
1676 *
Daniel Veillardf012a642001-07-23 19:10:52 +00001677 * Returns 0 if all the content was read and available, returns
1678 * -1 if received content length was less than specified or an error
1679 * occurred.
1680 */
1681int
1682xmlNanoHTTPFetchContent( void * ctx, char ** ptr, int * len ) {
1683 xmlNanoHTTPCtxtPtr ctxt = ctx;
1684
1685 int rc = 0;
1686 int cur_lgth;
1687 int rcvd_lgth;
1688 int dummy_int;
1689 char * dummy_ptr = NULL;
1690
1691 /* Dummy up return input parameters if not provided */
1692
1693 if ( len == NULL )
1694 len = &dummy_int;
1695
1696 if ( ptr == NULL )
1697 ptr = &dummy_ptr;
1698
1699 /* But can't work without the context pointer */
1700
1701 if ( ( ctxt == NULL ) || ( ctxt->content == NULL ) ) {
1702 *len = 0;
1703 *ptr = NULL;
1704 return ( -1 );
1705 }
1706
1707 rcvd_lgth = ctxt->inptr - ctxt->content;
1708
1709 while ( (cur_lgth = xmlNanoHTTPRecv( ctxt )) > 0 ) {
1710
1711 rcvd_lgth += cur_lgth;
1712 if ( (ctxt->ContentLength > 0) && (rcvd_lgth >= ctxt->ContentLength) )
1713 break;
1714 }
1715
1716 *ptr = ctxt->content;
1717 *len = rcvd_lgth;
1718
1719 if ( ( ctxt->ContentLength > 0 ) && ( rcvd_lgth < ctxt->ContentLength ) )
1720 rc = -1;
1721 else if ( rcvd_lgth == 0 )
1722 rc = -1;
1723
1724 return ( rc );
1725}
1726
Owen Taylor3473f882001-02-23 17:55:21 +00001727#ifdef STANDALONE
1728int main(int argc, char **argv) {
1729 char *contentType = NULL;
1730
1731 if (argv[1] != NULL) {
1732 if (argv[2] != NULL)
1733 xmlNanoHTTPFetch(argv[1], argv[2], &contentType);
1734 else
1735 xmlNanoHTTPFetch(argv[1], "-", &contentType);
1736 if (contentType != NULL) xmlFree(contentType);
1737 } else {
1738 xmlGenericError(xmlGenericErrorContext,
1739 "%s: minimal HTTP GET implementation\n", argv[0]);
1740 xmlGenericError(xmlGenericErrorContext,
1741 "\tusage %s [ URL [ filename ] ]\n", argv[0]);
1742 }
1743 xmlNanoHTTPCleanup();
1744 xmlMemoryDump();
1745 return(0);
1746}
1747#endif /* STANDALONE */
1748#else /* !LIBXML_HTTP_ENABLED */
1749#ifdef STANDALONE
1750#include <stdio.h>
1751int main(int argc, char **argv) {
1752 xmlGenericError(xmlGenericErrorContext,
1753 "%s : HTTP support not compiled in\n", argv[0]);
1754 return(0);
1755}
1756#endif /* STANDALONE */
1757#endif /* LIBXML_HTTP_ENABLED */