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