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