mod_cgi.c revision cb5134e26dede7d8a7439c5dd5f054750e42947b
c6d33447e28403a90ad817dba4df75fae785be28pquerna/* ====================================================================
c6d33447e28403a90ad817dba4df75fae785be28pquerna * Copyright (c) 1995-1999 The Apache Group. All rights reserved.
c6d33447e28403a90ad817dba4df75fae785be28pquerna *
c6d33447e28403a90ad817dba4df75fae785be28pquerna * Redistribution and use in source and binary forms, with or without
c6d33447e28403a90ad817dba4df75fae785be28pquerna * modification, are permitted provided that the following conditions
c6d33447e28403a90ad817dba4df75fae785be28pquerna * are met:
c6d33447e28403a90ad817dba4df75fae785be28pquerna *
c6d33447e28403a90ad817dba4df75fae785be28pquerna * 1. Redistributions of source code must retain the above copyright
c6d33447e28403a90ad817dba4df75fae785be28pquerna * notice, this list of conditions and the following disclaimer.
c6d33447e28403a90ad817dba4df75fae785be28pquerna *
c6d33447e28403a90ad817dba4df75fae785be28pquerna * 2. Redistributions in binary form must reproduce the above copyright
c6d33447e28403a90ad817dba4df75fae785be28pquerna * notice, this list of conditions and the following disclaimer in
c6d33447e28403a90ad817dba4df75fae785be28pquerna * the documentation and/or other materials provided with the
c6d33447e28403a90ad817dba4df75fae785be28pquerna * distribution.
c6d33447e28403a90ad817dba4df75fae785be28pquerna *
c6d33447e28403a90ad817dba4df75fae785be28pquerna * 3. All advertising materials mentioning features or use of this
c6d33447e28403a90ad817dba4df75fae785be28pquerna * software must display the following acknowledgment:
c6d33447e28403a90ad817dba4df75fae785be28pquerna * "This product includes software developed by the Apache Group
c6d33447e28403a90ad817dba4df75fae785be28pquerna * for use in the Apache HTTP server project (http://www.apache.org/)."
c6d33447e28403a90ad817dba4df75fae785be28pquerna *
c6d33447e28403a90ad817dba4df75fae785be28pquerna * 4. The names "Apache Server" and "Apache Group" must not be used to
c6d33447e28403a90ad817dba4df75fae785be28pquerna * endorse or promote products derived from this software without
c6d33447e28403a90ad817dba4df75fae785be28pquerna * prior written permission. For written permission, please contact
c6d33447e28403a90ad817dba4df75fae785be28pquerna * apache@apache.org.
c6d33447e28403a90ad817dba4df75fae785be28pquerna *
c6d33447e28403a90ad817dba4df75fae785be28pquerna * 5. Products derived from this software may not be called "Apache"
c6d33447e28403a90ad817dba4df75fae785be28pquerna * nor may "Apache" appear in their names without prior written
c6d33447e28403a90ad817dba4df75fae785be28pquerna * permission of the Apache Group.
c6d33447e28403a90ad817dba4df75fae785be28pquerna *
c6d33447e28403a90ad817dba4df75fae785be28pquerna * 6. Redistributions of any form whatsoever must retain the following
c6d33447e28403a90ad817dba4df75fae785be28pquerna * acknowledgment:
c6d33447e28403a90ad817dba4df75fae785be28pquerna * "This product includes software developed by the Apache Group
c6d33447e28403a90ad817dba4df75fae785be28pquerna * for use in the Apache HTTP server project (http://www.apache.org/)."
c6d33447e28403a90ad817dba4df75fae785be28pquerna *
c6d33447e28403a90ad817dba4df75fae785be28pquerna * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
c6d33447e28403a90ad817dba4df75fae785be28pquerna * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
c6d33447e28403a90ad817dba4df75fae785be28pquerna * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
c6d33447e28403a90ad817dba4df75fae785be28pquerna * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR
c6d33447e28403a90ad817dba4df75fae785be28pquerna * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
c6d33447e28403a90ad817dba4df75fae785be28pquerna * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
c6d33447e28403a90ad817dba4df75fae785be28pquerna * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
c6d33447e28403a90ad817dba4df75fae785be28pquerna * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
c6d33447e28403a90ad817dba4df75fae785be28pquerna * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
c6d33447e28403a90ad817dba4df75fae785be28pquerna * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
c6d33447e28403a90ad817dba4df75fae785be28pquerna * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
c6d33447e28403a90ad817dba4df75fae785be28pquerna * OF THE POSSIBILITY OF SUCH DAMAGE.
c6d33447e28403a90ad817dba4df75fae785be28pquerna * ====================================================================
c6d33447e28403a90ad817dba4df75fae785be28pquerna *
c6d33447e28403a90ad817dba4df75fae785be28pquerna * This software consists of voluntary contributions made by many
c6d33447e28403a90ad817dba4df75fae785be28pquerna * individuals on behalf of the Apache Group and was originally based
c6d33447e28403a90ad817dba4df75fae785be28pquerna * on public domain software written at the National Center for
c6d33447e28403a90ad817dba4df75fae785be28pquerna * Supercomputing Applications, University of Illinois, Urbana-Champaign.
c6d33447e28403a90ad817dba4df75fae785be28pquerna * For more information on the Apache Group and the Apache HTTP server
c6d33447e28403a90ad817dba4df75fae785be28pquerna * project, please see <http://www.apache.org/>.
c6d33447e28403a90ad817dba4df75fae785be28pquerna *
c6d33447e28403a90ad817dba4df75fae785be28pquerna */
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna/*
c6d33447e28403a90ad817dba4df75fae785be28pquerna * http_script: keeps all script-related ramblings together.
c6d33447e28403a90ad817dba4df75fae785be28pquerna *
c6d33447e28403a90ad817dba4df75fae785be28pquerna * Compliant to CGI/1.1 spec
c6d33447e28403a90ad817dba4df75fae785be28pquerna *
c6d33447e28403a90ad817dba4df75fae785be28pquerna * Adapted by rst from original NCSA code by Rob McCool
c6d33447e28403a90ad817dba4df75fae785be28pquerna *
c6d33447e28403a90ad817dba4df75fae785be28pquerna * Apache adds some new env vars; REDIRECT_URL and REDIRECT_QUERY_STRING for
c6d33447e28403a90ad817dba4df75fae785be28pquerna * custom error responses, and DOCUMENT_ROOT because we found it useful.
c6d33447e28403a90ad817dba4df75fae785be28pquerna * It also adds SERVER_ADMIN - useful for scripts to know who to mail when
c6d33447e28403a90ad817dba4df75fae785be28pquerna * they fail.
c6d33447e28403a90ad817dba4df75fae785be28pquerna */
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna#include "ap_config.h"
c6d33447e28403a90ad817dba4df75fae785be28pquerna#include "httpd.h"
c6d33447e28403a90ad817dba4df75fae785be28pquerna#include "http_config.h"
c6d33447e28403a90ad817dba4df75fae785be28pquerna#include "http_request.h"
c6d33447e28403a90ad817dba4df75fae785be28pquerna#include "http_core.h"
c6d33447e28403a90ad817dba4df75fae785be28pquerna#include "http_protocol.h"
c6d33447e28403a90ad817dba4df75fae785be28pquerna#include "http_main.h"
c6d33447e28403a90ad817dba4df75fae785be28pquerna#include "http_log.h"
c6d33447e28403a90ad817dba4df75fae785be28pquerna#include "util_script.h"
c6d33447e28403a90ad817dba4df75fae785be28pquerna#include "http_conf_globals.h"
c6d33447e28403a90ad817dba4df75fae785be28pquerna#ifdef HAVE_SYS_STAT_H
c6d33447e28403a90ad817dba4df75fae785be28pquerna#include <sys/stat.h>
c6d33447e28403a90ad817dba4df75fae785be28pquerna#endif
c6d33447e28403a90ad817dba4df75fae785be28pquerna#ifdef HAVE_STRING_H
c6d33447e28403a90ad817dba4df75fae785be28pquerna#include <string.h>
c6d33447e28403a90ad817dba4df75fae785be28pquerna#endif
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquernamodule MODULE_VAR_EXPORT cgi_module;
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna/* KLUDGE --- for back-combatibility, we don't have to check ExecCGI
c6d33447e28403a90ad817dba4df75fae785be28pquerna * in ScriptAliased directories, which means we need to know if this
c6d33447e28403a90ad817dba4df75fae785be28pquerna * request came through ScriptAlias or not... so the Alias module
c6d33447e28403a90ad817dba4df75fae785be28pquerna * leaves a note for us.
c6d33447e28403a90ad817dba4df75fae785be28pquerna */
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquernastatic int is_scriptaliased(request_rec *r)
c6d33447e28403a90ad817dba4df75fae785be28pquerna{
c6d33447e28403a90ad817dba4df75fae785be28pquerna const char *t = ap_table_get(r->notes, "alias-forced-type");
c6d33447e28403a90ad817dba4df75fae785be28pquerna return t && (!strcasecmp(t, "cgi-script"));
c6d33447e28403a90ad817dba4df75fae785be28pquerna}
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna/* Configuration stuff */
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna#define DEFAULT_LOGBYTES 10385760
c6d33447e28403a90ad817dba4df75fae785be28pquerna#define DEFAULT_BUFBYTES 1024
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquernatypedef struct {
c6d33447e28403a90ad817dba4df75fae785be28pquerna char *logname;
c6d33447e28403a90ad817dba4df75fae785be28pquerna long logbytes;
c6d33447e28403a90ad817dba4df75fae785be28pquerna int bufbytes;
c6d33447e28403a90ad817dba4df75fae785be28pquerna} cgi_server_conf;
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquernastatic void *create_cgi_config(ap_context_t *p, server_rec *s)
c6d33447e28403a90ad817dba4df75fae785be28pquerna{
c6d33447e28403a90ad817dba4df75fae785be28pquerna cgi_server_conf *c =
c6d33447e28403a90ad817dba4df75fae785be28pquerna (cgi_server_conf *) ap_pcalloc(p, sizeof(cgi_server_conf));
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna c->logname = NULL;
c6d33447e28403a90ad817dba4df75fae785be28pquerna c->logbytes = DEFAULT_LOGBYTES;
c6d33447e28403a90ad817dba4df75fae785be28pquerna c->bufbytes = DEFAULT_BUFBYTES;
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna return c;
c6d33447e28403a90ad817dba4df75fae785be28pquerna}
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquernastatic void *merge_cgi_config(ap_context_t *p, void *basev, void *overridesv)
c6d33447e28403a90ad817dba4df75fae785be28pquerna{
c6d33447e28403a90ad817dba4df75fae785be28pquerna cgi_server_conf *base = (cgi_server_conf *) basev, *overrides = (cgi_server_conf *) overridesv;
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna return overrides->logname ? overrides : base;
c6d33447e28403a90ad817dba4df75fae785be28pquerna}
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquernastatic const char *set_scriptlog(cmd_parms *cmd, void *dummy, char *arg)
c6d33447e28403a90ad817dba4df75fae785be28pquerna{
c6d33447e28403a90ad817dba4df75fae785be28pquerna server_rec *s = cmd->server;
c6d33447e28403a90ad817dba4df75fae785be28pquerna cgi_server_conf *conf =
c6d33447e28403a90ad817dba4df75fae785be28pquerna (cgi_server_conf *) ap_get_module_config(s->module_config, &cgi_module);
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna conf->logname = arg;
c6d33447e28403a90ad817dba4df75fae785be28pquerna return NULL;
c6d33447e28403a90ad817dba4df75fae785be28pquerna}
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquernastatic const char *set_scriptlog_length(cmd_parms *cmd, void *dummy, char *arg)
c6d33447e28403a90ad817dba4df75fae785be28pquerna{
c6d33447e28403a90ad817dba4df75fae785be28pquerna server_rec *s = cmd->server;
c6d33447e28403a90ad817dba4df75fae785be28pquerna cgi_server_conf *conf =
c6d33447e28403a90ad817dba4df75fae785be28pquerna (cgi_server_conf *) ap_get_module_config(s->module_config, &cgi_module);
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna conf->logbytes = atol(arg);
c6d33447e28403a90ad817dba4df75fae785be28pquerna return NULL;
c6d33447e28403a90ad817dba4df75fae785be28pquerna}
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquernastatic const char *set_scriptlog_buffer(cmd_parms *cmd, void *dummy, char *arg)
c6d33447e28403a90ad817dba4df75fae785be28pquerna{
c6d33447e28403a90ad817dba4df75fae785be28pquerna server_rec *s = cmd->server;
c6d33447e28403a90ad817dba4df75fae785be28pquerna cgi_server_conf *conf =
c6d33447e28403a90ad817dba4df75fae785be28pquerna (cgi_server_conf *) ap_get_module_config(s->module_config, &cgi_module);
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna conf->bufbytes = atoi(arg);
c6d33447e28403a90ad817dba4df75fae785be28pquerna return NULL;
c6d33447e28403a90ad817dba4df75fae785be28pquerna}
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquernastatic const command_rec cgi_cmds[] =
c6d33447e28403a90ad817dba4df75fae785be28pquerna{
c6d33447e28403a90ad817dba4df75fae785be28pquerna {"ScriptLog", set_scriptlog, NULL, RSRC_CONF, TAKE1,
c6d33447e28403a90ad817dba4df75fae785be28pquerna "the name of a log for script debugging info"},
c6d33447e28403a90ad817dba4df75fae785be28pquerna {"ScriptLogLength", set_scriptlog_length, NULL, RSRC_CONF, TAKE1,
c6d33447e28403a90ad817dba4df75fae785be28pquerna "the maximum length (in bytes) of the script debug log"},
c6d33447e28403a90ad817dba4df75fae785be28pquerna {"ScriptLogBuffer", set_scriptlog_buffer, NULL, RSRC_CONF, TAKE1,
c6d33447e28403a90ad817dba4df75fae785be28pquerna "the maximum size (in bytes) to record of a POST request"},
c6d33447e28403a90ad817dba4df75fae785be28pquerna {NULL}
c6d33447e28403a90ad817dba4df75fae785be28pquerna};
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquernastatic int log_scripterror(request_rec *r, cgi_server_conf * conf, int ret,
c6d33447e28403a90ad817dba4df75fae785be28pquerna int show_errno, char *error)
c6d33447e28403a90ad817dba4df75fae785be28pquerna{
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_file_t *f = NULL;
c6d33447e28403a90ad817dba4df75fae785be28pquerna struct stat finfo;
c6d33447e28403a90ad817dba4df75fae785be28pquerna char time_str[AP_CTIME_LEN];
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_log_rerror(APLOG_MARK, show_errno|APLOG_ERR, errno, r,
c6d33447e28403a90ad817dba4df75fae785be28pquerna "%s: %s", error, r->filename);
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (!conf->logname ||
c6d33447e28403a90ad817dba4df75fae785be28pquerna ((stat(ap_server_root_relative(r->pool, conf->logname), &finfo) == 0)
c6d33447e28403a90ad817dba4df75fae785be28pquerna && (finfo.st_size > conf->logbytes)) ||
c6d33447e28403a90ad817dba4df75fae785be28pquerna (ap_open(&f, ap_server_root_relative(r->pool, conf->logname),
c6d33447e28403a90ad817dba4df75fae785be28pquerna APR_APPEND | APR_BUFFERED, APR_OS_DEFAULT, r->pool) != APR_SUCCESS)) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna return ret;
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna /* "%% [Wed Jun 19 10:53:21 1996] GET /cgi-bin/printenv HTTP/1.0" */
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_ctime(time_str, ap_now());
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_fprintf(f, "%%%% [%s] %s %s%s%s %s\n", time_str, r->method, r->uri,
c6d33447e28403a90ad817dba4df75fae785be28pquerna r->args ? "?" : "", r->args ? r->args : "", r->protocol);
c6d33447e28403a90ad817dba4df75fae785be28pquerna /* "%% 500 /usr/local/apache/cgi-bin */
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_fprintf(f, "%%%% %d %s\n", ret, r->filename);
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_fprintf(f, "%%error\n%s\n", error);
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_close(f);
c6d33447e28403a90ad817dba4df75fae785be28pquerna return ret;
c6d33447e28403a90ad817dba4df75fae785be28pquerna}
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquernastatic int log_script(request_rec *r, cgi_server_conf * conf, int ret,
c6d33447e28403a90ad817dba4df75fae785be28pquerna char *dbuf, const char *sbuf, BUFF *script_in, BUFF *script_err)
c6d33447e28403a90ad817dba4df75fae785be28pquerna{
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_array_header_t *hdrs_arr = ap_table_elts(r->headers_in);
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_table_entry_t *hdrs = (ap_table_entry_t *) hdrs_arr->elts;
c6d33447e28403a90ad817dba4df75fae785be28pquerna char argsbuffer[HUGE_STRING_LEN];
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_file_t *f = NULL;
c6d33447e28403a90ad817dba4df75fae785be28pquerna int i;
c6d33447e28403a90ad817dba4df75fae785be28pquerna struct stat finfo;
c6d33447e28403a90ad817dba4df75fae785be28pquerna char time_str[AP_CTIME_LEN];
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (!conf->logname ||
c6d33447e28403a90ad817dba4df75fae785be28pquerna ((stat(ap_server_root_relative(r->pool, conf->logname), &finfo) == 0)
c6d33447e28403a90ad817dba4df75fae785be28pquerna && (finfo.st_size > conf->logbytes)) ||
c6d33447e28403a90ad817dba4df75fae785be28pquerna (ap_open(&f, ap_server_root_relative(r->pool, conf->logname),
c6d33447e28403a90ad817dba4df75fae785be28pquerna APR_APPEND, APR_OS_DEFAULT, r->pool) != APR_SUCCESS)) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna /* Soak up script output */
c6d33447e28403a90ad817dba4df75fae785be28pquerna while (ap_bgets(argsbuffer, HUGE_STRING_LEN, script_in) > 0)
c6d33447e28403a90ad817dba4df75fae785be28pquerna continue;
c6d33447e28403a90ad817dba4df75fae785be28pquerna#ifdef WIN32
c6d33447e28403a90ad817dba4df75fae785be28pquerna /* Soak up stderr and redirect it to the error log.
c6d33447e28403a90ad817dba4df75fae785be28pquerna * Script output to stderr is already directed to the error log
c6d33447e28403a90ad817dba4df75fae785be28pquerna * on Unix, thanks to the magic of fork().
c6d33447e28403a90ad817dba4df75fae785be28pquerna */
c6d33447e28403a90ad817dba4df75fae785be28pquerna while (ap_bgets(argsbuffer, HUGE_STRING_LEN, script_err) > 0) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_log_rerror(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, 0, r,
c6d33447e28403a90ad817dba4df75fae785be28pquerna "%s", argsbuffer);
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna#else
c6d33447e28403a90ad817dba4df75fae785be28pquerna while (ap_bgets(argsbuffer, HUGE_STRING_LEN, script_err) > 0)
c6d33447e28403a90ad817dba4df75fae785be28pquerna continue;
c6d33447e28403a90ad817dba4df75fae785be28pquerna#endif
c6d33447e28403a90ad817dba4df75fae785be28pquerna return ret;
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna /* "%% [Wed Jun 19 10:53:21 1996] GET /cgi-bin/printenv HTTP/1.0" */
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_ctime(time_str, ap_now());
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_fprintf(f, "%%%% [%s] %s %s%s%s %s\n", time_str, r->method, r->uri,
c6d33447e28403a90ad817dba4df75fae785be28pquerna r->args ? "?" : "", r->args ? r->args : "", r->protocol);
c6d33447e28403a90ad817dba4df75fae785be28pquerna /* "%% 500 /usr/local/apache/cgi-bin" */
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_fprintf(f, "%%%% %d %s\n", ret, r->filename);
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_puts("%request\n", f);
c6d33447e28403a90ad817dba4df75fae785be28pquerna for (i = 0; i < hdrs_arr->nelts; ++i) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (!hdrs[i].key)
c6d33447e28403a90ad817dba4df75fae785be28pquerna continue;
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_fprintf(f, "%s: %s\n", hdrs[i].key, hdrs[i].val);
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna if ((r->method_number == M_POST || r->method_number == M_PUT)
c6d33447e28403a90ad817dba4df75fae785be28pquerna && *dbuf) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_fprintf(f, "\n%s\n", dbuf);
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_puts("%response\n", f);
c6d33447e28403a90ad817dba4df75fae785be28pquerna hdrs_arr = ap_table_elts(r->err_headers_out);
c6d33447e28403a90ad817dba4df75fae785be28pquerna hdrs = (ap_table_entry_t *) hdrs_arr->elts;
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna for (i = 0; i < hdrs_arr->nelts; ++i) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (!hdrs[i].key)
c6d33447e28403a90ad817dba4df75fae785be28pquerna continue;
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_fprintf(f, "%s: %s\n", hdrs[i].key, hdrs[i].val);
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (sbuf && *sbuf)
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_fprintf(f, "%s\n", sbuf);
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (ap_bgets(argsbuffer, HUGE_STRING_LEN, script_in) > 0) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_puts("%stdout\n", f);
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_puts(argsbuffer, f);
c6d33447e28403a90ad817dba4df75fae785be28pquerna while (ap_bgets(argsbuffer, HUGE_STRING_LEN, script_in) > 0)
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_puts(argsbuffer, f);
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_puts("\n", f);
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (ap_bgets(argsbuffer, HUGE_STRING_LEN, script_err) > 0) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_puts("%stderr\n", f);
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_puts(argsbuffer, f);
c6d33447e28403a90ad817dba4df75fae785be28pquerna while (ap_bgets(argsbuffer, HUGE_STRING_LEN, script_err) > 0)
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_puts(argsbuffer, f);
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_puts("\n", f);
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_bclose(script_in);
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_bclose(script_err);
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_close(f);
c6d33447e28403a90ad817dba4df75fae785be28pquerna return ret;
c6d33447e28403a90ad817dba4df75fae785be28pquerna}
c6d33447e28403a90ad817dba4df75fae785be28pquernastatic ap_status_t run_cgi_child(BUFF **script_out, BUFF **script_in, BUFF **script_err,
c6d33447e28403a90ad817dba4df75fae785be28pquerna char *command, char *const argv[], request_rec *r, ap_context_t *p)
c6d33447e28403a90ad817dba4df75fae785be28pquerna{
c6d33447e28403a90ad817dba4df75fae785be28pquerna char **env;
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_procattr_t *procattr;
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_proc_t *procnew;
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_status_t rc = APR_SUCCESS;
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_file_t *file = NULL;
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_iol *iol;
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna#ifdef DEBUG_CGI
c6d33447e28403a90ad817dba4df75fae785be28pquerna#ifdef OS2
c6d33447e28403a90ad817dba4df75fae785be28pquerna /* Under OS/2 need to use device con. */
c6d33447e28403a90ad817dba4df75fae785be28pquerna FILE *dbg = fopen("con", "w");
c6d33447e28403a90ad817dba4df75fae785be28pquerna#else
c6d33447e28403a90ad817dba4df75fae785be28pquerna FILE *dbg = fopen("/dev/tty", "w");
c6d33447e28403a90ad817dba4df75fae785be28pquerna#endif
c6d33447e28403a90ad817dba4df75fae785be28pquerna int i;
c6d33447e28403a90ad817dba4df75fae785be28pquerna#endif
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_block_alarms();
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna RAISE_SIGSTOP(CGI_CHILD);
c6d33447e28403a90ad817dba4df75fae785be28pquerna#ifdef DEBUG_CGI
c6d33447e28403a90ad817dba4df75fae785be28pquerna fprintf(dbg, "Attempting to exec %s as %sCGI child (argv0 = %s)\n",
c6d33447e28403a90ad817dba4df75fae785be28pquerna r->filename, cld->nph ? "NPH " : "", argv0);
c6d33447e28403a90ad817dba4df75fae785be28pquerna#endif
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_add_cgi_vars(r);
c6d33447e28403a90ad817dba4df75fae785be28pquerna env = ap_create_environment(p, r->subprocess_env);
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna#ifdef DEBUG_CGI
c6d33447e28403a90ad817dba4df75fae785be28pquerna fprintf(dbg, "Environment: \n");
c6d33447e28403a90ad817dba4df75fae785be28pquerna for (i = 0; env[i]; ++i)
c6d33447e28403a90ad817dba4df75fae785be28pquerna fprintf(dbg, "'%s'\n", env[i]);
c6d33447e28403a90ad817dba4df75fae785be28pquerna#endif
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna /* Transumute ourselves into the script.
c6d33447e28403a90ad817dba4df75fae785be28pquerna * NB only ISINDEX scripts get decoded arguments.
c6d33447e28403a90ad817dba4df75fae785be28pquerna */
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (((rc = ap_createprocattr_init(&procattr, p)) != APR_SUCCESS) ||
c6d33447e28403a90ad817dba4df75fae785be28pquerna ((rc = ap_setprocattr_io(procattr,
c6d33447e28403a90ad817dba4df75fae785be28pquerna APR_CHILD_BLOCK,
c6d33447e28403a90ad817dba4df75fae785be28pquerna APR_CHILD_BLOCK,
c6d33447e28403a90ad817dba4df75fae785be28pquerna APR_CHILD_BLOCK)) != APR_SUCCESS) ||
c6d33447e28403a90ad817dba4df75fae785be28pquerna ((rc = ap_setprocattr_dir(procattr,
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_make_dirstr_parent(r->pool, r->filename))) != APR_SUCCESS) ||
c6d33447e28403a90ad817dba4df75fae785be28pquerna ((rc = ap_setprocattr_cmdtype(procattr, APR_PROGRAM)) != APR_SUCCESS)) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna /* Something bad happened, tell the world. */
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_log_rerror(APLOG_MARK, APLOG_ERR, rc, r,
c6d33447e28403a90ad817dba4df75fae785be28pquerna "couldn't create child process: %s", r->filename);
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna else {
c6d33447e28403a90ad817dba4df75fae785be28pquerna rc = ap_create_process(&procnew, command, argv, env, procattr, p);
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (rc != APR_SUCCESS) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna /* Bad things happened. Everyone should have cleaned up. */
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_log_rerror(APLOG_MARK, APLOG_ERR, rc, r,
c6d33447e28403a90ad817dba4df75fae785be28pquerna "couldn't create child process: %d: %s", rc, r->filename);
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna else {
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_note_subprocess(p, procnew, kill_after_timeout);
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna /* Fill in BUFF structure for parents pipe to child's stdout */
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_get_childout(&file, procnew);
c6d33447e28403a90ad817dba4df75fae785be28pquerna iol = ap_create_file_iol(file);
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (!iol)
c6d33447e28403a90ad817dba4df75fae785be28pquerna return APR_EBADF;
c6d33447e28403a90ad817dba4df75fae785be28pquerna *script_in = ap_bcreate(p, B_RD);
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_bpush_iol(*script_in, iol);
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna /* Fill in BUFF structure for parents pipe to child's stdin */
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_get_childin(&file, procnew);
c6d33447e28403a90ad817dba4df75fae785be28pquerna iol = ap_create_file_iol(file);
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (!iol)
c6d33447e28403a90ad817dba4df75fae785be28pquerna return APR_EBADF;
c6d33447e28403a90ad817dba4df75fae785be28pquerna *script_out = ap_bcreate(p, B_WR);
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_bpush_iol(*script_out, iol);
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna /* Fill in BUFF structure for parents pipe to child's stderr */
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_get_childerr(&file, procnew);
c6d33447e28403a90ad817dba4df75fae785be28pquerna iol = ap_create_file_iol(file);
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (!iol)
c6d33447e28403a90ad817dba4df75fae785be28pquerna return APR_EBADF;
c6d33447e28403a90ad817dba4df75fae785be28pquerna *script_err = ap_bcreate(p, B_RD);
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_bpush_iol(*script_err, iol);
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_unblock_alarms();
c6d33447e28403a90ad817dba4df75fae785be28pquerna return (rc);
c6d33447e28403a90ad817dba4df75fae785be28pquerna}
c6d33447e28403a90ad817dba4df75fae785be28pquernastatic ap_status_t build_argv_list(char ***argv, request_rec *r, ap_context_t *p)
c6d33447e28403a90ad817dba4df75fae785be28pquerna{
c6d33447e28403a90ad817dba4df75fae785be28pquerna int numwords, x, idx;
c6d33447e28403a90ad817dba4df75fae785be28pquerna char *w;
c6d33447e28403a90ad817dba4df75fae785be28pquerna const char *args = r->args;
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (!args || !args[0] || strchr(args, '=')) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna numwords = 1;
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna else {
c6d33447e28403a90ad817dba4df75fae785be28pquerna /* count the number of keywords */
c6d33447e28403a90ad817dba4df75fae785be28pquerna for (x = 0, numwords = 2; args[x]; x++) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (args[x] == '+') {
c6d33447e28403a90ad817dba4df75fae785be28pquerna ++numwords;
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna /* Everything is - 1 to account for the first parameter which is the
c6d33447e28403a90ad817dba4df75fae785be28pquerna * program name. We didn't used to have to do this, but APR wants it.
c6d33447e28403a90ad817dba4df75fae785be28pquerna */
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (numwords > APACHE_ARG_MAX - 1) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna numwords = APACHE_ARG_MAX - 1; /* Truncate args to prevent overrun */
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna *argv = (char **) ap_palloc(p, (numwords + 2) * sizeof(char *));
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna for (x = 1, idx = 1; x < numwords; x++) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna w = ap_getword_nulls(p, &args, '+');
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_unescape_url(w);
c6d33447e28403a90ad817dba4df75fae785be28pquerna (*argv)[idx++] = ap_escape_shell_cmd(p, w);
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna (*argv)[idx] = NULL;
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna return APR_SUCCESS;
c6d33447e28403a90ad817dba4df75fae785be28pquerna}
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquernastatic ap_status_t build_command_line(char **c, request_rec *r, ap_context_t *p)
c6d33447e28403a90ad817dba4df75fae785be28pquerna{
c6d33447e28403a90ad817dba4df75fae785be28pquerna#ifdef WIN32
c6d33447e28403a90ad817dba4df75fae785be28pquerna char *quoted_filename = NULL;
c6d33447e28403a90ad817dba4df75fae785be28pquerna char *interpreter = NULL;
c6d33447e28403a90ad817dba4df75fae785be28pquerna file_type_e fileType;
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna *c = NULL;
c6d33447e28403a90ad817dba4df75fae785be28pquerna fileType = ap_get_win32_interpreter(r, &interpreter);
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (fileType == eFileTypeUNKNOWN) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, r,
c6d33447e28403a90ad817dba4df75fae785be28pquerna "%s is not executable; ensure interpreted scripts have "
c6d33447e28403a90ad817dba4df75fae785be28pquerna "\"#!\" first line",
c6d33447e28403a90ad817dba4df75fae785be28pquerna r->filename);
c6d33447e28403a90ad817dba4df75fae785be28pquerna return APR_EBADF;
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna /*
c6d33447e28403a90ad817dba4df75fae785be28pquerna * Build the command string to pass to ap_create_process()
c6d33447e28403a90ad817dba4df75fae785be28pquerna */
c6d33447e28403a90ad817dba4df75fae785be28pquerna quoted_filename = ap_pstrcat(p, "\"", r->filename, "\"", NULL);
if (interpreter && *interpreter) {
*c = ap_pstrcat(p, interpreter, " ", quoted_filename, " ", NULL);
}
else {
*c = ap_pstrcat(p, quoted_filename, " ", NULL);
}
#else
*c = ap_pstrcat(p, r->filename, NULL);
#endif
return APR_SUCCESS;
}
static int cgi_handler(request_rec *r)
{
int retval, nph, dbpos = 0;
char *argv0, *dbuf = NULL;
char *command;
char **argv = NULL;
BUFF *script_out = NULL, *script_in = NULL, *script_err = NULL;
char argsbuffer[HUGE_STRING_LEN];
int is_included = !strcmp(r->protocol, "INCLUDED");
void *sconf = r->server->module_config;
ap_context_t *p;
cgi_server_conf *conf =
(cgi_server_conf *) ap_get_module_config(sconf, &cgi_module);
p = r->main ? r->main->pool : r->pool;
if (r->method_number == M_OPTIONS) {
/* 99 out of 100 CGI scripts, this is all they support */
r->allowed |= (1 << M_GET);
r->allowed |= (1 << M_POST);
return DECLINED;
}
if ((argv0 = strrchr(r->filename, '/')) != NULL)
argv0++;
else
argv0 = r->filename;
nph = !(strncmp(argv0, "nph-", 4));
if (!(ap_allow_options(r) & OPT_EXECCGI) && !is_scriptaliased(r))
return log_scripterror(r, conf, FORBIDDEN, APLOG_NOERRNO,
"Options ExecCGI is off in this directory");
if (nph && is_included)
return log_scripterror(r, conf, FORBIDDEN, APLOG_NOERRNO,
"attempt to include NPH CGI script");
#if defined(OS2) || defined(WIN32)
/* Allow for cgi files without the .EXE extension on them under OS/2 */
if (r->finfo.protection == 0) {
struct stat statbuf;
char *newfile;
newfile = ap_pstrcat(r->pool, r->filename, ".EXE", NULL);
if ((stat(newfile, &statbuf) != 0) || (!S_ISREG(statbuf.st_mode))) {
return log_scripterror(r, conf, NOT_FOUND, 0,
"script not found or unable to stat");
} else {
r->filename = newfile;
}
}
#else
if (r->finfo.protection == 0)
return log_scripterror(r, conf, NOT_FOUND, APLOG_NOERRNO,
"script not found or unable to stat");
#endif
if (r->finfo.filetype == APR_DIR)
return log_scripterror(r, conf, FORBIDDEN, APLOG_NOERRNO,
"attempt to invoke directory as script");
/*
if (!ap_suexec_enabled) {
if (!ap_can_exec(&r->finfo))
return log_scripterror(r, conf, FORBIDDEN, APLOG_NOERRNO,
"file permissions deny server execution");
}
*/
if ((retval = ap_setup_client_block(r, REQUEST_CHUNKED_ERROR)))
return retval;
ap_add_common_vars(r);
/* build the command line */
if (build_command_line(&command, r, p) != APR_SUCCESS) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, errno, r,
"couldn't spawn child process: %s", r->filename);
return HTTP_INTERNAL_SERVER_ERROR;
}
/* build the argument list */
else if (build_argv_list(&argv, r, p) != APR_SUCCESS) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, errno, r,
"couldn't spawn child process: %s", r->filename);
return HTTP_INTERNAL_SERVER_ERROR;
}
argv[0] = ap_pstrdup(p, command);
/* run the script in its own process */
if (run_cgi_child(&script_out, &script_in, &script_err, command, argv, r, p) != APR_SUCCESS) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, errno, r,
"couldn't spawn child process: %s", r->filename);
return HTTP_INTERNAL_SERVER_ERROR;
}
/* Transfer any put/post args, CERN style...
* Note that we already ignore SIGPIPE in the core server.
*/
if (ap_should_client_block(r)) {
int dbsize, len_read;
ap_ssize_t bytes_written;
if (conf->logname) {
dbuf = ap_pcalloc(r->pool, conf->bufbytes + 1);
dbpos = 0;
}
while ((len_read =
ap_get_client_block(r, argsbuffer, HUGE_STRING_LEN)) > 0) {
if (conf->logname) {
if ((dbpos + len_read) > conf->bufbytes) {
dbsize = conf->bufbytes - dbpos;
}
else {
dbsize = len_read;
}
memcpy(dbuf + dbpos, argsbuffer, dbsize);
dbpos += dbsize;
}
(void) ap_bwrite(script_out, argsbuffer, len_read, &bytes_written);
if (bytes_written < len_read) {
/* silly script stopped reading, soak up remaining message */
while (ap_get_client_block(r, argsbuffer, HUGE_STRING_LEN) > 0) {
/* dump it */
}
break;
}
}
ap_bflush(script_out);
}
ap_bclose(script_out);
/* Handle script return... */
if (script_in && !nph) {
const char *location;
char sbuf[MAX_STRING_LEN];
int ret;
if ((ret = ap_scan_script_header_err_buff(r, script_in, sbuf))) {
return log_script(r, conf, ret, dbuf, sbuf, script_in, script_err);
}
#ifdef CHARSET_EBCDIC
/* Now check the Content-Type to decide if conversion is needed */
ap_checkconv(r);
#endif /*CHARSET_EBCDIC*/
location = ap_table_get(r->headers_out, "Location");
if (location && location[0] == '/' && r->status == 200) {
/* Soak up all the script output */
while (ap_bgets(argsbuffer, HUGE_STRING_LEN, script_in) > 0) {
continue;
}
while (ap_bgets(argsbuffer, HUGE_STRING_LEN, script_err) > 0) {
continue;
}
/* This redirect needs to be a GET no matter what the original
* method was.
*/
r->method = ap_pstrdup(r->pool, "GET");
r->method_number = M_GET;
/* We already read the message body (if any), so don't allow
* the redirected request to think it has one. We can ignore
* Transfer-Encoding, since we used REQUEST_CHUNKED_ERROR.
*/
ap_table_unset(r->headers_in, "Content-Length");
ap_internal_redirect_handler(location, r);
return OK;
}
else if (location && r->status == 200) {
/* XX Note that if a script wants to produce its own Redirect
* body, it now has to explicitly *say* "Status: 302"
*/
return REDIRECT;
}
ap_send_http_header(r);
if (!r->header_only) {
ap_send_fb(script_in, r);
}
ap_bclose(script_in);
while (ap_bgets(argsbuffer, HUGE_STRING_LEN, script_err) > 0) {
continue;
}
ap_bclose(script_err);
}
if (script_in && nph) {
ap_send_fb(script_in, r);
}
return OK; /* NOT r->status, even if it has changed. */
}
static const handler_rec cgi_handlers[] =
{
{CGI_MAGIC_TYPE, cgi_handler},
{"cgi-script", cgi_handler},
{NULL}
};
module MODULE_VAR_EXPORT cgi_module =
{
STANDARD20_MODULE_STUFF,
NULL, /* dir config creater */
NULL, /* dir merger --- default is to override */
create_cgi_config, /* server config */
merge_cgi_config, /* merge server config */
cgi_cmds, /* command ap_table_t */
cgi_handlers, /* handlers */
NULL /* register hooks */
};