blob: 0a72683954afdb86d8a553a7182a3a6a73a0dc6d [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 Veillarda2351322004-06-27 12:08:10 +0000161static int xmlNanoHTTPFetchContent( void * ctx, char ** ptr, int * len );
Daniel Veillardf012a642001-07-23 19:10:52 +0000162
Owen Taylor3473f882001-02-23 17:55:21 +0000163/**
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000164 * xmlHTTPErrMemory:
165 * @extra: extra informations
166 *
167 * Handle an out of memory condition
168 */
169static void
170xmlHTTPErrMemory(const char *extra)
171{
172 __xmlSimpleError(XML_FROM_HTTP, XML_ERR_NO_MEMORY, NULL, NULL, extra);
173}
174
175/**
Owen Taylor3473f882001-02-23 17:55:21 +0000176 * A portability function
177 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000178static int socket_errno(void) {
Owen Taylor3473f882001-02-23 17:55:21 +0000179#ifdef _WINSOCKAPI_
180 return(WSAGetLastError());
181#else
182 return(errno);
183#endif
184}
185
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000186#ifdef SUPPORT_IP6
Daniel Veillard2db8c122003-07-08 12:16:59 +0000187static
188int have_ipv6(void) {
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000189 int s;
190
191 s = socket (AF_INET6, SOCK_STREAM, 0);
192 if (s != -1) {
193 close (s);
194 return (1);
195 }
196 return (0);
197}
198#endif
199
Owen Taylor3473f882001-02-23 17:55:21 +0000200/**
201 * xmlNanoHTTPInit:
202 *
203 * Initialize the HTTP protocol layer.
204 * Currently it just checks for proxy informations
205 */
206
207void
208xmlNanoHTTPInit(void) {
209 const char *env;
210#ifdef _WINSOCKAPI_
211 WSADATA wsaData;
212#endif
213
214 if (initialized)
215 return;
216
217#ifdef _WINSOCKAPI_
218 if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0)
219 return;
220#endif
221
222 if (proxy == NULL) {
223 proxyPort = 80;
224 env = getenv("no_proxy");
Daniel Veillard29b17482004-08-16 00:39:03 +0000225 if (env && ((env[0] == '*') && (env[1] == 0)))
Owen Taylor3473f882001-02-23 17:55:21 +0000226 goto done;
227 env = getenv("http_proxy");
228 if (env != NULL) {
229 xmlNanoHTTPScanProxy(env);
230 goto done;
231 }
232 env = getenv("HTTP_PROXY");
233 if (env != NULL) {
234 xmlNanoHTTPScanProxy(env);
235 goto done;
236 }
237 }
238done:
239 initialized = 1;
240}
241
242/**
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000243 * xmlNanoHTTPCleanup:
Owen Taylor3473f882001-02-23 17:55:21 +0000244 *
245 * Cleanup the HTTP protocol layer.
246 */
247
248void
249xmlNanoHTTPCleanup(void) {
250 if (proxy != NULL)
251 xmlFree(proxy);
252#ifdef _WINSOCKAPI_
253 if (initialized)
254 WSACleanup();
255#endif
256 initialized = 0;
257 return;
258}
259
260/**
Owen Taylor3473f882001-02-23 17:55:21 +0000261 * xmlNanoHTTPScanURL:
262 * @ctxt: an HTTP context
263 * @URL: The URL used to initialize the context
264 *
265 * (Re)Initialize an HTTP context by parsing the URL and finding
266 * the protocol host port and path it indicates.
267 */
268
269static void
270xmlNanoHTTPScanURL(xmlNanoHTTPCtxtPtr ctxt, const char *URL) {
271 const char *cur = URL;
272 char buf[4096];
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000273 int indx = 0;
Igor Zlatkovic537769a2004-02-09 17:40:31 +0000274 const int indxMax = 4096 - 1;
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;
Igor Zlatkovic537769a2004-02-09 17:40:31 +0000291 while ((*cur != 0) && (indx < indxMax)) {
Owen Taylor3473f882001-02-23 17:55:21 +0000292 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;
Igor Zlatkovic537769a2004-02-09 17:40:31 +0000304 while (indx < indxMax) {
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++;
Igor Zlatkovic537769a2004-02-09 17:40:31 +0000314 while ((cur[0] != ']') && (indx < indxMax))
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000315 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;
Igor Zlatkovic537769a2004-02-09 17:40:31 +0000371 while ((*cur != 0) && (indx < indxMax))
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;
Igor Zlatkovic537769a2004-02-09 17:40:31 +0000393 const int indxMax = 4096 - 1;
Owen Taylor3473f882001-02-23 17:55:21 +0000394 int port = 0;
395
396 if (proxy != NULL) {
397 xmlFree(proxy);
398 proxy = NULL;
399 }
400 if (proxyPort != 0) {
401 proxyPort = 0;
402 }
403#ifdef DEBUG_HTTP
404 if (URL == NULL)
405 xmlGenericError(xmlGenericErrorContext,
406 "Removing HTTP proxy info\n");
407 else
408 xmlGenericError(xmlGenericErrorContext,
409 "Using HTTP proxy %s\n", URL);
410#endif
411 if (URL == NULL) return;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000412 buf[indx] = 0;
Igor Zlatkovic537769a2004-02-09 17:40:31 +0000413 while ((*cur != 0) && (indx < indxMax)) {
Owen Taylor3473f882001-02-23 17:55:21 +0000414 if ((cur[0] == ':') && (cur[1] == '/') && (cur[2] == '/')) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000415 buf[indx] = 0;
416 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000417 cur += 3;
418 break;
419 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000420 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000421 }
422 if (*cur == 0) return;
423
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000424 buf[indx] = 0;
Igor Zlatkovic537769a2004-02-09 17:40:31 +0000425 while (indx < indxMax) {
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000426 if ((strchr (cur, '[') && !strchr (cur, ']')) ||
427 (!strchr (cur, '[') && strchr (cur, ']'))) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000428 __xmlIOErr(XML_FROM_HTTP, XML_HTTP_URL_SYNTAX, "Syntax Error\n");
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000429 return;
430 }
431
432 if (cur[0] == '[') {
433 cur++;
Igor Zlatkovic537769a2004-02-09 17:40:31 +0000434 while ((cur[0] != ']') && (indx < indxMax))
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000435 buf[indx++] = *cur++;
436
437 if (!strchr (buf, ':')) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000438 __xmlIOErr(XML_FROM_HTTP, XML_HTTP_USE_IP,
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000439 "Use [IPv6]/IPv4 format\n");
440 return;
441 }
442
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000443 buf[indx] = 0;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000444 proxy = xmlMemStrdup (buf);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000445 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000446 cur += 1;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000447 if (cur[0] == ':') {
Owen Taylor3473f882001-02-23 17:55:21 +0000448 cur++;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000449 while (*cur >= '0' && *cur <= '9') {
450 port *= 10;
451 port += *cur - '0';
452 cur++;
453 }
454
455 if (port != 0) proxyPort = port;
456 while ((cur[0] != '/') && (*cur != 0))
457 cur ++;
458 }
Owen Taylor3473f882001-02-23 17:55:21 +0000459 break;
460 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000461 else {
462 if (cur[0] == ':') {
463 buf[indx] = 0;
464 proxy = xmlMemStrdup (buf);
465 indx = 0;
466 cur += 1;
467 while ((*cur >= '0') && (*cur <= '9')) {
468 port *= 10;
469 port += *cur - '0';
470 cur++;
471 }
472 if (port != 0) proxyPort = port;
473 while ((cur[0] != '/') && (*cur != 0))
474 cur++;
475 break;
476 }
477 if ((*cur == '/') || (*cur == 0)) {
478 buf[indx] = 0;
479 proxy = xmlMemStrdup (buf);
480 indx = 0;
481 break;
482 }
Owen Taylor3473f882001-02-23 17:55:21 +0000483 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000484 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000485 }
486}
487
488/**
489 * xmlNanoHTTPNewCtxt:
490 * @URL: The URL used to initialize the context
491 *
492 * Allocate and initialize a new HTTP context.
493 *
494 * Returns an HTTP context or NULL in case of error.
495 */
496
497static xmlNanoHTTPCtxtPtr
498xmlNanoHTTPNewCtxt(const char *URL) {
499 xmlNanoHTTPCtxtPtr ret;
500
501 ret = (xmlNanoHTTPCtxtPtr) xmlMalloc(sizeof(xmlNanoHTTPCtxt));
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000502 if (ret == NULL) {
503 xmlHTTPErrMemory("allocating context");
504 return(NULL);
505 }
Owen Taylor3473f882001-02-23 17:55:21 +0000506
507 memset(ret, 0, sizeof(xmlNanoHTTPCtxt));
508 ret->port = 80;
509 ret->returnValue = 0;
510 ret->fd = -1;
Daniel Veillardf012a642001-07-23 19:10:52 +0000511 ret->ContentLength = -1;
Owen Taylor3473f882001-02-23 17:55:21 +0000512
Daniel Veillardcacbe5d2003-01-10 16:09:51 +0000513 xmlNanoHTTPScanURL(ret, URL);
Owen Taylor3473f882001-02-23 17:55:21 +0000514
515 return(ret);
516}
517
518/**
519 * xmlNanoHTTPFreeCtxt:
520 * @ctxt: an HTTP context
521 *
522 * Frees the context after closing the connection.
523 */
524
525static void
526xmlNanoHTTPFreeCtxt(xmlNanoHTTPCtxtPtr ctxt) {
527 if (ctxt == NULL) return;
528 if (ctxt->hostname != NULL) xmlFree(ctxt->hostname);
529 if (ctxt->protocol != NULL) xmlFree(ctxt->protocol);
530 if (ctxt->path != NULL) xmlFree(ctxt->path);
531 if (ctxt->out != NULL) xmlFree(ctxt->out);
532 if (ctxt->in != NULL) xmlFree(ctxt->in);
533 if (ctxt->contentType != NULL) xmlFree(ctxt->contentType);
Daniel Veillard847332a2003-10-18 11:29:40 +0000534 if (ctxt->encoding != NULL) xmlFree(ctxt->encoding);
Daniel Veillarda840b692003-10-19 13:35:37 +0000535 if (ctxt->mimeType != NULL) xmlFree(ctxt->mimeType);
Owen Taylor3473f882001-02-23 17:55:21 +0000536 if (ctxt->location != NULL) xmlFree(ctxt->location);
537 if (ctxt->authHeader != NULL) xmlFree(ctxt->authHeader);
538 ctxt->state = XML_NANO_HTTP_NONE;
539 if (ctxt->fd >= 0) closesocket(ctxt->fd);
540 ctxt->fd = -1;
541 xmlFree(ctxt);
542}
543
544/**
545 * xmlNanoHTTPSend:
546 * @ctxt: an HTTP context
547 *
548 * Send the input needed to initiate the processing on the server side
Daniel Veillardf012a642001-07-23 19:10:52 +0000549 * Returns number of bytes sent or -1 on error.
Owen Taylor3473f882001-02-23 17:55:21 +0000550 */
551
Daniel Veillardf012a642001-07-23 19:10:52 +0000552static int
553xmlNanoHTTPSend(xmlNanoHTTPCtxtPtr ctxt, const char * xmt_ptr, int outlen) {
554
555 int total_sent = 0;
556
557 if ( (ctxt->state & XML_NANO_HTTP_WRITE) && (xmt_ptr != NULL ) ) {
558 while (total_sent < outlen) {
559 int nsent = send(ctxt->fd, xmt_ptr + total_sent,
560 outlen - total_sent, 0);
Owen Taylor3473f882001-02-23 17:55:21 +0000561 if (nsent>0)
562 total_sent += nsent;
Daniel Veillardf012a642001-07-23 19:10:52 +0000563 else if ( ( nsent == -1 ) &&
Daniel Veillardba6db032001-07-31 16:25:45 +0000564#if defined(EAGAIN) && EAGAIN != EWOULDBLOCK
Daniel Veillardf012a642001-07-23 19:10:52 +0000565 ( socket_errno( ) != EAGAIN ) &&
Daniel Veillardba6db032001-07-31 16:25:45 +0000566#endif
Daniel Veillardf012a642001-07-23 19:10:52 +0000567 ( socket_errno( ) != EWOULDBLOCK ) ) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000568 __xmlIOErr(XML_FROM_HTTP, 0, "send failed\n");
Daniel Veillardf012a642001-07-23 19:10:52 +0000569 if ( total_sent == 0 )
570 total_sent = -1;
571 break;
572 }
573 else {
574 /*
575 ** No data sent
576 ** Since non-blocking sockets are used, wait for
577 ** socket to be writable or default timeout prior
578 ** to retrying.
579 */
580
581 struct timeval tv;
582 fd_set wfd;
583
584 tv.tv_sec = timeout;
585 tv.tv_usec = 0;
586 FD_ZERO( &wfd );
587 FD_SET( ctxt->fd, &wfd );
588 (void)select( ctxt->fd + 1, NULL, &wfd, NULL, &tv );
589 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000590 }
Owen Taylor3473f882001-02-23 17:55:21 +0000591 }
Daniel Veillardf012a642001-07-23 19:10:52 +0000592
593 return total_sent;
Owen Taylor3473f882001-02-23 17:55:21 +0000594}
595
596/**
597 * xmlNanoHTTPRecv:
598 * @ctxt: an HTTP context
599 *
600 * Read information coming from the HTTP connection.
601 * This is a blocking call (but it blocks in select(), not read()).
602 *
603 * Returns the number of byte read or -1 in case of error.
604 */
605
606static int
607xmlNanoHTTPRecv(xmlNanoHTTPCtxtPtr ctxt) {
608 fd_set rfd;
609 struct timeval tv;
610
611
612 while (ctxt->state & XML_NANO_HTTP_READ) {
613 if (ctxt->in == NULL) {
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000614 ctxt->in = (char *) xmlMallocAtomic(65000 * sizeof(char));
Owen Taylor3473f882001-02-23 17:55:21 +0000615 if (ctxt->in == NULL) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000616 xmlHTTPErrMemory("allocating input");
Owen Taylor3473f882001-02-23 17:55:21 +0000617 ctxt->last = -1;
618 return(-1);
619 }
620 ctxt->inlen = 65000;
621 ctxt->inptr = ctxt->content = ctxt->inrptr = ctxt->in;
622 }
623 if (ctxt->inrptr > ctxt->in + XML_NANO_HTTP_CHUNK) {
624 int delta = ctxt->inrptr - ctxt->in;
625 int len = ctxt->inptr - ctxt->inrptr;
626
627 memmove(ctxt->in, ctxt->inrptr, len);
628 ctxt->inrptr -= delta;
629 ctxt->content -= delta;
630 ctxt->inptr -= delta;
631 }
632 if ((ctxt->in + ctxt->inlen) < (ctxt->inptr + XML_NANO_HTTP_CHUNK)) {
633 int d_inptr = ctxt->inptr - ctxt->in;
634 int d_content = ctxt->content - ctxt->in;
635 int d_inrptr = ctxt->inrptr - ctxt->in;
Daniel Veillardf012a642001-07-23 19:10:52 +0000636 char * tmp_ptr = ctxt->in;
Owen Taylor3473f882001-02-23 17:55:21 +0000637
638 ctxt->inlen *= 2;
Daniel Veillardf012a642001-07-23 19:10:52 +0000639 ctxt->in = (char *) xmlRealloc(tmp_ptr, ctxt->inlen);
Owen Taylor3473f882001-02-23 17:55:21 +0000640 if (ctxt->in == NULL) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000641 xmlHTTPErrMemory("allocating input buffer");
Daniel Veillardf012a642001-07-23 19:10:52 +0000642 xmlFree( tmp_ptr );
Owen Taylor3473f882001-02-23 17:55:21 +0000643 ctxt->last = -1;
644 return(-1);
645 }
646 ctxt->inptr = ctxt->in + d_inptr;
647 ctxt->content = ctxt->in + d_content;
648 ctxt->inrptr = ctxt->in + d_inrptr;
649 }
650 ctxt->last = recv(ctxt->fd, ctxt->inptr, XML_NANO_HTTP_CHUNK, 0);
651 if (ctxt->last > 0) {
652 ctxt->inptr += ctxt->last;
653 return(ctxt->last);
654 }
655 if (ctxt->last == 0) {
656 return(0);
657 }
658 if (ctxt->last == -1) {
659 switch (socket_errno()) {
660 case EINPROGRESS:
661 case EWOULDBLOCK:
662#if defined(EAGAIN) && EAGAIN != EWOULDBLOCK
663 case EAGAIN:
664#endif
665 break;
Daniel Veillardf012a642001-07-23 19:10:52 +0000666
667 case ECONNRESET:
668 case ESHUTDOWN:
669 return ( 0 );
670
Owen Taylor3473f882001-02-23 17:55:21 +0000671 default:
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000672 __xmlIOErr(XML_FROM_HTTP, 0, "recv failed\n");
Daniel Veillardf012a642001-07-23 19:10:52 +0000673 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +0000674 }
675 }
676
677 tv.tv_sec = timeout;
678 tv.tv_usec = 0;
679 FD_ZERO(&rfd);
680 FD_SET(ctxt->fd, &rfd);
681
Daniel Veillard50f34372001-08-03 12:06:36 +0000682 if ( (select(ctxt->fd+1, &rfd, NULL, NULL, &tv)<1)
683#if defined(EINTR)
684 && (errno != EINTR)
685#endif
686 )
Owen Taylor3473f882001-02-23 17:55:21 +0000687 return(0);
688 }
689 return(0);
690}
691
692/**
693 * xmlNanoHTTPReadLine:
694 * @ctxt: an HTTP context
695 *
696 * Read one line in the HTTP server output, usually for extracting
697 * the HTTP protocol informations from the answer header.
698 *
699 * Returns a newly allocated string with a copy of the line, or NULL
700 * which indicate the end of the input.
701 */
702
703static char *
704xmlNanoHTTPReadLine(xmlNanoHTTPCtxtPtr ctxt) {
705 char buf[4096];
706 char *bp = buf;
Daniel Veillardf012a642001-07-23 19:10:52 +0000707 int rc;
Owen Taylor3473f882001-02-23 17:55:21 +0000708
709 while (bp - buf < 4095) {
710 if (ctxt->inrptr == ctxt->inptr) {
Daniel Veillardf012a642001-07-23 19:10:52 +0000711 if ( (rc = xmlNanoHTTPRecv(ctxt)) == 0) {
Owen Taylor3473f882001-02-23 17:55:21 +0000712 if (bp == buf)
713 return(NULL);
714 else
715 *bp = 0;
716 return(xmlMemStrdup(buf));
717 }
Daniel Veillardf012a642001-07-23 19:10:52 +0000718 else if ( rc == -1 ) {
719 return ( NULL );
720 }
Owen Taylor3473f882001-02-23 17:55:21 +0000721 }
722 *bp = *ctxt->inrptr++;
723 if (*bp == '\n') {
724 *bp = 0;
725 return(xmlMemStrdup(buf));
726 }
727 if (*bp != '\r')
728 bp++;
729 }
730 buf[4095] = 0;
731 return(xmlMemStrdup(buf));
732}
733
734
735/**
736 * xmlNanoHTTPScanAnswer:
737 * @ctxt: an HTTP context
738 * @line: an HTTP header line
739 *
740 * Try to extract useful informations from the server answer.
741 * We currently parse and process:
742 * - The HTTP revision/ return code
Daniel Veillarda840b692003-10-19 13:35:37 +0000743 * - The Content-Type, Mime-Type and charset used
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000744 * - The Location for redirect processing.
Owen Taylor3473f882001-02-23 17:55:21 +0000745 *
746 * Returns -1 in case of failure, the file descriptor number otherwise
747 */
748
749static void
750xmlNanoHTTPScanAnswer(xmlNanoHTTPCtxtPtr ctxt, const char *line) {
751 const char *cur = line;
752
753 if (line == NULL) return;
754
755 if (!strncmp(line, "HTTP/", 5)) {
756 int version = 0;
757 int ret = 0;
758
759 cur += 5;
760 while ((*cur >= '0') && (*cur <= '9')) {
761 version *= 10;
762 version += *cur - '0';
763 cur++;
764 }
765 if (*cur == '.') {
766 cur++;
767 if ((*cur >= '0') && (*cur <= '9')) {
768 version *= 10;
769 version += *cur - '0';
770 cur++;
771 }
772 while ((*cur >= '0') && (*cur <= '9'))
773 cur++;
774 } else
775 version *= 10;
776 if ((*cur != ' ') && (*cur != '\t')) return;
777 while ((*cur == ' ') || (*cur == '\t')) cur++;
778 if ((*cur < '0') || (*cur > '9')) return;
779 while ((*cur >= '0') && (*cur <= '9')) {
780 ret *= 10;
781 ret += *cur - '0';
782 cur++;
783 }
784 if ((*cur != 0) && (*cur != ' ') && (*cur != '\t')) return;
785 ctxt->returnValue = ret;
786 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Content-Type:", 13)) {
Daniel Veillarda840b692003-10-19 13:35:37 +0000787 const xmlChar *charset, *last, *mime;
Owen Taylor3473f882001-02-23 17:55:21 +0000788 cur += 13;
789 while ((*cur == ' ') || (*cur == '\t')) cur++;
790 if (ctxt->contentType != NULL)
791 xmlFree(ctxt->contentType);
792 ctxt->contentType = xmlMemStrdup(cur);
Daniel Veillarda840b692003-10-19 13:35:37 +0000793 mime = (const xmlChar *) cur;
794 last = mime;
795 while ((*last != 0) && (*last != ' ') && (*last != '\t') &&
796 (*last != ';') && (*last != ','))
797 last++;
798 if (ctxt->mimeType != NULL)
799 xmlFree(ctxt->mimeType);
800 ctxt->mimeType = (char *) xmlStrndup(mime, last - mime);
801 charset = xmlStrstr(BAD_CAST ctxt->contentType, BAD_CAST "charset=");
802 if (charset != NULL) {
803 charset += 8;
804 last = charset;
805 while ((*last != 0) && (*last != ' ') && (*last != '\t') &&
806 (*last != ';') && (*last != ','))
807 last++;
808 if (ctxt->encoding != NULL)
809 xmlFree(ctxt->encoding);
810 ctxt->encoding = (char *) xmlStrndup(charset, last - charset);
811 }
Owen Taylor3473f882001-02-23 17:55:21 +0000812 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"ContentType:", 12)) {
Daniel Veillarda840b692003-10-19 13:35:37 +0000813 const xmlChar *charset, *last, *mime;
Owen Taylor3473f882001-02-23 17:55:21 +0000814 cur += 12;
815 if (ctxt->contentType != NULL) return;
816 while ((*cur == ' ') || (*cur == '\t')) cur++;
817 ctxt->contentType = xmlMemStrdup(cur);
Daniel Veillarda840b692003-10-19 13:35:37 +0000818 mime = (const xmlChar *) cur;
819 last = mime;
820 while ((*last != 0) && (*last != ' ') && (*last != '\t') &&
821 (*last != ';') && (*last != ','))
822 last++;
823 if (ctxt->mimeType != NULL)
824 xmlFree(ctxt->mimeType);
825 ctxt->mimeType = (char *) xmlStrndup(mime, last - mime);
826 charset = xmlStrstr(BAD_CAST ctxt->contentType, BAD_CAST "charset=");
827 if (charset != NULL) {
828 charset += 8;
829 last = charset;
830 while ((*last != 0) && (*last != ' ') && (*last != '\t') &&
831 (*last != ';') && (*last != ','))
832 last++;
833 if (ctxt->encoding != NULL)
834 xmlFree(ctxt->encoding);
835 ctxt->encoding = (char *) xmlStrndup(charset, last - charset);
836 }
Owen Taylor3473f882001-02-23 17:55:21 +0000837 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Location:", 9)) {
838 cur += 9;
839 while ((*cur == ' ') || (*cur == '\t')) cur++;
840 if (ctxt->location != NULL)
841 xmlFree(ctxt->location);
William M. Brack7e29c0a2004-04-02 09:07:22 +0000842 if (*cur == '/') {
843 xmlChar *tmp_http = xmlStrdup(BAD_CAST "http://");
844 xmlChar *tmp_loc =
845 xmlStrcat(tmp_http, (const xmlChar *) ctxt->hostname);
846 ctxt->location =
847 (char *) xmlStrcat (tmp_loc, (const xmlChar *) cur);
848 } else {
849 ctxt->location = xmlMemStrdup(cur);
850 }
Owen Taylor3473f882001-02-23 17:55:21 +0000851 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"WWW-Authenticate:", 17)) {
852 cur += 17;
853 while ((*cur == ' ') || (*cur == '\t')) cur++;
854 if (ctxt->authHeader != NULL)
855 xmlFree(ctxt->authHeader);
856 ctxt->authHeader = xmlMemStrdup(cur);
857 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Proxy-Authenticate:", 19)) {
858 cur += 19;
859 while ((*cur == ' ') || (*cur == '\t')) cur++;
860 if (ctxt->authHeader != NULL)
861 xmlFree(ctxt->authHeader);
862 ctxt->authHeader = xmlMemStrdup(cur);
Daniel Veillardf012a642001-07-23 19:10:52 +0000863 } else if ( !xmlStrncasecmp( BAD_CAST line, BAD_CAST"Content-Length:", 15) ) {
864 cur += 15;
865 ctxt->ContentLength = strtol( cur, NULL, 10 );
Owen Taylor3473f882001-02-23 17:55:21 +0000866 }
867}
868
869/**
870 * xmlNanoHTTPConnectAttempt:
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000871 * @addr: a socket address structure
Owen Taylor3473f882001-02-23 17:55:21 +0000872 *
873 * Attempt a connection to the given IP:port endpoint. It forces
874 * non-blocking semantic on the socket, and allow 60 seconds for
875 * the host to answer.
876 *
877 * Returns -1 in case of failure, the file descriptor number otherwise
878 */
879
880static int
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000881xmlNanoHTTPConnectAttempt(struct sockaddr *addr)
Owen Taylor3473f882001-02-23 17:55:21 +0000882{
Owen Taylor3473f882001-02-23 17:55:21 +0000883 fd_set wfd;
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000884#ifdef _WINSOCKAPI_
885 fd_set xfd;
886#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000887 struct timeval tv;
888 int status;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000889 int addrlen;
890 SOCKET s;
Owen Taylor3473f882001-02-23 17:55:21 +0000891
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000892#ifdef SUPPORT_IP6
893 if (addr->sa_family == AF_INET6) {
894 s = socket (PF_INET6, SOCK_STREAM, IPPROTO_TCP);
895 addrlen = sizeof (struct sockaddr_in6);
896 }
897 else
898#endif
899 {
900 s = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
901 addrlen = sizeof (struct sockaddr_in);
902 }
Owen Taylor3473f882001-02-23 17:55:21 +0000903 if (s==-1) {
904#ifdef DEBUG_HTTP
905 perror("socket");
906#endif
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000907 __xmlIOErr(XML_FROM_HTTP, 0, "socket failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000908 return(-1);
909 }
910
911#ifdef _WINSOCKAPI_
912 {
913 u_long one = 1;
914
915 status = ioctlsocket(s, FIONBIO, &one) == SOCKET_ERROR ? -1 : 0;
916 }
917#else /* _WINSOCKAPI_ */
918#if defined(VMS)
919 {
920 int enable = 1;
921 status = ioctl(s, FIONBIO, &enable);
922 }
923#else /* VMS */
Daniel Veillard254b1262003-11-01 17:04:58 +0000924#if defined(__BEOS__)
925 {
926 bool noblock = true;
927 status = setsockopt(s, SOL_SOCKET, SO_NONBLOCK, &noblock, sizeof(noblock));
928 }
929#else /* __BEOS__ */
Owen Taylor3473f882001-02-23 17:55:21 +0000930 if ((status = fcntl(s, F_GETFL, 0)) != -1) {
931#ifdef O_NONBLOCK
932 status |= O_NONBLOCK;
933#else /* O_NONBLOCK */
934#ifdef F_NDELAY
935 status |= F_NDELAY;
936#endif /* F_NDELAY */
937#endif /* !O_NONBLOCK */
938 status = fcntl(s, F_SETFL, status);
939 }
940 if (status < 0) {
941#ifdef DEBUG_HTTP
942 perror("nonblocking");
943#endif
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000944 __xmlIOErr(XML_FROM_HTTP, 0, "error setting non-blocking IO\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000945 closesocket(s);
946 return(-1);
947 }
Daniel Veillard254b1262003-11-01 17:04:58 +0000948#endif /* !__BEOS__ */
Owen Taylor3473f882001-02-23 17:55:21 +0000949#endif /* !VMS */
950#endif /* !_WINSOCKAPI_ */
951
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000952 if (connect (s, addr, addrlen) == -1) {
Owen Taylor3473f882001-02-23 17:55:21 +0000953 switch (socket_errno()) {
954 case EINPROGRESS:
955 case EWOULDBLOCK:
956 break;
957 default:
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000958 __xmlIOErr(XML_FROM_HTTP, 0, "error connecting to HTTP server");
Owen Taylor3473f882001-02-23 17:55:21 +0000959 closesocket(s);
960 return(-1);
961 }
962 }
963
964 tv.tv_sec = timeout;
965 tv.tv_usec = 0;
966
967 FD_ZERO(&wfd);
968 FD_SET(s, &wfd);
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000969
970#ifdef _WINSOCKAPI_
971 FD_ZERO(&xfd);
972 FD_SET(s, &xfd);
Owen Taylor3473f882001-02-23 17:55:21 +0000973
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000974 switch(select(s+1, NULL, &wfd, &xfd, &tv))
975#else
Owen Taylor3473f882001-02-23 17:55:21 +0000976 switch(select(s+1, NULL, &wfd, NULL, &tv))
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000977#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000978 {
979 case 0:
980 /* Time out */
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000981 __xmlIOErr(XML_FROM_HTTP, 0, "Connect attempt timed out");
Owen Taylor3473f882001-02-23 17:55:21 +0000982 closesocket(s);
983 return(-1);
984 case -1:
985 /* Ermm.. ?? */
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000986 __xmlIOErr(XML_FROM_HTTP, 0, "Connect failed");
Owen Taylor3473f882001-02-23 17:55:21 +0000987 closesocket(s);
988 return(-1);
989 }
990
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000991 if ( FD_ISSET(s, &wfd)
992#ifdef _WINSOCKAPI_
993 || FD_ISSET(s, &xfd)
994#endif
995 ) {
Owen Taylor3473f882001-02-23 17:55:21 +0000996 SOCKLEN_T len;
997 len = sizeof(status);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000998#ifdef SO_ERROR
Owen Taylor3473f882001-02-23 17:55:21 +0000999 if (getsockopt(s, SOL_SOCKET, SO_ERROR, (char*)&status, &len) < 0 ) {
1000 /* Solaris error code */
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001001 __xmlIOErr(XML_FROM_HTTP, 0, "getsockopt failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001002 return (-1);
1003 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001004#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001005 if ( status ) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001006 __xmlIOErr(XML_FROM_HTTP, 0, "Error connecting to remote host");
Owen Taylor3473f882001-02-23 17:55:21 +00001007 closesocket(s);
1008 errno = status;
1009 return (-1);
1010 }
1011 } else {
1012 /* pbm */
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001013 __xmlIOErr(XML_FROM_HTTP, 0, "select failed\n");
Daniel Veillardf012a642001-07-23 19:10:52 +00001014 closesocket(s);
Owen Taylor3473f882001-02-23 17:55:21 +00001015 return (-1);
1016 }
1017
1018 return(s);
1019}
1020
1021/**
1022 * xmlNanoHTTPConnectHost:
1023 * @host: the host name
1024 * @port: the port number
1025 *
1026 * Attempt a connection to the given host:port endpoint. It tries
1027 * the multiple IP provided by the DNS if available.
1028 *
1029 * Returns -1 in case of failure, the file descriptor number otherwise
1030 */
1031
1032static int
1033xmlNanoHTTPConnectHost(const char *host, int port)
1034{
1035 struct hostent *h;
Daniel Veillard2db8c122003-07-08 12:16:59 +00001036 struct sockaddr *addr = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001037 struct in_addr ia;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001038 struct sockaddr_in sockin;
Daniel Veillard5c396542002-03-15 07:57:50 +00001039
Owen Taylor3473f882001-02-23 17:55:21 +00001040#ifdef SUPPORT_IP6
1041 struct in6_addr ia6;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001042 struct sockaddr_in6 sockin6;
Owen Taylor3473f882001-02-23 17:55:21 +00001043#endif
1044 int i;
1045 int s;
Daniel Veillard5c396542002-03-15 07:57:50 +00001046
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001047 memset (&sockin, 0, sizeof(sockin));
1048#ifdef SUPPORT_IP6
1049 memset (&sockin6, 0, sizeof(sockin6));
1050 if (have_ipv6 ())
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001051#if !defined(HAVE_GETADDRINFO) && defined(RES_USE_INET6)
Daniel Veillard560c2a42003-07-06 21:13:49 +00001052 {
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001053 if (!(_res.options & RES_INIT))
1054 res_init();
1055 _res.options |= RES_USE_INET6;
1056 }
1057#elif defined(HAVE_GETADDRINFO)
Daniel Veillard560c2a42003-07-06 21:13:49 +00001058 {
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001059 int status;
1060 struct addrinfo hints, *res, *result;
1061
1062 result = NULL;
1063 memset (&hints, 0,sizeof(hints));
1064 hints.ai_socktype = SOCK_STREAM;
1065
1066 status = getaddrinfo (host, NULL, &hints, &result);
1067 if (status) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001068 __xmlIOErr(XML_FROM_HTTP, 0, "getaddrinfo failed\n");
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001069 return (-1);
1070 }
1071
1072 for (res = result; res; res = res->ai_next) {
Daniel Veillard3dc93a42003-07-10 14:04:33 +00001073 if (res->ai_family == AF_INET || res->ai_family == AF_INET6) {
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001074 if (res->ai_family == AF_INET6) {
Daniel Veillard8e2c9792004-10-27 09:39:50 +00001075 if (res->ai_addrlen > sizeof(sockin6)) {
1076 __xmlIOErr(XML_FROM_HTTP, 0, "address size mismatch\n");
1077 freeaddrinfo (result);
1078 return (-1);
1079 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001080 memcpy (&sockin6, res->ai_addr, res->ai_addrlen);
1081 sockin6.sin6_port = htons (port);
1082 addr = (struct sockaddr *)&sockin6;
1083 }
Daniel Veillard3dc93a42003-07-10 14:04:33 +00001084 else {
Daniel Veillard8e2c9792004-10-27 09:39:50 +00001085 if (res->ai_addrlen > sizeof(sockin)) {
1086 __xmlIOErr(XML_FROM_HTTP, 0, "address size mismatch\n");
1087 freeaddrinfo (result);
1088 return (-1);
1089 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001090 memcpy (&sockin, res->ai_addr, res->ai_addrlen);
1091 sockin.sin_port = htons (port);
1092 addr = (struct sockaddr *)&sockin;
1093 }
1094
1095 s = xmlNanoHTTPConnectAttempt (addr);
1096 if (s != -1) {
1097 freeaddrinfo (result);
1098 return (s);
1099 }
1100 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001101 }
Daniel Veillard3dc93a42003-07-10 14:04:33 +00001102 if (result)
1103 freeaddrinfo (result);
1104 return (-1);
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001105 } else
Owen Taylor3473f882001-02-23 17:55:21 +00001106#endif
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001107#endif
1108 {
1109 h = gethostbyname (host);
1110 if (h == NULL) {
Daniel Veillard56b2db72002-03-25 16:35:28 +00001111
1112/*
1113 * Okay, I got fed up by the non-portability of this error message
1114 * extraction code. it work on Linux, if it work on your platform
1115 * and one want to enable it, send me the defined(foobar) needed
1116 */
1117#if defined(HAVE_NETDB_H) && defined(HOST_NOT_FOUND) && defined(linux)
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001118 const char *h_err_txt = "";
Daniel Veillardf012a642001-07-23 19:10:52 +00001119
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001120 switch (h_errno) {
1121 case HOST_NOT_FOUND:
1122 h_err_txt = "Authoritive host not found";
1123 break;
Daniel Veillardf012a642001-07-23 19:10:52 +00001124
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001125 case TRY_AGAIN:
1126 h_err_txt =
1127 "Non-authoritive host not found or server failure.";
1128 break;
Daniel Veillardf012a642001-07-23 19:10:52 +00001129
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001130 case NO_RECOVERY:
1131 h_err_txt =
1132 "Non-recoverable errors: FORMERR, REFUSED, or NOTIMP.";
1133 break;
Daniel Veillard5c396542002-03-15 07:57:50 +00001134
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001135 case NO_ADDRESS:
1136 h_err_txt =
1137 "Valid name, no data record of requested type.";
1138 break;
Daniel Veillard5c396542002-03-15 07:57:50 +00001139
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001140 default:
1141 h_err_txt = "No error text defined.";
1142 break;
1143 }
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001144 __xmlIOErr(XML_FROM_HTTP, 0, h_err_txt);
Daniel Veillard5c396542002-03-15 07:57:50 +00001145#else
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001146 __xmlIOErr(XML_FROM_HTTP, 0, "Failed to resolve host");
Owen Taylor3473f882001-02-23 17:55:21 +00001147#endif
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001148 return (-1);
1149 }
Daniel Veillard5c396542002-03-15 07:57:50 +00001150
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001151 for (i = 0; h->h_addr_list[i]; i++) {
1152 if (h->h_addrtype == AF_INET) {
1153 /* A records (IPv4) */
Daniel Veillard8e2c9792004-10-27 09:39:50 +00001154 if ((unsigned int) h->h_length > sizeof(ia)) {
1155 __xmlIOErr(XML_FROM_HTTP, 0, "address size mismatch\n");
1156 return (-1);
1157 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001158 memcpy (&ia, h->h_addr_list[i], h->h_length);
1159 sockin.sin_family = h->h_addrtype;
1160 sockin.sin_addr = ia;
1161 sockin.sin_port = htons (port);
1162 addr = (struct sockaddr *) &sockin;
Daniel Veillard5c396542002-03-15 07:57:50 +00001163#ifdef SUPPORT_IP6
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001164 } else if (have_ipv6 () && (h->h_addrtype == AF_INET6)) {
1165 /* AAAA records (IPv6) */
Daniel Veillard8e2c9792004-10-27 09:39:50 +00001166 if ((unsigned int) h->h_length > sizeof(ia6)) {
1167 __xmlIOErr(XML_FROM_HTTP, 0, "address size mismatch\n");
1168 return (-1);
1169 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001170 memcpy (&ia6, h->h_addr_list[i], h->h_length);
1171 sockin6.sin6_family = h->h_addrtype;
1172 sockin6.sin6_addr = ia6;
1173 sockin6.sin6_port = htons (port);
1174 addr = (struct sockaddr *) &sockin6;
Daniel Veillard5c396542002-03-15 07:57:50 +00001175#endif
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001176 } else
1177 break; /* for */
Daniel Veillard5c396542002-03-15 07:57:50 +00001178
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001179 s = xmlNanoHTTPConnectAttempt (addr);
1180 if (s != -1)
1181 return (s);
1182 }
Owen Taylor3473f882001-02-23 17:55:21 +00001183 }
Owen Taylor3473f882001-02-23 17:55:21 +00001184#ifdef DEBUG_HTTP
1185 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard5c396542002-03-15 07:57:50 +00001186 "xmlNanoHTTPConnectHost: unable to connect to '%s'.\n",
1187 host);
Owen Taylor3473f882001-02-23 17:55:21 +00001188#endif
Daniel Veillard5c396542002-03-15 07:57:50 +00001189 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001190}
1191
1192
1193/**
1194 * xmlNanoHTTPOpen:
1195 * @URL: The URL to load
1196 * @contentType: if available the Content-Type information will be
1197 * returned at that location
1198 *
1199 * This function try to open a connection to the indicated resource
1200 * via HTTP GET.
1201 *
1202 * Returns NULL in case of failure, otherwise a request handler.
1203 * The contentType, if provided must be freed by the caller
1204 */
1205
1206void*
1207xmlNanoHTTPOpen(const char *URL, char **contentType) {
1208 if (contentType != NULL) *contentType = NULL;
Daniel Veillardf012a642001-07-23 19:10:52 +00001209 return(xmlNanoHTTPMethod(URL, NULL, NULL, contentType, NULL, 0));
Daniel Veillard9403a042001-05-28 11:00:53 +00001210}
1211
1212/**
1213 * xmlNanoHTTPOpenRedir:
1214 * @URL: The URL to load
1215 * @contentType: if available the Content-Type information will be
1216 * returned at that location
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001217 * @redir: if available the redirected URL will be returned
Daniel Veillard9403a042001-05-28 11:00:53 +00001218 *
1219 * This function try to open a connection to the indicated resource
1220 * via HTTP GET.
1221 *
1222 * Returns NULL in case of failure, otherwise a request handler.
1223 * The contentType, if provided must be freed by the caller
1224 */
1225
1226void*
1227xmlNanoHTTPOpenRedir(const char *URL, char **contentType, char **redir) {
1228 if (contentType != NULL) *contentType = NULL;
1229 if (redir != NULL) *redir = NULL;
Daniel Veillardf012a642001-07-23 19:10:52 +00001230 return(xmlNanoHTTPMethodRedir(URL, NULL, NULL, contentType, redir, NULL,0));
Owen Taylor3473f882001-02-23 17:55:21 +00001231}
1232
1233/**
1234 * xmlNanoHTTPRead:
1235 * @ctx: the HTTP context
1236 * @dest: a buffer
1237 * @len: the buffer length
1238 *
1239 * This function tries to read @len bytes from the existing HTTP connection
1240 * and saves them in @dest. This is a blocking call.
1241 *
1242 * Returns the number of byte read. 0 is an indication of an end of connection.
1243 * -1 indicates a parameter error.
1244 */
1245int
1246xmlNanoHTTPRead(void *ctx, void *dest, int len) {
1247 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1248
1249 if (ctx == NULL) return(-1);
1250 if (dest == NULL) return(-1);
1251 if (len <= 0) return(0);
1252
1253 while (ctxt->inptr - ctxt->inrptr < len) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001254 if (xmlNanoHTTPRecv(ctxt) <= 0) break;
Owen Taylor3473f882001-02-23 17:55:21 +00001255 }
1256 if (ctxt->inptr - ctxt->inrptr < len)
1257 len = ctxt->inptr - ctxt->inrptr;
1258 memcpy(dest, ctxt->inrptr, len);
1259 ctxt->inrptr += len;
1260 return(len);
1261}
1262
1263/**
1264 * xmlNanoHTTPClose:
1265 * @ctx: the HTTP context
1266 *
1267 * This function closes an HTTP context, it ends up the connection and
1268 * free all data related to it.
1269 */
1270void
1271xmlNanoHTTPClose(void *ctx) {
1272 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1273
1274 if (ctx == NULL) return;
1275
1276 xmlNanoHTTPFreeCtxt(ctxt);
1277}
1278
1279/**
Daniel Veillard9403a042001-05-28 11:00:53 +00001280 * xmlNanoHTTPMethodRedir:
Owen Taylor3473f882001-02-23 17:55:21 +00001281 * @URL: The URL to load
1282 * @method: the HTTP method to use
1283 * @input: the input string if any
1284 * @contentType: the Content-Type information IN and OUT
Daniel Veillard9403a042001-05-28 11:00:53 +00001285 * @redir: the redirected URL OUT
Owen Taylor3473f882001-02-23 17:55:21 +00001286 * @headers: the extra headers
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001287 * @ilen: input length
Owen Taylor3473f882001-02-23 17:55:21 +00001288 *
1289 * This function try to open a connection to the indicated resource
1290 * via HTTP using the given @method, adding the given extra headers
1291 * and the input buffer for the request content.
1292 *
1293 * Returns NULL in case of failure, otherwise a request handler.
Daniel Veillard9403a042001-05-28 11:00:53 +00001294 * The contentType, or redir, if provided must be freed by the caller
Owen Taylor3473f882001-02-23 17:55:21 +00001295 */
1296
1297void*
Daniel Veillard9403a042001-05-28 11:00:53 +00001298xmlNanoHTTPMethodRedir(const char *URL, const char *method, const char *input,
Daniel Veillardf012a642001-07-23 19:10:52 +00001299 char **contentType, char **redir,
1300 const char *headers, int ilen ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001301 xmlNanoHTTPCtxtPtr ctxt;
1302 char *bp, *p;
Daniel Veillardf012a642001-07-23 19:10:52 +00001303 int blen, ret;
Owen Taylor3473f882001-02-23 17:55:21 +00001304 int head;
1305 int nbRedirects = 0;
1306 char *redirURL = NULL;
William M. Brack78637da2003-07-31 14:47:38 +00001307#ifdef DEBUG_HTTP
1308 int xmt_bytes;
1309#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001310
1311 if (URL == NULL) return(NULL);
1312 if (method == NULL) method = "GET";
1313 xmlNanoHTTPInit();
1314
1315retry:
1316 if (redirURL == NULL)
1317 ctxt = xmlNanoHTTPNewCtxt(URL);
1318 else {
1319 ctxt = xmlNanoHTTPNewCtxt(redirURL);
Daniel Veillarda840b692003-10-19 13:35:37 +00001320 ctxt->location = xmlMemStrdup(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001321 }
1322
Daniel Veillardf012a642001-07-23 19:10:52 +00001323 if ( ctxt == NULL ) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001324 return ( NULL );
1325 }
1326
Owen Taylor3473f882001-02-23 17:55:21 +00001327 if ((ctxt->protocol == NULL) || (strcmp(ctxt->protocol, "http"))) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001328 __xmlIOErr(XML_FROM_HTTP, XML_HTTP_URL_SYNTAX, "Not a valid HTTP URI");
Owen Taylor3473f882001-02-23 17:55:21 +00001329 xmlNanoHTTPFreeCtxt(ctxt);
1330 if (redirURL != NULL) xmlFree(redirURL);
1331 return(NULL);
1332 }
1333 if (ctxt->hostname == NULL) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001334 __xmlIOErr(XML_FROM_HTTP, XML_HTTP_UNKNOWN_HOST,
1335 "Failed to identify host in URI");
Owen Taylor3473f882001-02-23 17:55:21 +00001336 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001337 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001338 return(NULL);
1339 }
1340 if (proxy) {
1341 blen = strlen(ctxt->hostname) * 2 + 16;
1342 ret = xmlNanoHTTPConnectHost(proxy, proxyPort);
1343 }
1344 else {
1345 blen = strlen(ctxt->hostname);
1346 ret = xmlNanoHTTPConnectHost(ctxt->hostname, ctxt->port);
1347 }
1348 if (ret < 0) {
1349 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001350 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001351 return(NULL);
1352 }
1353 ctxt->fd = ret;
1354
Daniel Veillardf012a642001-07-23 19:10:52 +00001355 if (input == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00001356 ilen = 0;
Daniel Veillardf012a642001-07-23 19:10:52 +00001357 else
1358 blen += 36;
1359
Owen Taylor3473f882001-02-23 17:55:21 +00001360 if (headers != NULL)
Daniel Veillardf012a642001-07-23 19:10:52 +00001361 blen += strlen(headers) + 2;
Owen Taylor3473f882001-02-23 17:55:21 +00001362 if (contentType && *contentType)
1363 blen += strlen(*contentType) + 16;
Daniel Veillardf012a642001-07-23 19:10:52 +00001364 blen += strlen(method) + strlen(ctxt->path) + 24;
Daniel Veillard82cb3192003-10-29 13:39:15 +00001365 bp = (char*)xmlMallocAtomic(blen);
Daniel Veillardf012a642001-07-23 19:10:52 +00001366 if ( bp == NULL ) {
1367 xmlNanoHTTPFreeCtxt( ctxt );
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001368 xmlHTTPErrMemory("allocating header buffer");
Daniel Veillardf012a642001-07-23 19:10:52 +00001369 return ( NULL );
1370 }
1371
1372 p = bp;
1373
Owen Taylor3473f882001-02-23 17:55:21 +00001374 if (proxy) {
1375 if (ctxt->port != 80) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001376 p += snprintf( p, blen - (p - bp), "%s http://%s:%d%s",
1377 method, ctxt->hostname,
Daniel Veillardf012a642001-07-23 19:10:52 +00001378 ctxt->port, ctxt->path );
Owen Taylor3473f882001-02-23 17:55:21 +00001379 }
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001380 else
1381 p += snprintf( p, blen - (p - bp), "%s http://%s%s", method,
Daniel Veillardf012a642001-07-23 19:10:52 +00001382 ctxt->hostname, ctxt->path);
Owen Taylor3473f882001-02-23 17:55:21 +00001383 }
1384 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001385 p += snprintf( p, blen - (p - bp), "%s %s", method, ctxt->path);
Daniel Veillardf012a642001-07-23 19:10:52 +00001386
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001387 p += snprintf( p, blen - (p - bp), " HTTP/1.0\r\nHost: %s\r\n",
1388 ctxt->hostname);
Daniel Veillardf012a642001-07-23 19:10:52 +00001389
1390 if (contentType != NULL && *contentType)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001391 p += snprintf(p, blen - (p - bp), "Content-Type: %s\r\n", *contentType);
Daniel Veillardf012a642001-07-23 19:10:52 +00001392
1393 if (headers != NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001394 p += snprintf( p, blen - (p - bp), "%s", headers );
Daniel Veillardf012a642001-07-23 19:10:52 +00001395
Owen Taylor3473f882001-02-23 17:55:21 +00001396 if (input != NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001397 snprintf(p, blen - (p - bp), "Content-Length: %d\r\n\r\n", ilen );
Owen Taylor3473f882001-02-23 17:55:21 +00001398 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001399 snprintf(p, blen - (p - bp), "\r\n");
Daniel Veillardf012a642001-07-23 19:10:52 +00001400
Owen Taylor3473f882001-02-23 17:55:21 +00001401#ifdef DEBUG_HTTP
1402 xmlGenericError(xmlGenericErrorContext,
1403 "-> %s%s", proxy? "(Proxy) " : "", bp);
1404 if ((blen -= strlen(bp)+1) < 0)
1405 xmlGenericError(xmlGenericErrorContext,
1406 "ERROR: overflowed buffer by %d bytes\n", -blen);
1407#endif
1408 ctxt->outptr = ctxt->out = bp;
1409 ctxt->state = XML_NANO_HTTP_WRITE;
Daniel Veillardf012a642001-07-23 19:10:52 +00001410 blen = strlen( ctxt->out );
Daniel Veillardf012a642001-07-23 19:10:52 +00001411#ifdef DEBUG_HTTP
William M. Brack78637da2003-07-31 14:47:38 +00001412 xmt_bytes = xmlNanoHTTPSend(ctxt, ctxt->out, blen );
Daniel Veillardf012a642001-07-23 19:10:52 +00001413 if ( xmt_bytes != blen )
1414 xmlGenericError( xmlGenericErrorContext,
1415 "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
1416 xmt_bytes, blen,
1417 "bytes of HTTP headers sent to host",
1418 ctxt->hostname );
William M. Brack78637da2003-07-31 14:47:38 +00001419#else
1420 xmlNanoHTTPSend(ctxt, ctxt->out, blen );
Daniel Veillardf012a642001-07-23 19:10:52 +00001421#endif
1422
1423 if ( input != NULL ) {
William M. Brack78637da2003-07-31 14:47:38 +00001424#ifdef DEBUG_HTTP
Daniel Veillardf012a642001-07-23 19:10:52 +00001425 xmt_bytes = xmlNanoHTTPSend( ctxt, input, ilen );
1426
Daniel Veillardf012a642001-07-23 19:10:52 +00001427 if ( xmt_bytes != ilen )
1428 xmlGenericError( xmlGenericErrorContext,
1429 "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
1430 xmt_bytes, ilen,
1431 "bytes of HTTP content sent to host",
1432 ctxt->hostname );
William M. Brack78637da2003-07-31 14:47:38 +00001433#else
1434 xmlNanoHTTPSend( ctxt, input, ilen );
Daniel Veillardf012a642001-07-23 19:10:52 +00001435#endif
1436 }
1437
Owen Taylor3473f882001-02-23 17:55:21 +00001438 ctxt->state = XML_NANO_HTTP_READ;
1439 head = 1;
1440
1441 while ((p = xmlNanoHTTPReadLine(ctxt)) != NULL) {
1442 if (head && (*p == 0)) {
1443 head = 0;
1444 ctxt->content = ctxt->inrptr;
1445 xmlFree(p);
1446 break;
1447 }
1448 xmlNanoHTTPScanAnswer(ctxt, p);
1449
1450#ifdef DEBUG_HTTP
1451 xmlGenericError(xmlGenericErrorContext, "<- %s\n", p);
1452#endif
1453 xmlFree(p);
1454 }
1455
1456 if ((ctxt->location != NULL) && (ctxt->returnValue >= 300) &&
1457 (ctxt->returnValue < 400)) {
1458#ifdef DEBUG_HTTP
1459 xmlGenericError(xmlGenericErrorContext,
1460 "\nRedirect to: %s\n", ctxt->location);
1461#endif
Daniel Veillardf012a642001-07-23 19:10:52 +00001462 while ( xmlNanoHTTPRecv(ctxt) > 0 ) ;
Owen Taylor3473f882001-02-23 17:55:21 +00001463 if (nbRedirects < XML_NANO_HTTP_MAX_REDIR) {
1464 nbRedirects++;
Daniel Veillard9403a042001-05-28 11:00:53 +00001465 if (redirURL != NULL)
1466 xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001467 redirURL = xmlMemStrdup(ctxt->location);
1468 xmlNanoHTTPFreeCtxt(ctxt);
1469 goto retry;
1470 }
1471 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001472 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001473#ifdef DEBUG_HTTP
1474 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardf012a642001-07-23 19:10:52 +00001475 "xmlNanoHTTPMethodRedir: Too many redirects, aborting ...\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001476#endif
1477 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001478 }
1479
1480 if (contentType != NULL) {
1481 if (ctxt->contentType != NULL)
1482 *contentType = xmlMemStrdup(ctxt->contentType);
1483 else
1484 *contentType = NULL;
1485 }
1486
Daniel Veillard9403a042001-05-28 11:00:53 +00001487 if ((redir != NULL) && (redirURL != NULL)) {
1488 *redir = redirURL;
1489 } else {
1490 if (redirURL != NULL)
1491 xmlFree(redirURL);
1492 if (redir != NULL)
1493 *redir = NULL;
1494 }
1495
Owen Taylor3473f882001-02-23 17:55:21 +00001496#ifdef DEBUG_HTTP
1497 if (ctxt->contentType != NULL)
1498 xmlGenericError(xmlGenericErrorContext,
1499 "\nCode %d, content-type '%s'\n\n",
1500 ctxt->returnValue, ctxt->contentType);
1501 else
1502 xmlGenericError(xmlGenericErrorContext,
1503 "\nCode %d, no content-type\n\n",
1504 ctxt->returnValue);
1505#endif
1506
1507 return((void *) ctxt);
1508}
1509
1510/**
Daniel Veillard9403a042001-05-28 11:00:53 +00001511 * xmlNanoHTTPMethod:
1512 * @URL: The URL to load
1513 * @method: the HTTP method to use
1514 * @input: the input string if any
1515 * @contentType: the Content-Type information IN and OUT
1516 * @headers: the extra headers
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001517 * @ilen: input length
Daniel Veillard9403a042001-05-28 11:00:53 +00001518 *
1519 * This function try to open a connection to the indicated resource
1520 * via HTTP using the given @method, adding the given extra headers
1521 * and the input buffer for the request content.
1522 *
1523 * Returns NULL in case of failure, otherwise a request handler.
1524 * The contentType, if provided must be freed by the caller
1525 */
1526
1527void*
1528xmlNanoHTTPMethod(const char *URL, const char *method, const char *input,
Daniel Veillardf012a642001-07-23 19:10:52 +00001529 char **contentType, const char *headers, int ilen) {
Daniel Veillard9403a042001-05-28 11:00:53 +00001530 return(xmlNanoHTTPMethodRedir(URL, method, input, contentType,
Daniel Veillardf012a642001-07-23 19:10:52 +00001531 NULL, headers, ilen));
Daniel Veillard9403a042001-05-28 11:00:53 +00001532}
1533
1534/**
Owen Taylor3473f882001-02-23 17:55:21 +00001535 * xmlNanoHTTPFetch:
1536 * @URL: The URL to load
1537 * @filename: the filename where the content should be saved
1538 * @contentType: if available the Content-Type information will be
1539 * returned at that location
1540 *
1541 * This function try to fetch the indicated resource via HTTP GET
1542 * and save it's content in the file.
1543 *
1544 * Returns -1 in case of failure, 0 incase of success. The contentType,
1545 * if provided must be freed by the caller
1546 */
1547int
1548xmlNanoHTTPFetch(const char *URL, const char *filename, char **contentType) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001549 void *ctxt = NULL;
1550 char *buf = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001551 int fd;
1552 int len;
1553
1554 ctxt = xmlNanoHTTPOpen(URL, contentType);
1555 if (ctxt == NULL) return(-1);
1556
1557 if (!strcmp(filename, "-"))
1558 fd = 0;
1559 else {
1560 fd = open(filename, O_CREAT | O_WRONLY, 00644);
1561 if (fd < 0) {
1562 xmlNanoHTTPClose(ctxt);
1563 if ((contentType != NULL) && (*contentType != NULL)) {
1564 xmlFree(*contentType);
1565 *contentType = NULL;
1566 }
1567 return(-1);
1568 }
1569 }
1570
Daniel Veillardf012a642001-07-23 19:10:52 +00001571 xmlNanoHTTPFetchContent( ctxt, &buf, &len );
1572 if ( len > 0 ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001573 write(fd, buf, len);
1574 }
1575
1576 xmlNanoHTTPClose(ctxt);
1577 close(fd);
1578 return(0);
1579}
1580
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001581#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001582/**
1583 * xmlNanoHTTPSave:
1584 * @ctxt: the HTTP context
1585 * @filename: the filename where the content should be saved
1586 *
1587 * This function saves the output of the HTTP transaction to a file
1588 * It closes and free the context at the end
1589 *
1590 * Returns -1 in case of failure, 0 incase of success.
1591 */
1592int
1593xmlNanoHTTPSave(void *ctxt, const char *filename) {
Daniel Veillarde3924972001-07-25 20:25:21 +00001594 char *buf = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001595 int fd;
1596 int len;
1597
1598 if (ctxt == NULL) return(-1);
1599
1600 if (!strcmp(filename, "-"))
1601 fd = 0;
1602 else {
1603 fd = open(filename, O_CREAT | O_WRONLY);
1604 if (fd < 0) {
1605 xmlNanoHTTPClose(ctxt);
1606 return(-1);
1607 }
1608 }
1609
Daniel Veillardf012a642001-07-23 19:10:52 +00001610 xmlNanoHTTPFetchContent( ctxt, &buf, &len );
1611 if ( len > 0 ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001612 write(fd, buf, len);
1613 }
1614
1615 xmlNanoHTTPClose(ctxt);
William M. Brack20d82362004-03-17 08:44:46 +00001616 close(fd);
Owen Taylor3473f882001-02-23 17:55:21 +00001617 return(0);
1618}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001619#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001620
1621/**
1622 * xmlNanoHTTPReturnCode:
1623 * @ctx: the HTTP context
1624 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001625 * Get the latest HTTP return code received
1626 *
Owen Taylor3473f882001-02-23 17:55:21 +00001627 * Returns the HTTP return code for the request.
1628 */
1629int
1630xmlNanoHTTPReturnCode(void *ctx) {
1631 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1632
1633 if (ctxt == NULL) return(-1);
1634
1635 return(ctxt->returnValue);
1636}
1637
1638/**
1639 * xmlNanoHTTPAuthHeader:
1640 * @ctx: the HTTP context
1641 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001642 * Get the authentication header of an HTTP context
1643 *
Owen Taylor3473f882001-02-23 17:55:21 +00001644 * Returns the stashed value of the WWW-Authenticate or Proxy-Authenticate
1645 * header.
1646 */
1647const char *
1648xmlNanoHTTPAuthHeader(void *ctx) {
1649 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1650
1651 if (ctxt == NULL) return(NULL);
1652
1653 return(ctxt->authHeader);
1654}
1655
Daniel Veillardf012a642001-07-23 19:10:52 +00001656/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00001657 * xmlNanoHTTPContentLength:
Daniel Veillardf012a642001-07-23 19:10:52 +00001658 * @ctx: the HTTP context
1659 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001660 * Provides the specified content length from the HTTP header.
1661 *
Daniel Veillardf012a642001-07-23 19:10:52 +00001662 * Return the specified content length from the HTTP header. Note that
1663 * a value of -1 indicates that the content length element was not included in
1664 * the response header.
1665 */
1666int
1667xmlNanoHTTPContentLength( void * ctx ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001668 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillardf012a642001-07-23 19:10:52 +00001669
1670 return ( ( ctxt == NULL ) ? -1 : ctxt->ContentLength );
1671}
1672
1673/**
Daniel Veillard847332a2003-10-18 11:29:40 +00001674 * xmlNanoHTTPRedir:
1675 * @ctx: the HTTP context
1676 *
1677 * Provides the specified redirection URL if available from the HTTP header.
1678 *
1679 * Return the specified redirection URL or NULL if not redirected.
1680 */
1681const char *
1682xmlNanoHTTPRedir( void * ctx ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001683 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillard847332a2003-10-18 11:29:40 +00001684
1685 return ( ( ctxt == NULL ) ? NULL : ctxt->location );
1686}
1687
1688/**
1689 * xmlNanoHTTPEncoding:
1690 * @ctx: the HTTP context
1691 *
1692 * Provides the specified encoding if specified in the HTTP headers.
1693 *
1694 * Return the specified encoding or NULL if not available
1695 */
1696const char *
1697xmlNanoHTTPEncoding( void * ctx ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001698 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillard847332a2003-10-18 11:29:40 +00001699
1700 return ( ( ctxt == NULL ) ? NULL : ctxt->encoding );
1701}
1702
1703/**
Daniel Veillarda840b692003-10-19 13:35:37 +00001704 * xmlNanoHTTPMimeType:
1705 * @ctx: the HTTP context
1706 *
1707 * Provides the specified Mime-Type if specified in the HTTP headers.
1708 *
1709 * Return the specified Mime-Type or NULL if not available
1710 */
1711const char *
1712xmlNanoHTTPMimeType( void * ctx ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001713 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillarda840b692003-10-19 13:35:37 +00001714
1715 return ( ( ctxt == NULL ) ? NULL : ctxt->mimeType );
1716}
1717
1718/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00001719 * xmlNanoHTTPFetchContent:
Daniel Veillardf012a642001-07-23 19:10:52 +00001720 * @ctx: the HTTP context
1721 * @ptr: pointer to set to the content buffer.
1722 * @len: integer pointer to hold the length of the content
1723 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001724 * Check if all the content was read
1725 *
Daniel Veillardf012a642001-07-23 19:10:52 +00001726 * Returns 0 if all the content was read and available, returns
1727 * -1 if received content length was less than specified or an error
1728 * occurred.
1729 */
Daniel Veillarda2351322004-06-27 12:08:10 +00001730static int
Daniel Veillardf012a642001-07-23 19:10:52 +00001731xmlNanoHTTPFetchContent( void * ctx, char ** ptr, int * len ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001732 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillardf012a642001-07-23 19:10:52 +00001733
1734 int rc = 0;
1735 int cur_lgth;
1736 int rcvd_lgth;
1737 int dummy_int;
1738 char * dummy_ptr = NULL;
1739
1740 /* Dummy up return input parameters if not provided */
1741
1742 if ( len == NULL )
1743 len = &dummy_int;
1744
1745 if ( ptr == NULL )
1746 ptr = &dummy_ptr;
1747
1748 /* But can't work without the context pointer */
1749
1750 if ( ( ctxt == NULL ) || ( ctxt->content == NULL ) ) {
1751 *len = 0;
1752 *ptr = NULL;
1753 return ( -1 );
1754 }
1755
1756 rcvd_lgth = ctxt->inptr - ctxt->content;
1757
1758 while ( (cur_lgth = xmlNanoHTTPRecv( ctxt )) > 0 ) {
1759
1760 rcvd_lgth += cur_lgth;
1761 if ( (ctxt->ContentLength > 0) && (rcvd_lgth >= ctxt->ContentLength) )
1762 break;
1763 }
1764
1765 *ptr = ctxt->content;
1766 *len = rcvd_lgth;
1767
1768 if ( ( ctxt->ContentLength > 0 ) && ( rcvd_lgth < ctxt->ContentLength ) )
1769 rc = -1;
1770 else if ( rcvd_lgth == 0 )
1771 rc = -1;
1772
1773 return ( rc );
1774}
1775
Owen Taylor3473f882001-02-23 17:55:21 +00001776#ifdef STANDALONE
1777int main(int argc, char **argv) {
1778 char *contentType = NULL;
1779
1780 if (argv[1] != NULL) {
1781 if (argv[2] != NULL)
1782 xmlNanoHTTPFetch(argv[1], argv[2], &contentType);
1783 else
1784 xmlNanoHTTPFetch(argv[1], "-", &contentType);
1785 if (contentType != NULL) xmlFree(contentType);
1786 } else {
1787 xmlGenericError(xmlGenericErrorContext,
1788 "%s: minimal HTTP GET implementation\n", argv[0]);
1789 xmlGenericError(xmlGenericErrorContext,
1790 "\tusage %s [ URL [ filename ] ]\n", argv[0]);
1791 }
1792 xmlNanoHTTPCleanup();
1793 xmlMemoryDump();
1794 return(0);
1795}
1796#endif /* STANDALONE */
1797#else /* !LIBXML_HTTP_ENABLED */
1798#ifdef STANDALONE
1799#include <stdio.h>
1800int main(int argc, char **argv) {
1801 xmlGenericError(xmlGenericErrorContext,
1802 "%s : HTTP support not compiled in\n", argv[0]);
1803 return(0);
1804}
1805#endif /* STANDALONE */
1806#endif /* LIBXML_HTTP_ENABLED */