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 ); |
| 465 | FD_SET( ctxt->fd, &wfd ); |
| 466 | (void)select( ctxt->fd + 1, NULL, &wfd, NULL, &tv ); |
| 467 | } |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 468 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 469 | } |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 470 | |
| 471 | return total_sent; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | /** |
| 475 | * xmlNanoHTTPRecv: |
| 476 | * @ctxt: an HTTP context |
| 477 | * |
| 478 | * Read information coming from the HTTP connection. |
| 479 | * This is a blocking call (but it blocks in select(), not read()). |
| 480 | * |
| 481 | * Returns the number of byte read or -1 in case of error. |
| 482 | */ |
| 483 | |
| 484 | static int |
| 485 | xmlNanoHTTPRecv(xmlNanoHTTPCtxtPtr ctxt) { |
| 486 | fd_set rfd; |
| 487 | struct timeval tv; |
| 488 | |
| 489 | |
| 490 | while (ctxt->state & XML_NANO_HTTP_READ) { |
| 491 | if (ctxt->in == NULL) { |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 492 | ctxt->in = (char *) xmlMallocAtomic(65000 * sizeof(char)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 493 | if (ctxt->in == NULL) { |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 494 | xmlHTTPErrMemory("allocating input"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 495 | ctxt->last = -1; |
| 496 | return(-1); |
| 497 | } |
| 498 | ctxt->inlen = 65000; |
| 499 | ctxt->inptr = ctxt->content = ctxt->inrptr = ctxt->in; |
| 500 | } |
| 501 | if (ctxt->inrptr > ctxt->in + XML_NANO_HTTP_CHUNK) { |
| 502 | int delta = ctxt->inrptr - ctxt->in; |
| 503 | int len = ctxt->inptr - ctxt->inrptr; |
| 504 | |
| 505 | memmove(ctxt->in, ctxt->inrptr, len); |
| 506 | ctxt->inrptr -= delta; |
| 507 | ctxt->content -= delta; |
| 508 | ctxt->inptr -= delta; |
| 509 | } |
| 510 | if ((ctxt->in + ctxt->inlen) < (ctxt->inptr + XML_NANO_HTTP_CHUNK)) { |
| 511 | int d_inptr = ctxt->inptr - ctxt->in; |
| 512 | int d_content = ctxt->content - ctxt->in; |
| 513 | int d_inrptr = ctxt->inrptr - ctxt->in; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 514 | char * tmp_ptr = ctxt->in; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 515 | |
| 516 | ctxt->inlen *= 2; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 517 | ctxt->in = (char *) xmlRealloc(tmp_ptr, ctxt->inlen); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 518 | if (ctxt->in == NULL) { |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 519 | xmlHTTPErrMemory("allocating input buffer"); |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 520 | xmlFree( tmp_ptr ); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 521 | ctxt->last = -1; |
| 522 | return(-1); |
| 523 | } |
| 524 | ctxt->inptr = ctxt->in + d_inptr; |
| 525 | ctxt->content = ctxt->in + d_content; |
| 526 | ctxt->inrptr = ctxt->in + d_inrptr; |
| 527 | } |
| 528 | ctxt->last = recv(ctxt->fd, ctxt->inptr, XML_NANO_HTTP_CHUNK, 0); |
| 529 | if (ctxt->last > 0) { |
| 530 | ctxt->inptr += ctxt->last; |
| 531 | return(ctxt->last); |
| 532 | } |
| 533 | if (ctxt->last == 0) { |
| 534 | return(0); |
| 535 | } |
| 536 | if (ctxt->last == -1) { |
| 537 | switch (socket_errno()) { |
| 538 | case EINPROGRESS: |
| 539 | case EWOULDBLOCK: |
| 540 | #if defined(EAGAIN) && EAGAIN != EWOULDBLOCK |
| 541 | case EAGAIN: |
| 542 | #endif |
| 543 | break; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 544 | |
| 545 | case ECONNRESET: |
| 546 | case ESHUTDOWN: |
| 547 | return ( 0 ); |
| 548 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 549 | default: |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 550 | __xmlIOErr(XML_FROM_HTTP, 0, "recv failed\n"); |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 551 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 552 | } |
| 553 | } |
| 554 | |
| 555 | tv.tv_sec = timeout; |
| 556 | tv.tv_usec = 0; |
| 557 | FD_ZERO(&rfd); |
| 558 | FD_SET(ctxt->fd, &rfd); |
| 559 | |
Daniel Veillard | 50f3437 | 2001-08-03 12:06:36 +0000 | [diff] [blame] | 560 | if ( (select(ctxt->fd+1, &rfd, NULL, NULL, &tv)<1) |
| 561 | #if defined(EINTR) |
| 562 | && (errno != EINTR) |
| 563 | #endif |
| 564 | ) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 565 | return(0); |
| 566 | } |
| 567 | return(0); |
| 568 | } |
| 569 | |
| 570 | /** |
| 571 | * xmlNanoHTTPReadLine: |
| 572 | * @ctxt: an HTTP context |
| 573 | * |
| 574 | * Read one line in the HTTP server output, usually for extracting |
| 575 | * the HTTP protocol informations from the answer header. |
| 576 | * |
| 577 | * Returns a newly allocated string with a copy of the line, or NULL |
| 578 | * which indicate the end of the input. |
| 579 | */ |
| 580 | |
| 581 | static char * |
| 582 | xmlNanoHTTPReadLine(xmlNanoHTTPCtxtPtr ctxt) { |
| 583 | char buf[4096]; |
| 584 | char *bp = buf; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 585 | int rc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 586 | |
| 587 | while (bp - buf < 4095) { |
| 588 | if (ctxt->inrptr == ctxt->inptr) { |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 589 | if ( (rc = xmlNanoHTTPRecv(ctxt)) == 0) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 590 | if (bp == buf) |
| 591 | return(NULL); |
| 592 | else |
| 593 | *bp = 0; |
| 594 | return(xmlMemStrdup(buf)); |
| 595 | } |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 596 | else if ( rc == -1 ) { |
| 597 | return ( NULL ); |
| 598 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 599 | } |
| 600 | *bp = *ctxt->inrptr++; |
| 601 | if (*bp == '\n') { |
| 602 | *bp = 0; |
| 603 | return(xmlMemStrdup(buf)); |
| 604 | } |
| 605 | if (*bp != '\r') |
| 606 | bp++; |
| 607 | } |
| 608 | buf[4095] = 0; |
| 609 | return(xmlMemStrdup(buf)); |
| 610 | } |
| 611 | |
| 612 | |
| 613 | /** |
| 614 | * xmlNanoHTTPScanAnswer: |
| 615 | * @ctxt: an HTTP context |
| 616 | * @line: an HTTP header line |
| 617 | * |
| 618 | * Try to extract useful informations from the server answer. |
| 619 | * We currently parse and process: |
| 620 | * - The HTTP revision/ return code |
Daniel Veillard | a840b69 | 2003-10-19 13:35:37 +0000 | [diff] [blame] | 621 | * - The Content-Type, Mime-Type and charset used |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 622 | * - The Location for redirect processing. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 623 | * |
| 624 | * Returns -1 in case of failure, the file descriptor number otherwise |
| 625 | */ |
| 626 | |
| 627 | static void |
| 628 | xmlNanoHTTPScanAnswer(xmlNanoHTTPCtxtPtr ctxt, const char *line) { |
| 629 | const char *cur = line; |
| 630 | |
| 631 | if (line == NULL) return; |
| 632 | |
| 633 | if (!strncmp(line, "HTTP/", 5)) { |
| 634 | int version = 0; |
| 635 | int ret = 0; |
| 636 | |
| 637 | cur += 5; |
| 638 | while ((*cur >= '0') && (*cur <= '9')) { |
| 639 | version *= 10; |
| 640 | version += *cur - '0'; |
| 641 | cur++; |
| 642 | } |
| 643 | if (*cur == '.') { |
| 644 | cur++; |
| 645 | if ((*cur >= '0') && (*cur <= '9')) { |
| 646 | version *= 10; |
| 647 | version += *cur - '0'; |
| 648 | cur++; |
| 649 | } |
| 650 | while ((*cur >= '0') && (*cur <= '9')) |
| 651 | cur++; |
| 652 | } else |
| 653 | version *= 10; |
| 654 | if ((*cur != ' ') && (*cur != '\t')) return; |
| 655 | while ((*cur == ' ') || (*cur == '\t')) cur++; |
| 656 | if ((*cur < '0') || (*cur > '9')) return; |
| 657 | while ((*cur >= '0') && (*cur <= '9')) { |
| 658 | ret *= 10; |
| 659 | ret += *cur - '0'; |
| 660 | cur++; |
| 661 | } |
| 662 | if ((*cur != 0) && (*cur != ' ') && (*cur != '\t')) return; |
| 663 | ctxt->returnValue = ret; |
| 664 | } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Content-Type:", 13)) { |
Daniel Veillard | a840b69 | 2003-10-19 13:35:37 +0000 | [diff] [blame] | 665 | const xmlChar *charset, *last, *mime; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 666 | cur += 13; |
| 667 | while ((*cur == ' ') || (*cur == '\t')) cur++; |
| 668 | if (ctxt->contentType != NULL) |
| 669 | xmlFree(ctxt->contentType); |
| 670 | ctxt->contentType = xmlMemStrdup(cur); |
Daniel Veillard | a840b69 | 2003-10-19 13:35:37 +0000 | [diff] [blame] | 671 | mime = (const xmlChar *) cur; |
| 672 | last = mime; |
| 673 | while ((*last != 0) && (*last != ' ') && (*last != '\t') && |
| 674 | (*last != ';') && (*last != ',')) |
| 675 | last++; |
| 676 | if (ctxt->mimeType != NULL) |
| 677 | xmlFree(ctxt->mimeType); |
| 678 | ctxt->mimeType = (char *) xmlStrndup(mime, last - mime); |
| 679 | charset = xmlStrstr(BAD_CAST ctxt->contentType, BAD_CAST "charset="); |
| 680 | if (charset != NULL) { |
| 681 | charset += 8; |
| 682 | last = charset; |
| 683 | while ((*last != 0) && (*last != ' ') && (*last != '\t') && |
| 684 | (*last != ';') && (*last != ',')) |
| 685 | last++; |
| 686 | if (ctxt->encoding != NULL) |
| 687 | xmlFree(ctxt->encoding); |
| 688 | ctxt->encoding = (char *) xmlStrndup(charset, last - charset); |
| 689 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 690 | } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"ContentType:", 12)) { |
Daniel Veillard | a840b69 | 2003-10-19 13:35:37 +0000 | [diff] [blame] | 691 | const xmlChar *charset, *last, *mime; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 692 | cur += 12; |
| 693 | if (ctxt->contentType != NULL) return; |
| 694 | while ((*cur == ' ') || (*cur == '\t')) cur++; |
| 695 | ctxt->contentType = xmlMemStrdup(cur); |
Daniel Veillard | a840b69 | 2003-10-19 13:35:37 +0000 | [diff] [blame] | 696 | mime = (const xmlChar *) cur; |
| 697 | last = mime; |
| 698 | while ((*last != 0) && (*last != ' ') && (*last != '\t') && |
| 699 | (*last != ';') && (*last != ',')) |
| 700 | last++; |
| 701 | if (ctxt->mimeType != NULL) |
| 702 | xmlFree(ctxt->mimeType); |
| 703 | ctxt->mimeType = (char *) xmlStrndup(mime, last - mime); |
| 704 | charset = xmlStrstr(BAD_CAST ctxt->contentType, BAD_CAST "charset="); |
| 705 | if (charset != NULL) { |
| 706 | charset += 8; |
| 707 | last = charset; |
| 708 | while ((*last != 0) && (*last != ' ') && (*last != '\t') && |
| 709 | (*last != ';') && (*last != ',')) |
| 710 | last++; |
| 711 | if (ctxt->encoding != NULL) |
| 712 | xmlFree(ctxt->encoding); |
| 713 | ctxt->encoding = (char *) xmlStrndup(charset, last - charset); |
| 714 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 715 | } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Location:", 9)) { |
| 716 | cur += 9; |
| 717 | while ((*cur == ' ') || (*cur == '\t')) cur++; |
| 718 | if (ctxt->location != NULL) |
| 719 | xmlFree(ctxt->location); |
William M. Brack | 7e29c0a | 2004-04-02 09:07:22 +0000 | [diff] [blame] | 720 | if (*cur == '/') { |
| 721 | xmlChar *tmp_http = xmlStrdup(BAD_CAST "http://"); |
| 722 | xmlChar *tmp_loc = |
| 723 | xmlStrcat(tmp_http, (const xmlChar *) ctxt->hostname); |
| 724 | ctxt->location = |
| 725 | (char *) xmlStrcat (tmp_loc, (const xmlChar *) cur); |
| 726 | } else { |
| 727 | ctxt->location = xmlMemStrdup(cur); |
| 728 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 729 | } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"WWW-Authenticate:", 17)) { |
| 730 | cur += 17; |
| 731 | while ((*cur == ' ') || (*cur == '\t')) cur++; |
| 732 | if (ctxt->authHeader != NULL) |
| 733 | xmlFree(ctxt->authHeader); |
| 734 | ctxt->authHeader = xmlMemStrdup(cur); |
| 735 | } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Proxy-Authenticate:", 19)) { |
| 736 | cur += 19; |
| 737 | while ((*cur == ' ') || (*cur == '\t')) cur++; |
| 738 | if (ctxt->authHeader != NULL) |
| 739 | xmlFree(ctxt->authHeader); |
| 740 | ctxt->authHeader = xmlMemStrdup(cur); |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 741 | } else if ( !xmlStrncasecmp( BAD_CAST line, BAD_CAST"Content-Length:", 15) ) { |
| 742 | cur += 15; |
| 743 | ctxt->ContentLength = strtol( cur, NULL, 10 ); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 744 | } |
| 745 | } |
| 746 | |
| 747 | /** |
| 748 | * xmlNanoHTTPConnectAttempt: |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 749 | * @addr: a socket address structure |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 750 | * |
| 751 | * Attempt a connection to the given IP:port endpoint. It forces |
| 752 | * non-blocking semantic on the socket, and allow 60 seconds for |
| 753 | * the host to answer. |
| 754 | * |
| 755 | * Returns -1 in case of failure, the file descriptor number otherwise |
| 756 | */ |
| 757 | |
| 758 | static int |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 759 | xmlNanoHTTPConnectAttempt(struct sockaddr *addr) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 760 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 761 | fd_set wfd; |
Daniel Veillard | 5bb9ccd | 2004-02-09 12:39:02 +0000 | [diff] [blame] | 762 | #ifdef _WINSOCKAPI_ |
| 763 | fd_set xfd; |
| 764 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 765 | struct timeval tv; |
| 766 | int status; |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 767 | int addrlen; |
| 768 | SOCKET s; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 769 | |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 770 | #ifdef SUPPORT_IP6 |
| 771 | if (addr->sa_family == AF_INET6) { |
| 772 | s = socket (PF_INET6, SOCK_STREAM, IPPROTO_TCP); |
| 773 | addrlen = sizeof (struct sockaddr_in6); |
| 774 | } |
| 775 | else |
| 776 | #endif |
| 777 | { |
| 778 | s = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 779 | addrlen = sizeof (struct sockaddr_in); |
| 780 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 781 | if (s==-1) { |
| 782 | #ifdef DEBUG_HTTP |
| 783 | perror("socket"); |
| 784 | #endif |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 785 | __xmlIOErr(XML_FROM_HTTP, 0, "socket failed\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 786 | return(-1); |
| 787 | } |
| 788 | |
| 789 | #ifdef _WINSOCKAPI_ |
| 790 | { |
| 791 | u_long one = 1; |
| 792 | |
| 793 | status = ioctlsocket(s, FIONBIO, &one) == SOCKET_ERROR ? -1 : 0; |
| 794 | } |
| 795 | #else /* _WINSOCKAPI_ */ |
| 796 | #if defined(VMS) |
| 797 | { |
| 798 | int enable = 1; |
| 799 | status = ioctl(s, FIONBIO, &enable); |
| 800 | } |
| 801 | #else /* VMS */ |
Daniel Veillard | 254b126 | 2003-11-01 17:04:58 +0000 | [diff] [blame] | 802 | #if defined(__BEOS__) |
| 803 | { |
| 804 | bool noblock = true; |
| 805 | status = setsockopt(s, SOL_SOCKET, SO_NONBLOCK, &noblock, sizeof(noblock)); |
| 806 | } |
| 807 | #else /* __BEOS__ */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 808 | if ((status = fcntl(s, F_GETFL, 0)) != -1) { |
| 809 | #ifdef O_NONBLOCK |
| 810 | status |= O_NONBLOCK; |
| 811 | #else /* O_NONBLOCK */ |
| 812 | #ifdef F_NDELAY |
| 813 | status |= F_NDELAY; |
| 814 | #endif /* F_NDELAY */ |
| 815 | #endif /* !O_NONBLOCK */ |
| 816 | status = fcntl(s, F_SETFL, status); |
| 817 | } |
| 818 | if (status < 0) { |
| 819 | #ifdef DEBUG_HTTP |
| 820 | perror("nonblocking"); |
| 821 | #endif |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 822 | __xmlIOErr(XML_FROM_HTTP, 0, "error setting non-blocking IO\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 823 | closesocket(s); |
| 824 | return(-1); |
| 825 | } |
Daniel Veillard | 254b126 | 2003-11-01 17:04:58 +0000 | [diff] [blame] | 826 | #endif /* !__BEOS__ */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 827 | #endif /* !VMS */ |
| 828 | #endif /* !_WINSOCKAPI_ */ |
| 829 | |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 830 | if (connect (s, addr, addrlen) == -1) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 831 | switch (socket_errno()) { |
| 832 | case EINPROGRESS: |
| 833 | case EWOULDBLOCK: |
| 834 | break; |
| 835 | default: |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 836 | __xmlIOErr(XML_FROM_HTTP, 0, "error connecting to HTTP server"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 837 | closesocket(s); |
| 838 | return(-1); |
| 839 | } |
| 840 | } |
| 841 | |
| 842 | tv.tv_sec = timeout; |
| 843 | tv.tv_usec = 0; |
| 844 | |
| 845 | FD_ZERO(&wfd); |
| 846 | FD_SET(s, &wfd); |
Daniel Veillard | 5bb9ccd | 2004-02-09 12:39:02 +0000 | [diff] [blame] | 847 | |
| 848 | #ifdef _WINSOCKAPI_ |
| 849 | FD_ZERO(&xfd); |
| 850 | FD_SET(s, &xfd); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 851 | |
Daniel Veillard | 5bb9ccd | 2004-02-09 12:39:02 +0000 | [diff] [blame] | 852 | switch(select(s+1, NULL, &wfd, &xfd, &tv)) |
| 853 | #else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 854 | switch(select(s+1, NULL, &wfd, NULL, &tv)) |
Daniel Veillard | 5bb9ccd | 2004-02-09 12:39:02 +0000 | [diff] [blame] | 855 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 856 | { |
| 857 | case 0: |
| 858 | /* Time out */ |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 859 | __xmlIOErr(XML_FROM_HTTP, 0, "Connect attempt timed out"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 860 | closesocket(s); |
| 861 | return(-1); |
| 862 | case -1: |
| 863 | /* Ermm.. ?? */ |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 864 | __xmlIOErr(XML_FROM_HTTP, 0, "Connect failed"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 865 | closesocket(s); |
| 866 | return(-1); |
| 867 | } |
| 868 | |
Daniel Veillard | 5bb9ccd | 2004-02-09 12:39:02 +0000 | [diff] [blame] | 869 | if ( FD_ISSET(s, &wfd) |
| 870 | #ifdef _WINSOCKAPI_ |
| 871 | || FD_ISSET(s, &xfd) |
| 872 | #endif |
| 873 | ) { |
Daniel Veillard | c284c64 | 2005-03-31 10:24:24 +0000 | [diff] [blame] | 874 | XML_SOCKLEN_T len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 875 | len = sizeof(status); |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 876 | #ifdef SO_ERROR |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 877 | if (getsockopt(s, SOL_SOCKET, SO_ERROR, (char*)&status, &len) < 0 ) { |
| 878 | /* Solaris error code */ |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 879 | __xmlIOErr(XML_FROM_HTTP, 0, "getsockopt failed\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 880 | return (-1); |
| 881 | } |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 882 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 883 | if ( status ) { |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 884 | __xmlIOErr(XML_FROM_HTTP, 0, "Error connecting to remote host"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 885 | closesocket(s); |
| 886 | errno = status; |
| 887 | return (-1); |
| 888 | } |
| 889 | } else { |
| 890 | /* pbm */ |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 891 | __xmlIOErr(XML_FROM_HTTP, 0, "select failed\n"); |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 892 | closesocket(s); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 893 | return (-1); |
| 894 | } |
| 895 | |
| 896 | return(s); |
| 897 | } |
| 898 | |
| 899 | /** |
| 900 | * xmlNanoHTTPConnectHost: |
| 901 | * @host: the host name |
| 902 | * @port: the port number |
| 903 | * |
| 904 | * Attempt a connection to the given host:port endpoint. It tries |
| 905 | * the multiple IP provided by the DNS if available. |
| 906 | * |
| 907 | * Returns -1 in case of failure, the file descriptor number otherwise |
| 908 | */ |
| 909 | |
| 910 | static int |
| 911 | xmlNanoHTTPConnectHost(const char *host, int port) |
| 912 | { |
| 913 | struct hostent *h; |
Daniel Veillard | 2db8c12 | 2003-07-08 12:16:59 +0000 | [diff] [blame] | 914 | struct sockaddr *addr = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 915 | struct in_addr ia; |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 916 | struct sockaddr_in sockin; |
Daniel Veillard | 5c39654 | 2002-03-15 07:57:50 +0000 | [diff] [blame] | 917 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 918 | #ifdef SUPPORT_IP6 |
| 919 | struct in6_addr ia6; |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 920 | struct sockaddr_in6 sockin6; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 921 | #endif |
| 922 | int i; |
| 923 | int s; |
Daniel Veillard | 5c39654 | 2002-03-15 07:57:50 +0000 | [diff] [blame] | 924 | |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 925 | memset (&sockin, 0, sizeof(sockin)); |
| 926 | #ifdef SUPPORT_IP6 |
| 927 | memset (&sockin6, 0, sizeof(sockin6)); |
| 928 | if (have_ipv6 ()) |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 929 | #if !defined(HAVE_GETADDRINFO) && defined(RES_USE_INET6) |
Daniel Veillard | 560c2a4 | 2003-07-06 21:13:49 +0000 | [diff] [blame] | 930 | { |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 931 | if (!(_res.options & RES_INIT)) |
| 932 | res_init(); |
| 933 | _res.options |= RES_USE_INET6; |
| 934 | } |
| 935 | #elif defined(HAVE_GETADDRINFO) |
Daniel Veillard | 560c2a4 | 2003-07-06 21:13:49 +0000 | [diff] [blame] | 936 | { |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 937 | int status; |
| 938 | struct addrinfo hints, *res, *result; |
| 939 | |
| 940 | result = NULL; |
| 941 | memset (&hints, 0,sizeof(hints)); |
| 942 | hints.ai_socktype = SOCK_STREAM; |
| 943 | |
| 944 | status = getaddrinfo (host, NULL, &hints, &result); |
| 945 | if (status) { |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 946 | __xmlIOErr(XML_FROM_HTTP, 0, "getaddrinfo failed\n"); |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 947 | return (-1); |
| 948 | } |
| 949 | |
| 950 | for (res = result; res; res = res->ai_next) { |
Daniel Veillard | 3dc93a4 | 2003-07-10 14:04:33 +0000 | [diff] [blame] | 951 | if (res->ai_family == AF_INET || res->ai_family == AF_INET6) { |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 952 | if (res->ai_family == AF_INET6) { |
Daniel Veillard | 8e2c979 | 2004-10-27 09:39:50 +0000 | [diff] [blame] | 953 | if (res->ai_addrlen > sizeof(sockin6)) { |
| 954 | __xmlIOErr(XML_FROM_HTTP, 0, "address size mismatch\n"); |
| 955 | freeaddrinfo (result); |
| 956 | return (-1); |
| 957 | } |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 958 | memcpy (&sockin6, res->ai_addr, res->ai_addrlen); |
| 959 | sockin6.sin6_port = htons (port); |
| 960 | addr = (struct sockaddr *)&sockin6; |
| 961 | } |
Daniel Veillard | 3dc93a4 | 2003-07-10 14:04:33 +0000 | [diff] [blame] | 962 | else { |
Daniel Veillard | 8e2c979 | 2004-10-27 09:39:50 +0000 | [diff] [blame] | 963 | if (res->ai_addrlen > sizeof(sockin)) { |
| 964 | __xmlIOErr(XML_FROM_HTTP, 0, "address size mismatch\n"); |
| 965 | freeaddrinfo (result); |
| 966 | return (-1); |
| 967 | } |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 968 | memcpy (&sockin, res->ai_addr, res->ai_addrlen); |
| 969 | sockin.sin_port = htons (port); |
| 970 | addr = (struct sockaddr *)&sockin; |
| 971 | } |
| 972 | |
| 973 | s = xmlNanoHTTPConnectAttempt (addr); |
| 974 | if (s != -1) { |
| 975 | freeaddrinfo (result); |
| 976 | return (s); |
| 977 | } |
| 978 | } |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 979 | } |
Daniel Veillard | 3dc93a4 | 2003-07-10 14:04:33 +0000 | [diff] [blame] | 980 | if (result) |
| 981 | freeaddrinfo (result); |
| 982 | return (-1); |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 983 | } else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 984 | #endif |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 985 | #endif |
| 986 | { |
| 987 | h = gethostbyname (host); |
| 988 | if (h == NULL) { |
Daniel Veillard | 56b2db7 | 2002-03-25 16:35:28 +0000 | [diff] [blame] | 989 | |
| 990 | /* |
| 991 | * Okay, I got fed up by the non-portability of this error message |
| 992 | * extraction code. it work on Linux, if it work on your platform |
| 993 | * and one want to enable it, send me the defined(foobar) needed |
| 994 | */ |
| 995 | #if defined(HAVE_NETDB_H) && defined(HOST_NOT_FOUND) && defined(linux) |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 996 | const char *h_err_txt = ""; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 997 | |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 998 | switch (h_errno) { |
| 999 | case HOST_NOT_FOUND: |
| 1000 | h_err_txt = "Authoritive host not found"; |
| 1001 | break; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1002 | |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1003 | case TRY_AGAIN: |
| 1004 | h_err_txt = |
| 1005 | "Non-authoritive host not found or server failure."; |
| 1006 | break; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1007 | |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1008 | case NO_RECOVERY: |
| 1009 | h_err_txt = |
| 1010 | "Non-recoverable errors: FORMERR, REFUSED, or NOTIMP."; |
| 1011 | break; |
Daniel Veillard | 5c39654 | 2002-03-15 07:57:50 +0000 | [diff] [blame] | 1012 | |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1013 | case NO_ADDRESS: |
| 1014 | h_err_txt = |
| 1015 | "Valid name, no data record of requested type."; |
| 1016 | break; |
Daniel Veillard | 5c39654 | 2002-03-15 07:57:50 +0000 | [diff] [blame] | 1017 | |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1018 | default: |
| 1019 | h_err_txt = "No error text defined."; |
| 1020 | break; |
| 1021 | } |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 1022 | __xmlIOErr(XML_FROM_HTTP, 0, h_err_txt); |
Daniel Veillard | 5c39654 | 2002-03-15 07:57:50 +0000 | [diff] [blame] | 1023 | #else |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 1024 | __xmlIOErr(XML_FROM_HTTP, 0, "Failed to resolve host"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1025 | #endif |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1026 | return (-1); |
| 1027 | } |
Daniel Veillard | 5c39654 | 2002-03-15 07:57:50 +0000 | [diff] [blame] | 1028 | |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1029 | for (i = 0; h->h_addr_list[i]; i++) { |
| 1030 | if (h->h_addrtype == AF_INET) { |
| 1031 | /* A records (IPv4) */ |
Daniel Veillard | 8e2c979 | 2004-10-27 09:39:50 +0000 | [diff] [blame] | 1032 | if ((unsigned int) h->h_length > sizeof(ia)) { |
| 1033 | __xmlIOErr(XML_FROM_HTTP, 0, "address size mismatch\n"); |
| 1034 | return (-1); |
| 1035 | } |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1036 | memcpy (&ia, h->h_addr_list[i], h->h_length); |
| 1037 | sockin.sin_family = h->h_addrtype; |
| 1038 | sockin.sin_addr = ia; |
| 1039 | sockin.sin_port = htons (port); |
| 1040 | addr = (struct sockaddr *) &sockin; |
Daniel Veillard | 5c39654 | 2002-03-15 07:57:50 +0000 | [diff] [blame] | 1041 | #ifdef SUPPORT_IP6 |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1042 | } else if (have_ipv6 () && (h->h_addrtype == AF_INET6)) { |
| 1043 | /* AAAA records (IPv6) */ |
Daniel Veillard | 8e2c979 | 2004-10-27 09:39:50 +0000 | [diff] [blame] | 1044 | if ((unsigned int) h->h_length > sizeof(ia6)) { |
| 1045 | __xmlIOErr(XML_FROM_HTTP, 0, "address size mismatch\n"); |
| 1046 | return (-1); |
| 1047 | } |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1048 | memcpy (&ia6, h->h_addr_list[i], h->h_length); |
| 1049 | sockin6.sin6_family = h->h_addrtype; |
| 1050 | sockin6.sin6_addr = ia6; |
| 1051 | sockin6.sin6_port = htons (port); |
| 1052 | addr = (struct sockaddr *) &sockin6; |
Daniel Veillard | 5c39654 | 2002-03-15 07:57:50 +0000 | [diff] [blame] | 1053 | #endif |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1054 | } else |
| 1055 | break; /* for */ |
Daniel Veillard | 5c39654 | 2002-03-15 07:57:50 +0000 | [diff] [blame] | 1056 | |
Daniel Veillard | de2a67b | 2003-06-21 14:20:04 +0000 | [diff] [blame] | 1057 | s = xmlNanoHTTPConnectAttempt (addr); |
| 1058 | if (s != -1) |
| 1059 | return (s); |
| 1060 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1061 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1062 | #ifdef DEBUG_HTTP |
| 1063 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 5c39654 | 2002-03-15 07:57:50 +0000 | [diff] [blame] | 1064 | "xmlNanoHTTPConnectHost: unable to connect to '%s'.\n", |
| 1065 | host); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1066 | #endif |
Daniel Veillard | 5c39654 | 2002-03-15 07:57:50 +0000 | [diff] [blame] | 1067 | return (-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1068 | } |
| 1069 | |
| 1070 | |
| 1071 | /** |
| 1072 | * xmlNanoHTTPOpen: |
| 1073 | * @URL: The URL to load |
| 1074 | * @contentType: if available the Content-Type information will be |
| 1075 | * returned at that location |
| 1076 | * |
| 1077 | * This function try to open a connection to the indicated resource |
| 1078 | * via HTTP GET. |
| 1079 | * |
| 1080 | * Returns NULL in case of failure, otherwise a request handler. |
| 1081 | * The contentType, if provided must be freed by the caller |
| 1082 | */ |
| 1083 | |
| 1084 | void* |
| 1085 | xmlNanoHTTPOpen(const char *URL, char **contentType) { |
| 1086 | if (contentType != NULL) *contentType = NULL; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1087 | return(xmlNanoHTTPMethod(URL, NULL, NULL, contentType, NULL, 0)); |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 1088 | } |
| 1089 | |
| 1090 | /** |
| 1091 | * xmlNanoHTTPOpenRedir: |
| 1092 | * @URL: The URL to load |
| 1093 | * @contentType: if available the Content-Type information will be |
| 1094 | * returned at that location |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1095 | * @redir: if available the redirected URL will be returned |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 1096 | * |
| 1097 | * This function try to open a connection to the indicated resource |
| 1098 | * via HTTP GET. |
| 1099 | * |
| 1100 | * Returns NULL in case of failure, otherwise a request handler. |
| 1101 | * The contentType, if provided must be freed by the caller |
| 1102 | */ |
| 1103 | |
| 1104 | void* |
| 1105 | xmlNanoHTTPOpenRedir(const char *URL, char **contentType, char **redir) { |
| 1106 | if (contentType != NULL) *contentType = NULL; |
| 1107 | if (redir != NULL) *redir = NULL; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1108 | return(xmlNanoHTTPMethodRedir(URL, NULL, NULL, contentType, redir, NULL,0)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1109 | } |
| 1110 | |
| 1111 | /** |
| 1112 | * xmlNanoHTTPRead: |
| 1113 | * @ctx: the HTTP context |
| 1114 | * @dest: a buffer |
| 1115 | * @len: the buffer length |
| 1116 | * |
| 1117 | * This function tries to read @len bytes from the existing HTTP connection |
| 1118 | * and saves them in @dest. This is a blocking call. |
| 1119 | * |
| 1120 | * Returns the number of byte read. 0 is an indication of an end of connection. |
| 1121 | * -1 indicates a parameter error. |
| 1122 | */ |
| 1123 | int |
| 1124 | xmlNanoHTTPRead(void *ctx, void *dest, int len) { |
| 1125 | xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx; |
| 1126 | |
| 1127 | if (ctx == NULL) return(-1); |
| 1128 | if (dest == NULL) return(-1); |
| 1129 | if (len <= 0) return(0); |
| 1130 | |
| 1131 | while (ctxt->inptr - ctxt->inrptr < len) { |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1132 | if (xmlNanoHTTPRecv(ctxt) <= 0) break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1133 | } |
| 1134 | if (ctxt->inptr - ctxt->inrptr < len) |
| 1135 | len = ctxt->inptr - ctxt->inrptr; |
| 1136 | memcpy(dest, ctxt->inrptr, len); |
| 1137 | ctxt->inrptr += len; |
| 1138 | return(len); |
| 1139 | } |
| 1140 | |
| 1141 | /** |
| 1142 | * xmlNanoHTTPClose: |
| 1143 | * @ctx: the HTTP context |
| 1144 | * |
| 1145 | * This function closes an HTTP context, it ends up the connection and |
| 1146 | * free all data related to it. |
| 1147 | */ |
| 1148 | void |
| 1149 | xmlNanoHTTPClose(void *ctx) { |
| 1150 | xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx; |
| 1151 | |
| 1152 | if (ctx == NULL) return; |
| 1153 | |
| 1154 | xmlNanoHTTPFreeCtxt(ctxt); |
| 1155 | } |
| 1156 | |
| 1157 | /** |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 1158 | * xmlNanoHTTPMethodRedir: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1159 | * @URL: The URL to load |
| 1160 | * @method: the HTTP method to use |
| 1161 | * @input: the input string if any |
| 1162 | * @contentType: the Content-Type information IN and OUT |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 1163 | * @redir: the redirected URL OUT |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1164 | * @headers: the extra headers |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1165 | * @ilen: input length |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1166 | * |
| 1167 | * This function try to open a connection to the indicated resource |
| 1168 | * via HTTP using the given @method, adding the given extra headers |
| 1169 | * and the input buffer for the request content. |
| 1170 | * |
| 1171 | * Returns NULL in case of failure, otherwise a request handler. |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 1172 | * The contentType, or redir, if provided must be freed by the caller |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1173 | */ |
| 1174 | |
| 1175 | void* |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 1176 | xmlNanoHTTPMethodRedir(const char *URL, const char *method, const char *input, |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1177 | char **contentType, char **redir, |
| 1178 | const char *headers, int ilen ) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1179 | xmlNanoHTTPCtxtPtr ctxt; |
| 1180 | char *bp, *p; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1181 | int blen, ret; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1182 | int head; |
| 1183 | int nbRedirects = 0; |
| 1184 | char *redirURL = NULL; |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 1185 | #ifdef DEBUG_HTTP |
| 1186 | int xmt_bytes; |
| 1187 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1188 | |
| 1189 | if (URL == NULL) return(NULL); |
| 1190 | if (method == NULL) method = "GET"; |
| 1191 | xmlNanoHTTPInit(); |
| 1192 | |
| 1193 | retry: |
| 1194 | if (redirURL == NULL) |
| 1195 | ctxt = xmlNanoHTTPNewCtxt(URL); |
| 1196 | else { |
| 1197 | ctxt = xmlNanoHTTPNewCtxt(redirURL); |
Daniel Veillard | a840b69 | 2003-10-19 13:35:37 +0000 | [diff] [blame] | 1198 | ctxt->location = xmlMemStrdup(redirURL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1199 | } |
| 1200 | |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1201 | if ( ctxt == NULL ) { |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1202 | return ( NULL ); |
| 1203 | } |
| 1204 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1205 | if ((ctxt->protocol == NULL) || (strcmp(ctxt->protocol, "http"))) { |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 1206 | __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] | 1207 | xmlNanoHTTPFreeCtxt(ctxt); |
| 1208 | if (redirURL != NULL) xmlFree(redirURL); |
| 1209 | return(NULL); |
| 1210 | } |
| 1211 | if (ctxt->hostname == NULL) { |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 1212 | __xmlIOErr(XML_FROM_HTTP, XML_HTTP_UNKNOWN_HOST, |
| 1213 | "Failed to identify host in URI"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1214 | xmlNanoHTTPFreeCtxt(ctxt); |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 1215 | if (redirURL != NULL) xmlFree(redirURL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1216 | return(NULL); |
| 1217 | } |
| 1218 | if (proxy) { |
| 1219 | blen = strlen(ctxt->hostname) * 2 + 16; |
| 1220 | ret = xmlNanoHTTPConnectHost(proxy, proxyPort); |
| 1221 | } |
| 1222 | else { |
| 1223 | blen = strlen(ctxt->hostname); |
| 1224 | ret = xmlNanoHTTPConnectHost(ctxt->hostname, ctxt->port); |
| 1225 | } |
| 1226 | if (ret < 0) { |
| 1227 | xmlNanoHTTPFreeCtxt(ctxt); |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 1228 | if (redirURL != NULL) xmlFree(redirURL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1229 | return(NULL); |
| 1230 | } |
| 1231 | ctxt->fd = ret; |
| 1232 | |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1233 | if (input == NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1234 | ilen = 0; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1235 | else |
| 1236 | blen += 36; |
| 1237 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1238 | if (headers != NULL) |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1239 | blen += strlen(headers) + 2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1240 | if (contentType && *contentType) |
| 1241 | blen += strlen(*contentType) + 16; |
Daniel Veillard | 351f2d6 | 2005-04-13 02:55:12 +0000 | [diff] [blame] | 1242 | if (ctxt->query != NULL) |
| 1243 | blen += strlen(ctxt->query) + 1; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1244 | blen += strlen(method) + strlen(ctxt->path) + 24; |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 1245 | bp = (char*)xmlMallocAtomic(blen); |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1246 | if ( bp == NULL ) { |
| 1247 | xmlNanoHTTPFreeCtxt( ctxt ); |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 1248 | xmlHTTPErrMemory("allocating header buffer"); |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1249 | return ( NULL ); |
| 1250 | } |
| 1251 | |
| 1252 | p = bp; |
| 1253 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1254 | if (proxy) { |
| 1255 | if (ctxt->port != 80) { |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 1256 | p += snprintf( p, blen - (p - bp), "%s http://%s:%d%s", |
| 1257 | method, ctxt->hostname, |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1258 | ctxt->port, ctxt->path ); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1259 | } |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 1260 | else |
| 1261 | p += snprintf( p, blen - (p - bp), "%s http://%s%s", method, |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1262 | ctxt->hostname, ctxt->path); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1263 | } |
| 1264 | else |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 1265 | p += snprintf( p, blen - (p - bp), "%s %s", method, ctxt->path); |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1266 | |
Daniel Veillard | 351f2d6 | 2005-04-13 02:55:12 +0000 | [diff] [blame] | 1267 | if (ctxt->query != NULL) |
| 1268 | p += snprintf( p, blen - (p - bp), "?%s", ctxt->query); |
| 1269 | |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 1270 | p += snprintf( p, blen - (p - bp), " HTTP/1.0\r\nHost: %s\r\n", |
| 1271 | ctxt->hostname); |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1272 | |
| 1273 | if (contentType != NULL && *contentType) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 1274 | p += snprintf(p, blen - (p - bp), "Content-Type: %s\r\n", *contentType); |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1275 | |
| 1276 | if (headers != NULL) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 1277 | p += snprintf( p, blen - (p - bp), "%s", headers ); |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1278 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1279 | if (input != NULL) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 1280 | 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] | 1281 | else |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 1282 | snprintf(p, blen - (p - bp), "\r\n"); |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1283 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1284 | #ifdef DEBUG_HTTP |
| 1285 | xmlGenericError(xmlGenericErrorContext, |
| 1286 | "-> %s%s", proxy? "(Proxy) " : "", bp); |
| 1287 | if ((blen -= strlen(bp)+1) < 0) |
| 1288 | xmlGenericError(xmlGenericErrorContext, |
| 1289 | "ERROR: overflowed buffer by %d bytes\n", -blen); |
| 1290 | #endif |
| 1291 | ctxt->outptr = ctxt->out = bp; |
| 1292 | ctxt->state = XML_NANO_HTTP_WRITE; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1293 | blen = strlen( ctxt->out ); |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1294 | #ifdef DEBUG_HTTP |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 1295 | xmt_bytes = xmlNanoHTTPSend(ctxt, ctxt->out, blen ); |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1296 | if ( xmt_bytes != blen ) |
| 1297 | xmlGenericError( xmlGenericErrorContext, |
| 1298 | "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n", |
| 1299 | xmt_bytes, blen, |
| 1300 | "bytes of HTTP headers sent to host", |
| 1301 | ctxt->hostname ); |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 1302 | #else |
| 1303 | xmlNanoHTTPSend(ctxt, ctxt->out, blen ); |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1304 | #endif |
| 1305 | |
| 1306 | if ( input != NULL ) { |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 1307 | #ifdef DEBUG_HTTP |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1308 | xmt_bytes = xmlNanoHTTPSend( ctxt, input, ilen ); |
| 1309 | |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1310 | if ( xmt_bytes != ilen ) |
| 1311 | xmlGenericError( xmlGenericErrorContext, |
| 1312 | "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n", |
| 1313 | xmt_bytes, ilen, |
| 1314 | "bytes of HTTP content sent to host", |
| 1315 | ctxt->hostname ); |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 1316 | #else |
| 1317 | xmlNanoHTTPSend( ctxt, input, ilen ); |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1318 | #endif |
| 1319 | } |
| 1320 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1321 | ctxt->state = XML_NANO_HTTP_READ; |
| 1322 | head = 1; |
| 1323 | |
| 1324 | while ((p = xmlNanoHTTPReadLine(ctxt)) != NULL) { |
| 1325 | if (head && (*p == 0)) { |
| 1326 | head = 0; |
| 1327 | ctxt->content = ctxt->inrptr; |
| 1328 | xmlFree(p); |
| 1329 | break; |
| 1330 | } |
| 1331 | xmlNanoHTTPScanAnswer(ctxt, p); |
| 1332 | |
| 1333 | #ifdef DEBUG_HTTP |
| 1334 | xmlGenericError(xmlGenericErrorContext, "<- %s\n", p); |
| 1335 | #endif |
| 1336 | xmlFree(p); |
| 1337 | } |
| 1338 | |
| 1339 | if ((ctxt->location != NULL) && (ctxt->returnValue >= 300) && |
| 1340 | (ctxt->returnValue < 400)) { |
| 1341 | #ifdef DEBUG_HTTP |
| 1342 | xmlGenericError(xmlGenericErrorContext, |
| 1343 | "\nRedirect to: %s\n", ctxt->location); |
| 1344 | #endif |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1345 | while ( xmlNanoHTTPRecv(ctxt) > 0 ) ; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1346 | if (nbRedirects < XML_NANO_HTTP_MAX_REDIR) { |
| 1347 | nbRedirects++; |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 1348 | if (redirURL != NULL) |
| 1349 | xmlFree(redirURL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1350 | redirURL = xmlMemStrdup(ctxt->location); |
| 1351 | xmlNanoHTTPFreeCtxt(ctxt); |
| 1352 | goto retry; |
| 1353 | } |
| 1354 | xmlNanoHTTPFreeCtxt(ctxt); |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 1355 | if (redirURL != NULL) xmlFree(redirURL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1356 | #ifdef DEBUG_HTTP |
| 1357 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1358 | "xmlNanoHTTPMethodRedir: Too many redirects, aborting ...\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1359 | #endif |
| 1360 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1361 | } |
| 1362 | |
| 1363 | if (contentType != NULL) { |
| 1364 | if (ctxt->contentType != NULL) |
| 1365 | *contentType = xmlMemStrdup(ctxt->contentType); |
| 1366 | else |
| 1367 | *contentType = NULL; |
| 1368 | } |
| 1369 | |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 1370 | if ((redir != NULL) && (redirURL != NULL)) { |
| 1371 | *redir = redirURL; |
| 1372 | } else { |
| 1373 | if (redirURL != NULL) |
| 1374 | xmlFree(redirURL); |
| 1375 | if (redir != NULL) |
| 1376 | *redir = NULL; |
| 1377 | } |
| 1378 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1379 | #ifdef DEBUG_HTTP |
| 1380 | if (ctxt->contentType != NULL) |
| 1381 | xmlGenericError(xmlGenericErrorContext, |
| 1382 | "\nCode %d, content-type '%s'\n\n", |
| 1383 | ctxt->returnValue, ctxt->contentType); |
| 1384 | else |
| 1385 | xmlGenericError(xmlGenericErrorContext, |
| 1386 | "\nCode %d, no content-type\n\n", |
| 1387 | ctxt->returnValue); |
| 1388 | #endif |
| 1389 | |
| 1390 | return((void *) ctxt); |
| 1391 | } |
| 1392 | |
| 1393 | /** |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 1394 | * xmlNanoHTTPMethod: |
| 1395 | * @URL: The URL to load |
| 1396 | * @method: the HTTP method to use |
| 1397 | * @input: the input string if any |
| 1398 | * @contentType: the Content-Type information IN and OUT |
| 1399 | * @headers: the extra headers |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1400 | * @ilen: input length |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 1401 | * |
| 1402 | * This function try to open a connection to the indicated resource |
| 1403 | * via HTTP using the given @method, adding the given extra headers |
| 1404 | * and the input buffer for the request content. |
| 1405 | * |
| 1406 | * Returns NULL in case of failure, otherwise a request handler. |
| 1407 | * The contentType, if provided must be freed by the caller |
| 1408 | */ |
| 1409 | |
| 1410 | void* |
| 1411 | xmlNanoHTTPMethod(const char *URL, const char *method, const char *input, |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1412 | char **contentType, const char *headers, int ilen) { |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 1413 | return(xmlNanoHTTPMethodRedir(URL, method, input, contentType, |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1414 | NULL, headers, ilen)); |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 1415 | } |
| 1416 | |
| 1417 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1418 | * xmlNanoHTTPFetch: |
| 1419 | * @URL: The URL to load |
| 1420 | * @filename: the filename where the content should be saved |
| 1421 | * @contentType: if available the Content-Type information will be |
| 1422 | * returned at that location |
| 1423 | * |
| 1424 | * This function try to fetch the indicated resource via HTTP GET |
| 1425 | * and save it's content in the file. |
| 1426 | * |
| 1427 | * Returns -1 in case of failure, 0 incase of success. The contentType, |
| 1428 | * if provided must be freed by the caller |
| 1429 | */ |
| 1430 | int |
| 1431 | xmlNanoHTTPFetch(const char *URL, const char *filename, char **contentType) { |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1432 | void *ctxt = NULL; |
| 1433 | char *buf = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1434 | int fd; |
| 1435 | int len; |
| 1436 | |
William M. Brack | 015ccb2 | 2005-02-13 08:18:52 +0000 | [diff] [blame] | 1437 | if (filename == NULL) return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1438 | ctxt = xmlNanoHTTPOpen(URL, contentType); |
| 1439 | if (ctxt == NULL) return(-1); |
| 1440 | |
| 1441 | if (!strcmp(filename, "-")) |
| 1442 | fd = 0; |
| 1443 | else { |
| 1444 | fd = open(filename, O_CREAT | O_WRONLY, 00644); |
| 1445 | if (fd < 0) { |
| 1446 | xmlNanoHTTPClose(ctxt); |
| 1447 | if ((contentType != NULL) && (*contentType != NULL)) { |
| 1448 | xmlFree(*contentType); |
| 1449 | *contentType = NULL; |
| 1450 | } |
| 1451 | return(-1); |
| 1452 | } |
| 1453 | } |
| 1454 | |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1455 | xmlNanoHTTPFetchContent( ctxt, &buf, &len ); |
| 1456 | if ( len > 0 ) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1457 | write(fd, buf, len); |
| 1458 | } |
| 1459 | |
| 1460 | xmlNanoHTTPClose(ctxt); |
| 1461 | close(fd); |
| 1462 | return(0); |
| 1463 | } |
| 1464 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1465 | #ifdef LIBXML_OUTPUT_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1466 | /** |
| 1467 | * xmlNanoHTTPSave: |
| 1468 | * @ctxt: the HTTP context |
| 1469 | * @filename: the filename where the content should be saved |
| 1470 | * |
| 1471 | * This function saves the output of the HTTP transaction to a file |
| 1472 | * It closes and free the context at the end |
| 1473 | * |
| 1474 | * Returns -1 in case of failure, 0 incase of success. |
| 1475 | */ |
| 1476 | int |
| 1477 | xmlNanoHTTPSave(void *ctxt, const char *filename) { |
Daniel Veillard | e392497 | 2001-07-25 20:25:21 +0000 | [diff] [blame] | 1478 | char *buf = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1479 | int fd; |
| 1480 | int len; |
| 1481 | |
William M. Brack | 015ccb2 | 2005-02-13 08:18:52 +0000 | [diff] [blame] | 1482 | if ((ctxt == NULL) || (filename == NULL)) return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1483 | |
| 1484 | if (!strcmp(filename, "-")) |
| 1485 | fd = 0; |
| 1486 | else { |
| 1487 | fd = open(filename, O_CREAT | O_WRONLY); |
| 1488 | if (fd < 0) { |
| 1489 | xmlNanoHTTPClose(ctxt); |
| 1490 | return(-1); |
| 1491 | } |
| 1492 | } |
| 1493 | |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1494 | xmlNanoHTTPFetchContent( ctxt, &buf, &len ); |
| 1495 | if ( len > 0 ) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1496 | write(fd, buf, len); |
| 1497 | } |
| 1498 | |
| 1499 | xmlNanoHTTPClose(ctxt); |
William M. Brack | 20d8236 | 2004-03-17 08:44:46 +0000 | [diff] [blame] | 1500 | close(fd); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1501 | return(0); |
| 1502 | } |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1503 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1504 | |
| 1505 | /** |
| 1506 | * xmlNanoHTTPReturnCode: |
| 1507 | * @ctx: the HTTP context |
| 1508 | * |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1509 | * Get the latest HTTP return code received |
| 1510 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1511 | * Returns the HTTP return code for the request. |
| 1512 | */ |
| 1513 | int |
| 1514 | xmlNanoHTTPReturnCode(void *ctx) { |
| 1515 | xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx; |
| 1516 | |
| 1517 | if (ctxt == NULL) return(-1); |
| 1518 | |
| 1519 | return(ctxt->returnValue); |
| 1520 | } |
| 1521 | |
| 1522 | /** |
| 1523 | * xmlNanoHTTPAuthHeader: |
| 1524 | * @ctx: the HTTP context |
| 1525 | * |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1526 | * Get the authentication header of an HTTP context |
| 1527 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1528 | * Returns the stashed value of the WWW-Authenticate or Proxy-Authenticate |
| 1529 | * header. |
| 1530 | */ |
| 1531 | const char * |
| 1532 | xmlNanoHTTPAuthHeader(void *ctx) { |
| 1533 | xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx; |
| 1534 | |
| 1535 | if (ctxt == NULL) return(NULL); |
| 1536 | |
| 1537 | return(ctxt->authHeader); |
| 1538 | } |
| 1539 | |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1540 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 1541 | * xmlNanoHTTPContentLength: |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1542 | * @ctx: the HTTP context |
| 1543 | * |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 1544 | * Provides the specified content length from the HTTP header. |
| 1545 | * |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1546 | * Return the specified content length from the HTTP header. Note that |
| 1547 | * a value of -1 indicates that the content length element was not included in |
| 1548 | * the response header. |
| 1549 | */ |
| 1550 | int |
| 1551 | xmlNanoHTTPContentLength( void * ctx ) { |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 1552 | xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1553 | |
| 1554 | return ( ( ctxt == NULL ) ? -1 : ctxt->ContentLength ); |
| 1555 | } |
| 1556 | |
| 1557 | /** |
Daniel Veillard | 847332a | 2003-10-18 11:29:40 +0000 | [diff] [blame] | 1558 | * xmlNanoHTTPRedir: |
| 1559 | * @ctx: the HTTP context |
| 1560 | * |
| 1561 | * Provides the specified redirection URL if available from the HTTP header. |
| 1562 | * |
| 1563 | * Return the specified redirection URL or NULL if not redirected. |
| 1564 | */ |
| 1565 | const char * |
| 1566 | xmlNanoHTTPRedir( void * ctx ) { |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 1567 | xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx; |
Daniel Veillard | 847332a | 2003-10-18 11:29:40 +0000 | [diff] [blame] | 1568 | |
| 1569 | return ( ( ctxt == NULL ) ? NULL : ctxt->location ); |
| 1570 | } |
| 1571 | |
| 1572 | /** |
| 1573 | * xmlNanoHTTPEncoding: |
| 1574 | * @ctx: the HTTP context |
| 1575 | * |
| 1576 | * Provides the specified encoding if specified in the HTTP headers. |
| 1577 | * |
| 1578 | * Return the specified encoding or NULL if not available |
| 1579 | */ |
| 1580 | const char * |
| 1581 | xmlNanoHTTPEncoding( void * ctx ) { |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 1582 | xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx; |
Daniel Veillard | 847332a | 2003-10-18 11:29:40 +0000 | [diff] [blame] | 1583 | |
| 1584 | return ( ( ctxt == NULL ) ? NULL : ctxt->encoding ); |
| 1585 | } |
| 1586 | |
| 1587 | /** |
Daniel Veillard | a840b69 | 2003-10-19 13:35:37 +0000 | [diff] [blame] | 1588 | * xmlNanoHTTPMimeType: |
| 1589 | * @ctx: the HTTP context |
| 1590 | * |
| 1591 | * Provides the specified Mime-Type if specified in the HTTP headers. |
| 1592 | * |
| 1593 | * Return the specified Mime-Type or NULL if not available |
| 1594 | */ |
| 1595 | const char * |
| 1596 | xmlNanoHTTPMimeType( void * ctx ) { |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 1597 | xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx; |
Daniel Veillard | a840b69 | 2003-10-19 13:35:37 +0000 | [diff] [blame] | 1598 | |
| 1599 | return ( ( ctxt == NULL ) ? NULL : ctxt->mimeType ); |
| 1600 | } |
| 1601 | |
| 1602 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 1603 | * xmlNanoHTTPFetchContent: |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1604 | * @ctx: the HTTP context |
| 1605 | * @ptr: pointer to set to the content buffer. |
| 1606 | * @len: integer pointer to hold the length of the content |
| 1607 | * |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 1608 | * Check if all the content was read |
| 1609 | * |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1610 | * Returns 0 if all the content was read and available, returns |
| 1611 | * -1 if received content length was less than specified or an error |
| 1612 | * occurred. |
| 1613 | */ |
Daniel Veillard | a235132 | 2004-06-27 12:08:10 +0000 | [diff] [blame] | 1614 | static int |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1615 | xmlNanoHTTPFetchContent( void * ctx, char ** ptr, int * len ) { |
Daniel Veillard | 82cb319 | 2003-10-29 13:39:15 +0000 | [diff] [blame] | 1616 | xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr)ctx; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1617 | |
| 1618 | int rc = 0; |
| 1619 | int cur_lgth; |
| 1620 | int rcvd_lgth; |
| 1621 | int dummy_int; |
| 1622 | char * dummy_ptr = NULL; |
| 1623 | |
| 1624 | /* Dummy up return input parameters if not provided */ |
| 1625 | |
| 1626 | if ( len == NULL ) |
| 1627 | len = &dummy_int; |
| 1628 | |
| 1629 | if ( ptr == NULL ) |
| 1630 | ptr = &dummy_ptr; |
| 1631 | |
| 1632 | /* But can't work without the context pointer */ |
| 1633 | |
| 1634 | if ( ( ctxt == NULL ) || ( ctxt->content == NULL ) ) { |
| 1635 | *len = 0; |
| 1636 | *ptr = NULL; |
| 1637 | return ( -1 ); |
| 1638 | } |
| 1639 | |
| 1640 | rcvd_lgth = ctxt->inptr - ctxt->content; |
| 1641 | |
| 1642 | while ( (cur_lgth = xmlNanoHTTPRecv( ctxt )) > 0 ) { |
| 1643 | |
| 1644 | rcvd_lgth += cur_lgth; |
| 1645 | if ( (ctxt->ContentLength > 0) && (rcvd_lgth >= ctxt->ContentLength) ) |
| 1646 | break; |
| 1647 | } |
| 1648 | |
| 1649 | *ptr = ctxt->content; |
| 1650 | *len = rcvd_lgth; |
| 1651 | |
| 1652 | if ( ( ctxt->ContentLength > 0 ) && ( rcvd_lgth < ctxt->ContentLength ) ) |
| 1653 | rc = -1; |
| 1654 | else if ( rcvd_lgth == 0 ) |
| 1655 | rc = -1; |
| 1656 | |
| 1657 | return ( rc ); |
| 1658 | } |
| 1659 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1660 | #ifdef STANDALONE |
| 1661 | int main(int argc, char **argv) { |
| 1662 | char *contentType = NULL; |
| 1663 | |
| 1664 | if (argv[1] != NULL) { |
| 1665 | if (argv[2] != NULL) |
| 1666 | xmlNanoHTTPFetch(argv[1], argv[2], &contentType); |
| 1667 | else |
| 1668 | xmlNanoHTTPFetch(argv[1], "-", &contentType); |
| 1669 | if (contentType != NULL) xmlFree(contentType); |
| 1670 | } else { |
| 1671 | xmlGenericError(xmlGenericErrorContext, |
| 1672 | "%s: minimal HTTP GET implementation\n", argv[0]); |
| 1673 | xmlGenericError(xmlGenericErrorContext, |
| 1674 | "\tusage %s [ URL [ filename ] ]\n", argv[0]); |
| 1675 | } |
| 1676 | xmlNanoHTTPCleanup(); |
| 1677 | xmlMemoryDump(); |
| 1678 | return(0); |
| 1679 | } |
| 1680 | #endif /* STANDALONE */ |
| 1681 | #else /* !LIBXML_HTTP_ENABLED */ |
| 1682 | #ifdef STANDALONE |
| 1683 | #include <stdio.h> |
| 1684 | int main(int argc, char **argv) { |
| 1685 | xmlGenericError(xmlGenericErrorContext, |
| 1686 | "%s : HTTP support not compiled in\n", argv[0]); |
| 1687 | return(0); |
| 1688 | } |
| 1689 | #endif /* STANDALONE */ |
| 1690 | #endif /* LIBXML_HTTP_ENABLED */ |
Daniel Veillard | 5d4644e | 2005-04-01 13:11:58 +0000 | [diff] [blame] | 1691 | #define bottom_nanohttp |
| 1692 | #include "elfgcchack.h" |