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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "mod_proxy.h"
19 #include <sys/types.h>
20 #include <sys/socket.h>
24 #error This module only works on unix platforms with the correct OS support
27 #include "apr_version.h"
28 #if APR_MAJOR_VERSION < 2
29 /* for apr_wait_for_io_or_timeout */
30 #include "apr_support.h"
33 #include "mod_proxy_fdpass.h"
35 module AP_MODULE_DECLARE_DATA proxy_fdpass_module;
37 static int proxy_fdpass_canon(request_rec *r, char *url)
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));
52 if (strncasecmp(url, "fd://", 5) == 0) {
59 path = ap_server_root_relative(r->pool, url);
61 r->filename = apr_pstrcat(r->pool, "proxy:fd://", path, NULL);
63 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
64 "proxy: FD: set r->filename to %s", r->filename);
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)
73 apr_os_sock_t rawsock;
74 apr_interval_time_t t;
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");
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");
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);
99 if ((rv == -1) && (errno == EINPROGRESS || errno == EALREADY)
101 #if APR_MAJOR_VERSION < 2
102 rv = apr_wait_for_io_or_timeout(NULL, sock, 0);
104 rv = apr_socket_wait(sock, APR_WAIT_WRITE);
107 if (rv != APR_SUCCESS) {
108 ap_log_error(APLOG_MARK, APLOG_DEBUG, rv, NULL,
109 "proxy: FD: apr_socket_wait failed");
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);
123 static apr_status_t get_socket_from_path(request_rec *r, apr_pool_t *p,
125 apr_socket_t **out_sock)
127 struct sockaddr_un sa;
133 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
134 "proxy: FD: Failed to connect to '%s' %d xxx",
137 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
138 "proxy: FD: get_socket_from_path::START");
140 rv = apr_socket_create(&s, AF_UNIX, SOCK_STREAM, 0, p);
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);
148 sa.sun_family = AF_UNIX;
149 apr_cpystrn(sa.sun_path, path, sizeof(sa.sun_path));
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);
163 #define ANCIL_FD_BUFFER(n) \
169 static apr_status_t send_socket(apr_pool_t *p,
171 apr_socket_t *outbound,
172 apr_socket_t *ctrlsock)
175 apr_os_sock_t rawsock;
176 apr_os_sock_t srawsock;
177 apr_os_sock_t sctrlsock;
179 struct cmsghdr *cmsg;
182 ANCIL_FD_BUFFER(2) ancil_buf;
188 mop_fd = open("/tmp/apache_mop.log", O_WRONLY | O_APPEND | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO);
189 sprintf(mop_bf, "send_socket: start\n");
190 write(mop_fd, mop_bf, strlen(mop_bf));
195 rv = apr_os_sock_get(&rawsock, outbound);
196 if (rv != APR_SUCCESS) {
200 rv = apr_os_sock_get(&srawsock, s);
201 if (rv != APR_SUCCESS) {
205 rv = apr_os_sock_get(&sctrlsock, ctrlsock);
206 if (rv != APR_SUCCESS) {
214 mop_fd = open("/tmp/apache_mop.log", O_WRONLY | O_APPEND | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO);
215 write(mop_fd, "XX", 2);
216 write(mop_fd, &srawsock, sizeof(apr_os_sock_t));
217 write(mop_fd, "XX", 2);
222 memset(&msg, 0, sizeof(msg));
230 msg.msg_control = &ancil_buf;
231 msg.msg_controllen = sizeof(struct cmsghdr) + sizeof(rawsock) * 2;
233 // cmsg = apr_palloc(p, sizeof(*cmsg) + sizeof(rawsock));
234 cmsg = CMSG_FIRSTHDR(&msg);
235 cmsg->cmsg_len = sizeof(*cmsg) + sizeof(rawsock) * 2;
236 cmsg->cmsg_level = SOL_SOCKET;
237 cmsg->cmsg_type = SCM_RIGHTS;
239 ((int *)CMSG_DATA(cmsg))[0] = rawsock;
240 ((int *)CMSG_DATA(cmsg))[1] = sctrlsock;
242 rv = sendmsg(srawsock, &msg, 0);
248 mop_fd = open("/tmp/apache_mop.log", O_WRONLY | O_APPEND | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO);
249 sprintf(mop_bf, "SENT BYTES: %d\n", rv);
250 write(mop_fd, mop_bf, strlen(mop_bf));
263 static int headers_builder(void *rec, const char *key, const char *value)
269 // TODO: verify length
270 sprintf(s, "%s%s:%s\n", s, key, value);
273 // TODO: sanitize calloc
274 static int proxy_fdpass_handler(request_rec *r, proxy_worker *worker,
275 proxy_server_conf *conf,
276 char *url, const char *proxyname,
277 apr_port_t proxyport)
281 apr_socket_t *clientsock;
283 char *headers_out = NULL;
285 apr_socket_t *ctrlsock = NULL, *clientctrlsock = NULL;
287 if ((headers_out = calloc(8*1024, 1)) != NULL) {
288 apr_table_do(headers_builder, headers_out, r->headers_in, NULL);
296 mop_fd = open("/tmp/apache_mop.log", O_WRONLY | O_APPEND | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO);
297 sprintf(mop_bf, "proxy_fdpass_handler: start\n");
298 write(mop_fd, mop_bf, strlen(mop_bf));
299 write(mop_fd, headers_out, strlen(headers_out));
307 if (strncasecmp(url, "fd://", 5) == 0) {
314 rv = get_socket_from_path(r, r->pool, url, &sock);
316 if (rv != APR_SUCCESS) {
317 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
318 "proxy: FD: Failed to connect to '%s' %d xxx",
320 return HTTP_INTERNAL_SERVER_ERROR;
323 /* create a couple of sockets and pass one to the client for headers and so on */
324 if (socketpair(AF_UNIX, SOCK_STREAM, 0, ctrlrawsock)) {
325 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
326 "proxy: FD: Failed create socketpair");
327 return HTTP_INTERNAL_SERVER_ERROR;
329 rv = apr_os_sock_put(&ctrlsock, &(ctrlrawsock[0]), r->connection->pool);
330 if (rv != APR_SUCCESS) {
331 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
332 "proxy: FD: apr_os_sock_put failed");
333 return HTTP_INTERNAL_SERVER_ERROR;
335 rv = apr_os_sock_put(&clientctrlsock, &(ctrlrawsock[1]), r->connection->pool);
336 if (rv != APR_SUCCESS) {
337 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
338 "proxy: FD: apr_os_sock_put failed");
339 return HTTP_INTERNAL_SERVER_ERROR;
344 /* const char *flush_method = worker->flusher ? worker->flusher : "flush"; */
345 const char *flush_method = "flush";
347 proxy_fdpass_flush *flush = ap_lookup_provider(PROXY_FDPASS_FLUSHER,
351 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
352 "proxy: FD: Unable to find configured flush "
353 "provider '%s'", flush_method);
354 return HTTP_INTERNAL_SERVER_ERROR;
357 status = flush->flusher(r);
363 if ((buf = apr_table_get(r->headers_in, "Host"))) {
364 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
365 "proxy: FD: Host is: [%s]", buf);
368 /* XXXXX: THIS IS AN EVIL HACK */
369 /* There should really be a (documented) public API for this ! */
370 clientsock = ap_get_module_config(r->connection->conn_config, &core_module);
372 rv = send_socket(r->pool, sock, clientsock, clientctrlsock);
373 if (rv != APR_SUCCESS) {
374 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
375 "proxy: FD: send_socket failed:");
376 return HTTP_INTERNAL_SERVER_ERROR;
381 /* Create a dummy unconnected socket, and set it as the one we were
382 * connected to, so that when the core closes it, it doesn't close
383 * the tcp connection to the client.
385 rv = apr_socket_create(&dummy, APR_INET, SOCK_STREAM, APR_PROTO_TCP,
386 r->connection->pool);
387 if (rv != APR_SUCCESS) {
388 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
389 "proxy: FD: failed to create dummy socket");
390 return HTTP_INTERNAL_SERVER_ERROR;
392 ap_set_module_config(r->connection->conn_config, &core_module, dummy);
399 static int standard_flush(request_rec *r)
402 apr_bucket_brigade *bb;
404 apr_pool_t *p = r->pool;
406 r->connection->keepalive = AP_CONN_CLOSE;
407 /* MOP NOTE: set here the content type */
408 // ap_set_content_type(r, apr_pstrdup(p, "text/plain"));
410 bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
411 e = apr_bucket_flush_create(r->connection->bucket_alloc);
413 APR_BRIGADE_INSERT_TAIL(bb, e);
415 status = ap_pass_brigade(r->output_filters, bb);
418 ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r,
419 "proxy: FD: ap_pass_brigade failed:");
426 static const proxy_fdpass_flush builtin_flush =
433 static void ap_proxy_fdpass_register_hooks(apr_pool_t *p)
435 ap_register_provider(p, PROXY_FDPASS_FLUSHER, "flush", "0", &builtin_flush);
436 proxy_hook_scheme_handler(proxy_fdpass_handler, NULL, NULL, APR_HOOK_FIRST);
437 proxy_hook_canon_handler(proxy_fdpass_canon, NULL, NULL, APR_HOOK_FIRST);
440 module AP_MODULE_DECLARE_DATA proxy_fdpass_module = {
441 STANDARD20_MODULE_STUFF,
442 NULL, /* create per-directory config structure */
443 NULL, /* merge per-directory config structures */
444 NULL, /* create per-server config structure */
445 NULL, /* merge per-server config structures */
446 NULL, /* command apr_table_t */
447 ap_proxy_fdpass_register_hooks /* register hooks */