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