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.
17 #ifdef LIBXML_HTTP_ENABLED
26 #ifdef HAVE_SYS_TYPES_H
27 #include <sys/types.h>
29 #ifdef HAVE_SYS_SOCKET_H
30 #include <sys/socket.h>
32 #ifdef HAVE_NETINET_IN_H
33 #include <netinet/in.h>
35 #ifdef HAVE_ARPA_INET_H
36 #include <arpa/inet.h>
42 #ifdef HAVE_ARPA_NAMESER_H
43 #include <arpa/nameser.h>
53 #ifdef HAVE_SYS_TIME_H
57 #ifdef HAVE_SYS_SELECT_H
58 #include <sys/select.h>
73 #define XML_SOCKLEN_T unsigned int
76 #if defined(_WIN32) && !defined(__CYGWIN__)
77 #include <wsockcompat.h>
80 #include <libxml/globals.h>
81 #include <libxml/xmlerror.h>
82 #include <libxml/xmlmemory.h>
83 #include <libxml/parser.h> /* for xmlStr(n)casecmp() */
84 #include <libxml/nanohttp.h>
85 #include <libxml/globals.h>
86 #include <libxml/uri.h>
89 * A couple portability macros
92 #if !defined(__BEOS__) || defined(__HAIKU__)
93 #define closesocket(s) close(s)
96 #define INVALID_SOCKET (-1)
101 #define PF_INET AF_INET
105 #ifndef XML_SOCKLEN_T
106 #define XML_SOCKLEN_T unsigned int
111 #define xmlStrncasecmp(a, b, n) strncasecmp((char *)a, (char *)b, n)
112 #define xmlStrcasecmpi(a, b) strcasecmp((char *)a, (char *)b)
115 #define XML_NANO_HTTP_MAX_REDIR 10
117 #define XML_NANO_HTTP_CHUNK 4096
119 #define XML_NANO_HTTP_CLOSED 0
120 #define XML_NANO_HTTP_WRITE 1
121 #define XML_NANO_HTTP_READ 2
122 #define XML_NANO_HTTP_NONE 4
124 typedef struct xmlNanoHTTPCtxt
{
125 char *protocol
; /* the protocol name */
126 char *hostname
; /* the host name */
127 int port
; /* the port */
128 char *path
; /* the path within the URL */
129 char *query
; /* the query string */
130 SOCKET fd
; /* the file descriptor for the socket */
131 int state
; /* WRITE / READ / CLOSED */
132 char *out
; /* buffer sent (zero terminated) */
133 char *outptr
; /* index within the buffer sent */
134 char *in
; /* the receiving buffer */
135 char *content
; /* the start of the content */
136 char *inptr
; /* the next byte to read from network */
137 char *inrptr
; /* the next byte to give back to the client */
138 int inlen
; /* len of the input buffer */
139 int last
; /* return code for last operation */
140 int returnValue
; /* the protocol return value */
141 int version
; /* the protocol version */
142 int ContentLength
; /* specified content length from HTTP header */
143 char *contentType
; /* the MIME type for the input */
144 char *location
; /* the new URL in case of redirect */
145 char *authHeader
; /* contents of {WWW,Proxy}-Authenticate header */
146 char *encoding
; /* encoding extracted from the contentType */
147 char *mimeType
; /* Mime-Type extracted from the contentType */
149 z_stream
*strm
; /* Zlib stream object */
150 int usesGzip
; /* "Content-Encoding: gzip" was detected */
152 } xmlNanoHTTPCtxt
, *xmlNanoHTTPCtxtPtr
;
154 static int initialized
= 0;
155 static char *proxy
= NULL
; /* the proxy name if any */
156 static int proxyPort
; /* the proxy port if any */
157 static unsigned int timeout
= 60;/* the select() timeout in seconds */
159 static int xmlNanoHTTPFetchContent( void * ctx
, char ** ptr
, int * len
);
163 * @extra: extra informations
165 * Handle an out of memory condition
168 xmlHTTPErrMemory(const char *extra
)
170 __xmlSimpleError(XML_FROM_HTTP
, XML_ERR_NO_MEMORY
, NULL
, NULL
, extra
);
174 * A portability function
176 static int socket_errno(void) {
178 int err
= WSAGetLastError();
200 int have_ipv6(void) {
203 s
= socket (AF_INET6
, SOCK_STREAM
, 0);
204 if (s
!= INVALID_SOCKET
) {
215 * Initialize the HTTP protocol layer.
216 * Currently it just checks for proxy informations
220 xmlNanoHTTPInit(void) {
230 if (WSAStartup(MAKEWORD(1, 1), &wsaData
) != 0)
236 env
= getenv("no_proxy");
237 if (env
&& ((env
[0] == '*') && (env
[1] == 0)))
239 env
= getenv("http_proxy");
241 xmlNanoHTTPScanProxy(env
);
244 env
= getenv("HTTP_PROXY");
246 xmlNanoHTTPScanProxy(env
);
255 * xmlNanoHTTPCleanup:
257 * Cleanup the HTTP protocol layer.
261 xmlNanoHTTPCleanup(void) {
275 * xmlNanoHTTPScanURL:
276 * @ctxt: an HTTP context
277 * @URL: The URL used to initialize the context
279 * (Re)Initialize an HTTP context by parsing the URL and finding
280 * the protocol host port and path it indicates.
284 xmlNanoHTTPScanURL(xmlNanoHTTPCtxtPtr ctxt
, const char *URL
) {
289 * Clear any existing data from the context
291 if (ctxt
->protocol
!= NULL
) {
292 xmlFree(ctxt
->protocol
);
293 ctxt
->protocol
= NULL
;
295 if (ctxt
->hostname
!= NULL
) {
296 xmlFree(ctxt
->hostname
);
297 ctxt
->hostname
= NULL
;
299 if (ctxt
->path
!= NULL
) {
303 if (ctxt
->query
!= NULL
) {
304 xmlFree(ctxt
->query
);
307 if (URL
== NULL
) return;
309 uri
= xmlParseURIRaw(URL
, 1);
313 if ((uri
->scheme
== NULL
) || (uri
->server
== NULL
)) {
318 ctxt
->protocol
= xmlMemStrdup(uri
->scheme
);
319 /* special case of IPv6 addresses, the [] need to be removed */
320 if ((uri
->server
!= NULL
) && (*uri
->server
== '[')) {
321 len
= strlen(uri
->server
);
322 if ((len
> 2) && (uri
->server
[len
- 1] == ']')) {
323 ctxt
->hostname
= (char *) xmlCharStrndup(uri
->server
+ 1, len
-2);
325 ctxt
->hostname
= xmlMemStrdup(uri
->server
);
327 ctxt
->hostname
= xmlMemStrdup(uri
->server
);
328 if (uri
->path
!= NULL
)
329 ctxt
->path
= xmlMemStrdup(uri
->path
);
331 ctxt
->path
= xmlMemStrdup("/");
332 if (uri
->query
!= NULL
)
333 ctxt
->query
= xmlMemStrdup(uri
->query
);
335 ctxt
->port
= uri
->port
;
341 * xmlNanoHTTPScanProxy:
342 * @URL: The proxy URL used to initialize the proxy context
344 * (Re)Initialize the HTTP Proxy context by parsing the URL and finding
345 * the protocol host port it indicates.
346 * Should be like http://myproxy/ or http://myproxy:3128/
347 * A NULL URL cleans up proxy informations.
351 xmlNanoHTTPScanProxy(const char *URL
) {
362 xmlGenericError(xmlGenericErrorContext
,
363 "Removing HTTP proxy info\n");
365 xmlGenericError(xmlGenericErrorContext
,
366 "Using HTTP proxy %s\n", URL
);
368 if (URL
== NULL
) return;
370 uri
= xmlParseURIRaw(URL
, 1);
371 if ((uri
== NULL
) || (uri
->scheme
== NULL
) ||
372 (strcmp(uri
->scheme
, "http")) || (uri
->server
== NULL
)) {
373 __xmlIOErr(XML_FROM_HTTP
, XML_HTTP_URL_SYNTAX
, "Syntax Error\n");
379 proxy
= xmlMemStrdup(uri
->server
);
381 proxyPort
= uri
->port
;
387 * xmlNanoHTTPNewCtxt:
388 * @URL: The URL used to initialize the context
390 * Allocate and initialize a new HTTP context.
392 * Returns an HTTP context or NULL in case of error.
395 static xmlNanoHTTPCtxtPtr
396 xmlNanoHTTPNewCtxt(const char *URL
) {
397 xmlNanoHTTPCtxtPtr ret
;
399 ret
= (xmlNanoHTTPCtxtPtr
) xmlMalloc(sizeof(xmlNanoHTTPCtxt
));
401 xmlHTTPErrMemory("allocating context");
405 memset(ret
, 0, sizeof(xmlNanoHTTPCtxt
));
407 ret
->returnValue
= 0;
408 ret
->fd
= INVALID_SOCKET
;
409 ret
->ContentLength
= -1;
411 xmlNanoHTTPScanURL(ret
, URL
);
417 * xmlNanoHTTPFreeCtxt:
418 * @ctxt: an HTTP context
420 * Frees the context after closing the connection.
424 xmlNanoHTTPFreeCtxt(xmlNanoHTTPCtxtPtr ctxt
) {
425 if (ctxt
== NULL
) return;
426 if (ctxt
->hostname
!= NULL
) xmlFree(ctxt
->hostname
);
427 if (ctxt
->protocol
!= NULL
) xmlFree(ctxt
->protocol
);
428 if (ctxt
->path
!= NULL
) xmlFree(ctxt
->path
);
429 if (ctxt
->query
!= NULL
) xmlFree(ctxt
->query
);
430 if (ctxt
->out
!= NULL
) xmlFree(ctxt
->out
);
431 if (ctxt
->in
!= NULL
) xmlFree(ctxt
->in
);
432 if (ctxt
->contentType
!= NULL
) xmlFree(ctxt
->contentType
);
433 if (ctxt
->encoding
!= NULL
) xmlFree(ctxt
->encoding
);
434 if (ctxt
->mimeType
!= NULL
) xmlFree(ctxt
->mimeType
);
435 if (ctxt
->location
!= NULL
) xmlFree(ctxt
->location
);
436 if (ctxt
->authHeader
!= NULL
) xmlFree(ctxt
->authHeader
);
438 if (ctxt
->strm
!= NULL
) {
439 inflateEnd(ctxt
->strm
);
444 ctxt
->state
= XML_NANO_HTTP_NONE
;
445 if (ctxt
->fd
!= INVALID_SOCKET
) closesocket(ctxt
->fd
);
446 ctxt
->fd
= INVALID_SOCKET
;
452 * @ctxt: an HTTP context
454 * Send the input needed to initiate the processing on the server side
455 * Returns number of bytes sent or -1 on error.
459 xmlNanoHTTPSend(xmlNanoHTTPCtxtPtr ctxt
, const char *xmt_ptr
, int outlen
)
469 if ((ctxt
->state
& XML_NANO_HTTP_WRITE
) && (xmt_ptr
!= NULL
)) {
470 while (total_sent
< outlen
) {
471 int nsent
= send(ctxt
->fd
, SEND_ARG2_CAST (xmt_ptr
+ total_sent
),
472 outlen
- total_sent
, 0);
476 else if ((nsent
== -1) &&
477 #if defined(EAGAIN) && EAGAIN != EWOULDBLOCK
478 (socket_errno() != EAGAIN
) &&
480 (socket_errno() != EWOULDBLOCK
)) {
481 __xmlIOErr(XML_FROM_HTTP
, 0, "send failed\n");
488 * Since non-blocking sockets are used, wait for
489 * socket to be writable or default timeout prior
494 if (ctxt
->fd
> FD_SETSIZE
)
502 #pragma warning(push)
503 #pragma warning(disable: 4018)
505 FD_SET(ctxt
->fd
, &wfd
);
509 (void) select(ctxt
->fd
+ 1, NULL
, &wfd
, NULL
, &tv
);
513 (void) poll(&p
, 1, timeout
* 1000);
514 #endif /* !HAVE_POLL_H */
524 * @ctxt: an HTTP context
526 * Read information coming from the HTTP connection.
527 * This is a blocking call (but it blocks in select(), not read()).
529 * Returns the number of byte read or -1 in case of error.
533 xmlNanoHTTPRecv(xmlNanoHTTPCtxtPtr ctxt
)
543 while (ctxt
->state
& XML_NANO_HTTP_READ
) {
544 if (ctxt
->in
== NULL
) {
545 ctxt
->in
= (char *) xmlMallocAtomic(65000 * sizeof(char));
546 if (ctxt
->in
== NULL
) {
547 xmlHTTPErrMemory("allocating input");
552 ctxt
->inptr
= ctxt
->content
= ctxt
->inrptr
= ctxt
->in
;
554 if (ctxt
->inrptr
> ctxt
->in
+ XML_NANO_HTTP_CHUNK
) {
555 int delta
= ctxt
->inrptr
- ctxt
->in
;
556 int len
= ctxt
->inptr
- ctxt
->inrptr
;
558 memmove(ctxt
->in
, ctxt
->inrptr
, len
);
559 ctxt
->inrptr
-= delta
;
560 ctxt
->content
-= delta
;
561 ctxt
->inptr
-= delta
;
563 if ((ctxt
->in
+ ctxt
->inlen
) < (ctxt
->inptr
+ XML_NANO_HTTP_CHUNK
)) {
564 int d_inptr
= ctxt
->inptr
- ctxt
->in
;
565 int d_content
= ctxt
->content
- ctxt
->in
;
566 int d_inrptr
= ctxt
->inrptr
- ctxt
->in
;
567 char *tmp_ptr
= ctxt
->in
;
570 ctxt
->in
= (char *) xmlRealloc(tmp_ptr
, ctxt
->inlen
);
571 if (ctxt
->in
== NULL
) {
572 xmlHTTPErrMemory("allocating input buffer");
577 ctxt
->inptr
= ctxt
->in
+ d_inptr
;
578 ctxt
->content
= ctxt
->in
+ d_content
;
579 ctxt
->inrptr
= ctxt
->in
+ d_inrptr
;
581 ctxt
->last
= recv(ctxt
->fd
, ctxt
->inptr
, XML_NANO_HTTP_CHUNK
, 0);
582 if (ctxt
->last
> 0) {
583 ctxt
->inptr
+= ctxt
->last
;
586 if (ctxt
->last
== 0) {
589 if (ctxt
->last
== -1) {
590 switch (socket_errno()) {
593 #if defined(EAGAIN) && EAGAIN != EWOULDBLOCK
603 __xmlIOErr(XML_FROM_HTTP
, 0, "recv failed\n");
610 if ((poll(&p
, 1, timeout
* 1000) < 1)
616 #else /* !HAVE_POLL_H */
618 if (ctxt
->fd
> FD_SETSIZE
)
627 #pragma warning(push)
628 #pragma warning(disable: 4018)
631 FD_SET(ctxt
->fd
, &rfd
);
637 if ((select(ctxt
->fd
+ 1, &rfd
, NULL
, NULL
, &tv
) < 1)
639 && (socket_errno() != EINTR
)
643 #endif /* !HAVE_POLL_H */
649 * xmlNanoHTTPReadLine:
650 * @ctxt: an HTTP context
652 * Read one line in the HTTP server output, usually for extracting
653 * the HTTP protocol informations from the answer header.
655 * Returns a newly allocated string with a copy of the line, or NULL
656 * which indicate the end of the input.
660 xmlNanoHTTPReadLine(xmlNanoHTTPCtxtPtr ctxt
) {
665 while (bp
- buf
< 4095) {
666 if (ctxt
->inrptr
== ctxt
->inptr
) {
667 if ( (rc
= xmlNanoHTTPRecv(ctxt
)) == 0) {
672 return(xmlMemStrdup(buf
));
674 else if ( rc
== -1 ) {
678 *bp
= *ctxt
->inrptr
++;
681 return(xmlMemStrdup(buf
));
687 return(xmlMemStrdup(buf
));
692 * xmlNanoHTTPScanAnswer:
693 * @ctxt: an HTTP context
694 * @line: an HTTP header line
696 * Try to extract useful informations from the server answer.
697 * We currently parse and process:
698 * - The HTTP revision/ return code
699 * - The Content-Type, Mime-Type and charset used
700 * - The Location for redirect processing.
702 * Returns -1 in case of failure, the file descriptor number otherwise
706 xmlNanoHTTPScanAnswer(xmlNanoHTTPCtxtPtr ctxt
, const char *line
) {
707 const char *cur
= line
;
709 if (line
== NULL
) return;
711 if (!strncmp(line
, "HTTP/", 5)) {
716 while ((*cur
>= '0') && (*cur
<= '9')) {
718 version
+= *cur
- '0';
723 if ((*cur
>= '0') && (*cur
<= '9')) {
725 version
+= *cur
- '0';
728 while ((*cur
>= '0') && (*cur
<= '9'))
732 if ((*cur
!= ' ') && (*cur
!= '\t')) return;
733 while ((*cur
== ' ') || (*cur
== '\t')) cur
++;
734 if ((*cur
< '0') || (*cur
> '9')) return;
735 while ((*cur
>= '0') && (*cur
<= '9')) {
740 if ((*cur
!= 0) && (*cur
!= ' ') && (*cur
!= '\t')) return;
741 ctxt
->returnValue
= ret
;
742 ctxt
->version
= version
;
743 } else if (!xmlStrncasecmp(BAD_CAST line
, BAD_CAST
"Content-Type:", 13)) {
744 const xmlChar
*charset
, *last
, *mime
;
746 while ((*cur
== ' ') || (*cur
== '\t')) cur
++;
747 if (ctxt
->contentType
!= NULL
)
748 xmlFree(ctxt
->contentType
);
749 ctxt
->contentType
= xmlMemStrdup(cur
);
750 mime
= (const xmlChar
*) cur
;
752 while ((*last
!= 0) && (*last
!= ' ') && (*last
!= '\t') &&
753 (*last
!= ';') && (*last
!= ','))
755 if (ctxt
->mimeType
!= NULL
)
756 xmlFree(ctxt
->mimeType
);
757 ctxt
->mimeType
= (char *) xmlStrndup(mime
, last
- mime
);
758 charset
= xmlStrstr(BAD_CAST ctxt
->contentType
, BAD_CAST
"charset=");
759 if (charset
!= NULL
) {
762 while ((*last
!= 0) && (*last
!= ' ') && (*last
!= '\t') &&
763 (*last
!= ';') && (*last
!= ','))
765 if (ctxt
->encoding
!= NULL
)
766 xmlFree(ctxt
->encoding
);
767 ctxt
->encoding
= (char *) xmlStrndup(charset
, last
- charset
);
769 } else if (!xmlStrncasecmp(BAD_CAST line
, BAD_CAST
"ContentType:", 12)) {
770 const xmlChar
*charset
, *last
, *mime
;
772 if (ctxt
->contentType
!= NULL
) return;
773 while ((*cur
== ' ') || (*cur
== '\t')) cur
++;
774 ctxt
->contentType
= xmlMemStrdup(cur
);
775 mime
= (const xmlChar
*) cur
;
777 while ((*last
!= 0) && (*last
!= ' ') && (*last
!= '\t') &&
778 (*last
!= ';') && (*last
!= ','))
780 if (ctxt
->mimeType
!= NULL
)
781 xmlFree(ctxt
->mimeType
);
782 ctxt
->mimeType
= (char *) xmlStrndup(mime
, last
- mime
);
783 charset
= xmlStrstr(BAD_CAST ctxt
->contentType
, BAD_CAST
"charset=");
784 if (charset
!= NULL
) {
787 while ((*last
!= 0) && (*last
!= ' ') && (*last
!= '\t') &&
788 (*last
!= ';') && (*last
!= ','))
790 if (ctxt
->encoding
!= NULL
)
791 xmlFree(ctxt
->encoding
);
792 ctxt
->encoding
= (char *) xmlStrndup(charset
, last
- charset
);
794 } else if (!xmlStrncasecmp(BAD_CAST line
, BAD_CAST
"Location:", 9)) {
796 while ((*cur
== ' ') || (*cur
== '\t')) cur
++;
797 if (ctxt
->location
!= NULL
)
798 xmlFree(ctxt
->location
);
800 xmlChar
*tmp_http
= xmlStrdup(BAD_CAST
"http://");
802 xmlStrcat(tmp_http
, (const xmlChar
*) ctxt
->hostname
);
804 (char *) xmlStrcat (tmp_loc
, (const xmlChar
*) cur
);
806 ctxt
->location
= xmlMemStrdup(cur
);
808 } else if (!xmlStrncasecmp(BAD_CAST line
, BAD_CAST
"WWW-Authenticate:", 17)) {
810 while ((*cur
== ' ') || (*cur
== '\t')) cur
++;
811 if (ctxt
->authHeader
!= NULL
)
812 xmlFree(ctxt
->authHeader
);
813 ctxt
->authHeader
= xmlMemStrdup(cur
);
814 } else if (!xmlStrncasecmp(BAD_CAST line
, BAD_CAST
"Proxy-Authenticate:", 19)) {
816 while ((*cur
== ' ') || (*cur
== '\t')) cur
++;
817 if (ctxt
->authHeader
!= NULL
)
818 xmlFree(ctxt
->authHeader
);
819 ctxt
->authHeader
= xmlMemStrdup(cur
);
821 } else if ( !xmlStrncasecmp( BAD_CAST line
, BAD_CAST
"Content-Encoding:", 17) ) {
823 while ((*cur
== ' ') || (*cur
== '\t')) cur
++;
824 if ( !xmlStrncasecmp( BAD_CAST cur
, BAD_CAST
"gzip", 4) ) {
827 ctxt
->strm
= xmlMalloc(sizeof(z_stream
));
829 if (ctxt
->strm
!= NULL
) {
830 ctxt
->strm
->zalloc
= Z_NULL
;
831 ctxt
->strm
->zfree
= Z_NULL
;
832 ctxt
->strm
->opaque
= Z_NULL
;
833 ctxt
->strm
->avail_in
= 0;
834 ctxt
->strm
->next_in
= Z_NULL
;
836 inflateInit2( ctxt
->strm
, 31 );
840 } else if ( !xmlStrncasecmp( BAD_CAST line
, BAD_CAST
"Content-Length:", 15) ) {
842 ctxt
->ContentLength
= strtol( cur
, NULL
, 10 );
847 * xmlNanoHTTPConnectAttempt:
848 * @addr: a socket address structure
850 * Attempt a connection to the given IP:port endpoint. It forces
851 * non-blocking semantic on the socket, and allow 60 seconds for
852 * the host to answer.
854 * Returns -1 in case of failure, the file descriptor number otherwise
858 xmlNanoHTTPConnectAttempt(struct sockaddr
*addr
)
866 #else /* !HAVE_POLL_H */
868 #endif /* !HAVE_POLL_H */
876 if (addr
->sa_family
== AF_INET6
) {
877 s
= socket(PF_INET6
, SOCK_STREAM
, IPPROTO_TCP
);
878 addrlen
= sizeof(struct sockaddr_in6
);
882 s
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
883 addrlen
= sizeof(struct sockaddr_in
);
885 if (s
== INVALID_SOCKET
) {
889 __xmlIOErr(XML_FROM_HTTP
, 0, "socket failed\n");
890 return INVALID_SOCKET
;
896 status
= ioctlsocket(s
, FIONBIO
, &one
) == SOCKET_ERROR
? -1 : 0;
898 #else /* _WINSOCKAPI_ */
903 status
= ioctl(s
, FIONBIO
, &enable
);
906 #if defined(__BEOS__) && !defined(__HAIKU__)
911 setsockopt(s
, SOL_SOCKET
, SO_NONBLOCK
, &noblock
,
915 if ((status
= fcntl(s
, F_GETFL
, 0)) != -1) {
917 status
|= O_NONBLOCK
;
918 #else /* O_NONBLOCK */
921 #endif /* F_NDELAY */
922 #endif /* !O_NONBLOCK */
923 status
= fcntl(s
, F_SETFL
, status
);
927 perror("nonblocking");
929 __xmlIOErr(XML_FROM_HTTP
, 0, "error setting non-blocking IO\n");
931 return INVALID_SOCKET
;
933 #endif /* !__BEOS__ */
935 #endif /* !_WINSOCKAPI_ */
937 if (connect(s
, addr
, addrlen
) == -1) {
938 switch (socket_errno()) {
943 __xmlIOErr(XML_FROM_HTTP
, 0,
944 "error connecting to HTTP server");
946 return INVALID_SOCKET
;
954 #pragma warning(push)
955 #pragma warning(disable: 4018)
959 return INVALID_SOCKET
;
968 switch (select(s
+ 1, NULL
, &wfd
, &xfd
, &tv
))
970 switch (select(s
+ 1, NULL
, &wfd
, NULL
, &tv
))
976 #else /* !HAVE_POLL_H */
979 switch (poll(&p
, 1, timeout
* 1000))
980 #endif /* !HAVE_POLL_H */
985 __xmlIOErr(XML_FROM_HTTP
, 0, "Connect attempt timed out");
987 return INVALID_SOCKET
;
990 __xmlIOErr(XML_FROM_HTTP
, 0, "Connect failed");
992 return INVALID_SOCKET
;
996 if (FD_ISSET(s
, &wfd
)
1001 #else /* !HAVE_POLL_H */
1002 if (p
.revents
== POLLOUT
)
1003 #endif /* !HAVE_POLL_H */
1007 len
= sizeof(status
);
1009 if (getsockopt(s
, SOL_SOCKET
, SO_ERROR
, (char *) &status
, &len
) <
1011 /* Solaris error code */
1012 __xmlIOErr(XML_FROM_HTTP
, 0, "getsockopt failed\n");
1014 return INVALID_SOCKET
;
1018 __xmlIOErr(XML_FROM_HTTP
, 0,
1019 "Error connecting to remote host");
1022 return INVALID_SOCKET
;
1026 __xmlIOErr(XML_FROM_HTTP
, 0, "select failed\n");
1028 return INVALID_SOCKET
;
1035 * xmlNanoHTTPConnectHost:
1036 * @host: the host name
1037 * @port: the port number
1039 * Attempt a connection to the given host:port endpoint. It tries
1040 * the multiple IP provided by the DNS if available.
1042 * Returns -1 in case of failure, the file descriptor number otherwise
1046 xmlNanoHTTPConnectHost(const char *host
, int port
)
1048 struct sockaddr
*addr
= NULL
;
1049 struct sockaddr_in sockin
;
1052 struct in6_addr ia6
;
1053 struct sockaddr_in6 sockin6
;
1057 memset (&sockin
, 0, sizeof(sockin
));
1059 memset (&sockin6
, 0, sizeof(sockin6
));
1062 #if !defined(HAVE_GETADDRINFO) && defined(SUPPORT_IP6) && defined(RES_USE_INET6)
1065 if (!(_res
.options
& RES_INIT
))
1067 _res
.options
|= RES_USE_INET6
;
1071 #if defined(HAVE_GETADDRINFO) && defined(SUPPORT_IP6) && !defined(_WIN32)
1074 #if defined(HAVE_GETADDRINFO) && (defined(SUPPORT_IP6) || defined(_WIN32))
1077 struct addrinfo hints
, *res
, *result
;
1080 memset (&hints
, 0,sizeof(hints
));
1081 hints
.ai_socktype
= SOCK_STREAM
;
1083 status
= getaddrinfo (host
, NULL
, &hints
, &result
);
1085 __xmlIOErr(XML_FROM_HTTP
, 0, "getaddrinfo failed\n");
1086 return INVALID_SOCKET
;
1089 for (res
= result
; res
; res
= res
->ai_next
) {
1090 if (res
->ai_family
== AF_INET
) {
1091 if ((size_t)res
->ai_addrlen
> sizeof(sockin
)) {
1092 __xmlIOErr(XML_FROM_HTTP
, 0, "address size mismatch\n");
1093 freeaddrinfo (result
);
1094 return INVALID_SOCKET
;
1096 memcpy (&sockin
, res
->ai_addr
, res
->ai_addrlen
);
1097 sockin
.sin_port
= htons (port
);
1098 addr
= (struct sockaddr
*)&sockin
;
1100 } else if (have_ipv6 () && (res
->ai_family
== AF_INET6
)) {
1101 if ((size_t)res
->ai_addrlen
> sizeof(sockin6
)) {
1102 __xmlIOErr(XML_FROM_HTTP
, 0, "address size mismatch\n");
1103 freeaddrinfo (result
);
1104 return INVALID_SOCKET
;
1106 memcpy (&sockin6
, res
->ai_addr
, res
->ai_addrlen
);
1107 sockin6
.sin6_port
= htons (port
);
1108 addr
= (struct sockaddr
*)&sockin6
;
1113 s
= xmlNanoHTTPConnectAttempt (addr
);
1114 if (s
!= INVALID_SOCKET
) {
1115 freeaddrinfo (result
);
1121 freeaddrinfo (result
);
1124 #if defined(HAVE_GETADDRINFO) && defined(SUPPORT_IP6) && !defined(_WIN32)
1127 #if !defined(HAVE_GETADDRINFO) || !defined(_WIN32)
1133 h
= gethostbyname (GETHOSTBYNAME_ARG_CAST host
);
1137 * Okay, I got fed up by the non-portability of this error message
1138 * extraction code. it work on Linux, if it work on your platform
1139 * and one want to enable it, send me the defined(foobar) needed
1141 #if defined(HAVE_NETDB_H) && defined(HOST_NOT_FOUND) && defined(__linux__)
1142 const char *h_err_txt
= "";
1145 case HOST_NOT_FOUND
:
1146 h_err_txt
= "Authoritive host not found";
1151 "Non-authoritive host not found or server failure.";
1156 "Non-recoverable errors: FORMERR, REFUSED, or NOTIMP.";
1162 "Valid name, no data record of requested type.";
1167 h_err_txt
= "No error text defined.";
1170 __xmlIOErr(XML_FROM_HTTP
, 0, h_err_txt
);
1172 __xmlIOErr(XML_FROM_HTTP
, 0, "Failed to resolve host");
1174 return INVALID_SOCKET
;
1177 for (i
= 0; h
->h_addr_list
[i
]; i
++) {
1178 if (h
->h_addrtype
== AF_INET
) {
1179 /* A records (IPv4) */
1180 if ((unsigned int) h
->h_length
> sizeof(ia
)) {
1181 __xmlIOErr(XML_FROM_HTTP
, 0, "address size mismatch\n");
1182 return INVALID_SOCKET
;
1184 memcpy (&ia
, h
->h_addr_list
[i
], h
->h_length
);
1185 sockin
.sin_family
= h
->h_addrtype
;
1186 sockin
.sin_addr
= ia
;
1187 sockin
.sin_port
= (unsigned short)htons ((unsigned short)port
);
1188 addr
= (struct sockaddr
*) &sockin
;
1190 } else if (have_ipv6 () && (h
->h_addrtype
== AF_INET6
)) {
1191 /* AAAA records (IPv6) */
1192 if ((unsigned int) h
->h_length
> sizeof(ia6
)) {
1193 __xmlIOErr(XML_FROM_HTTP
, 0, "address size mismatch\n");
1194 return INVALID_SOCKET
;
1196 memcpy (&ia6
, h
->h_addr_list
[i
], h
->h_length
);
1197 sockin6
.sin6_family
= h
->h_addrtype
;
1198 sockin6
.sin6_addr
= ia6
;
1199 sockin6
.sin6_port
= htons (port
);
1200 addr
= (struct sockaddr
*) &sockin6
;
1205 s
= xmlNanoHTTPConnectAttempt (addr
);
1206 if (s
!= INVALID_SOCKET
)
1213 xmlGenericError(xmlGenericErrorContext
,
1214 "xmlNanoHTTPConnectHost: unable to connect to '%s'.\n",
1217 return INVALID_SOCKET
;
1223 * @URL: The URL to load
1224 * @contentType: if available the Content-Type information will be
1225 * returned at that location
1227 * This function try to open a connection to the indicated resource
1230 * Returns NULL in case of failure, otherwise a request handler.
1231 * The contentType, if provided must be freed by the caller
1235 xmlNanoHTTPOpen(const char *URL
, char **contentType
) {
1236 if (contentType
!= NULL
) *contentType
= NULL
;
1237 return(xmlNanoHTTPMethod(URL
, NULL
, NULL
, contentType
, NULL
, 0));
1241 * xmlNanoHTTPOpenRedir:
1242 * @URL: The URL to load
1243 * @contentType: if available the Content-Type information will be
1244 * returned at that location
1245 * @redir: if available the redirected URL will be returned
1247 * This function try to open a connection to the indicated resource
1250 * Returns NULL in case of failure, otherwise a request handler.
1251 * The contentType, if provided must be freed by the caller
1255 xmlNanoHTTPOpenRedir(const char *URL
, char **contentType
, char **redir
) {
1256 if (contentType
!= NULL
) *contentType
= NULL
;
1257 if (redir
!= NULL
) *redir
= NULL
;
1258 return(xmlNanoHTTPMethodRedir(URL
, NULL
, NULL
, contentType
, redir
, NULL
,0));
1263 * @ctx: the HTTP context
1265 * @len: the buffer length
1267 * This function tries to read @len bytes from the existing HTTP connection
1268 * and saves them in @dest. This is a blocking call.
1270 * Returns the number of byte read. 0 is an indication of an end of connection.
1271 * -1 indicates a parameter error.
1274 xmlNanoHTTPRead(void *ctx
, void *dest
, int len
) {
1275 xmlNanoHTTPCtxtPtr ctxt
= (xmlNanoHTTPCtxtPtr
) ctx
;
1282 if (ctx
== NULL
) return(-1);
1283 if (dest
== NULL
) return(-1);
1284 if (len
<= 0) return(0);
1287 if (ctxt
->usesGzip
== 1) {
1288 if (ctxt
->strm
== NULL
) return(0);
1290 ctxt
->strm
->next_out
= dest
;
1291 ctxt
->strm
->avail_out
= len
;
1292 ctxt
->strm
->avail_in
= ctxt
->inptr
- ctxt
->inrptr
;
1294 while (ctxt
->strm
->avail_out
> 0 &&
1295 (ctxt
->strm
->avail_in
> 0 || xmlNanoHTTPRecv(ctxt
) > 0)) {
1296 orig_avail_in
= ctxt
->strm
->avail_in
=
1297 ctxt
->inptr
- ctxt
->inrptr
- bytes_read
;
1298 ctxt
->strm
->next_in
= BAD_CAST (ctxt
->inrptr
+ bytes_read
);
1300 z_ret
= inflate(ctxt
->strm
, Z_NO_FLUSH
);
1301 bytes_read
+= orig_avail_in
- ctxt
->strm
->avail_in
;
1303 if (z_ret
!= Z_OK
) break;
1306 ctxt
->inrptr
+= bytes_read
;
1307 return(len
- ctxt
->strm
->avail_out
);
1311 while (ctxt
->inptr
- ctxt
->inrptr
< len
) {
1312 if (xmlNanoHTTPRecv(ctxt
) <= 0) break;
1314 if (ctxt
->inptr
- ctxt
->inrptr
< len
)
1315 len
= ctxt
->inptr
- ctxt
->inrptr
;
1316 memcpy(dest
, ctxt
->inrptr
, len
);
1317 ctxt
->inrptr
+= len
;
1323 * @ctx: the HTTP context
1325 * This function closes an HTTP context, it ends up the connection and
1326 * free all data related to it.
1329 xmlNanoHTTPClose(void *ctx
) {
1330 xmlNanoHTTPCtxtPtr ctxt
= (xmlNanoHTTPCtxtPtr
) ctx
;
1332 if (ctx
== NULL
) return;
1334 xmlNanoHTTPFreeCtxt(ctxt
);
1338 * xmlNanoHTTPMethodRedir:
1339 * @URL: The URL to load
1340 * @method: the HTTP method to use
1341 * @input: the input string if any
1342 * @contentType: the Content-Type information IN and OUT
1343 * @redir: the redirected URL OUT
1344 * @headers: the extra headers
1345 * @ilen: input length
1347 * This function try to open a connection to the indicated resource
1348 * via HTTP using the given @method, adding the given extra headers
1349 * and the input buffer for the request content.
1351 * Returns NULL in case of failure, otherwise a request handler.
1352 * The contentType, or redir, if provided must be freed by the caller
1356 xmlNanoHTTPMethodRedir(const char *URL
, const char *method
, const char *input
,
1357 char **contentType
, char **redir
,
1358 const char *headers
, int ilen
) {
1359 xmlNanoHTTPCtxtPtr ctxt
;
1363 int nbRedirects
= 0;
1364 char *redirURL
= NULL
;
1369 if (URL
== NULL
) return(NULL
);
1370 if (method
== NULL
) method
= "GET";
1374 if (redirURL
== NULL
) {
1375 ctxt
= xmlNanoHTTPNewCtxt(URL
);
1379 ctxt
= xmlNanoHTTPNewCtxt(redirURL
);
1382 ctxt
->location
= xmlMemStrdup(redirURL
);
1385 if ((ctxt
->protocol
== NULL
) || (strcmp(ctxt
->protocol
, "http"))) {
1386 __xmlIOErr(XML_FROM_HTTP
, XML_HTTP_URL_SYNTAX
, "Not a valid HTTP URI");
1387 xmlNanoHTTPFreeCtxt(ctxt
);
1388 if (redirURL
!= NULL
) xmlFree(redirURL
);
1391 if (ctxt
->hostname
== NULL
) {
1392 __xmlIOErr(XML_FROM_HTTP
, XML_HTTP_UNKNOWN_HOST
,
1393 "Failed to identify host in URI");
1394 xmlNanoHTTPFreeCtxt(ctxt
);
1395 if (redirURL
!= NULL
) xmlFree(redirURL
);
1399 blen
= strlen(ctxt
->hostname
) * 2 + 16;
1400 ret
= xmlNanoHTTPConnectHost(proxy
, proxyPort
);
1403 blen
= strlen(ctxt
->hostname
);
1404 ret
= xmlNanoHTTPConnectHost(ctxt
->hostname
, ctxt
->port
);
1406 if (ret
== INVALID_SOCKET
) {
1407 xmlNanoHTTPFreeCtxt(ctxt
);
1408 if (redirURL
!= NULL
) xmlFree(redirURL
);
1418 if (headers
!= NULL
)
1419 blen
+= strlen(headers
) + 2;
1420 if (contentType
&& *contentType
)
1421 /* reserve for string plus 'Content-Type: \r\n" */
1422 blen
+= strlen(*contentType
) + 16;
1423 if (ctxt
->query
!= NULL
)
1425 blen
+= strlen(ctxt
->query
) + 1;
1426 blen
+= strlen(method
) + strlen(ctxt
->path
) + 24;
1428 /* reserve for possible 'Accept-Encoding: gzip' string */
1431 if (ctxt
->port
!= 80) {
1432 /* reserve space for ':xxxxx', incl. potential proxy */
1438 bp
= (char*)xmlMallocAtomic(blen
);
1440 xmlNanoHTTPFreeCtxt( ctxt
);
1441 xmlHTTPErrMemory("allocating header buffer");
1448 if (ctxt
->port
!= 80) {
1449 p
+= snprintf( p
, blen
- (p
- bp
), "%s http://%s:%d%s",
1450 method
, ctxt
->hostname
,
1451 ctxt
->port
, ctxt
->path
);
1454 p
+= snprintf( p
, blen
- (p
- bp
), "%s http://%s%s", method
,
1455 ctxt
->hostname
, ctxt
->path
);
1458 p
+= snprintf( p
, blen
- (p
- bp
), "%s %s", method
, ctxt
->path
);
1460 if (ctxt
->query
!= NULL
)
1461 p
+= snprintf( p
, blen
- (p
- bp
), "?%s", ctxt
->query
);
1463 if (ctxt
->port
== 80) {
1464 p
+= snprintf( p
, blen
- (p
- bp
), " HTTP/1.0\r\nHost: %s\r\n",
1467 p
+= snprintf( p
, blen
- (p
- bp
), " HTTP/1.0\r\nHost: %s:%d\r\n",
1468 ctxt
->hostname
, ctxt
->port
);
1472 p
+= snprintf(p
, blen
- (p
- bp
), "Accept-Encoding: gzip\r\n");
1475 if (contentType
!= NULL
&& *contentType
)
1476 p
+= snprintf(p
, blen
- (p
- bp
), "Content-Type: %s\r\n", *contentType
);
1478 if (headers
!= NULL
)
1479 p
+= snprintf( p
, blen
- (p
- bp
), "%s", headers
);
1482 snprintf(p
, blen
- (p
- bp
), "Content-Length: %d\r\n\r\n", ilen
);
1484 snprintf(p
, blen
- (p
- bp
), "\r\n");
1487 xmlGenericError(xmlGenericErrorContext
,
1488 "-> %s%s", proxy
? "(Proxy) " : "", bp
);
1489 if ((blen
-= strlen(bp
)+1) < 0)
1490 xmlGenericError(xmlGenericErrorContext
,
1491 "ERROR: overflowed buffer by %d bytes\n", -blen
);
1493 ctxt
->outptr
= ctxt
->out
= bp
;
1494 ctxt
->state
= XML_NANO_HTTP_WRITE
;
1495 blen
= strlen( ctxt
->out
);
1497 xmt_bytes
= xmlNanoHTTPSend(ctxt
, ctxt
->out
, blen
);
1498 if ( xmt_bytes
!= blen
)
1499 xmlGenericError( xmlGenericErrorContext
,
1500 "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
1502 "bytes of HTTP headers sent to host",
1505 xmlNanoHTTPSend(ctxt
, ctxt
->out
, blen
);
1508 if ( input
!= NULL
) {
1510 xmt_bytes
= xmlNanoHTTPSend( ctxt
, input
, ilen
);
1512 if ( xmt_bytes
!= ilen
)
1513 xmlGenericError( xmlGenericErrorContext
,
1514 "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n",
1516 "bytes of HTTP content sent to host",
1519 xmlNanoHTTPSend( ctxt
, input
, ilen
);
1523 ctxt
->state
= XML_NANO_HTTP_READ
;
1525 while ((p
= xmlNanoHTTPReadLine(ctxt
)) != NULL
) {
1527 ctxt
->content
= ctxt
->inrptr
;
1531 xmlNanoHTTPScanAnswer(ctxt
, p
);
1534 xmlGenericError(xmlGenericErrorContext
, "<- %s\n", p
);
1539 if ((ctxt
->location
!= NULL
) && (ctxt
->returnValue
>= 300) &&
1540 (ctxt
->returnValue
< 400)) {
1542 xmlGenericError(xmlGenericErrorContext
,
1543 "\nRedirect to: %s\n", ctxt
->location
);
1545 while ( xmlNanoHTTPRecv(ctxt
) > 0 )
1547 if (nbRedirects
< XML_NANO_HTTP_MAX_REDIR
) {
1549 if (redirURL
!= NULL
)
1551 redirURL
= xmlMemStrdup(ctxt
->location
);
1552 xmlNanoHTTPFreeCtxt(ctxt
);
1555 xmlNanoHTTPFreeCtxt(ctxt
);
1556 if (redirURL
!= NULL
) xmlFree(redirURL
);
1558 xmlGenericError(xmlGenericErrorContext
,
1559 "xmlNanoHTTPMethodRedir: Too many redirects, aborting ...\n");
1564 if (contentType
!= NULL
) {
1565 if (ctxt
->contentType
!= NULL
)
1566 *contentType
= xmlMemStrdup(ctxt
->contentType
);
1568 *contentType
= NULL
;
1571 if ((redir
!= NULL
) && (redirURL
!= NULL
)) {
1574 if (redirURL
!= NULL
)
1581 if (ctxt
->contentType
!= NULL
)
1582 xmlGenericError(xmlGenericErrorContext
,
1583 "\nCode %d, content-type '%s'\n\n",
1584 ctxt
->returnValue
, ctxt
->contentType
);
1586 xmlGenericError(xmlGenericErrorContext
,
1587 "\nCode %d, no content-type\n\n",
1591 return((void *) ctxt
);
1595 * xmlNanoHTTPMethod:
1596 * @URL: The URL to load
1597 * @method: the HTTP method to use
1598 * @input: the input string if any
1599 * @contentType: the Content-Type information IN and OUT
1600 * @headers: the extra headers
1601 * @ilen: input length
1603 * This function try to open a connection to the indicated resource
1604 * via HTTP using the given @method, adding the given extra headers
1605 * and the input buffer for the request content.
1607 * Returns NULL in case of failure, otherwise a request handler.
1608 * The contentType, if provided must be freed by the caller
1612 xmlNanoHTTPMethod(const char *URL
, const char *method
, const char *input
,
1613 char **contentType
, const char *headers
, int ilen
) {
1614 return(xmlNanoHTTPMethodRedir(URL
, method
, input
, contentType
,
1615 NULL
, headers
, ilen
));
1620 * @URL: The URL to load
1621 * @filename: the filename where the content should be saved
1622 * @contentType: if available the Content-Type information will be
1623 * returned at that location
1625 * This function try to fetch the indicated resource via HTTP GET
1626 * and save it's content in the file.
1628 * Returns -1 in case of failure, 0 incase of success. The contentType,
1629 * if provided must be freed by the caller
1632 xmlNanoHTTPFetch(const char *URL
, const char *filename
, char **contentType
) {
1639 if (filename
== NULL
) return(-1);
1640 ctxt
= xmlNanoHTTPOpen(URL
, contentType
);
1641 if (ctxt
== NULL
) return(-1);
1643 if (!strcmp(filename
, "-"))
1646 fd
= open(filename
, O_CREAT
| O_WRONLY
, 00644);
1648 xmlNanoHTTPClose(ctxt
);
1649 if ((contentType
!= NULL
) && (*contentType
!= NULL
)) {
1650 xmlFree(*contentType
);
1651 *contentType
= NULL
;
1657 xmlNanoHTTPFetchContent( ctxt
, &buf
, &len
);
1659 if (write(fd
, buf
, len
) == -1) {
1664 xmlNanoHTTPClose(ctxt
);
1669 #ifdef LIBXML_OUTPUT_ENABLED
1672 * @ctxt: the HTTP context
1673 * @filename: the filename where the content should be saved
1675 * This function saves the output of the HTTP transaction to a file
1676 * It closes and free the context at the end
1678 * Returns -1 in case of failure, 0 incase of success.
1681 xmlNanoHTTPSave(void *ctxt
, const char *filename
) {
1687 if ((ctxt
== NULL
) || (filename
== NULL
)) return(-1);
1689 if (!strcmp(filename
, "-"))
1692 fd
= open(filename
, O_CREAT
| O_WRONLY
, 0666);
1694 xmlNanoHTTPClose(ctxt
);
1699 xmlNanoHTTPFetchContent( ctxt
, &buf
, &len
);
1701 if (write(fd
, buf
, len
) == -1) {
1706 xmlNanoHTTPClose(ctxt
);
1710 #endif /* LIBXML_OUTPUT_ENABLED */
1713 * xmlNanoHTTPReturnCode:
1714 * @ctx: the HTTP context
1716 * Get the latest HTTP return code received
1718 * Returns the HTTP return code for the request.
1721 xmlNanoHTTPReturnCode(void *ctx
) {
1722 xmlNanoHTTPCtxtPtr ctxt
= (xmlNanoHTTPCtxtPtr
) ctx
;
1724 if (ctxt
== NULL
) return(-1);
1726 return(ctxt
->returnValue
);
1730 * xmlNanoHTTPAuthHeader:
1731 * @ctx: the HTTP context
1733 * Get the authentication header of an HTTP context
1735 * Returns the stashed value of the WWW-Authenticate or Proxy-Authenticate
1739 xmlNanoHTTPAuthHeader(void *ctx
) {
1740 xmlNanoHTTPCtxtPtr ctxt
= (xmlNanoHTTPCtxtPtr
) ctx
;
1742 if (ctxt
== NULL
) return(NULL
);
1744 return(ctxt
->authHeader
);
1748 * xmlNanoHTTPContentLength:
1749 * @ctx: the HTTP context
1751 * Provides the specified content length from the HTTP header.
1753 * Return the specified content length from the HTTP header. Note that
1754 * a value of -1 indicates that the content length element was not included in
1755 * the response header.
1758 xmlNanoHTTPContentLength( void * ctx
) {
1759 xmlNanoHTTPCtxtPtr ctxt
= (xmlNanoHTTPCtxtPtr
)ctx
;
1761 return ( ( ctxt
== NULL
) ? -1 : ctxt
->ContentLength
);
1766 * @ctx: the HTTP context
1768 * Provides the specified redirection URL if available from the HTTP header.
1770 * Return the specified redirection URL or NULL if not redirected.
1773 xmlNanoHTTPRedir( void * ctx
) {
1774 xmlNanoHTTPCtxtPtr ctxt
= (xmlNanoHTTPCtxtPtr
)ctx
;
1776 return ( ( ctxt
== NULL
) ? NULL
: ctxt
->location
);
1780 * xmlNanoHTTPEncoding:
1781 * @ctx: the HTTP context
1783 * Provides the specified encoding if specified in the HTTP headers.
1785 * Return the specified encoding or NULL if not available
1788 xmlNanoHTTPEncoding( void * ctx
) {
1789 xmlNanoHTTPCtxtPtr ctxt
= (xmlNanoHTTPCtxtPtr
)ctx
;
1791 return ( ( ctxt
== NULL
) ? NULL
: ctxt
->encoding
);
1795 * xmlNanoHTTPMimeType:
1796 * @ctx: the HTTP context
1798 * Provides the specified Mime-Type if specified in the HTTP headers.
1800 * Return the specified Mime-Type or NULL if not available
1803 xmlNanoHTTPMimeType( void * ctx
) {
1804 xmlNanoHTTPCtxtPtr ctxt
= (xmlNanoHTTPCtxtPtr
)ctx
;
1806 return ( ( ctxt
== NULL
) ? NULL
: ctxt
->mimeType
);
1810 * xmlNanoHTTPFetchContent:
1811 * @ctx: the HTTP context
1812 * @ptr: pointer to set to the content buffer.
1813 * @len: integer pointer to hold the length of the content
1815 * Check if all the content was read
1817 * Returns 0 if all the content was read and available, returns
1818 * -1 if received content length was less than specified or an error
1822 xmlNanoHTTPFetchContent( void * ctx
, char ** ptr
, int * len
) {
1823 xmlNanoHTTPCtxtPtr ctxt
= (xmlNanoHTTPCtxtPtr
)ctx
;
1829 char * dummy_ptr
= NULL
;
1831 /* Dummy up return input parameters if not provided */
1839 /* But can't work without the context pointer */
1841 if ( ( ctxt
== NULL
) || ( ctxt
->content
== NULL
) ) {
1847 rcvd_lgth
= ctxt
->inptr
- ctxt
->content
;
1849 while ( (cur_lgth
= xmlNanoHTTPRecv( ctxt
)) > 0 ) {
1851 rcvd_lgth
+= cur_lgth
;
1852 if ( (ctxt
->ContentLength
> 0) && (rcvd_lgth
>= ctxt
->ContentLength
) )
1856 *ptr
= ctxt
->content
;
1859 if ( ( ctxt
->ContentLength
> 0 ) && ( rcvd_lgth
< ctxt
->ContentLength
) )
1861 else if ( rcvd_lgth
== 0 )
1868 int main(int argc
, char **argv
) {
1869 char *contentType
= NULL
;
1871 if (argv
[1] != NULL
) {
1872 if (argv
[2] != NULL
)
1873 xmlNanoHTTPFetch(argv
[1], argv
[2], &contentType
);
1875 xmlNanoHTTPFetch(argv
[1], "-", &contentType
);
1876 if (contentType
!= NULL
) xmlFree(contentType
);
1878 xmlGenericError(xmlGenericErrorContext
,
1879 "%s: minimal HTTP GET implementation\n", argv
[0]);
1880 xmlGenericError(xmlGenericErrorContext
,
1881 "\tusage %s [ URL [ filename ] ]\n", argv
[0]);
1883 xmlNanoHTTPCleanup();
1887 #endif /* STANDALONE */
1888 #else /* !LIBXML_HTTP_ENABLED */
1891 int main(int argc
, char **argv
) {
1892 xmlGenericError(xmlGenericErrorContext
,
1893 "%s : HTTP support not compiled in\n", argv
[0]);
1896 #endif /* STANDALONE */
1897 #endif /* LIBXML_HTTP_ENABLED */
1898 #define bottom_nanohttp
1899 #include "elfgcchack.h"