Skip to content

Commit 476b782

Browse files
committed
Add redirectionio set header for nginx module
1 parent 2e18bfe commit 476b782

File tree

4 files changed

+127
-9
lines changed

4 files changed

+127
-9
lines changed

nginx-test.conf.in

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ http {
3333
redirectionio_project_key 58056a48-664d-11e7-aeb0-0242ac130004;
3434
redirectionio_add_rule_ids_header on;
3535
redirectionio_pass agent:10301 min_conns=10 keep_conns=10 max_conns=1000 timeout=100;
36+
redirectionio_set_header X-Host-Ter $host;
37+
redirectionio_set_header X-Host-Ter-2 $host;
38+
redirectionio_set_header X-Host-Ter-3 YOLOLOLO;
3639

3740
location /noredirection {
3841
redirectionio off;
@@ -56,6 +59,9 @@ http {
5659
redirectionio on;
5760
redirectionio_project_key 58056a48-664d-11e7-aeb0-0242ac130004;
5861
redirectionio_pass agent:10301;
62+
redirectionio_set_header X-Host-Bis $host;
63+
redirectionio_set_header X-Host-Bis-2 $host;
64+
redirectionio_set_header X-Host-Bis-3 YOLOLOLO;
5965

6066
location / {
6167
proxy_set_header Host $host;

src/ngx_http_redirectionio_module.c

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ static ngx_conf_enum_t ngx_http_redirectionio_enable_state[] = {
1212
static void *ngx_http_redirectionio_create_conf(ngx_conf_t *cf);
1313
static char *ngx_http_redirectionio_merge_conf(ngx_conf_t *cf, void *parent, void *child);
1414
static char *ngx_http_redirectionio_set_url(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
15+
static char *ngx_http_redirectionio_set_header(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
1516

1617
static ngx_int_t ngx_http_redirectionio_postconfiguration(ngx_conf_t *cf);
1718

@@ -85,6 +86,14 @@ static ngx_command_t ngx_http_redirectionio_commands[] = {
8586
offsetof(ngx_http_redirectionio_conf_t, host),
8687
NULL
8788
},
89+
{
90+
ngx_string("redirectionio_set_header"),
91+
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE2,
92+
ngx_http_redirectionio_set_header,
93+
NGX_HTTP_LOC_CONF_OFFSET,
94+
offsetof(ngx_http_redirectionio_conf_t, headers_set),
95+
NULL
96+
},
8897
ngx_null_command /* command termination */
8998
};
9099

@@ -401,13 +410,20 @@ static void *ngx_http_redirectionio_create_conf(ngx_conf_t *cf) {
401410
conf->server.min_conns = RIO_MIN_CONNECTIONS;
402411
conf->server.max_conns = RIO_MAX_CONNECTIONS;
403412
conf->server.max_conns = RIO_MAX_CONNECTIONS;
413+
conf->server.timeout = RIO_DEFAULT_TIMEOUT;
414+
415+
if (ngx_array_init(&conf->headers_set, cf->pool, 10, sizeof(ngx_http_redirectionio_header_set_t)) != NGX_OK) {
416+
return NGX_CONF_ERROR;
417+
}
404418

405419
return conf;
406420
}
407421

408422
static char *ngx_http_redirectionio_merge_conf(ngx_conf_t *cf, void *parent, void *child) {
409423
ngx_http_redirectionio_conf_t *prev = parent;
410424
ngx_http_redirectionio_conf_t *conf = child;
425+
ngx_uint_t i;
426+
ngx_http_redirectionio_header_set_t *phs, *hs;
411427

412428
ngx_conf_merge_uint_value(conf->enable_logs, prev->enable_logs, NGX_HTTP_REDIRECTIONIO_ON);
413429
ngx_conf_merge_uint_value(conf->show_rule_ids, prev->show_rule_ids, NGX_HTTP_REDIRECTIONIO_OFF);
@@ -424,6 +440,15 @@ static char *ngx_http_redirectionio_merge_conf(ngx_conf_t *cf, void *parent, voi
424440
conf->host = prev->host;
425441
}
426442

443+
phs = prev->headers_set.elts;
444+
445+
for (i = 0; i < prev->headers_set.nelts ; i++) {
446+
hs = ngx_array_push(&conf->headers_set);
447+
448+
hs->name = phs[i].name;
449+
hs->value = phs[i].value;
450+
}
451+
427452
if (conf->server.pass.url.data == NULL) {
428453
if (prev->server.pass.url.data) {
429454
conf->server.pass = prev->server.pass;
@@ -477,7 +502,6 @@ static char *ngx_http_redirectionio_merge_conf(ngx_conf_t *cf, void *parent, voi
477502
ngx_conf_merge_uint_value(conf->enable, prev->enable, NGX_HTTP_REDIRECTIONIO_OFF);
478503
}
479504

480-
481505
return NGX_CONF_OK;
482506
}
483507

@@ -561,16 +585,72 @@ static char *ngx_http_redirectionio_set_url(ngx_conf_t *cf, ngx_command_t *cmd,
561585
return NGX_CONF_ERROR;
562586
}
563587

588+
static char *ngx_http_redirectionio_set_header(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
589+
char *p = conf;
590+
ngx_array_t *headers_set;
591+
ngx_str_t *value;
592+
ngx_http_redirectionio_header_set_t *h;
593+
ngx_http_compile_complex_value_t ccvk, ccvv;
594+
595+
headers_set = (ngx_array_t *) (p + cmd->offset);
596+
h = ngx_array_push(headers_set);
597+
598+
if (h == NULL) {
599+
return NGX_CONF_ERROR;
600+
}
601+
602+
h->name = ngx_palloc(cf->pool, sizeof(ngx_http_complex_value_t));
603+
604+
if (h->name == NULL) {
605+
return NGX_CONF_ERROR;
606+
}
607+
608+
h->value = ngx_palloc(cf->pool, sizeof(ngx_http_complex_value_t));
609+
610+
if (h->value == NULL) {
611+
return NGX_CONF_ERROR;
612+
}
613+
614+
value = cf->args->elts;
615+
616+
ngx_memzero(&ccvk, sizeof(ngx_http_compile_complex_value_t));
617+
ngx_memzero(&ccvv, sizeof(ngx_http_compile_complex_value_t));
618+
619+
ccvk.cf = cf;
620+
ccvk.value = &value[1];
621+
ccvk.complex_value = h->name;
622+
623+
ccvv.cf = cf;
624+
ccvv.value = &value[2];
625+
ccvv.complex_value = h->value;
626+
627+
if (ngx_http_compile_complex_value(&ccvk) != NGX_OK) {
628+
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid parameter \"%V\"", &value[1]);
629+
630+
return NGX_CONF_ERROR;
631+
}
632+
633+
if (ngx_http_compile_complex_value(&ccvv) != NGX_OK) {
634+
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid parameter \"%V\"", &value[2]);
635+
636+
return NGX_CONF_ERROR;
637+
}
638+
639+
return NGX_CONF_OK;
640+
}
641+
564642
static void ngx_http_redirectionio_write_match_action_handler(ngx_event_t *wev) {
565643
ngx_http_redirectionio_ctx_t *ctx;
566644
ngx_connection_t *c;
567645
ngx_http_request_t *r;
646+
ngx_http_redirectionio_conf_t *conf;
568647

569648
c = wev->data;
570649
r = c->data;
571650
ctx = ngx_http_get_module_ctx(r, ngx_http_redirectionio_module);
651+
conf = ngx_http_get_module_loc_conf(r, ngx_http_redirectionio_module);
572652

573-
ngx_add_timer(c->read, RIO_DEFAULT_TIMEOUT);
653+
ngx_add_timer(c->read, conf->server.timeout);
574654
ctx->read_handler = ngx_http_redirectionio_read_match_action_handler;
575655

576656
ngx_http_redirectionio_protocol_send_match(c, r, ctx, &ctx->project_key);

src/ngx_http_redirectionio_module.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ typedef struct {
4444
ngx_msec_t timeout;
4545
} ngx_http_redirectionio_server_t;
4646

47+
typedef struct {
48+
ngx_http_complex_value_t *name;
49+
ngx_http_complex_value_t *value;
50+
} ngx_http_redirectionio_header_set_t;
51+
4752
typedef struct {
4853
ngx_uint_t enable;
4954
ngx_uint_t enable_logs;
@@ -52,6 +57,7 @@ typedef struct {
5257
ngx_http_complex_value_t *host;
5358
ngx_uint_t show_rule_ids;
5459
ngx_http_redirectionio_server_t server;
60+
ngx_array_t headers_set;
5561
ngx_reslist_t *connection_pool;
5662
} ngx_http_redirectionio_conf_t;
5763

src/ngx_http_redirectionio_protocol.c

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,21 @@ static ngx_int_t ngx_http_redirectionio_send_string(ngx_connection_t *c, const c
1515
static ngx_int_t ngx_http_redirectionio_send_protocol_header(ngx_connection_t *c, ngx_str_t *project_key, uint16_t command);
1616

1717
void ngx_http_redirectionio_protocol_send_match(ngx_connection_t *c, ngx_http_request_t *r, ngx_http_redirectionio_ctx_t *ctx, ngx_str_t *project_key) {
18-
ngx_int_t rv;
19-
ngx_table_elt_t *h;
20-
ngx_list_part_t *part;
21-
struct REDIRECTIONIO_HeaderMap *first_header = NULL, *current_header = NULL;
22-
const char *request_serialized;
23-
char *method, *uri, *host = NULL, *scheme = NULL;
24-
ngx_uint_t i;
18+
ngx_int_t rv;
19+
ngx_table_elt_t *h;
20+
ngx_list_part_t *part;
21+
struct REDIRECTIONIO_HeaderMap *first_header = NULL, *current_header = NULL;
22+
const char *request_serialized;
23+
char *method, *uri, *host = NULL, *scheme = NULL;
24+
ngx_uint_t i;
25+
ngx_http_redirectionio_conf_t *conf;
26+
ngx_http_redirectionio_header_set_t *hs;
27+
ngx_str_t hsn, hsv;
28+
29+
conf = ngx_http_get_module_loc_conf(r, ngx_http_redirectionio_module);
2530

2631
// Create header map
32+
// First add request headers
2733
part = &r->headers_in.headers.part;
2834
h = part->elts;
2935

@@ -50,6 +56,26 @@ void ngx_http_redirectionio_protocol_send_match(ngx_connection_t *c, ngx_http_re
5056
first_header = current_header;
5157
}
5258

59+
// Then set headers
60+
hs = conf->headers_set.elts;
61+
62+
for (i = 0; i < conf->headers_set.nelts ; i++) {
63+
if (ngx_http_complex_value(r, hs[i].name, &hsn) != NGX_OK) {
64+
continue;
65+
}
66+
67+
if (ngx_http_complex_value(r, hs[i].value, &hsv) != NGX_OK) {
68+
continue;
69+
}
70+
71+
current_header = (struct REDIRECTIONIO_HeaderMap *)ngx_pcalloc(r->pool, sizeof(struct REDIRECTIONIO_HeaderMap));
72+
current_header->name = ngx_str_to_char(&hsn, r->pool);
73+
current_header->value = ngx_str_to_char(&hsv, r->pool);
74+
current_header->next = first_header;
75+
76+
first_header = current_header;
77+
}
78+
5379
if (ctx->scheme.len > 0) {
5480
scheme = ngx_str_to_char(&ctx->scheme, r->pool);
5581
} else {

0 commit comments

Comments
 (0)