139dd5ab8c359bb2a8fee1330ebf175853ec7c5a
[mod-proxy-fdpass.git] / mod_proxy_fdpass2.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 #include <time.h>
23 #include <stdlib.h>
24
25 #ifndef CMSG_DATA
26 #error This module only works on unix platforms with the correct OS support
27 #endif
28
29 #include "apr_version.h"
30 #if APR_MAJOR_VERSION < 2
31 /* for apr_wait_for_io_or_timeout */
32 #include "apr_support.h"
33 #endif
34
35 #include "mod_proxy_fdpass2.h"
36
37 module AP_MODULE_DECLARE_DATA proxy_fdpass2_module;
38
39 #define ALTOUT_USOCK_N 10
40 #define ALTOUT_DEBUG 1
41 #define ALTOUT_DBG_FILE "/home/log/fdpass2.log"
42
43 static int proxy_fdpass2_canon(request_rec *r, char *url)
44 {
45     const char *path, sfx[16];
46
47 #if ALTOUT_DEBUG > 1
48     {
49         int mop_fd;
50         char mop_bf[512];
51
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));
55         close(mop_fd);
56     }
57 #endif
58
59     if (strncasecmp(url, "fd://", 5) == 0) {
60         url += 5;
61     }
62     else {
63         return DECLINED;
64     }
65
66     path = ap_server_root_relative(r->pool, url);
67
68     sprintf(sfx, "%d.sock", rand() % ALTOUT_USOCK_N);
69     r->filename = apr_pstrcat(r->pool, "proxy:fd://", path, sfx, NULL);
70
71     /* ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
72        "proxy: FD: set r->filename to %s", r->filename); */
73     return OK;
74 }
75
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)
79 {
80     apr_status_t rv;
81     apr_os_sock_t rawsock;
82     apr_interval_time_t t;
83
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");
88         return rv;
89     }
90
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");
95         return rv;
96     }
97
98     do {
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);
106
107     if ((rv == -1) && (errno == EINPROGRESS || errno == EALREADY)
108         && (t > 0)) {
109 #if APR_MAJOR_VERSION < 2
110         rv = apr_wait_for_io_or_timeout(NULL, sock, 0);
111 #else
112         rv = apr_socket_wait(sock, APR_WAIT_WRITE);
113 #endif
114
115         if (rv != APR_SUCCESS) {
116             ap_log_error(APLOG_MARK, APLOG_DEBUG, rv, NULL,
117                          "proxy: FD: apr_socket_wait failed");
118             return rv;
119         }
120     }
121
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);
125         return errno;
126     }
127
128     return APR_SUCCESS;
129 }
130
131 static apr_status_t get_socket_from_path(request_rec *r, apr_pool_t *p,
132                                          const char* path,
133                                          apr_socket_t **out_sock)
134 {
135     struct sockaddr_un sa;
136     apr_socket_t *s;
137     apr_status_t rv;
138     *out_sock = NULL;
139
140     /*
141     ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
142                   "proxy: FD: Failed to connect to '%s' %d xxx",
143                       url, rv);
144     ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
145                  "proxy: FD: get_socket_from_path::START");
146     */
147
148     rv = apr_socket_create(&s, AF_UNIX, SOCK_STREAM, 0, p);
149
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);
153         return rv;
154     }
155
156     sa.sun_family = AF_UNIX;
157     apr_cpystrn(sa.sun_path, path, sizeof(sa.sun_path));
158
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);
163         return rv;
164     }
165
166     *out_sock = s;
167
168     return APR_SUCCESS;
169 }
170
171 #define ANCIL_FD_BUFFER(n) \
172     struct { \
173         struct cmsghdr h; \
174         int fd[n]; \
175     }
176
177 static apr_status_t send_socket(apr_pool_t *p,
178                                 apr_socket_t *s,
179                                 apr_socket_t *outbound,
180                                 apr_socket_t *ctrlsock)
181 {
182     apr_status_t rv;
183     apr_os_sock_t rawsock;
184     apr_os_sock_t srawsock;
185     apr_os_sock_t sctrlsock;
186     struct msghdr msg;
187     struct cmsghdr *cmsg;
188     struct iovec iov;
189     char b = '\0', *buf;
190     ANCIL_FD_BUFFER(2) ancil_buf;
191
192 #if ALTOUT_DEBUG > 1
193     {
194         int mop_fd;
195         char mop_bf[512];
196
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));
200         close(mop_fd);
201     }
202 #endif
203
204     rv = apr_os_sock_get(&rawsock, outbound);
205     if (rv != APR_SUCCESS) {
206         return rv;
207     }
208
209     rv = apr_os_sock_get(&srawsock, s);
210     if (rv != APR_SUCCESS) {
211         return rv;
212     }
213
214     rv = apr_os_sock_get(&sctrlsock, ctrlsock);
215     if (rv != APR_SUCCESS) {
216         return rv;
217     }
218
219 #if ALTOUT_DEBUG > 1
220     {
221         int mop_fd;
222         char mop_bf[512];
223
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);
228         close(mop_fd);
229     }
230 #endif
231
232     memset(&msg, 0, sizeof(msg));
233
234     msg.msg_iov = &iov;
235     msg.msg_iovlen = 1;
236
237     iov.iov_base = &b;
238     iov.iov_len = 1;
239
240     msg.msg_control = &ancil_buf;
241     msg.msg_controllen = sizeof(struct cmsghdr) + sizeof(rawsock) * 2;
242
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;
248
249     ((int *)CMSG_DATA(cmsg))[0] = rawsock;
250     ((int *)CMSG_DATA(cmsg))[1] = sctrlsock;
251
252     rv = sendmsg(srawsock, &msg, 0);
253
254 #if ALTOUT_DEBUG > 1
255     {
256         int mop_fd;
257         char mop_bf[512];
258
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));
262         close(mop_fd);
263     }
264 #endif
265
266     if (rv == -1) {
267         return errno;
268     }
269
270
271     return APR_SUCCESS;
272 }
273
274 static int headers_builder(void *rec, const char *key, const char *value)
275 {
276     char *s;
277
278     s = (char *)rec;
279
280 #if ALTOUT_DEBUG > 1
281     {
282         int mop_fd;
283         char mop_bf[512];
284
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));
288         close(mop_fd);
289     }
290 #endif
291
292     // TODO: verify length
293     // sprintf(s, "%s%s:%s\n", s, key, value);
294     strcat(s, key);
295     strcat(s, ":");
296     strcat(s, value);
297     strcat(s, "\n");
298 }
299
300 #define CTRL_BUFF_MAX_SZ (8*1024)
301
302 #define DEFAULT_ENCTYPE "application/x-www-form-urlencoded"
303
304 int util_read(request_rec *r, const char **rbuf)
305 {
306     int rc;
307
308     if ((rc = ap_setup_client_block(r, REQUEST_CHUNKED_ERROR)) != OK) {
309         return rc;
310     }
311
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;
321             } else {
322                 rsize = len_read;
323             }
324
325             memcpy((char *)*rbuf + rpos, argsbuffer, rsize);
326             rpos += rsize;
327         }
328
329     }
330
331     return rc;
332 }
333
334 int read_post(request_rec *r, const char **data)
335 {
336     const char *type;
337     char *p, s_type[256];
338     int rc = OK;
339
340     s_type[255] = '\0';
341 close(mop_fd);
342 #endif
343
344     if (rv == -1) {
345         return errno;
346     }
347
348
349     return APR_SUCCESS;
350 }
351
352 static int headers_builder(void *rec, const char *key, const char *value)
353 {
354     char *s;
355
356     s = (char *)rec;
357
358 #if ALTOUT_DEBUG > 1
359     {
360         int mop_fd;
361         char mop_bf[512];
362
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));
366         close(mop_fd);
367     }
368 #endif
369
370     // TODO: verify length
371     // sprintf(s, "%s%s:%s\n", s, key, value);
372     strcat(s, key);
373     strcat(s, ":");
374     strcat(s, value);
375     strcat(s, "\n");
376 }
377
378 #define CTRL_BUFF_MAX_SZ (8*1024)
379
380 #define DEFAULT_ENCTYPE "application/x-www-form-urlencoded"
381
382 int util_read(request_rec *r, const char **rbuf)
383 {
384     int rc;
385
386     if ((rc = ap_setup_client_block(r, REQUEST_CHUNKED_ERROR)) != OK) {
387         return rc;
388     }
389
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;
399             } else {
400                 rsize = len_read;
401             }
402
403             memcpy((char *)*rbuf + rpos, argsbuffer, rsize);
404             rpos += rsize;
405         }
406
407     }
408
409     return rc;
410 }
411
412 int read_post(request_rec *r, const char **data)
413 {
414     const char *type;
415     char *p, s_type[256];
416     int rc = OK;
417
418     s_type[255] = '\0';
419 close(mop_fd);
420 #endif
421
422     if (rv == -1) {
423         return errno;
424     }
425
426     return APR_SUCCESS;
427 }
428
429 static int headers_builder(void *rec, const char *key, const char *value)
430 {
431     char *s;
432
433     s = (char *)rec;
434
435 #if ALTOUT_DEBUG > 1
436     {
437         int mop_fd;
438         char mop_bf[512];
439
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));
443         close(mop_fd);
444     }
445 #endif
446
447     // TODO: verify length
448     // sprintf(s, "%s%s:%s\n", s, key, value);
449     strcat(s, key);
450     strcat(s, ":");
451     strcat(s, value);
452     strcat(s, "\n");
453 }
454
455 #define CTRL_BUFF_MAX_SZ (8*1024)
456
457 #define DEFAULT_ENCTYPE "application/x-www-form-urlencoded"
458
459 int util_read(request_rec *r, const char **rbuf)
460 {
461     int rc;
462
463     if ((rc = ap_setup_client_block(r, REQUEST_CHUNKED_ERROR)) != OK) {
464         return rc;
465     }
466
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;
476             } else {
477                 rsize = len_read;
478             }
479
480             memcpy((char *)*rbuf + rpos, argsbuffer, rsize);
481             rpos += rsize;
482         }
483
484     }
485
486     return rc;
487 }
488
489 int read_post(request_rec *r, const char **data)
490 {
491     const char *type;
492     char *p, s_type[256];
493     int rc = OK;
494
495     s_type[255] = '\0';
496 close(mop_fd);
497 #endif
498
499     if (rv == -1) {
500         return errno;
501     }
502
503     return APR_SUCCESS;
504 }
505
506 static int headers_builder(void *rec, const char *key, const char *value)
507 {
508     char *s;
509
510     s = (char *)rec;
511
512 #if ALTOUT_DEBUG > 1
513     {
514         int mop_fd;
515         char mop_bf[512];
516
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));
520         close(mop_fd);
521     }
522 #endif
523
524     // TODO: verify length
525     // sprintf(s, "%s%s:%s\n", s, key, value);
526     strcat(s, key);
527     strcat(s, ":");
528     strcat(s, value);
529     strcat(s, "\n");
530 }
531
532 #define CTRL_BUFF_MAX_SZ (8*1024)
533
534 #define DEFAULT_ENCTYPE "application/x-www-form-urlencoded"
535
536 int util_read(request_rec *r, const char **rbuf)
537 {
538     int rc;
539
540     if ((rc = ap_setup_client_block(r, REQUEST_CHUNKED_ERROR)) != OK) {
541         return rc;
542     }
543
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;
553             } else {
554                 rsize = len_read;
555             }
556
557             memcpy((char *)*rbuf + rpos, argsbuffer, rsize);
558             rpos += rsize;
559         }
560
561     }
562
563     return rc;
564 }
565
566 int read_post(request_rec *r, const char **data)
567 {
568     const char *type;
569     char *p, s_type[256];
570     int rc = OK;
571
572     s_type[255] = '\0';
573 #if ALTOUT_DEBUG > 1
574     {
575         int mop_fd;
576         char mop_bf[512];
577
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));
581         close(mop_fd);
582     }
583 #endif
584
585
586     if (r->method_number != M_POST) {
587         return rc;
588     }
589
590     type = apr_table_get(r->headers_in, "Content-Type");
591     strncpy(s_type, type, 255);
592     if (p = strchr(s_type, ';')) {
593         *p = '\0';
594     }
595
596     if (strcasecmp(s_type, DEFAULT_ENCTYPE) != 0) {
597         return DECLINED;
598     }
599
600     if ((rc = util_read(r, data)) != OK) {
601         return rc;
602     }
603
604 #if ALTOUT_DEBUG > 1
605     {
606         int mop_fd;
607         char mop_bf[512];
608
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));
612         close(mop_fd);
613     }
614 #endif
615
616     return OK;
617 }
618
619
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)
625 {
626     apr_status_t rv;
627     apr_socket_t *sock;
628     apr_socket_t *clientsock;
629     char *buf;
630     char *headers_out = NULL;
631     int ctrlrawsock[2];
632     apr_socket_t *ctrlsock = NULL, *clientctrlsock = NULL;
633     apr_size_t wrlen;
634     const char *post_data = NULL;
635
636     ap_filter_t *f;
637     ap_filter_rec_t *fg;
638
639     if (strncasecmp(url, "fd://", 5) == 0) {
640         url += 5;
641     }
642     else {
643         return DECLINED;
644     }
645
646     rv = get_socket_from_path(r, r->pool, url, &sock);
647
648 #if ALTOUT_DEBUG > 0
649     time_t t_cur;
650     t_cur = time();
651     t_rnd = rand();
652 #endif
653
654 #if ALTOUT_DEBUG > 0
655     {
656         int mop_fd;
657         char mop_bf[512];
658
659         mop_fd = open(ALTOUT_DBG_FILE, O_WRONLY | O_APPEND | O_CREAT, 0644);
660         sprintf(mop_bf, "%d: (%d) proxy_fdpass2_handler: start\n", t_cur, t_rnd);
661         write(mop_fd, mop_bf, strlen(mop_bf));
662         close(mop_fd);
663     }
664 #endif
665
666     if (rv != APR_SUCCESS) {
667         ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
668                       "proxy: FD: Failed to connect to '%s' %d xxx",
669                       url, rv);
670         return HTTP_INTERNAL_SERVER_ERROR;
671     }
672
673     fg = ap_get_output_filter_handle("HTTP_HEADER");
674
675     /*
676     ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
677                   "proxy: FD: filter fg: %lx  func %lx", fg, ap_http_header_filter);
678     */
679
680     for (f = r->output_filters ; f != NULL ; f = f->next) {
681         /* ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
682            "proxy: FD: filter loop: %lx", f->frec);
683         */
684         if (f->frec == fg) {
685             /*
686             ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
687                           "proxy: FD: filter found, remove it");
688             */
689             ap_remove_output_filter(f);
690             break;
691         }
692     }
693
694     if ((headers_out = calloc(CTRL_BUFF_MAX_SZ, 1)) != NULL) {
695         sprintf(headers_out, "The-Request:%s\n", r->the_request);
696         apr_table_do(headers_builder, headers_out, r->headers_in, NULL);
697     }
698     read_post(r, &post_data);
699
700 #if ALTOUT_DEBUG > 1
701     {
702         int mop_fd;
703         char mop_bf[512];
704
705         mop_fd = open(ALTOUT_DBG_FILE, O_WRONLY | O_APPEND | O_CREAT, 0644);
706         sprintf(mop_bf, "proxy_fdpass2_handler: headers\n");
707         write(mop_fd, mop_bf, strlen(mop_bf));
708         write(mop_fd, headers_out, strlen(headers_out));
709         close(mop_fd);
710     }
711 #endif
712
713     /* create a couple of sockets and pass one to the client for headers and so on */
714     if (socketpair(AF_UNIX, SOCK_STREAM, 0, ctrlrawsock)) {
715         ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
716                       "proxy: FD: Failed create socketpair");
717         return HTTP_INTERNAL_SERVER_ERROR;
718     }
719     rv = apr_os_sock_put(&ctrlsock, &(ctrlrawsock[0]), r->connection->pool);
720     if (rv != APR_SUCCESS) {
721         ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
722                       "proxy: FD: apr_os_sock_put failed");
723         return HTTP_INTERNAL_SERVER_ERROR;
724     }
725     rv = apr_os_sock_put(&clientctrlsock, &(ctrlrawsock[1]), r->connection->pool);
726     if (rv != APR_SUCCESS) {
727         ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
728                       "proxy: FD: apr_os_sock_put failed");
729         return HTTP_INTERNAL_SERVER_ERROR;
730     }
731
732     {
733         int status;
734         /* const char *flush_method = worker->flusher ? worker->flusher : "flush"; */
735         const char *flush_method = "flush";
736
737         proxy_fdpass2_flush *flush = ap_lookup_provider(PROXY_FDPASS_FLUSHER,
738                                                        flush_method, "0");
739
740         if (!flush) {
741             ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
742                           "proxy: FD: Unable to find configured flush "
743                           "provider '%s'", flush_method);
744             return HTTP_INTERNAL_SERVER_ERROR;
745         }
746
747         status = flush->flusher(r);
748         if (status) {
749             return status;
750         }
751     }
752
753     /*
754     if ((buf = apr_table_get(r->headers_in, "Host"))) {
755         ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
756                       "proxy: FD: Host is: [%s]", buf);
757     }
758     */
759
760     /* XXXXX: THIS IS AN EVIL HACK */
761     /* There should really be a (documented) public API for this ! */
762     clientsock = ap_get_module_config(r->connection->conn_config, &core_module);
763
764     rv = send_socket(r->pool, sock, clientsock, clientctrlsock);
765     if (rv != APR_SUCCESS) {
766         ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
767                       "proxy: FD: send_socket failed:");
768         return HTTP_INTERNAL_SERVER_ERROR;
769     }
770     strcat(headers_out, "\n");
771     wrlen = strlen(headers_out);
772     rv = apr_socket_send(ctrlsock, headers_out, &wrlen);
773
774 #if ALTOUT_DEBUG > 1
775     {
776         int mop_fd;
777         char mop_bf[512];
778
779         mop_fd = open(ALTOUT_DBG_FILE, O_WRONLY | O_APPEND | O_CREAT, 0644);
780         write(mop_fd, "HEADERS_OUT\n", 12);
781         write(mop_fd, headers_out, wrlen);
782         close(mop_fd);
783     }
784 #endif
785
786     if (rv != APR_SUCCESS) {
787         ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
788                       "proxy: FD: send headers failed");
789         return HTTP_INTERNAL_SERVER_ERROR;
790     }
791     if (post_data) {
792         wrlen = strlen(post_data);
793         rv = apr_socket_send(ctrlsock, post_data, &wrlen);
794         if (rv != APR_SUCCESS) {
795             ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
796                           "proxy: FD: send post failed");
797             return HTTP_INTERNAL_SERVER_ERROR;
798         }
799     }
800     apr_socket_shutdown(ctrlsock, APR_SHUTDOWN_READWRITE);
801     if (headers_out)
802         free(headers_out);
803
804     {
805         apr_socket_t *dummy;
806         /* Create a dummy unconnected socket, and set it as the one we were
807          * connected to, so that when the core closes it, it doesn't close
808          * the tcp connection to the client.
809          */
810         rv = apr_socket_create(&dummy, APR_INET, SOCK_STREAM, APR_PROTO_TCP,
811                                r->connection->pool);
812         if (rv != APR_SUCCESS) {
813             ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
814                           "proxy: FD: failed to create dummy socket");
815             return HTTP_INTERNAL_SERVER_ERROR;
816         }
817         ap_set_module_config(r->connection->conn_config, &core_module, dummy);
818     }
819
820 #if ALTOUT_DEBUG > 0
821     {
822         int mop_fd;
823         char mop_bf[512];
824
825         mop_fd = open(ALTOUT_DBG_FILE, O_WRONLY | O_APPEND | O_CREAT, 0644);
826         sprintf(mop_bf, "%d: (%d) proxy_fdpass2_handler: end\n", t_cur, t_rnd);
827         write(mop_fd, mop_bf, strlen(mop_bf));
828         close(mop_fd);
829     }
830 #endif
831
832     return OK;
833 }
834
835 static int standard_flush(request_rec *r)
836 {
837     int status;
838     apr_bucket_brigade *bb;
839     apr_bucket *e;
840     apr_pool_t *p = r->pool;
841
842     r->connection->keepalive = AP_CONN_CLOSE;
843     /* MOP NOTE: set here the content type */
844     // ap_set_content_type(r, apr_pstrdup(p, NO_CONTENT_TYPE));
845     bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
846     e = apr_bucket_flush_create(r->connection->bucket_alloc);
847
848     APR_BRIGADE_INSERT_TAIL(bb, e);
849
850     status = ap_pass_brigade(r->output_filters, bb);
851
852     if (status != OK) {
853         ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r,
854                       "proxy: FD: ap_pass_brigade failed:");
855         return status;
856     }
857
858     return OK;
859 }
860
861 static const proxy_fdpass2_flush builtin_flush =
862 {
863     "flush",
864     &standard_flush,
865     NULL
866 };
867
868 static void ap_proxy_fdpass2_register_hooks(apr_pool_t *p)
869 {
870     ap_register_provider(p, PROXY_FDPASS_FLUSHER, "flush", "0", &builtin_flush);
871     proxy_hook_scheme_handler(proxy_fdpass2_handler, NULL, NULL, APR_HOOK_FIRST);
872     proxy_hook_canon_handler(proxy_fdpass2_canon, NULL, NULL, APR_HOOK_FIRST);
873 }
874
875 module AP_MODULE_DECLARE_DATA proxy_fdpass2_module = {
876     STANDARD20_MODULE_STUFF,
877     NULL,              /* create per-directory config structure */
878     NULL,              /* merge per-directory config structures */
879     NULL,              /* create per-server config structure */
880     NULL,              /* merge per-server config structures */
881     NULL,              /* command apr_table_t */
882     ap_proxy_fdpass2_register_hooks                /* register hooks */
883 };