Skip to content

Commit

Permalink
Rewrite request host with absolute uri
Browse files Browse the repository at this point in the history
Behaviour changed accordingly with new RFC.

RFC 9112 Section 3.2.2:

"When a proxy receives a request with an absolute-form of
request-target, the proxy MUST ignore the received Host header field
(if any) and instead replace it with the host information of the
request-target. A proxy that forwards such a request MUST generate a
new Host field value based on the received request-target rather than
forward the received Host field value"
  • Loading branch information
const-t committed Aug 16, 2023
1 parent da00619 commit cf92616
Showing 1 changed file with 48 additions and 2 deletions.
50 changes: 48 additions & 2 deletions fw/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -3423,6 +3423,43 @@ tfw_h1_add_loc_hdrs(TfwHttpMsg *hm, const TfwHdrMods *h_mods, bool from_cache)
return r;
}

/**
* Add Host header to request.
*
* RFC 9112 section 3.2.2
*
* When a proxy receives a request with an absolute-form of
* request-target, the proxy MUST ignore the received Host header field
* if any) and instead replace it with the host information of the
* request-target. A proxy that forwards such a request MUST generate
* a new Host field value based on the received request-target rather
* than forward the received Host field value.
*/
static int
tfw_http_add_hdr_host(TfwHttpReq *req)
{
int r;
TfwHttpMsg *hm = (TfwHttpMsg *)req;
static const DEFINE_TFW_STR(host_n, "Host: ");
static const DEFINE_TFW_STR(crlf, S_CRLF);
TfwStr *host = &req->h_tbl->tbl[TFW_HTTP_HDR_HOST];

if (test_bit(TFW_HTTP_B_ABSOLUTE_URI, req->flags)) {
r = tfw_http_msg_expand_from_pool(hm, &host_n);
if (unlikely(r))
return r;
r = tfw_http_msg_expand_from_pool(hm, &req->host);
if (unlikely(r))
return r;
} else {
r = tfw_http_msg_expand_from_pool(hm, host);
if (unlikely(r))
return r;
}

return tfw_http_msg_expand_from_pool(hm, &crlf);
}

/**
* Adjust the request before proxying it to real server.
*/
Expand All @@ -3434,6 +3471,7 @@ tfw_h1_adjust_req(TfwHttpReq *req)
TfwHttpMsgCleanup *cleanup = req->cleanup;
const BasicStr *s_meth;
TfwStr *pos, *end, meth = {};
static const DEFINE_TFW_STR(slash, "/");
static const DEFINE_TFW_STR(sp, " ");
static const DEFINE_TFW_STR(crlf, S_CRLF);
static const DEFINE_TFW_STR(ver, " " S_VERSION11 S_CRLF);
Expand Down Expand Up @@ -3466,15 +3504,23 @@ tfw_h1_adjust_req(TfwHttpReq *req)
if (unlikely(r))
goto clean;

r = tfw_http_msg_expand_from_pool(hm, &req->uri_path);
if (TFW_STR_EMPTY(&req->uri_path))
r = tfw_http_msg_expand_from_pool(hm, &slash);
else
r = tfw_http_msg_expand_from_pool(hm, &req->uri_path);

if (unlikely(r))
goto clean;

r = tfw_http_msg_expand_from_pool(hm, &ver);
if (unlikely(r))
goto clean;

FOR_EACH_HDR_FIELD_FROM(pos, end, hm, TFW_HTTP_HDR_HOST) {
r = tfw_http_add_hdr_host(req);
if (unlikely(r))
goto clean;

FOR_EACH_HDR_FIELD_FROM(pos, end, hm, TFW_HTTP_HDR_CONTENT_LENGTH) {
int hid = pos - hm->h_tbl->tbl;
TfwStr *dup, *dup_end, *hdr = pos;

Expand Down

0 comments on commit cf92616

Please sign in to comment.