Skip to content

Commit 27c336f

Browse files
committed
Add a check to prevent sending the double body response to the agent
1 parent 49d489f commit 27c336f

5 files changed

+21
-4
lines changed

src/ngx_http_redirectionio_module.c

+1
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ static ngx_int_t ngx_http_redirectionio_create_ctx_handler(ngx_http_request_t *r
174174
ctx->is_redirected = 0;
175175
ctx->headers_sent = 0;
176176
ctx->body_buffer = NULL;
177+
ctx->body_sent = 0;
177178
ctx->read_binary_handler = NULL;
178179
ctx->first_buffer = 1;
179180
ctx->last_chain_sent = NULL;

src/ngx_http_redirectionio_module.h

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ typedef struct {
6161

6262
ngx_chain_t *body_buffer;
6363
ngx_chain_t *last_chain_sent;
64+
ngx_uint_t body_sent;
6465
} ngx_http_redirectionio_ctx_t;
6566

6667
void ngx_http_redirectionio_read_dummy_handler(ngx_event_t *rev, cJSON *json);

src/ngx_http_redirectionio_module_filter.c

+12-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ ngx_int_t ngx_http_redirectionio_match_on_response_status_header_filter(ngx_http
5757
// Avoid loop if we redirect on the same status as we match
5858
ctx->is_redirected = 1;
5959

60+
// @TODO This will made a double body response (one from nginx / one from upstream)
61+
// @TODO Find a way to cancel the current body response
6062
return ngx_http_special_response_handler(r, ctx->status);
6163
}
6264

@@ -142,6 +144,10 @@ ngx_int_t ngx_http_redirectionio_body_filter(ngx_http_request_t *r, ngx_chain_t
142144
return ngx_http_next_body_filter(r, in);
143145
}
144146

147+
if (ctx->body_sent) {
148+
return NGX_OK;
149+
}
150+
145151
// Check if we are waiting for filtering headers or connection
146152
if (ctx->wait_for_header_filtering || ctx->wait_for_connection) {
147153
// Set request is buffered to avoid its destruction by nginx
@@ -219,6 +225,7 @@ static void ngx_http_redirectionio_write_filter_body_handler(ngx_event_t *wev, n
219225
ngx_http_request_t *r;
220226
ngx_http_redirectionio_conf_t *conf;
221227
ngx_chain_t *cl;
228+
ngx_uint_t last;
222229

223230
c = wev->data;
224231
r = c->data;
@@ -240,7 +247,11 @@ static void ngx_http_redirectionio_write_filter_body_handler(ngx_event_t *wev, n
240247
ctx->last_chain_sent = in;
241248
ctx->read_binary_handler = ngx_http_redirectionio_read_filter_body_handler;
242249

243-
ngx_http_redirectionio_protocol_send_filter_body(c, in, &conf->project_key, &ctx->matched_rule_id, is_first);
250+
last = ngx_http_redirectionio_protocol_send_filter_body(c, in, &conf->project_key, &ctx->matched_rule_id, is_first);
251+
252+
if (last == 1) {
253+
ctx->body_sent = 1;
254+
}
244255
}
245256

246257
static void ngx_http_redirectionio_read_filter_headers_handler(ngx_event_t *rev, cJSON *json) {

src/ngx_http_redirectionio_protocol.c

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <ngx_http_redirectionio_protocol.h>
2+
#include <ngx_http_redirectionio_module.h>
23

34
const char COMMAND_MATCH_NAME[] = "MATCH_WITH_RESPONSE";
45
const char COMMAND_MATCH_QUERY[] = "{ \"project_id\": \"%V\", \"request_uri\": \"%V\", \"host\": \"%V\" }";
@@ -173,7 +174,7 @@ void ngx_http_redirectionio_protocol_send_filter_header(ngx_connection_t *c, ngx
173174
free((void *)dst);
174175
}
175176

176-
void ngx_http_redirectionio_protocol_send_filter_body(ngx_connection_t *c, ngx_chain_t *in, ngx_str_t *project_key, ngx_str_t *rule_id, ngx_uint_t is_first) {
177+
ngx_uint_t ngx_http_redirectionio_protocol_send_filter_body(ngx_connection_t *c, ngx_chain_t *in, ngx_str_t *project_key, ngx_str_t *rule_id, ngx_uint_t is_first) {
177178
ngx_chain_t *chain;
178179
uint64_t bsize;
179180

@@ -211,9 +212,12 @@ void ngx_http_redirectionio_protocol_send_filter_body(ngx_connection_t *c, ngx_c
211212
// If last write empty buffer
212213
if (chain->buf != NULL && chain->buf->last_buf) {
213214
bsize = htonll((uint64_t)-1);
214-
215215
ngx_send(c, (u_char *)&bsize, sizeof(uint64_t));
216+
217+
return 1;
216218
}
219+
220+
return 0;
217221
}
218222

219223
static void ngx_str_copy(ngx_str_t *src, ngx_str_t *dest) {

src/ngx_http_redirectionio_protocol.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ void ngx_http_redirectionio_protocol_send_log(ngx_connection_t *c, ngx_http_redi
2222
ngx_http_redirectionio_log_t* ngx_http_redirectionio_protocol_create_log(ngx_http_request_t *r, ngx_str_t *project_key, ngx_str_t *rule_id);
2323
void ngx_http_redirectionio_protocol_free_log(ngx_http_redirectionio_log_t *log);
2424
void ngx_http_redirectionio_protocol_send_filter_header(ngx_connection_t *c, ngx_http_request_t *r, ngx_str_t *project_key, ngx_str_t *rule_id);
25-
void ngx_http_redirectionio_protocol_send_filter_body(ngx_connection_t *c, ngx_chain_t *in, ngx_str_t *project_key, ngx_str_t *rule_id, ngx_uint_t is_first);
25+
ngx_uint_t ngx_http_redirectionio_protocol_send_filter_body(ngx_connection_t *c, ngx_chain_t *in, ngx_str_t *project_key, ngx_str_t *rule_id, ngx_uint_t is_first);
2626

2727
#endif

0 commit comments

Comments
 (0)