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)
41 if (strncasecmp(url, "fd://", 5) == 0) {
48 path = ap_server_root_relative(r->pool, url);
50 r->filename = apr_pstrcat(r->pool, "proxy:fd://", path, NULL);
52 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
53 "proxy: FD: set r->filename to %s", r->filename);
57 /* TODO: In APR 2.x: Extend apr_sockaddr_t to possibly be a path !!! */
58 static apr_status_t socket_connect_un(apr_socket_t *sock,
59 struct sockaddr_un *sa)
62 apr_os_sock_t rawsock;
63 apr_interval_time_t t;
65 rv = apr_os_sock_get(&rawsock, sock);
66 if (rv != APR_SUCCESS) {
70 rv = apr_socket_timeout_get(sock, &t);
71 if (rv != APR_SUCCESS) {
76 rv = connect(rawsock, (struct sockaddr*)sa,
77 sizeof(*sa) + strlen(sa->sun_path));
78 } while (rv == -1 && errno == EINTR);
80 if ((rv == -1) && (errno == EINPROGRESS || errno == EALREADY)
82 #if APR_MAJOR_VERSION < 2
83 rv = apr_wait_for_io_or_timeout(NULL, sock, 0);
85 rv = apr_socket_wait(sock, APR_WAIT_WRITE);
88 if (rv != APR_SUCCESS) {
93 if (rv == -1 && errno != EISCONN) {
100 static apr_status_t get_socket_from_path(apr_pool_t *p,
102 apr_socket_t **out_sock)
104 struct sockaddr_un sa;
109 rv = apr_socket_create(&s, AF_UNIX, SOCK_STREAM, 0, p);
111 if (rv != APR_SUCCESS) {
115 sa.sun_family = AF_UNIX;
116 apr_cpystrn(sa.sun_path, path, sizeof(sa.sun_path));
118 rv = socket_connect_un(s, &sa);
119 if (rv != APR_SUCCESS) {
129 static apr_status_t send_socket(apr_pool_t *p,
131 apr_socket_t *outbound)
134 apr_os_sock_t rawsock;
135 apr_os_sock_t srawsock;
137 struct cmsghdr *cmsg;
141 rv = apr_os_sock_get(&rawsock, outbound);
142 if (rv != APR_SUCCESS) {
146 rv = apr_os_sock_get(&srawsock, s);
147 if (rv != APR_SUCCESS) {
151 memset(&msg, 0, sizeof(msg));
159 cmsg = apr_palloc(p, sizeof(*cmsg) + sizeof(rawsock));
160 cmsg->cmsg_len = sizeof(*cmsg) + sizeof(rawsock);
161 cmsg->cmsg_level = SOL_SOCKET;
162 cmsg->cmsg_type = SCM_RIGHTS;
164 memcpy(CMSG_DATA(cmsg), &rawsock, sizeof(rawsock));
166 msg.msg_control = cmsg;
167 msg.msg_controllen = cmsg->cmsg_len;
169 rv = sendmsg(srawsock, &msg, 0);
179 static int proxy_fdpass_handler(request_rec *r, proxy_worker *worker,
180 proxy_server_conf *conf,
181 char *url, const char *proxyname,
182 apr_port_t proxyport)
186 apr_socket_t *clientsock;
188 if (strncasecmp(url, "fd://", 5) == 0) {
195 rv = get_socket_from_path(r->pool, url, &sock);
197 if (rv != APR_SUCCESS) {
198 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
199 "proxy: FD: Failed to connect to '%s'",
201 return HTTP_INTERNAL_SERVER_ERROR;
206 const char *flush_method = worker->s->flusher ? worker->s->flusher : "flush";
208 proxy_fdpass_flush *flush = ap_lookup_provider(PROXY_FDPASS_FLUSHER,
212 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
213 "proxy: FD: Unable to find configured flush "
214 "provider '%s'", flush_method);
215 return HTTP_INTERNAL_SERVER_ERROR;
218 status = flush->flusher(r);
224 clientsock = ap_get_conn_socket(r->connection);
226 rv = send_socket(r->pool, sock, clientsock);
227 if (rv != APR_SUCCESS) {
228 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
229 "proxy: FD: send_socket failed:");
230 return HTTP_INTERNAL_SERVER_ERROR;
235 /* Create a dummy unconnected socket, and set it as the one we were
236 * connected to, so that when the core closes it, it doesn't close
237 * the tcp connection to the client.
239 rv = apr_socket_create(&dummy, APR_INET, SOCK_STREAM, APR_PROTO_TCP,
240 r->connection->pool);
241 if (rv != APR_SUCCESS) {
242 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
243 "proxy: FD: failed to create dummy socket");
244 return HTTP_INTERNAL_SERVER_ERROR;
246 ap_set_core_module_config(r->connection->conn_config, dummy);
253 static int standard_flush(request_rec *r)
256 apr_bucket_brigade *bb;
259 r->connection->keepalive = AP_CONN_CLOSE;
261 bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
262 e = apr_bucket_flush_create(r->connection->bucket_alloc);
264 APR_BRIGADE_INSERT_TAIL(bb, e);
266 status = ap_pass_brigade(r->output_filters, bb);
269 ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r,
270 "proxy: FD: ap_pass_brigade failed:");
278 static const proxy_fdpass_flush builtin_flush =
285 static void register_hooks(apr_pool_t *p)
287 ap_register_provider(p, PROXY_FDPASS_FLUSHER, "flush", "0", &builtin_flush);
288 proxy_hook_scheme_handler(proxy_fdpass_handler, NULL, NULL, APR_HOOK_FIRST);
289 proxy_hook_canon_handler(proxy_fdpass_canon, NULL, NULL, APR_HOOK_FIRST);
292 AP_DECLARE_MODULE(proxy_fdpass) = {
293 STANDARD20_MODULE_STUFF,
294 NULL, /* create per-directory config structure */
295 NULL, /* merge per-directory config structures */
296 NULL, /* create per-server config structure */
297 NULL, /* merge per-server config structures */
298 NULL, /* command apr_table_t */
299 register_hooks /* register hooks */