blob: 8dbd9fc0601ee935cc6f72566ff19483042cab26 [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) {
1075 memcpy (&sockin6, res->ai_addr, res->ai_addrlen);
1076 sockin6.sin6_port = htons (port);
1077 addr = (struct sockaddr *)&sockin6;
1078 }
Daniel Veillard3dc93a42003-07-10 14:04:33 +00001079 else {
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001080 memcpy (&sockin, res->ai_addr, res->ai_addrlen);
1081 sockin.sin_port = htons (port);
1082 addr = (struct sockaddr *)&sockin;
1083 }
1084
1085 s = xmlNanoHTTPConnectAttempt (addr);
1086 if (s != -1) {
1087 freeaddrinfo (result);
1088 return (s);
1089 }
1090 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001091 }
Daniel Veillard3dc93a42003-07-10 14:04:33 +00001092 if (result)
1093 freeaddrinfo (result);
1094 return (-1);
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001095 } else
Owen Taylor3473f882001-02-23 17:55:21 +00001096#endif
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001097#endif
1098 {
1099 h = gethostbyname (host);
1100 if (h == NULL) {
Daniel Veillard56b2db72002-03-25 16:35:28 +00001101
1102/*
1103 * Okay, I got fed up by the non-portability of this error message
1104 * extraction code. it work on Linux, if it work on your platform
1105 * and one want to enable it, send me the defined(foobar) needed
1106 */
1107#if defined(HAVE_NETDB_H) && defined(HOST_NOT_FOUND) && defined(linux)
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001108 const char *h_err_txt = "";
Daniel Veillardf012a642001-07-23 19:10:52 +00001109
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001110 switch (h_errno) {
1111 case HOST_NOT_FOUND:
1112 h_err_txt = "Authoritive host not found";
1113 break;
Daniel Veillardf012a642001-07-23 19:10:52 +00001114
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001115 case TRY_AGAIN:
1116 h_err_txt =
1117 "Non-authoritive host not found or server failure.";
1118 break;
Daniel Veillardf012a642001-07-23 19:10:52 +00001119
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001120 case NO_RECOVERY:
1121 h_err_txt =
1122 "Non-recoverable errors: FORMERR, REFUSED, or NOTIMP.";
1123 break;
Daniel Veillard5c396542002-03-15 07:57:50 +00001124
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001125 case NO_ADDRESS:
1126 h_err_txt =
1127 "Valid name, no data record of requested type.";
1128 break;
Daniel Veillard5c396542002-03-15 07:57:50 +00001129
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001130 default:
1131 h_err_txt = "No error text defined.";
1132 break;
1133 }
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001134 __xmlIOErr(XML_FROM_HTTP, 0, h_err_txt);
Daniel Veillard5c396542002-03-15 07:57:50 +00001135#else
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001136 __xmlIOErr(XML_FROM_HTTP, 0, "Failed to resolve host");
Owen Taylor3473f882001-02-23 17:55:21 +00001137#endif
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001138 return (-1);
1139 }
Daniel Veillard5c396542002-03-15 07:57:50 +00001140
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001141 for (i = 0; h->h_addr_list[i]; i++) {
1142 if (h->h_addrtype == AF_INET) {
1143 /* A records (IPv4) */
1144 memcpy (&ia, h->h_addr_list[i], h->h_length);
1145 sockin.sin_family = h->h_addrtype;
1146 sockin.sin_addr = ia;
1147 sockin.sin_port = htons (port);
1148 addr = (struct sockaddr *) &sockin;
Daniel Veillard5c396542002-03-15 07:57:50 +00001149#ifdef SUPPORT_IP6
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001150 } else if (have_ipv6 () && (h->h_addrtype == AF_INET6)) {
1151 /* AAAA records (IPv6) */
1152 memcpy (&ia6, h->h_addr_list[i], h->h_length);
1153 sockin6.sin6_family = h->h_addrtype;
1154 sockin6.sin6_addr = ia6;
1155 sockin6.sin6_port = htons (port);
1156 addr = (struct sockaddr *) &sockin6;
Daniel Veillard5c396542002-03-15 07:57:50 +00001157#endif
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001158 } else
1159 break; /* for */
Daniel Veillard5c396542002-03-15 07:57:50 +00001160
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001161 s = xmlNanoHTTPConnectAttempt (addr);
1162 if (s != -1)
1163 return (s);
1164 }
Owen Taylor3473f882001-02-23 17:55:21 +00001165 }
Owen Taylor3473f882001-02-23 17:55:21 +00001166#ifdef DEBUG_HTTP
1167 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard5c396542002-03-15 07:57:50 +00001168 "xmlNanoHTTPConnectHost: unable to connect to '%s'.\n",
1169 host);
Owen Taylor3473f882001-02-23 17:55:21 +00001170#endif
Daniel Veillard5c396542002-03-15 07:57:50 +00001171 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001172}
1173
1174
1175/**
1176 * xmlNanoHTTPOpen:
1177 * @URL: The URL to load
1178 * @contentType: if available the Content-Type information will be
1179 * returned at that location
1180 *
1181 * This function try to open a connection to the indicated resource
1182 * via HTTP GET.
1183 *
1184 * Returns NULL in case of failure, otherwise a request handler.
1185 * The contentType, if provided must be freed by the caller
1186 */
1187
1188void*
1189xmlNanoHTTPOpen(const char *URL, char **contentType) {
1190 if (contentType != NULL) *contentType = NULL;
Daniel Veillardf012a642001-07-23 19:10:52 +00001191 return(xmlNanoHTTPMethod(URL, NULL, NULL, contentType, NULL, 0));
Daniel Veillard9403a042001-05-28 11:00:53 +00001192}
1193
1194/**
1195 * xmlNanoHTTPOpenRedir:
1196 * @URL: The URL to load
1197 * @contentType: if available the Content-Type information will be
1198 * returned at that location
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001199 * @redir: if available the redirected URL will be returned
Daniel Veillard9403a042001-05-28 11:00:53 +00001200 *
1201 * This function try to open a connection to the indicated resource
1202 * via HTTP GET.
1203 *
1204 * Returns NULL in case of failure, otherwise a request handler.
1205 * The contentType, if provided must be freed by the caller
1206 */
1207
1208void*
1209xmlNanoHTTPOpenRedir(const char *URL, char **contentType, char **redir) {
1210 if (contentType != NULL) *contentType = NULL;
1211 if (redir != NULL) *redir = NULL;
Daniel Veillardf012a642001-07-23 19:10:52 +00001212 return(xmlNanoHTTPMethodRedir(URL, NULL, NULL, contentType, redir, NULL,0));
Owen Taylor3473f882001-02-23 17:55:21 +00001213}
1214
1215/**
1216 * xmlNanoHTTPRead:
1217 * @ctx: the HTTP context
1218 * @dest: a buffer
1219 * @len: the buffer length
1220 *
1221 * This function tries to read @len bytes from the existing HTTP connection
1222 * and saves them in @dest. This is a blocking call.
1223 *
1224 * Returns the number of byte read. 0 is an indication of an end of connection.
1225 * -1 indicates a parameter error.
1226 */
1227int
1228xmlNanoHTTPRead(void *ctx, void *dest, int len) {
1229 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1230
1231 if (ctx == NULL) return(-1);
1232 if (dest == NULL) return(-1);
1233 if (len <= 0) return(0);
1234
1235 while (ctxt->inptr - ctxt->inrptr < len) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001236 if (xmlNanoHTTPRecv(ctxt) <= 0) break;
Owen Taylor3473f882001-02-23 17:55:21 +00001237 }
1238 if (ctxt->inptr - ctxt->inrptr < len)
1239 len = ctxt->inptr - ctxt->inrptr;
1240 memcpy(dest, ctxt->inrptr, len);
1241 ctxt->inrptr += len;
1242 return(len);
1243}
1244
1245/**
1246 * xmlNanoHTTPClose:
1247 * @ctx: the HTTP context
1248 *
1249 * This function closes an HTTP context, it ends up the connection and
1250 * free all data related to it.
1251 */
1252void
1253xmlNanoHTTPClose(void *ctx) {
1254 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1255
1256 if (ctx == NULL) return;
1257
1258 xmlNanoHTTPFreeCtxt(ctxt);
1259}
1260
1261/**
Daniel Veillard9403a042001-05-28 11:00:53 +00001262 * xmlNanoHTTPMethodRedir:
Owen Taylor3473f882001-02-23 17:55:21 +00001263 * @URL: The URL to load
1264 * @method: the HTTP method to use
1265 * @input: the input string if any
1266 * @contentType: the Content-Type information IN and OUT
Daniel Veillard9403a042001-05-28 11:00:53 +00001267 * @redir: the redirected URL OUT
Owen Taylor3473f882001-02-23 17:55:21 +00001268 * @headers: the extra headers
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001269 * @ilen: input length
Owen Taylor3473f882001-02-23 17:55:21 +00001270 *
1271 * This function try to open a connection to the indicated resource
1272 * via HTTP using the given @method, adding the given extra headers
1273 * and the input buffer for the request content.
1274 *
1275 * Returns NULL in case of failure, otherwise a request handler.
Daniel Veillard9403a042001-05-28 11:00:53 +00001276 * The contentType, or redir, if provided must be freed by the caller
Owen Taylor3473f882001-02-23 17:55:21 +00001277 */
1278
1279void*
Daniel Veillard9403a042001-05-28 11:00:53 +00001280xmlNanoHTTPMethodRedir(const char *URL, const char *method, const char *input,
Daniel Veillardf012a642001-07-23 19:10:52 +00001281 char **contentType, char **redir,
1282 const char *headers, int ilen ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001283 xmlNanoHTTPCtxtPtr ctxt;
1284 char *bp, *p;
Daniel Veillardf012a642001-07-23 19:10:52 +00001285 int blen, ret;
Owen Taylor3473f882001-02-23 17:55:21 +00001286 int head;
1287 int nbRedirects = 0;
1288 char *redirURL = NULL;
William M. Brack78637da2003-07-31 14:47:38 +00001289#ifdef DEBUG_HTTP
1290 int xmt_bytes;
1291#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001292
1293 if (URL == NULL) return(NULL);
1294 if (method == NULL) method = "GET";
1295 xmlNanoHTTPInit();
1296
1297retry:
1298 if (redirURL == NULL)
1299 ctxt = xmlNanoHTTPNewCtxt(URL);
1300 else {
1301 ctxt = xmlNanoHTTPNewCtxt(redirURL);
Daniel Veillarda840b692003-10-19 13:35:37 +00001302 ctxt->location = xmlMemStrdup(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001303 }
1304
Daniel Veillardf012a642001-07-23 19:10:52 +00001305 if ( ctxt == NULL ) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001306 return ( NULL );
1307 }
1308
Owen Taylor3473f882001-02-23 17:55:21 +00001309 if ((ctxt->protocol == NULL) || (strcmp(ctxt->protocol, "http"))) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001310 __xmlIOErr(XML_FROM_HTTP, XML_HTTP_URL_SYNTAX, "Not a valid HTTP URI");
Owen Taylor3473f882001-02-23 17:55:21 +00001311 xmlNanoHTTPFreeCtxt(ctxt);
1312 if (redirURL != NULL) xmlFree(redirURL);
1313 return(NULL);
1314 }
1315 if (ctxt->hostname == NULL) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001316 __xmlIOErr(XML_FROM_HTTP, XML_HTTP_UNKNOWN_HOST,
1317 "Failed to identify host in URI");
Owen Taylor3473f882001-02-23 17:55:21 +00001318 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001319 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001320 return(NULL);
1321 }
1322 if (proxy) {
1323 blen = strlen(ctxt->hostname) * 2 + 16;
1324 ret = xmlNanoHTTPConnectHost(proxy, proxyPort);
1325 }
1326 else {
1327 blen = strlen(ctxt->hostname);
1328 ret = xmlNanoHTTPConnectHost(ctxt->hostname, ctxt->port);
1329 }
1330 if (ret < 0) {
1331 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001332 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001333 return(NULL);
1334 }
1335 ctxt->fd = ret;
1336
Daniel Veillardf012a642001-07-23 19:10:52 +00001337 if (input == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00001338 ilen = 0;
Daniel Veillardf012a642001-07-23 19:10:52 +00001339 else
1340 blen += 36;
1341
Owen Taylor3473f882001-02-23 17:55:21 +00001342 if (headers != NULL)
Daniel Veillardf012a642001-07-23 19:10:52 +00001343 blen += strlen(headers) + 2;
Owen Taylor3473f882001-02-23 17:55:21 +00001344 if (contentType && *contentType)
1345 blen += strlen(*contentType) + 16;
Daniel Veillardf012a642001-07-23 19:10:52 +00001346 blen += strlen(method) + strlen(ctxt->path) + 24;
Daniel Veillard82cb3192003-10-29 13:39:15 +00001347 bp = (char*)xmlMallocAtomic(blen);
Daniel Veillardf012a642001-07-23 19:10:52 +00001348 if ( bp == NULL ) {
1349 xmlNanoHTTPFreeCtxt( ctxt );
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001350 xmlHTTPErrMemory("allocating header buffer");
Daniel Veillardf012a642001-07-23 19:10:52 +00001351 return ( NULL );
1352 }
1353
1354 p = bp;
1355
Owen Taylor3473f882001-02-23 17:55:21 +00001356 if (proxy) {
1357 if (ctxt->port != 80) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001358 p += snprintf( p, blen - (p - bp), "%s http://%s:%d%s",
1359 method, ctxt->hostname,
Daniel Veillardf012a642001-07-23 19:10:52 +00001360 ctxt->port, ctxt->path );
Owen Taylor3473f882001-02-23 17:55:21 +00001361 }
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001362 else
1363 p += snprintf( p, blen - (p - bp), "%s http://%s%s", method,
Daniel Veillardf012a642001-07-23 19:10:52 +00001364 ctxt->hostname, ctxt->path);
Owen Taylor3473f882001-02-23 17:55:21 +00001365 }
1366 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001367 p += snprintf( p, blen - (p - bp), "%s %s", method, ctxt->path);
Daniel Veillardf012a642001-07-23 19:10:52 +00001368
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001369 p += snprintf( p, blen - (p - bp), " HTTP/1.0\r\nHost: %s\r\n",
1370 ctxt->hostname);
Daniel Veillardf012a642001-07-23 19:10:52 +00001371
1372 if (contentType != NULL && *contentType)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001373 p += snprintf(p, blen - (p - bp), "Content-Type: %s\r\n", *contentType);
Daniel Veillardf012a642001-07-23 19:10:52 +00001374
1375 if (headers != NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001376 p += snprintf( p, blen - (p - bp), "%s", headers );
Daniel Veillardf012a642001-07-23 19:10:52 +00001377
Owen Taylor3473f882001-02-23 17:55:21 +00001378 if (input != NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001379 snprintf(p, blen - (p - bp), "Content-Length: %d\r\n\r\n", ilen );
Owen Taylor3473f882001-02-23 17:55:21 +00001380 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001381 snprintf(p, blen - (p - bp), "\r\n");
Daniel Veillardf012a642001-07-23 19:10:52 +00001382
Owen Taylor3473f882001-02-23 17:55:21 +00001383#ifdef DEBUG_HTTP
1384 xmlGenericError(xmlGenericErrorContext,
1385 "-> %s%s", proxy? "(Proxy) " : "", bp);
1386 if ((blen -= strlen(bp)+1) < 0)
1387 xmlGenericError(xmlGenericErrorContext,
1388 "ERROR: overflowed buffer by %d bytes\n", -blen);
1389#endif
1390 ctxt->outptr = ctxt->out = bp;
1391 ctxt->state = XML_NANO_HTTP_WRITE;
Daniel Veillardf012a642001-07-23 19:10:52 +00001392 blen = strlen( ctxt->out );
Daniel Veillardf012a642001-07-23 19:10:52 +00001393#ifdef DEBUG_HTTP
William M. Brack78637da2003-07-31 14:47:38 +00001394 xmt_bytes = xmlNanoHTTPSend(ctxt, ctxt->out, blen );
Daniel Veillardf012a642001-07-23 19:10:52 +00001395 if ( xmt_bytes != blen )
1396 xmlGenericError( xmlGenericErrorContext,
1397 "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
1398 xmt_bytes, blen,
1399 "bytes of HTTP headers sent to host",
1400 ctxt->hostname );
William M. Brack78637da2003-07-31 14:47:38 +00001401#else
1402 xmlNanoHTTPSend(ctxt, ctxt->out, blen );
Daniel Veillardf012a642001-07-23 19:10:52 +00001403#endif
1404
1405 if ( input != NULL ) {
William M. Brack78637da2003-07-31 14:47:38 +00001406#ifdef DEBUG_HTTP
Daniel Veillardf012a642001-07-23 19:10:52 +00001407 xmt_bytes = xmlNanoHTTPSend( ctxt, input, ilen );
1408
Daniel Veillardf012a642001-07-23 19:10:52 +00001409 if ( xmt_bytes != ilen )
1410 xmlGenericError( xmlGenericErrorContext,
1411 "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
1412 xmt_bytes, ilen,
1413 "bytes of HTTP content sent to host",
1414 ctxt->hostname );
William M. Brack78637da2003-07-31 14:47:38 +00001415#else
1416 xmlNanoHTTPSend( ctxt, input, ilen );
Daniel Veillardf012a642001-07-23 19:10:52 +00001417#endif
1418 }
1419
Owen Taylor3473f882001-02-23 17:55:21 +00001420 ctxt->state = XML_NANO_HTTP_READ;
1421 head = 1;
1422
1423 while ((p = xmlNanoHTTPReadLine(ctxt)) != NULL) {
1424 if (head && (*p == 0)) {
1425 head = 0;
1426 ctxt->content = ctxt->inrptr;
1427 xmlFree(p);
1428 break;
1429 }
1430 xmlNanoHTTPScanAnswer(ctxt, p);
1431
1432#ifdef DEBUG_HTTP
1433 xmlGenericError(xmlGenericErrorContext, "<- %s\n", p);
1434#endif
1435 xmlFree(p);
1436 }
1437
1438 if ((ctxt->location != NULL) && (ctxt->returnValue >= 300) &&
1439 (ctxt->returnValue < 400)) {
1440#ifdef DEBUG_HTTP
1441 xmlGenericError(xmlGenericErrorContext,
1442 "\nRedirect to: %s\n", ctxt->location);
1443#endif
Daniel Veillardf012a642001-07-23 19:10:52 +00001444 while ( xmlNanoHTTPRecv(ctxt) > 0 ) ;
Owen Taylor3473f882001-02-23 17:55:21 +00001445 if (nbRedirects < XML_NANO_HTTP_MAX_REDIR) {
1446 nbRedirects++;
Daniel Veillard9403a042001-05-28 11:00:53 +00001447 if (redirURL != NULL)
1448 xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001449 redirURL = xmlMemStrdup(ctxt->location);
1450 xmlNanoHTTPFreeCtxt(ctxt);
1451 goto retry;
1452 }
1453 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001454 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001455#ifdef DEBUG_HTTP
1456 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardf012a642001-07-23 19:10:52 +00001457 "xmlNanoHTTPMethodRedir: Too many redirects, aborting ...\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001458#endif
1459 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001460 }
1461
1462 if (contentType != NULL) {
1463 if (ctxt->contentType != NULL)
1464 *contentType = xmlMemStrdup(ctxt->contentType);
1465 else
1466 *contentType = NULL;
1467 }
1468
Daniel Veillard9403a042001-05-28 11:00:53 +00001469 if ((redir != NULL) && (redirURL != NULL)) {
1470 *redir = redirURL;
1471 } else {
1472 if (redirURL != NULL)
1473 xmlFree(redirURL);
1474 if (redir != NULL)
1475 *redir = NULL;
1476 }
1477
Owen Taylor3473f882001-02-23 17:55:21 +00001478#ifdef DEBUG_HTTP
1479 if (ctxt->contentType != NULL)
1480 xmlGenericError(xmlGenericErrorContext,
1481 "\nCode %d, content-type '%s'\n\n",
1482 ctxt->returnValue, ctxt->contentType);
1483 else
1484 xmlGenericError(xmlGenericErrorContext,
1485 "\nCode %d, no content-type\n\n",
1486 ctxt->returnValue);
1487#endif
1488
1489 return((void *) ctxt);
1490}
1491
1492/**
Daniel Veillard9403a042001-05-28 11:00:53 +00001493 * xmlNanoHTTPMethod:
1494 * @URL: The URL to load
1495 * @method: the HTTP method to use
1496 * @input: the input string if any
1497 * @contentType: the Content-Type information IN and OUT
1498 * @headers: the extra headers
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001499 * @ilen: input length
Daniel Veillard9403a042001-05-28 11:00:53 +00001500 *
1501 * This function try to open a connection to the indicated resource
1502 * via HTTP using the given @method, adding the given extra headers
1503 * and the input buffer for the request content.
1504 *
1505 * Returns NULL in case of failure, otherwise a request handler.
1506 * The contentType, if provided must be freed by the caller
1507 */
1508
1509void*
1510xmlNanoHTTPMethod(const char *URL, const char *method, const char *input,
Daniel Veillardf012a642001-07-23 19:10:52 +00001511 char **contentType, const char *headers, int ilen) {
Daniel Veillard9403a042001-05-28 11:00:53 +00001512 return(xmlNanoHTTPMethodRedir(URL, method, input, contentType,
Daniel Veillardf012a642001-07-23 19:10:52 +00001513 NULL, headers, ilen));
Daniel Veillard9403a042001-05-28 11:00:53 +00001514}
1515
1516/**
Owen Taylor3473f882001-02-23 17:55:21 +00001517 * xmlNanoHTTPFetch:
1518 * @URL: The URL to load
1519 * @filename: the filename where the content should be saved
1520 * @contentType: if available the Content-Type information will be
1521 * returned at that location
1522 *
1523 * This function try to fetch the indicated resource via HTTP GET
1524 * and save it's content in the file.
1525 *
1526 * Returns -1 in case of failure, 0 incase of success. The contentType,
1527 * if provided must be freed by the caller
1528 */
1529int
1530xmlNanoHTTPFetch(const char *URL, const char *filename, char **contentType) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001531 void *ctxt = NULL;
1532 char *buf = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001533 int fd;
1534 int len;
1535
1536 ctxt = xmlNanoHTTPOpen(URL, contentType);
1537 if (ctxt == NULL) return(-1);
1538
1539 if (!strcmp(filename, "-"))
1540 fd = 0;
1541 else {
1542 fd = open(filename, O_CREAT | O_WRONLY, 00644);
1543 if (fd < 0) {
1544 xmlNanoHTTPClose(ctxt);
1545 if ((contentType != NULL) && (*contentType != NULL)) {
1546 xmlFree(*contentType);
1547 *contentType = NULL;
1548 }
1549 return(-1);
1550 }
1551 }
1552
Daniel Veillardf012a642001-07-23 19:10:52 +00001553 xmlNanoHTTPFetchContent( ctxt, &buf, &len );
1554 if ( len > 0 ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001555 write(fd, buf, len);
1556 }
1557
1558 xmlNanoHTTPClose(ctxt);
1559 close(fd);
1560 return(0);
1561}
1562
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001563#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001564/**
1565 * xmlNanoHTTPSave:
1566 * @ctxt: the HTTP context
1567 * @filename: the filename where the content should be saved
1568 *
1569 * This function saves the output of the HTTP transaction to a file
1570 * It closes and free the context at the end
1571 *
1572 * Returns -1 in case of failure, 0 incase of success.
1573 */
1574int
1575xmlNanoHTTPSave(void *ctxt, const char *filename) {
Daniel Veillarde3924972001-07-25 20:25:21 +00001576 char *buf = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001577 int fd;
1578 int len;
1579
1580 if (ctxt == NULL) return(-1);
1581
1582 if (!strcmp(filename, "-"))
1583 fd = 0;
1584 else {
1585 fd = open(filename, O_CREAT | O_WRONLY);
1586 if (fd < 0) {
1587 xmlNanoHTTPClose(ctxt);
1588 return(-1);
1589 }
1590 }
1591
Daniel Veillardf012a642001-07-23 19:10:52 +00001592 xmlNanoHTTPFetchContent( ctxt, &buf, &len );
1593 if ( len > 0 ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001594 write(fd, buf, len);
1595 }
1596
1597 xmlNanoHTTPClose(ctxt);
William M. Brack20d82362004-03-17 08:44:46 +00001598 close(fd);
Owen Taylor3473f882001-02-23 17:55:21 +00001599 return(0);
1600}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001601#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001602
1603/**
1604 * xmlNanoHTTPReturnCode:
1605 * @ctx: the HTTP context
1606 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001607 * Get the latest HTTP return code received
1608 *
Owen Taylor3473f882001-02-23 17:55:21 +00001609 * Returns the HTTP return code for the request.
1610 */
1611int
1612xmlNanoHTTPReturnCode(void *ctx) {
1613 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1614
1615 if (ctxt == NULL) return(-1);
1616
1617 return(ctxt->returnValue);
1618}
1619
1620/**
1621 * xmlNanoHTTPAuthHeader:
1622 * @ctx: the HTTP context
1623 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001624 * Get the authentication header of an HTTP context
1625 *
Owen Taylor3473f882001-02-23 17:55:21 +00001626 * Returns the stashed value of the WWW-Authenticate or Proxy-Authenticate
1627 * header.
1628 */
1629const char *
1630xmlNanoHTTPAuthHeader(void *ctx) {
1631 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1632
1633 if (ctxt == NULL) return(NULL);
1634
1635 return(ctxt->authHeader);
1636}
1637
Daniel Veillardf012a642001-07-23 19:10:52 +00001638/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00001639 * xmlNanoHTTPContentLength:
Daniel Veillardf012a642001-07-23 19:10:52 +00001640 * @ctx: the HTTP context
1641 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001642 * Provides the specified content length from the HTTP header.
1643 *
Daniel Veillardf012a642001-07-23 19:10:52 +00001644 * Return the specified content length from the HTTP header. Note that
1645 * a value of -1 indicates that the content length element was not included in
1646 * the response header.
1647 */
1648int
1649xmlNanoHTTPContentLength( void * ctx ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001650 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillardf012a642001-07-23 19:10:52 +00001651
1652 return ( ( ctxt == NULL ) ? -1 : ctxt->ContentLength );
1653}
1654
1655/**
Daniel Veillard847332a2003-10-18 11:29:40 +00001656 * xmlNanoHTTPRedir:
1657 * @ctx: the HTTP context
1658 *
1659 * Provides the specified redirection URL if available from the HTTP header.
1660 *
1661 * Return the specified redirection URL or NULL if not redirected.
1662 */
1663const char *
1664xmlNanoHTTPRedir( void * ctx ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001665 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillard847332a2003-10-18 11:29:40 +00001666
1667 return ( ( ctxt == NULL ) ? NULL : ctxt->location );
1668}
1669
1670/**
1671 * xmlNanoHTTPEncoding:
1672 * @ctx: the HTTP context
1673 *
1674 * Provides the specified encoding if specified in the HTTP headers.
1675 *
1676 * Return the specified encoding or NULL if not available
1677 */
1678const char *
1679xmlNanoHTTPEncoding( void * ctx ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001680 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillard847332a2003-10-18 11:29:40 +00001681
1682 return ( ( ctxt == NULL ) ? NULL : ctxt->encoding );
1683}
1684
1685/**
Daniel Veillarda840b692003-10-19 13:35:37 +00001686 * xmlNanoHTTPMimeType:
1687 * @ctx: the HTTP context
1688 *
1689 * Provides the specified Mime-Type if specified in the HTTP headers.
1690 *
1691 * Return the specified Mime-Type or NULL if not available
1692 */
1693const char *
1694xmlNanoHTTPMimeType( void * ctx ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001695 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillarda840b692003-10-19 13:35:37 +00001696
1697 return ( ( ctxt == NULL ) ? NULL : ctxt->mimeType );
1698}
1699
1700/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00001701 * xmlNanoHTTPFetchContent:
Daniel Veillardf012a642001-07-23 19:10:52 +00001702 * @ctx: the HTTP context
1703 * @ptr: pointer to set to the content buffer.
1704 * @len: integer pointer to hold the length of the content
1705 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001706 * Check if all the content was read
1707 *
Daniel Veillardf012a642001-07-23 19:10:52 +00001708 * Returns 0 if all the content was read and available, returns
1709 * -1 if received content length was less than specified or an error
1710 * occurred.
1711 */
Daniel Veillarda2351322004-06-27 12:08:10 +00001712static int
Daniel Veillardf012a642001-07-23 19:10:52 +00001713xmlNanoHTTPFetchContent( void * ctx, char ** ptr, int * len ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001714 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillardf012a642001-07-23 19:10:52 +00001715
1716 int rc = 0;
1717 int cur_lgth;
1718 int rcvd_lgth;
1719 int dummy_int;
1720 char * dummy_ptr = NULL;
1721
1722 /* Dummy up return input parameters if not provided */
1723
1724 if ( len == NULL )
1725 len = &dummy_int;
1726
1727 if ( ptr == NULL )
1728 ptr = &dummy_ptr;
1729
1730 /* But can't work without the context pointer */
1731
1732 if ( ( ctxt == NULL ) || ( ctxt->content == NULL ) ) {
1733 *len = 0;
1734 *ptr = NULL;
1735 return ( -1 );
1736 }
1737
1738 rcvd_lgth = ctxt->inptr - ctxt->content;
1739
1740 while ( (cur_lgth = xmlNanoHTTPRecv( ctxt )) > 0 ) {
1741
1742 rcvd_lgth += cur_lgth;
1743 if ( (ctxt->ContentLength > 0) && (rcvd_lgth >= ctxt->ContentLength) )
1744 break;
1745 }
1746
1747 *ptr = ctxt->content;
1748 *len = rcvd_lgth;
1749
1750 if ( ( ctxt->ContentLength > 0 ) && ( rcvd_lgth < ctxt->ContentLength ) )
1751 rc = -1;
1752 else if ( rcvd_lgth == 0 )
1753 rc = -1;
1754
1755 return ( rc );
1756}
1757
Owen Taylor3473f882001-02-23 17:55:21 +00001758#ifdef STANDALONE
1759int main(int argc, char **argv) {
1760 char *contentType = NULL;
1761
1762 if (argv[1] != NULL) {
1763 if (argv[2] != NULL)
1764 xmlNanoHTTPFetch(argv[1], argv[2], &contentType);
1765 else
1766 xmlNanoHTTPFetch(argv[1], "-", &contentType);
1767 if (contentType != NULL) xmlFree(contentType);
1768 } else {
1769 xmlGenericError(xmlGenericErrorContext,
1770 "%s: minimal HTTP GET implementation\n", argv[0]);
1771 xmlGenericError(xmlGenericErrorContext,
1772 "\tusage %s [ URL [ filename ] ]\n", argv[0]);
1773 }
1774 xmlNanoHTTPCleanup();
1775 xmlMemoryDump();
1776 return(0);
1777}
1778#endif /* STANDALONE */
1779#else /* !LIBXML_HTTP_ENABLED */
1780#ifdef STANDALONE
1781#include <stdio.h>
1782int main(int argc, char **argv) {
1783 xmlGenericError(xmlGenericErrorContext,
1784 "%s : HTTP support not compiled in\n", argv[0]);
1785 return(0);
1786}
1787#endif /* STANDALONE */
1788#endif /* LIBXML_HTTP_ENABLED */