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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
18
* An example input filter - this converts input to upper case. Note that
19
* because of the moment it gets inserted it does NOT convert request headers.
23
#include "http_config.h"
24
#include "apr_buckets.h"
25
#include "apr_general.h"
27
#include "util_filter.h"
28
#include "http_request.h"
32
static const char s_szCaseFilterName[] = "CaseFilterIn";
33
module AP_MODULE_DECLARE_DATA case_filter_in_module;
42
apr_bucket_brigade *pbbTmp;
43
} CaseFilterInContext;
45
static void *CaseFilterInCreateServerConfig(apr_pool_t *p, server_rec *s)
47
CaseFilterInConfig *pConfig = apr_pcalloc(p, sizeof *pConfig);
49
pConfig->bEnabled = 0;
54
static void CaseFilterInInsertFilter(request_rec *r)
56
CaseFilterInConfig *pConfig=ap_get_module_config(r->server->module_config,
57
&case_filter_in_module);
58
if(!pConfig->bEnabled)
61
ap_add_input_filter(s_szCaseFilterName,NULL,r,r->connection);
64
static apr_status_t CaseFilterInFilter(ap_filter_t *f,
65
apr_bucket_brigade *pbbOut,
66
ap_input_mode_t eMode,
67
apr_read_type_e eBlock,
70
request_rec *r = f->r;
71
conn_rec *c = r->connection;
72
CaseFilterInContext *pCtx;
75
if (!(pCtx = f->ctx)) {
76
f->ctx = pCtx = apr_palloc(r->pool, sizeof *pCtx);
77
pCtx->pbbTmp = apr_brigade_create(r->pool, c->bucket_alloc);
80
if (APR_BRIGADE_EMPTY(pCtx->pbbTmp)) {
81
ret = ap_get_brigade(f->next, pCtx->pbbTmp, eMode, eBlock, nBytes);
83
if (eMode == AP_MODE_EATCRLF || ret != APR_SUCCESS)
87
while(!APR_BRIGADE_EMPTY(pCtx->pbbTmp)) {
88
apr_bucket *pbktIn = APR_BRIGADE_FIRST(pCtx->pbbTmp);
95
/* It is tempting to do this...
96
* APR_BUCKET_REMOVE(pB);
97
* APR_BRIGADE_INSERT_TAIL(pbbOut,pB);
98
* and change the case of the bucket data, but that would be wrong
99
* for a file or socket buffer, for example...
102
if(APR_BUCKET_IS_EOS(pbktIn)) {
103
APR_BUCKET_REMOVE(pbktIn);
104
APR_BRIGADE_INSERT_TAIL(pbbOut, pbktIn);
108
ret=apr_bucket_read(pbktIn, &data, &len, eBlock);
109
if(ret != APR_SUCCESS)
113
for(n=0 ; n < len ; ++n)
114
buf[n] = apr_toupper(data[n]);
116
pbktOut = apr_bucket_heap_create(buf, len, 0, c->bucket_alloc);
117
APR_BRIGADE_INSERT_TAIL(pbbOut, pbktOut);
118
apr_bucket_delete(pbktIn);
125
static const char *CaseFilterInEnable(cmd_parms *cmd, void *dummy, int arg)
127
CaseFilterInConfig *pConfig
128
= ap_get_module_config(cmd->server->module_config,
129
&case_filter_in_module);
130
pConfig->bEnabled=arg;
135
static const command_rec CaseFilterInCmds[] =
137
AP_INIT_FLAG("CaseFilterIn", CaseFilterInEnable, NULL, RSRC_CONF,
138
"Run an input case filter on this host"),
143
static void CaseFilterInRegisterHooks(apr_pool_t *p)
145
ap_hook_insert_filter(CaseFilterInInsertFilter, NULL, NULL,
147
ap_register_input_filter(s_szCaseFilterName, CaseFilterInFilter, NULL,
151
module AP_MODULE_DECLARE_DATA case_filter_in_module =
153
STANDARD20_MODULE_STUFF,
156
CaseFilterInCreateServerConfig,
159
CaseFilterInRegisterHooks