Skip to content

Commit

Permalink
lib-http: server: Properly handle empty Host header by providing a de…
Browse files Browse the repository at this point in the history
…fault authority.

This behavior is required by the RFC.
  • Loading branch information
stephanbosch committed Jul 2, 2018
1 parent aea9833 commit 3d8a216
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/lib-http/http-server-connection.c
Expand Up @@ -4,6 +4,7 @@
#include "llist.h"
#include "array.h"
#include "str.h"
#include "hostpid.h"
#include "ioloop.h"
#include "istream.h"
#include "istream-timeout.h"
Expand All @@ -14,6 +15,7 @@
#include "master-service.h"
#include "master-service-ssl.h"
#include "http-date.h"
#include "http-url.h"
#include "http-request-parser.h"

#include "http-server-private.h"
Expand Down Expand Up @@ -160,6 +162,8 @@ bool http_server_connection_shut_down(struct http_server_connection *conn)

static void http_server_connection_ready(struct http_server_connection *conn)
{
const struct http_server_settings *set = &conn->server->set;
struct http_url base_url;
struct stat st;

if (conn->server->set.rawlog_dir != NULL &&
Expand All @@ -168,8 +172,18 @@ static void http_server_connection_ready(struct http_server_connection *conn)
&conn->conn.input, &conn->conn.output);
}

i_zero(&base_url);
if (set->default_host != NULL)
base_url.host.name = set->default_host;
else if (conn->ip.family != 0)
base_url.host.ip = conn->ip;
else
base_url.host.name = my_hostname;
base_url.port = conn->port;
base_url.have_ssl = conn->ssl;

conn->http_parser = http_request_parser_init(
conn->conn.input, NULL, &conn->server->set.request_limits,
conn->conn.input, &base_url, &conn->server->set.request_limits,
HTTP_REQUEST_PARSE_FLAG_STRICT);
o_stream_set_flush_callback(conn->conn.output,
http_server_connection_output, conn);
Expand Down
3 changes: 3 additions & 0 deletions src/lib-http/http-server.c
Expand Up @@ -28,6 +28,9 @@ struct http_server *http_server_init(const struct http_server_settings *set)
pool = pool_alloconly_create("http server", 1024);
server = p_new(pool, struct http_server, 1);
server->pool = pool;

if (set->default_host != NULL && *set->default_host != '\0')
server->set.default_host = p_strdup(pool, set->default_host);
if (set->rawlog_dir != NULL && *set->rawlog_dir != '\0')
server->set.rawlog_dir = p_strdup(pool, set->rawlog_dir);
if (set->ssl != NULL) {
Expand Down
2 changes: 2 additions & 0 deletions src/lib-http/http-server.h
Expand Up @@ -19,6 +19,8 @@ struct http_server_response;
*/

struct http_server_settings {
const char *default_host;

const char *rawlog_dir;

/* SSL settings; if NULL, master_service_ssl_init() is used instead */
Expand Down

0 comments on commit 3d8a216

Please sign in to comment.