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