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>
74 #define XML_SOCKLEN_T unsigned int
77 #if defined(__MINGW32__) || defined(_WIN32_WCE)
81 #include <wsockcompat.h>
84 #define XML_SOCKLEN_T unsigned int
87 #include <libxml/globals.h>
88 #include <libxml/xmlerror.h>
89 #include <libxml/xmlmemory.h>
90 #include <libxml/parser.h> /* for xmlStr(n)casecmp() */
91 #include <libxml/nanohttp.h>
92 #include <libxml/globals.h>
93 #include <libxml/uri.h>
96 * A couple portability macros
99 #if !defined(__BEOS__) || defined(__HAIKU__)
100 #define closesocket(s) close(s)
103 #define INVALID_SOCKET (-1)
108 #define PF_INET AF_INET
112 #ifndef XML_SOCKLEN_T
113 #define XML_SOCKLEN_T unsigned int
118 #define xmlStrncasecmp(a, b, n) strncasecmp((char *)a, (char *)b, n)
119 #define xmlStrcasecmpi(a, b) strcasecmp((char *)a, (char *)b)
122 #define XML_NANO_HTTP_MAX_REDIR 10
124 #define XML_NANO_HTTP_CHUNK 4096
126 #define XML_NANO_HTTP_CLOSED 0
127 #define XML_NANO_HTTP_WRITE 1
128 #define XML_NANO_HTTP_READ 2
129 #define XML_NANO_HTTP_NONE 4
131 typedef struct xmlNanoHTTPCtxt
{
132 char *protocol
; /* the protocol name */
133 char *hostname
; /* the host name */
134 int port
; /* the port */
135 char *path
; /* the path within the URL */
136 char *query
; /* the query string */
137 SOCKET fd
; /* the file descriptor for the socket */
138 int state
; /* WRITE / READ / CLOSED */
139 char *out
; /* buffer sent (zero terminated) */
140 char *outptr
; /* index within the buffer sent */
141 char *in
; /* the receiving buffer */
142 char *content
; /* the start of the content */
143 char *inptr
; /* the next byte to read from network */
144 char *inrptr
; /* the next byte to give back to the client */
145 int inlen
; /* len of the input buffer */
146 int last
; /* return code for last operation */
147 int returnValue
; /* the protocol return value */
148 int version
; /* the protocol version */
149 int ContentLength
; /* specified content length from HTTP header */
150 char *contentType
; /* the MIME type for the input */
151 char *location
; /* the new URL in case of redirect */
152 char *authHeader
; /* contents of {WWW,Proxy}-Authenticate header */
153 char *encoding
; /* encoding extracted from the contentType */
154 char *mimeType
; /* Mime-Type extracted from the contentType */
156 z_stream
*strm
; /* Zlib stream object */
157 int usesGzip
; /* "Content-Encoding: gzip" was detected */
159 } xmlNanoHTTPCtxt
, *xmlNanoHTTPCtxtPtr
;
161 static int initialized
= 0;
162 static char *proxy
= NULL
; /* the proxy name if any */
163 static int proxyPort
; /* the proxy port if any */
164 static unsigned int timeout
= 60;/* the select() timeout in seconds */
166 static int xmlNanoHTTPFetchContent( void * ctx
, char ** ptr
, int * len
);
170 * @extra: extra informations
172 * Handle an out of memory condition
175 xmlHTTPErrMemory(const char *extra
)
177 __xmlSimpleError(XML_FROM_HTTP
, XML_ERR_NO_MEMORY
, NULL
, NULL
, extra
);
181 * A portability function
183 static int socket_errno(void) {
185 return(WSAGetLastError());
193 int have_ipv6(void) {
196 s
= socket (AF_INET6
, SOCK_STREAM
, 0);
197 if (s
!= INVALID_SOCKET
) {
208 * Initialize the HTTP protocol layer.
209 * Currently it just checks for proxy informations
213 xmlNanoHTTPInit(void) {
223 if (WSAStartup(MAKEWORD(1, 1), &wsaData
) != 0)
229 env
= getenv("no_proxy");
230 if (env
&& ((env
[0] == '*') && (env
[1] == 0)))
232 env
= getenv("http_proxy");
234 xmlNanoHTTPScanProxy(env
);
237 env
= getenv("HTTP_PROXY");
239 xmlNanoHTTPScanProxy(env
);
248 * xmlNanoHTTPCleanup:
250 * Cleanup the HTTP protocol layer.
254 xmlNanoHTTPCleanup(void) {
268 * xmlNanoHTTPScanURL:
269 * @ctxt: an HTTP context
270 * @URL: The URL used to initialize the context
272 * (Re)Initialize an HTTP context by parsing the URL and finding
273 * the protocol host port and path it indicates.
277 xmlNanoHTTPScanURL(xmlNanoHTTPCtxtPtr ctxt
, const char *URL
) {
282 * Clear any existing data from the context
284 if (ctxt
->protocol
!= NULL
) {
285 xmlFree(ctxt
->protocol
);
286 ctxt
->protocol
= NULL
;
288 if (ctxt
->hostname
!= NULL
) {
289 xmlFree(ctxt
->hostname
);
290 ctxt
->hostname
= NULL
;
292 if (ctxt
->path
!= NULL
) {
296 if (ctxt
->query
!= NULL
) {
297 xmlFree(ctxt
->query
);
300 if (URL
== NULL
) return;
302 uri
= xmlParseURIRaw(URL
, 1);
306 if ((uri
->scheme
== NULL
) || (uri
->server
== NULL
)) {
311 ctxt
->protocol
= xmlMemStrdup(uri
->scheme
);
312 /* special case of IPv6 addresses, the [] need to be removed */
313 if ((uri
->server
!= NULL
) && (*uri
->server
== '[')) {
314 len
= strlen(uri
->server
);
315 if ((len
> 2) && (uri
->server
[len
- 1] == ']')) {
316 ctxt
->hostname
= (char *) xmlCharStrndup(uri
->server
+ 1, len
-2);
318 ctxt
->hostname
= xmlMemStrdup(uri
->server
);
320 ctxt
->hostname
= xmlMemStrdup(uri
->server
);
321 if (uri
->path
!= NULL
)
322 ctxt
->path
= xmlMemStrdup(uri
->path
);
324 ctxt
->path
= xmlMemStrdup("/");
325 if (uri
->query
!= NULL
)
326 ctxt
->query
= xmlMemStrdup(uri
->query
);
328 ctxt
->port
= uri
->port
;
334 * xmlNanoHTTPScanProxy:
335 * @URL: The proxy URL used to initialize the proxy context
337 * (Re)Initialize the HTTP Proxy context by parsing the URL and finding
338 * the protocol host port it indicates.
339 * Should be like http://myproxy/ or http://myproxy:3128/
340 * A NULL URL cleans up proxy informations.
344 xmlNanoHTTPScanProxy(const char *URL
) {
355 xmlGenericError(xmlGenericErrorContext
,
356 "Removing HTTP proxy info\n");
358 xmlGenericError(xmlGenericErrorContext
,
359 "Using HTTP proxy %s\n", URL
);
361 if (URL
== NULL
) return;
363 uri
= xmlParseURIRaw(URL
, 1);
364 if ((uri
== NULL
) || (uri
->scheme
== NULL
) ||
365 (strcmp(uri
->scheme
, "http")) || (uri
->server
== NULL
)) {
366 __xmlIOErr(XML_FROM_HTTP
, XML_HTTP_URL_SYNTAX
, "Syntax Error\n");
372 proxy
= xmlMemStrdup(uri
->server
);
374 proxyPort
= uri
->port
;
380 * xmlNanoHTTPNewCtxt:
381 * @URL: The URL used to initialize the context
383 * Allocate and initialize a new HTTP context.
385 * Returns an HTTP context or NULL in case of error.
388 static xmlNanoHTTPCtxtPtr
389 xmlNanoHTTPNewCtxt(const char *URL
) {
390 xmlNanoHTTPCtxtPtr ret
;
392 ret
= (xmlNanoHTTPCtxtPtr
) xmlMalloc(sizeof(xmlNanoHTTPCtxt
));
394 xmlHTTPErrMemory("allocating context");
398 memset(ret
, 0, sizeof(xmlNanoHTTPCtxt
));
400 ret
->returnValue
= 0;
401 ret
->fd
= INVALID_SOCKET
;
402 ret
->ContentLength
= -1;
404 xmlNanoHTTPScanURL(ret
, URL
);
410 * xmlNanoHTTPFreeCtxt:
411 * @ctxt: an HTTP context
413 * Frees the context after closing the connection.
417 xmlNanoHTTPFreeCtxt(xmlNanoHTTPCtxtPtr ctxt
) {
418 if (ctxt
== NULL
) return;
419 if (ctxt
->hostname
!= NULL
) xmlFree(ctxt
->hostname
);
420 if (ctxt
->protocol
!= NULL
) xmlFree(ctxt
->protocol
);
421 if (ctxt
->path
!= NULL
) xmlFree(ctxt
->path
);
422 if (ctxt
->query
!= NULL
) xmlFree(ctxt
->query
);
423 if (ctxt
->out
!= NULL
) xmlFree(ctxt
->out
);
424 if (ctxt
->in
!= NULL
) xmlFree(ctxt
->in
);
425 if (ctxt
->contentType
!= NULL
) xmlFree(ctxt
->contentType
);
426 if (ctxt
->encoding
!= NULL
) xmlFree(ctxt
->encoding
);
427 if (ctxt
->mimeType
!= NULL
) xmlFree(ctxt
->mimeType
);
428 if (ctxt
->location
!= NULL
) xmlFree(ctxt
->location
);
429 if (ctxt
->authHeader
!= NULL
) xmlFree(ctxt
->authHeader
);
431 if (ctxt
->strm
!= NULL
) {
432 inflateEnd(ctxt
->strm
);
437 ctxt
->state
= XML_NANO_HTTP_NONE
;
438 if (ctxt
->fd
!= INVALID_SOCKET
) closesocket(ctxt
->fd
);
439 ctxt
->fd
= INVALID_SOCKET
;
445 * @ctxt: an HTTP context
447 * Send the input needed to initiate the processing on the server side
448 * Returns number of bytes sent or -1 on error.
452 xmlNanoHTTPSend(xmlNanoHTTPCtxtPtr ctxt
, const char *xmt_ptr
, int outlen
)
462 if ((ctxt
->state
& XML_NANO_HTTP_WRITE
) && (xmt_ptr
!= NULL
)) {
463 while (total_sent
< outlen
) {
464 int nsent
= send(ctxt
->fd
, xmt_ptr
+ total_sent
,
465 outlen
- total_sent
, 0);
469 else if ((nsent
== -1) &&
470 #if defined(EAGAIN) && EAGAIN != EWOULDBLOCK
471 (socket_errno() != EAGAIN
) &&
473 (socket_errno() != EWOULDBLOCK
)) {
474 __xmlIOErr(XML_FROM_HTTP
, 0, "send failed\n");
481 * Since non-blocking sockets are used, wait for
482 * socket to be writable or default timeout prior
487 if (ctxt
->fd
> FD_SETSIZE
)
495 #pragma warning(push)
496 #pragma warning(disable: 4018)
498 FD_SET(ctxt
->fd
, &wfd
);
502 (void) select(ctxt
->fd
+ 1, NULL
, &wfd
, NULL
, &tv
);
506 (void) poll(&p
, 1, timeout
* 1000);
507 #endif /* !HAVE_POLL_H */
517 * @ctxt: an HTTP context
519 * Read information coming from the HTTP connection.
520 * This is a blocking call (but it blocks in select(), not read()).
522 * Returns the number of byte read or -1 in case of error.
526 xmlNanoHTTPRecv(xmlNanoHTTPCtxtPtr ctxt
)
536 while (ctxt
->state
& XML_NANO_HTTP_READ
) {
537 if (ctxt
->in
== NULL
) {
538 ctxt
->in
= (char *) xmlMallocAtomic(65000 * sizeof(char));
539 if (ctxt
->in
== NULL
) {
540 xmlHTTPErrMemory("allocating input");
545 ctxt
->inptr
= ctxt
->content
= ctxt
->inrptr
= ctxt
->in
;
547 if (ctxt
->inrptr
> ctxt
->in
+ XML_NANO_HTTP_CHUNK
) {
548 int delta
= ctxt
->inrptr
- ctxt
->in
;
549 int len
= ctxt
->inptr
- ctxt
->inrptr
;
551 memmove(ctxt
->in
, ctxt
->inrptr
, len
);
552 ctxt
->inrptr
-= delta
;
553 ctxt
->content
-= delta
;
554 ctxt
->inptr
-= delta
;
556 if ((ctxt
->in
+ ctxt
->inlen
) < (ctxt
->inptr
+ XML_NANO_HTTP_CHUNK
)) {
557 int d_inptr
= ctxt
->inptr
- ctxt
->in
;
558 int d_content
= ctxt
->content
- ctxt
->in
;
559 int d_inrptr
= ctxt
->inrptr
- ctxt
->in
;
560 char *tmp_ptr
= ctxt
->in
;
563 ctxt
->in
= (char *) xmlRealloc(tmp_ptr
, ctxt
->inlen
);
564 if (ctxt
->in
== NULL
) {
565 xmlHTTPErrMemory("allocating input buffer");
570 ctxt
->inptr
= ctxt
->in
+ d_inptr
;
571 ctxt
->content
= ctxt
->in
+ d_content
;
572 ctxt
->inrptr
= ctxt
->in
+ d_inrptr
;
574 ctxt
->last
= recv(ctxt
->fd
, ctxt
->inptr
, XML_NANO_HTTP_CHUNK
, 0);
575 if (ctxt
->last
> 0) {
576 ctxt
->inptr
+= ctxt
->last
;
579 if (ctxt
->last
== 0) {
582 if (ctxt
->last
== -1) {
583 switch (socket_errno()) {
586 #if defined(EAGAIN) && EAGAIN != EWOULDBLOCK
596 __xmlIOErr(XML_FROM_HTTP
, 0, "recv failed\n");
603 if ((poll(&p
, 1, timeout
* 1000) < 1)
609 #else /* !HAVE_POLL_H */
611 if (ctxt
->fd
> FD_SETSIZE
)
620 #pragma warning(push)
621 #pragma warning(disable: 4018)
624 FD_SET(ctxt
->fd
, &rfd
);
630 if ((select(ctxt
->fd
+ 1, &rfd
, NULL
, NULL
, &tv
) < 1)
636 #endif /* !HAVE_POLL_H */
642 * xmlNanoHTTPReadLine:
643 * @ctxt: an HTTP context
645 * Read one line in the HTTP server output, usually for extracting
646 * the HTTP protocol informations from the answer header.
648 * Returns a newly allocated string with a copy of the line, or NULL
649 * which indicate the end of the input.
653 xmlNanoHTTPReadLine(xmlNanoHTTPCtxtPtr ctxt
) {
658 while (bp
- buf
< 4095) {
659 if (ctxt
->inrptr
== ctxt
->inptr
) {
660 if ( (rc
= xmlNanoHTTPRecv(ctxt
)) == 0) {
665 return(xmlMemStrdup(buf
));
667 else if ( rc
== -1 ) {
671 *bp
= *ctxt
->inrptr
++;
674 return(xmlMemStrdup(buf
));
680 return(xmlMemStrdup(buf
));
685 * xmlNanoHTTPScanAnswer:
686 * @ctxt: an HTTP context
687 * @line: an HTTP header line
689 * Try to extract useful informations from the server answer.
690 * We currently parse and process:
691 * - The HTTP revision/ return code
692 * - The Content-Type, Mime-Type and charset used
693 * - The Location for redirect processing.
695 * Returns -1 in case of failure, the file descriptor number otherwise
699 xmlNanoHTTPScanAnswer(xmlNanoHTTPCtxtPtr ctxt
, const char *line
) {
700 const char *cur
= line
;
702 if (line
== NULL
) return;
704 if (!strncmp(line
, "HTTP/", 5)) {
709 while ((*cur
>= '0') && (*cur
<= '9')) {
711 version
+= *cur
- '0';
716 if ((*cur
>= '0') && (*cur
<= '9')) {
718 version
+= *cur
- '0';
721 while ((*cur
>= '0') && (*cur
<= '9'))
725 if ((*cur
!= ' ') && (*cur
!= '\t')) return;
726 while ((*cur
== ' ') || (*cur
== '\t')) cur
++;
727 if ((*cur
< '0') || (*cur
> '9')) return;
728 while ((*cur
>= '0') && (*cur
<= '9')) {
733 if ((*cur
!= 0) && (*cur
!= ' ') && (*cur
!= '\t')) return;
734 ctxt
->returnValue
= ret
;
735 ctxt
->version
= version
;
736 } else if (!xmlStrncasecmp(BAD_CAST line
, BAD_CAST
"Content-Type:", 13)) {
737 const xmlChar
*charset
, *last
, *mime
;
739 while ((*cur
== ' ') || (*cur
== '\t')) cur
++;
740 if (ctxt
->contentType
!= NULL
)
741 xmlFree(ctxt
->contentType
);
742 ctxt
->contentType
= xmlMemStrdup(cur
);
743 mime
= (const xmlChar
*) cur
;
745 while ((*last
!= 0) && (*last
!= ' ') && (*last
!= '\t') &&
746 (*last
!= ';') && (*last
!= ','))
748 if (ctxt
->mimeType
!= NULL
)
749 xmlFree(ctxt
->mimeType
);
750 ctxt
->mimeType
= (char *) xmlStrndup(mime
, last
- mime
);
751 charset
= xmlStrstr(BAD_CAST ctxt
->contentType
, BAD_CAST
"charset=");
752 if (charset
!= NULL
) {
755 while ((*last
!= 0) && (*last
!= ' ') && (*last
!= '\t') &&
756 (*last
!= ';') && (*last
!= ','))
758 if (ctxt
->encoding
!= NULL
)
759 xmlFree(ctxt
->encoding
);
760 ctxt
->encoding
= (char *) xmlStrndup(charset
, last
- charset
);
762 } else if (!xmlStrncasecmp(BAD_CAST line
, BAD_CAST
"ContentType:", 12)) {
763 const xmlChar
*charset
, *last
, *mime
;
765 if (ctxt
->contentType
!= NULL
) return;
766 while ((*cur
== ' ') || (*cur
== '\t')) cur
++;
767 ctxt
->contentType
= xmlMemStrdup(cur
);
768 mime
= (const xmlChar
*) cur
;
770 while ((*last
!= 0) && (*last
!= ' ') && (*last
!= '\t') &&
771 (*last
!= ';') && (*last
!= ','))
773 if (ctxt
->mimeType
!= NULL
)
774 xmlFree(ctxt
->mimeType
);
775 ctxt
->mimeType
= (char *) xmlStrndup(mime
, last
- mime
);
776 charset
= xmlStrstr(BAD_CAST ctxt
->contentType
, BAD_CAST
"charset=");
777 if (charset
!= NULL
) {
780 while ((*last
!= 0) && (*last
!= ' ') && (*last
!= '\t') &&
781 (*last
!= ';') && (*last
!= ','))
783 if (ctxt
->encoding
!= NULL
)
784 xmlFree(ctxt
->encoding
);
785 ctxt
->encoding
= (char *) xmlStrndup(charset
, last
- charset
);
787 } else if (!xmlStrncasecmp(BAD_CAST line
, BAD_CAST
"Location:", 9)) {
789 while ((*cur
== ' ') || (*cur
== '\t')) cur
++;
790 if (ctxt
->location
!= NULL
)
791 xmlFree(ctxt
->location
);
793 xmlChar
*tmp_http
= xmlStrdup(BAD_CAST
"http://");
795 xmlStrcat(tmp_http
, (const xmlChar
*) ctxt
->hostname
);
797 (char *) xmlStrcat (tmp_loc
, (const xmlChar
*) cur
);
799 ctxt
->location
= xmlMemStrdup(cur
);
801 } else if (!xmlStrncasecmp(BAD_CAST line
, BAD_CAST
"WWW-Authenticate:", 17)) {
803 while ((*cur
== ' ') || (*cur
== '\t')) cur
++;
804 if (ctxt
->authHeader
!= NULL
)
805 xmlFree(ctxt
->authHeader
);
806 ctxt
->authHeader
= xmlMemStrdup(cur
);
807 } else if (!xmlStrncasecmp(BAD_CAST line
, BAD_CAST
"Proxy-Authenticate:", 19)) {
809 while ((*cur
== ' ') || (*cur
== '\t')) cur
++;
810 if (ctxt
->authHeader
!= NULL
)
811 xmlFree(ctxt
->authHeader
);
812 ctxt
->authHeader
= xmlMemStrdup(cur
);
814 } else if ( !xmlStrncasecmp( BAD_CAST line
, BAD_CAST
"Content-Encoding:", 17) ) {
816 while ((*cur
== ' ') || (*cur
== '\t')) cur
++;
817 if ( !xmlStrncasecmp( BAD_CAST cur
, BAD_CAST
"gzip", 4) ) {
820 ctxt
->strm
= xmlMalloc(sizeof(z_stream
));
822 if (ctxt
->strm
!= NULL
) {
823 ctxt
->strm
->zalloc
= Z_NULL
;
824 ctxt
->strm
->zfree
= Z_NULL
;
825 ctxt
->strm
->opaque
= Z_NULL
;
826 ctxt
->strm
->avail_in
= 0;
827 ctxt
->strm
->next_in
= Z_NULL
;
829 inflateInit2( ctxt
->strm
, 31 );
833 } else if ( !xmlStrncasecmp( BAD_CAST line
, BAD_CAST
"Content-Length:", 15) ) {
835 ctxt
->ContentLength
= strtol( cur
, NULL
, 10 );
840 * xmlNanoHTTPConnectAttempt:
841 * @addr: a socket address structure
843 * Attempt a connection to the given IP:port endpoint. It forces
844 * non-blocking semantic on the socket, and allow 60 seconds for
845 * the host to answer.
847 * Returns -1 in case of failure, the file descriptor number otherwise
851 xmlNanoHTTPConnectAttempt(struct sockaddr
*addr
)
859 #else /* !HAVE_POLL_H */
861 #endif /* !HAVE_POLL_H */
869 if (addr
->sa_family
== AF_INET6
) {
870 s
= socket(PF_INET6
, SOCK_STREAM
, IPPROTO_TCP
);
871 addrlen
= sizeof(struct sockaddr_in6
);
875 s
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
876 addrlen
= sizeof(struct sockaddr_in
);
878 if (s
== INVALID_SOCKET
) {
882 __xmlIOErr(XML_FROM_HTTP
, 0, "socket failed\n");
883 return INVALID_SOCKET
;
889 status
= ioctlsocket(s
, FIONBIO
, &one
) == SOCKET_ERROR
? -1 : 0;
891 #else /* _WINSOCKAPI_ */
896 status
= ioctl(s
, FIONBIO
, &enable
);
899 #if defined(__BEOS__) && !defined(__HAIKU__)
904 setsockopt(s
, SOL_SOCKET
, SO_NONBLOCK
, &noblock
,
908 if ((status
= fcntl(s
, F_GETFL
, 0)) != -1) {
910 status
|= O_NONBLOCK
;
911 #else /* O_NONBLOCK */
914 #endif /* F_NDELAY */
915 #endif /* !O_NONBLOCK */
916 status
= fcntl(s
, F_SETFL
, status
);
920 perror("nonblocking");
922 __xmlIOErr(XML_FROM_HTTP
, 0, "error setting non-blocking IO\n");
924 return INVALID_SOCKET
;
926 #endif /* !__BEOS__ */
928 #endif /* !_WINSOCKAPI_ */
930 if (connect(s
, addr
, addrlen
) == -1) {
931 switch (socket_errno()) {
936 __xmlIOErr(XML_FROM_HTTP
, 0,
937 "error connecting to HTTP server");
939 return INVALID_SOCKET
;
947 #pragma warning(push)
948 #pragma warning(disable: 4018)
952 return INVALID_SOCKET
;
961 switch (select(s
+ 1, NULL
, &wfd
, &xfd
, &tv
))
963 switch (select(s
+ 1, NULL
, &wfd
, NULL
, &tv
))
969 #else /* !HAVE_POLL_H */
972 switch (poll(&p
, 1, timeout
* 1000))
973 #endif /* !HAVE_POLL_H */
978 __xmlIOErr(XML_FROM_HTTP
, 0, "Connect attempt timed out");
980 return INVALID_SOCKET
;
983 __xmlIOErr(XML_FROM_HTTP
, 0, "Connect failed");
985 return INVALID_SOCKET
;
989 if (FD_ISSET(s
, &wfd
)
994 #else /* !HAVE_POLL_H */
995 if (p
.revents
== POLLOUT
)
996 #endif /* !HAVE_POLL_H */
1000 len
= sizeof(status
);
1002 if (getsockopt(s
, SOL_SOCKET
, SO_ERROR
, (char *) &status
, &len
) <
1004 /* Solaris error code */
1005 __xmlIOErr(XML_FROM_HTTP
, 0, "getsockopt failed\n");
1006 return INVALID_SOCKET
;
1010 __xmlIOErr(XML_FROM_HTTP
, 0,
1011 "Error connecting to remote host");
1014 return INVALID_SOCKET
;
1018 __xmlIOErr(XML_FROM_HTTP
, 0, "select failed\n");
1020 return INVALID_SOCKET
;
1027 * xmlNanoHTTPConnectHost:
1028 * @host: the host name
1029 * @port: the port number
1031 * Attempt a connection to the given host:port endpoint. It tries
1032 * the multiple IP provided by the DNS if available.
1034 * Returns -1 in case of failure, the file descriptor number otherwise
1038 xmlNanoHTTPConnectHost(const char *host
, int port
)
1041 struct sockaddr
*addr
= NULL
;
1043 struct sockaddr_in sockin
;
1046 struct in6_addr ia6
;
1047 struct sockaddr_in6 sockin6
;
1052 memset (&sockin
, 0, sizeof(sockin
));
1054 memset (&sockin6
, 0, sizeof(sockin6
));
1057 #if !defined(HAVE_GETADDRINFO) && defined(SUPPORT_IP6) && defined(RES_USE_INET6)
1060 if (!(_res
.options
& RES_INIT
))
1062 _res
.options
|= RES_USE_INET6
;
1066 #if defined(HAVE_GETADDRINFO) && defined(SUPPORT_IP6) && !defined(_WIN32)
1069 #if defined(HAVE_GETADDRINFO) && (defined(SUPPORT_IP6) || defined(_WIN32))
1072 struct addrinfo hints
, *res
, *result
;
1075 memset (&hints
, 0,sizeof(hints
));
1076 hints
.ai_socktype
= SOCK_STREAM
;
1078 status
= getaddrinfo (host
, NULL
, &hints
, &result
);
1080 __xmlIOErr(XML_FROM_HTTP
, 0, "getaddrinfo failed\n");
1081 return INVALID_SOCKET
;
1084 for (res
= result
; res
; res
= res
->ai_next
) {
1085 if (res
->ai_family
== AF_INET
) {
1086 if (res
->ai_addrlen
> sizeof(sockin
)) {
1087 __xmlIOErr(XML_FROM_HTTP
, 0, "address size mismatch\n");
1088 freeaddrinfo (result
);
1089 return INVALID_SOCKET
;
1091 memcpy (&sockin
, res
->ai_addr
, res
->ai_addrlen
);
1092 sockin
.sin_port
= htons (port
);
1093 addr
= (struct sockaddr
*)&sockin
;
1095 } else if (have_ipv6 () && (res
->ai_family
== AF_INET6
)) {
1096 if (res
->ai_addrlen
> sizeof(sockin6
)) {
1097 __xmlIOErr(XML_FROM_HTTP
, 0, "address size mismatch\n");
1098 freeaddrinfo (result
);
1099 return INVALID_SOCKET
;
1101 memcpy (&sockin6
, res
->ai_addr
, res
->ai_addrlen
);
1102 sockin6
.sin6_port
= htons (port
);
1103 addr
= (struct sockaddr
*)&sockin6
;
1108 s
= xmlNanoHTTPConnectAttempt (addr
);
1109 if (s
!= INVALID_SOCKET
) {
1110 freeaddrinfo (result
);
1116 freeaddrinfo (result
);
1119 #if defined(HAVE_GETADDRINFO) && defined(SUPPORT_IP6) && !defined(_WIN32)
1122 #if !defined(HAVE_GETADDRINFO) || !defined(_WIN32)
1124 h
= gethostbyname (host
);
1128 * Okay, I got fed up by the non-portability of this error message
1129 * extraction code. it work on Linux, if it work on your platform
1130 * and one want to enable it, send me the defined(foobar) needed
1132 #if defined(HAVE_NETDB_H) && defined(HOST_NOT_FOUND) && defined(linux)
1133 const char *h_err_txt
= "";
1136 case HOST_NOT_FOUND
:
1137 h_err_txt
= "Authoritive host not found";
1142 "Non-authoritive host not found or server failure.";
1147 "Non-recoverable errors: FORMERR, REFUSED, or NOTIMP.";
1153 "Valid name, no data record of requested type.";
1158 h_err_txt
= "No error text defined.";
1161 __xmlIOErr(XML_FROM_HTTP
, 0, h_err_txt
);
1163 __xmlIOErr(XML_FROM_HTTP
, 0, "Failed to resolve host");
1165 return INVALID_SOCKET
;
1168 for (i
= 0; h
->h_addr_list
[i
]; i
++) {
1169 if (h
->h_addrtype
== AF_INET
) {
1170 /* A records (IPv4) */
1171 if ((unsigned int) h
->h_length
> sizeof(ia
)) {
1172 __xmlIOErr(XML_FROM_HTTP
, 0, "address size mismatch\n");
1173 return INVALID_SOCKET
;
1175 memcpy (&ia
, h
->h_addr_list
[i
], h
->h_length
);
1176 sockin
.sin_family
= h
->h_addrtype
;
1177 sockin
.sin_addr
= ia
;
1178 sockin
.sin_port
= (unsigned short)htons ((unsigned short)port
);
1179 addr
= (struct sockaddr
*) &sockin
;
1181 } else if (have_ipv6 () && (h
->h_addrtype
== AF_INET6
)) {
1182 /* AAAA records (IPv6) */
1183 if ((unsigned int) h
->h_length
> sizeof(ia6
)) {
1184 __xmlIOErr(XML_FROM_HTTP
, 0, "address size mismatch\n");
1185 return INVALID_SOCKET
;
1187 memcpy (&ia6
, h
->h_addr_list
[i
], h
->h_length
);
1188 sockin6
.sin6_family
= h
->h_addrtype
;
1189 sockin6
.sin6_addr
= ia6
;
1190 sockin6
.sin6_port
= htons (port
);
1191 addr
= (struct sockaddr
*) &sockin6
;
1196 s
= xmlNanoHTTPConnectAttempt (addr
);
1197 if (s
!= INVALID_SOCKET
)
1204 xmlGenericError(xmlGenericErrorContext
,
1205 "xmlNanoHTTPConnectHost: unable to connect to '%s'.\n",
1208 return INVALID_SOCKET
;
1214 * @URL: The URL to load
1215 * @contentType: if available the Content-Type information will be
1216 * returned at that location
1218 * This function try to open a connection to the indicated resource
1221 * Returns NULL in case of failure, otherwise a request handler.
1222 * The contentType, if provided must be freed by the caller
1226 xmlNanoHTTPOpen(const char *URL
, char **contentType
) {
1227 if (contentType
!= NULL
) *contentType
= NULL
;
1228 return(xmlNanoHTTPMethod(URL
, NULL
, NULL
, contentType
, NULL
, 0));
1232 * xmlNanoHTTPOpenRedir:
1233 * @URL: The URL to load
1234 * @contentType: if available the Content-Type information will be
1235 * returned at that location
1236 * @redir: if available the redirected URL will be returned
1238 * This function try to open a connection to the indicated resource
1241 * Returns NULL in case of failure, otherwise a request handler.
1242 * The contentType, if provided must be freed by the caller
1246 xmlNanoHTTPOpenRedir(const char *URL
, char **contentType
, char **redir
) {
1247 if (contentType
!= NULL
) *contentType
= NULL
;
1248 if (redir
!= NULL
) *redir
= NULL
;
1249 return(xmlNanoHTTPMethodRedir(URL
, NULL
, NULL
, contentType
, redir
, NULL
,0));
1254 * @ctx: the HTTP context
1256 * @len: the buffer length
1258 * This function tries to read @len bytes from the existing HTTP connection
1259 * and saves them in @dest. This is a blocking call.
1261 * Returns the number of byte read. 0 is an indication of an end of connection.
1262 * -1 indicates a parameter error.
1265 xmlNanoHTTPRead(void *ctx
, void *dest
, int len
) {
1266 xmlNanoHTTPCtxtPtr ctxt
= (xmlNanoHTTPCtxtPtr
) ctx
;
1273 if (ctx
== NULL
) return(-1);
1274 if (dest
== NULL
) return(-1);
1275 if (len
<= 0) return(0);
1278 if (ctxt
->usesGzip
== 1) {
1279 if (ctxt
->strm
== NULL
) return(0);
1281 ctxt
->strm
->next_out
= dest
;
1282 ctxt
->strm
->avail_out
= len
;
1283 ctxt
->strm
->avail_in
= ctxt
->inptr
- ctxt
->inrptr
;
1285 while (ctxt
->strm
->avail_out
> 0 &&
1286 (ctxt
->strm
->avail_in
> 0 || xmlNanoHTTPRecv(ctxt
) > 0)) {
1287 orig_avail_in
= ctxt
->strm
->avail_in
=
1288 ctxt
->inptr
- ctxt
->inrptr
- bytes_read
;
1289 ctxt
->strm
->next_in
= BAD_CAST (ctxt
->inrptr
+ bytes_read
);
1291 z_ret
= inflate(ctxt
->strm
, Z_NO_FLUSH
);
1292 bytes_read
+= orig_avail_in
- ctxt
->strm
->avail_in
;
1294 if (z_ret
!= Z_OK
) break;
1297 ctxt
->inrptr
+= bytes_read
;
1298 return(len
- ctxt
->strm
->avail_out
);
1302 while (ctxt
->inptr
- ctxt
->inrptr
< len
) {
1303 if (xmlNanoHTTPRecv(ctxt
) <= 0) break;
1305 if (ctxt
->inptr
- ctxt
->inrptr
< len
)
1306 len
= ctxt
->inptr
- ctxt
->inrptr
;
1307 memcpy(dest
, ctxt
->inrptr
, len
);
1308 ctxt
->inrptr
+= len
;
1314 * @ctx: the HTTP context
1316 * This function closes an HTTP context, it ends up the connection and
1317 * free all data related to it.
1320 xmlNanoHTTPClose(void *ctx
) {
1321 xmlNanoHTTPCtxtPtr ctxt
= (xmlNanoHTTPCtxtPtr
) ctx
;
1323 if (ctx
== NULL
) return;
1325 xmlNanoHTTPFreeCtxt(ctxt
);
1329 * xmlNanoHTTPMethodRedir:
1330 * @URL: The URL to load
1331 * @method: the HTTP method to use
1332 * @input: the input string if any
1333 * @contentType: the Content-Type information IN and OUT
1334 * @redir: the redirected URL OUT
1335 * @headers: the extra headers
1336 * @ilen: input length
1338 * This function try to open a connection to the indicated resource
1339 * via HTTP using the given @method, adding the given extra headers
1340 * and the input buffer for the request content.
1342 * Returns NULL in case of failure, otherwise a request handler.
1343 * The contentType, or redir, if provided must be freed by the caller
1347 xmlNanoHTTPMethodRedir(const char *URL
, const char *method
, const char *input
,
1348 char **contentType
, char **redir
,
1349 const char *headers
, int ilen
) {
1350 xmlNanoHTTPCtxtPtr ctxt
;
1354 int nbRedirects
= 0;
1355 char *redirURL
= NULL
;
1360 if (URL
== NULL
) return(NULL
);
1361 if (method
== NULL
) method
= "GET";
1365 if (redirURL
== NULL
)
1366 ctxt
= xmlNanoHTTPNewCtxt(URL
);
1368 ctxt
= xmlNanoHTTPNewCtxt(redirURL
);
1369 ctxt
->location
= xmlMemStrdup(redirURL
);
1372 if ( ctxt
== NULL
) {
1376 if ((ctxt
->protocol
== NULL
) || (strcmp(ctxt
->protocol
, "http"))) {
1377 __xmlIOErr(XML_FROM_HTTP
, XML_HTTP_URL_SYNTAX
, "Not a valid HTTP URI");
1378 xmlNanoHTTPFreeCtxt(ctxt
);
1379 if (redirURL
!= NULL
) xmlFree(redirURL
);
1382 if (ctxt
->hostname
== NULL
) {
1383 __xmlIOErr(XML_FROM_HTTP
, XML_HTTP_UNKNOWN_HOST
,
1384 "Failed to identify host in URI");
1385 xmlNanoHTTPFreeCtxt(ctxt
);
1386 if (redirURL
!= NULL
) xmlFree(redirURL
);
1390 blen
= strlen(ctxt
->hostname
) * 2 + 16;
1391 ret
= xmlNanoHTTPConnectHost(proxy
, proxyPort
);
1394 blen
= strlen(ctxt
->hostname
);
1395 ret
= xmlNanoHTTPConnectHost(ctxt
->hostname
, ctxt
->port
);
1397 if (ret
== INVALID_SOCKET
) {
1398 xmlNanoHTTPFreeCtxt(ctxt
);
1399 if (redirURL
!= NULL
) xmlFree(redirURL
);
1409 if (headers
!= NULL
)
1410 blen
+= strlen(headers
) + 2;
1411 if (contentType
&& *contentType
)
1412 /* reserve for string plus 'Content-Type: \r\n" */
1413 blen
+= strlen(*contentType
) + 16;
1414 if (ctxt
->query
!= NULL
)
1416 blen
+= strlen(ctxt
->query
) + 1;
1417 blen
+= strlen(method
) + strlen(ctxt
->path
) + 24;
1419 /* reserve for possible 'Accept-Encoding: gzip' string */
1422 if (ctxt
->port
!= 80) {
1423 /* reserve space for ':xxxxx', incl. potential proxy */
1429 bp
= (char*)xmlMallocAtomic(blen
);
1431 xmlNanoHTTPFreeCtxt( ctxt
);
1432 xmlHTTPErrMemory("allocating header buffer");
1439 if (ctxt
->port
!= 80) {
1440 p
+= snprintf( p
, blen
- (p
- bp
), "%s http://%s:%d%s",
1441 method
, ctxt
->hostname
,
1442 ctxt
->port
, ctxt
->path
);
1445 p
+= snprintf( p
, blen
- (p
- bp
), "%s http://%s%s", method
,
1446 ctxt
->hostname
, ctxt
->path
);
1449 p
+= snprintf( p
, blen
- (p
- bp
), "%s %s", method
, ctxt
->path
);
1451 if (ctxt
->query
!= NULL
)
1452 p
+= snprintf( p
, blen
- (p
- bp
), "?%s", ctxt
->query
);
1454 if (ctxt
->port
== 80) {
1455 p
+= snprintf( p
, blen
- (p
- bp
), " HTTP/1.0\r\nHost: %s\r\n",
1458 p
+= snprintf( p
, blen
- (p
- bp
), " HTTP/1.0\r\nHost: %s:%d\r\n",
1459 ctxt
->hostname
, ctxt
->port
);
1463 p
+= snprintf(p
, blen
- (p
- bp
), "Accept-Encoding: gzip\r\n");
1466 if (contentType
!= NULL
&& *contentType
)
1467 p
+= snprintf(p
, blen
- (p
- bp
), "Content-Type: %s\r\n", *contentType
);
1469 if (headers
!= NULL
)
1470 p
+= snprintf( p
, blen
- (p
- bp
), "%s", headers
);
1473 snprintf(p
, blen
- (p
- bp
), "Content-Length: %d\r\n\r\n", ilen
);
1475 snprintf(p
, blen
- (p
- bp
), "\r\n");
1478 xmlGenericError(xmlGenericErrorContext
,
1479 "-> %s%s", proxy
? "(Proxy) " : "", bp
);
1480 if ((blen
-= strlen(bp
)+1) < 0)
1481 xmlGenericError(xmlGenericErrorContext
,
1482 "ERROR: overflowed buffer by %d bytes\n", -blen
);
1484 ctxt
->outptr
= ctxt
->out
= bp
;
1485 ctxt
->state
= XML_NANO_HTTP_WRITE
;
1486 blen
= strlen( ctxt
->out
);
1488 xmt_bytes
= xmlNanoHTTPSend(ctxt
, ctxt
->out
, blen
);
1489 if ( xmt_bytes
!= blen
)
1490 xmlGenericError( xmlGenericErrorContext
,
1491 "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
1493 "bytes of HTTP headers sent to host",
1496 xmlNanoHTTPSend(ctxt
, ctxt
->out
, blen
);
1499 if ( input
!= NULL
) {
1501 xmt_bytes
= xmlNanoHTTPSend( ctxt
, input
, ilen
);
1503 if ( xmt_bytes
!= ilen
)
1504 xmlGenericError( xmlGenericErrorContext
,
1505 "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
1507 "bytes of HTTP content sent to host",
1510 xmlNanoHTTPSend( ctxt
, input
, ilen
);
1514 ctxt
->state
= XML_NANO_HTTP_READ
;
1516 while ((p
= xmlNanoHTTPReadLine(ctxt
)) != NULL
) {
1518 ctxt
->content
= ctxt
->inrptr
;
1522 xmlNanoHTTPScanAnswer(ctxt
, p
);
1525 xmlGenericError(xmlGenericErrorContext
, "<- %s\n", p
);
1530 if ((ctxt
->location
!= NULL
) && (ctxt
->returnValue
>= 300) &&
1531 (ctxt
->returnValue
< 400)) {
1533 xmlGenericError(xmlGenericErrorContext
,
1534 "\nRedirect to: %s\n", ctxt
->location
);
1536 while ( xmlNanoHTTPRecv(ctxt
) > 0 ) ;
1537 if (nbRedirects
< XML_NANO_HTTP_MAX_REDIR
) {
1539 if (redirURL
!= NULL
)
1541 redirURL
= xmlMemStrdup(ctxt
->location
);
1542 xmlNanoHTTPFreeCtxt(ctxt
);
1545 xmlNanoHTTPFreeCtxt(ctxt
);
1546 if (redirURL
!= NULL
) xmlFree(redirURL
);
1548 xmlGenericError(xmlGenericErrorContext
,
1549 "xmlNanoHTTPMethodRedir: Too many redirects, aborting ...\n");
1554 if (contentType
!= NULL
) {
1555 if (ctxt
->contentType
!= NULL
)
1556 *contentType
= xmlMemStrdup(ctxt
->contentType
);
1558 *contentType
= NULL
;
1561 if ((redir
!= NULL
) && (redirURL
!= NULL
)) {
1564 if (redirURL
!= NULL
)
1571 if (ctxt
->contentType
!= NULL
)
1572 xmlGenericError(xmlGenericErrorContext
,
1573 "\nCode %d, content-type '%s'\n\n",
1574 ctxt
->returnValue
, ctxt
->contentType
);
1576 xmlGenericError(xmlGenericErrorContext
,
1577 "\nCode %d, no content-type\n\n",
1581 return((void *) ctxt
);
1585 * xmlNanoHTTPMethod:
1586 * @URL: The URL to load
1587 * @method: the HTTP method to use
1588 * @input: the input string if any
1589 * @contentType: the Content-Type information IN and OUT
1590 * @headers: the extra headers
1591 * @ilen: input length
1593 * This function try to open a connection to the indicated resource
1594 * via HTTP using the given @method, adding the given extra headers
1595 * and the input buffer for the request content.
1597 * Returns NULL in case of failure, otherwise a request handler.
1598 * The contentType, if provided must be freed by the caller
1602 xmlNanoHTTPMethod(const char *URL
, const char *method
, const char *input
,
1603 char **contentType
, const char *headers
, int ilen
) {
1604 return(xmlNanoHTTPMethodRedir(URL
, method
, input
, contentType
,
1605 NULL
, headers
, ilen
));
1610 * @URL: The URL to load
1611 * @filename: the filename where the content should be saved
1612 * @contentType: if available the Content-Type information will be
1613 * returned at that location
1615 * This function try to fetch the indicated resource via HTTP GET
1616 * and save it's content in the file.
1618 * Returns -1 in case of failure, 0 incase of success. The contentType,
1619 * if provided must be freed by the caller
1622 xmlNanoHTTPFetch(const char *URL
, const char *filename
, char **contentType
) {
1629 if (filename
== NULL
) return(-1);
1630 ctxt
= xmlNanoHTTPOpen(URL
, contentType
);
1631 if (ctxt
== NULL
) return(-1);
1633 if (!strcmp(filename
, "-"))
1636 fd
= open(filename
, O_CREAT
| O_WRONLY
, 00644);
1638 xmlNanoHTTPClose(ctxt
);
1639 if ((contentType
!= NULL
) && (*contentType
!= NULL
)) {
1640 xmlFree(*contentType
);
1641 *contentType
= NULL
;
1647 xmlNanoHTTPFetchContent( ctxt
, &buf
, &len
);
1649 if (write(fd
, buf
, len
) == -1) {
1654 xmlNanoHTTPClose(ctxt
);
1659 #ifdef LIBXML_OUTPUT_ENABLED
1662 * @ctxt: the HTTP context
1663 * @filename: the filename where the content should be saved
1665 * This function saves the output of the HTTP transaction to a file
1666 * It closes and free the context at the end
1668 * Returns -1 in case of failure, 0 incase of success.
1671 xmlNanoHTTPSave(void *ctxt
, const char *filename
) {
1677 if ((ctxt
== NULL
) || (filename
== NULL
)) return(-1);
1679 if (!strcmp(filename
, "-"))
1682 fd
= open(filename
, O_CREAT
| O_WRONLY
, 0666);
1684 xmlNanoHTTPClose(ctxt
);
1689 xmlNanoHTTPFetchContent( ctxt
, &buf
, &len
);
1691 if (write(fd
, buf
, len
) == -1) {
1696 xmlNanoHTTPClose(ctxt
);
1700 #endif /* LIBXML_OUTPUT_ENABLED */
1703 * xmlNanoHTTPReturnCode:
1704 * @ctx: the HTTP context
1706 * Get the latest HTTP return code received
1708 * Returns the HTTP return code for the request.
1711 xmlNanoHTTPReturnCode(void *ctx
) {
1712 xmlNanoHTTPCtxtPtr ctxt
= (xmlNanoHTTPCtxtPtr
) ctx
;
1714 if (ctxt
== NULL
) return(-1);
1716 return(ctxt
->returnValue
);
1720 * xmlNanoHTTPAuthHeader:
1721 * @ctx: the HTTP context
1723 * Get the authentication header of an HTTP context
1725 * Returns the stashed value of the WWW-Authenticate or Proxy-Authenticate
1729 xmlNanoHTTPAuthHeader(void *ctx
) {
1730 xmlNanoHTTPCtxtPtr ctxt
= (xmlNanoHTTPCtxtPtr
) ctx
;
1732 if (ctxt
== NULL
) return(NULL
);
1734 return(ctxt
->authHeader
);
1738 * xmlNanoHTTPContentLength:
1739 * @ctx: the HTTP context
1741 * Provides the specified content length from the HTTP header.
1743 * Return the specified content length from the HTTP header. Note that
1744 * a value of -1 indicates that the content length element was not included in
1745 * the response header.
1748 xmlNanoHTTPContentLength( void * ctx
) {
1749 xmlNanoHTTPCtxtPtr ctxt
= (xmlNanoHTTPCtxtPtr
)ctx
;
1751 return ( ( ctxt
== NULL
) ? -1 : ctxt
->ContentLength
);
1756 * @ctx: the HTTP context
1758 * Provides the specified redirection URL if available from the HTTP header.
1760 * Return the specified redirection URL or NULL if not redirected.
1763 xmlNanoHTTPRedir( void * ctx
) {
1764 xmlNanoHTTPCtxtPtr ctxt
= (xmlNanoHTTPCtxtPtr
)ctx
;
1766 return ( ( ctxt
== NULL
) ? NULL
: ctxt
->location
);
1770 * xmlNanoHTTPEncoding:
1771 * @ctx: the HTTP context
1773 * Provides the specified encoding if specified in the HTTP headers.
1775 * Return the specified encoding or NULL if not available
1778 xmlNanoHTTPEncoding( void * ctx
) {
1779 xmlNanoHTTPCtxtPtr ctxt
= (xmlNanoHTTPCtxtPtr
)ctx
;
1781 return ( ( ctxt
== NULL
) ? NULL
: ctxt
->encoding
);
1785 * xmlNanoHTTPMimeType:
1786 * @ctx: the HTTP context
1788 * Provides the specified Mime-Type if specified in the HTTP headers.
1790 * Return the specified Mime-Type or NULL if not available
1793 xmlNanoHTTPMimeType( void * ctx
) {
1794 xmlNanoHTTPCtxtPtr ctxt
= (xmlNanoHTTPCtxtPtr
)ctx
;
1796 return ( ( ctxt
== NULL
) ? NULL
: ctxt
->mimeType
);
1800 * xmlNanoHTTPFetchContent:
1801 * @ctx: the HTTP context
1802 * @ptr: pointer to set to the content buffer.
1803 * @len: integer pointer to hold the length of the content
1805 * Check if all the content was read
1807 * Returns 0 if all the content was read and available, returns
1808 * -1 if received content length was less than specified or an error
1812 xmlNanoHTTPFetchContent( void * ctx
, char ** ptr
, int * len
) {
1813 xmlNanoHTTPCtxtPtr ctxt
= (xmlNanoHTTPCtxtPtr
)ctx
;
1819 char * dummy_ptr
= NULL
;
1821 /* Dummy up return input parameters if not provided */
1829 /* But can't work without the context pointer */
1831 if ( ( ctxt
== NULL
) || ( ctxt
->content
== NULL
) ) {
1837 rcvd_lgth
= ctxt
->inptr
- ctxt
->content
;
1839 while ( (cur_lgth
= xmlNanoHTTPRecv( ctxt
)) > 0 ) {
1841 rcvd_lgth
+= cur_lgth
;
1842 if ( (ctxt
->ContentLength
> 0) && (rcvd_lgth
>= ctxt
->ContentLength
) )
1846 *ptr
= ctxt
->content
;
1849 if ( ( ctxt
->ContentLength
> 0 ) && ( rcvd_lgth
< ctxt
->ContentLength
) )
1851 else if ( rcvd_lgth
== 0 )
1858 int main(int argc
, char **argv
) {
1859 char *contentType
= NULL
;
1861 if (argv
[1] != NULL
) {
1862 if (argv
[2] != NULL
)
1863 xmlNanoHTTPFetch(argv
[1], argv
[2], &contentType
);
1865 xmlNanoHTTPFetch(argv
[1], "-", &contentType
);
1866 if (contentType
!= NULL
) xmlFree(contentType
);
1868 xmlGenericError(xmlGenericErrorContext
,
1869 "%s: minimal HTTP GET implementation\n", argv
[0]);
1870 xmlGenericError(xmlGenericErrorContext
,
1871 "\tusage %s [ URL [ filename ] ]\n", argv
[0]);
1873 xmlNanoHTTPCleanup();
1877 #endif /* STANDALONE */
1878 #else /* !LIBXML_HTTP_ENABLED */
1881 int main(int argc
, char **argv
) {
1882 xmlGenericError(xmlGenericErrorContext
,
1883 "%s : HTTP support not compiled in\n", argv
[0]);
1886 #endif /* STANDALONE */
1887 #endif /* LIBXML_HTTP_ENABLED */
1888 #define bottom_nanohttp
1889 #include "elfgcchack.h"