comparison src/http/ngx_http_request_body.c @ 83:a7e45c45a95c

nginx-0.0.1-2003-04-28-19:06:39 import
author Igor Sysoev <igor@sysoev.ru>
date Mon, 28 Apr 2003 15:06:39 +0000
parents fccdb921e8b8
children e7e094d34162
comparison
equal deleted inserted replaced
82:fccdb921e8b8 83:a7e45c45a95c
1
2 #include <ngx_config.h>
3 #include <ngx_core.h>
4 #include <ngx_http.h>
1 5
2 6
7 int ngx_http_init_client_request_body(ngx_http_request_t *r, int size)
8 {
9 int header_in_part, len;
10 ngx_hunk_t *h;
11 ngx_http_request_body_t *rb;
3 12
13 ngx_test_null(rb, ngx_pcalloc(r->pool, sizeof(ngx_http_request_body_t)),
14 NGX_HTTP_INTERNAL_SERVER_ERROR);
4 15
5 int ngx_http_start_read_client_body(ngx_http_proxy_ctx_t *p) 16 header_in_part = r->header_in->end - r->header_in->pos;
6 {
7 int first_part, size;
8 ngx_hunk_t *h;
9 ngx_http_request_t *r;
10 17
11 r = p->request; 18 if (header_in_part) {
19 rb->header_in_pos = r->header_in->pos;
20 }
12 21
13 first_part = r->header_in->last - r->header_in->pos; 22 if (header_in_part > r->headers_in.content_length_n) {
14 23 header_in_part = r->headers_in.content_length_n;
15 if (first_part > r->headers_in.content_length_n) {
16 first_part = r->headers_in.content_length_n;
17 size = 0;
18 24
19 } else { 25 } else {
20 size = r->headers_in.content_length_n - first_part; 26 len = r->headers_in.content_length_n - header_in_part;
21 if (size > p->lcf->client_request_buffer_size) { 27 if (len > size) {
22 size = p->lcf->client_request_buffer_size; 28 len = size;
23 29
24 } else if (size > NGX_PAGE_SIZE) { 30 } else if (len > NGX_PAGE_SIZE) {
25 size = ((size + NGX_PAGE_SIZE) / NGX_PAGE_SIZE) * NGX_PAGE_SIZE; 31 len = ((len + NGX_PAGE_SIZE - 1) / NGX_PAGE_SIZE) * NGX_PAGE_SIZE;
26 } 32 }
27 33
28 if (size) { 34 if (len) {
29 ngx_test_null(p->client_request_hunk, ngx_palloc(r->pool, size), 35 ngx_test_null(rb->hunk, ngx_create_temp_hunk(r->pool, len, 0, 0),
30 NGX_ERROR); 36 NGX_HTTP_INTERNAL_SERVER_ERROR);
31 } 37 }
32 } 38 }
33 39
34 if (first_part) { 40 r->request_body = rb;
35 ngx_test_null(h, ngx_alloc_hunk(r->pool), NGX_ERROR);
36
37 h->type = NGX_HUNK_IN_MEMORY|NGX_HUNK_TEMP;
38 h->pos = h->start = h->pre_start = r->header_in->pos;
39 h->last = h->end = h->post_end = r->header_in->pos + first_part;
40 h->file_pos = h->file_last = 0;
41 h->file = NULL;
42 h->shadow = NULL;
43 h->tag = 0;
44
45 p->client_first_part_hunk = h;
46 }
47 41
48 return NGX_OK; 42 return NGX_OK;
49 } 43 }
50 44
51 45
52 int ngx_http_read_client_body(ngx_event_t *rev) 46 int ngx_http_read_client_request_body(ngx_http_request_t *r)
53 { 47 {
48 int size, n, rc;
49 ngx_chain_t *entry;
50 ngx_http_request_body_t *rb;
51
52 rb = r->request_body;
54 53
55 do { 54 do {
56 if (r->header_in->last < r->header_in->end) { 55 if (r->header_in->last < r->header_in->end) {
57 rb->chain[0].hunk = r->header_in; 56 rb->chain[0].hunk = r->header_in;
58 57
68 } else { 67 } else {
69 rb->chain[0].hunk = rb->hunk; 68 rb->chain[0].hunk = rb->hunk;
70 rb->chain[0].next = NULL; 69 rb->chain[0].next = NULL;
71 } 70 }
72 71
73 n = ngx_recv_chain(c, &rb->chain); 72 n = ngx_recv_chain(r->connection, rb->chain);
74 73
75 if (n == NGX_ERROR) { 74 if (n == NGX_ERROR) {
76 return NGX_ERROR; 75 return NGX_ERROR;
77 } 76 }
78 77
79 if (n == NGX_AGAIN) { 78 if (n == NGX_AGAIN) {
80 return NGX_AGAIN; 79 return NGX_AGAIN;
81 } 80 }
82 81
83 for (entry = &rb->chain; entry; entry = entry->next) { 82 for (entry = rb->chain; entry; entry = entry->next) {
84 size = entry->hunk->end - entry->hunk->last; 83 size = entry->hunk->end - entry->hunk->last;
85 84
86 if (n >= size) { 85 if (n >= size) {
87 n -= size; 86 n -= size;
88 entry->hunk->last = entry->hunk->end; 87 entry->hunk->last = entry->hunk->end;
94 93
95 break; 94 break;
96 } 95 }
97 96
98 if (rb->hunk && rb->hunk->last == rb->hunk->end) { 97 if (rb->hunk && rb->hunk->last == rb->hunk->end) {
99 if (rb->temp_file->fd == NGX_INVALID_FILE) { 98 if (rb->temp_file.fd == NGX_INVALID_FILE) {
100 rc = ngx_create_temp_file(rb->temp_file, rb->temp_path, r->pool, 99 rc = ngx_create_temp_file(&rb->temp_file, rb->temp_path,
101 rb->number, rb->random, 0); 100 r->pool, 0);
102 101
103 if (rc == NGX_ERROR) { 102 if (rc == NGX_ERROR) {
104 return NGX_ERROR; 103 return NGX_ERROR;
105 } 104 }
106 105
107 if (rc == NGX_AGAIN) { 106 if (rc == NGX_AGAIN) {
108 return NGX_AGAIN; 107 return NGX_AGAIN;
109 } 108 }
110 } 109 }
111 110
112 n = ngx_write_file(rb->temp_file, rb->hunk, 111 n = ngx_write_file(&rb->temp_file, rb->hunk->pos,
113 rb->hunk->last - rb->hunk->pos, rb->offset); 112 rb->hunk->last - rb->hunk->pos, rb->offset);
114 113
115 if (rc == NGX_ERROR) { 114 if (rc == NGX_ERROR) {
116 return NGX_ERROR; 115 return NGX_ERROR;
117 } 116 }
118 117
119 rb->offset += n; 118 rb->offset += n;
120 rb->hunk->last = rb->hunk->pos; 119 rb->hunk->last = rb->hunk->pos;
121 } 120 }
122 121
123 } while (rev->ready); 122 } while (r->connection->read->ready);
124 123
125 return NGX_OK; 124 return NGX_OK;
126 } 125 }
127 126
128 127
129 int ngx_init_client_request_body_chain(ngx_http_reuqest_t *r) 128 int ngx_http_init_client_request_body_chain(ngx_http_request_t *r)
130 { 129 {
131 int i; 130 int i;
132 ngx_hunk_t *h; 131 ngx_hunk_t *h;
133 ngx_http_request_body_t *rb; 132 ngx_http_request_body_t *rb;
134 133
141 rb->chain[i].next = &rb->chain[i + 1]; 140 rb->chain[i].next = &rb->chain[i + 1];
142 i++; 141 i++;
143 rb->chain[i].hunk = r->header_in; 142 rb->chain[i].hunk = r->header_in;
144 } 143 }
145 144
146 if (rb->temp_file->fd != NGX_INVALID_FILE) { 145 if (rb->temp_file.fd != NGX_INVALID_FILE) {
146
147 if (rb->file_hunk == NULL) { 147 if (rb->file_hunk == NULL) {
148 ngx_test_null(h, ngx_alloc_hunk(r->pool), NGX_ERROR); 148 ngx_test_null(h, ngx_alloc_hunk(r->pool), NGX_ERROR);
149 149
150 h->type = NGX_HUNK_FILE; 150 h->type = NGX_HUNK_FILE;
151 h->pos = h->start = h->pre_start = 0; 151 h->pos = h->start = h->pre_start = 0;
152 h->last = h->end = h->post_end = 0; 152 h->last = h->end = h->post_end = 0;
153 h->file_pos = 0; 153 h->file_pos = 0;
154 h->file_last = rb->offset; 154 h->file_last = rb->offset;
155 h->file = rb->temp_file; 155 h->file = &rb->temp_file;
156 h->shadow = NULL; 156 h->shadow = NULL;
157 h->tag = 0; 157 h->tag = 0;
158 158
159 rb->file_hunk = h; 159 rb->file_hunk = h;
160 } 160 }
174 174
175 return NGX_OK; 175 return NGX_OK;
176 } 176 }
177 177
178 178
179 int ngx_reinit_client_request_body_hunks(ngx_http_reuqest_t *r) 179 void ngx_http_reinit_client_request_body_hunks(ngx_http_request_t *r)
180 { 180 {
181 ngx_http_request_body_t *rb; 181 ngx_http_request_body_t *rb;
182 182
183 rb = r->request_body; 183 rb = r->request_body;
184 184
185 if (rb->header_in_pos) { 185 if (rb->header_in_pos) {
186 r->header_in->pos = rb->header_in_pos; 186 r->header_in->pos = rb->header_in_pos;
187 } 187 }
188 188
189 if (rb->file_hunk) { 189 if (rb->file_hunk) {
190 rb->file_hunk->file_pos = rb->file_hunk->file_start; 190 rb->file_hunk->file_pos = 0;
191 } 191 }
192 192
193 if (rb->hunk) { 193 if (rb->hunk) {
194 rb->hunk->pos = rb->hunk->start; 194 rb->hunk->pos = rb->hunk->start;
195 } 195 }