mod_asis.c revision 2d4ea77e21ef7f3707d2c27f6117cc0a36e99b83
151b190e79e64824552e01849352ca8f6ac7dedbLennart Poettering/* Licensed to the Apache Software Foundation (ASF) under one or more
151b190e79e64824552e01849352ca8f6ac7dedbLennart Poettering * contributor license agreements. See the NOTICE file distributed with
151b190e79e64824552e01849352ca8f6ac7dedbLennart Poettering * this work for additional information regarding copyright ownership.
5430f7f2bc7330f3088b894166bf3524a067e3d8Lennart Poettering * The ASF licenses this file to You under the Apache License, Version 2.0
5430f7f2bc7330f3088b894166bf3524a067e3d8Lennart Poettering * (the "License"); you may not use this file except in compliance with
151b190e79e64824552e01849352ca8f6ac7dedbLennart Poettering * the License. You may obtain a copy of the License at
151b190e79e64824552e01849352ca8f6ac7dedbLennart Poettering *
151b190e79e64824552e01849352ca8f6ac7dedbLennart Poettering * http://www.apache.org/licenses/LICENSE-2.0
151b190e79e64824552e01849352ca8f6ac7dedbLennart Poettering *
9393a8774c1acd60deea40007061b9ffc783bf7eLennart Poettering * Unless required by applicable law or agreed to in writing, software
49dbfa7b2b0bf3906704dac1eaeb4eba91056a19Lennart Poettering * distributed under the License is distributed on an "AS IS" BASIS,
151b190e79e64824552e01849352ca8f6ac7dedbLennart Poettering * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
151b190e79e64824552e01849352ca8f6ac7dedbLennart Poettering * See the License for the specific language governing permissions and
d6bc8348d5be8576a475ac8ced2b0146e60fb71fDaniel Buch * limitations under the License.
151b190e79e64824552e01849352ca8f6ac7dedbLennart Poettering */
41807efb1594ae8e71e0255e154ea7d17be2251aLennart Poettering
9393a8774c1acd60deea40007061b9ffc783bf7eLennart Poettering#include "apr_strings.h"
adf70c5e4e6673353b8d702baee4e1d21eb05dfeKay Sievers#include "ap_config.h"
adf70c5e4e6673353b8d702baee4e1d21eb05dfeKay Sievers#include "httpd.h"
adf70c5e4e6673353b8d702baee4e1d21eb05dfeKay Sievers#include "http_config.h"
adf70c5e4e6673353b8d702baee4e1d21eb05dfeKay Sievers#include "http_protocol.h"
151b190e79e64824552e01849352ca8f6ac7dedbLennart Poettering#include "http_log.h"
151b190e79e64824552e01849352ca8f6ac7dedbLennart Poettering#include "util_script.h"
151b190e79e64824552e01849352ca8f6ac7dedbLennart Poettering#include "http_main.h"
151b190e79e64824552e01849352ca8f6ac7dedbLennart Poettering#include "http_request.h"
151b190e79e64824552e01849352ca8f6ac7dedbLennart Poettering
b53374f99b619abd90a7c19d69d37a3298b084d5Zbigniew Jędrzejewski-Szmek#include "mod_core.h"
#define ASIS_MAGIC_TYPE "httpd/send-as-is"
static int asis_handler(request_rec *r)
{
conn_rec *c = r->connection;
apr_file_t *f = NULL;
apr_status_t rv;
const char *location;
if(strcmp(r->handler,ASIS_MAGIC_TYPE) && strcmp(r->handler,"send-as-is"))
return DECLINED;
r->allowed |= (AP_METHOD_BIT << M_GET);
if (r->method_number != M_GET)
return DECLINED;
if (r->finfo.filetype == APR_NOFILE) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
"File does not exist: %s", r->filename);
return HTTP_NOT_FOUND;
}
if ((rv = apr_file_open(&f, r->filename, APR_READ,
APR_OS_DEFAULT, r->pool)) != APR_SUCCESS) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
"file permissions deny server access: %s", r->filename);
return HTTP_FORBIDDEN;
}
ap_scan_script_header_err(r, f, NULL);
location = apr_table_get(r->headers_out, "Location");
if (location && location[0] == '/' &&
((r->status == HTTP_OK) || ap_is_HTTP_REDIRECT(r->status))) {
apr_file_close(f);
/* Internal redirect -- fake-up a pseudo-request */
r->status = HTTP_OK;
/* This redirect needs to be a GET no matter what the original
* method was.
*/
r->method = apr_pstrdup(r->pool, "GET");
r->method_number = M_GET;
ap_internal_redirect_handler(location, r);
return OK;
}
if (!r->header_only) {
apr_bucket_brigade *bb;
apr_bucket *b;
apr_off_t pos = 0;
rv = apr_file_seek(f, APR_CUR, &pos);
if (rv != APR_SUCCESS) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
"mod_asis: failed to find end-of-headers position "
"for %s", r->filename);
apr_file_close(f);
return HTTP_INTERNAL_SERVER_ERROR;
}
bb = apr_brigade_create(r->pool, c->bucket_alloc);
apr_brigade_insert_file(bb, f, pos, r->finfo.size - pos, r->pool);
b = apr_bucket_eos_create(c->bucket_alloc);
APR_BRIGADE_INSERT_TAIL(bb, b);
rv = ap_pass_brigade(r->output_filters, bb);
if (rv != APR_SUCCESS) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
"mod_asis: ap_pass_brigade failed for file %s", r->filename);
return HTTP_INTERNAL_SERVER_ERROR;
}
}
else {
apr_file_close(f);
}
return OK;
}
static void register_hooks(apr_pool_t *p)
{
ap_hook_handler(asis_handler,NULL,NULL,APR_HOOK_MIDDLE);
}
AP_DECLARE_MODULE(asis) =
{
STANDARD20_MODULE_STUFF,
NULL, /* create per-directory config structure */
NULL, /* merge per-directory config structures */
NULL, /* create per-server config structure */
NULL, /* merge per-server config structures */
NULL, /* command apr_table_t */
register_hooks /* register hooks */
};