comparison src/http/modules/ngx_http_event_proxy_handler.c @ 21:df7fb216a149

nginx-0.0.1-2002-12-04-19:29:40 import
author Igor Sysoev <igor@sysoev.ru>
date Wed, 04 Dec 2002 16:29:40 +0000
parents a649c0a0adb3
children aa3b53e74728
comparison
equal deleted inserted replaced
20:a649c0a0adb3 21:df7fb216a149
14 static ngx_chain_t *ngx_http_proxy_create_request(ngx_http_request_t *r); 14 static ngx_chain_t *ngx_http_proxy_create_request(ngx_http_request_t *r);
15 static int ngx_http_proxy_connect(ngx_http_request_t *r, 15 static int ngx_http_proxy_connect(ngx_http_request_t *r,
16 struct sockaddr_in *addr, 16 struct sockaddr_in *addr,
17 char *addr_text); 17 char *addr_text);
18 static int ngx_http_proxy_send_request(ngx_event_t *ev); 18 static int ngx_http_proxy_send_request(ngx_event_t *ev);
19 static int ngx_http_proxy_read_response_header(ngx_event_t *ev);
20
21
22 static char conn_close[] = "Connection: close" CRLF;
19 23
20 24
21 int ngx_http_proxy_handler(ngx_http_request_t *r) 25 int ngx_http_proxy_handler(ngx_http_request_t *r)
22 { 26 {
23 struct sockaddr_in addr; 27 struct sockaddr_in addr;
51 size_t len; 55 size_t len;
52 ngx_hunk_t *hunk; 56 ngx_hunk_t *hunk;
53 ngx_chain_t *chain; 57 ngx_chain_t *chain;
54 ngx_table_elt_t *header; 58 ngx_table_elt_t *header;
55 59
56 /* STUB */ 60 /* "+ 4" is for "\r\n" after request line and at the header end */
57 int size = 1024; 61 len = r->request_line.len + 4;
58 62
59 /* "+ 2" is for "\r\n" */ 63 /* "Connection: close\r\n" */
60 len = r->request_line.len + 2; 64 len += sizeof(conn_close) - 1;
61 65
62 header = (ngx_table_elt_t *) r->headers_in.headers->elts; 66 header = (ngx_table_elt_t *) r->headers_in.headers->elts;
63 for (i = 0; i < r->headers_in.headers->nelts; i++) { 67 for (i = 0; i < r->headers_in.headers->nelts; i++) {
64 if (&header[i] == r->headers_in.host) 68 if (&header[i] == r->headers_in.host)
65 continue; 69 continue;
66 70
67 /* "+ 4" is for ": " and "\r\n" */ 71 /* "+ 4" is for ": " and "\r\n" */
68 len += header[i].key.len + header[i].value.len + 4; 72 len += header[i].key.len + header[i].value.len + 4;
69 } 73 }
70 74
71 /* add "\r\n" at the header end */
72 len += 2;
73
74 /* STUB */ len++; 75 /* STUB */ len++;
75 76
76 ngx_test_null(hunk, ngx_create_temp_hunk(r->pool, len, 0, 0), NULL); 77 ngx_test_null(hunk, ngx_create_temp_hunk(r->pool, len, 0, 0), NULL);
77 ngx_add_hunk_to_chain(chain, hunk, r->pool, NULL); 78 ngx_add_hunk_to_chain(chain, hunk, r->pool, NULL);
78 79
79 ngx_memcpy(hunk->last.mem, r->request_line.data, r->request_line.len); 80 ngx_memcpy(hunk->last.mem, r->request_line.data, r->request_line.len);
80 hunk->last.mem += r->request_line.len; 81 hunk->last.mem += r->request_line.len;
81 *(hunk->last.mem++) = CR; *(hunk->last.mem++) = LF; 82 *(hunk->last.mem++) = CR; *(hunk->last.mem++) = LF;
82 83
84 ngx_memcpy(hunk->last.mem, conn_close, sizeof(conn_close) - 1);
85 hunk->last.mem += sizeof(conn_close) - 1;
86
83 for (i = 0; i < r->headers_in.headers->nelts; i++) { 87 for (i = 0; i < r->headers_in.headers->nelts; i++) {
84 if (&header[i] == r->headers_in.host) 88 if (&header[i] == r->headers_in.host)
85 continue; 89 continue;
86 90
91 if (&header[i] == r->headers_in.connection)
92 continue;
93
87 ngx_memcpy(hunk->last.mem, header[i].key.data, header[i].key.len); 94 ngx_memcpy(hunk->last.mem, header[i].key.data, header[i].key.len);
88 hunk->last.mem += header[i].key.len; 95 hunk->last.mem += header[i].key.len;
89 96
90 *(hunk->last.mem++) = ':'; *(hunk->last.mem++) = ' '; 97 *(hunk->last.mem++) = ':'; *(hunk->last.mem++) = ' ';
91 98
104 /* STUB */ *(hunk->last.mem++) = '\0'; 111 /* STUB */ *(hunk->last.mem++) = '\0';
105 ngx_log_debug(r->connection->log, "PROXY:\n'%s'" _ hunk->pos.mem); 112 ngx_log_debug(r->connection->log, "PROXY:\n'%s'" _ hunk->pos.mem);
106 113
107 return chain; 114 return chain;
108 } 115 }
116
109 117
110 static int ngx_http_proxy_connect(ngx_http_request_t *r, 118 static int ngx_http_proxy_connect(ngx_http_request_t *r,
111 struct sockaddr_in *addr, 119 struct sockaddr_in *addr,
112 char *addr_text) 120 char *addr_text)
113 { 121 {
114 int rc; 122 int rc;
115 ngx_err_t err; 123 ngx_err_t err;
116 ngx_socket_t s; 124 ngx_socket_t s;
125 ngx_event_t *rev, *wev;
117 ngx_connection_t *c, *pc; 126 ngx_connection_t *c, *pc;
118 ngx_http_log_ctx_t *ctx; 127 ngx_http_log_ctx_t *ctx;
119 128
120 c = r->connection; 129 c = r->connection;
121 ctx = c->log->data; 130 ctx = c->log->data;
169 return NGX_ERROR; 178 return NGX_ERROR;
170 } 179 }
171 } 180 }
172 181
173 pc = &ngx_connections[s]; 182 pc = &ngx_connections[s];
174 183 rev = &ngx_read_events[s];
175 ngx_memzero(&ngx_read_events[s], sizeof(ngx_event_t)); 184 wev = &ngx_write_events[s];
176 ngx_memzero(&ngx_write_events[s], sizeof(ngx_event_t)); 185
177 ngx_memzero(&ngx_connections[s], sizeof(ngx_connection_t)); 186 ngx_memzero(rev, sizeof(ngx_event_t));
178 187 ngx_memzero(wev, sizeof(ngx_event_t));
179 ngx_read_events[s].data = ngx_write_events[s].data = &ngx_connections[s]; 188 ngx_memzero(pc, sizeof(ngx_connection_t));
180 ngx_connections[s].read = &ngx_read_events[s]; 189
181 ngx_connections[s].write = &ngx_write_events[s]; 190 rev->data = wev->data = pc;
182 191 pc->read = rev;
183 ngx_connections[s].data = r; 192 pc->write = wev;
184 193
185 ngx_connections[s].fd = s; 194 pc->data = r;
186 ngx_connections[s].server = c->server; 195
187 ngx_connections[s].servers = c->servers; 196 pc->fd = s;
188 197 pc->server = c->server;
189 ngx_connections[s].log = 198 pc->servers = c->servers;
190 ngx_read_events[s].log = ngx_write_events[s].log = c->log; 199
200 pc->log = rev->log = wev->log = c->log;
191 201
192 ngx_test_null(pc->pool, 202 ngx_test_null(pc->pool,
193 ngx_create_pool(/* STUB */ 1024 /* */, pc->log), 203 ngx_create_pool(/* STUB */ 1024 /* */, pc->log),
194 NGX_ERROR); 204 NGX_ERROR);
195 205
196 if (rc == -1) { 206 wev->event_handler = ngx_http_proxy_send_request;
197 ngx_write_events[s].event_handler = ngx_http_proxy_send_request; 207 rev->event_handler = ngx_http_proxy_read_response_header;
198 208
199 return ngx_add_event(&ngx_write_events[s], 209 #if (HAVE_CLEAR_EVENT)
200 NGX_WRITE_EVENT, NGX_ONESHOT_EVENT); 210 if (ngx_add_event(rev, NGX_READ_EVENT, NGX_CLEAR_EVENT) != NGX_OK)
201 } 211 #else
202 212 if (ngx_add_event(rev, NGX_READ_EVENT, NGX_LEVEL_EVENT) != NGX_OK)
203 ngx_write_events[s].write = 1; 213 #endif
204 ngx_write_events[s].ready = 1; 214 return NGX_ERROR;
205 215
206 return ngx_http_proxy_send_request(&ngx_write_events[s]); 216 if (rc == -1)
207 } 217 return ngx_add_event(wev, NGX_WRITE_EVENT, NGX_ONESHOT_EVENT);
218
219 wev->write = 1;
220 wev->ready = 1;
221
222 return ngx_http_proxy_send_request(wev);
223 }
224
208 225
209 static int ngx_http_proxy_send_request(ngx_event_t *ev) 226 static int ngx_http_proxy_send_request(ngx_event_t *ev)
210 { 227 {
211 ngx_chain_t *chain; 228 ngx_chain_t *chain;
212 ngx_connection_t *c; 229 ngx_connection_t *c;
221 if (chain == (ngx_chain_t *) -1) 238 if (chain == (ngx_chain_t *) -1)
222 return NGX_ERROR; 239 return NGX_ERROR;
223 240
224 p->out = chain; 241 p->out = chain;
225 242
226 /* STUB */ return NGX_ERROR; 243 return NGX_AGAIN;
227 return NGX_OK; 244 }
228 } 245
229 246
230 #if 0 247 static int ngx_http_proxy_read_response_header(ngx_event_t *ev)
231 248 {
232 static int ngx_http_proxy_send_request(ngx_event_t *ev) 249 int n;
233 {
234 ngx_connection_t *c; 250 ngx_connection_t *c;
235 ngx_http_request_t *r; 251 ngx_http_request_t *r;
236 ngx_http_proxy_ctx_t *p; 252 ngx_http_proxy_ctx_t *p;
237 253
254 if (ev->timedout)
255 return NGX_ERROR;
256
238 c = (ngx_connection_t *) ev->data; 257 c = (ngx_connection_t *) ev->data;
239 r = (ngx_http_request_t *) c->data; 258 r = (ngx_http_request_t *) c->data;
240 p = (ngx_http_proxy_ctx_t *) ngx_get_module_ctx(r, ngx_http_proxy_module); 259 p = (ngx_http_proxy_ctx_t *) ngx_get_module_ctx(r, ngx_http_proxy_module);
241 260
242 n = ngx_send(p->fd, p->header_out->pos.mem, 261 if (p->header_in == NULL) {
243 p->header_out->end.mem - p->header_out->pos.mem); 262 ngx_test_null(p->header_in,
244 263 ngx_palloc(r->pool, sizeof(ngx_http_proxy_header_in_t)),
245 if (n == NGX_ERROR) { 264 NGX_ERROR);
246 ngx_log_error(NGX_LOG_ERR, r->log, ngx_socket_errno, 265
247 ngx_send_n " %s falied", p->addr_text); 266 ngx_test_null(p->hunk,
248 return NGX_ERROR; 267 ngx_create_temp_hunk(r->pool,
249 } 268 /* STUB */ 1024 /* */, 0, 0),
250 269 NGX_ERROR);
251 p->header_out->pos.mem += n; 270 }
252 271
253 if (p->header_out->end.mem - p->header_out->pos.mem > 0) 272 n = ngx_event_recv(c, p->hunk->last.mem, p->hunk->end - p->hunk->last.mem);
254 return NGX_AGAIN; 273
255 274 ngx_log_debug(r->connection->log, "READ:%d" _ n);
256 /* TODO: body */ 275
257 276 p->hunk->last.mem += n;
258 return NGX_OK; 277
259 } 278 *p->hunk->last.mem = '\0';
260 279 ngx_log_debug(r->connection->log, "PROXY:\n'%s'" _ p->hunk->pos.mem);
261 static int ngx_http_proxy_read_response_header(ngx_event_t *ev) 280
281 /* STUB */ return NGX_ERROR;
282 }
283
284 #if 0
285 static int ngx_http_proxy_read_response_body(ngx_event_t *ev)
262 { 286 {
263 ngx_connection_t *c; 287 ngx_connection_t *c;
264 ngx_http_request_t *r; 288 ngx_http_request_t *r;
265 ngx_http_proxy_ctx_t *p; 289 ngx_http_proxy_ctx_t *p;
266 290
269 293
270 c = (ngx_connection_t *) ev->data; 294 c = (ngx_connection_t *) ev->data;
271 r = (ngx_http_request_t *) c->data; 295 r = (ngx_http_request_t *) c->data;
272 p = (ngx_http_proxy_ctx_t *) ngx_get_module_ctx(r, ngx_http_proxy_module); 296 p = (ngx_http_proxy_ctx_t *) ngx_get_module_ctx(r, ngx_http_proxy_module);
273 297
274 n = ngx_event_recv(c, p->header_in->last.mem,
275 p->header_in->end - p->header_in->last.mem);
276
277 }
278
279 static int ngx_http_proxy_read_response_body(ngx_event_t *ev)
280 {
281 ngx_connection_t *c;
282 ngx_http_request_t *r;
283 ngx_http_proxy_ctx_t *p;
284
285 if (ev->timedout)
286 return NGX_ERROR;
287
288 c = (ngx_connection_t *) ev->data;
289 r = (ngx_http_request_t *) c->data;
290 p = (ngx_http_proxy_ctx_t *) ngx_get_module_ctx(r, ngx_http_proxy_module);
291
292 } 298 }
293 299
294 static int ngx_http_proxy_write_to_client(ngx_event_t *ev) 300 static int ngx_http_proxy_write_to_client(ngx_event_t *ev)
295 { 301 {
296 /* если бэкенд быстрее, то CLEAR, иначе - ONESHOT */ 302 /* если бэкенд быстрее, то CLEAR, иначе - ONESHOT */