Blame modules/examples/mod_case_filter.c

Packit 90a5c9
/* Licensed to the Apache Software Foundation (ASF) under one or more
Packit 90a5c9
 * contributor license agreements.  See the NOTICE file distributed with
Packit 90a5c9
 * this work for additional information regarding copyright ownership.
Packit 90a5c9
 * The ASF licenses this file to You under the Apache License, Version 2.0
Packit 90a5c9
 * (the "License"); you may not use this file except in compliance with
Packit 90a5c9
 * the License.  You may obtain a copy of the License at
Packit 90a5c9
 *
Packit 90a5c9
 *     http://www.apache.org/licenses/LICENSE-2.0
Packit 90a5c9
 *
Packit 90a5c9
 * Unless required by applicable law or agreed to in writing, software
Packit 90a5c9
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit 90a5c9
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 90a5c9
 * See the License for the specific language governing permissions and
Packit 90a5c9
 * limitations under the License.
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
#include "httpd.h"
Packit 90a5c9
#include "http_config.h"
Packit 90a5c9
#include "apr_buckets.h"
Packit 90a5c9
#include "apr_general.h"
Packit 90a5c9
#include "apr_lib.h"
Packit 90a5c9
#include "util_filter.h"
Packit 90a5c9
#include "http_request.h"
Packit 90a5c9
Packit 90a5c9
#include <ctype.h>
Packit 90a5c9
Packit 90a5c9
static const char s_szCaseFilterName[] = "CaseFilter";
Packit 90a5c9
module AP_MODULE_DECLARE_DATA case_filter_module;
Packit 90a5c9
Packit 90a5c9
typedef struct
Packit 90a5c9
{
Packit 90a5c9
    int bEnabled;
Packit 90a5c9
} CaseFilterConfig;
Packit 90a5c9
Packit 90a5c9
static void *CaseFilterCreateServerConfig(apr_pool_t *p, server_rec *s)
Packit 90a5c9
{
Packit 90a5c9
    CaseFilterConfig *pConfig = apr_pcalloc(p,sizeof *pConfig);
Packit 90a5c9
Packit 90a5c9
    pConfig->bEnabled = 0;
Packit 90a5c9
Packit 90a5c9
    return pConfig;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static void CaseFilterInsertFilter(request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    CaseFilterConfig *pConfig = ap_get_module_config(r->server->module_config,
Packit 90a5c9
                                                     &case_filter_module);
Packit 90a5c9
Packit 90a5c9
    if (!pConfig->bEnabled)
Packit 90a5c9
        return;
Packit 90a5c9
Packit 90a5c9
    ap_add_output_filter(s_szCaseFilterName, NULL, r, r->connection);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static apr_status_t CaseFilterOutFilter(ap_filter_t *f,
Packit 90a5c9
                                        apr_bucket_brigade *pbbIn)
Packit 90a5c9
{
Packit 90a5c9
    request_rec *r = f->r;
Packit 90a5c9
    conn_rec *c = r->connection;
Packit 90a5c9
    apr_bucket *pbktIn;
Packit 90a5c9
    apr_bucket_brigade *pbbOut;
Packit 90a5c9
Packit 90a5c9
    pbbOut = apr_brigade_create(r->pool, c->bucket_alloc);
Packit 90a5c9
    for (pbktIn = APR_BRIGADE_FIRST(pbbIn);
Packit 90a5c9
         pbktIn != APR_BRIGADE_SENTINEL(pbbIn);
Packit 90a5c9
         pbktIn = APR_BUCKET_NEXT(pbktIn))
Packit 90a5c9
    {
Packit 90a5c9
        const char *data;
Packit 90a5c9
        apr_size_t len;
Packit 90a5c9
        char *buf;
Packit 90a5c9
        apr_size_t n;
Packit 90a5c9
        apr_bucket *pbktOut;
Packit 90a5c9
Packit 90a5c9
        if (APR_BUCKET_IS_EOS(pbktIn)) {
Packit 90a5c9
            apr_bucket *pbktEOS = apr_bucket_eos_create(c->bucket_alloc);
Packit 90a5c9
            APR_BRIGADE_INSERT_TAIL(pbbOut, pbktEOS);
Packit 90a5c9
            continue;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        /* read */
Packit 90a5c9
        apr_bucket_read(pbktIn, &data, &len, APR_BLOCK_READ);
Packit 90a5c9
Packit 90a5c9
        /* write */
Packit 90a5c9
        buf = apr_bucket_alloc(len, c->bucket_alloc);
Packit 90a5c9
        for (n=0 ; n < len ; ++n) {
Packit 90a5c9
            buf[n] = apr_toupper(data[n]);
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        pbktOut = apr_bucket_heap_create(buf, len, apr_bucket_free,
Packit 90a5c9
                                         c->bucket_alloc);
Packit 90a5c9
        APR_BRIGADE_INSERT_TAIL(pbbOut, pbktOut);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* Q: is there any advantage to passing a brigade for each bucket?
Packit 90a5c9
     * A: obviously, it can cut down server resource consumption, if this
Packit 90a5c9
     * experimental module was fed a file of 4MB, it would be using 8MB for
Packit 90a5c9
     * the 'read' buckets and the 'write' buckets.
Packit 90a5c9
     *
Packit 90a5c9
     * Note it is more efficient to consume (destroy) each bucket as it's
Packit 90a5c9
     * processed above than to do a single cleanup down here.  In any case,
Packit 90a5c9
     * don't let our caller pass the same buckets to us, twice;
Packit 90a5c9
     */
Packit 90a5c9
    apr_brigade_cleanup(pbbIn);
Packit 90a5c9
    return ap_pass_brigade(f->next, pbbOut);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static const char *CaseFilterEnable(cmd_parms *cmd, void *dummy, int arg)
Packit 90a5c9
{
Packit 90a5c9
    CaseFilterConfig *pConfig = ap_get_module_config(cmd->server->module_config,
Packit 90a5c9
                                                     &case_filter_module);
Packit 90a5c9
    pConfig->bEnabled = arg;
Packit 90a5c9
Packit 90a5c9
    return NULL;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static const command_rec CaseFilterCmds[] =
Packit 90a5c9
{
Packit 90a5c9
    AP_INIT_FLAG("CaseFilter", CaseFilterEnable, NULL, RSRC_CONF,
Packit 90a5c9
                 "Run a case filter on this host"),
Packit 90a5c9
    { NULL }
Packit 90a5c9
};
Packit 90a5c9
Packit 90a5c9
static void CaseFilterRegisterHooks(apr_pool_t *p)
Packit 90a5c9
{
Packit 90a5c9
    ap_hook_insert_filter(CaseFilterInsertFilter, NULL, NULL, APR_HOOK_MIDDLE);
Packit 90a5c9
    ap_register_output_filter(s_szCaseFilterName, CaseFilterOutFilter, NULL,
Packit 90a5c9
                              AP_FTYPE_RESOURCE);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE_MODULE(case_filter) =
Packit 90a5c9
{
Packit 90a5c9
    STANDARD20_MODULE_STUFF,
Packit 90a5c9
    NULL,
Packit 90a5c9
    NULL,
Packit 90a5c9
    CaseFilterCreateServerConfig,
Packit 90a5c9
    NULL,
Packit 90a5c9
    CaseFilterCmds,
Packit 90a5c9
    CaseFilterRegisterHooks
Packit 90a5c9
};