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