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