Skip to content

Commit c03509d

Browse files
committed
add unit test for X-Forwarded-For field in request
1 parent d6ad7db commit c03509d

File tree

3 files changed

+51
-8
lines changed

3 files changed

+51
-8
lines changed

tempesta_db/core/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ tdb_rec_get_alloc(TDB *db, unsigned long key, size_t *len,
267267

268268
*is_new = true;
269269
r = tdb_entry_alloc(db, key, len);
270-
init(iter.rec, cb, data);
270+
init(r, cb, data);
271271

272272
spin_unlock(&get_alloc_lock);
273273

tempesta_fw/client.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ static struct {
4040

4141
typedef struct {
4242
TfwClient cli;
43-
spinlock_t *hb_lock;
43+
spinlock_t hb_lock;
4444
time_t expires;
4545
} TfwClientEntry;
4646

@@ -63,17 +63,17 @@ tfw_client_put(TfwClient *cli)
6363
return;
6464

6565
ent = (TfwClientEntry *)cli;
66-
spin_lock(ent->hb_lock);
66+
spin_lock(&ent->hb_lock);
6767

6868
if (atomic_read(&cli->conn_users)) {
69-
spin_unlock(ent->hb_lock);
69+
spin_unlock(&ent->hb_lock);
7070
return;
7171
}
7272
BUG_ON(!list_empty(&cli->conn_list));
7373

7474
ent->expires = tfw_current_timestamp() + client_cfg.lifetime;
7575

76-
spin_unlock(ent->hb_lock);
76+
spin_unlock(&ent->hb_lock);
7777

7878
TFW_DBG("put client: cli=%p\n", cli);
7979
TFW_DEC_STAT_BH(clnt.online);
@@ -90,7 +90,7 @@ tfw_client_addr_eq(TdbRec *rec, void (*init)(void *), void *data)
9090

9191
if (!memcmp_fast(&cli->addr.sin6_addr, &addr->sin6_addr,
9292
sizeof(cli->addr.sin6_addr))) {
93-
spin_lock(ent->hb_lock);
93+
spin_lock(&ent->hb_lock);
9494

9595
if (curr_time > ent->expires) {
9696
bzero_fast(&cli->class_prvt, sizeof(cli->class_prvt));
@@ -104,7 +104,7 @@ tfw_client_addr_eq(TdbRec *rec, void (*init)(void *), void *data)
104104
if (conn_users == 1)
105105
TFW_INC_STAT_BH(clnt.online);
106106

107-
spin_unlock(ent->hb_lock);
107+
spin_unlock(&ent->hb_lock);
108108

109109
TFW_DBG("client was found in tdb\n");
110110
TFW_DBG2("client %p, conn_users=%d\n", cli, conn_users);
@@ -122,7 +122,7 @@ tfw_client_ent_init(TdbRec *rec, void (*init)(void *), void *data)
122122
TfwClient *cli = &ent->cli;
123123
TfwAddr *addr = (TfwAddr *)data;
124124

125-
spin_lock_init(ent->hb_lock);
125+
spin_lock_init(&ent->hb_lock);
126126

127127
ent->expires = LONG_MAX;
128128
if (init)

tempesta_fw/t/unit/test_http_parser.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2421,6 +2421,48 @@ TEST(http_parser, content_type_line_parser)
24212421
#undef TAIL
24222422
}
24232423

2424+
static
2425+
TfwStr get_next_str_val(TfwStr *str)
2426+
{
2427+
TfwStr v, *c, *end;
2428+
unsigned int nchunks = 0;
2429+
2430+
v = *str = tfw_str_next_str_val(str);
2431+
TFW_STR_FOR_EACH_CHUNK(c, &v, end) {
2432+
if (!(c->flags & TFW_STR_VALUE))
2433+
break;
2434+
nchunks++;
2435+
}
2436+
v.nchunks = nchunks;
2437+
2438+
return v;
2439+
}
2440+
2441+
TEST(http_parser, xff)
2442+
{
2443+
TfwStr xff, v;
2444+
2445+
const char *s_client = "203.0.113.195";
2446+
const char *s_proxy1 = "70.41.3.18";
2447+
const char *s_proxy2 = "150.172.238.178";
2448+
2449+
FOR_REQ("GET /foo HTTP/1.1\r\n"
2450+
"X-Forwarded-For: 203.0.113.195,70.41.3.18,150.172.238.178\r\n"
2451+
"\r\n");
2452+
{
2453+
xff = req->h_tbl->tbl[TFW_HTTP_HDR_X_FORWARDED_FOR];
2454+
2455+
v = get_next_str_val(&xff);
2456+
EXPECT_TRUE(tfw_str_eq_cstr(&v, s_client, strlen(s_client), 0));
2457+
2458+
v = get_next_str_val(&xff);
2459+
EXPECT_TRUE(tfw_str_eq_cstr(&v, s_proxy1, strlen(s_proxy1), 0));
2460+
2461+
v = get_next_str_val(&xff);
2462+
EXPECT_TRUE(tfw_str_eq_cstr(&v, s_proxy2, strlen(s_proxy2), 0));
2463+
}
2464+
}
2465+
24242466
TEST_SUITE(http_parser)
24252467
{
24262468
int r;
@@ -2458,6 +2500,7 @@ TEST_SUITE(http_parser)
24582500
TEST_RUN(http_parser, resp_hop_by_hop);
24592501
TEST_RUN(http_parser, fuzzer);
24602502
TEST_RUN(http_parser, content_type_line_parser);
2503+
TEST_RUN(http_parser, xff);
24612504

24622505
/*
24632506
* Testing for correctness of redirection mark parsing (in

0 commit comments

Comments
 (0)