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#include "apr_strings.h" 18#include "ap_config.h" 19#include "httpd.h" 20#include "http_config.h" 21#include "http_protocol.h" 22#include "http_log.h" 23#include "util_script.h" 24#include "http_main.h" 25#include "http_request.h" 26 27#include "mod_core.h" 28 29#define ASIS_MAGIC_TYPE "httpd/send-as-is" 30 31static int asis_handler(request_rec *r) 32{ 33 apr_file_t *f; 34 apr_status_t rv; 35 const char *location; 36 37 if (strcmp(r->handler, ASIS_MAGIC_TYPE) && strcmp(r->handler, "send-as-is")) { 38 return DECLINED; 39 } 40 41 r->allowed |= (AP_METHOD_BIT << M_GET); 42 if (r->method_number != M_GET) { 43 return DECLINED; 44 } 45 46 if (r->finfo.filetype == APR_NOFILE) { 47 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(01233) 48 "File does not exist: %s", r->filename); 49 return HTTP_NOT_FOUND; 50 } 51 52 if ((rv = apr_file_open(&f, r->filename, APR_READ, 53 APR_OS_DEFAULT, r->pool)) != APR_SUCCESS) { 54 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01234) 55 "file permissions deny server access: %s", r->filename); 56 return HTTP_FORBIDDEN; 57 } 58 59 ap_scan_script_header_err_ex(r, f, NULL, APLOG_MODULE_INDEX); 60 location = apr_table_get(r->headers_out, "Location"); 61 62 if (location && location[0] == '/' && 63 ((r->status == HTTP_OK) || ap_is_HTTP_REDIRECT(r->status))) { 64 65 apr_file_close(f); 66 67 /* Internal redirect -- fake-up a pseudo-request */ 68 r->status = HTTP_OK; 69 70 /* This redirect needs to be a GET no matter what the original 71 * method was. 72 */ 73 r->method = "GET"; 74 r->method_number = M_GET; 75 76 ap_internal_redirect_handler(location, r); 77 return OK; 78 } 79 80 if (!r->header_only) { 81 conn_rec *c = r->connection; 82 apr_bucket_brigade *bb; 83 apr_bucket *b; 84 apr_off_t pos = 0; 85 86 rv = apr_file_seek(f, APR_CUR, &pos); 87 if (rv != APR_SUCCESS) { 88 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01235) 89 "mod_asis: failed to find end-of-headers position " 90 "for %s", r->filename); 91 apr_file_close(f); 92 return HTTP_INTERNAL_SERVER_ERROR; 93 } 94 95 bb = apr_brigade_create(r->pool, c->bucket_alloc); 96 apr_brigade_insert_file(bb, f, pos, r->finfo.size - pos, r->pool); 97 98 b = apr_bucket_eos_create(c->bucket_alloc); 99 APR_BRIGADE_INSERT_TAIL(bb, b); 100 rv = ap_pass_brigade(r->output_filters, bb); 101 if (rv != APR_SUCCESS) { 102 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01236) 103 "mod_asis: ap_pass_brigade failed for file %s", r->filename); 104 return HTTP_INTERNAL_SERVER_ERROR; 105 } 106 } 107 else { 108 apr_file_close(f); 109 } 110 111 return OK; 112} 113 114static void register_hooks(apr_pool_t *p) 115{ 116 ap_hook_handler(asis_handler,NULL,NULL,APR_HOOK_MIDDLE); 117} 118 119AP_DECLARE_MODULE(asis) = 120{ 121 STANDARD20_MODULE_STUFF, 122 NULL, /* create per-directory config structure */ 123 NULL, /* merge per-directory config structures */ 124 NULL, /* create per-server config structure */ 125 NULL, /* merge per-server config structures */ 126 NULL, /* command apr_table_t */ 127 register_hooks /* register hooks */ 128}; 129