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