xref: /freebsd/contrib/ntp/sntp/libevent/http.c (revision 5b9c547c)
1 /*
2  * Copyright (c) 2002-2007 Niels Provos <provos@citi.umich.edu>
3  * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include "event2/event-config.h"
29 #include "evconfig-private.h"
30 
31 #ifdef EVENT__HAVE_SYS_PARAM_H
32 #include <sys/param.h>
33 #endif
34 #ifdef EVENT__HAVE_SYS_TYPES_H
35 #include <sys/types.h>
36 #endif
37 
38 #ifdef 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 	/* Callback can shut down connection with negative return value */
2154 	if (req->header_cb != NULL) {
2155 		if ((*req->header_cb)(req, req->cb_arg) < 0) {
2156 			evhttp_connection_fail_(evcon, EVREQ_HTTP_EOF);
2157 			return;
2158 		}
2159 	}
2160 
2161 	/* Done reading headers, do the real work */
2162 	switch (req->kind) {
2163 	case EVHTTP_REQUEST:
2164 		event_debug(("%s: checking for post data on "EV_SOCK_FMT"\n",
2165 			__func__, EV_SOCK_ARG(fd)));
2166 		evhttp_get_body(evcon, req);
2167 		/* note the request may have been freed in evhttp_get_body */
2168 		break;
2169 
2170 	case EVHTTP_RESPONSE:
2171 		/* Start over if we got a 100 Continue response. */
2172 		if (req->response_code == 100) {
2173 			evhttp_start_read_(evcon);
2174 			return;
2175 		}
2176 		if (!evhttp_response_needs_body(req)) {
2177 			event_debug(("%s: skipping body for code %d\n",
2178 					__func__, req->response_code));
2179 			evhttp_connection_done(evcon);
2180 		} else {
2181 			event_debug(("%s: start of read body for %s on "
2182 				EV_SOCK_FMT"\n",
2183 				__func__, req->remote_host, EV_SOCK_ARG(fd)));
2184 			evhttp_get_body(evcon, req);
2185 			/* note the request may have been freed in
2186 			 * evhttp_get_body */
2187 		}
2188 		break;
2189 
2190 	default:
2191 		event_warnx("%s: bad header on "EV_SOCK_FMT, __func__,
2192 		    EV_SOCK_ARG(fd));
2193 		evhttp_connection_fail_(evcon, EVREQ_HTTP_INVALID_HEADER);
2194 		break;
2195 	}
2196 	/* request may have been freed above */
2197 }
2198 
2199 /*
2200  * Creates a TCP connection to the specified port and executes a callback
2201  * when finished.  Failure or success is indicate by the passed connection
2202  * object.
2203  *
2204  * Although this interface accepts a hostname, it is intended to take
2205  * only numeric hostnames so that non-blocking DNS resolution can
2206  * happen elsewhere.
2207  */
2208 
2209 struct evhttp_connection *
2210 evhttp_connection_new(const char *address, unsigned short port)
2211 {
2212 	return (evhttp_connection_base_new(NULL, NULL, address, port));
2213 }
2214 
2215 struct evhttp_connection *
2216 evhttp_connection_base_bufferevent_new(struct event_base *base, struct evdns_base *dnsbase, struct bufferevent* bev,
2217     const char *address, unsigned short port)
2218 {
2219 	struct evhttp_connection *evcon = NULL;
2220 
2221 	event_debug(("Attempting connection to %s:%d\n", address, port));
2222 
2223 	if ((evcon = mm_calloc(1, sizeof(struct evhttp_connection))) == NULL) {
2224 		event_warn("%s: calloc failed", __func__);
2225 		goto error;
2226 	}
2227 
2228 	evcon->fd = -1;
2229 	evcon->port = port;
2230 
2231 	evcon->max_headers_size = EV_SIZE_MAX;
2232 	evcon->max_body_size = EV_SIZE_MAX;
2233 
2234 	evutil_timerclear(&evcon->timeout);
2235 	evcon->retry_cnt = evcon->retry_max = 0;
2236 
2237 	if ((evcon->address = mm_strdup(address)) == NULL) {
2238 		event_warn("%s: strdup failed", __func__);
2239 		goto error;
2240 	}
2241 
2242 	if (bev == NULL) {
2243 		if (!(bev = bufferevent_socket_new(base, -1, 0))) {
2244 			event_warn("%s: bufferevent_socket_new failed", __func__);
2245 			goto error;
2246 		}
2247 	}
2248 
2249 	bufferevent_setcb(bev, evhttp_read_cb, evhttp_write_cb, evhttp_error_cb, evcon);
2250 	evcon->bufev = bev;
2251 
2252 	evcon->state = EVCON_DISCONNECTED;
2253 	TAILQ_INIT(&evcon->requests);
2254 
2255 	evcon->initial_retry_timeout.tv_sec = 2;
2256 	evcon->initial_retry_timeout.tv_usec = 0;
2257 
2258 	if (base != NULL) {
2259 		evcon->base = base;
2260 		if (bufferevent_get_base(bev) != base)
2261 			bufferevent_base_set(base, evcon->bufev);
2262 	}
2263 
2264 	event_deferred_cb_init_(
2265 	    &evcon->read_more_deferred_cb,
2266 	    bufferevent_get_priority(bev),
2267 	    evhttp_deferred_read_cb, evcon);
2268 
2269 	evcon->dns_base = dnsbase;
2270 
2271 	return (evcon);
2272 
2273  error:
2274 	if (evcon != NULL)
2275 		evhttp_connection_free(evcon);
2276 	return (NULL);
2277 }
2278 
2279 struct bufferevent* evhttp_connection_get_bufferevent(struct evhttp_connection *evcon)
2280 {
2281 	return evcon->bufev;
2282 }
2283 
2284 struct evhttp *
2285 evhttp_connection_get_server(struct evhttp_connection *evcon)
2286 {
2287 	return evcon->http_server;
2288 }
2289 
2290 struct evhttp_connection *
2291 evhttp_connection_base_new(struct event_base *base, struct evdns_base *dnsbase,
2292     const char *address, unsigned short port)
2293 {
2294 	return evhttp_connection_base_bufferevent_new(base, dnsbase, NULL, address, port);
2295 }
2296 
2297 void
2298 evhttp_connection_set_base(struct evhttp_connection *evcon,
2299     struct event_base *base)
2300 {
2301 	EVUTIL_ASSERT(evcon->base == NULL);
2302 	EVUTIL_ASSERT(evcon->state == EVCON_DISCONNECTED);
2303 	evcon->base = base;
2304 	bufferevent_base_set(base, evcon->bufev);
2305 }
2306 
2307 void
2308 evhttp_connection_set_timeout(struct evhttp_connection *evcon,
2309     int timeout_in_secs)
2310 {
2311 	if (timeout_in_secs == -1)
2312 		evhttp_connection_set_timeout_tv(evcon, NULL);
2313 	else {
2314 		struct timeval tv;
2315 		tv.tv_sec = timeout_in_secs;
2316 		tv.tv_usec = 0;
2317 		evhttp_connection_set_timeout_tv(evcon, &tv);
2318 	}
2319 }
2320 
2321 void
2322 evhttp_connection_set_timeout_tv(struct evhttp_connection *evcon,
2323     const struct timeval* tv)
2324 {
2325 	if (tv) {
2326 		evcon->timeout = *tv;
2327 		bufferevent_set_timeouts(evcon->bufev, &evcon->timeout, &evcon->timeout);
2328 	} else {
2329 		const struct timeval read_tv = { HTTP_READ_TIMEOUT, 0 };
2330 		const struct timeval write_tv = { HTTP_WRITE_TIMEOUT, 0 };
2331 		evutil_timerclear(&evcon->timeout);
2332 		bufferevent_set_timeouts(evcon->bufev, &read_tv, &write_tv);
2333 	}
2334 }
2335 
2336 void
2337 evhttp_connection_set_initial_retry_tv(struct evhttp_connection *evcon,
2338     const struct timeval *tv)
2339 {
2340 	if (tv) {
2341 		evcon->initial_retry_timeout = *tv;
2342 	} else {
2343 		evutil_timerclear(&evcon->initial_retry_timeout);
2344 		evcon->initial_retry_timeout.tv_sec = 2;
2345 	}
2346 }
2347 
2348 void
2349 evhttp_connection_set_retries(struct evhttp_connection *evcon,
2350     int retry_max)
2351 {
2352 	evcon->retry_max = retry_max;
2353 }
2354 
2355 void
2356 evhttp_connection_set_closecb(struct evhttp_connection *evcon,
2357     void (*cb)(struct evhttp_connection *, void *), void *cbarg)
2358 {
2359 	evcon->closecb = cb;
2360 	evcon->closecb_arg = cbarg;
2361 }
2362 
2363 void
2364 evhttp_connection_get_peer(struct evhttp_connection *evcon,
2365     char **address, ev_uint16_t *port)
2366 {
2367 	*address = evcon->address;
2368 	*port = evcon->port;
2369 }
2370 
2371 const struct sockaddr*
2372 evhttp_connection_get_addr(struct evhttp_connection *evcon)
2373 {
2374 	return (struct sockaddr *)evcon->conn_address;
2375 }
2376 
2377 int
2378 evhttp_connection_connect_(struct evhttp_connection *evcon)
2379 {
2380 	int old_state = evcon->state;
2381 
2382 	if (evcon->state == EVCON_CONNECTING)
2383 		return (0);
2384 
2385 	evhttp_connection_reset_(evcon);
2386 
2387 	EVUTIL_ASSERT(!(evcon->flags & EVHTTP_CON_INCOMING));
2388 	evcon->flags |= EVHTTP_CON_OUTGOING;
2389 
2390 	if (evcon->bind_address || evcon->bind_port) {
2391 		evcon->fd = bind_socket(
2392 			evcon->bind_address, evcon->bind_port, 0 /*reuse*/);
2393 		if (evcon->fd == -1) {
2394 			event_debug(("%s: failed to bind to \"%s\"",
2395 				__func__, evcon->bind_address));
2396 			return (-1);
2397 		}
2398 
2399 		bufferevent_setfd(evcon->bufev, evcon->fd);
2400 	} else {
2401 		bufferevent_setfd(evcon->bufev, -1);
2402 	}
2403 
2404 	/* Set up a callback for successful connection setup */
2405 	bufferevent_setcb(evcon->bufev,
2406 	    NULL /* evhttp_read_cb */,
2407 	    NULL /* evhttp_write_cb */,
2408 	    evhttp_connection_cb,
2409 	    evcon);
2410 	if (!evutil_timerisset(&evcon->timeout)) {
2411 		const struct timeval conn_tv = { HTTP_CONNECT_TIMEOUT, 0 };
2412 		bufferevent_set_timeouts(evcon->bufev, NULL, &conn_tv);
2413 	} else {
2414 		bufferevent_set_timeouts(evcon->bufev, NULL, &evcon->timeout);
2415 	}
2416 	/* make sure that we get a write callback */
2417 	bufferevent_enable(evcon->bufev, EV_WRITE);
2418 
2419 	evcon->state = EVCON_CONNECTING;
2420 
2421 	if (bufferevent_socket_connect_hostname(evcon->bufev, evcon->dns_base,
2422 		AF_UNSPEC, evcon->address, evcon->port) < 0) {
2423 		evcon->state = old_state;
2424 		event_sock_warn(evcon->fd, "%s: connection to \"%s\" failed",
2425 		    __func__, evcon->address);
2426 		/* some operating systems return ECONNREFUSED immediately
2427 		 * when connecting to a local address.  the cleanup is going
2428 		 * to reschedule this function call.
2429 		 */
2430 		evhttp_connection_cb_cleanup(evcon);
2431 		return (0);
2432 	}
2433 
2434 	return (0);
2435 }
2436 
2437 /*
2438  * Starts an HTTP request on the provided evhttp_connection object.
2439  * If the connection object is not connected to the web server already,
2440  * this will start the connection.
2441  */
2442 
2443 int
2444 evhttp_make_request(struct evhttp_connection *evcon,
2445     struct evhttp_request *req,
2446     enum evhttp_cmd_type type, const char *uri)
2447 {
2448 	/* We are making a request */
2449 	req->kind = EVHTTP_REQUEST;
2450 	req->type = type;
2451 	if (req->uri != NULL)
2452 		mm_free(req->uri);
2453 	if ((req->uri = mm_strdup(uri)) == NULL) {
2454 		event_warn("%s: strdup", __func__);
2455 		evhttp_request_free(req);
2456 		return (-1);
2457 	}
2458 
2459 	/* Set the protocol version if it is not supplied */
2460 	if (!req->major && !req->minor) {
2461 		req->major = 1;
2462 		req->minor = 1;
2463 	}
2464 
2465 	EVUTIL_ASSERT(req->evcon == NULL);
2466 	req->evcon = evcon;
2467 	EVUTIL_ASSERT(!(req->flags & EVHTTP_REQ_OWN_CONNECTION));
2468 
2469 	TAILQ_INSERT_TAIL(&evcon->requests, req, next);
2470 
2471 	/* If the connection object is not connected; make it so */
2472 	if (!evhttp_connected(evcon)) {
2473 		int res = evhttp_connection_connect_(evcon);
2474 		/* evhttp_connection_fail_(), which is called through
2475 		 * evhttp_connection_connect_(), assumes that req lies in
2476 		 * evcon->requests.  Thus, enqueue the request in advance and
2477 		 * remove it in the error case. */
2478 		if (res != 0)
2479 			TAILQ_REMOVE(&evcon->requests, req, next);
2480 
2481 		return res;
2482 	}
2483 
2484 	/*
2485 	 * If it's connected already and we are the first in the queue,
2486 	 * then we can dispatch this request immediately.  Otherwise, it
2487 	 * will be dispatched once the pending requests are completed.
2488 	 */
2489 	if (TAILQ_FIRST(&evcon->requests) == req)
2490 		evhttp_request_dispatch(evcon);
2491 
2492 	return (0);
2493 }
2494 
2495 void
2496 evhttp_cancel_request(struct evhttp_request *req)
2497 {
2498 	struct evhttp_connection *evcon = req->evcon;
2499 	if (evcon != NULL) {
2500 		/* We need to remove it from the connection */
2501 		if (TAILQ_FIRST(&evcon->requests) == req) {
2502 			/* it's currently being worked on, so reset
2503 			 * the connection.
2504 			 */
2505 			evhttp_connection_fail_(evcon,
2506 			    EVREQ_HTTP_REQUEST_CANCEL);
2507 
2508 			/* connection fail freed the request */
2509 			return;
2510 		} else {
2511 			/* otherwise, we can just remove it from the
2512 			 * queue
2513 			 */
2514 			TAILQ_REMOVE(&evcon->requests, req, next);
2515 		}
2516 	}
2517 
2518 	evhttp_request_free(req);
2519 }
2520 
2521 /*
2522  * Reads data from file descriptor into request structure
2523  * Request structure needs to be set up correctly.
2524  */
2525 
2526 void
2527 evhttp_start_read_(struct evhttp_connection *evcon)
2528 {
2529 	/* Set up an event to read the headers */
2530 	bufferevent_disable(evcon->bufev, EV_WRITE);
2531 	bufferevent_enable(evcon->bufev, EV_READ);
2532 	evcon->state = EVCON_READING_FIRSTLINE;
2533 	/* Reset the bufferevent callbacks */
2534 	bufferevent_setcb(evcon->bufev,
2535 	    evhttp_read_cb,
2536 	    evhttp_write_cb,
2537 	    evhttp_error_cb,
2538 	    evcon);
2539 
2540 	/* If there's still data pending, process it next time through the
2541 	 * loop.  Don't do it now; that could get recusive. */
2542 	if (evbuffer_get_length(bufferevent_get_input(evcon->bufev))) {
2543 		event_deferred_cb_schedule_(get_deferred_queue(evcon),
2544 		    &evcon->read_more_deferred_cb);
2545 	}
2546 }
2547 
2548 static void
2549 evhttp_send_done(struct evhttp_connection *evcon, void *arg)
2550 {
2551 	int need_close;
2552 	struct evhttp_request *req = TAILQ_FIRST(&evcon->requests);
2553 	TAILQ_REMOVE(&evcon->requests, req, next);
2554 
2555 	if (req->on_complete_cb != NULL) {
2556 		req->on_complete_cb(req, req->on_complete_cb_arg);
2557 	}
2558 
2559 	need_close =
2560 	    (REQ_VERSION_BEFORE(req, 1, 1) &&
2561 		!evhttp_is_connection_keepalive(req->input_headers))||
2562 	    evhttp_is_connection_close(req->flags, req->input_headers) ||
2563 	    evhttp_is_connection_close(req->flags, req->output_headers);
2564 
2565 	EVUTIL_ASSERT(req->flags & EVHTTP_REQ_OWN_CONNECTION);
2566 	evhttp_request_free(req);
2567 
2568 	if (need_close) {
2569 		evhttp_connection_free(evcon);
2570 		return;
2571 	}
2572 
2573 	/* we have a persistent connection; try to accept another request. */
2574 	if (evhttp_associate_new_request_with_connection(evcon) == -1) {
2575 		evhttp_connection_free(evcon);
2576 	}
2577 }
2578 
2579 /*
2580  * Returns an error page.
2581  */
2582 
2583 void
2584 evhttp_send_error(struct evhttp_request *req, int error, const char *reason)
2585 {
2586 
2587 #define ERR_FORMAT "<HTML><HEAD>\n" \
2588 	    "<TITLE>%d %s</TITLE>\n" \
2589 	    "</HEAD><BODY>\n" \
2590 	    "<H1>%s</H1>\n" \
2591 	    "</BODY></HTML>\n"
2592 
2593 	struct evbuffer *buf = evbuffer_new();
2594 	if (buf == NULL) {
2595 		/* if we cannot allocate memory; we just drop the connection */
2596 		evhttp_connection_free(req->evcon);
2597 		return;
2598 	}
2599 	if (reason == NULL) {
2600 		reason = evhttp_response_phrase_internal(error);
2601 	}
2602 
2603 	evhttp_response_code_(req, error, reason);
2604 
2605 	evbuffer_add_printf(buf, ERR_FORMAT, error, reason, reason);
2606 
2607 	evhttp_send_page_(req, buf);
2608 
2609 	evbuffer_free(buf);
2610 #undef ERR_FORMAT
2611 }
2612 
2613 /* Requires that headers and response code are already set up */
2614 
2615 static inline void
2616 evhttp_send(struct evhttp_request *req, struct evbuffer *databuf)
2617 {
2618 	struct evhttp_connection *evcon = req->evcon;
2619 
2620 	if (evcon == NULL) {
2621 		evhttp_request_free(req);
2622 		return;
2623 	}
2624 
2625 	EVUTIL_ASSERT(TAILQ_FIRST(&evcon->requests) == req);
2626 
2627 	/* we expect no more calls form the user on this request */
2628 	req->userdone = 1;
2629 
2630 	/* xxx: not sure if we really should expose the data buffer this way */
2631 	if (databuf != NULL)
2632 		evbuffer_add_buffer(req->output_buffer, databuf);
2633 
2634 	/* Adds headers to the response */
2635 	evhttp_make_header(evcon, req);
2636 
2637 	evhttp_write_buffer(evcon, evhttp_send_done, NULL);
2638 }
2639 
2640 void
2641 evhttp_send_reply(struct evhttp_request *req, int code, const char *reason,
2642     struct evbuffer *databuf)
2643 {
2644 	evhttp_response_code_(req, code, reason);
2645 
2646 	evhttp_send(req, databuf);
2647 }
2648 
2649 void
2650 evhttp_send_reply_start(struct evhttp_request *req, int code,
2651     const char *reason)
2652 {
2653 	evhttp_response_code_(req, code, reason);
2654 	if (evhttp_find_header(req->output_headers, "Content-Length") == NULL &&
2655 	    REQ_VERSION_ATLEAST(req, 1, 1) &&
2656 	    evhttp_response_needs_body(req)) {
2657 		/*
2658 		 * prefer HTTP/1.1 chunked encoding to closing the connection;
2659 		 * note RFC 2616 section 4.4 forbids it with Content-Length:
2660 		 * and it's not necessary then anyway.
2661 		 */
2662 		evhttp_add_header(req->output_headers, "Transfer-Encoding",
2663 		    "chunked");
2664 		req->chunked = 1;
2665 	} else {
2666 		req->chunked = 0;
2667 	}
2668 	evhttp_make_header(req->evcon, req);
2669 	evhttp_write_buffer(req->evcon, NULL, NULL);
2670 }
2671 
2672 void
2673 evhttp_send_reply_chunk_with_cb(struct evhttp_request *req, struct evbuffer *databuf,
2674     void (*cb)(struct evhttp_connection *, void *), void *arg)
2675 {
2676 	struct evhttp_connection *evcon = req->evcon;
2677 	struct evbuffer *output;
2678 
2679 	if (evcon == NULL)
2680 		return;
2681 
2682 	output = bufferevent_get_output(evcon->bufev);
2683 
2684 	if (evbuffer_get_length(databuf) == 0)
2685 		return;
2686 	if (!evhttp_response_needs_body(req))
2687 		return;
2688 	if (req->chunked) {
2689 		evbuffer_add_printf(output, "%x\r\n",
2690 				    (unsigned)evbuffer_get_length(databuf));
2691 	}
2692 	evbuffer_add_buffer(output, databuf);
2693 	if (req->chunked) {
2694 		evbuffer_add(output, "\r\n", 2);
2695 	}
2696 	evhttp_write_buffer(evcon, cb, arg);
2697 }
2698 
2699 void
2700 evhttp_send_reply_chunk(struct evhttp_request *req, struct evbuffer *databuf)
2701 {
2702 	evhttp_send_reply_chunk_with_cb(req, databuf, NULL, NULL);
2703 }
2704 void
2705 evhttp_send_reply_end(struct evhttp_request *req)
2706 {
2707 	struct evhttp_connection *evcon = req->evcon;
2708 	struct evbuffer *output;
2709 
2710 	if (evcon == NULL) {
2711 		evhttp_request_free(req);
2712 		return;
2713 	}
2714 
2715 	output = bufferevent_get_output(evcon->bufev);
2716 
2717 	/* we expect no more calls form the user on this request */
2718 	req->userdone = 1;
2719 
2720 	if (req->chunked) {
2721 		evbuffer_add(output, "0\r\n\r\n", 5);
2722 		evhttp_write_buffer(req->evcon, evhttp_send_done, NULL);
2723 		req->chunked = 0;
2724 	} else if (evbuffer_get_length(output) == 0) {
2725 		/* let the connection know that we are done with the request */
2726 		evhttp_send_done(evcon, NULL);
2727 	} else {
2728 		/* make the callback execute after all data has been written */
2729 		evcon->cb = evhttp_send_done;
2730 		evcon->cb_arg = NULL;
2731 	}
2732 }
2733 
2734 static const char *informational_phrases[] = {
2735 	/* 100 */ "Continue",
2736 	/* 101 */ "Switching Protocols"
2737 };
2738 
2739 static const char *success_phrases[] = {
2740 	/* 200 */ "OK",
2741 	/* 201 */ "Created",
2742 	/* 202 */ "Accepted",
2743 	/* 203 */ "Non-Authoritative Information",
2744 	/* 204 */ "No Content",
2745 	/* 205 */ "Reset Content",
2746 	/* 206 */ "Partial Content"
2747 };
2748 
2749 static const char *redirection_phrases[] = {
2750 	/* 300 */ "Multiple Choices",
2751 	/* 301 */ "Moved Permanently",
2752 	/* 302 */ "Found",
2753 	/* 303 */ "See Other",
2754 	/* 304 */ "Not Modified",
2755 	/* 305 */ "Use Proxy",
2756 	/* 307 */ "Temporary Redirect"
2757 };
2758 
2759 static const char *client_error_phrases[] = {
2760 	/* 400 */ "Bad Request",
2761 	/* 401 */ "Unauthorized",
2762 	/* 402 */ "Payment Required",
2763 	/* 403 */ "Forbidden",
2764 	/* 404 */ "Not Found",
2765 	/* 405 */ "Method Not Allowed",
2766 	/* 406 */ "Not Acceptable",
2767 	/* 407 */ "Proxy Authentication Required",
2768 	/* 408 */ "Request Time-out",
2769 	/* 409 */ "Conflict",
2770 	/* 410 */ "Gone",
2771 	/* 411 */ "Length Required",
2772 	/* 412 */ "Precondition Failed",
2773 	/* 413 */ "Request Entity Too Large",
2774 	/* 414 */ "Request-URI Too Large",
2775 	/* 415 */ "Unsupported Media Type",
2776 	/* 416 */ "Requested range not satisfiable",
2777 	/* 417 */ "Expectation Failed"
2778 };
2779 
2780 static const char *server_error_phrases[] = {
2781 	/* 500 */ "Internal Server Error",
2782 	/* 501 */ "Not Implemented",
2783 	/* 502 */ "Bad Gateway",
2784 	/* 503 */ "Service Unavailable",
2785 	/* 504 */ "Gateway Time-out",
2786 	/* 505 */ "HTTP Version not supported"
2787 };
2788 
2789 struct response_class {
2790 	const char *name;
2791 	size_t num_responses;
2792 	const char **responses;
2793 };
2794 
2795 #ifndef MEMBERSOF
2796 #define MEMBERSOF(x) (sizeof(x)/sizeof(x[0]))
2797 #endif
2798 
2799 static const struct response_class response_classes[] = {
2800 	/* 1xx */ { "Informational", MEMBERSOF(informational_phrases), informational_phrases },
2801 	/* 2xx */ { "Success", MEMBERSOF(success_phrases), success_phrases },
2802 	/* 3xx */ { "Redirection", MEMBERSOF(redirection_phrases), redirection_phrases },
2803 	/* 4xx */ { "Client Error", MEMBERSOF(client_error_phrases), client_error_phrases },
2804 	/* 5xx */ { "Server Error", MEMBERSOF(server_error_phrases), server_error_phrases }
2805 };
2806 
2807 static const char *
2808 evhttp_response_phrase_internal(int code)
2809 {
2810 	int klass = code / 100 - 1;
2811 	int subcode = code % 100;
2812 
2813 	/* Unknown class - can't do any better here */
2814 	if (klass < 0 || klass >= (int) MEMBERSOF(response_classes))
2815 		return "Unknown Status Class";
2816 
2817 	/* Unknown sub-code, return class name at least */
2818 	if (subcode >= (int) response_classes[klass].num_responses)
2819 		return response_classes[klass].name;
2820 
2821 	return response_classes[klass].responses[subcode];
2822 }
2823 
2824 void
2825 evhttp_response_code_(struct evhttp_request *req, int code, const char *reason)
2826 {
2827 	req->kind = EVHTTP_RESPONSE;
2828 	req->response_code = code;
2829 	if (req->response_code_line != NULL)
2830 		mm_free(req->response_code_line);
2831 	if (reason == NULL)
2832 		reason = evhttp_response_phrase_internal(code);
2833 	req->response_code_line = mm_strdup(reason);
2834 	if (req->response_code_line == NULL) {
2835 		event_warn("%s: strdup", __func__);
2836 		/* XXX what else can we do? */
2837 	}
2838 }
2839 
2840 void
2841 evhttp_send_page_(struct evhttp_request *req, struct evbuffer *databuf)
2842 {
2843 	if (!req->major || !req->minor) {
2844 		req->major = 1;
2845 		req->minor = 1;
2846 	}
2847 
2848 	if (req->kind != EVHTTP_RESPONSE)
2849 		evhttp_response_code_(req, 200, "OK");
2850 
2851 	evhttp_clear_headers(req->output_headers);
2852 	evhttp_add_header(req->output_headers, "Content-Type", "text/html");
2853 	evhttp_add_header(req->output_headers, "Connection", "close");
2854 
2855 	evhttp_send(req, databuf);
2856 }
2857 
2858 static const char uri_chars[256] = {
2859 	/* 0 */
2860 	0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
2861 	0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
2862 	0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 1, 1, 0,
2863 	1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 0, 0, 0, 0, 0, 0,
2864 	/* 64 */
2865 	0, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
2866 	1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 0, 0, 0, 0, 1,
2867 	0, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
2868 	1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 0, 0, 0, 1, 0,
2869 	/* 128 */
2870 	0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
2871 	0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
2872 	0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
2873 	0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
2874 	/* 192 */
2875 	0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
2876 	0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
2877 	0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
2878 	0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
2879 };
2880 
2881 #define CHAR_IS_UNRESERVED(c)			\
2882 	(uri_chars[(unsigned char)(c)])
2883 
2884 /*
2885  * Helper functions to encode/decode a string for inclusion in a URI.
2886  * The returned string must be freed by the caller.
2887  */
2888 char *
2889 evhttp_uriencode(const char *uri, ev_ssize_t len, int space_as_plus)
2890 {
2891 	struct evbuffer *buf = evbuffer_new();
2892 	const char *p, *end;
2893 	char *result;
2894 
2895 	if (buf == NULL)
2896 		return (NULL);
2897 
2898 	if (len >= 0)
2899 		end = uri+len;
2900 	else
2901 		end = uri+strlen(uri);
2902 
2903 	for (p = uri; p < end; p++) {
2904 		if (CHAR_IS_UNRESERVED(*p)) {
2905 			evbuffer_add(buf, p, 1);
2906 		} else if (*p == ' ' && space_as_plus) {
2907 			evbuffer_add(buf, "+", 1);
2908 		} else {
2909 			evbuffer_add_printf(buf, "%%%02X", (unsigned char)(*p));
2910 		}
2911 	}
2912 	evbuffer_add(buf, "", 1); /* NUL-terminator. */
2913 	result = mm_malloc(evbuffer_get_length(buf));
2914 	if (result)
2915 		evbuffer_remove(buf, result, evbuffer_get_length(buf));
2916 	evbuffer_free(buf);
2917 
2918 	return (result);
2919 }
2920 
2921 char *
2922 evhttp_encode_uri(const char *str)
2923 {
2924 	return evhttp_uriencode(str, -1, 0);
2925 }
2926 
2927 /*
2928  * @param decode_plus_ctl: if 1, we decode plus into space.  If 0, we don't.
2929  *     If -1, when true we transform plus to space only after we've seen
2930  *     a ?.  -1 is deprecated.
2931  * @return the number of bytes written to 'ret'.
2932  */
2933 int
2934 evhttp_decode_uri_internal(
2935 	const char *uri, size_t length, char *ret, int decode_plus_ctl)
2936 {
2937 	char c;
2938 	int j;
2939 	int decode_plus = (decode_plus_ctl == 1) ? 1: 0;
2940 	unsigned i;
2941 
2942 	for (i = j = 0; i < length; i++) {
2943 		c = uri[i];
2944 		if (c == '?') {
2945 			if (decode_plus_ctl < 0)
2946 				decode_plus = 1;
2947 		} else if (c == '+' && decode_plus) {
2948 			c = ' ';
2949 		} else if ((i + 2) < length && c == '%' &&
2950 			EVUTIL_ISXDIGIT_(uri[i+1]) && EVUTIL_ISXDIGIT_(uri[i+2])) {
2951 			char tmp[3];
2952 			tmp[0] = uri[i+1];
2953 			tmp[1] = uri[i+2];
2954 			tmp[2] = '\0';
2955 			c = (char)strtol(tmp, NULL, 16);
2956 			i += 2;
2957 		}
2958 		ret[j++] = c;
2959 	}
2960 	ret[j] = '\0';
2961 
2962 	return (j);
2963 }
2964 
2965 /* deprecated */
2966 char *
2967 evhttp_decode_uri(const char *uri)
2968 {
2969 	char *ret;
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 	evhttp_decode_uri_internal(uri, strlen(uri),
2978 	    ret, -1 /*always_decode_plus*/);
2979 
2980 	return (ret);
2981 }
2982 
2983 char *
2984 evhttp_uridecode(const char *uri, int decode_plus, size_t *size_out)
2985 {
2986 	char *ret;
2987 	int n;
2988 
2989 	if ((ret = mm_malloc(strlen(uri) + 1)) == NULL) {
2990 		event_warn("%s: malloc(%lu)", __func__,
2991 			  (unsigned long)(strlen(uri) + 1));
2992 		return (NULL);
2993 	}
2994 
2995 	n = evhttp_decode_uri_internal(uri, strlen(uri),
2996 	    ret, !!decode_plus/*always_decode_plus*/);
2997 
2998 	if (size_out) {
2999 		EVUTIL_ASSERT(n >= 0);
3000 		*size_out = (size_t)n;
3001 	}
3002 
3003 	return (ret);
3004 }
3005 
3006 /*
3007  * Helper function to parse out arguments in a query.
3008  * The arguments are separated by key and value.
3009  */
3010 
3011 static int
3012 evhttp_parse_query_impl(const char *str, struct evkeyvalq *headers,
3013     int is_whole_uri)
3014 {
3015 	char *line=NULL;
3016 	char *argument;
3017 	char *p;
3018 	const char *query_part;
3019 	int result = -1;
3020 	struct evhttp_uri *uri=NULL;
3021 
3022 	TAILQ_INIT(headers);
3023 
3024 	if (is_whole_uri) {
3025 		uri = evhttp_uri_parse(str);
3026 		if (!uri)
3027 			goto error;
3028 		query_part = evhttp_uri_get_query(uri);
3029 	} else {
3030 		query_part = str;
3031 	}
3032 
3033 	/* No arguments - we are done */
3034 	if (!query_part || !strlen(query_part)) {
3035 		result = 0;
3036 		goto done;
3037 	}
3038 
3039 	if ((line = mm_strdup(query_part)) == NULL) {
3040 		event_warn("%s: strdup", __func__);
3041 		goto error;
3042 	}
3043 
3044 	p = argument = line;
3045 	while (p != NULL && *p != '\0') {
3046 		char *key, *value, *decoded_value;
3047 		argument = strsep(&p, "&");
3048 
3049 		value = argument;
3050 		key = strsep(&value, "=");
3051 		if (value == NULL || *key == '\0') {
3052 			goto error;
3053 		}
3054 
3055 		if ((decoded_value = mm_malloc(strlen(value) + 1)) == NULL) {
3056 			event_warn("%s: mm_malloc", __func__);
3057 			goto error;
3058 		}
3059 		evhttp_decode_uri_internal(value, strlen(value),
3060 		    decoded_value, 1 /*always_decode_plus*/);
3061 		event_debug(("Query Param: %s -> %s\n", key, decoded_value));
3062 		evhttp_add_header_internal(headers, key, decoded_value);
3063 		mm_free(decoded_value);
3064 	}
3065 
3066 	result = 0;
3067 	goto done;
3068 error:
3069 	evhttp_clear_headers(headers);
3070 done:
3071 	if (line)
3072 		mm_free(line);
3073 	if (uri)
3074 		evhttp_uri_free(uri);
3075 	return result;
3076 }
3077 
3078 int
3079 evhttp_parse_query(const char *uri, struct evkeyvalq *headers)
3080 {
3081 	return evhttp_parse_query_impl(uri, headers, 1);
3082 }
3083 int
3084 evhttp_parse_query_str(const char *uri, struct evkeyvalq *headers)
3085 {
3086 	return evhttp_parse_query_impl(uri, headers, 0);
3087 }
3088 
3089 static struct evhttp_cb *
3090 evhttp_dispatch_callback(struct httpcbq *callbacks, struct evhttp_request *req)
3091 {
3092 	struct evhttp_cb *cb;
3093 	size_t offset = 0;
3094 	char *translated;
3095 	const char *path;
3096 
3097 	/* Test for different URLs */
3098 	path = evhttp_uri_get_path(req->uri_elems);
3099 	offset = strlen(path);
3100 	if ((translated = mm_malloc(offset + 1)) == NULL)
3101 		return (NULL);
3102 	evhttp_decode_uri_internal(path, offset, translated,
3103 	    0 /* decode_plus */);
3104 
3105 	TAILQ_FOREACH(cb, callbacks, next) {
3106 		if (!strcmp(cb->what, translated)) {
3107 			mm_free(translated);
3108 			return (cb);
3109 		}
3110 	}
3111 
3112 	mm_free(translated);
3113 	return (NULL);
3114 }
3115 
3116 
3117 static int
3118 prefix_suffix_match(const char *pattern, const char *name, int ignorecase)
3119 {
3120 	char c;
3121 
3122 	while (1) {
3123 		switch (c = *pattern++) {
3124 		case '\0':
3125 			return *name == '\0';
3126 
3127 		case '*':
3128 			while (*name != '\0') {
3129 				if (prefix_suffix_match(pattern, name,
3130 					ignorecase))
3131 					return (1);
3132 				++name;
3133 			}
3134 			return (0);
3135 		default:
3136 			if (c != *name) {
3137 				if (!ignorecase ||
3138 				    EVUTIL_TOLOWER_(c) != EVUTIL_TOLOWER_(*name))
3139 					return (0);
3140 			}
3141 			++name;
3142 		}
3143 	}
3144 	/* NOTREACHED */
3145 }
3146 
3147 /*
3148    Search the vhost hierarchy beginning with http for a server alias
3149    matching hostname.  If a match is found, and outhttp is non-null,
3150    outhttp is set to the matching http object and 1 is returned.
3151 */
3152 
3153 static int
3154 evhttp_find_alias(struct evhttp *http, struct evhttp **outhttp,
3155 		  const char *hostname)
3156 {
3157 	struct evhttp_server_alias *alias;
3158 	struct evhttp *vhost;
3159 
3160 	TAILQ_FOREACH(alias, &http->aliases, next) {
3161 		/* XXX Do we need to handle IP addresses? */
3162 		if (!evutil_ascii_strcasecmp(alias->alias, hostname)) {
3163 			if (outhttp)
3164 				*outhttp = http;
3165 			return 1;
3166 		}
3167 	}
3168 
3169 	/* XXX It might be good to avoid recursion here, but I don't
3170 	   see a way to do that w/o a list. */
3171 	TAILQ_FOREACH(vhost, &http->virtualhosts, next_vhost) {
3172 		if (evhttp_find_alias(vhost, outhttp, hostname))
3173 			return 1;
3174 	}
3175 
3176 	return 0;
3177 }
3178 
3179 /*
3180    Attempts to find the best http object to handle a request for a hostname.
3181    All aliases for the root http object and vhosts are searched for an exact
3182    match. Then, the vhost hierarchy is traversed again for a matching
3183    pattern.
3184 
3185    If an alias or vhost is matched, 1 is returned, and outhttp, if non-null,
3186    is set with the best matching http object. If there are no matches, the
3187    root http object is stored in outhttp and 0 is returned.
3188 */
3189 
3190 static int
3191 evhttp_find_vhost(struct evhttp *http, struct evhttp **outhttp,
3192 		  const char *hostname)
3193 {
3194 	struct evhttp *vhost;
3195 	struct evhttp *oldhttp;
3196 	int match_found = 0;
3197 
3198 	if (evhttp_find_alias(http, outhttp, hostname))
3199 		return 1;
3200 
3201 	do {
3202 		oldhttp = http;
3203 		TAILQ_FOREACH(vhost, &http->virtualhosts, next_vhost) {
3204 			if (prefix_suffix_match(vhost->vhost_pattern,
3205 				hostname, 1 /* ignorecase */)) {
3206 				http = vhost;
3207 				match_found = 1;
3208 				break;
3209 			}
3210 		}
3211 	} while (oldhttp != http);
3212 
3213 	if (outhttp)
3214 		*outhttp = http;
3215 
3216 	return match_found;
3217 }
3218 
3219 static void
3220 evhttp_handle_request(struct evhttp_request *req, void *arg)
3221 {
3222 	struct evhttp *http = arg;
3223 	struct evhttp_cb *cb = NULL;
3224 	const char *hostname;
3225 
3226 	/* we have a new request on which the user needs to take action */
3227 	req->userdone = 0;
3228 
3229 	if (req->type == 0 || req->uri == NULL) {
3230 		evhttp_send_error(req, HTTP_BADREQUEST, NULL);
3231 		return;
3232 	}
3233 
3234 	if ((http->allowed_methods & req->type) == 0) {
3235 		event_debug(("Rejecting disallowed method %x (allowed: %x)\n",
3236 			(unsigned)req->type, (unsigned)http->allowed_methods));
3237 		evhttp_send_error(req, HTTP_NOTIMPLEMENTED, NULL);
3238 		return;
3239 	}
3240 
3241 	/* handle potential virtual hosts */
3242 	hostname = evhttp_request_get_host(req);
3243 	if (hostname != NULL) {
3244 		evhttp_find_vhost(http, &http, hostname);
3245 	}
3246 
3247 	if ((cb = evhttp_dispatch_callback(&http->callbacks, req)) != NULL) {
3248 		(*cb->cb)(req, cb->cbarg);
3249 		return;
3250 	}
3251 
3252 	/* Generic call back */
3253 	if (http->gencb) {
3254 		(*http->gencb)(req, http->gencbarg);
3255 		return;
3256 	} else {
3257 		/* We need to send a 404 here */
3258 #define ERR_FORMAT "<html><head>" \
3259 		    "<title>404 Not Found</title>" \
3260 		    "</head><body>" \
3261 		    "<h1>Not Found</h1>" \
3262 		    "<p>The requested URL %s was not found on this server.</p>"\
3263 		    "</body></html>\n"
3264 
3265 		char *escaped_html;
3266 		struct evbuffer *buf;
3267 
3268 		if ((escaped_html = evhttp_htmlescape(req->uri)) == NULL) {
3269 			evhttp_connection_free(req->evcon);
3270 			return;
3271 		}
3272 
3273 		if ((buf = evbuffer_new()) == NULL) {
3274 			mm_free(escaped_html);
3275 			evhttp_connection_free(req->evcon);
3276 			return;
3277 		}
3278 
3279 		evhttp_response_code_(req, HTTP_NOTFOUND, "Not Found");
3280 
3281 		evbuffer_add_printf(buf, ERR_FORMAT, escaped_html);
3282 
3283 		mm_free(escaped_html);
3284 
3285 		evhttp_send_page_(req, buf);
3286 
3287 		evbuffer_free(buf);
3288 #undef ERR_FORMAT
3289 	}
3290 }
3291 
3292 /* Listener callback when a connection arrives at a server. */
3293 static void
3294 accept_socket_cb(struct evconnlistener *listener, evutil_socket_t nfd, struct sockaddr *peer_sa, int peer_socklen, void *arg)
3295 {
3296 	struct evhttp *http = arg;
3297 
3298 	evhttp_get_request(http, nfd, peer_sa, peer_socklen);
3299 }
3300 
3301 int
3302 evhttp_bind_socket(struct evhttp *http, const char *address, ev_uint16_t port)
3303 {
3304 	struct evhttp_bound_socket *bound =
3305 		evhttp_bind_socket_with_handle(http, address, port);
3306 	if (bound == NULL)
3307 		return (-1);
3308 	return (0);
3309 }
3310 
3311 struct evhttp_bound_socket *
3312 evhttp_bind_socket_with_handle(struct evhttp *http, const char *address, ev_uint16_t port)
3313 {
3314 	evutil_socket_t fd;
3315 	struct evhttp_bound_socket *bound;
3316 
3317 	if ((fd = bind_socket(address, port, 1 /*reuse*/)) == -1)
3318 		return (NULL);
3319 
3320 	if (listen(fd, 128) == -1) {
3321 		event_sock_warn(fd, "%s: listen", __func__);
3322 		evutil_closesocket(fd);
3323 		return (NULL);
3324 	}
3325 
3326 	bound = evhttp_accept_socket_with_handle(http, fd);
3327 
3328 	if (bound != NULL) {
3329 		event_debug(("Bound to port %d - Awaiting connections ... ",
3330 			port));
3331 		return (bound);
3332 	}
3333 
3334 	return (NULL);
3335 }
3336 
3337 int
3338 evhttp_accept_socket(struct evhttp *http, evutil_socket_t fd)
3339 {
3340 	struct evhttp_bound_socket *bound =
3341 		evhttp_accept_socket_with_handle(http, fd);
3342 	if (bound == NULL)
3343 		return (-1);
3344 	return (0);
3345 }
3346 
3347 void
3348 evhttp_foreach_bound_socket(struct evhttp *http,
3349                             evhttp_bound_socket_foreach_fn *function,
3350                             void *argument)
3351 {
3352 	struct evhttp_bound_socket *bound;
3353 
3354 	TAILQ_FOREACH(bound, &http->sockets, next)
3355 		function(bound, argument);
3356 }
3357 
3358 struct evhttp_bound_socket *
3359 evhttp_accept_socket_with_handle(struct evhttp *http, evutil_socket_t fd)
3360 {
3361 	struct evhttp_bound_socket *bound;
3362 	struct evconnlistener *listener;
3363 	const int flags =
3364 	    LEV_OPT_REUSEABLE|LEV_OPT_CLOSE_ON_EXEC|LEV_OPT_CLOSE_ON_FREE;
3365 
3366 	listener = evconnlistener_new(http->base, NULL, NULL,
3367 	    flags,
3368 	    0, /* Backlog is '0' because we already said 'listen' */
3369 	    fd);
3370 	if (!listener)
3371 		return (NULL);
3372 
3373 	bound = evhttp_bind_listener(http, listener);
3374 	if (!bound) {
3375 		evconnlistener_free(listener);
3376 		return (NULL);
3377 	}
3378 	return (bound);
3379 }
3380 
3381 struct evhttp_bound_socket *
3382 evhttp_bind_listener(struct evhttp *http, struct evconnlistener *listener)
3383 {
3384 	struct evhttp_bound_socket *bound;
3385 
3386 	bound = mm_malloc(sizeof(struct evhttp_bound_socket));
3387 	if (bound == NULL)
3388 		return (NULL);
3389 
3390 	bound->listener = listener;
3391 	TAILQ_INSERT_TAIL(&http->sockets, bound, next);
3392 
3393 	evconnlistener_set_cb(listener, accept_socket_cb, http);
3394 	return bound;
3395 }
3396 
3397 evutil_socket_t
3398 evhttp_bound_socket_get_fd(struct evhttp_bound_socket *bound)
3399 {
3400 	return evconnlistener_get_fd(bound->listener);
3401 }
3402 
3403 struct evconnlistener *
3404 evhttp_bound_socket_get_listener(struct evhttp_bound_socket *bound)
3405 {
3406 	return bound->listener;
3407 }
3408 
3409 void
3410 evhttp_del_accept_socket(struct evhttp *http, struct evhttp_bound_socket *bound)
3411 {
3412 	TAILQ_REMOVE(&http->sockets, bound, next);
3413 	evconnlistener_free(bound->listener);
3414 	mm_free(bound);
3415 }
3416 
3417 static struct evhttp*
3418 evhttp_new_object(void)
3419 {
3420 	struct evhttp *http = NULL;
3421 
3422 	if ((http = mm_calloc(1, sizeof(struct evhttp))) == NULL) {
3423 		event_warn("%s: calloc", __func__);
3424 		return (NULL);
3425 	}
3426 
3427 	evutil_timerclear(&http->timeout);
3428 	evhttp_set_max_headers_size(http, EV_SIZE_MAX);
3429 	evhttp_set_max_body_size(http, EV_SIZE_MAX);
3430 	evhttp_set_default_content_type(http, "text/html; charset=ISO-8859-1");
3431 	evhttp_set_allowed_methods(http,
3432 	    EVHTTP_REQ_GET |
3433 	    EVHTTP_REQ_POST |
3434 	    EVHTTP_REQ_HEAD |
3435 	    EVHTTP_REQ_PUT |
3436 	    EVHTTP_REQ_DELETE);
3437 
3438 	TAILQ_INIT(&http->sockets);
3439 	TAILQ_INIT(&http->callbacks);
3440 	TAILQ_INIT(&http->connections);
3441 	TAILQ_INIT(&http->virtualhosts);
3442 	TAILQ_INIT(&http->aliases);
3443 
3444 	return (http);
3445 }
3446 
3447 struct evhttp *
3448 evhttp_new(struct event_base *base)
3449 {
3450 	struct evhttp *http = NULL;
3451 
3452 	http = evhttp_new_object();
3453 	if (http == NULL)
3454 		return (NULL);
3455 	http->base = base;
3456 
3457 	return (http);
3458 }
3459 
3460 /*
3461  * Start a web server on the specified address and port.
3462  */
3463 
3464 struct evhttp *
3465 evhttp_start(const char *address, unsigned short port)
3466 {
3467 	struct evhttp *http = NULL;
3468 
3469 	http = evhttp_new_object();
3470 	if (http == NULL)
3471 		return (NULL);
3472 	if (evhttp_bind_socket(http, address, port) == -1) {
3473 		mm_free(http);
3474 		return (NULL);
3475 	}
3476 
3477 	return (http);
3478 }
3479 
3480 void
3481 evhttp_free(struct evhttp* http)
3482 {
3483 	struct evhttp_cb *http_cb;
3484 	struct evhttp_connection *evcon;
3485 	struct evhttp_bound_socket *bound;
3486 	struct evhttp* vhost;
3487 	struct evhttp_server_alias *alias;
3488 
3489 	/* Remove the accepting part */
3490 	while ((bound = TAILQ_FIRST(&http->sockets)) != NULL) {
3491 		TAILQ_REMOVE(&http->sockets, bound, next);
3492 
3493 		evconnlistener_free(bound->listener);
3494 
3495 		mm_free(bound);
3496 	}
3497 
3498 	while ((evcon = TAILQ_FIRST(&http->connections)) != NULL) {
3499 		/* evhttp_connection_free removes the connection */
3500 		evhttp_connection_free(evcon);
3501 	}
3502 
3503 	while ((http_cb = TAILQ_FIRST(&http->callbacks)) != NULL) {
3504 		TAILQ_REMOVE(&http->callbacks, http_cb, next);
3505 		mm_free(http_cb->what);
3506 		mm_free(http_cb);
3507 	}
3508 
3509 	while ((vhost = TAILQ_FIRST(&http->virtualhosts)) != NULL) {
3510 		TAILQ_REMOVE(&http->virtualhosts, vhost, next_vhost);
3511 
3512 		evhttp_free(vhost);
3513 	}
3514 
3515 	if (http->vhost_pattern != NULL)
3516 		mm_free(http->vhost_pattern);
3517 
3518 	while ((alias = TAILQ_FIRST(&http->aliases)) != NULL) {
3519 		TAILQ_REMOVE(&http->aliases, alias, next);
3520 		mm_free(alias->alias);
3521 		mm_free(alias);
3522 	}
3523 
3524 	mm_free(http);
3525 }
3526 
3527 int
3528 evhttp_add_virtual_host(struct evhttp* http, const char *pattern,
3529     struct evhttp* vhost)
3530 {
3531 	/* a vhost can only be a vhost once and should not have bound sockets */
3532 	if (vhost->vhost_pattern != NULL ||
3533 	    TAILQ_FIRST(&vhost->sockets) != NULL)
3534 		return (-1);
3535 
3536 	vhost->vhost_pattern = mm_strdup(pattern);
3537 	if (vhost->vhost_pattern == NULL)
3538 		return (-1);
3539 
3540 	TAILQ_INSERT_TAIL(&http->virtualhosts, vhost, next_vhost);
3541 
3542 	return (0);
3543 }
3544 
3545 int
3546 evhttp_remove_virtual_host(struct evhttp* http, struct evhttp* vhost)
3547 {
3548 	if (vhost->vhost_pattern == NULL)
3549 		return (-1);
3550 
3551 	TAILQ_REMOVE(&http->virtualhosts, vhost, next_vhost);
3552 
3553 	mm_free(vhost->vhost_pattern);
3554 	vhost->vhost_pattern = NULL;
3555 
3556 	return (0);
3557 }
3558 
3559 int
3560 evhttp_add_server_alias(struct evhttp *http, const char *alias)
3561 {
3562 	struct evhttp_server_alias *evalias;
3563 
3564 	evalias = mm_calloc(1, sizeof(*evalias));
3565 	if (!evalias)
3566 		return -1;
3567 
3568 	evalias->alias = mm_strdup(alias);
3569 	if (!evalias->alias) {
3570 		mm_free(evalias);
3571 		return -1;
3572 	}
3573 
3574 	TAILQ_INSERT_TAIL(&http->aliases, evalias, next);
3575 
3576 	return 0;
3577 }
3578 
3579 int
3580 evhttp_remove_server_alias(struct evhttp *http, const char *alias)
3581 {
3582 	struct evhttp_server_alias *evalias;
3583 
3584 	TAILQ_FOREACH(evalias, &http->aliases, next) {
3585 		if (evutil_ascii_strcasecmp(evalias->alias, alias) == 0) {
3586 			TAILQ_REMOVE(&http->aliases, evalias, next);
3587 			mm_free(evalias->alias);
3588 			mm_free(evalias);
3589 			return 0;
3590 		}
3591 	}
3592 
3593 	return -1;
3594 }
3595 
3596 void
3597 evhttp_set_timeout(struct evhttp* http, int timeout_in_secs)
3598 {
3599 	if (timeout_in_secs == -1) {
3600 		evhttp_set_timeout_tv(http, NULL);
3601 	} else {
3602 		struct timeval tv;
3603 		tv.tv_sec = timeout_in_secs;
3604 		tv.tv_usec = 0;
3605 		evhttp_set_timeout_tv(http, &tv);
3606 	}
3607 }
3608 
3609 void
3610 evhttp_set_timeout_tv(struct evhttp* http, const struct timeval* tv)
3611 {
3612 	if (tv) {
3613 		http->timeout = *tv;
3614 	} else {
3615 		evutil_timerclear(&http->timeout);
3616 	}
3617 }
3618 
3619 void
3620 evhttp_set_max_headers_size(struct evhttp* http, ev_ssize_t max_headers_size)
3621 {
3622 	if (max_headers_size < 0)
3623 		http->default_max_headers_size = EV_SIZE_MAX;
3624 	else
3625 		http->default_max_headers_size = max_headers_size;
3626 }
3627 
3628 void
3629 evhttp_set_max_body_size(struct evhttp* http, ev_ssize_t max_body_size)
3630 {
3631 	if (max_body_size < 0)
3632 		http->default_max_body_size = EV_UINT64_MAX;
3633 	else
3634 		http->default_max_body_size = max_body_size;
3635 }
3636 
3637 void
3638 evhttp_set_default_content_type(struct evhttp *http,
3639 	const char *content_type) {
3640 	http->default_content_type = content_type;
3641 }
3642 
3643 void
3644 evhttp_set_allowed_methods(struct evhttp* http, ev_uint16_t methods)
3645 {
3646 	http->allowed_methods = methods;
3647 }
3648 
3649 int
3650 evhttp_set_cb(struct evhttp *http, const char *uri,
3651     void (*cb)(struct evhttp_request *, void *), void *cbarg)
3652 {
3653 	struct evhttp_cb *http_cb;
3654 
3655 	TAILQ_FOREACH(http_cb, &http->callbacks, next) {
3656 		if (strcmp(http_cb->what, uri) == 0)
3657 			return (-1);
3658 	}
3659 
3660 	if ((http_cb = mm_calloc(1, sizeof(struct evhttp_cb))) == NULL) {
3661 		event_warn("%s: calloc", __func__);
3662 		return (-2);
3663 	}
3664 
3665 	http_cb->what = mm_strdup(uri);
3666 	if (http_cb->what == NULL) {
3667 		event_warn("%s: strdup", __func__);
3668 		mm_free(http_cb);
3669 		return (-3);
3670 	}
3671 	http_cb->cb = cb;
3672 	http_cb->cbarg = cbarg;
3673 
3674 	TAILQ_INSERT_TAIL(&http->callbacks, http_cb, next);
3675 
3676 	return (0);
3677 }
3678 
3679 int
3680 evhttp_del_cb(struct evhttp *http, const char *uri)
3681 {
3682 	struct evhttp_cb *http_cb;
3683 
3684 	TAILQ_FOREACH(http_cb, &http->callbacks, next) {
3685 		if (strcmp(http_cb->what, uri) == 0)
3686 			break;
3687 	}
3688 	if (http_cb == NULL)
3689 		return (-1);
3690 
3691 	TAILQ_REMOVE(&http->callbacks, http_cb, next);
3692 	mm_free(http_cb->what);
3693 	mm_free(http_cb);
3694 
3695 	return (0);
3696 }
3697 
3698 void
3699 evhttp_set_gencb(struct evhttp *http,
3700     void (*cb)(struct evhttp_request *, void *), void *cbarg)
3701 {
3702 	http->gencb = cb;
3703 	http->gencbarg = cbarg;
3704 }
3705 
3706 void
3707 evhttp_set_bevcb(struct evhttp *http,
3708     struct bufferevent* (*cb)(struct event_base *, void *), void *cbarg)
3709 {
3710 	http->bevcb = cb;
3711 	http->bevcbarg = cbarg;
3712 }
3713 
3714 /*
3715  * Request related functions
3716  */
3717 
3718 struct evhttp_request *
3719 evhttp_request_new(void (*cb)(struct evhttp_request *, void *), void *arg)
3720 {
3721 	struct evhttp_request *req = NULL;
3722 
3723 	/* Allocate request structure */
3724 	if ((req = mm_calloc(1, sizeof(struct evhttp_request))) == NULL) {
3725 		event_warn("%s: calloc", __func__);
3726 		goto error;
3727 	}
3728 
3729 	req->headers_size = 0;
3730 	req->body_size = 0;
3731 
3732 	req->kind = EVHTTP_RESPONSE;
3733 	req->input_headers = mm_calloc(1, sizeof(struct evkeyvalq));
3734 	if (req->input_headers == NULL) {
3735 		event_warn("%s: calloc", __func__);
3736 		goto error;
3737 	}
3738 	TAILQ_INIT(req->input_headers);
3739 
3740 	req->output_headers = mm_calloc(1, sizeof(struct evkeyvalq));
3741 	if (req->output_headers == NULL) {
3742 		event_warn("%s: calloc", __func__);
3743 		goto error;
3744 	}
3745 	TAILQ_INIT(req->output_headers);
3746 
3747 	if ((req->input_buffer = evbuffer_new()) == NULL) {
3748 		event_warn("%s: evbuffer_new", __func__);
3749 		goto error;
3750 	}
3751 
3752 	if ((req->output_buffer = evbuffer_new()) == NULL) {
3753 		event_warn("%s: evbuffer_new", __func__);
3754 		goto error;
3755 	}
3756 
3757 	req->cb = cb;
3758 	req->cb_arg = arg;
3759 
3760 	return (req);
3761 
3762  error:
3763 	if (req != NULL)
3764 		evhttp_request_free(req);
3765 	return (NULL);
3766 }
3767 
3768 void
3769 evhttp_request_free(struct evhttp_request *req)
3770 {
3771 	if ((req->flags & EVHTTP_REQ_DEFER_FREE) != 0) {
3772 		req->flags |= EVHTTP_REQ_NEEDS_FREE;
3773 		return;
3774 	}
3775 
3776 	if (req->remote_host != NULL)
3777 		mm_free(req->remote_host);
3778 	if (req->uri != NULL)
3779 		mm_free(req->uri);
3780 	if (req->uri_elems != NULL)
3781 		evhttp_uri_free(req->uri_elems);
3782 	if (req->response_code_line != NULL)
3783 		mm_free(req->response_code_line);
3784 	if (req->host_cache != NULL)
3785 		mm_free(req->host_cache);
3786 
3787 	evhttp_clear_headers(req->input_headers);
3788 	mm_free(req->input_headers);
3789 
3790 	evhttp_clear_headers(req->output_headers);
3791 	mm_free(req->output_headers);
3792 
3793 	if (req->input_buffer != NULL)
3794 		evbuffer_free(req->input_buffer);
3795 
3796 	if (req->output_buffer != NULL)
3797 		evbuffer_free(req->output_buffer);
3798 
3799 	mm_free(req);
3800 }
3801 
3802 void
3803 evhttp_request_own(struct evhttp_request *req)
3804 {
3805 	req->flags |= EVHTTP_USER_OWNED;
3806 }
3807 
3808 int
3809 evhttp_request_is_owned(struct evhttp_request *req)
3810 {
3811 	return (req->flags & EVHTTP_USER_OWNED) != 0;
3812 }
3813 
3814 struct evhttp_connection *
3815 evhttp_request_get_connection(struct evhttp_request *req)
3816 {
3817 	return req->evcon;
3818 }
3819 
3820 struct event_base *
3821 evhttp_connection_get_base(struct evhttp_connection *conn)
3822 {
3823 	return conn->base;
3824 }
3825 
3826 void
3827 evhttp_request_set_chunked_cb(struct evhttp_request *req,
3828     void (*cb)(struct evhttp_request *, void *))
3829 {
3830 	req->chunk_cb = cb;
3831 }
3832 
3833 void
3834 evhttp_request_set_header_cb(struct evhttp_request *req,
3835     int (*cb)(struct evhttp_request *, void *))
3836 {
3837 	req->header_cb = cb;
3838 }
3839 
3840 void
3841 evhttp_request_set_error_cb(struct evhttp_request *req,
3842     void (*cb)(enum evhttp_request_error, void *))
3843 {
3844 	req->error_cb = cb;
3845 }
3846 
3847 void
3848 evhttp_request_set_on_complete_cb(struct evhttp_request *req,
3849     void (*cb)(struct evhttp_request *, void *), void *cb_arg)
3850 {
3851 	req->on_complete_cb = cb;
3852 	req->on_complete_cb_arg = cb_arg;
3853 }
3854 
3855 /*
3856  * Allows for inspection of the request URI
3857  */
3858 
3859 const char *
3860 evhttp_request_get_uri(const struct evhttp_request *req) {
3861 	if (req->uri == NULL)
3862 		event_debug(("%s: request %p has no uri\n", __func__, req));
3863 	return (req->uri);
3864 }
3865 
3866 const struct evhttp_uri *
3867 evhttp_request_get_evhttp_uri(const struct evhttp_request *req) {
3868 	if (req->uri_elems == NULL)
3869 		event_debug(("%s: request %p has no uri elems\n",
3870 			    __func__, req));
3871 	return (req->uri_elems);
3872 }
3873 
3874 const char *
3875 evhttp_request_get_host(struct evhttp_request *req)
3876 {
3877 	const char *host = NULL;
3878 
3879 	if (req->host_cache)
3880 		return req->host_cache;
3881 
3882 	if (req->uri_elems)
3883 		host = evhttp_uri_get_host(req->uri_elems);
3884 	if (!host && req->input_headers) {
3885 		const char *p;
3886 		size_t len;
3887 
3888 		host = evhttp_find_header(req->input_headers, "Host");
3889 		/* The Host: header may include a port. Remove it here
3890 		   to be consistent with uri_elems case above. */
3891 		if (host) {
3892 			p = host + strlen(host) - 1;
3893 			while (p > host && EVUTIL_ISDIGIT_(*p))
3894 				--p;
3895 			if (p > host && *p == ':') {
3896 				len = p - host;
3897 				req->host_cache = mm_malloc(len + 1);
3898 				if (!req->host_cache) {
3899 					event_warn("%s: malloc", __func__);
3900 					return NULL;
3901 				}
3902 				memcpy(req->host_cache, host, len);
3903 				req->host_cache[len] = '\0';
3904 				host = req->host_cache;
3905 			}
3906 		}
3907 	}
3908 
3909 	return host;
3910 }
3911 
3912 enum evhttp_cmd_type
3913 evhttp_request_get_command(const struct evhttp_request *req) {
3914 	return (req->type);
3915 }
3916 
3917 int
3918 evhttp_request_get_response_code(const struct evhttp_request *req)
3919 {
3920 	return req->response_code;
3921 }
3922 
3923 const char *
3924 evhttp_request_get_response_code_line(const struct evhttp_request *req)
3925 {
3926 	return req->response_code_line;
3927 }
3928 
3929 /** Returns the input headers */
3930 struct evkeyvalq *evhttp_request_get_input_headers(struct evhttp_request *req)
3931 {
3932 	return (req->input_headers);
3933 }
3934 
3935 /** Returns the output headers */
3936 struct evkeyvalq *evhttp_request_get_output_headers(struct evhttp_request *req)
3937 {
3938 	return (req->output_headers);
3939 }
3940 
3941 /** Returns the input buffer */
3942 struct evbuffer *evhttp_request_get_input_buffer(struct evhttp_request *req)
3943 {
3944 	return (req->input_buffer);
3945 }
3946 
3947 /** Returns the output buffer */
3948 struct evbuffer *evhttp_request_get_output_buffer(struct evhttp_request *req)
3949 {
3950 	return (req->output_buffer);
3951 }
3952 
3953 
3954 /*
3955  * Takes a file descriptor to read a request from.
3956  * The callback is executed once the whole request has been read.
3957  */
3958 
3959 static struct evhttp_connection*
3960 evhttp_get_request_connection(
3961 	struct evhttp* http,
3962 	evutil_socket_t fd, struct sockaddr *sa, ev_socklen_t salen)
3963 {
3964 	struct evhttp_connection *evcon;
3965 	char *hostname = NULL, *portname = NULL;
3966 	struct bufferevent* bev = NULL;
3967 
3968 	name_from_addr(sa, salen, &hostname, &portname);
3969 	if (hostname == NULL || portname == NULL) {
3970 		if (hostname) mm_free(hostname);
3971 		if (portname) mm_free(portname);
3972 		return (NULL);
3973 	}
3974 
3975 	event_debug(("%s: new request from %s:%s on "EV_SOCK_FMT"\n",
3976 		__func__, hostname, portname, EV_SOCK_ARG(fd)));
3977 
3978 	/* we need a connection object to put the http request on */
3979 	if (http->bevcb != NULL) {
3980 		bev = (*http->bevcb)(http->base, http->bevcbarg);
3981 	}
3982 	evcon = evhttp_connection_base_bufferevent_new(
3983 		http->base, NULL, bev, hostname, atoi(portname));
3984 	mm_free(hostname);
3985 	mm_free(portname);
3986 	if (evcon == NULL)
3987 		return (NULL);
3988 
3989 	evcon->max_headers_size = http->default_max_headers_size;
3990 	evcon->max_body_size = http->default_max_body_size;
3991 
3992 	evcon->flags |= EVHTTP_CON_INCOMING;
3993 	evcon->state = EVCON_READING_FIRSTLINE;
3994 
3995 	evcon->fd = fd;
3996 
3997 	bufferevent_setfd(evcon->bufev, fd);
3998 
3999 	return (evcon);
4000 }
4001 
4002 static int
4003 evhttp_associate_new_request_with_connection(struct evhttp_connection *evcon)
4004 {
4005 	struct evhttp *http = evcon->http_server;
4006 	struct evhttp_request *req;
4007 	if ((req = evhttp_request_new(evhttp_handle_request, http)) == NULL)
4008 		return (-1);
4009 
4010 	if ((req->remote_host = mm_strdup(evcon->address)) == NULL) {
4011 		event_warn("%s: strdup", __func__);
4012 		evhttp_request_free(req);
4013 		return (-1);
4014 	}
4015 	req->remote_port = evcon->port;
4016 
4017 	req->evcon = evcon;	/* the request ends up owning the connection */
4018 	req->flags |= EVHTTP_REQ_OWN_CONNECTION;
4019 
4020 	/* We did not present the request to the user user yet, so treat it as
4021 	 * if the user was done with the request.  This allows us to free the
4022 	 * request on a persistent connection if the client drops it without
4023 	 * sending a request.
4024 	 */
4025 	req->userdone = 1;
4026 
4027 	TAILQ_INSERT_TAIL(&evcon->requests, req, next);
4028 
4029 	req->kind = EVHTTP_REQUEST;
4030 
4031 
4032 	evhttp_start_read_(evcon);
4033 
4034 	return (0);
4035 }
4036 
4037 static void
4038 evhttp_get_request(struct evhttp *http, evutil_socket_t fd,
4039     struct sockaddr *sa, ev_socklen_t salen)
4040 {
4041 	struct evhttp_connection *evcon;
4042 
4043 	evcon = evhttp_get_request_connection(http, fd, sa, salen);
4044 	if (evcon == NULL) {
4045 		event_sock_warn(fd, "%s: cannot get connection on "EV_SOCK_FMT,
4046 		    __func__, EV_SOCK_ARG(fd));
4047 		evutil_closesocket(fd);
4048 		return;
4049 	}
4050 
4051 	/* the timeout can be used by the server to close idle connections */
4052 	if (evutil_timerisset(&http->timeout))
4053 		evhttp_connection_set_timeout_tv(evcon, &http->timeout);
4054 
4055 	/*
4056 	 * if we want to accept more than one request on a connection,
4057 	 * we need to know which http server it belongs to.
4058 	 */
4059 	evcon->http_server = http;
4060 	TAILQ_INSERT_TAIL(&http->connections, evcon, next);
4061 
4062 	if (evhttp_associate_new_request_with_connection(evcon) == -1)
4063 		evhttp_connection_free(evcon);
4064 }
4065 
4066 
4067 /*
4068  * Network helper functions that we do not want to export to the rest of
4069  * the world.
4070  */
4071 
4072 static void
4073 name_from_addr(struct sockaddr *sa, ev_socklen_t salen,
4074     char **phost, char **pport)
4075 {
4076 	char ntop[NI_MAXHOST];
4077 	char strport[NI_MAXSERV];
4078 	int ni_result;
4079 
4080 #ifdef EVENT__HAVE_GETNAMEINFO
4081 	ni_result = getnameinfo(sa, salen,
4082 		ntop, sizeof(ntop), strport, sizeof(strport),
4083 		NI_NUMERICHOST|NI_NUMERICSERV);
4084 
4085 	if (ni_result != 0) {
4086 #ifdef EAI_SYSTEM
4087 		/* Windows doesn't have an EAI_SYSTEM. */
4088 		if (ni_result == EAI_SYSTEM)
4089 			event_err(1, "getnameinfo failed");
4090 		else
4091 #endif
4092 			event_errx(1, "getnameinfo failed: %s", gai_strerror(ni_result));
4093 		return;
4094 	}
4095 #else
4096 	ni_result = fake_getnameinfo(sa, salen,
4097 		ntop, sizeof(ntop), strport, sizeof(strport),
4098 		NI_NUMERICHOST|NI_NUMERICSERV);
4099 	if (ni_result != 0)
4100 			return;
4101 #endif
4102 
4103 	*phost = mm_strdup(ntop);
4104 	*pport = mm_strdup(strport);
4105 }
4106 
4107 /* Create a non-blocking socket and bind it */
4108 /* todo: rename this function */
4109 static evutil_socket_t
4110 bind_socket_ai(struct evutil_addrinfo *ai, int reuse)
4111 {
4112 	evutil_socket_t fd;
4113 
4114 	int on = 1, r;
4115 	int serrno;
4116 
4117 	/* Create listen socket */
4118 	fd = evutil_socket_(ai ? ai->ai_family : AF_INET,
4119 	    SOCK_STREAM|EVUTIL_SOCK_NONBLOCK|EVUTIL_SOCK_CLOEXEC, 0);
4120 	if (fd == -1) {
4121 			event_sock_warn(-1, "socket");
4122 			return (-1);
4123 	}
4124 
4125 	if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (void *)&on, sizeof(on))<0)
4126 		goto out;
4127 	if (reuse) {
4128 		if (evutil_make_listen_socket_reuseable(fd) < 0)
4129 			goto out;
4130 	}
4131 
4132 	if (ai != NULL) {
4133 		r = bind(fd, ai->ai_addr, (ev_socklen_t)ai->ai_addrlen);
4134 		if (r == -1)
4135 			goto out;
4136 	}
4137 
4138 	return (fd);
4139 
4140  out:
4141 	serrno = EVUTIL_SOCKET_ERROR();
4142 	evutil_closesocket(fd);
4143 	EVUTIL_SET_SOCKET_ERROR(serrno);
4144 	return (-1);
4145 }
4146 
4147 static struct evutil_addrinfo *
4148 make_addrinfo(const char *address, ev_uint16_t port)
4149 {
4150 	struct evutil_addrinfo *ai = NULL;
4151 
4152 	struct evutil_addrinfo hints;
4153 	char strport[NI_MAXSERV];
4154 	int ai_result;
4155 
4156 	memset(&hints, 0, sizeof(hints));
4157 	hints.ai_family = AF_UNSPEC;
4158 	hints.ai_socktype = SOCK_STREAM;
4159 	/* turn NULL hostname into INADDR_ANY, and skip looking up any address
4160 	 * types we don't have an interface to connect to. */
4161 	hints.ai_flags = EVUTIL_AI_PASSIVE|EVUTIL_AI_ADDRCONFIG;
4162 	evutil_snprintf(strport, sizeof(strport), "%d", port);
4163 	if ((ai_result = evutil_getaddrinfo(address, strport, &hints, &ai))
4164 	    != 0) {
4165 		if (ai_result == EVUTIL_EAI_SYSTEM)
4166 			event_warn("getaddrinfo");
4167 		else
4168 			event_warnx("getaddrinfo: %s",
4169 			    evutil_gai_strerror(ai_result));
4170 		return (NULL);
4171 	}
4172 
4173 	return (ai);
4174 }
4175 
4176 static evutil_socket_t
4177 bind_socket(const char *address, ev_uint16_t port, int reuse)
4178 {
4179 	evutil_socket_t fd;
4180 	struct evutil_addrinfo *aitop = NULL;
4181 
4182 	/* just create an unbound socket */
4183 	if (address == NULL && port == 0)
4184 		return bind_socket_ai(NULL, 0);
4185 
4186 	aitop = make_addrinfo(address, port);
4187 
4188 	if (aitop == NULL)
4189 		return (-1);
4190 
4191 	fd = bind_socket_ai(aitop, reuse);
4192 
4193 	evutil_freeaddrinfo(aitop);
4194 
4195 	return (fd);
4196 }
4197 
4198 struct evhttp_uri {
4199 	unsigned flags;
4200 	char *scheme; /* scheme; e.g http, ftp etc */
4201 	char *userinfo; /* userinfo (typically username:pass), or NULL */
4202 	char *host; /* hostname, IP address, or NULL */
4203 	int port; /* port, or zero */
4204 	char *path; /* path, or "". */
4205 	char *query; /* query, or NULL */
4206 	char *fragment; /* fragment or NULL */
4207 };
4208 
4209 struct evhttp_uri *
4210 evhttp_uri_new(void)
4211 {
4212 	struct evhttp_uri *uri = mm_calloc(sizeof(struct evhttp_uri), 1);
4213 	if (uri)
4214 		uri->port = -1;
4215 	return uri;
4216 }
4217 
4218 void
4219 evhttp_uri_set_flags(struct evhttp_uri *uri, unsigned flags)
4220 {
4221 	uri->flags = flags;
4222 }
4223 
4224 /* Return true if the string starting at s and ending immediately before eos
4225  * is a valid URI scheme according to RFC3986
4226  */
4227 static int
4228 scheme_ok(const char *s, const char *eos)
4229 {
4230 	/* scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) */
4231 	EVUTIL_ASSERT(eos >= s);
4232 	if (s == eos)
4233 		return 0;
4234 	if (!EVUTIL_ISALPHA_(*s))
4235 		return 0;
4236 	while (++s < eos) {
4237 		if (! EVUTIL_ISALNUM_(*s) &&
4238 		    *s != '+' && *s != '-' && *s != '.')
4239 			return 0;
4240 	}
4241 	return 1;
4242 }
4243 
4244 #define SUBDELIMS "!$&'()*+,;="
4245 
4246 /* Return true iff [s..eos) is a valid userinfo */
4247 static int
4248 userinfo_ok(const char *s, const char *eos)
4249 {
4250 	while (s < eos) {
4251 		if (CHAR_IS_UNRESERVED(*s) ||
4252 		    strchr(SUBDELIMS, *s) ||
4253 		    *s == ':')
4254 			++s;
4255 		else if (*s == '%' && s+2 < eos &&
4256 		    EVUTIL_ISXDIGIT_(s[1]) &&
4257 		    EVUTIL_ISXDIGIT_(s[2]))
4258 			s += 3;
4259 		else
4260 			return 0;
4261 	}
4262 	return 1;
4263 }
4264 
4265 static int
4266 regname_ok(const char *s, const char *eos)
4267 {
4268 	while (s && s<eos) {
4269 		if (CHAR_IS_UNRESERVED(*s) ||
4270 		    strchr(SUBDELIMS, *s))
4271 			++s;
4272 		else if (*s == '%' &&
4273 		    EVUTIL_ISXDIGIT_(s[1]) &&
4274 		    EVUTIL_ISXDIGIT_(s[2]))
4275 			s += 3;
4276 		else
4277 			return 0;
4278 	}
4279 	return 1;
4280 }
4281 
4282 static int
4283 parse_port(const char *s, const char *eos)
4284 {
4285 	int portnum = 0;
4286 	while (s < eos) {
4287 		if (! EVUTIL_ISDIGIT_(*s))
4288 			return -1;
4289 		portnum = (portnum * 10) + (*s - '0');
4290 		if (portnum < 0)
4291 			return -1;
4292 		++s;
4293 	}
4294 	return portnum;
4295 }
4296 
4297 /* returns 0 for bad, 1 for ipv6, 2 for IPvFuture */
4298 static int
4299 bracket_addr_ok(const char *s, const char *eos)
4300 {
4301 	if (s + 3 > eos || *s != '[' || *(eos-1) != ']')
4302 		return 0;
4303 	if (s[1] == 'v') {
4304 		/* IPvFuture, or junk.
4305 		   "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" )
4306 		 */
4307 		s += 2; /* skip [v */
4308 		--eos;
4309 		if (!EVUTIL_ISXDIGIT_(*s)) /*require at least one*/
4310 			return 0;
4311 		while (s < eos && *s != '.') {
4312 			if (EVUTIL_ISXDIGIT_(*s))
4313 				++s;
4314 			else
4315 				return 0;
4316 		}
4317 		if (*s != '.')
4318 			return 0;
4319 		++s;
4320 		while (s < eos) {
4321 			if (CHAR_IS_UNRESERVED(*s) ||
4322 			    strchr(SUBDELIMS, *s) ||
4323 			    *s == ':')
4324 				++s;
4325 			else
4326 				return 0;
4327 		}
4328 		return 2;
4329 	} else {
4330 		/* IPv6, or junk */
4331 		char buf[64];
4332 		ev_ssize_t n_chars = eos-s-2;
4333 		struct in6_addr in6;
4334 		if (n_chars >= 64) /* way too long */
4335 			return 0;
4336 		memcpy(buf, s+1, n_chars);
4337 		buf[n_chars]='\0';
4338 		return (evutil_inet_pton(AF_INET6,buf,&in6)==1) ? 1 : 0;
4339 	}
4340 }
4341 
4342 static int
4343 parse_authority(struct evhttp_uri *uri, char *s, char *eos)
4344 {
4345 	char *cp, *port;
4346 	EVUTIL_ASSERT(eos);
4347 	if (eos == s) {
4348 		uri->host = mm_strdup("");
4349 		if (uri->host == NULL) {
4350 			event_warn("%s: strdup", __func__);
4351 			return -1;
4352 		}
4353 		return 0;
4354 	}
4355 
4356 	/* Optionally, we start with "userinfo@" */
4357 
4358 	cp = strchr(s, '@');
4359 	if (cp && cp < eos) {
4360 		if (! userinfo_ok(s,cp))
4361 			return -1;
4362 		*cp++ = '\0';
4363 		uri->userinfo = mm_strdup(s);
4364 		if (uri->userinfo == NULL) {
4365 			event_warn("%s: strdup", __func__);
4366 			return -1;
4367 		}
4368 	} else {
4369 		cp = s;
4370 	}
4371 	/* Optionally, we end with ":port" */
4372 	for (port=eos-1; port >= cp && EVUTIL_ISDIGIT_(*port); --port)
4373 		;
4374 	if (port >= cp && *port == ':') {
4375 		if (port+1 == eos) /* Leave port unspecified; the RFC allows a
4376 				    * nil port */
4377 			uri->port = -1;
4378 		else if ((uri->port = parse_port(port+1, eos))<0)
4379 			return -1;
4380 		eos = port;
4381 	}
4382 	/* Now, cp..eos holds the "host" port, which can be an IPv4Address,
4383 	 * an IP-Literal, or a reg-name */
4384 	EVUTIL_ASSERT(eos >= cp);
4385 	if (*cp == '[' && eos >= cp+2 && *(eos-1) == ']') {
4386 		/* IPv6address, IP-Literal, or junk. */
4387 		if (! bracket_addr_ok(cp, eos))
4388 			return -1;
4389 	} else {
4390 		/* Make sure the host part is ok. */
4391 		if (! regname_ok(cp,eos)) /* Match IPv4Address or reg-name */
4392 			return -1;
4393 	}
4394 	uri->host = mm_malloc(eos-cp+1);
4395 	if (uri->host == NULL) {
4396 		event_warn("%s: malloc", __func__);
4397 		return -1;
4398 	}
4399 	memcpy(uri->host, cp, eos-cp);
4400 	uri->host[eos-cp] = '\0';
4401 	return 0;
4402 
4403 }
4404 
4405 static char *
4406 end_of_authority(char *cp)
4407 {
4408 	while (*cp) {
4409 		if (*cp == '?' || *cp == '#' || *cp == '/')
4410 			return cp;
4411 		++cp;
4412 	}
4413 	return cp;
4414 }
4415 
4416 enum uri_part {
4417 	PART_PATH,
4418 	PART_QUERY,
4419 	PART_FRAGMENT
4420 };
4421 
4422 /* Return the character after the longest prefix of 'cp' that matches...
4423  *   *pchar / "/" if allow_qchars is false, or
4424  *   *(pchar / "/" / "?") if allow_qchars is true.
4425  */
4426 static char *
4427 end_of_path(char *cp, enum uri_part part, unsigned flags)
4428 {
4429 	if (flags & EVHTTP_URI_NONCONFORMANT) {
4430 		/* If NONCONFORMANT:
4431 		 *   Path is everything up to a # or ? or nul.
4432 		 *   Query is everything up a # or nul
4433 		 *   Fragment is everything up to a nul.
4434 		 */
4435 		switch (part) {
4436 		case PART_PATH:
4437 			while (*cp && *cp != '#' && *cp != '?')
4438 				++cp;
4439 			break;
4440 		case PART_QUERY:
4441 			while (*cp && *cp != '#')
4442 				++cp;
4443 			break;
4444 		case PART_FRAGMENT:
4445 			cp += strlen(cp);
4446 			break;
4447 		};
4448 		return cp;
4449 	}
4450 
4451 	while (*cp) {
4452 		if (CHAR_IS_UNRESERVED(*cp) ||
4453 		    strchr(SUBDELIMS, *cp) ||
4454 		    *cp == ':' || *cp == '@' || *cp == '/')
4455 			++cp;
4456 		else if (*cp == '%' && EVUTIL_ISXDIGIT_(cp[1]) &&
4457 		    EVUTIL_ISXDIGIT_(cp[2]))
4458 			cp += 3;
4459 		else if (*cp == '?' && part != PART_PATH)
4460 			++cp;
4461 		else
4462 			return cp;
4463 	}
4464 	return cp;
4465 }
4466 
4467 static int
4468 path_matches_noscheme(const char *cp)
4469 {
4470 	while (*cp) {
4471 		if (*cp == ':')
4472 			return 0;
4473 		else if (*cp == '/')
4474 			return 1;
4475 		++cp;
4476 	}
4477 	return 1;
4478 }
4479 
4480 struct evhttp_uri *
4481 evhttp_uri_parse(const char *source_uri)
4482 {
4483 	return evhttp_uri_parse_with_flags(source_uri, 0);
4484 }
4485 
4486 struct evhttp_uri *
4487 evhttp_uri_parse_with_flags(const char *source_uri, unsigned flags)
4488 {
4489 	char *readbuf = NULL, *readp = NULL, *token = NULL, *query = NULL;
4490 	char *path = NULL, *fragment = NULL;
4491 	int got_authority = 0;
4492 
4493 	struct evhttp_uri *uri = mm_calloc(1, sizeof(struct evhttp_uri));
4494 	if (uri == NULL) {
4495 		event_warn("%s: calloc", __func__);
4496 		goto err;
4497 	}
4498 	uri->port = -1;
4499 	uri->flags = flags;
4500 
4501 	readbuf = mm_strdup(source_uri);
4502 	if (readbuf == NULL) {
4503 		event_warn("%s: strdup", __func__);
4504 		goto err;
4505 	}
4506 
4507 	readp = readbuf;
4508 	token = NULL;
4509 
4510 	/* We try to follow RFC3986 here as much as we can, and match
4511 	   the productions
4512 
4513 	      URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
4514 
4515 	      relative-ref  = relative-part [ "?" query ] [ "#" fragment ]
4516 	 */
4517 
4518 	/* 1. scheme: */
4519 	token = strchr(readp, ':');
4520 	if (token && scheme_ok(readp,token)) {
4521 		*token = '\0';
4522 		uri->scheme = mm_strdup(readp);
4523 		if (uri->scheme == NULL) {
4524 			event_warn("%s: strdup", __func__);
4525 			goto err;
4526 		}
4527 		readp = token+1; /* eat : */
4528 	}
4529 
4530 	/* 2. Optionally, "//" then an 'authority' part. */
4531 	if (readp[0]=='/' && readp[1] == '/') {
4532 		char *authority;
4533 		readp += 2;
4534 		authority = readp;
4535 		path = end_of_authority(readp);
4536 		if (parse_authority(uri, authority, path) < 0)
4537 			goto err;
4538 		readp = path;
4539 		got_authority = 1;
4540 	}
4541 
4542 	/* 3. Query: path-abempty, path-absolute, path-rootless, or path-empty
4543 	 */
4544 	path = readp;
4545 	readp = end_of_path(path, PART_PATH, flags);
4546 
4547 	/* Query */
4548 	if (*readp == '?') {
4549 		*readp = '\0';
4550 		++readp;
4551 		query = readp;
4552 		readp = end_of_path(readp, PART_QUERY, flags);
4553 	}
4554 	/* fragment */
4555 	if (*readp == '#') {
4556 		*readp = '\0';
4557 		++readp;
4558 		fragment = readp;
4559 		readp = end_of_path(readp, PART_FRAGMENT, flags);
4560 	}
4561 	if (*readp != '\0') {
4562 		goto err;
4563 	}
4564 
4565 	/* These next two cases may be unreachable; I'm leaving them
4566 	 * in to be defensive. */
4567 	/* If you didn't get an authority, the path can't begin with "//" */
4568 	if (!got_authority && path[0]=='/' && path[1]=='/')
4569 		goto err;
4570 	/* If you did get an authority, the path must begin with "/" or be
4571 	 * empty. */
4572 	if (got_authority && path[0] != '/' && path[0] != '\0')
4573 		goto err;
4574 	/* (End of maybe-unreachable cases) */
4575 
4576 	/* If there was no scheme, the first part of the path (if any) must
4577 	 * have no colon in it. */
4578 	if (! uri->scheme && !path_matches_noscheme(path))
4579 		goto err;
4580 
4581 	EVUTIL_ASSERT(path);
4582 	uri->path = mm_strdup(path);
4583 	if (uri->path == NULL) {
4584 		event_warn("%s: strdup", __func__);
4585 		goto err;
4586 	}
4587 
4588 	if (query) {
4589 		uri->query = mm_strdup(query);
4590 		if (uri->query == NULL) {
4591 			event_warn("%s: strdup", __func__);
4592 			goto err;
4593 		}
4594 	}
4595 	if (fragment) {
4596 		uri->fragment = mm_strdup(fragment);
4597 		if (uri->fragment == NULL) {
4598 			event_warn("%s: strdup", __func__);
4599 			goto err;
4600 		}
4601 	}
4602 
4603 	mm_free(readbuf);
4604 
4605 	return uri;
4606 err:
4607 	if (uri)
4608 		evhttp_uri_free(uri);
4609 	if (readbuf)
4610 		mm_free(readbuf);
4611 	return NULL;
4612 }
4613 
4614 void
4615 evhttp_uri_free(struct evhttp_uri *uri)
4616 {
4617 #define URI_FREE_STR_(f)		\
4618 	if (uri->f) {			\
4619 		mm_free(uri->f);		\
4620 	}
4621 
4622 	URI_FREE_STR_(scheme);
4623 	URI_FREE_STR_(userinfo);
4624 	URI_FREE_STR_(host);
4625 	URI_FREE_STR_(path);
4626 	URI_FREE_STR_(query);
4627 	URI_FREE_STR_(fragment);
4628 
4629 	mm_free(uri);
4630 #undef URI_FREE_STR_
4631 }
4632 
4633 char *
4634 evhttp_uri_join(struct evhttp_uri *uri, char *buf, size_t limit)
4635 {
4636 	struct evbuffer *tmp = 0;
4637 	size_t joined_size = 0;
4638 	char *output = NULL;
4639 
4640 #define URI_ADD_(f)	evbuffer_add(tmp, uri->f, strlen(uri->f))
4641 
4642 	if (!uri || !buf || !limit)
4643 		return NULL;
4644 
4645 	tmp = evbuffer_new();
4646 	if (!tmp)
4647 		return NULL;
4648 
4649 	if (uri->scheme) {
4650 		URI_ADD_(scheme);
4651 		evbuffer_add(tmp, ":", 1);
4652 	}
4653 	if (uri->host) {
4654 		evbuffer_add(tmp, "//", 2);
4655 		if (uri->userinfo)
4656 			evbuffer_add_printf(tmp,"%s@", uri->userinfo);
4657 		URI_ADD_(host);
4658 		if (uri->port >= 0)
4659 			evbuffer_add_printf(tmp,":%d", uri->port);
4660 
4661 		if (uri->path && uri->path[0] != '/' && uri->path[0] != '\0')
4662 			goto err;
4663 	}
4664 
4665 	if (uri->path)
4666 		URI_ADD_(path);
4667 
4668 	if (uri->query) {
4669 		evbuffer_add(tmp, "?", 1);
4670 		URI_ADD_(query);
4671 	}
4672 
4673 	if (uri->fragment) {
4674 		evbuffer_add(tmp, "#", 1);
4675 		URI_ADD_(fragment);
4676 	}
4677 
4678 	evbuffer_add(tmp, "\0", 1); /* NUL */
4679 
4680 	joined_size = evbuffer_get_length(tmp);
4681 
4682 	if (joined_size > limit) {
4683 		/* It doesn't fit. */
4684 		evbuffer_free(tmp);
4685 		return NULL;
4686 	}
4687        	evbuffer_remove(tmp, buf, joined_size);
4688 
4689 	output = buf;
4690 err:
4691 	evbuffer_free(tmp);
4692 
4693 	return output;
4694 #undef URI_ADD_
4695 }
4696 
4697 const char *
4698 evhttp_uri_get_scheme(const struct evhttp_uri *uri)
4699 {
4700 	return uri->scheme;
4701 }
4702 const char *
4703 evhttp_uri_get_userinfo(const struct evhttp_uri *uri)
4704 {
4705 	return uri->userinfo;
4706 }
4707 const char *
4708 evhttp_uri_get_host(const struct evhttp_uri *uri)
4709 {
4710 	return uri->host;
4711 }
4712 int
4713 evhttp_uri_get_port(const struct evhttp_uri *uri)
4714 {
4715 	return uri->port;
4716 }
4717 const char *
4718 evhttp_uri_get_path(const struct evhttp_uri *uri)
4719 {
4720 	return uri->path;
4721 }
4722 const char *
4723 evhttp_uri_get_query(const struct evhttp_uri *uri)
4724 {
4725 	return uri->query;
4726 }
4727 const char *
4728 evhttp_uri_get_fragment(const struct evhttp_uri *uri)
4729 {
4730 	return uri->fragment;
4731 }
4732 
4733 #define URI_SET_STR_(f) do {					\
4734 	if (uri->f)						\
4735 		mm_free(uri->f);				\
4736 	if (f) {						\
4737 		if ((uri->f = mm_strdup(f)) == NULL) {		\
4738 			event_warn("%s: strdup()", __func__);	\
4739 			return -1;				\
4740 		}						\
4741 	} else {						\
4742 		uri->f = NULL;					\
4743 	}							\
4744 	} while(0)
4745 
4746 int
4747 evhttp_uri_set_scheme(struct evhttp_uri *uri, const char *scheme)
4748 {
4749 	if (scheme && !scheme_ok(scheme, scheme+strlen(scheme)))
4750 		return -1;
4751 
4752 	URI_SET_STR_(scheme);
4753 	return 0;
4754 }
4755 int
4756 evhttp_uri_set_userinfo(struct evhttp_uri *uri, const char *userinfo)
4757 {
4758 	if (userinfo && !userinfo_ok(userinfo, userinfo+strlen(userinfo)))
4759 		return -1;
4760 	URI_SET_STR_(userinfo);
4761 	return 0;
4762 }
4763 int
4764 evhttp_uri_set_host(struct evhttp_uri *uri, const char *host)
4765 {
4766 	if (host) {
4767 		if (host[0] == '[') {
4768 			if (! bracket_addr_ok(host, host+strlen(host)))
4769 				return -1;
4770 		} else {
4771 			if (! regname_ok(host, host+strlen(host)))
4772 				return -1;
4773 		}
4774 	}
4775 
4776 	URI_SET_STR_(host);
4777 	return 0;
4778 }
4779 int
4780 evhttp_uri_set_port(struct evhttp_uri *uri, int port)
4781 {
4782 	if (port < -1)
4783 		return -1;
4784 	uri->port = port;
4785 	return 0;
4786 }
4787 #define end_of_cpath(cp,p,f) \
4788 	((const char*)(end_of_path(((char*)(cp)), (p), (f))))
4789 
4790 int
4791 evhttp_uri_set_path(struct evhttp_uri *uri, const char *path)
4792 {
4793 	if (path && end_of_cpath(path, PART_PATH, uri->flags) != path+strlen(path))
4794 		return -1;
4795 
4796 	URI_SET_STR_(path);
4797 	return 0;
4798 }
4799 int
4800 evhttp_uri_set_query(struct evhttp_uri *uri, const char *query)
4801 {
4802 	if (query && end_of_cpath(query, PART_QUERY, uri->flags) != query+strlen(query))
4803 		return -1;
4804 	URI_SET_STR_(query);
4805 	return 0;
4806 }
4807 int
4808 evhttp_uri_set_fragment(struct evhttp_uri *uri, const char *fragment)
4809 {
4810 	if (fragment && end_of_cpath(fragment, PART_FRAGMENT, uri->flags) != fragment+strlen(fragment))
4811 		return -1;
4812 	URI_SET_STR_(fragment);
4813 	return 0;
4814 }
4815