blob: e43c37bc41025836c66e73998aa3d836afba9d58 [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;
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000875#ifdef _WINSOCKAPI_
876 fd_set xfd;
877#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000878 struct timeval tv;
879 int status;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000880 int addrlen;
881 SOCKET s;
Owen Taylor3473f882001-02-23 17:55:21 +0000882
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000883#ifdef SUPPORT_IP6
884 if (addr->sa_family == AF_INET6) {
885 s = socket (PF_INET6, SOCK_STREAM, IPPROTO_TCP);
886 addrlen = sizeof (struct sockaddr_in6);
887 }
888 else
889#endif
890 {
891 s = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
892 addrlen = sizeof (struct sockaddr_in);
893 }
Owen Taylor3473f882001-02-23 17:55:21 +0000894 if (s==-1) {
895#ifdef DEBUG_HTTP
896 perror("socket");
897#endif
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000898 __xmlIOErr(XML_FROM_HTTP, 0, "socket failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000899 return(-1);
900 }
901
902#ifdef _WINSOCKAPI_
903 {
904 u_long one = 1;
905
906 status = ioctlsocket(s, FIONBIO, &one) == SOCKET_ERROR ? -1 : 0;
907 }
908#else /* _WINSOCKAPI_ */
909#if defined(VMS)
910 {
911 int enable = 1;
912 status = ioctl(s, FIONBIO, &enable);
913 }
914#else /* VMS */
Daniel Veillard254b1262003-11-01 17:04:58 +0000915#if defined(__BEOS__)
916 {
917 bool noblock = true;
918 status = setsockopt(s, SOL_SOCKET, SO_NONBLOCK, &noblock, sizeof(noblock));
919 }
920#else /* __BEOS__ */
Owen Taylor3473f882001-02-23 17:55:21 +0000921 if ((status = fcntl(s, F_GETFL, 0)) != -1) {
922#ifdef O_NONBLOCK
923 status |= O_NONBLOCK;
924#else /* O_NONBLOCK */
925#ifdef F_NDELAY
926 status |= F_NDELAY;
927#endif /* F_NDELAY */
928#endif /* !O_NONBLOCK */
929 status = fcntl(s, F_SETFL, status);
930 }
931 if (status < 0) {
932#ifdef DEBUG_HTTP
933 perror("nonblocking");
934#endif
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000935 __xmlIOErr(XML_FROM_HTTP, 0, "error setting non-blocking IO\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000936 closesocket(s);
937 return(-1);
938 }
Daniel Veillard254b1262003-11-01 17:04:58 +0000939#endif /* !__BEOS__ */
Owen Taylor3473f882001-02-23 17:55:21 +0000940#endif /* !VMS */
941#endif /* !_WINSOCKAPI_ */
942
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000943 if (connect (s, addr, addrlen) == -1) {
Owen Taylor3473f882001-02-23 17:55:21 +0000944 switch (socket_errno()) {
945 case EINPROGRESS:
946 case EWOULDBLOCK:
947 break;
948 default:
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000949 __xmlIOErr(XML_FROM_HTTP, 0, "error connecting to HTTP server");
Owen Taylor3473f882001-02-23 17:55:21 +0000950 closesocket(s);
951 return(-1);
952 }
953 }
954
955 tv.tv_sec = timeout;
956 tv.tv_usec = 0;
957
958 FD_ZERO(&wfd);
959 FD_SET(s, &wfd);
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000960
961#ifdef _WINSOCKAPI_
962 FD_ZERO(&xfd);
963 FD_SET(s, &xfd);
Owen Taylor3473f882001-02-23 17:55:21 +0000964
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000965 switch(select(s+1, NULL, &wfd, &xfd, &tv))
966#else
Owen Taylor3473f882001-02-23 17:55:21 +0000967 switch(select(s+1, NULL, &wfd, NULL, &tv))
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000968#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000969 {
970 case 0:
971 /* Time out */
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000972 __xmlIOErr(XML_FROM_HTTP, 0, "Connect attempt timed out");
Owen Taylor3473f882001-02-23 17:55:21 +0000973 closesocket(s);
974 return(-1);
975 case -1:
976 /* Ermm.. ?? */
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000977 __xmlIOErr(XML_FROM_HTTP, 0, "Connect failed");
Owen Taylor3473f882001-02-23 17:55:21 +0000978 closesocket(s);
979 return(-1);
980 }
981
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000982 if ( FD_ISSET(s, &wfd)
983#ifdef _WINSOCKAPI_
984 || FD_ISSET(s, &xfd)
985#endif
986 ) {
Owen Taylor3473f882001-02-23 17:55:21 +0000987 SOCKLEN_T len;
988 len = sizeof(status);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000989#ifdef SO_ERROR
Owen Taylor3473f882001-02-23 17:55:21 +0000990 if (getsockopt(s, SOL_SOCKET, SO_ERROR, (char*)&status, &len) < 0 ) {
991 /* Solaris error code */
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000992 __xmlIOErr(XML_FROM_HTTP, 0, "getsockopt failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000993 return (-1);
994 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000995#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000996 if ( status ) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000997 __xmlIOErr(XML_FROM_HTTP, 0, "Error connecting to remote host");
Owen Taylor3473f882001-02-23 17:55:21 +0000998 closesocket(s);
999 errno = status;
1000 return (-1);
1001 }
1002 } else {
1003 /* pbm */
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001004 __xmlIOErr(XML_FROM_HTTP, 0, "select failed\n");
Daniel Veillardf012a642001-07-23 19:10:52 +00001005 closesocket(s);
Owen Taylor3473f882001-02-23 17:55:21 +00001006 return (-1);
1007 }
1008
1009 return(s);
1010}
1011
1012/**
1013 * xmlNanoHTTPConnectHost:
1014 * @host: the host name
1015 * @port: the port number
1016 *
1017 * Attempt a connection to the given host:port endpoint. It tries
1018 * the multiple IP provided by the DNS if available.
1019 *
1020 * Returns -1 in case of failure, the file descriptor number otherwise
1021 */
1022
1023static int
1024xmlNanoHTTPConnectHost(const char *host, int port)
1025{
1026 struct hostent *h;
Daniel Veillard2db8c122003-07-08 12:16:59 +00001027 struct sockaddr *addr = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001028 struct in_addr ia;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001029 struct sockaddr_in sockin;
Daniel Veillard5c396542002-03-15 07:57:50 +00001030
Owen Taylor3473f882001-02-23 17:55:21 +00001031#ifdef SUPPORT_IP6
1032 struct in6_addr ia6;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001033 struct sockaddr_in6 sockin6;
Owen Taylor3473f882001-02-23 17:55:21 +00001034#endif
1035 int i;
1036 int s;
Daniel Veillard5c396542002-03-15 07:57:50 +00001037
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001038 memset (&sockin, 0, sizeof(sockin));
1039#ifdef SUPPORT_IP6
1040 memset (&sockin6, 0, sizeof(sockin6));
1041 if (have_ipv6 ())
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001042#if !defined(HAVE_GETADDRINFO) && defined(RES_USE_INET6)
Daniel Veillard560c2a42003-07-06 21:13:49 +00001043 {
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001044 if (!(_res.options & RES_INIT))
1045 res_init();
1046 _res.options |= RES_USE_INET6;
1047 }
1048#elif defined(HAVE_GETADDRINFO)
Daniel Veillard560c2a42003-07-06 21:13:49 +00001049 {
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001050 int status;
1051 struct addrinfo hints, *res, *result;
1052
1053 result = NULL;
1054 memset (&hints, 0,sizeof(hints));
1055 hints.ai_socktype = SOCK_STREAM;
1056
1057 status = getaddrinfo (host, NULL, &hints, &result);
1058 if (status) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001059 __xmlIOErr(XML_FROM_HTTP, 0, "getaddrinfo failed\n");
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001060 return (-1);
1061 }
1062
1063 for (res = result; res; res = res->ai_next) {
Daniel Veillard3dc93a42003-07-10 14:04:33 +00001064 if (res->ai_family == AF_INET || res->ai_family == AF_INET6) {
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001065 if (res->ai_family == AF_INET6) {
1066 memcpy (&sockin6, res->ai_addr, res->ai_addrlen);
1067 sockin6.sin6_port = htons (port);
1068 addr = (struct sockaddr *)&sockin6;
1069 }
Daniel Veillard3dc93a42003-07-10 14:04:33 +00001070 else {
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001071 memcpy (&sockin, res->ai_addr, res->ai_addrlen);
1072 sockin.sin_port = htons (port);
1073 addr = (struct sockaddr *)&sockin;
1074 }
1075
1076 s = xmlNanoHTTPConnectAttempt (addr);
1077 if (s != -1) {
1078 freeaddrinfo (result);
1079 return (s);
1080 }
1081 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001082 }
Daniel Veillard3dc93a42003-07-10 14:04:33 +00001083 if (result)
1084 freeaddrinfo (result);
1085 return (-1);
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001086 } else
Owen Taylor3473f882001-02-23 17:55:21 +00001087#endif
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001088#endif
1089 {
1090 h = gethostbyname (host);
1091 if (h == NULL) {
Daniel Veillard56b2db72002-03-25 16:35:28 +00001092
1093/*
1094 * Okay, I got fed up by the non-portability of this error message
1095 * extraction code. it work on Linux, if it work on your platform
1096 * and one want to enable it, send me the defined(foobar) needed
1097 */
1098#if defined(HAVE_NETDB_H) && defined(HOST_NOT_FOUND) && defined(linux)
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001099 const char *h_err_txt = "";
Daniel Veillardf012a642001-07-23 19:10:52 +00001100
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001101 switch (h_errno) {
1102 case HOST_NOT_FOUND:
1103 h_err_txt = "Authoritive host not found";
1104 break;
Daniel Veillardf012a642001-07-23 19:10:52 +00001105
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001106 case TRY_AGAIN:
1107 h_err_txt =
1108 "Non-authoritive host not found or server failure.";
1109 break;
Daniel Veillardf012a642001-07-23 19:10:52 +00001110
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001111 case NO_RECOVERY:
1112 h_err_txt =
1113 "Non-recoverable errors: FORMERR, REFUSED, or NOTIMP.";
1114 break;
Daniel Veillard5c396542002-03-15 07:57:50 +00001115
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001116 case NO_ADDRESS:
1117 h_err_txt =
1118 "Valid name, no data record of requested type.";
1119 break;
Daniel Veillard5c396542002-03-15 07:57:50 +00001120
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001121 default:
1122 h_err_txt = "No error text defined.";
1123 break;
1124 }
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001125 __xmlIOErr(XML_FROM_HTTP, 0, h_err_txt);
Daniel Veillard5c396542002-03-15 07:57:50 +00001126#else
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001127 __xmlIOErr(XML_FROM_HTTP, 0, "Failed to resolve host");
Owen Taylor3473f882001-02-23 17:55:21 +00001128#endif
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001129 return (-1);
1130 }
Daniel Veillard5c396542002-03-15 07:57:50 +00001131
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001132 for (i = 0; h->h_addr_list[i]; i++) {
1133 if (h->h_addrtype == AF_INET) {
1134 /* A records (IPv4) */
1135 memcpy (&ia, h->h_addr_list[i], h->h_length);
1136 sockin.sin_family = h->h_addrtype;
1137 sockin.sin_addr = ia;
1138 sockin.sin_port = htons (port);
1139 addr = (struct sockaddr *) &sockin;
Daniel Veillard5c396542002-03-15 07:57:50 +00001140#ifdef SUPPORT_IP6
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001141 } else if (have_ipv6 () && (h->h_addrtype == AF_INET6)) {
1142 /* AAAA records (IPv6) */
1143 memcpy (&ia6, h->h_addr_list[i], h->h_length);
1144 sockin6.sin6_family = h->h_addrtype;
1145 sockin6.sin6_addr = ia6;
1146 sockin6.sin6_port = htons (port);
1147 addr = (struct sockaddr *) &sockin6;
Daniel Veillard5c396542002-03-15 07:57:50 +00001148#endif
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001149 } else
1150 break; /* for */
Daniel Veillard5c396542002-03-15 07:57:50 +00001151
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001152 s = xmlNanoHTTPConnectAttempt (addr);
1153 if (s != -1)
1154 return (s);
1155 }
Owen Taylor3473f882001-02-23 17:55:21 +00001156 }
Owen Taylor3473f882001-02-23 17:55:21 +00001157#ifdef DEBUG_HTTP
1158 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard5c396542002-03-15 07:57:50 +00001159 "xmlNanoHTTPConnectHost: unable to connect to '%s'.\n",
1160 host);
Owen Taylor3473f882001-02-23 17:55:21 +00001161#endif
Daniel Veillard5c396542002-03-15 07:57:50 +00001162 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001163}
1164
1165
1166/**
1167 * xmlNanoHTTPOpen:
1168 * @URL: The URL to load
1169 * @contentType: if available the Content-Type information will be
1170 * returned at that location
1171 *
1172 * This function try to open a connection to the indicated resource
1173 * via HTTP GET.
1174 *
1175 * Returns NULL in case of failure, otherwise a request handler.
1176 * The contentType, if provided must be freed by the caller
1177 */
1178
1179void*
1180xmlNanoHTTPOpen(const char *URL, char **contentType) {
1181 if (contentType != NULL) *contentType = NULL;
Daniel Veillardf012a642001-07-23 19:10:52 +00001182 return(xmlNanoHTTPMethod(URL, NULL, NULL, contentType, NULL, 0));
Daniel Veillard9403a042001-05-28 11:00:53 +00001183}
1184
1185/**
1186 * xmlNanoHTTPOpenRedir:
1187 * @URL: The URL to load
1188 * @contentType: if available the Content-Type information will be
1189 * returned at that location
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001190 * @redir: if available the redirected URL will be returned
Daniel Veillard9403a042001-05-28 11:00:53 +00001191 *
1192 * This function try to open a connection to the indicated resource
1193 * via HTTP GET.
1194 *
1195 * Returns NULL in case of failure, otherwise a request handler.
1196 * The contentType, if provided must be freed by the caller
1197 */
1198
1199void*
1200xmlNanoHTTPOpenRedir(const char *URL, char **contentType, char **redir) {
1201 if (contentType != NULL) *contentType = NULL;
1202 if (redir != NULL) *redir = NULL;
Daniel Veillardf012a642001-07-23 19:10:52 +00001203 return(xmlNanoHTTPMethodRedir(URL, NULL, NULL, contentType, redir, NULL,0));
Owen Taylor3473f882001-02-23 17:55:21 +00001204}
1205
1206/**
1207 * xmlNanoHTTPRead:
1208 * @ctx: the HTTP context
1209 * @dest: a buffer
1210 * @len: the buffer length
1211 *
1212 * This function tries to read @len bytes from the existing HTTP connection
1213 * and saves them in @dest. This is a blocking call.
1214 *
1215 * Returns the number of byte read. 0 is an indication of an end of connection.
1216 * -1 indicates a parameter error.
1217 */
1218int
1219xmlNanoHTTPRead(void *ctx, void *dest, int len) {
1220 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1221
1222 if (ctx == NULL) return(-1);
1223 if (dest == NULL) return(-1);
1224 if (len <= 0) return(0);
1225
1226 while (ctxt->inptr - ctxt->inrptr < len) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001227 if (xmlNanoHTTPRecv(ctxt) <= 0) break;
Owen Taylor3473f882001-02-23 17:55:21 +00001228 }
1229 if (ctxt->inptr - ctxt->inrptr < len)
1230 len = ctxt->inptr - ctxt->inrptr;
1231 memcpy(dest, ctxt->inrptr, len);
1232 ctxt->inrptr += len;
1233 return(len);
1234}
1235
1236/**
1237 * xmlNanoHTTPClose:
1238 * @ctx: the HTTP context
1239 *
1240 * This function closes an HTTP context, it ends up the connection and
1241 * free all data related to it.
1242 */
1243void
1244xmlNanoHTTPClose(void *ctx) {
1245 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1246
1247 if (ctx == NULL) return;
1248
1249 xmlNanoHTTPFreeCtxt(ctxt);
1250}
1251
1252/**
Daniel Veillard9403a042001-05-28 11:00:53 +00001253 * xmlNanoHTTPMethodRedir:
Owen Taylor3473f882001-02-23 17:55:21 +00001254 * @URL: The URL to load
1255 * @method: the HTTP method to use
1256 * @input: the input string if any
1257 * @contentType: the Content-Type information IN and OUT
Daniel Veillard9403a042001-05-28 11:00:53 +00001258 * @redir: the redirected URL OUT
Owen Taylor3473f882001-02-23 17:55:21 +00001259 * @headers: the extra headers
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001260 * @ilen: input length
Owen Taylor3473f882001-02-23 17:55:21 +00001261 *
1262 * This function try to open a connection to the indicated resource
1263 * via HTTP using the given @method, adding the given extra headers
1264 * and the input buffer for the request content.
1265 *
1266 * Returns NULL in case of failure, otherwise a request handler.
Daniel Veillard9403a042001-05-28 11:00:53 +00001267 * The contentType, or redir, if provided must be freed by the caller
Owen Taylor3473f882001-02-23 17:55:21 +00001268 */
1269
1270void*
Daniel Veillard9403a042001-05-28 11:00:53 +00001271xmlNanoHTTPMethodRedir(const char *URL, const char *method, const char *input,
Daniel Veillardf012a642001-07-23 19:10:52 +00001272 char **contentType, char **redir,
1273 const char *headers, int ilen ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001274 xmlNanoHTTPCtxtPtr ctxt;
1275 char *bp, *p;
Daniel Veillardf012a642001-07-23 19:10:52 +00001276 int blen, ret;
Owen Taylor3473f882001-02-23 17:55:21 +00001277 int head;
1278 int nbRedirects = 0;
1279 char *redirURL = NULL;
William M. Brack78637da2003-07-31 14:47:38 +00001280#ifdef DEBUG_HTTP
1281 int xmt_bytes;
1282#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001283
1284 if (URL == NULL) return(NULL);
1285 if (method == NULL) method = "GET";
1286 xmlNanoHTTPInit();
1287
1288retry:
1289 if (redirURL == NULL)
1290 ctxt = xmlNanoHTTPNewCtxt(URL);
1291 else {
1292 ctxt = xmlNanoHTTPNewCtxt(redirURL);
Daniel Veillarda840b692003-10-19 13:35:37 +00001293 ctxt->location = xmlMemStrdup(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001294 }
1295
Daniel Veillardf012a642001-07-23 19:10:52 +00001296 if ( ctxt == NULL ) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001297 return ( NULL );
1298 }
1299
Owen Taylor3473f882001-02-23 17:55:21 +00001300 if ((ctxt->protocol == NULL) || (strcmp(ctxt->protocol, "http"))) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001301 __xmlIOErr(XML_FROM_HTTP, XML_HTTP_URL_SYNTAX, "Not a valid HTTP URI");
Owen Taylor3473f882001-02-23 17:55:21 +00001302 xmlNanoHTTPFreeCtxt(ctxt);
1303 if (redirURL != NULL) xmlFree(redirURL);
1304 return(NULL);
1305 }
1306 if (ctxt->hostname == NULL) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001307 __xmlIOErr(XML_FROM_HTTP, XML_HTTP_UNKNOWN_HOST,
1308 "Failed to identify host in URI");
Owen Taylor3473f882001-02-23 17:55:21 +00001309 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001310 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001311 return(NULL);
1312 }
1313 if (proxy) {
1314 blen = strlen(ctxt->hostname) * 2 + 16;
1315 ret = xmlNanoHTTPConnectHost(proxy, proxyPort);
1316 }
1317 else {
1318 blen = strlen(ctxt->hostname);
1319 ret = xmlNanoHTTPConnectHost(ctxt->hostname, ctxt->port);
1320 }
1321 if (ret < 0) {
1322 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001323 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001324 return(NULL);
1325 }
1326 ctxt->fd = ret;
1327
Daniel Veillardf012a642001-07-23 19:10:52 +00001328 if (input == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00001329 ilen = 0;
Daniel Veillardf012a642001-07-23 19:10:52 +00001330 else
1331 blen += 36;
1332
Owen Taylor3473f882001-02-23 17:55:21 +00001333 if (headers != NULL)
Daniel Veillardf012a642001-07-23 19:10:52 +00001334 blen += strlen(headers) + 2;
Owen Taylor3473f882001-02-23 17:55:21 +00001335 if (contentType && *contentType)
1336 blen += strlen(*contentType) + 16;
Daniel Veillardf012a642001-07-23 19:10:52 +00001337 blen += strlen(method) + strlen(ctxt->path) + 24;
Daniel Veillard82cb3192003-10-29 13:39:15 +00001338 bp = (char*)xmlMallocAtomic(blen);
Daniel Veillardf012a642001-07-23 19:10:52 +00001339 if ( bp == NULL ) {
1340 xmlNanoHTTPFreeCtxt( ctxt );
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001341 xmlHTTPErrMemory("allocating header buffer");
Daniel Veillardf012a642001-07-23 19:10:52 +00001342 return ( NULL );
1343 }
1344
1345 p = bp;
1346
Owen Taylor3473f882001-02-23 17:55:21 +00001347 if (proxy) {
1348 if (ctxt->port != 80) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001349 p += snprintf( p, blen - (p - bp), "%s http://%s:%d%s",
1350 method, ctxt->hostname,
Daniel Veillardf012a642001-07-23 19:10:52 +00001351 ctxt->port, ctxt->path );
Owen Taylor3473f882001-02-23 17:55:21 +00001352 }
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001353 else
1354 p += snprintf( p, blen - (p - bp), "%s http://%s%s", method,
Daniel Veillardf012a642001-07-23 19:10:52 +00001355 ctxt->hostname, ctxt->path);
Owen Taylor3473f882001-02-23 17:55:21 +00001356 }
1357 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001358 p += snprintf( p, blen - (p - bp), "%s %s", method, ctxt->path);
Daniel Veillardf012a642001-07-23 19:10:52 +00001359
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001360 p += snprintf( p, blen - (p - bp), " HTTP/1.0\r\nHost: %s\r\n",
1361 ctxt->hostname);
Daniel Veillardf012a642001-07-23 19:10:52 +00001362
1363 if (contentType != NULL && *contentType)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001364 p += snprintf(p, blen - (p - bp), "Content-Type: %s\r\n", *contentType);
Daniel Veillardf012a642001-07-23 19:10:52 +00001365
1366 if (headers != NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001367 p += snprintf( p, blen - (p - bp), "%s", headers );
Daniel Veillardf012a642001-07-23 19:10:52 +00001368
Owen Taylor3473f882001-02-23 17:55:21 +00001369 if (input != NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001370 snprintf(p, blen - (p - bp), "Content-Length: %d\r\n\r\n", ilen );
Owen Taylor3473f882001-02-23 17:55:21 +00001371 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001372 snprintf(p, blen - (p - bp), "\r\n");
Daniel Veillardf012a642001-07-23 19:10:52 +00001373
Owen Taylor3473f882001-02-23 17:55:21 +00001374#ifdef DEBUG_HTTP
1375 xmlGenericError(xmlGenericErrorContext,
1376 "-> %s%s", proxy? "(Proxy) " : "", bp);
1377 if ((blen -= strlen(bp)+1) < 0)
1378 xmlGenericError(xmlGenericErrorContext,
1379 "ERROR: overflowed buffer by %d bytes\n", -blen);
1380#endif
1381 ctxt->outptr = ctxt->out = bp;
1382 ctxt->state = XML_NANO_HTTP_WRITE;
Daniel Veillardf012a642001-07-23 19:10:52 +00001383 blen = strlen( ctxt->out );
Daniel Veillardf012a642001-07-23 19:10:52 +00001384#ifdef DEBUG_HTTP
William M. Brack78637da2003-07-31 14:47:38 +00001385 xmt_bytes = xmlNanoHTTPSend(ctxt, ctxt->out, blen );
Daniel Veillardf012a642001-07-23 19:10:52 +00001386 if ( xmt_bytes != blen )
1387 xmlGenericError( xmlGenericErrorContext,
1388 "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
1389 xmt_bytes, blen,
1390 "bytes of HTTP headers sent to host",
1391 ctxt->hostname );
William M. Brack78637da2003-07-31 14:47:38 +00001392#else
1393 xmlNanoHTTPSend(ctxt, ctxt->out, blen );
Daniel Veillardf012a642001-07-23 19:10:52 +00001394#endif
1395
1396 if ( input != NULL ) {
William M. Brack78637da2003-07-31 14:47:38 +00001397#ifdef DEBUG_HTTP
Daniel Veillardf012a642001-07-23 19:10:52 +00001398 xmt_bytes = xmlNanoHTTPSend( ctxt, input, ilen );
1399
Daniel Veillardf012a642001-07-23 19:10:52 +00001400 if ( xmt_bytes != ilen )
1401 xmlGenericError( xmlGenericErrorContext,
1402 "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
1403 xmt_bytes, ilen,
1404 "bytes of HTTP content sent to host",
1405 ctxt->hostname );
William M. Brack78637da2003-07-31 14:47:38 +00001406#else
1407 xmlNanoHTTPSend( ctxt, input, ilen );
Daniel Veillardf012a642001-07-23 19:10:52 +00001408#endif
1409 }
1410
Owen Taylor3473f882001-02-23 17:55:21 +00001411 ctxt->state = XML_NANO_HTTP_READ;
1412 head = 1;
1413
1414 while ((p = xmlNanoHTTPReadLine(ctxt)) != NULL) {
1415 if (head && (*p == 0)) {
1416 head = 0;
1417 ctxt->content = ctxt->inrptr;
1418 xmlFree(p);
1419 break;
1420 }
1421 xmlNanoHTTPScanAnswer(ctxt, p);
1422
1423#ifdef DEBUG_HTTP
1424 xmlGenericError(xmlGenericErrorContext, "<- %s\n", p);
1425#endif
1426 xmlFree(p);
1427 }
1428
1429 if ((ctxt->location != NULL) && (ctxt->returnValue >= 300) &&
1430 (ctxt->returnValue < 400)) {
1431#ifdef DEBUG_HTTP
1432 xmlGenericError(xmlGenericErrorContext,
1433 "\nRedirect to: %s\n", ctxt->location);
1434#endif
Daniel Veillardf012a642001-07-23 19:10:52 +00001435 while ( xmlNanoHTTPRecv(ctxt) > 0 ) ;
Owen Taylor3473f882001-02-23 17:55:21 +00001436 if (nbRedirects < XML_NANO_HTTP_MAX_REDIR) {
1437 nbRedirects++;
Daniel Veillard9403a042001-05-28 11:00:53 +00001438 if (redirURL != NULL)
1439 xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001440 redirURL = xmlMemStrdup(ctxt->location);
1441 xmlNanoHTTPFreeCtxt(ctxt);
1442 goto retry;
1443 }
1444 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001445 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001446#ifdef DEBUG_HTTP
1447 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardf012a642001-07-23 19:10:52 +00001448 "xmlNanoHTTPMethodRedir: Too many redirects, aborting ...\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001449#endif
1450 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001451 }
1452
1453 if (contentType != NULL) {
1454 if (ctxt->contentType != NULL)
1455 *contentType = xmlMemStrdup(ctxt->contentType);
1456 else
1457 *contentType = NULL;
1458 }
1459
Daniel Veillard9403a042001-05-28 11:00:53 +00001460 if ((redir != NULL) && (redirURL != NULL)) {
1461 *redir = redirURL;
1462 } else {
1463 if (redirURL != NULL)
1464 xmlFree(redirURL);
1465 if (redir != NULL)
1466 *redir = NULL;
1467 }
1468
Owen Taylor3473f882001-02-23 17:55:21 +00001469#ifdef DEBUG_HTTP
1470 if (ctxt->contentType != NULL)
1471 xmlGenericError(xmlGenericErrorContext,
1472 "\nCode %d, content-type '%s'\n\n",
1473 ctxt->returnValue, ctxt->contentType);
1474 else
1475 xmlGenericError(xmlGenericErrorContext,
1476 "\nCode %d, no content-type\n\n",
1477 ctxt->returnValue);
1478#endif
1479
1480 return((void *) ctxt);
1481}
1482
1483/**
Daniel Veillard9403a042001-05-28 11:00:53 +00001484 * xmlNanoHTTPMethod:
1485 * @URL: The URL to load
1486 * @method: the HTTP method to use
1487 * @input: the input string if any
1488 * @contentType: the Content-Type information IN and OUT
1489 * @headers: the extra headers
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001490 * @ilen: input length
Daniel Veillard9403a042001-05-28 11:00:53 +00001491 *
1492 * This function try to open a connection to the indicated resource
1493 * via HTTP using the given @method, adding the given extra headers
1494 * and the input buffer for the request content.
1495 *
1496 * Returns NULL in case of failure, otherwise a request handler.
1497 * The contentType, if provided must be freed by the caller
1498 */
1499
1500void*
1501xmlNanoHTTPMethod(const char *URL, const char *method, const char *input,
Daniel Veillardf012a642001-07-23 19:10:52 +00001502 char **contentType, const char *headers, int ilen) {
Daniel Veillard9403a042001-05-28 11:00:53 +00001503 return(xmlNanoHTTPMethodRedir(URL, method, input, contentType,
Daniel Veillardf012a642001-07-23 19:10:52 +00001504 NULL, headers, ilen));
Daniel Veillard9403a042001-05-28 11:00:53 +00001505}
1506
1507/**
Owen Taylor3473f882001-02-23 17:55:21 +00001508 * xmlNanoHTTPFetch:
1509 * @URL: The URL to load
1510 * @filename: the filename where the content should be saved
1511 * @contentType: if available the Content-Type information will be
1512 * returned at that location
1513 *
1514 * This function try to fetch the indicated resource via HTTP GET
1515 * and save it's content in the file.
1516 *
1517 * Returns -1 in case of failure, 0 incase of success. The contentType,
1518 * if provided must be freed by the caller
1519 */
1520int
1521xmlNanoHTTPFetch(const char *URL, const char *filename, char **contentType) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001522 void *ctxt = NULL;
1523 char *buf = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001524 int fd;
1525 int len;
1526
1527 ctxt = xmlNanoHTTPOpen(URL, contentType);
1528 if (ctxt == NULL) return(-1);
1529
1530 if (!strcmp(filename, "-"))
1531 fd = 0;
1532 else {
1533 fd = open(filename, O_CREAT | O_WRONLY, 00644);
1534 if (fd < 0) {
1535 xmlNanoHTTPClose(ctxt);
1536 if ((contentType != NULL) && (*contentType != NULL)) {
1537 xmlFree(*contentType);
1538 *contentType = NULL;
1539 }
1540 return(-1);
1541 }
1542 }
1543
Daniel Veillardf012a642001-07-23 19:10:52 +00001544 xmlNanoHTTPFetchContent( ctxt, &buf, &len );
1545 if ( len > 0 ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001546 write(fd, buf, len);
1547 }
1548
1549 xmlNanoHTTPClose(ctxt);
1550 close(fd);
1551 return(0);
1552}
1553
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001554#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001555/**
1556 * xmlNanoHTTPSave:
1557 * @ctxt: the HTTP context
1558 * @filename: the filename where the content should be saved
1559 *
1560 * This function saves the output of the HTTP transaction to a file
1561 * It closes and free the context at the end
1562 *
1563 * Returns -1 in case of failure, 0 incase of success.
1564 */
1565int
1566xmlNanoHTTPSave(void *ctxt, const char *filename) {
Daniel Veillarde3924972001-07-25 20:25:21 +00001567 char *buf = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001568 int fd;
1569 int len;
1570
1571 if (ctxt == NULL) return(-1);
1572
1573 if (!strcmp(filename, "-"))
1574 fd = 0;
1575 else {
1576 fd = open(filename, O_CREAT | O_WRONLY);
1577 if (fd < 0) {
1578 xmlNanoHTTPClose(ctxt);
1579 return(-1);
1580 }
1581 }
1582
Daniel Veillardf012a642001-07-23 19:10:52 +00001583 xmlNanoHTTPFetchContent( ctxt, &buf, &len );
1584 if ( len > 0 ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001585 write(fd, buf, len);
1586 }
1587
1588 xmlNanoHTTPClose(ctxt);
1589 return(0);
1590}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001591#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001592
1593/**
1594 * xmlNanoHTTPReturnCode:
1595 * @ctx: the HTTP context
1596 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001597 * Get the latest HTTP return code received
1598 *
Owen Taylor3473f882001-02-23 17:55:21 +00001599 * Returns the HTTP return code for the request.
1600 */
1601int
1602xmlNanoHTTPReturnCode(void *ctx) {
1603 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1604
1605 if (ctxt == NULL) return(-1);
1606
1607 return(ctxt->returnValue);
1608}
1609
1610/**
1611 * xmlNanoHTTPAuthHeader:
1612 * @ctx: the HTTP context
1613 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001614 * Get the authentication header of an HTTP context
1615 *
Owen Taylor3473f882001-02-23 17:55:21 +00001616 * Returns the stashed value of the WWW-Authenticate or Proxy-Authenticate
1617 * header.
1618 */
1619const char *
1620xmlNanoHTTPAuthHeader(void *ctx) {
1621 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1622
1623 if (ctxt == NULL) return(NULL);
1624
1625 return(ctxt->authHeader);
1626}
1627
Daniel Veillardf012a642001-07-23 19:10:52 +00001628/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00001629 * xmlNanoHTTPContentLength:
Daniel Veillardf012a642001-07-23 19:10:52 +00001630 * @ctx: the HTTP context
1631 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001632 * Provides the specified content length from the HTTP header.
1633 *
Daniel Veillardf012a642001-07-23 19:10:52 +00001634 * Return the specified content length from the HTTP header. Note that
1635 * a value of -1 indicates that the content length element was not included in
1636 * the response header.
1637 */
1638int
1639xmlNanoHTTPContentLength( void * ctx ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001640 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillardf012a642001-07-23 19:10:52 +00001641
1642 return ( ( ctxt == NULL ) ? -1 : ctxt->ContentLength );
1643}
1644
1645/**
Daniel Veillard847332a2003-10-18 11:29:40 +00001646 * xmlNanoHTTPRedir:
1647 * @ctx: the HTTP context
1648 *
1649 * Provides the specified redirection URL if available from the HTTP header.
1650 *
1651 * Return the specified redirection URL or NULL if not redirected.
1652 */
1653const char *
1654xmlNanoHTTPRedir( void * ctx ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001655 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillard847332a2003-10-18 11:29:40 +00001656
1657 return ( ( ctxt == NULL ) ? NULL : ctxt->location );
1658}
1659
1660/**
1661 * xmlNanoHTTPEncoding:
1662 * @ctx: the HTTP context
1663 *
1664 * Provides the specified encoding if specified in the HTTP headers.
1665 *
1666 * Return the specified encoding or NULL if not available
1667 */
1668const char *
1669xmlNanoHTTPEncoding( void * ctx ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001670 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillard847332a2003-10-18 11:29:40 +00001671
1672 return ( ( ctxt == NULL ) ? NULL : ctxt->encoding );
1673}
1674
1675/**
Daniel Veillarda840b692003-10-19 13:35:37 +00001676 * xmlNanoHTTPMimeType:
1677 * @ctx: the HTTP context
1678 *
1679 * Provides the specified Mime-Type if specified in the HTTP headers.
1680 *
1681 * Return the specified Mime-Type or NULL if not available
1682 */
1683const char *
1684xmlNanoHTTPMimeType( void * ctx ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001685 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillarda840b692003-10-19 13:35:37 +00001686
1687 return ( ( ctxt == NULL ) ? NULL : ctxt->mimeType );
1688}
1689
1690/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00001691 * xmlNanoHTTPFetchContent:
Daniel Veillardf012a642001-07-23 19:10:52 +00001692 * @ctx: the HTTP context
1693 * @ptr: pointer to set to the content buffer.
1694 * @len: integer pointer to hold the length of the content
1695 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001696 * Check if all the content was read
1697 *
Daniel Veillardf012a642001-07-23 19:10:52 +00001698 * Returns 0 if all the content was read and available, returns
1699 * -1 if received content length was less than specified or an error
1700 * occurred.
1701 */
1702int
1703xmlNanoHTTPFetchContent( void * ctx, char ** ptr, int * len ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001704 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillardf012a642001-07-23 19:10:52 +00001705
1706 int rc = 0;
1707 int cur_lgth;
1708 int rcvd_lgth;
1709 int dummy_int;
1710 char * dummy_ptr = NULL;
1711
1712 /* Dummy up return input parameters if not provided */
1713
1714 if ( len == NULL )
1715 len = &dummy_int;
1716
1717 if ( ptr == NULL )
1718 ptr = &dummy_ptr;
1719
1720 /* But can't work without the context pointer */
1721
1722 if ( ( ctxt == NULL ) || ( ctxt->content == NULL ) ) {
1723 *len = 0;
1724 *ptr = NULL;
1725 return ( -1 );
1726 }
1727
1728 rcvd_lgth = ctxt->inptr - ctxt->content;
1729
1730 while ( (cur_lgth = xmlNanoHTTPRecv( ctxt )) > 0 ) {
1731
1732 rcvd_lgth += cur_lgth;
1733 if ( (ctxt->ContentLength > 0) && (rcvd_lgth >= ctxt->ContentLength) )
1734 break;
1735 }
1736
1737 *ptr = ctxt->content;
1738 *len = rcvd_lgth;
1739
1740 if ( ( ctxt->ContentLength > 0 ) && ( rcvd_lgth < ctxt->ContentLength ) )
1741 rc = -1;
1742 else if ( rcvd_lgth == 0 )
1743 rc = -1;
1744
1745 return ( rc );
1746}
1747
Owen Taylor3473f882001-02-23 17:55:21 +00001748#ifdef STANDALONE
1749int main(int argc, char **argv) {
1750 char *contentType = NULL;
1751
1752 if (argv[1] != NULL) {
1753 if (argv[2] != NULL)
1754 xmlNanoHTTPFetch(argv[1], argv[2], &contentType);
1755 else
1756 xmlNanoHTTPFetch(argv[1], "-", &contentType);
1757 if (contentType != NULL) xmlFree(contentType);
1758 } else {
1759 xmlGenericError(xmlGenericErrorContext,
1760 "%s: minimal HTTP GET implementation\n", argv[0]);
1761 xmlGenericError(xmlGenericErrorContext,
1762 "\tusage %s [ URL [ filename ] ]\n", argv[0]);
1763 }
1764 xmlNanoHTTPCleanup();
1765 xmlMemoryDump();
1766 return(0);
1767}
1768#endif /* STANDALONE */
1769#else /* !LIBXML_HTTP_ENABLED */
1770#ifdef STANDALONE
1771#include <stdio.h>
1772int main(int argc, char **argv) {
1773 xmlGenericError(xmlGenericErrorContext,
1774 "%s : HTTP support not compiled in\n", argv[0]);
1775 return(0);
1776}
1777#endif /* STANDALONE */
1778#endif /* LIBXML_HTTP_ENABLED */