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