1 /* ====================================================================
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
18 * ====================================================================
21 #include <apr_pools.h>
24 #include "serf_bucket_util.h"
29 serf_databuf_t databuf;
34 static apr_status_t file_reader(void *baton, apr_size_t bufsize,
35 char *buf, apr_size_t *len)
37 file_context_t *ctx = baton;
40 return apr_file_read(ctx->file, buf, len);
43 serf_bucket_t *serf_bucket_file_create(
45 serf_bucket_alloc_t *allocator)
50 const char *file_path;
52 /* See if we'd be better off mmap'ing this file instead.
54 * Note that there is a failure case here that we purposely fall through:
55 * if a file is buffered, apr_mmap will reject it. However, on older
56 * versions of APR, we have no way of knowing this - but apr_mmap_create
57 * will check for this and return APR_EBADF.
59 apr_file_name_get(&file_path, file);
60 apr_stat(&finfo, file_path, APR_FINFO_SIZE,
61 serf_bucket_allocator_get_pool(allocator));
62 if (APR_MMAP_CANDIDATE(finfo.size)) {
64 apr_mmap_t *file_mmap;
65 status = apr_mmap_create(&file_mmap, file, 0, finfo.size,
67 serf_bucket_allocator_get_pool(allocator));
69 if (status == APR_SUCCESS) {
70 return serf_bucket_mmap_create(file_mmap, allocator);
76 ctx = serf_bucket_mem_alloc(allocator, sizeof(*ctx));
79 serf_databuf_init(&ctx->databuf);
80 ctx->databuf.read = file_reader;
81 ctx->databuf.read_baton = ctx;
83 return serf_bucket_create(&serf_bucket_type_file, allocator, ctx);
86 static apr_status_t serf_file_read(serf_bucket_t *bucket,
88 const char **data, apr_size_t *len)
90 file_context_t *ctx = bucket->data;
92 return serf_databuf_read(&ctx->databuf, requested, data, len);
95 static apr_status_t serf_file_readline(serf_bucket_t *bucket,
96 int acceptable, int *found,
97 const char **data, apr_size_t *len)
99 file_context_t *ctx = bucket->data;
101 return serf_databuf_readline(&ctx->databuf, acceptable, found, data, len);
104 static apr_status_t serf_file_peek(serf_bucket_t *bucket,
108 file_context_t *ctx = bucket->data;
110 return serf_databuf_peek(&ctx->databuf, data, len);
113 const serf_bucket_type_t serf_bucket_type_file = {
117 serf_default_read_iovec,
118 serf_default_read_for_sendfile,
119 serf_default_read_bucket,
121 serf_default_destroy_and_data,