2 * nanohttp.c: minimalist HTTP GET implementation to fetch external subsets.
3 * focuses on size, streamability, reentrancy and portability
5 * This is clearly not a general purpose HTTP implementation
6 * If you look for one, check:
7 * http://www.w3.org/Library/
9 * See Copyright for the status of this software.
18 #ifdef LIBXML_HTTP_ENABLED
27 #ifdef HAVE_SYS_TYPES_H
28 #include <sys/types.h>
30 #ifdef HAVE_SYS_SOCKET_H
31 #include <sys/socket.h>
33 #ifdef HAVE_NETINET_IN_H
34 #include <netinet/in.h>
36 #ifdef HAVE_ARPA_INET_H
37 #include <arpa/inet.h>
43 #ifdef HAVE_ARPA_NAMESER_H
44 #include <arpa/nameser.h>
54 #ifdef HAVE_SYS_TIME_H
58 #ifdef HAVE_SYS_SELECT_H
59 #include <sys/select.h>
77 #define XML_SOCKLEN_T unsigned int
81 #if defined(__MINGW32__) || defined(_WIN32_WCE)
83 #include <wsockcompat.h>
86 #define XML_SOCKLEN_T unsigned int
90 #include <libxml/globals.h>
91 #include <libxml/xmlerror.h>
92 #include <libxml/xmlmemory.h>
93 #include <libxml/parser.h> /* for xmlStr(n)casecmp() */
94 #include <libxml/nanohttp.h>
95 #include <libxml/globals.h>
96 #include <libxml/uri.h>
99 * A couple portability macros
102 #if !defined(__BEOS__) || defined(__HAIKU__)
103 #define closesocket(s) close(s)
110 #define PF_INET AF_INET
114 #ifndef XML_SOCKLEN_T
115 #define XML_SOCKLEN_T unsigned int
123 #define xmlStrncasecmp(a, b, n) strncasecmp((char *)a, (char *)b, n)
124 #define xmlStrcasecmpi(a, b) strcasecmp((char *)a, (char *)b)
127 #define XML_NANO_HTTP_MAX_REDIR 10
129 #define XML_NANO_HTTP_CHUNK 4096
131 #define XML_NANO_HTTP_CLOSED 0
132 #define XML_NANO_HTTP_WRITE 1
133 #define XML_NANO_HTTP_READ 2
134 #define XML_NANO_HTTP_NONE 4
136 typedef struct xmlNanoHTTPCtxt
{
137 char *protocol
; /* the protocol name */
138 char *hostname
; /* the host name */
139 int port
; /* the port */
140 char *path
; /* the path within the URL */
141 char *query
; /* the query string */
142 SOCKET fd
; /* the file descriptor for the socket */
143 int state
; /* WRITE / READ / CLOSED */
144 char *out
; /* buffer sent (zero terminated) */
145 char *outptr
; /* index within the buffer sent */
146 char *in
; /* the receiving buffer */
147 char *content
; /* the start of the content */
148 char *inptr
; /* the next byte to read from network */
149 char *inrptr
; /* the next byte to give back to the client */
150 int inlen
; /* len of the input buffer */
151 int last
; /* return code for last operation */
152 int returnValue
; /* the protocol return value */
153 int version
; /* the protocol version */
154 int ContentLength
; /* specified content length from HTTP header */
155 char *contentType
; /* the MIME type for the input */
156 char *location
; /* the new URL in case of redirect */
157 char *authHeader
; /* contents of {WWW,Proxy}-Authenticate header */
158 char *encoding
; /* encoding extracted from the contentType */
159 char *mimeType
; /* Mime-Type extracted from the contentType */
161 z_stream
*strm
; /* Zlib stream object */
162 int usesGzip
; /* "Content-Encoding: gzip" was detected */
164 } xmlNanoHTTPCtxt
, *xmlNanoHTTPCtxtPtr
;
166 static int initialized
= 0;
167 static char *proxy
= NULL
; /* the proxy name if any */
168 static int proxyPort
; /* the proxy port if any */
169 static unsigned int timeout
= 60;/* the select() timeout in seconds */
171 static int xmlNanoHTTPFetchContent( void * ctx
, char ** ptr
, int * len
);
175 * @extra: extra informations
177 * Handle an out of memory condition
180 xmlHTTPErrMemory(const char *extra
)
182 __xmlSimpleError(XML_FROM_HTTP
, XML_ERR_NO_MEMORY
, NULL
, NULL
, extra
);
186 * A portability function
188 static int socket_errno(void) {
190 return(WSAGetLastError());
198 int have_ipv6(void) {
201 s
= socket (AF_INET6
, SOCK_STREAM
, 0);
213 * Initialize the HTTP protocol layer.
214 * Currently it just checks for proxy informations
218 xmlNanoHTTPInit(void) {
228 if (WSAStartup(MAKEWORD(1, 1), &wsaData
) != 0)
234 env
= getenv("no_proxy");
235 if (env
&& ((env
[0] == '*') && (env
[1] == 0)))
237 env
= getenv("http_proxy");
239 xmlNanoHTTPScanProxy(env
);
242 env
= getenv("HTTP_PROXY");
244 xmlNanoHTTPScanProxy(env
);
253 * xmlNanoHTTPCleanup:
255 * Cleanup the HTTP protocol layer.
259 xmlNanoHTTPCleanup(void) {
273 * xmlNanoHTTPScanURL:
274 * @ctxt: an HTTP context
275 * @URL: The URL used to initialize the context
277 * (Re)Initialize an HTTP context by parsing the URL and finding
278 * the protocol host port and path it indicates.
282 xmlNanoHTTPScanURL(xmlNanoHTTPCtxtPtr ctxt
, const char *URL
) {
285 * Clear any existing data from the context
287 if (ctxt
->protocol
!= NULL
) {
288 xmlFree(ctxt
->protocol
);
289 ctxt
->protocol
= NULL
;
291 if (ctxt
->hostname
!= NULL
) {
292 xmlFree(ctxt
->hostname
);
293 ctxt
->hostname
= NULL
;
295 if (ctxt
->path
!= NULL
) {
299 if (ctxt
->query
!= NULL
) {
300 xmlFree(ctxt
->query
);
303 if (URL
== NULL
) return;
305 uri
= xmlParseURIRaw(URL
, 1);
309 if ((uri
->scheme
== NULL
) || (uri
->server
== NULL
)) {
314 ctxt
->protocol
= xmlMemStrdup(uri
->scheme
);
315 ctxt
->hostname
= xmlMemStrdup(uri
->server
);
316 if (uri
->path
!= NULL
)
317 ctxt
->path
= xmlMemStrdup(uri
->path
);
319 ctxt
->path
= xmlMemStrdup("/");
320 if (uri
->query
!= NULL
)
321 ctxt
->query
= xmlMemStrdup(uri
->query
);
323 ctxt
->port
= uri
->port
;
329 * xmlNanoHTTPScanProxy:
330 * @URL: The proxy URL used to initialize the proxy context
332 * (Re)Initialize the HTTP Proxy context by parsing the URL and finding
333 * the protocol host port it indicates.
334 * Should be like http://myproxy/ or http://myproxy:3128/
335 * A NULL URL cleans up proxy informations.
339 xmlNanoHTTPScanProxy(const char *URL
) {
350 xmlGenericError(xmlGenericErrorContext
,
351 "Removing HTTP proxy info\n");
353 xmlGenericError(xmlGenericErrorContext
,
354 "Using HTTP proxy %s\n", URL
);
356 if (URL
== NULL
) return;
358 uri
= xmlParseURIRaw(URL
, 1);
359 if ((uri
== NULL
) || (uri
->scheme
== NULL
) ||
360 (strcmp(uri
->scheme
, "http")) || (uri
->server
== NULL
)) {
361 __xmlIOErr(XML_FROM_HTTP
, XML_HTTP_URL_SYNTAX
, "Syntax Error\n");
367 proxy
= xmlMemStrdup(uri
->server
);
369 proxyPort
= uri
->port
;
375 * xmlNanoHTTPNewCtxt:
376 * @URL: The URL used to initialize the context
378 * Allocate and initialize a new HTTP context.
380 * Returns an HTTP context or NULL in case of error.
383 static xmlNanoHTTPCtxtPtr
384 xmlNanoHTTPNewCtxt(const char *URL
) {
385 xmlNanoHTTPCtxtPtr ret
;
387 ret
= (xmlNanoHTTPCtxtPtr
) xmlMalloc(sizeof(xmlNanoHTTPCtxt
));
389 xmlHTTPErrMemory("allocating context");
393 memset(ret
, 0, sizeof(xmlNanoHTTPCtxt
));
395 ret
->returnValue
= 0;
397 ret
->ContentLength
= -1;
399 xmlNanoHTTPScanURL(ret
, URL
);
405 * xmlNanoHTTPFreeCtxt:
406 * @ctxt: an HTTP context
408 * Frees the context after closing the connection.
412 xmlNanoHTTPFreeCtxt(xmlNanoHTTPCtxtPtr ctxt
) {
413 if (ctxt
== NULL
) return;
414 if (ctxt
->hostname
!= NULL
) xmlFree(ctxt
->hostname
);
415 if (ctxt
->protocol
!= NULL
) xmlFree(ctxt
->protocol
);
416 if (ctxt
->path
!= NULL
) xmlFree(ctxt
->path
);
417 if (ctxt
->query
!= NULL
) xmlFree(ctxt
->query
);
418 if (ctxt
->out
!= NULL
) xmlFree(ctxt
->out
);
419 if (ctxt
->in
!= NULL
) xmlFree(ctxt
->in
);
420 if (ctxt
->contentType
!= NULL
) xmlFree(ctxt
->contentType
);
421 if (ctxt
->encoding
!= NULL
) xmlFree(ctxt
->encoding
);
422 if (ctxt
->mimeType
!= NULL
) xmlFree(ctxt
->mimeType
);
423 if (ctxt
->location
!= NULL
) xmlFree(ctxt
->location
);
424 if (ctxt
->authHeader
!= NULL
) xmlFree(ctxt
->authHeader
);
426 if (ctxt
->strm
!= NULL
) {
427 inflateEnd(ctxt
->strm
);
432 ctxt
->state
= XML_NANO_HTTP_NONE
;
433 if (ctxt
->fd
>= 0) closesocket(ctxt
->fd
);
440 * @ctxt: an HTTP context
442 * Send the input needed to initiate the processing on the server side
443 * Returns number of bytes sent or -1 on error.
447 xmlNanoHTTPSend(xmlNanoHTTPCtxtPtr ctxt
, const char *xmt_ptr
, int outlen
)
457 if ((ctxt
->state
& XML_NANO_HTTP_WRITE
) && (xmt_ptr
!= NULL
)) {
458 while (total_sent
< outlen
) {
459 int nsent
= send(ctxt
->fd
, xmt_ptr
+ total_sent
,
460 outlen
- total_sent
, 0);
464 else if ((nsent
== -1) &&
465 #if defined(EAGAIN) && EAGAIN != EWOULDBLOCK
466 (socket_errno() != EAGAIN
) &&
468 (socket_errno() != EWOULDBLOCK
)) {
469 __xmlIOErr(XML_FROM_HTTP
, 0, "send failed\n");
476 * Since non-blocking sockets are used, wait for
477 * socket to be writable or default timeout prior
482 if (ctxt
->fd
> FD_SETSIZE
)
490 #pragma warning(push)
491 #pragma warning(disable: 4018)
493 FD_SET(ctxt
->fd
, &wfd
);
497 (void) select(ctxt
->fd
+ 1, NULL
, &wfd
, NULL
, &tv
);
501 (void) poll(&p
, 1, timeout
* 1000);
502 #endif /* !HAVE_POLL_H */
512 * @ctxt: an HTTP context
514 * Read information coming from the HTTP connection.
515 * This is a blocking call (but it blocks in select(), not read()).
517 * Returns the number of byte read or -1 in case of error.
521 xmlNanoHTTPRecv(xmlNanoHTTPCtxtPtr ctxt
)
531 while (ctxt
->state
& XML_NANO_HTTP_READ
) {
532 if (ctxt
->in
== NULL
) {
533 ctxt
->in
= (char *) xmlMallocAtomic(65000 * sizeof(char));
534 if (ctxt
->in
== NULL
) {
535 xmlHTTPErrMemory("allocating input");
540 ctxt
->inptr
= ctxt
->content
= ctxt
->inrptr
= ctxt
->in
;
542 if (ctxt
->inrptr
> ctxt
->in
+ XML_NANO_HTTP_CHUNK
) {
543 int delta
= ctxt
->inrptr
- ctxt
->in
;
544 int len
= ctxt
->inptr
- ctxt
->inrptr
;
546 memmove(ctxt
->in
, ctxt
->inrptr
, len
);
547 ctxt
->inrptr
-= delta
;
548 ctxt
->content
-= delta
;
549 ctxt
->inptr
-= delta
;
551 if ((ctxt
->in
+ ctxt
->inlen
) < (ctxt
->inptr
+ XML_NANO_HTTP_CHUNK
)) {
552 int d_inptr
= ctxt
->inptr
- ctxt
->in
;
553 int d_content
= ctxt
->content
- ctxt
->in
;
554 int d_inrptr
= ctxt
->inrptr
- ctxt
->in
;
555 char *tmp_ptr
= ctxt
->in
;
558 ctxt
->in
= (char *) xmlRealloc(tmp_ptr
, ctxt
->inlen
);
559 if (ctxt
->in
== NULL
) {
560 xmlHTTPErrMemory("allocating input buffer");
565 ctxt
->inptr
= ctxt
->in
+ d_inptr
;
566 ctxt
->content
= ctxt
->in
+ d_content
;
567 ctxt
->inrptr
= ctxt
->in
+ d_inrptr
;
569 ctxt
->last
= recv(ctxt
->fd
, ctxt
->inptr
, XML_NANO_HTTP_CHUNK
, 0);
570 if (ctxt
->last
> 0) {
571 ctxt
->inptr
+= ctxt
->last
;
574 if (ctxt
->last
== 0) {
577 if (ctxt
->last
== -1) {
578 switch (socket_errno()) {
581 #if defined(EAGAIN) && EAGAIN != EWOULDBLOCK
591 __xmlIOErr(XML_FROM_HTTP
, 0, "recv failed\n");
598 if ((poll(&p
, 1, timeout
* 1000) < 1)
604 #else /* !HAVE_POLL_H */
606 if (ctxt
->fd
> FD_SETSIZE
)
615 #pragma warning(push)
616 #pragma warning(disable: 4018)
619 FD_SET(ctxt
->fd
, &rfd
);
625 if ((select(ctxt
->fd
+ 1, &rfd
, NULL
, NULL
, &tv
) < 1)
631 #endif /* !HAVE_POLL_H */
637 * xmlNanoHTTPReadLine:
638 * @ctxt: an HTTP context
640 * Read one line in the HTTP server output, usually for extracting
641 * the HTTP protocol informations from the answer header.
643 * Returns a newly allocated string with a copy of the line, or NULL
644 * which indicate the end of the input.
648 xmlNanoHTTPReadLine(xmlNanoHTTPCtxtPtr ctxt
) {
653 while (bp
- buf
< 4095) {
654 if (ctxt
->inrptr
== ctxt
->inptr
) {
655 if ( (rc
= xmlNanoHTTPRecv(ctxt
)) == 0) {
660 return(xmlMemStrdup(buf
));
662 else if ( rc
== -1 ) {
666 *bp
= *ctxt
->inrptr
++;
669 return(xmlMemStrdup(buf
));
675 return(xmlMemStrdup(buf
));
680 * xmlNanoHTTPScanAnswer:
681 * @ctxt: an HTTP context
682 * @line: an HTTP header line
684 * Try to extract useful informations from the server answer.
685 * We currently parse and process:
686 * - The HTTP revision/ return code
687 * - The Content-Type, Mime-Type and charset used
688 * - The Location for redirect processing.
690 * Returns -1 in case of failure, the file descriptor number otherwise
694 xmlNanoHTTPScanAnswer(xmlNanoHTTPCtxtPtr ctxt
, const char *line
) {
695 const char *cur
= line
;
697 if (line
== NULL
) return;
699 if (!strncmp(line
, "HTTP/", 5)) {
704 while ((*cur
>= '0') && (*cur
<= '9')) {
706 version
+= *cur
- '0';
711 if ((*cur
>= '0') && (*cur
<= '9')) {
713 version
+= *cur
- '0';
716 while ((*cur
>= '0') && (*cur
<= '9'))
720 if ((*cur
!= ' ') && (*cur
!= '\t')) return;
721 while ((*cur
== ' ') || (*cur
== '\t')) cur
++;
722 if ((*cur
< '0') || (*cur
> '9')) return;
723 while ((*cur
>= '0') && (*cur
<= '9')) {
728 if ((*cur
!= 0) && (*cur
!= ' ') && (*cur
!= '\t')) return;
729 ctxt
->returnValue
= ret
;
730 ctxt
->version
= version
;
731 } else if (!xmlStrncasecmp(BAD_CAST line
, BAD_CAST
"Content-Type:", 13)) {
732 const xmlChar
*charset
, *last
, *mime
;
734 while ((*cur
== ' ') || (*cur
== '\t')) cur
++;
735 if (ctxt
->contentType
!= NULL
)
736 xmlFree(ctxt
->contentType
);
737 ctxt
->contentType
= xmlMemStrdup(cur
);
738 mime
= (const xmlChar
*) cur
;
740 while ((*last
!= 0) && (*last
!= ' ') && (*last
!= '\t') &&
741 (*last
!= ';') && (*last
!= ','))
743 if (ctxt
->mimeType
!= NULL
)
744 xmlFree(ctxt
->mimeType
);
745 ctxt
->mimeType
= (char *) xmlStrndup(mime
, last
- mime
);
746 charset
= xmlStrstr(BAD_CAST ctxt
->contentType
, BAD_CAST
"charset=");
747 if (charset
!= NULL
) {
750 while ((*last
!= 0) && (*last
!= ' ') && (*last
!= '\t') &&
751 (*last
!= ';') && (*last
!= ','))
753 if (ctxt
->encoding
!= NULL
)
754 xmlFree(ctxt
->encoding
);
755 ctxt
->encoding
= (char *) xmlStrndup(charset
, last
- charset
);
757 } else if (!xmlStrncasecmp(BAD_CAST line
, BAD_CAST
"ContentType:", 12)) {
758 const xmlChar
*charset
, *last
, *mime
;
760 if (ctxt
->contentType
!= NULL
) return;
761 while ((*cur
== ' ') || (*cur
== '\t')) cur
++;
762 ctxt
->contentType
= xmlMemStrdup(cur
);
763 mime
= (const xmlChar
*) cur
;
765 while ((*last
!= 0) && (*last
!= ' ') && (*last
!= '\t') &&
766 (*last
!= ';') && (*last
!= ','))
768 if (ctxt
->mimeType
!= NULL
)
769 xmlFree(ctxt
->mimeType
);
770 ctxt
->mimeType
= (char *) xmlStrndup(mime
, last
- mime
);
771 charset
= xmlStrstr(BAD_CAST ctxt
->contentType
, BAD_CAST
"charset=");
772 if (charset
!= NULL
) {
775 while ((*last
!= 0) && (*last
!= ' ') && (*last
!= '\t') &&
776 (*last
!= ';') && (*last
!= ','))
778 if (ctxt
->encoding
!= NULL
)
779 xmlFree(ctxt
->encoding
);
780 ctxt
->encoding
= (char *) xmlStrndup(charset
, last
- charset
);
782 } else if (!xmlStrncasecmp(BAD_CAST line
, BAD_CAST
"Location:", 9)) {
784 while ((*cur
== ' ') || (*cur
== '\t')) cur
++;
785 if (ctxt
->location
!= NULL
)
786 xmlFree(ctxt
->location
);
788 xmlChar
*tmp_http
= xmlStrdup(BAD_CAST
"http://");
790 xmlStrcat(tmp_http
, (const xmlChar
*) ctxt
->hostname
);
792 (char *) xmlStrcat (tmp_loc
, (const xmlChar
*) cur
);
794 ctxt
->location
= xmlMemStrdup(cur
);
796 } else if (!xmlStrncasecmp(BAD_CAST line
, BAD_CAST
"WWW-Authenticate:", 17)) {
798 while ((*cur
== ' ') || (*cur
== '\t')) cur
++;
799 if (ctxt
->authHeader
!= NULL
)
800 xmlFree(ctxt
->authHeader
);
801 ctxt
->authHeader
= xmlMemStrdup(cur
);
802 } else if (!xmlStrncasecmp(BAD_CAST line
, BAD_CAST
"Proxy-Authenticate:", 19)) {
804 while ((*cur
== ' ') || (*cur
== '\t')) cur
++;
805 if (ctxt
->authHeader
!= NULL
)
806 xmlFree(ctxt
->authHeader
);
807 ctxt
->authHeader
= xmlMemStrdup(cur
);
809 } else if ( !xmlStrncasecmp( BAD_CAST line
, BAD_CAST
"Content-Encoding:", 17) ) {
811 while ((*cur
== ' ') || (*cur
== '\t')) cur
++;
812 if ( !xmlStrncasecmp( BAD_CAST cur
, BAD_CAST
"gzip", 4) ) {
815 ctxt
->strm
= xmlMalloc(sizeof(z_stream
));
817 if (ctxt
->strm
!= NULL
) {
818 ctxt
->strm
->zalloc
= Z_NULL
;
819 ctxt
->strm
->zfree
= Z_NULL
;
820 ctxt
->strm
->opaque
= Z_NULL
;
821 ctxt
->strm
->avail_in
= 0;
822 ctxt
->strm
->next_in
= Z_NULL
;
824 inflateInit2( ctxt
->strm
, 31 );
828 } else if ( !xmlStrncasecmp( BAD_CAST line
, BAD_CAST
"Content-Length:", 15) ) {
830 ctxt
->ContentLength
= strtol( cur
, NULL
, 10 );
835 * xmlNanoHTTPConnectAttempt:
836 * @addr: a socket address structure
838 * Attempt a connection to the given IP:port endpoint. It forces
839 * non-blocking semantic on the socket, and allow 60 seconds for
840 * the host to answer.
842 * Returns -1 in case of failure, the file descriptor number otherwise
846 xmlNanoHTTPConnectAttempt(struct sockaddr
*addr
)
854 #else /* !HAVE_POLL_H */
856 #endif /* !HAVE_POLL_H */
864 if (addr
->sa_family
== AF_INET6
) {
865 s
= socket(PF_INET6
, SOCK_STREAM
, IPPROTO_TCP
);
866 addrlen
= sizeof(struct sockaddr_in6
);
870 s
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
871 addrlen
= sizeof(struct sockaddr_in
);
877 __xmlIOErr(XML_FROM_HTTP
, 0, "socket failed\n");
884 status
= ioctlsocket(s
, FIONBIO
, &one
) == SOCKET_ERROR
? -1 : 0;
886 #else /* _WINSOCKAPI_ */
891 status
= ioctl(s
, FIONBIO
, &enable
);
894 #if defined(__BEOS__) && !defined(__HAIKU__)
899 setsockopt(s
, SOL_SOCKET
, SO_NONBLOCK
, &noblock
,
903 if ((status
= fcntl(s
, F_GETFL
, 0)) != -1) {
905 status
|= O_NONBLOCK
;
906 #else /* O_NONBLOCK */
909 #endif /* F_NDELAY */
910 #endif /* !O_NONBLOCK */
911 status
= fcntl(s
, F_SETFL
, status
);
915 perror("nonblocking");
917 __xmlIOErr(XML_FROM_HTTP
, 0, "error setting non-blocking IO\n");
921 #endif /* !__BEOS__ */
923 #endif /* !_WINSOCKAPI_ */
925 if (connect(s
, addr
, addrlen
) == -1) {
926 switch (socket_errno()) {
931 __xmlIOErr(XML_FROM_HTTP
, 0,
932 "error connecting to HTTP server");
942 #pragma warning(push)
943 #pragma warning(disable: 4018)
956 switch (select(s
+ 1, NULL
, &wfd
, &xfd
, &tv
))
958 switch (select(s
+ 1, NULL
, &wfd
, NULL
, &tv
))
964 #else /* !HAVE_POLL_H */
967 switch (poll(&p
, 1, timeout
* 1000))
968 #endif /* !HAVE_POLL_H */
973 __xmlIOErr(XML_FROM_HTTP
, 0, "Connect attempt timed out");
978 __xmlIOErr(XML_FROM_HTTP
, 0, "Connect failed");
984 if (FD_ISSET(s
, &wfd
)
989 #else /* !HAVE_POLL_H */
990 if (p
.revents
== POLLOUT
)
991 #endif /* !HAVE_POLL_H */
995 len
= sizeof(status
);
997 if (getsockopt(s
, SOL_SOCKET
, SO_ERROR
, (char *) &status
, &len
) <
999 /* Solaris error code */
1000 __xmlIOErr(XML_FROM_HTTP
, 0, "getsockopt failed\n");
1005 __xmlIOErr(XML_FROM_HTTP
, 0,
1006 "Error connecting to remote host");
1013 __xmlIOErr(XML_FROM_HTTP
, 0, "select failed\n");
1022 * xmlNanoHTTPConnectHost:
1023 * @host: the host name
1024 * @port: the port number
1026 * Attempt a connection to the given host:port endpoint. It tries
1027 * the multiple IP provided by the DNS if available.
1029 * Returns -1 in case of failure, the file descriptor number otherwise
1033 xmlNanoHTTPConnectHost(const char *host
, int port
)
1036 struct sockaddr
*addr
= NULL
;
1038 struct sockaddr_in sockin
;
1041 struct in6_addr ia6
;
1042 struct sockaddr_in6 sockin6
;
1047 memset (&sockin
, 0, sizeof(sockin
));
1049 memset (&sockin6
, 0, sizeof(sockin6
));
1052 #if !defined(HAVE_GETADDRINFO) && defined(SUPPORT_IP6) && defined(RES_USE_INET6)
1055 if (!(_res
.options
& RES_INIT
))
1057 _res
.options
|= RES_USE_INET6
;
1061 #if defined(HAVE_GETADDRINFO) && defined(SUPPORT_IP6) && !defined(_WIN32)
1064 #if defined(HAVE_GETADDRINFO) && (defined(SUPPORT_IP6) || defined(_WIN32))
1067 struct addrinfo hints
, *res
, *result
;
1070 memset (&hints
, 0,sizeof(hints
));
1071 hints
.ai_socktype
= SOCK_STREAM
;
1073 status
= getaddrinfo (host
, NULL
, &hints
, &result
);
1075 __xmlIOErr(XML_FROM_HTTP
, 0, "getaddrinfo failed\n");
1079 for (res
= result
; res
; res
= res
->ai_next
) {
1080 if (res
->ai_family
== AF_INET
) {
1081 if (res
->ai_addrlen
> sizeof(sockin
)) {
1082 __xmlIOErr(XML_FROM_HTTP
, 0, "address size mismatch\n");
1083 freeaddrinfo (result
);
1086 memcpy (&sockin
, res
->ai_addr
, res
->ai_addrlen
);
1087 sockin
.sin_port
= htons (port
);
1088 addr
= (struct sockaddr
*)&sockin
;
1090 } else if (have_ipv6 () && (res
->ai_family
== AF_INET6
)) {
1091 if (res
->ai_addrlen
> sizeof(sockin6
)) {
1092 __xmlIOErr(XML_FROM_HTTP
, 0, "address size mismatch\n");
1093 freeaddrinfo (result
);
1096 memcpy (&sockin6
, res
->ai_addr
, res
->ai_addrlen
);
1097 sockin6
.sin6_port
= htons (port
);
1098 addr
= (struct sockaddr
*)&sockin6
;
1103 s
= xmlNanoHTTPConnectAttempt (addr
);
1105 freeaddrinfo (result
);
1111 freeaddrinfo (result
);
1114 #if defined(HAVE_GETADDRINFO) && defined(SUPPORT_IP6) && !defined(_WIN32)
1117 #if !defined(HAVE_GETADDRINFO) || !defined(_WIN32)
1119 h
= gethostbyname (host
);
1123 * Okay, I got fed up by the non-portability of this error message
1124 * extraction code. it work on Linux, if it work on your platform
1125 * and one want to enable it, send me the defined(foobar) needed
1127 #if defined(HAVE_NETDB_H) && defined(HOST_NOT_FOUND) && defined(linux)
1128 const char *h_err_txt
= "";
1131 case HOST_NOT_FOUND
:
1132 h_err_txt
= "Authoritive host not found";
1137 "Non-authoritive host not found or server failure.";
1142 "Non-recoverable errors: FORMERR, REFUSED, or NOTIMP.";
1147 "Valid name, no data record of requested type.";
1151 h_err_txt
= "No error text defined.";
1154 __xmlIOErr(XML_FROM_HTTP
, 0, h_err_txt
);
1156 __xmlIOErr(XML_FROM_HTTP
, 0, "Failed to resolve host");
1161 for (i
= 0; h
->h_addr_list
[i
]; i
++) {
1162 if (h
->h_addrtype
== AF_INET
) {
1163 /* A records (IPv4) */
1164 if ((unsigned int) h
->h_length
> sizeof(ia
)) {
1165 __xmlIOErr(XML_FROM_HTTP
, 0, "address size mismatch\n");
1168 memcpy (&ia
, h
->h_addr_list
[i
], h
->h_length
);
1169 sockin
.sin_family
= h
->h_addrtype
;
1170 sockin
.sin_addr
= ia
;
1171 sockin
.sin_port
= (u_short
)htons ((unsigned short)port
);
1172 addr
= (struct sockaddr
*) &sockin
;
1174 } else if (have_ipv6 () && (h
->h_addrtype
== AF_INET6
)) {
1175 /* AAAA records (IPv6) */
1176 if ((unsigned int) h
->h_length
> sizeof(ia6
)) {
1177 __xmlIOErr(XML_FROM_HTTP
, 0, "address size mismatch\n");
1180 memcpy (&ia6
, h
->h_addr_list
[i
], h
->h_length
);
1181 sockin6
.sin6_family
= h
->h_addrtype
;
1182 sockin6
.sin6_addr
= ia6
;
1183 sockin6
.sin6_port
= htons (port
);
1184 addr
= (struct sockaddr
*) &sockin6
;
1189 s
= xmlNanoHTTPConnectAttempt (addr
);
1197 xmlGenericError(xmlGenericErrorContext
,
1198 "xmlNanoHTTPConnectHost: unable to connect to '%s'.\n",
1207 * @URL: The URL to load
1208 * @contentType: if available the Content-Type information will be
1209 * returned at that location
1211 * This function try to open a connection to the indicated resource
1214 * Returns NULL in case of failure, otherwise a request handler.
1215 * The contentType, if provided must be freed by the caller
1219 xmlNanoHTTPOpen(const char *URL
, char **contentType
) {
1220 if (contentType
!= NULL
) *contentType
= NULL
;
1221 return(xmlNanoHTTPMethod(URL
, NULL
, NULL
, contentType
, NULL
, 0));
1225 * xmlNanoHTTPOpenRedir:
1226 * @URL: The URL to load
1227 * @contentType: if available the Content-Type information will be
1228 * returned at that location
1229 * @redir: if available the redirected URL will be returned
1231 * This function try to open a connection to the indicated resource
1234 * Returns NULL in case of failure, otherwise a request handler.
1235 * The contentType, if provided must be freed by the caller
1239 xmlNanoHTTPOpenRedir(const char *URL
, char **contentType
, char **redir
) {
1240 if (contentType
!= NULL
) *contentType
= NULL
;
1241 if (redir
!= NULL
) *redir
= NULL
;
1242 return(xmlNanoHTTPMethodRedir(URL
, NULL
, NULL
, contentType
, redir
, NULL
,0));
1247 * @ctx: the HTTP context
1249 * @len: the buffer length
1251 * This function tries to read @len bytes from the existing HTTP connection
1252 * and saves them in @dest. This is a blocking call.
1254 * Returns the number of byte read. 0 is an indication of an end of connection.
1255 * -1 indicates a parameter error.
1258 xmlNanoHTTPRead(void *ctx
, void *dest
, int len
) {
1259 xmlNanoHTTPCtxtPtr ctxt
= (xmlNanoHTTPCtxtPtr
) ctx
;
1266 if (ctx
== NULL
) return(-1);
1267 if (dest
== NULL
) return(-1);
1268 if (len
<= 0) return(0);
1271 if (ctxt
->usesGzip
== 1) {
1272 if (ctxt
->strm
== NULL
) return(0);
1274 ctxt
->strm
->next_out
= dest
;
1275 ctxt
->strm
->avail_out
= len
;
1276 ctxt
->strm
->avail_in
= ctxt
->inptr
- ctxt
->inrptr
;
1278 while (ctxt
->strm
->avail_out
> 0 &&
1279 (ctxt
->strm
->avail_in
> 0 || xmlNanoHTTPRecv(ctxt
) > 0)) {
1280 orig_avail_in
= ctxt
->strm
->avail_in
=
1281 ctxt
->inptr
- ctxt
->inrptr
- bytes_read
;
1282 ctxt
->strm
->next_in
= BAD_CAST (ctxt
->inrptr
+ bytes_read
);
1284 z_ret
= inflate(ctxt
->strm
, Z_NO_FLUSH
);
1285 bytes_read
+= orig_avail_in
- ctxt
->strm
->avail_in
;
1287 if (z_ret
!= Z_OK
) break;
1290 ctxt
->inrptr
+= bytes_read
;
1291 return(len
- ctxt
->strm
->avail_out
);
1295 while (ctxt
->inptr
- ctxt
->inrptr
< len
) {
1296 if (xmlNanoHTTPRecv(ctxt
) <= 0) break;
1298 if (ctxt
->inptr
- ctxt
->inrptr
< len
)
1299 len
= ctxt
->inptr
- ctxt
->inrptr
;
1300 memcpy(dest
, ctxt
->inrptr
, len
);
1301 ctxt
->inrptr
+= len
;
1307 * @ctx: the HTTP context
1309 * This function closes an HTTP context, it ends up the connection and
1310 * free all data related to it.
1313 xmlNanoHTTPClose(void *ctx
) {
1314 xmlNanoHTTPCtxtPtr ctxt
= (xmlNanoHTTPCtxtPtr
) ctx
;
1316 if (ctx
== NULL
) return;
1318 xmlNanoHTTPFreeCtxt(ctxt
);
1322 * xmlNanoHTTPMethodRedir:
1323 * @URL: The URL to load
1324 * @method: the HTTP method to use
1325 * @input: the input string if any
1326 * @contentType: the Content-Type information IN and OUT
1327 * @redir: the redirected URL OUT
1328 * @headers: the extra headers
1329 * @ilen: input length
1331 * This function try to open a connection to the indicated resource
1332 * via HTTP using the given @method, adding the given extra headers
1333 * and the input buffer for the request content.
1335 * Returns NULL in case of failure, otherwise a request handler.
1336 * The contentType, or redir, if provided must be freed by the caller
1340 xmlNanoHTTPMethodRedir(const char *URL
, const char *method
, const char *input
,
1341 char **contentType
, char **redir
,
1342 const char *headers
, int ilen
) {
1343 xmlNanoHTTPCtxtPtr ctxt
;
1346 int nbRedirects
= 0;
1347 char *redirURL
= NULL
;
1352 if (URL
== NULL
) return(NULL
);
1353 if (method
== NULL
) method
= "GET";
1357 if (redirURL
== NULL
)
1358 ctxt
= xmlNanoHTTPNewCtxt(URL
);
1360 ctxt
= xmlNanoHTTPNewCtxt(redirURL
);
1361 ctxt
->location
= xmlMemStrdup(redirURL
);
1364 if ( ctxt
== NULL
) {
1368 if ((ctxt
->protocol
== NULL
) || (strcmp(ctxt
->protocol
, "http"))) {
1369 __xmlIOErr(XML_FROM_HTTP
, XML_HTTP_URL_SYNTAX
, "Not a valid HTTP URI");
1370 xmlNanoHTTPFreeCtxt(ctxt
);
1371 if (redirURL
!= NULL
) xmlFree(redirURL
);
1374 if (ctxt
->hostname
== NULL
) {
1375 __xmlIOErr(XML_FROM_HTTP
, XML_HTTP_UNKNOWN_HOST
,
1376 "Failed to identify host in URI");
1377 xmlNanoHTTPFreeCtxt(ctxt
);
1378 if (redirURL
!= NULL
) xmlFree(redirURL
);
1382 blen
= strlen(ctxt
->hostname
) * 2 + 16;
1383 ret
= xmlNanoHTTPConnectHost(proxy
, proxyPort
);
1386 blen
= strlen(ctxt
->hostname
);
1387 ret
= xmlNanoHTTPConnectHost(ctxt
->hostname
, ctxt
->port
);
1390 xmlNanoHTTPFreeCtxt(ctxt
);
1391 if (redirURL
!= NULL
) xmlFree(redirURL
);
1401 if (headers
!= NULL
)
1402 blen
+= strlen(headers
) + 2;
1403 if (contentType
&& *contentType
)
1404 /* reserve for string plus 'Content-Type: \r\n" */
1405 blen
+= strlen(*contentType
) + 16;
1406 if (ctxt
->query
!= NULL
)
1408 blen
+= strlen(ctxt
->query
) + 1;
1409 blen
+= strlen(method
) + strlen(ctxt
->path
) + 24;
1411 /* reserve for possible 'Accept-Encoding: gzip' string */
1414 if (ctxt
->port
!= 80) {
1415 /* reserve space for ':xxxxx', incl. potential proxy */
1421 bp
= (char*)xmlMallocAtomic(blen
);
1423 xmlNanoHTTPFreeCtxt( ctxt
);
1424 xmlHTTPErrMemory("allocating header buffer");
1431 if (ctxt
->port
!= 80) {
1432 p
+= snprintf( p
, blen
- (p
- bp
), "%s http://%s:%d%s",
1433 method
, ctxt
->hostname
,
1434 ctxt
->port
, ctxt
->path
);
1437 p
+= snprintf( p
, blen
- (p
- bp
), "%s http://%s%s", method
,
1438 ctxt
->hostname
, ctxt
->path
);
1441 p
+= snprintf( p
, blen
- (p
- bp
), "%s %s", method
, ctxt
->path
);
1443 if (ctxt
->query
!= NULL
)
1444 p
+= snprintf( p
, blen
- (p
- bp
), "?%s", ctxt
->query
);
1446 if (ctxt
->port
== 80) {
1447 p
+= snprintf( p
, blen
- (p
- bp
), " HTTP/1.0\r\nHost: %s\r\n",
1450 p
+= snprintf( p
, blen
- (p
- bp
), " HTTP/1.0\r\nHost: %s:%d\r\n",
1451 ctxt
->hostname
, ctxt
->port
);
1455 p
+= snprintf(p
, blen
- (p
- bp
), "Accept-Encoding: gzip\r\n");
1458 if (contentType
!= NULL
&& *contentType
)
1459 p
+= snprintf(p
, blen
- (p
- bp
), "Content-Type: %s\r\n", *contentType
);
1461 if (headers
!= NULL
)
1462 p
+= snprintf( p
, blen
- (p
- bp
), "%s", headers
);
1465 snprintf(p
, blen
- (p
- bp
), "Content-Length: %d\r\n\r\n", ilen
);
1467 snprintf(p
, blen
- (p
- bp
), "\r\n");
1470 xmlGenericError(xmlGenericErrorContext
,
1471 "-> %s%s", proxy
? "(Proxy) " : "", bp
);
1472 if ((blen
-= strlen(bp
)+1) < 0)
1473 xmlGenericError(xmlGenericErrorContext
,
1474 "ERROR: overflowed buffer by %d bytes\n", -blen
);
1476 ctxt
->outptr
= ctxt
->out
= bp
;
1477 ctxt
->state
= XML_NANO_HTTP_WRITE
;
1478 blen
= strlen( ctxt
->out
);
1480 xmt_bytes
= xmlNanoHTTPSend(ctxt
, ctxt
->out
, blen
);
1481 if ( xmt_bytes
!= blen
)
1482 xmlGenericError( xmlGenericErrorContext
,
1483 "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
1485 "bytes of HTTP headers sent to host",
1488 xmlNanoHTTPSend(ctxt
, ctxt
->out
, blen
);
1491 if ( input
!= NULL
) {
1493 xmt_bytes
= xmlNanoHTTPSend( ctxt
, input
, ilen
);
1495 if ( xmt_bytes
!= ilen
)
1496 xmlGenericError( xmlGenericErrorContext
,
1497 "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
1499 "bytes of HTTP content sent to host",
1502 xmlNanoHTTPSend( ctxt
, input
, ilen
);
1506 ctxt
->state
= XML_NANO_HTTP_READ
;
1508 while ((p
= xmlNanoHTTPReadLine(ctxt
)) != NULL
) {
1510 ctxt
->content
= ctxt
->inrptr
;
1514 xmlNanoHTTPScanAnswer(ctxt
, p
);
1517 xmlGenericError(xmlGenericErrorContext
, "<- %s\n", p
);
1522 if ((ctxt
->location
!= NULL
) && (ctxt
->returnValue
>= 300) &&
1523 (ctxt
->returnValue
< 400)) {
1525 xmlGenericError(xmlGenericErrorContext
,
1526 "\nRedirect to: %s\n", ctxt
->location
);
1528 while ( xmlNanoHTTPRecv(ctxt
) > 0 ) ;
1529 if (nbRedirects
< XML_NANO_HTTP_MAX_REDIR
) {
1531 if (redirURL
!= NULL
)
1533 redirURL
= xmlMemStrdup(ctxt
->location
);
1534 xmlNanoHTTPFreeCtxt(ctxt
);
1537 xmlNanoHTTPFreeCtxt(ctxt
);
1538 if (redirURL
!= NULL
) xmlFree(redirURL
);
1540 xmlGenericError(xmlGenericErrorContext
,
1541 "xmlNanoHTTPMethodRedir: Too many redirects, aborting ...\n");
1546 if (contentType
!= NULL
) {
1547 if (ctxt
->contentType
!= NULL
)
1548 *contentType
= xmlMemStrdup(ctxt
->contentType
);
1550 *contentType
= NULL
;
1553 if ((redir
!= NULL
) && (redirURL
!= NULL
)) {
1556 if (redirURL
!= NULL
)
1563 if (ctxt
->contentType
!= NULL
)
1564 xmlGenericError(xmlGenericErrorContext
,
1565 "\nCode %d, content-type '%s'\n\n",
1566 ctxt
->returnValue
, ctxt
->contentType
);
1568 xmlGenericError(xmlGenericErrorContext
,
1569 "\nCode %d, no content-type\n\n",
1573 return((void *) ctxt
);
1577 * xmlNanoHTTPMethod:
1578 * @URL: The URL to load
1579 * @method: the HTTP method to use
1580 * @input: the input string if any
1581 * @contentType: the Content-Type information IN and OUT
1582 * @headers: the extra headers
1583 * @ilen: input length
1585 * This function try to open a connection to the indicated resource
1586 * via HTTP using the given @method, adding the given extra headers
1587 * and the input buffer for the request content.
1589 * Returns NULL in case of failure, otherwise a request handler.
1590 * The contentType, if provided must be freed by the caller
1594 xmlNanoHTTPMethod(const char *URL
, const char *method
, const char *input
,
1595 char **contentType
, const char *headers
, int ilen
) {
1596 return(xmlNanoHTTPMethodRedir(URL
, method
, input
, contentType
,
1597 NULL
, headers
, ilen
));
1602 * @URL: The URL to load
1603 * @filename: the filename where the content should be saved
1604 * @contentType: if available the Content-Type information will be
1605 * returned at that location
1607 * This function try to fetch the indicated resource via HTTP GET
1608 * and save it's content in the file.
1610 * Returns -1 in case of failure, 0 incase of success. The contentType,
1611 * if provided must be freed by the caller
1614 xmlNanoHTTPFetch(const char *URL
, const char *filename
, char **contentType
) {
1620 if (filename
== NULL
) return(-1);
1621 ctxt
= xmlNanoHTTPOpen(URL
, contentType
);
1622 if (ctxt
== NULL
) return(-1);
1624 if (!strcmp(filename
, "-"))
1627 fd
= open(filename
, O_CREAT
| O_WRONLY
, 00644);
1629 xmlNanoHTTPClose(ctxt
);
1630 if ((contentType
!= NULL
) && (*contentType
!= NULL
)) {
1631 xmlFree(*contentType
);
1632 *contentType
= NULL
;
1638 xmlNanoHTTPFetchContent( ctxt
, &buf
, &len
);
1640 write(fd
, buf
, len
);
1643 xmlNanoHTTPClose(ctxt
);
1648 #ifdef LIBXML_OUTPUT_ENABLED
1651 * @ctxt: the HTTP context
1652 * @filename: the filename where the content should be saved
1654 * This function saves the output of the HTTP transaction to a file
1655 * It closes and free the context at the end
1657 * Returns -1 in case of failure, 0 incase of success.
1660 xmlNanoHTTPSave(void *ctxt
, const char *filename
) {
1665 if ((ctxt
== NULL
) || (filename
== NULL
)) return(-1);
1667 if (!strcmp(filename
, "-"))
1670 fd
= open(filename
, O_CREAT
| O_WRONLY
, 0666);
1672 xmlNanoHTTPClose(ctxt
);
1677 xmlNanoHTTPFetchContent( ctxt
, &buf
, &len
);
1679 write(fd
, buf
, len
);
1682 xmlNanoHTTPClose(ctxt
);
1686 #endif /* LIBXML_OUTPUT_ENABLED */
1689 * xmlNanoHTTPReturnCode:
1690 * @ctx: the HTTP context
1692 * Get the latest HTTP return code received
1694 * Returns the HTTP return code for the request.
1697 xmlNanoHTTPReturnCode(void *ctx
) {
1698 xmlNanoHTTPCtxtPtr ctxt
= (xmlNanoHTTPCtxtPtr
) ctx
;
1700 if (ctxt
== NULL
) return(-1);
1702 return(ctxt
->returnValue
);
1706 * xmlNanoHTTPAuthHeader:
1707 * @ctx: the HTTP context
1709 * Get the authentication header of an HTTP context
1711 * Returns the stashed value of the WWW-Authenticate or Proxy-Authenticate
1715 xmlNanoHTTPAuthHeader(void *ctx
) {
1716 xmlNanoHTTPCtxtPtr ctxt
= (xmlNanoHTTPCtxtPtr
) ctx
;
1718 if (ctxt
== NULL
) return(NULL
);
1720 return(ctxt
->authHeader
);
1724 * xmlNanoHTTPContentLength:
1725 * @ctx: the HTTP context
1727 * Provides the specified content length from the HTTP header.
1729 * Return the specified content length from the HTTP header. Note that
1730 * a value of -1 indicates that the content length element was not included in
1731 * the response header.
1734 xmlNanoHTTPContentLength( void * ctx
) {
1735 xmlNanoHTTPCtxtPtr ctxt
= (xmlNanoHTTPCtxtPtr
)ctx
;
1737 return ( ( ctxt
== NULL
) ? -1 : ctxt
->ContentLength
);
1742 * @ctx: the HTTP context
1744 * Provides the specified redirection URL if available from the HTTP header.
1746 * Return the specified redirection URL or NULL if not redirected.
1749 xmlNanoHTTPRedir( void * ctx
) {
1750 xmlNanoHTTPCtxtPtr ctxt
= (xmlNanoHTTPCtxtPtr
)ctx
;
1752 return ( ( ctxt
== NULL
) ? NULL
: ctxt
->location
);
1756 * xmlNanoHTTPEncoding:
1757 * @ctx: the HTTP context
1759 * Provides the specified encoding if specified in the HTTP headers.
1761 * Return the specified encoding or NULL if not available
1764 xmlNanoHTTPEncoding( void * ctx
) {
1765 xmlNanoHTTPCtxtPtr ctxt
= (xmlNanoHTTPCtxtPtr
)ctx
;
1767 return ( ( ctxt
== NULL
) ? NULL
: ctxt
->encoding
);
1771 * xmlNanoHTTPMimeType:
1772 * @ctx: the HTTP context
1774 * Provides the specified Mime-Type if specified in the HTTP headers.
1776 * Return the specified Mime-Type or NULL if not available
1779 xmlNanoHTTPMimeType( void * ctx
) {
1780 xmlNanoHTTPCtxtPtr ctxt
= (xmlNanoHTTPCtxtPtr
)ctx
;
1782 return ( ( ctxt
== NULL
) ? NULL
: ctxt
->mimeType
);
1786 * xmlNanoHTTPFetchContent:
1787 * @ctx: the HTTP context
1788 * @ptr: pointer to set to the content buffer.
1789 * @len: integer pointer to hold the length of the content
1791 * Check if all the content was read
1793 * Returns 0 if all the content was read and available, returns
1794 * -1 if received content length was less than specified or an error
1798 xmlNanoHTTPFetchContent( void * ctx
, char ** ptr
, int * len
) {
1799 xmlNanoHTTPCtxtPtr ctxt
= (xmlNanoHTTPCtxtPtr
)ctx
;
1805 char * dummy_ptr
= NULL
;
1807 /* Dummy up return input parameters if not provided */
1815 /* But can't work without the context pointer */
1817 if ( ( ctxt
== NULL
) || ( ctxt
->content
== NULL
) ) {
1823 rcvd_lgth
= ctxt
->inptr
- ctxt
->content
;
1825 while ( (cur_lgth
= xmlNanoHTTPRecv( ctxt
)) > 0 ) {
1827 rcvd_lgth
+= cur_lgth
;
1828 if ( (ctxt
->ContentLength
> 0) && (rcvd_lgth
>= ctxt
->ContentLength
) )
1832 *ptr
= ctxt
->content
;
1835 if ( ( ctxt
->ContentLength
> 0 ) && ( rcvd_lgth
< ctxt
->ContentLength
) )
1837 else if ( rcvd_lgth
== 0 )
1844 int main(int argc
, char **argv
) {
1845 char *contentType
= NULL
;
1847 if (argv
[1] != NULL
) {
1848 if (argv
[2] != NULL
)
1849 xmlNanoHTTPFetch(argv
[1], argv
[2], &contentType
);
1851 xmlNanoHTTPFetch(argv
[1], "-", &contentType
);
1852 if (contentType
!= NULL
) xmlFree(contentType
);
1854 xmlGenericError(xmlGenericErrorContext
,
1855 "%s: minimal HTTP GET implementation\n", argv
[0]);
1856 xmlGenericError(xmlGenericErrorContext
,
1857 "\tusage %s [ URL [ filename ] ]\n", argv
[0]);
1859 xmlNanoHTTPCleanup();
1863 #endif /* STANDALONE */
1864 #else /* !LIBXML_HTTP_ENABLED */
1867 int main(int argc
, char **argv
) {
1868 xmlGenericError(xmlGenericErrorContext
,
1869 "%s : HTTP support not compiled in\n", argv
[0]);
1872 #endif /* STANDALONE */
1873 #endif /* LIBXML_HTTP_ENABLED */
1874 #define bottom_nanohttp
1875 #include "elfgcchack.h"