[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [Libevent-users] [PATCH] Add Host header in HTTP/1.1 requests if it's missing.



On Fri, Aug 19, 2011 at 03:14:12PM +0400, Leonid Evdokimov wrote:
> From a87fc928f7a61ca691861154ffd9ba3e6aec0397 Mon Sep 17 00:00:00 2001
> From: Leonid Evdokimov <darkk@xxxxxxxxxxxxxx>
> Date: Fri, 19 Aug 2011 14:47:20 +0400
> Subject: [PATCH 6/6] Add Host header in HTTP/1.1 requests if it's missing.
> 
> ---
>  http.c |   12 ++++++++++++
>  1 files changed, 12 insertions(+), 0 deletions(-)
> 
> diff --git a/http.c b/http.c
> index 58fef16..823bf19 100644
> --- a/http.c
> +++ b/http.c
> @@ -456,6 +456,18 @@ evhttp_make_header_request(struct evhttp_connection *evcon,
>  	    "%s %s HTTP/%d.%d\r\n",
>  	    method, req->uri, req->major, req->minor);
>  
> +	if (req->major == 1 && req->minor >= 1 &&
> +	    evhttp_find_header(req->output_headers, "Host") == NULL) {
> +		if (evcon->port == 80) {
> +			evhttp_add_header(req->output_headers, "Host", evcon->address);
> +		}
> +		else {
> +			char host[255 + 1 + 5 + 1];
> +			evutil_snprintf(host, sizeof(host), "%s:%u", evcon->address, evcon->port);
> +			evhttp_add_header(req->output_headers, "Host", host);
> +		}
> +	}
> +
>  	/* Add the content length on a post or put request if missing */
>  	if ((req->type == EVHTTP_REQ_POST || req->type == EVHTTP_REQ_PUT) &&
>  	    evhttp_find_header(req->output_headers, "Content-Length") == NULL){
> -- 
> 1.7.4.1
> 


RFC states that the Host header *MUST* be included if it hasn't already
been specified in the URI (e.g., GET http://www.blah.com/ HTTP/1.1).
Otherwise the host value must be empty:

"A client MUST include a Host header field in all HTTP/1.1 request
 messages . If the requested URI does not include an Internet host
 name for the service being requested, then the Host header field MUST
 be given with an empty value."

Just kinda something to think about.
***********************************************************************
To unsubscribe, send an e-mail to majordomo@xxxxxxxxxxxxx with
unsubscribe libevent-users    in the body.