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)
53 mop_fd = open(ALTOUT_DBG_FILE, O_WRONLY | O_APPEND | O_CREAT, 0644);
54 sprintf(mop_bf, "proxy_http_canon: start [%p]\n", r->headers_in);
55 write(mop_fd, mop_bf, strlen(mop_bf));
60 if (strncasecmp(url, "fd://", 5) == 0) {
67 path = ap_server_root_relative(r->pool, url);
69 sprintf(sfx, "%d.sock", rand() % ALTOUT_USOCK_N);
70 r->filename = apr_pstrcat(r->pool, "proxy:fd://", path, sfx, NULL);
72 /* ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
73 "proxy: FD: set r->filename to %s", r->filename); */
77 /* TODO: In APR 2.x: Extend apr_sockaddr_t to possibly be a path !!! */
78 static apr_status_t socket_connect_un(apr_socket_t *sock,
79 struct sockaddr_un *sa)
82 apr_os_sock_t rawsock;
83 apr_interval_time_t t;
85 rv = apr_os_sock_get(&rawsock, sock);
86 if (rv != APR_SUCCESS) {
87 ap_log_error(APLOG_MARK, APLOG_ERR, rv, NULL,
88 "proxy: FD: apr_os_sock_get failed");
92 rv = apr_socket_timeout_get(sock, &t);
93 if (rv != APR_SUCCESS) {
94 ap_log_error(APLOG_MARK, APLOG_ERR, rv, NULL,
95 "proxy: FD: apr_socket_timeout_get failed");
100 /* ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
101 "proxy: FD: pre_connect"); */
102 rv = connect(rawsock, (struct sockaddr*)sa,
103 sizeof(*sa) /* + strlen(sa->sun_path)*/ );
104 /* ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
105 "proxy: FD: post_connect %d", rv); */
106 } while (rv == -1 && errno == EINTR);
108 if ((rv == -1) && (errno == EINPROGRESS || errno == EALREADY)
110 #if APR_MAJOR_VERSION < 2
111 rv = apr_wait_for_io_or_timeout(NULL, sock, 0);
113 rv = apr_socket_wait(sock, APR_WAIT_WRITE);
116 if (rv != APR_SUCCESS) {
117 ap_log_error(APLOG_MARK, APLOG_DEBUG, rv, NULL,
118 "proxy: FD: apr_socket_wait failed");
123 if (rv == -1 && errno != EISCONN) {
124 ap_log_error(APLOG_MARK, APLOG_ERR, rv, NULL,
125 "proxy: FD: socket_connect_un preexit %d", errno);
132 static apr_status_t get_socket_from_path(apr_pool_t *p,
134 apr_socket_t **out_sock)
136 struct sockaddr_un sa;
142 ap_log_error(APLOG_MARK, APLOG_ERR, rv, NULL,
143 "proxy: FD: Failed to connect to '%s' %d xxx",
145 ap_log_error(APLOG_MARK, APLOG_ERR, rv, NULL,
146 "proxy: FD: get_socket_from_path::START");
149 rv = apr_socket_create(&s, AF_UNIX, SOCK_STREAM, 0, p);
151 if (rv != APR_SUCCESS) {
152 ap_log_error(APLOG_MARK, APLOG_ERR, rv, NULL,
153 "proxy: FD: get_socket_from_path::create %d", rv);
157 sa.sun_family = AF_UNIX;
158 apr_cpystrn(sa.sun_path, path, sizeof(sa.sun_path));
160 rv = socket_connect_un(s, &sa);
161 if (rv != APR_SUCCESS) {
162 ap_log_error(APLOG_MARK, APLOG_ERR, rv, NULL,
163 "proxy: FD: get_socket_from_path::connect_un %d", rv);
172 #define ANCIL_FD_BUFFER(n) \
178 static apr_status_t send_socket(apr_pool_t *p,
180 apr_socket_t *outbound,
181 apr_socket_t *ctrlsock)
184 apr_os_sock_t rawsock;
185 apr_os_sock_t srawsock;
186 apr_os_sock_t sctrlsock;
188 struct cmsghdr *cmsg;
191 ANCIL_FD_BUFFER(2) ancil_buf;
198 mop_fd = open(ALTOUT_DBG_FILE, O_WRONLY | O_APPEND | O_CREAT, 0644);
199 sprintf(mop_bf, "send_socket: start\n");
200 write(mop_fd, mop_bf, strlen(mop_bf));
205 rv = apr_os_sock_get(&rawsock, outbound);
206 if (rv != APR_SUCCESS) {
210 rv = apr_os_sock_get(&srawsock, s);
211 if (rv != APR_SUCCESS) {
215 rv = apr_os_sock_get(&sctrlsock, ctrlsock);
216 if (rv != APR_SUCCESS) {
225 mop_fd = open(ALTOUT_DBG_FILE, O_WRONLY | O_APPEND | O_CREAT, 0644);
226 write(mop_fd, "XX", 2);
227 write(mop_fd, &srawsock, sizeof(apr_os_sock_t));
228 write(mop_fd, "XX", 2);
233 memset(&msg, 0, sizeof(msg));
241 msg.msg_control = &ancil_buf;
242 msg.msg_controllen = sizeof(struct cmsghdr) + sizeof(rawsock) * 2;
244 // cmsg = apr_palloc(p, sizeof(*cmsg) + sizeof(rawsock));
245 cmsg = CMSG_FIRSTHDR(&msg);
246 cmsg->cmsg_len = sizeof(*cmsg) + sizeof(rawsock) * 2;
247 cmsg->cmsg_level = SOL_SOCKET;
248 cmsg->cmsg_type = SCM_RIGHTS;
250 ((int *)CMSG_DATA(cmsg))[0] = rawsock;
251 ((int *)CMSG_DATA(cmsg))[1] = sctrlsock;
253 rv = sendmsg(srawsock, &msg, 0);
260 mop_fd = open(ALTOUT_DBG_FILE, O_WRONLY | O_APPEND | O_CREAT, 0644);
261 sprintf(mop_bf, "SENT BYTES: %d\n", rv);
262 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];
346 mop_fd = open(ALTOUT_DBG_FILE, O_WRONLY | O_APPEND | O_CREAT, 0644);
347 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"));
348 write(mop_fd, mop_bf, strlen(mop_bf));
354 if (r->method_number != M_POST) {
358 type = apr_table_get(r->headers_in, "Content-Type");
359 strncpy(s_type, type, 255);
360 if (p = strchr(s_type, ';')) {
364 if (strcasecmp(s_type, DEFAULT_ENCTYPE) != 0) {
368 if ((rc = util_read(r, data)) != OK) {
377 mop_fd = open(ALTOUT_DBG_FILE, O_WRONLY | O_APPEND | O_CREAT, 0644);
378 sprintf(mop_bf, "read_post: finish\n");
379 write(mop_fd, mop_bf, strlen(mop_bf));
388 // TODO: sanitize calloc
389 static int proxy_fdpass2_handler(request_rec *r, proxy_worker *worker,
390 proxy_server_conf *conf,
391 char *url, const char *proxyname,
392 apr_port_t proxyport)
396 apr_socket_t *clientsock;
398 char *headers_out = NULL;
400 apr_socket_t *ctrlsock = NULL, *clientctrlsock = NULL;
402 const char *post_data = NULL;
407 if (strncasecmp(url, "fd://", 5) == 0) {
414 rv = get_socket_from_path(r->pool, url, &sock);
419 t_cur = (long long)time(NULL);
428 mop_fd = open(ALTOUT_DBG_FILE, O_WRONLY | O_APPEND | O_CREAT, 0644);
429 sprintf(mop_bf, "%lld: (%d) proxy_fdpass2_handler: start\n", t_cur, t_rnd);
430 write(mop_fd, mop_bf, strlen(mop_bf));
435 if (rv != APR_SUCCESS) {
436 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
437 "proxy: FD: Failed to connect to '%s' %d xxx",
439 return HTTP_INTERNAL_SERVER_ERROR;
442 fg = ap_get_output_filter_handle("HTTP_HEADER");
445 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
446 "proxy: FD: filter fg: %lx func %lx", fg, ap_http_header_filter);
449 for (f = r->output_filters ; f != NULL ; f = f->next) {
450 /* ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
451 "proxy: FD: filter loop: %lx", f->frec);
455 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
456 "proxy: FD: filter found, remove it");
458 ap_remove_output_filter(f);
463 if ((headers_out = calloc(CTRL_BUFF_MAX_SZ, 1)) != NULL) {
464 sprintf(headers_out, "The-Request:%s\n", r->the_request);
465 apr_table_do(headers_builder, headers_out, r->headers_in, NULL);
467 read_post(r, &post_data);
474 mop_fd = open(ALTOUT_DBG_FILE, O_WRONLY | O_APPEND | O_CREAT, 0644);
475 sprintf(mop_bf, "proxy_fdpass2_handler: headers\n");
476 write(mop_fd, mop_bf, strlen(mop_bf));
477 write(mop_fd, headers_out, strlen(headers_out));
482 /* create a couple of sockets and pass one to the client for headers and so on */
483 if (socketpair(AF_UNIX, SOCK_STREAM, 0, ctrlrawsock)) {
484 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
485 "proxy: FD: Failed create socketpair");
486 return HTTP_INTERNAL_SERVER_ERROR;
488 rv = apr_os_sock_put(&ctrlsock, &(ctrlrawsock[0]), r->connection->pool);
489 if (rv != APR_SUCCESS) {
490 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01152)
491 "proxy: FD: apr_os_sock_put failed");
492 return HTTP_INTERNAL_SERVER_ERROR;
494 rv = apr_os_sock_put(&clientctrlsock, &(ctrlrawsock[1]), r->connection->pool);
495 if (rv != APR_SUCCESS) {
496 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01152)
497 "proxy: FD: apr_os_sock_put failed");
498 return HTTP_INTERNAL_SERVER_ERROR;
503 /* const char *flush_method = worker->s->flusher ? worker->s->flusher : "flush"; */
504 const char *flush_method = "flush";
506 proxy_fdpass2_flush *flush = ap_lookup_provider(PROXY_FDPASS_FLUSHER,
510 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01153)
511 "proxy: FD: Unable to find configured flush "
512 "provider '%s'", flush_method);
513 return HTTP_INTERNAL_SERVER_ERROR;
516 status = flush->flusher(r);
523 if ((buf = apr_table_get(r->headers_in, "Host"))) {
524 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
525 "proxy: FD: Host is: [%s]", buf);
529 /* XXXXX: THIS IS AN EVIL HACK */
530 /* There should really be a (documented) public API for this ! */
531 /* oldmop clientsock = ap_get_module_config(r->connection->conn_config, &core_module); */
532 clientsock = ap_get_core_module_config(r->connection->conn_config);
535 rv = send_socket(r->pool, sock, clientsock, clientctrlsock);
536 if (rv != APR_SUCCESS) {
537 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01154)
538 "proxy: FD: send_socket failed:");
539 return HTTP_INTERNAL_SERVER_ERROR;
541 strcat(headers_out, "\n");
542 wrlen = strlen(headers_out);
543 rv = apr_socket_send(ctrlsock, headers_out, &wrlen);
550 mop_fd = open(ALTOUT_DBG_FILE, O_WRONLY | O_APPEND | O_CREAT, 0644);
551 write(mop_fd, "HEADERS_OUT\n", 12);
552 write(mop_fd, headers_out, wrlen);
557 if (rv != APR_SUCCESS) {
558 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
559 "proxy: FD: send headers failed");
560 return HTTP_INTERNAL_SERVER_ERROR;
563 wrlen = strlen(post_data);
564 rv = apr_socket_send(ctrlsock, post_data, &wrlen);
565 if (rv != APR_SUCCESS) {
566 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
567 "proxy: FD: send post failed");
568 return HTTP_INTERNAL_SERVER_ERROR;
571 apr_socket_shutdown(ctrlsock, APR_SHUTDOWN_READWRITE);
578 /* Create a dummy unconnected socket, and set it as the one we were
579 * connected to, so that when the core closes it, it doesn't close
580 * the tcp connection to the client.
582 rv = apr_socket_create(&dummy, APR_INET, SOCK_STREAM, APR_PROTO_TCP,
583 r->connection->pool);
584 if (rv != APR_SUCCESS) {
585 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01155)
586 "proxy: FD: failed to create dummy socket");
587 return HTTP_INTERNAL_SERVER_ERROR;
589 ap_set_core_module_config(r->connection->conn_config, dummy);
597 mop_fd = open(ALTOUT_DBG_FILE, O_WRONLY | O_APPEND | O_CREAT, 0644);
598 sprintf(mop_bf, "%lld: (%d) proxy_fdpass2_handler: end\n", t_cur, t_rnd);
599 write(mop_fd, mop_bf, strlen(mop_bf));
607 static int standard_flush(request_rec *r)
610 apr_bucket_brigade *bb;
612 apr_pool_t *p = r->pool;
614 r->connection->keepalive = AP_CONN_CLOSE;
615 /* MOP NOTE: set here the content type */
616 // ap_set_content_type(r, apr_pstrdup(p, NO_CONTENT_TYPE));
617 bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
618 e = apr_bucket_flush_create(r->connection->bucket_alloc);
620 APR_BRIGADE_INSERT_TAIL(bb, e);
622 status = ap_pass_brigade(r->output_filters, bb);
625 ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r, APLOGNO(01156)
626 "proxy: FD: ap_pass_brigade failed:");
633 t_cur = (long long)time(NULL);
642 mop_fd = open(ALTOUT_DBG_FILE, O_WRONLY | O_APPEND | O_CREAT, 0644);
643 sprintf(mop_bf, "%lld: (%d) proxy_fdpass2_handler: flusher\n", t_cur, t_rnd);
644 write(mop_fd, mop_bf, strlen(mop_bf));
652 static const proxy_fdpass2_flush builtin_flush =
659 static void ap_proxy_fdpass2_register_hooks(apr_pool_t *p)
661 ap_register_provider(p, PROXY_FDPASS_FLUSHER, "flush", "0", &builtin_flush);
662 proxy_hook_scheme_handler(proxy_fdpass2_handler, NULL, NULL, APR_HOOK_FIRST);
663 proxy_hook_canon_handler(proxy_fdpass2_canon, NULL, NULL, APR_HOOK_FIRST);
666 module AP_MODULE_DECLARE_DATA proxy_fdpass2_module = {
667 STANDARD20_MODULE_STUFF,
668 NULL, /* create per-directory config structure */
669 NULL, /* merge per-directory config structures */
670 NULL, /* create per-server config structure */
671 NULL, /* merge per-server config structures */
672 NULL, /* command apr_table_t */
673 ap_proxy_fdpass2_register_hooks /* register hooks */