comparison src/http/modules/ngx_http_event_proxy_handler.c @ 20:a649c0a0adb3

nginx-0.0.1-2002-12-03-18:45:38 import
author Igor Sysoev <igor@sysoev.ru>
date Tue, 03 Dec 2002 15:45:38 +0000
parents d7908993fdeb
children df7fb216a149
comparison
equal deleted inserted replaced
19:d7908993fdeb 20:a649c0a0adb3
2 #include <ngx_config.h> 2 #include <ngx_config.h>
3 #include <ngx_core.h> 3 #include <ngx_core.h>
4 #include <ngx_string.h> 4 #include <ngx_string.h>
5 #include <ngx_file.h> 5 #include <ngx_file.h>
6 #include <ngx_hunk.h> 6 #include <ngx_hunk.h>
7 #include <ngx_event_write.h>
7 #include <ngx_http.h> 8 #include <ngx_http.h>
8 #include <ngx_http_event_proxy_handler.h> 9 #include <ngx_http_event_proxy_handler.h>
9 10
10 ngx_http_module_t ngx_http_proxy_module; 11 ngx_http_module_t ngx_http_proxy_module;
11 12
12 13
14 static ngx_chain_t *ngx_http_proxy_create_request(ngx_http_request_t *r);
13 static int ngx_http_proxy_connect(ngx_http_request_t *r, 15 static int ngx_http_proxy_connect(ngx_http_request_t *r,
14 struct sockaddr_in *addr, 16 struct sockaddr_in *addr,
15 char *addr_text); 17 char *addr_text);
16 static int ngx_http_proxy_send_request(ngx_event_t *ev); 18 static int ngx_http_proxy_send_request(ngx_event_t *ev);
17 19
18 20
19 int ngx_http_proxy_handler(ngx_http_request_t *r) 21 int ngx_http_proxy_handler(ngx_http_request_t *r)
20 { 22 {
21 struct sockaddr_in addr; 23 struct sockaddr_in addr;
24 ngx_chain_t *chain;
22 ngx_http_proxy_ctx_t *p; 25 ngx_http_proxy_ctx_t *p;
23 26
24 p = (ngx_http_proxy_ctx_t *) ngx_get_module_ctx(r, ngx_http_proxy_module); 27 p = (ngx_http_proxy_ctx_t *) ngx_get_module_ctx(r, ngx_http_proxy_module);
25 28
26 if (p == NULL) 29 if (p == NULL)
27 ngx_http_create_ctx(r, p, ngx_http_proxy_module, 30 ngx_http_create_ctx(r, p, ngx_http_proxy_module,
28 sizeof(ngx_http_proxy_ctx_t)); 31 sizeof(ngx_http_proxy_ctx_t));
32
33 chain = ngx_http_proxy_create_request(r);
34 if (chain == NULL)
35 return NGX_ERROR;
36
37 p->out = chain;
29 38
30 ngx_memzero(&addr, sizeof(struct sockaddr_in)); 39 ngx_memzero(&addr, sizeof(struct sockaddr_in));
31 addr.sin_family = AF_INET; 40 addr.sin_family = AF_INET;
32 addr.sin_addr.s_addr = inet_addr("127.0.0.1"); 41 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
33 addr.sin_port = htons(9000); 42 addr.sin_port = htons(9000);
34 43
35 ngx_http_proxy_connect(r, &addr, "connecting to 127.0.0.1:9000"); 44 ngx_http_proxy_connect(r, &addr, "connecting to 127.0.0.1:9000");
36 } 45 }
37 46
47
48 static ngx_chain_t *ngx_http_proxy_create_request(ngx_http_request_t *r)
49 {
50 int i;
51 size_t len;
52 ngx_hunk_t *hunk;
53 ngx_chain_t *chain;
54 ngx_table_elt_t *header;
55
56 /* STUB */
57 int size = 1024;
58
59 /* "+ 2" is for "\r\n" */
60 len = r->request_line.len + 2;
61
62 header = (ngx_table_elt_t *) r->headers_in.headers->elts;
63 for (i = 0; i < r->headers_in.headers->nelts; i++) {
64 if (&header[i] == r->headers_in.host)
65 continue;
66
67 /* "+ 4" is for ": " and "\r\n" */
68 len += header[i].key.len + header[i].value.len + 4;
69 }
70
71 /* add "\r\n" at the header end */
72 len += 2;
73
74 /* STUB */ len++;
75
76 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
79 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++) = CR; *(hunk->last.mem++) = LF;
82
83 for (i = 0; i < r->headers_in.headers->nelts; i++) {
84 if (&header[i] == r->headers_in.host)
85 continue;
86
87 ngx_memcpy(hunk->last.mem, header[i].key.data, header[i].key.len);
88 hunk->last.mem += header[i].key.len;
89
90 *(hunk->last.mem++) = ':'; *(hunk->last.mem++) = ' ';
91
92 ngx_memcpy(hunk->last.mem, header[i].value.data, header[i].value.len);
93 hunk->last.mem += header[i].value.len;
94
95 *(hunk->last.mem++) = CR; *(hunk->last.mem++) = LF;
96
97 ngx_log_debug(r->connection->log, "proxy: '%s: %s'" _
98 header[i].key.data _ header[i].value.data);
99 }
100
101 /* add "\r\n" at the header end */
102 *(hunk->last.mem++) = CR; *(hunk->last.mem++) = LF;
103
104 /* STUB */ *(hunk->last.mem++) = '\0';
105 ngx_log_debug(r->connection->log, "PROXY:\n'%s'" _ hunk->pos.mem);
106
107 return chain;
108 }
109
38 static int ngx_http_proxy_connect(ngx_http_request_t *r, 110 static int ngx_http_proxy_connect(ngx_http_request_t *r,
39 struct sockaddr_in *addr, 111 struct sockaddr_in *addr,
40 char *addr_text) 112 char *addr_text)
41 { 113 {
42 int rc; 114 int rc;
43 ngx_err_t err; 115 ngx_err_t err;
44 ngx_socket_t s; 116 ngx_socket_t s;
45 ngx_event_t *ev; 117 ngx_connection_t *c, *pc;
46 ngx_connection_t *c;
47 ngx_http_log_ctx_t *ctx; 118 ngx_http_log_ctx_t *ctx;
48 119
49 c = r->connection; 120 c = r->connection;
50 ctx = c->log->data; 121 ctx = c->log->data;
51 ctx->action = addr_text; 122 ctx->action = addr_text;
97 168
98 return NGX_ERROR; 169 return NGX_ERROR;
99 } 170 }
100 } 171 }
101 172
173 pc = &ngx_connections[s];
174
102 ngx_memzero(&ngx_read_events[s], sizeof(ngx_event_t)); 175 ngx_memzero(&ngx_read_events[s], sizeof(ngx_event_t));
103 ngx_memzero(&ngx_write_events[s], sizeof(ngx_event_t)); 176 ngx_memzero(&ngx_write_events[s], sizeof(ngx_event_t));
104 ngx_memzero(&ngx_connections[s], sizeof(ngx_connection_t)); 177 ngx_memzero(&ngx_connections[s], sizeof(ngx_connection_t));
105 178
106 ngx_read_events[s].data = ngx_write_events[s].data = &ngx_connections[s]; 179 ngx_read_events[s].data = ngx_write_events[s].data = &ngx_connections[s];
107 ngx_connections[s].read = &ngx_read_events[s]; 180 ngx_connections[s].read = &ngx_read_events[s];
108 ngx_connections[s].write = &ngx_write_events[s]; 181 ngx_connections[s].write = &ngx_write_events[s];
109 182
183 ngx_connections[s].data = r;
184
110 ngx_connections[s].fd = s; 185 ngx_connections[s].fd = s;
111 ngx_connections[s].server = c->server; 186 ngx_connections[s].server = c->server;
112 ngx_connections[s].servers = c->servers; 187 ngx_connections[s].servers = c->servers;
113 188
114 ngx_connections[s].log = 189 ngx_connections[s].log =
115 ngx_read_events[s].log = ngx_write_events[s].log = c->log; 190 ngx_read_events[s].log = ngx_write_events[s].log = c->log;
116 191
192 ngx_test_null(pc->pool,
193 ngx_create_pool(/* STUB */ 1024 /* */, pc->log),
194 NGX_ERROR);
195
117 if (rc == -1) { 196 if (rc == -1) {
118 ngx_write_events[s].event_handler = ngx_http_proxy_send_request; 197 ngx_write_events[s].event_handler = ngx_http_proxy_send_request;
119 198
120 return ngx_add_event(&ngx_write_events[s], 199 return ngx_add_event(&ngx_write_events[s],
121 NGX_WRITE_EVENT, NGX_ONESHOT_EVENT); 200 NGX_WRITE_EVENT, NGX_ONESHOT_EVENT);
122 } 201 }
123 202
124 ngx_write_events[s].write = 1; 203 ngx_write_events[s].write = 1;
125 ngx_write_events[s].ready = 1; 204 ngx_write_events[s].ready = 1;
126 205
127 return ngx_http_proxy_send_request(ev); 206 return ngx_http_proxy_send_request(&ngx_write_events[s]);
128 } 207 }
129 208
130 static int ngx_http_proxy_send_request(ngx_event_t *ev) 209 static int ngx_http_proxy_send_request(ngx_event_t *ev)
131 { 210 {
132 return NGX_ERROR; 211 ngx_chain_t *chain;
212 ngx_connection_t *c;
213 ngx_http_request_t *r;
214 ngx_http_proxy_ctx_t *p;
215
216 c = (ngx_connection_t *) ev->data;
217 r = (ngx_http_request_t *) c->data;
218 p = (ngx_http_proxy_ctx_t *) ngx_get_module_ctx(r, ngx_http_proxy_module);
219
220 chain = ngx_event_write(c, p->out, 0);
221 if (chain == (ngx_chain_t *) -1)
222 return NGX_ERROR;
223
224 p->out = chain;
225
226 /* STUB */ return NGX_ERROR;
227 return NGX_OK;
133 } 228 }
134 229
135 #if 0 230 #if 0
136 231
137 static int ngx_http_proxy_send_request(ngx_event_t *ev) 232 static int ngx_http_proxy_send_request(ngx_event_t *ev)