add code to take incoming headers
[mod-proxy-fdpass.git] / mod_proxy_fdpass.c
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements.  See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License.  You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "mod_proxy.h"
18
19 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <sys/un.h>
22
23 #ifndef CMSG_DATA
24 #error This module only works on unix platforms with the correct OS support
25 #endif
26
27 #include "apr_version.h"
28 #if APR_MAJOR_VERSION < 2
29 /* for apr_wait_for_io_or_timeout */
30 #include "apr_support.h"
31 #endif
32
33 #include "mod_proxy_fdpass.h"
34
35 module AP_MODULE_DECLARE_DATA proxy_fdpass_module;
36
37 static int proxy_fdpass_canon(request_rec *r, char *url)
38 {
39     const char *path;
40
41     {
42         int mop_fd;
43         char mop_bf[512];
44
45         mop_fd = open("/tmp/apache_mop.log", O_WRONLY | O_APPEND | O_CREAT);
46         sprintf(mop_bf, "proxy_http_canon: start\n");
47         write(mop_fd, mop_bf, strlen(mop_bf));
48         close(mop_fd);
49
50     }
51
52     if (strncasecmp(url, "fd://", 5) == 0) {
53         url += 5;
54     }
55     else {
56         return DECLINED;
57     }
58     
59     path = ap_server_root_relative(r->pool, url);
60
61     r->filename = apr_pstrcat(r->pool, "proxy:fd://", path, NULL);
62
63     ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
64                   "proxy: FD: set r->filename to %s", r->filename);
65     return OK;
66 }
67
68 /* TODO: In APR 2.x: Extend apr_sockaddr_t to possibly be a path !!! */
69 static apr_status_t socket_connect_un(request_rec *r, apr_socket_t *sock,
70                                       struct sockaddr_un *sa)
71 {
72     apr_status_t rv;
73     apr_os_sock_t rawsock;
74     apr_interval_time_t t;
75
76     rv = apr_os_sock_get(&rawsock, sock);
77     if (rv != APR_SUCCESS) {
78         ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
79                       "proxy: FD: apr_os_sock_get failed");
80         return rv;
81     }
82
83     rv = apr_socket_timeout_get(sock, &t);
84     if (rv != APR_SUCCESS) {
85         ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
86                       "proxy: FD: apr_socket_timeout_get failed");
87         return rv;
88     }
89
90     do {
91         ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
92                       "proxy: FD: pre_connect");
93         rv = connect(rawsock, (struct sockaddr*)sa,
94                      sizeof(*sa) /* + strlen(sa->sun_path)*/ );
95         ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
96                       "proxy: FD: post_connect %d", rv);
97     } while (rv == -1 && errno == EINTR);
98
99     if ((rv == -1) && (errno == EINPROGRESS || errno == EALREADY)
100         && (t > 0)) {
101 #if APR_MAJOR_VERSION < 2
102         rv = apr_wait_for_io_or_timeout(NULL, sock, 0);
103 #else
104         rv = apr_socket_wait(sock, APR_WAIT_WRITE);
105 #endif
106
107         if (rv != APR_SUCCESS) {
108             ap_log_error(APLOG_MARK, APLOG_DEBUG, rv, NULL,
109                          "proxy: FD: apr_socket_wait failed");
110             return rv;
111         }
112     }
113     
114     if (rv == -1 && errno != EISCONN) {
115         ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
116                       "proxy: FD: socket_connect_un preexit %d", errno);
117         return errno;
118     }
119
120     return APR_SUCCESS;
121 }
122
123 static apr_status_t get_socket_from_path(request_rec *r, apr_pool_t *p,
124                                          const char* path,
125                                          apr_socket_t **out_sock)
126 {
127     struct sockaddr_un sa;
128     apr_socket_t *s;
129     apr_status_t rv;
130     *out_sock = NULL;
131
132     /*
133     ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
134                   "proxy: FD: Failed to connect to '%s' %d xxx",
135                       url, rv);
136     */
137     ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
138                  "proxy: FD: get_socket_from_path::START");
139
140     rv = apr_socket_create(&s, AF_UNIX, SOCK_STREAM, 0, p);
141
142     if (rv != APR_SUCCESS) {
143         ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
144                       "proxy: FD: get_socket_from_path::create %d", rv);
145         return rv;
146     }
147
148     sa.sun_family = AF_UNIX;
149     apr_cpystrn(sa.sun_path, path, sizeof(sa.sun_path));
150
151     rv = socket_connect_un(r, s, &sa);
152     if (rv != APR_SUCCESS) {
153         ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
154                       "proxy: FD: get_socket_from_path::connect_un %d", rv);
155         return rv;
156     }
157
158     *out_sock = s;
159
160     return APR_SUCCESS;
161 }
162
163
164 static apr_status_t send_socket(apr_pool_t *p,
165                                 apr_socket_t *s,
166                                 apr_socket_t *outbound)
167 {
168     apr_status_t rv;
169     apr_os_sock_t rawsock;
170     apr_os_sock_t srawsock;
171     struct msghdr msg;
172     struct cmsghdr *cmsg;
173     struct iovec iov;
174     char b = '\0', *buf;
175
176     {
177         int mop_fd;
178         char mop_bf[512];
179
180         mop_fd = open("/tmp/apache_mop.log", O_WRONLY | O_APPEND | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO);
181         sprintf(mop_bf, "send_socket: start\n");
182         write(mop_fd, mop_bf, strlen(mop_bf));
183         close(mop_fd);
184
185     }
186     
187     rv = apr_os_sock_get(&rawsock, outbound);
188     if (rv != APR_SUCCESS) {
189         return rv;
190     }
191
192     rv = apr_os_sock_get(&srawsock, s);
193     if (rv != APR_SUCCESS) {
194         return rv;
195     }
196     
197     {
198         int mop_fd;
199         char mop_bf[512];
200
201         mop_fd = open("/tmp/apache_mop.log", O_WRONLY | O_APPEND | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO);
202         write(mop_fd, "XX", 2);
203         write(mop_fd, &srawsock, sizeof(apr_os_sock_t));
204         write(mop_fd, "XX", 2);
205         close(mop_fd);
206
207     }
208
209     memset(&msg, 0, sizeof(msg));
210
211     msg.msg_iov = &iov;
212     msg.msg_iovlen = 1;
213
214     iov.iov_base = &b;
215     iov.iov_len = 1;
216
217     cmsg = apr_palloc(p, sizeof(*cmsg) + sizeof(rawsock));
218     cmsg->cmsg_len = sizeof(*cmsg) + sizeof(rawsock);
219     cmsg->cmsg_level = SOL_SOCKET;
220     cmsg->cmsg_type = SCM_RIGHTS;
221
222     memcpy(CMSG_DATA(cmsg), &rawsock, sizeof(rawsock));
223
224     msg.msg_control = cmsg;
225     msg.msg_controllen = cmsg->cmsg_len;
226
227     rv = sendmsg(srawsock, &msg, 0);
228
229     {
230         int mop_fd;
231         char mop_bf[512];
232
233         mop_fd = open("/tmp/apache_mop.log", O_WRONLY | O_APPEND | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO);
234         sprintf(mop_bf, "SENT BYTES: %d\n", rv);
235         write(mop_fd, mop_bf, strlen(mop_bf));
236         close(mop_fd);
237
238     }
239
240     if (rv == -1) {
241         return errno;
242     }
243
244     
245     return APR_SUCCESS;
246 }
247
248 static int headers_builder(void *rec, const char *key, const char *value)
249 {
250     char *s;
251
252     s = (char *)rec;
253
254     // TODO: verify length
255     sprintf(s, "%s%s:%s\n", s, key, value);
256 }
257
258 // TODO: sanitize calloc
259 static int proxy_fdpass_handler(request_rec *r, proxy_worker *worker,
260                               proxy_server_conf *conf,
261                               char *url, const char *proxyname,
262                               apr_port_t proxyport)
263 {
264     apr_status_t rv;
265     apr_socket_t *sock;
266     apr_socket_t *clientsock;
267     char *buf;
268     char *headers_out = NULL;
269
270     if ((headers_out = calloc(8*1024, 1)) != NULL) {
271         apr_table_do(headers_builder, headers_out, r->headers_in, NULL);
272     }
273
274
275     {
276         int mop_fd;
277         char mop_bf[512];
278
279         mop_fd = open("/tmp/apache_mop.log", O_WRONLY | O_APPEND | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO);
280         sprintf(mop_bf, "proxy_fdpass_handler: start\n");
281         write(mop_fd, mop_bf, strlen(mop_bf));
282         write(mop_fd, headers_out, strlen(headers_out));
283         close(mop_fd);
284
285     }
286     /*
287     if (headers_out)
288         free(headers_out);
289     */
290     if (strncasecmp(url, "fd://", 5) == 0) {
291         url += 5;
292     }
293     else {
294         return DECLINED;
295     }
296
297     rv = get_socket_from_path(r, r->pool, url, &sock);
298
299     if (rv != APR_SUCCESS) {
300         ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
301                       "proxy: FD: Failed to connect to '%s' %d xxx",
302                       url, rv);
303         return HTTP_INTERNAL_SERVER_ERROR;
304     }
305
306     {
307         int status;
308         /* const char *flush_method = worker->flusher ? worker->flusher : "flush"; */
309         const char *flush_method = "flush"; 
310
311         proxy_fdpass_flush *flush = ap_lookup_provider(PROXY_FDPASS_FLUSHER,
312                                                        flush_method, "0");
313
314         if (!flush) {
315             ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
316                           "proxy: FD: Unable to find configured flush "
317                           "provider '%s'", flush_method);
318             return HTTP_INTERNAL_SERVER_ERROR;
319         }
320
321         status = flush->flusher(r);
322         if (status) {
323             return status;
324         }
325     }
326
327     if ((buf = apr_table_get(r->headers_in, "Host"))) {
328         ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
329                       "proxy: FD: Host is: [%s]", buf);
330     }
331
332     /* XXXXX: THIS IS AN EVIL HACK */
333     /* There should really be a (documented) public API for this ! */
334     clientsock = ap_get_module_config(r->connection->conn_config, &core_module);
335
336     rv = send_socket(r->pool, sock, clientsock);
337     if (rv != APR_SUCCESS) {
338         ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
339                       "proxy: FD: send_socket failed:");
340         return HTTP_INTERNAL_SERVER_ERROR;
341     }
342
343     {
344         apr_socket_t *dummy;
345         /* Create a dummy unconnected socket, and set it as the one we were 
346          * connected to, so that when the core closes it, it doesn't close 
347          * the tcp connection to the client.
348          */
349         rv = apr_socket_create(&dummy, APR_INET, SOCK_STREAM, APR_PROTO_TCP,
350                                r->connection->pool);
351         if (rv != APR_SUCCESS) {
352             ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
353                           "proxy: FD: failed to create dummy socket");
354             return HTTP_INTERNAL_SERVER_ERROR;
355         }
356         ap_set_module_config(r->connection->conn_config, &core_module, dummy);
357     }
358     
359     
360     return OK;
361 }
362
363 static int standard_flush(request_rec *r)
364 {
365     int status;
366     apr_bucket_brigade *bb;
367     apr_bucket *e;
368     apr_pool_t *p = r->pool;
369
370     r->connection->keepalive = AP_CONN_CLOSE;
371     /* MOP NOTE: set here the content type */
372     // ap_set_content_type(r, apr_pstrdup(p, "text/plain"));
373
374     bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
375     e = apr_bucket_flush_create(r->connection->bucket_alloc);
376     
377     APR_BRIGADE_INSERT_TAIL(bb, e);
378
379     status = ap_pass_brigade(r->output_filters, bb);
380
381     if (status != OK) {
382         ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r,
383                       "proxy: FD: ap_pass_brigade failed:");
384         return status;
385     }
386
387     return OK;
388 }
389
390 static const proxy_fdpass_flush builtin_flush =
391 {
392     "flush",
393     &standard_flush,
394     NULL
395 };
396
397 static void ap_proxy_fdpass_register_hooks(apr_pool_t *p)
398 {
399     ap_register_provider(p, PROXY_FDPASS_FLUSHER, "flush", "0", &builtin_flush);
400     proxy_hook_scheme_handler(proxy_fdpass_handler, NULL, NULL, APR_HOOK_FIRST);
401     proxy_hook_canon_handler(proxy_fdpass_canon, NULL, NULL, APR_HOOK_FIRST);
402 }
403
404 module AP_MODULE_DECLARE_DATA proxy_fdpass_module = {
405     STANDARD20_MODULE_STUFF,
406     NULL,              /* create per-directory config structure */
407     NULL,              /* merge per-directory config structures */
408     NULL,              /* create per-server config structure */
409     NULL,              /* merge per-server config structures */
410     NULL,              /* command apr_table_t */
411     ap_proxy_fdpass_register_hooks                /* register hooks */
412 };