]> granicus.if.org Git - libevent/blob - http.c
http: fix connection retries when there more then one request for connection
[libevent] / http.c
1 /*
2  * Copyright (c) 2002-2007 Niels Provos <provos@citi.umich.edu>
3  * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #include "event2/event-config.h"
29 #include "evconfig-private.h"
30
31 #ifdef EVENT__HAVE_SYS_PARAM_H
32 #include <sys/param.h>
33 #endif
34 #ifdef EVENT__HAVE_SYS_TYPES_H
35 #include <sys/types.h>
36 #endif
37
38 #ifdef HAVE_SYS_IOCCOM_H
39 #include <sys/ioccom.h>
40 #endif
41 #ifdef EVENT__HAVE_SYS_RESOURCE_H
42 #include <sys/resource.h>
43 #endif
44 #ifdef EVENT__HAVE_SYS_TIME_H
45 #include <sys/time.h>
46 #endif
47 #ifdef EVENT__HAVE_SYS_WAIT_H
48 #include <sys/wait.h>
49 #endif
50
51 #ifndef _WIN32
52 #include <sys/socket.h>
53 #include <sys/stat.h>
54 #else
55 #include <winsock2.h>
56 #include <ws2tcpip.h>
57 #endif
58
59 #include <sys/queue.h>
60
61 #ifdef EVENT__HAVE_NETINET_IN_H
62 #include <netinet/in.h>
63 #endif
64 #ifdef EVENT__HAVE_ARPA_INET_H
65 #include <arpa/inet.h>
66 #endif
67 #ifdef EVENT__HAVE_NETDB_H
68 #include <netdb.h>
69 #endif
70
71 #ifdef _WIN32
72 #include <winsock2.h>
73 #endif
74
75 #include <errno.h>
76 #include <stdio.h>
77 #include <stdlib.h>
78 #include <string.h>
79 #ifndef _WIN32
80 #include <syslog.h>
81 #endif
82 #include <signal.h>
83 #ifdef EVENT__HAVE_UNISTD_H
84 #include <unistd.h>
85 #endif
86 #ifdef EVENT__HAVE_FCNTL_H
87 #include <fcntl.h>
88 #endif
89
90 #undef timeout_pending
91 #undef timeout_initialized
92
93 #include "strlcpy-internal.h"
94 #include "event2/http.h"
95 #include "event2/event.h"
96 #include "event2/buffer.h"
97 #include "event2/bufferevent.h"
98 #include "event2/http_struct.h"
99 #include "event2/http_compat.h"
100 #include "event2/util.h"
101 #include "event2/listener.h"
102 #include "log-internal.h"
103 #include "util-internal.h"
104 #include "http-internal.h"
105 #include "mm-internal.h"
106 #include "bufferevent-internal.h"
107
108 #ifndef EVENT__HAVE_GETNAMEINFO
109 #define NI_MAXSERV 32
110 #define NI_MAXHOST 1025
111
112 #ifndef NI_NUMERICHOST
113 #define NI_NUMERICHOST 1
114 #endif
115
116 #ifndef NI_NUMERICSERV
117 #define NI_NUMERICSERV 2
118 #endif
119
120 static int
121 fake_getnameinfo(const struct sockaddr *sa, size_t salen, char *host,
122         size_t hostlen, char *serv, size_t servlen, int flags)
123 {
124         struct sockaddr_in *sin = (struct sockaddr_in *)sa;
125
126         if (serv != NULL) {
127                 char tmpserv[16];
128                 evutil_snprintf(tmpserv, sizeof(tmpserv),
129                     "%d", ntohs(sin->sin_port));
130                 if (strlcpy(serv, tmpserv, servlen) >= servlen)
131                         return (-1);
132         }
133
134         if (host != NULL) {
135                 if (flags & NI_NUMERICHOST) {
136                         if (strlcpy(host, inet_ntoa(sin->sin_addr),
137                             hostlen) >= hostlen)
138                                 return (-1);
139                         else
140                                 return (0);
141                 } else {
142                         struct hostent *hp;
143                         hp = gethostbyaddr((char *)&sin->sin_addr,
144                             sizeof(struct in_addr), AF_INET);
145                         if (hp == NULL)
146                                 return (-2);
147
148                         if (strlcpy(host, hp->h_name, hostlen) >= hostlen)
149                                 return (-1);
150                         else
151                                 return (0);
152                 }
153         }
154         return (0);
155 }
156
157 #endif
158
159 #define REQ_VERSION_BEFORE(req, major_v, minor_v)                       \
160         ((req)->major < (major_v) ||                                    \
161             ((req)->major == (major_v) && (req)->minor < (minor_v)))
162
163 #define REQ_VERSION_ATLEAST(req, major_v, minor_v)                      \
164         ((req)->major > (major_v) ||                                    \
165             ((req)->major == (major_v) && (req)->minor >= (minor_v)))
166
167 #ifndef MIN
168 #define MIN(a,b) (((a)<(b))?(a):(b))
169 #endif
170
171 extern int debug;
172
173 static evutil_socket_t bind_socket_ai(struct evutil_addrinfo *, int reuse);
174 static evutil_socket_t bind_socket(const char *, ev_uint16_t, int reuse);
175 static void name_from_addr(struct sockaddr *, ev_socklen_t, char **, char **);
176 static struct evhttp_uri *evhttp_uri_parse_authority(char *source_uri);
177 static int evhttp_associate_new_request_with_connection(
178         struct evhttp_connection *evcon);
179 static void evhttp_connection_start_detectclose(
180         struct evhttp_connection *evcon);
181 static void evhttp_connection_stop_detectclose(
182         struct evhttp_connection *evcon);
183 static void evhttp_request_dispatch(struct evhttp_connection* evcon);
184 static void evhttp_read_firstline(struct evhttp_connection *evcon,
185                                   struct evhttp_request *req);
186 static void evhttp_read_header(struct evhttp_connection *evcon,
187     struct evhttp_request *req);
188 static int evhttp_add_header_internal(struct evkeyvalq *headers,
189     const char *key, const char *value);
190 static const char *evhttp_response_phrase_internal(int code);
191 static void evhttp_get_request(struct evhttp *, evutil_socket_t, struct sockaddr *, ev_socklen_t);
192 static void evhttp_write_buffer(struct evhttp_connection *,
193     void (*)(struct evhttp_connection *, void *), void *);
194 static void evhttp_make_header(struct evhttp_connection *, struct evhttp_request *);
195
196 /* callbacks for bufferevent */
197 static void evhttp_read_cb(struct bufferevent *, void *);
198 static void evhttp_write_cb(struct bufferevent *, void *);
199 static void evhttp_error_cb(struct bufferevent *bufev, short what, void *arg);
200 static int evhttp_find_vhost(struct evhttp *http, struct evhttp **outhttp,
201                   const char *hostname);
202
203 #ifndef EVENT__HAVE_STRSEP
204 /* strsep replacement for platforms that lack it.  Only works if
205  * del is one character long. */
206 static char *
207 strsep(char **s, const char *del)
208 {
209         char *d, *tok;
210         EVUTIL_ASSERT(strlen(del) == 1);
211         if (!s || !*s)
212                 return NULL;
213         tok = *s;
214         d = strstr(tok, del);
215         if (d) {
216                 *d = '\0';
217                 *s = d + 1;
218         } else
219                 *s = NULL;
220         return tok;
221 }
222 #endif
223
224 static size_t
225 html_replace(const char ch, const char **escaped)
226 {
227         switch (ch) {
228         case '<':
229                 *escaped = "&lt;";
230                 return 4;
231         case '>':
232                 *escaped = "&gt;";
233                 return 4;
234         case '"':
235                 *escaped = "&quot;";
236                 return 6;
237         case '\'':
238                 *escaped = "&#039;";
239                 return 6;
240         case '&':
241                 *escaped = "&amp;";
242                 return 5;
243         default:
244                 break;
245         }
246
247         return 1;
248 }
249
250 /*
251  * Replaces <, >, ", ' and & with &lt;, &gt;, &quot;,
252  * &#039; and &amp; correspondingly.
253  *
254  * The returned string needs to be freed by the caller.
255  */
256
257 char *
258 evhttp_htmlescape(const char *html)
259 {
260         size_t i;
261         size_t new_size = 0, old_size = 0;
262         char *escaped_html, *p;
263
264         if (html == NULL)
265                 return (NULL);
266
267         old_size = strlen(html);
268         for (i = 0; i < old_size; ++i) {
269                 const char *replaced = NULL;
270                 const size_t replace_size = html_replace(html[i], &replaced);
271                 if (replace_size > EV_SIZE_MAX - new_size) {
272                         event_warn("%s: html_replace overflow", __func__);
273                         return (NULL);
274                 }
275                 new_size += replace_size;
276         }
277
278         if (new_size == EV_SIZE_MAX)
279                 return (NULL);
280         p = escaped_html = mm_malloc(new_size + 1);
281         if (escaped_html == NULL) {
282                 event_warn("%s: malloc(%lu)", __func__,
283                            (unsigned long)(new_size + 1));
284                 return (NULL);
285         }
286         for (i = 0; i < old_size; ++i) {
287                 const char *replaced = &html[i];
288                 const size_t len = html_replace(html[i], &replaced);
289                 memcpy(p, replaced, len);
290                 p += len;
291         }
292
293         *p = '\0';
294
295         return (escaped_html);
296 }
297
298 /** Given an evhttp_cmd_type, returns a constant string containing the
299  * equivalent HTTP command, or NULL if the evhttp_command_type is
300  * unrecognized. */
301 static const char *
302 evhttp_method(enum evhttp_cmd_type type)
303 {
304         const char *method;
305
306         switch (type) {
307         case EVHTTP_REQ_GET:
308                 method = "GET";
309                 break;
310         case EVHTTP_REQ_POST:
311                 method = "POST";
312                 break;
313         case EVHTTP_REQ_HEAD:
314                 method = "HEAD";
315                 break;
316         case EVHTTP_REQ_PUT:
317                 method = "PUT";
318                 break;
319         case EVHTTP_REQ_DELETE:
320                 method = "DELETE";
321                 break;
322         case EVHTTP_REQ_OPTIONS:
323                 method = "OPTIONS";
324                 break;
325         case EVHTTP_REQ_TRACE:
326                 method = "TRACE";
327                 break;
328         case EVHTTP_REQ_CONNECT:
329                 method = "CONNECT";
330                 break;
331         case EVHTTP_REQ_PATCH:
332                 method = "PATCH";
333                 break;
334         default:
335                 method = NULL;
336                 break;
337         }
338
339         return (method);
340 }
341
342 /**
343  * Determines if a response should have a body.
344  * Follows the rules in RFC 2616 section 4.3.
345  * @return 1 if the response MUST have a body; 0 if the response MUST NOT have
346  *     a body.
347  */
348 static int
349 evhttp_response_needs_body(struct evhttp_request *req)
350 {
351         return (req->response_code != HTTP_NOCONTENT &&
352                 req->response_code != HTTP_NOTMODIFIED &&
353                 (req->response_code < 100 || req->response_code >= 200) &&
354                 req->type != EVHTTP_REQ_HEAD);
355 }
356
357 /** Helper: called after we've added some data to an evcon's bufferevent's
358  * output buffer.  Sets the evconn's writing-is-done callback, and puts
359  * the bufferevent into writing mode.
360  */
361 static void
362 evhttp_write_buffer(struct evhttp_connection *evcon,
363     void (*cb)(struct evhttp_connection *, void *), void *arg)
364 {
365         event_debug(("%s: preparing to write buffer\n", __func__));
366
367         /* Set call back */
368         evcon->cb = cb;
369         evcon->cb_arg = arg;
370
371         /* Disable the read callback: we don't actually care about data;
372          * we only care about close detection. (We don't disable reading --
373          * EV_READ, since we *do* want to learn about any close events.) */
374         bufferevent_setcb(evcon->bufev,
375             NULL, /*read*/
376             evhttp_write_cb,
377             evhttp_error_cb,
378             evcon);
379
380         bufferevent_enable(evcon->bufev, EV_READ|EV_WRITE);
381 }
382
383 static void
384 evhttp_send_continue_done(struct evhttp_connection *evcon, void *arg)
385 {
386         bufferevent_disable(evcon->bufev, EV_WRITE);
387 }
388
389 static void
390 evhttp_send_continue(struct evhttp_connection *evcon,
391                         struct evhttp_request *req)
392 {
393         bufferevent_enable(evcon->bufev, EV_WRITE);
394         evbuffer_add_printf(bufferevent_get_output(evcon->bufev),
395                         "HTTP/%d.%d 100 Continue\r\n\r\n",
396                         req->major, req->minor);
397         evcon->cb = evhttp_send_continue_done;
398         evcon->cb_arg = NULL;
399         bufferevent_setcb(evcon->bufev,
400             evhttp_read_cb,
401             evhttp_write_cb,
402             evhttp_error_cb,
403             evcon);
404 }
405
406 /** Helper: returns true iff evconn is in any connected state. */
407 static int
408 evhttp_connected(struct evhttp_connection *evcon)
409 {
410         switch (evcon->state) {
411         case EVCON_DISCONNECTED:
412         case EVCON_CONNECTING:
413                 return (0);
414         case EVCON_IDLE:
415         case EVCON_READING_FIRSTLINE:
416         case EVCON_READING_HEADERS:
417         case EVCON_READING_BODY:
418         case EVCON_READING_TRAILER:
419         case EVCON_WRITING:
420         default:
421                 return (1);
422         }
423 }
424
425 /* Create the headers needed for an outgoing HTTP request, adds them to
426  * the request's header list, and writes the request line to the
427  * connection's output buffer.
428  */
429 static void
430 evhttp_make_header_request(struct evhttp_connection *evcon,
431     struct evhttp_request *req)
432 {
433         const char *method;
434
435         evhttp_remove_header(req->output_headers, "Proxy-Connection");
436
437         /* Generate request line */
438         if (!(method = evhttp_method(req->type))) {
439                 method = "NULL";
440         }
441
442         evbuffer_add_printf(bufferevent_get_output(evcon->bufev),
443             "%s %s HTTP/%d.%d\r\n",
444             method, req->uri, req->major, req->minor);
445
446         /* Add the content length on a post or put request if missing */
447         if ((req->type == EVHTTP_REQ_POST || req->type == EVHTTP_REQ_PUT) &&
448             evhttp_find_header(req->output_headers, "Content-Length") == NULL){
449                 char size[22];
450                 evutil_snprintf(size, sizeof(size), EV_SIZE_FMT,
451                     EV_SIZE_ARG(evbuffer_get_length(req->output_buffer)));
452                 evhttp_add_header(req->output_headers, "Content-Length", size);
453         }
454 }
455
456 /** Return true if the list of headers in 'headers', intepreted with respect
457  * to flags, means that we should send a "connection: close" when the request
458  * is done. */
459 static int
460 evhttp_is_connection_close(int flags, struct evkeyvalq* headers)
461 {
462         if (flags & EVHTTP_PROXY_REQUEST) {
463                 /* proxy connection */
464                 const char *connection = evhttp_find_header(headers, "Proxy-Connection");
465                 return (connection == NULL || evutil_ascii_strcasecmp(connection, "keep-alive") != 0);
466         } else {
467                 const char *connection = evhttp_find_header(headers, "Connection");
468                 return (connection != NULL && evutil_ascii_strcasecmp(connection, "close") == 0);
469         }
470 }
471 static int
472 evhttp_is_request_connection_close(struct evhttp_request *req)
473 {
474         return
475                 evhttp_is_connection_close(req->flags, req->input_headers) ||
476                 evhttp_is_connection_close(req->flags, req->output_headers);
477 }
478
479 /* Return true iff 'headers' contains 'Connection: keep-alive' */
480 static int
481 evhttp_is_connection_keepalive(struct evkeyvalq* headers)
482 {
483         const char *connection = evhttp_find_header(headers, "Connection");
484         return (connection != NULL
485             && evutil_ascii_strncasecmp(connection, "keep-alive", 10) == 0);
486 }
487
488 /* Add a correct "Date" header to headers, unless it already has one. */
489 static void
490 evhttp_maybe_add_date_header(struct evkeyvalq *headers)
491 {
492         if (evhttp_find_header(headers, "Date") == NULL) {
493                 char date[50];
494                 if (sizeof(date) - evutil_date_rfc1123(date, sizeof(date), NULL) > 0) {
495                         evhttp_add_header(headers, "Date", date);
496                 }
497         }
498 }
499
500 /* Add a "Content-Length" header with value 'content_length' to headers,
501  * unless it already has a content-length or transfer-encoding header. */
502 static void
503 evhttp_maybe_add_content_length_header(struct evkeyvalq *headers,
504     size_t content_length)
505 {
506         if (evhttp_find_header(headers, "Transfer-Encoding") == NULL &&
507             evhttp_find_header(headers, "Content-Length") == NULL) {
508                 char len[22];
509                 evutil_snprintf(len, sizeof(len), EV_SIZE_FMT,
510                     EV_SIZE_ARG(content_length));
511                 evhttp_add_header(headers, "Content-Length", len);
512         }
513 }
514
515 /*
516  * Create the headers needed for an HTTP reply in req->output_headers,
517  * and write the first HTTP response for req line to evcon.
518  */
519 static void
520 evhttp_make_header_response(struct evhttp_connection *evcon,
521     struct evhttp_request *req)
522 {
523         int is_keepalive = evhttp_is_connection_keepalive(req->input_headers);
524         evbuffer_add_printf(bufferevent_get_output(evcon->bufev),
525             "HTTP/%d.%d %d %s\r\n",
526             req->major, req->minor, req->response_code,
527             req->response_code_line);
528
529         if (req->major == 1) {
530                 if (req->minor >= 1)
531                         evhttp_maybe_add_date_header(req->output_headers);
532
533                 /*
534                  * if the protocol is 1.0; and the connection was keep-alive
535                  * we need to add a keep-alive header, too.
536                  */
537                 if (req->minor == 0 && is_keepalive)
538                         evhttp_add_header(req->output_headers,
539                             "Connection", "keep-alive");
540
541                 if ((req->minor >= 1 || is_keepalive) &&
542                     evhttp_response_needs_body(req)) {
543                         /*
544                          * we need to add the content length if the
545                          * user did not give it, this is required for
546                          * persistent connections to work.
547                          */
548                         evhttp_maybe_add_content_length_header(
549                                 req->output_headers,
550                                 evbuffer_get_length(req->output_buffer));
551                 }
552         }
553
554         /* Potentially add headers for unidentified content. */
555         if (evhttp_response_needs_body(req)) {
556                 if (evhttp_find_header(req->output_headers,
557                         "Content-Type") == NULL
558                     && evcon->http_server->default_content_type) {
559                         evhttp_add_header(req->output_headers,
560                             "Content-Type",
561                             evcon->http_server->default_content_type);
562                 }
563         }
564
565         /* if the request asked for a close, we send a close, too */
566         if (evhttp_is_connection_close(req->flags, req->input_headers)) {
567                 evhttp_remove_header(req->output_headers, "Connection");
568                 if (!(req->flags & EVHTTP_PROXY_REQUEST))
569                     evhttp_add_header(req->output_headers, "Connection", "close");
570                 evhttp_remove_header(req->output_headers, "Proxy-Connection");
571         }
572 }
573
574 enum expect { NO, CONTINUE, OTHER };
575 static enum expect evhttp_have_expect(struct evhttp_request *req, int input)
576 {
577         const char *expect;
578         struct evkeyvalq *h = input ? req->input_headers : req->output_headers;
579
580         if (!(req->kind == EVHTTP_REQUEST) || !REQ_VERSION_ATLEAST(req, 1, 1))
581                 return NO;
582
583         expect = evhttp_find_header(h, "Expect");
584         if (!expect)
585                 return NO;
586
587         return !evutil_ascii_strcasecmp(expect, "100-continue") ? CONTINUE : OTHER;
588 }
589
590
591 /** Generate all headers appropriate for sending the http request in req (or
592  * the response, if we're sending a response), and write them to evcon's
593  * bufferevent. Also writes all data from req->output_buffer */
594 static void
595 evhttp_make_header(struct evhttp_connection *evcon, struct evhttp_request *req)
596 {
597         struct evkeyval *header;
598         struct evbuffer *output = bufferevent_get_output(evcon->bufev);
599
600         /*
601          * Depending if this is a HTTP request or response, we might need to
602          * add some new headers or remove existing headers.
603          */
604         if (req->kind == EVHTTP_REQUEST) {
605                 evhttp_make_header_request(evcon, req);
606         } else {
607                 evhttp_make_header_response(evcon, req);
608         }
609
610         TAILQ_FOREACH(header, req->output_headers, next) {
611                 evbuffer_add_printf(output, "%s: %s\r\n",
612                     header->key, header->value);
613         }
614         evbuffer_add(output, "\r\n", 2);
615
616         if (evhttp_have_expect(req, 0) != CONTINUE &&
617                 evbuffer_get_length(req->output_buffer)) {
618                 /*
619                  * For a request, we add the POST data, for a reply, this
620                  * is the regular data.
621                  */
622                 evbuffer_add_buffer(output, req->output_buffer);
623         }
624 }
625
626 void
627 evhttp_connection_set_max_headers_size(struct evhttp_connection *evcon,
628     ev_ssize_t new_max_headers_size)
629 {
630         if (new_max_headers_size<0)
631                 evcon->max_headers_size = EV_SIZE_MAX;
632         else
633                 evcon->max_headers_size = new_max_headers_size;
634 }
635 void
636 evhttp_connection_set_max_body_size(struct evhttp_connection* evcon,
637     ev_ssize_t new_max_body_size)
638 {
639         if (new_max_body_size<0)
640                 evcon->max_body_size = EV_UINT64_MAX;
641         else
642                 evcon->max_body_size = new_max_body_size;
643 }
644
645 static int
646 evhttp_connection_incoming_fail(struct evhttp_request *req,
647     enum evhttp_request_error error)
648 {
649         switch (error) {
650                 case EVREQ_HTTP_DATA_TOO_LONG:
651                         req->response_code = HTTP_ENTITYTOOLARGE;
652                         break;
653                 default:
654                         req->response_code = HTTP_BADREQUEST;
655         }
656
657         switch (error) {
658         case EVREQ_HTTP_TIMEOUT:
659         case EVREQ_HTTP_EOF:
660                 /*
661                  * these are cases in which we probably should just
662                  * close the connection and not send a reply.  this
663                  * case may happen when a browser keeps a persistent
664                  * connection open and we timeout on the read.  when
665                  * the request is still being used for sending, we
666                  * need to disassociated it from the connection here.
667                  */
668                 if (!req->userdone) {
669                         /* remove it so that it will not be freed */
670                         TAILQ_REMOVE(&req->evcon->requests, req, next);
671                         /* indicate that this request no longer has a
672                          * connection object
673                          */
674                         req->evcon = NULL;
675                 }
676                 return (-1);
677         case EVREQ_HTTP_INVALID_HEADER:
678         case EVREQ_HTTP_BUFFER_ERROR:
679         case EVREQ_HTTP_REQUEST_CANCEL:
680         case EVREQ_HTTP_DATA_TOO_LONG:
681         default:        /* xxx: probably should just error on default */
682                 /* the callback looks at the uri to determine errors */
683                 if (req->uri) {
684                         mm_free(req->uri);
685                         req->uri = NULL;
686                 }
687                 if (req->uri_elems) {
688                         evhttp_uri_free(req->uri_elems);
689                         req->uri_elems = NULL;
690                 }
691
692                 /*
693                  * the callback needs to send a reply, once the reply has
694                  * been send, the connection should get freed.
695                  */
696                 (*req->cb)(req, req->cb_arg);
697         }
698
699         return (0);
700 }
701
702 /* Free connection ownership of which can be acquired by user using
703  * evhttp_request_own(). */
704 static inline void
705 evhttp_request_free_auto(struct evhttp_request *req)
706 {
707         if (!(req->flags & EVHTTP_USER_OWNED))
708                 evhttp_request_free(req);
709 }
710
711 static void
712 evhttp_request_free_(struct evhttp_connection *evcon, struct evhttp_request *req)
713 {
714         TAILQ_REMOVE(&evcon->requests, req, next);
715         evhttp_request_free_auto(req);
716 }
717
718 /* Called when evcon has experienced a (non-recoverable? -NM) error, as
719  * given in error. If it's an outgoing connection, reset the connection,
720  * retry any pending requests, and inform the user.  If it's incoming,
721  * delegates to evhttp_connection_incoming_fail(). */
722 void
723 evhttp_connection_fail_(struct evhttp_connection *evcon,
724     enum evhttp_request_error error)
725 {
726         const int errsave = EVUTIL_SOCKET_ERROR();
727         struct evhttp_request* req = TAILQ_FIRST(&evcon->requests);
728         void (*cb)(struct evhttp_request *, void *);
729         void *cb_arg;
730         void (*error_cb)(enum evhttp_request_error, void *);
731         void *error_cb_arg;
732         EVUTIL_ASSERT(req != NULL);
733
734         bufferevent_disable(evcon->bufev, EV_READ|EV_WRITE);
735
736         if (evcon->flags & EVHTTP_CON_INCOMING) {
737                 /*
738                  * for incoming requests, there are two different
739                  * failure cases.  it's either a network level error
740                  * or an http layer error. for problems on the network
741                  * layer like timeouts we just drop the connections.
742                  * For HTTP problems, we might have to send back a
743                  * reply before the connection can be freed.
744                  */
745                 if (evhttp_connection_incoming_fail(req, error) == -1)
746                         evhttp_connection_free(evcon);
747                 return;
748         }
749
750         error_cb = req->error_cb;
751         error_cb_arg = req->cb_arg;
752         /* when the request was canceled, the callback is not executed */
753         if (error != EVREQ_HTTP_REQUEST_CANCEL) {
754                 /* save the callback for later; the cb might free our object */
755                 cb = req->cb;
756                 cb_arg = req->cb_arg;
757         } else {
758                 cb = NULL;
759                 cb_arg = NULL;
760         }
761
762         /* do not fail all requests; the next request is going to get
763          * send over a new connection.   when a user cancels a request,
764          * all other pending requests should be processed as normal
765          */
766         evhttp_request_free_(evcon, req);
767
768         /* reset the connection */
769         evhttp_connection_reset_(evcon);
770
771         /* We are trying the next request that was queued on us */
772         if (TAILQ_FIRST(&evcon->requests) != NULL)
773                 evhttp_connection_connect_(evcon);
774
775         /* The call to evhttp_connection_reset_ overwrote errno.
776          * Let's restore the original errno, so that the user's
777          * callback can have a better idea of what the error was.
778          */
779         EVUTIL_SET_SOCKET_ERROR(errsave);
780
781         /* inform the user */
782         if (error_cb != NULL)
783                 error_cb(error, error_cb_arg);
784         if (cb != NULL)
785                 (*cb)(NULL, cb_arg);
786 }
787
788 /* Bufferevent callback: invoked when any data has been written from an
789  * http connection's bufferevent */
790 static void
791 evhttp_write_cb(struct bufferevent *bufev, void *arg)
792 {
793         struct evhttp_connection *evcon = arg;
794
795         /* Activate our call back */
796         if (evcon->cb != NULL)
797                 (*evcon->cb)(evcon, evcon->cb_arg);
798 }
799
800 /**
801  * Advance the connection state.
802  * - If this is an outgoing connection, we've just processed the response;
803  *   idle or close the connection.
804  * - If this is an incoming connection, we've just processed the request;
805  *   respond.
806  */
807 static void
808 evhttp_connection_done(struct evhttp_connection *evcon)
809 {
810         struct evhttp_request *req = TAILQ_FIRST(&evcon->requests);
811         int con_outgoing = evcon->flags & EVHTTP_CON_OUTGOING;
812         int free_evcon = 0;
813
814         if (con_outgoing) {
815                 /* idle or close the connection */
816                 int need_close = evhttp_is_request_connection_close(req);
817                 TAILQ_REMOVE(&evcon->requests, req, next);
818                 req->evcon = NULL;
819
820                 evcon->state = EVCON_IDLE;
821
822                 /* check if we got asked to close the connection */
823                 if (need_close)
824                         evhttp_connection_reset_(evcon);
825
826                 if (TAILQ_FIRST(&evcon->requests) != NULL) {
827                         /*
828                          * We have more requests; reset the connection
829                          * and deal with the next request.
830                          */
831                         if (!evhttp_connected(evcon))
832                                 evhttp_connection_connect_(evcon);
833                         else
834                                 evhttp_request_dispatch(evcon);
835                 } else if (!need_close) {
836                         /*
837                          * The connection is going to be persistent, but we
838                          * need to detect if the other side closes it.
839                          */
840                         evhttp_connection_start_detectclose(evcon);
841                 } else if ((evcon->flags & EVHTTP_CON_AUTOFREE)) {
842                         /*
843                          * If we have no more requests that need completion
844                          * and we're not waiting for the connection to close
845                          */
846                          free_evcon = 1;
847                 }
848         } else {
849                 /*
850                  * incoming connection - we need to leave the request on the
851                  * connection so that we can reply to it.
852                  */
853                 evcon->state = EVCON_WRITING;
854         }
855
856         /* notify the user of the request */
857         (*req->cb)(req, req->cb_arg);
858
859         /* if this was an outgoing request, we own and it's done. so free it. */
860         if (con_outgoing) {
861                 evhttp_request_free_auto(req);
862         }
863
864         /* If this was the last request of an outgoing connection and we're
865          * not waiting to receive a connection close event and we want to
866          * automatically free the connection. We check to ensure our request
867          * list is empty one last time just in case our callback added a
868          * new request.
869          */
870         if (free_evcon && TAILQ_FIRST(&evcon->requests) == NULL) {
871                 evhttp_connection_free(evcon);
872         }
873 }
874
875 /*
876  * Handles reading from a chunked request.
877  *   return ALL_DATA_READ:
878  *     all data has been read
879  *   return MORE_DATA_EXPECTED:
880  *     more data is expected
881  *   return DATA_CORRUPTED:
882  *     data is corrupted
883  *   return REQUEST_CANCELED:
884  *     request was canceled by the user calling evhttp_cancel_request
885  *   return DATA_TOO_LONG:
886  *     ran over the maximum limit
887  */
888
889 static enum message_read_status
890 evhttp_handle_chunked_read(struct evhttp_request *req, struct evbuffer *buf)
891 {
892         if (req == NULL || buf == NULL) {
893             return DATA_CORRUPTED;
894         }
895
896         while (1) {
897                 size_t buflen;
898
899                 if ((buflen = evbuffer_get_length(buf)) == 0) {
900                         break;
901                 }
902
903                 /* evbuffer_get_length returns size_t, but len variable is ssize_t,
904                  * check for overflow conditions */
905                 if (buflen > EV_SSIZE_MAX) {
906                         return DATA_CORRUPTED;
907                 }
908
909                 if (req->ntoread < 0) {
910                         /* Read chunk size */
911                         ev_int64_t ntoread;
912                         char *p = evbuffer_readln(buf, NULL, EVBUFFER_EOL_CRLF);
913                         char *endp;
914                         int error;
915                         if (p == NULL)
916                                 break;
917                         /* the last chunk is on a new line? */
918                         if (strlen(p) == 0) {
919                                 mm_free(p);
920                                 continue;
921                         }
922                         ntoread = evutil_strtoll(p, &endp, 16);
923                         error = (*p == '\0' ||
924                             (*endp != '\0' && *endp != ' ') ||
925                             ntoread < 0);
926                         mm_free(p);
927                         if (error) {
928                                 /* could not get chunk size */
929                                 return (DATA_CORRUPTED);
930                         }
931
932                         /* ntoread is signed int64, body_size is unsigned size_t, check for under/overflow conditions */
933                         if ((ev_uint64_t)ntoread > EV_SIZE_MAX - req->body_size) {
934                             return DATA_CORRUPTED;
935                         }
936
937                         if (req->body_size + (size_t)ntoread > req->evcon->max_body_size) {
938                                 /* failed body length test */
939                                 event_debug(("Request body is too long"));
940                                 return (DATA_TOO_LONG);
941                         }
942
943                         req->body_size += (size_t)ntoread;
944                         req->ntoread = ntoread;
945                         if (req->ntoread == 0) {
946                                 /* Last chunk */
947                                 return (ALL_DATA_READ);
948                         }
949                         continue;
950                 }
951
952                 /* req->ntoread is signed int64, len is ssize_t, based on arch,
953                  * ssize_t could only be 32b, check for these conditions */
954                 if (req->ntoread > EV_SSIZE_MAX) {
955                         return DATA_CORRUPTED;
956                 }
957
958                 /* don't have enough to complete a chunk; wait for more */
959                 if (req->ntoread > 0 && buflen < (ev_uint64_t)req->ntoread)
960                         return (MORE_DATA_EXPECTED);
961
962                 /* Completed chunk */
963                 evbuffer_remove_buffer(buf, req->input_buffer, (size_t)req->ntoread);
964                 req->ntoread = -1;
965                 if (req->chunk_cb != NULL) {
966                         req->flags |= EVHTTP_REQ_DEFER_FREE;
967                         (*req->chunk_cb)(req, req->cb_arg);
968                         evbuffer_drain(req->input_buffer,
969                             evbuffer_get_length(req->input_buffer));
970                         req->flags &= ~EVHTTP_REQ_DEFER_FREE;
971                         if ((req->flags & EVHTTP_REQ_NEEDS_FREE) != 0) {
972                                 return (REQUEST_CANCELED);
973                         }
974                 }
975         }
976
977         return (MORE_DATA_EXPECTED);
978 }
979
980 static void
981 evhttp_read_trailer(struct evhttp_connection *evcon, struct evhttp_request *req)
982 {
983         struct evbuffer *buf = bufferevent_get_input(evcon->bufev);
984
985         switch (evhttp_parse_headers_(req, buf)) {
986         case DATA_CORRUPTED:
987         case DATA_TOO_LONG:
988                 evhttp_connection_fail_(evcon, EVREQ_HTTP_DATA_TOO_LONG);
989                 break;
990         case ALL_DATA_READ:
991                 bufferevent_disable(evcon->bufev, EV_READ);
992                 evhttp_connection_done(evcon);
993                 break;
994         case MORE_DATA_EXPECTED:
995         case REQUEST_CANCELED: /* ??? */
996         default:
997                 break;
998         }
999 }
1000
1001 static void
1002 evhttp_lingering_close(struct evhttp_connection *evcon,
1003         struct evhttp_request *req)
1004 {
1005         struct evbuffer *buf = bufferevent_get_input(evcon->bufev);
1006
1007         size_t n = evbuffer_get_length(buf);
1008         if (n > (size_t) req->ntoread)
1009                 n = (size_t) req->ntoread;
1010         req->ntoread -= n;
1011         req->body_size += n;
1012
1013         event_debug(("Request body is too long, left " EV_I64_FMT,
1014                 EV_I64_ARG(req->ntoread)));
1015
1016         evbuffer_drain(buf, n);
1017         if (!req->ntoread)
1018                 evhttp_connection_fail_(evcon, EVREQ_HTTP_DATA_TOO_LONG);
1019 }
1020 static void
1021 evhttp_lingering_fail(struct evhttp_connection *evcon,
1022         struct evhttp_request *req)
1023 {
1024         if (evcon->flags & EVHTTP_CON_LINGERING_CLOSE)
1025                 evhttp_lingering_close(evcon, req);
1026         else
1027                 evhttp_connection_fail_(evcon, EVREQ_HTTP_DATA_TOO_LONG);
1028 }
1029
1030 static void
1031 evhttp_read_body(struct evhttp_connection *evcon, struct evhttp_request *req)
1032 {
1033         struct evbuffer *buf = bufferevent_get_input(evcon->bufev);
1034
1035         if (req->chunked) {
1036                 switch (evhttp_handle_chunked_read(req, buf)) {
1037                 case ALL_DATA_READ:
1038                         /* finished last chunk */
1039                         evcon->state = EVCON_READING_TRAILER;
1040                         evhttp_read_trailer(evcon, req);
1041                         return;
1042                 case DATA_CORRUPTED:
1043                 case DATA_TOO_LONG:
1044                         /* corrupted data */
1045                         evhttp_connection_fail_(evcon,
1046                             EVREQ_HTTP_DATA_TOO_LONG);
1047                         return;
1048                 case REQUEST_CANCELED:
1049                         /* request canceled */
1050                         evhttp_request_free_auto(req);
1051                         return;
1052                 case MORE_DATA_EXPECTED:
1053                 default:
1054                         break;
1055                 }
1056         } else if (req->ntoread < 0) {
1057                 /* Read until connection close. */
1058                 if ((size_t)(req->body_size + evbuffer_get_length(buf)) < req->body_size) {
1059                         evhttp_connection_fail_(evcon, EVREQ_HTTP_INVALID_HEADER);
1060                         return;
1061                 }
1062
1063                 req->body_size += evbuffer_get_length(buf);
1064                 evbuffer_add_buffer(req->input_buffer, buf);
1065         } else if (req->chunk_cb != NULL || evbuffer_get_length(buf) >= (size_t)req->ntoread) {
1066                 /* XXX: the above get_length comparison has to be fixed for overflow conditions! */
1067                 /* We've postponed moving the data until now, but we're
1068                  * about to use it. */
1069                 size_t n = evbuffer_get_length(buf);
1070
1071                 if (n > (size_t) req->ntoread)
1072                         n = (size_t) req->ntoread;
1073                 req->ntoread -= n;
1074                 req->body_size += n;
1075                 evbuffer_remove_buffer(buf, req->input_buffer, n);
1076         }
1077
1078         if (req->body_size > req->evcon->max_body_size ||
1079             (!req->chunked && req->ntoread >= 0 &&
1080                 (size_t)req->ntoread > req->evcon->max_body_size)) {
1081                 /* XXX: The above casted comparison must checked for overflow */
1082                 /* failed body length test */
1083
1084                 evhttp_lingering_fail(evcon, req);
1085                 return;
1086         }
1087
1088         if (evbuffer_get_length(req->input_buffer) > 0 && req->chunk_cb != NULL) {
1089                 req->flags |= EVHTTP_REQ_DEFER_FREE;
1090                 (*req->chunk_cb)(req, req->cb_arg);
1091                 req->flags &= ~EVHTTP_REQ_DEFER_FREE;
1092                 evbuffer_drain(req->input_buffer,
1093                     evbuffer_get_length(req->input_buffer));
1094                 if ((req->flags & EVHTTP_REQ_NEEDS_FREE) != 0) {
1095                         evhttp_request_free_auto(req);
1096                         return;
1097                 }
1098         }
1099
1100         if (!req->ntoread) {
1101                 bufferevent_disable(evcon->bufev, EV_READ);
1102                 /* Completed content length */
1103                 evhttp_connection_done(evcon);
1104                 return;
1105         }
1106 }
1107
1108 #define get_deferred_queue(evcon)               \
1109         ((evcon)->base)
1110
1111 /*
1112  * Gets called when more data becomes available
1113  */
1114
1115 static void
1116 evhttp_read_cb(struct bufferevent *bufev, void *arg)
1117 {
1118         struct evhttp_connection *evcon = arg;
1119         struct evhttp_request *req = TAILQ_FIRST(&evcon->requests);
1120
1121         /* Cancel if it's pending. */
1122         event_deferred_cb_cancel_(get_deferred_queue(evcon),
1123             &evcon->read_more_deferred_cb);
1124
1125         switch (evcon->state) {
1126         case EVCON_READING_FIRSTLINE:
1127                 evhttp_read_firstline(evcon, req);
1128                 /* note the request may have been freed in
1129                  * evhttp_read_body */
1130                 break;
1131         case EVCON_READING_HEADERS:
1132                 evhttp_read_header(evcon, req);
1133                 /* note the request may have been freed in
1134                  * evhttp_read_body */
1135                 break;
1136         case EVCON_READING_BODY:
1137                 evhttp_read_body(evcon, req);
1138                 /* note the request may have been freed in
1139                  * evhttp_read_body */
1140                 break;
1141         case EVCON_READING_TRAILER:
1142                 evhttp_read_trailer(evcon, req);
1143                 break;
1144         case EVCON_IDLE:
1145                 {
1146 #ifdef USE_DEBUG
1147                         struct evbuffer *input;
1148                         size_t total_len;
1149
1150                         input = bufferevent_get_input(evcon->bufev);
1151                         total_len = evbuffer_get_length(input);
1152                         event_debug(("%s: read "EV_SIZE_FMT
1153                                 " bytes in EVCON_IDLE state,"
1154                                 " resetting connection",
1155                                 __func__, EV_SIZE_ARG(total_len)));
1156 #endif
1157
1158                         evhttp_connection_reset_(evcon);
1159                 }
1160                 break;
1161         case EVCON_DISCONNECTED:
1162         case EVCON_CONNECTING:
1163         case EVCON_WRITING:
1164         default:
1165                 event_errx(1, "%s: illegal connection state %d",
1166                            __func__, evcon->state);
1167         }
1168 }
1169
1170 static void
1171 evhttp_deferred_read_cb(struct event_callback *cb, void *data)
1172 {
1173         struct evhttp_connection *evcon = data;
1174         evhttp_read_cb(evcon->bufev, evcon);
1175 }
1176
1177 static void
1178 evhttp_write_connectioncb(struct evhttp_connection *evcon, void *arg)
1179 {
1180         /* This is after writing the request to the server */
1181         struct evhttp_request *req = TAILQ_FIRST(&evcon->requests);
1182         struct evbuffer *output = bufferevent_get_output(evcon->bufev);
1183         EVUTIL_ASSERT(req != NULL);
1184
1185         EVUTIL_ASSERT(evcon->state == EVCON_WRITING);
1186
1187         /* We need to wait until we've written all of our output data before we can
1188          * continue */
1189         if (evbuffer_get_length(output) > 0)
1190                 return;
1191
1192         /* We are done writing our header and are now expecting the response */
1193         req->kind = EVHTTP_RESPONSE;
1194
1195         evhttp_start_read_(evcon);
1196 }
1197
1198 /*
1199  * Clean up a connection object
1200  */
1201
1202 void
1203 evhttp_connection_free(struct evhttp_connection *evcon)
1204 {
1205         struct evhttp_request *req;
1206         int need_close = 0;
1207
1208         /* notify interested parties that this connection is going down */
1209         if (evcon->fd != -1) {
1210                 if (evhttp_connected(evcon) && evcon->closecb != NULL)
1211                         (*evcon->closecb)(evcon, evcon->closecb_arg);
1212         }
1213
1214         /* remove all requests that might be queued on this
1215          * connection.  for server connections, this should be empty.
1216          * because it gets dequeued either in evhttp_connection_done or
1217          * evhttp_connection_fail_.
1218          */
1219         while ((req = TAILQ_FIRST(&evcon->requests)) != NULL) {
1220                 evhttp_request_free_(evcon, req);
1221         }
1222
1223         if (evcon->http_server != NULL) {
1224                 struct evhttp *http = evcon->http_server;
1225                 TAILQ_REMOVE(&http->connections, evcon, next);
1226         }
1227
1228         if (event_initialized(&evcon->retry_ev)) {
1229                 event_del(&evcon->retry_ev);
1230                 event_debug_unassign(&evcon->retry_ev);
1231         }
1232
1233         event_deferred_cb_cancel_(get_deferred_queue(evcon),
1234             &evcon->read_more_deferred_cb);
1235
1236         if (evcon->bufev != NULL) {
1237                 need_close =
1238                         !(bufferevent_get_options_(evcon->bufev) & BEV_OPT_CLOSE_ON_FREE);
1239                 if (evcon->fd == -1)
1240                         evcon->fd = bufferevent_getfd(evcon->bufev);
1241
1242                 bufferevent_free(evcon->bufev);
1243         }
1244
1245         if (evcon->fd != -1) {
1246                 shutdown(evcon->fd, EVUTIL_SHUT_WR);
1247                 if (need_close)
1248                         evutil_closesocket(evcon->fd);
1249         }
1250
1251         if (evcon->bind_address != NULL)
1252                 mm_free(evcon->bind_address);
1253
1254         if (evcon->address != NULL)
1255                 mm_free(evcon->address);
1256
1257         mm_free(evcon);
1258 }
1259
1260 void
1261 evhttp_connection_free_on_completion(struct evhttp_connection *evcon) {
1262         evcon->flags |= EVHTTP_CON_AUTOFREE;
1263 }
1264
1265 void
1266 evhttp_connection_set_local_address(struct evhttp_connection *evcon,
1267     const char *address)
1268 {
1269         EVUTIL_ASSERT(evcon->state == EVCON_DISCONNECTED);
1270         if (evcon->bind_address)
1271                 mm_free(evcon->bind_address);
1272         if ((evcon->bind_address = mm_strdup(address)) == NULL)
1273                 event_warn("%s: strdup", __func__);
1274 }
1275
1276 void
1277 evhttp_connection_set_local_port(struct evhttp_connection *evcon,
1278     ev_uint16_t port)
1279 {
1280         EVUTIL_ASSERT(evcon->state == EVCON_DISCONNECTED);
1281         evcon->bind_port = port;
1282 }
1283
1284 static void
1285 evhttp_request_dispatch(struct evhttp_connection* evcon)
1286 {
1287         struct evhttp_request *req = TAILQ_FIRST(&evcon->requests);
1288
1289         /* this should not usually happy but it's possible */
1290         if (req == NULL)
1291                 return;
1292
1293         EVUTIL_ASSERT(req->kind == EVHTTP_REQUEST);
1294
1295         /* delete possible close detection events */
1296         evhttp_connection_stop_detectclose(evcon);
1297
1298         /* we assume that the connection is connected already */
1299         EVUTIL_ASSERT(evcon->state == EVCON_IDLE);
1300
1301         evcon->state = EVCON_WRITING;
1302
1303         /* Create the header from the store arguments */
1304         evhttp_make_header(evcon, req);
1305
1306         evhttp_write_buffer(evcon, evhttp_write_connectioncb, NULL);
1307 }
1308
1309 /* Reset our connection state: disables reading/writing, closes our fd (if
1310 * any), clears out buffers, and puts us in state DISCONNECTED. */
1311 void
1312 evhttp_connection_reset_(struct evhttp_connection *evcon)
1313 {
1314         struct evbuffer *tmp;
1315         int err;
1316
1317         bufferevent_setcb(evcon->bufev, NULL, NULL, NULL, NULL);
1318
1319         /* XXXX This is not actually an optimal fix.  Instead we ought to have
1320            an API for "stop connecting", or use bufferevent_setfd to turn off
1321            connecting.  But for Libevent 2.0, this seems like a minimal change
1322            least likely to disrupt the rest of the bufferevent and http code.
1323
1324            Why is this here?  If the fd is set in the bufferevent, and the
1325            bufferevent is connecting, then you can't actually stop the
1326            bufferevent from trying to connect with bufferevent_disable().  The
1327            connect will never trigger, since we close the fd, but the timeout
1328            might.  That caused an assertion failure in evhttp_connection_fail_.
1329         */
1330         bufferevent_disable_hard_(evcon->bufev, EV_READ|EV_WRITE);
1331
1332         if (evcon->fd == -1)
1333                 evcon->fd = bufferevent_getfd(evcon->bufev);
1334
1335         if (evcon->fd != -1) {
1336                 /* inform interested parties about connection close */
1337                 if (evhttp_connected(evcon) && evcon->closecb != NULL)
1338                         (*evcon->closecb)(evcon, evcon->closecb_arg);
1339
1340                 shutdown(evcon->fd, EVUTIL_SHUT_WR);
1341                 evutil_closesocket(evcon->fd);
1342                 evcon->fd = -1;
1343         }
1344         err = bufferevent_setfd(evcon->bufev, -1);
1345         EVUTIL_ASSERT(!err && "setfd");
1346
1347         /* we need to clean up any buffered data */
1348         tmp = bufferevent_get_output(evcon->bufev);
1349         err = evbuffer_drain(tmp, -1);
1350         EVUTIL_ASSERT(!err && "drain output");
1351         tmp = bufferevent_get_input(evcon->bufev);
1352         err = evbuffer_drain(tmp, -1);
1353         EVUTIL_ASSERT(!err && "drain input");
1354
1355         evcon->flags &= ~EVHTTP_CON_READING_ERROR;
1356
1357         evcon->state = EVCON_DISCONNECTED;
1358 }
1359
1360 static void
1361 evhttp_connection_start_detectclose(struct evhttp_connection *evcon)
1362 {
1363         evcon->flags |= EVHTTP_CON_CLOSEDETECT;
1364         bufferevent_enable(evcon->bufev, EV_READ);
1365 }
1366
1367 static void
1368 evhttp_connection_stop_detectclose(struct evhttp_connection *evcon)
1369 {
1370         evcon->flags &= ~EVHTTP_CON_CLOSEDETECT;
1371         bufferevent_disable(evcon->bufev, EV_READ);
1372 }
1373
1374 static void
1375 evhttp_connection_retry(evutil_socket_t fd, short what, void *arg)
1376 {
1377         struct evhttp_connection *evcon = arg;
1378
1379         evcon->state = EVCON_DISCONNECTED;
1380         evhttp_connection_connect_(evcon);
1381 }
1382
1383 static void
1384 evhttp_connection_cb_cleanup(struct evhttp_connection *evcon)
1385 {
1386         struct evcon_requestq requests;
1387
1388         evhttp_connection_reset_(evcon);
1389         if (evcon->retry_max < 0 || evcon->retry_cnt < evcon->retry_max) {
1390                 struct timeval tv_retry = evcon->initial_retry_timeout;
1391                 int i;
1392                 evtimer_assign(&evcon->retry_ev, evcon->base, evhttp_connection_retry, evcon);
1393                 /* XXXX handle failure from evhttp_add_event */
1394                 for (i=0; i < evcon->retry_cnt; ++i) {
1395                         tv_retry.tv_usec *= 2;
1396                         if (tv_retry.tv_usec > 1000000) {
1397                                 tv_retry.tv_usec -= 1000000;
1398                                 tv_retry.tv_sec += 1;
1399                         }
1400                         tv_retry.tv_sec *= 2;
1401                         if (tv_retry.tv_sec > 3600) {
1402                                 tv_retry.tv_sec = 3600;
1403                                 tv_retry.tv_usec = 0;
1404                         }
1405                 }
1406                 event_add(&evcon->retry_ev, &tv_retry);
1407                 evcon->retry_cnt++;
1408                 return;
1409         }
1410
1411         /*
1412          * User callback can do evhttp_make_request() on the same
1413          * evcon so new request will be added to evcon->requests.  To
1414          * avoid freeing it prematurely we iterate over the copy of
1415          * the queue.
1416          */
1417         TAILQ_INIT(&requests);
1418         while (TAILQ_FIRST(&evcon->requests) != NULL) {
1419                 struct evhttp_request *request = TAILQ_FIRST(&evcon->requests);
1420                 TAILQ_REMOVE(&evcon->requests, request, next);
1421                 TAILQ_INSERT_TAIL(&requests, request, next);
1422         }
1423
1424         /* for now, we just signal all requests by executing their callbacks */
1425         while (TAILQ_FIRST(&requests) != NULL) {
1426                 struct evhttp_request *request = TAILQ_FIRST(&requests);
1427                 TAILQ_REMOVE(&requests, request, next);
1428                 request->evcon = NULL;
1429
1430                 /* we might want to set an error here */
1431                 request->cb(request, request->cb_arg);
1432                 evhttp_request_free_auto(request);
1433         }
1434 }
1435
1436 static void
1437 evhttp_connection_read_on_write_error(struct evhttp_connection *evcon,
1438     struct evhttp_request *req)
1439 {
1440         struct evbuffer *buf;
1441
1442         /** Second time, we can't read anything */
1443         if (evcon->flags & EVHTTP_CON_READING_ERROR) {
1444                 evcon->flags &= ~EVHTTP_CON_READING_ERROR;
1445                 evhttp_connection_fail_(evcon, EVREQ_HTTP_EOF);
1446                 return;
1447         }
1448
1449         req->kind = EVHTTP_RESPONSE;
1450
1451         buf = bufferevent_get_output(evcon->bufev);
1452         evbuffer_unfreeze(buf, 1);
1453         evbuffer_drain(buf, evbuffer_get_length(buf));
1454         evbuffer_freeze(buf, 1);
1455
1456         evhttp_start_read_(evcon);
1457         evcon->flags |= EVHTTP_CON_READING_ERROR;
1458 }
1459
1460 static void
1461 evhttp_error_cb(struct bufferevent *bufev, short what, void *arg)
1462 {
1463         struct evhttp_connection *evcon = arg;
1464         struct evhttp_request *req = TAILQ_FIRST(&evcon->requests);
1465
1466         if (evcon->fd == -1)
1467                 evcon->fd = bufferevent_getfd(bufev);
1468
1469         switch (evcon->state) {
1470         case EVCON_CONNECTING:
1471                 if (what & BEV_EVENT_TIMEOUT) {
1472                         event_debug(("%s: connection timeout for \"%s:%d\" on "
1473                                 EV_SOCK_FMT,
1474                                 __func__, evcon->address, evcon->port,
1475                                 EV_SOCK_ARG(evcon->fd)));
1476                         evhttp_connection_cb_cleanup(evcon);
1477                         return;
1478                 }
1479                 break;
1480
1481         case EVCON_READING_BODY:
1482                 if (!req->chunked && req->ntoread < 0
1483                     && what == (BEV_EVENT_READING|BEV_EVENT_EOF)) {
1484                         /* EOF on read can be benign */
1485                         evhttp_connection_done(evcon);
1486                         return;
1487                 }
1488                 break;
1489
1490         case EVCON_DISCONNECTED:
1491         case EVCON_IDLE:
1492         case EVCON_READING_FIRSTLINE:
1493         case EVCON_READING_HEADERS:
1494         case EVCON_READING_TRAILER:
1495         case EVCON_WRITING:
1496         default:
1497                 break;
1498         }
1499
1500         /* when we are in close detect mode, a read error means that
1501          * the other side closed their connection.
1502          */
1503         if (evcon->flags & EVHTTP_CON_CLOSEDETECT) {
1504                 evcon->flags &= ~EVHTTP_CON_CLOSEDETECT;
1505                 EVUTIL_ASSERT(evcon->http_server == NULL);
1506                 /* For connections from the client, we just
1507                  * reset the connection so that it becomes
1508                  * disconnected.
1509                  */
1510                 EVUTIL_ASSERT(evcon->state == EVCON_IDLE);
1511                 evhttp_connection_reset_(evcon);
1512
1513                 /*
1514                  * If we have no more requests that need completion
1515                  * and we want to auto-free the connection when all
1516                  * requests have been completed.
1517                  */
1518                 if (TAILQ_FIRST(&evcon->requests) == NULL
1519                   && (evcon->flags & EVHTTP_CON_OUTGOING)
1520                   && (evcon->flags & EVHTTP_CON_AUTOFREE)) {
1521                         evhttp_connection_free(evcon);
1522                 }
1523                 return;
1524         }
1525
1526         if (what & BEV_EVENT_TIMEOUT) {
1527                 evhttp_connection_fail_(evcon, EVREQ_HTTP_TIMEOUT);
1528         } else if (what & (BEV_EVENT_EOF|BEV_EVENT_ERROR)) {
1529                 if (what & BEV_EVENT_WRITING &&
1530                         evcon->flags & EVHTTP_CON_READ_ON_WRITE_ERROR) {
1531                         evhttp_connection_read_on_write_error(evcon, req);
1532                         return;
1533                 }
1534
1535                 evhttp_connection_fail_(evcon, EVREQ_HTTP_EOF);
1536         } else if (what == BEV_EVENT_CONNECTED) {
1537         } else {
1538                 evhttp_connection_fail_(evcon, EVREQ_HTTP_BUFFER_ERROR);
1539         }
1540 }
1541
1542 /*
1543  * Event callback for asynchronous connection attempt.
1544  */
1545 static void
1546 evhttp_connection_cb(struct bufferevent *bufev, short what, void *arg)
1547 {
1548         struct evhttp_connection *evcon = arg;
1549         int error;
1550         ev_socklen_t errsz = sizeof(error);
1551
1552         if (evcon->fd == -1)
1553                 evcon->fd = bufferevent_getfd(bufev);
1554
1555         if (!(what & BEV_EVENT_CONNECTED)) {
1556                 /* some operating systems return ECONNREFUSED immediately
1557                  * when connecting to a local address.  the cleanup is going
1558                  * to reschedule this function call.
1559                  */
1560 #ifndef _WIN32
1561                 if (errno == ECONNREFUSED)
1562                         goto cleanup;
1563 #endif
1564                 evhttp_error_cb(bufev, what, arg);
1565                 return;
1566         }
1567
1568         if (evcon->fd == -1) {
1569                 event_debug(("%s: bufferevent_getfd returned -1",
1570                         __func__));
1571                 goto cleanup;
1572         }
1573
1574         /* Check if the connection completed */
1575         if (getsockopt(evcon->fd, SOL_SOCKET, SO_ERROR, (void*)&error,
1576                        &errsz) == -1) {
1577                 event_debug(("%s: getsockopt for \"%s:%d\" on "EV_SOCK_FMT,
1578                         __func__, evcon->address, evcon->port,
1579                         EV_SOCK_ARG(evcon->fd)));
1580                 goto cleanup;
1581         }
1582
1583         if (error) {
1584                 event_debug(("%s: connect failed for \"%s:%d\" on "
1585                         EV_SOCK_FMT": %s",
1586                         __func__, evcon->address, evcon->port,
1587                         EV_SOCK_ARG(evcon->fd),
1588                         evutil_socket_error_to_string(error)));
1589                 goto cleanup;
1590         }
1591
1592         /* We are connected to the server now */
1593         event_debug(("%s: connected to \"%s:%d\" on "EV_SOCK_FMT"\n",
1594                         __func__, evcon->address, evcon->port,
1595                         EV_SOCK_ARG(evcon->fd)));
1596
1597         /* Reset the retry count as we were successful in connecting */
1598         evcon->retry_cnt = 0;
1599         evcon->state = EVCON_IDLE;
1600
1601         /* reset the bufferevent cbs */
1602         bufferevent_setcb(evcon->bufev,
1603             evhttp_read_cb,
1604             evhttp_write_cb,
1605             evhttp_error_cb,
1606             evcon);
1607
1608         if (!evutil_timerisset(&evcon->timeout)) {
1609                 const struct timeval read_tv = { HTTP_READ_TIMEOUT, 0 };
1610                 const struct timeval write_tv = { HTTP_WRITE_TIMEOUT, 0 };
1611                 bufferevent_set_timeouts(evcon->bufev, &read_tv, &write_tv);
1612         } else {
1613                 bufferevent_set_timeouts(evcon->bufev, &evcon->timeout, &evcon->timeout);
1614         }
1615
1616         /* try to start requests that have queued up on this connection */
1617         evhttp_request_dispatch(evcon);
1618         return;
1619
1620  cleanup:
1621         evhttp_connection_cb_cleanup(evcon);
1622 }
1623
1624 /*
1625  * Check if we got a valid response code.
1626  */
1627
1628 static int
1629 evhttp_valid_response_code(int code)
1630 {
1631         if (code == 0)
1632                 return (0);
1633
1634         return (1);
1635 }
1636
1637 static int
1638 evhttp_parse_http_version(const char *version, struct evhttp_request *req)
1639 {
1640         int major, minor;
1641         char ch;
1642         int n = sscanf(version, "HTTP/%d.%d%c", &major, &minor, &ch);
1643         if (n != 2 || major > 1) {
1644                 event_debug(("%s: bad version %s on message %p from %s",
1645                         __func__, version, req, req->remote_host));
1646                 return (-1);
1647         }
1648         req->major = major;
1649         req->minor = minor;
1650         return (0);
1651 }
1652
1653 /* Parses the status line of a web server */
1654
1655 static int
1656 evhttp_parse_response_line(struct evhttp_request *req, char *line)
1657 {
1658         char *protocol;
1659         char *number;
1660         const char *readable = "";
1661
1662         protocol = strsep(&line, " ");
1663         if (line == NULL)
1664                 return (-1);
1665         number = strsep(&line, " ");
1666         if (line != NULL)
1667                 readable = line;
1668
1669         if (evhttp_parse_http_version(protocol, req) < 0)
1670                 return (-1);
1671
1672         req->response_code = atoi(number);
1673         if (!evhttp_valid_response_code(req->response_code)) {
1674                 event_debug(("%s: bad response code \"%s\"",
1675                         __func__, number));
1676                 return (-1);
1677         }
1678
1679         if (req->response_code_line != NULL)
1680                 mm_free(req->response_code_line);
1681         if ((req->response_code_line = mm_strdup(readable)) == NULL) {
1682                 event_warn("%s: strdup", __func__);
1683                 return (-1);
1684         }
1685
1686         return (0);
1687 }
1688
1689 /* Parse the first line of a HTTP request */
1690
1691 static int
1692 evhttp_parse_request_line(struct evhttp_request *req, char *line, size_t len)
1693 {
1694         char *eos = line + len;
1695         char *method;
1696         char *uri;
1697         char *version;
1698         const char *hostname;
1699         const char *scheme;
1700         size_t method_len;
1701         enum evhttp_cmd_type type;
1702
1703         while (eos > line && *(eos-1) == ' ') {
1704                 *(eos-1) = '\0';
1705                 --eos;
1706                 --len;
1707         }
1708         if (len < strlen("GET / HTTP/1.0"))
1709                 return -1;
1710
1711         /* Parse the request line */
1712         method = strsep(&line, " ");
1713         if (!line)
1714                 return -1;
1715         uri = line;
1716         version = strrchr(uri, ' ');
1717         if (!version || uri == version)
1718                 return -1;
1719         *version = '\0';
1720         version++;
1721
1722         method_len = (uri - method) - 1;
1723         type       = EVHTTP_REQ_UNKNOWN_;
1724
1725         /* First line */
1726         switch (method_len) {
1727             case 3:
1728                 /* The length of the method string is 3, meaning it can only be one of two methods: GET or PUT */
1729             
1730                 /* Since both GET and PUT share the same character 'T' at the end,
1731                  * if the string doesn't have 'T', we can immediately determine this
1732                  * is an invalid HTTP method */
1733             
1734                 if (method[2] != 'T') {
1735                     break;
1736                 }
1737             
1738                 switch (*method) {
1739                     case 'G':
1740                         /* This first byte is 'G', so make sure the next byte is
1741                          * 'E', if it isn't then this isn't a valid method */
1742                     
1743                         if (method[1] == 'E') {
1744                             type = EVHTTP_REQ_GET;
1745                         }
1746                     
1747                         break;
1748                     case 'P':
1749                         /* First byte is P, check second byte for 'U', if not,
1750                          * we know it's an invalid method */
1751                         if (method[1] == 'U') {
1752                             type = EVHTTP_REQ_PUT;
1753                         }
1754                         break;
1755                     default:
1756                         break;
1757                 }
1758                 break;
1759             case 4:
1760                 /* The method length is 4 bytes, leaving only the methods "POST" and "HEAD" */
1761                 switch (*method) {
1762                     case 'P':
1763                         if (method[3] == 'T' && method[2] == 'S' && method[1] == 'O') {
1764                             type = EVHTTP_REQ_POST;
1765                         }
1766                         break;
1767                     case 'H':
1768                         if (method[3] == 'D' && method[2] == 'A' && method[1] == 'E') {
1769                             type = EVHTTP_REQ_HEAD;
1770                         }
1771                         break;
1772                     default:
1773                         break;
1774                 }
1775                 break;
1776             case 5:
1777                 /* Method length is 5 bytes, which can only encompass PATCH and TRACE */
1778                 switch (*method) {
1779                     case 'P':
1780                         if (method[4] == 'H' && method[3] == 'C' && method[2] == 'T' && method[1] == 'A') {
1781                             type = EVHTTP_REQ_PATCH;
1782                         }
1783                         break;
1784                     case 'T':
1785                         if (method[4] == 'E' && method[3] == 'C' && method[2] == 'A' && method[1] == 'R') {
1786                             type = EVHTTP_REQ_TRACE;
1787                         }
1788                     
1789                         break;
1790                     default:
1791                         break;
1792                 }
1793                 break;
1794             case 6:
1795                 /* Method length is 6, only valid method 6 bytes in length is DELEte */
1796             
1797                 /* If the first byte isn't 'D' then it's invalid */
1798                 if (*method != 'D') {
1799                     break;
1800                 }
1801
1802                 if (method[5] == 'E' && method[4] == 'T' && method[3] == 'E' && method[2] == 'L' && method[1] == 'E') {
1803                     type = EVHTTP_REQ_DELETE;
1804                 }
1805
1806                 break;
1807             case 7:
1808                 /* Method length is 7, only valid methods are "OPTIONS" and "CONNECT" */
1809                 switch (*method) {
1810                     case 'O':
1811                         if (method[6] == 'S' && method[5] == 'N' && method[4] == 'O' &&
1812                                 method[3] == 'I' && method[2] == 'T' && method[1] == 'P') {
1813                             type = EVHTTP_REQ_OPTIONS;
1814                         }
1815                    
1816                         break;
1817                     case 'C':
1818                         if (method[6] == 'T' && method[5] == 'C' && method[4] == 'E' &&
1819                                 method[3] == 'N' && method[2] == 'N' && method[1] == 'O') {
1820                             type = EVHTTP_REQ_CONNECT;
1821                         }
1822                     
1823                         break;
1824                     default:
1825                         break;
1826                 }
1827                 break;
1828         } /* switch */
1829
1830         if ((int)type == EVHTTP_REQ_UNKNOWN_) {
1831                 event_debug(("%s: bad method %s on request %p from %s",
1832                         __func__, method, req, req->remote_host));
1833                 /* No error yet; we'll give a better error later when
1834                  * we see that req->type is unsupported. */
1835         }
1836             
1837         req->type = type;
1838
1839         if (evhttp_parse_http_version(version, req) < 0)
1840                 return -1;
1841
1842         if ((req->uri = mm_strdup(uri)) == NULL) {
1843                 event_debug(("%s: mm_strdup", __func__));
1844                 return -1;
1845         }
1846
1847         if (type == EVHTTP_REQ_CONNECT) {
1848                 if ((req->uri_elems = evhttp_uri_parse_authority(req->uri)) == NULL) {
1849                         return -1;
1850                 }
1851         } else {
1852                 if ((req->uri_elems = evhttp_uri_parse_with_flags(req->uri,
1853                             EVHTTP_URI_NONCONFORMANT)) == NULL) {
1854                         return -1;
1855                 }
1856         }
1857
1858         /* If we have an absolute-URI, check to see if it is an http request
1859            for a known vhost or server alias. If we don't know about this
1860            host, we consider it a proxy request. */
1861         scheme = evhttp_uri_get_scheme(req->uri_elems);
1862         hostname = evhttp_uri_get_host(req->uri_elems);
1863         if (scheme && (!evutil_ascii_strcasecmp(scheme, "http") ||
1864                        !evutil_ascii_strcasecmp(scheme, "https")) &&
1865             hostname &&
1866             !evhttp_find_vhost(req->evcon->http_server, NULL, hostname))
1867                 req->flags |= EVHTTP_PROXY_REQUEST;
1868
1869         return 0;
1870 }
1871
1872 const char *
1873 evhttp_find_header(const struct evkeyvalq *headers, const char *key)
1874 {
1875         struct evkeyval *header;
1876
1877         TAILQ_FOREACH(header, headers, next) {
1878                 if (evutil_ascii_strcasecmp(header->key, key) == 0)
1879                         return (header->value);
1880         }
1881
1882         return (NULL);
1883 }
1884
1885 void
1886 evhttp_clear_headers(struct evkeyvalq *headers)
1887 {
1888         struct evkeyval *header;
1889
1890         for (header = TAILQ_FIRST(headers);
1891             header != NULL;
1892             header = TAILQ_FIRST(headers)) {
1893                 TAILQ_REMOVE(headers, header, next);
1894                 mm_free(header->key);
1895                 mm_free(header->value);
1896                 mm_free(header);
1897         }
1898 }
1899
1900 /*
1901  * Returns 0,  if the header was successfully removed.
1902  * Returns -1, if the header could not be found.
1903  */
1904
1905 int
1906 evhttp_remove_header(struct evkeyvalq *headers, const char *key)
1907 {
1908         struct evkeyval *header;
1909
1910         TAILQ_FOREACH(header, headers, next) {
1911                 if (evutil_ascii_strcasecmp(header->key, key) == 0)
1912                         break;
1913         }
1914
1915         if (header == NULL)
1916                 return (-1);
1917
1918         /* Free and remove the header that we found */
1919         TAILQ_REMOVE(headers, header, next);
1920         mm_free(header->key);
1921         mm_free(header->value);
1922         mm_free(header);
1923
1924         return (0);
1925 }
1926
1927 static int
1928 evhttp_header_is_valid_value(const char *value)
1929 {
1930         const char *p = value;
1931
1932         while ((p = strpbrk(p, "\r\n")) != NULL) {
1933                 /* we really expect only one new line */
1934                 p += strspn(p, "\r\n");
1935                 /* we expect a space or tab for continuation */
1936                 if (*p != ' ' && *p != '\t')
1937                         return (0);
1938         }
1939         return (1);
1940 }
1941
1942 int
1943 evhttp_add_header(struct evkeyvalq *headers,
1944     const char *key, const char *value)
1945 {
1946         event_debug(("%s: key: %s val: %s\n", __func__, key, value));
1947
1948         if (strchr(key, '\r') != NULL || strchr(key, '\n') != NULL) {
1949                 /* drop illegal headers */
1950                 event_debug(("%s: dropping illegal header key\n", __func__));
1951                 return (-1);
1952         }
1953
1954         if (!evhttp_header_is_valid_value(value)) {
1955                 event_debug(("%s: dropping illegal header value\n", __func__));
1956                 return (-1);
1957         }
1958
1959         return (evhttp_add_header_internal(headers, key, value));
1960 }
1961
1962 static int
1963 evhttp_add_header_internal(struct evkeyvalq *headers,
1964     const char *key, const char *value)
1965 {
1966         struct evkeyval *header = mm_calloc(1, sizeof(struct evkeyval));
1967         if (header == NULL) {
1968                 event_warn("%s: calloc", __func__);
1969                 return (-1);
1970         }
1971         if ((header->key = mm_strdup(key)) == NULL) {
1972                 mm_free(header);
1973                 event_warn("%s: strdup", __func__);
1974                 return (-1);
1975         }
1976         if ((header->value = mm_strdup(value)) == NULL) {
1977                 mm_free(header->key);
1978                 mm_free(header);
1979                 event_warn("%s: strdup", __func__);
1980                 return (-1);
1981         }
1982
1983         TAILQ_INSERT_TAIL(headers, header, next);
1984
1985         return (0);
1986 }
1987
1988 /*
1989  * Parses header lines from a request or a response into the specified
1990  * request object given an event buffer.
1991  *
1992  * Returns
1993  *   DATA_CORRUPTED      on error
1994  *   MORE_DATA_EXPECTED  when we need to read more headers
1995  *   ALL_DATA_READ       when all headers have been read.
1996  */
1997
1998 enum message_read_status
1999 evhttp_parse_firstline_(struct evhttp_request *req, struct evbuffer *buffer)
2000 {
2001         char *line;
2002         enum message_read_status status = ALL_DATA_READ;
2003
2004         size_t len;
2005         /* XXX try */
2006         line = evbuffer_readln(buffer, &len, EVBUFFER_EOL_CRLF);
2007         if (line == NULL) {
2008                 if (req->evcon != NULL &&
2009                     evbuffer_get_length(buffer) > req->evcon->max_headers_size)
2010                         return (DATA_TOO_LONG);
2011                 else
2012                         return (MORE_DATA_EXPECTED);
2013         }
2014
2015         if (req->evcon != NULL && len > req->evcon->max_headers_size) {
2016                 mm_free(line);
2017                 return (DATA_TOO_LONG);
2018         }
2019
2020         req->headers_size = len;
2021
2022         switch (req->kind) {
2023         case EVHTTP_REQUEST:
2024                 if (evhttp_parse_request_line(req, line, len) == -1)
2025                         status = DATA_CORRUPTED;
2026                 break;
2027         case EVHTTP_RESPONSE:
2028                 if (evhttp_parse_response_line(req, line) == -1)
2029                         status = DATA_CORRUPTED;
2030                 break;
2031         default:
2032                 status = DATA_CORRUPTED;
2033         }
2034
2035         mm_free(line);
2036         return (status);
2037 }
2038
2039 static int
2040 evhttp_append_to_last_header(struct evkeyvalq *headers, char *line)
2041 {
2042         struct evkeyval *header = TAILQ_LAST(headers, evkeyvalq);
2043         char *newval;
2044         size_t old_len, line_len;
2045
2046         if (header == NULL)
2047                 return (-1);
2048
2049         old_len = strlen(header->value);
2050
2051         /* Strip space from start and end of line. */
2052         while (*line == ' ' || *line == '\t')
2053                 ++line;
2054         evutil_rtrim_lws_(line);
2055
2056         line_len = strlen(line);
2057
2058         newval = mm_realloc(header->value, old_len + line_len + 2);
2059         if (newval == NULL)
2060                 return (-1);
2061
2062         newval[old_len] = ' ';
2063         memcpy(newval + old_len + 1, line, line_len + 1);
2064         header->value = newval;
2065
2066         return (0);
2067 }
2068
2069 enum message_read_status
2070 evhttp_parse_headers_(struct evhttp_request *req, struct evbuffer* buffer)
2071 {
2072         enum message_read_status errcode = DATA_CORRUPTED;
2073         char *line;
2074         enum message_read_status status = MORE_DATA_EXPECTED;
2075
2076         struct evkeyvalq* headers = req->input_headers;
2077         size_t len;
2078         while ((line = evbuffer_readln(buffer, &len, EVBUFFER_EOL_CRLF))
2079                != NULL) {
2080                 char *skey, *svalue;
2081
2082                 req->headers_size += len;
2083
2084                 if (req->evcon != NULL &&
2085                     req->headers_size > req->evcon->max_headers_size) {
2086                         errcode = DATA_TOO_LONG;
2087                         goto error;
2088                 }
2089
2090                 if (*line == '\0') { /* Last header - Done */
2091                         status = ALL_DATA_READ;
2092                         mm_free(line);
2093                         break;
2094                 }
2095
2096                 /* Check if this is a continuation line */
2097                 if (*line == ' ' || *line == '\t') {
2098                         if (evhttp_append_to_last_header(headers, line) == -1)
2099                                 goto error;
2100                         mm_free(line);
2101                         continue;
2102                 }
2103
2104                 /* Processing of header lines */
2105                 svalue = line;
2106                 skey = strsep(&svalue, ":");
2107                 if (svalue == NULL)
2108                         goto error;
2109
2110                 svalue += strspn(svalue, " ");
2111                 evutil_rtrim_lws_(svalue);
2112
2113                 if (evhttp_add_header(headers, skey, svalue) == -1)
2114                         goto error;
2115
2116                 mm_free(line);
2117         }
2118
2119         if (status == MORE_DATA_EXPECTED) {
2120                 if (req->evcon != NULL &&
2121                 req->headers_size + evbuffer_get_length(buffer) > req->evcon->max_headers_size)
2122                         return (DATA_TOO_LONG);
2123         }
2124
2125         return (status);
2126
2127  error:
2128         mm_free(line);
2129         return (errcode);
2130 }
2131
2132 static int
2133 evhttp_get_body_length(struct evhttp_request *req)
2134 {
2135         struct evkeyvalq *headers = req->input_headers;
2136         const char *content_length;
2137         const char *connection;
2138
2139         content_length = evhttp_find_header(headers, "Content-Length");
2140         connection = evhttp_find_header(headers, "Connection");
2141
2142         if (content_length == NULL && connection == NULL)
2143                 req->ntoread = -1;
2144         else if (content_length == NULL &&
2145             evutil_ascii_strcasecmp(connection, "Close") != 0) {
2146                 req->ntoread = 0;
2147         } else if (content_length == NULL) {
2148                 req->ntoread = -1;
2149         } else {
2150                 char *endp;
2151                 ev_int64_t ntoread = evutil_strtoll(content_length, &endp, 10);
2152                 if (*content_length == '\0' || *endp != '\0' || ntoread < 0) {
2153                         event_debug(("%s: illegal content length: %s",
2154                                 __func__, content_length));
2155                         return (-1);
2156                 }
2157                 req->ntoread = ntoread;
2158         }
2159
2160         event_debug(("%s: bytes to read: "EV_I64_FMT" (in buffer "EV_SIZE_FMT")\n",
2161                 __func__, EV_I64_ARG(req->ntoread),
2162                 EV_SIZE_ARG(evbuffer_get_length(bufferevent_get_input(req->evcon->bufev)))));
2163
2164         return (0);
2165 }
2166
2167 static int
2168 evhttp_method_may_have_body(enum evhttp_cmd_type type)
2169 {
2170         switch (type) {
2171         case EVHTTP_REQ_POST:
2172         case EVHTTP_REQ_PUT:
2173         case EVHTTP_REQ_PATCH:
2174
2175         case EVHTTP_REQ_GET:
2176         case EVHTTP_REQ_DELETE:
2177         case EVHTTP_REQ_OPTIONS:
2178         case EVHTTP_REQ_CONNECT:
2179                 return 1;
2180
2181         case EVHTTP_REQ_TRACE:
2182         case EVHTTP_REQ_HEAD:
2183         default:
2184                 return 0;
2185         }
2186 }
2187
2188 static void
2189 evhttp_get_body(struct evhttp_connection *evcon, struct evhttp_request *req)
2190 {
2191         const char *xfer_enc;
2192
2193         /* If this is a request without a body, then we are done */
2194         if (req->kind == EVHTTP_REQUEST &&
2195             !evhttp_method_may_have_body(req->type)) {
2196                 evhttp_connection_done(evcon);
2197                 return;
2198         }
2199         evcon->state = EVCON_READING_BODY;
2200         xfer_enc = evhttp_find_header(req->input_headers, "Transfer-Encoding");
2201         if (xfer_enc != NULL && evutil_ascii_strcasecmp(xfer_enc, "chunked") == 0) {
2202                 req->chunked = 1;
2203                 req->ntoread = -1;
2204         } else {
2205                 if (evhttp_get_body_length(req) == -1) {
2206                         evhttp_connection_fail_(evcon, EVREQ_HTTP_INVALID_HEADER);
2207                         return;
2208                 }
2209                 if (req->kind == EVHTTP_REQUEST && req->ntoread < 1) {
2210                         /* An incoming request with no content-length and no
2211                          * transfer-encoding has no body. */
2212                         evhttp_connection_done(evcon);
2213                         return;
2214                 }
2215         }
2216
2217         /* Should we send a 100 Continue status line? */
2218         switch (evhttp_have_expect(req, 1)) {
2219                 case CONTINUE:
2220                                 /* XXX It would be nice to do some sanity
2221                                    checking here. Does the resource exist?
2222                                    Should the resource accept post requests? If
2223                                    no, we should respond with an error. For
2224                                    now, just optimistically tell the client to
2225                                    send their message body. */
2226                                 if (req->ntoread > 0) {
2227                                         /* ntoread is ev_int64_t, max_body_size is ev_uint64_t */ 
2228                                         if ((req->evcon->max_body_size <= EV_INT64_MAX) &&
2229                                                 (ev_uint64_t)req->ntoread > req->evcon->max_body_size) {
2230                                                 evhttp_lingering_fail(evcon, req);
2231                                                 return;
2232                                         }
2233                                 }
2234                                 if (!evbuffer_get_length(bufferevent_get_input(evcon->bufev)))
2235                                         evhttp_send_continue(evcon, req);
2236                         break;
2237                 case OTHER:
2238                         evhttp_send_error(req, HTTP_EXPECTATIONFAILED, NULL);
2239                         return;
2240                 case NO: break;
2241         }
2242
2243         evhttp_read_body(evcon, req);
2244         /* note the request may have been freed in evhttp_read_body */
2245 }
2246
2247 static void
2248 evhttp_read_firstline(struct evhttp_connection *evcon,
2249                       struct evhttp_request *req)
2250 {
2251         enum message_read_status res;
2252
2253         res = evhttp_parse_firstline_(req, bufferevent_get_input(evcon->bufev));
2254         if (res == DATA_CORRUPTED || res == DATA_TOO_LONG) {
2255                 /* Error while reading, terminate */
2256                 event_debug(("%s: bad header lines on "EV_SOCK_FMT"\n",
2257                         __func__, EV_SOCK_ARG(evcon->fd)));
2258                 evhttp_connection_fail_(evcon, EVREQ_HTTP_INVALID_HEADER);
2259                 return;
2260         } else if (res == MORE_DATA_EXPECTED) {
2261                 /* Need more header lines */
2262                 return;
2263         }
2264
2265         evcon->state = EVCON_READING_HEADERS;
2266         evhttp_read_header(evcon, req);
2267 }
2268
2269 static void
2270 evhttp_read_header(struct evhttp_connection *evcon,
2271                    struct evhttp_request *req)
2272 {
2273         enum message_read_status res;
2274         evutil_socket_t fd = evcon->fd;
2275
2276         res = evhttp_parse_headers_(req, bufferevent_get_input(evcon->bufev));
2277         if (res == DATA_CORRUPTED || res == DATA_TOO_LONG) {
2278                 /* Error while reading, terminate */
2279                 event_debug(("%s: bad header lines on "EV_SOCK_FMT"\n",
2280                         __func__, EV_SOCK_ARG(fd)));
2281                 evhttp_connection_fail_(evcon, EVREQ_HTTP_INVALID_HEADER);
2282                 return;
2283         } else if (res == MORE_DATA_EXPECTED) {
2284                 /* Need more header lines */
2285                 return;
2286         }
2287
2288         /* Callback can shut down connection with negative return value */
2289         if (req->header_cb != NULL) {
2290                 if ((*req->header_cb)(req, req->cb_arg) < 0) {
2291                         evhttp_connection_fail_(evcon, EVREQ_HTTP_EOF);
2292                         return;
2293                 }
2294         }
2295
2296         /* Done reading headers, do the real work */
2297         switch (req->kind) {
2298         case EVHTTP_REQUEST:
2299                 event_debug(("%s: checking for post data on "EV_SOCK_FMT"\n",
2300                         __func__, EV_SOCK_ARG(fd)));
2301                 evhttp_get_body(evcon, req);
2302                 /* note the request may have been freed in evhttp_get_body */
2303                 break;
2304
2305         case EVHTTP_RESPONSE:
2306                 /* Start over if we got a 100 Continue response. */
2307                 if (req->response_code == 100) {
2308                         struct evbuffer *output = bufferevent_get_output(evcon->bufev);
2309                         evbuffer_add_buffer(output, req->output_buffer);
2310                         evhttp_start_write_(evcon);
2311                         return;
2312                 }
2313                 if (!evhttp_response_needs_body(req)) {
2314                         event_debug(("%s: skipping body for code %d\n",
2315                                         __func__, req->response_code));
2316                         evhttp_connection_done(evcon);
2317                 } else {
2318                         event_debug(("%s: start of read body for %s on "
2319                                 EV_SOCK_FMT"\n",
2320                                 __func__, req->remote_host, EV_SOCK_ARG(fd)));
2321                         evhttp_get_body(evcon, req);
2322                         /* note the request may have been freed in
2323                          * evhttp_get_body */
2324                 }
2325                 break;
2326
2327         default:
2328                 event_warnx("%s: bad header on "EV_SOCK_FMT, __func__,
2329                     EV_SOCK_ARG(fd));
2330                 evhttp_connection_fail_(evcon, EVREQ_HTTP_INVALID_HEADER);
2331                 break;
2332         }
2333         /* request may have been freed above */
2334 }
2335
2336 /*
2337  * Creates a TCP connection to the specified port and executes a callback
2338  * when finished.  Failure or success is indicate by the passed connection
2339  * object.
2340  *
2341  * Although this interface accepts a hostname, it is intended to take
2342  * only numeric hostnames so that non-blocking DNS resolution can
2343  * happen elsewhere.
2344  */
2345
2346 struct evhttp_connection *
2347 evhttp_connection_new(const char *address, ev_uint16_t port)
2348 {
2349         return (evhttp_connection_base_new(NULL, NULL, address, port));
2350 }
2351
2352 struct evhttp_connection *
2353 evhttp_connection_base_bufferevent_new(struct event_base *base, struct evdns_base *dnsbase, struct bufferevent* bev,
2354     const char *address, ev_uint16_t port)
2355 {
2356         struct evhttp_connection *evcon = NULL;
2357
2358         event_debug(("Attempting connection to %s:%d\n", address, port));
2359
2360         if ((evcon = mm_calloc(1, sizeof(struct evhttp_connection))) == NULL) {
2361                 event_warn("%s: calloc failed", __func__);
2362                 goto error;
2363         }
2364
2365         evcon->fd = -1;
2366         evcon->port = port;
2367
2368         evcon->max_headers_size = EV_SIZE_MAX;
2369         evcon->max_body_size = EV_SIZE_MAX;
2370
2371         evutil_timerclear(&evcon->timeout);
2372         evcon->retry_cnt = evcon->retry_max = 0;
2373
2374         if ((evcon->address = mm_strdup(address)) == NULL) {
2375                 event_warn("%s: strdup failed", __func__);
2376                 goto error;
2377         }
2378
2379         if (bev == NULL) {
2380                 if (!(bev = bufferevent_socket_new(base, -1, 0))) {
2381                         event_warn("%s: bufferevent_socket_new failed", __func__);
2382                         goto error;
2383                 }
2384         }
2385
2386         bufferevent_setcb(bev, evhttp_read_cb, evhttp_write_cb, evhttp_error_cb, evcon);
2387         evcon->bufev = bev;
2388
2389         evcon->state = EVCON_DISCONNECTED;
2390         TAILQ_INIT(&evcon->requests);
2391
2392         evcon->initial_retry_timeout.tv_sec = 2;
2393         evcon->initial_retry_timeout.tv_usec = 0;
2394
2395         if (base != NULL) {
2396                 evcon->base = base;
2397                 if (bufferevent_get_base(bev) != base)
2398                         bufferevent_base_set(base, evcon->bufev);
2399         }
2400
2401         event_deferred_cb_init_(
2402             &evcon->read_more_deferred_cb,
2403             bufferevent_get_priority(bev),
2404             evhttp_deferred_read_cb, evcon);
2405
2406         evcon->dns_base = dnsbase;
2407         evcon->ai_family = AF_UNSPEC;
2408
2409         return (evcon);
2410
2411  error:
2412         if (evcon != NULL)
2413                 evhttp_connection_free(evcon);
2414         return (NULL);
2415 }
2416
2417 struct bufferevent* evhttp_connection_get_bufferevent(struct evhttp_connection *evcon)
2418 {
2419         return evcon->bufev;
2420 }
2421
2422 struct evhttp *
2423 evhttp_connection_get_server(struct evhttp_connection *evcon)
2424 {
2425         return evcon->http_server;
2426 }
2427
2428 struct evhttp_connection *
2429 evhttp_connection_base_new(struct event_base *base, struct evdns_base *dnsbase,
2430     const char *address, ev_uint16_t port)
2431 {
2432         return evhttp_connection_base_bufferevent_new(base, dnsbase, NULL, address, port);
2433 }
2434
2435 void evhttp_connection_set_family(struct evhttp_connection *evcon,
2436         int family)
2437 {
2438         evcon->ai_family = family;
2439 }
2440
2441 int evhttp_connection_set_flags(struct evhttp_connection *evcon,
2442         int flags)
2443 {
2444         int avail_flags = 0;
2445         avail_flags |= EVHTTP_CON_REUSE_CONNECTED_ADDR;
2446         avail_flags |= EVHTTP_CON_READ_ON_WRITE_ERROR;
2447
2448         if (flags & ~avail_flags || flags > EVHTTP_CON_PUBLIC_FLAGS_END)
2449                 return 1;
2450         evcon->flags &= ~avail_flags;
2451
2452         evcon->flags |= flags;
2453
2454         return 0;
2455 }
2456
2457 void
2458 evhttp_connection_set_base(struct evhttp_connection *evcon,
2459     struct event_base *base)
2460 {
2461         EVUTIL_ASSERT(evcon->base == NULL);
2462         EVUTIL_ASSERT(evcon->state == EVCON_DISCONNECTED);
2463         evcon->base = base;
2464         bufferevent_base_set(base, evcon->bufev);
2465 }
2466
2467 void
2468 evhttp_connection_set_timeout(struct evhttp_connection *evcon,
2469     int timeout_in_secs)
2470 {
2471         if (timeout_in_secs == -1)
2472                 evhttp_connection_set_timeout_tv(evcon, NULL);
2473         else {
2474                 struct timeval tv;
2475                 tv.tv_sec = timeout_in_secs;
2476                 tv.tv_usec = 0;
2477                 evhttp_connection_set_timeout_tv(evcon, &tv);
2478         }
2479 }
2480
2481 void
2482 evhttp_connection_set_timeout_tv(struct evhttp_connection *evcon,
2483     const struct timeval* tv)
2484 {
2485         if (tv) {
2486                 evcon->timeout = *tv;
2487                 bufferevent_set_timeouts(evcon->bufev, &evcon->timeout, &evcon->timeout);
2488         } else {
2489                 const struct timeval read_tv = { HTTP_READ_TIMEOUT, 0 };
2490                 const struct timeval write_tv = { HTTP_WRITE_TIMEOUT, 0 };
2491                 evutil_timerclear(&evcon->timeout);
2492                 bufferevent_set_timeouts(evcon->bufev, &read_tv, &write_tv);
2493         }
2494 }
2495
2496 void
2497 evhttp_connection_set_initial_retry_tv(struct evhttp_connection *evcon,
2498     const struct timeval *tv)
2499 {
2500         if (tv) {
2501                 evcon->initial_retry_timeout = *tv;
2502         } else {
2503                 evutil_timerclear(&evcon->initial_retry_timeout);
2504                 evcon->initial_retry_timeout.tv_sec = 2;
2505         }
2506 }
2507
2508 void
2509 evhttp_connection_set_retries(struct evhttp_connection *evcon,
2510     int retry_max)
2511 {
2512         evcon->retry_max = retry_max;
2513 }
2514
2515 void
2516 evhttp_connection_set_closecb(struct evhttp_connection *evcon,
2517     void (*cb)(struct evhttp_connection *, void *), void *cbarg)
2518 {
2519         evcon->closecb = cb;
2520         evcon->closecb_arg = cbarg;
2521 }
2522
2523 void
2524 evhttp_connection_get_peer(struct evhttp_connection *evcon,
2525     char **address, ev_uint16_t *port)
2526 {
2527         *address = evcon->address;
2528         *port = evcon->port;
2529 }
2530
2531 const struct sockaddr*
2532 evhttp_connection_get_addr(struct evhttp_connection *evcon)
2533 {
2534         return bufferevent_socket_get_conn_address_(evcon->bufev);
2535 }
2536
2537 int
2538 evhttp_connection_connect_(struct evhttp_connection *evcon)
2539 {
2540         int old_state = evcon->state;
2541         const char *address = evcon->address;
2542         const struct sockaddr *sa = evhttp_connection_get_addr(evcon);
2543         int ret;
2544
2545         if (evcon->state == EVCON_CONNECTING)
2546                 return (0);
2547
2548         evhttp_connection_reset_(evcon);
2549
2550         EVUTIL_ASSERT(!(evcon->flags & EVHTTP_CON_INCOMING));
2551         evcon->flags |= EVHTTP_CON_OUTGOING;
2552
2553         if (evcon->bind_address || evcon->bind_port) {
2554                 evcon->fd = bind_socket(
2555                         evcon->bind_address, evcon->bind_port, 0 /*reuse*/);
2556                 if (evcon->fd == -1) {
2557                         event_debug(("%s: failed to bind to \"%s\"",
2558                                 __func__, evcon->bind_address));
2559                         return (-1);
2560                 }
2561
2562                 if (bufferevent_setfd(evcon->bufev, evcon->fd))
2563                         return (-1);
2564         } else {
2565                 if (bufferevent_setfd(evcon->bufev, -1))
2566                         return (-1);
2567         }
2568
2569         /* Set up a callback for successful connection setup */
2570         bufferevent_setcb(evcon->bufev,
2571             NULL /* evhttp_read_cb */,
2572             NULL /* evhttp_write_cb */,
2573             evhttp_connection_cb,
2574             evcon);
2575         if (!evutil_timerisset(&evcon->timeout)) {
2576                 const struct timeval conn_tv = { HTTP_CONNECT_TIMEOUT, 0 };
2577                 bufferevent_set_timeouts(evcon->bufev, &conn_tv, &conn_tv);
2578         } else {
2579                 bufferevent_set_timeouts(evcon->bufev, &evcon->timeout, &evcon->timeout);
2580         }
2581         /* make sure that we get a write callback */
2582         if (bufferevent_enable(evcon->bufev, EV_WRITE))
2583                 return (-1);
2584
2585         evcon->state = EVCON_CONNECTING;
2586
2587         if (evcon->flags & EVHTTP_CON_REUSE_CONNECTED_ADDR &&
2588                 sa &&
2589                 (sa->sa_family == AF_INET || sa->sa_family == AF_INET6)) {
2590                 int socklen = sizeof(struct sockaddr_in);
2591                 if (sa->sa_family == AF_INET6) {
2592                         socklen = sizeof(struct sockaddr_in6);
2593                 }
2594                 ret = bufferevent_socket_connect(evcon->bufev, sa, socklen);
2595         } else {
2596                 ret = bufferevent_socket_connect_hostname(evcon->bufev,
2597                                 evcon->dns_base, evcon->ai_family, address, evcon->port);
2598         }
2599
2600         if (ret < 0) {
2601                 evcon->state = old_state;
2602                 event_sock_warn(evcon->fd, "%s: connection to \"%s\" failed",
2603                     __func__, evcon->address);
2604                 /* some operating systems return ECONNREFUSED immediately
2605                  * when connecting to a local address.  the cleanup is going
2606                  * to reschedule this function call.
2607                  */
2608                 evhttp_connection_cb_cleanup(evcon);
2609                 return (0);
2610         }
2611
2612         return (0);
2613 }
2614
2615 /*
2616  * Starts an HTTP request on the provided evhttp_connection object.
2617  * If the connection object is not connected to the web server already,
2618  * this will start the connection.
2619  */
2620
2621 int
2622 evhttp_make_request(struct evhttp_connection *evcon,
2623     struct evhttp_request *req,
2624     enum evhttp_cmd_type type, const char *uri)
2625 {
2626         /* We are making a request */
2627         req->kind = EVHTTP_REQUEST;
2628         req->type = type;
2629         if (req->uri != NULL)
2630                 mm_free(req->uri);
2631         if ((req->uri = mm_strdup(uri)) == NULL) {
2632                 event_warn("%s: strdup", __func__);
2633                 evhttp_request_free_auto(req);
2634                 return (-1);
2635         }
2636
2637         /* Set the protocol version if it is not supplied */
2638         if (!req->major && !req->minor) {
2639                 req->major = 1;
2640                 req->minor = 1;
2641         }
2642
2643         EVUTIL_ASSERT(req->evcon == NULL);
2644         req->evcon = evcon;
2645         EVUTIL_ASSERT(!(req->flags & EVHTTP_REQ_OWN_CONNECTION));
2646
2647         TAILQ_INSERT_TAIL(&evcon->requests, req, next);
2648
2649         /* We do not want to conflict with retry_ev */
2650         if (evcon->retry_cnt)
2651                 return (0);
2652
2653         /* If the connection object is not connected; make it so */
2654         if (!evhttp_connected(evcon)) {
2655                 int res = evhttp_connection_connect_(evcon);
2656                 /* evhttp_connection_fail_(), which is called through
2657                  * evhttp_connection_connect_(), assumes that req lies in
2658                  * evcon->requests.  Thus, enqueue the request in advance and
2659                  * remove it in the error case. */
2660                 if (res != 0)
2661                         TAILQ_REMOVE(&evcon->requests, req, next);
2662
2663                 return (res);
2664         }
2665
2666         /*
2667          * If it's connected already and we are the first in the queue,
2668          * then we can dispatch this request immediately.  Otherwise, it
2669          * will be dispatched once the pending requests are completed.
2670          */
2671         if (TAILQ_FIRST(&evcon->requests) == req)
2672                 evhttp_request_dispatch(evcon);
2673
2674         return (0);
2675 }
2676
2677 void
2678 evhttp_cancel_request(struct evhttp_request *req)
2679 {
2680         struct evhttp_connection *evcon = req->evcon;
2681         if (evcon != NULL) {
2682                 /* We need to remove it from the connection */
2683                 if (TAILQ_FIRST(&evcon->requests) == req) {
2684                         /* it's currently being worked on, so reset
2685                          * the connection.
2686                          */
2687                         evhttp_connection_fail_(evcon,
2688                             EVREQ_HTTP_REQUEST_CANCEL);
2689
2690                         /* connection fail freed the request */
2691                         return;
2692                 } else {
2693                         /* otherwise, we can just remove it from the
2694                          * queue
2695                          */
2696                         TAILQ_REMOVE(&evcon->requests, req, next);
2697                 }
2698         }
2699
2700         evhttp_request_free_auto(req);
2701 }
2702
2703 /*
2704  * Reads data from file descriptor into request structure
2705  * Request structure needs to be set up correctly.
2706  */
2707
2708 void
2709 evhttp_start_read_(struct evhttp_connection *evcon)
2710 {
2711         bufferevent_disable(evcon->bufev, EV_WRITE);
2712         bufferevent_enable(evcon->bufev, EV_READ);
2713
2714         evcon->state = EVCON_READING_FIRSTLINE;
2715         /* Reset the bufferevent callbacks */
2716         bufferevent_setcb(evcon->bufev,
2717             evhttp_read_cb,
2718             evhttp_write_cb,
2719             evhttp_error_cb,
2720             evcon);
2721
2722         /* If there's still data pending, process it next time through the
2723          * loop.  Don't do it now; that could get recusive. */
2724         if (evbuffer_get_length(bufferevent_get_input(evcon->bufev))) {
2725                 event_deferred_cb_schedule_(get_deferred_queue(evcon),
2726                     &evcon->read_more_deferred_cb);
2727         }
2728 }
2729
2730 void
2731 evhttp_start_write_(struct evhttp_connection *evcon)
2732 {
2733         bufferevent_disable(evcon->bufev, EV_WRITE);
2734         bufferevent_enable(evcon->bufev, EV_READ);
2735
2736         evcon->state = EVCON_WRITING;
2737         evhttp_write_buffer(evcon, evhttp_write_connectioncb, NULL);
2738 }
2739
2740 static void
2741 evhttp_send_done(struct evhttp_connection *evcon, void *arg)
2742 {
2743         int need_close;
2744         struct evhttp_request *req = TAILQ_FIRST(&evcon->requests);
2745         TAILQ_REMOVE(&evcon->requests, req, next);
2746
2747         if (req->on_complete_cb != NULL) {
2748                 req->on_complete_cb(req, req->on_complete_cb_arg);
2749         }
2750
2751         need_close =
2752             (REQ_VERSION_BEFORE(req, 1, 1) &&
2753             !evhttp_is_connection_keepalive(req->input_headers)) ||
2754             evhttp_is_request_connection_close(req);
2755
2756         EVUTIL_ASSERT(req->flags & EVHTTP_REQ_OWN_CONNECTION);
2757         evhttp_request_free(req);
2758
2759         if (need_close) {
2760                 evhttp_connection_free(evcon);
2761                 return;
2762         }
2763
2764         /* we have a persistent connection; try to accept another request. */
2765         if (evhttp_associate_new_request_with_connection(evcon) == -1) {
2766                 evhttp_connection_free(evcon);
2767         }
2768 }
2769
2770 /*
2771  * Returns an error page.
2772  */
2773
2774 void
2775 evhttp_send_error(struct evhttp_request *req, int error, const char *reason)
2776 {
2777
2778 #define ERR_FORMAT "<HTML><HEAD>\n" \
2779             "<TITLE>%d %s</TITLE>\n" \
2780             "</HEAD><BODY>\n" \
2781             "<H1>%s</H1>\n" \
2782             "</BODY></HTML>\n"
2783
2784         struct evbuffer *buf = evbuffer_new();
2785         if (buf == NULL) {
2786                 /* if we cannot allocate memory; we just drop the connection */
2787                 evhttp_connection_free(req->evcon);
2788                 return;
2789         }
2790         if (reason == NULL) {
2791                 reason = evhttp_response_phrase_internal(error);
2792         }
2793
2794         evhttp_response_code_(req, error, reason);
2795
2796         evbuffer_add_printf(buf, ERR_FORMAT, error, reason, reason);
2797
2798         evhttp_send_page_(req, buf);
2799
2800         evbuffer_free(buf);
2801 #undef ERR_FORMAT
2802 }
2803
2804 /* Requires that headers and response code are already set up */
2805
2806 static inline void
2807 evhttp_send(struct evhttp_request *req, struct evbuffer *databuf)
2808 {
2809         struct evhttp_connection *evcon = req->evcon;
2810
2811         if (evcon == NULL) {
2812                 evhttp_request_free(req);
2813                 return;
2814         }
2815
2816         EVUTIL_ASSERT(TAILQ_FIRST(&evcon->requests) == req);
2817
2818         /* we expect no more calls form the user on this request */
2819         req->userdone = 1;
2820
2821         /* xxx: not sure if we really should expose the data buffer this way */
2822         if (databuf != NULL)
2823                 evbuffer_add_buffer(req->output_buffer, databuf);
2824
2825         /* Adds headers to the response */
2826         evhttp_make_header(evcon, req);
2827
2828         evhttp_write_buffer(evcon, evhttp_send_done, NULL);
2829 }
2830
2831 void
2832 evhttp_send_reply(struct evhttp_request *req, int code, const char *reason,
2833     struct evbuffer *databuf)
2834 {
2835         evhttp_response_code_(req, code, reason);
2836
2837         evhttp_send(req, databuf);
2838 }
2839
2840 void
2841 evhttp_send_reply_start(struct evhttp_request *req, int code,
2842     const char *reason)
2843 {
2844         evhttp_response_code_(req, code, reason);
2845
2846         if (req->evcon == NULL)
2847                 return;
2848
2849         if (evhttp_find_header(req->output_headers, "Content-Length") == NULL &&
2850             REQ_VERSION_ATLEAST(req, 1, 1) &&
2851             evhttp_response_needs_body(req)) {
2852                 /*
2853                  * prefer HTTP/1.1 chunked encoding to closing the connection;
2854                  * note RFC 2616 section 4.4 forbids it with Content-Length:
2855                  * and it's not necessary then anyway.
2856                  */
2857                 evhttp_add_header(req->output_headers, "Transfer-Encoding",
2858                     "chunked");
2859                 req->chunked = 1;
2860         } else {
2861                 req->chunked = 0;
2862         }
2863         evhttp_make_header(req->evcon, req);
2864         evhttp_write_buffer(req->evcon, NULL, NULL);
2865 }
2866
2867 void
2868 evhttp_send_reply_chunk_with_cb(struct evhttp_request *req, struct evbuffer *databuf,
2869     void (*cb)(struct evhttp_connection *, void *), void *arg)
2870 {
2871         struct evhttp_connection *evcon = req->evcon;
2872         struct evbuffer *output;
2873
2874         if (evcon == NULL)
2875                 return;
2876
2877         output = bufferevent_get_output(evcon->bufev);
2878
2879         if (evbuffer_get_length(databuf) == 0)
2880                 return;
2881         if (!evhttp_response_needs_body(req))
2882                 return;
2883         if (req->chunked) {
2884                 evbuffer_add_printf(output, "%x\r\n",
2885                                     (unsigned)evbuffer_get_length(databuf));
2886         }
2887         evbuffer_add_buffer(output, databuf);
2888         if (req->chunked) {
2889                 evbuffer_add(output, "\r\n", 2);
2890         }
2891         evhttp_write_buffer(evcon, cb, arg);
2892 }
2893
2894 void
2895 evhttp_send_reply_chunk(struct evhttp_request *req, struct evbuffer *databuf)
2896 {
2897         evhttp_send_reply_chunk_with_cb(req, databuf, NULL, NULL);
2898 }
2899 void
2900 evhttp_send_reply_end(struct evhttp_request *req)
2901 {
2902         struct evhttp_connection *evcon = req->evcon;
2903         struct evbuffer *output;
2904
2905         if (evcon == NULL) {
2906                 evhttp_request_free(req);
2907                 return;
2908         }
2909
2910         output = bufferevent_get_output(evcon->bufev);
2911
2912         /* we expect no more calls form the user on this request */
2913         req->userdone = 1;
2914
2915         if (req->chunked) {
2916                 evbuffer_add(output, "0\r\n\r\n", 5);
2917                 evhttp_write_buffer(req->evcon, evhttp_send_done, NULL);
2918                 req->chunked = 0;
2919         } else if (evbuffer_get_length(output) == 0) {
2920                 /* let the connection know that we are done with the request */
2921                 evhttp_send_done(evcon, NULL);
2922         } else {
2923                 /* make the callback execute after all data has been written */
2924                 evcon->cb = evhttp_send_done;
2925                 evcon->cb_arg = NULL;
2926         }
2927 }
2928
2929 static const char *informational_phrases[] = {
2930         /* 100 */ "Continue",
2931         /* 101 */ "Switching Protocols"
2932 };
2933
2934 static const char *success_phrases[] = {
2935         /* 200 */ "OK",
2936         /* 201 */ "Created",
2937         /* 202 */ "Accepted",
2938         /* 203 */ "Non-Authoritative Information",
2939         /* 204 */ "No Content",
2940         /* 205 */ "Reset Content",
2941         /* 206 */ "Partial Content"
2942 };
2943
2944 static const char *redirection_phrases[] = {
2945         /* 300 */ "Multiple Choices",
2946         /* 301 */ "Moved Permanently",
2947         /* 302 */ "Found",
2948         /* 303 */ "See Other",
2949         /* 304 */ "Not Modified",
2950         /* 305 */ "Use Proxy",
2951         /* 307 */ "Temporary Redirect"
2952 };
2953
2954 static const char *client_error_phrases[] = {
2955         /* 400 */ "Bad Request",
2956         /* 401 */ "Unauthorized",
2957         /* 402 */ "Payment Required",
2958         /* 403 */ "Forbidden",
2959         /* 404 */ "Not Found",
2960         /* 405 */ "Method Not Allowed",
2961         /* 406 */ "Not Acceptable",
2962         /* 407 */ "Proxy Authentication Required",
2963         /* 408 */ "Request Time-out",
2964         /* 409 */ "Conflict",
2965         /* 410 */ "Gone",
2966         /* 411 */ "Length Required",
2967         /* 412 */ "Precondition Failed",
2968         /* 413 */ "Request Entity Too Large",
2969         /* 414 */ "Request-URI Too Large",
2970         /* 415 */ "Unsupported Media Type",
2971         /* 416 */ "Requested range not satisfiable",
2972         /* 417 */ "Expectation Failed"
2973 };
2974
2975 static const char *server_error_phrases[] = {
2976         /* 500 */ "Internal Server Error",
2977         /* 501 */ "Not Implemented",
2978         /* 502 */ "Bad Gateway",
2979         /* 503 */ "Service Unavailable",
2980         /* 504 */ "Gateway Time-out",
2981         /* 505 */ "HTTP Version not supported"
2982 };
2983
2984 struct response_class {
2985         const char *name;
2986         size_t num_responses;
2987         const char **responses;
2988 };
2989
2990 #ifndef MEMBERSOF
2991 #define MEMBERSOF(x) (sizeof(x)/sizeof(x[0]))
2992 #endif
2993
2994 static const struct response_class response_classes[] = {
2995         /* 1xx */ { "Informational", MEMBERSOF(informational_phrases), informational_phrases },
2996         /* 2xx */ { "Success", MEMBERSOF(success_phrases), success_phrases },
2997         /* 3xx */ { "Redirection", MEMBERSOF(redirection_phrases), redirection_phrases },
2998         /* 4xx */ { "Client Error", MEMBERSOF(client_error_phrases), client_error_phrases },
2999         /* 5xx */ { "Server Error", MEMBERSOF(server_error_phrases), server_error_phrases }
3000 };
3001
3002 static const char *
3003 evhttp_response_phrase_internal(int code)
3004 {
3005         int klass = code / 100 - 1;
3006         int subcode = code % 100;
3007
3008         /* Unknown class - can't do any better here */
3009         if (klass < 0 || klass >= (int) MEMBERSOF(response_classes))
3010                 return "Unknown Status Class";
3011
3012         /* Unknown sub-code, return class name at least */
3013         if (subcode >= (int) response_classes[klass].num_responses)
3014                 return response_classes[klass].name;
3015
3016         return response_classes[klass].responses[subcode];
3017 }
3018
3019 void
3020 evhttp_response_code_(struct evhttp_request *req, int code, const char *reason)
3021 {
3022         req->kind = EVHTTP_RESPONSE;
3023         req->response_code = code;
3024         if (req->response_code_line != NULL)
3025                 mm_free(req->response_code_line);
3026         if (reason == NULL)
3027                 reason = evhttp_response_phrase_internal(code);
3028         req->response_code_line = mm_strdup(reason);
3029         if (req->response_code_line == NULL) {
3030                 event_warn("%s: strdup", __func__);
3031                 /* XXX what else can we do? */
3032         }
3033 }
3034
3035 void
3036 evhttp_send_page_(struct evhttp_request *req, struct evbuffer *databuf)
3037 {
3038         if (!req->major || !req->minor) {
3039                 req->major = 1;
3040                 req->minor = 1;
3041         }
3042
3043         if (req->kind != EVHTTP_RESPONSE)
3044                 evhttp_response_code_(req, 200, "OK");
3045
3046         evhttp_clear_headers(req->output_headers);
3047         evhttp_add_header(req->output_headers, "Content-Type", "text/html");
3048         evhttp_add_header(req->output_headers, "Connection", "close");
3049
3050         evhttp_send(req, databuf);
3051 }
3052
3053 static const char uri_chars[256] = {
3054         /* 0 */
3055         0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
3056         0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
3057         0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 1, 1, 0,
3058         1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 0, 0, 0, 0, 0, 0,
3059         /* 64 */
3060         0, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
3061         1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 0, 0, 0, 0, 1,
3062         0, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
3063         1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 0, 0, 0, 1, 0,
3064         /* 128 */
3065         0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
3066         0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
3067         0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
3068         0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
3069         /* 192 */
3070         0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
3071         0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
3072         0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
3073         0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
3074 };
3075
3076 #define CHAR_IS_UNRESERVED(c)                   \
3077         (uri_chars[(unsigned char)(c)])
3078
3079 /*
3080  * Helper functions to encode/decode a string for inclusion in a URI.
3081  * The returned string must be freed by the caller.
3082  */
3083 char *
3084 evhttp_uriencode(const char *uri, ev_ssize_t len, int space_as_plus)
3085 {
3086         struct evbuffer *buf = evbuffer_new();
3087         const char *p, *end;
3088         char *result = NULL;
3089
3090         if (!buf) {
3091                 goto out;
3092         }
3093
3094         if (len >= 0) {
3095                 if (uri + len < uri) {
3096                         goto out;
3097                 }
3098
3099                 end = uri + len;
3100         } else {
3101                 size_t slen = strlen(uri);
3102
3103                 if (slen >= EV_SSIZE_MAX) {
3104                         /* we don't want to mix signed and unsigned */
3105                         goto out;
3106                 }
3107
3108                 if (uri + slen < uri) {
3109                         goto out;
3110                 }
3111
3112                 end = uri + slen;
3113         }
3114
3115         for (p = uri; p < end; p++) {
3116                 if (CHAR_IS_UNRESERVED(*p)) {
3117                         evbuffer_add(buf, p, 1);
3118                 } else if (*p == ' ' && space_as_plus) {
3119                         evbuffer_add(buf, "+", 1);
3120                 } else {
3121                         evbuffer_add_printf(buf, "%%%02X", (unsigned char)(*p));
3122                 }
3123         }
3124
3125         evbuffer_add(buf, "", 1); /* NUL-terminator. */
3126         result = mm_malloc(evbuffer_get_length(buf));
3127
3128         if (result)
3129                 evbuffer_remove(buf, result, evbuffer_get_length(buf));
3130
3131 out:
3132         if (buf)
3133                 evbuffer_free(buf);
3134         return result;
3135 }
3136
3137 char *
3138 evhttp_encode_uri(const char *str)
3139 {
3140         return evhttp_uriencode(str, -1, 0);
3141 }
3142
3143 /*
3144  * @param decode_plus_ctl: if 1, we decode plus into space.  If 0, we don't.
3145  *     If -1, when true we transform plus to space only after we've seen
3146  *     a ?.  -1 is deprecated.
3147  * @return the number of bytes written to 'ret'.
3148  */
3149 int
3150 evhttp_decode_uri_internal(
3151         const char *uri, size_t length, char *ret, int decode_plus_ctl)
3152 {
3153         char c;
3154         int j;
3155         int decode_plus = (decode_plus_ctl == 1) ? 1: 0;
3156         unsigned i;
3157
3158         for (i = j = 0; i < length; i++) {
3159                 c = uri[i];
3160                 if (c == '?') {
3161                         if (decode_plus_ctl < 0)
3162                                 decode_plus = 1;
3163                 } else if (c == '+' && decode_plus) {
3164                         c = ' ';
3165                 } else if ((i + 2) < length && c == '%' &&
3166                         EVUTIL_ISXDIGIT_(uri[i+1]) && EVUTIL_ISXDIGIT_(uri[i+2])) {
3167                         char tmp[3];
3168                         tmp[0] = uri[i+1];
3169                         tmp[1] = uri[i+2];
3170                         tmp[2] = '\0';
3171                         c = (char)strtol(tmp, NULL, 16);
3172                         i += 2;
3173                 }
3174                 ret[j++] = c;
3175         }
3176         ret[j] = '\0';
3177
3178         return (j);
3179 }
3180
3181 /* deprecated */
3182 char *
3183 evhttp_decode_uri(const char *uri)
3184 {
3185         char *ret;
3186
3187         if ((ret = mm_malloc(strlen(uri) + 1)) == NULL) {
3188                 event_warn("%s: malloc(%lu)", __func__,
3189                           (unsigned long)(strlen(uri) + 1));
3190                 return (NULL);
3191         }
3192
3193         evhttp_decode_uri_internal(uri, strlen(uri),
3194             ret, -1 /*always_decode_plus*/);
3195
3196         return (ret);
3197 }
3198
3199 char *
3200 evhttp_uridecode(const char *uri, int decode_plus, size_t *size_out)
3201 {
3202         char *ret;
3203         int n;
3204
3205         if ((ret = mm_malloc(strlen(uri) + 1)) == NULL) {
3206                 event_warn("%s: malloc(%lu)", __func__,
3207                           (unsigned long)(strlen(uri) + 1));
3208                 return (NULL);
3209         }
3210
3211         n = evhttp_decode_uri_internal(uri, strlen(uri),
3212             ret, !!decode_plus/*always_decode_plus*/);
3213
3214         if (size_out) {
3215                 EVUTIL_ASSERT(n >= 0);
3216                 *size_out = (size_t)n;
3217         }
3218
3219         return (ret);
3220 }
3221
3222 /*
3223  * Helper function to parse out arguments in a query.
3224  * The arguments are separated by key and value.
3225  */
3226
3227 static int
3228 evhttp_parse_query_impl(const char *str, struct evkeyvalq *headers,
3229     int is_whole_uri, unsigned flags)
3230 {
3231         char *line=NULL;
3232         char *argument;
3233         char *p;
3234         const char *query_part;
3235         int result = -1;
3236         struct evhttp_uri *uri=NULL;
3237
3238         TAILQ_INIT(headers);
3239
3240         if (is_whole_uri) {
3241                 uri = evhttp_uri_parse(str);
3242                 if (!uri)
3243                         goto error;
3244                 query_part = evhttp_uri_get_query(uri);
3245         } else {
3246                 query_part = str;
3247         }
3248
3249         /* No arguments - we are done */
3250         if (!query_part || !strlen(query_part)) {
3251                 result = 0;
3252                 goto done;
3253         }
3254
3255         if ((line = mm_strdup(query_part)) == NULL) {
3256                 event_warn("%s: strdup", __func__);
3257                 goto error;
3258         }
3259
3260         p = argument = line;
3261         while (p != NULL && *p != '\0') {
3262                 char *key, *value, *decoded_value;
3263                 argument = strsep(&p, "&");
3264
3265                 value = argument;
3266                 key = strsep(&value, "=");
3267                 if (flags & EVHTTP_URI_QUERY_NONCONFORMANT) {
3268                         if (value == NULL)
3269                                 value = "";
3270                         if (*key == '\0')
3271                                 continue;
3272                 } else {
3273                         if (value == NULL || *key == '\0')
3274                                 goto error;
3275                 }
3276
3277                 if ((decoded_value = mm_malloc(strlen(value) + 1)) == NULL) {
3278                         event_warn("%s: mm_malloc", __func__);
3279                         goto error;
3280                 }
3281                 evhttp_decode_uri_internal(value, strlen(value),
3282                     decoded_value, 1 /*always_decode_plus*/);
3283                 event_debug(("Query Param: %s -> %s\n", key, decoded_value));
3284                 if (flags & EVHTTP_URI_QUERY_LAST_VAL)
3285                         evhttp_remove_header(headers, key);
3286                 evhttp_add_header_internal(headers, key, decoded_value);
3287                 mm_free(decoded_value);
3288         }
3289
3290         result = 0;
3291         goto done;
3292 error:
3293         evhttp_clear_headers(headers);
3294 done:
3295         if (line)
3296                 mm_free(line);
3297         if (uri)
3298                 evhttp_uri_free(uri);
3299         return result;
3300 }
3301
3302 int
3303 evhttp_parse_query(const char *uri, struct evkeyvalq *headers)
3304 {
3305         return evhttp_parse_query_impl(uri, headers, 1, 0);
3306 }
3307 int
3308 evhttp_parse_query_str(const char *uri, struct evkeyvalq *headers)
3309 {
3310         return evhttp_parse_query_impl(uri, headers, 0, 0);
3311 }
3312 int
3313 evhttp_parse_query_str_flags(const char *uri, struct evkeyvalq *headers, unsigned flags)
3314 {
3315         return evhttp_parse_query_impl(uri, headers, 0, flags);
3316 }
3317
3318 static struct evhttp_cb *
3319 evhttp_dispatch_callback(struct httpcbq *callbacks, struct evhttp_request *req)
3320 {
3321         struct evhttp_cb *cb;
3322         size_t offset = 0;
3323         char *translated;
3324         const char *path;
3325
3326         /* Test for different URLs */
3327         path = evhttp_uri_get_path(req->uri_elems);
3328         offset = strlen(path);
3329         if ((translated = mm_malloc(offset + 1)) == NULL)
3330                 return (NULL);
3331         evhttp_decode_uri_internal(path, offset, translated,
3332             0 /* decode_plus */);
3333
3334         TAILQ_FOREACH(cb, callbacks, next) {
3335                 if (!strcmp(cb->what, translated)) {
3336                         mm_free(translated);
3337                         return (cb);
3338                 }
3339         }
3340
3341         mm_free(translated);
3342         return (NULL);
3343 }
3344
3345
3346 static int
3347 prefix_suffix_match(const char *pattern, const char *name, int ignorecase)
3348 {
3349         char c;
3350
3351         while (1) {
3352                 switch (c = *pattern++) {
3353                 case '\0':
3354                         return *name == '\0';
3355
3356                 case '*':
3357                         while (*name != '\0') {
3358                                 if (prefix_suffix_match(pattern, name,
3359                                         ignorecase))
3360                                         return (1);
3361                                 ++name;
3362                         }
3363                         return (0);
3364                 default:
3365                         if (c != *name) {
3366                                 if (!ignorecase ||
3367                                     EVUTIL_TOLOWER_(c) != EVUTIL_TOLOWER_(*name))
3368                                         return (0);
3369                         }
3370                         ++name;
3371                 }
3372         }
3373         /* NOTREACHED */
3374 }
3375
3376 /*
3377    Search the vhost hierarchy beginning with http for a server alias
3378    matching hostname.  If a match is found, and outhttp is non-null,
3379    outhttp is set to the matching http object and 1 is returned.
3380 */
3381
3382 static int
3383 evhttp_find_alias(struct evhttp *http, struct evhttp **outhttp,
3384                   const char *hostname)
3385 {
3386         struct evhttp_server_alias *alias;
3387         struct evhttp *vhost;
3388
3389         TAILQ_FOREACH(alias, &http->aliases, next) {
3390                 /* XXX Do we need to handle IP addresses? */
3391                 if (!evutil_ascii_strcasecmp(alias->alias, hostname)) {
3392                         if (outhttp)
3393                                 *outhttp = http;
3394                         return 1;
3395                 }
3396         }
3397
3398         /* XXX It might be good to avoid recursion here, but I don't
3399            see a way to do that w/o a list. */
3400         TAILQ_FOREACH(vhost, &http->virtualhosts, next_vhost) {
3401                 if (evhttp_find_alias(vhost, outhttp, hostname))
3402                         return 1;
3403         }
3404
3405         return 0;
3406 }
3407
3408 /*
3409    Attempts to find the best http object to handle a request for a hostname.
3410    All aliases for the root http object and vhosts are searched for an exact
3411    match. Then, the vhost hierarchy is traversed again for a matching
3412    pattern.
3413
3414    If an alias or vhost is matched, 1 is returned, and outhttp, if non-null,
3415    is set with the best matching http object. If there are no matches, the
3416    root http object is stored in outhttp and 0 is returned.
3417 */
3418
3419 static int
3420 evhttp_find_vhost(struct evhttp *http, struct evhttp **outhttp,
3421                   const char *hostname)
3422 {
3423         struct evhttp *vhost;
3424         struct evhttp *oldhttp;
3425         int match_found = 0;
3426
3427         if (evhttp_find_alias(http, outhttp, hostname))
3428                 return 1;
3429
3430         do {
3431                 oldhttp = http;
3432                 TAILQ_FOREACH(vhost, &http->virtualhosts, next_vhost) {
3433                         if (prefix_suffix_match(vhost->vhost_pattern,
3434                                 hostname, 1 /* ignorecase */)) {
3435                                 http = vhost;
3436                                 match_found = 1;
3437                                 break;
3438                         }
3439                 }
3440         } while (oldhttp != http);
3441
3442         if (outhttp)
3443                 *outhttp = http;
3444
3445         return match_found;
3446 }
3447
3448 static void
3449 evhttp_handle_request(struct evhttp_request *req, void *arg)
3450 {
3451         struct evhttp *http = arg;
3452         struct evhttp_cb *cb = NULL;
3453         const char *hostname;
3454
3455         /* we have a new request on which the user needs to take action */
3456         req->userdone = 0;
3457
3458         bufferevent_disable(req->evcon->bufev, EV_READ);
3459
3460         if (req->type == 0 || req->uri == NULL) {
3461                 evhttp_send_error(req, req->response_code, NULL);
3462                 return;
3463         }
3464
3465         if ((http->allowed_methods & req->type) == 0) {
3466                 event_debug(("Rejecting disallowed method %x (allowed: %x)\n",
3467                         (unsigned)req->type, (unsigned)http->allowed_methods));
3468                 evhttp_send_error(req, HTTP_NOTIMPLEMENTED, NULL);
3469                 return;
3470         }
3471
3472         /* handle potential virtual hosts */
3473         hostname = evhttp_request_get_host(req);
3474         if (hostname != NULL) {
3475                 evhttp_find_vhost(http, &http, hostname);
3476         }
3477
3478         if ((cb = evhttp_dispatch_callback(&http->callbacks, req)) != NULL) {
3479                 (*cb->cb)(req, cb->cbarg);
3480                 return;
3481         }
3482
3483         /* Generic call back */
3484         if (http->gencb) {
3485                 (*http->gencb)(req, http->gencbarg);
3486                 return;
3487         } else {
3488                 /* We need to send a 404 here */
3489 #define ERR_FORMAT "<html><head>" \
3490                     "<title>404 Not Found</title>" \
3491                     "</head><body>" \
3492                     "<h1>Not Found</h1>" \
3493                     "<p>The requested URL %s was not found on this server.</p>"\
3494                     "</body></html>\n"
3495
3496                 char *escaped_html;
3497                 struct evbuffer *buf;
3498
3499                 if ((escaped_html = evhttp_htmlescape(req->uri)) == NULL) {
3500                         evhttp_connection_free(req->evcon);
3501                         return;
3502                 }
3503
3504                 if ((buf = evbuffer_new()) == NULL) {
3505                         mm_free(escaped_html);
3506                         evhttp_connection_free(req->evcon);
3507                         return;
3508                 }
3509
3510                 evhttp_response_code_(req, HTTP_NOTFOUND, "Not Found");
3511
3512                 evbuffer_add_printf(buf, ERR_FORMAT, escaped_html);
3513
3514                 mm_free(escaped_html);
3515
3516                 evhttp_send_page_(req, buf);
3517
3518                 evbuffer_free(buf);
3519 #undef ERR_FORMAT
3520         }
3521 }
3522
3523 /* Listener callback when a connection arrives at a server. */
3524 static void
3525 accept_socket_cb(struct evconnlistener *listener, evutil_socket_t nfd, struct sockaddr *peer_sa, int peer_socklen, void *arg)
3526 {
3527         struct evhttp *http = arg;
3528
3529         evhttp_get_request(http, nfd, peer_sa, peer_socklen);
3530 }
3531
3532 int
3533 evhttp_bind_socket(struct evhttp *http, const char *address, ev_uint16_t port)
3534 {
3535         struct evhttp_bound_socket *bound =
3536                 evhttp_bind_socket_with_handle(http, address, port);
3537         if (bound == NULL)
3538                 return (-1);
3539         return (0);
3540 }
3541
3542 struct evhttp_bound_socket *
3543 evhttp_bind_socket_with_handle(struct evhttp *http, const char *address, ev_uint16_t port)
3544 {
3545         evutil_socket_t fd;
3546         struct evhttp_bound_socket *bound;
3547
3548         if ((fd = bind_socket(address, port, 1 /*reuse*/)) == -1)
3549                 return (NULL);
3550
3551         if (listen(fd, 128) == -1) {
3552                 event_sock_warn(fd, "%s: listen", __func__);
3553                 evutil_closesocket(fd);
3554                 return (NULL);
3555         }
3556
3557         bound = evhttp_accept_socket_with_handle(http, fd);
3558
3559         if (bound != NULL) {
3560                 event_debug(("Bound to port %d - Awaiting connections ... ",
3561                         port));
3562                 return (bound);
3563         }
3564
3565         return (NULL);
3566 }
3567
3568 int
3569 evhttp_accept_socket(struct evhttp *http, evutil_socket_t fd)
3570 {
3571         struct evhttp_bound_socket *bound =
3572                 evhttp_accept_socket_with_handle(http, fd);
3573         if (bound == NULL)
3574                 return (-1);
3575         return (0);
3576 }
3577
3578 void
3579 evhttp_foreach_bound_socket(struct evhttp *http,
3580                             evhttp_bound_socket_foreach_fn *function,
3581                             void *argument)
3582 {
3583         struct evhttp_bound_socket *bound;
3584
3585         TAILQ_FOREACH(bound, &http->sockets, next)
3586                 function(bound, argument);
3587 }
3588
3589 struct evhttp_bound_socket *
3590 evhttp_accept_socket_with_handle(struct evhttp *http, evutil_socket_t fd)
3591 {
3592         struct evhttp_bound_socket *bound;
3593         struct evconnlistener *listener;
3594         const int flags =
3595             LEV_OPT_REUSEABLE|LEV_OPT_CLOSE_ON_EXEC|LEV_OPT_CLOSE_ON_FREE;
3596
3597         listener = evconnlistener_new(http->base, NULL, NULL,
3598             flags,
3599             0, /* Backlog is '0' because we already said 'listen' */
3600             fd);
3601         if (!listener)
3602                 return (NULL);
3603
3604         bound = evhttp_bind_listener(http, listener);
3605         if (!bound) {
3606                 evconnlistener_free(listener);
3607                 return (NULL);
3608         }
3609         return (bound);
3610 }
3611
3612 struct evhttp_bound_socket *
3613 evhttp_bind_listener(struct evhttp *http, struct evconnlistener *listener)
3614 {
3615         struct evhttp_bound_socket *bound;
3616
3617         bound = mm_malloc(sizeof(struct evhttp_bound_socket));
3618         if (bound == NULL)
3619                 return (NULL);
3620
3621         bound->listener = listener;
3622         TAILQ_INSERT_TAIL(&http->sockets, bound, next);
3623
3624         evconnlistener_set_cb(listener, accept_socket_cb, http);
3625         return bound;
3626 }
3627
3628 evutil_socket_t
3629 evhttp_bound_socket_get_fd(struct evhttp_bound_socket *bound)
3630 {
3631         return evconnlistener_get_fd(bound->listener);
3632 }
3633
3634 struct evconnlistener *
3635 evhttp_bound_socket_get_listener(struct evhttp_bound_socket *bound)
3636 {
3637         return bound->listener;
3638 }
3639
3640 void
3641 evhttp_del_accept_socket(struct evhttp *http, struct evhttp_bound_socket *bound)
3642 {
3643         TAILQ_REMOVE(&http->sockets, bound, next);
3644         evconnlistener_free(bound->listener);
3645         mm_free(bound);
3646 }
3647
3648 static struct evhttp*
3649 evhttp_new_object(void)
3650 {
3651         struct evhttp *http = NULL;
3652
3653         if ((http = mm_calloc(1, sizeof(struct evhttp))) == NULL) {
3654                 event_warn("%s: calloc", __func__);
3655                 return (NULL);
3656         }
3657
3658         evutil_timerclear(&http->timeout);
3659         evhttp_set_max_headers_size(http, EV_SIZE_MAX);
3660         evhttp_set_max_body_size(http, EV_SIZE_MAX);
3661         evhttp_set_default_content_type(http, "text/html; charset=ISO-8859-1");
3662         evhttp_set_allowed_methods(http,
3663             EVHTTP_REQ_GET |
3664             EVHTTP_REQ_POST |
3665             EVHTTP_REQ_HEAD |
3666             EVHTTP_REQ_PUT |
3667             EVHTTP_REQ_DELETE);
3668
3669         TAILQ_INIT(&http->sockets);
3670         TAILQ_INIT(&http->callbacks);
3671         TAILQ_INIT(&http->connections);
3672         TAILQ_INIT(&http->virtualhosts);
3673         TAILQ_INIT(&http->aliases);
3674
3675         return (http);
3676 }
3677
3678 struct evhttp *
3679 evhttp_new(struct event_base *base)
3680 {
3681         struct evhttp *http = NULL;
3682
3683         http = evhttp_new_object();
3684         if (http == NULL)
3685                 return (NULL);
3686         http->base = base;
3687
3688         return (http);
3689 }
3690
3691 /*
3692  * Start a web server on the specified address and port.
3693  */
3694
3695 struct evhttp *
3696 evhttp_start(const char *address, ev_uint16_t port)
3697 {
3698         struct evhttp *http = NULL;
3699
3700         http = evhttp_new_object();
3701         if (http == NULL)
3702                 return (NULL);
3703         if (evhttp_bind_socket(http, address, port) == -1) {
3704                 mm_free(http);
3705                 return (NULL);
3706         }
3707
3708         return (http);
3709 }
3710
3711 void
3712 evhttp_free(struct evhttp* http)
3713 {
3714         struct evhttp_cb *http_cb;
3715         struct evhttp_connection *evcon;
3716         struct evhttp_bound_socket *bound;
3717         struct evhttp* vhost;
3718         struct evhttp_server_alias *alias;
3719
3720         /* Remove the accepting part */
3721         while ((bound = TAILQ_FIRST(&http->sockets)) != NULL) {
3722                 TAILQ_REMOVE(&http->sockets, bound, next);
3723
3724                 evconnlistener_free(bound->listener);
3725
3726                 mm_free(bound);
3727         }
3728
3729         while ((evcon = TAILQ_FIRST(&http->connections)) != NULL) {
3730                 /* evhttp_connection_free removes the connection */
3731                 evhttp_connection_free(evcon);
3732         }
3733
3734         while ((http_cb = TAILQ_FIRST(&http->callbacks)) != NULL) {
3735                 TAILQ_REMOVE(&http->callbacks, http_cb, next);
3736                 mm_free(http_cb->what);
3737                 mm_free(http_cb);
3738         }
3739
3740         while ((vhost = TAILQ_FIRST(&http->virtualhosts)) != NULL) {
3741                 TAILQ_REMOVE(&http->virtualhosts, vhost, next_vhost);
3742
3743                 evhttp_free(vhost);
3744         }
3745
3746         if (http->vhost_pattern != NULL)
3747                 mm_free(http->vhost_pattern);
3748
3749         while ((alias = TAILQ_FIRST(&http->aliases)) != NULL) {
3750                 TAILQ_REMOVE(&http->aliases, alias, next);
3751                 mm_free(alias->alias);
3752                 mm_free(alias);
3753         }
3754
3755         mm_free(http);
3756 }
3757
3758 int
3759 evhttp_add_virtual_host(struct evhttp* http, const char *pattern,
3760     struct evhttp* vhost)
3761 {
3762         /* a vhost can only be a vhost once and should not have bound sockets */
3763         if (vhost->vhost_pattern != NULL ||
3764             TAILQ_FIRST(&vhost->sockets) != NULL)
3765                 return (-1);
3766
3767         vhost->vhost_pattern = mm_strdup(pattern);
3768         if (vhost->vhost_pattern == NULL)
3769                 return (-1);
3770
3771         TAILQ_INSERT_TAIL(&http->virtualhosts, vhost, next_vhost);
3772
3773         return (0);
3774 }
3775
3776 int
3777 evhttp_remove_virtual_host(struct evhttp* http, struct evhttp* vhost)
3778 {
3779         if (vhost->vhost_pattern == NULL)
3780                 return (-1);
3781
3782         TAILQ_REMOVE(&http->virtualhosts, vhost, next_vhost);
3783
3784         mm_free(vhost->vhost_pattern);
3785         vhost->vhost_pattern = NULL;
3786
3787         return (0);
3788 }
3789
3790 int
3791 evhttp_add_server_alias(struct evhttp *http, const char *alias)
3792 {
3793         struct evhttp_server_alias *evalias;
3794
3795         evalias = mm_calloc(1, sizeof(*evalias));
3796         if (!evalias)
3797                 return -1;
3798
3799         evalias->alias = mm_strdup(alias);
3800         if (!evalias->alias) {
3801                 mm_free(evalias);
3802                 return -1;
3803         }
3804
3805         TAILQ_INSERT_TAIL(&http->aliases, evalias, next);
3806
3807         return 0;
3808 }
3809
3810 int
3811 evhttp_remove_server_alias(struct evhttp *http, const char *alias)
3812 {
3813         struct evhttp_server_alias *evalias;
3814
3815         TAILQ_FOREACH(evalias, &http->aliases, next) {
3816                 if (evutil_ascii_strcasecmp(evalias->alias, alias) == 0) {
3817                         TAILQ_REMOVE(&http->aliases, evalias, next);
3818                         mm_free(evalias->alias);
3819                         mm_free(evalias);
3820                         return 0;
3821                 }
3822         }
3823
3824         return -1;
3825 }
3826
3827 void
3828 evhttp_set_timeout(struct evhttp* http, int timeout_in_secs)
3829 {
3830         if (timeout_in_secs == -1) {
3831                 evhttp_set_timeout_tv(http, NULL);
3832         } else {
3833                 struct timeval tv;
3834                 tv.tv_sec = timeout_in_secs;
3835                 tv.tv_usec = 0;
3836                 evhttp_set_timeout_tv(http, &tv);
3837         }
3838 }
3839
3840 void
3841 evhttp_set_timeout_tv(struct evhttp* http, const struct timeval* tv)
3842 {
3843         if (tv) {
3844                 http->timeout = *tv;
3845         } else {
3846                 evutil_timerclear(&http->timeout);
3847         }
3848 }
3849
3850 int evhttp_set_flags(struct evhttp *http, int flags)
3851 {
3852         int avail_flags = 0;
3853         avail_flags |= EVHTTP_SERVER_LINGERING_CLOSE;
3854
3855         if (flags & ~avail_flags)
3856                 return 1;
3857         http->flags &= ~avail_flags;
3858
3859         http->flags |= flags;
3860
3861         return 0;
3862 }
3863
3864 void
3865 evhttp_set_max_headers_size(struct evhttp* http, ev_ssize_t max_headers_size)
3866 {
3867         if (max_headers_size < 0)
3868                 http->default_max_headers_size = EV_SIZE_MAX;
3869         else
3870                 http->default_max_headers_size = max_headers_size;
3871 }
3872
3873 void
3874 evhttp_set_max_body_size(struct evhttp* http, ev_ssize_t max_body_size)
3875 {
3876         if (max_body_size < 0)
3877                 http->default_max_body_size = EV_UINT64_MAX;
3878         else
3879                 http->default_max_body_size = max_body_size;
3880 }
3881
3882 void
3883 evhttp_set_default_content_type(struct evhttp *http,
3884         const char *content_type) {
3885         http->default_content_type = content_type;
3886 }
3887
3888 void
3889 evhttp_set_allowed_methods(struct evhttp* http, ev_uint16_t methods)
3890 {
3891         http->allowed_methods = methods;
3892 }
3893
3894 int
3895 evhttp_set_cb(struct evhttp *http, const char *uri,
3896     void (*cb)(struct evhttp_request *, void *), void *cbarg)
3897 {
3898         struct evhttp_cb *http_cb;
3899
3900         TAILQ_FOREACH(http_cb, &http->callbacks, next) {
3901                 if (strcmp(http_cb->what, uri) == 0)
3902                         return (-1);
3903         }
3904
3905         if ((http_cb = mm_calloc(1, sizeof(struct evhttp_cb))) == NULL) {
3906                 event_warn("%s: calloc", __func__);
3907                 return (-2);
3908         }
3909
3910         http_cb->what = mm_strdup(uri);
3911         if (http_cb->what == NULL) {
3912                 event_warn("%s: strdup", __func__);
3913                 mm_free(http_cb);
3914                 return (-3);
3915         }
3916         http_cb->cb = cb;
3917         http_cb->cbarg = cbarg;
3918
3919         TAILQ_INSERT_TAIL(&http->callbacks, http_cb, next);
3920
3921         return (0);
3922 }
3923
3924 int
3925 evhttp_del_cb(struct evhttp *http, const char *uri)
3926 {
3927         struct evhttp_cb *http_cb;
3928
3929         TAILQ_FOREACH(http_cb, &http->callbacks, next) {
3930                 if (strcmp(http_cb->what, uri) == 0)
3931                         break;
3932         }
3933         if (http_cb == NULL)
3934                 return (-1);
3935
3936         TAILQ_REMOVE(&http->callbacks, http_cb, next);
3937         mm_free(http_cb->what);
3938         mm_free(http_cb);
3939
3940         return (0);
3941 }
3942
3943 void
3944 evhttp_set_gencb(struct evhttp *http,
3945     void (*cb)(struct evhttp_request *, void *), void *cbarg)
3946 {
3947         http->gencb = cb;
3948         http->gencbarg = cbarg;
3949 }
3950
3951 void
3952 evhttp_set_bevcb(struct evhttp *http,
3953     struct bufferevent* (*cb)(struct event_base *, void *), void *cbarg)
3954 {
3955         http->bevcb = cb;
3956         http->bevcbarg = cbarg;
3957 }
3958
3959 void
3960 evhttp_set_newreqcb(struct evhttp *http,
3961     int (*cb)(struct evhttp_request *, void *), void *cbarg)
3962 {
3963         http->newreqcb = cb;
3964         http->newreqcbarg = cbarg;
3965 }
3966
3967 /*
3968  * Request related functions
3969  */
3970
3971 struct evhttp_request *
3972 evhttp_request_new(void (*cb)(struct evhttp_request *, void *), void *arg)
3973 {
3974         struct evhttp_request *req = NULL;
3975
3976         /* Allocate request structure */
3977         if ((req = mm_calloc(1, sizeof(struct evhttp_request))) == NULL) {
3978                 event_warn("%s: calloc", __func__);
3979                 goto error;
3980         }
3981
3982         req->headers_size = 0;
3983         req->body_size = 0;
3984
3985         req->kind = EVHTTP_RESPONSE;
3986         req->input_headers = mm_calloc(1, sizeof(struct evkeyvalq));
3987         if (req->input_headers == NULL) {
3988                 event_warn("%s: calloc", __func__);
3989                 goto error;
3990         }
3991         TAILQ_INIT(req->input_headers);
3992
3993         req->output_headers = mm_calloc(1, sizeof(struct evkeyvalq));
3994         if (req->output_headers == NULL) {
3995                 event_warn("%s: calloc", __func__);
3996                 goto error;
3997         }
3998         TAILQ_INIT(req->output_headers);
3999
4000         if ((req->input_buffer = evbuffer_new()) == NULL) {
4001                 event_warn("%s: evbuffer_new", __func__);
4002                 goto error;
4003         }
4004
4005         if ((req->output_buffer = evbuffer_new()) == NULL) {
4006                 event_warn("%s: evbuffer_new", __func__);
4007                 goto error;
4008         }
4009
4010         req->cb = cb;
4011         req->cb_arg = arg;
4012
4013         return (req);
4014
4015  error:
4016         if (req != NULL)
4017                 evhttp_request_free(req);
4018         return (NULL);
4019 }
4020
4021 void
4022 evhttp_request_free(struct evhttp_request *req)
4023 {
4024         if ((req->flags & EVHTTP_REQ_DEFER_FREE) != 0) {
4025                 req->flags |= EVHTTP_REQ_NEEDS_FREE;
4026                 return;
4027         }
4028
4029         if (req->remote_host != NULL)
4030                 mm_free(req->remote_host);
4031         if (req->uri != NULL)
4032                 mm_free(req->uri);
4033         if (req->uri_elems != NULL)
4034                 evhttp_uri_free(req->uri_elems);
4035         if (req->response_code_line != NULL)
4036                 mm_free(req->response_code_line);
4037         if (req->host_cache != NULL)
4038                 mm_free(req->host_cache);
4039
4040         evhttp_clear_headers(req->input_headers);
4041         mm_free(req->input_headers);
4042
4043         evhttp_clear_headers(req->output_headers);
4044         mm_free(req->output_headers);
4045
4046         if (req->input_buffer != NULL)
4047                 evbuffer_free(req->input_buffer);
4048
4049         if (req->output_buffer != NULL)
4050                 evbuffer_free(req->output_buffer);
4051
4052         mm_free(req);
4053 }
4054
4055 void
4056 evhttp_request_own(struct evhttp_request *req)
4057 {
4058         req->flags |= EVHTTP_USER_OWNED;
4059 }
4060
4061 int
4062 evhttp_request_is_owned(struct evhttp_request *req)
4063 {
4064         return (req->flags & EVHTTP_USER_OWNED) != 0;
4065 }
4066
4067 struct evhttp_connection *
4068 evhttp_request_get_connection(struct evhttp_request *req)
4069 {
4070         return req->evcon;
4071 }
4072
4073 struct event_base *
4074 evhttp_connection_get_base(struct evhttp_connection *conn)
4075 {
4076         return conn->base;
4077 }
4078
4079 void
4080 evhttp_request_set_chunked_cb(struct evhttp_request *req,
4081     void (*cb)(struct evhttp_request *, void *))
4082 {
4083         req->chunk_cb = cb;
4084 }
4085
4086 void
4087 evhttp_request_set_header_cb(struct evhttp_request *req,
4088     int (*cb)(struct evhttp_request *, void *))
4089 {
4090         req->header_cb = cb;
4091 }
4092
4093 void
4094 evhttp_request_set_error_cb(struct evhttp_request *req,
4095     void (*cb)(enum evhttp_request_error, void *))
4096 {
4097         req->error_cb = cb;
4098 }
4099
4100 void
4101 evhttp_request_set_on_complete_cb(struct evhttp_request *req,
4102     void (*cb)(struct evhttp_request *, void *), void *cb_arg)
4103 {
4104         req->on_complete_cb = cb;
4105         req->on_complete_cb_arg = cb_arg;
4106 }
4107
4108 /*
4109  * Allows for inspection of the request URI
4110  */
4111
4112 const char *
4113 evhttp_request_get_uri(const struct evhttp_request *req) {
4114         if (req->uri == NULL)
4115                 event_debug(("%s: request %p has no uri\n", __func__, req));
4116         return (req->uri);
4117 }
4118
4119 const struct evhttp_uri *
4120 evhttp_request_get_evhttp_uri(const struct evhttp_request *req) {
4121         if (req->uri_elems == NULL)
4122                 event_debug(("%s: request %p has no uri elems\n",
4123                             __func__, req));
4124         return (req->uri_elems);
4125 }
4126
4127 const char *
4128 evhttp_request_get_host(struct evhttp_request *req)
4129 {
4130         const char *host = NULL;
4131
4132         if (req->host_cache)
4133                 return req->host_cache;
4134
4135         if (req->uri_elems)
4136                 host = evhttp_uri_get_host(req->uri_elems);
4137         if (!host && req->input_headers) {
4138                 const char *p;
4139                 size_t len;
4140
4141                 host = evhttp_find_header(req->input_headers, "Host");
4142                 /* The Host: header may include a port. Remove it here
4143                    to be consistent with uri_elems case above. */
4144                 if (host) {
4145                         p = host + strlen(host) - 1;
4146                         while (p > host && EVUTIL_ISDIGIT_(*p))
4147                                 --p;
4148                         if (p > host && *p == ':') {
4149                                 len = p - host;
4150                                 req->host_cache = mm_malloc(len + 1);
4151                                 if (!req->host_cache) {
4152                                         event_warn("%s: malloc", __func__);
4153                                         return NULL;
4154                                 }
4155                                 memcpy(req->host_cache, host, len);
4156                                 req->host_cache[len] = '\0';
4157                                 host = req->host_cache;
4158                         }
4159                 }
4160         }
4161
4162         return host;
4163 }
4164
4165 enum evhttp_cmd_type
4166 evhttp_request_get_command(const struct evhttp_request *req) {
4167         return (req->type);
4168 }
4169
4170 int
4171 evhttp_request_get_response_code(const struct evhttp_request *req)
4172 {
4173         return req->response_code;
4174 }
4175
4176 const char *
4177 evhttp_request_get_response_code_line(const struct evhttp_request *req)
4178 {
4179         return req->response_code_line;
4180 }
4181
4182 /** Returns the input headers */
4183 struct evkeyvalq *evhttp_request_get_input_headers(struct evhttp_request *req)
4184 {
4185         return (req->input_headers);
4186 }
4187
4188 /** Returns the output headers */
4189 struct evkeyvalq *evhttp_request_get_output_headers(struct evhttp_request *req)
4190 {
4191         return (req->output_headers);
4192 }
4193
4194 /** Returns the input buffer */
4195 struct evbuffer *evhttp_request_get_input_buffer(struct evhttp_request *req)
4196 {
4197         return (req->input_buffer);
4198 }
4199
4200 /** Returns the output buffer */
4201 struct evbuffer *evhttp_request_get_output_buffer(struct evhttp_request *req)
4202 {
4203         return (req->output_buffer);
4204 }
4205
4206
4207 /*
4208  * Takes a file descriptor to read a request from.
4209  * The callback is executed once the whole request has been read.
4210  */
4211
4212 static struct evhttp_connection*
4213 evhttp_get_request_connection(
4214         struct evhttp* http,
4215         evutil_socket_t fd, struct sockaddr *sa, ev_socklen_t salen)
4216 {
4217         struct evhttp_connection *evcon;
4218         char *hostname = NULL, *portname = NULL;
4219         struct bufferevent* bev = NULL;
4220
4221         name_from_addr(sa, salen, &hostname, &portname);
4222         if (hostname == NULL || portname == NULL) {
4223                 if (hostname) mm_free(hostname);
4224                 if (portname) mm_free(portname);
4225                 return (NULL);
4226         }
4227
4228         event_debug(("%s: new request from %s:%s on "EV_SOCK_FMT"\n",
4229                 __func__, hostname, portname, EV_SOCK_ARG(fd)));
4230
4231         /* we need a connection object to put the http request on */
4232         if (http->bevcb != NULL) {
4233                 bev = (*http->bevcb)(http->base, http->bevcbarg);
4234         }
4235         evcon = evhttp_connection_base_bufferevent_new(
4236                 http->base, NULL, bev, hostname, atoi(portname));
4237         mm_free(hostname);
4238         mm_free(portname);
4239         if (evcon == NULL)
4240                 return (NULL);
4241
4242         evcon->max_headers_size = http->default_max_headers_size;
4243         evcon->max_body_size = http->default_max_body_size;
4244         if (http->flags & EVHTTP_SERVER_LINGERING_CLOSE)
4245                 evcon->flags |= EVHTTP_CON_LINGERING_CLOSE;
4246
4247         evcon->flags |= EVHTTP_CON_INCOMING;
4248         evcon->state = EVCON_READING_FIRSTLINE;
4249
4250         evcon->fd = fd;
4251
4252         if (bufferevent_setfd(evcon->bufev, fd))
4253                 goto err;
4254         if (bufferevent_enable(evcon->bufev, EV_READ))
4255                 goto err;
4256         if (bufferevent_disable(evcon->bufev, EV_WRITE))
4257                 goto err;
4258         bufferevent_socket_set_conn_address_(evcon->bufev, sa, salen);
4259
4260         return (evcon);
4261
4262 err:
4263         evhttp_connection_free(evcon);
4264         return (NULL);
4265 }
4266
4267 static int
4268 evhttp_associate_new_request_with_connection(struct evhttp_connection *evcon)
4269 {
4270         struct evhttp *http = evcon->http_server;
4271         struct evhttp_request *req;
4272         if ((req = evhttp_request_new(evhttp_handle_request, http)) == NULL)
4273                 return (-1);
4274
4275         if ((req->remote_host = mm_strdup(evcon->address)) == NULL) {
4276                 event_warn("%s: strdup", __func__);
4277                 evhttp_request_free(req);
4278                 return (-1);
4279         }
4280         req->remote_port = evcon->port;
4281
4282         req->evcon = evcon;     /* the request ends up owning the connection */
4283         req->flags |= EVHTTP_REQ_OWN_CONNECTION;
4284
4285         /* We did not present the request to the user yet, so treat it
4286          * as if the user was done with the request.  This allows us
4287          * to free the request on a persistent connection if the
4288          * client drops it without sending a request.
4289          */
4290         req->userdone = 1;
4291         req->kind = EVHTTP_REQUEST;
4292
4293         if (http->newreqcb && http->newreqcb(req, http->newreqcbarg) == -1) {
4294                 evhttp_request_free(req);
4295                 return (-1);
4296         }
4297
4298         TAILQ_INSERT_TAIL(&evcon->requests, req, next);
4299
4300         evhttp_start_read_(evcon);
4301
4302         return (0);
4303 }
4304
4305 static void
4306 evhttp_get_request(struct evhttp *http, evutil_socket_t fd,
4307     struct sockaddr *sa, ev_socklen_t salen)
4308 {
4309         struct evhttp_connection *evcon;
4310
4311         evcon = evhttp_get_request_connection(http, fd, sa, salen);
4312         if (evcon == NULL) {
4313                 event_sock_warn(fd, "%s: cannot get connection on "EV_SOCK_FMT,
4314                     __func__, EV_SOCK_ARG(fd));
4315                 evutil_closesocket(fd);
4316                 return;
4317         }
4318
4319         /* the timeout can be used by the server to close idle connections */
4320         if (evutil_timerisset(&http->timeout))
4321                 evhttp_connection_set_timeout_tv(evcon, &http->timeout);
4322
4323         /*
4324          * if we want to accept more than one request on a connection,
4325          * we need to know which http server it belongs to.
4326          */
4327         evcon->http_server = http;
4328         TAILQ_INSERT_TAIL(&http->connections, evcon, next);
4329
4330         if (evhttp_associate_new_request_with_connection(evcon) == -1)
4331                 evhttp_connection_free(evcon);
4332 }
4333
4334
4335 /*
4336  * Network helper functions that we do not want to export to the rest of
4337  * the world.
4338  */
4339
4340 static void
4341 name_from_addr(struct sockaddr *sa, ev_socklen_t salen,
4342     char **phost, char **pport)
4343 {
4344         char ntop[NI_MAXHOST];
4345         char strport[NI_MAXSERV];
4346         int ni_result;
4347
4348 #ifdef EVENT__HAVE_GETNAMEINFO
4349         ni_result = getnameinfo(sa, salen,
4350                 ntop, sizeof(ntop), strport, sizeof(strport),
4351                 NI_NUMERICHOST|NI_NUMERICSERV);
4352
4353         if (ni_result != 0) {
4354 #ifdef EAI_SYSTEM
4355                 /* Windows doesn't have an EAI_SYSTEM. */
4356                 if (ni_result == EAI_SYSTEM)
4357                         event_err(1, "getnameinfo failed");
4358                 else
4359 #endif
4360                         event_errx(1, "getnameinfo failed: %s", gai_strerror(ni_result));
4361                 return;
4362         }
4363 #else
4364         ni_result = fake_getnameinfo(sa, salen,
4365                 ntop, sizeof(ntop), strport, sizeof(strport),
4366                 NI_NUMERICHOST|NI_NUMERICSERV);
4367         if (ni_result != 0)
4368                         return;
4369 #endif
4370
4371         *phost = mm_strdup(ntop);
4372         *pport = mm_strdup(strport);
4373 }
4374
4375 /* Create a non-blocking socket and bind it */
4376 /* todo: rename this function */
4377 static evutil_socket_t
4378 bind_socket_ai(struct evutil_addrinfo *ai, int reuse)
4379 {
4380         evutil_socket_t fd;
4381
4382         int on = 1, r;
4383         int serrno;
4384
4385         /* Create listen socket */
4386         fd = evutil_socket_(ai ? ai->ai_family : AF_INET,
4387             SOCK_STREAM|EVUTIL_SOCK_NONBLOCK|EVUTIL_SOCK_CLOEXEC, 0);
4388         if (fd == -1) {
4389                         event_sock_warn(-1, "socket");
4390                         return (-1);
4391         }
4392
4393         if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (void *)&on, sizeof(on))<0)
4394                 goto out;
4395         if (reuse) {
4396                 if (evutil_make_listen_socket_reuseable(fd) < 0)
4397                         goto out;
4398         }
4399
4400         if (ai != NULL) {
4401                 r = bind(fd, ai->ai_addr, (ev_socklen_t)ai->ai_addrlen);
4402                 if (r == -1)
4403                         goto out;
4404         }
4405
4406         return (fd);
4407
4408  out:
4409         serrno = EVUTIL_SOCKET_ERROR();
4410         evutil_closesocket(fd);
4411         EVUTIL_SET_SOCKET_ERROR(serrno);
4412         return (-1);
4413 }
4414
4415 static struct evutil_addrinfo *
4416 make_addrinfo(const char *address, ev_uint16_t port)
4417 {
4418         struct evutil_addrinfo *ai = NULL;
4419
4420         struct evutil_addrinfo hints;
4421         char strport[NI_MAXSERV];
4422         int ai_result;
4423
4424         memset(&hints, 0, sizeof(hints));
4425         hints.ai_family = AF_UNSPEC;
4426         hints.ai_socktype = SOCK_STREAM;
4427         /* turn NULL hostname into INADDR_ANY, and skip looking up any address
4428          * types we don't have an interface to connect to. */
4429         hints.ai_flags = EVUTIL_AI_PASSIVE|EVUTIL_AI_ADDRCONFIG;
4430         evutil_snprintf(strport, sizeof(strport), "%d", port);
4431         if ((ai_result = evutil_getaddrinfo(address, strport, &hints, &ai))
4432             != 0) {
4433                 if (ai_result == EVUTIL_EAI_SYSTEM)
4434                         event_warn("getaddrinfo");
4435                 else
4436                         event_warnx("getaddrinfo: %s",
4437                             evutil_gai_strerror(ai_result));
4438                 return (NULL);
4439         }
4440
4441         return (ai);
4442 }
4443
4444 static evutil_socket_t
4445 bind_socket(const char *address, ev_uint16_t port, int reuse)
4446 {
4447         evutil_socket_t fd;
4448         struct evutil_addrinfo *aitop = NULL;
4449
4450         /* just create an unbound socket */
4451         if (address == NULL && port == 0)
4452                 return bind_socket_ai(NULL, 0);
4453
4454         aitop = make_addrinfo(address, port);
4455
4456         if (aitop == NULL)
4457                 return (-1);
4458
4459         fd = bind_socket_ai(aitop, reuse);
4460
4461         evutil_freeaddrinfo(aitop);
4462
4463         return (fd);
4464 }
4465
4466 struct evhttp_uri {
4467         unsigned flags;
4468         char *scheme; /* scheme; e.g http, ftp etc */
4469         char *userinfo; /* userinfo (typically username:pass), or NULL */
4470         char *host; /* hostname, IP address, or NULL */
4471         int port; /* port, or zero */
4472         char *path; /* path, or "". */
4473         char *query; /* query, or NULL */
4474         char *fragment; /* fragment or NULL */
4475 };
4476
4477 struct evhttp_uri *
4478 evhttp_uri_new(void)
4479 {
4480         struct evhttp_uri *uri = mm_calloc(sizeof(struct evhttp_uri), 1);
4481         if (uri)
4482                 uri->port = -1;
4483         return uri;
4484 }
4485
4486 void
4487 evhttp_uri_set_flags(struct evhttp_uri *uri, unsigned flags)
4488 {
4489         uri->flags = flags;
4490 }
4491
4492 /* Return true if the string starting at s and ending immediately before eos
4493  * is a valid URI scheme according to RFC3986
4494  */
4495 static int
4496 scheme_ok(const char *s, const char *eos)
4497 {
4498         /* scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) */
4499         EVUTIL_ASSERT(eos >= s);
4500         if (s == eos)
4501                 return 0;
4502         if (!EVUTIL_ISALPHA_(*s))
4503                 return 0;
4504         while (++s < eos) {
4505                 if (! EVUTIL_ISALNUM_(*s) &&
4506                     *s != '+' && *s != '-' && *s != '.')
4507                         return 0;
4508         }
4509         return 1;
4510 }
4511
4512 #define SUBDELIMS "!$&'()*+,;="
4513
4514 /* Return true iff [s..eos) is a valid userinfo */
4515 static int
4516 userinfo_ok(const char *s, const char *eos)
4517 {
4518         while (s < eos) {
4519                 if (CHAR_IS_UNRESERVED(*s) ||
4520                     strchr(SUBDELIMS, *s) ||
4521                     *s == ':')
4522                         ++s;
4523                 else if (*s == '%' && s+2 < eos &&
4524                     EVUTIL_ISXDIGIT_(s[1]) &&
4525                     EVUTIL_ISXDIGIT_(s[2]))
4526                         s += 3;
4527                 else
4528                         return 0;
4529         }
4530         return 1;
4531 }
4532
4533 static int
4534 regname_ok(const char *s, const char *eos)
4535 {
4536         while (s && s<eos) {
4537                 if (CHAR_IS_UNRESERVED(*s) ||
4538                     strchr(SUBDELIMS, *s))
4539                         ++s;
4540                 else if (*s == '%' &&
4541                     EVUTIL_ISXDIGIT_(s[1]) &&
4542                     EVUTIL_ISXDIGIT_(s[2]))
4543                         s += 3;
4544                 else
4545                         return 0;
4546         }
4547         return 1;
4548 }
4549
4550 static int
4551 parse_port(const char *s, const char *eos)
4552 {
4553         int portnum = 0;
4554         while (s < eos) {
4555                 if (! EVUTIL_ISDIGIT_(*s))
4556                         return -1;
4557                 portnum = (portnum * 10) + (*s - '0');
4558                 if (portnum < 0)
4559                         return -1;
4560                 if (portnum > 65535)
4561                         return -1;
4562                 ++s;
4563         }
4564         return portnum;
4565 }
4566
4567 /* returns 0 for bad, 1 for ipv6, 2 for IPvFuture */
4568 static int
4569 bracket_addr_ok(const char *s, const char *eos)
4570 {
4571         if (s + 3 > eos || *s != '[' || *(eos-1) != ']')
4572                 return 0;
4573         if (s[1] == 'v') {
4574                 /* IPvFuture, or junk.
4575                    "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" )
4576                  */
4577                 s += 2; /* skip [v */
4578                 --eos;
4579                 if (!EVUTIL_ISXDIGIT_(*s)) /*require at least one*/
4580                         return 0;
4581                 while (s < eos && *s != '.') {
4582                         if (EVUTIL_ISXDIGIT_(*s))
4583                                 ++s;
4584                         else
4585                                 return 0;
4586                 }
4587                 if (*s != '.')
4588                         return 0;
4589                 ++s;
4590                 while (s < eos) {
4591                         if (CHAR_IS_UNRESERVED(*s) ||
4592                             strchr(SUBDELIMS, *s) ||
4593                             *s == ':')
4594                                 ++s;
4595                         else
4596                                 return 0;
4597                 }
4598                 return 2;
4599         } else {
4600                 /* IPv6, or junk */
4601                 char buf[64];
4602                 ev_ssize_t n_chars = eos-s-2;
4603                 struct in6_addr in6;
4604                 if (n_chars >= 64) /* way too long */
4605                         return 0;
4606                 memcpy(buf, s+1, n_chars);
4607                 buf[n_chars]='\0';
4608                 return (evutil_inet_pton(AF_INET6,buf,&in6)==1) ? 1 : 0;
4609         }
4610 }
4611
4612 static int
4613 parse_authority(struct evhttp_uri *uri, char *s, char *eos)
4614 {
4615         char *cp, *port;
4616         EVUTIL_ASSERT(eos);
4617         if (eos == s) {
4618                 uri->host = mm_strdup("");
4619                 if (uri->host == NULL) {
4620                         event_warn("%s: strdup", __func__);
4621                         return -1;
4622                 }
4623                 return 0;
4624         }
4625
4626         /* Optionally, we start with "userinfo@" */
4627
4628         cp = strchr(s, '@');
4629         if (cp && cp < eos) {
4630                 if (! userinfo_ok(s,cp))
4631                         return -1;
4632                 *cp++ = '\0';
4633                 uri->userinfo = mm_strdup(s);
4634                 if (uri->userinfo == NULL) {
4635                         event_warn("%s: strdup", __func__);
4636                         return -1;
4637                 }
4638         } else {
4639                 cp = s;
4640         }
4641         /* Optionally, we end with ":port" */
4642         for (port=eos-1; port >= cp && EVUTIL_ISDIGIT_(*port); --port)
4643                 ;
4644         if (port >= cp && *port == ':') {
4645                 if (port+1 == eos) /* Leave port unspecified; the RFC allows a
4646                                     * nil port */
4647                         uri->port = -1;
4648                 else if ((uri->port = parse_port(port+1, eos))<0)
4649                         return -1;
4650                 eos = port;
4651         }
4652         /* Now, cp..eos holds the "host" port, which can be an IPv4Address,
4653          * an IP-Literal, or a reg-name */
4654         EVUTIL_ASSERT(eos >= cp);
4655         if (*cp == '[' && eos >= cp+2 && *(eos-1) == ']') {
4656                 /* IPv6address, IP-Literal, or junk. */
4657                 if (! bracket_addr_ok(cp, eos))
4658                         return -1;
4659         } else {
4660                 /* Make sure the host part is ok. */
4661                 if (! regname_ok(cp,eos)) /* Match IPv4Address or reg-name */
4662                         return -1;
4663         }
4664         uri->host = mm_malloc(eos-cp+1);
4665         if (uri->host == NULL) {
4666                 event_warn("%s: malloc", __func__);
4667                 return -1;
4668         }
4669         memcpy(uri->host, cp, eos-cp);
4670         uri->host[eos-cp] = '\0';
4671         return 0;
4672
4673 }
4674
4675 static char *
4676 end_of_authority(char *cp)
4677 {
4678         while (*cp) {
4679                 if (*cp == '?' || *cp == '#' || *cp == '/')
4680                         return cp;
4681                 ++cp;
4682         }
4683         return cp;
4684 }
4685
4686 enum uri_part {
4687         PART_PATH,
4688         PART_QUERY,
4689         PART_FRAGMENT
4690 };
4691
4692 /* Return the character after the longest prefix of 'cp' that matches...
4693  *   *pchar / "/" if allow_qchars is false, or
4694  *   *(pchar / "/" / "?") if allow_qchars is true.
4695  */
4696 static char *
4697 end_of_path(char *cp, enum uri_part part, unsigned flags)
4698 {
4699         if (flags & EVHTTP_URI_NONCONFORMANT) {
4700                 /* If NONCONFORMANT:
4701                  *   Path is everything up to a # or ? or nul.
4702                  *   Query is everything up a # or nul
4703                  *   Fragment is everything up to a nul.
4704                  */
4705                 switch (part) {
4706                 case PART_PATH:
4707                         while (*cp && *cp != '#' && *cp != '?')
4708                                 ++cp;
4709                         break;
4710                 case PART_QUERY:
4711                         while (*cp && *cp != '#')
4712                                 ++cp;
4713                         break;
4714                 case PART_FRAGMENT:
4715                         cp += strlen(cp);
4716                         break;
4717                 };
4718                 return cp;
4719         }
4720
4721         while (*cp) {
4722                 if (CHAR_IS_UNRESERVED(*cp) ||
4723                     strchr(SUBDELIMS, *cp) ||
4724                     *cp == ':' || *cp == '@' || *cp == '/')
4725                         ++cp;
4726                 else if (*cp == '%' && EVUTIL_ISXDIGIT_(cp[1]) &&
4727                     EVUTIL_ISXDIGIT_(cp[2]))
4728                         cp += 3;
4729                 else if (*cp == '?' && part != PART_PATH)
4730                         ++cp;
4731                 else
4732                         return cp;
4733         }
4734         return cp;
4735 }
4736
4737 static int
4738 path_matches_noscheme(const char *cp)
4739 {
4740         while (*cp) {
4741                 if (*cp == ':')
4742                         return 0;
4743                 else if (*cp == '/')
4744                         return 1;
4745                 ++cp;
4746         }
4747         return 1;
4748 }
4749
4750 struct evhttp_uri *
4751 evhttp_uri_parse(const char *source_uri)
4752 {
4753         return evhttp_uri_parse_with_flags(source_uri, 0);
4754 }
4755
4756 struct evhttp_uri *
4757 evhttp_uri_parse_with_flags(const char *source_uri, unsigned flags)
4758 {
4759         char *readbuf = NULL, *readp = NULL, *token = NULL, *query = NULL;
4760         char *path = NULL, *fragment = NULL;
4761         int got_authority = 0;
4762
4763         struct evhttp_uri *uri = mm_calloc(1, sizeof(struct evhttp_uri));
4764         if (uri == NULL) {
4765                 event_warn("%s: calloc", __func__);
4766                 goto err;
4767         }
4768         uri->port = -1;
4769         uri->flags = flags;
4770
4771         readbuf = mm_strdup(source_uri);
4772         if (readbuf == NULL) {
4773                 event_warn("%s: strdup", __func__);
4774                 goto err;
4775         }
4776
4777         readp = readbuf;
4778         token = NULL;
4779
4780         /* We try to follow RFC3986 here as much as we can, and match
4781            the productions
4782
4783               URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
4784
4785               relative-ref  = relative-part [ "?" query ] [ "#" fragment ]
4786          */
4787
4788         /* 1. scheme: */
4789         token = strchr(readp, ':');
4790         if (token && scheme_ok(readp,token)) {
4791                 *token = '\0';
4792                 uri->scheme = mm_strdup(readp);
4793                 if (uri->scheme == NULL) {
4794                         event_warn("%s: strdup", __func__);
4795                         goto err;
4796                 }
4797                 readp = token+1; /* eat : */
4798         }
4799
4800         /* 2. Optionally, "//" then an 'authority' part. */
4801         if (readp[0]=='/' && readp[1] == '/') {
4802                 char *authority;
4803                 readp += 2;
4804                 authority = readp;
4805                 path = end_of_authority(readp);
4806                 if (parse_authority(uri, authority, path) < 0)
4807                         goto err;
4808                 readp = path;
4809                 got_authority = 1;
4810         }
4811
4812         /* 3. Query: path-abempty, path-absolute, path-rootless, or path-empty
4813          */
4814         path = readp;
4815         readp = end_of_path(path, PART_PATH, flags);
4816
4817         /* Query */
4818         if (*readp == '?') {
4819                 *readp = '\0';
4820                 ++readp;
4821                 query = readp;
4822                 readp = end_of_path(readp, PART_QUERY, flags);
4823         }
4824         /* fragment */
4825         if (*readp == '#') {
4826                 *readp = '\0';
4827                 ++readp;
4828                 fragment = readp;
4829                 readp = end_of_path(readp, PART_FRAGMENT, flags);
4830         }
4831         if (*readp != '\0') {
4832                 goto err;
4833         }
4834
4835         /* These next two cases may be unreachable; I'm leaving them
4836          * in to be defensive. */
4837         /* If you didn't get an authority, the path can't begin with "//" */
4838         if (!got_authority && path[0]=='/' && path[1]=='/')
4839                 goto err;
4840         /* If you did get an authority, the path must begin with "/" or be
4841          * empty. */
4842         if (got_authority && path[0] != '/' && path[0] != '\0')
4843                 goto err;
4844         /* (End of maybe-unreachable cases) */
4845
4846         /* If there was no scheme, the first part of the path (if any) must
4847          * have no colon in it. */
4848         if (! uri->scheme && !path_matches_noscheme(path))
4849                 goto err;
4850
4851         EVUTIL_ASSERT(path);
4852         uri->path = mm_strdup(path);
4853         if (uri->path == NULL) {
4854                 event_warn("%s: strdup", __func__);
4855                 goto err;
4856         }
4857
4858         if (query) {
4859                 uri->query = mm_strdup(query);
4860                 if (uri->query == NULL) {
4861                         event_warn("%s: strdup", __func__);
4862                         goto err;
4863                 }
4864         }
4865         if (fragment) {
4866                 uri->fragment = mm_strdup(fragment);
4867                 if (uri->fragment == NULL) {
4868                         event_warn("%s: strdup", __func__);
4869                         goto err;
4870                 }
4871         }
4872
4873         mm_free(readbuf);
4874
4875         return uri;
4876 err:
4877         if (uri)
4878                 evhttp_uri_free(uri);
4879         if (readbuf)
4880                 mm_free(readbuf);
4881         return NULL;
4882 }
4883
4884 static struct evhttp_uri *
4885 evhttp_uri_parse_authority(char *source_uri)
4886 {
4887         struct evhttp_uri *uri = mm_calloc(1, sizeof(struct evhttp_uri));
4888         char *end;
4889
4890         if (uri == NULL) {
4891                 event_warn("%s: calloc", __func__);
4892                 goto err;
4893         }
4894         uri->port = -1;
4895         uri->flags = 0;
4896
4897         end = end_of_authority(source_uri);
4898         if (parse_authority(uri, source_uri, end) < 0)
4899                 goto err;
4900
4901         uri->path = mm_strdup("");
4902         if (uri->path == NULL) {
4903                 event_warn("%s: strdup", __func__);
4904                 goto err;
4905         }
4906
4907         return uri;
4908 err:
4909         if (uri)
4910                 evhttp_uri_free(uri);
4911         return NULL;
4912 }
4913
4914 void
4915 evhttp_uri_free(struct evhttp_uri *uri)
4916 {
4917 #define URI_FREE_STR_(f)                \
4918         if (uri->f) {                   \
4919                 mm_free(uri->f);                \
4920         }
4921
4922         URI_FREE_STR_(scheme);
4923         URI_FREE_STR_(userinfo);
4924         URI_FREE_STR_(host);
4925         URI_FREE_STR_(path);
4926         URI_FREE_STR_(query);
4927         URI_FREE_STR_(fragment);
4928
4929         mm_free(uri);
4930 #undef URI_FREE_STR_
4931 }
4932
4933 char *
4934 evhttp_uri_join(struct evhttp_uri *uri, char *buf, size_t limit)
4935 {
4936         struct evbuffer *tmp = 0;
4937         size_t joined_size = 0;
4938         char *output = NULL;
4939
4940 #define URI_ADD_(f)     evbuffer_add(tmp, uri->f, strlen(uri->f))
4941
4942         if (!uri || !buf || !limit)
4943                 return NULL;
4944
4945         tmp = evbuffer_new();
4946         if (!tmp)
4947                 return NULL;
4948
4949         if (uri->scheme) {
4950                 URI_ADD_(scheme);
4951                 evbuffer_add(tmp, ":", 1);
4952         }
4953         if (uri->host) {
4954                 evbuffer_add(tmp, "//", 2);
4955                 if (uri->userinfo)
4956                         evbuffer_add_printf(tmp,"%s@", uri->userinfo);
4957                 URI_ADD_(host);
4958                 if (uri->port >= 0)
4959                         evbuffer_add_printf(tmp,":%d", uri->port);
4960
4961                 if (uri->path && uri->path[0] != '/' && uri->path[0] != '\0')
4962                         goto err;
4963         }
4964
4965         if (uri->path)
4966                 URI_ADD_(path);
4967
4968         if (uri->query) {
4969                 evbuffer_add(tmp, "?", 1);
4970                 URI_ADD_(query);
4971         }
4972
4973         if (uri->fragment) {
4974                 evbuffer_add(tmp, "#", 1);
4975                 URI_ADD_(fragment);
4976         }
4977
4978         evbuffer_add(tmp, "\0", 1); /* NUL */
4979
4980         joined_size = evbuffer_get_length(tmp);
4981
4982         if (joined_size > limit) {
4983                 /* It doesn't fit. */
4984                 evbuffer_free(tmp);
4985                 return NULL;
4986         }
4987         evbuffer_remove(tmp, buf, joined_size);
4988
4989         output = buf;
4990 err:
4991         evbuffer_free(tmp);
4992
4993         return output;
4994 #undef URI_ADD_
4995 }
4996
4997 const char *
4998 evhttp_uri_get_scheme(const struct evhttp_uri *uri)
4999 {
5000         return uri->scheme;
5001 }
5002 const char *
5003 evhttp_uri_get_userinfo(const struct evhttp_uri *uri)
5004 {
5005         return uri->userinfo;
5006 }
5007 const char *
5008 evhttp_uri_get_host(const struct evhttp_uri *uri)
5009 {
5010         return uri->host;
5011 }
5012 int
5013 evhttp_uri_get_port(const struct evhttp_uri *uri)
5014 {
5015         return uri->port;
5016 }
5017 const char *
5018 evhttp_uri_get_path(const struct evhttp_uri *uri)
5019 {
5020         return uri->path;
5021 }
5022 const char *
5023 evhttp_uri_get_query(const struct evhttp_uri *uri)
5024 {
5025         return uri->query;
5026 }
5027 const char *
5028 evhttp_uri_get_fragment(const struct evhttp_uri *uri)
5029 {
5030         return uri->fragment;
5031 }
5032
5033 #define URI_SET_STR_(f) do {                                    \
5034         if (uri->f)                                             \
5035                 mm_free(uri->f);                                \
5036         if (f) {                                                \
5037                 if ((uri->f = mm_strdup(f)) == NULL) {          \
5038                         event_warn("%s: strdup()", __func__);   \
5039                         return -1;                              \
5040                 }                                               \
5041         } else {                                                \
5042                 uri->f = NULL;                                  \
5043         }                                                       \
5044         } while(0)
5045
5046 int
5047 evhttp_uri_set_scheme(struct evhttp_uri *uri, const char *scheme)
5048 {
5049         if (scheme && !scheme_ok(scheme, scheme+strlen(scheme)))
5050                 return -1;
5051
5052         URI_SET_STR_(scheme);
5053         return 0;
5054 }
5055 int
5056 evhttp_uri_set_userinfo(struct evhttp_uri *uri, const char *userinfo)
5057 {
5058         if (userinfo && !userinfo_ok(userinfo, userinfo+strlen(userinfo)))
5059                 return -1;
5060         URI_SET_STR_(userinfo);
5061         return 0;
5062 }
5063 int
5064 evhttp_uri_set_host(struct evhttp_uri *uri, const char *host)
5065 {
5066         if (host) {
5067                 if (host[0] == '[') {
5068                         if (! bracket_addr_ok(host, host+strlen(host)))
5069                                 return -1;
5070                 } else {
5071                         if (! regname_ok(host, host+strlen(host)))
5072                                 return -1;
5073                 }
5074         }
5075
5076         URI_SET_STR_(host);
5077         return 0;
5078 }
5079 int
5080 evhttp_uri_set_port(struct evhttp_uri *uri, int port)
5081 {
5082         if (port < -1)
5083                 return -1;
5084         uri->port = port;
5085         return 0;
5086 }
5087 #define end_of_cpath(cp,p,f) \
5088         ((const char*)(end_of_path(((char*)(cp)), (p), (f))))
5089
5090 int
5091 evhttp_uri_set_path(struct evhttp_uri *uri, const char *path)
5092 {
5093         if (path && end_of_cpath(path, PART_PATH, uri->flags) != path+strlen(path))
5094                 return -1;
5095
5096         URI_SET_STR_(path);
5097         return 0;
5098 }
5099 int
5100 evhttp_uri_set_query(struct evhttp_uri *uri, const char *query)
5101 {
5102         if (query && end_of_cpath(query, PART_QUERY, uri->flags) != query+strlen(query))
5103                 return -1;
5104         URI_SET_STR_(query);
5105         return 0;
5106 }
5107 int
5108 evhttp_uri_set_fragment(struct evhttp_uri *uri, const char *fragment)
5109 {
5110         if (fragment && end_of_cpath(fragment, PART_FRAGMENT, uri->flags) != fragment+strlen(fragment))
5111                 return -1;
5112         URI_SET_STR_(fragment);
5113         return 0;
5114 }