Skip to content

Commit

Permalink
fix review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
avbelov23 committed May 20, 2022
1 parent 30de466 commit b26974e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 75 deletions.
26 changes: 10 additions & 16 deletions fw/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,14 @@ static struct {
#define S_V_MULTIPART "multipart/form-data; boundary="
#define S_V_WARN "110 - Response is stale"

/*
* A string with enough chunks to hold any element of `http_predef_resps`
*/
#define MAX_PREDEF_RESP { \
.chunks = (TfwStr []){ {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, \
{}, {}, {}, {} }, \
.len = 0, .nchunks = 13 \
}
/*
* Array with predefined response data
*/
Expand Down Expand Up @@ -745,8 +753,6 @@ tfw_http_prep_redir(TfwHttpResp *resp, unsigned short status, TfwStr *rmark,
url.len += req->uri_path.len;

body_len = tfw_str_to_cstr(body, body_val, BODY_BUF_LEN);
if (!body_len)
return TFW_BLOCK;

{
TfwStr msg = {
Expand Down Expand Up @@ -1145,15 +1151,9 @@ tfw_http_prep_err_resp(TfwHttpReq *req, int status, TfwStr *msg)
static void
tfw_h2_send_err_resp(TfwHttpReq *req, int status, unsigned int stream_id)
{
TfwStr msg = {
.chunks = (TfwStr []){ {}, {}, {}, {}, {}, {}, {}, {}, {}, {},
{}, {}, {}, {} },
.len = 0,
.nchunks = 13
};
TfwStr msg = MAX_PREDEF_RESP;

tfw_http_prep_err_resp(req, status, &msg);

tfw_h2_send_resp(req, &msg, status, stream_id);
}

Expand All @@ -1166,15 +1166,9 @@ tfw_h2_send_err_resp(TfwHttpReq *req, int status, unsigned int stream_id)
static void
tfw_h1_send_err_resp(TfwHttpReq *req, int status)
{
TfwStr msg = {
.chunks = (TfwStr []){ {}, {}, {}, {}, {}, {}, {}, {}, {}, {},
{}, {}, {}, {} },
.len = 0,
.nchunks = 13
};
TfwStr msg = MAX_PREDEF_RESP;

tfw_http_prep_err_resp(req, status, &msg);

tfw_h1_send_resp(req, &msg, status);
}

Expand Down
85 changes: 39 additions & 46 deletions fw/http_tbl.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,8 @@ tfw_http_tbl_scan(TfwMsg *msg, TfwHttpTable *table, TfwHttpActionResult *action)
if (unlikely(!rule)) {
T_DBG("http_tbl: No rule found in HTTP chain '%s'\n",
chain->name);
*action = (TfwHttpActionResult){
.type = TFW_HTTP_RES_VHOST,
.vhost = NULL
};
action->type = TFW_HTTP_RES_VHOST;
action->vhost = NULL;
return 0;
}
chain = (rule->act.type == TFW_HTTP_MATCH_ACT_CHAIN)
Expand All @@ -149,25 +147,19 @@ tfw_http_tbl_scan(TfwMsg *msg, TfwHttpTable *table, TfwHttpActionResult *action)

switch (rule->act.type) {
case TFW_HTTP_MATCH_ACT_VHOST:
*action = (TfwHttpActionResult){
.type = TFW_HTTP_RES_VHOST,
.vhost = rule->act.vhost
};
action->type = TFW_HTTP_RES_VHOST;
action->vhost = rule->act.vhost;
return 0;
case TFW_HTTP_MATCH_ACT_REDIR:
*action = (TfwHttpActionResult){
.type = TFW_HTTP_RES_REDIR,
.redir = rule->act.redir
};
action->type = TFW_HTTP_RES_REDIR;
action->redir = rule->act.redir;
return 0;
case TFW_HTTP_MATCH_ACT_BLOCK:
return -1;

default:
*action = (TfwHttpActionResult){
.type = TFW_HTTP_RES_VHOST,
.vhost = NULL
};
action->type = TFW_HTTP_RES_VHOST;
action->vhost = NULL;
return 0;
}
}
Expand Down Expand Up @@ -537,7 +529,7 @@ tfw_cfgop_http_rule(TfwCfgSpec *cs, TfwCfgEntry *e)
return -EINVAL;
}

url->chunks = kmalloc((TFW_HTTP_MAX_REDIR_VARS + 1) *
url->chunks = kzalloc((TFW_HTTP_MAX_REDIR_VARS + 1) *
sizeof(TfwStr), GFP_KERNEL);
if (!url->chunks) {
T_ERR_NL("http_tbl: can't allocate memory for "
Expand Down Expand Up @@ -568,38 +560,39 @@ do { \
for (begin = pos = action_val, i = 0;
*pos && i < TFW_HTTP_MAX_REDIR_VARS;)
{
if (*pos == '$') {
tfw_http_redir_var_t var;

if (val_end - pos > SLEN("request_uri") &&
!strncmp(pos + 1, "request_uri",
SLEN("request_uri")))
{
CREATE_CHUNK();

begin = pos += SLEN("$request_uri");

var = TFW_HTTP_REDIR_URI;
} else if (val_end - pos > SLEN("host") &&
!strncmp(pos + 1, "host",
SLEN("host")))
{
CREATE_CHUNK();

begin = pos += SLEN("$host");

var = TFW_HTTP_REDIR_HOST;
} else {
++pos;
continue;
}

rule->act.redir.var[i] = var;
rule->act.redir.nvar++;
i++;
unsigned char var;

if (*pos != '$') {
++pos;
continue;
}

if (val_end - pos > SLEN("request_uri") &&
!strncmp(pos + 1, "request_uri",
SLEN("request_uri")))
{
CREATE_CHUNK();

begin = pos += SLEN("$request_uri");

var = TFW_HTTP_REDIR_URI;
} else if (val_end - pos > SLEN("host") &&
!strncmp(pos + 1, "host",
SLEN("host")))
{
CREATE_CHUNK();

begin = pos += SLEN("$host");

var = TFW_HTTP_REDIR_HOST;
} else {
++pos;
continue;
}

rule->act.redir.var[i] = var;
rule->act.redir.nvar++;
i++;
}

if (pos - begin)
Expand Down
20 changes: 7 additions & 13 deletions fw/http_tbl.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,21 @@

#define TFW_HTTP_MAX_REDIR_VARS 8

typedef enum {
TFW_HTTP_REDIR_URI,
TFW_HTTP_REDIR_HOST,
_TFW_HTTP_REDIR_VAR_COUNT
} tfw_http_redir_var_t;
#define TFW_HTTP_REDIR_URI 0
#define TFW_HTTP_REDIR_HOST 1

typedef enum {
TFW_HTTP_RES_VHOST,
TFW_HTTP_RES_REDIR,
_TFW_HTTP_RES_COUNT
} tfw_http_res_act_t;
#define TFW_HTTP_RES_VHOST 0
#define TFW_HTTP_RES_REDIR 1

typedef struct {
TfwStr url;
tfw_http_redir_var_t var[TFW_HTTP_MAX_REDIR_VARS];
size_t nvar;
unsigned char var[TFW_HTTP_MAX_REDIR_VARS];
size_t nvar;
unsigned int resp_code;
} TfwHttpRedir;

typedef struct {
tfw_http_res_act_t type;
unsigned char type;
union {
TfwVhost *vhost;
TfwHttpRedir redir;
Expand Down

0 comments on commit b26974e

Please sign in to comment.