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