comparison src/http/ngx_http_request_body.c @ 297:ee394e997c77

nginx-0.0.3-2004-03-29-21:43:58 import
author Igor Sysoev <igor@sysoev.ru>
date Mon, 29 Mar 2004 17:43:58 +0000
parents bfe099e3f5b4
children 46b7eeb8a116
comparison
equal deleted inserted replaced
296:bfe099e3f5b4 297:ee394e997c77
7 7
8 static void ngx_http_read_client_request_body_handler(ngx_event_t *rev); 8 static void ngx_http_read_client_request_body_handler(ngx_event_t *rev);
9 static ngx_int_t ngx_http_do_read_client_request_body(ngx_http_request_t *r); 9 static ngx_int_t ngx_http_do_read_client_request_body(ngx_http_request_t *r);
10 10
11 11
12 ngx_int_t ngx_http_read_client_request_body(ngx_http_request_t *r, 12 ngx_int_t ngx_http_read_client_request_body(ngx_http_request_t *r)
13 size_t request_buffer_size)
14 { 13 {
15 ssize_t size; 14 ssize_t size;
16 ngx_hunk_t *h; 15 ngx_hunk_t *h;
17 ngx_chain_t *cl; 16 ngx_chain_t *cl;
18 17
18
19 size = r->header_in->last - r->header_in->pos; 19 size = r->header_in->last - r->header_in->pos;
20 20
21 if (size) { 21 if (size) {
22 22
23 /* there is the pre-read part of the request body */ 23 /* there is the pre-read part of the request body */
24 24
25 ngx_test_null(h, ngx_calloc_hunk(r->pool), NGX_ERROR); 25 ngx_test_null(h, ngx_calloc_hunk(r->pool),
26 NGX_HTTP_INTERNAL_SERVER_ERROR);
26 27
27 h->type = NGX_HUNK_IN_MEMORY|NGX_HUNK_TEMP; 28 h->type = NGX_HUNK_IN_MEMORY|NGX_HUNK_TEMP;
28 h->start = h->pos = r->header_in->pos; 29 h->start = h->pos = r->header_in->pos;
29 h->end = h->last = r->header_in->last; 30 h->end = h->last = r->header_in->last;
30 31
31 ngx_alloc_link_and_set_hunk(r->request_hunks, h, r->pool, NGX_ERROR); 32 ngx_alloc_link_and_set_hunk(r->request_body->bufs, h, r->pool,
33 NGX_HTTP_INTERNAL_SERVER_ERROR);
32 34
33 if (size >= r->headers_in.content_length_n) { 35 if (size >= r->headers_in.content_length_n) {
34 36
35 /* the whole request body was pre-read */ 37 /* the whole request body was pre-read */
36 38
40 42
41 r->header_in->pos = r->header_in->last; 43 r->header_in->pos = r->header_in->last;
42 } 44 }
43 45
44 46
45 r->remaining_body_len = r->headers_in.content_length_n - size; 47 r->request_body->rest = r->headers_in.content_length_n - size;
46 48
47 if (r->remaining_body_len 49 if (r->request_body->rest
48 < request_buffer_size + (request_buffer_size >> 2)) 50 < r->request_body->buf_size + (r->request_body->buf_size >> 2))
49 { 51 {
50 size = r->remaining_body_len; 52 size = r->request_body->rest;
51 53
52 } else { 54 } else {
53 size = request_buffer_size; 55 size = r->request_body->buf_size;
54 } 56 }
55 57
56 ngx_test_null(r->request_body_hunk, ngx_create_temp_hunk(r->pool, size), 58 ngx_test_null(r->request_body->buf, ngx_create_temp_hunk(r->pool, size),
57 NGX_ERROR); 59 NGX_HTTP_INTERNAL_SERVER_ERROR);
58 60
59 ngx_alloc_link_and_set_hunk(cl, r->request_body_hunk, r->pool, 61 ngx_alloc_link_and_set_hunk(cl, r->request_body->buf, r->pool,
60 NGX_ERROR); 62 NGX_HTTP_INTERNAL_SERVER_ERROR);
61 63
62 if (r->request_hunks) { 64 if (r->request_body->bufs) {
63 r->request_hunks->next = cl; 65 r->request_body->bufs->next = cl;
64 66
65 } else { 67 } else {
66 r->request_hunks = cl; 68 r->request_body->bufs = cl;
67 } 69 }
68 70
69 r->connection->read->event_handler = 71 r->connection->read->event_handler =
70 ngx_http_read_client_request_body_handler; 72 ngx_http_read_client_request_body_handler;
71 73
102 104
103 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, 105 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
104 "http read client request body"); 106 "http read client request body");
105 107
106 for ( ;; ) { 108 for ( ;; ) {
107 if (r->request_body_hunk->last == r->request_body_hunk->end) { 109 if (r->request_body->buf->last == r->request_body->buf->end) {
108 n = ngx_write_chain_to_temp_file(r->temp_file, 110 n = ngx_write_chain_to_temp_file(r->request_body->temp_file,
109 r->request_hunks->next ? r->request_hunks->next: 111 r->request_body->bufs->next ? r->request_body->bufs->next:
110 r->request_hunks); 112 r->request_body->bufs);
111 113
112 /* TODO: n == 0 or not complete and level event */ 114 /* TODO: n == 0 or not complete and level event */
113 115
114 if (n == NGX_ERROR) { 116 if (n == NGX_ERROR) {
115 return NGX_HTTP_INTERNAL_SERVER_ERROR; 117 return NGX_HTTP_INTERNAL_SERVER_ERROR;
116 } 118 }
117 119
118 r->temp_file->offset += n; 120 r->request_body->temp_file->offset += n;
119 121
120 r->request_body_hunk->pos = r->request_body_hunk->start; 122 r->request_body->buf->pos = r->request_body->buf->start;
121 r->request_body_hunk->last = r->request_body_hunk->start; 123 r->request_body->buf->last = r->request_body->buf->start;
122 } 124 }
123 125
124 size = r->request_body_hunk->end - r->request_body_hunk->last; 126 size = r->request_body->buf->end - r->request_body->buf->last;
125 127
126 if (size > r->remaining_body_len) { 128 if (size > r->request_body->rest) {
127 size = r->remaining_body_len; 129 size = r->request_body->rest;
128 } 130 }
129 131
130 n = ngx_recv(c, r->request_body_hunk->last, size); 132 n = ngx_recv(c, r->request_body->buf->last, size);
131 133
132 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, 134 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
133 "http client request body recv " SIZE_T_FMT, n); 135 "http client request body recv " SIZE_T_FMT, n);
134 136
135 if (n == NGX_AGAIN) { 137 if (n == NGX_AGAIN) {
151 if (n == 0 || n == NGX_ERROR) { 153 if (n == 0 || n == NGX_ERROR) {
152 r->closed = 1; 154 r->closed = 1;
153 return NGX_HTTP_BAD_REQUEST; 155 return NGX_HTTP_BAD_REQUEST;
154 } 156 }
155 157
156 r->request_body_hunk->last += n; 158 r->request_body->buf->last += n;
157 r->remaining_body_len -= n; 159 r->request_body->rest -= n;
158 160
159 if (r->remaining_body_len == 0) { 161 if (r->request_body->rest == 0) {
160 break; 162 break;
161 } 163 }
162 164
163 if (r->request_body_hunk->last < r->request_body_hunk->end) { 165 if (r->request_body->buf->last < r->request_body->buf->end) {
164 break; 166 break;
165 } 167 }
166 } 168 }
167 169
168 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, 170 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
169 "http client request body left " SIZE_T_FMT, 171 "http client request body rest " SIZE_T_FMT,
170 r->remaining_body_len); 172 r->request_body->rest);
171 173
172 if (r->remaining_body_len) { 174 if (r->request_body->rest) {
173 return NGX_AGAIN; 175 return NGX_AGAIN;
174 } 176 }
175 177
176 if (r->temp_file->file.fd != NGX_INVALID_FILE) { 178 if (r->request_body->temp_file->file.fd != NGX_INVALID_FILE) {
177 179
178 /* save the last part */ 180 /* save the last part */
179 n = ngx_write_chain_to_temp_file(r->temp_file, 181 n = ngx_write_chain_to_temp_file(r->request_body->temp_file,
180 r->request_hunks->next ? r->request_hunks->next: 182 r->request_body->bufs->next ? r->request_body->bufs->next:
181 r->request_hunks); 183 r->request_body->bufs);
182 184
183 /* TODO: n == 0 or not complete and level event */ 185 /* TODO: n == 0 or not complete and level event */
184 186
185 if (n == NGX_ERROR) { 187 if (n == NGX_ERROR) {
186 return NGX_HTTP_INTERNAL_SERVER_ERROR; 188 return NGX_HTTP_INTERNAL_SERVER_ERROR;
191 return NGX_HTTP_INTERNAL_SERVER_ERROR; 193 return NGX_HTTP_INTERNAL_SERVER_ERROR;
192 } 194 }
193 195
194 h->type = NGX_HUNK_FILE; 196 h->type = NGX_HUNK_FILE;
195 h->file_pos = 0; 197 h->file_pos = 0;
196 h->file_last = r->temp_file->file.offset; 198 h->file_last = r->request_body->temp_file->file.offset;
197 h->file = &r->temp_file->file; 199 h->file = &r->request_body->temp_file->file;
198 200
199 if (r->request_hunks->next) { 201 if (r->request_body->bufs->next) {
200 r->request_hunks->next->hunk = h; 202 r->request_body->bufs->next->hunk = h;
201 203
202 } else { 204 } else {
203 r->request_hunks->hunk = h; 205 r->request_body->bufs->hunk = h;
204 } 206 }
205 } 207 }
206 208
207 r->request_body_handler(r->data); 209 r->request_body->handler(r->request_body->data);
208 210
209 return NGX_OK; 211 return NGX_OK;
210 } 212 }