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