0
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #include <ngx_config.h>
|
|
8 #include <ngx_core.h>
|
|
9 #include <ngx_event.h>
|
|
10 #include <ngx_http.h>
|
|
11
|
|
12
|
58
|
13 static void ngx_http_read_client_request_body_handler(ngx_http_request_t *r);
|
|
14 static ngx_int_t ngx_http_do_read_client_request_body(ngx_http_request_t *r);
|
182
|
15 static ngx_int_t ngx_http_write_request_body(ngx_http_request_t *r,
|
|
16 ngx_chain_t *body);
|
|
17 static void ngx_http_finalize_request_body(ngx_http_request_t *r, ngx_int_t rc);
|
|
18 static void ngx_http_read_discarded_body_handler(ngx_http_request_t *r);
|
|
19 static ngx_int_t ngx_http_read_discarded_body(ngx_http_request_t *r);
|
|
20
|
0
|
21
|
28
|
22 /*
|
|
23 * on completion ngx_http_read_client_request_body() adds to
|
|
24 * r->request_body->bufs one or two bufs:
|
|
25 * *) one memory buf that was preread in r->header_in;
|
|
26 * *) one memory or file buf that contains the rest of the body
|
|
27 */
|
0
|
28
|
50
|
29 ngx_int_t
|
|
30 ngx_http_read_client_request_body(ngx_http_request_t *r,
|
|
31 ngx_http_client_body_handler_pt post_handler)
|
0
|
32 {
|
212
|
33 size_t preread;
|
|
34 ssize_t size;
|
0
|
35 ngx_buf_t *b;
|
182
|
36 ngx_chain_t *cl, **next;
|
238
|
37 ngx_temp_file_t *tf;
|
28
|
38 ngx_http_request_body_t *rb;
|
0
|
39 ngx_http_core_loc_conf_t *clcf;
|
|
40
|
86
|
41 if (r->request_body || r->discard_body) {
|
58
|
42 post_handler(r);
|
|
43 return NGX_OK;
|
|
44 }
|
|
45
|
50
|
46 rb = ngx_pcalloc(r->pool, sizeof(ngx_http_request_body_t));
|
|
47 if (rb == NULL) {
|
28
|
48 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
49 }
|
|
50
|
|
51 r->request_body = rb;
|
|
52
|
238
|
53 if (r->headers_in.content_length_n < 0) {
|
|
54 post_handler(r);
|
|
55 return NGX_OK;
|
|
56 }
|
|
57
|
|
58 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
59
|
|
60 if (r->headers_in.content_length_n == 0) {
|
|
61
|
|
62 if (r->request_body_in_file_only) {
|
|
63 tf = ngx_pcalloc(r->pool, sizeof(ngx_temp_file_t));
|
|
64 if (tf == NULL) {
|
|
65 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
66 }
|
|
67
|
|
68 tf->file.fd = NGX_INVALID_FILE;
|
|
69 tf->file.log = r->connection->log;
|
|
70 tf->path = clcf->client_body_temp_path;
|
|
71 tf->pool = r->pool;
|
|
72 tf->warn = "a client request body is buffered to a temporary file";
|
|
73 tf->log_level = r->request_body_file_log_level;
|
|
74 tf->persistent = r->request_body_in_persistent_file;
|
|
75
|
|
76 if (r->request_body_file_group_access) {
|
276
|
77 tf->access = 0660;
|
238
|
78 }
|
|
79
|
|
80 rb->temp_file = tf;
|
|
81
|
|
82 if (ngx_create_temp_file(&tf->file, tf->path, tf->pool,
|
276
|
83 tf->persistent, tf->access)
|
238
|
84 != NGX_OK)
|
|
85 {
|
|
86 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
87 }
|
|
88 }
|
|
89
|
28
|
90 post_handler(r);
|
|
91 return NGX_OK;
|
|
92 }
|
|
93
|
|
94 rb->post_handler = post_handler;
|
|
95
|
|
96 /*
|
|
97 * set by ngx_pcalloc():
|
|
98 *
|
|
99 * rb->bufs = NULL;
|
|
100 * rb->buf = NULL;
|
|
101 * rb->rest = 0;
|
|
102 */
|
|
103
|
182
|
104 preread = r->header_in->last - r->header_in->pos;
|
0
|
105
|
182
|
106 if (preread) {
|
0
|
107
|
|
108 /* there is the pre-read part of the request body */
|
|
109
|
182
|
110 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
111 "http client request body preread %uz", preread);
|
|
112
|
50
|
113 b = ngx_calloc_buf(r->pool);
|
|
114 if (b == NULL) {
|
28
|
115 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
116 }
|
0
|
117
|
|
118 b->temporary = 1;
|
182
|
119 b->start = r->header_in->pos;
|
|
120 b->pos = r->header_in->pos;
|
|
121 b->last = r->header_in->last;
|
|
122 b->end = r->header_in->end;
|
0
|
123
|
50
|
124 rb->bufs = ngx_alloc_chain_link(r->pool);
|
|
125 if (rb->bufs == NULL) {
|
28
|
126 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
127 }
|
|
128
|
|
129 rb->bufs->buf = b;
|
|
130 rb->bufs->next = NULL;
|
0
|
131
|
216
|
132 rb->buf = b;
|
|
133
|
214
|
134 if ((off_t) preread >= r->headers_in.content_length_n) {
|
0
|
135
|
|
136 /* the whole request body was pre-read */
|
|
137
|
212
|
138 r->header_in->pos += (size_t) r->headers_in.content_length_n;
|
24
|
139 r->request_length += r->headers_in.content_length_n;
|
0
|
140
|
182
|
141 if (r->request_body_in_file_only) {
|
|
142 if (ngx_http_write_request_body(r, rb->bufs) != NGX_OK) {
|
|
143 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
144 }
|
|
145 }
|
|
146
|
28
|
147 post_handler(r);
|
0
|
148
|
|
149 return NGX_OK;
|
|
150 }
|
|
151
|
182
|
152 /*
|
|
153 * to not consider the body as pipelined request in
|
|
154 * ngx_http_set_keepalive()
|
|
155 */
|
0
|
156 r->header_in->pos = r->header_in->last;
|
182
|
157
|
|
158 r->request_length += preread;
|
|
159
|
|
160 rb->rest = r->headers_in.content_length_n - preread;
|
|
161
|
214
|
162 if (rb->rest <= (off_t) (b->end - b->last)) {
|
182
|
163
|
|
164 /* the whole request body may be placed in r->header_in */
|
|
165
|
276
|
166 rb->to_write = rb->bufs;
|
|
167
|
182
|
168 r->read_event_handler = ngx_http_read_client_request_body_handler;
|
|
169
|
|
170 return ngx_http_do_read_client_request_body(r);
|
|
171 }
|
|
172
|
|
173 next = &rb->bufs->next;
|
|
174
|
|
175 } else {
|
|
176 b = NULL;
|
|
177 rb->rest = r->headers_in.content_length_n;
|
|
178 next = &rb->bufs;
|
0
|
179 }
|
|
180
|
182
|
181 size = clcf->client_body_buffer_size;
|
|
182 size += size >> 2;
|
0
|
183
|
212
|
184 if (rb->rest < size) {
|
|
185 size = (ssize_t) rb->rest;
|
0
|
186
|
182
|
187 if (r->request_body_in_single_buf) {
|
|
188 size += preread;
|
|
189 }
|
|
190
|
0
|
191 } else {
|
|
192 size = clcf->client_body_buffer_size;
|
182
|
193
|
|
194 /* disable copying buffer for r->request_body_in_single_buf */
|
|
195 b = NULL;
|
0
|
196 }
|
|
197
|
50
|
198 rb->buf = ngx_create_temp_buf(r->pool, size);
|
|
199 if (rb->buf == NULL) {
|
28
|
200 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
201 }
|
0
|
202
|
50
|
203 cl = ngx_alloc_chain_link(r->pool);
|
|
204 if (cl == NULL) {
|
28
|
205 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
206 }
|
0
|
207
|
28
|
208 cl->buf = rb->buf;
|
|
209 cl->next = NULL;
|
|
210
|
182
|
211 if (b && r->request_body_in_single_buf) {
|
|
212 size = b->last - b->pos;
|
|
213 ngx_memcpy(rb->buf->pos, b->pos, size);
|
|
214 rb->buf->last += size;
|
|
215
|
|
216 next = &rb->bufs;
|
|
217 }
|
|
218
|
|
219 *next = cl;
|
|
220
|
|
221 if (r->request_body_in_file_only || r->request_body_in_single_buf) {
|
|
222 rb->to_write = rb->bufs;
|
0
|
223
|
|
224 } else {
|
182
|
225 rb->to_write = rb->bufs->next ? rb->bufs->next : rb->bufs;
|
0
|
226 }
|
|
227
|
58
|
228 r->read_event_handler = ngx_http_read_client_request_body_handler;
|
0
|
229
|
58
|
230 return ngx_http_do_read_client_request_body(r);
|
0
|
231 }
|
|
232
|
|
233
|
50
|
234 static void
|
58
|
235 ngx_http_read_client_request_body_handler(ngx_http_request_t *r)
|
0
|
236 {
|
58
|
237 ngx_int_t rc;
|
0
|
238
|
58
|
239 if (r->connection->read->timedout) {
|
126
|
240 r->connection->timedout = 1;
|
182
|
241 ngx_http_finalize_request_body(r, NGX_HTTP_REQUEST_TIME_OUT);
|
0
|
242 return;
|
|
243 }
|
|
244
|
58
|
245 rc = ngx_http_do_read_client_request_body(r);
|
0
|
246
|
|
247 if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
|
182
|
248 ngx_http_finalize_request_body(r, rc);
|
0
|
249 }
|
|
250 }
|
|
251
|
|
252
|
50
|
253 static ngx_int_t
|
58
|
254 ngx_http_do_read_client_request_body(ngx_http_request_t *r)
|
0
|
255 {
|
|
256 size_t size;
|
|
257 ssize_t n;
|
|
258 ngx_buf_t *b;
|
58
|
259 ngx_connection_t *c;
|
28
|
260 ngx_http_request_body_t *rb;
|
0
|
261 ngx_http_core_loc_conf_t *clcf;
|
|
262
|
58
|
263 c = r->connection;
|
28
|
264 rb = r->request_body;
|
0
|
265
|
|
266 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
|
267 "http read client request body");
|
|
268
|
|
269 for ( ;; ) {
|
256
|
270 for ( ;; ) {
|
|
271 if (rb->buf->last == rb->buf->end) {
|
|
272
|
|
273 if (ngx_http_write_request_body(r, rb->to_write) != NGX_OK) {
|
|
274 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
275 }
|
28
|
276
|
256
|
277 rb->to_write = rb->bufs->next ? rb->bufs->next : rb->bufs;
|
|
278 rb->buf->last = rb->buf->start;
|
|
279 }
|
|
280
|
|
281 size = rb->buf->end - rb->buf->last;
|
|
282
|
|
283 if ((off_t) size > rb->rest) {
|
|
284 size = (size_t) rb->rest;
|
0
|
285 }
|
|
286
|
256
|
287 n = c->recv(c, rb->buf->last, size);
|
|
288
|
|
289 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
|
290 "http client request body recv %z", n);
|
0
|
291
|
256
|
292 if (n == NGX_AGAIN) {
|
|
293 break;
|
|
294 }
|
|
295
|
|
296 if (n == 0) {
|
|
297 ngx_log_error(NGX_LOG_INFO, c->log, 0,
|
|
298 "client closed prematurely connection");
|
|
299 }
|
0
|
300
|
256
|
301 if (n == 0 || n == NGX_ERROR) {
|
|
302 c->error = 1;
|
|
303 return NGX_HTTP_BAD_REQUEST;
|
|
304 }
|
|
305
|
|
306 rb->buf->last += n;
|
|
307 rb->rest -= n;
|
|
308 r->request_length += n;
|
|
309
|
|
310 if (rb->rest == 0) {
|
|
311 break;
|
|
312 }
|
|
313
|
|
314 if (rb->buf->last < rb->buf->end) {
|
|
315 break;
|
|
316 }
|
0
|
317 }
|
|
318
|
|
319 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
256
|
320 "http client request body rest %uz", rb->rest);
|
0
|
321
|
28
|
322 if (rb->rest == 0) {
|
0
|
323 break;
|
|
324 }
|
|
325
|
256
|
326 if (!c->read->ready) {
|
|
327 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
328 ngx_add_timer(c->read, clcf->client_body_timeout);
|
0
|
329
|
256
|
330 if (ngx_handle_read_event(c->read, 0) == NGX_ERROR) {
|
|
331 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
332 }
|
122
|
333
|
256
|
334 return NGX_AGAIN;
|
122
|
335 }
|
0
|
336 }
|
|
337
|
122
|
338 if (c->read->timer_set) {
|
|
339 ngx_del_timer(c->read);
|
|
340 }
|
|
341
|
182
|
342 if (rb->temp_file || r->request_body_in_file_only) {
|
0
|
343
|
|
344 /* save the last part */
|
28
|
345
|
182
|
346 if (ngx_http_write_request_body(r, rb->to_write) != NGX_OK) {
|
0
|
347 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
348 }
|
|
349
|
50
|
350 b = ngx_calloc_buf(r->pool);
|
|
351 if (b == NULL) {
|
0
|
352 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
353 }
|
|
354
|
|
355 b->in_file = 1;
|
|
356 b->file_pos = 0;
|
28
|
357 b->file_last = rb->temp_file->file.offset;
|
|
358 b->file = &rb->temp_file->file;
|
0
|
359
|
28
|
360 if (rb->bufs->next) {
|
|
361 rb->bufs->next->buf = b;
|
0
|
362
|
|
363 } else {
|
28
|
364 rb->bufs->buf = b;
|
0
|
365 }
|
|
366 }
|
|
367
|
182
|
368 if (r->request_body_in_file_only && rb->bufs->next) {
|
|
369 rb->bufs = rb->bufs->next;
|
|
370 }
|
|
371
|
28
|
372 rb->post_handler(r);
|
0
|
373
|
|
374 return NGX_OK;
|
|
375 }
|
182
|
376
|
|
377
|
|
378 static ngx_int_t
|
|
379 ngx_http_write_request_body(ngx_http_request_t *r, ngx_chain_t *body)
|
|
380 {
|
|
381 ssize_t n;
|
|
382 ngx_temp_file_t *tf;
|
|
383 ngx_http_request_body_t *rb;
|
|
384 ngx_http_core_loc_conf_t *clcf;
|
|
385
|
|
386 rb = r->request_body;
|
|
387
|
|
388 if (rb->temp_file == NULL) {
|
|
389 tf = ngx_pcalloc(r->pool, sizeof(ngx_temp_file_t));
|
|
390 if (tf == NULL) {
|
|
391 return NGX_ERROR;
|
|
392 }
|
|
393
|
|
394 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
395
|
|
396 tf->file.fd = NGX_INVALID_FILE;
|
|
397 tf->file.log = r->connection->log;
|
|
398 tf->path = clcf->client_body_temp_path;
|
|
399 tf->pool = r->pool;
|
|
400 tf->warn = "a client request body is buffered to a temporary file";
|
186
|
401 tf->log_level = r->request_body_file_log_level;
|
182
|
402 tf->persistent = r->request_body_in_persistent_file;
|
|
403
|
|
404 if (r->request_body_file_group_access) {
|
276
|
405 tf->access = 0660;
|
182
|
406 }
|
|
407
|
|
408 rb->temp_file = tf;
|
|
409 }
|
|
410
|
|
411 n = ngx_write_chain_to_temp_file(rb->temp_file, body);
|
|
412
|
|
413 /* TODO: n == 0 or not complete and level event */
|
|
414
|
|
415 if (n == NGX_ERROR) {
|
|
416 return NGX_ERROR;
|
|
417 }
|
|
418
|
|
419 rb->temp_file->offset += n;
|
|
420
|
|
421 return NGX_OK;
|
|
422 }
|
|
423
|
|
424
|
|
425 static void
|
|
426 ngx_http_finalize_request_body(ngx_http_request_t *r, ngx_int_t rc)
|
|
427 {
|
|
428 if (r->request_body->temp_file
|
|
429 && r->request_body_in_persistent_file
|
|
430 && r->request_body_delete_incomplete_file)
|
|
431 {
|
|
432 if (ngx_delete_file(r->request_body->temp_file->file.name.data)
|
|
433 == NGX_FILE_ERROR)
|
|
434 {
|
|
435 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
|
|
436 ngx_delete_file_n " \"%s\" failed",
|
|
437 r->request_body->temp_file->file.name.data);
|
|
438 }
|
|
439 }
|
|
440
|
|
441 ngx_http_finalize_request(r, rc);
|
|
442 }
|
|
443
|
|
444
|
|
445 ngx_int_t
|
|
446 ngx_http_discard_body(ngx_http_request_t *r)
|
|
447 {
|
|
448 ssize_t size;
|
|
449 ngx_event_t *rev;
|
|
450
|
222
|
451 if (r != r->main || r->discard_body) {
|
182
|
452 return NGX_OK;
|
|
453 }
|
|
454
|
|
455 rev = r->connection->read;
|
|
456
|
|
457 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0, "http set discard body");
|
|
458
|
|
459 if (rev->timer_set) {
|
|
460 ngx_del_timer(rev);
|
|
461 }
|
|
462
|
230
|
463 r->discard_body = 1;
|
|
464
|
182
|
465 if (r->headers_in.content_length_n <= 0) {
|
|
466 return NGX_OK;
|
|
467 }
|
|
468
|
|
469 size = r->header_in->last - r->header_in->pos;
|
|
470
|
|
471 if (size) {
|
|
472 if (r->headers_in.content_length_n > size) {
|
|
473 r->headers_in.content_length_n -= size;
|
|
474
|
|
475 } else {
|
212
|
476 r->header_in->pos += (size_t) r->headers_in.content_length_n;
|
182
|
477 r->headers_in.content_length_n = 0;
|
|
478 return NGX_OK;
|
|
479 }
|
|
480 }
|
|
481
|
|
482 r->read_event_handler = ngx_http_read_discarded_body_handler;
|
|
483
|
|
484 if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
|
|
485 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
486 }
|
|
487
|
|
488 return ngx_http_read_discarded_body(r);
|
|
489 }
|
|
490
|
|
491
|
|
492 static void
|
|
493 ngx_http_read_discarded_body_handler(ngx_http_request_t *r)
|
|
494 {
|
|
495 ngx_int_t rc;
|
|
496
|
|
497 rc = ngx_http_read_discarded_body(r);
|
|
498
|
|
499 if (rc == NGX_AGAIN) {
|
|
500 if (ngx_handle_read_event(r->connection->read, 0) == NGX_ERROR) {
|
|
501 ngx_http_finalize_request(r, rc);
|
|
502 return;
|
|
503 }
|
|
504 }
|
|
505
|
|
506 if (rc != NGX_OK) {
|
|
507 ngx_http_finalize_request(r, rc);
|
|
508 }
|
|
509 }
|
|
510
|
|
511
|
|
512 static ngx_int_t
|
|
513 ngx_http_read_discarded_body(ngx_http_request_t *r)
|
|
514 {
|
212
|
515 size_t size;
|
|
516 ssize_t n;
|
182
|
517 u_char buffer[NGX_HTTP_DISCARD_BUFFER_SIZE];
|
|
518
|
|
519 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
520 "http read discarded body");
|
|
521
|
|
522 if (r->headers_in.content_length_n == 0) {
|
|
523 return NGX_OK;
|
|
524 }
|
|
525
|
212
|
526 size = (r->headers_in.content_length_n > NGX_HTTP_DISCARD_BUFFER_SIZE) ?
|
|
527 NGX_HTTP_DISCARD_BUFFER_SIZE:
|
|
528 (size_t) r->headers_in.content_length_n;
|
182
|
529
|
|
530 n = r->connection->recv(r->connection, buffer, size);
|
|
531
|
|
532 if (n == NGX_ERROR) {
|
|
533
|
|
534 r->connection->error = 1;
|
|
535
|
|
536 /*
|
|
537 * if a client request body is discarded then we already set
|
|
538 * some HTTP response code for client and we can ignore the error
|
|
539 */
|
|
540
|
|
541 return NGX_OK;
|
|
542 }
|
|
543
|
|
544 if (n == NGX_AGAIN) {
|
|
545 return NGX_AGAIN;
|
|
546 }
|
|
547
|
|
548 r->headers_in.content_length_n -= n;
|
|
549
|
|
550 return NGX_OK;
|
|
551 }
|