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