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