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) {
|
|
77 tf->mode = 0660;
|
|
78 }
|
|
79
|
|
80 rb->temp_file = tf;
|
|
81
|
|
82 if (ngx_create_temp_file(&tf->file, tf->path, tf->pool,
|
|
83 tf->persistent, tf->mode)
|
|
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
|
|
166 r->read_event_handler = ngx_http_read_client_request_body_handler;
|
|
167
|
|
168 return ngx_http_do_read_client_request_body(r);
|
|
169 }
|
|
170
|
|
171 next = &rb->bufs->next;
|
|
172
|
|
173 } else {
|
|
174 b = NULL;
|
|
175 rb->rest = r->headers_in.content_length_n;
|
|
176 next = &rb->bufs;
|
0
|
177 }
|
|
178
|
182
|
179 size = clcf->client_body_buffer_size;
|
|
180 size += size >> 2;
|
0
|
181
|
212
|
182 if (rb->rest < size) {
|
|
183 size = (ssize_t) rb->rest;
|
0
|
184
|
182
|
185 if (r->request_body_in_single_buf) {
|
|
186 size += preread;
|
|
187 }
|
|
188
|
0
|
189 } else {
|
|
190 size = clcf->client_body_buffer_size;
|
182
|
191
|
|
192 /* disable copying buffer for r->request_body_in_single_buf */
|
|
193 b = NULL;
|
0
|
194 }
|
|
195
|
50
|
196 rb->buf = ngx_create_temp_buf(r->pool, size);
|
|
197 if (rb->buf == NULL) {
|
28
|
198 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
199 }
|
0
|
200
|
50
|
201 cl = ngx_alloc_chain_link(r->pool);
|
|
202 if (cl == NULL) {
|
28
|
203 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
204 }
|
0
|
205
|
28
|
206 cl->buf = rb->buf;
|
|
207 cl->next = NULL;
|
|
208
|
182
|
209 if (b && r->request_body_in_single_buf) {
|
|
210 size = b->last - b->pos;
|
|
211 ngx_memcpy(rb->buf->pos, b->pos, size);
|
|
212 rb->buf->last += size;
|
|
213
|
|
214 next = &rb->bufs;
|
|
215 }
|
|
216
|
|
217 *next = cl;
|
|
218
|
|
219 if (r->request_body_in_file_only || r->request_body_in_single_buf) {
|
|
220 rb->to_write = rb->bufs;
|
0
|
221
|
|
222 } else {
|
182
|
223 rb->to_write = rb->bufs->next ? rb->bufs->next : rb->bufs;
|
0
|
224 }
|
|
225
|
58
|
226 r->read_event_handler = ngx_http_read_client_request_body_handler;
|
0
|
227
|
58
|
228 return ngx_http_do_read_client_request_body(r);
|
0
|
229 }
|
|
230
|
|
231
|
50
|
232 static void
|
58
|
233 ngx_http_read_client_request_body_handler(ngx_http_request_t *r)
|
0
|
234 {
|
58
|
235 ngx_int_t rc;
|
0
|
236
|
58
|
237 if (r->connection->read->timedout) {
|
126
|
238 r->connection->timedout = 1;
|
182
|
239 ngx_http_finalize_request_body(r, NGX_HTTP_REQUEST_TIME_OUT);
|
0
|
240 return;
|
|
241 }
|
|
242
|
58
|
243 rc = ngx_http_do_read_client_request_body(r);
|
0
|
244
|
|
245 if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
|
182
|
246 ngx_http_finalize_request_body(r, rc);
|
0
|
247 }
|
|
248 }
|
|
249
|
|
250
|
50
|
251 static ngx_int_t
|
58
|
252 ngx_http_do_read_client_request_body(ngx_http_request_t *r)
|
0
|
253 {
|
|
254 size_t size;
|
|
255 ssize_t n;
|
|
256 ngx_buf_t *b;
|
58
|
257 ngx_connection_t *c;
|
28
|
258 ngx_http_request_body_t *rb;
|
0
|
259 ngx_http_core_loc_conf_t *clcf;
|
|
260
|
58
|
261 c = r->connection;
|
28
|
262 rb = r->request_body;
|
0
|
263
|
|
264 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
|
265 "http read client request body");
|
|
266
|
|
267 for ( ;; ) {
|
256
|
268 for ( ;; ) {
|
|
269 if (rb->buf->last == rb->buf->end) {
|
|
270
|
|
271 if (ngx_http_write_request_body(r, rb->to_write) != NGX_OK) {
|
|
272 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
273 }
|
28
|
274
|
256
|
275 rb->to_write = rb->bufs->next ? rb->bufs->next : rb->bufs;
|
|
276 rb->buf->last = rb->buf->start;
|
|
277 }
|
|
278
|
|
279 size = rb->buf->end - rb->buf->last;
|
|
280
|
|
281 if ((off_t) size > rb->rest) {
|
|
282 size = (size_t) rb->rest;
|
0
|
283 }
|
|
284
|
256
|
285 n = c->recv(c, rb->buf->last, size);
|
|
286
|
|
287 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
|
288 "http client request body recv %z", n);
|
0
|
289
|
256
|
290 if (n == NGX_AGAIN) {
|
|
291 break;
|
|
292 }
|
|
293
|
|
294 if (n == 0) {
|
|
295 ngx_log_error(NGX_LOG_INFO, c->log, 0,
|
|
296 "client closed prematurely connection");
|
|
297 }
|
0
|
298
|
256
|
299 if (n == 0 || n == NGX_ERROR) {
|
|
300 c->error = 1;
|
|
301 return NGX_HTTP_BAD_REQUEST;
|
|
302 }
|
|
303
|
|
304 rb->buf->last += n;
|
|
305 rb->rest -= n;
|
|
306 r->request_length += n;
|
|
307
|
|
308 if (rb->rest == 0) {
|
|
309 break;
|
|
310 }
|
|
311
|
|
312 if (rb->buf->last < rb->buf->end) {
|
|
313 break;
|
|
314 }
|
0
|
315 }
|
|
316
|
|
317 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
256
|
318 "http client request body rest %uz", rb->rest);
|
0
|
319
|
28
|
320 if (rb->rest == 0) {
|
0
|
321 break;
|
|
322 }
|
|
323
|
256
|
324 if (!c->read->ready) {
|
|
325 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
326 ngx_add_timer(c->read, clcf->client_body_timeout);
|
0
|
327
|
256
|
328 if (ngx_handle_read_event(c->read, 0) == NGX_ERROR) {
|
|
329 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
330 }
|
122
|
331
|
256
|
332 return NGX_AGAIN;
|
122
|
333 }
|
0
|
334 }
|
|
335
|
122
|
336 if (c->read->timer_set) {
|
|
337 ngx_del_timer(c->read);
|
|
338 }
|
|
339
|
182
|
340 if (rb->temp_file || r->request_body_in_file_only) {
|
0
|
341
|
|
342 /* save the last part */
|
28
|
343
|
182
|
344 if (ngx_http_write_request_body(r, rb->to_write) != NGX_OK) {
|
0
|
345 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
346 }
|
|
347
|
50
|
348 b = ngx_calloc_buf(r->pool);
|
|
349 if (b == NULL) {
|
0
|
350 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
351 }
|
|
352
|
|
353 b->in_file = 1;
|
|
354 b->file_pos = 0;
|
28
|
355 b->file_last = rb->temp_file->file.offset;
|
|
356 b->file = &rb->temp_file->file;
|
0
|
357
|
28
|
358 if (rb->bufs->next) {
|
|
359 rb->bufs->next->buf = b;
|
0
|
360
|
|
361 } else {
|
28
|
362 rb->bufs->buf = b;
|
0
|
363 }
|
|
364 }
|
|
365
|
182
|
366 if (r->request_body_in_file_only && rb->bufs->next) {
|
|
367 rb->bufs = rb->bufs->next;
|
|
368 }
|
|
369
|
28
|
370 rb->post_handler(r);
|
0
|
371
|
|
372 return NGX_OK;
|
|
373 }
|
182
|
374
|
|
375
|
|
376 static ngx_int_t
|
|
377 ngx_http_write_request_body(ngx_http_request_t *r, ngx_chain_t *body)
|
|
378 {
|
|
379 ssize_t n;
|
|
380 ngx_temp_file_t *tf;
|
|
381 ngx_http_request_body_t *rb;
|
|
382 ngx_http_core_loc_conf_t *clcf;
|
|
383
|
|
384 rb = r->request_body;
|
|
385
|
|
386 if (rb->temp_file == NULL) {
|
|
387 tf = ngx_pcalloc(r->pool, sizeof(ngx_temp_file_t));
|
|
388 if (tf == NULL) {
|
|
389 return NGX_ERROR;
|
|
390 }
|
|
391
|
|
392 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
393
|
|
394 tf->file.fd = NGX_INVALID_FILE;
|
|
395 tf->file.log = r->connection->log;
|
|
396 tf->path = clcf->client_body_temp_path;
|
|
397 tf->pool = r->pool;
|
|
398 tf->warn = "a client request body is buffered to a temporary file";
|
186
|
399 tf->log_level = r->request_body_file_log_level;
|
182
|
400 tf->persistent = r->request_body_in_persistent_file;
|
|
401
|
|
402 if (r->request_body_file_group_access) {
|
|
403 tf->mode = 0660;
|
|
404 }
|
|
405
|
|
406 rb->temp_file = tf;
|
|
407 }
|
|
408
|
|
409 n = ngx_write_chain_to_temp_file(rb->temp_file, body);
|
|
410
|
|
411 /* TODO: n == 0 or not complete and level event */
|
|
412
|
|
413 if (n == NGX_ERROR) {
|
|
414 return NGX_ERROR;
|
|
415 }
|
|
416
|
|
417 rb->temp_file->offset += n;
|
|
418
|
|
419 return NGX_OK;
|
|
420 }
|
|
421
|
|
422
|
|
423 static void
|
|
424 ngx_http_finalize_request_body(ngx_http_request_t *r, ngx_int_t rc)
|
|
425 {
|
|
426 if (r->request_body->temp_file
|
|
427 && r->request_body_in_persistent_file
|
|
428 && r->request_body_delete_incomplete_file)
|
|
429 {
|
|
430 if (ngx_delete_file(r->request_body->temp_file->file.name.data)
|
|
431 == NGX_FILE_ERROR)
|
|
432 {
|
|
433 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
|
|
434 ngx_delete_file_n " \"%s\" failed",
|
|
435 r->request_body->temp_file->file.name.data);
|
|
436 }
|
|
437 }
|
|
438
|
|
439 ngx_http_finalize_request(r, rc);
|
|
440 }
|
|
441
|
|
442
|
|
443 ngx_int_t
|
|
444 ngx_http_discard_body(ngx_http_request_t *r)
|
|
445 {
|
|
446 ssize_t size;
|
|
447 ngx_event_t *rev;
|
|
448
|
222
|
449 if (r != r->main || r->discard_body) {
|
182
|
450 return NGX_OK;
|
|
451 }
|
|
452
|
|
453 rev = r->connection->read;
|
|
454
|
|
455 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0, "http set discard body");
|
|
456
|
|
457 if (rev->timer_set) {
|
|
458 ngx_del_timer(rev);
|
|
459 }
|
|
460
|
230
|
461 r->discard_body = 1;
|
|
462
|
182
|
463 if (r->headers_in.content_length_n <= 0) {
|
|
464 return NGX_OK;
|
|
465 }
|
|
466
|
|
467 size = r->header_in->last - r->header_in->pos;
|
|
468
|
|
469 if (size) {
|
|
470 if (r->headers_in.content_length_n > size) {
|
|
471 r->headers_in.content_length_n -= size;
|
|
472
|
|
473 } else {
|
212
|
474 r->header_in->pos += (size_t) r->headers_in.content_length_n;
|
182
|
475 r->headers_in.content_length_n = 0;
|
|
476 return NGX_OK;
|
|
477 }
|
|
478 }
|
|
479
|
|
480 r->read_event_handler = ngx_http_read_discarded_body_handler;
|
|
481
|
|
482 if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
|
|
483 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
484 }
|
|
485
|
|
486 return ngx_http_read_discarded_body(r);
|
|
487 }
|
|
488
|
|
489
|
|
490 static void
|
|
491 ngx_http_read_discarded_body_handler(ngx_http_request_t *r)
|
|
492 {
|
|
493 ngx_int_t rc;
|
|
494
|
|
495 rc = ngx_http_read_discarded_body(r);
|
|
496
|
|
497 if (rc == NGX_AGAIN) {
|
|
498 if (ngx_handle_read_event(r->connection->read, 0) == NGX_ERROR) {
|
|
499 ngx_http_finalize_request(r, rc);
|
|
500 return;
|
|
501 }
|
|
502 }
|
|
503
|
|
504 if (rc != NGX_OK) {
|
|
505 ngx_http_finalize_request(r, rc);
|
|
506 }
|
|
507 }
|
|
508
|
|
509
|
|
510 static ngx_int_t
|
|
511 ngx_http_read_discarded_body(ngx_http_request_t *r)
|
|
512 {
|
212
|
513 size_t size;
|
|
514 ssize_t n;
|
182
|
515 u_char buffer[NGX_HTTP_DISCARD_BUFFER_SIZE];
|
|
516
|
|
517 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
518 "http read discarded body");
|
|
519
|
|
520 if (r->headers_in.content_length_n == 0) {
|
|
521 return NGX_OK;
|
|
522 }
|
|
523
|
212
|
524 size = (r->headers_in.content_length_n > NGX_HTTP_DISCARD_BUFFER_SIZE) ?
|
|
525 NGX_HTTP_DISCARD_BUFFER_SIZE:
|
|
526 (size_t) r->headers_in.content_length_n;
|
182
|
527
|
|
528 n = r->connection->recv(r->connection, buffer, size);
|
|
529
|
|
530 if (n == NGX_ERROR) {
|
|
531
|
|
532 r->connection->error = 1;
|
|
533
|
|
534 /*
|
|
535 * if a client request body is discarded then we already set
|
|
536 * some HTTP response code for client and we can ignore the error
|
|
537 */
|
|
538
|
|
539 return NGX_OK;
|
|
540 }
|
|
541
|
|
542 if (n == NGX_AGAIN) {
|
|
543 return NGX_AGAIN;
|
|
544 }
|
|
545
|
|
546 r->headers_in.content_length_n -= n;
|
|
547
|
|
548 return NGX_OK;
|
|
549 }
|