Debian Codegraph @

deps/libneon/src/ne_request.c

raw
1 /*
2 HTTP request/response handling
3 Copyright (C) 1999-2010, Joe Orton <[email protected]>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public
16 License along with this library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18 MA 02111-1307, USA
19
20 */
21
22 /* This is the HTTP client request/response implementation.
23 * The goal of this code is to be modular and simple.
24 */
25
26 #include "config.h"
27
28 #include <time.h>
29 #include <sys/types.h>
30
31 #include <errno.h>
32 #include <fcntl.h>
33
34
35 #ifdef HAVE_STRING_H
36 #include <string.h>
37 #endif
38 #ifdef HAVE_STRINGS_H
39 #include <strings.h>
40 #endif
41 #ifdef HAVE_STDLIB_H
42 #include <stdlib.h>
43 #endif
44 #ifdef HAVE_UNISTD_H
45 #include <unistd.h>
46 #endif
47
48 #include "ne_internal.h"
49
50 #include "ne_alloc.h"
51 #include "ne_request.h"
52 #include "ne_string.h" /* for ne_buffer */
53 #include "ne_utils.h"
54 #include "ne_socket.h"
55 #include "ne_uri.h"
56
57
58 #include "ne_private.h"
59
60 #define SOCK_ERR(req, op, msg) do { ssize_t sret = (op); \
61 if (sret < 0) return aborted(req, msg, sret); } while (0)
62
63 #define EOL "\r\n"
64
65 struct body_reader {
66 ne_block_reader handler;
67 ne_accept_response accept_response;
68 unsigned int use;
69 void *userdata;
70 struct body_reader *next;
71 };
72
73 struct field {
74 char *name, *value;
75 size_t vlen;
76 struct field *next;
77 };
78
79 /* Maximum number of header fields per response: */
80 #define MAX_HEADER_FIELDS (100)
81 /* Size of hash table; 43 is the smallest prime for which the common
82 * header names hash uniquely using the *33 hash function. */
83 #define HH_HASHSIZE (43)
84 /* Hash iteration step: *33 known to be a good hash for ASCII, see RSE. */
85 #define HH_ITERATE(hash, ch) (((hash)*33 + (unsigned char)(ch)) % HH_HASHSIZE)
86
87 /* pre-calculated hash values for given header names: */
88 #define HH_HV_CONNECTION (0x14)
89 #define HH_HV_PROXY_CONNECTION (0x1A)
90 #define HH_HV_CONTENT_LENGTH (0x13)
91 #define HH_HV_TRANSFER_ENCODING (0x07)
92
93 struct ne_request_s {
94 char *method, *uri; /* method and Request-URI */
95
96 ne_buffer *headers; /* request headers */
97
98 /* Request body. */
99 ne_provide_body body_cb;
100 void *body_ud;
101
102 /* Request body source: file or buffer (if not callback). */
103 union {
104 struct {
105 int fd;
106 ne_off_t offset, length;
107 ne_off_t remain; /* remaining bytes to send. */
108 } file;
109 struct {
110 /* length bytes @ buffer = whole body.
111 * remain bytes @ pnt = remaining bytes to send */
112 const char *buffer, *pnt;
113 size_t length, remain;
114 } buf;
115 } body;
116
117 ne_off_t body_length; /* length of request body */
118
119 /* temporary store for response lines. */
120 char respbuf[NE_BUFSIZ];
121
122 /**** Response ***/
123
124 /* The transfer encoding types */
125 struct ne_response {
126 enum {
127 R_TILLEOF = 0, /* read till eof */
128 R_NO_BODY, /* implicitly no body (HEAD, 204, 304) */
129 R_CHUNKED, /* using chunked transfer-encoding */
130 R_CLENGTH /* using given content-length */
131 } mode;
132 union {
133 /* clen: used if mode == R_CLENGTH; total and bytes
134 * remaining to be read of response body. */
135 struct {
136 ne_off_t total, remain;
137 } clen;
138 /* chunk: used if mode == R_CHUNKED; total and bytes
139 * remaining to be read of current chunk */
140 struct {
141 size_t total, remain;
142 } chunk;
143 } body;
144 ne_off_t progress; /* number of bytes read of response */
145 } resp;
146
147 struct hook *private;
148
149 /* response header fields */
150 struct field *response_headers[HH_HASHSIZE];
151
152 unsigned int current_index; /* response_headers cursor for iterator */
153
154 /* List of callbacks which are passed response body blocks */
155 struct body_reader *body_readers;
156
157 /*** Miscellaneous ***/
158 unsigned int method_is_head;
159 unsigned int can_persist;
160
161 struct timespec expiration_time;
162
163 int flags[NE_REQFLAG_LAST];
164
165 ne_session *session;
166 ne_status status;
167 };
168
169 static int open_connection(ne_session *sess);
170
171 /* Returns hash value for header 'name', converting it to lower-case
172 * in-place. */
173 static inline unsigned int hash_and_lower(char *name)
174 {
175 char *pnt;
176 unsigned int hash = 0;
177
178 for (pnt = name; *pnt != '\0'; pnt++) {
179 *pnt = ne_tolower(*pnt);
180 hash = HH_ITERATE(hash,*pnt);
181 }
182
183 return hash;
184 }
185
186 /* Abort a request due to an non-recoverable HTTP protocol error,
187 * whilst doing 'doing'. 'code', if non-zero, is the socket error
188 * code, NE_SOCK_*, or if zero, is ignored. */
189 static int aborted(ne_request *req, const char *doing, ssize_t code)
190 {
191 ne_session *sess = req->session;
192 int ret = NE_ERROR;
193
194 NE_DEBUG(NE_DBG_HTTP, "Aborted request (%" NE_FMT_SSIZE_T "): %s",
195 code, doing);
196
197 switch(code) {
198 case NE_SOCK_CLOSED:
199 if (sess->nexthop->proxy != PROXY_NONE) {
200 ne_set_error(sess, _("%s: connection was closed by proxy server"),
201 doing);
202 } else {
203 ne_set_error(sess, _("%s: connection was closed by server"),
204 doing);
205 }
206 break;
207 case NE_SOCK_TIMEOUT:
208 ne_set_error(sess, _("%s: connection timed out"), doing);
209 ret = NE_TIMEOUT;
210 break;
211 case NE_SOCK_ERROR:
212 case NE_SOCK_RESET:
213 case NE_SOCK_TRUNC:
214 ne_set_error(sess, "%s: %s", doing, ne_sock_error(sess->socket));
215 break;
216 case 0:
217 ne_set_error(sess, "%s", doing);
218 break;
219 }
220
221 ne_close_connection(sess);
222 return ret;
223 }
224
225 static void notify_status(ne_session *sess, ne_session_status status)
226 {
227 if (sess->notify_cb) {
228 sess->notify_cb(sess->notify_ud, status, &sess->status);
229 }
230 }
231
232 static void *get_private(const struct hook *hk, const char *id)
233 {
234 for (; hk != NULL; hk = hk->next)
235 if (strcmp(hk->id, id) == 0)
236 return hk->userdata;
237 return NULL;
238 }
239
240 void *ne_get_request_private(ne_request *req, const char *id)
241 {
242 return get_private(req->private, id);
243 }
244
245 void *ne_get_session_private(ne_session *sess, const char *id)
246 {
247 return get_private(sess->private, id);
248 }
249
250 void ne_set_request_private(ne_request *req, const char *id, void *userdata)
251 {
252 struct hook *hk = ne_malloc(sizeof (struct hook)), *pos;
253
254 if (req->private != NULL) {
255 for (pos = req->private; pos->next != NULL; pos = pos->next)
256 /* nullop */;
257 pos->next = hk;
258 } else {
259 req->private = hk;
260 }
261
262 hk->id = id;
263 hk->fn = NULL;
264 hk->userdata = userdata;
265 hk->next = NULL;
266 }
267
268 static ssize_t body_string_send(void *userdata, char *buffer, size_t count)
269 {
270 ne_request *req = userdata;
271
272 if (count == 0) {
273 req->body.buf.remain = req->body.buf.length;
274 req->body.buf.pnt = req->body.buf.buffer;
275 } else {
276 /* if body_left == 0 we fall through and return 0. */
277 if (req->body.buf.remain < count)
278 count = req->body.buf.remain;
279
280 memcpy(buffer, req->body.buf.pnt, count);
281 req->body.buf.pnt += count;
282 req->body.buf.remain -= count;
283 }
284
285 return count;
286 }
287
288 static ssize_t body_fd_send(void *userdata, char *buffer, size_t count)
289 {
290 ne_request *req = userdata;
291
292 if (count) {
293 ssize_t ret;
294
295 if (req->body.file.remain == 0)
296 return 0;
297
298 /* Casts here are necessary for LFS platforms for safe and
299 * warning-free assignment/comparison between 32-bit size_t
300 * and 64-bit off64_t: */
301 if ((ne_off_t)count > req->body.file.remain)
302 count = (size_t)req->body.file.remain;
303
304 ret = read(req->body.file.fd, buffer, count);
305 if (ret > 0) {
306 req->body.file.remain -= ret;
307 return ret;
308 }
309 else if (ret == 0) {
310 ne_set_error(req->session,
311 _("Premature EOF in request body file"));
312 }
313 else if (ret < 0) {
314 char err[200];
315 int errnum = errno;
316
317 ne_set_error(req->session,
318 _("Failed reading request body file: %s"),
319 ne_strerror(errnum, err, sizeof err));
320 }
321
322 return -1;
323 } else {
324 ne_off_t newoff;
325
326 /* rewind for next send. */
327 newoff = ne_lseek(req->body.file.fd, req->body.file.offset, SEEK_SET);
328 if (newoff == req->body.file.offset) {
329 req->body.file.remain = req->body.file.length;
330 return 0;
331 } else {
332 char err[200], offstr[20];
333
334 if (newoff == -1) {
335 /* errno was set */
336 ne_strerror(errno, err, sizeof err);
337 } else {
338 strcpy(err, _("offset invalid"));
339 }
340 ne_snprintf(offstr, sizeof offstr, "%" FMT_NE_OFF_T,
341 req->body.file.offset);
342 ne_set_error(req->session,
343 _("Could not seek to offset %s"
344 " of request body file: %s"),
345 offstr, err);
346 return -1;
347 }
348 }
349 }
350
351 /* For accurate persistent connection handling, for any write() or
352 * read() operation for a new request on an already-open connection,
353 * an EOF or RST error MUST be treated as a persistent connection
354 * timeout, and the request retried on a new connection. Once a
355 * read() operation has succeeded, any subsequent error MUST be
356 * treated as fatal. A 'retry' flag is used; retry=1 represents the
357 * first case, retry=0 the latter. */
358
359 /* RETRY_RET() crafts a function return value given the 'retry' flag,
360 * the socket error 'code', and the return value 'acode' from the
361 * aborted() function. */
362 #define RETRY_RET(retry, code, acode) \
363 ((((code) == NE_SOCK_CLOSED || (code) == NE_SOCK_RESET || \
364 (code) == NE_SOCK_TRUNC) && retry) ? NE_RETRY : (acode))
365
366 /* Sends the request body; returns 0 on success or an NE_* error code.
367 * If retry is non-zero; will return NE_RETRY on persistent connection
368 * timeout. On error, the session error string is set and the
369 * connection is closed. */
370 static int send_request_body(ne_request *req, int retry)
371 {
372 ne_session *const sess = req->session;
373 char buffer[NE_BUFSIZ_LARGE];
374 ssize_t bytes;
375
376 NE_DEBUG(NE_DBG_CORE, "Sending request body:");
377
378 req->session->status.sr.progress = 0;
379 req->session->status.sr.total = req->body_length;
380 notify_status(sess, ne_status_sending);
381
382 /* tell the source to start again from the beginning. */
383 if (req->body_cb(req->body_ud, NULL, 0) != 0) {
384 ne_close_connection(sess);
385 return NE_ERROR;
386 }
387
388 while ((bytes = req->body_cb(req->body_ud, buffer, sizeof buffer)) > 0) {
389 int ret = ne_sock_fullwrite(sess->socket, buffer, bytes);
390 if (ret < 0) {
391 int aret = aborted(req, _("Could not send request body"), ret);
392 return RETRY_RET(retry, ret, aret);
393 }
394
395 NE_DEBUG(NE_DBG_HTTPBODY,
396 "Body block (%" NE_FMT_SSIZE_T " bytes):\n[%.*s]\n",
397 bytes, (int)bytes, buffer);
398
399 /* invoke progress callback */
400 req->session->status.sr.progress += bytes;
401 notify_status(sess, ne_status_sending);
402 }
403
404 if (bytes == 0) {
405 NE_DEBUG(NE_DBG_CORE, "Request body sent successfully");
406 return NE_OK;
407 } else {
408 NE_DEBUG(NE_DBG_HTTP, "Request body provider failed with "
409 "%" NE_FMT_SSIZE_T "\n", bytes);
410 ne_close_connection(sess);
411 return NE_ERROR;
412 }
413 }
414
415 /* Lob the User-Agent, connection and host headers in to the request
416 * headers */
417 static void add_fixed_headers(ne_request *req)
418 {
419 ne_session *const sess = req->session;
420
421 if (sess->user_agent) {
422 ne_buffer_zappend(req->headers, sess->user_agent);
423 }
424
425 /* If persistent connections are disabled, just send Connection:
426 * close; otherwise, send Connection: Keep-Alive to pre-1.1 origin
427 * servers to try harder to get a persistent connection, except if
428 * using a proxy as per 2068§19.7.1. Always add TE: trailers. */
429 if (!sess->flags[NE_SESSFLAG_PERSIST]) {
430 ne_buffer_czappend(req->headers, "Connection: close" EOL);
431 }
432 else if (!sess->is_http11 && !sess->any_proxy_http) {
433 ne_buffer_czappend(req->headers,
434 "Keep-Alive: " EOL
435 "Connection: Keep-Alive" EOL);
436 }
437 else if (!req->session->is_http11 && !sess->any_proxy_http) {
438 ne_buffer_czappend(req->headers,
439 "Keep-Alive: " EOL
440 "Proxy-Connection: Keep-Alive" EOL);
441 }
442
443 ne_buffer_concat(req->headers, "TE: trailers" EOL "Host: ",
444 req->session->server.hostport, EOL, NULL);
445 }
446
447 int ne_accept_always(void *userdata, ne_request *req, const ne_status *st)
448 {
449 return 1;
450 }
451
452 int ne_accept_2xx(void *userdata, ne_request *req, const ne_status *st)
453 {
454 return (st->klass == 2);
455 }
456
457 ne_request *ne_request_create(ne_session *sess,
458 const char *method, const char *path)
459 {
460 ne_request *req = ne_calloc(sizeof *req);
461
462 req->session = sess;
463 req->headers = ne_buffer_create();
464
465 /* Presume the method is idempotent by default. */
466 req->flags[NE_REQFLAG_IDEMPOTENT] = 1;
467 /* Expect-100 default follows the corresponding session flag. */
468 req->flags[NE_REQFLAG_EXPECT100] = sess->flags[NE_SESSFLAG_EXPECT100];
469
470 /* Add in the fixed headers */
471 add_fixed_headers(req);
472
473 /* Set the standard stuff */
474 req->method = ne_strdup(method);
475 req->method_is_head = (strcmp(method, "HEAD") == 0);
476
477 /* Only use an absoluteURI here when we might be using an HTTP
478 * proxy, and SSL is in use: some servers can't parse them. */
479 if (sess->any_proxy_http && !req->session->use_ssl && path[0] == '/')
480 req->uri = ne_concat(req->session->scheme, "://",
481 req->session->server.hostport, path, NULL);
482 else
483 req->uri = ne_strdup(path);
484
485 {
486 struct hook *hk;
487
488 for (hk = sess->create_req_hooks; hk != NULL; hk = hk->next) {
489 ne_create_request_fn fn = (ne_create_request_fn)hk->fn;
490 fn(req, hk->userdata, req->method, req->uri);
491 }
492 }
493
494 req->expiration_time.tv_sec = 0;
495 req->expiration_time.tv_nsec= 0;
496
497 return req;
498 }
499
500 /* Set the request body length to 'length' */
501 static void set_body_length(ne_request *req, ne_off_t length)
502 {
503 req->body_length = length;
504 ne_print_request_header(req, "Content-Length", "%" FMT_NE_OFF_T, length);
505 }
506
507 void ne_set_request_body_buffer(ne_request *req, const char *buffer,
508 size_t size)
509 {
510 req->body.buf.buffer = buffer;
511 req->body.buf.length = size;
512 req->body_cb = body_string_send;
513 req->body_ud = req;
514 set_body_length(req, size);
515 }
516
517 void ne_set_request_body_provider(ne_request *req, ne_off_t bodysize,
518 ne_provide_body provider, void *ud)
519 {
520 req->body_cb = provider;
521 req->body_ud = ud;
522 set_body_length(req, bodysize);
523 }
524
525 void ne_set_request_body_fd(ne_request *req, int fd,
526 ne_off_t offset, ne_off_t length)
527 {
528 req->body.file.fd = fd;
529 req->body.file.offset = offset;
530 req->body.file.length = length;
531 req->body_cb = body_fd_send;
532 req->body_ud = req;
533 set_body_length(req, length);
534 }
535
536 void ne_set_request_flag(ne_request *req, ne_request_flag flag, int value)
537 {
538 if (flag < NE_REQFLAG_LAST) {
539 req->flags[flag] = value;
540 }
541 }
542
543 int ne_get_request_flag(ne_request *req, ne_request_flag flag)
544 {
545 if (flag < NE_REQFLAG_LAST) {
546 return req->flags[flag];
547 }
548 return -1;
549 }
550
551
552 void ne_add_request_header(ne_request *req, const char *name,
553 const char *value)
554 {
555 ne_buffer_concat(req->headers, name, ": ", value, EOL, NULL);
556 }
557
558 void ne_print_request_header(ne_request *req, const char *name,
559 const char *format, ...)
560 {
561 va_list params;
562 char buf[NE_BUFSIZ];
563
564 va_start(params, format);
565 ne_vsnprintf(buf, sizeof buf, format, params);
566 va_end(params);
567
568 ne_buffer_concat(req->headers, name, ": ", buf, EOL, NULL);
569 }
570
571 /* Returns the value of the response header 'name', for which the hash
572 * value is 'h', or NULL if the header is not found. */
573 static inline char *get_response_header_hv(ne_request *req, unsigned int h,
574 const char *name)
575 {
576 struct field *f;
577
578 for (f = req->response_headers[h]; f; f = f->next)
579 if (strcmp(f->name, name) == 0)
580 return f->value;
581
582 return NULL;
583 }
584
585 const char *ne_get_response_header(ne_request *req, const char *name)
586 {
587 char *lcname = ne_strdup(name);
588 unsigned int hash = hash_and_lower(lcname);
589 char *value = get_response_header_hv(req, hash, lcname);
590 ne_free(lcname);
591 return value;
592 }
593
594 /* The return value of the iterator function is a pointer to the
595 * struct field of the previously returned header. */
596 void *ne_response_header_iterate(ne_request *req, void *iterator,
597 const char **name, const char **value)
598 {
599 struct field *f = iterator;
600 unsigned int n;
601
602 if (f == NULL) {
603 n = 0;
604 } else if ((f = f->next) == NULL) {
605 n = req->current_index + 1;
606 }
607
608 if (f == NULL) {
609 while (n < HH_HASHSIZE && req->response_headers[n] == NULL)
610 n++;
611 if (n == HH_HASHSIZE)
612 return NULL; /* no more headers */
613 f = req->response_headers[n];
614 req->current_index = n;
615 }
616
617 *name = f->name;
618 *value = f->value;
619 return f;
620 }
621
622 /* Removes the response header 'name', which has hash value 'hash'. */
623 static void remove_response_header(ne_request *req, const char *name,
624 unsigned int hash)
625 {
626 struct field **ptr = req->response_headers + hash;
627
628 while (*ptr) {
629 struct field *const f = *ptr;
630
631 if (strcmp(f->name, name) == 0) {
632 *ptr = f->next;
633 ne_free(f->name);
634 ne_free(f->value);
635 ne_free(f);
636 return;
637 }
638
639 ptr = &f->next;
640 }
641 }
642
643 /* Free all stored response headers. */
644 static void free_response_headers(ne_request *req)
645 {
646 int n;
647
648 for (n = 0; n < HH_HASHSIZE; n++) {
649 struct field **ptr = req->response_headers + n;
650
651 while (*ptr) {
652 struct field *const f = *ptr;
653 *ptr = f->next;
654 ne_free(f->name);
655 ne_free(f->value);
656 ne_free(f);
657 }
658 }
659 }
660
661 void ne_add_response_body_reader(ne_request *req, ne_accept_response acpt,
662 ne_block_reader rdr, void *userdata)
663 {
664 struct body_reader *new = ne_malloc(sizeof *new);
665 new->accept_response = acpt;
666 new->handler = rdr;
667 new->userdata = userdata;
668 new->next = req->body_readers;
669 req->body_readers = new;
670 }
671
672 void ne_request_destroy(ne_request *req)
673 {
674 struct body_reader *rdr, *next_rdr;
675 struct hook *hk, *next_hk;
676
677 ne_free(req->uri);
678 ne_free(req->method);
679
680 for (rdr = req->body_readers; rdr != NULL; rdr = next_rdr) {
681 next_rdr = rdr->next;
682 ne_free(rdr);
683 }
684
685 free_response_headers(req);
686
687 ne_buffer_destroy(req->headers);
688
689 NE_DEBUG(NE_DBG_CORE, "Running destroy hooks.");
690 for (hk = req->session->destroy_req_hooks; hk; hk = next_hk) {
691 ne_destroy_req_fn fn = (ne_destroy_req_fn)hk->fn;
692 next_hk = hk->next;
693 fn(req, hk->userdata);
694 }
695
696 for (hk = req->private; hk; hk = next_hk) {
697 next_hk = hk->next;
698 ne_free(hk);
699 }
700
701 if (req->status.reason_phrase)
702 ne_free(req->status.reason_phrase);
703
704 NE_DEBUG(NE_DBG_CORE, "Request ends.");
705 ne_free(req);
706 }
707
708
709 /* Reads a block of the response into BUFFER, which is of size
710 * *BUFLEN. Returns zero on success or non-zero on error. On
711 * success, *BUFLEN is updated to be the number of bytes read into
712 * BUFFER (which will be 0 to indicate the end of the repsonse). On
713 * error, the connection is closed and the session error string is
714 * set. */
715 static int read_response_block(ne_request *req, struct ne_response *resp,
716 char *buffer, size_t *buflen)
717 {
718 ne_socket *const sock = req->session->socket;
719 size_t willread;
720 ssize_t readlen;
721
722 switch (resp->mode) {
723 case R_CHUNKED:
724 /* Chunked transfer-encoding: chunk syntax is "SIZE CRLF CHUNK
725 * CRLF SIZE CRLF CHUNK CRLF ..." followed by zero-length
726 * chunk: "CHUNK CRLF 0 CRLF". resp.chunk.remain contains the
727 * number of bytes left to read in the current chunk. */
728 if (resp->body.chunk.remain == 0) {
729 unsigned long chunk_len;
730 char *ptr;
731
732 /* Read the chunk size line into a temporary buffer. */
733 SOCK_ERR(req,
734 ne_sock_readline(sock, req->respbuf, sizeof req->respbuf),
735 _("Could not read chunk size"));
736 NE_DEBUG(NE_DBG_HTTP, "[chunk] < %s", req->respbuf);
737 chunk_len = strtoul(req->respbuf, &ptr, 16);
738 /* limit chunk size to <= UINT_MAX, so it will probably
739 * fit in a size_t. */
740 if (ptr == req->respbuf ||
741 chunk_len == ULONG_MAX || chunk_len > UINT_MAX) {
742 return aborted(req, _("Could not parse chunk size"), 0);
743 }
744 NE_DEBUG(NE_DBG_HTTP, "Got chunk size: %lu", chunk_len);
745 resp->body.chunk.remain = chunk_len;
746 }
747 willread = resp->body.chunk.remain > *buflen
748 ? *buflen : resp->body.chunk.remain;
749 break;
750 case R_CLENGTH:
751 willread = resp->body.clen.remain > (off_t)*buflen
752 ? *buflen : (size_t)resp->body.clen.remain;
753 break;
754 case R_TILLEOF:
755 willread = *buflen;
756 break;
757 case R_NO_BODY:
758 default:
759 willread = 0;
760 break;
761 }
762 if (willread == 0) {
763 *buflen = 0;
764 return 0;
765 }
766 NE_DEBUG(NE_DBG_CORE,
767 "Reading %" NE_FMT_SIZE_T " bytes of response body.\n", willread);
768 readlen = ne_sock_read(sock, buffer, willread);
769
770 /* EOF is only valid when response body is delimited by it.
771 * Strictly, an SSL truncation should not be treated as an EOF in
772 * any case, but SSL servers are just too buggy. */
773 if (resp->mode == R_TILLEOF &&
774 (readlen == NE_SOCK_CLOSED || readlen == NE_SOCK_TRUNC)) {
775 NE_DEBUG(NE_DBG_HTTP, "Got EOF.");
776 req->can_persist = 0;
777 readlen = 0;
778 } else if (readlen < 0) {
779 return aborted(req, _("Could not read response body"), readlen);
780 } else {
781 NE_DEBUG(NE_DBG_CORE, "Got %" NE_FMT_SSIZE_T " bytes.", readlen);
782 }
783 /* safe to cast: readlen guaranteed to be >= 0 above */
784 *buflen = (size_t)readlen;
785 NE_DEBUG(NE_DBG_HTTPBODY,
786 "Read block (%" NE_FMT_SSIZE_T " bytes):\n[%.*s]\n",
787 readlen, (int)readlen, buffer);
788 if (resp->mode == R_CHUNKED) {
789 resp->body.chunk.remain -= readlen;
790 if (resp->body.chunk.remain == 0) {
791 char crlfbuf[2];
792 /* If we've read a whole chunk, read a CRLF */
793 readlen = ne_sock_fullread(sock, crlfbuf, 2);
794 if (readlen < 0)
795 return aborted(req, _("Could not read chunk delimiter"),
796 readlen);
797 else if (crlfbuf[0] != '\r' || crlfbuf[1] != '\n')
798 return aborted(req, _("Chunk delimiter was invalid"), 0);
799 }
800 } else if (resp->mode == R_CLENGTH) {
801 resp->body.clen.remain -= readlen;
802 }
803 resp->progress += readlen;
804 return NE_OK;
805 }
806
807 ssize_t ne_read_response_block(ne_request *req, char *buffer, size_t buflen)
808 {
809 struct body_reader *rdr;
810 size_t readlen = buflen;
811 struct ne_response *const resp = &req->resp;
812
813 if (read_response_block(req, resp, buffer, &readlen))
814 return -1;
815
816 if (readlen) {
817 req->session->status.sr.progress += readlen;
818 notify_status(req->session, ne_status_recving);
819 }
820
821 for (rdr = req->body_readers; rdr!=NULL; rdr=rdr->next) {
822 if (rdr->use && rdr->handler(rdr->userdata, buffer, readlen) != 0) {
823 ne_close_connection(req->session);
824 return -1;
825 }
826 }
827
828 return readlen;
829 }
830
831 /* Build the request string, returning the buffer. */
832 static ne_buffer *build_request(ne_request *req)
833 {
834 struct hook *hk;
835 ne_buffer *buf = ne_buffer_create();
836
837 /* Add Request-Line and headers: */
838 ne_buffer_concat(buf, req->method, " ", req->uri, " HTTP/1.1" EOL, NULL);
839
840 /* Add custom headers: */
841 ne_buffer_append(buf, req->headers->data, ne_buffer_size(req->headers));
842
843 if (req->body_length && req->flags[NE_REQFLAG_EXPECT100]) {
844 ne_buffer_czappend(buf, "Expect: 100-continue\r\n");
845 }
846
847 NE_DEBUG(NE_DBG_CORE, "Running pre_send hooks");
848 for (hk = req->session->pre_send_hooks; hk!=NULL; hk = hk->next) {
849 ne_pre_send_fn fn = (ne_pre_send_fn)hk->fn;
850 fn(req, hk->userdata, buf);
851 }
852
853 ne_buffer_czappend(buf, "\r\n");
854 return buf;
855 }
856
857 #define MAX_HEADER_LEN (8192)
858
859 #ifdef NE_DEBUGGING
860 #define DEBUG_DUMP_REQUEST(x) dump_request(x)
861
862 static void dump_request(const char *request)
863 {
864 ne_buffer *hdr_dbg_buf = ne_buffer_create();
865 char *hdr_tmp = ne_strdup(request);
866 char* token_saveptr = hdr_tmp;
867 char* token;
868
869 while ((token = strtok_r(token_saveptr, "\n", &token_saveptr)) != NULL) {
870 ne_buffer_concat(hdr_dbg_buf, "> ", token, "\n", NULL);
871 }
872
873 char *hdr_debug = ne_buffer_finish(hdr_dbg_buf);
874
875 if (davix_get_log_scope() & NE_DBG_HTTPPLAIN) {
876 /* Display everything mode */
877 NE_DEBUG(NE_DBG_HDR, "%s", hdr_debug);
878 } else if (davix_get_log_scope() & NE_DBG_HDR) {
879 /* Blank out the Authorization parameters */
880 char *reqdebug = ne_strdup(hdr_debug), *pnt = reqdebug;
881 while ((pnt = strstr(pnt, "Authorization: ")) != NULL) {
882 for (pnt += 15; *pnt != '\r' && *pnt != '\0'; pnt++) {
883 *pnt = 'x';
884 }
885 }
886 NE_DEBUG(NE_DBG_HDR, "%s",reqdebug);
887 ne_free(reqdebug);
888 }
889
890 ne_free(hdr_debug);
891 ne_free(hdr_tmp);
892 }
893
894 #else
895 #define DEBUG_DUMP_REQUEST(x)
896 #endif /* DEBUGGING */
897
898 /* remove trailing EOL from 'buf', where strlen(buf) == *len. *len is
899 * adjusted in accordance with any changes made to the string to
900 * remain equal to strlen(buf). */
901 static inline void strip_eol(char *buf, ssize_t *len)
902 {
903 char *pnt = buf + *len - 1;
904 while (pnt >= buf && (*pnt == '\r' || *pnt == '\n')) {
905 *pnt-- = '\0';
906 (*len)--;
907 }
908 }
909
910 /* Read and parse response status-line into 'status'. 'retry' is non-zero
911 * if an NE_RETRY should be returned if an EOF is received. */
912 static int read_status_line(ne_request *req, ne_status *status, int retry)
913 {
914 char *buffer = req->respbuf;
915 ssize_t ret;
916
917 ret = ne_sock_readline(req->session->socket, buffer, sizeof req->respbuf);
918 if (ret <= 0) {
919 int aret = aborted(req, _("Could not read status line"), ret);
920 return RETRY_RET(retry, ret, aret);
921 }
922
923 strip_eol(buffer, &ret);
924 NE_DEBUG(NE_DBG_HDR, "< %s", buffer);
925
926 if (status->reason_phrase) ne_free(status->reason_phrase);
927 memset(status, 0, sizeof *status);
928
929 /* Hack to allow ShoutCast-style servers, if requested. */
930 if (req->session->flags[NE_SESSFLAG_ICYPROTO]
931 && strncmp(buffer, "ICY ", 4) == 0 && strlen(buffer) > 8
932 && buffer[7] == ' ') {
933 status->code = atoi(buffer + 4);
934 status->major_version = 1;
935 status->minor_version = 0;
936 status->reason_phrase = ne_strclean(ne_strdup(buffer + 8));
937 status->klass = buffer[4] - '0';
938 NE_DEBUG(NE_DBG_HTTP, "[status-line] ICY protocol; code %d",
939 status->code);
940 } else if (ne_parse_statusline(buffer, status)) {
941 return aborted(req, _("Could not parse response status line"), 0);
942 }
943
944 return 0;
945 }
946
947 /* Discard a set of message headers. */
948 static int discard_headers(ne_request *req)
949 {
950 do {
951 SOCK_ERR(req, ne_sock_readline(req->session->socket, req->respbuf,
952 sizeof req->respbuf),
953 _("Could not read interim response headers"));
954 NE_DEBUG(NE_DBG_HTTP, "[discard] < %s", req->respbuf);
955 } while (strcmp(req->respbuf, EOL) != 0);
956 return NE_OK;
957 }
958
959 /* Send the request, and read the response Status-Line. Returns:
960 * NE_RETRY connection closed by server; persistent connection
961 * timeout
962 * NE_OK success
963 * NE_* error
964 * On NE_RETRY and NE_* responses, the connection will have been
965 * closed already.
966 */
967 static int send_request(ne_request *req, const ne_buffer *request)
968 {
969 ne_session *const sess = req->session;
970 ne_status *const status = &req->status;
971 int sentbody = 0; /* zero until body has been sent. */
972 int ret, retry; /* retry non-zero whilst the request should be retried */
973 ssize_t sret;
974
975 /* Send the Request-Line and headers */
976 NE_DEBUG(NE_DBG_CORE, "Sending request-line and headers:");
977 /* Open the connection if necessary */
978 ret = open_connection(sess);
979 if (ret) return ret;
980
981 /* Allow retry if a persistent connection has been used. */
982 retry = sess->persisted;
983
984 sret = ne_sock_fullwrite(req->session->socket, request->data,
985 ne_buffer_size(request));
986 if (sret < 0) {
987 int aret = aborted(req, _("Could not send request"), sret);
988 return RETRY_RET(retry, sret, aret);
989 }
990
991 if (!req->flags[NE_REQFLAG_EXPECT100] && req->body_length > 0) {
992 /* Send request body, if not using 100-continue. */
993 ret = send_request_body(req, retry);
994 if (ret) {
995 return ret;
996 }
997 }
998
999 NE_DEBUG(NE_DBG_CORE, "Request sent; retry is %d.", retry);
1000
1001 /* Loop eating interim 1xx responses (RFC2616 says these MAY be
1002 * sent by the server, even if 100-continue is not used). */
1003 while ((ret = read_status_line(req, status, retry)) == NE_OK
1004 && status->klass == 1) {
1005 NE_DEBUG(NE_DBG_HTTP, "Interim %d response.", status->code);
1006 retry = 0; /* successful read() => never retry now. */
1007 /* Discard headers with the interim response. */
1008 if ((ret = discard_headers(req)) != NE_OK) break;
1009
1010 if (req->flags[NE_REQFLAG_EXPECT100] && (status->code == 100)
1011 && req->body_length > 0 && !sentbody) {
1012 /* Send the body after receiving the first 100 Continue */
1013 if ((ret = send_request_body(req, 0)) != NE_OK) break;
1014 sentbody = 1;
1015 }
1016 }
1017
1018 return ret;
1019 }
1020
1021 /* Read a message header from sock into buf, which has size 'buflen'.
1022 *
1023 * Returns:
1024 * NE_RETRY: Success, read a header into buf.
1025 * NE_OK: End of headers reached.
1026 * NE_ERROR: Error (session error is set, connection closed).
1027 */
1028 static int read_message_header(ne_request *req, char *buf, size_t buflen)
1029 {
1030 ssize_t n;
1031 ne_socket *sock = req->session->socket;
1032
1033 n = ne_sock_readline(sock, buf, buflen);
1034 if (n <= 0)
1035 return aborted(req, _("Error reading response headers"), n);
1036
1037 strip_eol(buf, &n);
1038 NE_DEBUG(NE_DBG_HDR, "< %s", buf);
1039
1040 if (n == 0) {
1041 NE_DEBUG(NE_DBG_CORE, "End of headers.");
1042 return NE_OK;
1043 }
1044
1045 buf += n;
1046 buflen -= n;
1047
1048 while (buflen > 0) {
1049 char ch;
1050
1051 /* Collect any extra lines into buffer */
1052 SOCK_ERR(req, ne_sock_peek(sock, &ch, 1),
1053 _("Error reading response headers"));
1054
1055 if (ch != ' ' && ch != '\t') {
1056 /* No continuation of this header: stop reading. */
1057 return NE_RETRY;
1058 }
1059
1060 /* Otherwise, read the next line onto the end of 'buf'. */
1061 n = ne_sock_readline(sock, buf, buflen);
1062 if (n <= 0) {
1063 return aborted(req, _("Error reading response headers"), n);
1064 }
1065
1066 NE_DEBUG(NE_DBG_HTTP, "[cont] %s", buf);
1067
1068 strip_eol(buf, &n);
1069
1070 /* assert(buf[0] == ch), which implies len(buf) > 0.
1071 * Otherwise the TCP stack is lying, but we'll be paranoid.
1072 * This might be a \t, so replace it with a space to be
1073 * friendly to applications (2616 says we MAY do this). */
1074 if (n) buf[0] = ' ';
1075
1076 /* ready for the next header. */
1077 buf += n;
1078 buflen -= n;
1079 }
1080
1081 ne_set_error(req->session, _("Response header too long"));
1082 return NE_ERROR;
1083 }
1084
1085
1086
1087 /* Add a respnose header field for the given request, using
1088 * precalculated hash value. */
1089 static void add_response_header(ne_request *req, unsigned int hash,
1090 char *name, char *value)
1091 {
1092 struct field **nextf = &req->response_headers[hash];
1093 size_t vlen = strlen(value);
1094
1095 while (*nextf) {
1096 struct field *const f = *nextf;
1097 if (strcmp(f->name, name) == 0) {
1098 if (vlen + f->vlen < MAX_HEADER_LEN) {
1099 /* merge the header field */
1100 f->value = ne_realloc(f->value, f->vlen + vlen + 3);
1101 memcpy(f->value + f->vlen, ", ", 2);
1102 memcpy(f->value + f->vlen + 2, value, vlen + 1);
1103 f->vlen += vlen + 2;
1104 }
1105 return;
1106 }
1107 nextf = &f->next;
1108 }
1109
1110 (*nextf) = ne_malloc(sizeof **nextf);
1111 (*nextf)->name = ne_strdup(name);
1112 (*nextf)->value = ne_strdup(value);
1113 (*nextf)->vlen = vlen;
1114 (*nextf)->next = NULL;
1115 }
1116
1117 /* Read response headers. Returns NE_* code, sets session error and
1118 * closes connection on error. */
1119 static int read_response_headers(ne_request *req)
1120 {
1121 char hdr[MAX_HEADER_LEN];
1122 int ret, count = 0;
1123
1124 while ((ret = read_message_header(req, hdr, sizeof hdr)) == NE_RETRY
1125 && ++count < MAX_HEADER_FIELDS) {
1126 char *pnt;
1127 unsigned int hash = 0;
1128
1129 /* Strip any trailing whitespace */
1130 pnt = hdr + strlen(hdr) - 1;
1131 while (pnt > hdr && (*pnt == ' ' || *pnt == '\t'))
1132 *pnt-- = '\0';
1133
1134 /* Convert the header name to lower case and hash it. */
1135 for (pnt = hdr; (*pnt != '\0' && *pnt != ':' &&
1136 *pnt != ' ' && *pnt != '\t'); pnt++) {
1137 *pnt = ne_tolower(*pnt);
1138 hash = HH_ITERATE(hash,*pnt);
1139 }
1140
1141 /* Skip over any whitespace before the colon. */
1142 while (*pnt == ' ' || *pnt == '\t')
1143 *pnt++ = '\0';
1144
1145 /* ignore header lines which lack a ':'. */
1146 if (*pnt != ':')
1147 continue;
1148
1149 /* NUL-terminate at the colon (when no whitespace before) */
1150 *pnt++ = '\0';
1151
1152 /* Skip any whitespace after the colon... */
1153 while (*pnt == ' ' || *pnt == '\t')
1154 pnt++;
1155
1156 /* pnt now points to the header value. */
1157 //NE_DEBUG(NE_DBG_HTTP, "Header Name: [%s], Value: [%s]", hdr, pnt);
1158 add_response_header(req, hash, hdr, pnt);
1159 }
1160
1161 if (count == MAX_HEADER_FIELDS)
1162 ret = aborted(
1163 req, _("Response exceeded maximum number of header fields"), 0);
1164
1165 return ret;
1166 }
1167
1168 /* Perform any necessary DNS lookup for the host given by *info;
1169 * returns NE_ code with error string set on error. */
1170 static int lookup_host(ne_session *sess, struct host_info *info)
1171 {
1172 NE_DEBUG(NE_DBG_CORE, "Doing DNS lookup on %s...", info->hostname);
1173 sess->status.lu.hostname = info->hostname;
1174 notify_status(sess, ne_status_lookup);
1175 info->address = ne_addr_resolve(info->hostname, 0);
1176 if (ne_addr_result(info->address)) {
1177 char buf[256];
1178 ne_set_error(sess, _("Could not resolve hostname `%s': %s"),
1179 info->hostname,
1180 ne_addr_error(info->address, buf, sizeof buf));
1181 ne_addr_destroy(info->address);
1182 info->address = NULL;
1183 return NE_LOOKUP;
1184 } else {
1185 return NE_OK;
1186 }
1187 }
1188
1189 int ne_begin_request(ne_request *req)
1190 {
1191 struct body_reader *rdr;
1192 ne_buffer *data;
1193 const ne_status *const st = &req->status;
1194 const char *value;
1195 struct hook *hk;
1196 int ret, forced_closure = 0;
1197
1198 /* If a non-idempotent request is sent on a persisted connection,
1199 * then it is impossible to distinguish between a server failure
1200 * and a connection timeout if an EOF/RST is received. So don't
1201 * do that. */
1202
1203 /*
1204 if (!req->flags[NE_REQFLAG_IDEMPOTENT] && req->session->persisted
1205 && !req->session->flags[NE_SESSFLAG_CONNAUTH]) {
1206 NE_DEBUG(NE_DBG_HTTP, "req: Closing connection for non-idempotent "
1207 "request.\n");
1208 ne_close_connection(req->session);
1209 }
1210 */
1211
1212 /* Build the request string, and send it */
1213 data = build_request(req);
1214 if(davix_get_log_scope() & NE_DBG_HDR){
1215 dump_request(data->data);
1216 }
1217 ret = send_request(req, data);
1218 /* Retry this once after a persistent connection timeout. */
1219 if (ret == NE_RETRY) {
1220 NE_DEBUG(NE_DBG_HTTP, "Persistent connection timed out, retrying.");
1221 ret = send_request(req, data);
1222 }
1223 ne_buffer_destroy(data);
1224 if (ret != NE_OK) return ret == NE_RETRY ? NE_ERROR : ret;
1225
1226 /* Determine whether server claims HTTP/1.1 compliance. */
1227 req->session->is_http11 = (st->major_version == 1 &&
1228 st->minor_version > 0) || st->major_version > 1;
1229
1230 /* Persistent connections supported implicitly in HTTP/1.1 */
1231 if (req->session->is_http11) req->can_persist = 1;
1232
1233 ne_set_error(req->session, "%d %s", st->code, st->reason_phrase);
1234
1235 /* Empty the response header hash, in case this request was
1236 * retried: */
1237 free_response_headers(req);
1238
1239 /* Read the headers */
1240 ret = read_response_headers(req);
1241 if (ret) return ret;
1242
1243 /* check the Connection header */
1244 value = get_response_header_hv(req, HH_HV_CONNECTION, "connection");
1245 if (value) {
1246 char *vcopy = ne_strdup(value), *ptr = vcopy;
1247
1248 do {
1249 char *token = ne_shave(ne_token(&ptr, ','), " \t");
1250 unsigned int hash = hash_and_lower(token);
1251
1252 if (strcmp(token, "close") == 0) {
1253 req->can_persist = 0;
1254 forced_closure = 1;
1255 } else if (strcmp(token, "keep-alive") == 0) {
1256 req->can_persist = 1;
1257 } else if (!req->session->is_http11
1258 && strcmp(token, "connection")) {
1259 /* Strip the header per 2616§14.10, last para. Avoid
1260 * danger from "Connection: connection". */
1261 remove_response_header(req, token, hash);
1262 }
1263 } while (ptr);
1264
1265 ne_free(vcopy);
1266 }
1267
1268 /* Support "Proxy-Connection: keep-alive" for compatibility with
1269 * some HTTP/1.0 proxies; it is risky to do this, because an
1270 * intermediary proxy may not support this HTTP/1.0 extension, but
1271 * will not strip the header either. Persistent connection
1272 * support is enabled based on the presence of this header if:
1273 * a) it is *necessary* to do so due to the use of a connection-auth
1274 * scheme, and
1275 * b) connection closure was not forced via "Connection: close". */
1276 if (req->session->nexthop->proxy == PROXY_HTTP && !req->session->is_http11
1277 && !forced_closure && req->session->flags[NE_SESSFLAG_CONNAUTH]) {
1278 value = get_response_header_hv(req, HH_HV_PROXY_CONNECTION,
1279 "proxy-connection");
1280 if (value && ne_strcasecmp(value, "keep-alive") == 0) {
1281 NE_DEBUG(NE_DBG_HTTP, "req: Using persistent connection "
1282 "for HTTP/1.0 proxy requiring conn-auth hack.\n");
1283 req->can_persist = 1;
1284 }
1285 }
1286
1287 /* Decide which method determines the response message-length per
1288 * 2616§4.4 (multipart/byteranges is not supported): */
1289
1290 #ifdef NE_HAVE_SSL
1291 /* Special case for CONNECT handling: the response has no body,
1292 * and the connection can persist. */
1293 if (req->session->in_connect && st->klass == 2) {
1294 req->resp.mode = R_NO_BODY;
1295 req->can_persist = 1;
1296 } else
1297 #endif
1298 /* HEAD requests and 204, 304 responses have no response body,
1299 * regardless of what headers are present. */
1300 if (req->method_is_head || st->code == 204 || st->code == 304) {
1301 req->resp.mode = R_NO_BODY;
1302 }
1303 /* Broken intermediaries exist which use "transfer-encoding: identity"
1304 * to mean "no transfer-coding". So that case must be ignored. */
1305 else if ((value = get_response_header_hv(req, HH_HV_TRANSFER_ENCODING,
1306 "transfer-encoding")) != NULL
1307 && ne_strcasecmp(value, "identity") != 0) {
1308 /* Otherwise, fail iff an unknown transfer-coding is used. */
1309 if (ne_strcasecmp(value, "chunked") == 0) {
1310 req->resp.mode = R_CHUNKED;
1311 req->resp.body.chunk.remain = 0;
1312 }
1313 else {
1314 return aborted(req, _("Unknown transfer-coding in response"), 0);
1315 }
1316 }
1317 else if ((value = get_response_header_hv(req, HH_HV_CONTENT_LENGTH,
1318 "content-length")) != NULL) {
1319 char *endptr = NULL;
1320 ne_off_t len = ne_strtoff(value, &endptr, 10);
1321
1322 if (*value && len != NE_OFFT_MAX && len >= 0 && endptr && *endptr == '\0') {
1323 req->resp.mode = R_CLENGTH;
1324 req->resp.body.clen.total = req->resp.body.clen.remain = len;
1325 } else {
1326 /* fail for an invalid content-length header. */
1327 return aborted(req, _("Invalid Content-Length in response"), 0);
1328 }
1329 } else {
1330 req->resp.mode = R_TILLEOF; /* otherwise: read-till-eof mode */
1331 }
1332
1333 NE_DEBUG(NE_DBG_CORE, "Running post_headers hooks");
1334 for (hk = req->session->post_headers_hooks; hk != NULL; hk = hk->next) {
1335 ne_post_headers_fn fn = (ne_post_headers_fn)hk->fn;
1336 fn(req, hk->userdata, &req->status);
1337 }
1338
1339 /* Prepare for reading the response entity-body. Call each of the
1340 * body readers and ask them whether they want to accept this
1341 * response or not. */
1342 for (rdr = req->body_readers; rdr != NULL; rdr=rdr->next) {
1343 rdr->use = rdr->accept_response(rdr->userdata, req, st);
1344 }
1345
1346 req->session->status.sr.progress = 0;
1347 req->session->status.sr.total =
1348 req->resp.mode == R_CLENGTH ? req->resp.body.clen.total : -1;
1349 notify_status(req->session, ne_status_recving);
1350
1351 return NE_OK;
1352 }
1353
1354 int ne_end_request(ne_request *req)
1355 {
1356 struct hook *hk;
1357 int ret;
1358
1359 /* Read headers in chunked trailers */
1360 if (req->resp.mode == R_CHUNKED) {
1361 ret = read_response_headers(req);
1362 if (ret) return ret;
1363 } else {
1364 ret = NE_OK;
1365 }
1366
1367 NE_DEBUG(NE_DBG_CORE, "Running post_send hooks");
1368 for (hk = req->session->post_send_hooks;
1369 ret == NE_OK && hk != NULL; hk = hk->next) {
1370 ne_post_send_fn fn = (ne_post_send_fn)hk->fn;
1371 ret = fn(req, hk->userdata, &req->status);
1372 }
1373
1374 /* Close the connection if persistent connections are disabled or
1375 * not supported by the server. */
1376 if (!req->session->flags[NE_SESSFLAG_PERSIST] || !req->can_persist)
1377 ne_close_connection(req->session);
1378 else
1379 req->session->persisted = 1;
1380
1381 return ret;
1382 }
1383
1384
1385 int ne_abort_request(ne_request* req){
1386 if(req->session)
1387 ne_close_connection(req->session);
1388 return 0;
1389 }
1390
1391 int ne_read_response_to_fd(ne_request *req, int fd)
1392 {
1393 ssize_t len;
1394
1395 while ((len = ne_read_response_block(req, req->respbuf,
1396 sizeof req->respbuf)) > 0) {
1397 const char *block = req->respbuf;
1398
1399 do {
1400 ssize_t ret = write(fd, block, len);
1401 if (ret == -1 && errno == EINTR) {
1402 continue;
1403 } else if (ret < 0) {
1404 char err[200];
1405 ne_strerror(errno, err, sizeof err);
1406 ne_set_error(ne_get_session(req),
1407 _("Could not write to file: %s"), err);
1408 return NE_ERROR;
1409 } else {
1410 len -= ret;
1411 block += ret;
1412 }
1413 } while (len > 0);
1414 }
1415
1416 return len == 0 ? NE_OK : NE_ERROR;
1417 }
1418
1419 int ne_discard_response(ne_request *req)
1420 {
1421 ssize_t len;
1422
1423 do {
1424 len = ne_read_response_block(req, req->respbuf, sizeof req->respbuf);
1425 } while (len > 0);
1426
1427 return len == 0 ? NE_OK : NE_ERROR;
1428 }
1429
1430 int ne_request_dispatch(ne_request *req)
1431 {
1432 int ret;
1433
1434 do {
1435 ret = ne_begin_request(req);
1436 if (ret == NE_OK) ret = ne_discard_response(req);
1437 if (ret == NE_OK) ret = ne_end_request(req);
1438 } while (ret == NE_RETRY);
1439
1440 NE_DEBUG(NE_DBG_HTTP | NE_DBG_FLUSH,
1441 "Request ends, status %d class %dxx, error line:\n%s\n",
1442 req->status.code, req->status.klass, req->session->error);
1443
1444 return ret;
1445 }
1446
1447 const ne_status *ne_get_status(const ne_request *req)
1448 {
1449 return &req->status;
1450 }
1451
1452 ne_session *ne_get_session(const ne_request *req)
1453 {
1454 return req->session;
1455 }
1456
1457 #ifdef NE_HAVE_SSL
1458 /* Create a CONNECT tunnel through the proxy server.
1459 * Returns HTTP_* */
1460 static int proxy_tunnel(ne_session *sess)
1461 {
1462 /* Hack up an HTTP CONNECT request... */
1463 ne_request *req;
1464 int ret = NE_OK;
1465 char ruri[200];
1466
1467 /* Can't use server.hostport here; Request-URI must include `:port' */
1468 ne_snprintf(ruri, sizeof ruri, "%s:%u", sess->server.hostname,
1469 sess->server.port);
1470 req = ne_request_create(sess, "CONNECT", ruri);
1471
1472 sess->in_connect = 1;
1473 ret = ne_request_dispatch(req);
1474 sess->in_connect = 0;
1475
1476 sess->persisted = 0; /* don't treat this is a persistent connection. */
1477
1478 if (ret != NE_OK || !sess->connected || req->status.klass != 2) {
1479 char *err = ne_strdup(sess->error);
1480 ne_set_error(sess, _("Could not create SSL connection "
1481 "through proxy server: %s"), err);
1482 ne_free(err);
1483 if (ret == NE_OK) ret = NE_ERROR;
1484 }
1485
1486 ne_request_destroy(req);
1487 return ret;
1488 }
1489 #endif
1490
1491 /* Return the first resolved address for the given host. */
1492 static const ne_inet_addr *resolve_first(struct host_info *host)
1493 {
1494 return host->network ? host->network : ne_addr_first(host->address);
1495 }
1496
1497 /* Return the next resolved address for the given host or NULL if
1498 * there are no more addresses. */
1499 static const ne_inet_addr *resolve_next(struct host_info *host)
1500 {
1501 return host->network ? NULL : ne_addr_next(host->address);
1502 }
1503
1504 /* Make new TCP connection to server at 'host' of type 'name'. Note
1505 * that once a connection to a particular network address has
1506 * succeeded, that address will be used first for the next attempt to
1507 * connect. */
1508 static int do_connect(ne_session *sess, struct host_info *host)
1509 {
1510 int ret;
1511
1512 /* Resolve hostname if necessary. */
1513 if (host->address == NULL && host->network == NULL) {
1514 ret = lookup_host(sess, host);
1515 if (ret) return ret;
1516 }
1517
1518 if ((sess->socket = ne_sock_create()) == NULL) {
1519 ne_set_error(sess, _("Could not create socket"));
1520 return NE_ERROR;
1521 }
1522
1523 if (sess->cotimeout)
1524 ne_sock_connect_timeout(sess->socket, sess->cotimeout);
1525
1526 if (sess->local_addr)
1527 ne_sock_prebind(sess->socket, sess->local_addr, 0);
1528
1529 if (host->current == NULL)
1530 host->current = resolve_first(host);
1531
1532 sess->status.ci.hostname = host->hostname;
1533
1534 struct timespec deadline_timeout;
1535 if(sess->rdtimeout > 0){
1536 davix_get_monotonic_time(&deadline_timeout);
1537 deadline_timeout.tv_sec += sess->rdtimeout;
1538 }
1539
1540 do {
1541
1542 sess->status.ci.address = host->current;
1543 notify_status(sess, ne_status_connecting);
1544 #ifdef NE_DEBUGGING
1545 if (ne_debug_mask & NE_DBG_HTTP) {
1546 char buf[150];
1547 NE_DEBUG(NE_DBG_HTTP, "req: Connecting to %s:%u",
1548 ne_iaddr_print(host->current, buf, sizeof buf),
1549 host->port);
1550 }
1551 #endif
1552 ret = ne_sock_connect(sess->socket, host->current, host->port);
1553
1554 if(ret && sess->rdtimeout > 0){
1555 struct timespec current_time;
1556 davix_get_monotonic_time(&current_time);
1557 if(current_time.tv_sec > deadline_timeout.tv_sec){
1558 ret= NE_SOCK_TIMEOUT;
1559 break;
1560 }
1561 }
1562
1563 } while (ret && /* try the next address... */
1564 (host->current = resolve_next(host)) != NULL);
1565
1566 if (ret) {
1567 const char *msg;
1568
1569 if (host->proxy == PROXY_NONE)
1570 msg = _("Could not connect to server");
1571 else
1572 msg = _("Could not connect to proxy server");
1573
1574 ne_set_error(sess, "%s: %s", msg, ne_sock_error(sess->socket));
1575 ne_sock_close(sess->socket);
1576 return ret == NE_SOCK_TIMEOUT ? NE_TIMEOUT : NE_CONNECT;
1577 }
1578
1579 if (sess->rdtimeout)
1580 ne_sock_read_timeout(sess->socket, sess->rdtimeout);
1581
1582 notify_status(sess, ne_status_connected);
1583 sess->nexthop = host;
1584
1585 sess->connected = 1;
1586 /* clear persistent connection flag. */
1587 sess->persisted = 0;
1588 return NE_OK;
1589 }
1590
1591 /* For a SOCKSv4 proxy only, the IP address of the origin server (in
1592 * addition to the proxy) must be known, and must be an IPv4 address.
1593 * Returns NE_*; connection closed and error string set on error. */
1594 static int socks_origin_lookup(ne_session *sess)
1595 {
1596 const ne_inet_addr *ia;
1597 int ret;
1598
1599 ret = lookup_host(sess, &sess->server);
1600 if (ret) {
1601 /* lookup_host already set the error string. */
1602 ne_close_connection(sess);
1603 return ret;
1604 }
1605
1606 /* Find the first IPv4 address available for the server. */
1607 for (ia = ne_addr_first(sess->server.address);
1608 ia && ne_iaddr_typeof(ia) == ne_iaddr_ipv6;
1609 ia = ne_addr_next(sess->server.address)) {
1610 /* noop */
1611 }
1612
1613 /* ... if any */
1614 if (ia == NULL) {
1615 ne_set_error(sess, _("Could not find IPv4 address of "
1616 "hostname %s for SOCKS v4 proxy"),
1617 sess->server.hostname);
1618 ne_close_connection(sess);
1619 return NE_LOOKUP;
1620 }
1621
1622 sess->server.current = ia;
1623
1624 return ret;
1625 }
1626
1627 static int open_connection(ne_session *sess)
1628 {
1629 int ret;
1630
1631 if (sess->connected) return NE_OK;
1632
1633 if (!sess->proxies) {
1634 ret = do_connect(sess, &sess->server);
1635 if (ret) {
1636 sess->nexthop = NULL;
1637 return ret;
1638 }
1639 }
1640 else {
1641 struct host_info *hi;
1642
1643 /* Attempt to re-use proxy to avoid iterating through
1644 * unnecessarily. */
1645 if (sess->prev_proxy)
1646 ret = do_connect(sess, sess->prev_proxy);
1647 else
1648 ret = NE_ERROR;
1649
1650 /* Otherwise, try everything - but omitting prev_proxy if that
1651 * has already been tried. */
1652 for (hi = sess->proxies; hi && ret; hi = hi->next) {
1653 if (hi != sess->prev_proxy)
1654 ret = do_connect(sess, hi);
1655 }
1656
1657 if (ret == NE_OK && sess->nexthop->proxy == PROXY_SOCKS) {
1658 /* Special-case for SOCKS v4 proxies, which require the
1659 * client to resolve the origin server IP address. */
1660 if (sess->socks_ver == NE_SOCK_SOCKSV4) {
1661 ret = socks_origin_lookup(sess);
1662 }
1663
1664 if (ret == NE_OK) {
1665 /* Perform the SOCKS handshake, instructing the proxy
1666 * to set up the connection to the origin server. */
1667 ret = ne_sock_proxy(sess->socket, sess->socks_ver,
1668 sess->server.current,
1669 sess->server.hostname, sess->server.port,
1670 sess->socks_user, sess->socks_password);
1671 if (ret) {
1672 ne_set_error(sess,
1673 _("Could not establish connection from "
1674 "SOCKS proxy (%s:%u): %s"),
1675 sess->nexthop->hostname,
1676 sess->nexthop->port,
1677 ne_sock_error(sess->socket));
1678 ne_close_connection(sess);
1679 ret = NE_ERROR;
1680 }
1681 }
1682 }
1683
1684 if (ret != NE_OK) {
1685 sess->nexthop = NULL;
1686 sess->prev_proxy = NULL;
1687 return ret;
1688 }
1689
1690 /* Success - make this proxy stick. */
1691 sess->prev_proxy = hi;
1692 }
1693
1694 #ifdef NE_HAVE_SSL
1695 /* Negotiate SSL layer if required. */
1696 if (sess->use_ssl && !sess->in_connect) {
1697 /* Set up CONNECT tunnel if using an HTTP proxy. */
1698 if (sess->nexthop->proxy == PROXY_HTTP)
1699 ret = proxy_tunnel(sess);
1700
1701 if (ret == NE_OK) {
1702 ret = ne__negotiate_ssl(sess);
1703 if (ret != NE_OK)
1704 ne_close_connection(sess);
1705 }
1706 }
1707 #endif
1708
1709 return ret;
1710 }