Skip to content

Commit 2a70921

Browse files
committed
Request body: introduced rb->last_saved flag.
It indicates that the last buffer was received by the save filter, and can be used to check this at higher levels. To be used in the following changes.
1 parent fd9d43b commit 2a70921

File tree

2 files changed

+54
-15
lines changed

2 files changed

+54
-15
lines changed

src/http/ngx_http_request.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ typedef struct {
302302
ngx_chain_t *busy;
303303
ngx_http_chunked_t *chunked;
304304
ngx_http_client_body_handler_pt post_handler;
305+
unsigned last_saved:1;
305306
} ngx_http_request_body_t;
306307

307308

src/http/ngx_http_request_body.c

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ ngx_http_read_client_request_body(ngx_http_request_t *r,
6969
* rb->busy = NULL;
7070
* rb->chunked = NULL;
7171
* rb->received = 0;
72+
* rb->last_saved = 0;
7273
*/
7374

7475
rb->rest = -1;
@@ -941,15 +942,32 @@ ngx_http_request_body_length_filter(ngx_http_request_t *r, ngx_chain_t *in)
941942

942943
rb = r->request_body;
943944

945+
out = NULL;
946+
ll = &out;
947+
944948
if (rb->rest == -1) {
945949
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
946950
"http request body content length filter");
947951

948952
rb->rest = r->headers_in.content_length_n;
949-
}
950953

951-
out = NULL;
952-
ll = &out;
954+
if (rb->rest == 0) {
955+
956+
tl = ngx_chain_get_free_buf(r->pool, &rb->free);
957+
if (tl == NULL) {
958+
return NGX_HTTP_INTERNAL_SERVER_ERROR;
959+
}
960+
961+
b = tl->buf;
962+
963+
ngx_memzero(b, sizeof(ngx_buf_t));
964+
965+
b->last_buf = 1;
966+
967+
*ll = tl;
968+
ll = &tl->next;
969+
}
970+
}
953971

954972
for (cl = in; cl; cl = cl->next) {
955973

@@ -1013,6 +1031,9 @@ ngx_http_request_body_chunked_filter(ngx_http_request_t *r, ngx_chain_t *in)
10131031

10141032
rb = r->request_body;
10151033

1034+
out = NULL;
1035+
ll = &out;
1036+
10161037
if (rb->rest == -1) {
10171038

10181039
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
@@ -1029,9 +1050,6 @@ ngx_http_request_body_chunked_filter(ngx_http_request_t *r, ngx_chain_t *in)
10291050
rb->rest = cscf->large_client_header_buffers.size;
10301051
}
10311052

1032-
out = NULL;
1033-
ll = &out;
1034-
10351053
for (cl = in; cl; cl = cl->next) {
10361054

10371055
b = NULL;
@@ -1188,15 +1206,16 @@ ngx_int_t
11881206
ngx_http_request_body_save_filter(ngx_http_request_t *r, ngx_chain_t *in)
11891207
{
11901208
ngx_buf_t *b;
1191-
ngx_chain_t *cl;
1209+
ngx_chain_t *cl, *tl, **ll;
11921210
ngx_http_request_body_t *rb;
11931211

11941212
rb = r->request_body;
11951213

1196-
#if (NGX_DEBUG)
1214+
ll = &rb->bufs;
11971215

1198-
#if 0
11991216
for (cl = rb->bufs; cl; cl = cl->next) {
1217+
1218+
#if 0
12001219
ngx_log_debug7(NGX_LOG_DEBUG_EVENT, r->connection->log, 0,
12011220
"http body old buf t:%d f:%d %p, pos %p, size: %z "
12021221
"file: %O, size: %O",
@@ -1205,10 +1224,13 @@ ngx_http_request_body_save_filter(ngx_http_request_t *r, ngx_chain_t *in)
12051224
cl->buf->last - cl->buf->pos,
12061225
cl->buf->file_pos,
12071226
cl->buf->file_last - cl->buf->file_pos);
1208-
}
12091227
#endif
12101228

1229+
ll = &cl->next;
1230+
}
1231+
12111232
for (cl = in; cl; cl = cl->next) {
1233+
12121234
ngx_log_debug7(NGX_LOG_DEBUG_EVENT, r->connection->log, 0,
12131235
"http body new buf t:%d f:%d %p, pos %p, size: %z "
12141236
"file: %O, size: %O",
@@ -1217,16 +1239,32 @@ ngx_http_request_body_save_filter(ngx_http_request_t *r, ngx_chain_t *in)
12171239
cl->buf->last - cl->buf->pos,
12181240
cl->buf->file_pos,
12191241
cl->buf->file_last - cl->buf->file_pos);
1220-
}
12211242

1222-
#endif
1243+
if (cl->buf->last_buf) {
12231244

1224-
/* TODO: coalesce neighbouring buffers */
1245+
if (rb->last_saved) {
1246+
ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
1247+
"duplicate last buf in save filter");
1248+
*ll = NULL;
1249+
return NGX_HTTP_INTERNAL_SERVER_ERROR;
1250+
}
12251251

1226-
if (ngx_chain_add_copy(r->pool, &rb->bufs, in) != NGX_OK) {
1227-
return NGX_HTTP_INTERNAL_SERVER_ERROR;
1252+
rb->last_saved = 1;
1253+
}
1254+
1255+
tl = ngx_alloc_chain_link(r->pool);
1256+
if (tl == NULL) {
1257+
*ll = NULL;
1258+
return NGX_HTTP_INTERNAL_SERVER_ERROR;
1259+
}
1260+
1261+
tl->buf = cl->buf;
1262+
*ll = tl;
1263+
ll = &tl->next;
12281264
}
12291265

1266+
*ll = NULL;
1267+
12301268
if (r->request_body_no_buffering) {
12311269
return NGX_OK;
12321270
}

0 commit comments

Comments
 (0)