mod_charset_lite.c revision d025a1bfef81af5c3a1e784762bf27971114b6e3
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2000 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
*
* Portions of this software are based upon public domain software
* originally written at the National Center for Supercomputing Applications,
* University of Illinois, Urbana-Champaign.
*/
/*
* simple hokey charset recoding configuration module
*
* See mod_ebcdic and mod_charset for more thought-out examples. This
* one is just so Jeff can learn how a module works and experiment with
* basic character set recoding configuration.
*
* !!!This is an extremely cheap ripoff of mod_charset.c from Russian Apache!!!
*/
#include "httpd.h"
#include "http_config.h"
#include "http_core.h"
#include "http_log.h"
#include "http_main.h"
#include "http_protocol.h"
#include "http_request.h"
#include "util_charset.h"
#include "ap_buckets.h"
#include "util_filter.h"
#include "apr_strings.h"
#ifndef APACHE_XLATE
#endif
* space is left in the translation buffer
*/
* two buckets
*/
/* extended error status codes; this is used in addition to an apr_status_t to
* track errors in the translation filter
*/
typedef enum {
EES_INIT = 0, /* no error info yet; value must be 0 for easy init */
EES_LIMIT, /* built-in restriction encountered */
EES_INCOMPLETE_CHAR, /* incomplete multi-byte char at end of content */
EES_BAD_INPUT /* input data invalid */
} ees_t;
typedef struct charset_dir_t {
/** debug level; -1 means uninitialized, 0 means no debug */
int debug;
const char *charset_source; /* source encoding */
const char *charset_default; /* how to ship on wire */
/** module does ap_add_*_filter()? */
/* charset_filter_ctx_t is created for each filter instance; because the same
* filter code is used for translating in both directions, we need this context
* data to tell the filter which translation handle to use; it also can hold a
* character which was split between buckets
*/
typedef struct charset_filter_ctx_t {
int ran; /* has filter instance run before? */
int noop; /* should we pass brigades through unchanged? */
/* charset_req_t is available via r->request_config if any translation is
* being performed
*/
typedef struct charset_req_t {
/* debug level definitions */
* each request */
{
return dc;
}
{
/* If it is defined in the current container, use it. Otherwise, use the one
* from the enclosing container.
*/
a->debug =
a->charset_default =
a->charset_source =
a->implicit_add =
return a;
}
/* CharsetSourceEnc charset
*/
const char *name)
{
return NULL;
}
/* CharsetDefault charset
*/
const char *name)
{
return NULL;
}
/* CharsetOptions optionflag...
*/
const char *flag)
{
}
}
}
else {
"Invalid CharsetOptions option: ",
flag,
NULL);
}
return NULL;
}
/* find_code_page() is a fixup hook that decides if translation should be
* enabled; if so, it sets up request data for use by the filter registration
* hook so that it knows what to do
*/
static int find_code_page(request_rec *r)
{
const char *mime_type;
"uri: %s file: %s method: %d "
"imt: %s flags: %s%s%s %s->%s",
}
/* If we don't have a full directory configuration, bail out.
*/
"incomplete configuration: src %s, dst %s",
}
return DECLINED;
}
/* catch proxy requests */
/* mod_rewrite indicators */
/* If mime type isn't text or message, bail out.
*/
"mime type is %s; no translation selected",
}
return DECLINED;
}
"dc: %X charset_source: %s charset_default: %s",
(unsigned)dc,
}
/* Get storage for the request data and the output filter context.
* We rarely need the input filter context, so allocate that separately.
*/
sizeof(charset_req_t) +
sizeof(charset_filter_ctx_t));
if (rv != APR_SUCCESS) {
"can't open translation %s->%s",
return HTTP_INTERNAL_SERVER_ERROR;
}
switch (r->method_number) {
case M_PUT:
case M_POST:
/* Set up input translation. Note: A request body can be included
* with the OPTIONS method, but for now we don't set up translation
* of it.
*/
if (rv != APR_SUCCESS) {
"can't open translation %s->%s",
return HTTP_INTERNAL_SERVER_ERROR;
}
/* Can't delete this yet :( #ifdef OLD */
if (rv != APR_SUCCESS) {
"can't set content input translation");
return HTTP_INTERNAL_SERVER_ERROR;
}
/* #endif */
}
return DECLINED;
}
/* xlate_insert_filter() is a filter hook which decides whether or not
* to insert a translation filter for the current request.
*/
static void xlate_insert_filter(request_rec *r)
{
/* Hey... don't be so quick to use reqinfo->dc here; reqinfo may be NULL */
"xlate_insert_filter() - "
"dc: %X charset_source: %s charset_default: %s "
"reqinfo %p impadd %d",
(unsigned)dc,
}
if (reqinfo &&
reqinfo->output_ctx) {
r->connection);
}
#ifdef NOT_YET /* no input filters yet; we still rely on BUFF */
if (reqinfo &&
/* ap_add_input_filter(XLATEIN_FILTER_NAME, reqinfo->input_ctx, r); */
}
#endif
}
/* stuff that sucks that I know of:
*
* bucket handling:
* why create an eos bucket when we see it come down the stream? just send the one
* passed as input
*
* translation mechanics:
* we don't handle characters that straddle more than two buckets; an error
* will be generated
*/
/* send_downstream() is passed the translated data; it puts it in a single-
* bucket brigade and passes the brigade to the next filter
*/
{
ap_bucket *b;
AP_BRIGADE_INSERT_TAIL(bb, b);
}
{
ap_bucket *b;
b = ap_bucket_create_eos();
AP_BRIGADE_INSERT_TAIL(bb, b);
}
{
rv = APR_SUCCESS;
}
else {
rv = APR_INCOMPLETE;
* buckets
*/
}
return rv;
}
/* input buffer: */
const char **cur_str,
/* output buffer: */
char **out_str,
{
/* Keep adding bytes from the input string to the saved string until we
* 1) finish the input char
* 2) get an error
* or 3) run out of bytes to add
*/
do {
++*cur_str;
--*cur_len;
*out_str,
out_len);
if (rv == APR_SUCCESS) {
}
else {
* straddling more than two buckets
*/
}
return rv;
}
{
const char *msg;
char msgbuf[100];
int cur;
case EES_LIMIT:
msg = "xlate_filter() - a built-in restriction was encountered";
break;
case EES_BAD_INPUT:
msg = "xlate_filter() - an input character was invalid";
break;
case EES_BUCKET_READ:
msg = "xlate_filter() - bucket read routine failed";
break;
case EES_INCOMPLETE_CHAR:
cur = 0;
++cur;
}
break;
default:
msg = "xlate_filter() - returning error";
}
"%s", msg);
}
/* chk_filter_chain() is called once per filter instance; it tries to
* determine if the current filter instance should be disabled because
* its translation is incompatible with the translation of an existing
* instance of the translate filter
*
* Example bad scenario:
*
* configured filter chain for the request:
* INCLUDES XLATEOUT(8859-1->UTS-16)
* configured filter chain for the subrequest:
* XLATEOUT(8859-1->UTS-16)
*
* When the subrequest is processed, the filter chain will be
* XLATEOUT(8859-1->UTS-16) XLATEOUT(8859-1->UTS-16)
* This makes no sense, so the instance of XLATEOUT added for the
* subrequest will be noop-ed.
*
* Example good scenario:
*
* configured filter chain for the request:
* INCLUDES XLATEOUT(8859-1->UTS-16)
* configured filter chain for the subrequest:
* XLATEOUT(IBM-1047->8859-1)
*
* When the subrequest is processed, the filter chain will be
* XLATEOUT(IBM-1047->8859-1) XLATEOUT(8859-1->UTS-16)
* This makes sense, so the instance of XLATEOUT added for the
* subrequest will be left alone and it will translate from
* IBM-1047->8859-1.
*/
static void chk_filter_chain(ap_filter_t *f)
{
/* walk the filter chain; see if it makes sense for our filter to
* do any translation
*/
curf = f->r->output_filters;
while (curf) {
if (!last_xlate_ctx) {
}
else {
/* incompatible translation
* if our filter instance is incompatible with an instance
* already in place, noop our instance
* Notes:
* . We are only willing to noop our own instance.
* . It is possible to noop another instance which has not
* yet run, but this is not currently implemented.
* Hopefully it will not be needed.
* . It is not possible to noop an instance which has
* already run.
*/
if (last_xlate_ctx == f->ctx) {
if (debug >= DBGLVL_PMC) {
0, f->r,
"%s %s - disabling "
"translation %s->%s; existing "
"translation %s->%s",
}
}
else {
0, f->r,
"chk_filter_chain() - can't disable "
"translation %s->%s; existing "
"translation %s->%s",
}
break;
}
}
}
}
}
/* xlate_filter() handles (almost) arbitrary conversions from one charset
* to another...
* translation is determined in the fixup hook (find_code_page), which is
* where the filter's context data is set up... the context data gives us
* the translation handle
*/
{
const char *cur_str;
char tmp[XLATE_BUF_SIZE];
int done;
if (!ctx) { /* this is AddOutputFilter path */
}
else {
}
}
"xlate_filter() - "
"dc: %X charset_source: %s charset_default: %s",
(unsigned)dc,
}
chk_filter_chain(f);
}
}
done = 0;
cur_len = 0;
space_avail = sizeof(tmp);
while (!done) {
if (!cur_len) { /* no bytes left to process in the current bucket... */
if (consumed_bucket) {
}
done = 1;
break;
}
done = 1;
* eos down; when we minimize our bb construction
* we'll fix this crap */
/* Oops... we have a partial char from the previous bucket
* that won't be completed because there's no more data.
*/
rv = APR_INCOMPLETE;
}
break;
}
if (rv != APR_SUCCESS) {
done = 1;
break;
}
* next bucket */
}
/* Try to fill up our tmp buffer with translated data. */
if (cur_len) { /* maybe we just hit the end of a pipe (len = 0) ? */
/* Rats... we need to finish a partial character from the previous
* bucket.
*/
char *tmp_tmp;
&tmp_tmp, &space_avail);
}
else {
/* Update input ptr and len after consuming some bytes */
/* We need to save the final byte(s) for next time; we can't
* convert it until we look at the next bucket.
*/
cur_len = 0;
}
}
}
if (rv != APR_SUCCESS) {
/* bad input byte or partial char too big to store */
done = 1;
}
if (space_avail < XLATE_MIN_BUFF_LEFT) {
/* It is time to flush, as there is not enough space left in the
* current output buffer to bother with converting more data.
*/
if (rv != APR_SUCCESS) {
done = 1;
}
/* tmp is now empty */
space_avail = sizeof(tmp);
}
}
if (rv == APR_SUCCESS) {
}
}
if (rv == APR_SUCCESS) {
if (cur_len == AP_END_OF_BRIGADE) {
}
}
else {
log_xlate_error(f, rv);
}
return rv;
}
static const command_rec cmds[] =
{
AP_INIT_TAKE1("CharsetSourceEnc",
NULL,
"source (html,cgi,ssi) file charset"),
AP_INIT_TAKE1("CharsetDefault",
NULL,
"name of default charset"),
AP_INIT_ITERATE("CharsetOptions",
NULL,
"valid options: ImplicitAdd, NoImplicitAdd, DebugLevel=n"),
{NULL}
};
static void charset_register_hooks(void)
{
}
{
NULL,
NULL,
cmds,
NULL,
};