Igor Sysoev | a09f08d | 2003-04-25 14:43:13 +0000 | [diff] [blame] | 1 | |
Igor Sysoev | d90282d | 2004-09-28 08:34:51 +0000 | [diff] [blame] | 2 | /* |
Igor Sysoev | ff8da91 | 2004-09-29 16:00:49 +0000 | [diff] [blame] | 3 | * Copyright (C) Igor Sysoev |
Igor Sysoev | d90282d | 2004-09-28 08:34:51 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | |
Igor Sysoev | 1d8d9ee | 2003-04-28 15:06:39 +0000 | [diff] [blame] | 7 | #include <ngx_config.h> |
| 8 | #include <ngx_core.h> |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 9 | #include <ngx_event.h> |
Igor Sysoev | 1d8d9ee | 2003-04-28 15:06:39 +0000 | [diff] [blame] | 10 | #include <ngx_http.h> |
Igor Sysoev | a09f08d | 2003-04-25 14:43:13 +0000 | [diff] [blame] | 11 | |
| 12 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 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); |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 15 | static ngx_int_t ngx_http_write_request_body(ngx_http_request_t *r, |
| 16 | ngx_chain_t *body); |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 17 | static void ngx_http_read_discarded_body_handler(ngx_http_request_t *r); |
| 18 | static ngx_int_t ngx_http_read_discarded_body(ngx_http_request_t *r); |
| 19 | |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 20 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 21 | /* |
| 22 | * on completion ngx_http_read_client_request_body() adds to |
| 23 | * r->request_body->bufs one or two bufs: |
| 24 | * *) one memory buf that was preread in r->header_in; |
| 25 | * *) one memory or file buf that contains the rest of the body |
| 26 | */ |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 27 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 28 | ngx_int_t |
| 29 | ngx_http_read_client_request_body(ngx_http_request_t *r, |
| 30 | ngx_http_client_body_handler_pt post_handler) |
Igor Sysoev | a09f08d | 2003-04-25 14:43:13 +0000 | [diff] [blame] | 31 | { |
Igor Sysoev | 1765f47 | 2006-07-07 16:33:19 +0000 | [diff] [blame] | 32 | size_t preread; |
| 33 | ssize_t size; |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 34 | ngx_buf_t *b; |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 35 | ngx_chain_t *cl, **next; |
Igor Sysoev | 4c7f511 | 2006-10-01 07:17:01 +0000 | [diff] [blame] | 36 | ngx_temp_file_t *tf; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 37 | ngx_http_request_body_t *rb; |
Igor Sysoev | dbb2776 | 2004-04-01 16:20:53 +0000 | [diff] [blame] | 38 | ngx_http_core_loc_conf_t *clcf; |
Igor Sysoev | 11dbe97 | 2004-03-29 17:43:58 +0000 | [diff] [blame] | 39 | |
Igor Sysoev | e503539 | 2005-08-30 10:55:07 +0000 | [diff] [blame] | 40 | if (r->request_body || r->discard_body) { |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 41 | post_handler(r); |
| 42 | return NGX_OK; |
| 43 | } |
| 44 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 45 | rb = ngx_pcalloc(r->pool, sizeof(ngx_http_request_body_t)); |
| 46 | if (rb == NULL) { |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 47 | return NGX_HTTP_INTERNAL_SERVER_ERROR; |
| 48 | } |
| 49 | |
| 50 | r->request_body = rb; |
| 51 | |
Igor Sysoev | 4c7f511 | 2006-10-01 07:17:01 +0000 | [diff] [blame] | 52 | if (r->headers_in.content_length_n < 0) { |
| 53 | post_handler(r); |
| 54 | return NGX_OK; |
| 55 | } |
| 56 | |
| 57 | clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); |
| 58 | |
| 59 | if (r->headers_in.content_length_n == 0) { |
| 60 | |
| 61 | if (r->request_body_in_file_only) { |
| 62 | tf = ngx_pcalloc(r->pool, sizeof(ngx_temp_file_t)); |
| 63 | if (tf == NULL) { |
| 64 | return NGX_HTTP_INTERNAL_SERVER_ERROR; |
| 65 | } |
| 66 | |
| 67 | tf->file.fd = NGX_INVALID_FILE; |
| 68 | tf->file.log = r->connection->log; |
| 69 | tf->path = clcf->client_body_temp_path; |
| 70 | tf->pool = r->pool; |
| 71 | tf->warn = "a client request body is buffered to a temporary file"; |
| 72 | tf->log_level = r->request_body_file_log_level; |
| 73 | tf->persistent = r->request_body_in_persistent_file; |
Igor Sysoev | cd5b99a | 2007-01-25 08:45:04 +0000 | [diff] [blame] | 74 | tf->clean = r->request_body_in_clean_file; |
Igor Sysoev | 4c7f511 | 2006-10-01 07:17:01 +0000 | [diff] [blame] | 75 | |
| 76 | if (r->request_body_file_group_access) { |
Igor Sysoev | fe1cb8c | 2007-01-18 19:52:18 +0000 | [diff] [blame] | 77 | tf->access = 0660; |
Igor Sysoev | 4c7f511 | 2006-10-01 07:17:01 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | rb->temp_file = tf; |
| 81 | |
| 82 | if (ngx_create_temp_file(&tf->file, tf->path, tf->pool, |
Igor Sysoev | cd5b99a | 2007-01-25 08:45:04 +0000 | [diff] [blame] | 83 | tf->persistent, tf->clean, tf->access) |
Igor Sysoev | 4c7f511 | 2006-10-01 07:17:01 +0000 | [diff] [blame] | 84 | != NGX_OK) |
| 85 | { |
| 86 | return NGX_HTTP_INTERNAL_SERVER_ERROR; |
| 87 | } |
| 88 | } |
| 89 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 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 | |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 104 | preread = r->header_in->last - r->header_in->pos; |
Igor Sysoev | a09f08d | 2003-04-25 14:43:13 +0000 | [diff] [blame] | 105 | |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 106 | if (preread) { |
Igor Sysoev | 14f02ed | 2004-03-26 16:13:01 +0000 | [diff] [blame] | 107 | |
| 108 | /* there is the pre-read part of the request body */ |
| 109 | |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 110 | ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
| 111 | "http client request body preread %uz", preread); |
| 112 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 113 | b = ngx_calloc_buf(r->pool); |
| 114 | if (b == NULL) { |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 115 | return NGX_HTTP_INTERNAL_SERVER_ERROR; |
| 116 | } |
Igor Sysoev | a09f08d | 2003-04-25 14:43:13 +0000 | [diff] [blame] | 117 | |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 118 | b->temporary = 1; |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 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; |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 123 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 124 | rb->bufs = ngx_alloc_chain_link(r->pool); |
| 125 | if (rb->bufs == NULL) { |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 126 | return NGX_HTTP_INTERNAL_SERVER_ERROR; |
| 127 | } |
| 128 | |
| 129 | rb->bufs->buf = b; |
| 130 | rb->bufs->next = NULL; |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 131 | |
Igor Sysoev | 8f12558 | 2006-07-28 15:16:17 +0000 | [diff] [blame] | 132 | rb->buf = b; |
| 133 | |
Igor Sysoev | bb28b6d | 2006-07-11 13:20:19 +0000 | [diff] [blame] | 134 | if ((off_t) preread >= r->headers_in.content_length_n) { |
Igor Sysoev | 2b0c76c | 2003-10-27 21:01:00 +0000 | [diff] [blame] | 135 | |
Igor Sysoev | 14f02ed | 2004-03-26 16:13:01 +0000 | [diff] [blame] | 136 | /* the whole request body was pre-read */ |
| 137 | |
Igor Sysoev | 1765f47 | 2006-07-07 16:33:19 +0000 | [diff] [blame] | 138 | r->header_in->pos += (size_t) r->headers_in.content_length_n; |
Igor Sysoev | 6a12fc9 | 2004-12-06 14:45:08 +0000 | [diff] [blame] | 139 | r->request_length += r->headers_in.content_length_n; |
Igor Sysoev | 6707ba9 | 2004-03-30 15:59:50 +0000 | [diff] [blame] | 140 | |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 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 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 147 | post_handler(r); |
Igor Sysoev | 6707ba9 | 2004-03-30 15:59:50 +0000 | [diff] [blame] | 148 | |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 149 | return NGX_OK; |
| 150 | } |
Igor Sysoev | 2b0c76c | 2003-10-27 21:01:00 +0000 | [diff] [blame] | 151 | |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 152 | /* |
| 153 | * to not consider the body as pipelined request in |
| 154 | * ngx_http_set_keepalive() |
| 155 | */ |
Igor Sysoev | 2b0c76c | 2003-10-27 21:01:00 +0000 | [diff] [blame] | 156 | r->header_in->pos = r->header_in->last; |
Igor Sysoev | 1d8d9ee | 2003-04-28 15:06:39 +0000 | [diff] [blame] | 157 | |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 158 | r->request_length += preread; |
| 159 | |
| 160 | rb->rest = r->headers_in.content_length_n - preread; |
| 161 | |
Igor Sysoev | bb28b6d | 2006-07-11 13:20:19 +0000 | [diff] [blame] | 162 | if (rb->rest <= (off_t) (b->end - b->last)) { |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 163 | |
| 164 | /* the whole request body may be placed in r->header_in */ |
| 165 | |
Igor Sysoev | 3658896 | 2007-01-19 16:13:15 +0000 | [diff] [blame] | 166 | rb->to_write = rb->bufs; |
| 167 | |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 168 | r->read_event_handler = ngx_http_read_client_request_body_handler; |
| 169 | |
Igor Sysoev | cd5b99a | 2007-01-25 08:45:04 +0000 | [diff] [blame] | 170 | return ngx_http_do_read_client_request_body(r); |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 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; |
| 179 | } |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 180 | |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 181 | size = clcf->client_body_buffer_size; |
| 182 | size += size >> 2; |
Igor Sysoev | 14f02ed | 2004-03-26 16:13:01 +0000 | [diff] [blame] | 183 | |
Igor Sysoev | 1765f47 | 2006-07-07 16:33:19 +0000 | [diff] [blame] | 184 | if (rb->rest < size) { |
| 185 | size = (ssize_t) rb->rest; |
Igor Sysoev | a09f08d | 2003-04-25 14:43:13 +0000 | [diff] [blame] | 186 | |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 187 | if (r->request_body_in_single_buf) { |
| 188 | size += preread; |
| 189 | } |
| 190 | |
Igor Sysoev | a09f08d | 2003-04-25 14:43:13 +0000 | [diff] [blame] | 191 | } else { |
Igor Sysoev | dbb2776 | 2004-04-01 16:20:53 +0000 | [diff] [blame] | 192 | size = clcf->client_body_buffer_size; |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 193 | |
| 194 | /* disable copying buffer for r->request_body_in_single_buf */ |
| 195 | b = NULL; |
Igor Sysoev | a09f08d | 2003-04-25 14:43:13 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 198 | rb->buf = ngx_create_temp_buf(r->pool, size); |
| 199 | if (rb->buf == NULL) { |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 200 | return NGX_HTTP_INTERNAL_SERVER_ERROR; |
| 201 | } |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 202 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 203 | cl = ngx_alloc_chain_link(r->pool); |
| 204 | if (cl == NULL) { |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 205 | return NGX_HTTP_INTERNAL_SERVER_ERROR; |
| 206 | } |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 207 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 208 | cl->buf = rb->buf; |
| 209 | cl->next = NULL; |
| 210 | |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 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; |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 223 | |
| 224 | } else { |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 225 | rb->to_write = rb->bufs->next ? rb->bufs->next : rb->bufs; |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 228 | r->read_event_handler = ngx_http_read_client_request_body_handler; |
Igor Sysoev | a09f08d | 2003-04-25 14:43:13 +0000 | [diff] [blame] | 229 | |
Igor Sysoev | cd5b99a | 2007-01-25 08:45:04 +0000 | [diff] [blame] | 230 | return ngx_http_do_read_client_request_body(r); |
Igor Sysoev | a09f08d | 2003-04-25 14:43:13 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 234 | static void |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 235 | ngx_http_read_client_request_body_handler(ngx_http_request_t *r) |
Igor Sysoev | a09f08d | 2003-04-25 14:43:13 +0000 | [diff] [blame] | 236 | { |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 237 | ngx_int_t rc; |
Igor Sysoev | ae02c19 | 2004-03-19 05:25:53 +0000 | [diff] [blame] | 238 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 239 | if (r->connection->read->timedout) { |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 240 | r->connection->timedout = 1; |
Igor Sysoev | cd5b99a | 2007-01-25 08:45:04 +0000 | [diff] [blame] | 241 | ngx_http_finalize_request(r, NGX_HTTP_REQUEST_TIME_OUT); |
Igor Sysoev | 6707ba9 | 2004-03-30 15:59:50 +0000 | [diff] [blame] | 242 | return; |
| 243 | } |
| 244 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 245 | rc = ngx_http_do_read_client_request_body(r); |
Igor Sysoev | ae02c19 | 2004-03-19 05:25:53 +0000 | [diff] [blame] | 246 | |
| 247 | if (rc >= NGX_HTTP_SPECIAL_RESPONSE) { |
Igor Sysoev | cd5b99a | 2007-01-25 08:45:04 +0000 | [diff] [blame] | 248 | ngx_http_finalize_request(r, rc); |
Igor Sysoev | ae02c19 | 2004-03-19 05:25:53 +0000 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | |
| 252 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 253 | static ngx_int_t |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 254 | ngx_http_do_read_client_request_body(ngx_http_request_t *r) |
Igor Sysoev | ae02c19 | 2004-03-19 05:25:53 +0000 | [diff] [blame] | 255 | { |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 256 | size_t size; |
| 257 | ssize_t n; |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 258 | ngx_buf_t *b; |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 259 | ngx_connection_t *c; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 260 | ngx_http_request_body_t *rb; |
Igor Sysoev | 2b0c76c | 2003-10-27 21:01:00 +0000 | [diff] [blame] | 261 | ngx_http_core_loc_conf_t *clcf; |
Igor Sysoev | 1d8d9ee | 2003-04-28 15:06:39 +0000 | [diff] [blame] | 262 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 263 | c = r->connection; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 264 | rb = r->request_body; |
Igor Sysoev | a09f08d | 2003-04-25 14:43:13 +0000 | [diff] [blame] | 265 | |
Igor Sysoev | da85f7f | 2004-03-16 21:26:01 +0000 | [diff] [blame] | 266 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, |
| 267 | "http read client request body"); |
| 268 | |
| 269 | for ( ;; ) { |
Igor Sysoev | 12c94ae | 2006-11-13 20:40:17 +0000 | [diff] [blame] | 270 | for ( ;; ) { |
| 271 | if (rb->buf->last == rb->buf->end) { |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 272 | |
Igor Sysoev | 12c94ae | 2006-11-13 20:40:17 +0000 | [diff] [blame] | 273 | if (ngx_http_write_request_body(r, rb->to_write) != NGX_OK) { |
| 274 | return NGX_HTTP_INTERNAL_SERVER_ERROR; |
| 275 | } |
| 276 | |
| 277 | rb->to_write = rb->bufs->next ? rb->bufs->next : rb->bufs; |
| 278 | rb->buf->last = rb->buf->start; |
Igor Sysoev | da85f7f | 2004-03-16 21:26:01 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Igor Sysoev | 12c94ae | 2006-11-13 20:40:17 +0000 | [diff] [blame] | 281 | size = rb->buf->end - rb->buf->last; |
| 282 | |
| 283 | if ((off_t) size > rb->rest) { |
| 284 | size = (size_t) rb->rest; |
| 285 | } |
| 286 | |
| 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); |
| 291 | |
| 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 | } |
| 300 | |
| 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 | } |
Igor Sysoev | da85f7f | 2004-03-16 21:26:01 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Igor Sysoev | ae02c19 | 2004-03-19 05:25:53 +0000 | [diff] [blame] | 319 | ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, |
Igor Sysoev | cf81502 | 2007-02-15 14:13:24 +0000 | [diff] [blame] | 320 | "http client request body rest %O", rb->rest); |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 321 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 322 | if (rb->rest == 0) { |
Igor Sysoev | 89690bf | 2004-03-23 06:01:52 +0000 | [diff] [blame] | 323 | break; |
| 324 | } |
| 325 | |
Igor Sysoev | 12c94ae | 2006-11-13 20:40:17 +0000 | [diff] [blame] | 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); |
| 329 | |
| 330 | if (ngx_handle_read_event(c->read, 0) == NGX_ERROR) { |
| 331 | return NGX_HTTP_INTERNAL_SERVER_ERROR; |
| 332 | } |
| 333 | |
| 334 | return NGX_AGAIN; |
Igor Sysoev | da85f7f | 2004-03-16 21:26:01 +0000 | [diff] [blame] | 335 | } |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 338 | if (c->read->timer_set) { |
| 339 | ngx_del_timer(c->read); |
| 340 | } |
| 341 | |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 342 | if (rb->temp_file || r->request_body_in_file_only) { |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 343 | |
| 344 | /* save the last part */ |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 345 | |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 346 | if (ngx_http_write_request_body(r, rb->to_write) != NGX_OK) { |
Igor Sysoev | ae02c19 | 2004-03-19 05:25:53 +0000 | [diff] [blame] | 347 | return NGX_HTTP_INTERNAL_SERVER_ERROR; |
Igor Sysoev | 2b0c76c | 2003-10-27 21:01:00 +0000 | [diff] [blame] | 348 | } |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 349 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 350 | b = ngx_calloc_buf(r->pool); |
| 351 | if (b == NULL) { |
Igor Sysoev | ae02c19 | 2004-03-19 05:25:53 +0000 | [diff] [blame] | 352 | return NGX_HTTP_INTERNAL_SERVER_ERROR; |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 355 | b->in_file = 1; |
| 356 | b->file_pos = 0; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 357 | b->file_last = rb->temp_file->file.offset; |
| 358 | b->file = &rb->temp_file->file; |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 359 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 360 | if (rb->bufs->next) { |
| 361 | rb->bufs->next->buf = b; |
Igor Sysoev | a09f08d | 2003-04-25 14:43:13 +0000 | [diff] [blame] | 362 | |
| 363 | } else { |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 364 | rb->bufs->buf = b; |
Igor Sysoev | a09f08d | 2003-04-25 14:43:13 +0000 | [diff] [blame] | 365 | } |
Igor Sysoev | a09f08d | 2003-04-25 14:43:13 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 368 | if (r->request_body_in_file_only && rb->bufs->next) { |
| 369 | rb->bufs = rb->bufs->next; |
| 370 | } |
| 371 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 372 | rb->post_handler(r); |
Igor Sysoev | ae02c19 | 2004-03-19 05:25:53 +0000 | [diff] [blame] | 373 | |
| 374 | return NGX_OK; |
Igor Sysoev | a09f08d | 2003-04-25 14:43:13 +0000 | [diff] [blame] | 375 | } |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 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"; |
Igor Sysoev | 7bdb720 | 2006-04-19 15:30:56 +0000 | [diff] [blame] | 401 | tf->log_level = r->request_body_file_log_level; |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 402 | tf->persistent = r->request_body_in_persistent_file; |
Igor Sysoev | cd5b99a | 2007-01-25 08:45:04 +0000 | [diff] [blame] | 403 | tf->clean = r->request_body_in_clean_file; |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 404 | |
| 405 | if (r->request_body_file_group_access) { |
Igor Sysoev | fe1cb8c | 2007-01-18 19:52:18 +0000 | [diff] [blame] | 406 | tf->access = 0660; |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | rb->temp_file = tf; |
| 410 | } |
| 411 | |
| 412 | n = ngx_write_chain_to_temp_file(rb->temp_file, body); |
| 413 | |
| 414 | /* TODO: n == 0 or not complete and level event */ |
| 415 | |
| 416 | if (n == NGX_ERROR) { |
| 417 | return NGX_ERROR; |
| 418 | } |
| 419 | |
| 420 | rb->temp_file->offset += n; |
| 421 | |
| 422 | return NGX_OK; |
| 423 | } |
| 424 | |
| 425 | |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 426 | ngx_int_t |
| 427 | ngx_http_discard_body(ngx_http_request_t *r) |
| 428 | { |
| 429 | ssize_t size; |
| 430 | ngx_event_t *rev; |
| 431 | |
Igor Sysoev | 08e63d4 | 2006-08-14 15:09:38 +0000 | [diff] [blame] | 432 | if (r != r->main || r->discard_body) { |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 433 | return NGX_OK; |
| 434 | } |
| 435 | |
| 436 | rev = r->connection->read; |
| 437 | |
| 438 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0, "http set discard body"); |
| 439 | |
| 440 | if (rev->timer_set) { |
| 441 | ngx_del_timer(rev); |
| 442 | } |
| 443 | |
Igor Sysoev | da173ab | 2006-08-30 10:39:17 +0000 | [diff] [blame] | 444 | r->discard_body = 1; |
| 445 | |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 446 | if (r->headers_in.content_length_n <= 0) { |
| 447 | return NGX_OK; |
| 448 | } |
| 449 | |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 450 | size = r->header_in->last - r->header_in->pos; |
| 451 | |
| 452 | if (size) { |
| 453 | if (r->headers_in.content_length_n > size) { |
| 454 | r->headers_in.content_length_n -= size; |
| 455 | |
| 456 | } else { |
Igor Sysoev | 1765f47 | 2006-07-07 16:33:19 +0000 | [diff] [blame] | 457 | r->header_in->pos += (size_t) r->headers_in.content_length_n; |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 458 | r->headers_in.content_length_n = 0; |
| 459 | return NGX_OK; |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | r->read_event_handler = ngx_http_read_discarded_body_handler; |
| 464 | |
| 465 | if (ngx_handle_read_event(rev, 0) == NGX_ERROR) { |
| 466 | return NGX_HTTP_INTERNAL_SERVER_ERROR; |
| 467 | } |
| 468 | |
| 469 | return ngx_http_read_discarded_body(r); |
| 470 | } |
| 471 | |
| 472 | |
| 473 | static void |
| 474 | ngx_http_read_discarded_body_handler(ngx_http_request_t *r) |
| 475 | { |
| 476 | ngx_int_t rc; |
| 477 | |
| 478 | rc = ngx_http_read_discarded_body(r); |
| 479 | |
| 480 | if (rc == NGX_AGAIN) { |
| 481 | if (ngx_handle_read_event(r->connection->read, 0) == NGX_ERROR) { |
| 482 | ngx_http_finalize_request(r, rc); |
| 483 | return; |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | if (rc != NGX_OK) { |
| 488 | ngx_http_finalize_request(r, rc); |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | |
| 493 | static ngx_int_t |
| 494 | ngx_http_read_discarded_body(ngx_http_request_t *r) |
| 495 | { |
Igor Sysoev | 1765f47 | 2006-07-07 16:33:19 +0000 | [diff] [blame] | 496 | size_t size; |
| 497 | ssize_t n; |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 498 | u_char buffer[NGX_HTTP_DISCARD_BUFFER_SIZE]; |
| 499 | |
| 500 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
| 501 | "http read discarded body"); |
| 502 | |
| 503 | if (r->headers_in.content_length_n == 0) { |
| 504 | return NGX_OK; |
| 505 | } |
| 506 | |
Igor Sysoev | 1765f47 | 2006-07-07 16:33:19 +0000 | [diff] [blame] | 507 | size = (r->headers_in.content_length_n > NGX_HTTP_DISCARD_BUFFER_SIZE) ? |
| 508 | NGX_HTTP_DISCARD_BUFFER_SIZE: |
| 509 | (size_t) r->headers_in.content_length_n; |
Igor Sysoev | 8a2b2fb | 2006-04-14 09:53:38 +0000 | [diff] [blame] | 510 | |
| 511 | n = r->connection->recv(r->connection, buffer, size); |
| 512 | |
| 513 | if (n == NGX_ERROR) { |
| 514 | |
| 515 | r->connection->error = 1; |
| 516 | |
| 517 | /* |
| 518 | * if a client request body is discarded then we already set |
| 519 | * some HTTP response code for client and we can ignore the error |
| 520 | */ |
| 521 | |
| 522 | return NGX_OK; |
| 523 | } |
| 524 | |
| 525 | if (n == NGX_AGAIN) { |
| 526 | return NGX_AGAIN; |
| 527 | } |
| 528 | |
| 529 | r->headers_in.content_length_n -= n; |
| 530 | |
| 531 | return NGX_OK; |
| 532 | } |