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