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
|
|
13 static void ngx_http_read_client_request_body_handler(ngx_event_t *rev);
|
28
|
14 static ngx_int_t ngx_http_do_read_client_request_body(ngx_http_request_t *r,
|
50
|
15 ngx_connection_t *c);
|
0
|
16
|
28
|
17 /*
|
|
18 * on completion ngx_http_read_client_request_body() adds to
|
|
19 * r->request_body->bufs one or two bufs:
|
|
20 * *) one memory buf that was preread in r->header_in;
|
|
21 * *) one memory or file buf that contains the rest of the body
|
|
22 */
|
0
|
23
|
50
|
24 ngx_int_t
|
|
25 ngx_http_read_client_request_body(ngx_http_request_t *r,
|
|
26 ngx_http_client_body_handler_pt post_handler)
|
28
|
27
|
0
|
28 {
|
|
29 ssize_t size;
|
|
30 ngx_buf_t *b;
|
|
31 ngx_chain_t *cl;
|
50
|
32 ngx_connection_t *c;
|
28
|
33 ngx_http_request_body_t *rb;
|
0
|
34 ngx_http_core_loc_conf_t *clcf;
|
|
35
|
50
|
36 rb = ngx_pcalloc(r->pool, sizeof(ngx_http_request_body_t));
|
|
37 if (rb == NULL) {
|
28
|
38 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
39 }
|
|
40
|
|
41 r->request_body = rb;
|
|
42
|
50
|
43 /* STUB */
|
|
44 if (r->file.fd != NGX_INVALID_FILE) {
|
|
45 if (ngx_close_file(r->file.fd) == NGX_FILE_ERROR) {
|
|
46 ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno,
|
|
47 ngx_close_file_n " \"%V\" failed", &r->file.name);
|
|
48 }
|
|
49 r->file.fd = NGX_INVALID_FILE;
|
|
50 }
|
|
51 /**/
|
|
52
|
28
|
53 if (r->headers_in.content_length_n <= 0) {
|
|
54 post_handler(r);
|
|
55 return NGX_OK;
|
|
56 }
|
|
57
|
|
58 rb->post_handler = post_handler;
|
|
59
|
|
60 /*
|
|
61 * set by ngx_pcalloc():
|
|
62 *
|
|
63 * rb->bufs = NULL;
|
|
64 * rb->buf = NULL;
|
|
65 * rb->rest = 0;
|
|
66 */
|
|
67
|
0
|
68 size = r->header_in->last - r->header_in->pos;
|
|
69
|
|
70 if (size) {
|
|
71
|
|
72 /* there is the pre-read part of the request body */
|
|
73
|
50
|
74 b = ngx_calloc_buf(r->pool);
|
|
75 if (b == NULL) {
|
28
|
76 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
77 }
|
0
|
78
|
|
79 b->temporary = 1;
|
|
80 b->start = b->pos = r->header_in->pos;
|
|
81 b->end = b->last = r->header_in->last;
|
|
82
|
50
|
83 rb->bufs = ngx_alloc_chain_link(r->pool);
|
|
84 if (rb->bufs == NULL) {
|
28
|
85 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
86 }
|
|
87
|
|
88 rb->bufs->buf = b;
|
|
89 rb->bufs->next = NULL;
|
0
|
90
|
|
91 if (size >= r->headers_in.content_length_n) {
|
|
92
|
|
93 /* the whole request body was pre-read */
|
|
94
|
|
95 r->header_in->pos += r->headers_in.content_length_n;
|
24
|
96 r->request_length += r->headers_in.content_length_n;
|
0
|
97
|
28
|
98 post_handler(r);
|
0
|
99
|
|
100 return NGX_OK;
|
|
101 }
|
|
102
|
|
103 r->header_in->pos = r->header_in->last;
|
24
|
104 r->request_length += size;
|
0
|
105 }
|
|
106
|
|
107
|
|
108 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
109
|
28
|
110 rb->rest = r->headers_in.content_length_n - size;
|
0
|
111
|
28
|
112 if (rb->rest < clcf->client_body_buffer_size
|
0
|
113 + (clcf->client_body_buffer_size >> 2))
|
|
114 {
|
28
|
115 size = rb->rest;
|
0
|
116
|
|
117 } else {
|
|
118 size = clcf->client_body_buffer_size;
|
|
119 }
|
|
120
|
50
|
121 rb->buf = ngx_create_temp_buf(r->pool, size);
|
|
122 if (rb->buf == NULL) {
|
28
|
123 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
124 }
|
0
|
125
|
50
|
126 cl = ngx_alloc_chain_link(r->pool);
|
|
127 if (cl == NULL) {
|
28
|
128 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
129 }
|
0
|
130
|
28
|
131 cl->buf = rb->buf;
|
|
132 cl->next = NULL;
|
|
133
|
|
134 if (rb->bufs) {
|
|
135 rb->bufs->next = cl;
|
0
|
136
|
|
137 } else {
|
28
|
138 rb->bufs = cl;
|
0
|
139 }
|
|
140
|
50
|
141 c = r->connection;
|
0
|
142
|
50
|
143 c->read->event_handler = ngx_http_read_client_request_body_handler;
|
|
144
|
|
145 return ngx_http_do_read_client_request_body(r, c);
|
0
|
146 }
|
|
147
|
|
148
|
50
|
149 static void
|
|
150 ngx_http_read_client_request_body_handler(ngx_event_t *rev)
|
0
|
151 {
|
|
152 ngx_int_t rc;
|
|
153 ngx_connection_t *c;
|
|
154 ngx_http_request_t *r;
|
|
155
|
|
156 c = rev->data;
|
|
157 r = c->data;
|
|
158
|
|
159 if (rev->timedout) {
|
|
160 ngx_http_finalize_request(r, NGX_HTTP_REQUEST_TIME_OUT);
|
|
161 return;
|
|
162 }
|
|
163
|
28
|
164 rc = ngx_http_do_read_client_request_body(r, c);
|
0
|
165
|
|
166 if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
|
|
167 ngx_http_finalize_request(r, rc);
|
|
168 }
|
|
169 }
|
|
170
|
|
171
|
50
|
172 static ngx_int_t
|
|
173 ngx_http_do_read_client_request_body(ngx_http_request_t *r,
|
|
174 ngx_connection_t *c)
|
0
|
175 {
|
|
176 size_t size;
|
|
177 ssize_t n;
|
|
178 ngx_buf_t *b;
|
28
|
179 ngx_temp_file_t *tf;
|
|
180 ngx_http_request_body_t *rb;
|
0
|
181 ngx_http_core_loc_conf_t *clcf;
|
|
182
|
28
|
183 rb = r->request_body;
|
0
|
184
|
|
185 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
|
186 "http read client request body");
|
|
187
|
|
188 for ( ;; ) {
|
28
|
189 if (rb->buf->last == rb->buf->end) {
|
|
190
|
|
191 if (rb->temp_file == NULL) {
|
50
|
192 tf = ngx_pcalloc(r->pool, sizeof(ngx_temp_file_t));
|
|
193 if (tf == NULL) {
|
28
|
194 return NGX_ERROR;
|
|
195 }
|
|
196
|
|
197 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
198
|
|
199 tf->file.fd = NGX_INVALID_FILE;
|
|
200 tf->file.log = r->connection->log;
|
|
201 tf->path = clcf->client_body_temp_path;
|
|
202 tf->pool = r->pool;
|
|
203 tf->warn = "a client request body is buffered "
|
|
204 "to a temporary file";
|
|
205
|
|
206 rb->temp_file = tf;
|
|
207
|
|
208 }
|
|
209
|
|
210 n = ngx_write_chain_to_temp_file(rb->temp_file,
|
|
211 rb->bufs->next ? rb->bufs->next:
|
|
212 rb->bufs);
|
0
|
213
|
|
214 /* TODO: n == 0 or not complete and level event */
|
|
215
|
|
216 if (n == NGX_ERROR) {
|
|
217 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
218 }
|
|
219
|
28
|
220 rb->temp_file->offset += n;
|
0
|
221
|
28
|
222 rb->buf->pos = rb->buf->start;
|
|
223 rb->buf->last = rb->buf->start;
|
0
|
224 }
|
|
225
|
28
|
226 size = rb->buf->end - rb->buf->last;
|
0
|
227
|
28
|
228 if (size > rb->rest) {
|
|
229 size = rb->rest;
|
0
|
230 }
|
|
231
|
28
|
232 n = c->recv(c, rb->buf->last, size);
|
0
|
233
|
|
234 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
10
|
235 "http client request body recv %z", n);
|
0
|
236
|
|
237 if (n == NGX_AGAIN) {
|
|
238 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
239 ngx_add_timer(c->read, clcf->client_body_timeout);
|
|
240
|
|
241 if (ngx_handle_read_event(c->read, 0) == NGX_ERROR) {
|
|
242 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
243 }
|
|
244
|
|
245 return NGX_AGAIN;
|
|
246 }
|
|
247
|
|
248 if (n == 0) {
|
|
249 ngx_log_error(NGX_LOG_INFO, c->log, 0,
|
|
250 "client closed prematurely connection");
|
|
251 }
|
|
252
|
|
253 if (n == 0 || n == NGX_ERROR) {
|
|
254 r->closed = 1;
|
|
255 return NGX_HTTP_BAD_REQUEST;
|
|
256 }
|
|
257
|
28
|
258 rb->buf->last += n;
|
|
259 rb->rest -= n;
|
24
|
260 r->request_length += n;
|
0
|
261
|
28
|
262 if (rb->rest == 0) {
|
0
|
263 break;
|
|
264 }
|
|
265
|
28
|
266 if (rb->buf->last < rb->buf->end) {
|
0
|
267 break;
|
|
268 }
|
|
269 }
|
|
270
|
|
271 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
28
|
272 "http client request body rest %uz", rb->rest);
|
0
|
273
|
28
|
274 if (rb->rest) {
|
0
|
275 return NGX_AGAIN;
|
|
276 }
|
|
277
|
28
|
278 if (rb->temp_file) {
|
0
|
279
|
|
280 /* save the last part */
|
28
|
281
|
|
282 n = ngx_write_chain_to_temp_file(rb->temp_file,
|
|
283 rb->bufs->next ? rb->bufs->next:
|
|
284 rb->bufs);
|
0
|
285
|
|
286 /* TODO: n == 0 or not complete and level event */
|
|
287
|
|
288 if (n == NGX_ERROR) {
|
|
289 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
290 }
|
|
291
|
50
|
292 b = ngx_calloc_buf(r->pool);
|
|
293 if (b == NULL) {
|
0
|
294 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
295 }
|
|
296
|
|
297 b->in_file = 1;
|
|
298 b->file_pos = 0;
|
28
|
299 b->file_last = rb->temp_file->file.offset;
|
|
300 b->file = &rb->temp_file->file;
|
0
|
301
|
28
|
302 if (rb->bufs->next) {
|
|
303 rb->bufs->next->buf = b;
|
0
|
304
|
|
305 } else {
|
28
|
306 rb->bufs->buf = b;
|
0
|
307 }
|
|
308 }
|
|
309
|
28
|
310 rb->post_handler(r);
|
0
|
311
|
|
312 return NGX_OK;
|
|
313 }
|