blob: ad46b8df197948b41b5419ffa5af5027bb7d9fec [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 +0000162int xmlNanoHTTPContentLength( void * ctx );
163
Owen Taylor3473f882001-02-23 17:55:21 +0000164/**
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000165 * xmlHTTPErrMemory:
166 * @extra: extra informations
167 *
168 * Handle an out of memory condition
169 */
170static void
171xmlHTTPErrMemory(const char *extra)
172{
173 __xmlSimpleError(XML_FROM_HTTP, XML_ERR_NO_MEMORY, NULL, NULL, extra);
174}
175
176/**
Owen Taylor3473f882001-02-23 17:55:21 +0000177 * A portability function
178 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000179static int socket_errno(void) {
Owen Taylor3473f882001-02-23 17:55:21 +0000180#ifdef _WINSOCKAPI_
181 return(WSAGetLastError());
182#else
183 return(errno);
184#endif
185}
186
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000187#ifdef SUPPORT_IP6
Daniel Veillard2db8c122003-07-08 12:16:59 +0000188static
189int have_ipv6(void) {
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000190 int s;
191
192 s = socket (AF_INET6, SOCK_STREAM, 0);
193 if (s != -1) {
194 close (s);
195 return (1);
196 }
197 return (0);
198}
199#endif
200
Owen Taylor3473f882001-02-23 17:55:21 +0000201/**
202 * xmlNanoHTTPInit:
203 *
204 * Initialize the HTTP protocol layer.
205 * Currently it just checks for proxy informations
206 */
207
208void
209xmlNanoHTTPInit(void) {
210 const char *env;
211#ifdef _WINSOCKAPI_
212 WSADATA wsaData;
213#endif
214
215 if (initialized)
216 return;
217
218#ifdef _WINSOCKAPI_
219 if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0)
220 return;
221#endif
222
223 if (proxy == NULL) {
224 proxyPort = 80;
225 env = getenv("no_proxy");
Daniel Veillard29b17482004-08-16 00:39:03 +0000226 if (env && ((env[0] == '*') && (env[1] == 0)))
Owen Taylor3473f882001-02-23 17:55:21 +0000227 goto done;
228 env = getenv("http_proxy");
229 if (env != NULL) {
230 xmlNanoHTTPScanProxy(env);
231 goto done;
232 }
233 env = getenv("HTTP_PROXY");
234 if (env != NULL) {
235 xmlNanoHTTPScanProxy(env);
236 goto done;
237 }
238 }
239done:
240 initialized = 1;
241}
242
243/**
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000244 * xmlNanoHTTPCleanup:
Owen Taylor3473f882001-02-23 17:55:21 +0000245 *
246 * Cleanup the HTTP protocol layer.
247 */
248
249void
250xmlNanoHTTPCleanup(void) {
251 if (proxy != NULL)
252 xmlFree(proxy);
253#ifdef _WINSOCKAPI_
254 if (initialized)
255 WSACleanup();
256#endif
257 initialized = 0;
258 return;
259}
260
261/**
Owen Taylor3473f882001-02-23 17:55:21 +0000262 * xmlNanoHTTPScanURL:
263 * @ctxt: an HTTP context
264 * @URL: The URL used to initialize the context
265 *
266 * (Re)Initialize an HTTP context by parsing the URL and finding
267 * the protocol host port and path it indicates.
268 */
269
270static void
271xmlNanoHTTPScanURL(xmlNanoHTTPCtxtPtr ctxt, const char *URL) {
272 const char *cur = URL;
273 char buf[4096];
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000274 int indx = 0;
Igor Zlatkovic537769a2004-02-09 17:40:31 +0000275 const int indxMax = 4096 - 1;
Owen Taylor3473f882001-02-23 17:55:21 +0000276 int port = 0;
277
278 if (ctxt->protocol != NULL) {
279 xmlFree(ctxt->protocol);
280 ctxt->protocol = NULL;
281 }
282 if (ctxt->hostname != NULL) {
283 xmlFree(ctxt->hostname);
284 ctxt->hostname = NULL;
285 }
286 if (ctxt->path != NULL) {
287 xmlFree(ctxt->path);
288 ctxt->path = NULL;
289 }
290 if (URL == NULL) return;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000291 buf[indx] = 0;
Igor Zlatkovic537769a2004-02-09 17:40:31 +0000292 while ((*cur != 0) && (indx < indxMax)) {
Owen Taylor3473f882001-02-23 17:55:21 +0000293 if ((cur[0] == ':') && (cur[1] == '/') && (cur[2] == '/')) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000294 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000295 ctxt->protocol = xmlMemStrdup(buf);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000296 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000297 cur += 3;
298 break;
299 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000300 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000301 }
302 if (*cur == 0) return;
303
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000304 buf[indx] = 0;
Igor Zlatkovic537769a2004-02-09 17:40:31 +0000305 while (indx < indxMax) {
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000306 if ((strchr (cur, '[') && !strchr (cur, ']')) ||
307 (!strchr (cur, '[') && strchr (cur, ']'))) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000308 __xmlIOErr(XML_FROM_HTTP, XML_HTTP_URL_SYNTAX,
309 "Syntax Error\n");
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000310 return;
311 }
312
313 if (cur[0] == '[') {
314 cur++;
Igor Zlatkovic537769a2004-02-09 17:40:31 +0000315 while ((cur[0] != ']') && (indx < indxMax))
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000316 buf[indx++] = *cur++;
317
318 if (!strchr (buf, ':')) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000319 __xmlIOErr(XML_FROM_HTTP, XML_HTTP_USE_IP,
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000320 "Use [IPv6]/IPv4 format\n");
321 return;
322 }
323
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000324 buf[indx] = 0;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000325 ctxt->hostname = xmlMemStrdup (buf);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000326 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000327 cur += 1;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000328 if (cur[0] == ':') {
Owen Taylor3473f882001-02-23 17:55:21 +0000329 cur++;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000330 while (*cur >= '0' && *cur <= '9') {
331 port *= 10;
332 port += *cur - '0';
333 cur++;
334 }
335
336 if (port != 0) ctxt->port = port;
337 while ((cur[0] != '/') && (*cur != 0))
338 cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000339 }
Owen Taylor3473f882001-02-23 17:55:21 +0000340 break;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000341 }
342 else {
343 if (cur[0] == ':') {
344 buf[indx] = 0;
345 ctxt->hostname = xmlMemStrdup (buf);
346 indx = 0;
347 cur += 1;
348 while ((*cur >= '0') && (*cur <= '9')) {
349 port *= 10;
350 port += *cur - '0';
351 cur++;
352 }
353 if (port != 0) ctxt->port = port;
354 while ((cur[0] != '/') && (*cur != 0))
355 cur++;
356 break;
357 }
358 if ((*cur == '/') || (*cur == 0)) {
359 buf[indx] = 0;
360 ctxt->hostname = xmlMemStrdup (buf);
361 indx = 0;
362 break;
363 }
Owen Taylor3473f882001-02-23 17:55:21 +0000364 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000365 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000366 }
367 if (*cur == 0)
368 ctxt->path = xmlMemStrdup("/");
369 else {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000370 indx = 0;
371 buf[indx] = 0;
Igor Zlatkovic537769a2004-02-09 17:40:31 +0000372 while ((*cur != 0) && (indx < indxMax))
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000373 buf[indx++] = *cur++;
374 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000375 ctxt->path = xmlMemStrdup(buf);
376 }
377}
378
379/**
380 * xmlNanoHTTPScanProxy:
381 * @URL: The proxy URL used to initialize the proxy context
382 *
383 * (Re)Initialize the HTTP Proxy context by parsing the URL and finding
384 * the protocol host port it indicates.
385 * Should be like http://myproxy/ or http://myproxy:3128/
386 * A NULL URL cleans up proxy informations.
387 */
388
389void
390xmlNanoHTTPScanProxy(const char *URL) {
391 const char *cur = URL;
392 char buf[4096];
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000393 int indx = 0;
Igor Zlatkovic537769a2004-02-09 17:40:31 +0000394 const int indxMax = 4096 - 1;
Owen Taylor3473f882001-02-23 17:55:21 +0000395 int port = 0;
396
397 if (proxy != NULL) {
398 xmlFree(proxy);
399 proxy = NULL;
400 }
401 if (proxyPort != 0) {
402 proxyPort = 0;
403 }
404#ifdef DEBUG_HTTP
405 if (URL == NULL)
406 xmlGenericError(xmlGenericErrorContext,
407 "Removing HTTP proxy info\n");
408 else
409 xmlGenericError(xmlGenericErrorContext,
410 "Using HTTP proxy %s\n", URL);
411#endif
412 if (URL == NULL) return;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000413 buf[indx] = 0;
Igor Zlatkovic537769a2004-02-09 17:40:31 +0000414 while ((*cur != 0) && (indx < indxMax)) {
Owen Taylor3473f882001-02-23 17:55:21 +0000415 if ((cur[0] == ':') && (cur[1] == '/') && (cur[2] == '/')) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000416 buf[indx] = 0;
417 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000418 cur += 3;
419 break;
420 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000421 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000422 }
423 if (*cur == 0) return;
424
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000425 buf[indx] = 0;
Igor Zlatkovic537769a2004-02-09 17:40:31 +0000426 while (indx < indxMax) {
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000427 if ((strchr (cur, '[') && !strchr (cur, ']')) ||
428 (!strchr (cur, '[') && strchr (cur, ']'))) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000429 __xmlIOErr(XML_FROM_HTTP, XML_HTTP_URL_SYNTAX, "Syntax Error\n");
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000430 return;
431 }
432
433 if (cur[0] == '[') {
434 cur++;
Igor Zlatkovic537769a2004-02-09 17:40:31 +0000435 while ((cur[0] != ']') && (indx < indxMax))
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000436 buf[indx++] = *cur++;
437
438 if (!strchr (buf, ':')) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000439 __xmlIOErr(XML_FROM_HTTP, XML_HTTP_USE_IP,
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000440 "Use [IPv6]/IPv4 format\n");
441 return;
442 }
443
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000444 buf[indx] = 0;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000445 proxy = xmlMemStrdup (buf);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000446 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000447 cur += 1;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000448 if (cur[0] == ':') {
Owen Taylor3473f882001-02-23 17:55:21 +0000449 cur++;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000450 while (*cur >= '0' && *cur <= '9') {
451 port *= 10;
452 port += *cur - '0';
453 cur++;
454 }
455
456 if (port != 0) proxyPort = port;
457 while ((cur[0] != '/') && (*cur != 0))
458 cur ++;
459 }
Owen Taylor3473f882001-02-23 17:55:21 +0000460 break;
461 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000462 else {
463 if (cur[0] == ':') {
464 buf[indx] = 0;
465 proxy = xmlMemStrdup (buf);
466 indx = 0;
467 cur += 1;
468 while ((*cur >= '0') && (*cur <= '9')) {
469 port *= 10;
470 port += *cur - '0';
471 cur++;
472 }
473 if (port != 0) proxyPort = port;
474 while ((cur[0] != '/') && (*cur != 0))
475 cur++;
476 break;
477 }
478 if ((*cur == '/') || (*cur == 0)) {
479 buf[indx] = 0;
480 proxy = xmlMemStrdup (buf);
481 indx = 0;
482 break;
483 }
Owen Taylor3473f882001-02-23 17:55:21 +0000484 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000485 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000486 }
487}
488
489/**
490 * xmlNanoHTTPNewCtxt:
491 * @URL: The URL used to initialize the context
492 *
493 * Allocate and initialize a new HTTP context.
494 *
495 * Returns an HTTP context or NULL in case of error.
496 */
497
498static xmlNanoHTTPCtxtPtr
499xmlNanoHTTPNewCtxt(const char *URL) {
500 xmlNanoHTTPCtxtPtr ret;
501
502 ret = (xmlNanoHTTPCtxtPtr) xmlMalloc(sizeof(xmlNanoHTTPCtxt));
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000503 if (ret == NULL) {
504 xmlHTTPErrMemory("allocating context");
505 return(NULL);
506 }
Owen Taylor3473f882001-02-23 17:55:21 +0000507
508 memset(ret, 0, sizeof(xmlNanoHTTPCtxt));
509 ret->port = 80;
510 ret->returnValue = 0;
511 ret->fd = -1;
Daniel Veillardf012a642001-07-23 19:10:52 +0000512 ret->ContentLength = -1;
Owen Taylor3473f882001-02-23 17:55:21 +0000513
Daniel Veillardcacbe5d2003-01-10 16:09:51 +0000514 xmlNanoHTTPScanURL(ret, URL);
Owen Taylor3473f882001-02-23 17:55:21 +0000515
516 return(ret);
517}
518
519/**
520 * xmlNanoHTTPFreeCtxt:
521 * @ctxt: an HTTP context
522 *
523 * Frees the context after closing the connection.
524 */
525
526static void
527xmlNanoHTTPFreeCtxt(xmlNanoHTTPCtxtPtr ctxt) {
528 if (ctxt == NULL) return;
529 if (ctxt->hostname != NULL) xmlFree(ctxt->hostname);
530 if (ctxt->protocol != NULL) xmlFree(ctxt->protocol);
531 if (ctxt->path != NULL) xmlFree(ctxt->path);
532 if (ctxt->out != NULL) xmlFree(ctxt->out);
533 if (ctxt->in != NULL) xmlFree(ctxt->in);
534 if (ctxt->contentType != NULL) xmlFree(ctxt->contentType);
Daniel Veillard847332a2003-10-18 11:29:40 +0000535 if (ctxt->encoding != NULL) xmlFree(ctxt->encoding);
Daniel Veillarda840b692003-10-19 13:35:37 +0000536 if (ctxt->mimeType != NULL) xmlFree(ctxt->mimeType);
Owen Taylor3473f882001-02-23 17:55:21 +0000537 if (ctxt->location != NULL) xmlFree(ctxt->location);
538 if (ctxt->authHeader != NULL) xmlFree(ctxt->authHeader);
539 ctxt->state = XML_NANO_HTTP_NONE;
540 if (ctxt->fd >= 0) closesocket(ctxt->fd);
541 ctxt->fd = -1;
542 xmlFree(ctxt);
543}
544
545/**
546 * xmlNanoHTTPSend:
547 * @ctxt: an HTTP context
548 *
549 * Send the input needed to initiate the processing on the server side
Daniel Veillardf012a642001-07-23 19:10:52 +0000550 * Returns number of bytes sent or -1 on error.
Owen Taylor3473f882001-02-23 17:55:21 +0000551 */
552
Daniel Veillardf012a642001-07-23 19:10:52 +0000553static int
554xmlNanoHTTPSend(xmlNanoHTTPCtxtPtr ctxt, const char * xmt_ptr, int outlen) {
555
556 int total_sent = 0;
557
558 if ( (ctxt->state & XML_NANO_HTTP_WRITE) && (xmt_ptr != NULL ) ) {
559 while (total_sent < outlen) {
560 int nsent = send(ctxt->fd, xmt_ptr + total_sent,
561 outlen - total_sent, 0);
Owen Taylor3473f882001-02-23 17:55:21 +0000562 if (nsent>0)
563 total_sent += nsent;
Daniel Veillardf012a642001-07-23 19:10:52 +0000564 else if ( ( nsent == -1 ) &&
Daniel Veillardba6db032001-07-31 16:25:45 +0000565#if defined(EAGAIN) && EAGAIN != EWOULDBLOCK
Daniel Veillardf012a642001-07-23 19:10:52 +0000566 ( socket_errno( ) != EAGAIN ) &&
Daniel Veillardba6db032001-07-31 16:25:45 +0000567#endif
Daniel Veillardf012a642001-07-23 19:10:52 +0000568 ( socket_errno( ) != EWOULDBLOCK ) ) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000569 __xmlIOErr(XML_FROM_HTTP, 0, "send failed\n");
Daniel Veillardf012a642001-07-23 19:10:52 +0000570 if ( total_sent == 0 )
571 total_sent = -1;
572 break;
573 }
574 else {
575 /*
576 ** No data sent
577 ** Since non-blocking sockets are used, wait for
578 ** socket to be writable or default timeout prior
579 ** to retrying.
580 */
581
582 struct timeval tv;
583 fd_set wfd;
584
585 tv.tv_sec = timeout;
586 tv.tv_usec = 0;
587 FD_ZERO( &wfd );
588 FD_SET( ctxt->fd, &wfd );
589 (void)select( ctxt->fd + 1, NULL, &wfd, NULL, &tv );
590 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000591 }
Owen Taylor3473f882001-02-23 17:55:21 +0000592 }
Daniel Veillardf012a642001-07-23 19:10:52 +0000593
594 return total_sent;
Owen Taylor3473f882001-02-23 17:55:21 +0000595}
596
597/**
598 * xmlNanoHTTPRecv:
599 * @ctxt: an HTTP context
600 *
601 * Read information coming from the HTTP connection.
602 * This is a blocking call (but it blocks in select(), not read()).
603 *
604 * Returns the number of byte read or -1 in case of error.
605 */
606
607static int
608xmlNanoHTTPRecv(xmlNanoHTTPCtxtPtr ctxt) {
609 fd_set rfd;
610 struct timeval tv;
611
612
613 while (ctxt->state & XML_NANO_HTTP_READ) {
614 if (ctxt->in == NULL) {
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000615 ctxt->in = (char *) xmlMallocAtomic(65000 * sizeof(char));
Owen Taylor3473f882001-02-23 17:55:21 +0000616 if (ctxt->in == NULL) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000617 xmlHTTPErrMemory("allocating input");
Owen Taylor3473f882001-02-23 17:55:21 +0000618 ctxt->last = -1;
619 return(-1);
620 }
621 ctxt->inlen = 65000;
622 ctxt->inptr = ctxt->content = ctxt->inrptr = ctxt->in;
623 }
624 if (ctxt->inrptr > ctxt->in + XML_NANO_HTTP_CHUNK) {
625 int delta = ctxt->inrptr - ctxt->in;
626 int len = ctxt->inptr - ctxt->inrptr;
627
628 memmove(ctxt->in, ctxt->inrptr, len);
629 ctxt->inrptr -= delta;
630 ctxt->content -= delta;
631 ctxt->inptr -= delta;
632 }
633 if ((ctxt->in + ctxt->inlen) < (ctxt->inptr + XML_NANO_HTTP_CHUNK)) {
634 int d_inptr = ctxt->inptr - ctxt->in;
635 int d_content = ctxt->content - ctxt->in;
636 int d_inrptr = ctxt->inrptr - ctxt->in;
Daniel Veillardf012a642001-07-23 19:10:52 +0000637 char * tmp_ptr = ctxt->in;
Owen Taylor3473f882001-02-23 17:55:21 +0000638
639 ctxt->inlen *= 2;
Daniel Veillardf012a642001-07-23 19:10:52 +0000640 ctxt->in = (char *) xmlRealloc(tmp_ptr, ctxt->inlen);
Owen Taylor3473f882001-02-23 17:55:21 +0000641 if (ctxt->in == NULL) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000642 xmlHTTPErrMemory("allocating input buffer");
Daniel Veillardf012a642001-07-23 19:10:52 +0000643 xmlFree( tmp_ptr );
Owen Taylor3473f882001-02-23 17:55:21 +0000644 ctxt->last = -1;
645 return(-1);
646 }
647 ctxt->inptr = ctxt->in + d_inptr;
648 ctxt->content = ctxt->in + d_content;
649 ctxt->inrptr = ctxt->in + d_inrptr;
650 }
651 ctxt->last = recv(ctxt->fd, ctxt->inptr, XML_NANO_HTTP_CHUNK, 0);
652 if (ctxt->last > 0) {
653 ctxt->inptr += ctxt->last;
654 return(ctxt->last);
655 }
656 if (ctxt->last == 0) {
657 return(0);
658 }
659 if (ctxt->last == -1) {
660 switch (socket_errno()) {
661 case EINPROGRESS:
662 case EWOULDBLOCK:
663#if defined(EAGAIN) && EAGAIN != EWOULDBLOCK
664 case EAGAIN:
665#endif
666 break;
Daniel Veillardf012a642001-07-23 19:10:52 +0000667
668 case ECONNRESET:
669 case ESHUTDOWN:
670 return ( 0 );
671
Owen Taylor3473f882001-02-23 17:55:21 +0000672 default:
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000673 __xmlIOErr(XML_FROM_HTTP, 0, "recv failed\n");
Daniel Veillardf012a642001-07-23 19:10:52 +0000674 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +0000675 }
676 }
677
678 tv.tv_sec = timeout;
679 tv.tv_usec = 0;
680 FD_ZERO(&rfd);
681 FD_SET(ctxt->fd, &rfd);
682
Daniel Veillard50f34372001-08-03 12:06:36 +0000683 if ( (select(ctxt->fd+1, &rfd, NULL, NULL, &tv)<1)
684#if defined(EINTR)
685 && (errno != EINTR)
686#endif
687 )
Owen Taylor3473f882001-02-23 17:55:21 +0000688 return(0);
689 }
690 return(0);
691}
692
693/**
694 * xmlNanoHTTPReadLine:
695 * @ctxt: an HTTP context
696 *
697 * Read one line in the HTTP server output, usually for extracting
698 * the HTTP protocol informations from the answer header.
699 *
700 * Returns a newly allocated string with a copy of the line, or NULL
701 * which indicate the end of the input.
702 */
703
704static char *
705xmlNanoHTTPReadLine(xmlNanoHTTPCtxtPtr ctxt) {
706 char buf[4096];
707 char *bp = buf;
Daniel Veillardf012a642001-07-23 19:10:52 +0000708 int rc;
Owen Taylor3473f882001-02-23 17:55:21 +0000709
710 while (bp - buf < 4095) {
711 if (ctxt->inrptr == ctxt->inptr) {
Daniel Veillardf012a642001-07-23 19:10:52 +0000712 if ( (rc = xmlNanoHTTPRecv(ctxt)) == 0) {
Owen Taylor3473f882001-02-23 17:55:21 +0000713 if (bp == buf)
714 return(NULL);
715 else
716 *bp = 0;
717 return(xmlMemStrdup(buf));
718 }
Daniel Veillardf012a642001-07-23 19:10:52 +0000719 else if ( rc == -1 ) {
720 return ( NULL );
721 }
Owen Taylor3473f882001-02-23 17:55:21 +0000722 }
723 *bp = *ctxt->inrptr++;
724 if (*bp == '\n') {
725 *bp = 0;
726 return(xmlMemStrdup(buf));
727 }
728 if (*bp != '\r')
729 bp++;
730 }
731 buf[4095] = 0;
732 return(xmlMemStrdup(buf));
733}
734
735
736/**
737 * xmlNanoHTTPScanAnswer:
738 * @ctxt: an HTTP context
739 * @line: an HTTP header line
740 *
741 * Try to extract useful informations from the server answer.
742 * We currently parse and process:
743 * - The HTTP revision/ return code
Daniel Veillarda840b692003-10-19 13:35:37 +0000744 * - The Content-Type, Mime-Type and charset used
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000745 * - The Location for redirect processing.
Owen Taylor3473f882001-02-23 17:55:21 +0000746 *
747 * Returns -1 in case of failure, the file descriptor number otherwise
748 */
749
750static void
751xmlNanoHTTPScanAnswer(xmlNanoHTTPCtxtPtr ctxt, const char *line) {
752 const char *cur = line;
753
754 if (line == NULL) return;
755
756 if (!strncmp(line, "HTTP/", 5)) {
757 int version = 0;
758 int ret = 0;
759
760 cur += 5;
761 while ((*cur >= '0') && (*cur <= '9')) {
762 version *= 10;
763 version += *cur - '0';
764 cur++;
765 }
766 if (*cur == '.') {
767 cur++;
768 if ((*cur >= '0') && (*cur <= '9')) {
769 version *= 10;
770 version += *cur - '0';
771 cur++;
772 }
773 while ((*cur >= '0') && (*cur <= '9'))
774 cur++;
775 } else
776 version *= 10;
777 if ((*cur != ' ') && (*cur != '\t')) return;
778 while ((*cur == ' ') || (*cur == '\t')) cur++;
779 if ((*cur < '0') || (*cur > '9')) return;
780 while ((*cur >= '0') && (*cur <= '9')) {
781 ret *= 10;
782 ret += *cur - '0';
783 cur++;
784 }
785 if ((*cur != 0) && (*cur != ' ') && (*cur != '\t')) return;
786 ctxt->returnValue = ret;
787 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Content-Type:", 13)) {
Daniel Veillarda840b692003-10-19 13:35:37 +0000788 const xmlChar *charset, *last, *mime;
Owen Taylor3473f882001-02-23 17:55:21 +0000789 cur += 13;
790 while ((*cur == ' ') || (*cur == '\t')) cur++;
791 if (ctxt->contentType != NULL)
792 xmlFree(ctxt->contentType);
793 ctxt->contentType = xmlMemStrdup(cur);
Daniel Veillarda840b692003-10-19 13:35:37 +0000794 mime = (const xmlChar *) cur;
795 last = mime;
796 while ((*last != 0) && (*last != ' ') && (*last != '\t') &&
797 (*last != ';') && (*last != ','))
798 last++;
799 if (ctxt->mimeType != NULL)
800 xmlFree(ctxt->mimeType);
801 ctxt->mimeType = (char *) xmlStrndup(mime, last - mime);
802 charset = xmlStrstr(BAD_CAST ctxt->contentType, BAD_CAST "charset=");
803 if (charset != NULL) {
804 charset += 8;
805 last = charset;
806 while ((*last != 0) && (*last != ' ') && (*last != '\t') &&
807 (*last != ';') && (*last != ','))
808 last++;
809 if (ctxt->encoding != NULL)
810 xmlFree(ctxt->encoding);
811 ctxt->encoding = (char *) xmlStrndup(charset, last - charset);
812 }
Owen Taylor3473f882001-02-23 17:55:21 +0000813 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"ContentType:", 12)) {
Daniel Veillarda840b692003-10-19 13:35:37 +0000814 const xmlChar *charset, *last, *mime;
Owen Taylor3473f882001-02-23 17:55:21 +0000815 cur += 12;
816 if (ctxt->contentType != NULL) return;
817 while ((*cur == ' ') || (*cur == '\t')) cur++;
818 ctxt->contentType = xmlMemStrdup(cur);
Daniel Veillarda840b692003-10-19 13:35:37 +0000819 mime = (const xmlChar *) cur;
820 last = mime;
821 while ((*last != 0) && (*last != ' ') && (*last != '\t') &&
822 (*last != ';') && (*last != ','))
823 last++;
824 if (ctxt->mimeType != NULL)
825 xmlFree(ctxt->mimeType);
826 ctxt->mimeType = (char *) xmlStrndup(mime, last - mime);
827 charset = xmlStrstr(BAD_CAST ctxt->contentType, BAD_CAST "charset=");
828 if (charset != NULL) {
829 charset += 8;
830 last = charset;
831 while ((*last != 0) && (*last != ' ') && (*last != '\t') &&
832 (*last != ';') && (*last != ','))
833 last++;
834 if (ctxt->encoding != NULL)
835 xmlFree(ctxt->encoding);
836 ctxt->encoding = (char *) xmlStrndup(charset, last - charset);
837 }
Owen Taylor3473f882001-02-23 17:55:21 +0000838 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Location:", 9)) {
839 cur += 9;
840 while ((*cur == ' ') || (*cur == '\t')) cur++;
841 if (ctxt->location != NULL)
842 xmlFree(ctxt->location);
William M. Brack7e29c0a2004-04-02 09:07:22 +0000843 if (*cur == '/') {
844 xmlChar *tmp_http = xmlStrdup(BAD_CAST "http://");
845 xmlChar *tmp_loc =
846 xmlStrcat(tmp_http, (const xmlChar *) ctxt->hostname);
847 ctxt->location =
848 (char *) xmlStrcat (tmp_loc, (const xmlChar *) cur);
849 } else {
850 ctxt->location = xmlMemStrdup(cur);
851 }
Owen Taylor3473f882001-02-23 17:55:21 +0000852 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"WWW-Authenticate:", 17)) {
853 cur += 17;
854 while ((*cur == ' ') || (*cur == '\t')) cur++;
855 if (ctxt->authHeader != NULL)
856 xmlFree(ctxt->authHeader);
857 ctxt->authHeader = xmlMemStrdup(cur);
858 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Proxy-Authenticate:", 19)) {
859 cur += 19;
860 while ((*cur == ' ') || (*cur == '\t')) cur++;
861 if (ctxt->authHeader != NULL)
862 xmlFree(ctxt->authHeader);
863 ctxt->authHeader = xmlMemStrdup(cur);
Daniel Veillardf012a642001-07-23 19:10:52 +0000864 } else if ( !xmlStrncasecmp( BAD_CAST line, BAD_CAST"Content-Length:", 15) ) {
865 cur += 15;
866 ctxt->ContentLength = strtol( cur, NULL, 10 );
Owen Taylor3473f882001-02-23 17:55:21 +0000867 }
868}
869
870/**
871 * xmlNanoHTTPConnectAttempt:
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000872 * @addr: a socket address structure
Owen Taylor3473f882001-02-23 17:55:21 +0000873 *
874 * Attempt a connection to the given IP:port endpoint. It forces
875 * non-blocking semantic on the socket, and allow 60 seconds for
876 * the host to answer.
877 *
878 * Returns -1 in case of failure, the file descriptor number otherwise
879 */
880
881static int
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000882xmlNanoHTTPConnectAttempt(struct sockaddr *addr)
Owen Taylor3473f882001-02-23 17:55:21 +0000883{
Owen Taylor3473f882001-02-23 17:55:21 +0000884 fd_set wfd;
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000885#ifdef _WINSOCKAPI_
886 fd_set xfd;
887#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000888 struct timeval tv;
889 int status;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000890 int addrlen;
891 SOCKET s;
Owen Taylor3473f882001-02-23 17:55:21 +0000892
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000893#ifdef SUPPORT_IP6
894 if (addr->sa_family == AF_INET6) {
895 s = socket (PF_INET6, SOCK_STREAM, IPPROTO_TCP);
896 addrlen = sizeof (struct sockaddr_in6);
897 }
898 else
899#endif
900 {
901 s = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
902 addrlen = sizeof (struct sockaddr_in);
903 }
Owen Taylor3473f882001-02-23 17:55:21 +0000904 if (s==-1) {
905#ifdef DEBUG_HTTP
906 perror("socket");
907#endif
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000908 __xmlIOErr(XML_FROM_HTTP, 0, "socket failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000909 return(-1);
910 }
911
912#ifdef _WINSOCKAPI_
913 {
914 u_long one = 1;
915
916 status = ioctlsocket(s, FIONBIO, &one) == SOCKET_ERROR ? -1 : 0;
917 }
918#else /* _WINSOCKAPI_ */
919#if defined(VMS)
920 {
921 int enable = 1;
922 status = ioctl(s, FIONBIO, &enable);
923 }
924#else /* VMS */
Daniel Veillard254b1262003-11-01 17:04:58 +0000925#if defined(__BEOS__)
926 {
927 bool noblock = true;
928 status = setsockopt(s, SOL_SOCKET, SO_NONBLOCK, &noblock, sizeof(noblock));
929 }
930#else /* __BEOS__ */
Owen Taylor3473f882001-02-23 17:55:21 +0000931 if ((status = fcntl(s, F_GETFL, 0)) != -1) {
932#ifdef O_NONBLOCK
933 status |= O_NONBLOCK;
934#else /* O_NONBLOCK */
935#ifdef F_NDELAY
936 status |= F_NDELAY;
937#endif /* F_NDELAY */
938#endif /* !O_NONBLOCK */
939 status = fcntl(s, F_SETFL, status);
940 }
941 if (status < 0) {
942#ifdef DEBUG_HTTP
943 perror("nonblocking");
944#endif
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000945 __xmlIOErr(XML_FROM_HTTP, 0, "error setting non-blocking IO\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000946 closesocket(s);
947 return(-1);
948 }
Daniel Veillard254b1262003-11-01 17:04:58 +0000949#endif /* !__BEOS__ */
Owen Taylor3473f882001-02-23 17:55:21 +0000950#endif /* !VMS */
951#endif /* !_WINSOCKAPI_ */
952
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000953 if (connect (s, addr, addrlen) == -1) {
Owen Taylor3473f882001-02-23 17:55:21 +0000954 switch (socket_errno()) {
955 case EINPROGRESS:
956 case EWOULDBLOCK:
957 break;
958 default:
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000959 __xmlIOErr(XML_FROM_HTTP, 0, "error connecting to HTTP server");
Owen Taylor3473f882001-02-23 17:55:21 +0000960 closesocket(s);
961 return(-1);
962 }
963 }
964
965 tv.tv_sec = timeout;
966 tv.tv_usec = 0;
967
968 FD_ZERO(&wfd);
969 FD_SET(s, &wfd);
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000970
971#ifdef _WINSOCKAPI_
972 FD_ZERO(&xfd);
973 FD_SET(s, &xfd);
Owen Taylor3473f882001-02-23 17:55:21 +0000974
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000975 switch(select(s+1, NULL, &wfd, &xfd, &tv))
976#else
Owen Taylor3473f882001-02-23 17:55:21 +0000977 switch(select(s+1, NULL, &wfd, NULL, &tv))
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000978#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000979 {
980 case 0:
981 /* Time out */
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000982 __xmlIOErr(XML_FROM_HTTP, 0, "Connect attempt timed out");
Owen Taylor3473f882001-02-23 17:55:21 +0000983 closesocket(s);
984 return(-1);
985 case -1:
986 /* Ermm.. ?? */
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000987 __xmlIOErr(XML_FROM_HTTP, 0, "Connect failed");
Owen Taylor3473f882001-02-23 17:55:21 +0000988 closesocket(s);
989 return(-1);
990 }
991
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000992 if ( FD_ISSET(s, &wfd)
993#ifdef _WINSOCKAPI_
994 || FD_ISSET(s, &xfd)
995#endif
996 ) {
Owen Taylor3473f882001-02-23 17:55:21 +0000997 SOCKLEN_T len;
998 len = sizeof(status);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000999#ifdef SO_ERROR
Owen Taylor3473f882001-02-23 17:55:21 +00001000 if (getsockopt(s, SOL_SOCKET, SO_ERROR, (char*)&status, &len) < 0 ) {
1001 /* Solaris error code */
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001002 __xmlIOErr(XML_FROM_HTTP, 0, "getsockopt failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001003 return (-1);
1004 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001005#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001006 if ( status ) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001007 __xmlIOErr(XML_FROM_HTTP, 0, "Error connecting to remote host");
Owen Taylor3473f882001-02-23 17:55:21 +00001008 closesocket(s);
1009 errno = status;
1010 return (-1);
1011 }
1012 } else {
1013 /* pbm */
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001014 __xmlIOErr(XML_FROM_HTTP, 0, "select failed\n");
Daniel Veillardf012a642001-07-23 19:10:52 +00001015 closesocket(s);
Owen Taylor3473f882001-02-23 17:55:21 +00001016 return (-1);
1017 }
1018
1019 return(s);
1020}
1021
1022/**
1023 * xmlNanoHTTPConnectHost:
1024 * @host: the host name
1025 * @port: the port number
1026 *
1027 * Attempt a connection to the given host:port endpoint. It tries
1028 * the multiple IP provided by the DNS if available.
1029 *
1030 * Returns -1 in case of failure, the file descriptor number otherwise
1031 */
1032
1033static int
1034xmlNanoHTTPConnectHost(const char *host, int port)
1035{
1036 struct hostent *h;
Daniel Veillard2db8c122003-07-08 12:16:59 +00001037 struct sockaddr *addr = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001038 struct in_addr ia;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001039 struct sockaddr_in sockin;
Daniel Veillard5c396542002-03-15 07:57:50 +00001040
Owen Taylor3473f882001-02-23 17:55:21 +00001041#ifdef SUPPORT_IP6
1042 struct in6_addr ia6;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001043 struct sockaddr_in6 sockin6;
Owen Taylor3473f882001-02-23 17:55:21 +00001044#endif
1045 int i;
1046 int s;
Daniel Veillard5c396542002-03-15 07:57:50 +00001047
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001048 memset (&sockin, 0, sizeof(sockin));
1049#ifdef SUPPORT_IP6
1050 memset (&sockin6, 0, sizeof(sockin6));
1051 if (have_ipv6 ())
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001052#if !defined(HAVE_GETADDRINFO) && defined(RES_USE_INET6)
Daniel Veillard560c2a42003-07-06 21:13:49 +00001053 {
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001054 if (!(_res.options & RES_INIT))
1055 res_init();
1056 _res.options |= RES_USE_INET6;
1057 }
1058#elif defined(HAVE_GETADDRINFO)
Daniel Veillard560c2a42003-07-06 21:13:49 +00001059 {
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001060 int status;
1061 struct addrinfo hints, *res, *result;
1062
1063 result = NULL;
1064 memset (&hints, 0,sizeof(hints));
1065 hints.ai_socktype = SOCK_STREAM;
1066
1067 status = getaddrinfo (host, NULL, &hints, &result);
1068 if (status) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001069 __xmlIOErr(XML_FROM_HTTP, 0, "getaddrinfo failed\n");
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001070 return (-1);
1071 }
1072
1073 for (res = result; res; res = res->ai_next) {
Daniel Veillard3dc93a42003-07-10 14:04:33 +00001074 if (res->ai_family == AF_INET || res->ai_family == AF_INET6) {
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001075 if (res->ai_family == AF_INET6) {
1076 memcpy (&sockin6, res->ai_addr, res->ai_addrlen);
1077 sockin6.sin6_port = htons (port);
1078 addr = (struct sockaddr *)&sockin6;
1079 }
Daniel Veillard3dc93a42003-07-10 14:04:33 +00001080 else {
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001081 memcpy (&sockin, res->ai_addr, res->ai_addrlen);
1082 sockin.sin_port = htons (port);
1083 addr = (struct sockaddr *)&sockin;
1084 }
1085
1086 s = xmlNanoHTTPConnectAttempt (addr);
1087 if (s != -1) {
1088 freeaddrinfo (result);
1089 return (s);
1090 }
1091 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001092 }
Daniel Veillard3dc93a42003-07-10 14:04:33 +00001093 if (result)
1094 freeaddrinfo (result);
1095 return (-1);
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001096 } else
Owen Taylor3473f882001-02-23 17:55:21 +00001097#endif
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001098#endif
1099 {
1100 h = gethostbyname (host);
1101 if (h == NULL) {
Daniel Veillard56b2db72002-03-25 16:35:28 +00001102
1103/*
1104 * Okay, I got fed up by the non-portability of this error message
1105 * extraction code. it work on Linux, if it work on your platform
1106 * and one want to enable it, send me the defined(foobar) needed
1107 */
1108#if defined(HAVE_NETDB_H) && defined(HOST_NOT_FOUND) && defined(linux)
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001109 const char *h_err_txt = "";
Daniel Veillardf012a642001-07-23 19:10:52 +00001110
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001111 switch (h_errno) {
1112 case HOST_NOT_FOUND:
1113 h_err_txt = "Authoritive host not found";
1114 break;
Daniel Veillardf012a642001-07-23 19:10:52 +00001115
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001116 case TRY_AGAIN:
1117 h_err_txt =
1118 "Non-authoritive host not found or server failure.";
1119 break;
Daniel Veillardf012a642001-07-23 19:10:52 +00001120
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001121 case NO_RECOVERY:
1122 h_err_txt =
1123 "Non-recoverable errors: FORMERR, REFUSED, or NOTIMP.";
1124 break;
Daniel Veillard5c396542002-03-15 07:57:50 +00001125
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001126 case NO_ADDRESS:
1127 h_err_txt =
1128 "Valid name, no data record of requested type.";
1129 break;
Daniel Veillard5c396542002-03-15 07:57:50 +00001130
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001131 default:
1132 h_err_txt = "No error text defined.";
1133 break;
1134 }
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001135 __xmlIOErr(XML_FROM_HTTP, 0, h_err_txt);
Daniel Veillard5c396542002-03-15 07:57:50 +00001136#else
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001137 __xmlIOErr(XML_FROM_HTTP, 0, "Failed to resolve host");
Owen Taylor3473f882001-02-23 17:55:21 +00001138#endif
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001139 return (-1);
1140 }
Daniel Veillard5c396542002-03-15 07:57:50 +00001141
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001142 for (i = 0; h->h_addr_list[i]; i++) {
1143 if (h->h_addrtype == AF_INET) {
1144 /* A records (IPv4) */
1145 memcpy (&ia, h->h_addr_list[i], h->h_length);
1146 sockin.sin_family = h->h_addrtype;
1147 sockin.sin_addr = ia;
1148 sockin.sin_port = htons (port);
1149 addr = (struct sockaddr *) &sockin;
Daniel Veillard5c396542002-03-15 07:57:50 +00001150#ifdef SUPPORT_IP6
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001151 } else if (have_ipv6 () && (h->h_addrtype == AF_INET6)) {
1152 /* AAAA records (IPv6) */
1153 memcpy (&ia6, h->h_addr_list[i], h->h_length);
1154 sockin6.sin6_family = h->h_addrtype;
1155 sockin6.sin6_addr = ia6;
1156 sockin6.sin6_port = htons (port);
1157 addr = (struct sockaddr *) &sockin6;
Daniel Veillard5c396542002-03-15 07:57:50 +00001158#endif
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001159 } else
1160 break; /* for */
Daniel Veillard5c396542002-03-15 07:57:50 +00001161
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001162 s = xmlNanoHTTPConnectAttempt (addr);
1163 if (s != -1)
1164 return (s);
1165 }
Owen Taylor3473f882001-02-23 17:55:21 +00001166 }
Owen Taylor3473f882001-02-23 17:55:21 +00001167#ifdef DEBUG_HTTP
1168 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard5c396542002-03-15 07:57:50 +00001169 "xmlNanoHTTPConnectHost: unable to connect to '%s'.\n",
1170 host);
Owen Taylor3473f882001-02-23 17:55:21 +00001171#endif
Daniel Veillard5c396542002-03-15 07:57:50 +00001172 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001173}
1174
1175
1176/**
1177 * xmlNanoHTTPOpen:
1178 * @URL: The URL to load
1179 * @contentType: if available the Content-Type information will be
1180 * returned at that location
1181 *
1182 * This function try to open a connection to the indicated resource
1183 * via HTTP GET.
1184 *
1185 * Returns NULL in case of failure, otherwise a request handler.
1186 * The contentType, if provided must be freed by the caller
1187 */
1188
1189void*
1190xmlNanoHTTPOpen(const char *URL, char **contentType) {
1191 if (contentType != NULL) *contentType = NULL;
Daniel Veillardf012a642001-07-23 19:10:52 +00001192 return(xmlNanoHTTPMethod(URL, NULL, NULL, contentType, NULL, 0));
Daniel Veillard9403a042001-05-28 11:00:53 +00001193}
1194
1195/**
1196 * xmlNanoHTTPOpenRedir:
1197 * @URL: The URL to load
1198 * @contentType: if available the Content-Type information will be
1199 * returned at that location
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001200 * @redir: if available the redirected URL will be returned
Daniel Veillard9403a042001-05-28 11:00:53 +00001201 *
1202 * This function try to open a connection to the indicated resource
1203 * via HTTP GET.
1204 *
1205 * Returns NULL in case of failure, otherwise a request handler.
1206 * The contentType, if provided must be freed by the caller
1207 */
1208
1209void*
1210xmlNanoHTTPOpenRedir(const char *URL, char **contentType, char **redir) {
1211 if (contentType != NULL) *contentType = NULL;
1212 if (redir != NULL) *redir = NULL;
Daniel Veillardf012a642001-07-23 19:10:52 +00001213 return(xmlNanoHTTPMethodRedir(URL, NULL, NULL, contentType, redir, NULL,0));
Owen Taylor3473f882001-02-23 17:55:21 +00001214}
1215
1216/**
1217 * xmlNanoHTTPRead:
1218 * @ctx: the HTTP context
1219 * @dest: a buffer
1220 * @len: the buffer length
1221 *
1222 * This function tries to read @len bytes from the existing HTTP connection
1223 * and saves them in @dest. This is a blocking call.
1224 *
1225 * Returns the number of byte read. 0 is an indication of an end of connection.
1226 * -1 indicates a parameter error.
1227 */
1228int
1229xmlNanoHTTPRead(void *ctx, void *dest, int len) {
1230 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1231
1232 if (ctx == NULL) return(-1);
1233 if (dest == NULL) return(-1);
1234 if (len <= 0) return(0);
1235
1236 while (ctxt->inptr - ctxt->inrptr < len) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001237 if (xmlNanoHTTPRecv(ctxt) <= 0) break;
Owen Taylor3473f882001-02-23 17:55:21 +00001238 }
1239 if (ctxt->inptr - ctxt->inrptr < len)
1240 len = ctxt->inptr - ctxt->inrptr;
1241 memcpy(dest, ctxt->inrptr, len);
1242 ctxt->inrptr += len;
1243 return(len);
1244}
1245
1246/**
1247 * xmlNanoHTTPClose:
1248 * @ctx: the HTTP context
1249 *
1250 * This function closes an HTTP context, it ends up the connection and
1251 * free all data related to it.
1252 */
1253void
1254xmlNanoHTTPClose(void *ctx) {
1255 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1256
1257 if (ctx == NULL) return;
1258
1259 xmlNanoHTTPFreeCtxt(ctxt);
1260}
1261
1262/**
Daniel Veillard9403a042001-05-28 11:00:53 +00001263 * xmlNanoHTTPMethodRedir:
Owen Taylor3473f882001-02-23 17:55:21 +00001264 * @URL: The URL to load
1265 * @method: the HTTP method to use
1266 * @input: the input string if any
1267 * @contentType: the Content-Type information IN and OUT
Daniel Veillard9403a042001-05-28 11:00:53 +00001268 * @redir: the redirected URL OUT
Owen Taylor3473f882001-02-23 17:55:21 +00001269 * @headers: the extra headers
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001270 * @ilen: input length
Owen Taylor3473f882001-02-23 17:55:21 +00001271 *
1272 * This function try to open a connection to the indicated resource
1273 * via HTTP using the given @method, adding the given extra headers
1274 * and the input buffer for the request content.
1275 *
1276 * Returns NULL in case of failure, otherwise a request handler.
Daniel Veillard9403a042001-05-28 11:00:53 +00001277 * The contentType, or redir, if provided must be freed by the caller
Owen Taylor3473f882001-02-23 17:55:21 +00001278 */
1279
1280void*
Daniel Veillard9403a042001-05-28 11:00:53 +00001281xmlNanoHTTPMethodRedir(const char *URL, const char *method, const char *input,
Daniel Veillardf012a642001-07-23 19:10:52 +00001282 char **contentType, char **redir,
1283 const char *headers, int ilen ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001284 xmlNanoHTTPCtxtPtr ctxt;
1285 char *bp, *p;
Daniel Veillardf012a642001-07-23 19:10:52 +00001286 int blen, ret;
Owen Taylor3473f882001-02-23 17:55:21 +00001287 int head;
1288 int nbRedirects = 0;
1289 char *redirURL = NULL;
William M. Brack78637da2003-07-31 14:47:38 +00001290#ifdef DEBUG_HTTP
1291 int xmt_bytes;
1292#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001293
1294 if (URL == NULL) return(NULL);
1295 if (method == NULL) method = "GET";
1296 xmlNanoHTTPInit();
1297
1298retry:
1299 if (redirURL == NULL)
1300 ctxt = xmlNanoHTTPNewCtxt(URL);
1301 else {
1302 ctxt = xmlNanoHTTPNewCtxt(redirURL);
Daniel Veillarda840b692003-10-19 13:35:37 +00001303 ctxt->location = xmlMemStrdup(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001304 }
1305
Daniel Veillardf012a642001-07-23 19:10:52 +00001306 if ( ctxt == NULL ) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001307 return ( NULL );
1308 }
1309
Owen Taylor3473f882001-02-23 17:55:21 +00001310 if ((ctxt->protocol == NULL) || (strcmp(ctxt->protocol, "http"))) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001311 __xmlIOErr(XML_FROM_HTTP, XML_HTTP_URL_SYNTAX, "Not a valid HTTP URI");
Owen Taylor3473f882001-02-23 17:55:21 +00001312 xmlNanoHTTPFreeCtxt(ctxt);
1313 if (redirURL != NULL) xmlFree(redirURL);
1314 return(NULL);
1315 }
1316 if (ctxt->hostname == NULL) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001317 __xmlIOErr(XML_FROM_HTTP, XML_HTTP_UNKNOWN_HOST,
1318 "Failed to identify host in URI");
Owen Taylor3473f882001-02-23 17:55:21 +00001319 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001320 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001321 return(NULL);
1322 }
1323 if (proxy) {
1324 blen = strlen(ctxt->hostname) * 2 + 16;
1325 ret = xmlNanoHTTPConnectHost(proxy, proxyPort);
1326 }
1327 else {
1328 blen = strlen(ctxt->hostname);
1329 ret = xmlNanoHTTPConnectHost(ctxt->hostname, ctxt->port);
1330 }
1331 if (ret < 0) {
1332 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001333 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001334 return(NULL);
1335 }
1336 ctxt->fd = ret;
1337
Daniel Veillardf012a642001-07-23 19:10:52 +00001338 if (input == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00001339 ilen = 0;
Daniel Veillardf012a642001-07-23 19:10:52 +00001340 else
1341 blen += 36;
1342
Owen Taylor3473f882001-02-23 17:55:21 +00001343 if (headers != NULL)
Daniel Veillardf012a642001-07-23 19:10:52 +00001344 blen += strlen(headers) + 2;
Owen Taylor3473f882001-02-23 17:55:21 +00001345 if (contentType && *contentType)
1346 blen += strlen(*contentType) + 16;
Daniel Veillardf012a642001-07-23 19:10:52 +00001347 blen += strlen(method) + strlen(ctxt->path) + 24;
Daniel Veillard82cb3192003-10-29 13:39:15 +00001348 bp = (char*)xmlMallocAtomic(blen);
Daniel Veillardf012a642001-07-23 19:10:52 +00001349 if ( bp == NULL ) {
1350 xmlNanoHTTPFreeCtxt( ctxt );
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001351 xmlHTTPErrMemory("allocating header buffer");
Daniel Veillardf012a642001-07-23 19:10:52 +00001352 return ( NULL );
1353 }
1354
1355 p = bp;
1356
Owen Taylor3473f882001-02-23 17:55:21 +00001357 if (proxy) {
1358 if (ctxt->port != 80) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001359 p += snprintf( p, blen - (p - bp), "%s http://%s:%d%s",
1360 method, ctxt->hostname,
Daniel Veillardf012a642001-07-23 19:10:52 +00001361 ctxt->port, ctxt->path );
Owen Taylor3473f882001-02-23 17:55:21 +00001362 }
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001363 else
1364 p += snprintf( p, blen - (p - bp), "%s http://%s%s", method,
Daniel Veillardf012a642001-07-23 19:10:52 +00001365 ctxt->hostname, ctxt->path);
Owen Taylor3473f882001-02-23 17:55:21 +00001366 }
1367 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001368 p += snprintf( p, blen - (p - bp), "%s %s", method, ctxt->path);
Daniel Veillardf012a642001-07-23 19:10:52 +00001369
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001370 p += snprintf( p, blen - (p - bp), " HTTP/1.0\r\nHost: %s\r\n",
1371 ctxt->hostname);
Daniel Veillardf012a642001-07-23 19:10:52 +00001372
1373 if (contentType != NULL && *contentType)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001374 p += snprintf(p, blen - (p - bp), "Content-Type: %s\r\n", *contentType);
Daniel Veillardf012a642001-07-23 19:10:52 +00001375
1376 if (headers != NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001377 p += snprintf( p, blen - (p - bp), "%s", headers );
Daniel Veillardf012a642001-07-23 19:10:52 +00001378
Owen Taylor3473f882001-02-23 17:55:21 +00001379 if (input != NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001380 snprintf(p, blen - (p - bp), "Content-Length: %d\r\n\r\n", ilen );
Owen Taylor3473f882001-02-23 17:55:21 +00001381 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001382 snprintf(p, blen - (p - bp), "\r\n");
Daniel Veillardf012a642001-07-23 19:10:52 +00001383
Owen Taylor3473f882001-02-23 17:55:21 +00001384#ifdef DEBUG_HTTP
1385 xmlGenericError(xmlGenericErrorContext,
1386 "-> %s%s", proxy? "(Proxy) " : "", bp);
1387 if ((blen -= strlen(bp)+1) < 0)
1388 xmlGenericError(xmlGenericErrorContext,
1389 "ERROR: overflowed buffer by %d bytes\n", -blen);
1390#endif
1391 ctxt->outptr = ctxt->out = bp;
1392 ctxt->state = XML_NANO_HTTP_WRITE;
Daniel Veillardf012a642001-07-23 19:10:52 +00001393 blen = strlen( ctxt->out );
Daniel Veillardf012a642001-07-23 19:10:52 +00001394#ifdef DEBUG_HTTP
William M. Brack78637da2003-07-31 14:47:38 +00001395 xmt_bytes = xmlNanoHTTPSend(ctxt, ctxt->out, blen );
Daniel Veillardf012a642001-07-23 19:10:52 +00001396 if ( xmt_bytes != blen )
1397 xmlGenericError( xmlGenericErrorContext,
1398 "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
1399 xmt_bytes, blen,
1400 "bytes of HTTP headers sent to host",
1401 ctxt->hostname );
William M. Brack78637da2003-07-31 14:47:38 +00001402#else
1403 xmlNanoHTTPSend(ctxt, ctxt->out, blen );
Daniel Veillardf012a642001-07-23 19:10:52 +00001404#endif
1405
1406 if ( input != NULL ) {
William M. Brack78637da2003-07-31 14:47:38 +00001407#ifdef DEBUG_HTTP
Daniel Veillardf012a642001-07-23 19:10:52 +00001408 xmt_bytes = xmlNanoHTTPSend( ctxt, input, ilen );
1409
Daniel Veillardf012a642001-07-23 19:10:52 +00001410 if ( xmt_bytes != ilen )
1411 xmlGenericError( xmlGenericErrorContext,
1412 "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
1413 xmt_bytes, ilen,
1414 "bytes of HTTP content sent to host",
1415 ctxt->hostname );
William M. Brack78637da2003-07-31 14:47:38 +00001416#else
1417 xmlNanoHTTPSend( ctxt, input, ilen );
Daniel Veillardf012a642001-07-23 19:10:52 +00001418#endif
1419 }
1420
Owen Taylor3473f882001-02-23 17:55:21 +00001421 ctxt->state = XML_NANO_HTTP_READ;
1422 head = 1;
1423
1424 while ((p = xmlNanoHTTPReadLine(ctxt)) != NULL) {
1425 if (head && (*p == 0)) {
1426 head = 0;
1427 ctxt->content = ctxt->inrptr;
1428 xmlFree(p);
1429 break;
1430 }
1431 xmlNanoHTTPScanAnswer(ctxt, p);
1432
1433#ifdef DEBUG_HTTP
1434 xmlGenericError(xmlGenericErrorContext, "<- %s\n", p);
1435#endif
1436 xmlFree(p);
1437 }
1438
1439 if ((ctxt->location != NULL) && (ctxt->returnValue >= 300) &&
1440 (ctxt->returnValue < 400)) {
1441#ifdef DEBUG_HTTP
1442 xmlGenericError(xmlGenericErrorContext,
1443 "\nRedirect to: %s\n", ctxt->location);
1444#endif
Daniel Veillardf012a642001-07-23 19:10:52 +00001445 while ( xmlNanoHTTPRecv(ctxt) > 0 ) ;
Owen Taylor3473f882001-02-23 17:55:21 +00001446 if (nbRedirects < XML_NANO_HTTP_MAX_REDIR) {
1447 nbRedirects++;
Daniel Veillard9403a042001-05-28 11:00:53 +00001448 if (redirURL != NULL)
1449 xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001450 redirURL = xmlMemStrdup(ctxt->location);
1451 xmlNanoHTTPFreeCtxt(ctxt);
1452 goto retry;
1453 }
1454 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001455 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001456#ifdef DEBUG_HTTP
1457 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardf012a642001-07-23 19:10:52 +00001458 "xmlNanoHTTPMethodRedir: Too many redirects, aborting ...\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001459#endif
1460 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001461 }
1462
1463 if (contentType != NULL) {
1464 if (ctxt->contentType != NULL)
1465 *contentType = xmlMemStrdup(ctxt->contentType);
1466 else
1467 *contentType = NULL;
1468 }
1469
Daniel Veillard9403a042001-05-28 11:00:53 +00001470 if ((redir != NULL) && (redirURL != NULL)) {
1471 *redir = redirURL;
1472 } else {
1473 if (redirURL != NULL)
1474 xmlFree(redirURL);
1475 if (redir != NULL)
1476 *redir = NULL;
1477 }
1478
Owen Taylor3473f882001-02-23 17:55:21 +00001479#ifdef DEBUG_HTTP
1480 if (ctxt->contentType != NULL)
1481 xmlGenericError(xmlGenericErrorContext,
1482 "\nCode %d, content-type '%s'\n\n",
1483 ctxt->returnValue, ctxt->contentType);
1484 else
1485 xmlGenericError(xmlGenericErrorContext,
1486 "\nCode %d, no content-type\n\n",
1487 ctxt->returnValue);
1488#endif
1489
1490 return((void *) ctxt);
1491}
1492
1493/**
Daniel Veillard9403a042001-05-28 11:00:53 +00001494 * xmlNanoHTTPMethod:
1495 * @URL: The URL to load
1496 * @method: the HTTP method to use
1497 * @input: the input string if any
1498 * @contentType: the Content-Type information IN and OUT
1499 * @headers: the extra headers
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001500 * @ilen: input length
Daniel Veillard9403a042001-05-28 11:00:53 +00001501 *
1502 * This function try to open a connection to the indicated resource
1503 * via HTTP using the given @method, adding the given extra headers
1504 * and the input buffer for the request content.
1505 *
1506 * Returns NULL in case of failure, otherwise a request handler.
1507 * The contentType, if provided must be freed by the caller
1508 */
1509
1510void*
1511xmlNanoHTTPMethod(const char *URL, const char *method, const char *input,
Daniel Veillardf012a642001-07-23 19:10:52 +00001512 char **contentType, const char *headers, int ilen) {
Daniel Veillard9403a042001-05-28 11:00:53 +00001513 return(xmlNanoHTTPMethodRedir(URL, method, input, contentType,
Daniel Veillardf012a642001-07-23 19:10:52 +00001514 NULL, headers, ilen));
Daniel Veillard9403a042001-05-28 11:00:53 +00001515}
1516
1517/**
Owen Taylor3473f882001-02-23 17:55:21 +00001518 * xmlNanoHTTPFetch:
1519 * @URL: The URL to load
1520 * @filename: the filename where the content should be saved
1521 * @contentType: if available the Content-Type information will be
1522 * returned at that location
1523 *
1524 * This function try to fetch the indicated resource via HTTP GET
1525 * and save it's content in the file.
1526 *
1527 * Returns -1 in case of failure, 0 incase of success. The contentType,
1528 * if provided must be freed by the caller
1529 */
1530int
1531xmlNanoHTTPFetch(const char *URL, const char *filename, char **contentType) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001532 void *ctxt = NULL;
1533 char *buf = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001534 int fd;
1535 int len;
1536
1537 ctxt = xmlNanoHTTPOpen(URL, contentType);
1538 if (ctxt == NULL) return(-1);
1539
1540 if (!strcmp(filename, "-"))
1541 fd = 0;
1542 else {
1543 fd = open(filename, O_CREAT | O_WRONLY, 00644);
1544 if (fd < 0) {
1545 xmlNanoHTTPClose(ctxt);
1546 if ((contentType != NULL) && (*contentType != NULL)) {
1547 xmlFree(*contentType);
1548 *contentType = NULL;
1549 }
1550 return(-1);
1551 }
1552 }
1553
Daniel Veillardf012a642001-07-23 19:10:52 +00001554 xmlNanoHTTPFetchContent( ctxt, &buf, &len );
1555 if ( len > 0 ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001556 write(fd, buf, len);
1557 }
1558
1559 xmlNanoHTTPClose(ctxt);
1560 close(fd);
1561 return(0);
1562}
1563
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001564#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001565/**
1566 * xmlNanoHTTPSave:
1567 * @ctxt: the HTTP context
1568 * @filename: the filename where the content should be saved
1569 *
1570 * This function saves the output of the HTTP transaction to a file
1571 * It closes and free the context at the end
1572 *
1573 * Returns -1 in case of failure, 0 incase of success.
1574 */
1575int
1576xmlNanoHTTPSave(void *ctxt, const char *filename) {
Daniel Veillarde3924972001-07-25 20:25:21 +00001577 char *buf = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001578 int fd;
1579 int len;
1580
1581 if (ctxt == NULL) return(-1);
1582
1583 if (!strcmp(filename, "-"))
1584 fd = 0;
1585 else {
1586 fd = open(filename, O_CREAT | O_WRONLY);
1587 if (fd < 0) {
1588 xmlNanoHTTPClose(ctxt);
1589 return(-1);
1590 }
1591 }
1592
Daniel Veillardf012a642001-07-23 19:10:52 +00001593 xmlNanoHTTPFetchContent( ctxt, &buf, &len );
1594 if ( len > 0 ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001595 write(fd, buf, len);
1596 }
1597
1598 xmlNanoHTTPClose(ctxt);
William M. Brack20d82362004-03-17 08:44:46 +00001599 close(fd);
Owen Taylor3473f882001-02-23 17:55:21 +00001600 return(0);
1601}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001602#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001603
1604/**
1605 * xmlNanoHTTPReturnCode:
1606 * @ctx: the HTTP context
1607 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001608 * Get the latest HTTP return code received
1609 *
Owen Taylor3473f882001-02-23 17:55:21 +00001610 * Returns the HTTP return code for the request.
1611 */
1612int
1613xmlNanoHTTPReturnCode(void *ctx) {
1614 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1615
1616 if (ctxt == NULL) return(-1);
1617
1618 return(ctxt->returnValue);
1619}
1620
1621/**
1622 * xmlNanoHTTPAuthHeader:
1623 * @ctx: the HTTP context
1624 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001625 * Get the authentication header of an HTTP context
1626 *
Owen Taylor3473f882001-02-23 17:55:21 +00001627 * Returns the stashed value of the WWW-Authenticate or Proxy-Authenticate
1628 * header.
1629 */
1630const char *
1631xmlNanoHTTPAuthHeader(void *ctx) {
1632 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1633
1634 if (ctxt == NULL) return(NULL);
1635
1636 return(ctxt->authHeader);
1637}
1638
Daniel Veillardf012a642001-07-23 19:10:52 +00001639/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00001640 * xmlNanoHTTPContentLength:
Daniel Veillardf012a642001-07-23 19:10:52 +00001641 * @ctx: the HTTP context
1642 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001643 * Provides the specified content length from the HTTP header.
1644 *
Daniel Veillardf012a642001-07-23 19:10:52 +00001645 * Return the specified content length from the HTTP header. Note that
1646 * a value of -1 indicates that the content length element was not included in
1647 * the response header.
1648 */
1649int
1650xmlNanoHTTPContentLength( void * ctx ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001651 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillardf012a642001-07-23 19:10:52 +00001652
1653 return ( ( ctxt == NULL ) ? -1 : ctxt->ContentLength );
1654}
1655
1656/**
Daniel Veillard847332a2003-10-18 11:29:40 +00001657 * xmlNanoHTTPRedir:
1658 * @ctx: the HTTP context
1659 *
1660 * Provides the specified redirection URL if available from the HTTP header.
1661 *
1662 * Return the specified redirection URL or NULL if not redirected.
1663 */
1664const char *
1665xmlNanoHTTPRedir( void * ctx ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001666 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillard847332a2003-10-18 11:29:40 +00001667
1668 return ( ( ctxt == NULL ) ? NULL : ctxt->location );
1669}
1670
1671/**
1672 * xmlNanoHTTPEncoding:
1673 * @ctx: the HTTP context
1674 *
1675 * Provides the specified encoding if specified in the HTTP headers.
1676 *
1677 * Return the specified encoding or NULL if not available
1678 */
1679const char *
1680xmlNanoHTTPEncoding( void * ctx ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001681 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillard847332a2003-10-18 11:29:40 +00001682
1683 return ( ( ctxt == NULL ) ? NULL : ctxt->encoding );
1684}
1685
1686/**
Daniel Veillarda840b692003-10-19 13:35:37 +00001687 * xmlNanoHTTPMimeType:
1688 * @ctx: the HTTP context
1689 *
1690 * Provides the specified Mime-Type if specified in the HTTP headers.
1691 *
1692 * Return the specified Mime-Type or NULL if not available
1693 */
1694const char *
1695xmlNanoHTTPMimeType( void * ctx ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001696 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillarda840b692003-10-19 13:35:37 +00001697
1698 return ( ( ctxt == NULL ) ? NULL : ctxt->mimeType );
1699}
1700
1701/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00001702 * xmlNanoHTTPFetchContent:
Daniel Veillardf012a642001-07-23 19:10:52 +00001703 * @ctx: the HTTP context
1704 * @ptr: pointer to set to the content buffer.
1705 * @len: integer pointer to hold the length of the content
1706 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001707 * Check if all the content was read
1708 *
Daniel Veillardf012a642001-07-23 19:10:52 +00001709 * Returns 0 if all the content was read and available, returns
1710 * -1 if received content length was less than specified or an error
1711 * occurred.
1712 */
Daniel Veillarda2351322004-06-27 12:08:10 +00001713static int
Daniel Veillardf012a642001-07-23 19:10:52 +00001714xmlNanoHTTPFetchContent( void * ctx, char ** ptr, int * len ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001715 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillardf012a642001-07-23 19:10:52 +00001716
1717 int rc = 0;
1718 int cur_lgth;
1719 int rcvd_lgth;
1720 int dummy_int;
1721 char * dummy_ptr = NULL;
1722
1723 /* Dummy up return input parameters if not provided */
1724
1725 if ( len == NULL )
1726 len = &dummy_int;
1727
1728 if ( ptr == NULL )
1729 ptr = &dummy_ptr;
1730
1731 /* But can't work without the context pointer */
1732
1733 if ( ( ctxt == NULL ) || ( ctxt->content == NULL ) ) {
1734 *len = 0;
1735 *ptr = NULL;
1736 return ( -1 );
1737 }
1738
1739 rcvd_lgth = ctxt->inptr - ctxt->content;
1740
1741 while ( (cur_lgth = xmlNanoHTTPRecv( ctxt )) > 0 ) {
1742
1743 rcvd_lgth += cur_lgth;
1744 if ( (ctxt->ContentLength > 0) && (rcvd_lgth >= ctxt->ContentLength) )
1745 break;
1746 }
1747
1748 *ptr = ctxt->content;
1749 *len = rcvd_lgth;
1750
1751 if ( ( ctxt->ContentLength > 0 ) && ( rcvd_lgth < ctxt->ContentLength ) )
1752 rc = -1;
1753 else if ( rcvd_lgth == 0 )
1754 rc = -1;
1755
1756 return ( rc );
1757}
1758
Owen Taylor3473f882001-02-23 17:55:21 +00001759#ifdef STANDALONE
1760int main(int argc, char **argv) {
1761 char *contentType = NULL;
1762
1763 if (argv[1] != NULL) {
1764 if (argv[2] != NULL)
1765 xmlNanoHTTPFetch(argv[1], argv[2], &contentType);
1766 else
1767 xmlNanoHTTPFetch(argv[1], "-", &contentType);
1768 if (contentType != NULL) xmlFree(contentType);
1769 } else {
1770 xmlGenericError(xmlGenericErrorContext,
1771 "%s: minimal HTTP GET implementation\n", argv[0]);
1772 xmlGenericError(xmlGenericErrorContext,
1773 "\tusage %s [ URL [ filename ] ]\n", argv[0]);
1774 }
1775 xmlNanoHTTPCleanup();
1776 xmlMemoryDump();
1777 return(0);
1778}
1779#endif /* STANDALONE */
1780#else /* !LIBXML_HTTP_ENABLED */
1781#ifdef STANDALONE
1782#include <stdio.h>
1783int main(int argc, char **argv) {
1784 xmlGenericError(xmlGenericErrorContext,
1785 "%s : HTTP support not compiled in\n", argv[0]);
1786 return(0);
1787}
1788#endif /* STANDALONE */
1789#endif /* LIBXML_HTTP_ENABLED */