comparison src/http/ngx_http_request_body.c @ 293:ec3c049681fd

nginx-0.0.3-2004-03-19-08:25:53 import
author Igor Sysoev <igor@sysoev.ru>
date Fri, 19 Mar 2004 05:25:53 +0000
parents a472bfb778b3
children 5cfd65b8b0a7
comparison
equal deleted inserted replaced
292:a472bfb778b3 293:ec3c049681fd
4 #include <ngx_event.h> 4 #include <ngx_event.h>
5 #include <ngx_http.h> 5 #include <ngx_http.h>
6 6
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 10
10 11
11 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,
12 size_t request_buffer_size) 13 size_t request_buffer_size)
13 { 14 {
15 ngx_int_t rc;
14 ssize_t size; 16 ssize_t size;
15 ngx_hunk_t *h; 17 ngx_hunk_t *h;
16 ngx_chain_t *cl; 18 ngx_chain_t *cl;
17 19
18 size = r->header_in->last - r->header_in->pos; 20 size = r->header_in->last - r->header_in->pos;
46 } 48 }
47 49
48 ngx_test_null(r->request_body_hunk, ngx_create_temp_hunk(r->pool, size), 50 ngx_test_null(r->request_body_hunk, ngx_create_temp_hunk(r->pool, size),
49 NGX_ERROR); 51 NGX_ERROR);
50 52
51 r->connection->read->event_handler =
52 ngx_http_read_client_request_body_handler;
53
54 ngx_http_read_client_request_body_handler(r->connection->read);
55
56 ngx_alloc_link_and_set_hunk(cl, r->request_body_hunk, r->pool, 53 ngx_alloc_link_and_set_hunk(cl, r->request_body_hunk, r->pool,
57 NGX_ERROR); 54 NGX_ERROR);
58 55
59 if (r->request_hunks) { 56 if (r->request_hunks) {
60 r->request_hunks->next = cl; 57 r->request_hunks->next = cl;
61 58
62 } else { 59 } else {
63 r->request_hunks = cl; 60 r->request_hunks = cl;
64 } 61 }
65 62
66 if (r->request_body_len) { 63 r->connection->read->event_handler =
67 return NGX_AGAIN; 64 ngx_http_read_client_request_body_handler;
68 }
69 65
70 return NGX_OK; 66 return ngx_http_do_read_client_request_body(r);
71 } 67 }
72 68
73 69
74 static void ngx_http_read_client_request_body_handler(ngx_event_t *rev) 70 static void ngx_http_read_client_request_body_handler(ngx_event_t *rev)
75 { 71 {
72 ngx_int_t rc;
73 ngx_connection_t *c;
74 ngx_http_request_t *r;
75
76 c = rev->data;
77 r = c->data;
78
79 rc = ngx_http_do_read_client_request_body(r);
80
81 if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
82 ngx_http_finalize_request(r, rc);
83 }
84 }
85
86
87 static ngx_int_t ngx_http_do_read_client_request_body(ngx_http_request_t *r)
88 {
76 size_t size; 89 size_t size;
77 ssize_t n; 90 ssize_t n;
78 ngx_hunk_t *h; 91 ngx_hunk_t *h;
79 ngx_connection_t *c; 92 ngx_connection_t *c;
80 ngx_http_request_t *r;
81 ngx_http_core_loc_conf_t *clcf; 93 ngx_http_core_loc_conf_t *clcf;
82 94
83 c = rev->data; 95 c = r->connection;
84 r = c->data;
85 96
86 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, 97 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
87 "http read client request body"); 98 "http read client request body");
88 99
89 for ( ;; ) { 100 for ( ;; ) {
93 r->request_hunks); 104 r->request_hunks);
94 105
95 /* TODO: n == 0 or not complete and level event */ 106 /* TODO: n == 0 or not complete and level event */
96 107
97 if (n == NGX_ERROR) { 108 if (n == NGX_ERROR) {
98 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); 109 return NGX_HTTP_INTERNAL_SERVER_ERROR;
99 return;
100 } 110 }
101 111
102 r->temp_file->offset += n; 112 r->temp_file->offset += n;
103 113
104 r->request_body_hunk->pos = r->request_body_hunk->start; 114 r->request_body_hunk->pos = r->request_body_hunk->start;
111 size = r->request_body_len; 121 size = r->request_body_len;
112 } 122 }
113 123
114 n = ngx_recv(c, r->request_body_hunk->last, size); 124 n = ngx_recv(c, r->request_body_hunk->last, size);
115 125
126 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
127 "http client request body recv " SIZE_T_FMT, n);
128
116 if (n == NGX_AGAIN) { 129 if (n == NGX_AGAIN) {
117 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); 130 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
118 ngx_add_timer(rev, clcf->client_body_timeout); 131 ngx_add_timer(c->read, clcf->client_body_timeout);
119 132
120 if (ngx_handle_read_event(rev, 0) == NGX_ERROR) { 133 if (ngx_handle_read_event(c->read, 0) == NGX_ERROR) {
121 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); 134 return NGX_HTTP_INTERNAL_SERVER_ERROR;
122 } 135 }
123 136
124 return; 137 return NGX_AGAIN;
125 } 138 }
126 139
127 if (n == 0) { 140 if (n == 0) {
128 ngx_log_error(NGX_LOG_INFO, c->log, 0, 141 ngx_log_error(NGX_LOG_INFO, c->log, 0,
129 "client closed prematurely connection"); 142 "client closed prematurely connection");
130 } 143 }
131 144
132 if (n == 0 || n == NGX_ERROR) { 145 if (n == 0 || n == NGX_ERROR) {
133 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST); 146 return NGX_HTTP_BAD_REQUEST;
134 return;
135 } 147 }
136 148
137 r->request_body_hunk->last += n; 149 r->request_body_hunk->last += n;
138 r->request_body_len -= n; 150 r->request_body_len -= n;
139 151
140 if (r->request_body_hunk->last < r->request_body_hunk->end) { 152 if (r->request_body_hunk->last < r->request_body_hunk->end) {
141 break; 153 break;
142 } 154 }
143 } 155 }
144 156
157 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
158 "http client request body left " SIZE_T_FMT,
159 r->request_body_len);
160
145 if (r->request_body_len) { 161 if (r->request_body_len) {
146 return; 162 return NGX_AGAIN;
147 } 163 }
148 164
149 if (r->temp_file->file.fd != NGX_INVALID_FILE) { 165 if (r->temp_file->file.fd != NGX_INVALID_FILE) {
150 166
151 /* save the last part */ 167 /* save the last part */
154 r->request_hunks); 170 r->request_hunks);
155 171
156 /* TODO: n == 0 or not complete and level event */ 172 /* TODO: n == 0 or not complete and level event */
157 173
158 if (n == NGX_ERROR) { 174 if (n == NGX_ERROR) {
159 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); 175 return NGX_HTTP_INTERNAL_SERVER_ERROR;
160 return;
161 } 176 }
162 177
163 h = ngx_calloc_hunk(r->pool); 178 h = ngx_calloc_hunk(r->pool);
164 if (h == NULL) { 179 if (h == NULL) {
165 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); 180 return NGX_HTTP_INTERNAL_SERVER_ERROR;
166 return;
167 } 181 }
168 182
169 h->type = NGX_HUNK_FILE; 183 h->type = NGX_HUNK_FILE;
170 h->file_pos = 0; 184 h->file_pos = 0;
171 h->file_last = r->temp_file->file.offset; 185 h->file_last = r->temp_file->file.offset;
178 r->request_hunks->hunk = h; 192 r->request_hunks->hunk = h;
179 } 193 }
180 } 194 }
181 195
182 r->request_body_handler(r->data); 196 r->request_body_handler(r->data);
197
198 return NGX_OK;
183 } 199 }