Skip to content

Commit 3825e30

Browse files
committed
Handle response from agent in multiple tcp packet
1 parent d72ec93 commit 3825e30

File tree

3 files changed

+47
-29
lines changed

3 files changed

+47
-29
lines changed

src/ngx_http_redirectionio_module.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ static ngx_int_t ngx_http_redirectionio_create_ctx_handler(ngx_http_request_t *r
203203
ctx->action = NULL;
204204
ctx->response_headers = NULL;
205205
ctx->body_filter = NULL;
206+
ctx->action_string = NULL;
207+
ctx->action_string_len = 0;
208+
ctx->action_string_readed = 0;
206209
ctx->connection_error = 0;
207210
ctx->wait_for_connection = 0;
208211
ctx->last_buffer_sent = 0;

src/ngx_http_redirectionio_module.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ typedef struct {
5858
typedef struct {
5959
ngx_http_redirectionio_resource_t *resource;
6060
ngx_uint_t matched_action_status;
61+
uint32_t action_string_len;
62+
ssize_t action_string_readed;
63+
char *action_string;
6164
struct REDIRECTIONIO_Request *request;
6265
struct REDIRECTIONIO_Action *action;
6366
struct REDIRECTIONIO_HeaderMap *response_headers;

src/ngx_http_redirectionio_module_pool.c

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ static void ngx_http_redirectionio_dummy_handler(ngx_event_t *wev);
66

77
static ngx_int_t ngx_http_redirectionio_read_uint32(ngx_connection_t *c, uint32_t *uint32);
88

9-
static ngx_int_t ngx_http_redirectionio_read_string(ngx_connection_t *c, char *string, ssize_t buf_size);
9+
static ngx_int_t ngx_http_redirectionio_read_string(ngx_connection_t *c, char *string, ssize_t buf_size, ssize_t *readed);
1010

1111
ngx_int_t ngx_http_redirectionio_pool_construct(void **rp, void *params) {
1212
ngx_pool_t *pool;
@@ -157,55 +157,69 @@ void ngx_http_redirectionio_read_handler(ngx_event_t *rev) {
157157
ngx_connection_t *c;
158158
ngx_http_request_t *r;
159159
ngx_http_redirectionio_ctx_t *ctx;
160-
char *read;
161-
uint32_t rlen;
162160
ngx_int_t rv;
163161

164162
c = rev->data;
165163
r = c->data;
166164
ctx = ngx_http_get_module_ctx(r, ngx_http_redirectionio_module);
167-
ctx->resource->peer.connection->read->handler = ngx_http_redirectionio_dummy_handler;
168-
169165

170166
if (rev->timedout) {
171-
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "[redirectionio] connection timeout while reading, skipping module for this request");
172-
173167
ctx->connection_error = 1;
174168
ctx->read_handler(rev, NULL);
169+
ctx->resource->peer.connection->read->handler = ngx_http_redirectionio_dummy_handler;
170+
171+
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "[redirectionio] connection timeout while reading, skipping module for this request");
175172

176173
return;
177174
}
178175

179-
if (rev->timer_set) {
180-
ngx_del_timer(rev);
181-
}
176+
if (ctx->action_string_len == 0) {
177+
// Read uint32
178+
rv = ngx_http_redirectionio_read_uint32(c, &ctx->action_string_len);
182179

183-
// Read uint32
184-
rv = ngx_http_redirectionio_read_uint32(c, &rlen);
180+
if (rv != NGX_OK) {
181+
ctx->connection_error = 1;
182+
ctx->read_handler(rev, NULL);
183+
ctx->resource->peer.connection->read->handler = ngx_http_redirectionio_dummy_handler;
185184

186-
if (rv != NGX_OK) {
187-
ctx->connection_error = 1;
188-
ctx->read_handler(rev, NULL);
185+
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "[redirectionio] connection error while reading length, skipping module for this request");
189186

190-
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "[redirectionio] connection error while reading length, skipping module for this request");
187+
if (rev->timer_set) {
188+
ngx_del_timer(rev);
189+
}
191190

192-
return;
191+
return;
192+
}
193+
194+
ctx->action_string = (char *)ngx_pcalloc(r->pool, ctx->action_string_len + 1);
193195
}
194196

195-
// Read string
196-
read = (char *)ngx_pcalloc(r->pool, rlen + 1);
197-
rv = ngx_http_redirectionio_read_string(c, read, rlen);
197+
rv = ngx_http_redirectionio_read_string(c, ctx->action_string, ctx->action_string_len, &ctx->action_string_readed);
198+
199+
if (rv == NGX_AGAIN) {
200+
return;
201+
}
198202

199203
if (rv != NGX_OK) {
200204
ctx->connection_error = 1;
201205
ctx->read_handler(rev, NULL);
206+
ctx->resource->peer.connection->read->handler = ngx_http_redirectionio_dummy_handler;
202207

203-
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "[redirectionio] connection error while reading length, skipping module for this request");
208+
if (rev->timer_set) {
209+
ngx_del_timer(rev);
210+
}
211+
212+
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "[redirectionio] connection error while reading string: %d, skipping module for this request", rv);
204213

205214
return;
206215
}
207216

208-
ctx->read_handler(rev, (const char *)read);
217+
if (rev->timer_set) {
218+
ngx_del_timer(rev);
219+
}
220+
221+
ctx->resource->peer.connection->read->handler = ngx_http_redirectionio_dummy_handler;
222+
ctx->read_handler(rev, (const char *)ctx->action_string);
209223
}
210224

211225
static void ngx_http_redirectionio_dummy_handler(ngx_event_t *wev) {
@@ -233,19 +247,17 @@ static ngx_int_t ngx_http_redirectionio_read_uint32(ngx_connection_t *c, uint32_
233247
return NGX_OK;
234248
}
235249

236-
static ngx_int_t ngx_http_redirectionio_read_string(ngx_connection_t *c, char *string, ssize_t buf_size) {
250+
static ngx_int_t ngx_http_redirectionio_read_string(ngx_connection_t *c, char *string, ssize_t buf_size, ssize_t *readed) {
237251
ssize_t srlen = buf_size;
238-
ssize_t sdrlen = 0;
239252

240-
while (sdrlen < buf_size) {
241-
srlen = ngx_recv(c, (u_char *)(string + sdrlen), srlen);
253+
while (*readed < buf_size) {
254+
srlen = ngx_recv(c, (u_char *)(string + *readed), buf_size - *readed);
242255

243-
if (srlen <= 0) {
256+
if (srlen < 0) {
244257
return srlen;
245258
}
246259

247-
sdrlen += srlen;
248-
srlen = buf_size - sdrlen;
260+
*readed += srlen;
249261
}
250262

251263
*(string + buf_size) = '\0';

0 commit comments

Comments
 (0)