]> granicus.if.org Git - apache/blob - server/core_filters.c
Merge r1741310, r1741461 from trunk:
[apache] / server / core_filters.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 /**
18  * @file  core_filters.c
19  * @brief Core input/output network filters.
20  */
21
22 #include "apr.h"
23 #include "apr_strings.h"
24 #include "apr_lib.h"
25 #include "apr_fnmatch.h"
26 #include "apr_hash.h"
27 #include "apr_thread_proc.h"    /* for RLIMIT stuff */
28
29 #define APR_WANT_IOVEC
30 #define APR_WANT_STRFUNC
31 #define APR_WANT_MEMFUNC
32 #include "apr_want.h"
33
34 #include "ap_config.h"
35 #include "httpd.h"
36 #include "http_config.h"
37 #include "http_core.h"
38 #include "http_protocol.h" /* For index_of_response().  Grump. */
39 #include "http_request.h"
40 #include "http_vhost.h"
41 #include "http_main.h"     /* For the default_handler below... */
42 #include "http_log.h"
43 #include "util_md5.h"
44 #include "http_connection.h"
45 #include "apr_buckets.h"
46 #include "util_filter.h"
47 #include "util_ebcdic.h"
48 #include "mpm_common.h"
49 #include "scoreboard.h"
50 #include "mod_core.h"
51 #include "ap_listen.h"
52
53 #include "mod_so.h" /* for ap_find_loaded_module_symbol */
54
55 #define AP_MIN_SENDFILE_BYTES           (256)
56
57 /**
58  * Remove all zero length buckets from the brigade.
59  */
60 #define BRIGADE_NORMALIZE(b) \
61 do { \
62     apr_bucket *e = APR_BRIGADE_FIRST(b); \
63     do {  \
64         if (e->length == 0 && !APR_BUCKET_IS_METADATA(e)) { \
65             apr_bucket *d; \
66             d = APR_BUCKET_NEXT(e); \
67             apr_bucket_delete(e); \
68             e = d; \
69         } \
70         else { \
71             e = APR_BUCKET_NEXT(e); \
72         } \
73     } while (!APR_BRIGADE_EMPTY(b) && (e != APR_BRIGADE_SENTINEL(b))); \
74 } while (0)
75
76 /* we know core's module_index is 0 */
77 #undef APLOG_MODULE_INDEX
78 #define APLOG_MODULE_INDEX AP_CORE_MODULE_INDEX
79
80 struct core_output_filter_ctx {
81     apr_bucket_brigade *buffered_bb;
82     apr_bucket_brigade *tmp_flush_bb;
83     apr_pool_t *deferred_write_pool;
84     apr_size_t bytes_written;
85 };
86
87 struct core_filter_ctx {
88     apr_bucket_brigade *b;
89     apr_bucket_brigade *tmpbb;
90 };
91
92
93 apr_status_t ap_core_input_filter(ap_filter_t *f, apr_bucket_brigade *b,
94                                   ap_input_mode_t mode, apr_read_type_e block,
95                                   apr_off_t readbytes)
96 {
97     apr_status_t rv;
98     core_net_rec *net = f->ctx;
99     core_ctx_t *ctx = net->in_ctx;
100     const char *str;
101     apr_size_t len;
102
103     if (mode == AP_MODE_INIT) {
104         /*
105          * this mode is for filters that might need to 'initialize'
106          * a connection before reading request data from a client.
107          * NNTP over SSL for example needs to handshake before the
108          * server sends the welcome message.
109          * such filters would have changed the mode before this point
110          * is reached.  however, protocol modules such as NNTP should
111          * not need to know anything about SSL.  given the example, if
112          * SSL is not in the filter chain, AP_MODE_INIT is a noop.
113          */
114         return APR_SUCCESS;
115     }
116
117     if (!ctx)
118     {
119         net->in_ctx = ctx = apr_palloc(f->c->pool, sizeof(*ctx));
120         ctx->b = apr_brigade_create(f->c->pool, f->c->bucket_alloc);
121         ctx->tmpbb = apr_brigade_create(f->c->pool, f->c->bucket_alloc);
122         /* seed the brigade with the client socket. */
123         rv = ap_run_insert_network_bucket(f->c, ctx->b, net->client_socket);
124         if (rv != APR_SUCCESS)
125             return rv;
126     }
127     else if (APR_BRIGADE_EMPTY(ctx->b)) {
128         return APR_EOF;
129     }
130
131     /* ### This is bad. */
132     BRIGADE_NORMALIZE(ctx->b);
133
134     /* check for empty brigade again *AFTER* BRIGADE_NORMALIZE()
135      * If we have lost our socket bucket (see above), we are EOF.
136      *
137      * Ideally, this should be returning SUCCESS with EOS bucket, but
138      * some higher-up APIs (spec. read_request_line via ap_rgetline)
139      * want an error code. */
140     if (APR_BRIGADE_EMPTY(ctx->b)) {
141         return APR_EOF;
142     }
143
144     if (mode == AP_MODE_GETLINE) {
145         /* we are reading a single LF line, e.g. the HTTP headers */
146         rv = apr_brigade_split_line(b, ctx->b, block, HUGE_STRING_LEN);
147         /* We should treat EAGAIN here the same as we do for EOF (brigade is
148          * empty).  We do this by returning whatever we have read.  This may
149          * or may not be bogus, but is consistent (for now) with EOF logic.
150          */
151         if (APR_STATUS_IS_EAGAIN(rv) && block == APR_NONBLOCK_READ) {
152             rv = APR_SUCCESS;
153         }
154         return rv;
155     }
156
157     /* ### AP_MODE_PEEK is a horrific name for this mode because we also
158      * eat any CRLFs that we see.  That's not the obvious intention of
159      * this mode.  Determine whether anyone actually uses this or not. */
160     if (mode == AP_MODE_EATCRLF) {
161         apr_bucket *e;
162         const char *c;
163
164         /* The purpose of this loop is to ignore any CRLF (or LF) at the end
165          * of a request.  Many browsers send extra lines at the end of POST
166          * requests.  We use the PEEK method to determine if there is more
167          * data on the socket, so that we know if we should delay sending the
168          * end of one request until we have served the second request in a
169          * pipelined situation.  We don't want to actually delay sending a
170          * response if the server finds a CRLF (or LF), becuause that doesn't
171          * mean that there is another request, just a blank line.
172          */
173         while (1) {
174             if (APR_BRIGADE_EMPTY(ctx->b))
175                 return APR_EOF;
176
177             e = APR_BRIGADE_FIRST(ctx->b);
178
179             rv = apr_bucket_read(e, &str, &len, APR_NONBLOCK_READ);
180
181             if (rv != APR_SUCCESS)
182                 return rv;
183
184             c = str;
185             while (c < str + len) {
186                 if (*c == APR_ASCII_LF)
187                     c++;
188                 else if (*c == APR_ASCII_CR && *(c + 1) == APR_ASCII_LF)
189                     c += 2;
190                 else
191                     return APR_SUCCESS;
192             }
193
194             /* If we reach here, we were a bucket just full of CRLFs, so
195              * just toss the bucket. */
196             /* FIXME: Is this the right thing to do in the core? */
197             apr_bucket_delete(e);
198         }
199         return APR_SUCCESS;
200     }
201
202     /* If mode is EXHAUSTIVE, we want to just read everything until the end
203      * of the brigade, which in this case means the end of the socket.
204      * To do this, we attach the brigade that has currently been setaside to
205      * the brigade that was passed down, and send that brigade back.
206      *
207      * NOTE:  This is VERY dangerous to use, and should only be done with
208      * extreme caution.  FWLIW, this would be needed by an MPM like Perchild;
209      * such an MPM can easily request the socket and all data that has been
210      * read, which means that it can pass it to the correct child process.
211      */
212     if (mode == AP_MODE_EXHAUSTIVE) {
213         apr_bucket *e;
214
215         /* Tack on any buckets that were set aside. */
216         APR_BRIGADE_CONCAT(b, ctx->b);
217
218         /* Since we've just added all potential buckets (which will most
219          * likely simply be the socket bucket) we know this is the end,
220          * so tack on an EOS too. */
221         /* We have read until the brigade was empty, so we know that we
222          * must be EOS. */
223         e = apr_bucket_eos_create(f->c->bucket_alloc);
224         APR_BRIGADE_INSERT_TAIL(b, e);
225         return APR_SUCCESS;
226     }
227
228     /* read up to the amount they specified. */
229     if (mode == AP_MODE_READBYTES || mode == AP_MODE_SPECULATIVE) {
230         apr_bucket *e;
231
232         AP_DEBUG_ASSERT(readbytes > 0);
233
234         e = APR_BRIGADE_FIRST(ctx->b);
235         rv = apr_bucket_read(e, &str, &len, block);
236
237         if (APR_STATUS_IS_EAGAIN(rv) && block == APR_NONBLOCK_READ) {
238             /* getting EAGAIN for a blocking read is an error; for a
239              * non-blocking read, return an empty brigade. */
240             return APR_SUCCESS;
241         }
242         else if (rv != APR_SUCCESS) {
243             return rv;
244         }
245         else if (block == APR_BLOCK_READ && len == 0) {
246             /* We wanted to read some bytes in blocking mode.  We read
247              * 0 bytes.  Hence, we now assume we are EOS.
248              *
249              * When we are in normal mode, return an EOS bucket to the
250              * caller.
251              * When we are in speculative mode, leave ctx->b empty, so
252              * that the next call returns an EOS bucket.
253              */
254             apr_bucket_delete(e);
255
256             if (mode == AP_MODE_READBYTES) {
257                 e = apr_bucket_eos_create(f->c->bucket_alloc);
258                 APR_BRIGADE_INSERT_TAIL(b, e);
259             }
260             return APR_SUCCESS;
261         }
262
263         /* Have we read as much data as we wanted (be greedy)? */
264         if (len < readbytes) {
265             apr_size_t bucket_len;
266
267             rv = APR_SUCCESS;
268             /* We already registered the data in e in len */
269             e = APR_BUCKET_NEXT(e);
270             while ((len < readbytes) && (rv == APR_SUCCESS)
271                    && (e != APR_BRIGADE_SENTINEL(ctx->b))) {
272                 /* Check for the availability of buckets with known length */
273                 if (e->length != -1) {
274                     len += e->length;
275                     e = APR_BUCKET_NEXT(e);
276                 }
277                 else {
278                     /*
279                      * Read from bucket, but non blocking. If there isn't any
280                      * more data, well than this is fine as well, we will
281                      * not wait for more since we already got some and we are
282                      * only checking if there isn't more.
283                      */
284                     rv = apr_bucket_read(e, &str, &bucket_len,
285                                          APR_NONBLOCK_READ);
286                     if (rv == APR_SUCCESS) {
287                         len += bucket_len;
288                         e = APR_BUCKET_NEXT(e);
289                     }
290                 }
291             }
292         }
293
294         /* We can only return at most what we read. */
295         if (len < readbytes) {
296             readbytes = len;
297         }
298
299         rv = apr_brigade_partition(ctx->b, readbytes, &e);
300         if (rv != APR_SUCCESS) {
301             return rv;
302         }
303
304         /* Must do move before CONCAT */
305         ctx->tmpbb = apr_brigade_split_ex(ctx->b, e, ctx->tmpbb);
306
307         if (mode == AP_MODE_READBYTES) {
308             APR_BRIGADE_CONCAT(b, ctx->b);
309         }
310         else if (mode == AP_MODE_SPECULATIVE) {
311             apr_bucket *copy_bucket;
312
313             for (e = APR_BRIGADE_FIRST(ctx->b);
314                  e != APR_BRIGADE_SENTINEL(ctx->b);
315                  e = APR_BUCKET_NEXT(e))
316             {
317                 rv = apr_bucket_copy(e, &copy_bucket);
318                 if (rv != APR_SUCCESS) {
319                     return rv;
320                 }
321                 APR_BRIGADE_INSERT_TAIL(b, copy_bucket);
322             }
323         }
324
325         /* Take what was originally there and place it back on ctx->b */
326         APR_BRIGADE_CONCAT(ctx->b, ctx->tmpbb);
327     }
328     return APR_SUCCESS;
329 }
330
331 static void setaside_remaining_output(ap_filter_t *f,
332                                       core_output_filter_ctx_t *ctx,
333                                       apr_bucket_brigade *bb,
334                                       conn_rec *c);
335
336 static apr_status_t send_brigade_nonblocking(apr_socket_t *s,
337                                              apr_bucket_brigade *bb,
338                                              apr_size_t *bytes_written,
339                                              conn_rec *c);
340
341 static void remove_empty_buckets(apr_bucket_brigade *bb);
342
343 static apr_status_t send_brigade_blocking(apr_socket_t *s,
344                                           apr_bucket_brigade *bb,
345                                           apr_size_t *bytes_written,
346                                           conn_rec *c);
347
348 static apr_status_t writev_nonblocking(apr_socket_t *s,
349                                        struct iovec *vec, apr_size_t nvec,
350                                        apr_bucket_brigade *bb,
351                                        apr_size_t *cumulative_bytes_written,
352                                        conn_rec *c);
353
354 #if APR_HAS_SENDFILE
355 static apr_status_t sendfile_nonblocking(apr_socket_t *s,
356                                          apr_bucket *bucket,
357                                          apr_size_t *cumulative_bytes_written,
358                                          conn_rec *c);
359 #endif
360
361 /* XXX: Should these be configurable parameters? */
362 #define THRESHOLD_MIN_WRITE 4096
363 #define THRESHOLD_MAX_BUFFER 65536
364 #define MAX_REQUESTS_IN_PIPELINE 5
365
366 /* Optional function coming from mod_logio, used for logging of output
367  * traffic
368  */
369 extern APR_OPTIONAL_FN_TYPE(ap_logio_add_bytes_out) *ap__logio_add_bytes_out;
370
371 apr_status_t ap_core_output_filter(ap_filter_t *f, apr_bucket_brigade *new_bb)
372 {
373     conn_rec *c = f->c;
374     core_net_rec *net = f->ctx;
375     core_output_filter_ctx_t *ctx = net->out_ctx;
376     apr_bucket_brigade *bb = NULL;
377     apr_bucket *bucket, *next, *flush_upto = NULL;
378     apr_size_t bytes_in_brigade, non_file_bytes_in_brigade;
379     int eor_buckets_in_brigade, morphing_bucket_in_brigade;
380     apr_status_t rv;
381
382     /* Fail quickly if the connection has already been aborted. */
383     if (c->aborted) {
384         if (new_bb != NULL) {
385             apr_brigade_cleanup(new_bb);
386         }
387         return APR_ECONNABORTED;
388     }
389
390     if (ctx == NULL) {
391         ctx = apr_pcalloc(c->pool, sizeof(*ctx));
392         net->out_ctx = (core_output_filter_ctx_t *)ctx;
393         /*
394          * Need to create tmp brigade with correct lifetime. Passing
395          * NULL to apr_brigade_split_ex would result in a brigade
396          * allocated from bb->pool which might be wrong.
397          */
398         ctx->tmp_flush_bb = apr_brigade_create(c->pool, c->bucket_alloc);
399         /* same for buffered_bb and ap_save_brigade */
400         ctx->buffered_bb = apr_brigade_create(c->pool, c->bucket_alloc);
401     }
402
403     if (new_bb != NULL)
404         bb = new_bb;
405
406     if ((ctx->buffered_bb != NULL) &&
407         !APR_BRIGADE_EMPTY(ctx->buffered_bb)) {
408         if (new_bb != NULL) {
409             APR_BRIGADE_PREPEND(bb, ctx->buffered_bb);
410         }
411         else {
412             bb = ctx->buffered_bb;
413         }
414         c->data_in_output_filters = 0;
415     }
416     else if (new_bb == NULL) {
417         return APR_SUCCESS;
418     }
419
420     /* Scan through the brigade and decide whether to attempt a write,
421      * and how much to write, based on the following rules:
422      *
423      *  1) The new_bb is null: Do a nonblocking write of as much as
424      *     possible: do a nonblocking write of as much data as possible,
425      *     then save the rest in ctx->buffered_bb.  (If new_bb == NULL,
426      *     it probably means that the MPM is doing asynchronous write
427      *     completion and has just determined that this connection
428      *     is writable.)
429      *
430      *  2) Determine if and up to which bucket we need to do a blocking
431      *     write:
432      *
433      *  a) The brigade contains a flush bucket: Do a blocking write
434      *     of everything up that point.
435      *
436      *  b) The request is in CONN_STATE_HANDLER state, and the brigade
437      *     contains at least THRESHOLD_MAX_BUFFER bytes in non-file
438      *     buckets: Do blocking writes until the amount of data in the
439      *     buffer is less than THRESHOLD_MAX_BUFFER.  (The point of this
440      *     rule is to provide flow control, in case a handler is
441      *     streaming out lots of data faster than the data can be
442      *     sent to the client.)
443      *
444      *  c) The request is in CONN_STATE_HANDLER state, and the brigade
445      *     contains at least MAX_REQUESTS_IN_PIPELINE EOR buckets:
446      *     Do blocking writes until less than MAX_REQUESTS_IN_PIPELINE EOR
447      *     buckets are left. (The point of this rule is to prevent too many
448      *     FDs being kept open by pipelined requests, possibly allowing a
449      *     DoS).
450      *
451      *  d) The brigade contains a morphing bucket: If there was no other
452      *     reason to do a blocking write yet, try reading the bucket. If its
453      *     contents fit into memory before THRESHOLD_MAX_BUFFER is reached,
454      *     everything is fine. Otherwise we need to do a blocking write the
455      *     up to and including the morphing bucket, because ap_save_brigade()
456      *     would read the whole bucket into memory later on.
457      *
458      *  3) Actually do the blocking write up to the last bucket determined
459      *     by rules 2a-d. The point of doing only one flush is to make as
460      *     few calls to writev() as possible.
461      *
462      *  4) If the brigade contains at least THRESHOLD_MIN_WRITE
463      *     bytes: Do a nonblocking write of as much data as possible,
464      *     then save the rest in ctx->buffered_bb.
465      */
466
467     if (new_bb == NULL) {
468         rv = send_brigade_nonblocking(net->client_socket, bb,
469                                       &(ctx->bytes_written), c);
470         if (rv != APR_SUCCESS && !APR_STATUS_IS_EAGAIN(rv)) {
471             /* The client has aborted the connection */
472             ap_log_cerror(APLOG_MARK, APLOG_TRACE1, rv, c,
473                           "core_output_filter: writing data to the network");
474             apr_brigade_cleanup(bb);
475             c->aborted = 1;
476             return rv;
477         }
478         setaside_remaining_output(f, ctx, bb, c);
479         return APR_SUCCESS;
480     }
481
482     bytes_in_brigade = 0;
483     non_file_bytes_in_brigade = 0;
484     eor_buckets_in_brigade = 0;
485     morphing_bucket_in_brigade = 0;
486
487     for (bucket = APR_BRIGADE_FIRST(bb); bucket != APR_BRIGADE_SENTINEL(bb);
488          bucket = next) {
489         next = APR_BUCKET_NEXT(bucket);
490
491         if (!APR_BUCKET_IS_METADATA(bucket)) {
492             if (bucket->length == (apr_size_t)-1) {
493                 /*
494                  * A setaside of morphing buckets would read everything into
495                  * memory. Instead, we will flush everything up to and
496                  * including this bucket.
497                  */
498                 morphing_bucket_in_brigade = 1;
499             }
500             else {
501                 bytes_in_brigade += bucket->length;
502                 if (!APR_BUCKET_IS_FILE(bucket))
503                     non_file_bytes_in_brigade += bucket->length;
504             }
505         }
506         else if (AP_BUCKET_IS_EOR(bucket)) {
507             eor_buckets_in_brigade++;
508         }
509
510         if (APR_BUCKET_IS_FLUSH(bucket)
511             || non_file_bytes_in_brigade >= THRESHOLD_MAX_BUFFER
512             || morphing_bucket_in_brigade
513             || eor_buckets_in_brigade > MAX_REQUESTS_IN_PIPELINE) {
514             /* this segment of the brigade MUST be sent before returning. */
515
516             if (APLOGctrace6(c)) {
517                 char *reason = APR_BUCKET_IS_FLUSH(bucket) ?
518                                "FLUSH bucket" :
519                                (non_file_bytes_in_brigade >= THRESHOLD_MAX_BUFFER) ?
520                                "THRESHOLD_MAX_BUFFER" :
521                                morphing_bucket_in_brigade ? "morphing bucket" :
522                                "MAX_REQUESTS_IN_PIPELINE";
523                 ap_log_cerror(APLOG_MARK, APLOG_TRACE6, 0, c,
524                               "core_output_filter: flushing because of %s",
525                               reason);
526             }
527             /*
528              * Defer the actual blocking write to avoid doing many writes.
529              */
530             flush_upto = next;
531
532             bytes_in_brigade = 0;
533             non_file_bytes_in_brigade = 0;
534             eor_buckets_in_brigade = 0;
535             morphing_bucket_in_brigade = 0;
536         }
537     }
538
539     if (flush_upto != NULL) {
540         ctx->tmp_flush_bb = apr_brigade_split_ex(bb, flush_upto,
541                                                  ctx->tmp_flush_bb);
542         rv = send_brigade_blocking(net->client_socket, bb,
543                                    &(ctx->bytes_written), c);
544         if (rv != APR_SUCCESS) {
545             /* The client has aborted the connection */
546             ap_log_cerror(APLOG_MARK, APLOG_TRACE1, rv, c,
547                           "core_output_filter: writing data to the network");
548             apr_brigade_cleanup(bb);
549             c->aborted = 1;
550             return rv;
551         }
552         APR_BRIGADE_CONCAT(bb, ctx->tmp_flush_bb);
553     }
554
555     if (bytes_in_brigade >= THRESHOLD_MIN_WRITE) {
556         rv = send_brigade_nonblocking(net->client_socket, bb,
557                                       &(ctx->bytes_written), c);
558         if ((rv != APR_SUCCESS) && (!APR_STATUS_IS_EAGAIN(rv))) {
559             /* The client has aborted the connection */
560             ap_log_cerror(APLOG_MARK, APLOG_TRACE1, rv, c,
561                           "core_output_filter: writing data to the network");
562             apr_brigade_cleanup(bb);
563             c->aborted = 1;
564             return rv;
565         }
566     }
567
568     setaside_remaining_output(f, ctx, bb, c);
569     return APR_SUCCESS;
570 }
571
572 /*
573  * This function assumes that either ctx->buffered_bb == NULL, or
574  * ctx->buffered_bb is empty, or ctx->buffered_bb == bb
575  */
576 static void setaside_remaining_output(ap_filter_t *f,
577                                       core_output_filter_ctx_t *ctx,
578                                       apr_bucket_brigade *bb,
579                                       conn_rec *c)
580 {
581     if (bb == NULL) {
582         return;
583     }
584     remove_empty_buckets(bb);
585     if (!APR_BRIGADE_EMPTY(bb)) {
586         c->data_in_output_filters = 1;
587         if (bb != ctx->buffered_bb) {
588             if (!ctx->deferred_write_pool) {
589                 apr_pool_create(&ctx->deferred_write_pool, c->pool);
590                 apr_pool_tag(ctx->deferred_write_pool, "deferred_write");
591             }
592             ap_save_brigade(f, &(ctx->buffered_bb), &bb,
593                             ctx->deferred_write_pool);
594         }
595     }
596     else if (ctx->deferred_write_pool) {
597         /*
598          * There are no more requests in the pipeline. We can just clear the
599          * pool.
600          */
601         apr_pool_clear(ctx->deferred_write_pool);
602     }
603 }
604
605 #ifndef APR_MAX_IOVEC_SIZE
606 #define MAX_IOVEC_TO_WRITE 16
607 #else
608 #if APR_MAX_IOVEC_SIZE > 16
609 #define MAX_IOVEC_TO_WRITE 16
610 #else
611 #define MAX_IOVEC_TO_WRITE APR_MAX_IOVEC_SIZE
612 #endif
613 #endif
614
615 static apr_status_t send_brigade_nonblocking(apr_socket_t *s,
616                                              apr_bucket_brigade *bb,
617                                              apr_size_t *bytes_written,
618                                              conn_rec *c)
619 {
620     apr_bucket *bucket, *next;
621     apr_status_t rv;
622     struct iovec vec[MAX_IOVEC_TO_WRITE];
623     apr_size_t nvec = 0;
624
625     remove_empty_buckets(bb);
626
627     for (bucket = APR_BRIGADE_FIRST(bb);
628          bucket != APR_BRIGADE_SENTINEL(bb);
629          bucket = next) {
630         next = APR_BUCKET_NEXT(bucket);
631 #if APR_HAS_SENDFILE
632         if (APR_BUCKET_IS_FILE(bucket)) {
633             apr_bucket_file *file_bucket = (apr_bucket_file *)(bucket->data);
634             apr_file_t *fd = file_bucket->fd;
635             /* Use sendfile to send this file unless:
636              *   - the platform doesn't support sendfile,
637              *   - the file is too small for sendfile to be useful, or
638              *   - sendfile is disabled in the httpd config via "EnableSendfile off"
639              */
640
641             if ((apr_file_flags_get(fd) & APR_SENDFILE_ENABLED) &&
642                 (bucket->length >= AP_MIN_SENDFILE_BYTES)) {
643                 if (nvec > 0) {
644                     (void)apr_socket_opt_set(s, APR_TCP_NOPUSH, 1);
645                     rv = writev_nonblocking(s, vec, nvec, bb, bytes_written, c);
646                     if (rv != APR_SUCCESS) {
647                         (void)apr_socket_opt_set(s, APR_TCP_NOPUSH, 0);
648                         return rv;
649                     }
650                 }
651                 rv = sendfile_nonblocking(s, bucket, bytes_written, c);
652                 if (nvec > 0) {
653                     (void)apr_socket_opt_set(s, APR_TCP_NOPUSH, 0);
654                     nvec = 0;
655                 }
656                 if (rv != APR_SUCCESS) {
657                     return rv;
658                 }
659                 break;
660             }
661         }
662 #endif /* APR_HAS_SENDFILE */
663         /* didn't sendfile */
664         if (!APR_BUCKET_IS_METADATA(bucket)) {
665             const char *data;
666             apr_size_t length;
667             
668             /* Non-blocking read first, in case this is a morphing
669              * bucket type. */
670             rv = apr_bucket_read(bucket, &data, &length, APR_NONBLOCK_READ);
671             if (APR_STATUS_IS_EAGAIN(rv)) {
672                 /* Read would block; flush any pending data and retry. */
673                 if (nvec) {
674                     rv = writev_nonblocking(s, vec, nvec, bb, bytes_written, c);
675                     if (rv) {
676                         return rv;
677                     }
678                     nvec = 0;
679                 }
680                 
681                 rv = apr_bucket_read(bucket, &data, &length, APR_BLOCK_READ);
682             }
683             if (rv != APR_SUCCESS) {
684                 return rv;
685             }
686
687             /* reading may have split the bucket, so recompute next: */
688             next = APR_BUCKET_NEXT(bucket);
689             vec[nvec].iov_base = (char *)data;
690             vec[nvec].iov_len = length;
691             nvec++;
692             if (nvec == MAX_IOVEC_TO_WRITE) {
693                 rv = writev_nonblocking(s, vec, nvec, bb, bytes_written, c);
694                 nvec = 0;
695                 if (rv != APR_SUCCESS) {
696                     return rv;
697                 }
698                 break;
699             }
700         }
701     }
702
703     if (nvec > 0) {
704         rv = writev_nonblocking(s, vec, nvec, bb, bytes_written, c);
705         if (rv != APR_SUCCESS) {
706             return rv;
707         }
708     }
709
710     remove_empty_buckets(bb);
711
712     return APR_SUCCESS;
713 }
714
715 static void remove_empty_buckets(apr_bucket_brigade *bb)
716 {
717     apr_bucket *bucket;
718     while (((bucket = APR_BRIGADE_FIRST(bb)) != APR_BRIGADE_SENTINEL(bb)) &&
719            (APR_BUCKET_IS_METADATA(bucket) || (bucket->length == 0))) {
720         apr_bucket_delete(bucket);
721     }
722 }
723
724 static apr_status_t send_brigade_blocking(apr_socket_t *s,
725                                           apr_bucket_brigade *bb,
726                                           apr_size_t *bytes_written,
727                                           conn_rec *c)
728 {
729     apr_status_t rv;
730
731     rv = APR_SUCCESS;
732     while (!APR_BRIGADE_EMPTY(bb)) {
733         rv = send_brigade_nonblocking(s, bb, bytes_written, c);
734         if (rv != APR_SUCCESS) {
735             if (APR_STATUS_IS_EAGAIN(rv)) {
736                 /* Wait until we can send more data */
737                 apr_int32_t nsds;
738                 apr_interval_time_t timeout;
739                 apr_pollfd_t pollset;
740
741                 pollset.p = c->pool;
742                 pollset.desc_type = APR_POLL_SOCKET;
743                 pollset.reqevents = APR_POLLOUT;
744                 pollset.desc.s = s;
745                 apr_socket_timeout_get(s, &timeout);
746                 do {
747                     rv = apr_poll(&pollset, 1, &nsds, timeout);
748                 } while (APR_STATUS_IS_EINTR(rv));
749                 if (rv != APR_SUCCESS) {
750                     break;
751                 }
752             }
753             else {
754                 break;
755             }
756         }
757     }
758     return rv;
759 }
760
761 static apr_status_t writev_nonblocking(apr_socket_t *s,
762                                        struct iovec *vec, apr_size_t nvec,
763                                        apr_bucket_brigade *bb,
764                                        apr_size_t *cumulative_bytes_written,
765                                        conn_rec *c)
766 {
767     apr_status_t rv = APR_SUCCESS, arv;
768     apr_size_t bytes_written = 0, bytes_to_write = 0;
769     apr_size_t i, offset;
770     apr_interval_time_t old_timeout;
771
772     arv = apr_socket_timeout_get(s, &old_timeout);
773     if (arv != APR_SUCCESS) {
774         return arv;
775     }
776     arv = apr_socket_timeout_set(s, 0);
777     if (arv != APR_SUCCESS) {
778         return arv;
779     }
780
781     for (i = 0; i < nvec; i++) {
782         bytes_to_write += vec[i].iov_len;
783     }
784     offset = 0;
785     while (bytes_written < bytes_to_write) {
786         apr_size_t n = 0;
787         rv = apr_socket_sendv(s, vec + offset, nvec - offset, &n);
788         if (n > 0) {
789             bytes_written += n;
790             for (i = offset; i < nvec; ) {
791                 apr_bucket *bucket = APR_BRIGADE_FIRST(bb);
792                 if (APR_BUCKET_IS_METADATA(bucket)) {
793                     apr_bucket_delete(bucket);
794                 }
795                 else if (n >= vec[i].iov_len) {
796                     apr_bucket_delete(bucket);
797                     offset++;
798                     n -= vec[i++].iov_len;
799                 }
800                 else {
801                     apr_bucket_split(bucket, n);
802                     apr_bucket_delete(bucket);
803                     vec[i].iov_len -= n;
804                     vec[i].iov_base = (char *) vec[i].iov_base + n;
805                     break;
806                 }
807             }
808         }
809         if (rv != APR_SUCCESS) {
810             break;
811         }
812     }
813     if ((ap__logio_add_bytes_out != NULL) && (bytes_written > 0)) {
814         ap__logio_add_bytes_out(c, bytes_written);
815     }
816     *cumulative_bytes_written += bytes_written;
817
818     arv = apr_socket_timeout_set(s, old_timeout);
819     if ((arv != APR_SUCCESS) && (rv == APR_SUCCESS)) {
820         return arv;
821     }
822     else {
823         return rv;
824     }
825 }
826
827 #if APR_HAS_SENDFILE
828
829 static apr_status_t sendfile_nonblocking(apr_socket_t *s,
830                                          apr_bucket *bucket,
831                                          apr_size_t *cumulative_bytes_written,
832                                          conn_rec *c)
833 {
834     apr_status_t rv = APR_SUCCESS;
835     apr_bucket_file *file_bucket;
836     apr_file_t *fd;
837     apr_size_t file_length;
838     apr_off_t file_offset;
839     apr_size_t bytes_written = 0;
840
841     if (!APR_BUCKET_IS_FILE(bucket)) {
842         ap_log_error(APLOG_MARK, APLOG_ERR, rv, c->base_server, APLOGNO(00006)
843                      "core_filter: sendfile_nonblocking: "
844                      "this should never happen");
845         return APR_EGENERAL;
846     }
847     file_bucket = (apr_bucket_file *)(bucket->data);
848     fd = file_bucket->fd;
849     file_length = bucket->length;
850     file_offset = bucket->start;
851
852     if (bytes_written < file_length) {
853         apr_size_t n = file_length - bytes_written;
854         apr_status_t arv;
855         apr_interval_time_t old_timeout;
856
857         arv = apr_socket_timeout_get(s, &old_timeout);
858         if (arv != APR_SUCCESS) {
859             return arv;
860         }
861         arv = apr_socket_timeout_set(s, 0);
862         if (arv != APR_SUCCESS) {
863             return arv;
864         }
865         rv = apr_socket_sendfile(s, fd, NULL, &file_offset, &n, 0);
866         if (rv == APR_SUCCESS) {
867             bytes_written += n;
868             file_offset += n;
869         }
870         arv = apr_socket_timeout_set(s, old_timeout);
871         if ((arv != APR_SUCCESS) && (rv == APR_SUCCESS)) {
872             rv = arv;
873         }
874     }
875     if ((ap__logio_add_bytes_out != NULL) && (bytes_written > 0)) {
876         ap__logio_add_bytes_out(c, bytes_written);
877     }
878     *cumulative_bytes_written += bytes_written;
879     if ((bytes_written < file_length) && (bytes_written > 0)) {
880         apr_bucket_split(bucket, bytes_written);
881         apr_bucket_delete(bucket);
882     }
883     else if (bytes_written == file_length) {
884         apr_bucket_delete(bucket);
885     }
886     return rv;
887 }
888
889 #endif