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