util_script.c revision 070d2f9fd52a3f0e45ca7ae1235c5a5755b80d24
6de8046f8f7e07cd83895a528df25d977e502c76nd/* ====================================================================
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * The Apache Software License, Version 1.1
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd *
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * Copyright (c) 2000-2001 The Apache Software Foundation. All rights
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * reserved.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb *
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * Redistribution and use in source and binary forms, with or without
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * modification, are permitted provided that the following conditions
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * are met:
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd *
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * 1. Redistributions of source code must retain the above copyright
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * notice, this list of conditions and the following disclaimer.
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd *
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * 2. Redistributions in binary form must reproduce the above copyright
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * notice, this list of conditions and the following disclaimer in
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * the documentation and/or other materials provided with the
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * distribution.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb *
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * 3. The end-user documentation included with the redistribution,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * if any, must include the following acknowledgment:
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * "This product includes software developed by the
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * Apache Software Foundation (http://www.apache.org/)."
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * Alternately, this acknowledgment may appear in the software itself,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * if and wherever such third-party acknowledgments normally appear.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb *
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * 4. The names "Apache" and "Apache Software Foundation" must
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * not be used to endorse or promote products derived from this
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * software without prior written permission. For written
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * permission, please contact apache@apache.org.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb *
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * 5. Products derived from this software may not be called "Apache",
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * nor may "Apache" appear in their name, without prior written
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * permission of the Apache Software Foundation.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb *
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
f888346b48f5e5b5e3f0a47dedb8cefd2759a4e2gregames * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * SUCH DAMAGE.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * ====================================================================
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb *
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * This software consists of voluntary contributions made by many
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * individuals on behalf of the Apache Software Foundation. For more
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * information on the Apache Software Foundation, please see
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * <http://www.apache.org/>.
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz *
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz * Portions of this software are based upon public domain software
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz * originally written at the National Center for Supercomputing Applications,
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz * University of Illinois, Urbana-Champaign.
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz */
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz#include "apr.h"
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz#include "apr_lib.h"
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz#include "apr_strings.h"
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb#define APR_WANT_STRFUNC
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb#include "apr_want.h"
4f9c22c4f27571d54197be9674e1fc0d528192aestriker
a2a0abd88b19e042a3eb2a9fa1702c25ad51303dwrowe#if APR_HAVE_STDLIB_H
4f9c22c4f27571d54197be9674e1fc0d528192aestriker#include <stdlib.h>
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb#endif
4f9c22c4f27571d54197be9674e1fc0d528192aestriker
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb#define CORE_PRIVATE
4f9c22c4f27571d54197be9674e1fc0d528192aestriker#include "ap_config.h"
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb#include "httpd.h"
4f9c22c4f27571d54197be9674e1fc0d528192aestriker#include "http_config.h"
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb#include "http_main.h"
4f9c22c4f27571d54197be9674e1fc0d528192aestriker#include "http_log.h"
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb#include "http_core.h"
4f9c22c4f27571d54197be9674e1fc0d528192aestriker#include "http_protocol.h"
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb#include "http_request.h" /* for sub_req_lookup_uri() */
4f9c22c4f27571d54197be9674e1fc0d528192aestriker#include "util_script.h"
4f9c22c4f27571d54197be9674e1fc0d528192aestriker#include "apr_date.h" /* For apr_date_parse_http() */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb#include "util_ebcdic.h"
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe#ifdef OS2
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe#define INCL_DOS
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe#include <os2.h>
2d399cd7535887fceaa9f8f116eb98ce68ddd602trawick#endif
c2cf53a40a9814eb91db2cdf820f97d943f21628coar
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe/*
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe * Various utility functions which are common to a whole lot of
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar * script-type extensions mechanisms, and might as well be gathered
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe * in one place (if only to avoid creating inter-module dependancies
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar * where there don't have to be).
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe */
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe#define MALFORMED_MESSAGE "malformed header from script. Bad header="
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe#define MALFORMED_HEADER_LENGTH_TO_SHOW 30
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowestatic char *http2env(apr_pool_t *a, char *w)
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe{
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe char *res = apr_pstrcat(a, "HTTP_", w, NULL);
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe char *cp = res;
3e392a5afd51526de3cb15d57ee46d8cb160ae65gregames
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe while (*++cp) {
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe if (!apr_isalnum(*cp) && *cp != '_') {
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe *cp = '_';
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe }
8419e6f8bff1a3617933f3ba760d2bdec7442f44coar else {
8419e6f8bff1a3617933f3ba760d2bdec7442f44coar *cp = apr_toupper(*cp);
8419e6f8bff1a3617933f3ba760d2bdec7442f44coar }
8419e6f8bff1a3617933f3ba760d2bdec7442f44coar }
8419e6f8bff1a3617933f3ba760d2bdec7442f44coar
8419e6f8bff1a3617933f3ba760d2bdec7442f44coar return res;
8419e6f8bff1a3617933f3ba760d2bdec7442f44coar}
8419e6f8bff1a3617933f3ba760d2bdec7442f44coar
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantzAP_DECLARE(char **) ap_create_environment(apr_pool_t *p, apr_table_t *t)
2a6e98ba4ffa30ded5d8831664c5cb2a170a56b6coar{
8419e6f8bff1a3617933f3ba760d2bdec7442f44coar apr_array_header_t *env_arr = apr_table_elts(t);
8419e6f8bff1a3617933f3ba760d2bdec7442f44coar apr_table_entry_t *elts = (apr_table_entry_t *) env_arr->elts;
8419e6f8bff1a3617933f3ba760d2bdec7442f44coar char **env = (char **) apr_palloc(p, (env_arr->nelts + 2) * sizeof(char *));
8419e6f8bff1a3617933f3ba760d2bdec7442f44coar int i, j;
8419e6f8bff1a3617933f3ba760d2bdec7442f44coar char *tz;
8419e6f8bff1a3617933f3ba760d2bdec7442f44coar char *whack;
2a6e98ba4ffa30ded5d8831664c5cb2a170a56b6coar
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz j = 0;
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz if (!apr_table_get(t, "TZ")) {
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe tz = getenv("TZ");
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe if (tz != NULL) {
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe env[j++] = apr_pstrcat(p, "TZ=", tz, NULL);
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe }
4f9c22c4f27571d54197be9674e1fc0d528192aestriker }
4f9c22c4f27571d54197be9674e1fc0d528192aestriker for (i = 0; i < env_arr->nelts; ++i) {
3e392a5afd51526de3cb15d57ee46d8cb160ae65gregames if (!elts[i].key) {
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe continue;
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar }
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe env[j] = apr_pstrcat(p, elts[i].key, "=", elts[i].val, NULL);
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe whack = env[j];
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe if (apr_isdigit(*whack)) {
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe *whack++ = '_';
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe }
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe while (*whack != '=') {
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe if (!apr_isalnum(*whack) && *whack != '_') {
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe *whack = '_';
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe }
4f9c22c4f27571d54197be9674e1fc0d528192aestriker ++whack;
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe }
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe ++j;
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe }
c2cf53a40a9814eb91db2cdf820f97d943f21628coar
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe env[j] = NULL;
4775dfc34c90fada8c7c4d6a57ed8a3114d55c2dtrawick return env;
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe}
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wroweAP_DECLARE(void) ap_add_common_vars(request_rec *r)
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe{
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar apr_table_t *e;
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe server_rec *s = r->server;
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe conn_rec *c = r->connection;
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe const char *rem_logname;
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe char *env_path;
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe#if defined(WIN32) || defined(OS2) || defined(BEOS)
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe char *env_temp;
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe#endif
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe const char *host;
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe apr_array_header_t *hdrs_arr = apr_table_elts(r->headers_in);
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe apr_table_entry_t *hdrs = (apr_table_entry_t *) hdrs_arr->elts;
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe int i;
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe apr_port_t rport;
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe apr_sockaddr_t *remotesa;
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe /* use a temporary apr_table_t which we'll overlap onto
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe * r->subprocess_env later
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe */
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe e = apr_table_make(r->pool, 25 + hdrs_arr->nelts);
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe /* First, add environment vars from headers... this is as per
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * CGI specs, though other sorts of scripting interfaces see
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe * the same vars...
4f9c22c4f27571d54197be9674e1fc0d528192aestriker */
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe
4f9c22c4f27571d54197be9674e1fc0d528192aestriker for (i = 0; i < hdrs_arr->nelts; ++i) {
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe if (!hdrs[i].key) {
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe continue;
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar }
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe /* A few headers are special cased --- Authorization to prevent
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe * rogue scripts from capturing passwords; content-type and -length
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe * for no particular reason.
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe */
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe
4f9c22c4f27571d54197be9674e1fc0d528192aestriker if (!strcasecmp(hdrs[i].key, "Content-type")) {
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe apr_table_addn(e, "CONTENT_TYPE", hdrs[i].val);
c2cf53a40a9814eb91db2cdf820f97d943f21628coar }
c2cf53a40a9814eb91db2cdf820f97d943f21628coar else if (!strcasecmp(hdrs[i].key, "Content-length")) {
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe apr_table_addn(e, "CONTENT_LENGTH", hdrs[i].val);
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz }
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz /*
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz * You really don't want to disable this check, since it leaves you
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe * wide open to CGIs stealing passwords and people viewing them
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * in the environment with "ps -e". But, if you must...
c2cf53a40a9814eb91db2cdf820f97d943f21628coar */
c2cf53a40a9814eb91db2cdf820f97d943f21628coar#ifndef SECURITY_HOLE_PASS_AUTHORIZATION
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe else if (!strcasecmp(hdrs[i].key, "Authorization")
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz || !strcasecmp(hdrs[i].key, "Proxy-Authorization")) {
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz continue;
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz }
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe#endif
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe else {
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe apr_table_addn(e, http2env(r->pool, hdrs[i].key), hdrs[i].val);
4f9c22c4f27571d54197be9674e1fc0d528192aestriker }
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe }
08ab23ee4e41ac8587da5a3bb8d4e9c49523b0b8nd
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe if (!(env_path = getenv("PATH"))) {
08ab23ee4e41ac8587da5a3bb8d4e9c49523b0b8nd env_path = DEFAULT_PATH;
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe }
4f9c22c4f27571d54197be9674e1fc0d528192aestriker apr_table_addn(e, "PATH", apr_pstrdup(r->pool, env_path));
c2cf53a40a9814eb91db2cdf820f97d943f21628coar
c2cf53a40a9814eb91db2cdf820f97d943f21628coar#ifdef WIN32
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe if (env_temp = getenv("SystemRoot")) {
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz apr_table_addn(e, "SystemRoot", env_temp);
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz }
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz if (env_temp = getenv("COMSPEC")) {
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe apr_table_addn(e, "COMSPEC", env_temp);
4f9c22c4f27571d54197be9674e1fc0d528192aestriker }
c2cf53a40a9814eb91db2cdf820f97d943f21628coar if (env_temp = getenv("WINDIR")) {
c2cf53a40a9814eb91db2cdf820f97d943f21628coar apr_table_addn(e, "WINDIR", env_temp);
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe }
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz#endif
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz#ifdef OS2
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe if ((env_temp = getenv("COMSPEC")) != NULL) {
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe apr_table_addn(e, "COMSPEC", env_temp);
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe }
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe if ((env_temp = getenv("ETC")) != NULL) {
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe apr_table_addn(e, "ETC", env_temp);
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe }
4f9c22c4f27571d54197be9674e1fc0d528192aestriker if ((env_temp = getenv("DPATH")) != NULL) {
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe apr_table_addn(e, "DPATH", env_temp);
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe }
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe if ((env_temp = getenv("PERLLIB_PREFIX")) != NULL) {
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz apr_table_addn(e, "PERLLIB_PREFIX", env_temp);
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe }
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe#endif
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe#ifdef BEOS
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe if ((env_temp = getenv("LIBRARY_PATH")) != NULL) {
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe apr_table_addn(e, "LIBRARY_PATH", env_temp);
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe }
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe#endif
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe apr_table_addn(e, "SERVER_SIGNATURE", ap_psignature("", r));
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe apr_table_addn(e, "SERVER_SOFTWARE", ap_get_server_version());
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe apr_table_addn(e, "SERVER_NAME", ap_get_server_name(r));
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe apr_table_addn(e, "SERVER_ADDR", r->connection->local_ip); /* Apache */
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe apr_table_addn(e, "SERVER_PORT",
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe apr_psprintf(r->pool, "%u", ap_get_server_port(r)));
4f9c22c4f27571d54197be9674e1fc0d528192aestriker host = ap_get_remote_host(c, r->per_dir_config, REMOTE_HOST, NULL);
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe if (host) {
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe apr_table_addn(e, "REMOTE_HOST", host);
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe }
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe apr_table_addn(e, "REMOTE_ADDR", c->remote_ip);
0540a0b469147b52e858587270dba31c2aaa9e09wrowe apr_table_addn(e, "DOCUMENT_ROOT", ap_document_root(r)); /* Apache */
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe apr_table_addn(e, "SERVER_ADMIN", s->server_admin); /* Apache */
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe apr_table_addn(e, "SCRIPT_FILENAME", r->filename); /* Apache */
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe apr_socket_addr_get(&remotesa, APR_REMOTE, c->client_socket);
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe apr_sockaddr_port_get(&rport, remotesa);
4f9c22c4f27571d54197be9674e1fc0d528192aestriker apr_table_addn(e, "REMOTE_PORT", apr_psprintf(r->pool, "%d", rport));
4f9c22c4f27571d54197be9674e1fc0d528192aestriker
4f9c22c4f27571d54197be9674e1fc0d528192aestriker if (r->user) {
4f9c22c4f27571d54197be9674e1fc0d528192aestriker apr_table_addn(e, "REMOTE_USER", r->user);
4f9c22c4f27571d54197be9674e1fc0d528192aestriker }
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe if (r->ap_auth_type) {
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe apr_table_addn(e, "AUTH_TYPE", r->ap_auth_type);
a2b181763cb35fd899feb4a436aeadaa80bf91eabrianp }
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe rem_logname = ap_get_remote_logname(r);
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe if (rem_logname) {
a2b181763cb35fd899feb4a436aeadaa80bf91eabrianp apr_table_addn(e, "REMOTE_IDENT", apr_pstrdup(r->pool, rem_logname));
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe }
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe /* Apache custom error responses. If we have redirected set two new vars */
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe if (r->prev) {
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe if (r->prev->args) {
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe apr_table_addn(e, "REDIRECT_QUERY_STRING", r->prev->args);
a2b181763cb35fd899feb4a436aeadaa80bf91eabrianp }
a2b181763cb35fd899feb4a436aeadaa80bf91eabrianp if (r->prev->uri) {
a2b181763cb35fd899feb4a436aeadaa80bf91eabrianp apr_table_addn(e, "REDIRECT_URL", r->prev->uri);
a2b181763cb35fd899feb4a436aeadaa80bf91eabrianp }
4f9c22c4f27571d54197be9674e1fc0d528192aestriker }
a2b181763cb35fd899feb4a436aeadaa80bf91eabrianp
a2b181763cb35fd899feb4a436aeadaa80bf91eabrianp apr_table_overlap(r->subprocess_env, e, APR_OVERLAP_TABLES_SET);
4f9c22c4f27571d54197be9674e1fc0d528192aestriker}
4f9c22c4f27571d54197be9674e1fc0d528192aestriker
4f9c22c4f27571d54197be9674e1fc0d528192aestriker/* This "cute" little function comes about because the path info on
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * filenames and URLs aren't always the same. So we take the two,
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * and find as much of the two that match as possible.
4f9c22c4f27571d54197be9674e1fc0d528192aestriker */
4f9c22c4f27571d54197be9674e1fc0d528192aestriker
a2b181763cb35fd899feb4a436aeadaa80bf91eabrianpAP_DECLARE(int) ap_find_path_info(const char *uri, const char *path_info)
7a2edaa0193cbb0d79a65a8461a609a9402aea49brianp{
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe int lu = strlen(uri);
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe int lp = strlen(path_info);
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe while (lu-- && lp-- && uri[lu] == path_info[lp]);
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe if (lu == -1) {
4f9c22c4f27571d54197be9674e1fc0d528192aestriker lu = 0;
a2b181763cb35fd899feb4a436aeadaa80bf91eabrianp }
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe while (uri[lu] != '\0' && uri[lu] != '/') {
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe lu++;
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe }
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb return lu;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb}
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb/* Obtain the Request-URI from the original request-line, returning
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * a new string from the request pool containing the URI or "".
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbbstatic char *original_uri(request_rec *r)
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb{
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb char *first, *last;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb if (r->the_request == NULL) {
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb return (char *) apr_pcalloc(r->pool, 1);
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb }
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb first = r->the_request; /* use the request-line */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe while (*first && !apr_isspace(*first)) {
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe ++first; /* skip over the method */
4f9c22c4f27571d54197be9674e1fc0d528192aestriker }
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe while (apr_isspace(*first)) {
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe ++first; /* and the space(s) */
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe }
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe last = first;
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe while (*last && !apr_isspace(*last)) {
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe ++last; /* end at next whitespace */
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe }
4f9c22c4f27571d54197be9674e1fc0d528192aestriker
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe return apr_pstrndup(r->pool, first, last - first);
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe}
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwroweAP_DECLARE(void) ap_add_cgi_vars(request_rec *r)
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe{
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe apr_table_t *e = r->subprocess_env;
7c301a1818939f85da8f3629cc3e9b5588610ef0jerenkrantz
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe apr_table_setn(e, "GATEWAY_INTERFACE", "CGI/1.1");
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar apr_table_setn(e, "SERVER_PROTOCOL", r->protocol);
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe apr_table_setn(e, "REQUEST_METHOD", r->method);
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar apr_table_setn(e, "QUERY_STRING", r->args ? r->args : "");
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe apr_table_setn(e, "REQUEST_URI", original_uri(r));
7c301a1818939f85da8f3629cc3e9b5588610ef0jerenkrantz
7c301a1818939f85da8f3629cc3e9b5588610ef0jerenkrantz /* Note that the code below special-cases scripts run from includes,
7c301a1818939f85da8f3629cc3e9b5588610ef0jerenkrantz * because it "knows" that the sub_request has been hacked to have the
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe * args and path_info of the original request, and not any that may have
659ad814f714e556bdd03e1d771cba156baab92ewrowe * come with the script URI in the include command. Ugh.
659ad814f714e556bdd03e1d771cba156baab92ewrowe */
659ad814f714e556bdd03e1d771cba156baab92ewrowe
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe if (!strcmp(r->protocol, "INCLUDED")) {
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar apr_table_setn(e, "SCRIPT_NAME", r->uri);
11fb2f3611e6ff9a541e10b13e3108934f828141gregames if (r->path_info && *r->path_info) {
11fb2f3611e6ff9a541e10b13e3108934f828141gregames apr_table_setn(e, "PATH_INFO", r->path_info);
11fb2f3611e6ff9a541e10b13e3108934f828141gregames }
7c301a1818939f85da8f3629cc3e9b5588610ef0jerenkrantz }
7c301a1818939f85da8f3629cc3e9b5588610ef0jerenkrantz else if (!r->path_info || !*r->path_info) {
7c301a1818939f85da8f3629cc3e9b5588610ef0jerenkrantz apr_table_setn(e, "SCRIPT_NAME", r->uri);
7c301a1818939f85da8f3629cc3e9b5588610ef0jerenkrantz }
4f9c22c4f27571d54197be9674e1fc0d528192aestriker else {
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe int path_info_start = ap_find_path_info(r->uri, r->path_info);
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe apr_table_setn(e, "SCRIPT_NAME",
4f9c22c4f27571d54197be9674e1fc0d528192aestriker apr_pstrndup(r->pool, r->uri, path_info_start));
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe apr_table_setn(e, "PATH_INFO", r->path_info);
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe }
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar
1fbf6ba0f5207e6637b49f9a9dfcc779bbe952a9trawick if (r->path_info && r->path_info[0]) {
1fbf6ba0f5207e6637b49f9a9dfcc779bbe952a9trawick /*
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar * To get PATH_TRANSLATED, treat PATH_INFO as a URI path.
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe * Need to re-escape it for this, since the entire URI was
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar * un-escaped before we determined where the PATH_INFO began.
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar */
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe request_rec *pa_req;
4f9c22c4f27571d54197be9674e1fc0d528192aestriker
4f9c22c4f27571d54197be9674e1fc0d528192aestriker pa_req = ap_sub_req_lookup_uri(ap_escape_uri(r->pool, r->path_info), r,
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe NULL);
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe if (pa_req->filename) {
1fbf6ba0f5207e6637b49f9a9dfcc779bbe952a9trawick char *pt = apr_pstrcat(r->pool, pa_req->filename, pa_req->path_info,
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe NULL);
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar#ifdef WIN32
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe char buffer[HUGE_STRING_LEN];
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe /* We need to make this a real Windows path name */
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe GetFullPathName(pt, HUGE_STRING_LEN, buffer, NULL);
7c301a1818939f85da8f3629cc3e9b5588610ef0jerenkrantz apr_table_setn(e, "PATH_TRANSLATED", apr_pstrdup(r->pool, buffer));
7c301a1818939f85da8f3629cc3e9b5588610ef0jerenkrantz#else
7c301a1818939f85da8f3629cc3e9b5588610ef0jerenkrantz apr_table_setn(e, "PATH_TRANSLATED", pt);
7c301a1818939f85da8f3629cc3e9b5588610ef0jerenkrantz#endif
4f9c22c4f27571d54197be9674e1fc0d528192aestriker }
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe ap_destroy_sub_req(pa_req);
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe }
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb}
8aefbd756763807188d2e3ce336a8680e4893066wrowe
700b96db75e7cfadb5219978c1735b710d583763wrowe
700b96db75e7cfadb5219978c1735b710d583763wrowestatic int set_cookie_doo_doo(void *v, const char *key, const char *val)
700b96db75e7cfadb5219978c1735b710d583763wrowe{
700b96db75e7cfadb5219978c1735b710d583763wrowe apr_table_addn(v, key, val);
700b96db75e7cfadb5219978c1735b710d583763wrowe return 1;
700b96db75e7cfadb5219978c1735b710d583763wrowe}
700b96db75e7cfadb5219978c1735b710d583763wrowe
700b96db75e7cfadb5219978c1735b710d583763wroweAP_DECLARE(int) ap_scan_script_header_err_core(request_rec *r, char *buffer,
700b96db75e7cfadb5219978c1735b710d583763wrowe int (*getsfunc) (char *, int, void *),
700b96db75e7cfadb5219978c1735b710d583763wrowe void *getsfunc_data)
700b96db75e7cfadb5219978c1735b710d583763wrowe{
700b96db75e7cfadb5219978c1735b710d583763wrowe char x[MAX_STRING_LEN];
700b96db75e7cfadb5219978c1735b710d583763wrowe char *w, *l;
700b96db75e7cfadb5219978c1735b710d583763wrowe int p;
700b96db75e7cfadb5219978c1735b710d583763wrowe int cgi_status = HTTP_OK;
700b96db75e7cfadb5219978c1735b710d583763wrowe apr_table_t *merge;
103a93c625bcde1a6a7a5155b64dcda36f612180pquerna apr_table_t *cookie_table;
700b96db75e7cfadb5219978c1735b710d583763wrowe
700b96db75e7cfadb5219978c1735b710d583763wrowe if (buffer) {
6964758306167dd898baedd21048bd1515dd9d30trawick *buffer = '\0';
700b96db75e7cfadb5219978c1735b710d583763wrowe }
700b96db75e7cfadb5219978c1735b710d583763wrowe w = buffer ? buffer : x;
700b96db75e7cfadb5219978c1735b710d583763wrowe
700b96db75e7cfadb5219978c1735b710d583763wrowe /* temporary place to hold headers to merge in later */
700b96db75e7cfadb5219978c1735b710d583763wrowe merge = apr_table_make(r->pool, 10);
700b96db75e7cfadb5219978c1735b710d583763wrowe
700b96db75e7cfadb5219978c1735b710d583763wrowe /* The HTTP specification says that it is legal to merge duplicate
700b96db75e7cfadb5219978c1735b710d583763wrowe * headers into one. Some browsers that support Cookies don't like
700b96db75e7cfadb5219978c1735b710d583763wrowe * merged headers and prefer that each Set-Cookie header is sent
700b96db75e7cfadb5219978c1735b710d583763wrowe * separately. Lets humour those browsers by not merging.
700b96db75e7cfadb5219978c1735b710d583763wrowe * Oh what a pain it is.
700b96db75e7cfadb5219978c1735b710d583763wrowe */
700b96db75e7cfadb5219978c1735b710d583763wrowe cookie_table = apr_table_make(r->pool, 2);
700b96db75e7cfadb5219978c1735b710d583763wrowe apr_table_do(set_cookie_doo_doo, cookie_table, r->err_headers_out, "Set-Cookie", NULL);
700b96db75e7cfadb5219978c1735b710d583763wrowe
700b96db75e7cfadb5219978c1735b710d583763wrowe while (1) {
700b96db75e7cfadb5219978c1735b710d583763wrowe
700b96db75e7cfadb5219978c1735b710d583763wrowe if ((*getsfunc) (w, MAX_STRING_LEN - 1, getsfunc_data) == 0) {
700b96db75e7cfadb5219978c1735b710d583763wrowe ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
700b96db75e7cfadb5219978c1735b710d583763wrowe "Premature end of script headers: %s", r->filename);
700b96db75e7cfadb5219978c1735b710d583763wrowe return HTTP_INTERNAL_SERVER_ERROR;
700b96db75e7cfadb5219978c1735b710d583763wrowe }
700b96db75e7cfadb5219978c1735b710d583763wrowe
103a93c625bcde1a6a7a5155b64dcda36f612180pquerna /* Delete terminal (CR?)LF */
103a93c625bcde1a6a7a5155b64dcda36f612180pquerna
103a93c625bcde1a6a7a5155b64dcda36f612180pquerna p = strlen(w);
700b96db75e7cfadb5219978c1735b710d583763wrowe /* Indeed, the host's '\n':
700b96db75e7cfadb5219978c1735b710d583763wrowe '\012' for UNIX; '\015' for MacOS; '\025' for OS/390
700b96db75e7cfadb5219978c1735b710d583763wrowe -- whatever the script generates.
8aefbd756763807188d2e3ce336a8680e4893066wrowe */
8aefbd756763807188d2e3ce336a8680e4893066wrowe if (p > 0 && w[p - 1] == '\n') {
8aefbd756763807188d2e3ce336a8680e4893066wrowe if (p > 1 && w[p - 2] == CR) {
8aefbd756763807188d2e3ce336a8680e4893066wrowe w[p - 2] = '\0';
8aefbd756763807188d2e3ce336a8680e4893066wrowe }
8aefbd756763807188d2e3ce336a8680e4893066wrowe else {
8aefbd756763807188d2e3ce336a8680e4893066wrowe w[p - 1] = '\0';
8aefbd756763807188d2e3ce336a8680e4893066wrowe }
8aefbd756763807188d2e3ce336a8680e4893066wrowe }
8aefbd756763807188d2e3ce336a8680e4893066wrowe
8aefbd756763807188d2e3ce336a8680e4893066wrowe /*
8aefbd756763807188d2e3ce336a8680e4893066wrowe * If we've finished reading the headers, check to make sure any
8aefbd756763807188d2e3ce336a8680e4893066wrowe * HTTP/1.1 conditions are met. If so, we're done; normal processing
8aefbd756763807188d2e3ce336a8680e4893066wrowe * will handle the script's output. If not, just return the error.
8aefbd756763807188d2e3ce336a8680e4893066wrowe * The appropriate thing to do would be to send the script process a
a2a0abd88b19e042a3eb2a9fa1702c25ad51303dwrowe * SIGPIPE to let it know we're ignoring it, close the channel to the
8aefbd756763807188d2e3ce336a8680e4893066wrowe * script process, and *then* return the failed-to-meet-condition
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * error. Otherwise we'd be waiting for the script to finish
8aefbd756763807188d2e3ce336a8680e4893066wrowe * blithering before telling the client the output was no good.
8aefbd756763807188d2e3ce336a8680e4893066wrowe * However, we don't have the information to do that, so we have to
2fa5b5878e7567e2875807c3e2a2b3b0d3ef74bewrowe * leave it to an upper layer.
2fa5b5878e7567e2875807c3e2a2b3b0d3ef74bewrowe */
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe if (w[0] == '\0') {
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe int cond_status = OK;
8aefbd756763807188d2e3ce336a8680e4893066wrowe
8aefbd756763807188d2e3ce336a8680e4893066wrowe if ((cgi_status == HTTP_OK) && (r->method_number == M_GET)) {
dc8692c6c0ca616a09aa12dad005f2ef23baa1a0wrowe cond_status = ap_meets_conditions(r);
a8d11d78181478da6a672f7fbc58b8d523351f49wrowe }
4f9c22c4f27571d54197be9674e1fc0d528192aestriker apr_table_overlap(r->err_headers_out, merge,
23c6309e36a63b13b61c35999c978017521993d6wrowe APR_OVERLAP_TABLES_MERGE);
23c6309e36a63b13b61c35999c978017521993d6wrowe if (!apr_is_empty_table(cookie_table)) {
4f9c22c4f27571d54197be9674e1fc0d528192aestriker /* the cookies have already been copied to the cookie_table */
23c6309e36a63b13b61c35999c978017521993d6wrowe apr_table_unset(r->err_headers_out, "Set-Cookie");
8aefbd756763807188d2e3ce336a8680e4893066wrowe r->err_headers_out = apr_table_overlay(r->pool,
23c6309e36a63b13b61c35999c978017521993d6wrowe r->err_headers_out, cookie_table);
2d399cd7535887fceaa9f8f116eb98ce68ddd602trawick }
4f9c22c4f27571d54197be9674e1fc0d528192aestriker return cond_status;
23c6309e36a63b13b61c35999c978017521993d6wrowe }
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe
8aefbd756763807188d2e3ce336a8680e4893066wrowe /* if we see a bogus header don't ignore it. Shout and scream */
8aefbd756763807188d2e3ce336a8680e4893066wrowe
cf6ef072483172309861d06e85b1aeff4573c060wrowe#if APR_CHARSET_EBCDIC
cf6ef072483172309861d06e85b1aeff4573c060wrowe /* Chances are that we received an ASCII header text instead of
cf6ef072483172309861d06e85b1aeff4573c060wrowe * the expected EBCDIC header lines. Try to auto-detect:
8aefbd756763807188d2e3ce336a8680e4893066wrowe */
4f9c22c4f27571d54197be9674e1fc0d528192aestriker if (!(l = strchr(w, ':'))) {
4f9c22c4f27571d54197be9674e1fc0d528192aestriker int maybeASCII = 0, maybeEBCDIC = 0;
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe unsigned char *cp, native;
2d399cd7535887fceaa9f8f116eb98ce68ddd602trawick apr_size_t inbytes_left, outbytes_left;
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe
4f9c22c4f27571d54197be9674e1fc0d528192aestriker for (cp = w; *cp != '\0'; ++cp) {
a8d11d78181478da6a672f7fbc58b8d523351f49wrowe native = apr_xlate_conv_byte(ap_hdrs_from_ascii, *cp);
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe if (isprint(*cp) && !isprint(native))
8aefbd756763807188d2e3ce336a8680e4893066wrowe ++maybeEBCDIC;
8aefbd756763807188d2e3ce336a8680e4893066wrowe if (!isprint(*cp) && isprint(native))
cf6ef072483172309861d06e85b1aeff4573c060wrowe ++maybeASCII;
cf6ef072483172309861d06e85b1aeff4573c060wrowe }
cf6ef072483172309861d06e85b1aeff4573c060wrowe if (maybeASCII > maybeEBCDIC) {
8aefbd756763807188d2e3ce336a8680e4893066wrowe ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r->server,
cf6ef072483172309861d06e85b1aeff4573c060wrowe "CGI Interface Error: Script headers apparently ASCII: (CGI = %s)",
cf6ef072483172309861d06e85b1aeff4573c060wrowe r->filename);
a2b181763cb35fd899feb4a436aeadaa80bf91eabrianp inbytes_left = outbytes_left = cp - w;
cf6ef072483172309861d06e85b1aeff4573c060wrowe apr_xlate_conv_buffer(ap_hdrs_from_ascii,
4f9c22c4f27571d54197be9674e1fc0d528192aestriker w, &inbytes_left, w, &outbytes_left);
cf6ef072483172309861d06e85b1aeff4573c060wrowe }
cf6ef072483172309861d06e85b1aeff4573c060wrowe }
cf6ef072483172309861d06e85b1aeff4573c060wrowe#endif /*APR_CHARSET_EBCDIC*/
cf6ef072483172309861d06e85b1aeff4573c060wrowe if (!(l = strchr(w, ':'))) {
cf6ef072483172309861d06e85b1aeff4573c060wrowe char malformed[(sizeof MALFORMED_MESSAGE) + 1
cf6ef072483172309861d06e85b1aeff4573c060wrowe + MALFORMED_HEADER_LENGTH_TO_SHOW];
cf6ef072483172309861d06e85b1aeff4573c060wrowe
cf6ef072483172309861d06e85b1aeff4573c060wrowe strcpy(malformed, MALFORMED_MESSAGE);
cf6ef072483172309861d06e85b1aeff4573c060wrowe strncat(malformed, w, MALFORMED_HEADER_LENGTH_TO_SHOW);
69adb3d949e3dd17c0492a01fc2cf298832c7eefwrowe
ecde48c75338ff5712f2036711f813c6dedca28ewrowe if (!buffer) {
d75626f0952c6152a99acd013a4f127d46f0f9edtrawick /* Soak up all the script output - may save an outright kill */
d75626f0952c6152a99acd013a4f127d46f0f9edtrawick while ((*getsfunc) (w, MAX_STRING_LEN - 1, getsfunc_data)) {
d75626f0952c6152a99acd013a4f127d46f0f9edtrawick continue;
d75626f0952c6152a99acd013a4f127d46f0f9edtrawick }
d75626f0952c6152a99acd013a4f127d46f0f9edtrawick }
d75626f0952c6152a99acd013a4f127d46f0f9edtrawick
d75626f0952c6152a99acd013a4f127d46f0f9edtrawick ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
d75626f0952c6152a99acd013a4f127d46f0f9edtrawick "%s: %s", malformed, r->filename);
ecde48c75338ff5712f2036711f813c6dedca28ewrowe return HTTP_INTERNAL_SERVER_ERROR;
ecde48c75338ff5712f2036711f813c6dedca28ewrowe }
d75626f0952c6152a99acd013a4f127d46f0f9edtrawick
ecde48c75338ff5712f2036711f813c6dedca28ewrowe *l++ = '\0';
ecde48c75338ff5712f2036711f813c6dedca28ewrowe while (*l && apr_isspace(*l)) {
ecde48c75338ff5712f2036711f813c6dedca28ewrowe ++l;
ecde48c75338ff5712f2036711f813c6dedca28ewrowe }
ecde48c75338ff5712f2036711f813c6dedca28ewrowe
d75626f0952c6152a99acd013a4f127d46f0f9edtrawick if (!strcasecmp(w, "Content-type")) {
cf6ef072483172309861d06e85b1aeff4573c060wrowe char *tmp;
cf6ef072483172309861d06e85b1aeff4573c060wrowe
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar /* Nuke trailing whitespace */
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar char *endp = l + strlen(l) - 1;
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar while (endp > l && apr_isspace(*endp)) {
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe *endp-- = '\0';
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar }
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe tmp = apr_pstrdup(r->pool, l);
4f9c22c4f27571d54197be9674e1fc0d528192aestriker ap_content_type_tolower(tmp);
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe r->content_type = tmp;
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe }
4f9c22c4f27571d54197be9674e1fc0d528192aestriker /*
23c6309e36a63b13b61c35999c978017521993d6wrowe * If the script returned a specific status, that's what
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * we'll use - otherwise we assume 200 OK.
4f9c22c4f27571d54197be9674e1fc0d528192aestriker */
4f9c22c4f27571d54197be9674e1fc0d528192aestriker else if (!strcasecmp(w, "Status")) {
c2cf53a40a9814eb91db2cdf820f97d943f21628coar r->status = cgi_status = atoi(l);
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe r->status_line = apr_pstrdup(r->pool, l);
4f9c22c4f27571d54197be9674e1fc0d528192aestriker }
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe else if (!strcasecmp(w, "Location")) {
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe apr_table_set(r->headers_out, w, l);
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe }
a9a4544168a37b43bd180b3703ccee995f27a80awrowe else if (!strcasecmp(w, "Content-Length")) {
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe apr_table_set(r->headers_out, w, l);
a9a4544168a37b43bd180b3703ccee995f27a80awrowe }
4f9c22c4f27571d54197be9674e1fc0d528192aestriker else if (!strcasecmp(w, "Transfer-Encoding")) {
a9a4544168a37b43bd180b3703ccee995f27a80awrowe apr_table_set(r->headers_out, w, l);
a9a4544168a37b43bd180b3703ccee995f27a80awrowe }
a9a4544168a37b43bd180b3703ccee995f27a80awrowe /*
a9a4544168a37b43bd180b3703ccee995f27a80awrowe * If the script gave us a Last-Modified header, we can't just
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * pass it on blindly because of restrictions on future values.
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar */
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe else if (!strcasecmp(w, "Last-Modified")) {
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar ap_update_mtime(r, apr_date_parse_http(l));
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar ap_set_last_modified(r);
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe }
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe else if (!strcasecmp(w, "Set-Cookie")) {
4f9c22c4f27571d54197be9674e1fc0d528192aestriker apr_table_add(cookie_table, w, l);
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe }
8aefbd756763807188d2e3ce336a8680e4893066wrowe else {
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe apr_table_add(merge, w, l);
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe }
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe }
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe
700b96db75e7cfadb5219978c1735b710d583763wrowe return OK;
cf6ef072483172309861d06e85b1aeff4573c060wrowe}
cf6ef072483172309861d06e85b1aeff4573c060wrowe
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowestatic int getsfunc_FILE(char *buf, int len, void *f)
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe{
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe return apr_file_gets(buf, len, (apr_file_t *) f) == APR_SUCCESS;
8aefbd756763807188d2e3ce336a8680e4893066wrowe}
0e58e92812f2f679d6bf2ff66cbcfa6c1d1e14bbjerenkrantz
0e58e92812f2f679d6bf2ff66cbcfa6c1d1e14bbjerenkrantzAP_DECLARE(int) ap_scan_script_header_err(request_rec *r, apr_file_t *f,
0e58e92812f2f679d6bf2ff66cbcfa6c1d1e14bbjerenkrantz char *buffer)
cf6ef072483172309861d06e85b1aeff4573c060wrowe{
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe return ap_scan_script_header_err_core(r, buffer, getsfunc_FILE, f);
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe}
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe
4f9c22c4f27571d54197be9674e1fc0d528192aestrikerstruct vastrs {
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe va_list args;
2fd8d1e14fc47762d1e01660b544d5e2a75c825dtrawick int arg;
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe const char *curpos;
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe};
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe
8aefbd756763807188d2e3ce336a8680e4893066wrowestatic int getsfunc_STRING(char *w, int len, void *pvastrs)
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe{
700b96db75e7cfadb5219978c1735b710d583763wrowe struct vastrs *strs = (struct vastrs*) pvastrs;
700b96db75e7cfadb5219978c1735b710d583763wrowe const char *p;
700b96db75e7cfadb5219978c1735b710d583763wrowe int t;
700b96db75e7cfadb5219978c1735b710d583763wrowe
8aefbd756763807188d2e3ce336a8680e4893066wrowe if (!strs->curpos || !*strs->curpos)
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe return 0;
4f9c22c4f27571d54197be9674e1fc0d528192aestriker p = ap_strchr_c(strs->curpos, '\n');
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe if (p)
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe ++p;
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe else
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe p = ap_strchr_c(strs->curpos, '\0');
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe t = p - strs->curpos;
59513b1275fdc2021d4949ee03ae8229469abb86wrowe if (t > len)
4f9c22c4f27571d54197be9674e1fc0d528192aestriker t = len;
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe strncpy (w, strs->curpos, t);
4f9c22c4f27571d54197be9674e1fc0d528192aestriker w[t] = '\0';
4f9c22c4f27571d54197be9674e1fc0d528192aestriker if (!strs->curpos[t]) {
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe ++strs->arg;
4f9c22c4f27571d54197be9674e1fc0d528192aestriker strs->curpos = va_arg(strs->args, const char *);
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe }
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe else
4f9c22c4f27571d54197be9674e1fc0d528192aestriker strs->curpos += t;
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe return t;
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe}
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe
4f9c22c4f27571d54197be9674e1fc0d528192aestriker/* ap_scan_script_header_err_strs() accepts additional const char* args...
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe * each is treated as one or more header lines, and the first non-header
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe * character is returned to **arg, **data. (The first optional arg is
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe * counted as 0.)
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe */
1b315ee865b0f11e582beb64127ca3a99a319d2fwroweAP_DECLARE_NONSTD(int) ap_scan_script_header_err_strs(request_rec *r,
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe char *buffer,
cf6ef072483172309861d06e85b1aeff4573c060wrowe const char **termch,
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe int *termarg, ...)
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe{
4f9c22c4f27571d54197be9674e1fc0d528192aestriker struct vastrs strs;
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe int res;
4f9c22c4f27571d54197be9674e1fc0d528192aestriker
4f9c22c4f27571d54197be9674e1fc0d528192aestriker va_start(strs.args, termarg);
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe strs.arg = 0;
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe strs.curpos = va_arg(strs.args, char*);
4f9c22c4f27571d54197be9674e1fc0d528192aestriker res = ap_scan_script_header_err_core(r, buffer, getsfunc_STRING, (void *) &strs);
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe if (termch)
4f9c22c4f27571d54197be9674e1fc0d528192aestriker *termch = strs.curpos;
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe if (termarg)
4f9c22c4f27571d54197be9674e1fc0d528192aestriker *termarg = strs.arg;
4f9c22c4f27571d54197be9674e1fc0d528192aestriker va_end(strs.args);
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe return res;
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe}
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe