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