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>
26 #error This module only works on unix platforms with the correct OS support
29 #include "apr_version.h"
30 #if APR_MAJOR_VERSION < 2
31 /* for apr_wait_for_io_or_timeout */
32 #include "apr_support.h"
35 #include "mod_proxy_fdpass2.h"
37 module AP_MODULE_DECLARE_DATA proxy_fdpass2_module;
39 #define ALTOUT_USOCK_N 10
40 #define ALTOUT_DEBUG 1
41 #define ALTOUT_DBG_FILE "/home/log/fdpass2.log"
43 static int proxy_fdpass2_canon(request_rec *r, char *url)
45 const char *path, sfx[16];
52 mop_fd = open(ALTOUT_DBG_FILE, O_WRONLY | O_APPEND | O_CREAT, 0644);
53 sprintf(mop_bf, "proxy_http_canon: start [%p]\n", r->headers_in);
54 write(mop_fd, mop_bf, strlen(mop_bf));
59 if (strncasecmp(url, "fd://", 5) == 0) {
66 path = ap_server_root_relative(r->pool, url);
68 sprintf(sfx, "%d.sock", rand() % ALTOUT_USOCK_N);
69 r->filename = apr_pstrcat(r->pool, "proxy:fd://", path, sfx, NULL);
71 /* ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
72 "proxy: FD: set r->filename to %s", r->filename); */
76 /* TODO: In APR 2.x: Extend apr_sockaddr_t to possibly be a path !!! */
77 static apr_status_t socket_connect_un(request_rec *r, apr_socket_t *sock,
78 struct sockaddr_un *sa)
81 apr_os_sock_t rawsock;
82 apr_interval_time_t t;
84 rv = apr_os_sock_get(&rawsock, sock);
85 if (rv != APR_SUCCESS) {
86 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
87 "proxy: FD: apr_os_sock_get failed");
91 rv = apr_socket_timeout_get(sock, &t);
92 if (rv != APR_SUCCESS) {
93 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
94 "proxy: FD: apr_socket_timeout_get failed");
99 /* ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
100 "proxy: FD: pre_connect"); */
101 rv = connect(rawsock, (struct sockaddr*)sa,
102 sizeof(*sa) /* + strlen(sa->sun_path)*/ );
103 /* ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
104 "proxy: FD: post_connect %d", rv); */
105 } while (rv == -1 && errno == EINTR);
107 if ((rv == -1) && (errno == EINPROGRESS || errno == EALREADY)
109 #if APR_MAJOR_VERSION < 2
110 rv = apr_wait_for_io_or_timeout(NULL, sock, 0);
112 rv = apr_socket_wait(sock, APR_WAIT_WRITE);
115 if (rv != APR_SUCCESS) {
116 ap_log_error(APLOG_MARK, APLOG_DEBUG, rv, NULL,
117 "proxy: FD: apr_socket_wait failed");
122 if (rv == -1 && errno != EISCONN) {
123 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
124 "proxy: FD: socket_connect_un preexit %d", errno);
131 static apr_status_t get_socket_from_path(request_rec *r, apr_pool_t *p,
133 apr_socket_t **out_sock)
135 struct sockaddr_un sa;
141 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
142 "proxy: FD: Failed to connect to '%s' %d xxx",
144 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
145 "proxy: FD: get_socket_from_path::START");
148 rv = apr_socket_create(&s, AF_UNIX, SOCK_STREAM, 0, p);
150 if (rv != APR_SUCCESS) {
151 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
152 "proxy: FD: get_socket_from_path::create %d", rv);
156 sa.sun_family = AF_UNIX;
157 apr_cpystrn(sa.sun_path, path, sizeof(sa.sun_path));
159 rv = socket_connect_un(r, s, &sa);
160 if (rv != APR_SUCCESS) {
161 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
162 "proxy: FD: get_socket_from_path::connect_un %d", rv);
171 #define ANCIL_FD_BUFFER(n) \
177 static apr_status_t send_socket(apr_pool_t *p,
179 apr_socket_t *outbound,
180 apr_socket_t *ctrlsock)
183 apr_os_sock_t rawsock;
184 apr_os_sock_t srawsock;
185 apr_os_sock_t sctrlsock;
187 struct cmsghdr *cmsg;
190 ANCIL_FD_BUFFER(2) ancil_buf;
197 mop_fd = open(ALTOUT_DBG_FILE, O_WRONLY | O_APPEND | O_CREAT, 0644);
198 sprintf(mop_bf, "send_socket: start\n");
199 write(mop_fd, mop_bf, strlen(mop_bf));
204 rv = apr_os_sock_get(&rawsock, outbound);
205 if (rv != APR_SUCCESS) {
209 rv = apr_os_sock_get(&srawsock, s);
210 if (rv != APR_SUCCESS) {
214 rv = apr_os_sock_get(&sctrlsock, ctrlsock);
215 if (rv != APR_SUCCESS) {
224 mop_fd = open(ALTOUT_DBG_FILE, O_WRONLY | O_APPEND | O_CREAT, 0644);
225 write(mop_fd, "XX", 2);
226 write(mop_fd, &srawsock, sizeof(apr_os_sock_t));
227 write(mop_fd, "XX", 2);
232 memset(&msg, 0, sizeof(msg));
240 msg.msg_control = &ancil_buf;
241 msg.msg_controllen = sizeof(struct cmsghdr) + sizeof(rawsock) * 2;
243 // cmsg = apr_palloc(p, sizeof(*cmsg) + sizeof(rawsock));
244 cmsg = CMSG_FIRSTHDR(&msg);
245 cmsg->cmsg_len = sizeof(*cmsg) + sizeof(rawsock) * 2;
246 cmsg->cmsg_level = SOL_SOCKET;
247 cmsg->cmsg_type = SCM_RIGHTS;
249 ((int *)CMSG_DATA(cmsg))[0] = rawsock;
250 ((int *)CMSG_DATA(cmsg))[1] = sctrlsock;
252 rv = sendmsg(srawsock, &msg, 0);
259 mop_fd = open(ALTOUT_DBG_FILE, O_WRONLY | O_APPEND | O_CREAT, 0644);
260 sprintf(mop_bf, "SENT BYTES: %d\n", rv);
261 write(mop_fd, mop_bf, strlen(mop_bf));
274 static int headers_builder(void *rec, const char *key, const char *value)
285 mop_fd = open(ALTOUT_DBG_FILE, O_WRONLY | O_APPEND | O_CREAT, 0644);
286 sprintf(mop_bf, "HEADERS_BUILDER: [%s:%s]\n", key, value);
287 write(mop_fd, mop_bf, strlen(mop_bf));
292 // TODO: verify length
293 // sprintf(s, "%s%s:%s\n", s, key, value);
300 #define CTRL_BUFF_MAX_SZ (8*1024)
302 #define DEFAULT_ENCTYPE "application/x-www-form-urlencoded"
304 int util_read(request_rec *r, const char **rbuf)
308 if ((rc = ap_setup_client_block(r, REQUEST_CHUNKED_ERROR)) != OK) {
312 if (ap_should_client_block(r)) {
313 char argsbuffer[HUGE_STRING_LEN];
314 int rsize, len_read, rpos=0;
315 long length = r->remaining;
316 *rbuf = (char *)apr_pcalloc(r->pool, length +1);
317 if ((len_read = ap_get_client_block(r, argsbuffer,
318 sizeof(argsbuffer))) > 0) {
319 if ((rpos + len_read) > length) {
320 rsize = length - rpos;
325 memcpy((char *)*rbuf + rpos, argsbuffer, rsize);
334 int read_post(request_rec *r, const char **data)
337 char *p, s_type[256];
352 static int headers_builder(void *rec, const char *key, const char *value)
363 mop_fd = open(ALTOUT_DBG_FILE, O_WRONLY | O_APPEND | O_CREAT, 0644);
364 sprintf(mop_bf, "HEADERS_BUILDER: [%s:%s]\n", key, value);
365 write(mop_fd, mop_bf, strlen(mop_bf));
370 // TODO: verify length
371 // sprintf(s, "%s%s:%s\n", s, key, value);
378 #define CTRL_BUFF_MAX_SZ (8*1024)
380 #define DEFAULT_ENCTYPE "application/x-www-form-urlencoded"
382 int util_read(request_rec *r, const char **rbuf)
386 if ((rc = ap_setup_client_block(r, REQUEST_CHUNKED_ERROR)) != OK) {
390 if (ap_should_client_block(r)) {
391 char argsbuffer[HUGE_STRING_LEN];
392 int rsize, len_read, rpos=0;
393 long length = r->remaining;
394 *rbuf = (char *)apr_pcalloc(r->pool, length +1);
395 if ((len_read = ap_get_client_block(r, argsbuffer,
396 sizeof(argsbuffer))) > 0) {
397 if ((rpos + len_read) > length) {
398 rsize = length - rpos;
403 memcpy((char *)*rbuf + rpos, argsbuffer, rsize);
412 int read_post(request_rec *r, const char **data)
415 char *p, s_type[256];
429 static int headers_builder(void *rec, const char *key, const char *value)
440 mop_fd = open(ALTOUT_DBG_FILE, O_WRONLY | O_APPEND | O_CREAT, 0644);
441 sprintf(mop_bf, "HEADERS_BUILDER: [%s:%s]\n", key, value);
442 write(mop_fd, mop_bf, strlen(mop_bf));
447 // TODO: verify length
448 // sprintf(s, "%s%s:%s\n", s, key, value);
455 #define CTRL_BUFF_MAX_SZ (8*1024)
457 #define DEFAULT_ENCTYPE "application/x-www-form-urlencoded"
459 int util_read(request_rec *r, const char **rbuf)
463 if ((rc = ap_setup_client_block(r, REQUEST_CHUNKED_ERROR)) != OK) {
467 if (ap_should_client_block(r)) {
468 char argsbuffer[HUGE_STRING_LEN];
469 int rsize, len_read, rpos=0;
470 long length = r->remaining;
471 *rbuf = (char *)apr_pcalloc(r->pool, length +1);
472 if ((len_read = ap_get_client_block(r, argsbuffer,
473 sizeof(argsbuffer))) > 0) {
474 if ((rpos + len_read) > length) {
475 rsize = length - rpos;
480 memcpy((char *)*rbuf + rpos, argsbuffer, rsize);
489 int read_post(request_rec *r, const char **data)
492 char *p, s_type[256];
506 static int headers_builder(void *rec, const char *key, const char *value)
517 mop_fd = open(ALTOUT_DBG_FILE, O_WRONLY | O_APPEND | O_CREAT, 0644);
518 sprintf(mop_bf, "HEADERS_BUILDER: [%s:%s]\n", key, value);
519 write(mop_fd, mop_bf, strlen(mop_bf));
524 // TODO: verify length
525 // sprintf(s, "%s%s:%s\n", s, key, value);
532 #define CTRL_BUFF_MAX_SZ (8*1024)
534 #define DEFAULT_ENCTYPE "application/x-www-form-urlencoded"
536 int util_read(request_rec *r, const char **rbuf)
540 if ((rc = ap_setup_client_block(r, REQUEST_CHUNKED_ERROR)) != OK) {
544 if (ap_should_client_block(r)) {
545 char argsbuffer[HUGE_STRING_LEN];
546 int rsize, len_read, rpos=0;
547 long length = r->remaining;
548 *rbuf = (char *)apr_pcalloc(r->pool, length +1);
549 if ((len_read = ap_get_client_block(r, argsbuffer,
550 sizeof(argsbuffer))) > 0) {
551 if ((rpos + len_read) > length) {
552 rsize = length - rpos;
557 memcpy((char *)*rbuf + rpos, argsbuffer, rsize);
566 int read_post(request_rec *r, const char **data)
569 char *p, s_type[256];
578 mop_fd = open(ALTOUT_DBG_FILE, O_WRONLY | O_APPEND | O_CREAT, 0644);
579 sprintf(mop_bf, "read_post: start: numb: %d %d head_in: [%s]\n", r->method_number, M_POST, apr_table_get(r->headers_in, "Content-Type"));
580 write(mop_fd, mop_bf, strlen(mop_bf));
586 if (r->method_number != M_POST) {
590 type = apr_table_get(r->headers_in, "Content-Type");
591 strncpy(s_type, type, 255);
592 if (p = strchr(s_type, ';')) {
596 if (strcasecmp(s_type, DEFAULT_ENCTYPE) != 0) {
600 if ((rc = util_read(r, data)) != OK) {
609 mop_fd = open(ALTOUT_DBG_FILE, O_WRONLY | O_APPEND | O_CREAT, 0644);
610 sprintf(mop_bf, "read_post: finish\n");
611 write(mop_fd, mop_bf, strlen(mop_bf));
620 // TODO: sanitize calloc
621 static int proxy_fdpass2_handler(request_rec *r, proxy_worker *worker,
622 proxy_server_conf *conf,
623 char *url, const char *proxyname,
624 apr_port_t proxyport)
628 apr_socket_t *clientsock;
630 char *headers_out = NULL;
632 apr_socket_t *ctrlsock = NULL, *clientctrlsock = NULL;
634 const char *post_data = NULL;
639 if (strncasecmp(url, "fd://", 5) == 0) {
646 rv = get_socket_from_path(r, r->pool, url, &sock);
660 mop_fd = open(ALTOUT_DBG_FILE, O_WRONLY | O_APPEND | O_CREAT, 0644);
661 sprintf(mop_bf, "%d: (%d) proxy_fdpass2_handler: start\n", t_cur, t_rnd);
662 write(mop_fd, mop_bf, strlen(mop_bf));
667 if (rv != APR_SUCCESS) {
668 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
669 "proxy: FD: Failed to connect to '%s' %d xxx",
671 return HTTP_INTERNAL_SERVER_ERROR;
674 fg = ap_get_output_filter_handle("HTTP_HEADER");
677 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
678 "proxy: FD: filter fg: %lx func %lx", fg, ap_http_header_filter);
681 for (f = r->output_filters ; f != NULL ; f = f->next) {
682 /* ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
683 "proxy: FD: filter loop: %lx", f->frec);
687 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
688 "proxy: FD: filter found, remove it");
690 ap_remove_output_filter(f);
695 if ((headers_out = calloc(CTRL_BUFF_MAX_SZ, 1)) != NULL) {
696 sprintf(headers_out, "The-Request:%s\n", r->the_request);
697 apr_table_do(headers_builder, headers_out, r->headers_in, NULL);
699 read_post(r, &post_data);
706 mop_fd = open(ALTOUT_DBG_FILE, O_WRONLY | O_APPEND | O_CREAT, 0644);
707 sprintf(mop_bf, "proxy_fdpass2_handler: headers\n");
708 write(mop_fd, mop_bf, strlen(mop_bf));
709 write(mop_fd, headers_out, strlen(headers_out));
714 /* create a couple of sockets and pass one to the client for headers and so on */
715 if (socketpair(AF_UNIX, SOCK_STREAM, 0, ctrlrawsock)) {
716 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
717 "proxy: FD: Failed create socketpair");
718 return HTTP_INTERNAL_SERVER_ERROR;
720 rv = apr_os_sock_put(&ctrlsock, &(ctrlrawsock[0]), r->connection->pool);
721 if (rv != APR_SUCCESS) {
722 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
723 "proxy: FD: apr_os_sock_put failed");
724 return HTTP_INTERNAL_SERVER_ERROR;
726 rv = apr_os_sock_put(&clientctrlsock, &(ctrlrawsock[1]), r->connection->pool);
727 if (rv != APR_SUCCESS) {
728 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
729 "proxy: FD: apr_os_sock_put failed");
730 return HTTP_INTERNAL_SERVER_ERROR;
735 /* const char *flush_method = worker->flusher ? worker->flusher : "flush"; */
736 const char *flush_method = "flush";
738 proxy_fdpass2_flush *flush = ap_lookup_provider(PROXY_FDPASS_FLUSHER,
742 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
743 "proxy: FD: Unable to find configured flush "
744 "provider '%s'", flush_method);
745 return HTTP_INTERNAL_SERVER_ERROR;
748 status = flush->flusher(r);
755 if ((buf = apr_table_get(r->headers_in, "Host"))) {
756 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
757 "proxy: FD: Host is: [%s]", buf);
761 /* XXXXX: THIS IS AN EVIL HACK */
762 /* There should really be a (documented) public API for this ! */
763 clientsock = ap_get_module_config(r->connection->conn_config, &core_module);
765 rv = send_socket(r->pool, sock, clientsock, clientctrlsock);
766 if (rv != APR_SUCCESS) {
767 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
768 "proxy: FD: send_socket failed:");
769 return HTTP_INTERNAL_SERVER_ERROR;
771 strcat(headers_out, "\n");
772 wrlen = strlen(headers_out);
773 rv = apr_socket_send(ctrlsock, headers_out, &wrlen);
780 mop_fd = open(ALTOUT_DBG_FILE, O_WRONLY | O_APPEND | O_CREAT, 0644);
781 write(mop_fd, "HEADERS_OUT\n", 12);
782 write(mop_fd, headers_out, wrlen);
787 if (rv != APR_SUCCESS) {
788 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
789 "proxy: FD: send headers failed");
790 return HTTP_INTERNAL_SERVER_ERROR;
793 wrlen = strlen(post_data);
794 rv = apr_socket_send(ctrlsock, post_data, &wrlen);
795 if (rv != APR_SUCCESS) {
796 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
797 "proxy: FD: send post failed");
798 return HTTP_INTERNAL_SERVER_ERROR;
801 apr_socket_shutdown(ctrlsock, APR_SHUTDOWN_READWRITE);
807 /* Create a dummy unconnected socket, and set it as the one we were
808 * connected to, so that when the core closes it, it doesn't close
809 * the tcp connection to the client.
811 rv = apr_socket_create(&dummy, APR_INET, SOCK_STREAM, APR_PROTO_TCP,
812 r->connection->pool);
813 if (rv != APR_SUCCESS) {
814 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
815 "proxy: FD: failed to create dummy socket");
816 return HTTP_INTERNAL_SERVER_ERROR;
818 ap_set_module_config(r->connection->conn_config, &core_module, dummy);
826 mop_fd = open(ALTOUT_DBG_FILE, O_WRONLY | O_APPEND | O_CREAT, 0644);
827 sprintf(mop_bf, "%d: (%d) proxy_fdpass2_handler: end\n", t_cur, t_rnd);
828 write(mop_fd, mop_bf, strlen(mop_bf));
836 static int standard_flush(request_rec *r)
839 apr_bucket_brigade *bb;
841 apr_pool_t *p = r->pool;
843 r->connection->keepalive = AP_CONN_CLOSE;
844 /* MOP NOTE: set here the content type */
845 // ap_set_content_type(r, apr_pstrdup(p, NO_CONTENT_TYPE));
846 bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
847 e = apr_bucket_flush_create(r->connection->bucket_alloc);
849 APR_BRIGADE_INSERT_TAIL(bb, e);
851 status = ap_pass_brigade(r->output_filters, bb);
854 ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r,
855 "proxy: FD: ap_pass_brigade failed:");
862 static const proxy_fdpass2_flush builtin_flush =
869 static void ap_proxy_fdpass2_register_hooks(apr_pool_t *p)
871 ap_register_provider(p, PROXY_FDPASS_FLUSHER, "flush", "0", &builtin_flush);
872 proxy_hook_scheme_handler(proxy_fdpass2_handler, NULL, NULL, APR_HOOK_FIRST);
873 proxy_hook_canon_handler(proxy_fdpass2_canon, NULL, NULL, APR_HOOK_FIRST);
876 module AP_MODULE_DECLARE_DATA proxy_fdpass2_module = {
877 STANDARD20_MODULE_STUFF,
878 NULL, /* create per-directory config structure */
879 NULL, /* merge per-directory config structures */
880 NULL, /* create per-server config structure */
881 NULL, /* merge per-server config structures */
882 NULL, /* command apr_table_t */
883 ap_proxy_fdpass2_register_hooks /* register hooks */