Mercurial > hg > nginx
comparison src/http/modules/ngx_http_event_proxy_handler.c @ 19:d7908993fdeb
nginx-0.0.1-2002-12-02-19:09:40 import; resume after 2 months stall
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Mon, 02 Dec 2002 16:09:40 +0000 |
parents | |
children | a649c0a0adb3 |
comparison
equal
deleted
inserted
replaced
18:72ad26c77d2d | 19:d7908993fdeb |
---|---|
1 | |
2 #include <ngx_config.h> | |
3 #include <ngx_core.h> | |
4 #include <ngx_string.h> | |
5 #include <ngx_file.h> | |
6 #include <ngx_hunk.h> | |
7 #include <ngx_http.h> | |
8 #include <ngx_http_event_proxy_handler.h> | |
9 | |
10 ngx_http_module_t ngx_http_proxy_module; | |
11 | |
12 | |
13 static int ngx_http_proxy_connect(ngx_http_request_t *r, | |
14 struct sockaddr_in *addr, | |
15 char *addr_text); | |
16 static int ngx_http_proxy_send_request(ngx_event_t *ev); | |
17 | |
18 | |
19 int ngx_http_proxy_handler(ngx_http_request_t *r) | |
20 { | |
21 struct sockaddr_in addr; | |
22 ngx_http_proxy_ctx_t *p; | |
23 | |
24 p = (ngx_http_proxy_ctx_t *) ngx_get_module_ctx(r, ngx_http_proxy_module); | |
25 | |
26 if (p == NULL) | |
27 ngx_http_create_ctx(r, p, ngx_http_proxy_module, | |
28 sizeof(ngx_http_proxy_ctx_t)); | |
29 | |
30 ngx_memzero(&addr, sizeof(struct sockaddr_in)); | |
31 addr.sin_family = AF_INET; | |
32 addr.sin_addr.s_addr = inet_addr("127.0.0.1"); | |
33 addr.sin_port = htons(9000); | |
34 | |
35 ngx_http_proxy_connect(r, &addr, "connecting to 127.0.0.1:9000"); | |
36 } | |
37 | |
38 static int ngx_http_proxy_connect(ngx_http_request_t *r, | |
39 struct sockaddr_in *addr, | |
40 char *addr_text) | |
41 { | |
42 int rc; | |
43 ngx_err_t err; | |
44 ngx_socket_t s; | |
45 ngx_event_t *ev; | |
46 ngx_connection_t *c; | |
47 ngx_http_log_ctx_t *ctx; | |
48 | |
49 c = r->connection; | |
50 ctx = c->log->data; | |
51 ctx->action = addr_text; | |
52 | |
53 s = ngx_socket(AF_INET, SOCK_STREAM, IPPROTO_IP, 0); | |
54 if (s == -1) { | |
55 ngx_log_error(NGX_LOG_ERR, c->log, ngx_socket_errno, | |
56 ngx_socket_n " failed"); | |
57 return NGX_ERROR; | |
58 } | |
59 | |
60 #if 0 | |
61 if (rcvbuf) { | |
62 if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, | |
63 (const void *) &rcvbuf, sizeof(int)) == -1) { | |
64 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_socket_errno, | |
65 "setsockopt(SO_RCVBUF) failed"); | |
66 | |
67 if (ngx_close_socket(s) == -1) | |
68 ngx_log_error(NGX_LOG_ERR, c->log, ngx_socket_errno, | |
69 ngx_close_socket_n " failed"); | |
70 | |
71 return NGX_ERROR; | |
72 } | |
73 } | |
74 #endif | |
75 | |
76 if (ngx_nonblocking(s) == -1) { | |
77 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_socket_errno, | |
78 ngx_nonblocking_n " failed"); | |
79 | |
80 if (ngx_close_socket(s) == -1) | |
81 ngx_log_error(NGX_LOG_ERR, c->log, ngx_socket_errno, | |
82 ngx_close_socket_n " failed"); | |
83 | |
84 return NGX_ERROR; | |
85 } | |
86 | |
87 rc = connect(s, (struct sockaddr *) addr, sizeof(struct sockaddr_in)); | |
88 | |
89 if (rc == -1) { | |
90 err = ngx_socket_errno; | |
91 if (err != NGX_EINPROGRESS) { | |
92 ngx_log_error(NGX_LOG_ERR, c->log, err, "connect() failed"); | |
93 | |
94 if (ngx_close_socket(s) == -1) | |
95 ngx_log_error(NGX_LOG_ERR, c->log, ngx_socket_errno, | |
96 ngx_close_socket_n " failed"); | |
97 | |
98 return NGX_ERROR; | |
99 } | |
100 } | |
101 | |
102 ngx_memzero(&ngx_read_events[s], sizeof(ngx_event_t)); | |
103 ngx_memzero(&ngx_write_events[s], sizeof(ngx_event_t)); | |
104 ngx_memzero(&ngx_connections[s], sizeof(ngx_connection_t)); | |
105 | |
106 ngx_read_events[s].data = ngx_write_events[s].data = &ngx_connections[s]; | |
107 ngx_connections[s].read = &ngx_read_events[s]; | |
108 ngx_connections[s].write = &ngx_write_events[s]; | |
109 | |
110 ngx_connections[s].fd = s; | |
111 ngx_connections[s].server = c->server; | |
112 ngx_connections[s].servers = c->servers; | |
113 | |
114 ngx_connections[s].log = | |
115 ngx_read_events[s].log = ngx_write_events[s].log = c->log; | |
116 | |
117 if (rc == -1) { | |
118 ngx_write_events[s].event_handler = ngx_http_proxy_send_request; | |
119 | |
120 return ngx_add_event(&ngx_write_events[s], | |
121 NGX_WRITE_EVENT, NGX_ONESHOT_EVENT); | |
122 } | |
123 | |
124 ngx_write_events[s].write = 1; | |
125 ngx_write_events[s].ready = 1; | |
126 | |
127 return ngx_http_proxy_send_request(ev); | |
128 } | |
129 | |
130 static int ngx_http_proxy_send_request(ngx_event_t *ev) | |
131 { | |
132 return NGX_ERROR; | |
133 } | |
134 | |
135 #if 0 | |
136 | |
137 static int ngx_http_proxy_send_request(ngx_event_t *ev) | |
138 { | |
139 ngx_connection_t *c; | |
140 ngx_http_request_t *r; | |
141 ngx_http_proxy_ctx_t *p; | |
142 | |
143 c = (ngx_connection_t *) ev->data; | |
144 r = (ngx_http_request_t *) c->data; | |
145 p = (ngx_http_proxy_ctx_t *) ngx_get_module_ctx(r, ngx_http_proxy_module); | |
146 | |
147 n = ngx_send(p->fd, p->header_out->pos.mem, | |
148 p->header_out->end.mem - p->header_out->pos.mem); | |
149 | |
150 if (n == NGX_ERROR) { | |
151 ngx_log_error(NGX_LOG_ERR, r->log, ngx_socket_errno, | |
152 ngx_send_n " %s falied", p->addr_text); | |
153 return NGX_ERROR; | |
154 } | |
155 | |
156 p->header_out->pos.mem += n; | |
157 | |
158 if (p->header_out->end.mem - p->header_out->pos.mem > 0) | |
159 return NGX_AGAIN; | |
160 | |
161 /* TODO: body */ | |
162 | |
163 return NGX_OK; | |
164 } | |
165 | |
166 static int ngx_http_proxy_read_response_header(ngx_event_t *ev) | |
167 { | |
168 ngx_connection_t *c; | |
169 ngx_http_request_t *r; | |
170 ngx_http_proxy_ctx_t *p; | |
171 | |
172 if (ev->timedout) | |
173 return NGX_ERROR; | |
174 | |
175 c = (ngx_connection_t *) ev->data; | |
176 r = (ngx_http_request_t *) c->data; | |
177 p = (ngx_http_proxy_ctx_t *) ngx_get_module_ctx(r, ngx_http_proxy_module); | |
178 | |
179 n = ngx_event_recv(c, p->header_in->last.mem, | |
180 p->header_in->end - p->header_in->last.mem); | |
181 | |
182 } | |
183 | |
184 static int ngx_http_proxy_read_response_body(ngx_event_t *ev) | |
185 { | |
186 ngx_connection_t *c; | |
187 ngx_http_request_t *r; | |
188 ngx_http_proxy_ctx_t *p; | |
189 | |
190 if (ev->timedout) | |
191 return NGX_ERROR; | |
192 | |
193 c = (ngx_connection_t *) ev->data; | |
194 r = (ngx_http_request_t *) c->data; | |
195 p = (ngx_http_proxy_ctx_t *) ngx_get_module_ctx(r, ngx_http_proxy_module); | |
196 | |
197 } | |
198 | |
199 static int ngx_http_proxy_write_to_client(ngx_event_t *ev) | |
200 { | |
201 /* если бэкенд быстрее, то CLEAR, иначе - ONESHOT */ | |
202 | |
203 rc = ngx_http_output_filter(r, h); | |
204 } | |
205 | |
206 #endif |