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 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 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 17 * under the License. 18 * ==================================================================== 19 */ 20 21#include <apr_pools.h> 22 23#include "serf.h" 24#include "serf_bucket_util.h" 25 26typedef struct { 27 apr_file_t *file; 28 29 serf_databuf_t databuf; 30 31} file_context_t; 32 33 34static apr_status_t file_reader(void *baton, apr_size_t bufsize, 35 char *buf, apr_size_t *len) 36{ 37 file_context_t *ctx = baton; 38 39 *len = bufsize; 40 return apr_file_read(ctx->file, buf, len); 41} 42 43serf_bucket_t *serf_bucket_file_create( 44 apr_file_t *file, 45 serf_bucket_alloc_t *allocator) 46{ 47 file_context_t *ctx; 48#if APR_HAS_MMAP 49 apr_finfo_t finfo; 50 const char *file_path; 51 52 /* See if we'd be better off mmap'ing this file instead. 53 * 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. 58 */ 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)) { 63 apr_status_t status; 64 apr_mmap_t *file_mmap; 65 status = apr_mmap_create(&file_mmap, file, 0, finfo.size, 66 APR_MMAP_READ, 67 serf_bucket_allocator_get_pool(allocator)); 68 69 if (status == APR_SUCCESS) { 70 return serf_bucket_mmap_create(file_mmap, allocator); 71 } 72 } 73#endif 74 75 /* Oh, well. */ 76 ctx = serf_bucket_mem_alloc(allocator, sizeof(*ctx)); 77 ctx->file = file; 78 79 serf_databuf_init(&ctx->databuf); 80 ctx->databuf.read = file_reader; 81 ctx->databuf.read_baton = ctx; 82 83 return serf_bucket_create(&serf_bucket_type_file, allocator, ctx); 84} 85 86static apr_status_t serf_file_read(serf_bucket_t *bucket, 87 apr_size_t requested, 88 const char **data, apr_size_t *len) 89{ 90 file_context_t *ctx = bucket->data; 91 92 return serf_databuf_read(&ctx->databuf, requested, data, len); 93} 94 95static apr_status_t serf_file_readline(serf_bucket_t *bucket, 96 int acceptable, int *found, 97 const char **data, apr_size_t *len) 98{ 99 file_context_t *ctx = bucket->data; 100 101 return serf_databuf_readline(&ctx->databuf, acceptable, found, data, len); 102} 103 104static apr_status_t serf_file_peek(serf_bucket_t *bucket, 105 const char **data, 106 apr_size_t *len) 107{ 108 file_context_t *ctx = bucket->data; 109 110 return serf_databuf_peek(&ctx->databuf, data, len); 111} 112 113const serf_bucket_type_t serf_bucket_type_file = { 114 "FILE", 115 serf_file_read, 116 serf_file_readline, 117 serf_default_read_iovec, 118 serf_default_read_for_sendfile, 119 serf_default_read_bucket, 120 serf_file_peek, 121 serf_default_destroy_and_data, 122}; 123