Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * nanohttp.c: minimalist HTTP GET implementation to fetch external subsets. |
| 3 | * focuses on size, streamability, reentrancy and portability |
| 4 | * |
| 5 | * This is clearly not a general purpose HTTP implementation |
| 6 | * If you look for one, check: |
| 7 | * http://www.w3.org/Library/ |
| 8 | * |
| 9 | * See Copyright for the status of this software. |
| 10 | * |
Daniel Veillard | c5d6434 | 2001-06-24 12:13:24 +0000 | [diff] [blame] | 11 | * daniel@veillard.com |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12 | */ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 13 | |
Daniel Veillard | f3afa7d | 2001-06-09 13:52:58 +0000 | [diff] [blame] | 14 | #define NEED_SOCKETS |
Daniel Veillard | 34ce8be | 2002-03-18 19:37:11 +0000 | [diff] [blame] | 15 | #define IN_LIBXML |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 16 | #include "libxml.h" |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 17 | |
| 18 | #ifdef LIBXML_HTTP_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 19 | #include <string.h> |
| 20 | |
| 21 | #ifdef HAVE_STDLIB_H |
| 22 | #include <stdlib.h> |
| 23 | #endif |
| 24 | #ifdef HAVE_UNISTD_H |
| 25 | #include <unistd.h> |
| 26 | #endif |
Daniel Veillard | 75eb1ad | 2003-07-07 14:42:44 +0000 | [diff] [blame] | 27 | #ifdef HAVE_SYS_TYPES_H |
| 28 | #include <sys/types.h> |
| 29 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 30 | #ifdef HAVE_SYS_SOCKET_H |
| 31 | #include <sys/socket.h> |
| 32 | #endif |
| 33 | #ifdef HAVE_NETINET_IN_H |
| 34 | #include <netinet/in.h> |
| 35 | #endif |
| 36 | #ifdef HAVE_ARPA_INET_H |
| 37 | #include <arpa/inet.h> |
| 38 | #endif |
| 39 | #ifdef HAVE_NETDB_H |
| 40 | #include <netdb.h> |
| 41 | #endif |
Daniel Veillard | d85f4f4 | 2002-03-25 10:48:46 +0000 | [diff] [blame] | 42 | #ifdef HAVE_RESOLV_H |
Daniel Veillard | 9b731d7 | 2002-04-14 12:56:08 +0000 | [diff] [blame] | 43 | #ifdef HAVE_ARPA_NAMESER_H |
| 44 | #include <arpa/nameser.h> |
| 45 | #endif |
Daniel Veillard | d85f4f4 | 2002-03-25 10:48:46 +0000 | [diff] [blame] | 46 | #include <resolv.h> |
| 47 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 48 | #ifdef HAVE_FCNTL_H |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 49 | #include <fcntl.h> |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 50 | #endif |
| 51 | #ifdef HAVE_ERRNO_H |
| 52 | #include <errno.h> |
| 53 | #endif |
| 54 | #ifdef HAVE_SYS_TIME_H |
| 55 | #include <sys/time.h> |
| 56 | #endif |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 57 | #ifndef HAVE_POLL_H |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 58 | #ifdef HAVE_SYS_SELECT_H |
| 59 | #include <sys/select.h> |
| 60 | #endif |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 61 | #else |
| 62 | #include <poll.h> |
| 63 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 64 | #ifdef HAVE_STRINGS_H |
| 65 | #include <strings.h> |
| 66 | #endif |
Daniel Veillard | 9a2724d | 2005-12-15 11:12:26 +0000 | [diff] [blame] | 67 | #ifdef HAVE_ZLIB_H |
| 68 | #include <zlib.h> |
| 69 | #endif |
| 70 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 71 | |
| 72 | #ifdef VMS |
| 73 | #include <stropts> |
Daniel Veillard | c284c64 | 2005-03-31 10:24:24 +0000 | [diff] [blame] | 74 | #define XML_SOCKLEN_T unsigned int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 75 | #endif |
| 76 | |
Daniel Veillard | 59d3ed8 | 2007-04-17 12:44:58 +0000 | [diff] [blame] | 77 | #if defined(__MINGW32__) || defined(_WIN32_WCE) |
Ozkan Sezer | f99d222 | 2010-11-04 12:08:08 +0100 | [diff] [blame] | 78 | #ifndef _WINSOCKAPI_ |
Daniel Veillard | 1638a47 | 2003-08-14 01:23:25 +0000 | [diff] [blame] | 79 | #define _WINSOCKAPI_ |
Ozkan Sezer | f99d222 | 2010-11-04 12:08:08 +0100 | [diff] [blame] | 80 | #endif |
Daniel Veillard | 1638a47 | 2003-08-14 01:23:25 +0000 | [diff] [blame] | 81 | #include <wsockcompat.h> |
| 82 | #include <winsock2.h> |
Daniel Veillard | c284c64 | 2005-03-31 10:24:24 +0000 | [diff] [blame] | 83 | #undef XML_SOCKLEN_T |
| 84 | #define XML_SOCKLEN_T unsigned int |
Daniel Veillard | 1638a47 | 2003-08-14 01:23:25 +0000 | [diff] [blame] | 85 | #endif |
| 86 | |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 87 | #include <libxml/globals.h> |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 88 | #include <libxml/xmlerror.h> |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 89 | #include <libxml/xmlmemory.h> |
| 90 | #include <libxml/parser.h> /* for xmlStr(n)casecmp() */ |
| 91 | #include <libxml/nanohttp.h> |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 92 | #include <libxml/globals.h> |
Daniel Veillard | 8efff67 | 2002-12-04 11:44:48 +0000 | [diff] [blame] | 93 | #include <libxml/uri.h> |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 94 | |
| 95 | /** |
| 96 | * A couple portability macros |
| 97 | */ |
| 98 | #ifndef _WINSOCKAPI_ |
Daniel Veillard | cba6839 | 2008-08-29 12:43:40 +0000 | [diff] [blame] | 99 | #if !defined(__BEOS__) || defined(__HAIKU__) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 100 | #define closesocket(s) close(s) |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 101 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 102 | #define SOCKET int |
Ozkan Sezer | f99d222 | 2010-11-04 12:08:08 +0100 | [diff] [blame] | 103 | #define INVALID_SOCKET (-1) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 104 | #endif |
| 105 | |
Daniel Veillard | 89f7f27 | 2003-09-29 13:29:09 +0000 | [diff] [blame] | 106 | #ifdef __BEOS__ |
| 107 | #ifndef PF_INET |
| 108 | #define PF_INET AF_INET |
| 109 | #endif |
| 110 | #endif |
| 111 | |
Daniel Veillard | c284c64 | 2005-03-31 10:24:24 +0000 | [diff] [blame] | 112 | #ifndef XML_SOCKLEN_T |
| 113 | #define XML_SOCKLEN_T unsigned int |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 114 | #endif |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 115 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 116 | #ifdef STANDALONE |
| 117 | #define DEBUG_HTTP |
| 118 | #define xmlStrncasecmp(a, b, n) strncasecmp((char *)a, (char *)b, n) |
| 119 | #define xmlStrcasecmpi(a, b) strcasecmp((char *)a, (char *)b) |
| 120 | #endif |
| 121 | |
| 122 | #define XML_NANO_HTTP_MAX_REDIR 10 |
| 123 | |
| 124 | #define XML_NANO_HTTP_CHUNK 4096 |
| 125 | |
| 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 |
| 130 | |
| 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 */ |
Daniel Veillard | 351f2d6 | 2005-04-13 02:55:12 +0000 | [diff] [blame] | 136 | char *query; /* the query string */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 137 | SOCKET fd; /* the file descriptor for the socket */ |
| 138 | int state; /* WRITE / READ / CLOSED */ |
| 139 | char *out; /* buffer sent (zero terminated) */ |
| 140 | char *outptr; /* index within the buffer sent */ |
| 141 | char *in; /* the receiving buffer */ |
| 142 | char *content; /* the start of the content */ |
| 143 | char *inptr; /* the next byte to read from network */ |
| 144 | char *inrptr; /* the next byte to give back to the client */ |
| 145 | int inlen; /* len of the input buffer */ |
| 146 | int last; /* return code for last operation */ |
| 147 | int returnValue; /* the protocol return value */ |
Daniel Veillard | 13cee4e | 2009-09-05 14:52:55 +0200 | [diff] [blame] | 148 | int version; /* the protocol version */ |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 149 | int ContentLength; /* specified content length from HTTP header */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 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 */ |
Daniel Veillard | 847332a | 2003-10-18 11:29:40 +0000 | [diff] [blame] | 153 | char *encoding; /* encoding extracted from the contentType */ |
Daniel Veillard | a840b69 | 2003-10-19 13:35:37 +0000 | [diff] [blame] | 154 | char *mimeType; /* Mime-Type extracted from the contentType */ |
Daniel Veillard | 9a2724d | 2005-12-15 11:12:26 +0000 | [diff] [blame] | 155 | #ifdef HAVE_ZLIB_H |
| 156 | z_stream *strm; /* Zlib stream object */ |
| 157 | int usesGzip; /* "Content-Encoding: gzip" was detected */ |
| 158 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 159 | } xmlNanoHTTPCtxt, *xmlNanoHTTPCtxtPtr; |
| 160 | |
| 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 */ |
| 165 | |
Daniel Veillard | a235132 | 2004-06-27 12:08:10 +0000 | [diff] [blame] | 166 | static int xmlNanoHTTPFetchContent( void * ctx, char ** ptr, int * len ); |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 167 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 168 | /** |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 169 | * xmlHTTPErrMemory: |
| 170 | * @extra: extra informations |
| 171 | * |
| 172 | * Handle an out of memory condition |
| 173 | */ |
| 174 | static void |
| 175 | xmlHTTPErrMemory(const char *extra) |
| 176 | { |
| 177 | __xmlSimpleError(XML_FROM_HTTP, XML_ERR_NO_MEMORY, NULL, NULL, extra); |
| 178 | } |
| 179 | |
| 180 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 181 | * A portability function |
| 182 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 183 | static int socket_errno(void) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 184 | #ifdef _WINSOCKAPI_ |
| 185 | return(WSAGetLastError()); |
| 186 | #else |
| 187 | return(errno); |
| 188 | #endif |
| 189 | } |
| 190 | |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 191 | #ifdef SUPPORT_IP6 |
Daniel Veillard | 2db8c12 | 2003-07-08 12:16:59 +0000 | [diff] [blame] | 192 | static |
| 193 | int have_ipv6(void) { |
Ozkan Sezer | f99d222 | 2010-11-04 12:08:08 +0100 | [diff] [blame] | 194 | SOCKET s; |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 195 | |
| 196 | s = socket (AF_INET6, SOCK_STREAM, 0); |
Ozkan Sezer | f99d222 | 2010-11-04 12:08:08 +0100 | [diff] [blame] | 197 | if (s != INVALID_SOCKET) { |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 198 | close (s); |
| 199 | return (1); |
| 200 | } |
| 201 | return (0); |
| 202 | } |
| 203 | #endif |
| 204 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 205 | /** |
| 206 | * xmlNanoHTTPInit: |
| 207 | * |
| 208 | * Initialize the HTTP protocol layer. |
| 209 | * Currently it just checks for proxy informations |
| 210 | */ |
| 211 | |
| 212 | void |
| 213 | xmlNanoHTTPInit(void) { |
| 214 | const char *env; |
| 215 | #ifdef _WINSOCKAPI_ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 216 | WSADATA wsaData; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 217 | #endif |
| 218 | |
| 219 | if (initialized) |
| 220 | return; |
| 221 | |
| 222 | #ifdef _WINSOCKAPI_ |
| 223 | if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) |
| 224 | return; |
| 225 | #endif |
| 226 | |
| 227 | if (proxy == NULL) { |
| 228 | proxyPort = 80; |
| 229 | env = getenv("no_proxy"); |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 230 | if (env && ((env[0] == '*') && (env[1] == 0))) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 231 | goto done; |
| 232 | env = getenv("http_proxy"); |
| 233 | if (env != NULL) { |
| 234 | xmlNanoHTTPScanProxy(env); |
| 235 | goto done; |
| 236 | } |
| 237 | env = getenv("HTTP_PROXY"); |
| 238 | if (env != NULL) { |
| 239 | xmlNanoHTTPScanProxy(env); |
| 240 | goto done; |
| 241 | } |
| 242 | } |
| 243 | done: |
| 244 | initialized = 1; |
| 245 | } |
| 246 | |
| 247 | /** |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 248 | * xmlNanoHTTPCleanup: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 249 | * |
| 250 | * Cleanup the HTTP protocol layer. |
| 251 | */ |
| 252 | |
| 253 | void |
| 254 | xmlNanoHTTPCleanup(void) { |
Daniel Veillard | 744acff | 2005-07-12 15:09:53 +0000 | [diff] [blame] | 255 | if (proxy != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 256 | xmlFree(proxy); |
Daniel Veillard | 744acff | 2005-07-12 15:09:53 +0000 | [diff] [blame] | 257 | proxy = NULL; |
| 258 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 259 | #ifdef _WINSOCKAPI_ |
| 260 | if (initialized) |
| 261 | WSACleanup(); |
| 262 | #endif |
| 263 | initialized = 0; |
| 264 | return; |
| 265 | } |
| 266 | |
| 267 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 268 | * xmlNanoHTTPScanURL: |
| 269 | * @ctxt: an HTTP context |
| 270 | * @URL: The URL used to initialize the context |
| 271 | * |
| 272 | * (Re)Initialize an HTTP context by parsing the URL and finding |
| 273 | * the protocol host port and path it indicates. |
| 274 | */ |
| 275 | |
| 276 | static void |
| 277 | xmlNanoHTTPScanURL(xmlNanoHTTPCtxtPtr ctxt, const char *URL) { |
William M. Brack | 015ccb2 | 2005-02-13 08:18:52 +0000 | [diff] [blame] | 278 | xmlURIPtr uri; |
Steve Wolf | 19d785b | 2013-02-28 18:22:46 +0800 | [diff] [blame] | 279 | int len; |
| 280 | |
William M. Brack | 015ccb2 | 2005-02-13 08:18:52 +0000 | [diff] [blame] | 281 | /* |
| 282 | * Clear any existing data from the context |
| 283 | */ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 284 | if (ctxt->protocol != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 285 | xmlFree(ctxt->protocol); |
| 286 | ctxt->protocol = NULL; |
| 287 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 288 | if (ctxt->hostname != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 289 | xmlFree(ctxt->hostname); |
| 290 | ctxt->hostname = NULL; |
| 291 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 292 | if (ctxt->path != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 293 | xmlFree(ctxt->path); |
| 294 | ctxt->path = NULL; |
| 295 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 296 | if (ctxt->query != NULL) { |
Daniel Veillard | 351f2d6 | 2005-04-13 02:55:12 +0000 | [diff] [blame] | 297 | xmlFree(ctxt->query); |
| 298 | ctxt->query = NULL; |
| 299 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 300 | if (URL == NULL) return; |
William M. Brack | 015ccb2 | 2005-02-13 08:18:52 +0000 | [diff] [blame] | 301 | |
Daniel Veillard | 336a8e1 | 2005-08-07 10:46:19 +0000 | [diff] [blame] | 302 | uri = xmlParseURIRaw(URL, 1); |
William M. Brack | 015ccb2 | 2005-02-13 08:18:52 +0000 | [diff] [blame] | 303 | if (uri == NULL) |
| 304 | return; |
| 305 | |
| 306 | if ((uri->scheme == NULL) || (uri->server == NULL)) { |
| 307 | xmlFreeURI(uri); |
| 308 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 309 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 310 | |
William M. Brack | 015ccb2 | 2005-02-13 08:18:52 +0000 | [diff] [blame] | 311 | ctxt->protocol = xmlMemStrdup(uri->scheme); |
Steve Wolf | 19d785b | 2013-02-28 18:22:46 +0800 | [diff] [blame] | 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); |
| 317 | } else |
| 318 | ctxt->hostname = xmlMemStrdup(uri->server); |
| 319 | } else |
| 320 | ctxt->hostname = xmlMemStrdup(uri->server); |
William M. Brack | 015ccb2 | 2005-02-13 08:18:52 +0000 | [diff] [blame] | 321 | if (uri->path != NULL) |
| 322 | ctxt->path = xmlMemStrdup(uri->path); |
| 323 | else |
| 324 | ctxt->path = xmlMemStrdup("/"); |
Daniel Veillard | 351f2d6 | 2005-04-13 02:55:12 +0000 | [diff] [blame] | 325 | if (uri->query != NULL) |
| 326 | ctxt->query = xmlMemStrdup(uri->query); |
William M. Brack | 015ccb2 | 2005-02-13 08:18:52 +0000 | [diff] [blame] | 327 | if (uri->port != 0) |
| 328 | ctxt->port = uri->port; |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 329 | |
William M. Brack | 015ccb2 | 2005-02-13 08:18:52 +0000 | [diff] [blame] | 330 | xmlFreeURI(uri); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | /** |
| 334 | * xmlNanoHTTPScanProxy: |
| 335 | * @URL: The proxy URL used to initialize the proxy context |
| 336 | * |
| 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. |
| 341 | */ |
| 342 | |
| 343 | void |
| 344 | xmlNanoHTTPScanProxy(const char *URL) { |
William M. Brack | 015ccb2 | 2005-02-13 08:18:52 +0000 | [diff] [blame] | 345 | xmlURIPtr uri; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 346 | |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 347 | if (proxy != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 348 | xmlFree(proxy); |
| 349 | proxy = NULL; |
| 350 | } |
William M. Brack | 015ccb2 | 2005-02-13 08:18:52 +0000 | [diff] [blame] | 351 | proxyPort = 0; |
| 352 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 353 | #ifdef DEBUG_HTTP |
| 354 | if (URL == NULL) |
| 355 | xmlGenericError(xmlGenericErrorContext, |
| 356 | "Removing HTTP proxy info\n"); |
| 357 | else |
| 358 | xmlGenericError(xmlGenericErrorContext, |
| 359 | "Using HTTP proxy %s\n", URL); |
| 360 | #endif |
| 361 | if (URL == NULL) return; |
William M. Brack | 015ccb2 | 2005-02-13 08:18:52 +0000 | [diff] [blame] | 362 | |
Daniel Veillard | 336a8e1 | 2005-08-07 10:46:19 +0000 | [diff] [blame] | 363 | uri = xmlParseURIRaw(URL, 1); |
William M. Brack | 015ccb2 | 2005-02-13 08:18:52 +0000 | [diff] [blame] | 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"); |
| 367 | if (uri != NULL) |
| 368 | xmlFreeURI(uri); |
| 369 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 370 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 371 | |
William M. Brack | 015ccb2 | 2005-02-13 08:18:52 +0000 | [diff] [blame] | 372 | proxy = xmlMemStrdup(uri->server); |
| 373 | if (uri->port != 0) |
| 374 | proxyPort = uri->port; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 375 | |
William M. Brack | 015ccb2 | 2005-02-13 08:18:52 +0000 | [diff] [blame] | 376 | xmlFreeURI(uri); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | /** |
| 380 | * xmlNanoHTTPNewCtxt: |
| 381 | * @URL: The URL used to initialize the context |
| 382 | * |
| 383 | * Allocate and initialize a new HTTP context. |
| 384 | * |
| 385 | * Returns an HTTP context or NULL in case of error. |
| 386 | */ |
| 387 | |
| 388 | static xmlNanoHTTPCtxtPtr |
| 389 | xmlNanoHTTPNewCtxt(const char *URL) { |
| 390 | xmlNanoHTTPCtxtPtr ret; |
| 391 | |
| 392 | ret = (xmlNanoHTTPCtxtPtr) xmlMalloc(sizeof(xmlNanoHTTPCtxt)); |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 393 | if (ret == NULL) { |
| 394 | xmlHTTPErrMemory("allocating context"); |
| 395 | return(NULL); |
| 396 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 397 | |
| 398 | memset(ret, 0, sizeof(xmlNanoHTTPCtxt)); |
| 399 | ret->port = 80; |
| 400 | ret->returnValue = 0; |
Ozkan Sezer | f99d222 | 2010-11-04 12:08:08 +0100 | [diff] [blame] | 401 | ret->fd = INVALID_SOCKET; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 402 | ret->ContentLength = -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 403 | |
Daniel Veillard | cacbe5d | 2003-01-10 16:09:51 +0000 | [diff] [blame] | 404 | xmlNanoHTTPScanURL(ret, URL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 405 | |
| 406 | return(ret); |
| 407 | } |
| 408 | |
| 409 | /** |
| 410 | * xmlNanoHTTPFreeCtxt: |
| 411 | * @ctxt: an HTTP context |
| 412 | * |
| 413 | * Frees the context after closing the connection. |
| 414 | */ |
| 415 | |
| 416 | static void |
| 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); |
Daniel Veillard | 351f2d6 | 2005-04-13 02:55:12 +0000 | [diff] [blame] | 422 | if (ctxt->query != NULL) xmlFree(ctxt->query); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 423 | if (ctxt->out != NULL) xmlFree(ctxt->out); |
| 424 | if (ctxt->in != NULL) xmlFree(ctxt->in); |
| 425 | if (ctxt->contentType != NULL) xmlFree(ctxt->contentType); |
Daniel Veillard | 847332a | 2003-10-18 11:29:40 +0000 | [diff] [blame] | 426 | if (ctxt->encoding != NULL) xmlFree(ctxt->encoding); |
Daniel Veillard | a840b69 | 2003-10-19 13:35:37 +0000 | [diff] [blame] | 427 | if (ctxt->mimeType != NULL) xmlFree(ctxt->mimeType); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 428 | if (ctxt->location != NULL) xmlFree(ctxt->location); |
| 429 | if (ctxt->authHeader != NULL) xmlFree(ctxt->authHeader); |
Daniel Veillard | 9a2724d | 2005-12-15 11:12:26 +0000 | [diff] [blame] | 430 | #ifdef HAVE_ZLIB_H |
| 431 | if (ctxt->strm != NULL) { |
| 432 | inflateEnd(ctxt->strm); |
| 433 | xmlFree(ctxt->strm); |
| 434 | } |
| 435 | #endif |
| 436 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 437 | ctxt->state = XML_NANO_HTTP_NONE; |
Ozkan Sezer | f99d222 | 2010-11-04 12:08:08 +0100 | [diff] [blame] | 438 | if (ctxt->fd != INVALID_SOCKET) closesocket(ctxt->fd); |
| 439 | ctxt->fd = INVALID_SOCKET; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 440 | xmlFree(ctxt); |
| 441 | } |
| 442 | |
| 443 | /** |
| 444 | * xmlNanoHTTPSend: |
| 445 | * @ctxt: an HTTP context |
| 446 | * |
| 447 | * Send the input needed to initiate the processing on the server side |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 448 | * Returns number of bytes sent or -1 on error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 449 | */ |
| 450 | |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 451 | static int |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 452 | xmlNanoHTTPSend(xmlNanoHTTPCtxtPtr ctxt, const char *xmt_ptr, int outlen) |
| 453 | { |
| 454 | int total_sent = 0; |
| 455 | #ifdef HAVE_POLL_H |
| 456 | struct pollfd p; |
| 457 | #else |
| 458 | struct timeval tv; |
| 459 | fd_set wfd; |
| 460 | #endif |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 461 | |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 462 | if ((ctxt->state & XML_NANO_HTTP_WRITE) && (xmt_ptr != NULL)) { |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 463 | while (total_sent < outlen) { |
Patrick Monnerat | 437f4f5 | 2013-12-12 15:23:09 +0800 | [diff] [blame] | 464 | int nsent = send(ctxt->fd, SEND_ARG2_CAST (xmt_ptr + total_sent), |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 465 | outlen - total_sent, 0); |
| 466 | |
| 467 | if (nsent > 0) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 468 | total_sent += nsent; |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 469 | else if ((nsent == -1) && |
Daniel Veillard | ba6db03 | 2001-07-31 16:25:45 +0000 | [diff] [blame] | 470 | #if defined(EAGAIN) && EAGAIN != EWOULDBLOCK |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 471 | (socket_errno() != EAGAIN) && |
Daniel Veillard | ba6db03 | 2001-07-31 16:25:45 +0000 | [diff] [blame] | 472 | #endif |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 473 | (socket_errno() != EWOULDBLOCK)) { |
| 474 | __xmlIOErr(XML_FROM_HTTP, 0, "send failed\n"); |
| 475 | if (total_sent == 0) |
| 476 | total_sent = -1; |
| 477 | break; |
| 478 | } else { |
| 479 | /* |
| 480 | * No data sent |
| 481 | * Since non-blocking sockets are used, wait for |
| 482 | * socket to be writable or default timeout prior |
| 483 | * to retrying. |
| 484 | */ |
| 485 | #ifndef HAVE_POLL_H |
spadix | d29a5c8 | 2009-10-19 14:03:25 +0200 | [diff] [blame] | 486 | #ifndef _WINSOCKAPI_ |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 487 | if (ctxt->fd > FD_SETSIZE) |
| 488 | return -1; |
spadix | d29a5c8 | 2009-10-19 14:03:25 +0200 | [diff] [blame] | 489 | #endif |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 490 | |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 491 | tv.tv_sec = timeout; |
| 492 | tv.tv_usec = 0; |
| 493 | FD_ZERO(&wfd); |
Daniel Veillard | 9e2110b | 2005-08-08 20:33:54 +0000 | [diff] [blame] | 494 | #ifdef _MSC_VER |
| 495 | #pragma warning(push) |
| 496 | #pragma warning(disable: 4018) |
| 497 | #endif |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 498 | FD_SET(ctxt->fd, &wfd); |
Daniel Veillard | 9e2110b | 2005-08-08 20:33:54 +0000 | [diff] [blame] | 499 | #ifdef _MSC_VER |
| 500 | #pragma warning(pop) |
| 501 | #endif |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 502 | (void) select(ctxt->fd + 1, NULL, &wfd, NULL, &tv); |
| 503 | #else |
| 504 | p.fd = ctxt->fd; |
| 505 | p.events = POLLOUT; |
| 506 | (void) poll(&p, 1, timeout * 1000); |
| 507 | #endif /* !HAVE_POLL_H */ |
| 508 | } |
| 509 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 510 | } |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 511 | |
| 512 | return total_sent; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | /** |
| 516 | * xmlNanoHTTPRecv: |
| 517 | * @ctxt: an HTTP context |
| 518 | * |
| 519 | * Read information coming from the HTTP connection. |
| 520 | * This is a blocking call (but it blocks in select(), not read()). |
| 521 | * |
| 522 | * Returns the number of byte read or -1 in case of error. |
| 523 | */ |
| 524 | |
| 525 | static int |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 526 | xmlNanoHTTPRecv(xmlNanoHTTPCtxtPtr ctxt) |
| 527 | { |
| 528 | #ifdef HAVE_POLL_H |
| 529 | struct pollfd p; |
| 530 | #else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 531 | fd_set rfd; |
| 532 | struct timeval tv; |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 533 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 534 | |
| 535 | |
| 536 | while (ctxt->state & XML_NANO_HTTP_READ) { |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 537 | if (ctxt->in == NULL) { |
| 538 | ctxt->in = (char *) xmlMallocAtomic(65000 * sizeof(char)); |
| 539 | if (ctxt->in == NULL) { |
| 540 | xmlHTTPErrMemory("allocating input"); |
| 541 | ctxt->last = -1; |
| 542 | return (-1); |
| 543 | } |
| 544 | ctxt->inlen = 65000; |
| 545 | ctxt->inptr = ctxt->content = ctxt->inrptr = ctxt->in; |
| 546 | } |
| 547 | if (ctxt->inrptr > ctxt->in + XML_NANO_HTTP_CHUNK) { |
| 548 | int delta = ctxt->inrptr - ctxt->in; |
| 549 | int len = ctxt->inptr - ctxt->inrptr; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 550 | |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 551 | memmove(ctxt->in, ctxt->inrptr, len); |
| 552 | ctxt->inrptr -= delta; |
| 553 | ctxt->content -= delta; |
| 554 | ctxt->inptr -= delta; |
| 555 | } |
| 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; |
| 561 | |
| 562 | ctxt->inlen *= 2; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 563 | ctxt->in = (char *) xmlRealloc(tmp_ptr, ctxt->inlen); |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 564 | if (ctxt->in == NULL) { |
| 565 | xmlHTTPErrMemory("allocating input buffer"); |
| 566 | xmlFree(tmp_ptr); |
| 567 | ctxt->last = -1; |
| 568 | return (-1); |
| 569 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 570 | ctxt->inptr = ctxt->in + d_inptr; |
| 571 | ctxt->content = ctxt->in + d_content; |
| 572 | ctxt->inrptr = ctxt->in + d_inrptr; |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 573 | } |
| 574 | ctxt->last = recv(ctxt->fd, ctxt->inptr, XML_NANO_HTTP_CHUNK, 0); |
| 575 | if (ctxt->last > 0) { |
| 576 | ctxt->inptr += ctxt->last; |
| 577 | return (ctxt->last); |
| 578 | } |
| 579 | if (ctxt->last == 0) { |
| 580 | return (0); |
| 581 | } |
| 582 | if (ctxt->last == -1) { |
| 583 | switch (socket_errno()) { |
| 584 | case EINPROGRESS: |
| 585 | case EWOULDBLOCK: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 586 | #if defined(EAGAIN) && EAGAIN != EWOULDBLOCK |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 587 | case EAGAIN: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 588 | #endif |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 589 | break; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 590 | |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 591 | case ECONNRESET: |
| 592 | case ESHUTDOWN: |
| 593 | return (0); |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 594 | |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 595 | default: |
| 596 | __xmlIOErr(XML_FROM_HTTP, 0, "recv failed\n"); |
| 597 | return (-1); |
| 598 | } |
| 599 | } |
| 600 | #ifdef HAVE_POLL_H |
| 601 | p.fd = ctxt->fd; |
| 602 | p.events = POLLIN; |
| 603 | if ((poll(&p, 1, timeout * 1000) < 1) |
| 604 | #if defined(EINTR) |
| 605 | && (errno != EINTR) |
| 606 | #endif |
| 607 | ) |
| 608 | return (0); |
| 609 | #else /* !HAVE_POLL_H */ |
spadix | d29a5c8 | 2009-10-19 14:03:25 +0200 | [diff] [blame] | 610 | #ifndef _WINSOCKAPI_ |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 611 | if (ctxt->fd > FD_SETSIZE) |
| 612 | return 0; |
spadix | d29a5c8 | 2009-10-19 14:03:25 +0200 | [diff] [blame] | 613 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 614 | |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 615 | tv.tv_sec = timeout; |
| 616 | tv.tv_usec = 0; |
| 617 | FD_ZERO(&rfd); |
| 618 | |
Daniel Veillard | 9e2110b | 2005-08-08 20:33:54 +0000 | [diff] [blame] | 619 | #ifdef _MSC_VER |
| 620 | #pragma warning(push) |
| 621 | #pragma warning(disable: 4018) |
| 622 | #endif |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 623 | |
| 624 | FD_SET(ctxt->fd, &rfd); |
| 625 | |
Daniel Veillard | 9e2110b | 2005-08-08 20:33:54 +0000 | [diff] [blame] | 626 | #ifdef _MSC_VER |
| 627 | #pragma warning(pop) |
| 628 | #endif |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 629 | |
| 630 | if ((select(ctxt->fd + 1, &rfd, NULL, NULL, &tv) < 1) |
Daniel Veillard | 50f3437 | 2001-08-03 12:06:36 +0000 | [diff] [blame] | 631 | #if defined(EINTR) |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 632 | && (errno != EINTR) |
Daniel Veillard | 50f3437 | 2001-08-03 12:06:36 +0000 | [diff] [blame] | 633 | #endif |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 634 | ) |
| 635 | return (0); |
| 636 | #endif /* !HAVE_POLL_H */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 637 | } |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 638 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | /** |
| 642 | * xmlNanoHTTPReadLine: |
| 643 | * @ctxt: an HTTP context |
| 644 | * |
| 645 | * Read one line in the HTTP server output, usually for extracting |
| 646 | * the HTTP protocol informations from the answer header. |
| 647 | * |
| 648 | * Returns a newly allocated string with a copy of the line, or NULL |
| 649 | * which indicate the end of the input. |
| 650 | */ |
| 651 | |
| 652 | static char * |
| 653 | xmlNanoHTTPReadLine(xmlNanoHTTPCtxtPtr ctxt) { |
| 654 | char buf[4096]; |
| 655 | char *bp = buf; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 656 | int rc; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 657 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 658 | while (bp - buf < 4095) { |
| 659 | if (ctxt->inrptr == ctxt->inptr) { |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 660 | if ( (rc = xmlNanoHTTPRecv(ctxt)) == 0) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 661 | if (bp == buf) |
| 662 | return(NULL); |
| 663 | else |
| 664 | *bp = 0; |
| 665 | return(xmlMemStrdup(buf)); |
| 666 | } |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 667 | else if ( rc == -1 ) { |
| 668 | return ( NULL ); |
| 669 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 670 | } |
| 671 | *bp = *ctxt->inrptr++; |
| 672 | if (*bp == '\n') { |
| 673 | *bp = 0; |
| 674 | return(xmlMemStrdup(buf)); |
| 675 | } |
| 676 | if (*bp != '\r') |
| 677 | bp++; |
| 678 | } |
| 679 | buf[4095] = 0; |
| 680 | return(xmlMemStrdup(buf)); |
| 681 | } |
| 682 | |
| 683 | |
| 684 | /** |
| 685 | * xmlNanoHTTPScanAnswer: |
| 686 | * @ctxt: an HTTP context |
| 687 | * @line: an HTTP header line |
| 688 | * |
| 689 | * Try to extract useful informations from the server answer. |
| 690 | * We currently parse and process: |
| 691 | * - The HTTP revision/ return code |
Daniel Veillard | a840b69 | 2003-10-19 13:35:37 +0000 | [diff] [blame] | 692 | * - The Content-Type, Mime-Type and charset used |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 693 | * - The Location for redirect processing. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 694 | * |
| 695 | * Returns -1 in case of failure, the file descriptor number otherwise |
| 696 | */ |
| 697 | |
| 698 | static void |
| 699 | xmlNanoHTTPScanAnswer(xmlNanoHTTPCtxtPtr ctxt, const char *line) { |
| 700 | const char *cur = line; |
| 701 | |
| 702 | if (line == NULL) return; |
| 703 | |
| 704 | if (!strncmp(line, "HTTP/", 5)) { |
| 705 | int version = 0; |
| 706 | int ret = 0; |
| 707 | |
| 708 | cur += 5; |
| 709 | while ((*cur >= '0') && (*cur <= '9')) { |
| 710 | version *= 10; |
| 711 | version += *cur - '0'; |
| 712 | cur++; |
| 713 | } |
| 714 | if (*cur == '.') { |
| 715 | cur++; |
| 716 | if ((*cur >= '0') && (*cur <= '9')) { |
| 717 | version *= 10; |
| 718 | version += *cur - '0'; |
| 719 | cur++; |
| 720 | } |
| 721 | while ((*cur >= '0') && (*cur <= '9')) |
| 722 | cur++; |
| 723 | } else |
| 724 | version *= 10; |
| 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')) { |
| 729 | ret *= 10; |
| 730 | ret += *cur - '0'; |
| 731 | cur++; |
| 732 | } |
| 733 | if ((*cur != 0) && (*cur != ' ') && (*cur != '\t')) return; |
| 734 | ctxt->returnValue = ret; |
Daniel Veillard | 13cee4e | 2009-09-05 14:52:55 +0200 | [diff] [blame] | 735 | ctxt->version = version; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 736 | } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Content-Type:", 13)) { |
Daniel Veillard | a840b69 | 2003-10-19 13:35:37 +0000 | [diff] [blame] | 737 | const xmlChar *charset, *last, *mime; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 738 | cur += 13; |
| 739 | while ((*cur == ' ') || (*cur == '\t')) cur++; |
| 740 | if (ctxt->contentType != NULL) |
| 741 | xmlFree(ctxt->contentType); |
| 742 | ctxt->contentType = xmlMemStrdup(cur); |
Daniel Veillard | a840b69 | 2003-10-19 13:35:37 +0000 | [diff] [blame] | 743 | mime = (const xmlChar *) cur; |
| 744 | last = mime; |
| 745 | while ((*last != 0) && (*last != ' ') && (*last != '\t') && |
| 746 | (*last != ';') && (*last != ',')) |
| 747 | 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) { |
| 753 | charset += 8; |
| 754 | last = charset; |
| 755 | while ((*last != 0) && (*last != ' ') && (*last != '\t') && |
| 756 | (*last != ';') && (*last != ',')) |
| 757 | last++; |
| 758 | if (ctxt->encoding != NULL) |
| 759 | xmlFree(ctxt->encoding); |
| 760 | ctxt->encoding = (char *) xmlStrndup(charset, last - charset); |
| 761 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 762 | } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"ContentType:", 12)) { |
Daniel Veillard | a840b69 | 2003-10-19 13:35:37 +0000 | [diff] [blame] | 763 | const xmlChar *charset, *last, *mime; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 764 | cur += 12; |
| 765 | if (ctxt->contentType != NULL) return; |
| 766 | while ((*cur == ' ') || (*cur == '\t')) cur++; |
| 767 | ctxt->contentType = xmlMemStrdup(cur); |
Daniel Veillard | a840b69 | 2003-10-19 13:35:37 +0000 | [diff] [blame] | 768 | mime = (const xmlChar *) cur; |
| 769 | last = mime; |
| 770 | while ((*last != 0) && (*last != ' ') && (*last != '\t') && |
| 771 | (*last != ';') && (*last != ',')) |
| 772 | 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) { |
| 778 | charset += 8; |
| 779 | last = charset; |
| 780 | while ((*last != 0) && (*last != ' ') && (*last != '\t') && |
| 781 | (*last != ';') && (*last != ',')) |
| 782 | last++; |
| 783 | if (ctxt->encoding != NULL) |
| 784 | xmlFree(ctxt->encoding); |
| 785 | ctxt->encoding = (char *) xmlStrndup(charset, last - charset); |
| 786 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 787 | } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Location:", 9)) { |
| 788 | cur += 9; |
| 789 | while ((*cur == ' ') || (*cur == '\t')) cur++; |
| 790 | if (ctxt->location != NULL) |
| 791 | xmlFree(ctxt->location); |
William M. Brack | 7e29c0a | 2004-04-02 09:07:22 +0000 | [diff] [blame] | 792 | if (*cur == '/') { |
| 793 | xmlChar *tmp_http = xmlStrdup(BAD_CAST "http://"); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 794 | xmlChar *tmp_loc = |
William M. Brack | 7e29c0a | 2004-04-02 09:07:22 +0000 | [diff] [blame] | 795 | xmlStrcat(tmp_http, (const xmlChar *) ctxt->hostname); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 796 | ctxt->location = |
William M. Brack | 7e29c0a | 2004-04-02 09:07:22 +0000 | [diff] [blame] | 797 | (char *) xmlStrcat (tmp_loc, (const xmlChar *) cur); |
| 798 | } else { |
| 799 | ctxt->location = xmlMemStrdup(cur); |
| 800 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 801 | } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"WWW-Authenticate:", 17)) { |
| 802 | cur += 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)) { |
| 808 | cur += 19; |
| 809 | while ((*cur == ' ') || (*cur == '\t')) cur++; |
| 810 | if (ctxt->authHeader != NULL) |
| 811 | xmlFree(ctxt->authHeader); |
| 812 | ctxt->authHeader = xmlMemStrdup(cur); |
Daniel Veillard | 9a2724d | 2005-12-15 11:12:26 +0000 | [diff] [blame] | 813 | #ifdef HAVE_ZLIB_H |
| 814 | } else if ( !xmlStrncasecmp( BAD_CAST line, BAD_CAST"Content-Encoding:", 17) ) { |
| 815 | cur += 17; |
| 816 | while ((*cur == ' ') || (*cur == '\t')) cur++; |
| 817 | if ( !xmlStrncasecmp( BAD_CAST cur, BAD_CAST"gzip", 4) ) { |
| 818 | ctxt->usesGzip = 1; |
| 819 | |
| 820 | ctxt->strm = xmlMalloc(sizeof(z_stream)); |
| 821 | |
| 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; |
| 828 | |
| 829 | inflateInit2( ctxt->strm, 31 ); |
| 830 | } |
| 831 | } |
| 832 | #endif |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 833 | } else if ( !xmlStrncasecmp( BAD_CAST line, BAD_CAST"Content-Length:", 15) ) { |
| 834 | cur += 15; |
| 835 | ctxt->ContentLength = strtol( cur, NULL, 10 ); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 836 | } |
| 837 | } |
| 838 | |
| 839 | /** |
| 840 | * xmlNanoHTTPConnectAttempt: |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 841 | * @addr: a socket address structure |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 842 | * |
| 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. |
| 846 | * |
| 847 | * Returns -1 in case of failure, the file descriptor number otherwise |
| 848 | */ |
| 849 | |
Ozkan Sezer | f99d222 | 2010-11-04 12:08:08 +0100 | [diff] [blame] | 850 | static SOCKET |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 851 | xmlNanoHTTPConnectAttempt(struct sockaddr *addr) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 852 | { |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 853 | #ifndef HAVE_POLL_H |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 854 | fd_set wfd; |
Daniel Veillard | 5bb9ccd | 2004-02-09 12:39:02 +0000 | [diff] [blame] | 855 | #ifdef _WINSOCKAPI_ |
| 856 | fd_set xfd; |
| 857 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 858 | struct timeval tv; |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 859 | #else /* !HAVE_POLL_H */ |
| 860 | struct pollfd p; |
| 861 | #endif /* !HAVE_POLL_H */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 862 | int status; |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 863 | |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 864 | int addrlen; |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 865 | |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 866 | SOCKET s; |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 867 | |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 868 | #ifdef SUPPORT_IP6 |
| 869 | if (addr->sa_family == AF_INET6) { |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 870 | s = socket(PF_INET6, SOCK_STREAM, IPPROTO_TCP); |
| 871 | addrlen = sizeof(struct sockaddr_in6); |
| 872 | } else |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 873 | #endif |
| 874 | { |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 875 | s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 876 | addrlen = sizeof(struct sockaddr_in); |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 877 | } |
Ozkan Sezer | f99d222 | 2010-11-04 12:08:08 +0100 | [diff] [blame] | 878 | if (s == INVALID_SOCKET) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 879 | #ifdef DEBUG_HTTP |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 880 | perror("socket"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 881 | #endif |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 882 | __xmlIOErr(XML_FROM_HTTP, 0, "socket failed\n"); |
Ozkan Sezer | f99d222 | 2010-11-04 12:08:08 +0100 | [diff] [blame] | 883 | return INVALID_SOCKET; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 884 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 885 | #ifdef _WINSOCKAPI_ |
| 886 | { |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 887 | u_long one = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 888 | |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 889 | status = ioctlsocket(s, FIONBIO, &one) == SOCKET_ERROR ? -1 : 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 890 | } |
| 891 | #else /* _WINSOCKAPI_ */ |
| 892 | #if defined(VMS) |
| 893 | { |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 894 | int enable = 1; |
| 895 | |
| 896 | status = ioctl(s, FIONBIO, &enable); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 897 | } |
| 898 | #else /* VMS */ |
Daniel Veillard | cba6839 | 2008-08-29 12:43:40 +0000 | [diff] [blame] | 899 | #if defined(__BEOS__) && !defined(__HAIKU__) |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 900 | { |
| 901 | bool noblock = true; |
| 902 | |
| 903 | status = |
| 904 | setsockopt(s, SOL_SOCKET, SO_NONBLOCK, &noblock, |
| 905 | sizeof(noblock)); |
| 906 | } |
Daniel Veillard | 254b126 | 2003-11-01 17:04:58 +0000 | [diff] [blame] | 907 | #else /* __BEOS__ */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 908 | if ((status = fcntl(s, F_GETFL, 0)) != -1) { |
| 909 | #ifdef O_NONBLOCK |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 910 | status |= O_NONBLOCK; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 911 | #else /* O_NONBLOCK */ |
| 912 | #ifdef F_NDELAY |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 913 | status |= F_NDELAY; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 914 | #endif /* F_NDELAY */ |
| 915 | #endif /* !O_NONBLOCK */ |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 916 | status = fcntl(s, F_SETFL, status); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 917 | } |
| 918 | if (status < 0) { |
| 919 | #ifdef DEBUG_HTTP |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 920 | perror("nonblocking"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 921 | #endif |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 922 | __xmlIOErr(XML_FROM_HTTP, 0, "error setting non-blocking IO\n"); |
| 923 | closesocket(s); |
Ozkan Sezer | f99d222 | 2010-11-04 12:08:08 +0100 | [diff] [blame] | 924 | return INVALID_SOCKET; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 925 | } |
Daniel Veillard | 254b126 | 2003-11-01 17:04:58 +0000 | [diff] [blame] | 926 | #endif /* !__BEOS__ */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 927 | #endif /* !VMS */ |
| 928 | #endif /* !_WINSOCKAPI_ */ |
| 929 | |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 930 | if (connect(s, addr, addrlen) == -1) { |
| 931 | switch (socket_errno()) { |
| 932 | case EINPROGRESS: |
| 933 | case EWOULDBLOCK: |
| 934 | break; |
| 935 | default: |
| 936 | __xmlIOErr(XML_FROM_HTTP, 0, |
| 937 | "error connecting to HTTP server"); |
| 938 | closesocket(s); |
Ozkan Sezer | f99d222 | 2010-11-04 12:08:08 +0100 | [diff] [blame] | 939 | return INVALID_SOCKET; |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 940 | } |
| 941 | } |
| 942 | #ifndef HAVE_POLL_H |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 943 | tv.tv_sec = timeout; |
| 944 | tv.tv_usec = 0; |
Daniel Veillard | 9e2110b | 2005-08-08 20:33:54 +0000 | [diff] [blame] | 945 | |
| 946 | #ifdef _MSC_VER |
| 947 | #pragma warning(push) |
| 948 | #pragma warning(disable: 4018) |
| 949 | #endif |
spadix | d29a5c8 | 2009-10-19 14:03:25 +0200 | [diff] [blame] | 950 | #ifndef _WINSOCKAPI_ |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 951 | if (s > FD_SETSIZE) |
Ozkan Sezer | f99d222 | 2010-11-04 12:08:08 +0100 | [diff] [blame] | 952 | return INVALID_SOCKET; |
spadix | d29a5c8 | 2009-10-19 14:03:25 +0200 | [diff] [blame] | 953 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 954 | FD_ZERO(&wfd); |
| 955 | FD_SET(s, &wfd); |
Daniel Veillard | 5bb9ccd | 2004-02-09 12:39:02 +0000 | [diff] [blame] | 956 | |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 957 | #ifdef _WINSOCKAPI_ |
Daniel Veillard | 5bb9ccd | 2004-02-09 12:39:02 +0000 | [diff] [blame] | 958 | FD_ZERO(&xfd); |
| 959 | FD_SET(s, &xfd); |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 960 | |
| 961 | switch (select(s + 1, NULL, &wfd, &xfd, &tv)) |
Daniel Veillard | 5bb9ccd | 2004-02-09 12:39:02 +0000 | [diff] [blame] | 962 | #else |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 963 | switch (select(s + 1, NULL, &wfd, NULL, &tv)) |
Daniel Veillard | 5bb9ccd | 2004-02-09 12:39:02 +0000 | [diff] [blame] | 964 | #endif |
Daniel Veillard | 9e2110b | 2005-08-08 20:33:54 +0000 | [diff] [blame] | 965 | #ifdef _MSC_VER |
| 966 | #pragma warning(pop) |
| 967 | #endif |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 968 | |
| 969 | #else /* !HAVE_POLL_H */ |
| 970 | p.fd = s; |
| 971 | p.events = POLLOUT; |
| 972 | switch (poll(&p, 1, timeout * 1000)) |
| 973 | #endif /* !HAVE_POLL_H */ |
| 974 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 975 | { |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 976 | case 0: |
| 977 | /* Time out */ |
| 978 | __xmlIOErr(XML_FROM_HTTP, 0, "Connect attempt timed out"); |
| 979 | closesocket(s); |
Ozkan Sezer | f99d222 | 2010-11-04 12:08:08 +0100 | [diff] [blame] | 980 | return INVALID_SOCKET; |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 981 | case -1: |
| 982 | /* Ermm.. ?? */ |
| 983 | __xmlIOErr(XML_FROM_HTTP, 0, "Connect failed"); |
| 984 | closesocket(s); |
Ozkan Sezer | f99d222 | 2010-11-04 12:08:08 +0100 | [diff] [blame] | 985 | return INVALID_SOCKET; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 986 | } |
| 987 | |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 988 | #ifndef HAVE_POLL_H |
| 989 | if (FD_ISSET(s, &wfd) |
Daniel Veillard | 5bb9ccd | 2004-02-09 12:39:02 +0000 | [diff] [blame] | 990 | #ifdef _WINSOCKAPI_ |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 991 | || FD_ISSET(s, &xfd) |
Daniel Veillard | 5bb9ccd | 2004-02-09 12:39:02 +0000 | [diff] [blame] | 992 | #endif |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 993 | ) |
| 994 | #else /* !HAVE_POLL_H */ |
| 995 | if (p.revents == POLLOUT) |
| 996 | #endif /* !HAVE_POLL_H */ |
| 997 | { |
| 998 | XML_SOCKLEN_T len; |
| 999 | |
| 1000 | len = sizeof(status); |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1001 | #ifdef SO_ERROR |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 1002 | if (getsockopt(s, SOL_SOCKET, SO_ERROR, (char *) &status, &len) < |
| 1003 | 0) { |
| 1004 | /* Solaris error code */ |
| 1005 | __xmlIOErr(XML_FROM_HTTP, 0, "getsockopt failed\n"); |
Denis Pauk | 283c83e | 2013-08-06 09:49:42 +0300 | [diff] [blame] | 1006 | closesocket(s); |
Ozkan Sezer | f99d222 | 2010-11-04 12:08:08 +0100 | [diff] [blame] | 1007 | return INVALID_SOCKET; |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 1008 | } |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1009 | #endif |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 1010 | if (status) { |
| 1011 | __xmlIOErr(XML_FROM_HTTP, 0, |
| 1012 | "Error connecting to remote host"); |
| 1013 | closesocket(s); |
| 1014 | errno = status; |
Ozkan Sezer | f99d222 | 2010-11-04 12:08:08 +0100 | [diff] [blame] | 1015 | return INVALID_SOCKET; |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 1016 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1017 | } else { |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 1018 | /* pbm */ |
| 1019 | __xmlIOErr(XML_FROM_HTTP, 0, "select failed\n"); |
| 1020 | closesocket(s); |
Ozkan Sezer | f99d222 | 2010-11-04 12:08:08 +0100 | [diff] [blame] | 1021 | return INVALID_SOCKET; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1022 | } |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 1023 | |
| 1024 | return (s); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1025 | } |
Raphael Prevost | 48b60c3 | 2009-08-23 13:11:01 +0200 | [diff] [blame] | 1026 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1027 | /** |
| 1028 | * xmlNanoHTTPConnectHost: |
| 1029 | * @host: the host name |
| 1030 | * @port: the port number |
| 1031 | * |
| 1032 | * Attempt a connection to the given host:port endpoint. It tries |
| 1033 | * the multiple IP provided by the DNS if available. |
| 1034 | * |
| 1035 | * Returns -1 in case of failure, the file descriptor number otherwise |
| 1036 | */ |
| 1037 | |
Ozkan Sezer | f99d222 | 2010-11-04 12:08:08 +0100 | [diff] [blame] | 1038 | static SOCKET |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1039 | xmlNanoHTTPConnectHost(const char *host, int port) |
| 1040 | { |
| 1041 | struct hostent *h; |
Daniel Veillard | 2db8c12 | 2003-07-08 12:16:59 +0000 | [diff] [blame] | 1042 | struct sockaddr *addr = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1043 | struct in_addr ia; |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1044 | struct sockaddr_in sockin; |
Daniel Veillard | 5c39654 | 2002-03-15 07:57:50 +0000 | [diff] [blame] | 1045 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1046 | #ifdef SUPPORT_IP6 |
| 1047 | struct in6_addr ia6; |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1048 | struct sockaddr_in6 sockin6; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1049 | #endif |
| 1050 | int i; |
Ozkan Sezer | f99d222 | 2010-11-04 12:08:08 +0100 | [diff] [blame] | 1051 | SOCKET s; |
Daniel Veillard | 5c39654 | 2002-03-15 07:57:50 +0000 | [diff] [blame] | 1052 | |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1053 | memset (&sockin, 0, sizeof(sockin)); |
| 1054 | #ifdef SUPPORT_IP6 |
| 1055 | memset (&sockin6, 0, sizeof(sockin6)); |
Rob Richards | cb418de | 2005-10-13 23:12:42 +0000 | [diff] [blame] | 1056 | #endif |
| 1057 | |
| 1058 | #if !defined(HAVE_GETADDRINFO) && defined(SUPPORT_IP6) && defined(RES_USE_INET6) |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1059 | if (have_ipv6 ()) |
Daniel Veillard | 560c2a4 | 2003-07-06 21:13:49 +0000 | [diff] [blame] | 1060 | { |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1061 | if (!(_res.options & RES_INIT)) |
| 1062 | res_init(); |
| 1063 | _res.options |= RES_USE_INET6; |
| 1064 | } |
Rob Richards | cb418de | 2005-10-13 23:12:42 +0000 | [diff] [blame] | 1065 | #endif |
| 1066 | |
| 1067 | #if defined(HAVE_GETADDRINFO) && defined(SUPPORT_IP6) && !defined(_WIN32) |
| 1068 | if (have_ipv6 ()) |
| 1069 | #endif |
| 1070 | #if defined(HAVE_GETADDRINFO) && (defined(SUPPORT_IP6) || defined(_WIN32)) |
Daniel Veillard | 560c2a4 | 2003-07-06 21:13:49 +0000 | [diff] [blame] | 1071 | { |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1072 | int status; |
| 1073 | struct addrinfo hints, *res, *result; |
| 1074 | |
| 1075 | result = NULL; |
| 1076 | memset (&hints, 0,sizeof(hints)); |
| 1077 | hints.ai_socktype = SOCK_STREAM; |
| 1078 | |
| 1079 | status = getaddrinfo (host, NULL, &hints, &result); |
| 1080 | if (status) { |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 1081 | __xmlIOErr(XML_FROM_HTTP, 0, "getaddrinfo failed\n"); |
Ozkan Sezer | f99d222 | 2010-11-04 12:08:08 +0100 | [diff] [blame] | 1082 | return INVALID_SOCKET; |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1083 | } |
| 1084 | |
| 1085 | for (res = result; res; res = res->ai_next) { |
Rob Richards | cb418de | 2005-10-13 23:12:42 +0000 | [diff] [blame] | 1086 | if (res->ai_family == AF_INET) { |
| 1087 | if (res->ai_addrlen > sizeof(sockin)) { |
| 1088 | __xmlIOErr(XML_FROM_HTTP, 0, "address size mismatch\n"); |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1089 | freeaddrinfo (result); |
Ozkan Sezer | f99d222 | 2010-11-04 12:08:08 +0100 | [diff] [blame] | 1090 | return INVALID_SOCKET; |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1091 | } |
Rob Richards | cb418de | 2005-10-13 23:12:42 +0000 | [diff] [blame] | 1092 | memcpy (&sockin, res->ai_addr, res->ai_addrlen); |
| 1093 | sockin.sin_port = htons (port); |
| 1094 | addr = (struct sockaddr *)&sockin; |
| 1095 | #ifdef SUPPORT_IP6 |
| 1096 | } else if (have_ipv6 () && (res->ai_family == AF_INET6)) { |
| 1097 | if (res->ai_addrlen > sizeof(sockin6)) { |
| 1098 | __xmlIOErr(XML_FROM_HTTP, 0, "address size mismatch\n"); |
| 1099 | freeaddrinfo (result); |
Ozkan Sezer | f99d222 | 2010-11-04 12:08:08 +0100 | [diff] [blame] | 1100 | return INVALID_SOCKET; |
Rob Richards | cb418de | 2005-10-13 23:12:42 +0000 | [diff] [blame] | 1101 | } |
| 1102 | memcpy (&sockin6, res->ai_addr, res->ai_addrlen); |
| 1103 | sockin6.sin6_port = htons (port); |
| 1104 | addr = (struct sockaddr *)&sockin6; |
| 1105 | #endif |
| 1106 | } else |
| 1107 | continue; /* for */ |
| 1108 | |
| 1109 | s = xmlNanoHTTPConnectAttempt (addr); |
Ozkan Sezer | f99d222 | 2010-11-04 12:08:08 +0100 | [diff] [blame] | 1110 | if (s != INVALID_SOCKET) { |
Rob Richards | cb418de | 2005-10-13 23:12:42 +0000 | [diff] [blame] | 1111 | freeaddrinfo (result); |
| 1112 | return (s); |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1113 | } |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1114 | } |
Rob Richards | cb418de | 2005-10-13 23:12:42 +0000 | [diff] [blame] | 1115 | |
Daniel Veillard | 3dc93a4 | 2003-07-10 14:04:33 +0000 | [diff] [blame] | 1116 | if (result) |
| 1117 | freeaddrinfo (result); |
Rob Richards | cb418de | 2005-10-13 23:12:42 +0000 | [diff] [blame] | 1118 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1119 | #endif |
Rob Richards | cb418de | 2005-10-13 23:12:42 +0000 | [diff] [blame] | 1120 | #if defined(HAVE_GETADDRINFO) && defined(SUPPORT_IP6) && !defined(_WIN32) |
| 1121 | else |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1122 | #endif |
Rob Richards | cb418de | 2005-10-13 23:12:42 +0000 | [diff] [blame] | 1123 | #if !defined(HAVE_GETADDRINFO) || !defined(_WIN32) |
| 1124 | { |
Patrick Monnerat | 437f4f5 | 2013-12-12 15:23:09 +0800 | [diff] [blame] | 1125 | h = gethostbyname (GETHOSTBYNAME_ARG_CAST host); |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1126 | if (h == NULL) { |
Daniel Veillard | 56b2db7 | 2002-03-25 16:35:28 +0000 | [diff] [blame] | 1127 | |
| 1128 | /* |
| 1129 | * Okay, I got fed up by the non-portability of this error message |
| 1130 | * extraction code. it work on Linux, if it work on your platform |
| 1131 | * and one want to enable it, send me the defined(foobar) needed |
| 1132 | */ |
| 1133 | #if defined(HAVE_NETDB_H) && defined(HOST_NOT_FOUND) && defined(linux) |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1134 | const char *h_err_txt = ""; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1135 | |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1136 | switch (h_errno) { |
| 1137 | case HOST_NOT_FOUND: |
| 1138 | h_err_txt = "Authoritive host not found"; |
| 1139 | break; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1140 | |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1141 | case TRY_AGAIN: |
| 1142 | h_err_txt = |
| 1143 | "Non-authoritive host not found or server failure."; |
| 1144 | break; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1145 | |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1146 | case NO_RECOVERY: |
| 1147 | h_err_txt = |
| 1148 | "Non-recoverable errors: FORMERR, REFUSED, or NOTIMP."; |
| 1149 | break; |
Daniel Veillard | 5c39654 | 2002-03-15 07:57:50 +0000 | [diff] [blame] | 1150 | |
Daniel Veillard | d95b689 | 2012-04-02 17:48:53 +0800 | [diff] [blame] | 1151 | #ifdef NO_ADDRESS |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1152 | case NO_ADDRESS: |
| 1153 | h_err_txt = |
| 1154 | "Valid name, no data record of requested type."; |
| 1155 | break; |
Daniel Veillard | d95b689 | 2012-04-02 17:48:53 +0800 | [diff] [blame] | 1156 | #endif |
Daniel Veillard | 5c39654 | 2002-03-15 07:57:50 +0000 | [diff] [blame] | 1157 | |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1158 | default: |
| 1159 | h_err_txt = "No error text defined."; |
| 1160 | break; |
| 1161 | } |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 1162 | __xmlIOErr(XML_FROM_HTTP, 0, h_err_txt); |
Daniel Veillard | 5c39654 | 2002-03-15 07:57:50 +0000 | [diff] [blame] | 1163 | #else |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 1164 | __xmlIOErr(XML_FROM_HTTP, 0, "Failed to resolve host"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1165 | #endif |
Ozkan Sezer | f99d222 | 2010-11-04 12:08:08 +0100 | [diff] [blame] | 1166 | return INVALID_SOCKET; |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1167 | } |
Daniel Veillard | 5c39654 | 2002-03-15 07:57:50 +0000 | [diff] [blame] | 1168 | |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1169 | for (i = 0; h->h_addr_list[i]; i++) { |
| 1170 | if (h->h_addrtype == AF_INET) { |
| 1171 | /* A records (IPv4) */ |
Daniel Veillard | 8e2c979 | 2004-10-27 09:39:50 +0000 | [diff] [blame] | 1172 | if ((unsigned int) h->h_length > sizeof(ia)) { |
| 1173 | __xmlIOErr(XML_FROM_HTTP, 0, "address size mismatch\n"); |
Ozkan Sezer | f99d222 | 2010-11-04 12:08:08 +0100 | [diff] [blame] | 1174 | return INVALID_SOCKET; |
Daniel Veillard | 8e2c979 | 2004-10-27 09:39:50 +0000 | [diff] [blame] | 1175 | } |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1176 | memcpy (&ia, h->h_addr_list[i], h->h_length); |
| 1177 | sockin.sin_family = h->h_addrtype; |
| 1178 | sockin.sin_addr = ia; |
Daniel Veillard | ac17e59 | 2012-04-02 15:45:13 +0800 | [diff] [blame] | 1179 | sockin.sin_port = (unsigned short)htons ((unsigned short)port); |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1180 | addr = (struct sockaddr *) &sockin; |
Daniel Veillard | 5c39654 | 2002-03-15 07:57:50 +0000 | [diff] [blame] | 1181 | #ifdef SUPPORT_IP6 |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1182 | } else if (have_ipv6 () && (h->h_addrtype == AF_INET6)) { |
| 1183 | /* AAAA records (IPv6) */ |
Daniel Veillard | 8e2c979 | 2004-10-27 09:39:50 +0000 | [diff] [blame] | 1184 | if ((unsigned int) h->h_length > sizeof(ia6)) { |
| 1185 | __xmlIOErr(XML_FROM_HTTP, 0, "address size mismatch\n"); |
Ozkan Sezer | f99d222 | 2010-11-04 12:08:08 +0100 | [diff] [blame] | 1186 | return INVALID_SOCKET; |
Daniel Veillard | 8e2c979 | 2004-10-27 09:39:50 +0000 | [diff] [blame] | 1187 | } |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1188 | memcpy (&ia6, h->h_addr_list[i], h->h_length); |
| 1189 | sockin6.sin6_family = h->h_addrtype; |
| 1190 | sockin6.sin6_addr = ia6; |
| 1191 | sockin6.sin6_port = htons (port); |
| 1192 | addr = (struct sockaddr *) &sockin6; |
Daniel Veillard | 5c39654 | 2002-03-15 07:57:50 +0000 | [diff] [blame] | 1193 | #endif |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1194 | } else |
| 1195 | break; /* for */ |
Daniel Veillard | 5c39654 | 2002-03-15 07:57:50 +0000 | [diff] [blame] | 1196 | |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1197 | s = xmlNanoHTTPConnectAttempt (addr); |
Ozkan Sezer | f99d222 | 2010-11-04 12:08:08 +0100 | [diff] [blame] | 1198 | if (s != INVALID_SOCKET) |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1199 | return (s); |
| 1200 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1201 | } |
Rob Richards | cb418de | 2005-10-13 23:12:42 +0000 | [diff] [blame] | 1202 | #endif |
| 1203 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1204 | #ifdef DEBUG_HTTP |
| 1205 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 5c39654 | 2002-03-15 07:57:50 +0000 | [diff] [blame] | 1206 | "xmlNanoHTTPConnectHost: unable to connect to '%s'.\n", |
| 1207 | host); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1208 | #endif |
Ozkan Sezer | f99d222 | 2010-11-04 12:08:08 +0100 | [diff] [blame] | 1209 | return INVALID_SOCKET; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1210 | } |
| 1211 | |
| 1212 | |
| 1213 | /** |
| 1214 | * xmlNanoHTTPOpen: |
| 1215 | * @URL: The URL to load |
| 1216 | * @contentType: if available the Content-Type information will be |
| 1217 | * returned at that location |
| 1218 | * |
| 1219 | * This function try to open a connection to the indicated resource |
| 1220 | * via HTTP GET. |
| 1221 | * |
| 1222 | * Returns NULL in case of failure, otherwise a request handler. |
| 1223 | * The contentType, if provided must be freed by the caller |
| 1224 | */ |
| 1225 | |
| 1226 | void* |
| 1227 | xmlNanoHTTPOpen(const char *URL, char **contentType) { |
| 1228 | if (contentType != NULL) *contentType = NULL; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1229 | return(xmlNanoHTTPMethod(URL, NULL, NULL, contentType, NULL, 0)); |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 1230 | } |
| 1231 | |
| 1232 | /** |
| 1233 | * xmlNanoHTTPOpenRedir: |
| 1234 | * @URL: The URL to load |
| 1235 | * @contentType: if available the Content-Type information will be |
| 1236 | * returned at that location |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1237 | * @redir: if available the redirected URL will be returned |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 1238 | * |
| 1239 | * This function try to open a connection to the indicated resource |
| 1240 | * via HTTP GET. |
| 1241 | * |
| 1242 | * Returns NULL in case of failure, otherwise a request handler. |
| 1243 | * The contentType, if provided must be freed by the caller |
| 1244 | */ |
| 1245 | |
| 1246 | void* |
| 1247 | xmlNanoHTTPOpenRedir(const char *URL, char **contentType, char **redir) { |
| 1248 | if (contentType != NULL) *contentType = NULL; |
| 1249 | if (redir != NULL) *redir = NULL; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1250 | return(xmlNanoHTTPMethodRedir(URL, NULL, NULL, contentType, redir, NULL,0)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1251 | } |
| 1252 | |
| 1253 | /** |
| 1254 | * xmlNanoHTTPRead: |
| 1255 | * @ctx: the HTTP context |
| 1256 | * @dest: a buffer |
| 1257 | * @len: the buffer length |
| 1258 | * |
| 1259 | * This function tries to read @len bytes from the existing HTTP connection |
| 1260 | * and saves them in @dest. This is a blocking call. |
| 1261 | * |
| 1262 | * Returns the number of byte read. 0 is an indication of an end of connection. |
| 1263 | * -1 indicates a parameter error. |
| 1264 | */ |
| 1265 | int |
| 1266 | xmlNanoHTTPRead(void *ctx, void *dest, int len) { |
| 1267 | xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx; |
Daniel Veillard | 9a2724d | 2005-12-15 11:12:26 +0000 | [diff] [blame] | 1268 | #ifdef HAVE_ZLIB_H |
| 1269 | int bytes_read = 0; |
| 1270 | int orig_avail_in; |
| 1271 | int z_ret; |
| 1272 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1273 | |
| 1274 | if (ctx == NULL) return(-1); |
| 1275 | if (dest == NULL) return(-1); |
| 1276 | if (len <= 0) return(0); |
| 1277 | |
Daniel Veillard | 9a2724d | 2005-12-15 11:12:26 +0000 | [diff] [blame] | 1278 | #ifdef HAVE_ZLIB_H |
| 1279 | if (ctxt->usesGzip == 1) { |
| 1280 | if (ctxt->strm == NULL) return(0); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 1281 | |
Daniel Veillard | 9a2724d | 2005-12-15 11:12:26 +0000 | [diff] [blame] | 1282 | ctxt->strm->next_out = dest; |
| 1283 | ctxt->strm->avail_out = len; |
William M. Brack | e882765 | 2007-05-16 05:19:13 +0000 | [diff] [blame] | 1284 | ctxt->strm->avail_in = ctxt->inptr - ctxt->inrptr; |
Daniel Veillard | 9a2724d | 2005-12-15 11:12:26 +0000 | [diff] [blame] | 1285 | |
William M. Brack | e882765 | 2007-05-16 05:19:13 +0000 | [diff] [blame] | 1286 | while (ctxt->strm->avail_out > 0 && |
| 1287 | (ctxt->strm->avail_in > 0 || xmlNanoHTTPRecv(ctxt) > 0)) { |
William M. Brack | d2f682a | 2007-05-15 19:42:08 +0000 | [diff] [blame] | 1288 | orig_avail_in = ctxt->strm->avail_in = |
| 1289 | ctxt->inptr - ctxt->inrptr - bytes_read; |
Daniel Veillard | 9a2724d | 2005-12-15 11:12:26 +0000 | [diff] [blame] | 1290 | ctxt->strm->next_in = BAD_CAST (ctxt->inrptr + bytes_read); |
| 1291 | |
| 1292 | z_ret = inflate(ctxt->strm, Z_NO_FLUSH); |
| 1293 | bytes_read += orig_avail_in - ctxt->strm->avail_in; |
| 1294 | |
| 1295 | if (z_ret != Z_OK) break; |
William M. Brack | d2f682a | 2007-05-15 19:42:08 +0000 | [diff] [blame] | 1296 | } |
Daniel Veillard | 9a2724d | 2005-12-15 11:12:26 +0000 | [diff] [blame] | 1297 | |
| 1298 | ctxt->inrptr += bytes_read; |
| 1299 | return(len - ctxt->strm->avail_out); |
| 1300 | } |
| 1301 | #endif |
| 1302 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1303 | while (ctxt->inptr - ctxt->inrptr < len) { |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1304 | if (xmlNanoHTTPRecv(ctxt) <= 0) break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1305 | } |
| 1306 | if (ctxt->inptr - ctxt->inrptr < len) |
| 1307 | len = ctxt->inptr - ctxt->inrptr; |
| 1308 | memcpy(dest, ctxt->inrptr, len); |
| 1309 | ctxt->inrptr += len; |
| 1310 | return(len); |
| 1311 | } |
| 1312 | |
| 1313 | /** |
| 1314 | * xmlNanoHTTPClose: |
| 1315 | * @ctx: the HTTP context |
| 1316 | * |
| 1317 | * This function closes an HTTP context, it ends up the connection and |
| 1318 | * free all data related to it. |
| 1319 | */ |
| 1320 | void |
| 1321 | xmlNanoHTTPClose(void *ctx) { |
| 1322 | xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx; |
| 1323 | |
| 1324 | if (ctx == NULL) return; |
| 1325 | |
| 1326 | xmlNanoHTTPFreeCtxt(ctxt); |
| 1327 | } |
| 1328 | |
| 1329 | /** |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 1330 | * xmlNanoHTTPMethodRedir: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1331 | * @URL: The URL to load |
| 1332 | * @method: the HTTP method to use |
| 1333 | * @input: the input string if any |
| 1334 | * @contentType: the Content-Type information IN and OUT |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 1335 | * @redir: the redirected URL OUT |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1336 | * @headers: the extra headers |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1337 | * @ilen: input length |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1338 | * |
| 1339 | * This function try to open a connection to the indicated resource |
| 1340 | * via HTTP using the given @method, adding the given extra headers |
| 1341 | * and the input buffer for the request content. |
| 1342 | * |
| 1343 | * Returns NULL in case of failure, otherwise a request handler. |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 1344 | * The contentType, or redir, if provided must be freed by the caller |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1345 | */ |
| 1346 | |
| 1347 | void* |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 1348 | xmlNanoHTTPMethodRedir(const char *URL, const char *method, const char *input, |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1349 | char **contentType, char **redir, |
| 1350 | const char *headers, int ilen ) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1351 | xmlNanoHTTPCtxtPtr ctxt; |
| 1352 | char *bp, *p; |
Ozkan Sezer | f99d222 | 2010-11-04 12:08:08 +0100 | [diff] [blame] | 1353 | int blen; |
| 1354 | SOCKET ret; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1355 | int nbRedirects = 0; |
| 1356 | char *redirURL = NULL; |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 1357 | #ifdef DEBUG_HTTP |
| 1358 | int xmt_bytes; |
| 1359 | #endif |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 1360 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1361 | if (URL == NULL) return(NULL); |
| 1362 | if (method == NULL) method = "GET"; |
| 1363 | xmlNanoHTTPInit(); |
| 1364 | |
| 1365 | retry: |
Gaurav Gupta | 1811add | 2014-07-14 17:50:27 +0800 | [diff] [blame] | 1366 | if (redirURL == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1367 | ctxt = xmlNanoHTTPNewCtxt(URL); |
Gaurav Gupta | 1811add | 2014-07-14 17:50:27 +0800 | [diff] [blame] | 1368 | if (ctxt == NULL) |
| 1369 | return(NULL); |
| 1370 | } else { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1371 | ctxt = xmlNanoHTTPNewCtxt(redirURL); |
Gaurav Gupta | 1811add | 2014-07-14 17:50:27 +0800 | [diff] [blame] | 1372 | if (ctxt == NULL) |
| 1373 | return(NULL); |
Daniel Veillard | a840b69 | 2003-10-19 13:35:37 +0000 | [diff] [blame] | 1374 | ctxt->location = xmlMemStrdup(redirURL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1375 | } |
| 1376 | |
| 1377 | if ((ctxt->protocol == NULL) || (strcmp(ctxt->protocol, "http"))) { |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 1378 | __xmlIOErr(XML_FROM_HTTP, XML_HTTP_URL_SYNTAX, "Not a valid HTTP URI"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1379 | xmlNanoHTTPFreeCtxt(ctxt); |
| 1380 | if (redirURL != NULL) xmlFree(redirURL); |
| 1381 | return(NULL); |
| 1382 | } |
| 1383 | if (ctxt->hostname == NULL) { |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 1384 | __xmlIOErr(XML_FROM_HTTP, XML_HTTP_UNKNOWN_HOST, |
| 1385 | "Failed to identify host in URI"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1386 | xmlNanoHTTPFreeCtxt(ctxt); |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 1387 | if (redirURL != NULL) xmlFree(redirURL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1388 | return(NULL); |
| 1389 | } |
| 1390 | if (proxy) { |
| 1391 | blen = strlen(ctxt->hostname) * 2 + 16; |
| 1392 | ret = xmlNanoHTTPConnectHost(proxy, proxyPort); |
| 1393 | } |
| 1394 | else { |
| 1395 | blen = strlen(ctxt->hostname); |
| 1396 | ret = xmlNanoHTTPConnectHost(ctxt->hostname, ctxt->port); |
| 1397 | } |
Ozkan Sezer | f99d222 | 2010-11-04 12:08:08 +0100 | [diff] [blame] | 1398 | if (ret == INVALID_SOCKET) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1399 | xmlNanoHTTPFreeCtxt(ctxt); |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 1400 | if (redirURL != NULL) xmlFree(redirURL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1401 | return(NULL); |
| 1402 | } |
| 1403 | ctxt->fd = ret; |
| 1404 | |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1405 | if (input == NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1406 | ilen = 0; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1407 | else |
| 1408 | blen += 36; |
| 1409 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1410 | if (headers != NULL) |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1411 | blen += strlen(headers) + 2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1412 | if (contentType && *contentType) |
William M. Brack | ead3583 | 2008-02-06 04:12:46 +0000 | [diff] [blame] | 1413 | /* reserve for string plus 'Content-Type: \r\n" */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1414 | blen += strlen(*contentType) + 16; |
Daniel Veillard | 351f2d6 | 2005-04-13 02:55:12 +0000 | [diff] [blame] | 1415 | if (ctxt->query != NULL) |
William M. Brack | ead3583 | 2008-02-06 04:12:46 +0000 | [diff] [blame] | 1416 | /* 1 for '?' */ |
Daniel Veillard | 351f2d6 | 2005-04-13 02:55:12 +0000 | [diff] [blame] | 1417 | blen += strlen(ctxt->query) + 1; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1418 | blen += strlen(method) + strlen(ctxt->path) + 24; |
Daniel Veillard | 9a2724d | 2005-12-15 11:12:26 +0000 | [diff] [blame] | 1419 | #ifdef HAVE_ZLIB_H |
William M. Brack | ead3583 | 2008-02-06 04:12:46 +0000 | [diff] [blame] | 1420 | /* reserve for possible 'Accept-Encoding: gzip' string */ |
Daniel Veillard | 9a2724d | 2005-12-15 11:12:26 +0000 | [diff] [blame] | 1421 | blen += 23; |
| 1422 | #endif |
William M. Brack | ead3583 | 2008-02-06 04:12:46 +0000 | [diff] [blame] | 1423 | if (ctxt->port != 80) { |
| 1424 | /* reserve space for ':xxxxx', incl. potential proxy */ |
| 1425 | if (proxy) |
| 1426 | blen += 12; |
| 1427 | else |
| 1428 | blen += 6; |
| 1429 | } |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 1430 | bp = (char*)xmlMallocAtomic(blen); |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1431 | if ( bp == NULL ) { |
| 1432 | xmlNanoHTTPFreeCtxt( ctxt ); |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 1433 | xmlHTTPErrMemory("allocating header buffer"); |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1434 | return ( NULL ); |
| 1435 | } |
| 1436 | |
| 1437 | p = bp; |
| 1438 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1439 | if (proxy) { |
| 1440 | if (ctxt->port != 80) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 1441 | p += snprintf( p, blen - (p - bp), "%s http://%s:%d%s", |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 1442 | method, ctxt->hostname, |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 1443 | ctxt->port, ctxt->path ); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1444 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 1445 | else |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 1446 | p += snprintf( p, blen - (p - bp), "%s http://%s%s", method, |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 1447 | ctxt->hostname, ctxt->path); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1448 | } |
| 1449 | else |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 1450 | p += snprintf( p, blen - (p - bp), "%s %s", method, ctxt->path); |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1451 | |
Daniel Veillard | 351f2d6 | 2005-04-13 02:55:12 +0000 | [diff] [blame] | 1452 | if (ctxt->query != NULL) |
| 1453 | p += snprintf( p, blen - (p - bp), "?%s", ctxt->query); |
| 1454 | |
William M. Brack | ec72008 | 2007-08-24 02:57:38 +0000 | [diff] [blame] | 1455 | if (ctxt->port == 80) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 1456 | p += snprintf( p, blen - (p - bp), " HTTP/1.0\r\nHost: %s\r\n", |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 1457 | ctxt->hostname); |
William M. Brack | ec72008 | 2007-08-24 02:57:38 +0000 | [diff] [blame] | 1458 | } else { |
| 1459 | p += snprintf( p, blen - (p - bp), " HTTP/1.0\r\nHost: %s:%d\r\n", |
| 1460 | ctxt->hostname, ctxt->port); |
| 1461 | } |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1462 | |
Daniel Veillard | 9a2724d | 2005-12-15 11:12:26 +0000 | [diff] [blame] | 1463 | #ifdef HAVE_ZLIB_H |
| 1464 | p += snprintf(p, blen - (p - bp), "Accept-Encoding: gzip\r\n"); |
| 1465 | #endif |
| 1466 | |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 1467 | if (contentType != NULL && *contentType) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 1468 | p += snprintf(p, blen - (p - bp), "Content-Type: %s\r\n", *contentType); |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1469 | |
| 1470 | if (headers != NULL) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 1471 | p += snprintf( p, blen - (p - bp), "%s", headers ); |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1472 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1473 | if (input != NULL) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 1474 | snprintf(p, blen - (p - bp), "Content-Length: %d\r\n\r\n", ilen ); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1475 | else |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 1476 | snprintf(p, blen - (p - bp), "\r\n"); |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1477 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1478 | #ifdef DEBUG_HTTP |
| 1479 | xmlGenericError(xmlGenericErrorContext, |
| 1480 | "-> %s%s", proxy? "(Proxy) " : "", bp); |
| 1481 | if ((blen -= strlen(bp)+1) < 0) |
| 1482 | xmlGenericError(xmlGenericErrorContext, |
| 1483 | "ERROR: overflowed buffer by %d bytes\n", -blen); |
| 1484 | #endif |
| 1485 | ctxt->outptr = ctxt->out = bp; |
| 1486 | ctxt->state = XML_NANO_HTTP_WRITE; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1487 | blen = strlen( ctxt->out ); |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1488 | #ifdef DEBUG_HTTP |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 1489 | xmt_bytes = xmlNanoHTTPSend(ctxt, ctxt->out, blen ); |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1490 | if ( xmt_bytes != blen ) |
| 1491 | xmlGenericError( xmlGenericErrorContext, |
| 1492 | "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n", |
| 1493 | xmt_bytes, blen, |
| 1494 | "bytes of HTTP headers sent to host", |
| 1495 | ctxt->hostname ); |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 1496 | #else |
| 1497 | xmlNanoHTTPSend(ctxt, ctxt->out, blen ); |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1498 | #endif |
| 1499 | |
| 1500 | if ( input != NULL ) { |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 1501 | #ifdef DEBUG_HTTP |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1502 | xmt_bytes = xmlNanoHTTPSend( ctxt, input, ilen ); |
| 1503 | |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1504 | if ( xmt_bytes != ilen ) |
| 1505 | xmlGenericError( xmlGenericErrorContext, |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 1506 | "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n", |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1507 | xmt_bytes, ilen, |
| 1508 | "bytes of HTTP content sent to host", |
| 1509 | ctxt->hostname ); |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 1510 | #else |
| 1511 | xmlNanoHTTPSend( ctxt, input, ilen ); |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1512 | #endif |
| 1513 | } |
| 1514 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1515 | ctxt->state = XML_NANO_HTTP_READ; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1516 | |
| 1517 | while ((p = xmlNanoHTTPReadLine(ctxt)) != NULL) { |
Daniel Veillard | 594e5df | 2009-09-07 14:58:47 +0200 | [diff] [blame] | 1518 | if (*p == 0) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1519 | ctxt->content = ctxt->inrptr; |
| 1520 | xmlFree(p); |
| 1521 | break; |
| 1522 | } |
| 1523 | xmlNanoHTTPScanAnswer(ctxt, p); |
| 1524 | |
| 1525 | #ifdef DEBUG_HTTP |
| 1526 | xmlGenericError(xmlGenericErrorContext, "<- %s\n", p); |
| 1527 | #endif |
| 1528 | xmlFree(p); |
| 1529 | } |
| 1530 | |
| 1531 | if ((ctxt->location != NULL) && (ctxt->returnValue >= 300) && |
| 1532 | (ctxt->returnValue < 400)) { |
| 1533 | #ifdef DEBUG_HTTP |
| 1534 | xmlGenericError(xmlGenericErrorContext, |
| 1535 | "\nRedirect to: %s\n", ctxt->location); |
| 1536 | #endif |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1537 | while ( xmlNanoHTTPRecv(ctxt) > 0 ) ; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1538 | if (nbRedirects < XML_NANO_HTTP_MAX_REDIR) { |
| 1539 | nbRedirects++; |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 1540 | if (redirURL != NULL) |
| 1541 | xmlFree(redirURL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1542 | redirURL = xmlMemStrdup(ctxt->location); |
| 1543 | xmlNanoHTTPFreeCtxt(ctxt); |
| 1544 | goto retry; |
| 1545 | } |
| 1546 | xmlNanoHTTPFreeCtxt(ctxt); |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 1547 | if (redirURL != NULL) xmlFree(redirURL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1548 | #ifdef DEBUG_HTTP |
| 1549 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1550 | "xmlNanoHTTPMethodRedir: Too many redirects, aborting ...\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1551 | #endif |
| 1552 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1553 | } |
| 1554 | |
| 1555 | if (contentType != NULL) { |
| 1556 | if (ctxt->contentType != NULL) |
| 1557 | *contentType = xmlMemStrdup(ctxt->contentType); |
| 1558 | else |
| 1559 | *contentType = NULL; |
| 1560 | } |
| 1561 | |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 1562 | if ((redir != NULL) && (redirURL != NULL)) { |
| 1563 | *redir = redirURL; |
| 1564 | } else { |
| 1565 | if (redirURL != NULL) |
| 1566 | xmlFree(redirURL); |
| 1567 | if (redir != NULL) |
| 1568 | *redir = NULL; |
| 1569 | } |
| 1570 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1571 | #ifdef DEBUG_HTTP |
| 1572 | if (ctxt->contentType != NULL) |
| 1573 | xmlGenericError(xmlGenericErrorContext, |
| 1574 | "\nCode %d, content-type '%s'\n\n", |
| 1575 | ctxt->returnValue, ctxt->contentType); |
| 1576 | else |
| 1577 | xmlGenericError(xmlGenericErrorContext, |
| 1578 | "\nCode %d, no content-type\n\n", |
| 1579 | ctxt->returnValue); |
| 1580 | #endif |
| 1581 | |
| 1582 | return((void *) ctxt); |
| 1583 | } |
| 1584 | |
| 1585 | /** |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 1586 | * xmlNanoHTTPMethod: |
| 1587 | * @URL: The URL to load |
| 1588 | * @method: the HTTP method to use |
| 1589 | * @input: the input string if any |
| 1590 | * @contentType: the Content-Type information IN and OUT |
| 1591 | * @headers: the extra headers |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1592 | * @ilen: input length |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 1593 | * |
| 1594 | * This function try to open a connection to the indicated resource |
| 1595 | * via HTTP using the given @method, adding the given extra headers |
| 1596 | * and the input buffer for the request content. |
| 1597 | * |
| 1598 | * Returns NULL in case of failure, otherwise a request handler. |
| 1599 | * The contentType, if provided must be freed by the caller |
| 1600 | */ |
| 1601 | |
| 1602 | void* |
| 1603 | xmlNanoHTTPMethod(const char *URL, const char *method, const char *input, |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1604 | char **contentType, const char *headers, int ilen) { |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 1605 | return(xmlNanoHTTPMethodRedir(URL, method, input, contentType, |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1606 | NULL, headers, ilen)); |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 1607 | } |
| 1608 | |
| 1609 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1610 | * xmlNanoHTTPFetch: |
| 1611 | * @URL: The URL to load |
| 1612 | * @filename: the filename where the content should be saved |
| 1613 | * @contentType: if available the Content-Type information will be |
| 1614 | * returned at that location |
| 1615 | * |
| 1616 | * This function try to fetch the indicated resource via HTTP GET |
| 1617 | * and save it's content in the file. |
| 1618 | * |
| 1619 | * Returns -1 in case of failure, 0 incase of success. The contentType, |
| 1620 | * if provided must be freed by the caller |
| 1621 | */ |
| 1622 | int |
| 1623 | xmlNanoHTTPFetch(const char *URL, const char *filename, char **contentType) { |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1624 | void *ctxt = NULL; |
| 1625 | char *buf = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1626 | int fd; |
| 1627 | int len; |
Stefan Kost | dff8d0f | 2011-05-09 12:14:59 +0300 | [diff] [blame] | 1628 | int ret = 0; |
| 1629 | |
William M. Brack | 015ccb2 | 2005-02-13 08:18:52 +0000 | [diff] [blame] | 1630 | if (filename == NULL) return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1631 | ctxt = xmlNanoHTTPOpen(URL, contentType); |
| 1632 | if (ctxt == NULL) return(-1); |
| 1633 | |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 1634 | if (!strcmp(filename, "-")) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1635 | fd = 0; |
| 1636 | else { |
| 1637 | fd = open(filename, O_CREAT | O_WRONLY, 00644); |
| 1638 | if (fd < 0) { |
| 1639 | xmlNanoHTTPClose(ctxt); |
| 1640 | if ((contentType != NULL) && (*contentType != NULL)) { |
| 1641 | xmlFree(*contentType); |
| 1642 | *contentType = NULL; |
| 1643 | } |
| 1644 | return(-1); |
| 1645 | } |
| 1646 | } |
| 1647 | |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1648 | xmlNanoHTTPFetchContent( ctxt, &buf, &len ); |
| 1649 | if ( len > 0 ) { |
Stefan Kost | dff8d0f | 2011-05-09 12:14:59 +0300 | [diff] [blame] | 1650 | if (write(fd, buf, len) == -1) { |
| 1651 | ret = -1; |
| 1652 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1653 | } |
| 1654 | |
| 1655 | xmlNanoHTTPClose(ctxt); |
| 1656 | close(fd); |
Stefan Kost | dff8d0f | 2011-05-09 12:14:59 +0300 | [diff] [blame] | 1657 | return(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1658 | } |
| 1659 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1660 | #ifdef LIBXML_OUTPUT_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1661 | /** |
| 1662 | * xmlNanoHTTPSave: |
| 1663 | * @ctxt: the HTTP context |
| 1664 | * @filename: the filename where the content should be saved |
| 1665 | * |
| 1666 | * This function saves the output of the HTTP transaction to a file |
| 1667 | * It closes and free the context at the end |
| 1668 | * |
| 1669 | * Returns -1 in case of failure, 0 incase of success. |
| 1670 | */ |
| 1671 | int |
| 1672 | xmlNanoHTTPSave(void *ctxt, const char *filename) { |
Daniel Veillard | e392497 | 2001-07-25 20:25:21 +0000 | [diff] [blame] | 1673 | char *buf = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1674 | int fd; |
| 1675 | int len; |
Stefan Kost | dff8d0f | 2011-05-09 12:14:59 +0300 | [diff] [blame] | 1676 | int ret = 0; |
| 1677 | |
William M. Brack | 015ccb2 | 2005-02-13 08:18:52 +0000 | [diff] [blame] | 1678 | if ((ctxt == NULL) || (filename == NULL)) return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1679 | |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 1680 | if (!strcmp(filename, "-")) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1681 | fd = 0; |
| 1682 | else { |
Daniel Veillard | cd2ebab | 2007-08-23 20:47:33 +0000 | [diff] [blame] | 1683 | fd = open(filename, O_CREAT | O_WRONLY, 0666); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1684 | if (fd < 0) { |
| 1685 | xmlNanoHTTPClose(ctxt); |
| 1686 | return(-1); |
| 1687 | } |
| 1688 | } |
| 1689 | |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1690 | xmlNanoHTTPFetchContent( ctxt, &buf, &len ); |
| 1691 | if ( len > 0 ) { |
Stefan Kost | dff8d0f | 2011-05-09 12:14:59 +0300 | [diff] [blame] | 1692 | if (write(fd, buf, len) == -1) { |
| 1693 | ret = -1; |
| 1694 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1695 | } |
| 1696 | |
| 1697 | xmlNanoHTTPClose(ctxt); |
William M. Brack | 20d8236 | 2004-03-17 08:44:46 +0000 | [diff] [blame] | 1698 | close(fd); |
Stefan Kost | dff8d0f | 2011-05-09 12:14:59 +0300 | [diff] [blame] | 1699 | return(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1700 | } |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1701 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1702 | |
| 1703 | /** |
| 1704 | * xmlNanoHTTPReturnCode: |
| 1705 | * @ctx: the HTTP context |
| 1706 | * |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1707 | * Get the latest HTTP return code received |
| 1708 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1709 | * Returns the HTTP return code for the request. |
| 1710 | */ |
| 1711 | int |
| 1712 | xmlNanoHTTPReturnCode(void *ctx) { |
| 1713 | xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx; |
| 1714 | |
| 1715 | if (ctxt == NULL) return(-1); |
| 1716 | |
| 1717 | return(ctxt->returnValue); |
| 1718 | } |
| 1719 | |
| 1720 | /** |
| 1721 | * xmlNanoHTTPAuthHeader: |
| 1722 | * @ctx: the HTTP context |
| 1723 | * |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1724 | * Get the authentication header of an HTTP context |
| 1725 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1726 | * Returns the stashed value of the WWW-Authenticate or Proxy-Authenticate |
| 1727 | * header. |
| 1728 | */ |
| 1729 | const char * |
| 1730 | xmlNanoHTTPAuthHeader(void *ctx) { |
| 1731 | xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx; |
| 1732 | |
| 1733 | if (ctxt == NULL) return(NULL); |
| 1734 | |
| 1735 | return(ctxt->authHeader); |
| 1736 | } |
| 1737 | |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1738 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 1739 | * xmlNanoHTTPContentLength: |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1740 | * @ctx: the HTTP context |
| 1741 | * |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 1742 | * Provides the specified content length from the HTTP header. |
| 1743 | * |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1744 | * Return the specified content length from the HTTP header. Note that |
| 1745 | * a value of -1 indicates that the content length element was not included in |
| 1746 | * the response header. |
| 1747 | */ |
| 1748 | int |
| 1749 | xmlNanoHTTPContentLength( void * ctx ) { |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 1750 | xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1751 | |
| 1752 | return ( ( ctxt == NULL ) ? -1 : ctxt->ContentLength ); |
| 1753 | } |
| 1754 | |
| 1755 | /** |
Daniel Veillard | 847332a | 2003-10-18 11:29:40 +0000 | [diff] [blame] | 1756 | * xmlNanoHTTPRedir: |
| 1757 | * @ctx: the HTTP context |
| 1758 | * |
| 1759 | * Provides the specified redirection URL if available from the HTTP header. |
| 1760 | * |
| 1761 | * Return the specified redirection URL or NULL if not redirected. |
| 1762 | */ |
| 1763 | const char * |
| 1764 | xmlNanoHTTPRedir( void * ctx ) { |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 1765 | xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx; |
Daniel Veillard | 847332a | 2003-10-18 11:29:40 +0000 | [diff] [blame] | 1766 | |
| 1767 | return ( ( ctxt == NULL ) ? NULL : ctxt->location ); |
| 1768 | } |
| 1769 | |
| 1770 | /** |
| 1771 | * xmlNanoHTTPEncoding: |
| 1772 | * @ctx: the HTTP context |
| 1773 | * |
| 1774 | * Provides the specified encoding if specified in the HTTP headers. |
| 1775 | * |
| 1776 | * Return the specified encoding or NULL if not available |
| 1777 | */ |
| 1778 | const char * |
| 1779 | xmlNanoHTTPEncoding( void * ctx ) { |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 1780 | xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx; |
Daniel Veillard | 847332a | 2003-10-18 11:29:40 +0000 | [diff] [blame] | 1781 | |
| 1782 | return ( ( ctxt == NULL ) ? NULL : ctxt->encoding ); |
| 1783 | } |
| 1784 | |
| 1785 | /** |
Daniel Veillard | a840b69 | 2003-10-19 13:35:37 +0000 | [diff] [blame] | 1786 | * xmlNanoHTTPMimeType: |
| 1787 | * @ctx: the HTTP context |
| 1788 | * |
| 1789 | * Provides the specified Mime-Type if specified in the HTTP headers. |
| 1790 | * |
| 1791 | * Return the specified Mime-Type or NULL if not available |
| 1792 | */ |
| 1793 | const char * |
| 1794 | xmlNanoHTTPMimeType( void * ctx ) { |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 1795 | xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx; |
Daniel Veillard | a840b69 | 2003-10-19 13:35:37 +0000 | [diff] [blame] | 1796 | |
| 1797 | return ( ( ctxt == NULL ) ? NULL : ctxt->mimeType ); |
| 1798 | } |
| 1799 | |
| 1800 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 1801 | * xmlNanoHTTPFetchContent: |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1802 | * @ctx: the HTTP context |
| 1803 | * @ptr: pointer to set to the content buffer. |
| 1804 | * @len: integer pointer to hold the length of the content |
| 1805 | * |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 1806 | * Check if all the content was read |
| 1807 | * |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1808 | * Returns 0 if all the content was read and available, returns |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 1809 | * -1 if received content length was less than specified or an error |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1810 | * occurred. |
| 1811 | */ |
Daniel Veillard | a235132 | 2004-06-27 12:08:10 +0000 | [diff] [blame] | 1812 | static int |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1813 | xmlNanoHTTPFetchContent( void * ctx, char ** ptr, int * len ) { |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 1814 | xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1815 | |
| 1816 | int rc = 0; |
| 1817 | int cur_lgth; |
| 1818 | int rcvd_lgth; |
| 1819 | int dummy_int; |
| 1820 | char * dummy_ptr = NULL; |
| 1821 | |
| 1822 | /* Dummy up return input parameters if not provided */ |
| 1823 | |
| 1824 | if ( len == NULL ) |
| 1825 | len = &dummy_int; |
| 1826 | |
| 1827 | if ( ptr == NULL ) |
| 1828 | ptr = &dummy_ptr; |
| 1829 | |
| 1830 | /* But can't work without the context pointer */ |
| 1831 | |
| 1832 | if ( ( ctxt == NULL ) || ( ctxt->content == NULL ) ) { |
| 1833 | *len = 0; |
| 1834 | *ptr = NULL; |
| 1835 | return ( -1 ); |
| 1836 | } |
| 1837 | |
| 1838 | rcvd_lgth = ctxt->inptr - ctxt->content; |
| 1839 | |
| 1840 | while ( (cur_lgth = xmlNanoHTTPRecv( ctxt )) > 0 ) { |
| 1841 | |
| 1842 | rcvd_lgth += cur_lgth; |
| 1843 | if ( (ctxt->ContentLength > 0) && (rcvd_lgth >= ctxt->ContentLength) ) |
| 1844 | break; |
| 1845 | } |
| 1846 | |
| 1847 | *ptr = ctxt->content; |
| 1848 | *len = rcvd_lgth; |
| 1849 | |
| 1850 | if ( ( ctxt->ContentLength > 0 ) && ( rcvd_lgth < ctxt->ContentLength ) ) |
| 1851 | rc = -1; |
| 1852 | else if ( rcvd_lgth == 0 ) |
| 1853 | rc = -1; |
| 1854 | |
| 1855 | return ( rc ); |
| 1856 | } |
| 1857 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1858 | #ifdef STANDALONE |
| 1859 | int main(int argc, char **argv) { |
| 1860 | char *contentType = NULL; |
| 1861 | |
| 1862 | if (argv[1] != NULL) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 1863 | if (argv[2] != NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1864 | xmlNanoHTTPFetch(argv[1], argv[2], &contentType); |
| 1865 | else |
| 1866 | xmlNanoHTTPFetch(argv[1], "-", &contentType); |
| 1867 | if (contentType != NULL) xmlFree(contentType); |
| 1868 | } else { |
| 1869 | xmlGenericError(xmlGenericErrorContext, |
| 1870 | "%s: minimal HTTP GET implementation\n", argv[0]); |
| 1871 | xmlGenericError(xmlGenericErrorContext, |
| 1872 | "\tusage %s [ URL [ filename ] ]\n", argv[0]); |
| 1873 | } |
| 1874 | xmlNanoHTTPCleanup(); |
| 1875 | xmlMemoryDump(); |
| 1876 | return(0); |
| 1877 | } |
| 1878 | #endif /* STANDALONE */ |
| 1879 | #else /* !LIBXML_HTTP_ENABLED */ |
| 1880 | #ifdef STANDALONE |
| 1881 | #include <stdio.h> |
| 1882 | int main(int argc, char **argv) { |
| 1883 | xmlGenericError(xmlGenericErrorContext, |
| 1884 | "%s : HTTP support not compiled in\n", argv[0]); |
| 1885 | return(0); |
| 1886 | } |
| 1887 | #endif /* STANDALONE */ |
| 1888 | #endif /* LIBXML_HTTP_ENABLED */ |
Daniel Veillard | 5d4644e | 2005-04-01 13:11:58 +0000 | [diff] [blame] | 1889 | #define bottom_nanohttp |
| 1890 | #include "elfgcchack.h" |