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