better printf type for time_t
[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     int t_rnd;
651     t_cur = time();
652     t_rnd = rand();
653 #endif
654
655 #if ALTOUT_DEBUG > 0
656     {
657         int mop_fd;
658         char mop_bf[512];
659
660         mop_fd = open(ALTOUT_DBG_FILE, O_WRONLY | O_APPEND | O_CREAT, 0644);
661         sprintf(mop_bf, "%lld: (%d) proxy_fdpass2_handler: start\n", t_cur, t_rnd);
662         write(mop_fd, mop_bf, strlen(mop_bf));
663         close(mop_fd);
664     }
665 #endif
666
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",
670                       url, rv);
671         return HTTP_INTERNAL_SERVER_ERROR;
672     }
673
674     fg = ap_get_output_filter_handle("HTTP_HEADER");
675
676     /*
677     ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
678                   "proxy: FD: filter fg: %lx  func %lx", fg, ap_http_header_filter);
679     */
680
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);
684         */
685         if (f->frec == fg) {
686             /*
687             ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
688                           "proxy: FD: filter found, remove it");
689             */
690             ap_remove_output_filter(f);
691             break;
692         }
693     }
694
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);
698     }
699     read_post(r, &post_data);
700
701 #if ALTOUT_DEBUG > 1
702     {
703         int mop_fd;
704         char mop_bf[512];
705
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));
710         close(mop_fd);
711     }
712 #endif
713
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;
719     }
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;
725     }
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;
731     }
732
733     {
734         int status;
735         /* const char *flush_method = worker->flusher ? worker->flusher : "flush"; */
736         const char *flush_method = "flush";
737
738         proxy_fdpass2_flush *flush = ap_lookup_provider(PROXY_FDPASS_FLUSHER,
739                                                        flush_method, "0");
740
741         if (!flush) {
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;
746         }
747
748         status = flush->flusher(r);
749         if (status) {
750             return status;
751         }
752     }
753
754     /*
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);
758     }
759     */
760
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);
764
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;
770     }
771     strcat(headers_out, "\n");
772     wrlen = strlen(headers_out);
773     rv = apr_socket_send(ctrlsock, headers_out, &wrlen);
774
775 #if ALTOUT_DEBUG > 1
776     {
777         int mop_fd;
778         char mop_bf[512];
779
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);
783         close(mop_fd);
784     }
785 #endif
786
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;
791     }
792     if (post_data) {
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;
799         }
800     }
801     apr_socket_shutdown(ctrlsock, APR_SHUTDOWN_READWRITE);
802     if (headers_out)
803         free(headers_out);
804
805     {
806         apr_socket_t *dummy;
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.
810          */
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;
817         }
818         ap_set_module_config(r->connection->conn_config, &core_module, dummy);
819     }
820
821 #if ALTOUT_DEBUG > 0
822     {
823         int mop_fd;
824         char mop_bf[512];
825
826         mop_fd = open(ALTOUT_DBG_FILE, O_WRONLY | O_APPEND | O_CREAT, 0644);
827         sprintf(mop_bf, "%lld: (%d) proxy_fdpass2_handler: end\n", t_cur, t_rnd);
828         write(mop_fd, mop_bf, strlen(mop_bf));
829         close(mop_fd);
830     }
831 #endif
832
833     return OK;
834 }
835
836 static int standard_flush(request_rec *r)
837 {
838     int status;
839     apr_bucket_brigade *bb;
840     apr_bucket *e;
841     apr_pool_t *p = r->pool;
842
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);
848
849     APR_BRIGADE_INSERT_TAIL(bb, e);
850
851     status = ap_pass_brigade(r->output_filters, bb);
852
853     if (status != OK) {
854         ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r,
855                       "proxy: FD: ap_pass_brigade failed:");
856         return status;
857     }
858
859     return OK;
860 }
861
862 static const proxy_fdpass2_flush builtin_flush =
863 {
864     "flush",
865     &standard_flush,
866     NULL
867 };
868
869 static void ap_proxy_fdpass2_register_hooks(apr_pool_t *p)
870 {
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);
874 }
875
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 */
884 };