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 |
| 30 | #ifdef HAVE_SYS_SOCKET_H |
| 31 | #include <sys/socket.h> |
| 32 | #endif |
| 33 | #ifdef HAVE_NETINET_IN_H |
| 34 | #include <netinet/in.h> |
| 35 | #endif |
| 36 | #ifdef HAVE_ARPA_INET_H |
| 37 | #include <arpa/inet.h> |
| 38 | #endif |
| 39 | #ifdef HAVE_NETDB_H |
| 40 | #include <netdb.h> |
| 41 | #endif |
Daniel Veillard | d85f4f4 | 2002-03-25 10:48:46 +0000 | [diff] [blame^] | 42 | #ifdef HAVE_RESOLV_H |
| 43 | #include <resolv.h> |
| 44 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 45 | #ifdef HAVE_FCNTL_H |
| 46 | #include <fcntl.h> |
| 47 | #endif |
| 48 | #ifdef HAVE_ERRNO_H |
| 49 | #include <errno.h> |
| 50 | #endif |
| 51 | #ifdef HAVE_SYS_TIME_H |
| 52 | #include <sys/time.h> |
| 53 | #endif |
| 54 | #ifdef HAVE_SYS_SELECT_H |
| 55 | #include <sys/select.h> |
| 56 | #endif |
| 57 | #ifdef HAVE_STRINGS_H |
| 58 | #include <strings.h> |
| 59 | #endif |
| 60 | #ifdef SUPPORT_IP6 |
| 61 | #include <resolv.h> |
| 62 | #endif |
| 63 | |
| 64 | #ifdef VMS |
| 65 | #include <stropts> |
| 66 | #define SOCKLEN_T unsigned int |
| 67 | #define SOCKET int |
| 68 | #endif |
| 69 | |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 70 | #include <libxml/globals.h> |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 71 | #include <libxml/xmlerror.h> |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 72 | #include <libxml/xmlmemory.h> |
| 73 | #include <libxml/parser.h> /* for xmlStr(n)casecmp() */ |
| 74 | #include <libxml/nanohttp.h> |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 75 | #include <libxml/globals.h> |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 76 | |
| 77 | /** |
| 78 | * A couple portability macros |
| 79 | */ |
| 80 | #ifndef _WINSOCKAPI_ |
| 81 | #define closesocket(s) close(s) |
| 82 | #define SOCKET int |
| 83 | #endif |
| 84 | |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 85 | #ifndef SOCKLEN_T |
| 86 | #define SOCKLEN_T unsigned int |
| 87 | #endif |
| 88 | #ifndef SOCKET |
| 89 | #define SOCKET int |
| 90 | #endif |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 91 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 92 | #ifdef STANDALONE |
| 93 | #define DEBUG_HTTP |
| 94 | #define xmlStrncasecmp(a, b, n) strncasecmp((char *)a, (char *)b, n) |
| 95 | #define xmlStrcasecmpi(a, b) strcasecmp((char *)a, (char *)b) |
| 96 | #endif |
| 97 | |
| 98 | #define XML_NANO_HTTP_MAX_REDIR 10 |
| 99 | |
| 100 | #define XML_NANO_HTTP_CHUNK 4096 |
| 101 | |
| 102 | #define XML_NANO_HTTP_CLOSED 0 |
| 103 | #define XML_NANO_HTTP_WRITE 1 |
| 104 | #define XML_NANO_HTTP_READ 2 |
| 105 | #define XML_NANO_HTTP_NONE 4 |
| 106 | |
| 107 | typedef struct xmlNanoHTTPCtxt { |
| 108 | char *protocol; /* the protocol name */ |
| 109 | char *hostname; /* the host name */ |
| 110 | int port; /* the port */ |
| 111 | char *path; /* the path within the URL */ |
| 112 | SOCKET fd; /* the file descriptor for the socket */ |
| 113 | int state; /* WRITE / READ / CLOSED */ |
| 114 | char *out; /* buffer sent (zero terminated) */ |
| 115 | char *outptr; /* index within the buffer sent */ |
| 116 | char *in; /* the receiving buffer */ |
| 117 | char *content; /* the start of the content */ |
| 118 | char *inptr; /* the next byte to read from network */ |
| 119 | char *inrptr; /* the next byte to give back to the client */ |
| 120 | int inlen; /* len of the input buffer */ |
| 121 | int last; /* return code for last operation */ |
| 122 | int returnValue; /* the protocol return value */ |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 123 | int ContentLength; /* specified content length from HTTP header */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 124 | char *contentType; /* the MIME type for the input */ |
| 125 | char *location; /* the new URL in case of redirect */ |
| 126 | char *authHeader; /* contents of {WWW,Proxy}-Authenticate header */ |
| 127 | } xmlNanoHTTPCtxt, *xmlNanoHTTPCtxtPtr; |
| 128 | |
| 129 | static int initialized = 0; |
| 130 | static char *proxy = NULL; /* the proxy name if any */ |
| 131 | static int proxyPort; /* the proxy port if any */ |
| 132 | static unsigned int timeout = 60;/* the select() timeout in seconds */ |
| 133 | |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 134 | int xmlNanoHTTPFetchContent( void * ctx, char ** ptr, int * len ); |
| 135 | int xmlNanoHTTPContentLength( void * ctx ); |
| 136 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 137 | /** |
| 138 | * A portability function |
| 139 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 140 | static int socket_errno(void) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 141 | #ifdef _WINSOCKAPI_ |
| 142 | return(WSAGetLastError()); |
| 143 | #else |
| 144 | return(errno); |
| 145 | #endif |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * xmlNanoHTTPInit: |
| 150 | * |
| 151 | * Initialize the HTTP protocol layer. |
| 152 | * Currently it just checks for proxy informations |
| 153 | */ |
| 154 | |
| 155 | void |
| 156 | xmlNanoHTTPInit(void) { |
| 157 | const char *env; |
| 158 | #ifdef _WINSOCKAPI_ |
| 159 | WSADATA wsaData; |
| 160 | #endif |
| 161 | |
| 162 | if (initialized) |
| 163 | return; |
| 164 | |
| 165 | #ifdef _WINSOCKAPI_ |
| 166 | if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) |
| 167 | return; |
| 168 | #endif |
| 169 | |
| 170 | if (proxy == NULL) { |
| 171 | proxyPort = 80; |
| 172 | env = getenv("no_proxy"); |
| 173 | if (env != NULL) |
| 174 | goto done; |
| 175 | env = getenv("http_proxy"); |
| 176 | if (env != NULL) { |
| 177 | xmlNanoHTTPScanProxy(env); |
| 178 | goto done; |
| 179 | } |
| 180 | env = getenv("HTTP_PROXY"); |
| 181 | if (env != NULL) { |
| 182 | xmlNanoHTTPScanProxy(env); |
| 183 | goto done; |
| 184 | } |
| 185 | } |
| 186 | done: |
| 187 | initialized = 1; |
| 188 | } |
| 189 | |
| 190 | /** |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 191 | * xmlNanoHTTPCleanup: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 192 | * |
| 193 | * Cleanup the HTTP protocol layer. |
| 194 | */ |
| 195 | |
| 196 | void |
| 197 | xmlNanoHTTPCleanup(void) { |
| 198 | if (proxy != NULL) |
| 199 | xmlFree(proxy); |
| 200 | #ifdef _WINSOCKAPI_ |
| 201 | if (initialized) |
| 202 | WSACleanup(); |
| 203 | #endif |
| 204 | initialized = 0; |
| 205 | return; |
| 206 | } |
| 207 | |
| 208 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 209 | * xmlNanoHTTPScanURL: |
| 210 | * @ctxt: an HTTP context |
| 211 | * @URL: The URL used to initialize the context |
| 212 | * |
| 213 | * (Re)Initialize an HTTP context by parsing the URL and finding |
| 214 | * the protocol host port and path it indicates. |
| 215 | */ |
| 216 | |
| 217 | static void |
| 218 | xmlNanoHTTPScanURL(xmlNanoHTTPCtxtPtr ctxt, const char *URL) { |
| 219 | const char *cur = URL; |
| 220 | char buf[4096]; |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 221 | int indx = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 222 | int port = 0; |
| 223 | |
| 224 | if (ctxt->protocol != NULL) { |
| 225 | xmlFree(ctxt->protocol); |
| 226 | ctxt->protocol = NULL; |
| 227 | } |
| 228 | if (ctxt->hostname != NULL) { |
| 229 | xmlFree(ctxt->hostname); |
| 230 | ctxt->hostname = NULL; |
| 231 | } |
| 232 | if (ctxt->path != NULL) { |
| 233 | xmlFree(ctxt->path); |
| 234 | ctxt->path = NULL; |
| 235 | } |
| 236 | if (URL == NULL) return; |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 237 | buf[indx] = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 238 | while (*cur != 0) { |
| 239 | if ((cur[0] == ':') && (cur[1] == '/') && (cur[2] == '/')) { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 240 | buf[indx] = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 241 | ctxt->protocol = xmlMemStrdup(buf); |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 242 | indx = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 243 | cur += 3; |
| 244 | break; |
| 245 | } |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 246 | buf[indx++] = *cur++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 247 | } |
| 248 | if (*cur == 0) return; |
| 249 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 250 | buf[indx] = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 251 | while (1) { |
| 252 | if (cur[0] == ':') { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 253 | buf[indx] = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 254 | ctxt->hostname = xmlMemStrdup(buf); |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 255 | indx = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 256 | cur += 1; |
| 257 | while ((*cur >= '0') && (*cur <= '9')) { |
| 258 | port *= 10; |
| 259 | port += *cur - '0'; |
| 260 | cur++; |
| 261 | } |
| 262 | if (port != 0) ctxt->port = port; |
| 263 | while ((cur[0] != '/') && (*cur != 0)) |
| 264 | cur++; |
| 265 | break; |
| 266 | } |
| 267 | if ((*cur == '/') || (*cur == 0)) { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 268 | buf[indx] = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 269 | ctxt->hostname = xmlMemStrdup(buf); |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 270 | indx = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 271 | break; |
| 272 | } |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 273 | buf[indx++] = *cur++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 274 | } |
| 275 | if (*cur == 0) |
| 276 | ctxt->path = xmlMemStrdup("/"); |
| 277 | else { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 278 | indx = 0; |
| 279 | buf[indx] = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 280 | while (*cur != 0) |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 281 | buf[indx++] = *cur++; |
| 282 | buf[indx] = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 283 | ctxt->path = xmlMemStrdup(buf); |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * xmlNanoHTTPScanProxy: |
| 289 | * @URL: The proxy URL used to initialize the proxy context |
| 290 | * |
| 291 | * (Re)Initialize the HTTP Proxy context by parsing the URL and finding |
| 292 | * the protocol host port it indicates. |
| 293 | * Should be like http://myproxy/ or http://myproxy:3128/ |
| 294 | * A NULL URL cleans up proxy informations. |
| 295 | */ |
| 296 | |
| 297 | void |
| 298 | xmlNanoHTTPScanProxy(const char *URL) { |
| 299 | const char *cur = URL; |
| 300 | char buf[4096]; |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 301 | int indx = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 302 | int port = 0; |
| 303 | |
| 304 | if (proxy != NULL) { |
| 305 | xmlFree(proxy); |
| 306 | proxy = NULL; |
| 307 | } |
| 308 | if (proxyPort != 0) { |
| 309 | proxyPort = 0; |
| 310 | } |
| 311 | #ifdef DEBUG_HTTP |
| 312 | if (URL == NULL) |
| 313 | xmlGenericError(xmlGenericErrorContext, |
| 314 | "Removing HTTP proxy info\n"); |
| 315 | else |
| 316 | xmlGenericError(xmlGenericErrorContext, |
| 317 | "Using HTTP proxy %s\n", URL); |
| 318 | #endif |
| 319 | if (URL == NULL) return; |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 320 | buf[indx] = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 321 | while (*cur != 0) { |
| 322 | if ((cur[0] == ':') && (cur[1] == '/') && (cur[2] == '/')) { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 323 | buf[indx] = 0; |
| 324 | indx = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 325 | cur += 3; |
| 326 | break; |
| 327 | } |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 328 | buf[indx++] = *cur++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 329 | } |
| 330 | if (*cur == 0) return; |
| 331 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 332 | buf[indx] = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 333 | while (1) { |
| 334 | if (cur[0] == ':') { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 335 | buf[indx] = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 336 | proxy = xmlMemStrdup(buf); |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 337 | indx = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 338 | cur += 1; |
| 339 | while ((*cur >= '0') && (*cur <= '9')) { |
| 340 | port *= 10; |
| 341 | port += *cur - '0'; |
| 342 | cur++; |
| 343 | } |
| 344 | if (port != 0) proxyPort = port; |
| 345 | while ((cur[0] != '/') && (*cur != 0)) |
| 346 | cur++; |
| 347 | break; |
| 348 | } |
| 349 | if ((*cur == '/') || (*cur == 0)) { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 350 | buf[indx] = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 351 | proxy = xmlMemStrdup(buf); |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 352 | indx = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 353 | break; |
| 354 | } |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 355 | buf[indx++] = *cur++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 356 | } |
| 357 | } |
| 358 | |
| 359 | /** |
| 360 | * xmlNanoHTTPNewCtxt: |
| 361 | * @URL: The URL used to initialize the context |
| 362 | * |
| 363 | * Allocate and initialize a new HTTP context. |
| 364 | * |
| 365 | * Returns an HTTP context or NULL in case of error. |
| 366 | */ |
| 367 | |
| 368 | static xmlNanoHTTPCtxtPtr |
| 369 | xmlNanoHTTPNewCtxt(const char *URL) { |
| 370 | xmlNanoHTTPCtxtPtr ret; |
| 371 | |
| 372 | ret = (xmlNanoHTTPCtxtPtr) xmlMalloc(sizeof(xmlNanoHTTPCtxt)); |
| 373 | if (ret == NULL) return(NULL); |
| 374 | |
| 375 | memset(ret, 0, sizeof(xmlNanoHTTPCtxt)); |
| 376 | ret->port = 80; |
| 377 | ret->returnValue = 0; |
| 378 | ret->fd = -1; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 379 | ret->ContentLength = -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 380 | |
| 381 | xmlNanoHTTPScanURL(ret, URL); |
| 382 | |
| 383 | return(ret); |
| 384 | } |
| 385 | |
| 386 | /** |
| 387 | * xmlNanoHTTPFreeCtxt: |
| 388 | * @ctxt: an HTTP context |
| 389 | * |
| 390 | * Frees the context after closing the connection. |
| 391 | */ |
| 392 | |
| 393 | static void |
| 394 | xmlNanoHTTPFreeCtxt(xmlNanoHTTPCtxtPtr ctxt) { |
| 395 | if (ctxt == NULL) return; |
| 396 | if (ctxt->hostname != NULL) xmlFree(ctxt->hostname); |
| 397 | if (ctxt->protocol != NULL) xmlFree(ctxt->protocol); |
| 398 | if (ctxt->path != NULL) xmlFree(ctxt->path); |
| 399 | if (ctxt->out != NULL) xmlFree(ctxt->out); |
| 400 | if (ctxt->in != NULL) xmlFree(ctxt->in); |
| 401 | if (ctxt->contentType != NULL) xmlFree(ctxt->contentType); |
| 402 | if (ctxt->location != NULL) xmlFree(ctxt->location); |
| 403 | if (ctxt->authHeader != NULL) xmlFree(ctxt->authHeader); |
| 404 | ctxt->state = XML_NANO_HTTP_NONE; |
| 405 | if (ctxt->fd >= 0) closesocket(ctxt->fd); |
| 406 | ctxt->fd = -1; |
| 407 | xmlFree(ctxt); |
| 408 | } |
| 409 | |
| 410 | /** |
| 411 | * xmlNanoHTTPSend: |
| 412 | * @ctxt: an HTTP context |
| 413 | * |
| 414 | * Send the input needed to initiate the processing on the server side |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 415 | * Returns number of bytes sent or -1 on error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 416 | */ |
| 417 | |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 418 | static int |
| 419 | xmlNanoHTTPSend(xmlNanoHTTPCtxtPtr ctxt, const char * xmt_ptr, int outlen) { |
| 420 | |
| 421 | int total_sent = 0; |
| 422 | |
| 423 | if ( (ctxt->state & XML_NANO_HTTP_WRITE) && (xmt_ptr != NULL ) ) { |
| 424 | while (total_sent < outlen) { |
| 425 | int nsent = send(ctxt->fd, xmt_ptr + total_sent, |
| 426 | outlen - total_sent, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 427 | if (nsent>0) |
| 428 | total_sent += nsent; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 429 | else if ( ( nsent == -1 ) && |
Daniel Veillard | ba6db03 | 2001-07-31 16:25:45 +0000 | [diff] [blame] | 430 | #if defined(EAGAIN) && EAGAIN != EWOULDBLOCK |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 431 | ( socket_errno( ) != EAGAIN ) && |
Daniel Veillard | ba6db03 | 2001-07-31 16:25:45 +0000 | [diff] [blame] | 432 | #endif |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 433 | ( socket_errno( ) != EWOULDBLOCK ) ) { |
| 434 | xmlGenericError( xmlGenericErrorContext, |
| 435 | "xmlNanoHTTPSend error: %s", |
| 436 | strerror( socket_errno( ) ) ); |
| 437 | |
| 438 | if ( total_sent == 0 ) |
| 439 | total_sent = -1; |
| 440 | break; |
| 441 | } |
| 442 | else { |
| 443 | /* |
| 444 | ** No data sent |
| 445 | ** Since non-blocking sockets are used, wait for |
| 446 | ** socket to be writable or default timeout prior |
| 447 | ** to retrying. |
| 448 | */ |
| 449 | |
| 450 | struct timeval tv; |
| 451 | fd_set wfd; |
| 452 | |
| 453 | tv.tv_sec = timeout; |
| 454 | tv.tv_usec = 0; |
| 455 | FD_ZERO( &wfd ); |
| 456 | FD_SET( ctxt->fd, &wfd ); |
| 457 | (void)select( ctxt->fd + 1, NULL, &wfd, NULL, &tv ); |
| 458 | } |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 459 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 460 | } |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 461 | |
| 462 | return total_sent; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | /** |
| 466 | * xmlNanoHTTPRecv: |
| 467 | * @ctxt: an HTTP context |
| 468 | * |
| 469 | * Read information coming from the HTTP connection. |
| 470 | * This is a blocking call (but it blocks in select(), not read()). |
| 471 | * |
| 472 | * Returns the number of byte read or -1 in case of error. |
| 473 | */ |
| 474 | |
| 475 | static int |
| 476 | xmlNanoHTTPRecv(xmlNanoHTTPCtxtPtr ctxt) { |
| 477 | fd_set rfd; |
| 478 | struct timeval tv; |
| 479 | |
| 480 | |
| 481 | while (ctxt->state & XML_NANO_HTTP_READ) { |
| 482 | if (ctxt->in == NULL) { |
| 483 | ctxt->in = (char *) xmlMalloc(65000 * sizeof(char)); |
| 484 | if (ctxt->in == NULL) { |
| 485 | ctxt->last = -1; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 486 | xmlGenericError( xmlGenericErrorContext, |
| 487 | "xmlNanoHTTPRecv: Error allocating input memory." ); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 488 | return(-1); |
| 489 | } |
| 490 | ctxt->inlen = 65000; |
| 491 | ctxt->inptr = ctxt->content = ctxt->inrptr = ctxt->in; |
| 492 | } |
| 493 | if (ctxt->inrptr > ctxt->in + XML_NANO_HTTP_CHUNK) { |
| 494 | int delta = ctxt->inrptr - ctxt->in; |
| 495 | int len = ctxt->inptr - ctxt->inrptr; |
| 496 | |
| 497 | memmove(ctxt->in, ctxt->inrptr, len); |
| 498 | ctxt->inrptr -= delta; |
| 499 | ctxt->content -= delta; |
| 500 | ctxt->inptr -= delta; |
| 501 | } |
| 502 | if ((ctxt->in + ctxt->inlen) < (ctxt->inptr + XML_NANO_HTTP_CHUNK)) { |
| 503 | int d_inptr = ctxt->inptr - ctxt->in; |
| 504 | int d_content = ctxt->content - ctxt->in; |
| 505 | int d_inrptr = ctxt->inrptr - ctxt->in; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 506 | char * tmp_ptr = ctxt->in; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 507 | |
| 508 | ctxt->inlen *= 2; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 509 | ctxt->in = (char *) xmlRealloc(tmp_ptr, ctxt->inlen); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 510 | if (ctxt->in == NULL) { |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 511 | xmlGenericError( xmlGenericErrorContext, |
| 512 | "xmlNanoHTTPRecv: %s %d bytes.", |
| 513 | "Failed to realloc input buffer to", |
| 514 | ctxt->inlen ); |
| 515 | xmlFree( tmp_ptr ); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 516 | ctxt->last = -1; |
| 517 | return(-1); |
| 518 | } |
| 519 | ctxt->inptr = ctxt->in + d_inptr; |
| 520 | ctxt->content = ctxt->in + d_content; |
| 521 | ctxt->inrptr = ctxt->in + d_inrptr; |
| 522 | } |
| 523 | ctxt->last = recv(ctxt->fd, ctxt->inptr, XML_NANO_HTTP_CHUNK, 0); |
| 524 | if (ctxt->last > 0) { |
| 525 | ctxt->inptr += ctxt->last; |
| 526 | return(ctxt->last); |
| 527 | } |
| 528 | if (ctxt->last == 0) { |
| 529 | return(0); |
| 530 | } |
| 531 | if (ctxt->last == -1) { |
| 532 | switch (socket_errno()) { |
| 533 | case EINPROGRESS: |
| 534 | case EWOULDBLOCK: |
| 535 | #if defined(EAGAIN) && EAGAIN != EWOULDBLOCK |
| 536 | case EAGAIN: |
| 537 | #endif |
| 538 | break; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 539 | |
| 540 | case ECONNRESET: |
| 541 | case ESHUTDOWN: |
| 542 | return ( 0 ); |
| 543 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 544 | default: |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 545 | xmlGenericError( xmlGenericErrorContext, |
| 546 | "xmlNanoHTTPRecv: recv( ) failure - %s", |
| 547 | strerror( socket_errno( ) ) ); |
| 548 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 549 | } |
| 550 | } |
| 551 | |
| 552 | tv.tv_sec = timeout; |
| 553 | tv.tv_usec = 0; |
| 554 | FD_ZERO(&rfd); |
| 555 | FD_SET(ctxt->fd, &rfd); |
| 556 | |
Daniel Veillard | 50f3437 | 2001-08-03 12:06:36 +0000 | [diff] [blame] | 557 | if ( (select(ctxt->fd+1, &rfd, NULL, NULL, &tv)<1) |
| 558 | #if defined(EINTR) |
| 559 | && (errno != EINTR) |
| 560 | #endif |
| 561 | ) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 562 | return(0); |
| 563 | } |
| 564 | return(0); |
| 565 | } |
| 566 | |
| 567 | /** |
| 568 | * xmlNanoHTTPReadLine: |
| 569 | * @ctxt: an HTTP context |
| 570 | * |
| 571 | * Read one line in the HTTP server output, usually for extracting |
| 572 | * the HTTP protocol informations from the answer header. |
| 573 | * |
| 574 | * Returns a newly allocated string with a copy of the line, or NULL |
| 575 | * which indicate the end of the input. |
| 576 | */ |
| 577 | |
| 578 | static char * |
| 579 | xmlNanoHTTPReadLine(xmlNanoHTTPCtxtPtr ctxt) { |
| 580 | char buf[4096]; |
| 581 | char *bp = buf; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 582 | int rc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 583 | |
| 584 | while (bp - buf < 4095) { |
| 585 | if (ctxt->inrptr == ctxt->inptr) { |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 586 | if ( (rc = xmlNanoHTTPRecv(ctxt)) == 0) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 587 | if (bp == buf) |
| 588 | return(NULL); |
| 589 | else |
| 590 | *bp = 0; |
| 591 | return(xmlMemStrdup(buf)); |
| 592 | } |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 593 | else if ( rc == -1 ) { |
| 594 | return ( NULL ); |
| 595 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 596 | } |
| 597 | *bp = *ctxt->inrptr++; |
| 598 | if (*bp == '\n') { |
| 599 | *bp = 0; |
| 600 | return(xmlMemStrdup(buf)); |
| 601 | } |
| 602 | if (*bp != '\r') |
| 603 | bp++; |
| 604 | } |
| 605 | buf[4095] = 0; |
| 606 | return(xmlMemStrdup(buf)); |
| 607 | } |
| 608 | |
| 609 | |
| 610 | /** |
| 611 | * xmlNanoHTTPScanAnswer: |
| 612 | * @ctxt: an HTTP context |
| 613 | * @line: an HTTP header line |
| 614 | * |
| 615 | * Try to extract useful informations from the server answer. |
| 616 | * We currently parse and process: |
| 617 | * - The HTTP revision/ return code |
| 618 | * - The Content-Type |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 619 | * - The Location for redirect processing. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 620 | * |
| 621 | * Returns -1 in case of failure, the file descriptor number otherwise |
| 622 | */ |
| 623 | |
| 624 | static void |
| 625 | xmlNanoHTTPScanAnswer(xmlNanoHTTPCtxtPtr ctxt, const char *line) { |
| 626 | const char *cur = line; |
| 627 | |
| 628 | if (line == NULL) return; |
| 629 | |
| 630 | if (!strncmp(line, "HTTP/", 5)) { |
| 631 | int version = 0; |
| 632 | int ret = 0; |
| 633 | |
| 634 | cur += 5; |
| 635 | while ((*cur >= '0') && (*cur <= '9')) { |
| 636 | version *= 10; |
| 637 | version += *cur - '0'; |
| 638 | cur++; |
| 639 | } |
| 640 | if (*cur == '.') { |
| 641 | cur++; |
| 642 | if ((*cur >= '0') && (*cur <= '9')) { |
| 643 | version *= 10; |
| 644 | version += *cur - '0'; |
| 645 | cur++; |
| 646 | } |
| 647 | while ((*cur >= '0') && (*cur <= '9')) |
| 648 | cur++; |
| 649 | } else |
| 650 | version *= 10; |
| 651 | if ((*cur != ' ') && (*cur != '\t')) return; |
| 652 | while ((*cur == ' ') || (*cur == '\t')) cur++; |
| 653 | if ((*cur < '0') || (*cur > '9')) return; |
| 654 | while ((*cur >= '0') && (*cur <= '9')) { |
| 655 | ret *= 10; |
| 656 | ret += *cur - '0'; |
| 657 | cur++; |
| 658 | } |
| 659 | if ((*cur != 0) && (*cur != ' ') && (*cur != '\t')) return; |
| 660 | ctxt->returnValue = ret; |
| 661 | } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Content-Type:", 13)) { |
| 662 | cur += 13; |
| 663 | while ((*cur == ' ') || (*cur == '\t')) cur++; |
| 664 | if (ctxt->contentType != NULL) |
| 665 | xmlFree(ctxt->contentType); |
| 666 | ctxt->contentType = xmlMemStrdup(cur); |
| 667 | } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"ContentType:", 12)) { |
| 668 | cur += 12; |
| 669 | if (ctxt->contentType != NULL) return; |
| 670 | while ((*cur == ' ') || (*cur == '\t')) cur++; |
| 671 | ctxt->contentType = xmlMemStrdup(cur); |
| 672 | } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Location:", 9)) { |
| 673 | cur += 9; |
| 674 | while ((*cur == ' ') || (*cur == '\t')) cur++; |
| 675 | if (ctxt->location != NULL) |
| 676 | xmlFree(ctxt->location); |
| 677 | ctxt->location = xmlMemStrdup(cur); |
| 678 | } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"WWW-Authenticate:", 17)) { |
| 679 | cur += 17; |
| 680 | while ((*cur == ' ') || (*cur == '\t')) cur++; |
| 681 | if (ctxt->authHeader != NULL) |
| 682 | xmlFree(ctxt->authHeader); |
| 683 | ctxt->authHeader = xmlMemStrdup(cur); |
| 684 | } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Proxy-Authenticate:", 19)) { |
| 685 | cur += 19; |
| 686 | while ((*cur == ' ') || (*cur == '\t')) cur++; |
| 687 | if (ctxt->authHeader != NULL) |
| 688 | xmlFree(ctxt->authHeader); |
| 689 | ctxt->authHeader = xmlMemStrdup(cur); |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 690 | } else if ( !xmlStrncasecmp( BAD_CAST line, BAD_CAST"Content-Length:", 15) ) { |
| 691 | cur += 15; |
| 692 | ctxt->ContentLength = strtol( cur, NULL, 10 ); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 693 | } |
| 694 | } |
| 695 | |
| 696 | /** |
| 697 | * xmlNanoHTTPConnectAttempt: |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 698 | * @addr: a socket address structure |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 699 | * |
| 700 | * Attempt a connection to the given IP:port endpoint. It forces |
| 701 | * non-blocking semantic on the socket, and allow 60 seconds for |
| 702 | * the host to answer. |
| 703 | * |
| 704 | * Returns -1 in case of failure, the file descriptor number otherwise |
| 705 | */ |
| 706 | |
| 707 | static int |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 708 | xmlNanoHTTPConnectAttempt(struct sockaddr *addr) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 709 | { |
| 710 | SOCKET s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 711 | fd_set wfd; |
| 712 | struct timeval tv; |
| 713 | int status; |
| 714 | |
| 715 | if (s==-1) { |
| 716 | #ifdef DEBUG_HTTP |
| 717 | perror("socket"); |
| 718 | #endif |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 719 | xmlGenericError( xmlGenericErrorContext, |
| 720 | "xmlNanoHTTPConnectAttempt: %s - %s", |
| 721 | "socket creation failure", |
| 722 | strerror( socket_errno( ) ) ); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 723 | return(-1); |
| 724 | } |
| 725 | |
| 726 | #ifdef _WINSOCKAPI_ |
| 727 | { |
| 728 | u_long one = 1; |
| 729 | |
| 730 | status = ioctlsocket(s, FIONBIO, &one) == SOCKET_ERROR ? -1 : 0; |
| 731 | } |
| 732 | #else /* _WINSOCKAPI_ */ |
| 733 | #if defined(VMS) |
| 734 | { |
| 735 | int enable = 1; |
| 736 | status = ioctl(s, FIONBIO, &enable); |
| 737 | } |
| 738 | #else /* VMS */ |
| 739 | if ((status = fcntl(s, F_GETFL, 0)) != -1) { |
| 740 | #ifdef O_NONBLOCK |
| 741 | status |= O_NONBLOCK; |
| 742 | #else /* O_NONBLOCK */ |
| 743 | #ifdef F_NDELAY |
| 744 | status |= F_NDELAY; |
| 745 | #endif /* F_NDELAY */ |
| 746 | #endif /* !O_NONBLOCK */ |
| 747 | status = fcntl(s, F_SETFL, status); |
| 748 | } |
| 749 | if (status < 0) { |
| 750 | #ifdef DEBUG_HTTP |
| 751 | perror("nonblocking"); |
| 752 | #endif |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 753 | xmlGenericError( xmlGenericErrorContext, |
| 754 | "xmlNanoHTTPConnectAttempt: %s - %s", |
| 755 | "error setting non-blocking IO", |
| 756 | strerror( socket_errno( ) ) ); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 757 | closesocket(s); |
| 758 | return(-1); |
| 759 | } |
| 760 | #endif /* !VMS */ |
| 761 | #endif /* !_WINSOCKAPI_ */ |
| 762 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 763 | if ((connect(s, addr, sizeof(*addr))==-1)) { |
| 764 | switch (socket_errno()) { |
| 765 | case EINPROGRESS: |
| 766 | case EWOULDBLOCK: |
| 767 | break; |
| 768 | default: |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 769 | xmlGenericError( xmlGenericErrorContext, |
| 770 | "xmlNanoHTTPConnectAttempt: %s - %s", |
| 771 | "error connecting to HTTP server", |
| 772 | strerror( socket_errno( ) ) ); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 773 | closesocket(s); |
| 774 | return(-1); |
| 775 | } |
| 776 | } |
| 777 | |
| 778 | tv.tv_sec = timeout; |
| 779 | tv.tv_usec = 0; |
| 780 | |
| 781 | FD_ZERO(&wfd); |
| 782 | FD_SET(s, &wfd); |
| 783 | |
| 784 | switch(select(s+1, NULL, &wfd, NULL, &tv)) |
| 785 | { |
| 786 | case 0: |
| 787 | /* Time out */ |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 788 | xmlGenericError( xmlGenericErrorContext, |
| 789 | "xmlNanoHTTPConnectAttempt: %s", |
| 790 | "Connect attempt timed out." ); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 791 | closesocket(s); |
| 792 | return(-1); |
| 793 | case -1: |
| 794 | /* Ermm.. ?? */ |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 795 | xmlGenericError( xmlGenericErrorContext, |
| 796 | "xmlNanoHTTPConnectAttempt: %s - %s", |
| 797 | "Error connecting to host", |
| 798 | strerror( socket_errno( ) ) ); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 799 | closesocket(s); |
| 800 | return(-1); |
| 801 | } |
| 802 | |
| 803 | if ( FD_ISSET(s, &wfd) ) { |
| 804 | SOCKLEN_T len; |
| 805 | len = sizeof(status); |
| 806 | if (getsockopt(s, SOL_SOCKET, SO_ERROR, (char*)&status, &len) < 0 ) { |
| 807 | /* Solaris error code */ |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 808 | xmlGenericError( xmlGenericErrorContext, |
| 809 | "xmlNanoHTTPConnectAttempt: %s - %s", |
| 810 | "Error retrieving pending socket errors", |
| 811 | strerror( socket_errno( ) ) ); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 812 | return (-1); |
| 813 | } |
| 814 | if ( status ) { |
| 815 | closesocket(s); |
| 816 | errno = status; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 817 | xmlGenericError( xmlGenericErrorContext, |
| 818 | "xmlNanoHTTPConnectAttempt: %s - %s", |
| 819 | "Error connecting to remote host", |
| 820 | strerror( status ) ); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 821 | return (-1); |
| 822 | } |
| 823 | } else { |
| 824 | /* pbm */ |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 825 | xmlGenericError( xmlGenericErrorContext, |
| 826 | "xmlNanoHTTPConnectAttempt: %s\n", |
| 827 | "Select returned, but descriptor not set for connection.\n" ); |
| 828 | closesocket(s); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 829 | return (-1); |
| 830 | } |
| 831 | |
| 832 | return(s); |
| 833 | } |
| 834 | |
| 835 | /** |
| 836 | * xmlNanoHTTPConnectHost: |
| 837 | * @host: the host name |
| 838 | * @port: the port number |
| 839 | * |
| 840 | * Attempt a connection to the given host:port endpoint. It tries |
| 841 | * the multiple IP provided by the DNS if available. |
| 842 | * |
| 843 | * Returns -1 in case of failure, the file descriptor number otherwise |
| 844 | */ |
| 845 | |
| 846 | static int |
| 847 | xmlNanoHTTPConnectHost(const char *host, int port) |
| 848 | { |
| 849 | struct hostent *h; |
| 850 | struct sockaddr *addr; |
| 851 | struct in_addr ia; |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 852 | struct sockaddr_in sockin; |
Daniel Veillard | 5c39654 | 2002-03-15 07:57:50 +0000 | [diff] [blame] | 853 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 854 | #ifdef SUPPORT_IP6 |
| 855 | struct in6_addr ia6; |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 856 | struct sockaddr_in6 sockin6; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 857 | #endif |
| 858 | int i; |
| 859 | int s; |
Daniel Veillard | 5c39654 | 2002-03-15 07:57:50 +0000 | [diff] [blame] | 860 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 861 | #if defined(SUPPORT_IP6) && defined(RES_USE_INET6) |
| 862 | if (!(_res.options & RES_INIT)) |
Daniel Veillard | 5c39654 | 2002-03-15 07:57:50 +0000 | [diff] [blame] | 863 | res_init(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 864 | _res.options |= RES_USE_INET6; |
| 865 | #endif |
Daniel Veillard | 5c39654 | 2002-03-15 07:57:50 +0000 | [diff] [blame] | 866 | h = gethostbyname(host); |
| 867 | if (h == NULL) { |
| 868 | #if defined(HAVE_NETDB_H) && defined(HOST_NOT_FOUND) |
| 869 | const char *h_err_txt = ""; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 870 | |
Daniel Veillard | 5c39654 | 2002-03-15 07:57:50 +0000 | [diff] [blame] | 871 | switch (h_errno) { |
| 872 | case HOST_NOT_FOUND: |
| 873 | h_err_txt = "Authoritive host not found"; |
| 874 | break; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 875 | |
Daniel Veillard | 5c39654 | 2002-03-15 07:57:50 +0000 | [diff] [blame] | 876 | case TRY_AGAIN: |
| 877 | h_err_txt = |
| 878 | "Non-authoritive host not found or server failure."; |
| 879 | break; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 880 | |
Daniel Veillard | 5c39654 | 2002-03-15 07:57:50 +0000 | [diff] [blame] | 881 | case NO_RECOVERY: |
| 882 | h_err_txt = |
| 883 | "Non-recoverable errors: FORMERR, REFUSED, or NOTIMP."; |
| 884 | break; |
| 885 | |
| 886 | case NO_ADDRESS: |
| 887 | h_err_txt = |
| 888 | "Valid name, no data record of requested type."; |
| 889 | break; |
| 890 | |
| 891 | default: |
| 892 | h_err_txt = "No error text defined."; |
| 893 | break; |
| 894 | } |
| 895 | xmlGenericError(xmlGenericErrorContext, |
| 896 | "xmlNanoHTTPConnectHost: %s '%s' - %s", |
| 897 | "Failed to resolve host", host, h_err_txt); |
| 898 | #else |
| 899 | xmlGenericError(xmlGenericErrorContext, |
| 900 | "xmlNanoHTTPConnectHost: %s '%s'", |
| 901 | "Failed to resolve host", host); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 902 | #endif |
Daniel Veillard | 5c39654 | 2002-03-15 07:57:50 +0000 | [diff] [blame] | 903 | return (-1); |
| 904 | } |
| 905 | |
| 906 | for (i = 0; h->h_addr_list[i]; i++) { |
| 907 | if (h->h_addrtype == AF_INET) { |
| 908 | /* A records (IPv4) */ |
| 909 | memcpy(&ia, h->h_addr_list[i], h->h_length); |
| 910 | sockin.sin_family = h->h_addrtype; |
| 911 | sockin.sin_addr = ia; |
| 912 | sockin.sin_port = htons(port); |
| 913 | addr = (struct sockaddr *) &sockin; |
| 914 | #ifdef SUPPORT_IP6 |
| 915 | } else if (h->h_addrtype == AF_INET6) { |
| 916 | /* AAAA records (IPv6) */ |
| 917 | memcpy(&ia6, h->h_addr_list[i], h->h_length); |
| 918 | sockin6.sin_family = h->h_addrtype; |
| 919 | sockin6.sin_addr = ia6; |
| 920 | sockin6.sin_port = htons(port); |
| 921 | addr = (struct sockaddr *) &sockin6; |
| 922 | #endif |
| 923 | } else |
| 924 | break; /* for */ |
| 925 | |
| 926 | s = xmlNanoHTTPConnectAttempt(addr); |
| 927 | if (s != -1) |
| 928 | return (s); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | #ifdef DEBUG_HTTP |
| 932 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 5c39654 | 2002-03-15 07:57:50 +0000 | [diff] [blame] | 933 | "xmlNanoHTTPConnectHost: unable to connect to '%s'.\n", |
| 934 | host); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 935 | #endif |
Daniel Veillard | 5c39654 | 2002-03-15 07:57:50 +0000 | [diff] [blame] | 936 | return (-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 937 | } |
| 938 | |
| 939 | |
| 940 | /** |
| 941 | * xmlNanoHTTPOpen: |
| 942 | * @URL: The URL to load |
| 943 | * @contentType: if available the Content-Type information will be |
| 944 | * returned at that location |
| 945 | * |
| 946 | * This function try to open a connection to the indicated resource |
| 947 | * via HTTP GET. |
| 948 | * |
| 949 | * Returns NULL in case of failure, otherwise a request handler. |
| 950 | * The contentType, if provided must be freed by the caller |
| 951 | */ |
| 952 | |
| 953 | void* |
| 954 | xmlNanoHTTPOpen(const char *URL, char **contentType) { |
| 955 | if (contentType != NULL) *contentType = NULL; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 956 | return(xmlNanoHTTPMethod(URL, NULL, NULL, contentType, NULL, 0)); |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 957 | } |
| 958 | |
| 959 | /** |
| 960 | * xmlNanoHTTPOpenRedir: |
| 961 | * @URL: The URL to load |
| 962 | * @contentType: if available the Content-Type information will be |
| 963 | * returned at that location |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 964 | * @redir: if available the redirected URL will be returned |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 965 | * |
| 966 | * This function try to open a connection to the indicated resource |
| 967 | * via HTTP GET. |
| 968 | * |
| 969 | * Returns NULL in case of failure, otherwise a request handler. |
| 970 | * The contentType, if provided must be freed by the caller |
| 971 | */ |
| 972 | |
| 973 | void* |
| 974 | xmlNanoHTTPOpenRedir(const char *URL, char **contentType, char **redir) { |
| 975 | if (contentType != NULL) *contentType = NULL; |
| 976 | if (redir != NULL) *redir = NULL; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 977 | return(xmlNanoHTTPMethodRedir(URL, NULL, NULL, contentType, redir, NULL,0)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | /** |
| 981 | * xmlNanoHTTPRead: |
| 982 | * @ctx: the HTTP context |
| 983 | * @dest: a buffer |
| 984 | * @len: the buffer length |
| 985 | * |
| 986 | * This function tries to read @len bytes from the existing HTTP connection |
| 987 | * and saves them in @dest. This is a blocking call. |
| 988 | * |
| 989 | * Returns the number of byte read. 0 is an indication of an end of connection. |
| 990 | * -1 indicates a parameter error. |
| 991 | */ |
| 992 | int |
| 993 | xmlNanoHTTPRead(void *ctx, void *dest, int len) { |
| 994 | xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx; |
| 995 | |
| 996 | if (ctx == NULL) return(-1); |
| 997 | if (dest == NULL) return(-1); |
| 998 | if (len <= 0) return(0); |
| 999 | |
| 1000 | while (ctxt->inptr - ctxt->inrptr < len) { |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1001 | if (xmlNanoHTTPRecv(ctxt) <= 0) break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1002 | } |
| 1003 | if (ctxt->inptr - ctxt->inrptr < len) |
| 1004 | len = ctxt->inptr - ctxt->inrptr; |
| 1005 | memcpy(dest, ctxt->inrptr, len); |
| 1006 | ctxt->inrptr += len; |
| 1007 | return(len); |
| 1008 | } |
| 1009 | |
| 1010 | /** |
| 1011 | * xmlNanoHTTPClose: |
| 1012 | * @ctx: the HTTP context |
| 1013 | * |
| 1014 | * This function closes an HTTP context, it ends up the connection and |
| 1015 | * free all data related to it. |
| 1016 | */ |
| 1017 | void |
| 1018 | xmlNanoHTTPClose(void *ctx) { |
| 1019 | xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx; |
| 1020 | |
| 1021 | if (ctx == NULL) return; |
| 1022 | |
| 1023 | xmlNanoHTTPFreeCtxt(ctxt); |
| 1024 | } |
| 1025 | |
| 1026 | /** |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 1027 | * xmlNanoHTTPMethodRedir: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1028 | * @URL: The URL to load |
| 1029 | * @method: the HTTP method to use |
| 1030 | * @input: the input string if any |
| 1031 | * @contentType: the Content-Type information IN and OUT |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 1032 | * @redir: the redirected URL OUT |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1033 | * @headers: the extra headers |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1034 | * @ilen: input length |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1035 | * |
| 1036 | * This function try to open a connection to the indicated resource |
| 1037 | * via HTTP using the given @method, adding the given extra headers |
| 1038 | * and the input buffer for the request content. |
| 1039 | * |
| 1040 | * Returns NULL in case of failure, otherwise a request handler. |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 1041 | * The contentType, or redir, if provided must be freed by the caller |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1042 | */ |
| 1043 | |
| 1044 | void* |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 1045 | xmlNanoHTTPMethodRedir(const char *URL, const char *method, const char *input, |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1046 | char **contentType, char **redir, |
| 1047 | const char *headers, int ilen ) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1048 | xmlNanoHTTPCtxtPtr ctxt; |
| 1049 | char *bp, *p; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1050 | int blen, ret; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1051 | int head; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1052 | int xmt_bytes; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1053 | int nbRedirects = 0; |
| 1054 | char *redirURL = NULL; |
| 1055 | |
| 1056 | if (URL == NULL) return(NULL); |
| 1057 | if (method == NULL) method = "GET"; |
| 1058 | xmlNanoHTTPInit(); |
| 1059 | |
| 1060 | retry: |
| 1061 | if (redirURL == NULL) |
| 1062 | ctxt = xmlNanoHTTPNewCtxt(URL); |
| 1063 | else { |
| 1064 | ctxt = xmlNanoHTTPNewCtxt(redirURL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1065 | } |
| 1066 | |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1067 | if ( ctxt == NULL ) { |
| 1068 | xmlGenericError( xmlGenericErrorContext, |
| 1069 | "xmlNanoHTTPMethodRedir: %s %s.", |
| 1070 | "Unable to allocate HTTP context to URI", |
| 1071 | ( ( redirURL == NULL ) ? URL : redirURL ) ); |
| 1072 | return ( NULL ); |
| 1073 | } |
| 1074 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1075 | if ((ctxt->protocol == NULL) || (strcmp(ctxt->protocol, "http"))) { |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1076 | xmlGenericError( xmlGenericErrorContext, |
| 1077 | "xmlNanoHTTPMethodRedir: %s - %s.", |
| 1078 | "Not a valid HTTP URI", |
| 1079 | ( ( redirURL == NULL ) ? URL : redirURL ) ); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1080 | xmlNanoHTTPFreeCtxt(ctxt); |
| 1081 | if (redirURL != NULL) xmlFree(redirURL); |
| 1082 | return(NULL); |
| 1083 | } |
| 1084 | if (ctxt->hostname == NULL) { |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1085 | xmlGenericError( xmlGenericErrorContext, |
| 1086 | "xmlNanoHTTPMethodRedir: %s - %s", |
| 1087 | "Failed to identify host in URI", |
| 1088 | ( ( redirURL == NULL ) ? URL : redirURL ) ); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1089 | xmlNanoHTTPFreeCtxt(ctxt); |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 1090 | if (redirURL != NULL) xmlFree(redirURL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1091 | return(NULL); |
| 1092 | } |
| 1093 | if (proxy) { |
| 1094 | blen = strlen(ctxt->hostname) * 2 + 16; |
| 1095 | ret = xmlNanoHTTPConnectHost(proxy, proxyPort); |
| 1096 | } |
| 1097 | else { |
| 1098 | blen = strlen(ctxt->hostname); |
| 1099 | ret = xmlNanoHTTPConnectHost(ctxt->hostname, ctxt->port); |
| 1100 | } |
| 1101 | if (ret < 0) { |
| 1102 | xmlNanoHTTPFreeCtxt(ctxt); |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 1103 | if (redirURL != NULL) xmlFree(redirURL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1104 | return(NULL); |
| 1105 | } |
| 1106 | ctxt->fd = ret; |
| 1107 | |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1108 | if (input == NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1109 | ilen = 0; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1110 | else |
| 1111 | blen += 36; |
| 1112 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1113 | if (headers != NULL) |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1114 | blen += strlen(headers) + 2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1115 | if (contentType && *contentType) |
| 1116 | blen += strlen(*contentType) + 16; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1117 | blen += strlen(method) + strlen(ctxt->path) + 24; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1118 | bp = xmlMalloc(blen); |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1119 | if ( bp == NULL ) { |
| 1120 | xmlNanoHTTPFreeCtxt( ctxt ); |
| 1121 | xmlGenericError( xmlGenericErrorContext, |
| 1122 | "xmlNanoHTTPMethodRedir: %s", |
| 1123 | "Error allocating HTTP header buffer." ); |
| 1124 | return ( NULL ); |
| 1125 | } |
| 1126 | |
| 1127 | p = bp; |
| 1128 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1129 | if (proxy) { |
| 1130 | if (ctxt->port != 80) { |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1131 | p += sprintf( p, "%s http://%s:%d%s", method, ctxt->hostname, |
| 1132 | ctxt->port, ctxt->path ); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1133 | } |
| 1134 | else |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1135 | p += sprintf( p, "%s http://%s%s", method, |
| 1136 | ctxt->hostname, ctxt->path); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1137 | } |
| 1138 | else |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1139 | p += sprintf( p, "%s %s", method, ctxt->path); |
| 1140 | |
| 1141 | p += sprintf(p, " HTTP/1.0\r\nHost: %s\r\n", ctxt->hostname); |
| 1142 | |
| 1143 | if (contentType != NULL && *contentType) |
| 1144 | p += sprintf(p, "Content-Type: %s\r\n", *contentType); |
| 1145 | |
| 1146 | if (headers != NULL) |
| 1147 | p += sprintf( p, "%s", headers ); |
| 1148 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1149 | if (input != NULL) |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1150 | sprintf(p, "Content-Length: %d\r\n\r\n", ilen ); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1151 | else |
| 1152 | strcpy(p, "\r\n"); |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1153 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1154 | #ifdef DEBUG_HTTP |
| 1155 | xmlGenericError(xmlGenericErrorContext, |
| 1156 | "-> %s%s", proxy? "(Proxy) " : "", bp); |
| 1157 | if ((blen -= strlen(bp)+1) < 0) |
| 1158 | xmlGenericError(xmlGenericErrorContext, |
| 1159 | "ERROR: overflowed buffer by %d bytes\n", -blen); |
| 1160 | #endif |
| 1161 | ctxt->outptr = ctxt->out = bp; |
| 1162 | ctxt->state = XML_NANO_HTTP_WRITE; |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1163 | blen = strlen( ctxt->out ); |
| 1164 | xmt_bytes = xmlNanoHTTPSend(ctxt, ctxt->out, blen ); |
| 1165 | #ifdef DEBUG_HTTP |
| 1166 | if ( xmt_bytes != blen ) |
| 1167 | xmlGenericError( xmlGenericErrorContext, |
| 1168 | "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n", |
| 1169 | xmt_bytes, blen, |
| 1170 | "bytes of HTTP headers sent to host", |
| 1171 | ctxt->hostname ); |
| 1172 | #endif |
| 1173 | |
| 1174 | if ( input != NULL ) { |
| 1175 | xmt_bytes = xmlNanoHTTPSend( ctxt, input, ilen ); |
| 1176 | |
| 1177 | #ifdef DEBUG_HTTP |
| 1178 | if ( xmt_bytes != ilen ) |
| 1179 | xmlGenericError( xmlGenericErrorContext, |
| 1180 | "xmlNanoHTTPMethodRedir: Only %d of %d %s %s\n", |
| 1181 | xmt_bytes, ilen, |
| 1182 | "bytes of HTTP content sent to host", |
| 1183 | ctxt->hostname ); |
| 1184 | #endif |
| 1185 | } |
| 1186 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1187 | ctxt->state = XML_NANO_HTTP_READ; |
| 1188 | head = 1; |
| 1189 | |
| 1190 | while ((p = xmlNanoHTTPReadLine(ctxt)) != NULL) { |
| 1191 | if (head && (*p == 0)) { |
| 1192 | head = 0; |
| 1193 | ctxt->content = ctxt->inrptr; |
| 1194 | xmlFree(p); |
| 1195 | break; |
| 1196 | } |
| 1197 | xmlNanoHTTPScanAnswer(ctxt, p); |
| 1198 | |
| 1199 | #ifdef DEBUG_HTTP |
| 1200 | xmlGenericError(xmlGenericErrorContext, "<- %s\n", p); |
| 1201 | #endif |
| 1202 | xmlFree(p); |
| 1203 | } |
| 1204 | |
| 1205 | if ((ctxt->location != NULL) && (ctxt->returnValue >= 300) && |
| 1206 | (ctxt->returnValue < 400)) { |
| 1207 | #ifdef DEBUG_HTTP |
| 1208 | xmlGenericError(xmlGenericErrorContext, |
| 1209 | "\nRedirect to: %s\n", ctxt->location); |
| 1210 | #endif |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1211 | while ( xmlNanoHTTPRecv(ctxt) > 0 ) ; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1212 | if (nbRedirects < XML_NANO_HTTP_MAX_REDIR) { |
| 1213 | nbRedirects++; |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 1214 | if (redirURL != NULL) |
| 1215 | xmlFree(redirURL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1216 | redirURL = xmlMemStrdup(ctxt->location); |
| 1217 | xmlNanoHTTPFreeCtxt(ctxt); |
| 1218 | goto retry; |
| 1219 | } |
| 1220 | xmlNanoHTTPFreeCtxt(ctxt); |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 1221 | if (redirURL != NULL) xmlFree(redirURL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1222 | #ifdef DEBUG_HTTP |
| 1223 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1224 | "xmlNanoHTTPMethodRedir: Too many redirects, aborting ...\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1225 | #endif |
| 1226 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1227 | } |
| 1228 | |
| 1229 | if (contentType != NULL) { |
| 1230 | if (ctxt->contentType != NULL) |
| 1231 | *contentType = xmlMemStrdup(ctxt->contentType); |
| 1232 | else |
| 1233 | *contentType = NULL; |
| 1234 | } |
| 1235 | |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 1236 | if ((redir != NULL) && (redirURL != NULL)) { |
| 1237 | *redir = redirURL; |
| 1238 | } else { |
| 1239 | if (redirURL != NULL) |
| 1240 | xmlFree(redirURL); |
| 1241 | if (redir != NULL) |
| 1242 | *redir = NULL; |
| 1243 | } |
| 1244 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1245 | #ifdef DEBUG_HTTP |
| 1246 | if (ctxt->contentType != NULL) |
| 1247 | xmlGenericError(xmlGenericErrorContext, |
| 1248 | "\nCode %d, content-type '%s'\n\n", |
| 1249 | ctxt->returnValue, ctxt->contentType); |
| 1250 | else |
| 1251 | xmlGenericError(xmlGenericErrorContext, |
| 1252 | "\nCode %d, no content-type\n\n", |
| 1253 | ctxt->returnValue); |
| 1254 | #endif |
| 1255 | |
| 1256 | return((void *) ctxt); |
| 1257 | } |
| 1258 | |
| 1259 | /** |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 1260 | * xmlNanoHTTPMethod: |
| 1261 | * @URL: The URL to load |
| 1262 | * @method: the HTTP method to use |
| 1263 | * @input: the input string if any |
| 1264 | * @contentType: the Content-Type information IN and OUT |
| 1265 | * @headers: the extra headers |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1266 | * @ilen: input length |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 1267 | * |
| 1268 | * This function try to open a connection to the indicated resource |
| 1269 | * via HTTP using the given @method, adding the given extra headers |
| 1270 | * and the input buffer for the request content. |
| 1271 | * |
| 1272 | * Returns NULL in case of failure, otherwise a request handler. |
| 1273 | * The contentType, if provided must be freed by the caller |
| 1274 | */ |
| 1275 | |
| 1276 | void* |
| 1277 | xmlNanoHTTPMethod(const char *URL, const char *method, const char *input, |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1278 | char **contentType, const char *headers, int ilen) { |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 1279 | return(xmlNanoHTTPMethodRedir(URL, method, input, contentType, |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1280 | NULL, headers, ilen)); |
Daniel Veillard | 9403a04 | 2001-05-28 11:00:53 +0000 | [diff] [blame] | 1281 | } |
| 1282 | |
| 1283 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1284 | * xmlNanoHTTPFetch: |
| 1285 | * @URL: The URL to load |
| 1286 | * @filename: the filename where the content should be saved |
| 1287 | * @contentType: if available the Content-Type information will be |
| 1288 | * returned at that location |
| 1289 | * |
| 1290 | * This function try to fetch the indicated resource via HTTP GET |
| 1291 | * and save it's content in the file. |
| 1292 | * |
| 1293 | * Returns -1 in case of failure, 0 incase of success. The contentType, |
| 1294 | * if provided must be freed by the caller |
| 1295 | */ |
| 1296 | int |
| 1297 | xmlNanoHTTPFetch(const char *URL, const char *filename, char **contentType) { |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1298 | void *ctxt = NULL; |
| 1299 | char *buf = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1300 | int fd; |
| 1301 | int len; |
| 1302 | |
| 1303 | ctxt = xmlNanoHTTPOpen(URL, contentType); |
| 1304 | if (ctxt == NULL) return(-1); |
| 1305 | |
| 1306 | if (!strcmp(filename, "-")) |
| 1307 | fd = 0; |
| 1308 | else { |
| 1309 | fd = open(filename, O_CREAT | O_WRONLY, 00644); |
| 1310 | if (fd < 0) { |
| 1311 | xmlNanoHTTPClose(ctxt); |
| 1312 | if ((contentType != NULL) && (*contentType != NULL)) { |
| 1313 | xmlFree(*contentType); |
| 1314 | *contentType = NULL; |
| 1315 | } |
| 1316 | return(-1); |
| 1317 | } |
| 1318 | } |
| 1319 | |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1320 | xmlNanoHTTPFetchContent( ctxt, &buf, &len ); |
| 1321 | if ( len > 0 ) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1322 | write(fd, buf, len); |
| 1323 | } |
| 1324 | |
| 1325 | xmlNanoHTTPClose(ctxt); |
| 1326 | close(fd); |
| 1327 | return(0); |
| 1328 | } |
| 1329 | |
| 1330 | /** |
| 1331 | * xmlNanoHTTPSave: |
| 1332 | * @ctxt: the HTTP context |
| 1333 | * @filename: the filename where the content should be saved |
| 1334 | * |
| 1335 | * This function saves the output of the HTTP transaction to a file |
| 1336 | * It closes and free the context at the end |
| 1337 | * |
| 1338 | * Returns -1 in case of failure, 0 incase of success. |
| 1339 | */ |
| 1340 | int |
| 1341 | xmlNanoHTTPSave(void *ctxt, const char *filename) { |
Daniel Veillard | e392497 | 2001-07-25 20:25:21 +0000 | [diff] [blame] | 1342 | char *buf = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1343 | int fd; |
| 1344 | int len; |
| 1345 | |
| 1346 | if (ctxt == NULL) return(-1); |
| 1347 | |
| 1348 | if (!strcmp(filename, "-")) |
| 1349 | fd = 0; |
| 1350 | else { |
| 1351 | fd = open(filename, O_CREAT | O_WRONLY); |
| 1352 | if (fd < 0) { |
| 1353 | xmlNanoHTTPClose(ctxt); |
| 1354 | return(-1); |
| 1355 | } |
| 1356 | } |
| 1357 | |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1358 | xmlNanoHTTPFetchContent( ctxt, &buf, &len ); |
| 1359 | if ( len > 0 ) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1360 | write(fd, buf, len); |
| 1361 | } |
| 1362 | |
| 1363 | xmlNanoHTTPClose(ctxt); |
| 1364 | return(0); |
| 1365 | } |
| 1366 | |
| 1367 | /** |
| 1368 | * xmlNanoHTTPReturnCode: |
| 1369 | * @ctx: the HTTP context |
| 1370 | * |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1371 | * Get the latest HTTP return code received |
| 1372 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1373 | * Returns the HTTP return code for the request. |
| 1374 | */ |
| 1375 | int |
| 1376 | xmlNanoHTTPReturnCode(void *ctx) { |
| 1377 | xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx; |
| 1378 | |
| 1379 | if (ctxt == NULL) return(-1); |
| 1380 | |
| 1381 | return(ctxt->returnValue); |
| 1382 | } |
| 1383 | |
| 1384 | /** |
| 1385 | * xmlNanoHTTPAuthHeader: |
| 1386 | * @ctx: the HTTP context |
| 1387 | * |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1388 | * Get the authentication header of an HTTP context |
| 1389 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1390 | * Returns the stashed value of the WWW-Authenticate or Proxy-Authenticate |
| 1391 | * header. |
| 1392 | */ |
| 1393 | const char * |
| 1394 | xmlNanoHTTPAuthHeader(void *ctx) { |
| 1395 | xmlNanoHTTPCtxtPtr ctxt = (xmlNanoHTTPCtxtPtr) ctx; |
| 1396 | |
| 1397 | if (ctxt == NULL) return(NULL); |
| 1398 | |
| 1399 | return(ctxt->authHeader); |
| 1400 | } |
| 1401 | |
Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1402 | /** |
| 1403 | * xmlNanoHTTPContentLength |
| 1404 | * @ctx: the HTTP context |
| 1405 | * |
| 1406 | * Return the specified content length from the HTTP header. Note that |
| 1407 | * a value of -1 indicates that the content length element was not included in |
| 1408 | * the response header. |
| 1409 | */ |
| 1410 | int |
| 1411 | xmlNanoHTTPContentLength( void * ctx ) { |
| 1412 | xmlNanoHTTPCtxtPtr ctxt = ctx; |
| 1413 | |
| 1414 | return ( ( ctxt == NULL ) ? -1 : ctxt->ContentLength ); |
| 1415 | } |
| 1416 | |
| 1417 | /** |
| 1418 | * xmlNanoHTTPFetchContent |
| 1419 | * @ctx: the HTTP context |
| 1420 | * @ptr: pointer to set to the content buffer. |
| 1421 | * @len: integer pointer to hold the length of the content |
| 1422 | * |
| 1423 | * Returns 0 if all the content was read and available, returns |
| 1424 | * -1 if received content length was less than specified or an error |
| 1425 | * occurred. |
| 1426 | */ |
| 1427 | int |
| 1428 | xmlNanoHTTPFetchContent( void * ctx, char ** ptr, int * len ) { |
| 1429 | xmlNanoHTTPCtxtPtr ctxt = ctx; |
| 1430 | |
| 1431 | int rc = 0; |
| 1432 | int cur_lgth; |
| 1433 | int rcvd_lgth; |
| 1434 | int dummy_int; |
| 1435 | char * dummy_ptr = NULL; |
| 1436 | |
| 1437 | /* Dummy up return input parameters if not provided */ |
| 1438 | |
| 1439 | if ( len == NULL ) |
| 1440 | len = &dummy_int; |
| 1441 | |
| 1442 | if ( ptr == NULL ) |
| 1443 | ptr = &dummy_ptr; |
| 1444 | |
| 1445 | /* But can't work without the context pointer */ |
| 1446 | |
| 1447 | if ( ( ctxt == NULL ) || ( ctxt->content == NULL ) ) { |
| 1448 | *len = 0; |
| 1449 | *ptr = NULL; |
| 1450 | return ( -1 ); |
| 1451 | } |
| 1452 | |
| 1453 | rcvd_lgth = ctxt->inptr - ctxt->content; |
| 1454 | |
| 1455 | while ( (cur_lgth = xmlNanoHTTPRecv( ctxt )) > 0 ) { |
| 1456 | |
| 1457 | rcvd_lgth += cur_lgth; |
| 1458 | if ( (ctxt->ContentLength > 0) && (rcvd_lgth >= ctxt->ContentLength) ) |
| 1459 | break; |
| 1460 | } |
| 1461 | |
| 1462 | *ptr = ctxt->content; |
| 1463 | *len = rcvd_lgth; |
| 1464 | |
| 1465 | if ( ( ctxt->ContentLength > 0 ) && ( rcvd_lgth < ctxt->ContentLength ) ) |
| 1466 | rc = -1; |
| 1467 | else if ( rcvd_lgth == 0 ) |
| 1468 | rc = -1; |
| 1469 | |
| 1470 | return ( rc ); |
| 1471 | } |
| 1472 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1473 | #ifdef STANDALONE |
| 1474 | int main(int argc, char **argv) { |
| 1475 | char *contentType = NULL; |
| 1476 | |
| 1477 | if (argv[1] != NULL) { |
| 1478 | if (argv[2] != NULL) |
| 1479 | xmlNanoHTTPFetch(argv[1], argv[2], &contentType); |
| 1480 | else |
| 1481 | xmlNanoHTTPFetch(argv[1], "-", &contentType); |
| 1482 | if (contentType != NULL) xmlFree(contentType); |
| 1483 | } else { |
| 1484 | xmlGenericError(xmlGenericErrorContext, |
| 1485 | "%s: minimal HTTP GET implementation\n", argv[0]); |
| 1486 | xmlGenericError(xmlGenericErrorContext, |
| 1487 | "\tusage %s [ URL [ filename ] ]\n", argv[0]); |
| 1488 | } |
| 1489 | xmlNanoHTTPCleanup(); |
| 1490 | xmlMemoryDump(); |
| 1491 | return(0); |
| 1492 | } |
| 1493 | #endif /* STANDALONE */ |
| 1494 | #else /* !LIBXML_HTTP_ENABLED */ |
| 1495 | #ifdef STANDALONE |
| 1496 | #include <stdio.h> |
| 1497 | int main(int argc, char **argv) { |
| 1498 | xmlGenericError(xmlGenericErrorContext, |
| 1499 | "%s : HTTP support not compiled in\n", argv[0]); |
| 1500 | return(0); |
| 1501 | } |
| 1502 | #endif /* STANDALONE */ |
| 1503 | #endif /* LIBXML_HTTP_ENABLED */ |