1 /* Copyright 2002-2004 Justin Erenkrantz and Greg Stein
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include <apr_pools.h>
19 #include "serf_bucket_util.h"
24 serf_databuf_t databuf;
29 static apr_status_t file_reader(void *baton, apr_size_t bufsize,
30 char *buf, apr_size_t *len)
32 file_context_t *ctx = baton;
35 return apr_file_read(ctx->file, buf, len);
38 serf_bucket_t *serf_bucket_file_create(
40 serf_bucket_alloc_t *allocator)
45 const char *file_path;
47 /* See if we'd be better off mmap'ing this file instead.
49 * Note that there is a failure case here that we purposely fall through:
50 * if a file is buffered, apr_mmap will reject it. However, on older
51 * versions of APR, we have no way of knowing this - but apr_mmap_create
52 * will check for this and return APR_EBADF.
54 apr_file_name_get(&file_path, file);
55 apr_stat(&finfo, file_path, APR_FINFO_SIZE,
56 serf_bucket_allocator_get_pool(allocator));
57 if (APR_MMAP_CANDIDATE(finfo.size)) {
59 apr_mmap_t *file_mmap;
60 status = apr_mmap_create(&file_mmap, file, 0, finfo.size,
62 serf_bucket_allocator_get_pool(allocator));
64 if (status == APR_SUCCESS) {
65 return serf_bucket_mmap_create(file_mmap, allocator);
71 ctx = serf_bucket_mem_alloc(allocator, sizeof(*ctx));
74 serf_databuf_init(&ctx->databuf);
75 ctx->databuf.read = file_reader;
76 ctx->databuf.read_baton = ctx;
78 return serf_bucket_create(&serf_bucket_type_file, allocator, ctx);
81 static apr_status_t serf_file_read(serf_bucket_t *bucket,
83 const char **data, apr_size_t *len)
85 file_context_t *ctx = bucket->data;
87 return serf_databuf_read(&ctx->databuf, requested, data, len);
90 static apr_status_t serf_file_readline(serf_bucket_t *bucket,
91 int acceptable, int *found,
92 const char **data, apr_size_t *len)
94 file_context_t *ctx = bucket->data;
96 return serf_databuf_readline(&ctx->databuf, acceptable, found, data, len);
99 static apr_status_t serf_file_peek(serf_bucket_t *bucket,
103 file_context_t *ctx = bucket->data;
105 return serf_databuf_peek(&ctx->databuf, data, len);
108 const serf_bucket_type_t serf_bucket_type_file = {
112 serf_default_read_iovec,
113 serf_default_read_for_sendfile,
114 serf_default_read_bucket,
116 serf_default_destroy_and_data,