Skip to content

Commit 8be2f15

Browse files
committed
Fix marked header setting
1 parent d49ef4d commit 8be2f15

File tree

2 files changed

+13
-29
lines changed

2 files changed

+13
-29
lines changed

ngx_http_repsheet_lookup.c

+11-27
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,13 @@
44
#include "ngx_http_repsheet_xff.h"
55
#include "ngx_http_repsheet_lookup.h"
66

7-
static int
8-
flag_request(ngx_http_request_t *r, char *reason)
9-
{
10-
repsheet_ctx_t *ctx;
11-
ctx = ngx_pcalloc(r->pool, sizeof(repsheet_ctx_t));
12-
if (ctx == NULL) {
13-
return NGX_ERROR;
14-
}
15-
16-
ngx_http_set_ctx(r, ctx, ngx_http_repsheet_module);
17-
18-
ctx->flagged = 1;
19-
20-
int len = strlen(reason);
21-
ctx->reason.len = len;
22-
ctx->reason.data = ngx_palloc(r->pool, len);
23-
if (ctx->reason.data == NULL) {
24-
return NGX_ERROR;
25-
}
26-
27-
memcpy(ctx->reason.data, reason, len);
28-
29-
return NGX_OK;
7+
void set_reason_header(ngx_http_request_t *r, ngx_str_t *reason) {
8+
ngx_table_elt_t *h;
9+
h = ngx_list_push(&r->headers_in.headers);
10+
h->hash = 1;
11+
ngx_str_set(&h->key, "X-Repsheet");
12+
h->value.data = reason->data;
13+
h->value.len = reason->len;
3014
}
3115

3216
ngx_int_t lookup_ip(ngx_http_request_t *r, repsheet_main_conf_t *main_conf, repsheet_loc_conf_t *loc_conf)
@@ -58,10 +42,10 @@ ngx_int_t lookup_ip(ngx_http_request_t *r, repsheet_main_conf_t *main_conf, reps
5842

5943
if (is_ip_marked(lookup_result)) {
6044
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "[Repsheet] - IP %s was found on repsheet. Reason: %s", address, reason);
61-
if (flag_request(r, reason) != NGX_OK) {
62-
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "[Repsheet] - failed to flag request");
63-
return NGX_DECLINED;
64-
}
45+
ngx_str_t value;
46+
ngx_memcpy(value.data, reason, ngx_strlen(reason));
47+
value.len = ngx_strlen(reason);
48+
set_reason_header(r, &value);
6549
}
6650

6751
if (lookup_result == WHITELISTED) {

sample_application/app.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
require 'sinatra'
22

33
get '/app' do
4-
if request.env['HTTP_X_REPSHEET'] == "true"
5-
"Actor is on the Repsheet"
4+
if request.env['HTTP_X_REPSHEET']
5+
"Actor is marked. Reason: #{request.env['HTTP_X_REPSHEET']}"
66
else
77
"Actor is not on the Repsheet"
88
end

0 commit comments

Comments
 (0)