Skip to content

Commit 3a42283

Browse files
committed
Allow complex values for scheme and project_key, allow to override host sent to agent
1 parent 7eef02b commit 3a42283

File tree

4 files changed

+56
-22
lines changed

4 files changed

+56
-22
lines changed

nginx-test.conf.in

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ http {
4141
location /nolog {
4242
redirectionio_logs off;
4343
}
44+
45+
location /force-value {
46+
redirectionio_host myhost.com;
47+
redirectionio_scheme https;
48+
}
4449
}
4550

4651
server {

src/ngx_http_redirectionio_module.c

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ static ngx_command_t ngx_http_redirectionio_commands[] = {
4040
{
4141
ngx_string("redirectionio_project_key"),
4242
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
43-
ngx_conf_set_str_slot,
43+
ngx_http_set_complex_value_slot,
4444
NGX_HTTP_LOC_CONF_OFFSET,
4545
offsetof(ngx_http_redirectionio_conf_t, project_key),
4646
NULL
@@ -72,11 +72,19 @@ static ngx_command_t ngx_http_redirectionio_commands[] = {
7272
{
7373
ngx_string("redirectionio_scheme"),
7474
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
75-
ngx_conf_set_str_slot,
75+
ngx_http_set_complex_value_slot,
7676
NGX_HTTP_LOC_CONF_OFFSET,
7777
offsetof(ngx_http_redirectionio_conf_t, scheme),
7878
NULL
7979
},
80+
{
81+
ngx_string("redirectionio_host"),
82+
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
83+
ngx_http_set_complex_value_slot,
84+
NGX_HTTP_LOC_CONF_OFFSET,
85+
offsetof(ngx_http_redirectionio_conf_t, host),
86+
NULL
87+
},
8088
ngx_null_command /* command termination */
8189
};
8290

@@ -198,6 +206,21 @@ static ngx_int_t ngx_http_redirectionio_create_ctx_handler(ngx_http_request_t *r
198206
ctx->wait_for_connection = 0;
199207
ctx->last_buffer_sent = 0;
200208
ctx->read_handler = ngx_http_redirectionio_read_dummy_handler;
209+
ctx->project_key.len = 0;
210+
ctx->scheme.len = 0;
211+
ctx->host.len = 0;
212+
213+
if (ngx_http_complex_value(r, conf->project_key, &ctx->project_key) != NGX_OK) {
214+
return NGX_DECLINED;
215+
}
216+
217+
if (conf->scheme != NULL && ngx_http_complex_value(r, conf->scheme, &ctx->scheme) != NGX_OK) {
218+
return NGX_DECLINED;
219+
}
220+
221+
if (conf->host != NULL && ngx_http_complex_value(r, conf->host, &ctx->host) != NGX_OK) {
222+
return NGX_DECLINED;
223+
}
201224

202225
ngx_http_set_ctx(r, ctx, ngx_http_redirectionio_module);
203226

@@ -329,7 +352,7 @@ static ngx_int_t ngx_http_redirectionio_log_handler(ngx_http_request_t *r) {
329352
return NGX_DECLINED;
330353
}
331354

332-
log = ngx_http_redirectionio_protocol_create_log(r, ctx, &conf->project_key);
355+
log = ngx_http_redirectionio_protocol_create_log(r, ctx, &ctx->project_key);
333356

334357
if (log == NULL) {
335358
return NGX_DECLINED;
@@ -363,8 +386,18 @@ static char *ngx_http_redirectionio_merge_conf(ngx_conf_t *cf, void *parent, voi
363386

364387
ngx_conf_merge_uint_value(conf->enable_logs, prev->enable_logs, NGX_HTTP_REDIRECTIONIO_ON);
365388
ngx_conf_merge_uint_value(conf->show_rule_ids, prev->show_rule_ids, NGX_HTTP_REDIRECTIONIO_OFF);
366-
ngx_conf_merge_str_value(conf->project_key, prev->project_key, "");
367-
ngx_conf_merge_str_value(conf->scheme, prev->scheme, "");
389+
390+
if (conf->project_key == NULL) {
391+
conf->project_key = prev->project_key;
392+
}
393+
394+
if (conf->scheme == NULL) {
395+
conf->scheme = prev->scheme;
396+
}
397+
398+
if (conf->host == NULL) {
399+
conf->host = prev->host;
400+
}
368401

369402
if (conf->pass.url.data == NULL) {
370403
if (prev->pass.url.data) {
@@ -413,7 +446,7 @@ static char *ngx_http_redirectionio_merge_conf(ngx_conf_t *cf, void *parent, voi
413446
}
414447
}
415448

416-
if (conf->project_key.len > 0) {
449+
if (conf->project_key != NULL) {
417450
ngx_conf_merge_uint_value(conf->enable, prev->enable, NGX_HTTP_REDIRECTIONIO_ON);
418451
} else {
419452
ngx_conf_merge_uint_value(conf->enable, prev->enable, NGX_HTTP_REDIRECTIONIO_OFF);
@@ -456,17 +489,15 @@ static void ngx_http_redirectionio_write_match_action_handler(ngx_event_t *wev)
456489
ngx_http_redirectionio_ctx_t *ctx;
457490
ngx_connection_t *c;
458491
ngx_http_request_t *r;
459-
ngx_http_redirectionio_conf_t *conf;
460492

461493
c = wev->data;
462494
r = c->data;
463495
ctx = ngx_http_get_module_ctx(r, ngx_http_redirectionio_module);
464-
conf = ngx_http_get_module_loc_conf(r, ngx_http_redirectionio_module);
465496

466497
ngx_add_timer(c->read, RIO_TIMEOUT);
467498
ctx->read_handler = ngx_http_redirectionio_read_match_action_handler;
468499

469-
ngx_http_redirectionio_protocol_send_match(c, r, ctx, &conf->project_key);
500+
ngx_http_redirectionio_protocol_send_match(c, r, ctx, &ctx->project_key);
470501
}
471502

472503
static void ngx_http_redirectionio_read_match_action_handler(ngx_event_t *rev, const char *action_serialized) {

src/ngx_http_redirectionio_module.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@
3939
typedef struct {
4040
ngx_uint_t enable;
4141
ngx_uint_t enable_logs;
42-
ngx_str_t project_key;
43-
ngx_str_t scheme;
42+
ngx_http_complex_value_t *project_key;
43+
ngx_http_complex_value_t *scheme;
44+
ngx_http_complex_value_t *host;
4445
ngx_uint_t show_rule_ids;
45-
ngx_http_complex_value_t *complex_target;
4646
ngx_url_t pass;
4747
ngx_reslist_t *connection_pool;
4848
} ngx_http_redirectionio_conf_t;
@@ -68,6 +68,9 @@ typedef struct {
6868
ngx_uint_t last_buffer_sent;
6969

7070
ngx_http_redirectionio_read_handler_t read_handler;
71+
ngx_str_t project_key;
72+
ngx_str_t scheme;
73+
ngx_str_t host;
7174
} ngx_http_redirectionio_ctx_t;
7275

7376
typedef struct {

src/ngx_http_redirectionio_protocol.c

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,6 @@ void ngx_http_redirectionio_protocol_send_match(ngx_connection_t *c, ngx_http_re
2525
char *method, *uri, *host = NULL, *scheme = NULL;
2626
ngx_uint_t i;
2727
ngx_pool_cleanup_t *cln;
28-
ngx_http_redirectionio_conf_t *conf;
29-
30-
conf = ngx_http_get_module_loc_conf(r, ngx_http_redirectionio_module);
31-
32-
if (conf == NULL) {
33-
return;
34-
}
3528

3629
// Create header map
3730
part = &r->headers_in.headers.part;
@@ -60,8 +53,8 @@ void ngx_http_redirectionio_protocol_send_match(ngx_connection_t *c, ngx_http_re
6053
first_header = current_header;
6154
}
6255

63-
if (conf->scheme.len > 0) {
64-
scheme = ngx_str_to_char(&conf->scheme, r->pool);
56+
if (ctx->scheme.len > 0) {
57+
scheme = ngx_str_to_char(&ctx->scheme, r->pool);
6558
} else {
6659
scheme = "http";
6760
}
@@ -75,7 +68,9 @@ void ngx_http_redirectionio_protocol_send_match(ngx_connection_t *c, ngx_http_re
7568
uri = ngx_str_to_char(&r->unparsed_uri, r->pool);
7669
method = ngx_str_to_char(&r->method_name, r->pool);
7770

78-
if (r->headers_in.host != NULL) {
71+
if (ctx->host.len > 0) {
72+
host = ngx_str_to_char(&ctx->host, r->pool);
73+
} else if (r->headers_in.host != NULL) {
7974
host = ngx_str_to_char(&r->headers_in.host->value, r->pool);
8075
}
8176

0 commit comments

Comments
 (0)