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