Skip to content

Commit

Permalink
Rework HTTP1 request adjusting (Part 2/2)
Browse files Browse the repository at this point in the history
`tfw_http_msg_expand_from_pool` splitted into two functions
first `tfw_http_msg_expand_from_pool` for HTTP1 messages
and second `tfw_h2_msg_expand_from_pool` for HTTP2 messages.
  • Loading branch information
const-t committed Aug 16, 2023
1 parent 256a5ab commit da00619
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 77 deletions.
13 changes: 6 additions & 7 deletions fw/cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ tfw_cache_h2_write(TDB *db, TdbVRec **trec, TfwHttpResp *resp, char **data,
TfwStr c = { 0 };
TdbVRec *tr = *trec;
TfwHttpTransIter *mit = &resp->mit;
TfwMsgIter *it = &mit->iter;
TfwMsgIter *it = &resp->iter;
int r = 0, copied = 0;

while (1) {
Expand Down Expand Up @@ -713,7 +713,7 @@ tfw_cache_set_status(TDB *db, TfwCacheEntry *ce, TfwHttpResp *resp,
TdbVRec **trec, char **p, unsigned long *acc_len)
{
int r;
TfwMsgIter *it = &resp->mit.iter;
TfwMsgIter *it = &resp->iter;
struct sk_buff **skb_head = &resp->msg.skb_head;
bool h2_mode = TFW_MSG_H2(resp->req);
TfwDecodeCacheIter dc_iter = {};
Expand Down Expand Up @@ -906,7 +906,7 @@ tfw_cache_send_304(TfwHttpReq *req, TfwCacheEntry *ce)
if (!(resp = tfw_http_msg_alloc_resp_light(req)))
goto err_create;

it = &resp->mit.iter;
it = &resp->iter;
skb_head = &resp->msg.skb_head;

if (!TFW_MSG_H2(req)) {
Expand Down Expand Up @@ -2628,7 +2628,6 @@ tfw_cache_set_hdr_age(TfwHttpResp *resp, TfwCacheEntry *ce)
int r;
size_t digs;
bool to_h2 = TFW_MSG_H2(resp->req);
TfwHttpTransIter *mit = &resp->mit;
struct sk_buff **skb_head = &resp->msg.skb_head;
long age = tfw_cache_entry_age(ce);
char cstr_age[TFW_ULTOA_BUF_SIZ] = {0};
Expand Down Expand Up @@ -2657,11 +2656,11 @@ tfw_cache_set_hdr_age(TfwHttpResp *resp, TfwCacheEntry *ce)
if ((r = tfw_hpack_encode(resp, &h_age, false, false)))
goto err;
} else {
if ((r = tfw_http_msg_expand_data(&mit->iter, skb_head,
if ((r = tfw_http_msg_expand_data(&resp->iter, skb_head,
&h_age, NULL)))
goto err;

if ((r = tfw_http_msg_expand_data(&mit->iter, skb_head,
if ((r = tfw_http_msg_expand_data(&resp->iter, skb_head,
&g_crlf, NULL)))
goto err;
}
Expand Down Expand Up @@ -2750,7 +2749,7 @@ tfw_cache_build_resp(TfwHttpReq *req, TfwCacheEntry *ce, long lifetime,
mit = &resp->mit;
skb_head = &resp->msg.skb_head;
WARN_ON_ONCE(mit->acc_len);
it = &mit->iter;
it = &resp->iter;

/*
* Set 'set-cookie' header if needed, for HTTP/2 or HTTP/1.1
Expand Down
31 changes: 16 additions & 15 deletions fw/hpack.c
Original file line number Diff line number Diff line change
Expand Up @@ -1773,8 +1773,7 @@ tfw_hpack_cache_decode_expand(TfwHPack *__restrict hp,
unsigned int state;
int r = T_OK;
TfwStr exp_str = {};
TfwHttpTransIter *mit = &resp->mit;
TfwMsgIter *it = &mit->iter;
TfwMsgIter *it = &resp->iter;
const unsigned char *last = src + n;
unsigned char *prev = src;
struct sk_buff **skb_head = &resp->msg.skb_head;
Expand Down Expand Up @@ -3366,7 +3365,7 @@ tfw_hpack_write_idx(TfwHttpResp *__restrict resp, TfwHPackInt *__restrict idx,
bool use_pool)
{
TfwHttpTransIter *mit = &resp->mit;
TfwMsgIter *iter = &mit->iter;
TfwMsgIter *iter = &resp->iter;
struct sk_buff **skb_head = &resp->msg.skb_head;
const TfwStr s_idx = {
.data = idx->buf,
Expand All @@ -3378,8 +3377,8 @@ tfw_hpack_write_idx(TfwHttpResp *__restrict resp, TfwHPackInt *__restrict idx,
s_idx.data);

if (use_pool)
return tfw_http_msg_expand_from_pool((TfwHttpMsg *)resp,
&s_idx);
return tfw_h2_msg_expand_from_pool((TfwHttpMsg *)resp,
&s_idx, &resp->mit);

return tfw_http_msg_expand_data(iter, skb_head, &s_idx,
&mit->start_off);
Expand Down Expand Up @@ -3419,14 +3418,16 @@ tfw_hpack_hdr_add(TfwHttpResp *__restrict resp, TfwStr *__restrict hdr,
s_nlen.data = nlen.buf;
s_nlen.len = nlen.sz;

r = tfw_http_msg_expand_from_pool(msg, &s_nlen);
r = tfw_h2_msg_expand_from_pool(msg, &s_nlen, &resp->mit);
if (unlikely(r))
return r;

if (trans)
r = tfw_http_msg_expand_from_pool_lc(msg, &s_name);
r = tfw_h2_msg_expand_from_pool_lc(msg, &s_name,
&resp->mit);
else
r = tfw_http_msg_expand_from_pool(msg, &s_name);
r = tfw_h2_msg_expand_from_pool(msg, &s_name,
&resp->mit);
if (unlikely(r))
return r;
}
Expand All @@ -3435,12 +3436,12 @@ tfw_hpack_hdr_add(TfwHttpResp *__restrict resp, TfwStr *__restrict hdr,
s_vlen.data = vlen.buf;
s_vlen.len = vlen.sz;

r = tfw_http_msg_expand_from_pool(msg, &s_vlen);
r = tfw_h2_msg_expand_from_pool(msg, &s_vlen, &resp->mit);

if (unlikely(r))
return r;
if (!TFW_STR_EMPTY(&s_val))
r = tfw_http_msg_expand_from_pool(msg, &s_val);
r = tfw_h2_msg_expand_from_pool(msg, &s_val, &resp->mit);

return r;
}
Expand All @@ -3456,7 +3457,7 @@ tfw_hpack_hdr_expand(TfwHttpResp *__restrict resp, TfwStr *__restrict hdr,
int ret;
TfwStr *c, *end;
TfwHttpTransIter *mit = &resp->mit;
TfwMsgIter *iter = &mit->iter;
TfwMsgIter *iter = &resp->iter;
struct sk_buff **skb_head = &resp->msg.skb_head;
TfwStr s_val;

Expand Down Expand Up @@ -3530,7 +3531,7 @@ __tfw_hpack_encode(TfwHttpResp *__restrict resp, TfwStr *__restrict hdr,
TfwHPackETbl *tbl = &ctx->hpack.enc_tbl;
int r = HPACK_IDX_ST_NOT_FOUND;
bool name_indexed = true;
struct sk_buff *skb = resp->mit.iter.skb;
struct sk_buff *skb = resp->iter.skb;

if (WARN_ON_ONCE(!hdr || TFW_STR_EMPTY(hdr)))
return -EINVAL;
Expand Down Expand Up @@ -3607,14 +3608,14 @@ __tfw_hpack_encode(TfwHttpResp *__restrict resp, TfwStr *__restrict hdr,
* Very long headers can be located in several skbs,
* mark them all.
*/
while(skb && unlikely(skb != resp->mit.iter.skb)) {
while(skb && unlikely(skb != resp->iter.skb)) {
skb_set_tfw_flags(skb, SS_F_HTTT2_FRAME_HEADERS);
skb_set_tfw_cb(skb, resp->req->stream->id);
skb = skb->next;
}

skb_set_tfw_flags(resp->mit.iter.skb, SS_F_HTTT2_FRAME_HEADERS);
skb_set_tfw_cb(resp->mit.iter.skb, resp->req->stream->id);
skb_set_tfw_flags(resp->iter.skb, SS_F_HTTT2_FRAME_HEADERS);
skb_set_tfw_cb(resp->iter.skb, resp->req->stream->id);
}
return r;
}
Expand Down
40 changes: 18 additions & 22 deletions fw/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ tfw_h2_prep_resp(TfwHttpResp *resp, unsigned short status, TfwStr *msg,
__TFW_STR_CH(&hdr, 0)->len = name->len - SLEN(S_CRLF) - 2;

if (__TFW_STR_CH(msg, i + 1)->nchunks) {
TfwMsgIter *iter = &mit->iter;
TfwMsgIter *iter = &resp->iter;
struct sk_buff **skb_head = &resp->msg.skb_head;
TfwHPackInt vlen;
TfwStr s_vlen = {};
Expand Down Expand Up @@ -647,7 +647,7 @@ tfw_h2_prep_resp(TfwHttpResp *resp, unsigned short status, TfwStr *msg,
* Responses built locally has room for frame header reserved
* in SKB linear data.
*/
mit->frame_head = mit->iter.skb->data;
mit->frame_head = resp->iter.skb->data;

hdrs_len += mit->acc_len;

Expand Down Expand Up @@ -2930,7 +2930,6 @@ int
tfw_http_expand_hbh(TfwHttpResp *resp, unsigned short status)
{
TfwHttpReq *req = resp->req;
TfwHttpTransIter *mit = &resp->mit;
struct sk_buff **skb_head = &resp->msg.skb_head;
bool proxy_close = test_bit(TFW_HTTP_B_CONN_CLOSE, resp->flags)
&& (status / 100 == 4);
Expand Down Expand Up @@ -2968,7 +2967,7 @@ tfw_http_expand_hbh(TfwHttpResp *resp, unsigned short status)
tfw_http_req_set_conn_close(req);

return add_h_conn
? tfw_http_msg_expand_data(&mit->iter, skb_head, &h_conn, NULL)
? tfw_http_msg_expand_data(&resp->iter, skb_head, &h_conn, NULL)
: 0;
}

Expand Down Expand Up @@ -3027,7 +3026,6 @@ tfw_http_expand_stale_warn(TfwHttpResp *resp)
{
/* TODO: adjust for #865 */
struct sk_buff **skb_head = &resp->msg.skb_head;
TfwHttpTransIter *mit = &resp->mit;
TfwStr wh = {
.chunks = (TfwStr []){
{ .data = S_WARN, .len = SLEN(S_WARN) },
Expand All @@ -3039,15 +3037,14 @@ tfw_http_expand_stale_warn(TfwHttpResp *resp)
.nchunks = 4,
};

return tfw_http_msg_expand_data(&mit->iter, skb_head, &wh, NULL);
return tfw_http_msg_expand_data(&resp->iter, skb_head, &wh, NULL);
}

static inline int
__tfw_http_add_hdr_date(TfwHttpResp *resp, bool cache)
{
int r;
struct sk_buff **skb_head = &resp->msg.skb_head;
TfwHttpTransIter *mit = &resp->mit;
char *date = *this_cpu_ptr(&g_buf);
TfwStr h_date = {
.chunks = (TfwStr []) {
Expand All @@ -3064,7 +3061,7 @@ __tfw_http_add_hdr_date(TfwHttpResp *resp, bool cache)
if (!cache)
r = tfw_http_msg_expand_from_pool((TfwHttpMsg *)resp, &h_date);
else
r = tfw_http_msg_expand_data(&mit->iter, skb_head, &h_date,
r = tfw_http_msg_expand_data(&resp->iter, skb_head, &h_date,
NULL);

if (unlikely(r))
Expand Down Expand Up @@ -3098,14 +3095,13 @@ __tfw_http_add_hdr_server(TfwHttpResp *resp, bool cache)
{
int r;
struct sk_buff **skb_head = &resp->msg.skb_head;
TfwHttpTransIter *mit = &resp->mit;
static char s_server[] = S_F_SERVER TFW_NAME "/" TFW_VERSION S_CRLF;
TfwStr hdr = { .data = s_server, .len = SLEN(s_server) };

if (!cache)
r = tfw_http_msg_expand_from_pool((TfwHttpMsg *)resp, &hdr);
else
r = tfw_http_msg_expand_data(&mit->iter, skb_head, &hdr, NULL);
r = tfw_http_msg_expand_data(&resp->iter, skb_head, &hdr, NULL);

if (unlikely(r))
T_ERR("Unable to add Server: header to resp [%p]\n", resp);
Expand Down Expand Up @@ -3163,13 +3159,13 @@ __tfw_http_add_hdr_via(TfwHttpMsg *hm, int http_version, bool from_cache)
&TFW_STR_STRING(S_CRLF));
} else {
struct sk_buff **skb_head = &hm->msg.skb_head;
TfwHttpTransIter *mit = &((TfwHttpResp *)hm)->mit;
TfwMsgIter *it = &hm->iter;
TfwStr crlf = { .data = S_CRLF, .len = SLEN(S_CRLF) };

r = tfw_http_msg_expand_data(&mit->iter, skb_head, &rh, NULL);
r = tfw_http_msg_expand_data(it, skb_head, &rh, NULL);
if (unlikely(r))
goto err;
r = tfw_http_msg_expand_data(&mit->iter, skb_head, &crlf, NULL);
r = tfw_http_msg_expand_data(it, skb_head, &crlf, NULL);
}

if (unlikely(r))
Expand Down Expand Up @@ -4098,12 +4094,12 @@ tfw_h1_purge_resp_clean(TfwHttpResp *resp, TfwHttpMsgCleanup *cleanup)
static int
tfw_http_adjust_resp(TfwHttpResp *resp)
{
int r, hdr_start = TFW_HTTP_HDR_CONTENT_LENGTH;;
int r, hdr_start = TFW_HTTP_HDR_CONTENT_LENGTH;
TfwHttpReq *req = resp->req;
TfwHttpMsg *hm = (TfwHttpMsg *)resp;
unsigned long conn_flg = 0;
TfwHttpMsgCleanup cleanup = {};
TfwHttpTransIter *mit = &resp->mit;
TfwMsgIter *iter = &resp->iter;
TfwStr *pos, *end, *s_line = &resp->h_tbl->tbl[TFW_HTTP_STATUS_LINE];
static const DEFINE_TFW_STR(crlf, S_CRLF);
const TfwHdrMods *h_mods = tfw_vhost_get_hdr_mods(req->location,
Expand Down Expand Up @@ -4138,13 +4134,13 @@ tfw_http_adjust_resp(TfwHttpResp *resp)
if (unlikely(r))
goto clean;

tfw_h2_msg_transform_setup(mit, resp->msg.skb_head);
tfw_msg_transform_setup(iter, resp->msg.skb_head);
} else {
/*
* When response doesn't have body, just remove
* headers and use current skb as skb head.
*/
tfw_h2_msg_transform_setup(mit, resp->msg.skb_head);
tfw_msg_transform_setup(iter, resp->msg.skb_head);

r = tfw_http_msg_cutoff_headers(hm, &cleanup);
if (unlikely(r))
Expand All @@ -4169,7 +4165,7 @@ tfw_http_adjust_resp(TfwHttpResp *resp)
hdr_start = TFW_HTTP_HDR_CONTENT_TYPE;
} else {
/* Response for regular request. */
tfw_h2_msg_transform_setup(mit, resp->msg.skb_head);
tfw_msg_transform_setup(iter, resp->msg.skb_head);
r = tfw_http_msg_cutoff_headers(hm, &cleanup);
if (unlikely(r))
goto clean;
Expand Down Expand Up @@ -4870,8 +4866,7 @@ static int
tfw_h2_append_predefined_body(TfwHttpResp *resp, unsigned int stream_id,
const TfwStr *body)
{
TfwHttpTransIter *mit = &resp->mit;
TfwMsgIter *it = &mit->iter;
TfwMsgIter *it = &resp->iter;
size_t len, max_copy = PAGE_SIZE;
char *data;
int r;
Expand Down Expand Up @@ -5323,7 +5318,8 @@ tfw_h2_resp_adjust_fwd(TfwHttpResp *resp)
* adjusting of particular headers.
*/
WARN_ON_ONCE(mit->acc_len);
tfw_h2_msg_transform_setup(mit, resp->msg.skb_head);
BUG_ON(mit->frame_head);
tfw_msg_transform_setup(&resp->iter, resp->msg.skb_head);

if (tfw_http_msg_cutoff_headers((TfwHttpMsg *)resp, &cleanup))
goto clean;
Expand All @@ -5332,7 +5328,7 @@ tfw_h2_resp_adjust_fwd(TfwHttpResp *resp)
* Alloc room for frame header. After this call resp->pool
* must be used only as skb paged data.
*/
r = tfw_http_msg_setup_transform_pool(mit, resp->pool);
r = tfw_http_msg_setup_transform_pool(mit, &resp->iter, resp->pool);
if (unlikely(r))
goto clean;

Expand Down
Loading

0 comments on commit da00619

Please sign in to comment.