|
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 |
/*
|
|
Packit |
90a5c9 |
* simple hokey charset recoding configuration module
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* See mod_ebcdic and mod_charset for more thought-out examples. This
|
|
Packit |
90a5c9 |
* one is just so Jeff can learn how a module works and experiment with
|
|
Packit |
90a5c9 |
* basic character set recoding configuration.
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* !!!This is an extremely cheap ripoff of mod_charset.c from Russian Apache!!!
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#include "httpd.h"
|
|
Packit |
90a5c9 |
#include "http_config.h"
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#include "http_core.h"
|
|
Packit |
90a5c9 |
#include "http_log.h"
|
|
Packit |
90a5c9 |
#include "http_main.h"
|
|
Packit |
90a5c9 |
#include "http_protocol.h"
|
|
Packit |
90a5c9 |
#include "http_request.h"
|
|
Packit |
90a5c9 |
#include "util_charset.h"
|
|
Packit |
90a5c9 |
#include "apr_buckets.h"
|
|
Packit |
90a5c9 |
#include "util_filter.h"
|
|
Packit |
90a5c9 |
#include "apr_strings.h"
|
|
Packit |
90a5c9 |
#include "apr_lib.h"
|
|
Packit |
90a5c9 |
#include "apr_xlate.h"
|
|
Packit |
90a5c9 |
#define APR_WANT_STRFUNC
|
|
Packit |
90a5c9 |
#include "apr_want.h"
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#define OUTPUT_XLATE_BUF_SIZE (16*1024) /* size of translation buffer used on output */
|
|
Packit |
90a5c9 |
#define INPUT_XLATE_BUF_SIZE (8*1024) /* size of translation buffer used on input */
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#define XLATE_MIN_BUFF_LEFT 128 /* flush once there is no more than this much
|
|
Packit |
90a5c9 |
* space left in the translation buffer
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#define FATTEST_CHAR 8 /* we don't handle chars wider than this that straddle
|
|
Packit |
90a5c9 |
* two buckets
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* extended error status codes; this is used in addition to an apr_status_t to
|
|
Packit |
90a5c9 |
* track errors in the translation filter
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
typedef enum {
|
|
Packit |
90a5c9 |
EES_INIT = 0, /* no error info yet; value must be 0 for easy init */
|
|
Packit |
90a5c9 |
EES_LIMIT, /* built-in restriction encountered */
|
|
Packit |
90a5c9 |
EES_INCOMPLETE_CHAR, /* incomplete multi-byte char at end of content */
|
|
Packit |
90a5c9 |
EES_BUCKET_READ,
|
|
Packit |
90a5c9 |
EES_DOWNSTREAM, /* something bad happened in a filter below xlate */
|
|
Packit |
90a5c9 |
EES_BAD_INPUT /* input data invalid */
|
|
Packit |
90a5c9 |
} ees_t;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* registered name of the output translation filter */
|
|
Packit |
90a5c9 |
#define XLATEOUT_FILTER_NAME "XLATEOUT"
|
|
Packit |
90a5c9 |
/* registered name of input translation filter */
|
|
Packit |
90a5c9 |
#define XLATEIN_FILTER_NAME "XLATEIN"
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
typedef struct charset_dir_t {
|
|
Packit |
90a5c9 |
const char *charset_source; /* source encoding */
|
|
Packit |
90a5c9 |
const char *charset_default; /* how to ship on wire */
|
|
Packit |
90a5c9 |
/** module does ap_add_*_filter()? */
|
|
Packit |
90a5c9 |
enum {IA_INIT, IA_IMPADD, IA_NOIMPADD} implicit_add;
|
|
Packit |
90a5c9 |
/** treat all mimetypes as text? */
|
|
Packit |
90a5c9 |
enum {FX_INIT, FX_FORCE, FX_NOFORCE} force_xlate;
|
|
Packit |
90a5c9 |
} charset_dir_t;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* charset_filter_ctx_t is created for each filter instance; because the same
|
|
Packit |
90a5c9 |
* filter code is used for translating in both directions, we need this context
|
|
Packit |
90a5c9 |
* data to tell the filter which translation handle to use; it also can hold a
|
|
Packit |
90a5c9 |
* character which was split between buckets
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
typedef struct charset_filter_ctx_t {
|
|
Packit |
90a5c9 |
apr_xlate_t *xlate;
|
|
Packit |
90a5c9 |
int is_sb; /* single-byte translation? */
|
|
Packit |
90a5c9 |
charset_dir_t *dc;
|
|
Packit |
90a5c9 |
ees_t ees; /* extended error status */
|
|
Packit |
90a5c9 |
apr_size_t saved;
|
|
Packit |
90a5c9 |
char buf[FATTEST_CHAR]; /* we want to be able to build a complete char here */
|
|
Packit |
90a5c9 |
int ran; /* has filter instance run before? */
|
|
Packit |
90a5c9 |
int noop; /* should we pass brigades through unchanged? */
|
|
Packit |
90a5c9 |
char *tmp; /* buffer for input filtering */
|
|
Packit |
90a5c9 |
apr_bucket_brigade *bb; /* input buckets we couldn't finish translating */
|
|
Packit |
90a5c9 |
apr_bucket_brigade *tmpbb; /* used for passing downstream */
|
|
Packit |
90a5c9 |
} charset_filter_ctx_t;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* charset_req_t is available via r->request_config if any translation is
|
|
Packit |
90a5c9 |
* being performed
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
typedef struct charset_req_t {
|
|
Packit |
90a5c9 |
charset_dir_t *dc;
|
|
Packit |
90a5c9 |
charset_filter_ctx_t *output_ctx, *input_ctx;
|
|
Packit |
90a5c9 |
} charset_req_t;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
module AP_MODULE_DECLARE_DATA charset_lite_module;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static void *create_charset_dir_conf(apr_pool_t *p,char *dummy)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
charset_dir_t *dc = (charset_dir_t *)apr_pcalloc(p,sizeof(charset_dir_t));
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
return dc;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static void *merge_charset_dir_conf(apr_pool_t *p, void *basev, void *overridesv)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
charset_dir_t *a = (charset_dir_t *)apr_pcalloc (p, sizeof(charset_dir_t));
|
|
Packit |
90a5c9 |
charset_dir_t *base = (charset_dir_t *)basev,
|
|
Packit |
90a5c9 |
*over = (charset_dir_t *)overridesv;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* If it is defined in the current container, use it. Otherwise, use the one
|
|
Packit |
90a5c9 |
* from the enclosing container.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
a->charset_default =
|
|
Packit |
90a5c9 |
over->charset_default ? over->charset_default : base->charset_default;
|
|
Packit |
90a5c9 |
a->charset_source =
|
|
Packit |
90a5c9 |
over->charset_source ? over->charset_source : base->charset_source;
|
|
Packit |
90a5c9 |
a->implicit_add =
|
|
Packit |
90a5c9 |
over->implicit_add != IA_INIT ? over->implicit_add : base->implicit_add;
|
|
Packit |
90a5c9 |
a->force_xlate=
|
|
Packit |
90a5c9 |
over->force_xlate != FX_INIT ? over->force_xlate : base->force_xlate;
|
|
Packit |
90a5c9 |
return a;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* CharsetSourceEnc charset
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
static const char *add_charset_source(cmd_parms *cmd, void *in_dc,
|
|
Packit |
90a5c9 |
const char *name)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
charset_dir_t *dc = in_dc;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
dc->charset_source = name;
|
|
Packit |
90a5c9 |
return NULL;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* CharsetDefault charset
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
static const char *add_charset_default(cmd_parms *cmd, void *in_dc,
|
|
Packit |
90a5c9 |
const char *name)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
charset_dir_t *dc = in_dc;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
dc->charset_default = name;
|
|
Packit |
90a5c9 |
return NULL;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* CharsetOptions optionflag...
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
static const char *add_charset_options(cmd_parms *cmd, void *in_dc,
|
|
Packit |
90a5c9 |
const char *flag)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
charset_dir_t *dc = in_dc;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (!strcasecmp(flag, "ImplicitAdd")) {
|
|
Packit |
90a5c9 |
dc->implicit_add = IA_IMPADD;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else if (!strcasecmp(flag, "NoImplicitAdd")) {
|
|
Packit |
90a5c9 |
dc->implicit_add = IA_NOIMPADD;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else if (!strcasecmp(flag, "TranslateAllMimeTypes")) {
|
|
Packit |
90a5c9 |
dc->force_xlate = FX_FORCE;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else if (!strcasecmp(flag, "NoTranslateAllMimeTypes")) {
|
|
Packit |
90a5c9 |
dc->force_xlate = FX_NOFORCE;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else {
|
|
Packit |
90a5c9 |
return apr_pstrcat(cmd->temp_pool,
|
|
Packit |
90a5c9 |
"Invalid CharsetOptions option: ",
|
|
Packit |
90a5c9 |
flag,
|
|
Packit |
90a5c9 |
NULL);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
return NULL;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* find_code_page() is a fixup hook that checks if the module is
|
|
Packit |
90a5c9 |
* configured and the input or output potentially need to be translated.
|
|
Packit |
90a5c9 |
* If so, context is initialized for the filters.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
static int find_code_page(request_rec *r)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
charset_dir_t *dc = ap_get_module_config(r->per_dir_config,
|
|
Packit |
90a5c9 |
&charset_lite_module);
|
|
Packit |
90a5c9 |
charset_req_t *reqinfo;
|
|
Packit |
90a5c9 |
charset_filter_ctx_t *input_ctx, *output_ctx;
|
|
Packit |
90a5c9 |
apr_status_t rv;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
ap_log_rerror(APLOG_MARK, APLOG_TRACE3, 0, r,
|
|
Packit |
90a5c9 |
"uri: %s file: %s method: %d "
|
|
Packit |
90a5c9 |
"imt: %s flags: %s%s%s %s->%s",
|
|
Packit |
90a5c9 |
r->uri,
|
|
Packit |
90a5c9 |
r->filename ? r->filename : "(none)",
|
|
Packit |
90a5c9 |
r->method_number,
|
|
Packit |
90a5c9 |
r->content_type ? r->content_type : "(unknown)",
|
|
Packit |
90a5c9 |
r->main ? "S" : "", /* S if subrequest */
|
|
Packit |
90a5c9 |
r->prev ? "R" : "", /* R if redirect */
|
|
Packit |
90a5c9 |
r->proxyreq ? "P" : "", /* P if proxy */
|
|
Packit |
90a5c9 |
dc->charset_source, dc->charset_default);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* If we don't have a full directory configuration, bail out.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
if (!dc->charset_source || !dc->charset_default) {
|
|
Packit |
90a5c9 |
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01448)
|
|
Packit |
90a5c9 |
"incomplete configuration: src %s, dst %s",
|
|
Packit |
90a5c9 |
dc->charset_source ? dc->charset_source : "unspecified",
|
|
Packit |
90a5c9 |
dc->charset_default ? dc->charset_default : "unspecified");
|
|
Packit |
90a5c9 |
return DECLINED;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* catch proxy requests */
|
|
Packit |
90a5c9 |
if (r->proxyreq) {
|
|
Packit |
90a5c9 |
return DECLINED;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* mod_rewrite indicators */
|
|
Packit |
90a5c9 |
if (r->filename
|
|
Packit |
90a5c9 |
&& (!strncmp(r->filename, "redirect:", 9)
|
|
Packit |
90a5c9 |
|| !strncmp(r->filename, "gone:", 5)
|
|
Packit |
90a5c9 |
|| !strncmp(r->filename, "passthrough:", 12)
|
|
Packit |
90a5c9 |
|| !strncmp(r->filename, "forbidden:", 10))) {
|
|
Packit |
90a5c9 |
return DECLINED;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* no translation when server and network charsets are set to the same value */
|
|
Packit |
90a5c9 |
if (!strcasecmp(dc->charset_source, dc->charset_default)) {
|
|
Packit |
90a5c9 |
return DECLINED;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* Get storage for the request data and the output filter context.
|
|
Packit |
90a5c9 |
* We rarely need the input filter context, so allocate that separately.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
reqinfo = (charset_req_t *)apr_pcalloc(r->pool,
|
|
Packit |
90a5c9 |
sizeof(charset_req_t) +
|
|
Packit |
90a5c9 |
sizeof(charset_filter_ctx_t));
|
|
Packit |
90a5c9 |
output_ctx = (charset_filter_ctx_t *)(reqinfo + 1);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
reqinfo->dc = dc;
|
|
Packit |
90a5c9 |
output_ctx->dc = dc;
|
|
Packit |
90a5c9 |
output_ctx->tmpbb = apr_brigade_create(r->pool,
|
|
Packit |
90a5c9 |
r->connection->bucket_alloc);
|
|
Packit |
90a5c9 |
ap_set_module_config(r->request_config, &charset_lite_module, reqinfo);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
reqinfo->output_ctx = output_ctx;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
switch (r->method_number) {
|
|
Packit |
90a5c9 |
case M_PUT:
|
|
Packit |
90a5c9 |
case M_POST:
|
|
Packit |
90a5c9 |
/* Set up input translation. Note: A request body can be included
|
|
Packit |
90a5c9 |
* with the OPTIONS method, but for now we don't set up translation
|
|
Packit |
90a5c9 |
* of it.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
input_ctx = apr_pcalloc(r->pool, sizeof(charset_filter_ctx_t));
|
|
Packit |
90a5c9 |
input_ctx->bb = apr_brigade_create(r->pool,
|
|
Packit |
90a5c9 |
r->connection->bucket_alloc);
|
|
Packit |
90a5c9 |
input_ctx->tmp = apr_palloc(r->pool, INPUT_XLATE_BUF_SIZE);
|
|
Packit |
90a5c9 |
input_ctx->dc = dc;
|
|
Packit |
90a5c9 |
reqinfo->input_ctx = input_ctx;
|
|
Packit |
90a5c9 |
rv = apr_xlate_open(&input_ctx->xlate, dc->charset_source,
|
|
Packit |
90a5c9 |
dc->charset_default, r->pool);
|
|
Packit |
90a5c9 |
if (rv != APR_SUCCESS) {
|
|
Packit |
90a5c9 |
ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01449)
|
|
Packit |
90a5c9 |
"can't open translation %s->%s",
|
|
Packit |
90a5c9 |
dc->charset_default, dc->charset_source);
|
|
Packit |
90a5c9 |
return HTTP_INTERNAL_SERVER_ERROR;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
if (apr_xlate_sb_get(input_ctx->xlate, &input_ctx->is_sb) != APR_SUCCESS) {
|
|
Packit |
90a5c9 |
input_ctx->is_sb = 0;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
return DECLINED;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static int configured_in_list(request_rec *r, const char *filter_name,
|
|
Packit |
90a5c9 |
struct ap_filter_t *filter_list)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
struct ap_filter_t *filter = filter_list;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
while (filter) {
|
|
Packit |
90a5c9 |
if (!strcasecmp(filter_name, filter->frec->name)) {
|
|
Packit |
90a5c9 |
return 1;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
filter = filter->next;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
return 0;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static int configured_on_input(request_rec *r, const char *filter_name)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
return configured_in_list(r, filter_name, r->input_filters);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static int configured_on_output(request_rec *r, const char *filter_name)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
return configured_in_list(r, filter_name, r->output_filters);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* xlate_insert_filter() is a filter hook which decides whether or not
|
|
Packit |
90a5c9 |
* to insert a translation filter for the current request.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
static void xlate_insert_filter(request_rec *r)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
/* Hey... don't be so quick to use reqinfo->dc here; reqinfo may be NULL */
|
|
Packit |
90a5c9 |
charset_req_t *reqinfo = ap_get_module_config(r->request_config,
|
|
Packit |
90a5c9 |
&charset_lite_module);
|
|
Packit |
90a5c9 |
charset_dir_t *dc = ap_get_module_config(r->per_dir_config,
|
|
Packit |
90a5c9 |
&charset_lite_module);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (dc && (dc->implicit_add == IA_NOIMPADD)) {
|
|
Packit |
90a5c9 |
ap_log_rerror(APLOG_MARK, APLOG_TRACE6, 0, r,
|
|
Packit |
90a5c9 |
"xlate output filter not added implicitly because "
|
|
Packit |
90a5c9 |
"CharsetOptions included 'NoImplicitAdd'");
|
|
Packit |
90a5c9 |
return;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (reqinfo) {
|
|
Packit |
90a5c9 |
if (reqinfo->output_ctx && !configured_on_output(r, XLATEOUT_FILTER_NAME)) {
|
|
Packit |
90a5c9 |
ap_add_output_filter(XLATEOUT_FILTER_NAME, reqinfo->output_ctx, r,
|
|
Packit |
90a5c9 |
r->connection);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
ap_log_rerror(APLOG_MARK, APLOG_TRACE3, 0, r,
|
|
Packit |
90a5c9 |
"xlate output filter not added implicitly because %s",
|
|
Packit |
90a5c9 |
!reqinfo->output_ctx ?
|
|
Packit |
90a5c9 |
"no output configuration available" :
|
|
Packit |
90a5c9 |
"another module added the filter");
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (reqinfo->input_ctx && !configured_on_input(r, XLATEIN_FILTER_NAME)) {
|
|
Packit |
90a5c9 |
ap_add_input_filter(XLATEIN_FILTER_NAME, reqinfo->input_ctx, r,
|
|
Packit |
90a5c9 |
r->connection);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
ap_log_rerror(APLOG_MARK, APLOG_TRACE3, 0, r,
|
|
Packit |
90a5c9 |
"xlate input filter not added implicitly because %s",
|
|
Packit |
90a5c9 |
!reqinfo->input_ctx ?
|
|
Packit |
90a5c9 |
"no input configuration available" :
|
|
Packit |
90a5c9 |
"another module added the filter");
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* stuff that sucks that I know of:
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* bucket handling:
|
|
Packit |
90a5c9 |
* why create an eos bucket when we see it come down the stream? just send the one
|
|
Packit |
90a5c9 |
* passed as input... news flash: this will be fixed when xlate_out_filter() starts
|
|
Packit |
90a5c9 |
* using the more generic xlate_brigade()
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* translation mechanics:
|
|
Packit |
90a5c9 |
* we don't handle characters that straddle more than two buckets; an error
|
|
Packit |
90a5c9 |
* will be generated
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static apr_status_t send_bucket_downstream(ap_filter_t *f, apr_bucket *b)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
charset_filter_ctx_t *ctx = f->ctx;
|
|
Packit |
90a5c9 |
apr_status_t rv;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
APR_BRIGADE_INSERT_TAIL(ctx->tmpbb, b);
|
|
Packit |
90a5c9 |
rv = ap_pass_brigade(f->next, ctx->tmpbb);
|
|
Packit |
90a5c9 |
if (rv != APR_SUCCESS) {
|
|
Packit |
90a5c9 |
ctx->ees = EES_DOWNSTREAM;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
apr_brigade_cleanup(ctx->tmpbb);
|
|
Packit |
90a5c9 |
return rv;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* send_downstream() is passed the translated data; it puts it in a single-
|
|
Packit |
90a5c9 |
* bucket brigade and passes the brigade to the next filter
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
static apr_status_t send_downstream(ap_filter_t *f, const char *tmp, apr_size_t len)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
request_rec *r = f->r;
|
|
Packit |
90a5c9 |
conn_rec *c = r->connection;
|
|
Packit |
90a5c9 |
apr_bucket *b;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
b = apr_bucket_transient_create(tmp, len, c->bucket_alloc);
|
|
Packit |
90a5c9 |
return send_bucket_downstream(f, b);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static apr_status_t send_eos(ap_filter_t *f)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
request_rec *r = f->r;
|
|
Packit |
90a5c9 |
conn_rec *c = r->connection;
|
|
Packit |
90a5c9 |
apr_bucket_brigade *bb;
|
|
Packit |
90a5c9 |
apr_bucket *b;
|
|
Packit |
90a5c9 |
charset_filter_ctx_t *ctx = f->ctx;
|
|
Packit |
90a5c9 |
apr_status_t rv;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
bb = apr_brigade_create(r->pool, c->bucket_alloc);
|
|
Packit |
90a5c9 |
b = apr_bucket_eos_create(c->bucket_alloc);
|
|
Packit |
90a5c9 |
APR_BRIGADE_INSERT_TAIL(bb, b);
|
|
Packit |
90a5c9 |
rv = ap_pass_brigade(f->next, bb);
|
|
Packit |
90a5c9 |
if (rv != APR_SUCCESS) {
|
|
Packit |
90a5c9 |
ctx->ees = EES_DOWNSTREAM;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
return rv;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static apr_status_t set_aside_partial_char(charset_filter_ctx_t *ctx,
|
|
Packit |
90a5c9 |
const char *partial,
|
|
Packit |
90a5c9 |
apr_size_t partial_len)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
apr_status_t rv;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (sizeof(ctx->buf) > partial_len) {
|
|
Packit |
90a5c9 |
ctx->saved = partial_len;
|
|
Packit |
90a5c9 |
memcpy(ctx->buf, partial, partial_len);
|
|
Packit |
90a5c9 |
rv = APR_SUCCESS;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else {
|
|
Packit |
90a5c9 |
rv = APR_INCOMPLETE;
|
|
Packit |
90a5c9 |
ctx->ees = EES_LIMIT; /* we don't handle chars this wide which straddle
|
|
Packit |
90a5c9 |
* buckets
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
return rv;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static apr_status_t finish_partial_char(charset_filter_ctx_t *ctx,
|
|
Packit |
90a5c9 |
/* input buffer: */
|
|
Packit |
90a5c9 |
const char **cur_str,
|
|
Packit |
90a5c9 |
apr_size_t *cur_len,
|
|
Packit |
90a5c9 |
/* output buffer: */
|
|
Packit |
90a5c9 |
char **out_str,
|
|
Packit |
90a5c9 |
apr_size_t *out_len)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
apr_status_t rv;
|
|
Packit |
90a5c9 |
apr_size_t tmp_input_len;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* Keep adding bytes from the input string to the saved string until we
|
|
Packit |
90a5c9 |
* 1) finish the input char
|
|
Packit |
90a5c9 |
* 2) get an error
|
|
Packit |
90a5c9 |
* or 3) run out of bytes to add
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
do {
|
|
Packit |
90a5c9 |
ctx->buf[ctx->saved] = **cur_str;
|
|
Packit |
90a5c9 |
++ctx->saved;
|
|
Packit |
90a5c9 |
++*cur_str;
|
|
Packit |
90a5c9 |
--*cur_len;
|
|
Packit |
90a5c9 |
tmp_input_len = ctx->saved;
|
|
Packit |
90a5c9 |
rv = apr_xlate_conv_buffer(ctx->xlate,
|
|
Packit |
90a5c9 |
ctx->buf,
|
|
Packit |
90a5c9 |
&tmp_input_len,
|
|
Packit |
90a5c9 |
*out_str,
|
|
Packit |
90a5c9 |
out_len);
|
|
Packit |
90a5c9 |
} while (rv == APR_INCOMPLETE && *cur_len);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (rv == APR_SUCCESS) {
|
|
Packit |
90a5c9 |
ctx->saved = 0;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else {
|
|
Packit |
90a5c9 |
ctx->ees = EES_LIMIT; /* code isn't smart enough to handle chars
|
|
Packit |
90a5c9 |
* straddling more than two buckets
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
return rv;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static void log_xlate_error(ap_filter_t *f, apr_status_t rv)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
charset_filter_ctx_t *ctx = f->ctx;
|
|
Packit |
90a5c9 |
const char *msg;
|
|
Packit |
90a5c9 |
char msgbuf[100];
|
|
Packit |
90a5c9 |
apr_size_t len;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
switch(ctx->ees) {
|
|
Packit |
90a5c9 |
case EES_LIMIT:
|
|
Packit |
90a5c9 |
rv = 0;
|
|
Packit |
90a5c9 |
msg = APLOGNO(02193) "xlate filter - a built-in restriction was encountered";
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
case EES_BAD_INPUT:
|
|
Packit |
90a5c9 |
rv = 0;
|
|
Packit |
90a5c9 |
msg = APLOGNO(02194) "xlate filter - an input character was invalid";
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
case EES_BUCKET_READ:
|
|
Packit |
90a5c9 |
rv = 0;
|
|
Packit |
90a5c9 |
msg = APLOGNO(02195) "xlate filter - bucket read routine failed";
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
case EES_INCOMPLETE_CHAR:
|
|
Packit |
90a5c9 |
rv = 0;
|
|
Packit |
90a5c9 |
strcpy(msgbuf, APLOGNO(02196) "xlate filter - incomplete char at end of input - ");
|
|
Packit |
90a5c9 |
len = ctx->saved;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* We must ensure not to process more than what would fit in the
|
|
Packit |
90a5c9 |
* remaining of the destination buffer, including terminating NULL */
|
|
Packit |
90a5c9 |
if (len > (sizeof(msgbuf) - strlen(msgbuf) - 1) / 2)
|
|
Packit |
90a5c9 |
len = (sizeof(msgbuf) - strlen(msgbuf) - 1) / 2;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
ap_bin2hex(ctx->buf, len, msgbuf + strlen(msgbuf));
|
|
Packit |
90a5c9 |
msg = msgbuf;
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
case EES_DOWNSTREAM:
|
|
Packit |
90a5c9 |
msg = APLOGNO(02197) "xlate filter - an error occurred in a lower filter";
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
default:
|
|
Packit |
90a5c9 |
msg = APLOGNO(02198) "xlate filter - returning error";
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, f->r, APLOGNO(02997) "%s", msg);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* chk_filter_chain() is called once per filter instance; it tries to
|
|
Packit |
90a5c9 |
* determine if the current filter instance should be disabled because
|
|
Packit |
90a5c9 |
* its translation is incompatible with the translation of an existing
|
|
Packit |
90a5c9 |
* instance of the translate filter
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* Example bad scenario:
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* configured filter chain for the request:
|
|
Packit |
90a5c9 |
* INCLUDES XLATEOUT(8859-1->UTS-16)
|
|
Packit |
90a5c9 |
* configured filter chain for the subrequest:
|
|
Packit |
90a5c9 |
* XLATEOUT(8859-1->UTS-16)
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* When the subrequest is processed, the filter chain will be
|
|
Packit |
90a5c9 |
* XLATEOUT(8859-1->UTS-16) XLATEOUT(8859-1->UTS-16)
|
|
Packit |
90a5c9 |
* This makes no sense, so the instance of XLATEOUT added for the
|
|
Packit |
90a5c9 |
* subrequest will be noop-ed.
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* Example good scenario:
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* configured filter chain for the request:
|
|
Packit |
90a5c9 |
* INCLUDES XLATEOUT(8859-1->UTS-16)
|
|
Packit |
90a5c9 |
* configured filter chain for the subrequest:
|
|
Packit |
90a5c9 |
* XLATEOUT(IBM-1047->8859-1)
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* When the subrequest is processed, the filter chain will be
|
|
Packit |
90a5c9 |
* XLATEOUT(IBM-1047->8859-1) XLATEOUT(8859-1->UTS-16)
|
|
Packit |
90a5c9 |
* This makes sense, so the instance of XLATEOUT added for the
|
|
Packit |
90a5c9 |
* subrequest will be left alone and it will translate from
|
|
Packit |
90a5c9 |
* IBM-1047->8859-1.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
static void chk_filter_chain(ap_filter_t *f)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
ap_filter_t *curf;
|
|
Packit |
90a5c9 |
charset_filter_ctx_t *curctx, *last_xlate_ctx = NULL,
|
|
Packit |
90a5c9 |
*ctx = f->ctx;
|
|
Packit |
90a5c9 |
int output = !strcasecmp(f->frec->name, XLATEOUT_FILTER_NAME);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (ctx->noop) {
|
|
Packit |
90a5c9 |
return;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* walk the filter chain; see if it makes sense for our filter to
|
|
Packit |
90a5c9 |
* do any translation
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
curf = output ? f->r->output_filters : f->r->input_filters;
|
|
Packit |
90a5c9 |
while (curf) {
|
|
Packit |
90a5c9 |
if (!strcasecmp(curf->frec->name, f->frec->name) &&
|
|
Packit |
90a5c9 |
curf->ctx) {
|
|
Packit |
90a5c9 |
curctx = (charset_filter_ctx_t *)curf->ctx;
|
|
Packit |
90a5c9 |
if (!last_xlate_ctx) {
|
|
Packit |
90a5c9 |
last_xlate_ctx = curctx;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else {
|
|
Packit |
90a5c9 |
if (strcmp(last_xlate_ctx->dc->charset_default,
|
|
Packit |
90a5c9 |
curctx->dc->charset_source)) {
|
|
Packit |
90a5c9 |
/* incompatible translation
|
|
Packit |
90a5c9 |
* if our filter instance is incompatible with an instance
|
|
Packit |
90a5c9 |
* already in place, noop our instance
|
|
Packit |
90a5c9 |
* Notes:
|
|
Packit |
90a5c9 |
* . We are only willing to noop our own instance.
|
|
Packit |
90a5c9 |
* . It is possible to noop another instance which has not
|
|
Packit |
90a5c9 |
* yet run, but this is not currently implemented.
|
|
Packit |
90a5c9 |
* Hopefully it will not be needed.
|
|
Packit |
90a5c9 |
* . It is not possible to noop an instance which has
|
|
Packit |
90a5c9 |
* already run.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
if (last_xlate_ctx == f->ctx) {
|
|
Packit |
90a5c9 |
last_xlate_ctx->noop = 1;
|
|
Packit |
90a5c9 |
if (APLOGrtrace1(f->r)) {
|
|
Packit |
90a5c9 |
const char *symbol = output ? "->" : "<-";
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
ap_log_rerror(APLOG_MARK, APLOG_DEBUG,
|
|
Packit |
90a5c9 |
0, f->r, APLOGNO(01451)
|
|
Packit |
90a5c9 |
"%s %s - disabling "
|
|
Packit |
90a5c9 |
"translation %s%s%s; existing "
|
|
Packit |
90a5c9 |
"translation %s%s%s",
|
|
Packit |
90a5c9 |
f->r->uri ? "uri" : "file",
|
|
Packit |
90a5c9 |
f->r->uri ? f->r->uri : f->r->filename,
|
|
Packit |
90a5c9 |
last_xlate_ctx->dc->charset_source,
|
|
Packit |
90a5c9 |
symbol,
|
|
Packit |
90a5c9 |
last_xlate_ctx->dc->charset_default,
|
|
Packit |
90a5c9 |
curctx->dc->charset_source,
|
|
Packit |
90a5c9 |
symbol,
|
|
Packit |
90a5c9 |
curctx->dc->charset_default);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else {
|
|
Packit |
90a5c9 |
const char *symbol = output ? "->" : "<-";
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
ap_log_rerror(APLOG_MARK, APLOG_ERR,
|
|
Packit |
90a5c9 |
0, f->r, APLOGNO(01452)
|
|
Packit |
90a5c9 |
"chk_filter_chain() - can't disable "
|
|
Packit |
90a5c9 |
"translation %s%s%s; existing "
|
|
Packit |
90a5c9 |
"translation %s%s%s",
|
|
Packit |
90a5c9 |
last_xlate_ctx->dc->charset_source,
|
|
Packit |
90a5c9 |
symbol,
|
|
Packit |
90a5c9 |
last_xlate_ctx->dc->charset_default,
|
|
Packit |
90a5c9 |
curctx->dc->charset_source,
|
|
Packit |
90a5c9 |
symbol,
|
|
Packit |
90a5c9 |
curctx->dc->charset_default);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
curf = curf->next;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* xlate_brigade() is used to filter request and response bodies
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* we'll stop when one of the following occurs:
|
|
Packit |
90a5c9 |
* . we run out of buckets
|
|
Packit |
90a5c9 |
* . we run out of space in the output buffer
|
|
Packit |
90a5c9 |
* . we hit an error or metadata
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* inputs:
|
|
Packit |
90a5c9 |
* bb: brigade to process
|
|
Packit |
90a5c9 |
* buffer: storage to hold the translated characters
|
|
Packit |
90a5c9 |
* buffer_avail: size of buffer
|
|
Packit |
90a5c9 |
* (and a few more uninteresting parms)
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* outputs:
|
|
Packit |
90a5c9 |
* return value: APR_SUCCESS or some error code
|
|
Packit |
90a5c9 |
* bb: we've removed any buckets representing the
|
|
Packit |
90a5c9 |
* translated characters; the eos bucket, if
|
|
Packit |
90a5c9 |
* present, will be left in the brigade
|
|
Packit |
90a5c9 |
* buffer: filled in with translated characters
|
|
Packit |
90a5c9 |
* buffer_avail: updated with the bytes remaining
|
|
Packit |
90a5c9 |
* hit_eos: did we hit an EOS bucket?
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
static apr_status_t xlate_brigade(charset_filter_ctx_t *ctx,
|
|
Packit |
90a5c9 |
apr_bucket_brigade *bb,
|
|
Packit |
90a5c9 |
char *buffer,
|
|
Packit |
90a5c9 |
apr_size_t *buffer_avail,
|
|
Packit |
90a5c9 |
int *hit_eos)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
apr_bucket *b = NULL; /* set to NULL only to quiet some gcc */
|
|
Packit |
90a5c9 |
apr_bucket *consumed_bucket;
|
|
Packit |
90a5c9 |
const char *bucket;
|
|
Packit |
90a5c9 |
apr_size_t bytes_in_bucket; /* total bytes read from current bucket */
|
|
Packit |
90a5c9 |
apr_size_t bucket_avail; /* bytes left in current bucket */
|
|
Packit |
90a5c9 |
apr_status_t rv = APR_SUCCESS;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
*hit_eos = 0;
|
|
Packit |
90a5c9 |
bucket_avail = 0;
|
|
Packit |
90a5c9 |
consumed_bucket = NULL;
|
|
Packit |
90a5c9 |
while (1) {
|
|
Packit |
90a5c9 |
if (!bucket_avail) { /* no bytes left to process in the current bucket... */
|
|
Packit |
90a5c9 |
if (consumed_bucket) {
|
|
Packit |
90a5c9 |
apr_bucket_delete(consumed_bucket);
|
|
Packit |
90a5c9 |
consumed_bucket = NULL;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
b = APR_BRIGADE_FIRST(bb);
|
|
Packit |
90a5c9 |
if (b == APR_BRIGADE_SENTINEL(bb) ||
|
|
Packit |
90a5c9 |
APR_BUCKET_IS_METADATA(b)) {
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
rv = apr_bucket_read(b, &bucket, &bytes_in_bucket, APR_BLOCK_READ);
|
|
Packit |
90a5c9 |
if (rv != APR_SUCCESS) {
|
|
Packit |
90a5c9 |
ctx->ees = EES_BUCKET_READ;
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
bucket_avail = bytes_in_bucket;
|
|
Packit |
90a5c9 |
consumed_bucket = b; /* for axing when we're done reading it */
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
if (bucket_avail) {
|
|
Packit |
90a5c9 |
/* We've got data, so translate it. */
|
|
Packit |
90a5c9 |
if (ctx->saved) {
|
|
Packit |
90a5c9 |
/* Rats... we need to finish a partial character from the previous
|
|
Packit |
90a5c9 |
* bucket.
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* Strangely, finish_partial_char() increments the input buffer
|
|
Packit |
90a5c9 |
* pointer but does not increment the output buffer pointer.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
apr_size_t old_buffer_avail = *buffer_avail;
|
|
Packit |
90a5c9 |
rv = finish_partial_char(ctx,
|
|
Packit |
90a5c9 |
&bucket, &bucket_avail,
|
|
Packit |
90a5c9 |
&buffer, buffer_avail);
|
|
Packit |
90a5c9 |
buffer += old_buffer_avail - *buffer_avail;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else {
|
|
Packit |
90a5c9 |
apr_size_t old_buffer_avail = *buffer_avail;
|
|
Packit |
90a5c9 |
apr_size_t old_bucket_avail = bucket_avail;
|
|
Packit |
90a5c9 |
rv = apr_xlate_conv_buffer(ctx->xlate,
|
|
Packit |
90a5c9 |
bucket, &bucket_avail,
|
|
Packit |
90a5c9 |
buffer,
|
|
Packit |
90a5c9 |
buffer_avail);
|
|
Packit |
90a5c9 |
buffer += old_buffer_avail - *buffer_avail;
|
|
Packit |
90a5c9 |
bucket += old_bucket_avail - bucket_avail;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (rv == APR_INCOMPLETE) { /* partial character at end of input */
|
|
Packit |
90a5c9 |
/* We need to save the final byte(s) for next time; we can't
|
|
Packit |
90a5c9 |
* convert it until we look at the next bucket.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
rv = set_aside_partial_char(ctx, bucket, bucket_avail);
|
|
Packit |
90a5c9 |
bucket_avail = 0;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
if (rv != APR_SUCCESS) {
|
|
Packit |
90a5c9 |
/* bad input byte or partial char too big to store */
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
if (*buffer_avail < XLATE_MIN_BUFF_LEFT) {
|
|
Packit |
90a5c9 |
/* if any data remains in the current bucket, split there */
|
|
Packit |
90a5c9 |
if (bucket_avail) {
|
|
Packit |
90a5c9 |
apr_bucket_split(b, bytes_in_bucket - bucket_avail);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
apr_bucket_delete(b);
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (!APR_BRIGADE_EMPTY(bb)) {
|
|
Packit |
90a5c9 |
b = APR_BRIGADE_FIRST(bb);
|
|
Packit |
90a5c9 |
if (APR_BUCKET_IS_EOS(b)) {
|
|
Packit |
90a5c9 |
/* Leave the eos bucket in the brigade for reporting to
|
|
Packit |
90a5c9 |
* subsequent filters.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
*hit_eos = 1;
|
|
Packit |
90a5c9 |
if (ctx->saved) {
|
|
Packit |
90a5c9 |
/* Oops... we have a partial char from the previous bucket
|
|
Packit |
90a5c9 |
* that won't be completed because there's no more data.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
rv = APR_INCOMPLETE;
|
|
Packit |
90a5c9 |
ctx->ees = EES_INCOMPLETE_CHAR;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
return rv;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* xlate_out_filter() handles (almost) arbitrary conversions from one charset
|
|
Packit |
90a5c9 |
* to another...
|
|
Packit |
90a5c9 |
* translation is determined in the fixup hook (find_code_page), which is
|
|
Packit |
90a5c9 |
* where the filter's context data is set up... the context data gives us
|
|
Packit |
90a5c9 |
* the translation handle
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
static apr_status_t xlate_out_filter(ap_filter_t *f, apr_bucket_brigade *bb)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
charset_req_t *reqinfo = ap_get_module_config(f->r->request_config,
|
|
Packit |
90a5c9 |
&charset_lite_module);
|
|
Packit |
90a5c9 |
charset_dir_t *dc = ap_get_module_config(f->r->per_dir_config,
|
|
Packit |
90a5c9 |
&charset_lite_module);
|
|
Packit |
90a5c9 |
charset_filter_ctx_t *ctx = f->ctx;
|
|
Packit |
90a5c9 |
apr_bucket *dptr, *consumed_bucket;
|
|
Packit |
90a5c9 |
const char *cur_str;
|
|
Packit |
90a5c9 |
apr_size_t cur_len, cur_avail;
|
|
Packit |
90a5c9 |
char tmp[OUTPUT_XLATE_BUF_SIZE];
|
|
Packit |
90a5c9 |
apr_size_t space_avail;
|
|
Packit |
90a5c9 |
int done;
|
|
Packit |
90a5c9 |
apr_status_t rv = APR_SUCCESS;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (!ctx) {
|
|
Packit |
90a5c9 |
/* this is SetOutputFilter path; grab the preallocated context,
|
|
Packit |
90a5c9 |
* if any; note that if we decided not to do anything in an earlier
|
|
Packit |
90a5c9 |
* handler, we won't even have a reqinfo
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
if (reqinfo) {
|
|
Packit |
90a5c9 |
ctx = f->ctx = reqinfo->output_ctx;
|
|
Packit |
90a5c9 |
reqinfo->output_ctx = NULL; /* prevent SNAFU if user coded us twice
|
|
Packit |
90a5c9 |
* in the filter chain; we can't have two
|
|
Packit |
90a5c9 |
* instances using the same context
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
if (!ctx) { /* no idea how to translate; don't do anything */
|
|
Packit |
90a5c9 |
ctx = f->ctx = apr_pcalloc(f->r->pool, sizeof(charset_filter_ctx_t));
|
|
Packit |
90a5c9 |
ctx->dc = dc;
|
|
Packit |
90a5c9 |
ctx->noop = 1;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* Check the mime type to see if translation should be performed.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
if (!ctx->noop && ctx->xlate == NULL) {
|
|
Packit |
90a5c9 |
const char *mime_type = f->r->content_type;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (mime_type && (strncasecmp(mime_type, "text/", 5) == 0 ||
|
|
Packit |
90a5c9 |
#if APR_CHARSET_EBCDIC
|
|
Packit |
90a5c9 |
/* On an EBCDIC machine, be willing to translate mod_autoindex-
|
|
Packit |
90a5c9 |
* generated output. Otherwise, it doesn't look too cool.
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* XXX This isn't a perfect fix because this doesn't trigger us
|
|
Packit |
90a5c9 |
* to convert from the charset of the source code to ASCII. The
|
|
Packit |
90a5c9 |
* general solution seems to be to allow a generator to set an
|
|
Packit |
90a5c9 |
* indicator in the r specifying that the body is coded in the
|
|
Packit |
90a5c9 |
* implementation character set (i.e., the charset of the source
|
|
Packit |
90a5c9 |
* code). This would get several different types of documents
|
|
Packit |
90a5c9 |
* translated properly: mod_autoindex output, mod_status output,
|
|
Packit |
90a5c9 |
* mod_info output, hard-coded error documents, etc.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
strcmp(mime_type, DIR_MAGIC_TYPE) == 0 ||
|
|
Packit |
90a5c9 |
#endif
|
|
Packit |
90a5c9 |
strncasecmp(mime_type, "message/", 8) == 0 ||
|
|
Packit |
90a5c9 |
dc->force_xlate == FX_FORCE)) {
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
rv = apr_xlate_open(&ctx->xlate,
|
|
Packit |
90a5c9 |
dc->charset_default, dc->charset_source, f->r->pool);
|
|
Packit |
90a5c9 |
if (rv != APR_SUCCESS) {
|
|
Packit |
90a5c9 |
ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, f->r, APLOGNO(01453)
|
|
Packit |
90a5c9 |
"can't open translation %s->%s",
|
|
Packit |
90a5c9 |
dc->charset_source, dc->charset_default);
|
|
Packit |
90a5c9 |
ctx->noop = 1;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else {
|
|
Packit |
90a5c9 |
if (apr_xlate_sb_get(ctx->xlate, &ctx->is_sb) != APR_SUCCESS) {
|
|
Packit |
90a5c9 |
ctx->is_sb = 0;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else {
|
|
Packit |
90a5c9 |
ctx->noop = 1;
|
|
Packit |
90a5c9 |
if (mime_type) {
|
|
Packit |
90a5c9 |
ap_log_rerror(APLOG_MARK, APLOG_TRACE6, 0, f->r,
|
|
Packit |
90a5c9 |
"mime type is %s; no translation selected",
|
|
Packit |
90a5c9 |
mime_type);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
ap_log_rerror(APLOG_MARK, APLOG_TRACE6, 0, f->r,
|
|
Packit |
90a5c9 |
"xlate_out_filter() - "
|
|
Packit |
90a5c9 |
"charset_source: %s charset_default: %s",
|
|
Packit |
90a5c9 |
dc && dc->charset_source ? dc->charset_source : "(none)",
|
|
Packit |
90a5c9 |
dc && dc->charset_default ? dc->charset_default : "(none)");
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (!ctx->ran) { /* filter never ran before */
|
|
Packit |
90a5c9 |
chk_filter_chain(f);
|
|
Packit |
90a5c9 |
ctx->ran = 1;
|
|
Packit |
90a5c9 |
if (!ctx->noop && !ctx->is_sb) {
|
|
Packit |
90a5c9 |
/* We're not converting between two single-byte charsets, so unset
|
|
Packit |
90a5c9 |
* Content-Length since it is unlikely to remain the same.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
apr_table_unset(f->r->headers_out, "Content-Length");
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (ctx->noop) {
|
|
Packit |
90a5c9 |
return ap_pass_brigade(f->next, bb);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
dptr = APR_BRIGADE_FIRST(bb);
|
|
Packit |
90a5c9 |
done = 0;
|
|
Packit |
90a5c9 |
cur_len = 0;
|
|
Packit |
90a5c9 |
space_avail = sizeof(tmp);
|
|
Packit |
90a5c9 |
consumed_bucket = NULL;
|
|
Packit |
90a5c9 |
while (!done) {
|
|
Packit |
90a5c9 |
if (!cur_len) { /* no bytes left to process in the current bucket... */
|
|
Packit |
90a5c9 |
if (consumed_bucket) {
|
|
Packit |
90a5c9 |
apr_bucket_delete(consumed_bucket);
|
|
Packit |
90a5c9 |
consumed_bucket = NULL;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
if (dptr == APR_BRIGADE_SENTINEL(bb)) {
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
if (APR_BUCKET_IS_EOS(dptr)) {
|
|
Packit |
90a5c9 |
cur_len = -1; /* XXX yuck, but that tells us to send
|
|
Packit |
90a5c9 |
* eos down; when we minimize our bb construction
|
|
Packit |
90a5c9 |
* we'll fix this crap */
|
|
Packit |
90a5c9 |
if (ctx->saved) {
|
|
Packit |
90a5c9 |
/* Oops... we have a partial char from the previous bucket
|
|
Packit |
90a5c9 |
* that won't be completed because there's no more data.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
rv = APR_INCOMPLETE;
|
|
Packit |
90a5c9 |
ctx->ees = EES_INCOMPLETE_CHAR;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
if (APR_BUCKET_IS_METADATA(dptr)) {
|
|
Packit |
90a5c9 |
apr_bucket *metadata_bucket;
|
|
Packit |
90a5c9 |
metadata_bucket = dptr;
|
|
Packit |
90a5c9 |
dptr = APR_BUCKET_NEXT(dptr);
|
|
Packit |
90a5c9 |
APR_BUCKET_REMOVE(metadata_bucket);
|
|
Packit |
90a5c9 |
rv = send_bucket_downstream(f, metadata_bucket);
|
|
Packit |
90a5c9 |
if (rv != APR_SUCCESS) {
|
|
Packit |
90a5c9 |
done = 1;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
continue;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
rv = apr_bucket_read(dptr, &cur_str, &cur_len, APR_BLOCK_READ);
|
|
Packit |
90a5c9 |
if (rv != APR_SUCCESS) {
|
|
Packit |
90a5c9 |
ctx->ees = EES_BUCKET_READ;
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
consumed_bucket = dptr; /* for axing when we're done reading it */
|
|
Packit |
90a5c9 |
dptr = APR_BUCKET_NEXT(dptr); /* get ready for when we access the
|
|
Packit |
90a5c9 |
* next bucket */
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
/* Try to fill up our tmp buffer with translated data. */
|
|
Packit |
90a5c9 |
cur_avail = cur_len;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (cur_len) { /* maybe we just hit the end of a pipe (len = 0) ? */
|
|
Packit |
90a5c9 |
if (ctx->saved) {
|
|
Packit |
90a5c9 |
/* Rats... we need to finish a partial character from the previous
|
|
Packit |
90a5c9 |
* bucket.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
char *tmp_tmp;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
tmp_tmp = tmp + sizeof(tmp) - space_avail;
|
|
Packit |
90a5c9 |
rv = finish_partial_char(ctx,
|
|
Packit |
90a5c9 |
&cur_str, &cur_len,
|
|
Packit |
90a5c9 |
&tmp_tmp, &space_avail);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else {
|
|
Packit |
90a5c9 |
rv = apr_xlate_conv_buffer(ctx->xlate,
|
|
Packit |
90a5c9 |
cur_str, &cur_avail,
|
|
Packit |
90a5c9 |
tmp + sizeof(tmp) - space_avail, &space_avail);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* Update input ptr and len after consuming some bytes */
|
|
Packit |
90a5c9 |
cur_str += cur_len - cur_avail;
|
|
Packit |
90a5c9 |
cur_len = cur_avail;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (rv == APR_INCOMPLETE) { /* partial character at end of input */
|
|
Packit |
90a5c9 |
/* We need to save the final byte(s) for next time; we can't
|
|
Packit |
90a5c9 |
* convert it until we look at the next bucket.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
rv = set_aside_partial_char(ctx, cur_str, cur_len);
|
|
Packit |
90a5c9 |
cur_len = 0;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (rv != APR_SUCCESS) {
|
|
Packit |
90a5c9 |
/* bad input byte or partial char too big to store */
|
|
Packit |
90a5c9 |
done = 1;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (space_avail < XLATE_MIN_BUFF_LEFT) {
|
|
Packit |
90a5c9 |
/* It is time to flush, as there is not enough space left in the
|
|
Packit |
90a5c9 |
* current output buffer to bother with converting more data.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
rv = send_downstream(f, tmp, sizeof(tmp) - space_avail);
|
|
Packit |
90a5c9 |
if (rv != APR_SUCCESS) {
|
|
Packit |
90a5c9 |
done = 1;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* tmp is now empty */
|
|
Packit |
90a5c9 |
space_avail = sizeof(tmp);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (rv == APR_SUCCESS) {
|
|
Packit |
90a5c9 |
if (space_avail < sizeof(tmp)) { /* gotta write out what we converted */
|
|
Packit |
90a5c9 |
rv = send_downstream(f, tmp, sizeof(tmp) - space_avail);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
if (rv == APR_SUCCESS) {
|
|
Packit |
90a5c9 |
if (cur_len == -1) {
|
|
Packit |
90a5c9 |
rv = send_eos(f);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else {
|
|
Packit |
90a5c9 |
log_xlate_error(f, rv);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
return rv;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static apr_status_t xlate_in_filter(ap_filter_t *f, apr_bucket_brigade *bb,
|
|
Packit |
90a5c9 |
ap_input_mode_t mode, apr_read_type_e block,
|
|
Packit |
90a5c9 |
apr_off_t readbytes)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
apr_status_t rv;
|
|
Packit |
90a5c9 |
charset_req_t *reqinfo = ap_get_module_config(f->r->request_config,
|
|
Packit |
90a5c9 |
&charset_lite_module);
|
|
Packit |
90a5c9 |
charset_dir_t *dc = ap_get_module_config(f->r->per_dir_config,
|
|
Packit |
90a5c9 |
&charset_lite_module);
|
|
Packit |
90a5c9 |
charset_filter_ctx_t *ctx = f->ctx;
|
|
Packit |
90a5c9 |
apr_size_t buffer_size;
|
|
Packit |
90a5c9 |
int hit_eos;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* just get out of the way of things we don't want. */
|
|
Packit |
90a5c9 |
if (mode != AP_MODE_READBYTES) {
|
|
Packit |
90a5c9 |
return ap_get_brigade(f->next, bb, mode, block, readbytes);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (!ctx) {
|
|
Packit |
90a5c9 |
/* this is SetInputFilter path; grab the preallocated context,
|
|
Packit |
90a5c9 |
* if any; note that if we decided not to do anything in an earlier
|
|
Packit |
90a5c9 |
* handler, we won't even have a reqinfo
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
if (reqinfo) {
|
|
Packit |
90a5c9 |
ctx = f->ctx = reqinfo->input_ctx;
|
|
Packit |
90a5c9 |
reqinfo->input_ctx = NULL; /* prevent SNAFU if user coded us twice
|
|
Packit |
90a5c9 |
* in the filter chain; we can't have two
|
|
Packit |
90a5c9 |
* instances using the same context
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
if (!ctx) { /* no idea how to translate; don't do anything */
|
|
Packit |
90a5c9 |
ctx = f->ctx = apr_pcalloc(f->r->pool, sizeof(charset_filter_ctx_t));
|
|
Packit |
90a5c9 |
ctx->dc = dc;
|
|
Packit |
90a5c9 |
ctx->noop = 1;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
ap_log_rerror(APLOG_MARK, APLOG_TRACE6, 0, f->r,
|
|
Packit |
90a5c9 |
"xlate_in_filter() - "
|
|
Packit |
90a5c9 |
"charset_source: %s charset_default: %s",
|
|
Packit |
90a5c9 |
dc && dc->charset_source ? dc->charset_source : "(none)",
|
|
Packit |
90a5c9 |
dc && dc->charset_default ? dc->charset_default : "(none)");
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (!ctx->ran) { /* filter never ran before */
|
|
Packit |
90a5c9 |
chk_filter_chain(f);
|
|
Packit |
90a5c9 |
ctx->ran = 1;
|
|
Packit |
90a5c9 |
if (!ctx->noop && !ctx->is_sb
|
|
Packit |
90a5c9 |
&& apr_table_get(f->r->headers_in, "Content-Length")) {
|
|
Packit |
90a5c9 |
/* A Content-Length header is present, but it won't be valid after
|
|
Packit |
90a5c9 |
* conversion because we're not converting between two single-byte
|
|
Packit |
90a5c9 |
* charsets. This will affect most CGI scripts and may affect
|
|
Packit |
90a5c9 |
* some modules.
|
|
Packit |
90a5c9 |
* Content-Length can't be unset here because that would break
|
|
Packit |
90a5c9 |
* being able to read the request body.
|
|
Packit |
90a5c9 |
* Processing of chunked request bodies is not impacted by this
|
|
Packit |
90a5c9 |
* filter since the length was not declared anyway.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
ap_log_rerror(APLOG_MARK, APLOG_TRACE1, 0, f->r,
|
|
Packit |
90a5c9 |
"Request body length may change, resulting in "
|
|
Packit |
90a5c9 |
"misprocessing by some modules or scripts");
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (ctx->noop) {
|
|
Packit |
90a5c9 |
return ap_get_brigade(f->next, bb, mode, block, readbytes);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (APR_BRIGADE_EMPTY(ctx->bb)) {
|
|
Packit |
90a5c9 |
if ((rv = ap_get_brigade(f->next, bb, mode, block,
|
|
Packit |
90a5c9 |
readbytes)) != APR_SUCCESS) {
|
|
Packit |
90a5c9 |
return rv;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else {
|
|
Packit |
90a5c9 |
APR_BRIGADE_PREPEND(bb, ctx->bb); /* first use the leftovers */
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
buffer_size = INPUT_XLATE_BUF_SIZE;
|
|
Packit |
90a5c9 |
rv = xlate_brigade(ctx, bb, ctx->tmp, &buffer_size, &hit_eos);
|
|
Packit |
90a5c9 |
if (rv == APR_SUCCESS) {
|
|
Packit |
90a5c9 |
if (!hit_eos) {
|
|
Packit |
90a5c9 |
/* move anything leftover into our context for next time;
|
|
Packit |
90a5c9 |
* we don't currently "set aside" since the data came from
|
|
Packit |
90a5c9 |
* down below, but I suspect that for long-term we need to
|
|
Packit |
90a5c9 |
* do that
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
APR_BRIGADE_CONCAT(ctx->bb, bb);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
if (buffer_size < INPUT_XLATE_BUF_SIZE) { /* do we have output? */
|
|
Packit |
90a5c9 |
apr_bucket *e;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
e = apr_bucket_heap_create(ctx->tmp,
|
|
Packit |
90a5c9 |
INPUT_XLATE_BUF_SIZE - buffer_size,
|
|
Packit |
90a5c9 |
NULL, f->r->connection->bucket_alloc);
|
|
Packit |
90a5c9 |
/* make sure we insert at the head, because there may be
|
|
Packit |
90a5c9 |
* an eos bucket already there, and the eos bucket should
|
|
Packit |
90a5c9 |
* come after the data
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
APR_BRIGADE_INSERT_HEAD(bb, e);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else {
|
|
Packit |
90a5c9 |
/* XXX need to get some more data... what if the last brigade
|
|
Packit |
90a5c9 |
* we got had only the first byte of a multibyte char? we need
|
|
Packit |
90a5c9 |
* to grab more data from the network instead of returning an
|
|
Packit |
90a5c9 |
* empty brigade
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
/* If we have any metadata at the head of ctx->bb, go ahead and move it
|
|
Packit |
90a5c9 |
* onto the end of bb to be returned to our caller.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
if (!APR_BRIGADE_EMPTY(ctx->bb)) {
|
|
Packit |
90a5c9 |
apr_bucket *b = APR_BRIGADE_FIRST(ctx->bb);
|
|
Packit |
90a5c9 |
while (b != APR_BRIGADE_SENTINEL(ctx->bb)
|
|
Packit |
90a5c9 |
&& APR_BUCKET_IS_METADATA(b)) {
|
|
Packit |
90a5c9 |
APR_BUCKET_REMOVE(b);
|
|
Packit |
90a5c9 |
APR_BRIGADE_INSERT_TAIL(bb, b);
|
|
Packit |
90a5c9 |
b = APR_BRIGADE_FIRST(ctx->bb);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else {
|
|
Packit |
90a5c9 |
log_xlate_error(f, rv);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
return rv;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static const command_rec cmds[] =
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
AP_INIT_TAKE1("CharsetSourceEnc",
|
|
Packit |
90a5c9 |
add_charset_source,
|
|
Packit |
90a5c9 |
NULL,
|
|
Packit |
90a5c9 |
OR_FILEINFO,
|
|
Packit |
90a5c9 |
"source (html,cgi,ssi) file charset"),
|
|
Packit |
90a5c9 |
AP_INIT_TAKE1("CharsetDefault",
|
|
Packit |
90a5c9 |
add_charset_default,
|
|
Packit |
90a5c9 |
NULL,
|
|
Packit |
90a5c9 |
OR_FILEINFO,
|
|
Packit |
90a5c9 |
"name of default charset"),
|
|
Packit |
90a5c9 |
AP_INIT_ITERATE("CharsetOptions",
|
|
Packit |
90a5c9 |
add_charset_options,
|
|
Packit |
90a5c9 |
NULL,
|
|
Packit |
90a5c9 |
OR_FILEINFO,
|
|
Packit |
90a5c9 |
"valid options: ImplicitAdd, NoImplicitAdd, TranslateAllMimeTypes, "
|
|
Packit |
90a5c9 |
"NoTranslateAllMimeTypes"),
|
|
Packit |
90a5c9 |
{NULL}
|
|
Packit |
90a5c9 |
};
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static void charset_register_hooks(apr_pool_t *p)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
ap_hook_fixups(find_code_page, NULL, NULL, APR_HOOK_MIDDLE);
|
|
Packit |
90a5c9 |
ap_hook_insert_filter(xlate_insert_filter, NULL, NULL, APR_HOOK_REALLY_LAST);
|
|
Packit |
90a5c9 |
ap_register_output_filter(XLATEOUT_FILTER_NAME, xlate_out_filter, NULL,
|
|
Packit |
90a5c9 |
AP_FTYPE_RESOURCE);
|
|
Packit |
90a5c9 |
ap_register_input_filter(XLATEIN_FILTER_NAME, xlate_in_filter, NULL,
|
|
Packit |
90a5c9 |
AP_FTYPE_RESOURCE);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
AP_DECLARE_MODULE(charset_lite) =
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
STANDARD20_MODULE_STUFF,
|
|
Packit |
90a5c9 |
create_charset_dir_conf,
|
|
Packit |
90a5c9 |
merge_charset_dir_conf,
|
|
Packit |
90a5c9 |
NULL,
|
|
Packit |
90a5c9 |
NULL,
|
|
Packit |
90a5c9 |
cmds,
|
|
Packit |
90a5c9 |
charset_register_hooks
|
|
Packit |
90a5c9 |
};
|
|
Packit |
90a5c9 |
|