Bring validate_host/1 from original
Project: http://git-wip-us.apache.org/repos/asf/couchdb-couch-httpd/repo Commit: http://git-wip-us.apache.org/repos/asf/couchdb-couch-httpd/commit/63be3e75 Tree: http://git-wip-us.apache.org/repos/asf/couchdb-couch-httpd/tree/63be3e75 Diff: http://git-wip-us.apache.org/repos/asf/couchdb-couch-httpd/diff/63be3e75 Branch: refs/heads/split_out_httpd_stack Commit: 63be3e75e316521d252ec99b6b358d8f1732c08a Parents: 38073bb Author: ILYA Khlopotov <iil...@ca.ibm.com> Authored: Thu Feb 25 11:55:20 2016 -0800 Committer: ILYA Khlopotov <iil...@ca.ibm.com> Committed: Tue Mar 1 08:35:07 2016 -0800 ---------------------------------------------------------------------- src/couch_httpd.erl | 32 +++++++++++++++++++++++++++++++- src/couch_httpd_original.erl | 31 ++----------------------------- 2 files changed, 33 insertions(+), 30 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/couchdb-couch-httpd/blob/63be3e75/src/couch_httpd.erl ---------------------------------------------------------------------- diff --git a/src/couch_httpd.erl b/src/couch_httpd.erl index aff29fe..55f46b7 100644 --- a/src/couch_httpd.erl +++ b/src/couch_httpd.erl @@ -86,7 +86,8 @@ ]). -export([ - validate_ctype/2 + validate_ctype/2, + validate_host/1 ]). -export([ @@ -417,6 +418,21 @@ validate_ctype(Req, Ctype) -> end end. +validate_host(#httpd{} = Req) -> + case config:get_boolean("httpd", "validate_host", false) of + true -> + Host = hostname(Req), + ValidHosts = valid_hosts(), + case lists:member(Host, ValidHosts) of + true -> + ok; + false -> + throw({bad_request, <<"Invalid host header">>}) + end; + false -> + ok + end. + host_for_request(#httpd{mochi_req = MochiReq}) -> XHost = config:get("httpd", "x_forwarded_host", "X-Forwarded-Host"), case MochiReq:get_header_value(XHost) of @@ -780,6 +796,20 @@ maybe_decompress(Httpd, Body) -> throw({bad_ctype, [Else, " is not a supported content encoding."]}) end. +hostname(#httpd{} = Req) -> + case header_value(Req, "Host") of + undefined -> + undefined; + Host -> + [Name | _] = re:split(Host, ":[0-9]+$", [{parts, 2}, {return, list}]), + Name + end. + +valid_hosts() -> + List = config:get("httpd", "valid_hosts", ""), + re:split(List, ",", [{return, list}]). + + log_error_with_stack_trace({bad_request, _, _}) -> ok; log_error_with_stack_trace({Error, Reason, Stack}) -> http://git-wip-us.apache.org/repos/asf/couchdb-couch-httpd/blob/63be3e75/src/couch_httpd_original.erl ---------------------------------------------------------------------- diff --git a/src/couch_httpd_original.erl b/src/couch_httpd_original.erl index 29f9845..e3b84bf 100644 --- a/src/couch_httpd_original.erl +++ b/src/couch_httpd_original.erl @@ -23,7 +23,6 @@ -export([send_error/2,send_error/4, send_chunked_error/2]). -export([accepted_encodings/1,handle_request_int/5,validate_referer/1]). --export([validate_host/1]). -export([validate_bind_address/1]). -import(couch_httpd, [ @@ -70,7 +69,8 @@ send_chunk/2, etag_maybe/2, send_response/4, - start_chunked_response/3 + start_chunked_response/3, + validate_host/1 ]). -define(HANDLER_NAME_IN_MODULE_POS, 6). @@ -386,33 +386,6 @@ handle_request_int(MochiReq, DefaultFun, couch_stats:increment_counter([couchdb, httpd, requests]), {ok, Resp}. -validate_host(#httpd{} = Req) -> - case config:get_boolean("httpd", "validate_host", false) of - true -> - Host = hostname(Req), - ValidHosts = valid_hosts(), - case lists:member(Host, ValidHosts) of - true -> - ok; - false -> - throw({bad_request, <<"Invalid host header">>}) - end; - false -> - ok - end. - -hostname(#httpd{} = Req) -> - case header_value(Req, "Host") of - undefined -> - undefined; - Host -> - [Name | _] = re:split(Host, ":[0-9]+$", [{parts, 2}, {return, list}]), - Name - end. - -valid_hosts() -> - List = config:get("httpd", "valid_hosts", ""), - re:split(List, ",", [{return, list}]). check_request_uri_length(Uri) -> check_request_uri_length(Uri, config:get("httpd", "max_uri_length")).