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