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