blob: c16cf0bcfb634e0c5a106b635d4f9185ed39ede9 [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 Veillardd0463562001-10-13 09:15:48 +000076#include <libxml/globals.h>
Daniel Veillardf012a642001-07-23 19:10:52 +000077#include <libxml/xmlerror.h>
Owen Taylor3473f882001-02-23 17:55:21 +000078#include <libxml/xmlmemory.h>
79#include <libxml/parser.h> /* for xmlStr(n)casecmp() */
80#include <libxml/nanohttp.h>
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000081#include <libxml/globals.h>
Daniel Veillard8efff672002-12-04 11:44:48 +000082#include <libxml/uri.h>
Daniel Veillardde2a67b2003-06-21 14:20:04 +000083#include <config.h>
Owen Taylor3473f882001-02-23 17:55:21 +000084
85/**
86 * A couple portability macros
87 */
88#ifndef _WINSOCKAPI_
89#define closesocket(s) close(s)
90#define SOCKET int
91#endif
92
Daniel Veillard75be0132002-03-13 10:03:35 +000093#ifndef SOCKLEN_T
94#define SOCKLEN_T unsigned int
95#endif
96#ifndef SOCKET
97#define SOCKET int
98#endif
Daniel Veillardf012a642001-07-23 19:10:52 +000099
Owen Taylor3473f882001-02-23 17:55:21 +0000100#ifdef STANDALONE
101#define DEBUG_HTTP
102#define xmlStrncasecmp(a, b, n) strncasecmp((char *)a, (char *)b, n)
103#define xmlStrcasecmpi(a, b) strcasecmp((char *)a, (char *)b)
104#endif
105
106#define XML_NANO_HTTP_MAX_REDIR 10
107
108#define XML_NANO_HTTP_CHUNK 4096
109
110#define XML_NANO_HTTP_CLOSED 0
111#define XML_NANO_HTTP_WRITE 1
112#define XML_NANO_HTTP_READ 2
113#define XML_NANO_HTTP_NONE 4
114
115typedef struct xmlNanoHTTPCtxt {
116 char *protocol; /* the protocol name */
117 char *hostname; /* the host name */
118 int port; /* the port */
119 char *path; /* the path within the URL */
120 SOCKET fd; /* the file descriptor for the socket */
121 int state; /* WRITE / READ / CLOSED */
122 char *out; /* buffer sent (zero terminated) */
123 char *outptr; /* index within the buffer sent */
124 char *in; /* the receiving buffer */
125 char *content; /* the start of the content */
126 char *inptr; /* the next byte to read from network */
127 char *inrptr; /* the next byte to give back to the client */
128 int inlen; /* len of the input buffer */
129 int last; /* return code for last operation */
130 int returnValue; /* the protocol return value */
Daniel Veillardf012a642001-07-23 19:10:52 +0000131 int ContentLength; /* specified content length from HTTP header */
Owen Taylor3473f882001-02-23 17:55:21 +0000132 char *contentType; /* the MIME type for the input */
133 char *location; /* the new URL in case of redirect */
134 char *authHeader; /* contents of {WWW,Proxy}-Authenticate header */
135} xmlNanoHTTPCtxt, *xmlNanoHTTPCtxtPtr;
136
137static int initialized = 0;
138static char *proxy = NULL; /* the proxy name if any */
139static int proxyPort; /* the proxy port if any */
140static unsigned int timeout = 60;/* the select() timeout in seconds */
141
Daniel Veillardf012a642001-07-23 19:10:52 +0000142int xmlNanoHTTPFetchContent( void * ctx, char ** ptr, int * len );
143int xmlNanoHTTPContentLength( void * ctx );
144
Owen Taylor3473f882001-02-23 17:55:21 +0000145/**
146 * A portability function
147 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000148static int socket_errno(void) {
Owen Taylor3473f882001-02-23 17:55:21 +0000149#ifdef _WINSOCKAPI_
150 return(WSAGetLastError());
151#else
152 return(errno);
153#endif
154}
155
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000156#ifdef SUPPORT_IP6
157static int have_ipv6() {
158 int s;
159
160 s = socket (AF_INET6, SOCK_STREAM, 0);
161 if (s != -1) {
162 close (s);
163 return (1);
164 }
165 return (0);
166}
167#endif
168
Owen Taylor3473f882001-02-23 17:55:21 +0000169/**
170 * xmlNanoHTTPInit:
171 *
172 * Initialize the HTTP protocol layer.
173 * Currently it just checks for proxy informations
174 */
175
176void
177xmlNanoHTTPInit(void) {
178 const char *env;
179#ifdef _WINSOCKAPI_
180 WSADATA wsaData;
181#endif
182
183 if (initialized)
184 return;
185
186#ifdef _WINSOCKAPI_
187 if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0)
188 return;
189#endif
190
191 if (proxy == NULL) {
192 proxyPort = 80;
193 env = getenv("no_proxy");
194 if (env != NULL)
195 goto done;
196 env = getenv("http_proxy");
197 if (env != NULL) {
198 xmlNanoHTTPScanProxy(env);
199 goto done;
200 }
201 env = getenv("HTTP_PROXY");
202 if (env != NULL) {
203 xmlNanoHTTPScanProxy(env);
204 goto done;
205 }
206 }
207done:
208 initialized = 1;
209}
210
211/**
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000212 * xmlNanoHTTPCleanup:
Owen Taylor3473f882001-02-23 17:55:21 +0000213 *
214 * Cleanup the HTTP protocol layer.
215 */
216
217void
218xmlNanoHTTPCleanup(void) {
219 if (proxy != NULL)
220 xmlFree(proxy);
221#ifdef _WINSOCKAPI_
222 if (initialized)
223 WSACleanup();
224#endif
225 initialized = 0;
226 return;
227}
228
229/**
Owen Taylor3473f882001-02-23 17:55:21 +0000230 * xmlNanoHTTPScanURL:
231 * @ctxt: an HTTP context
232 * @URL: The URL used to initialize the context
233 *
234 * (Re)Initialize an HTTP context by parsing the URL and finding
235 * the protocol host port and path it indicates.
236 */
237
238static void
239xmlNanoHTTPScanURL(xmlNanoHTTPCtxtPtr ctxt, const char *URL) {
240 const char *cur = URL;
241 char buf[4096];
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000242 int indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000243 int port = 0;
244
245 if (ctxt->protocol != NULL) {
246 xmlFree(ctxt->protocol);
247 ctxt->protocol = NULL;
248 }
249 if (ctxt->hostname != NULL) {
250 xmlFree(ctxt->hostname);
251 ctxt->hostname = NULL;
252 }
253 if (ctxt->path != NULL) {
254 xmlFree(ctxt->path);
255 ctxt->path = NULL;
256 }
257 if (URL == NULL) return;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000258 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000259 while (*cur != 0) {
260 if ((cur[0] == ':') && (cur[1] == '/') && (cur[2] == '/')) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000261 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000262 ctxt->protocol = xmlMemStrdup(buf);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000263 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000264 cur += 3;
265 break;
266 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000267 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000268 }
269 if (*cur == 0) return;
270
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000271 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000272 while (1) {
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000273 if ((strchr (cur, '[') && !strchr (cur, ']')) ||
274 (!strchr (cur, '[') && strchr (cur, ']'))) {
275 xmlGenericError (xmlGenericErrorContext, "\nxmlNanoHTTPScanURL: %s",
276 "Syntax Error\n");
277 return;
278 }
279
280 if (cur[0] == '[') {
281 cur++;
282 while (cur[0] != ']')
283 buf[indx++] = *cur++;
284
285 if (!strchr (buf, ':')) {
286 xmlGenericError (xmlGenericErrorContext, "\nxmlNanoHTTPScanURL: %s",
287 "Use [IPv6]/IPv4 format\n");
288 return;
289 }
290
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000291 buf[indx] = 0;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000292 ctxt->hostname = xmlMemStrdup (buf);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000293 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000294 cur += 1;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000295 if (cur[0] == ':') {
Owen Taylor3473f882001-02-23 17:55:21 +0000296 cur++;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000297 while (*cur >= '0' && *cur <= '9') {
298 port *= 10;
299 port += *cur - '0';
300 cur++;
301 }
302
303 if (port != 0) ctxt->port = port;
304 while ((cur[0] != '/') && (*cur != 0))
305 cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000306 }
Owen Taylor3473f882001-02-23 17:55:21 +0000307 break;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000308 }
309 else {
310 if (cur[0] == ':') {
311 buf[indx] = 0;
312 ctxt->hostname = xmlMemStrdup (buf);
313 indx = 0;
314 cur += 1;
315 while ((*cur >= '0') && (*cur <= '9')) {
316 port *= 10;
317 port += *cur - '0';
318 cur++;
319 }
320 if (port != 0) ctxt->port = port;
321 while ((cur[0] != '/') && (*cur != 0))
322 cur++;
323 break;
324 }
325 if ((*cur == '/') || (*cur == 0)) {
326 buf[indx] = 0;
327 ctxt->hostname = xmlMemStrdup (buf);
328 indx = 0;
329 break;
330 }
Owen Taylor3473f882001-02-23 17:55:21 +0000331 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000332 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000333 }
334 if (*cur == 0)
335 ctxt->path = xmlMemStrdup("/");
336 else {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000337 indx = 0;
338 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000339 while (*cur != 0)
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000340 buf[indx++] = *cur++;
341 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000342 ctxt->path = xmlMemStrdup(buf);
343 }
344}
345
346/**
347 * xmlNanoHTTPScanProxy:
348 * @URL: The proxy URL used to initialize the proxy context
349 *
350 * (Re)Initialize the HTTP Proxy context by parsing the URL and finding
351 * the protocol host port it indicates.
352 * Should be like http://myproxy/ or http://myproxy:3128/
353 * A NULL URL cleans up proxy informations.
354 */
355
356void
357xmlNanoHTTPScanProxy(const char *URL) {
358 const char *cur = URL;
359 char buf[4096];
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000360 int indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000361 int port = 0;
362
363 if (proxy != NULL) {
364 xmlFree(proxy);
365 proxy = NULL;
366 }
367 if (proxyPort != 0) {
368 proxyPort = 0;
369 }
370#ifdef DEBUG_HTTP
371 if (URL == NULL)
372 xmlGenericError(xmlGenericErrorContext,
373 "Removing HTTP proxy info\n");
374 else
375 xmlGenericError(xmlGenericErrorContext,
376 "Using HTTP proxy %s\n", URL);
377#endif
378 if (URL == NULL) return;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000379 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000380 while (*cur != 0) {
381 if ((cur[0] == ':') && (cur[1] == '/') && (cur[2] == '/')) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000382 buf[indx] = 0;
383 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000384 cur += 3;
385 break;
386 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000387 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000388 }
389 if (*cur == 0) return;
390
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000391 buf[indx] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000392 while (1) {
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000393 if ((strchr (cur, '[') && !strchr (cur, ']')) ||
394 (!strchr (cur, '[') && strchr (cur, ']'))) {
395 xmlGenericError (xmlGenericErrorContext, "\nxmlNanoHTTPScanProxy: %s",
396 "Syntax error\n");
397 return;
398 }
399
400 if (cur[0] == '[') {
401 cur++;
402 while (cur[0] != ']')
403 buf[indx++] = *cur++;
404
405 if (!strchr (buf, ':')) {
406 xmlGenericError (xmlGenericErrorContext, "\nxmlNanoHTTPScanProxy: %s",
407 "Use [IPv6]/IPv4 format\n");
408 return;
409 }
410
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000411 buf[indx] = 0;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000412 proxy = xmlMemStrdup (buf);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000413 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000414 cur += 1;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000415 if (cur[0] == ':') {
Owen Taylor3473f882001-02-23 17:55:21 +0000416 cur++;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000417 while (*cur >= '0' && *cur <= '9') {
418 port *= 10;
419 port += *cur - '0';
420 cur++;
421 }
422
423 if (port != 0) proxyPort = port;
424 while ((cur[0] != '/') && (*cur != 0))
425 cur ++;
426 }
Owen Taylor3473f882001-02-23 17:55:21 +0000427 break;
428 }
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000429 else {
430 if (cur[0] == ':') {
431 buf[indx] = 0;
432 proxy = xmlMemStrdup (buf);
433 indx = 0;
434 cur += 1;
435 while ((*cur >= '0') && (*cur <= '9')) {
436 port *= 10;
437 port += *cur - '0';
438 cur++;
439 }
440 if (port != 0) proxyPort = port;
441 while ((cur[0] != '/') && (*cur != 0))
442 cur++;
443 break;
444 }
445 if ((*cur == '/') || (*cur == 0)) {
446 buf[indx] = 0;
447 proxy = xmlMemStrdup (buf);
448 indx = 0;
449 break;
450 }
Owen Taylor3473f882001-02-23 17:55:21 +0000451 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000452 buf[indx++] = *cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000453 }
454}
455
456/**
457 * xmlNanoHTTPNewCtxt:
458 * @URL: The URL used to initialize the context
459 *
460 * Allocate and initialize a new HTTP context.
461 *
462 * Returns an HTTP context or NULL in case of error.
463 */
464
465static xmlNanoHTTPCtxtPtr
466xmlNanoHTTPNewCtxt(const char *URL) {
467 xmlNanoHTTPCtxtPtr ret;
468
469 ret = (xmlNanoHTTPCtxtPtr) xmlMalloc(sizeof(xmlNanoHTTPCtxt));
470 if (ret == NULL) return(NULL);
471
472 memset(ret, 0, sizeof(xmlNanoHTTPCtxt));
473 ret->port = 80;
474 ret->returnValue = 0;
475 ret->fd = -1;
Daniel Veillardf012a642001-07-23 19:10:52 +0000476 ret->ContentLength = -1;
Owen Taylor3473f882001-02-23 17:55:21 +0000477
Daniel Veillardcacbe5d2003-01-10 16:09:51 +0000478 xmlNanoHTTPScanURL(ret, URL);
Owen Taylor3473f882001-02-23 17:55:21 +0000479
480 return(ret);
481}
482
483/**
484 * xmlNanoHTTPFreeCtxt:
485 * @ctxt: an HTTP context
486 *
487 * Frees the context after closing the connection.
488 */
489
490static void
491xmlNanoHTTPFreeCtxt(xmlNanoHTTPCtxtPtr ctxt) {
492 if (ctxt == NULL) return;
493 if (ctxt->hostname != NULL) xmlFree(ctxt->hostname);
494 if (ctxt->protocol != NULL) xmlFree(ctxt->protocol);
495 if (ctxt->path != NULL) xmlFree(ctxt->path);
496 if (ctxt->out != NULL) xmlFree(ctxt->out);
497 if (ctxt->in != NULL) xmlFree(ctxt->in);
498 if (ctxt->contentType != NULL) xmlFree(ctxt->contentType);
499 if (ctxt->location != NULL) xmlFree(ctxt->location);
500 if (ctxt->authHeader != NULL) xmlFree(ctxt->authHeader);
501 ctxt->state = XML_NANO_HTTP_NONE;
502 if (ctxt->fd >= 0) closesocket(ctxt->fd);
503 ctxt->fd = -1;
504 xmlFree(ctxt);
505}
506
507/**
508 * xmlNanoHTTPSend:
509 * @ctxt: an HTTP context
510 *
511 * Send the input needed to initiate the processing on the server side
Daniel Veillardf012a642001-07-23 19:10:52 +0000512 * Returns number of bytes sent or -1 on error.
Owen Taylor3473f882001-02-23 17:55:21 +0000513 */
514
Daniel Veillardf012a642001-07-23 19:10:52 +0000515static int
516xmlNanoHTTPSend(xmlNanoHTTPCtxtPtr ctxt, const char * xmt_ptr, int outlen) {
517
518 int total_sent = 0;
519
520 if ( (ctxt->state & XML_NANO_HTTP_WRITE) && (xmt_ptr != NULL ) ) {
521 while (total_sent < outlen) {
522 int nsent = send(ctxt->fd, xmt_ptr + total_sent,
523 outlen - total_sent, 0);
Owen Taylor3473f882001-02-23 17:55:21 +0000524 if (nsent>0)
525 total_sent += nsent;
Daniel Veillardf012a642001-07-23 19:10:52 +0000526 else if ( ( nsent == -1 ) &&
Daniel Veillardba6db032001-07-31 16:25:45 +0000527#if defined(EAGAIN) && EAGAIN != EWOULDBLOCK
Daniel Veillardf012a642001-07-23 19:10:52 +0000528 ( socket_errno( ) != EAGAIN ) &&
Daniel Veillardba6db032001-07-31 16:25:45 +0000529#endif
Daniel Veillardf012a642001-07-23 19:10:52 +0000530 ( socket_errno( ) != EWOULDBLOCK ) ) {
531 xmlGenericError( xmlGenericErrorContext,
532 "xmlNanoHTTPSend error: %s",
533 strerror( socket_errno( ) ) );
534
535 if ( total_sent == 0 )
536 total_sent = -1;
537 break;
538 }
539 else {
540 /*
541 ** No data sent
542 ** Since non-blocking sockets are used, wait for
543 ** socket to be writable or default timeout prior
544 ** to retrying.
545 */
546
547 struct timeval tv;
548 fd_set wfd;
549
550 tv.tv_sec = timeout;
551 tv.tv_usec = 0;
552 FD_ZERO( &wfd );
553 FD_SET( ctxt->fd, &wfd );
554 (void)select( ctxt->fd + 1, NULL, &wfd, NULL, &tv );
555 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000556 }
Owen Taylor3473f882001-02-23 17:55:21 +0000557 }
Daniel Veillardf012a642001-07-23 19:10:52 +0000558
559 return total_sent;
Owen Taylor3473f882001-02-23 17:55:21 +0000560}
561
562/**
563 * xmlNanoHTTPRecv:
564 * @ctxt: an HTTP context
565 *
566 * Read information coming from the HTTP connection.
567 * This is a blocking call (but it blocks in select(), not read()).
568 *
569 * Returns the number of byte read or -1 in case of error.
570 */
571
572static int
573xmlNanoHTTPRecv(xmlNanoHTTPCtxtPtr ctxt) {
574 fd_set rfd;
575 struct timeval tv;
576
577
578 while (ctxt->state & XML_NANO_HTTP_READ) {
579 if (ctxt->in == NULL) {
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000580 ctxt->in = (char *) xmlMallocAtomic(65000 * sizeof(char));
Owen Taylor3473f882001-02-23 17:55:21 +0000581 if (ctxt->in == NULL) {
582 ctxt->last = -1;
Daniel Veillardf012a642001-07-23 19:10:52 +0000583 xmlGenericError( xmlGenericErrorContext,
584 "xmlNanoHTTPRecv: Error allocating input memory." );
Owen Taylor3473f882001-02-23 17:55:21 +0000585 return(-1);
586 }
587 ctxt->inlen = 65000;
588 ctxt->inptr = ctxt->content = ctxt->inrptr = ctxt->in;
589 }
590 if (ctxt->inrptr > ctxt->in + XML_NANO_HTTP_CHUNK) {
591 int delta = ctxt->inrptr - ctxt->in;
592 int len = ctxt->inptr - ctxt->inrptr;
593
594 memmove(ctxt->in, ctxt->inrptr, len);
595 ctxt->inrptr -= delta;
596 ctxt->content -= delta;
597 ctxt->inptr -= delta;
598 }
599 if ((ctxt->in + ctxt->inlen) < (ctxt->inptr + XML_NANO_HTTP_CHUNK)) {
600 int d_inptr = ctxt->inptr - ctxt->in;
601 int d_content = ctxt->content - ctxt->in;
602 int d_inrptr = ctxt->inrptr - ctxt->in;
Daniel Veillardf012a642001-07-23 19:10:52 +0000603 char * tmp_ptr = ctxt->in;
Owen Taylor3473f882001-02-23 17:55:21 +0000604
605 ctxt->inlen *= 2;
Daniel Veillardf012a642001-07-23 19:10:52 +0000606 ctxt->in = (char *) xmlRealloc(tmp_ptr, ctxt->inlen);
Owen Taylor3473f882001-02-23 17:55:21 +0000607 if (ctxt->in == NULL) {
Daniel Veillardf012a642001-07-23 19:10:52 +0000608 xmlGenericError( xmlGenericErrorContext,
609 "xmlNanoHTTPRecv: %s %d bytes.",
610 "Failed to realloc input buffer to",
611 ctxt->inlen );
612 xmlFree( tmp_ptr );
Owen Taylor3473f882001-02-23 17:55:21 +0000613 ctxt->last = -1;
614 return(-1);
615 }
616 ctxt->inptr = ctxt->in + d_inptr;
617 ctxt->content = ctxt->in + d_content;
618 ctxt->inrptr = ctxt->in + d_inrptr;
619 }
620 ctxt->last = recv(ctxt->fd, ctxt->inptr, XML_NANO_HTTP_CHUNK, 0);
621 if (ctxt->last > 0) {
622 ctxt->inptr += ctxt->last;
623 return(ctxt->last);
624 }
625 if (ctxt->last == 0) {
626 return(0);
627 }
628 if (ctxt->last == -1) {
629 switch (socket_errno()) {
630 case EINPROGRESS:
631 case EWOULDBLOCK:
632#if defined(EAGAIN) && EAGAIN != EWOULDBLOCK
633 case EAGAIN:
634#endif
635 break;
Daniel Veillardf012a642001-07-23 19:10:52 +0000636
637 case ECONNRESET:
638 case ESHUTDOWN:
639 return ( 0 );
640
Owen Taylor3473f882001-02-23 17:55:21 +0000641 default:
Daniel Veillardf012a642001-07-23 19:10:52 +0000642 xmlGenericError( xmlGenericErrorContext,
643 "xmlNanoHTTPRecv: recv( ) failure - %s",
644 strerror( socket_errno( ) ) );
645 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +0000646 }
647 }
648
649 tv.tv_sec = timeout;
650 tv.tv_usec = 0;
651 FD_ZERO(&rfd);
652 FD_SET(ctxt->fd, &rfd);
653
Daniel Veillard50f34372001-08-03 12:06:36 +0000654 if ( (select(ctxt->fd+1, &rfd, NULL, NULL, &tv)<1)
655#if defined(EINTR)
656 && (errno != EINTR)
657#endif
658 )
Owen Taylor3473f882001-02-23 17:55:21 +0000659 return(0);
660 }
661 return(0);
662}
663
664/**
665 * xmlNanoHTTPReadLine:
666 * @ctxt: an HTTP context
667 *
668 * Read one line in the HTTP server output, usually for extracting
669 * the HTTP protocol informations from the answer header.
670 *
671 * Returns a newly allocated string with a copy of the line, or NULL
672 * which indicate the end of the input.
673 */
674
675static char *
676xmlNanoHTTPReadLine(xmlNanoHTTPCtxtPtr ctxt) {
677 char buf[4096];
678 char *bp = buf;
Daniel Veillardf012a642001-07-23 19:10:52 +0000679 int rc;
Owen Taylor3473f882001-02-23 17:55:21 +0000680
681 while (bp - buf < 4095) {
682 if (ctxt->inrptr == ctxt->inptr) {
Daniel Veillardf012a642001-07-23 19:10:52 +0000683 if ( (rc = xmlNanoHTTPRecv(ctxt)) == 0) {
Owen Taylor3473f882001-02-23 17:55:21 +0000684 if (bp == buf)
685 return(NULL);
686 else
687 *bp = 0;
688 return(xmlMemStrdup(buf));
689 }
Daniel Veillardf012a642001-07-23 19:10:52 +0000690 else if ( rc == -1 ) {
691 return ( NULL );
692 }
Owen Taylor3473f882001-02-23 17:55:21 +0000693 }
694 *bp = *ctxt->inrptr++;
695 if (*bp == '\n') {
696 *bp = 0;
697 return(xmlMemStrdup(buf));
698 }
699 if (*bp != '\r')
700 bp++;
701 }
702 buf[4095] = 0;
703 return(xmlMemStrdup(buf));
704}
705
706
707/**
708 * xmlNanoHTTPScanAnswer:
709 * @ctxt: an HTTP context
710 * @line: an HTTP header line
711 *
712 * Try to extract useful informations from the server answer.
713 * We currently parse and process:
714 * - The HTTP revision/ return code
715 * - The Content-Type
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000716 * - The Location for redirect processing.
Owen Taylor3473f882001-02-23 17:55:21 +0000717 *
718 * Returns -1 in case of failure, the file descriptor number otherwise
719 */
720
721static void
722xmlNanoHTTPScanAnswer(xmlNanoHTTPCtxtPtr ctxt, const char *line) {
723 const char *cur = line;
724
725 if (line == NULL) return;
726
727 if (!strncmp(line, "HTTP/", 5)) {
728 int version = 0;
729 int ret = 0;
730
731 cur += 5;
732 while ((*cur >= '0') && (*cur <= '9')) {
733 version *= 10;
734 version += *cur - '0';
735 cur++;
736 }
737 if (*cur == '.') {
738 cur++;
739 if ((*cur >= '0') && (*cur <= '9')) {
740 version *= 10;
741 version += *cur - '0';
742 cur++;
743 }
744 while ((*cur >= '0') && (*cur <= '9'))
745 cur++;
746 } else
747 version *= 10;
748 if ((*cur != ' ') && (*cur != '\t')) return;
749 while ((*cur == ' ') || (*cur == '\t')) cur++;
750 if ((*cur < '0') || (*cur > '9')) return;
751 while ((*cur >= '0') && (*cur <= '9')) {
752 ret *= 10;
753 ret += *cur - '0';
754 cur++;
755 }
756 if ((*cur != 0) && (*cur != ' ') && (*cur != '\t')) return;
757 ctxt->returnValue = ret;
758 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Content-Type:", 13)) {
759 cur += 13;
760 while ((*cur == ' ') || (*cur == '\t')) cur++;
761 if (ctxt->contentType != NULL)
762 xmlFree(ctxt->contentType);
763 ctxt->contentType = xmlMemStrdup(cur);
764 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"ContentType:", 12)) {
765 cur += 12;
766 if (ctxt->contentType != NULL) return;
767 while ((*cur == ' ') || (*cur == '\t')) cur++;
768 ctxt->contentType = xmlMemStrdup(cur);
769 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Location:", 9)) {
770 cur += 9;
771 while ((*cur == ' ') || (*cur == '\t')) cur++;
772 if (ctxt->location != NULL)
773 xmlFree(ctxt->location);
774 ctxt->location = xmlMemStrdup(cur);
775 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"WWW-Authenticate:", 17)) {
776 cur += 17;
777 while ((*cur == ' ') || (*cur == '\t')) cur++;
778 if (ctxt->authHeader != NULL)
779 xmlFree(ctxt->authHeader);
780 ctxt->authHeader = xmlMemStrdup(cur);
781 } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Proxy-Authenticate:", 19)) {
782 cur += 19;
783 while ((*cur == ' ') || (*cur == '\t')) cur++;
784 if (ctxt->authHeader != NULL)
785 xmlFree(ctxt->authHeader);
786 ctxt->authHeader = xmlMemStrdup(cur);
Daniel Veillardf012a642001-07-23 19:10:52 +0000787 } else if ( !xmlStrncasecmp( BAD_CAST line, BAD_CAST"Content-Length:", 15) ) {
788 cur += 15;
789 ctxt->ContentLength = strtol( cur, NULL, 10 );
Owen Taylor3473f882001-02-23 17:55:21 +0000790 }
791}
792
793/**
794 * xmlNanoHTTPConnectAttempt:
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000795 * @addr: a socket address structure
Owen Taylor3473f882001-02-23 17:55:21 +0000796 *
797 * Attempt a connection to the given IP:port endpoint. It forces
798 * non-blocking semantic on the socket, and allow 60 seconds for
799 * the host to answer.
800 *
801 * Returns -1 in case of failure, the file descriptor number otherwise
802 */
803
804static int
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000805xmlNanoHTTPConnectAttempt(struct sockaddr *addr)
Owen Taylor3473f882001-02-23 17:55:21 +0000806{
Owen Taylor3473f882001-02-23 17:55:21 +0000807 fd_set wfd;
808 struct timeval tv;
809 int status;
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000810 int addrlen;
811 SOCKET s;
Owen Taylor3473f882001-02-23 17:55:21 +0000812
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000813#ifdef SUPPORT_IP6
814 if (addr->sa_family == AF_INET6) {
815 s = socket (PF_INET6, SOCK_STREAM, IPPROTO_TCP);
816 addrlen = sizeof (struct sockaddr_in6);
817 }
818 else
819#endif
820 {
821 s = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
822 addrlen = sizeof (struct sockaddr_in);
823 }
Owen Taylor3473f882001-02-23 17:55:21 +0000824 if (s==-1) {
825#ifdef DEBUG_HTTP
826 perror("socket");
827#endif
Daniel Veillardf012a642001-07-23 19:10:52 +0000828 xmlGenericError( xmlGenericErrorContext,
829 "xmlNanoHTTPConnectAttempt: %s - %s",
830 "socket creation failure",
831 strerror( socket_errno( ) ) );
Owen Taylor3473f882001-02-23 17:55:21 +0000832 return(-1);
833 }
834
835#ifdef _WINSOCKAPI_
836 {
837 u_long one = 1;
838
839 status = ioctlsocket(s, FIONBIO, &one) == SOCKET_ERROR ? -1 : 0;
840 }
841#else /* _WINSOCKAPI_ */
842#if defined(VMS)
843 {
844 int enable = 1;
845 status = ioctl(s, FIONBIO, &enable);
846 }
847#else /* VMS */
848 if ((status = fcntl(s, F_GETFL, 0)) != -1) {
849#ifdef O_NONBLOCK
850 status |= O_NONBLOCK;
851#else /* O_NONBLOCK */
852#ifdef F_NDELAY
853 status |= F_NDELAY;
854#endif /* F_NDELAY */
855#endif /* !O_NONBLOCK */
856 status = fcntl(s, F_SETFL, status);
857 }
858 if (status < 0) {
859#ifdef DEBUG_HTTP
860 perror("nonblocking");
861#endif
Daniel Veillardf012a642001-07-23 19:10:52 +0000862 xmlGenericError( xmlGenericErrorContext,
863 "xmlNanoHTTPConnectAttempt: %s - %s",
864 "error setting non-blocking IO",
865 strerror( socket_errno( ) ) );
Owen Taylor3473f882001-02-23 17:55:21 +0000866 closesocket(s);
867 return(-1);
868 }
869#endif /* !VMS */
870#endif /* !_WINSOCKAPI_ */
871
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000872 if (connect (s, addr, addrlen) == -1) {
Owen Taylor3473f882001-02-23 17:55:21 +0000873 switch (socket_errno()) {
874 case EINPROGRESS:
875 case EWOULDBLOCK:
876 break;
877 default:
Daniel Veillardf012a642001-07-23 19:10:52 +0000878 xmlGenericError( xmlGenericErrorContext,
879 "xmlNanoHTTPConnectAttempt: %s - %s",
880 "error connecting to HTTP server",
881 strerror( socket_errno( ) ) );
Owen Taylor3473f882001-02-23 17:55:21 +0000882 closesocket(s);
883 return(-1);
884 }
885 }
886
887 tv.tv_sec = timeout;
888 tv.tv_usec = 0;
889
890 FD_ZERO(&wfd);
891 FD_SET(s, &wfd);
892
893 switch(select(s+1, NULL, &wfd, NULL, &tv))
894 {
895 case 0:
896 /* Time out */
Daniel Veillardf012a642001-07-23 19:10:52 +0000897 xmlGenericError( xmlGenericErrorContext,
898 "xmlNanoHTTPConnectAttempt: %s",
899 "Connect attempt timed out." );
Owen Taylor3473f882001-02-23 17:55:21 +0000900 closesocket(s);
901 return(-1);
902 case -1:
903 /* Ermm.. ?? */
Daniel Veillardf012a642001-07-23 19:10:52 +0000904 xmlGenericError( xmlGenericErrorContext,
905 "xmlNanoHTTPConnectAttempt: %s - %s",
906 "Error connecting to host",
907 strerror( socket_errno( ) ) );
Owen Taylor3473f882001-02-23 17:55:21 +0000908 closesocket(s);
909 return(-1);
910 }
911
912 if ( FD_ISSET(s, &wfd) ) {
913 SOCKLEN_T len;
914 len = sizeof(status);
915 if (getsockopt(s, SOL_SOCKET, SO_ERROR, (char*)&status, &len) < 0 ) {
916 /* Solaris error code */
Daniel Veillardf012a642001-07-23 19:10:52 +0000917 xmlGenericError( xmlGenericErrorContext,
918 "xmlNanoHTTPConnectAttempt: %s - %s",
919 "Error retrieving pending socket errors",
920 strerror( socket_errno( ) ) );
Owen Taylor3473f882001-02-23 17:55:21 +0000921 return (-1);
922 }
923 if ( status ) {
924 closesocket(s);
925 errno = status;
Daniel Veillardf012a642001-07-23 19:10:52 +0000926 xmlGenericError( xmlGenericErrorContext,
927 "xmlNanoHTTPConnectAttempt: %s - %s",
928 "Error connecting to remote host",
929 strerror( status ) );
Owen Taylor3473f882001-02-23 17:55:21 +0000930 return (-1);
931 }
932 } else {
933 /* pbm */
Daniel Veillardf012a642001-07-23 19:10:52 +0000934 xmlGenericError( xmlGenericErrorContext,
935 "xmlNanoHTTPConnectAttempt: %s\n",
936 "Select returned, but descriptor not set for connection.\n" );
937 closesocket(s);
Owen Taylor3473f882001-02-23 17:55:21 +0000938 return (-1);
939 }
940
941 return(s);
942}
943
944/**
945 * xmlNanoHTTPConnectHost:
946 * @host: the host name
947 * @port: the port number
948 *
949 * Attempt a connection to the given host:port endpoint. It tries
950 * the multiple IP provided by the DNS if available.
951 *
952 * Returns -1 in case of failure, the file descriptor number otherwise
953 */
954
955static int
956xmlNanoHTTPConnectHost(const char *host, int port)
957{
958 struct hostent *h;
959 struct sockaddr *addr;
960 struct in_addr ia;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000961 struct sockaddr_in sockin;
Daniel Veillard5c396542002-03-15 07:57:50 +0000962
Owen Taylor3473f882001-02-23 17:55:21 +0000963#ifdef SUPPORT_IP6
964 struct in6_addr ia6;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000965 struct sockaddr_in6 sockin6;
Owen Taylor3473f882001-02-23 17:55:21 +0000966#endif
967 int i;
968 int s;
Daniel Veillard5c396542002-03-15 07:57:50 +0000969
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000970 memset (&sockin, 0, sizeof(sockin));
971#ifdef SUPPORT_IP6
972 memset (&sockin6, 0, sizeof(sockin6));
973 if (have_ipv6 ())
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000974#if !defined(HAVE_GETADDRINFO) && defined(RES_USE_INET6)
Daniel Veillard560c2a42003-07-06 21:13:49 +0000975 {
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000976 if (!(_res.options & RES_INIT))
977 res_init();
978 _res.options |= RES_USE_INET6;
979 }
980#elif defined(HAVE_GETADDRINFO)
Daniel Veillard560c2a42003-07-06 21:13:49 +0000981 {
Daniel Veillardde2a67b2003-06-21 14:20:04 +0000982 int status;
983 struct addrinfo hints, *res, *result;
984
985 result = NULL;
986 memset (&hints, 0,sizeof(hints));
987 hints.ai_socktype = SOCK_STREAM;
988
989 status = getaddrinfo (host, NULL, &hints, &result);
990 if (status) {
991 xmlGenericError (xmlGenericErrorContext,
992 "xmlNanoHTTPConnectHost: %s '%s' - %s",
993 "Failed to resolve host", host, gai_strerror (status));
994
995 return (-1);
996 }
997
998 for (res = result; res; res = res->ai_next) {
999 if (res) {
1000 if (res->ai_family == AF_INET6) {
1001 memcpy (&sockin6, res->ai_addr, res->ai_addrlen);
1002 sockin6.sin6_port = htons (port);
1003 addr = (struct sockaddr *)&sockin6;
1004 }
1005
1006 if (res->ai_family == AF_INET) {
1007 memcpy (&sockin, res->ai_addr, res->ai_addrlen);
1008 sockin.sin_port = htons (port);
1009 addr = (struct sockaddr *)&sockin;
1010 }
1011
1012 s = xmlNanoHTTPConnectAttempt (addr);
1013 if (s != -1) {
1014 freeaddrinfo (result);
1015 return (s);
1016 }
1017 }
1018 else {
1019 freeaddrinfo (result);
1020 return (-1);
1021 }
1022 }
1023 } else
Owen Taylor3473f882001-02-23 17:55:21 +00001024#endif
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001025#endif
1026 {
1027 h = gethostbyname (host);
1028 if (h == NULL) {
Daniel Veillard56b2db72002-03-25 16:35:28 +00001029
1030/*
1031 * Okay, I got fed up by the non-portability of this error message
1032 * extraction code. it work on Linux, if it work on your platform
1033 * and one want to enable it, send me the defined(foobar) needed
1034 */
1035#if defined(HAVE_NETDB_H) && defined(HOST_NOT_FOUND) && defined(linux)
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001036 const char *h_err_txt = "";
Daniel Veillardf012a642001-07-23 19:10:52 +00001037
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001038 switch (h_errno) {
1039 case HOST_NOT_FOUND:
1040 h_err_txt = "Authoritive host not found";
1041 break;
Daniel Veillardf012a642001-07-23 19:10:52 +00001042
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001043 case TRY_AGAIN:
1044 h_err_txt =
1045 "Non-authoritive host not found or server failure.";
1046 break;
Daniel Veillardf012a642001-07-23 19:10:52 +00001047
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001048 case NO_RECOVERY:
1049 h_err_txt =
1050 "Non-recoverable errors: FORMERR, REFUSED, or NOTIMP.";
1051 break;
Daniel Veillard5c396542002-03-15 07:57:50 +00001052
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001053 case NO_ADDRESS:
1054 h_err_txt =
1055 "Valid name, no data record of requested type.";
1056 break;
Daniel Veillard5c396542002-03-15 07:57:50 +00001057
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001058 default:
1059 h_err_txt = "No error text defined.";
1060 break;
1061 }
1062 xmlGenericError (xmlGenericErrorContext,
1063 "xmlNanoHTTPConnectHost: %s '%s' - %s",
1064 "Failed to resolve host", host, h_err_txt);
Daniel Veillard5c396542002-03-15 07:57:50 +00001065#else
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001066 xmlGenericError (xmlGenericErrorContext,
1067 "xmlNanoHTTPConnectHost: %s '%s'",
1068 "Failed to resolve host", host);
Owen Taylor3473f882001-02-23 17:55:21 +00001069#endif
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001070 return (-1);
1071 }
Daniel Veillard5c396542002-03-15 07:57:50 +00001072
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001073 for (i = 0; h->h_addr_list[i]; i++) {
1074 if (h->h_addrtype == AF_INET) {
1075 /* A records (IPv4) */
1076 memcpy (&ia, h->h_addr_list[i], h->h_length);
1077 sockin.sin_family = h->h_addrtype;
1078 sockin.sin_addr = ia;
1079 sockin.sin_port = htons (port);
1080 addr = (struct sockaddr *) &sockin;
Daniel Veillard5c396542002-03-15 07:57:50 +00001081#ifdef SUPPORT_IP6
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001082 } else if (have_ipv6 () && (h->h_addrtype == AF_INET6)) {
1083 /* AAAA records (IPv6) */
1084 memcpy (&ia6, h->h_addr_list[i], h->h_length);
1085 sockin6.sin6_family = h->h_addrtype;
1086 sockin6.sin6_addr = ia6;
1087 sockin6.sin6_port = htons (port);
1088 addr = (struct sockaddr *) &sockin6;
Daniel Veillard5c396542002-03-15 07:57:50 +00001089#endif
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001090 } else
1091 break; /* for */
Daniel Veillard5c396542002-03-15 07:57:50 +00001092
Daniel Veillardde2a67b2003-06-21 14:20:04 +00001093 s = xmlNanoHTTPConnectAttempt (addr);
1094 if (s != -1)
1095 return (s);
1096 }
Owen Taylor3473f882001-02-23 17:55:21 +00001097 }
Owen Taylor3473f882001-02-23 17:55:21 +00001098#ifdef DEBUG_HTTP
1099 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard5c396542002-03-15 07:57:50 +00001100 "xmlNanoHTTPConnectHost: unable to connect to '%s'.\n",
1101 host);
Owen Taylor3473f882001-02-23 17:55:21 +00001102#endif
Daniel Veillard5c396542002-03-15 07:57:50 +00001103 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001104}
1105
1106
1107/**
1108 * xmlNanoHTTPOpen:
1109 * @URL: The URL to load
1110 * @contentType: if available the Content-Type information will be
1111 * returned at that location
1112 *
1113 * This function try to open a connection to the indicated resource
1114 * via HTTP GET.
1115 *
1116 * Returns NULL in case of failure, otherwise a request handler.
1117 * The contentType, if provided must be freed by the caller
1118 */
1119
1120void*
1121xmlNanoHTTPOpen(const char *URL, char **contentType) {
1122 if (contentType != NULL) *contentType = NULL;
Daniel Veillardf012a642001-07-23 19:10:52 +00001123 return(xmlNanoHTTPMethod(URL, NULL, NULL, contentType, NULL, 0));
Daniel Veillard9403a042001-05-28 11:00:53 +00001124}
1125
1126/**
1127 * xmlNanoHTTPOpenRedir:
1128 * @URL: The URL to load
1129 * @contentType: if available the Content-Type information will be
1130 * returned at that location
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001131 * @redir: if available the redirected URL will be returned
Daniel Veillard9403a042001-05-28 11:00:53 +00001132 *
1133 * This function try to open a connection to the indicated resource
1134 * via HTTP GET.
1135 *
1136 * Returns NULL in case of failure, otherwise a request handler.
1137 * The contentType, if provided must be freed by the caller
1138 */
1139
1140void*
1141xmlNanoHTTPOpenRedir(const char *URL, char **contentType, char **redir) {
1142 if (contentType != NULL) *contentType = NULL;
1143 if (redir != NULL) *redir = NULL;
Daniel Veillardf012a642001-07-23 19:10:52 +00001144 return(xmlNanoHTTPMethodRedir(URL, NULL, NULL, contentType, redir, NULL,0));
Owen Taylor3473f882001-02-23 17:55:21 +00001145}
1146
1147/**
1148 * xmlNanoHTTPRead:
1149 * @ctx: the HTTP context
1150 * @dest: a buffer
1151 * @len: the buffer length
1152 *
1153 * This function tries to read @len bytes from the existing HTTP connection
1154 * and saves them in @dest. This is a blocking call.
1155 *
1156 * Returns the number of byte read. 0 is an indication of an end of connection.
1157 * -1 indicates a parameter error.
1158 */
1159int
1160xmlNanoHTTPRead(void *ctx, void *dest, int len) {
1161 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1162
1163 if (ctx == NULL) return(-1);
1164 if (dest == NULL) return(-1);
1165 if (len <= 0) return(0);
1166
1167 while (ctxt->inptr - ctxt->inrptr < len) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001168 if (xmlNanoHTTPRecv(ctxt) <= 0) break;
Owen Taylor3473f882001-02-23 17:55:21 +00001169 }
1170 if (ctxt->inptr - ctxt->inrptr < len)
1171 len = ctxt->inptr - ctxt->inrptr;
1172 memcpy(dest, ctxt->inrptr, len);
1173 ctxt->inrptr += len;
1174 return(len);
1175}
1176
1177/**
1178 * xmlNanoHTTPClose:
1179 * @ctx: the HTTP context
1180 *
1181 * This function closes an HTTP context, it ends up the connection and
1182 * free all data related to it.
1183 */
1184void
1185xmlNanoHTTPClose(void *ctx) {
1186 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1187
1188 if (ctx == NULL) return;
1189
1190 xmlNanoHTTPFreeCtxt(ctxt);
1191}
1192
1193/**
Daniel Veillard9403a042001-05-28 11:00:53 +00001194 * xmlNanoHTTPMethodRedir:
Owen Taylor3473f882001-02-23 17:55:21 +00001195 * @URL: The URL to load
1196 * @method: the HTTP method to use
1197 * @input: the input string if any
1198 * @contentType: the Content-Type information IN and OUT
Daniel Veillard9403a042001-05-28 11:00:53 +00001199 * @redir: the redirected URL OUT
Owen Taylor3473f882001-02-23 17:55:21 +00001200 * @headers: the extra headers
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001201 * @ilen: input length
Owen Taylor3473f882001-02-23 17:55:21 +00001202 *
1203 * This function try to open a connection to the indicated resource
1204 * via HTTP using the given @method, adding the given extra headers
1205 * and the input buffer for the request content.
1206 *
1207 * Returns NULL in case of failure, otherwise a request handler.
Daniel Veillard9403a042001-05-28 11:00:53 +00001208 * The contentType, or redir, if provided must be freed by the caller
Owen Taylor3473f882001-02-23 17:55:21 +00001209 */
1210
1211void*
Daniel Veillard9403a042001-05-28 11:00:53 +00001212xmlNanoHTTPMethodRedir(const char *URL, const char *method, const char *input,
Daniel Veillardf012a642001-07-23 19:10:52 +00001213 char **contentType, char **redir,
1214 const char *headers, int ilen ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001215 xmlNanoHTTPCtxtPtr ctxt;
1216 char *bp, *p;
Daniel Veillardf012a642001-07-23 19:10:52 +00001217 int blen, ret;
Owen Taylor3473f882001-02-23 17:55:21 +00001218 int head;
Daniel Veillardf012a642001-07-23 19:10:52 +00001219 int xmt_bytes;
Owen Taylor3473f882001-02-23 17:55:21 +00001220 int nbRedirects = 0;
1221 char *redirURL = NULL;
1222
1223 if (URL == NULL) return(NULL);
1224 if (method == NULL) method = "GET";
1225 xmlNanoHTTPInit();
1226
1227retry:
1228 if (redirURL == NULL)
1229 ctxt = xmlNanoHTTPNewCtxt(URL);
1230 else {
1231 ctxt = xmlNanoHTTPNewCtxt(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001232 }
1233
Daniel Veillardf012a642001-07-23 19:10:52 +00001234 if ( ctxt == NULL ) {
1235 xmlGenericError( xmlGenericErrorContext,
1236 "xmlNanoHTTPMethodRedir: %s %s.",
1237 "Unable to allocate HTTP context to URI",
1238 ( ( redirURL == NULL ) ? URL : redirURL ) );
1239 return ( NULL );
1240 }
1241
Owen Taylor3473f882001-02-23 17:55:21 +00001242 if ((ctxt->protocol == NULL) || (strcmp(ctxt->protocol, "http"))) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001243 xmlGenericError( xmlGenericErrorContext,
1244 "xmlNanoHTTPMethodRedir: %s - %s.",
1245 "Not a valid HTTP URI",
1246 ( ( redirURL == NULL ) ? URL : redirURL ) );
Owen Taylor3473f882001-02-23 17:55:21 +00001247 xmlNanoHTTPFreeCtxt(ctxt);
1248 if (redirURL != NULL) xmlFree(redirURL);
1249 return(NULL);
1250 }
1251 if (ctxt->hostname == NULL) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001252 xmlGenericError( xmlGenericErrorContext,
1253 "xmlNanoHTTPMethodRedir: %s - %s",
1254 "Failed to identify host in URI",
1255 ( ( redirURL == NULL ) ? URL : redirURL ) );
Owen Taylor3473f882001-02-23 17:55:21 +00001256 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001257 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001258 return(NULL);
1259 }
1260 if (proxy) {
1261 blen = strlen(ctxt->hostname) * 2 + 16;
1262 ret = xmlNanoHTTPConnectHost(proxy, proxyPort);
1263 }
1264 else {
1265 blen = strlen(ctxt->hostname);
1266 ret = xmlNanoHTTPConnectHost(ctxt->hostname, ctxt->port);
1267 }
1268 if (ret < 0) {
1269 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001270 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001271 return(NULL);
1272 }
1273 ctxt->fd = ret;
1274
Daniel Veillardf012a642001-07-23 19:10:52 +00001275 if (input == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00001276 ilen = 0;
Daniel Veillardf012a642001-07-23 19:10:52 +00001277 else
1278 blen += 36;
1279
Owen Taylor3473f882001-02-23 17:55:21 +00001280 if (headers != NULL)
Daniel Veillardf012a642001-07-23 19:10:52 +00001281 blen += strlen(headers) + 2;
Owen Taylor3473f882001-02-23 17:55:21 +00001282 if (contentType && *contentType)
1283 blen += strlen(*contentType) + 16;
Daniel Veillardf012a642001-07-23 19:10:52 +00001284 blen += strlen(method) + strlen(ctxt->path) + 24;
Daniel Veillard3c908dc2003-04-19 00:07:51 +00001285 bp = xmlMallocAtomic(blen);
Daniel Veillardf012a642001-07-23 19:10:52 +00001286 if ( bp == NULL ) {
1287 xmlNanoHTTPFreeCtxt( ctxt );
1288 xmlGenericError( xmlGenericErrorContext,
1289 "xmlNanoHTTPMethodRedir: %s",
1290 "Error allocating HTTP header buffer." );
1291 return ( NULL );
1292 }
1293
1294 p = bp;
1295
Owen Taylor3473f882001-02-23 17:55:21 +00001296 if (proxy) {
1297 if (ctxt->port != 80) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001298 p += snprintf( p, blen - (p - bp), "%s http://%s:%d%s",
1299 method, ctxt->hostname,
Daniel Veillardf012a642001-07-23 19:10:52 +00001300 ctxt->port, ctxt->path );
Owen Taylor3473f882001-02-23 17:55:21 +00001301 }
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001302 else
1303 p += snprintf( p, blen - (p - bp), "%s http://%s%s", method,
Daniel Veillardf012a642001-07-23 19:10:52 +00001304 ctxt->hostname, ctxt->path);
Owen Taylor3473f882001-02-23 17:55:21 +00001305 }
1306 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001307 p += snprintf( p, blen - (p - bp), "%s %s", method, ctxt->path);
Daniel Veillardf012a642001-07-23 19:10:52 +00001308
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001309 p += snprintf( p, blen - (p - bp), " HTTP/1.0\r\nHost: %s\r\n",
1310 ctxt->hostname);
Daniel Veillardf012a642001-07-23 19:10:52 +00001311
1312 if (contentType != NULL && *contentType)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001313 p += snprintf(p, blen - (p - bp), "Content-Type: %s\r\n", *contentType);
Daniel Veillardf012a642001-07-23 19:10:52 +00001314
1315 if (headers != NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001316 p += snprintf( p, blen - (p - bp), "%s", headers );
Daniel Veillardf012a642001-07-23 19:10:52 +00001317
Owen Taylor3473f882001-02-23 17:55:21 +00001318 if (input != NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001319 snprintf(p, blen - (p - bp), "Content-Length: %d\r\n\r\n", ilen );
Owen Taylor3473f882001-02-23 17:55:21 +00001320 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001321 snprintf(p, blen - (p - bp), "\r\n");
Daniel Veillardf012a642001-07-23 19:10:52 +00001322
Owen Taylor3473f882001-02-23 17:55:21 +00001323#ifdef DEBUG_HTTP
1324 xmlGenericError(xmlGenericErrorContext,
1325 "-> %s%s", proxy? "(Proxy) " : "", bp);
1326 if ((blen -= strlen(bp)+1) < 0)
1327 xmlGenericError(xmlGenericErrorContext,
1328 "ERROR: overflowed buffer by %d bytes\n", -blen);
1329#endif
1330 ctxt->outptr = ctxt->out = bp;
1331 ctxt->state = XML_NANO_HTTP_WRITE;
Daniel Veillardf012a642001-07-23 19:10:52 +00001332 blen = strlen( ctxt->out );
1333 xmt_bytes = xmlNanoHTTPSend(ctxt, ctxt->out, blen );
1334#ifdef DEBUG_HTTP
1335 if ( xmt_bytes != blen )
1336 xmlGenericError( xmlGenericErrorContext,
1337 "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
1338 xmt_bytes, blen,
1339 "bytes of HTTP headers sent to host",
1340 ctxt->hostname );
1341#endif
1342
1343 if ( input != NULL ) {
1344 xmt_bytes = xmlNanoHTTPSend( ctxt, input, ilen );
1345
1346#ifdef DEBUG_HTTP
1347 if ( xmt_bytes != ilen )
1348 xmlGenericError( xmlGenericErrorContext,
1349 "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
1350 xmt_bytes, ilen,
1351 "bytes of HTTP content sent to host",
1352 ctxt->hostname );
1353#endif
1354 }
1355
Owen Taylor3473f882001-02-23 17:55:21 +00001356 ctxt->state = XML_NANO_HTTP_READ;
1357 head = 1;
1358
1359 while ((p = xmlNanoHTTPReadLine(ctxt)) != NULL) {
1360 if (head && (*p == 0)) {
1361 head = 0;
1362 ctxt->content = ctxt->inrptr;
1363 xmlFree(p);
1364 break;
1365 }
1366 xmlNanoHTTPScanAnswer(ctxt, p);
1367
1368#ifdef DEBUG_HTTP
1369 xmlGenericError(xmlGenericErrorContext, "<- %s\n", p);
1370#endif
1371 xmlFree(p);
1372 }
1373
1374 if ((ctxt->location != NULL) && (ctxt->returnValue >= 300) &&
1375 (ctxt->returnValue < 400)) {
1376#ifdef DEBUG_HTTP
1377 xmlGenericError(xmlGenericErrorContext,
1378 "\nRedirect to: %s\n", ctxt->location);
1379#endif
Daniel Veillardf012a642001-07-23 19:10:52 +00001380 while ( xmlNanoHTTPRecv(ctxt) > 0 ) ;
Owen Taylor3473f882001-02-23 17:55:21 +00001381 if (nbRedirects < XML_NANO_HTTP_MAX_REDIR) {
1382 nbRedirects++;
Daniel Veillard9403a042001-05-28 11:00:53 +00001383 if (redirURL != NULL)
1384 xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001385 redirURL = xmlMemStrdup(ctxt->location);
1386 xmlNanoHTTPFreeCtxt(ctxt);
1387 goto retry;
1388 }
1389 xmlNanoHTTPFreeCtxt(ctxt);
Daniel Veillard9403a042001-05-28 11:00:53 +00001390 if (redirURL != NULL) xmlFree(redirURL);
Owen Taylor3473f882001-02-23 17:55:21 +00001391#ifdef DEBUG_HTTP
1392 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardf012a642001-07-23 19:10:52 +00001393 "xmlNanoHTTPMethodRedir: Too many redirects, aborting ...\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001394#endif
1395 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001396 }
1397
1398 if (contentType != NULL) {
1399 if (ctxt->contentType != NULL)
1400 *contentType = xmlMemStrdup(ctxt->contentType);
1401 else
1402 *contentType = NULL;
1403 }
1404
Daniel Veillard9403a042001-05-28 11:00:53 +00001405 if ((redir != NULL) && (redirURL != NULL)) {
1406 *redir = redirURL;
1407 } else {
1408 if (redirURL != NULL)
1409 xmlFree(redirURL);
1410 if (redir != NULL)
1411 *redir = NULL;
1412 }
1413
Owen Taylor3473f882001-02-23 17:55:21 +00001414#ifdef DEBUG_HTTP
1415 if (ctxt->contentType != NULL)
1416 xmlGenericError(xmlGenericErrorContext,
1417 "\nCode %d, content-type '%s'\n\n",
1418 ctxt->returnValue, ctxt->contentType);
1419 else
1420 xmlGenericError(xmlGenericErrorContext,
1421 "\nCode %d, no content-type\n\n",
1422 ctxt->returnValue);
1423#endif
1424
1425 return((void *) ctxt);
1426}
1427
1428/**
Daniel Veillard9403a042001-05-28 11:00:53 +00001429 * xmlNanoHTTPMethod:
1430 * @URL: The URL to load
1431 * @method: the HTTP method to use
1432 * @input: the input string if any
1433 * @contentType: the Content-Type information IN and OUT
1434 * @headers: the extra headers
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001435 * @ilen: input length
Daniel Veillard9403a042001-05-28 11:00:53 +00001436 *
1437 * This function try to open a connection to the indicated resource
1438 * via HTTP using the given @method, adding the given extra headers
1439 * and the input buffer for the request content.
1440 *
1441 * Returns NULL in case of failure, otherwise a request handler.
1442 * The contentType, if provided must be freed by the caller
1443 */
1444
1445void*
1446xmlNanoHTTPMethod(const char *URL, const char *method, const char *input,
Daniel Veillardf012a642001-07-23 19:10:52 +00001447 char **contentType, const char *headers, int ilen) {
Daniel Veillard9403a042001-05-28 11:00:53 +00001448 return(xmlNanoHTTPMethodRedir(URL, method, input, contentType,
Daniel Veillardf012a642001-07-23 19:10:52 +00001449 NULL, headers, ilen));
Daniel Veillard9403a042001-05-28 11:00:53 +00001450}
1451
1452/**
Owen Taylor3473f882001-02-23 17:55:21 +00001453 * xmlNanoHTTPFetch:
1454 * @URL: The URL to load
1455 * @filename: the filename where the content should be saved
1456 * @contentType: if available the Content-Type information will be
1457 * returned at that location
1458 *
1459 * This function try to fetch the indicated resource via HTTP GET
1460 * and save it's content in the file.
1461 *
1462 * Returns -1 in case of failure, 0 incase of success. The contentType,
1463 * if provided must be freed by the caller
1464 */
1465int
1466xmlNanoHTTPFetch(const char *URL, const char *filename, char **contentType) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001467 void *ctxt = NULL;
1468 char *buf = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001469 int fd;
1470 int len;
1471
1472 ctxt = xmlNanoHTTPOpen(URL, contentType);
1473 if (ctxt == NULL) return(-1);
1474
1475 if (!strcmp(filename, "-"))
1476 fd = 0;
1477 else {
1478 fd = open(filename, O_CREAT | O_WRONLY, 00644);
1479 if (fd < 0) {
1480 xmlNanoHTTPClose(ctxt);
1481 if ((contentType != NULL) && (*contentType != NULL)) {
1482 xmlFree(*contentType);
1483 *contentType = NULL;
1484 }
1485 return(-1);
1486 }
1487 }
1488
Daniel Veillardf012a642001-07-23 19:10:52 +00001489 xmlNanoHTTPFetchContent( ctxt, &buf, &len );
1490 if ( len > 0 ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001491 write(fd, buf, len);
1492 }
1493
1494 xmlNanoHTTPClose(ctxt);
1495 close(fd);
1496 return(0);
1497}
1498
1499/**
1500 * xmlNanoHTTPSave:
1501 * @ctxt: the HTTP context
1502 * @filename: the filename where the content should be saved
1503 *
1504 * This function saves the output of the HTTP transaction to a file
1505 * It closes and free the context at the end
1506 *
1507 * Returns -1 in case of failure, 0 incase of success.
1508 */
1509int
1510xmlNanoHTTPSave(void *ctxt, const char *filename) {
Daniel Veillarde3924972001-07-25 20:25:21 +00001511 char *buf = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001512 int fd;
1513 int len;
1514
1515 if (ctxt == NULL) return(-1);
1516
1517 if (!strcmp(filename, "-"))
1518 fd = 0;
1519 else {
1520 fd = open(filename, O_CREAT | O_WRONLY);
1521 if (fd < 0) {
1522 xmlNanoHTTPClose(ctxt);
1523 return(-1);
1524 }
1525 }
1526
Daniel Veillardf012a642001-07-23 19:10:52 +00001527 xmlNanoHTTPFetchContent( ctxt, &buf, &len );
1528 if ( len > 0 ) {
Owen Taylor3473f882001-02-23 17:55:21 +00001529 write(fd, buf, len);
1530 }
1531
1532 xmlNanoHTTPClose(ctxt);
1533 return(0);
1534}
1535
1536/**
1537 * xmlNanoHTTPReturnCode:
1538 * @ctx: the HTTP context
1539 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001540 * Get the latest HTTP return code received
1541 *
Owen Taylor3473f882001-02-23 17:55:21 +00001542 * Returns the HTTP return code for the request.
1543 */
1544int
1545xmlNanoHTTPReturnCode(void *ctx) {
1546 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1547
1548 if (ctxt == NULL) return(-1);
1549
1550 return(ctxt->returnValue);
1551}
1552
1553/**
1554 * xmlNanoHTTPAuthHeader:
1555 * @ctx: the HTTP context
1556 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001557 * Get the authentication header of an HTTP context
1558 *
Owen Taylor3473f882001-02-23 17:55:21 +00001559 * Returns the stashed value of the WWW-Authenticate or Proxy-Authenticate
1560 * header.
1561 */
1562const char *
1563xmlNanoHTTPAuthHeader(void *ctx) {
1564 xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx;
1565
1566 if (ctxt == NULL) return(NULL);
1567
1568 return(ctxt->authHeader);
1569}
1570
Daniel Veillardf012a642001-07-23 19:10:52 +00001571/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00001572 * xmlNanoHTTPContentLength:
Daniel Veillardf012a642001-07-23 19:10:52 +00001573 * @ctx: the HTTP context
1574 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001575 * Provides the specified content length from the HTTP header.
1576 *
Daniel Veillardf012a642001-07-23 19:10:52 +00001577 * Return the specified content length from the HTTP header. Note that
1578 * a value of -1 indicates that the content length element was not included in
1579 * the response header.
1580 */
1581int
1582xmlNanoHTTPContentLength( void * ctx ) {
1583 xmlNanoHTTPCtxtPtr ctxt = ctx;
1584
1585 return ( ( ctxt == NULL ) ? -1 : ctxt->ContentLength );
1586}
1587
1588/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00001589 * xmlNanoHTTPFetchContent:
Daniel Veillardf012a642001-07-23 19:10:52 +00001590 * @ctx: the HTTP context
1591 * @ptr: pointer to set to the content buffer.
1592 * @len: integer pointer to hold the length of the content
1593 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001594 * Check if all the content was read
1595 *
Daniel Veillardf012a642001-07-23 19:10:52 +00001596 * Returns 0 if all the content was read and available, returns
1597 * -1 if received content length was less than specified or an error
1598 * occurred.
1599 */
1600int
1601xmlNanoHTTPFetchContent( void * ctx, char ** ptr, int * len ) {
1602 xmlNanoHTTPCtxtPtr ctxt = ctx;
1603
1604 int rc = 0;
1605 int cur_lgth;
1606 int rcvd_lgth;
1607 int dummy_int;
1608 char * dummy_ptr = NULL;
1609
1610 /* Dummy up return input parameters if not provided */
1611
1612 if ( len == NULL )
1613 len = &dummy_int;
1614
1615 if ( ptr == NULL )
1616 ptr = &dummy_ptr;
1617
1618 /* But can't work without the context pointer */
1619
1620 if ( ( ctxt == NULL ) || ( ctxt->content == NULL ) ) {
1621 *len = 0;
1622 *ptr = NULL;
1623 return ( -1 );
1624 }
1625
1626 rcvd_lgth = ctxt->inptr - ctxt->content;
1627
1628 while ( (cur_lgth = xmlNanoHTTPRecv( ctxt )) > 0 ) {
1629
1630 rcvd_lgth += cur_lgth;
1631 if ( (ctxt->ContentLength > 0) && (rcvd_lgth >= ctxt->ContentLength) )
1632 break;
1633 }
1634
1635 *ptr = ctxt->content;
1636 *len = rcvd_lgth;
1637
1638 if ( ( ctxt->ContentLength > 0 ) && ( rcvd_lgth < ctxt->ContentLength ) )
1639 rc = -1;
1640 else if ( rcvd_lgth == 0 )
1641 rc = -1;
1642
1643 return ( rc );
1644}
1645
Owen Taylor3473f882001-02-23 17:55:21 +00001646#ifdef STANDALONE
1647int main(int argc, char **argv) {
1648 char *contentType = NULL;
1649
1650 if (argv[1] != NULL) {
1651 if (argv[2] != NULL)
1652 xmlNanoHTTPFetch(argv[1], argv[2], &contentType);
1653 else
1654 xmlNanoHTTPFetch(argv[1], "-", &contentType);
1655 if (contentType != NULL) xmlFree(contentType);
1656 } else {
1657 xmlGenericError(xmlGenericErrorContext,
1658 "%s: minimal HTTP GET implementation\n", argv[0]);
1659 xmlGenericError(xmlGenericErrorContext,
1660 "\tusage %s [ URL [ filename ] ]\n", argv[0]);
1661 }
1662 xmlNanoHTTPCleanup();
1663 xmlMemoryDump();
1664 return(0);
1665}
1666#endif /* STANDALONE */
1667#else /* !LIBXML_HTTP_ENABLED */
1668#ifdef STANDALONE
1669#include <stdio.h>
1670int main(int argc, char **argv) {
1671 xmlGenericError(xmlGenericErrorContext,
1672 "%s : HTTP support not compiled in\n", argv[0]);
1673 return(0);
1674}
1675#endif /* STANDALONE */
1676#endif /* LIBXML_HTTP_ENABLED */