Mercurial > hg > nginx
comparison src/http/modules/proxy/ngx_http_proxy_handler.c @ 131:049e78b1f852
nginx-0.0.1-2003-08-14-10:00:28 import
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Thu, 14 Aug 2003 06:00:28 +0000 |
parents | 842a78cebbb7 |
children | e29909bd9b8a |
comparison
equal
deleted
inserted
replaced
130:a490ad05164d | 131:049e78b1f852 |
---|---|
6 #include <ngx_http.h> | 6 #include <ngx_http.h> |
7 #include <ngx_http_proxy_handler.h> | 7 #include <ngx_http_proxy_handler.h> |
8 | 8 |
9 | 9 |
10 | 10 |
11 static void ngx_http_proxy_send_request(ngx_event_t *wev); | |
12 static void *ngx_http_proxy_create_loc_conf(ngx_conf_t *cf); | |
13 | |
14 | |
11 static ngx_command_t ngx_http_proxy_commands[] = { | 15 static ngx_command_t ngx_http_proxy_commands[] = { |
12 ngx_null_command | 16 ngx_null_command |
13 }; | 17 }; |
14 | 18 |
15 | 19 |
18 NULL, /* init main configuration */ | 22 NULL, /* init main configuration */ |
19 | 23 |
20 NULL, /* create server configuration */ | 24 NULL, /* create server configuration */ |
21 NULL, /* merge server configuration */ | 25 NULL, /* merge server configuration */ |
22 | 26 |
23 #if 0 | 27 ngx_http_proxy_create_loc_conf, /* create location configration */ |
24 ngx_http_proxy_create_conf, /* create location configration */ | 28 #if 0 |
25 ngx_http_proxy_merge_conf /* merge location configration */ | 29 ngx_http_proxy_merge_conf /* merge location configration */ |
26 #endif | 30 #endif |
27 | 31 |
28 NULL, | |
29 NULL | 32 NULL |
30 }; | 33 }; |
31 | 34 |
32 | 35 |
33 ngx_module_t ngx_http_proxy_module = { | 36 ngx_module_t ngx_http_proxy_module = { |
44 static | 47 static |
45 #endif | 48 #endif |
46 | 49 |
47 int ngx_http_proxy_handler(ngx_http_request_t *r) | 50 int ngx_http_proxy_handler(ngx_http_request_t *r) |
48 { | 51 { |
49 int rc; | 52 int rc; |
50 ngx_http_proxy_ctx_t *p; | 53 ngx_http_proxy_ctx_t *p; |
51 ngx_peer_connection_t *pc; | 54 ngx_http_proxy_loc_conf_t *lcf; |
52 | |
53 | 55 |
54 ngx_http_create_ctx(r, p, ngx_http_proxy_module, | 56 ngx_http_create_ctx(r, p, ngx_http_proxy_module, |
55 sizeof(ngx_http_proxy_ctx_t), | 57 sizeof(ngx_http_proxy_ctx_t), |
56 NGX_HTTP_INTERNAL_SERVER_ERROR); | 58 NGX_HTTP_INTERNAL_SERVER_ERROR); |
57 | 59 |
60 p->lcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module); | |
61 | |
62 #if 0 | |
63 create_request; | |
64 #endif | |
58 | 65 |
59 p->action = "connecting to upstream"; | 66 p->action = "connecting to upstream"; |
60 p->request = r; | 67 p->request = r; |
61 | 68 p->upstream.peers = p->lcf->peers; |
62 | 69 |
63 #if 0 | 70 /* TODO: change log->data, how to restore log->data ? */ |
64 pc->peers = lcf->peers; | |
65 #endif | |
66 | |
67 p->upstream.log = r->connection->log; | 71 p->upstream.log = r->connection->log; |
68 | 72 |
69 do { | 73 do { |
70 rc = ngx_event_connect_peer(&p->upstream); | 74 rc = ngx_event_connect_peer(&p->upstream); |
71 | 75 |
72 if (rc == NGX_ERROR) { | 76 if (rc == NGX_ERROR) { |
73 return NGX_HTTP_INTERNAL_SERVER_ERROR; | 77 return NGX_HTTP_INTERNAL_SERVER_ERROR; |
74 } | 78 } |
75 | 79 |
76 if (rc == NGX_OK) { | 80 if (rc == NGX_OK) { |
77 send_proxy_request(p); | 81 ngx_http_proxy_send_request(p->upstream.connection->write); |
78 return NGX_OK; | 82 /* ??? */ return NGX_OK; |
79 } | 83 } |
80 | 84 |
81 if (rc == NGX_AGAIN && p->upstream.connection) { | 85 if (rc == NGX_AGAIN) { |
82 return NGX_OK; | 86 /* ??? */ return NGX_OK; |
83 } | 87 } |
88 | |
89 /* rc == NGX_CONNECT_FAILED */ | |
90 | |
91 ngx_event_connect_peer_failed(&p->upstream); | |
84 | 92 |
85 } while (p->upstream.tries); | 93 } while (p->upstream.tries); |
86 | 94 |
87 return NGX_HTTP_BAD_GATEWAY; | 95 return NGX_HTTP_BAD_GATEWAY; |
88 } | 96 } |
89 | 97 |
90 | 98 |
91 #if 0 | 99 static void ngx_http_proxy_send_request(ngx_event_t *wev) |
92 | 100 { |
93 ngx_http_proxy_connect() | 101 int rc; |
94 do { | 102 ngx_chain_t *chain; |
95 ngx_event_connect_peer() | 103 ngx_connection_t *c; |
96 if error | 104 ngx_http_proxy_ctx_t *p; |
97 return error | 105 |
98 if ok | 106 c = wev->data; |
99 return ok | 107 p = c->data; |
100 if again | 108 |
101 return again | |
102 | |
103 /* next */ | |
104 while (tries) | |
105 } | |
106 | |
107 | |
108 ngx_http_proxy_send_request(ngx_event_t *wev) | |
109 for ( ;; ) { | 109 for ( ;; ) { |
110 send | 110 chain = ngx_write_chain(c, p->request_hunks); |
111 if ok | 111 |
112 ??? | 112 if (chain == (ngx_chain_t *) -1) { |
113 if again | 113 ngx_http_proxy_close_connection(c); |
114 return | 114 |
115 if error | 115 do { |
116 close | 116 rc = ngx_event_connect_peer(&p->upstream); |
117 ngx_http_proxy_connect() | 117 |
118 if ok | 118 if (rc == NGX_OK) { |
119 continue | 119 #if 0 |
120 if error | 120 copy chain and hunks p->request_hunks from p->initial_request_hunks; |
121 return | 121 #endif |
122 if again | 122 c = p->connection; |
123 return | 123 wev = c->write; |
124 | |
125 break; | |
126 } | |
127 | |
128 if (rc == NGX_ERROR) { | |
129 ngx_http_finalize_request(p->request, | |
130 NGX_HTTP_INTERNAL_SERVER_ERROR); | |
131 return; | |
132 } | |
133 | |
134 if (rc == NGX_AGAIN) { | |
135 return; | |
136 } | |
137 | |
138 /* rc == NGX_CONNECT_FAILED */ | |
139 | |
140 ngx_event_connect_peer_failed(&p->upstream); | |
141 | |
142 } while (p->upstream.tries); | |
143 | |
144 return; | |
145 | |
146 } else { | |
147 p->request_hunks = chain; | |
148 | |
149 ngx_del_timer(wev); | |
150 | |
151 if (chain) { | |
152 ngx_add_timer(wev, p->lcf->send_timeout); | |
153 wev->timer_set = 1; | |
154 | |
155 } else { | |
156 wev->timer_set = 0; | |
157 /* TODO: del event */ | |
158 } | |
159 | |
160 return; | |
161 } | |
124 } | 162 } |
125 | 163 } |
126 #endif | |
127 | 164 |
128 | 165 |
129 static size_t ngx_http_proxy_log_error(void *data, char *buf, size_t len) | 166 static size_t ngx_http_proxy_log_error(void *data, char *buf, size_t len) |
130 { | 167 { |
131 ngx_http_proxy_ctx_t *p = data; | 168 ngx_http_proxy_ctx_t *p = data; |
135 p->action, | 172 p->action, |
136 p->upstream.peers->peers[p->upstream.cur_peer].addr_port_text.data, | 173 p->upstream.peers->peers[p->upstream.cur_peer].addr_port_text.data, |
137 p->request->connection->addr_text.data, | 174 p->request->connection->addr_text.data, |
138 p->request->unparsed_uri.data); | 175 p->request->unparsed_uri.data); |
139 } | 176 } |
177 | |
178 | |
179 static void *ngx_http_proxy_create_loc_conf(ngx_conf_t *cf) | |
180 { | |
181 ngx_http_proxy_loc_conf_t *conf; | |
182 | |
183 ngx_test_null(conf, | |
184 ngx_pcalloc(cf->pool, sizeof(ngx_http_proxy_loc_conf_t)), | |
185 NGX_CONF_ERROR); | |
186 | |
187 /* STUB */ | |
188 ngx_test_null(conf->peers, ngx_pcalloc(cf->pool, sizeof(ngx_peers_t)), | |
189 NGX_CONF_ERROR); | |
190 | |
191 conf->peers->number = 1; | |
192 conf->peers->peers[0].addr = inet_addr("127.0.0.1"); | |
193 conf->peers->peers[0].host.data = "localhost"; | |
194 conf->peers->peers[0].host.len = sizeof("localhost") - 1; | |
195 conf->peers->peers[0].port = htons(9000); | |
196 conf->peers->peers[0].addr_port_text.data = "127.0.0.1:9000"; | |
197 conf->peers->peers[0].addr_port_text.len = sizeof("127.0.0.1:9000") - 1; | |
198 | |
199 conf->send_timeout = 30000; | |
200 /* */ | |
201 | |
202 return conf; | |
203 } |