1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * @file core_filters.c
19 * @brief Core input/output network filters.
23 #include "apr_strings.h"
25 #include "apr_fnmatch.h"
27 #include "apr_thread_proc.h" /* for RLIMIT stuff */
29 #define APR_WANT_IOVEC
30 #define APR_WANT_STRFUNC
31 #define APR_WANT_MEMFUNC
34 #include "ap_config.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... */
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"
51 #include "mod_proxy.h"
52 #include "ap_listen.h"
54 #include "mod_so.h" /* for ap_find_loaded_module_symbol */
56 #define AP_MIN_SENDFILE_BYTES (256)
59 * Remove all zero length buckets from the brigade.
61 #define BRIGADE_NORMALIZE(b) \
63 apr_bucket *e = APR_BRIGADE_FIRST(b); \
65 if (e->length == 0 && !APR_BUCKET_IS_METADATA(e)) { \
67 d = APR_BUCKET_NEXT(e); \
68 apr_bucket_delete(e); \
72 e = APR_BUCKET_NEXT(e); \
74 } while (!APR_BRIGADE_EMPTY(b) && (e != APR_BRIGADE_SENTINEL(b))); \
77 /* we know core's module_index is 0 */
78 #undef APLOG_MODULE_INDEX
79 #define APLOG_MODULE_INDEX AP_CORE_MODULE_INDEX
81 int ap_core_input_filter(ap_filter_t *f, apr_bucket_brigade *b,
82 ap_input_mode_t mode, apr_read_type_e block,
87 core_net_rec *net = f->ctx;
88 core_ctx_t *ctx = net->in_ctx;
92 if (mode == AP_MODE_INIT) {
94 * this mode is for filters that might need to 'initialize'
95 * a connection before reading request data from a client.
96 * NNTP over SSL for example needs to handshake before the
97 * server sends the welcome message.
98 * such filters would have changed the mode before this point
99 * is reached. however, protocol modules such as NNTP should
100 * not need to know anything about SSL. given the example, if
101 * SSL is not in the filter chain, AP_MODE_INIT is a noop.
108 ctx = apr_pcalloc(f->c->pool, sizeof(*ctx));
109 ctx->b = apr_brigade_create(f->c->pool, f->c->bucket_alloc);
110 ctx->tmpbb = apr_brigade_create(ctx->b->p, ctx->b->bucket_alloc);
111 /* seed the brigade with the client socket. */
112 e = apr_bucket_socket_create(net->client_socket, f->c->bucket_alloc);
113 APR_BRIGADE_INSERT_TAIL(ctx->b, e);
116 else if (APR_BRIGADE_EMPTY(ctx->b)) {
120 /* ### This is bad. */
121 BRIGADE_NORMALIZE(ctx->b);
123 /* check for empty brigade again *AFTER* BRIGADE_NORMALIZE()
124 * If we have lost our socket bucket (see above), we are EOF.
126 * Ideally, this should be returning SUCCESS with EOS bucket, but
127 * some higher-up APIs (spec. read_request_line via ap_rgetline)
128 * want an error code. */
129 if (APR_BRIGADE_EMPTY(ctx->b)) {
133 if (mode == AP_MODE_GETLINE) {
134 /* we are reading a single LF line, e.g. the HTTP headers */
135 rv = apr_brigade_split_line(b, ctx->b, block, HUGE_STRING_LEN);
136 /* We should treat EAGAIN here the same as we do for EOF (brigade is
137 * empty). We do this by returning whatever we have read. This may
138 * or may not be bogus, but is consistent (for now) with EOF logic.
140 if (APR_STATUS_IS_EAGAIN(rv)) {
146 /* ### AP_MODE_PEEK is a horrific name for this mode because we also
147 * eat any CRLFs that we see. That's not the obvious intention of
148 * this mode. Determine whether anyone actually uses this or not. */
149 if (mode == AP_MODE_EATCRLF) {
153 /* The purpose of this loop is to ignore any CRLF (or LF) at the end
154 * of a request. Many browsers send extra lines at the end of POST
155 * requests. We use the PEEK method to determine if there is more
156 * data on the socket, so that we know if we should delay sending the
157 * end of one request until we have served the second request in a
158 * pipelined situation. We don't want to actually delay sending a
159 * response if the server finds a CRLF (or LF), becuause that doesn't
160 * mean that there is another request, just a blank line.
163 if (APR_BRIGADE_EMPTY(ctx->b))
166 e = APR_BRIGADE_FIRST(ctx->b);
168 rv = apr_bucket_read(e, &str, &len, APR_NONBLOCK_READ);
170 if (rv != APR_SUCCESS)
174 while (c < str + len) {
175 if (*c == APR_ASCII_LF)
177 else if (*c == APR_ASCII_CR && *(c + 1) == APR_ASCII_LF)
183 /* If we reach here, we were a bucket just full of CRLFs, so
184 * just toss the bucket. */
185 /* FIXME: Is this the right thing to do in the core? */
186 apr_bucket_delete(e);
191 /* If mode is EXHAUSTIVE, we want to just read everything until the end
192 * of the brigade, which in this case means the end of the socket.
193 * To do this, we attach the brigade that has currently been setaside to
194 * the brigade that was passed down, and send that brigade back.
196 * NOTE: This is VERY dangerous to use, and should only be done with
197 * extreme caution. FWLIW, this would be needed by an MPM like Perchild;
198 * such an MPM can easily request the socket and all data that has been
199 * read, which means that it can pass it to the correct child process.
201 if (mode == AP_MODE_EXHAUSTIVE) {
204 /* Tack on any buckets that were set aside. */
205 APR_BRIGADE_CONCAT(b, ctx->b);
207 /* Since we've just added all potential buckets (which will most
208 * likely simply be the socket bucket) we know this is the end,
209 * so tack on an EOS too. */
210 /* We have read until the brigade was empty, so we know that we
212 e = apr_bucket_eos_create(f->c->bucket_alloc);
213 APR_BRIGADE_INSERT_TAIL(b, e);
217 /* read up to the amount they specified. */
218 if (mode == AP_MODE_READBYTES || mode == AP_MODE_SPECULATIVE) {
221 AP_DEBUG_ASSERT(readbytes > 0);
223 e = APR_BRIGADE_FIRST(ctx->b);
224 rv = apr_bucket_read(e, &str, &len, block);
226 if (APR_STATUS_IS_EAGAIN(rv)) {
229 else if (rv != APR_SUCCESS) {
232 else if (block == APR_BLOCK_READ && len == 0) {
233 /* We wanted to read some bytes in blocking mode. We read
234 * 0 bytes. Hence, we now assume we are EOS.
236 * When we are in normal mode, return an EOS bucket to the
238 * When we are in speculative mode, leave ctx->b empty, so
239 * that the next call returns an EOS bucket.
241 apr_bucket_delete(e);
243 if (mode == AP_MODE_READBYTES) {
244 e = apr_bucket_eos_create(f->c->bucket_alloc);
245 APR_BRIGADE_INSERT_TAIL(b, e);
250 /* Have we read as much data as we wanted (be greedy)? */
251 if (len < readbytes) {
252 apr_size_t bucket_len;
255 /* We already registered the data in e in len */
256 e = APR_BUCKET_NEXT(e);
257 while ((len < readbytes) && (rv == APR_SUCCESS)
258 && (e != APR_BRIGADE_SENTINEL(ctx->b))) {
259 /* Check for the availability of buckets with known length */
260 if (e->length != -1) {
262 e = APR_BUCKET_NEXT(e);
266 * Read from bucket, but non blocking. If there isn't any
267 * more data, well than this is fine as well, we will
268 * not wait for more since we already got some and we are
269 * only checking if there isn't more.
271 rv = apr_bucket_read(e, &str, &bucket_len,
273 if (rv == APR_SUCCESS) {
275 e = APR_BUCKET_NEXT(e);
281 /* We can only return at most what we read. */
282 if (len < readbytes) {
286 rv = apr_brigade_partition(ctx->b, readbytes, &e);
287 if (rv != APR_SUCCESS) {
291 /* Must do move before CONCAT */
292 ctx->tmpbb = apr_brigade_split_ex(ctx->b, e, ctx->tmpbb);
294 if (mode == AP_MODE_READBYTES) {
295 APR_BRIGADE_CONCAT(b, ctx->b);
297 else if (mode == AP_MODE_SPECULATIVE) {
298 apr_bucket *copy_bucket;
300 for (e = APR_BRIGADE_FIRST(ctx->b);
301 e != APR_BRIGADE_SENTINEL(ctx->b);
302 e = APR_BUCKET_NEXT(e))
304 rv = apr_bucket_copy(e, ©_bucket);
305 if (rv != APR_SUCCESS) {
308 APR_BRIGADE_INSERT_TAIL(b, copy_bucket);
312 /* Take what was originally there and place it back on ctx->b */
313 APR_BRIGADE_CONCAT(ctx->b, ctx->tmpbb);
318 static void setaside_remaining_output(ap_filter_t *f,
319 core_output_filter_ctx_t *ctx,
320 apr_bucket_brigade *bb,
323 static apr_status_t send_brigade_nonblocking(apr_socket_t *s,
324 apr_bucket_brigade *bb,
325 apr_size_t *bytes_written,
328 static void remove_empty_buckets(apr_bucket_brigade *bb);
330 static apr_status_t send_brigade_blocking(apr_socket_t *s,
331 apr_bucket_brigade *bb,
332 apr_size_t *bytes_written,
335 static apr_status_t writev_nonblocking(apr_socket_t *s,
336 struct iovec *vec, apr_size_t nvec,
337 apr_bucket_brigade *bb,
338 apr_size_t *cumulative_bytes_written,
342 static apr_status_t sendfile_nonblocking(apr_socket_t *s,
344 apr_size_t *cumulative_bytes_written,
348 /* XXX: Should these be configurable parameters? */
349 #define THRESHOLD_MIN_WRITE 4096
350 #define THRESHOLD_MAX_BUFFER 65536
351 #define MAX_REQUESTS_IN_PIPELINE 5
353 /* Optional function coming from mod_logio, used for logging of output
356 extern APR_OPTIONAL_FN_TYPE(ap_logio_add_bytes_out) *ap__logio_add_bytes_out;
358 apr_status_t ap_core_output_filter(ap_filter_t *f, apr_bucket_brigade *new_bb)
361 core_net_rec *net = f->ctx;
362 core_output_filter_ctx_t *ctx = net->out_ctx;
363 apr_bucket_brigade *bb = NULL;
364 apr_bucket *bucket, *next, *flush_upto = NULL;
365 apr_size_t bytes_in_brigade, non_file_bytes_in_brigade;
366 int eor_buckets_in_brigade;
369 /* Fail quickly if the connection has already been aborted. */
371 if (new_bb != NULL) {
372 apr_brigade_cleanup(new_bb);
374 return APR_ECONNABORTED;
378 ctx = apr_pcalloc(c->pool, sizeof(*ctx));
379 net->out_ctx = (core_output_filter_ctx_t *)ctx;
380 rv = apr_socket_opt_set(net->client_socket, APR_SO_NONBLOCK, 1);
381 if (rv != APR_SUCCESS) {
385 * Need to create tmp brigade with correct lifetime. Passing
386 * NULL to apr_brigade_split_ex would result in a brigade
387 * allocated from bb->pool which might be wrong.
389 ctx->tmp_flush_bb = apr_brigade_create(c->pool, c->bucket_alloc);
390 /* same for buffered_bb and ap_save_brigade */
391 ctx->buffered_bb = apr_brigade_create(c->pool, c->bucket_alloc);
394 if (new_bb != NULL) {
395 for (bucket = APR_BRIGADE_FIRST(new_bb); bucket != APR_BRIGADE_SENTINEL(new_bb); bucket = APR_BUCKET_NEXT(bucket)) {
396 if (bucket->length > 0) {
397 ctx->bytes_in += bucket->length;
403 if ((ctx->buffered_bb != NULL) &&
404 !APR_BRIGADE_EMPTY(ctx->buffered_bb)) {
405 if (new_bb != NULL) {
406 APR_BRIGADE_PREPEND(bb, ctx->buffered_bb);
409 bb = ctx->buffered_bb;
411 c->data_in_output_filters = 0;
413 else if (new_bb == NULL) {
417 /* Scan through the brigade and decide whether to attempt a write,
418 * based on the following rules:
420 * 1) The new_bb is null: Do a nonblocking write of as much as
421 * possible: do a nonblocking write of as much data as possible,
422 * then save the rest in ctx->buffered_bb. (If new_bb == NULL,
423 * it probably means that the MPM is doing asynchronous write
424 * completion and has just determined that this connection
427 * 2) The brigade contains a flush bucket: Do a blocking write
428 * of everything up that point.
430 * 3) The request is in CONN_STATE_HANDLER state, and the brigade
431 * contains at least THRESHOLD_MAX_BUFFER bytes in non-file
432 * buckets: Do blocking writes until the amount of data in the
433 * buffer is less than THRESHOLD_MAX_BUFFER. (The point of this
434 * rule is to provide flow control, in case a handler is
435 * streaming out lots of data faster than the data can be
436 * sent to the client.)
438 * 4) The request is in CONN_STATE_HANDLER state, and the brigade
439 * contains at least MAX_REQUESTS_IN_PIPELINE EOR buckets:
440 * Do blocking writes until less than MAX_REQUESTS_IN_PIPELINE EOR
441 * buckets are left. (The point of this rule is to prevent too many
442 * FDs being kept open by pipelined requests, possibly allowing a
445 * 5) The brigade contains at least THRESHOLD_MIN_WRITE
446 * bytes: Do a nonblocking write of as much data as possible,
447 * then save the rest in ctx->buffered_bb.
450 if (new_bb == NULL) {
451 rv = send_brigade_nonblocking(net->client_socket, bb,
452 &(ctx->bytes_written), c);
453 if (APR_STATUS_IS_EAGAIN(rv)) {
456 else if (rv != APR_SUCCESS) {
457 /* The client has aborted the connection */
460 setaside_remaining_output(f, ctx, bb, c);
464 bytes_in_brigade = 0;
465 non_file_bytes_in_brigade = 0;
466 eor_buckets_in_brigade = 0;
467 for (bucket = APR_BRIGADE_FIRST(bb); bucket != APR_BRIGADE_SENTINEL(bb);
469 next = APR_BUCKET_NEXT(bucket);
471 if (!APR_BUCKET_IS_METADATA(bucket)) {
472 if (bucket->length == (apr_size_t)-1) {
475 /* XXX support nonblocking read here? */
476 rv = apr_bucket_read(bucket, &data, &length, APR_BLOCK_READ);
477 if (rv != APR_SUCCESS) {
480 /* reading may have split the bucket, so recompute next: */
481 next = APR_BUCKET_NEXT(bucket);
483 bytes_in_brigade += bucket->length;
484 if (!APR_BUCKET_IS_FILE(bucket)) {
485 non_file_bytes_in_brigade += bucket->length;
488 else if (AP_BUCKET_IS_EOR(bucket)) {
489 eor_buckets_in_brigade++;
492 if (APR_BUCKET_IS_FLUSH(bucket) ||
493 (non_file_bytes_in_brigade >= THRESHOLD_MAX_BUFFER) ||
494 (eor_buckets_in_brigade > MAX_REQUESTS_IN_PIPELINE) )
496 if (APLOGctrace6(c)) {
497 char *reason = APR_BUCKET_IS_FLUSH(bucket) ?
499 (non_file_bytes_in_brigade >= THRESHOLD_MAX_BUFFER) ?
500 "THRESHOLD_MAX_BUFFER" :
501 "MAX_REQUESTS_IN_PIPELINE";
502 ap_log_cerror(APLOG_MARK, APLOG_TRACE6, 0, c,
503 "core_output_filter: flushing because of %s",
507 * Defer the actual blocking write to avoid doing many writes.
511 bytes_in_brigade = 0;
512 non_file_bytes_in_brigade = 0;
513 eor_buckets_in_brigade = 0;
517 if (flush_upto != NULL) {
518 ctx->tmp_flush_bb = apr_brigade_split_ex(bb, flush_upto,
520 rv = send_brigade_blocking(net->client_socket, bb,
521 &(ctx->bytes_written), c);
522 if (rv != APR_SUCCESS) {
523 /* The client has aborted the connection */
527 APR_BRIGADE_CONCAT(bb, ctx->tmp_flush_bb);
530 if (bytes_in_brigade >= THRESHOLD_MIN_WRITE) {
531 rv = send_brigade_nonblocking(net->client_socket, bb,
532 &(ctx->bytes_written), c);
533 if ((rv != APR_SUCCESS) && (!APR_STATUS_IS_EAGAIN(rv))) {
534 /* The client has aborted the connection */
540 setaside_remaining_output(f, ctx, bb, c);
545 * This function assumes that either ctx->buffered_bb == NULL, or
546 * ctx->buffered_bb is empty, or ctx->buffered_bb == bb
548 static void setaside_remaining_output(ap_filter_t *f,
549 core_output_filter_ctx_t *ctx,
550 apr_bucket_brigade *bb,
556 remove_empty_buckets(bb);
557 if (!APR_BRIGADE_EMPTY(bb)) {
558 c->data_in_output_filters = 1;
559 if (bb != ctx->buffered_bb) {
560 if (!ctx->deferred_write_pool) {
561 apr_pool_create(&ctx->deferred_write_pool, c->pool);
562 apr_pool_tag(ctx->deferred_write_pool, "deferred_write");
564 ap_save_brigade(f, &(ctx->buffered_bb), &bb,
565 ctx->deferred_write_pool);
566 apr_brigade_cleanup(bb);
569 else if (ctx->deferred_write_pool) {
571 * There are no more requests in the pipeline. We can just clear the
574 apr_pool_clear(ctx->deferred_write_pool);
578 #ifndef APR_MAX_IOVEC_SIZE
579 #define MAX_IOVEC_TO_WRITE 16
581 #if APR_MAX_IOVEC_SIZE > 16
582 #define MAX_IOVEC_TO_WRITE 16
584 #define MAX_IOVEC_TO_WRITE APR_MAX_IOVEC_SIZE
588 static apr_status_t send_brigade_nonblocking(apr_socket_t *s,
589 apr_bucket_brigade *bb,
590 apr_size_t *bytes_written,
593 apr_bucket *bucket, *next;
595 struct iovec vec[MAX_IOVEC_TO_WRITE];
598 remove_empty_buckets(bb);
600 for (bucket = APR_BRIGADE_FIRST(bb);
601 bucket != APR_BRIGADE_SENTINEL(bb);
603 next = APR_BUCKET_NEXT(bucket);
605 if (APR_BUCKET_IS_FILE(bucket)) {
606 apr_bucket_file *file_bucket = (apr_bucket_file *)(bucket->data);
607 apr_file_t *fd = file_bucket->fd;
608 /* Use sendfile to send this file unless:
609 * - the platform doesn't support sendfile,
610 * - the file is too small for sendfile to be useful, or
611 * - sendfile is disabled in the httpd config via "EnableSendfile off"
614 if ((apr_file_flags_get(fd) & APR_SENDFILE_ENABLED) &&
615 (bucket->length >= AP_MIN_SENDFILE_BYTES)) {
617 (void)apr_socket_opt_set(s, APR_TCP_NOPUSH, 1);
618 rv = writev_nonblocking(s, vec, nvec, bb, bytes_written, c);
620 if (rv != APR_SUCCESS) {
621 (void)apr_socket_opt_set(s, APR_TCP_NOPUSH, 0);
625 rv = sendfile_nonblocking(s, bucket, bytes_written, c);
627 (void)apr_socket_opt_set(s, APR_TCP_NOPUSH, 0);
629 if (rv != APR_SUCCESS) {
635 #endif /* APR_HAS_SENDFILE */
636 /* didn't sendfile */
637 if (!APR_BUCKET_IS_METADATA(bucket)) {
640 rv = apr_bucket_read(bucket, &data, &length, APR_BLOCK_READ);
641 if (rv != APR_SUCCESS) {
644 /* reading may have split the bucket, so recompute next: */
645 next = APR_BUCKET_NEXT(bucket);
646 vec[nvec].iov_base = (char *)data;
647 vec[nvec].iov_len = length;
649 if (nvec == MAX_IOVEC_TO_WRITE) {
650 rv = writev_nonblocking(s, vec, nvec, bb, bytes_written, c);
652 if (rv != APR_SUCCESS) {
661 rv = writev_nonblocking(s, vec, nvec, bb, bytes_written, c);
662 if (rv != APR_SUCCESS) {
667 remove_empty_buckets(bb);
672 static void remove_empty_buckets(apr_bucket_brigade *bb)
675 while (((bucket = APR_BRIGADE_FIRST(bb)) != APR_BRIGADE_SENTINEL(bb)) &&
676 (APR_BUCKET_IS_METADATA(bucket) || (bucket->length == 0))) {
677 APR_BUCKET_REMOVE(bucket);
678 apr_bucket_destroy(bucket);
682 static apr_status_t send_brigade_blocking(apr_socket_t *s,
683 apr_bucket_brigade *bb,
684 apr_size_t *bytes_written,
690 while (!APR_BRIGADE_EMPTY(bb)) {
691 rv = send_brigade_nonblocking(s, bb, bytes_written, c);
692 if (rv != APR_SUCCESS) {
693 if (APR_STATUS_IS_EAGAIN(rv)) {
694 /* Wait until we can send more data */
696 apr_interval_time_t timeout;
697 apr_pollfd_t pollset;
700 pollset.desc_type = APR_POLL_SOCKET;
701 pollset.reqevents = APR_POLLOUT;
703 apr_socket_timeout_get(s, &timeout);
704 rv = apr_poll(&pollset, 1, &nsds, timeout);
705 if (rv != APR_SUCCESS) {
717 static apr_status_t writev_nonblocking(apr_socket_t *s,
718 struct iovec *vec, apr_size_t nvec,
719 apr_bucket_brigade *bb,
720 apr_size_t *cumulative_bytes_written,
723 apr_status_t rv = APR_SUCCESS, arv;
724 apr_size_t bytes_written = 0, bytes_to_write = 0;
725 apr_size_t i, offset;
726 apr_interval_time_t old_timeout;
728 arv = apr_socket_timeout_get(s, &old_timeout);
729 if (arv != APR_SUCCESS) {
732 arv = apr_socket_timeout_set(s, 0);
733 if (arv != APR_SUCCESS) {
737 for (i = 0; i < nvec; i++) {
738 bytes_to_write += vec[i].iov_len;
741 while (bytes_written < bytes_to_write) {
743 rv = apr_socket_sendv(s, vec + offset, nvec - offset, &n);
746 for (i = offset; i < nvec; ) {
747 apr_bucket *bucket = APR_BRIGADE_FIRST(bb);
748 if (APR_BUCKET_IS_METADATA(bucket)) {
749 APR_BUCKET_REMOVE(bucket);
750 apr_bucket_destroy(bucket);
752 else if (n >= vec[i].iov_len) {
753 APR_BUCKET_REMOVE(bucket);
754 apr_bucket_destroy(bucket);
756 n -= vec[i++].iov_len;
759 apr_bucket_split(bucket, n);
760 APR_BUCKET_REMOVE(bucket);
761 apr_bucket_destroy(bucket);
763 vec[i].iov_base = (char *) vec[i].iov_base + n;
768 if (rv != APR_SUCCESS) {
772 if ((ap__logio_add_bytes_out != NULL) && (bytes_written > 0)) {
773 ap__logio_add_bytes_out(c, bytes_written);
775 *cumulative_bytes_written += bytes_written;
777 arv = apr_socket_timeout_set(s, old_timeout);
778 if ((arv != APR_SUCCESS) && (rv == APR_SUCCESS)) {
788 static apr_status_t sendfile_nonblocking(apr_socket_t *s,
790 apr_size_t *cumulative_bytes_written,
793 apr_status_t rv = APR_SUCCESS;
794 apr_bucket_file *file_bucket;
796 apr_size_t file_length;
797 apr_off_t file_offset;
798 apr_size_t bytes_written = 0;
800 if (!APR_BUCKET_IS_FILE(bucket)) {
801 ap_log_error(APLOG_MARK, APLOG_ERR, rv, c->base_server,
802 "core_filter: sendfile_nonblocking: "
803 "this should never happen");
806 file_bucket = (apr_bucket_file *)(bucket->data);
807 fd = file_bucket->fd;
808 file_length = bucket->length;
809 file_offset = bucket->start;
811 if (bytes_written < file_length) {
812 apr_size_t n = file_length - bytes_written;
814 apr_interval_time_t old_timeout;
816 arv = apr_socket_timeout_get(s, &old_timeout);
817 if (arv != APR_SUCCESS) {
820 arv = apr_socket_timeout_set(s, 0);
821 if (arv != APR_SUCCESS) {
824 rv = apr_socket_sendfile(s, fd, NULL, &file_offset, &n, 0);
825 if (rv == APR_SUCCESS) {
829 arv = apr_socket_timeout_set(s, old_timeout);
830 if ((arv != APR_SUCCESS) && (rv == APR_SUCCESS)) {
834 if ((ap__logio_add_bytes_out != NULL) && (bytes_written > 0)) {
835 ap__logio_add_bytes_out(c, bytes_written);
837 *cumulative_bytes_written += bytes_written;
838 if ((bytes_written < file_length) && (bytes_written > 0)) {
839 apr_bucket_split(bucket, bytes_written);
840 APR_BUCKET_REMOVE(bucket);
841 apr_bucket_destroy(bucket);
843 else if (bytes_written == file_length) {
844 APR_BUCKET_REMOVE(bucket);
845 apr_bucket_destroy(bucket);