blob: 0bb5e77a62acb7146efc69d4d5f09cadddb1647c [file] [log] [blame]
Owen Taylor3473f882001-02-23 17:55:21 +00001/*
2 * nanohttp.c: minimalist HTTP GET implementation to fetch external subsets.
3 * focuses on size, streamability, reentrancy and portability
4 *
5 * This is clearly not a general purpose HTTP implementation
6 * If you look for one, check:
7 * http://www.w3.org/Library/
8 *
9 * See Copyright for the status of this software.
10 *
Daniel Veillardc5d64342001-06-24 12:13:24 +000011 * daniel@veillard.com
Owen Taylor3473f882001-02-23 17:55:21 +000012 */
13
14/* TODO add compression support, Send the Accept- , and decompress on the
15 fly with ZLIB if found at compile-time */
16
Daniel Veillardf3afa7d2001-06-09 13:52:58 +000017#define NEED_SOCKETS
Daniel Veillard34ce8be2002-03-18 19:37:11 +000018#define IN_LIBXML
Bjorn Reese70a9da52001-04-21 16:57:29 +000019#include "libxml.h"
Owen Taylor3473f882001-02-23 17:55:21 +000020
21#ifdef LIBXML_HTTP_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +000022#include <string.h>
23
24#ifdef HAVE_STDLIB_H
25#include <stdlib.h>
26#endif
27#ifdef HAVE_UNISTD_H
28#include <unistd.h>
29#endif
Daniel Veillard75eb1ad2003-07-07 14:42:44 +000030#ifdef HAVE_SYS_TYPES_H
31#include <sys/types.h>
32#endif
Owen Taylor3473f882001-02-23 17:55:21 +000033#ifdef HAVE_SYS_SOCKET_H
34#include <sys/socket.h>
35#endif
36#ifdef HAVE_NETINET_IN_H
37#include <netinet/in.h>
38#endif
39#ifdef HAVE_ARPA_INET_H
40#include <arpa/inet.h>
41#endif
42#ifdef HAVE_NETDB_H
43#include <netdb.h>
44#endif
Daniel Veillardd85f4f42002-03-25 10:48:46 +000045#ifdef HAVE_RESOLV_H
Daniel Veillard9b731d72002-04-14 12:56:08 +000046#ifdef HAVE_ARPA_NAMESER_H
47#include <arpa/nameser.h>
48#endif
Daniel Veillardd85f4f42002-03-25 10:48:46 +000049#include <resolv.h>
50#endif
Owen Taylor3473f882001-02-23 17:55:21 +000051#ifdef HAVE_FCNTL_H
52#include <fcntl.h>
53#endif
54#ifdef HAVE_ERRNO_H
55#include <errno.h>
56#endif
57#ifdef HAVE_SYS_TIME_H
58#include <sys/time.h>
59#endif
60#ifdef HAVE_SYS_SELECT_H
61#include <sys/select.h>
62#endif
63#ifdef HAVE_STRINGS_H
64#include <strings.h>
65#endif
66#ifdef SUPPORT_IP6
67#include <resolv.h>
68#endif
69
70#ifdef VMS
71#include <stropts>
72#define SOCKLEN_T unsigned int
73#define SOCKET int
74#endif
75
Daniel Veillard1638a472003-08-14 01:23:25 +000076
77#ifdef __MINGW32__
78#define _WINSOCKAPI_
79#include <wsockcompat.h>
80#include <winsock2.h>
81#undef SOCKLEN_T
82#define SOCKLEN_T unsigned int
83#endif
84
85
Daniel Veillardd0463562001-10-13 09:15:48 +000086#include <libxml/globals.h>
Daniel Veillardf012a642001-07-23 19:10:52 +000087#include <libxml/xmlerror.h>
Owen Taylor3473f882001-02-23 17:55:21 +000088#include <libxml/xmlmemory.h>
89#include <libxml/parser.h> /* for xmlStr(n)casecmp() */
90#include <libxml/nanohttp.h>
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000091#include <libxml/globals.h>
Daniel Veillard8efff672002-12-04 11:44:48 +000092#include <libxml/uri.h>
Owen Taylor3473f882001-02-23 17:55:21 +000093
94/**
95 * A couple portability macros
96 */
97#ifndef _WINSOCKAPI_
Daniel Veillarda9cce9c2003-09-29 13:20:24 +000098#ifndef __BEOS__
Owen Taylor3473f882001-02-23 17:55:21 +000099#define closesocket(s) close(s)
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000100#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000101#define SOCKET int
102#endif
103
Daniel Veillard89f7f272003-09-29 13:29:09 +0000104#ifdef __BEOS__
105#ifndef PF_INET
106#define PF_INET AF_INET
107#endif
108#endif
109
Daniel Veillard75be0132002-03-13 10:03:35 +0000110#ifndef SOCKLEN_T
111#define SOCKLEN_T unsigned int
112#endif
113#ifndef SOCKET
114#define SOCKET int
115#endif
Daniel Veillardf012a642001-07-23 19:10:52 +0000116
Owen Taylor3473f882001-02-23 17:55:21 +0000117#ifdef STANDALONE
118#define DEBUG_HTTP
119#define xmlStrncasecmp(a, b, n) strncasecmp((char *)a, (char *)b, n)
120#define xmlStrcasecmpi(a, b) strcasecmp((char *)a, (char *)b)
121#endif
122
123#define XML_NANO_HTTP_MAX_REDIR 10
124
125#define XML_NANO_HTTP_CHUNK 4096
126
127#define XML_NANO_HTTP_CLOSED 0
128#define XML_NANO_HTTP_WRITE 1
129#define XML_NANO_HTTP_READ 2
130#define XML_NANO_HTTP_NONE 4
131
132typedef struct xmlNanoHTTPCtxt {
133 char *protocol; /* the protocol name */
134 char *hostname; /* the host name */
135 int port; /* the port */
136 char *path; /* the path within the URL */
137 SOCKET fd; /* the file descriptor for the socket */
138 int state; /* WRITE / READ / CLOSED */
139 char *out; /* buffer sent (zero terminated) */
140 char *outptr; /* index within the buffer sent */
141 char *in; /* the receiving buffer */
142 char *content; /* the start of the content */
143 char *inptr; /* the next byte to read from network */
144 char *inrptr; /* the next byte to give back to the client */
145 int inlen; /* len of the input buffer */
146 int last; /* return code for last operation */
147 int returnValue; /* the protocol return value */
Daniel Veillardf012a642001-07-23 19:10:52 +0000148 int ContentLength; /* specified content length from HTTP header */
Owen Taylor3473f882001-02-23 17:55:21 +0000149 char *contentType; /* the MIME type for the input */
150 char *location; /* the new URL in case of redirect */
151 char *authHeader; /* contents of {WWW,Proxy}-Authenticate header */
Daniel Veillard847332a2003-10-18 11:29:40 +0000152 char *encoding; /* encoding extracted from the contentType */
Daniel Veillarda840b692003-10-19 13:35:37 +0000153 char *mimeType; /* Mime-Type extracted from the contentType */
Owen Taylor3473f882001-02-23 17:55:21 +0000154} xmlNanoHTTPCtxt, *xmlNanoHTTPCtxtPtr;
155
156static int initialized = 0;
157static char *proxy = NULL; /* the proxy name if any */
158static int proxyPort; /* the proxy port if any */
159static unsigned int timeout = 60;/* the select() timeout in seconds */
160
Daniel Veillardf012a642001-07-23 19:10:52 +0000161int xmlNanoHTTPFetchContent( void * ctx, char ** ptr, int * len );
162int xmlNanoHTTPContentLength( void * ctx );
163
Owen Taylor3473f882001-02-23 17:55:21 +0000164/**
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000165 * xmlHTTPErrMemory:
166 * @extra: extra informations
167 *
168 * Handle an out of memory condition
169 */
170static void
171xmlHTTPErrMemory(const char *extra)
172{
173 __xmlSimpleError(XML_FROM_HTTP, XML_ERR_NO_MEMORY, NULL, NULL, extra);
174}
175
176/**
Owen Taylor3473f882001-02-23 17:55:21 +0000177 * A portability function
178 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000179static int socket_errno(void) {
Owen Taylor3473f882001-02-23 17:55:21 +0000180#ifdef _WINSOCKAPI_
181 return(WSAGetLastError());
182#else
183 return(errno);
184#endif
185}
186
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000187#ifdef SUPPORT_IP6
Daniel Veillard2db8c122003-07-08 12:16:59 +0000188static
189int have_ipv6(void) {
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000190 int s;
191
192 s = socket (AF_INET6, SOCK_STREAM, 0);
193 if (s != -1) {
194 close (s);
195 return (1);
196 }
197 return (0);
198}
199#endif
200
Owen Taylor3473f882001-02-23 17:55:21 +0000201/**
202 * xmlNanoHTTPInit:
203 *
204 * Initialize the HTTP protocol layer.
205 * Currently it just checks for proxy informations
206 */
207
208void
209xmlNanoHTTPInit(void) {
210 const char *env;
211#ifdef _WINSOCKAPI_
212 WSADATA wsaData;
213#endif
214
215 if (initialized)
216 return;
217
218#ifdef _WINSOCKAPI_
219 if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0)
220 return;
221#endif
222
223 if (proxy == NULL) {
224 proxyPort = 80;
225 env = getenv("no_proxy");
226 if (env != NULL)
227 goto done;
228 env = getenv("http_proxy");
229 if (env != NULL) {
230 xmlNanoHTTPScanProxy(env);
231 goto done;
232 }
233 env = getenv("HTTP_PROXY");
234 if (env != NULL) {
235 xmlNanoHTTPScanProxy(env);
236 goto done;
237 }
238 }
239done:
240 initialized = 1;
241}
242
243/**
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000244 * xmlNanoHTTPCleanup:
Owen Taylor3473f882001-02-23 17:55:21 +0000245 *
246 * Cleanup the HTTP protocol layer.
247 */
248
249void
250xmlNanoHTTPCleanup(void) {
251 if (proxy != NULL)
252 xmlFree(proxy);
253#ifdef _WINSOCKAPI_
254 if (initialized)
255 WSACleanup();
256#endif
257 initialized = 0;
258 return;
259}
260
261/**
Owen Taylor3473f882001-02-23 17:55:21 +0000262 * xmlNanoHTTPScanURL:
263 * @ctxt: an HTTP context
264 * @URL: The URL used to initialize the context
265 *
266 * (Re)Initialize an HTTP context by parsing the URL and finding
267 * the protocol host port and path it indicates.
268 */
269
270static void
271xmlNanoHTTPScanURL(xmlNanoHTTPCtxtPtr ctxt, const char *URL) {
272 const char *cur = URL;
273 char buf[4096];
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000274 int indx = 0;
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);
843 ctxt->location = xmlMemStrdup(cur);
844 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"WWW-Authenticate:", 17)) {
845 cur += 17;
846 while ((*cur == ' ') || (*cur == '\t')) cur++;
847 if (ctxt->authHeader != NULL)
848 xmlFree(ctxt->authHeader);
849 ctxt->authHeader = xmlMemStrdup(cur);
850 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Proxy-Authenticate:", 19)) {
851 cur += 19;
852 while ((*cur == ' ') || (*cur == '\t')) cur++;
853 if (ctxt->authHeader != NULL)
854 xmlFree(ctxt->authHeader);
855 ctxt->authHeader = xmlMemStrdup(cur);
Daniel Veillardf012a642001-07-23 19:10:52 +0000856 } else if ( !xmlStrncasecmp( BAD_CAST line, BAD_CAST"Content-Length:", 15) ) {
857 cur += 15;
858 ctxt->ContentLength = strtol( cur, NULL, 10 );
Owen Taylor3473f882001-02-23 17:55:21 +0000859 }
860}
861
862/**
863 * xmlNanoHTTPConnectAttempt:
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000864 * @addr: a socket address structure
Owen Taylor3473f882001-02-23 17:55:21 +0000865 *
866 * Attempt a connection to the given IP:port endpoint. It forces
867 * non-blocking semantic on the socket, and allow 60 seconds for
868 * the host to answer.
869 *
870 * Returns -1 in case of failure, the file descriptor number otherwise
871 */
872
873static int
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000874xmlNanoHTTPConnectAttempt(struct sockaddr *addr)
Owen Taylor3473f882001-02-23 17:55:21 +0000875{
Owen Taylor3473f882001-02-23 17:55:21 +0000876 fd_set wfd;
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000877#ifdef _WINSOCKAPI_
878 fd_set xfd;
879#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000880 struct timeval tv;
881 int status;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000882 int addrlen;
883 SOCKET s;
Owen Taylor3473f882001-02-23 17:55:21 +0000884
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000885#ifdef SUPPORT_IP6
886 if (addr->sa_family == AF_INET6) {
887 s = socket (PF_INET6, SOCK_STREAM, IPPROTO_TCP);
888 addrlen = sizeof (struct sockaddr_in6);
889 }
890 else
891#endif
892 {
893 s = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
894 addrlen = sizeof (struct sockaddr_in);
895 }
Owen Taylor3473f882001-02-23 17:55:21 +0000896 if (s==-1) {
897#ifdef DEBUG_HTTP
898 perror("socket");
899#endif
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000900 __xmlIOErr(XML_FROM_HTTP, 0, "socket failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000901 return(-1);
902 }
903
904#ifdef _WINSOCKAPI_
905 {
906 u_long one = 1;
907
908 status = ioctlsocket(s, FIONBIO, &one) == SOCKET_ERROR ? -1 : 0;
909 }
910#else /* _WINSOCKAPI_ */
911#if defined(VMS)
912 {
913 int enable = 1;
914 status = ioctl(s, FIONBIO, &enable);
915 }
916#else /* VMS */
Daniel Veillard254b1262003-11-01 17:04:58 +0000917#if defined(__BEOS__)
918 {
919 bool noblock = true;
920 status = setsockopt(s, SOL_SOCKET, SO_NONBLOCK, &noblock, sizeof(noblock));
921 }
922#else /* __BEOS__ */
Owen Taylor3473f882001-02-23 17:55:21 +0000923 if ((status = fcntl(s, F_GETFL, 0)) != -1) {
924#ifdef O_NONBLOCK
925 status |= O_NONBLOCK;
926#else /* O_NONBLOCK */
927#ifdef F_NDELAY
928 status |= F_NDELAY;
929#endif /* F_NDELAY */
930#endif /* !O_NONBLOCK */
931 status = fcntl(s, F_SETFL, status);
932 }
933 if (status < 0) {
934#ifdef DEBUG_HTTP
935 perror("nonblocking");
936#endif
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000937 __xmlIOErr(XML_FROM_HTTP, 0, "error setting non-blocking IO\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000938 closesocket(s);
939 return(-1);
940 }
Daniel Veillard254b1262003-11-01 17:04:58 +0000941#endif /* !__BEOS__ */
Owen Taylor3473f882001-02-23 17:55:21 +0000942#endif /* !VMS */
943#endif /* !_WINSOCKAPI_ */
944
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000945 if (connect (s, addr, addrlen) == -1) {
Owen Taylor3473f882001-02-23 17:55:21 +0000946 switch (socket_errno()) {
947 case EINPROGRESS:
948 case EWOULDBLOCK:
949 break;
950 default:
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000951 __xmlIOErr(XML_FROM_HTTP, 0, "error connecting to HTTP server");
Owen Taylor3473f882001-02-23 17:55:21 +0000952 closesocket(s);
953 return(-1);
954 }
955 }
956
957 tv.tv_sec = timeout;
958 tv.tv_usec = 0;
959
960 FD_ZERO(&wfd);
961 FD_SET(s, &wfd);
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000962
963#ifdef _WINSOCKAPI_
964 FD_ZERO(&xfd);
965 FD_SET(s, &xfd);
Owen Taylor3473f882001-02-23 17:55:21 +0000966
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000967 switch(select(s+1, NULL, &wfd, &xfd, &tv))
968#else
Owen Taylor3473f882001-02-23 17:55:21 +0000969 switch(select(s+1, NULL, &wfd, NULL, &tv))
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000970#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000971 {
972 case 0:
973 /* Time out */
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000974 __xmlIOErr(XML_FROM_HTTP, 0, "Connect attempt timed out");
Owen Taylor3473f882001-02-23 17:55:21 +0000975 closesocket(s);
976 return(-1);
977 case -1:
978 /* Ermm.. ?? */
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000979 __xmlIOErr(XML_FROM_HTTP, 0, "Connect failed");
Owen Taylor3473f882001-02-23 17:55:21 +0000980 closesocket(s);
981 return(-1);
982 }
983
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000984 if ( FD_ISSET(s, &wfd)
985#ifdef _WINSOCKAPI_
986 || FD_ISSET(s, &xfd)
987#endif
988 ) {
Owen Taylor3473f882001-02-23 17:55:21 +0000989 SOCKLEN_T len;
990 len = sizeof(status);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000991#ifdef SO_ERROR
Owen Taylor3473f882001-02-23 17:55:21 +0000992 if (getsockopt(s, SOL_SOCKET, SO_ERROR, (char*)&status, &len) < 0 ) {
993 /* Solaris error code */
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000994 __xmlIOErr(XML_FROM_HTTP, 0, "getsockopt failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000995 return (-1);
996 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000997#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000998 if ( status ) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000999 __xmlIOErr(XML_FROM_HTTP, 0, "Error connecting to remote host");
Owen Taylor3473f882001-02-23 17:55:21 +00001000 closesocket(s);
1001 errno = status;
1002 return (-1);
1003 }
1004 } else {
1005 /* pbm */
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001006 __xmlIOErr(XML_FROM_HTTP, 0, "select failed\n");
Daniel Veillardf012a642001-07-23 19:10:52 +00001007 closesocket(s);
Owen Taylor3473f882001-02-23 17:55:21 +00001008 return (-1);
1009 }
1010
1011 return(s);
1012}
1013
1014/**
1015 * xmlNanoHTTPConnectHost:
1016 * @host: the host name
1017 * @port: the port number
1018 *
1019 * Attempt a connection to the given host:port endpoint. It tries
1020 * the multiple IP provided by the DNS if available.
1021 *
1022 * Returns -1 in case of failure, the file descriptor number otherwise
1023 */
1024
1025static int
1026xmlNanoHTTPConnectHost(const char *host, int port)
1027{
1028 struct hostent *h;
Daniel Veillard2db8c122003-07-08 12:16:59 +00001029 struct sockaddr *addr = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001030 struct in_addr ia;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001031 struct sockaddr_in sockin;
Daniel Veillard5c396542002-03-15 07:57:50 +00001032
Owen Taylor3473f882001-02-23 17:55:21 +00001033#ifdef SUPPORT_IP6
1034 struct in6_addr ia6;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001035 struct sockaddr_in6 sockin6;
Owen Taylor3473f882001-02-23 17:55:21 +00001036#endif
1037 int i;
1038 int s;
Daniel Veillard5c396542002-03-15 07:57:50 +00001039
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001040 memset (&sockin, 0, sizeof(sockin));
1041#ifdef SUPPORT_IP6
1042 memset (&sockin6, 0, sizeof(sockin6));
1043 if (have_ipv6 ())
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001044#if !defined(HAVE_GETADDRINFO) && defined(RES_USE_INET6)
Daniel Veillard560c2a42003-07-06 21:13:49 +00001045 {
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001046 if (!(_res.options & RES_INIT))
1047 res_init();
1048 _res.options |= RES_USE_INET6;
1049 }
1050#elif defined(HAVE_GETADDRINFO)
Daniel Veillard560c2a42003-07-06 21:13:49 +00001051 {
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001052 int status;
1053 struct addrinfo hints, *res, *result;
1054
1055 result = NULL;
1056 memset (&hints, 0,sizeof(hints));
1057 hints.ai_socktype = SOCK_STREAM;
1058
1059 status = getaddrinfo (host, NULL, &hints, &result);
1060 if (status) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001061 __xmlIOErr(XML_FROM_HTTP, 0, "getaddrinfo failed\n");
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001062 return (-1);
1063 }
1064
1065 for (res = result; res; res = res->ai_next) {
Daniel Veillard3dc93a42003-07-10 14:04:33 +00001066 if (res->ai_family == AF_INET || res->ai_family == AF_INET6) {
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001067 if (res->ai_family == AF_INET6) {
1068 memcpy (&sockin6, res->ai_addr, res->ai_addrlen);
1069 sockin6.sin6_port = htons (port);
1070 addr = (struct sockaddr *)&sockin6;
1071 }
Daniel Veillard3dc93a42003-07-10 14:04:33 +00001072 else {
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001073 memcpy (&sockin, res->ai_addr, res->ai_addrlen);
1074 sockin.sin_port = htons (port);
1075 addr = (struct sockaddr *)&sockin;
1076 }
1077
1078 s = xmlNanoHTTPConnectAttempt (addr);
1079 if (s != -1) {
1080 freeaddrinfo (result);
1081 return (s);
1082 }
1083 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001084 }
Daniel Veillard3dc93a42003-07-10 14:04:33 +00001085 if (result)
1086 freeaddrinfo (result);
1087 return (-1);
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001088 } else
Owen Taylor3473f882001-02-23 17:55:21 +00001089#endif
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001090#endif
1091 {
1092 h = gethostbyname (host);
1093 if (h == NULL) {
Daniel Veillard56b2db72002-03-25 16:35:28 +00001094
1095/*
1096 * Okay, I got fed up by the non-portability of this error message
1097 * extraction code. it work on Linux, if it work on your platform
1098 * and one want to enable it, send me the defined(foobar) needed
1099 */
1100#if defined(HAVE_NETDB_H) && defined(HOST_NOT_FOUND) && defined(linux)
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001101 const char *h_err_txt = "";
Daniel Veillardf012a642001-07-23 19:10:52 +00001102
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001103 switch (h_errno) {
1104 case HOST_NOT_FOUND:
1105 h_err_txt = "Authoritive host not found";
1106 break;
Daniel Veillardf012a642001-07-23 19:10:52 +00001107
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001108 case TRY_AGAIN:
1109 h_err_txt =
1110 "Non-authoritive host not found or server failure.";
1111 break;
Daniel Veillardf012a642001-07-23 19:10:52 +00001112
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001113 case NO_RECOVERY:
1114 h_err_txt =
1115 "Non-recoverable errors: FORMERR, REFUSED, or NOTIMP.";
1116 break;
Daniel Veillard5c396542002-03-15 07:57:50 +00001117
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001118 case NO_ADDRESS:
1119 h_err_txt =
1120 "Valid name, no data record of requested type.";
1121 break;
Daniel Veillard5c396542002-03-15 07:57:50 +00001122
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001123 default:
1124 h_err_txt = "No error text defined.";
1125 break;
1126 }
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001127 __xmlIOErr(XML_FROM_HTTP, 0, h_err_txt);
Daniel Veillard5c396542002-03-15 07:57:50 +00001128#else
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001129 __xmlIOErr(XML_FROM_HTTP, 0, "Failed to resolve host");
Owen Taylor3473f882001-02-23 17:55:21 +00001130#endif
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001131 return (-1);
1132 }
Daniel Veillard5c396542002-03-15 07:57:50 +00001133
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001134 for (i = 0; h->h_addr_list[i]; i++) {
1135 if (h->h_addrtype == AF_INET) {
1136 /* A records (IPv4) */
1137 memcpy (&ia, h->h_addr_list[i], h->h_length);
1138 sockin.sin_family = h->h_addrtype;
1139 sockin.sin_addr = ia;
1140 sockin.sin_port = htons (port);
1141 addr = (struct sockaddr *) &sockin;
Daniel Veillard5c396542002-03-15 07:57:50 +00001142#ifdef SUPPORT_IP6
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001143 } else if (have_ipv6 () && (h->h_addrtype == AF_INET6)) {
1144 /* AAAA records (IPv6) */
1145 memcpy (&ia6, h->h_addr_list[i], h->h_length);
1146 sockin6.sin6_family = h->h_addrtype;
1147 sockin6.sin6_addr = ia6;
1148 sockin6.sin6_port = htons (port);
1149 addr = (struct sockaddr *) &sockin6;
Daniel Veillard5c396542002-03-15 07:57:50 +00001150#endif
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001151 } else
1152 break; /* for */
Daniel Veillard5c396542002-03-15 07:57:50 +00001153
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001154 s = xmlNanoHTTPConnectAttempt (addr);
1155 if (s != -1)
1156 return (s);
1157 }
Owen Taylor3473f882001-02-23 17:55:21 +00001158 }
Owen Taylor3473f882001-02-23 17:55:21 +00001159#ifdef DEBUG_HTTP
1160 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard5c396542002-03-15 07:57:50 +00001161 "xmlNanoHTTPConnectHost: unable to connect to '%s'.\n",
1162 host);
Owen Taylor3473f882001-02-23 17:55:21 +00001163#endif
Daniel Veillard5c396542002-03-15 07:57:50 +00001164 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001165}
1166
1167
1168/**
1169 * xmlNanoHTTPOpen:
1170 * @URL: The URL to load
1171 * @contentType: if available the Content-Type information will be
1172 * returned at that location
1173 *
1174 * This function try to open a connection to the indicated resource
1175 * via HTTP GET.
1176 *
1177 * Returns NULL in case of failure, otherwise a request handler.
1178 * The contentType, if provided must be freed by the caller
1179 */
1180
1181void*
1182xmlNanoHTTPOpen(const char *URL, char **contentType) {
1183 if (contentType != NULL) *contentType = NULL;
Daniel Veillardf012a642001-07-23 19:10:52 +00001184 return(xmlNanoHTTPMethod(URL, NULL, NULL, contentType, NULL, 0));
Daniel Veillard9403a042001-05-28 11:00:53 +00001185}
1186
1187/**
1188 * xmlNanoHTTPOpenRedir:
1189 * @URL: The URL to load
1190 * @contentType: if available the Content-Type information will be
1191 * returned at that location
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001192 * @redir: if available the redirected URL will be returned
Daniel Veillard9403a042001-05-28 11:00:53 +00001193 *
1194 * This function try to open a connection to the indicated resource
1195 * via HTTP GET.
1196 *
1197 * Returns NULL in case of failure, otherwise a request handler.
1198 * The contentType, if provided must be freed by the caller
1199 */
1200
1201void*
1202xmlNanoHTTPOpenRedir(const char *URL, char **contentType, char **redir) {
1203 if (contentType != NULL) *contentType = NULL;
1204 if (redir != NULL) *redir = NULL;
Daniel Veillardf012a642001-07-23 19:10:52 +00001205 return(xmlNanoHTTPMethodRedir(URL, NULL, NULL, contentType, redir, NULL,0));
Owen Taylor3473f882001-02-23 17:55:21 +00001206}
1207
1208/**
1209 * xmlNanoHTTPRead:
1210 * @ctx: the HTTP context
1211 * @dest: a buffer
1212 * @len: the buffer length
1213 *
1214 * This function tries to read @len bytes from the existing HTTP connection
1215 * and saves them in @dest. This is a blocking call.
1216 *
1217 * Returns the number of byte read. 0 is an indication of an end of connection.
1218 * -1 indicates a parameter error.
1219 */
1220int
1221xmlNanoHTTPRead(void *ctx, void *dest, int len) {
1222 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1223
1224 if (ctx == NULL) return(-1);
1225 if (dest == NULL) return(-1);
1226 if (len <= 0) return(0);
1227
1228 while (ctxt->inptr - ctxt->inrptr < len) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001229 if (xmlNanoHTTPRecv(ctxt) <= 0) break;
Owen Taylor3473f882001-02-23 17:55:21 +00001230 }
1231 if (ctxt->inptr - ctxt->inrptr < len)
1232 len = ctxt->inptr - ctxt->inrptr;
1233 memcpy(dest, ctxt->inrptr, len);
1234 ctxt->inrptr += len;
1235 return(len);
1236}
1237
1238/**
1239 * xmlNanoHTTPClose:
1240 * @ctx: the HTTP context
1241 *
1242 * This function closes an HTTP context, it ends up the connection and
1243 * free all data related to it.
1244 */
1245void
1246xmlNanoHTTPClose(void *ctx) {
1247 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1248
1249 if (ctx == NULL) return;
1250
1251 xmlNanoHTTPFreeCtxt(ctxt);
1252}
1253
1254/**
Daniel Veillard9403a042001-05-28 11:00:53 +00001255 * xmlNanoHTTPMethodRedir:
Owen Taylor3473f882001-02-23 17:55:21 +00001256 * @URL: The URL to load
1257 * @method: the HTTP method to use
1258 * @input: the input string if any
1259 * @contentType: the Content-Type information IN and OUT
Daniel Veillard9403a042001-05-28 11:00:53 +00001260 * @redir: the redirected URL OUT
Owen Taylor3473f882001-02-23 17:55:21 +00001261 * @headers: the extra headers
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001262 * @ilen: input length
Owen Taylor3473f882001-02-23 17:55:21 +00001263 *
1264 * This function try to open a connection to the indicated resource
1265 * via HTTP using the given @method, adding the given extra headers
1266 * and the input buffer for the request content.
1267 *
1268 * Returns NULL in case of failure, otherwise a request handler.
Daniel Veillard9403a042001-05-28 11:00:53 +00001269 * The contentType, or redir, if provided must be freed by the caller
Owen Taylor3473f882001-02-23 17:55:21 +00001270 */
1271
1272void*
Daniel Veillard9403a042001-05-28 11:00:53 +00001273xmlNanoHTTPMethodRedir(const char *URL, const char *method, const char *input,
Daniel Veillardf012a642001-07-23 19:10:52 +00001274 char **contentType, char **redir,
1275 const char *headers, int ilen ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001276 xmlNanoHTTPCtxtPtr ctxt;
1277 char *bp, *p;
Daniel Veillardf012a642001-07-23 19:10:52 +00001278 int blen, ret;
Owen Taylor3473f882001-02-23 17:55:21 +00001279 int head;
1280 int nbRedirects = 0;
1281 char *redirURL = NULL;
William M. Brack78637da2003-07-31 14:47:38 +00001282#ifdef DEBUG_HTTP
1283 int xmt_bytes;
1284#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001285
1286 if (URL == NULL) return(NULL);
1287 if (method == NULL) method = "GET";
1288 xmlNanoHTTPInit();
1289
1290retry:
1291 if (redirURL == NULL)
1292 ctxt = xmlNanoHTTPNewCtxt(URL);
1293 else {
1294 ctxt = xmlNanoHTTPNewCtxt(redirURL);
Daniel Veillarda840b692003-10-19 13:35:37 +00001295 ctxt->location = xmlMemStrdup(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001296 }
1297
Daniel Veillardf012a642001-07-23 19:10:52 +00001298 if ( ctxt == NULL ) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001299 return ( NULL );
1300 }
1301
Owen Taylor3473f882001-02-23 17:55:21 +00001302 if ((ctxt->protocol == NULL) || (strcmp(ctxt->protocol, "http"))) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001303 __xmlIOErr(XML_FROM_HTTP, XML_HTTP_URL_SYNTAX, "Not a valid HTTP URI");
Owen Taylor3473f882001-02-23 17:55:21 +00001304 xmlNanoHTTPFreeCtxt(ctxt);
1305 if (redirURL != NULL) xmlFree(redirURL);
1306 return(NULL);
1307 }
1308 if (ctxt->hostname == NULL) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001309 __xmlIOErr(XML_FROM_HTTP, XML_HTTP_UNKNOWN_HOST,
1310 "Failed to identify host in URI");
Owen Taylor3473f882001-02-23 17:55:21 +00001311 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001312 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001313 return(NULL);
1314 }
1315 if (proxy) {
1316 blen = strlen(ctxt->hostname) * 2 + 16;
1317 ret = xmlNanoHTTPConnectHost(proxy, proxyPort);
1318 }
1319 else {
1320 blen = strlen(ctxt->hostname);
1321 ret = xmlNanoHTTPConnectHost(ctxt->hostname, ctxt->port);
1322 }
1323 if (ret < 0) {
1324 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001325 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001326 return(NULL);
1327 }
1328 ctxt->fd = ret;
1329
Daniel Veillardf012a642001-07-23 19:10:52 +00001330 if (input == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00001331 ilen = 0;
Daniel Veillardf012a642001-07-23 19:10:52 +00001332 else
1333 blen += 36;
1334
Owen Taylor3473f882001-02-23 17:55:21 +00001335 if (headers != NULL)
Daniel Veillardf012a642001-07-23 19:10:52 +00001336 blen += strlen(headers) + 2;
Owen Taylor3473f882001-02-23 17:55:21 +00001337 if (contentType && *contentType)
1338 blen += strlen(*contentType) + 16;
Daniel Veillardf012a642001-07-23 19:10:52 +00001339 blen += strlen(method) + strlen(ctxt->path) + 24;
Daniel Veillard82cb3192003-10-29 13:39:15 +00001340 bp = (char*)xmlMallocAtomic(blen);
Daniel Veillardf012a642001-07-23 19:10:52 +00001341 if ( bp == NULL ) {
1342 xmlNanoHTTPFreeCtxt( ctxt );
Daniel Veillard2b0f8792003-10-10 19:36:36 +00001343 xmlHTTPErrMemory("allocating header buffer");
Daniel Veillardf012a642001-07-23 19:10:52 +00001344 return ( NULL );
1345 }
1346
1347 p = bp;
1348
Owen Taylor3473f882001-02-23 17:55:21 +00001349 if (proxy) {
1350 if (ctxt->port != 80) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001351 p += snprintf( p, blen - (p - bp), "%s http://%s:%d%s",
1352 method, ctxt->hostname,
Daniel Veillardf012a642001-07-23 19:10:52 +00001353 ctxt->port, ctxt->path );
Owen Taylor3473f882001-02-23 17:55:21 +00001354 }
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001355 else
1356 p += snprintf( p, blen - (p - bp), "%s http://%s%s", method,
Daniel Veillardf012a642001-07-23 19:10:52 +00001357 ctxt->hostname, ctxt->path);
Owen Taylor3473f882001-02-23 17:55:21 +00001358 }
1359 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001360 p += snprintf( p, blen - (p - bp), "%s %s", method, ctxt->path);
Daniel Veillardf012a642001-07-23 19:10:52 +00001361
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001362 p += snprintf( p, blen - (p - bp), " HTTP/1.0\r\nHost: %s\r\n",
1363 ctxt->hostname);
Daniel Veillardf012a642001-07-23 19:10:52 +00001364
1365 if (contentType != NULL && *contentType)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001366 p += snprintf(p, blen - (p - bp), "Content-Type: %s\r\n", *contentType);
Daniel Veillardf012a642001-07-23 19:10:52 +00001367
1368 if (headers != NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001369 p += snprintf( p, blen - (p - bp), "%s", headers );
Daniel Veillardf012a642001-07-23 19:10:52 +00001370
Owen Taylor3473f882001-02-23 17:55:21 +00001371 if (input != NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001372 snprintf(p, blen - (p - bp), "Content-Length: %d\r\n\r\n", ilen );
Owen Taylor3473f882001-02-23 17:55:21 +00001373 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001374 snprintf(p, blen - (p - bp), "\r\n");
Daniel Veillardf012a642001-07-23 19:10:52 +00001375
Owen Taylor3473f882001-02-23 17:55:21 +00001376#ifdef DEBUG_HTTP
1377 xmlGenericError(xmlGenericErrorContext,
1378 "-> %s%s", proxy? "(Proxy) " : "", bp);
1379 if ((blen -= strlen(bp)+1) < 0)
1380 xmlGenericError(xmlGenericErrorContext,
1381 "ERROR: overflowed buffer by %d bytes\n", -blen);
1382#endif
1383 ctxt->outptr = ctxt->out = bp;
1384 ctxt->state = XML_NANO_HTTP_WRITE;
Daniel Veillardf012a642001-07-23 19:10:52 +00001385 blen = strlen( ctxt->out );
Daniel Veillardf012a642001-07-23 19:10:52 +00001386#ifdef DEBUG_HTTP
William M. Brack78637da2003-07-31 14:47:38 +00001387 xmt_bytes = xmlNanoHTTPSend(ctxt, ctxt->out, blen );
Daniel Veillardf012a642001-07-23 19:10:52 +00001388 if ( xmt_bytes != blen )
1389 xmlGenericError( xmlGenericErrorContext,
1390 "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
1391 xmt_bytes, blen,
1392 "bytes of HTTP headers sent to host",
1393 ctxt->hostname );
William M. Brack78637da2003-07-31 14:47:38 +00001394#else
1395 xmlNanoHTTPSend(ctxt, ctxt->out, blen );
Daniel Veillardf012a642001-07-23 19:10:52 +00001396#endif
1397
1398 if ( input != NULL ) {
William M. Brack78637da2003-07-31 14:47:38 +00001399#ifdef DEBUG_HTTP
Daniel Veillardf012a642001-07-23 19:10:52 +00001400 xmt_bytes = xmlNanoHTTPSend( ctxt, input, ilen );
1401
Daniel Veillardf012a642001-07-23 19:10:52 +00001402 if ( xmt_bytes != ilen )
1403 xmlGenericError( xmlGenericErrorContext,
1404 "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
1405 xmt_bytes, ilen,
1406 "bytes of HTTP content sent to host",
1407 ctxt->hostname );
William M. Brack78637da2003-07-31 14:47:38 +00001408#else
1409 xmlNanoHTTPSend( ctxt, input, ilen );
Daniel Veillardf012a642001-07-23 19:10:52 +00001410#endif
1411 }
1412
Owen Taylor3473f882001-02-23 17:55:21 +00001413 ctxt->state = XML_NANO_HTTP_READ;
1414 head = 1;
1415
1416 while ((p = xmlNanoHTTPReadLine(ctxt)) != NULL) {
1417 if (head && (*p == 0)) {
1418 head = 0;
1419 ctxt->content = ctxt->inrptr;
1420 xmlFree(p);
1421 break;
1422 }
1423 xmlNanoHTTPScanAnswer(ctxt, p);
1424
1425#ifdef DEBUG_HTTP
1426 xmlGenericError(xmlGenericErrorContext, "<- %s\n", p);
1427#endif
1428 xmlFree(p);
1429 }
1430
1431 if ((ctxt->location != NULL) && (ctxt->returnValue >= 300) &&
1432 (ctxt->returnValue < 400)) {
1433#ifdef DEBUG_HTTP
1434 xmlGenericError(xmlGenericErrorContext,
1435 "\nRedirect to: %s\n", ctxt->location);
1436#endif
Daniel Veillardf012a642001-07-23 19:10:52 +00001437 while ( xmlNanoHTTPRecv(ctxt) > 0 ) ;
Owen Taylor3473f882001-02-23 17:55:21 +00001438 if (nbRedirects < XML_NANO_HTTP_MAX_REDIR) {
1439 nbRedirects++;
Daniel Veillard9403a042001-05-28 11:00:53 +00001440 if (redirURL != NULL)
1441 xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001442 redirURL = xmlMemStrdup(ctxt->location);
1443 xmlNanoHTTPFreeCtxt(ctxt);
1444 goto retry;
1445 }
1446 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001447 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001448#ifdef DEBUG_HTTP
1449 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardf012a642001-07-23 19:10:52 +00001450 "xmlNanoHTTPMethodRedir: Too many redirects, aborting ...\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001451#endif
1452 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001453 }
1454
1455 if (contentType != NULL) {
1456 if (ctxt->contentType != NULL)
1457 *contentType = xmlMemStrdup(ctxt->contentType);
1458 else
1459 *contentType = NULL;
1460 }
1461
Daniel Veillard9403a042001-05-28 11:00:53 +00001462 if ((redir != NULL) && (redirURL != NULL)) {
1463 *redir = redirURL;
1464 } else {
1465 if (redirURL != NULL)
1466 xmlFree(redirURL);
1467 if (redir != NULL)
1468 *redir = NULL;
1469 }
1470
Owen Taylor3473f882001-02-23 17:55:21 +00001471#ifdef DEBUG_HTTP
1472 if (ctxt->contentType != NULL)
1473 xmlGenericError(xmlGenericErrorContext,
1474 "\nCode %d, content-type '%s'\n\n",
1475 ctxt->returnValue, ctxt->contentType);
1476 else
1477 xmlGenericError(xmlGenericErrorContext,
1478 "\nCode %d, no content-type\n\n",
1479 ctxt->returnValue);
1480#endif
1481
1482 return((void *) ctxt);
1483}
1484
1485/**
Daniel Veillard9403a042001-05-28 11:00:53 +00001486 * xmlNanoHTTPMethod:
1487 * @URL: The URL to load
1488 * @method: the HTTP method to use
1489 * @input: the input string if any
1490 * @contentType: the Content-Type information IN and OUT
1491 * @headers: the extra headers
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001492 * @ilen: input length
Daniel Veillard9403a042001-05-28 11:00:53 +00001493 *
1494 * This function try to open a connection to the indicated resource
1495 * via HTTP using the given @method, adding the given extra headers
1496 * and the input buffer for the request content.
1497 *
1498 * Returns NULL in case of failure, otherwise a request handler.
1499 * The contentType, if provided must be freed by the caller
1500 */
1501
1502void*
1503xmlNanoHTTPMethod(const char *URL, const char *method, const char *input,
Daniel Veillardf012a642001-07-23 19:10:52 +00001504 char **contentType, const char *headers, int ilen) {
Daniel Veillard9403a042001-05-28 11:00:53 +00001505 return(xmlNanoHTTPMethodRedir(URL, method, input, contentType,
Daniel Veillardf012a642001-07-23 19:10:52 +00001506 NULL, headers, ilen));
Daniel Veillard9403a042001-05-28 11:00:53 +00001507}
1508
1509/**
Owen Taylor3473f882001-02-23 17:55:21 +00001510 * xmlNanoHTTPFetch:
1511 * @URL: The URL to load
1512 * @filename: the filename where the content should be saved
1513 * @contentType: if available the Content-Type information will be
1514 * returned at that location
1515 *
1516 * This function try to fetch the indicated resource via HTTP GET
1517 * and save it's content in the file.
1518 *
1519 * Returns -1 in case of failure, 0 incase of success. The contentType,
1520 * if provided must be freed by the caller
1521 */
1522int
1523xmlNanoHTTPFetch(const char *URL, const char *filename, char **contentType) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001524 void *ctxt = NULL;
1525 char *buf = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001526 int fd;
1527 int len;
1528
1529 ctxt = xmlNanoHTTPOpen(URL, contentType);
1530 if (ctxt == NULL) return(-1);
1531
1532 if (!strcmp(filename, "-"))
1533 fd = 0;
1534 else {
1535 fd = open(filename, O_CREAT | O_WRONLY, 00644);
1536 if (fd < 0) {
1537 xmlNanoHTTPClose(ctxt);
1538 if ((contentType != NULL) && (*contentType != NULL)) {
1539 xmlFree(*contentType);
1540 *contentType = NULL;
1541 }
1542 return(-1);
1543 }
1544 }
1545
Daniel Veillardf012a642001-07-23 19:10:52 +00001546 xmlNanoHTTPFetchContent( ctxt, &buf, &len );
1547 if ( len > 0 ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001548 write(fd, buf, len);
1549 }
1550
1551 xmlNanoHTTPClose(ctxt);
1552 close(fd);
1553 return(0);
1554}
1555
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001556#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001557/**
1558 * xmlNanoHTTPSave:
1559 * @ctxt: the HTTP context
1560 * @filename: the filename where the content should be saved
1561 *
1562 * This function saves the output of the HTTP transaction to a file
1563 * It closes and free the context at the end
1564 *
1565 * Returns -1 in case of failure, 0 incase of success.
1566 */
1567int
1568xmlNanoHTTPSave(void *ctxt, const char *filename) {
Daniel Veillarde3924972001-07-25 20:25:21 +00001569 char *buf = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001570 int fd;
1571 int len;
1572
1573 if (ctxt == NULL) return(-1);
1574
1575 if (!strcmp(filename, "-"))
1576 fd = 0;
1577 else {
1578 fd = open(filename, O_CREAT | O_WRONLY);
1579 if (fd < 0) {
1580 xmlNanoHTTPClose(ctxt);
1581 return(-1);
1582 }
1583 }
1584
Daniel Veillardf012a642001-07-23 19:10:52 +00001585 xmlNanoHTTPFetchContent( ctxt, &buf, &len );
1586 if ( len > 0 ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001587 write(fd, buf, len);
1588 }
1589
1590 xmlNanoHTTPClose(ctxt);
1591 return(0);
1592}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001593#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001594
1595/**
1596 * xmlNanoHTTPReturnCode:
1597 * @ctx: the HTTP context
1598 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001599 * Get the latest HTTP return code received
1600 *
Owen Taylor3473f882001-02-23 17:55:21 +00001601 * Returns the HTTP return code for the request.
1602 */
1603int
1604xmlNanoHTTPReturnCode(void *ctx) {
1605 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1606
1607 if (ctxt == NULL) return(-1);
1608
1609 return(ctxt->returnValue);
1610}
1611
1612/**
1613 * xmlNanoHTTPAuthHeader:
1614 * @ctx: the HTTP context
1615 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001616 * Get the authentication header of an HTTP context
1617 *
Owen Taylor3473f882001-02-23 17:55:21 +00001618 * Returns the stashed value of the WWW-Authenticate or Proxy-Authenticate
1619 * header.
1620 */
1621const char *
1622xmlNanoHTTPAuthHeader(void *ctx) {
1623 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1624
1625 if (ctxt == NULL) return(NULL);
1626
1627 return(ctxt->authHeader);
1628}
1629
Daniel Veillardf012a642001-07-23 19:10:52 +00001630/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00001631 * xmlNanoHTTPContentLength:
Daniel Veillardf012a642001-07-23 19:10:52 +00001632 * @ctx: the HTTP context
1633 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001634 * Provides the specified content length from the HTTP header.
1635 *
Daniel Veillardf012a642001-07-23 19:10:52 +00001636 * Return the specified content length from the HTTP header. Note that
1637 * a value of -1 indicates that the content length element was not included in
1638 * the response header.
1639 */
1640int
1641xmlNanoHTTPContentLength( void * ctx ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001642 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillardf012a642001-07-23 19:10:52 +00001643
1644 return ( ( ctxt == NULL ) ? -1 : ctxt->ContentLength );
1645}
1646
1647/**
Daniel Veillard847332a2003-10-18 11:29:40 +00001648 * xmlNanoHTTPRedir:
1649 * @ctx: the HTTP context
1650 *
1651 * Provides the specified redirection URL if available from the HTTP header.
1652 *
1653 * Return the specified redirection URL or NULL if not redirected.
1654 */
1655const char *
1656xmlNanoHTTPRedir( void * ctx ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001657 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillard847332a2003-10-18 11:29:40 +00001658
1659 return ( ( ctxt == NULL ) ? NULL : ctxt->location );
1660}
1661
1662/**
1663 * xmlNanoHTTPEncoding:
1664 * @ctx: the HTTP context
1665 *
1666 * Provides the specified encoding if specified in the HTTP headers.
1667 *
1668 * Return the specified encoding or NULL if not available
1669 */
1670const char *
1671xmlNanoHTTPEncoding( void * ctx ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001672 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillard847332a2003-10-18 11:29:40 +00001673
1674 return ( ( ctxt == NULL ) ? NULL : ctxt->encoding );
1675}
1676
1677/**
Daniel Veillarda840b692003-10-19 13:35:37 +00001678 * xmlNanoHTTPMimeType:
1679 * @ctx: the HTTP context
1680 *
1681 * Provides the specified Mime-Type if specified in the HTTP headers.
1682 *
1683 * Return the specified Mime-Type or NULL if not available
1684 */
1685const char *
1686xmlNanoHTTPMimeType( void * ctx ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001687 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillarda840b692003-10-19 13:35:37 +00001688
1689 return ( ( ctxt == NULL ) ? NULL : ctxt->mimeType );
1690}
1691
1692/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00001693 * xmlNanoHTTPFetchContent:
Daniel Veillardf012a642001-07-23 19:10:52 +00001694 * @ctx: the HTTP context
1695 * @ptr: pointer to set to the content buffer.
1696 * @len: integer pointer to hold the length of the content
1697 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001698 * Check if all the content was read
1699 *
Daniel Veillardf012a642001-07-23 19:10:52 +00001700 * Returns 0 if all the content was read and available, returns
1701 * -1 if received content length was less than specified or an error
1702 * occurred.
1703 */
1704int
1705xmlNanoHTTPFetchContent( void * ctx, char ** ptr, int * len ) {
Daniel Veillard82cb3192003-10-29 13:39:15 +00001706 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx;
Daniel Veillardf012a642001-07-23 19:10:52 +00001707
1708 int rc = 0;
1709 int cur_lgth;
1710 int rcvd_lgth;
1711 int dummy_int;
1712 char * dummy_ptr = NULL;
1713
1714 /* Dummy up return input parameters if not provided */
1715
1716 if ( len == NULL )
1717 len = &dummy_int;
1718
1719 if ( ptr == NULL )
1720 ptr = &dummy_ptr;
1721
1722 /* But can't work without the context pointer */
1723
1724 if ( ( ctxt == NULL ) || ( ctxt->content == NULL ) ) {
1725 *len = 0;
1726 *ptr = NULL;
1727 return ( -1 );
1728 }
1729
1730 rcvd_lgth = ctxt->inptr - ctxt->content;
1731
1732 while ( (cur_lgth = xmlNanoHTTPRecv( ctxt )) > 0 ) {
1733
1734 rcvd_lgth += cur_lgth;
1735 if ( (ctxt->ContentLength > 0) && (rcvd_lgth >= ctxt->ContentLength) )
1736 break;
1737 }
1738
1739 *ptr = ctxt->content;
1740 *len = rcvd_lgth;
1741
1742 if ( ( ctxt->ContentLength > 0 ) && ( rcvd_lgth < ctxt->ContentLength ) )
1743 rc = -1;
1744 else if ( rcvd_lgth == 0 )
1745 rc = -1;
1746
1747 return ( rc );
1748}
1749
Owen Taylor3473f882001-02-23 17:55:21 +00001750#ifdef STANDALONE
1751int main(int argc, char **argv) {
1752 char *contentType = NULL;
1753
1754 if (argv[1] != NULL) {
1755 if (argv[2] != NULL)
1756 xmlNanoHTTPFetch(argv[1], argv[2], &contentType);
1757 else
1758 xmlNanoHTTPFetch(argv[1], "-", &contentType);
1759 if (contentType != NULL) xmlFree(contentType);
1760 } else {
1761 xmlGenericError(xmlGenericErrorContext,
1762 "%s: minimal HTTP GET implementation\n", argv[0]);
1763 xmlGenericError(xmlGenericErrorContext,
1764 "\tusage %s [ URL [ filename ] ]\n", argv[0]);
1765 }
1766 xmlNanoHTTPCleanup();
1767 xmlMemoryDump();
1768 return(0);
1769}
1770#endif /* STANDALONE */
1771#else /* !LIBXML_HTTP_ENABLED */
1772#ifdef STANDALONE
1773#include <stdio.h>
1774int main(int argc, char **argv) {
1775 xmlGenericError(xmlGenericErrorContext,
1776 "%s : HTTP support not compiled in\n", argv[0]);
1777 return(0);
1778}
1779#endif /* STANDALONE */
1780#endif /* LIBXML_HTTP_ENABLED */