util_script.c revision 842ae4bd224140319ae7feec1872b93dfd491143
fae04f4a69922eb1ddf0f46b34fa15a5a080b693Felix Gabriel Mance/* Licensed to the Apache Software Foundation (ASF) under one or more
fae04f4a69922eb1ddf0f46b34fa15a5a080b693Felix Gabriel Mance * contributor license agreements. See the NOTICE file distributed with
fae04f4a69922eb1ddf0f46b34fa15a5a080b693Felix Gabriel Mance * this work for additional information regarding copyright ownership.
fae04f4a69922eb1ddf0f46b34fa15a5a080b693Felix Gabriel Mance * The ASF licenses this file to You under the Apache License, Version 2.0
fae04f4a69922eb1ddf0f46b34fa15a5a080b693Felix Gabriel Mance * (the "License"); you may not use this file except in compliance with
fae04f4a69922eb1ddf0f46b34fa15a5a080b693Felix Gabriel Mance * the License. You may obtain a copy of the License at
fae04f4a69922eb1ddf0f46b34fa15a5a080b693Felix Gabriel Mance * http://www.apache.org/licenses/LICENSE-2.0
28b76e08d15cd924bb672449613c410a33361b6eFelix Gabriel Mance * Unless required by applicable law or agreed to in writing, software
fae04f4a69922eb1ddf0f46b34fa15a5a080b693Felix Gabriel Mance * distributed under the License is distributed on an "AS IS" BASIS,
fae04f4a69922eb1ddf0f46b34fa15a5a080b693Felix Gabriel Mance * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
fae04f4a69922eb1ddf0f46b34fa15a5a080b693Felix Gabriel Mance * See the License for the specific language governing permissions and
fae04f4a69922eb1ddf0f46b34fa15a5a080b693Felix Gabriel Mance * limitations under the License.
ae2e84ab0a53874417f01b792cbc6907ee6d09f6Felix Gabriel Mance#include "http_request.h" /* for sub_req_lookup_uri() */
28b76e08d15cd924bb672449613c410a33361b6eFelix Gabriel Mance#include "apr_date.h" /* For apr_date_parse_http() */
28b76e08d15cd924bb672449613c410a33361b6eFelix Gabriel Mance * Various utility functions which are common to a whole lot of
ae2e84ab0a53874417f01b792cbc6907ee6d09f6Felix Gabriel Mance * script-type extensions mechanisms, and might as well be gathered
28b76e08d15cd924bb672449613c410a33361b6eFelix Gabriel Mance * in one place (if only to avoid creating inter-module dependancies
28b76e08d15cd924bb672449613c410a33361b6eFelix Gabriel Mance * where there don't have to be).
28b76e08d15cd924bb672449613c410a33361b6eFelix Gabriel Mance#define MALFORMED_MESSAGE "malformed header from script. Bad header="
ae2e84ab0a53874417f01b792cbc6907ee6d09f6Felix Gabriel Mance#define MALFORMED_HEADER_LENGTH_TO_SHOW 30
45e34c7696f9dd6163686ff6798b33a126590fa2Felix Gabriel Mancestatic char *http2env(apr_pool_t *a, const char *w)
45e34c7696f9dd6163686ff6798b33a126590fa2Felix Gabriel Mance char *res = (char *)apr_palloc(a, sizeof("HTTP_") + strlen(w));
808e2693447ecc5a311a2b9de6b81ca07f193778Felix Gabriel Mance while ((c = *w++) != 0) {
28b76e08d15cd924bb672449613c410a33361b6eFelix Gabriel ManceAP_DECLARE(char **) ap_create_environment(apr_pool_t *p, apr_table_t *t)
c92573b85930868b709024284c0f13dbcaec9554Felix Gabriel Mance const apr_array_header_t *env_arr = apr_table_elts(t);
28b76e08d15cd924bb672449613c410a33361b6eFelix Gabriel Mance const apr_table_entry_t *elts = (const apr_table_entry_t *) env_arr->elts;
c92573b85930868b709024284c0f13dbcaec9554Felix Gabriel Mance char **env = (char **) apr_palloc(p, (env_arr->nelts + 2) * sizeof(char *));
ce5d83770556362fe2c8b567975c2a3758888358Felix Gabriel Mance env[j++] = apr_pstrcat(p, "TZ=", tz, NULL);
ce5d83770556362fe2c8b567975c2a3758888358Felix Gabriel Mance env[j] = apr_pstrcat(p, elts[i].key, "=", elts[i].val, NULL);
ee87c7423bf599b4f29e9b31945e00482a8b31caFelix Gabriel Mance if (!apr_isalnum(*whack) && *whack != '_') {
ce5d83770556362fe2c8b567975c2a3758888358Felix Gabriel ManceAP_DECLARE(void) ap_add_common_vars(request_rec *r)
e615997caa046409fc68114cd72e10a528a4bb71Felix Gabriel Mance#if defined(WIN32) || defined(OS2) || defined(BEOS)
31f536d7b9e901779ab6de28d804f53d5eb4b908Felix Gabriel Mance const apr_array_header_t *hdrs_arr = apr_table_elts(r->headers_in);
e615997caa046409fc68114cd72e10a528a4bb71Felix Gabriel Mance const apr_table_entry_t *hdrs = (const apr_table_entry_t *) hdrs_arr->elts;
28b76e08d15cd924bb672449613c410a33361b6eFelix Gabriel Mance /* use a temporary apr_table_t which we'll overlap onto
28b76e08d15cd924bb672449613c410a33361b6eFelix Gabriel Mance * r->subprocess_env later
28b76e08d15cd924bb672449613c410a33361b6eFelix Gabriel Mance * (exception: if r->subprocess_env is empty at the start,
28b76e08d15cd924bb672449613c410a33361b6eFelix Gabriel Mance * write directly into it)
28b76e08d15cd924bb672449613c410a33361b6eFelix Gabriel Mance if (apr_is_empty_table(r->subprocess_env)) {
28b76e08d15cd924bb672449613c410a33361b6eFelix Gabriel Mance e = apr_table_make(r->pool, 25 + hdrs_arr->nelts);
28b76e08d15cd924bb672449613c410a33361b6eFelix Gabriel Mance /* First, add environment vars from headers... this is as per
28b76e08d15cd924bb672449613c410a33361b6eFelix Gabriel Mance * CGI specs, though other sorts of scripting interfaces see
28b76e08d15cd924bb672449613c410a33361b6eFelix Gabriel Mance * the same vars...
28b76e08d15cd924bb672449613c410a33361b6eFelix Gabriel Mance /* A few headers are special cased --- Authorization to prevent
28b76e08d15cd924bb672449613c410a33361b6eFelix Gabriel Mance * rogue scripts from capturing passwords; content-type and -length
28b76e08d15cd924bb672449613c410a33361b6eFelix Gabriel Mance * for no particular reason.
28b76e08d15cd924bb672449613c410a33361b6eFelix Gabriel Mance if (!strcasecmp(hdrs[i].key, "Content-type")) {
28b76e08d15cd924bb672449613c410a33361b6eFelix Gabriel Mance apr_table_addn(e, "CONTENT_TYPE", hdrs[i].val);
28b76e08d15cd924bb672449613c410a33361b6eFelix Gabriel Mance else if (!strcasecmp(hdrs[i].key, "Content-length")) {
28b76e08d15cd924bb672449613c410a33361b6eFelix Gabriel Mance apr_table_addn(e, "CONTENT_LENGTH", hdrs[i].val);
4440f5c4ab1cb6dfd445da97f87a72d87d24c25aFelix Gabriel Mance * You really don't want to disable this check, since it leaves you
28b76e08d15cd924bb672449613c410a33361b6eFelix Gabriel Mance * wide open to CGIs stealing passwords and people viewing them
28b76e08d15cd924bb672449613c410a33361b6eFelix Gabriel Mance * in the environment with "ps -e". But, if you must...
28b76e08d15cd924bb672449613c410a33361b6eFelix Gabriel Mance else if (!strcasecmp(hdrs[i].key, "Authorization")
28b76e08d15cd924bb672449613c410a33361b6eFelix Gabriel Mance || !strcasecmp(hdrs[i].key, "Proxy-Authorization")) {
28b76e08d15cd924bb672449613c410a33361b6eFelix Gabriel Mance apr_table_addn(e, http2env(r->pool, hdrs[i].key), hdrs[i].val);
28b76e08d15cd924bb672449613c410a33361b6eFelix Gabriel Mance apr_table_addn(e, "PATH", apr_pstrdup(r->pool, env_path));
28b76e08d15cd924bb672449613c410a33361b6eFelix Gabriel Mance apr_table_addn(e, "SystemRoot", env_temp);
28b76e08d15cd924bb672449613c410a33361b6eFelix Gabriel Mance if ((env_temp = getenv("COMSPEC")) != NULL) {
28b76e08d15cd924bb672449613c410a33361b6eFelix Gabriel Mance if ((env_temp = getenv("ETC")) != NULL) {
28b76e08d15cd924bb672449613c410a33361b6eFelix Gabriel Mance if ((env_temp = getenv("DPATH")) != NULL) {
28b76e08d15cd924bb672449613c410a33361b6eFelix Gabriel Mance if ((env_temp = getenv("PERLLIB_PREFIX")) != NULL) {
28b76e08d15cd924bb672449613c410a33361b6eFelix Gabriel Mance apr_table_addn(e, "PERLLIB_PREFIX", env_temp);
28b76e08d15cd924bb672449613c410a33361b6eFelix Gabriel Mance if ((env_temp = getenv("LIBRARY_PATH")) != NULL) {
28b76e08d15cd924bb672449613c410a33361b6eFelix Gabriel Mance apr_table_addn(e, "LIBRARY_PATH", env_temp);
28b76e08d15cd924bb672449613c410a33361b6eFelix Gabriel Mance apr_table_addn(e, "SERVER_SIGNATURE", ap_psignature("", r));
28b76e08d15cd924bb672449613c410a33361b6eFelix Gabriel Mance apr_table_addn(e, "SERVER_SOFTWARE", ap_get_server_version());
28b76e08d15cd924bb672449613c410a33361b6eFelix Gabriel Mance ap_escape_html(r->pool, ap_get_server_name(r)));
28b76e08d15cd924bb672449613c410a33361b6eFelix Gabriel Mance apr_table_addn(e, "SERVER_ADDR", r->connection->local_ip); /* Apache */
28b76e08d15cd924bb672449613c410a33361b6eFelix Gabriel Mance apr_psprintf(r->pool, "%u", ap_get_server_port(r)));
28b76e08d15cd924bb672449613c410a33361b6eFelix Gabriel Mance host = ap_get_remote_host(c, r->per_dir_config, REMOTE_HOST, NULL);
28b76e08d15cd924bb672449613c410a33361b6eFelix Gabriel Mance apr_table_addn(e, "REMOTE_ADDR", c->remote_ip);
28b76e08d15cd924bb672449613c410a33361b6eFelix Gabriel Mance apr_table_addn(e, "DOCUMENT_ROOT", ap_document_root(r)); /* Apache */
28b76e08d15cd924bb672449613c410a33361b6eFelix Gabriel Mance apr_table_addn(e, "SERVER_ADMIN", s->server_admin); /* Apache */
28b76e08d15cd924bb672449613c410a33361b6eFelix Gabriel Mance apr_table_addn(e, "SCRIPT_FILENAME", r->filename); /* Apache */
28b76e08d15cd924bb672449613c410a33361b6eFelix Gabriel Mance apr_table_addn(e, "REMOTE_PORT", apr_itoa(r->pool, rport));
28b76e08d15cd924bb672449613c410a33361b6eFelix Gabriel Mance apr_table_addn(e, "REMOTE_USER", r->user);
61c1a9c658ddc79ef33b01c14ee8cf5039430dd8Felix Gabriel Mance else if (r->prev) {
28b76e08d15cd924bb672449613c410a33361b6eFelix Gabriel Mance apr_table_addn(e, "REDIRECT_REMOTE_USER", back->user);
28b76e08d15cd924bb672449613c410a33361b6eFelix Gabriel Mance apr_table_addn(e, "AUTH_TYPE", r->ap_auth_type);
28b76e08d15cd924bb672449613c410a33361b6eFelix Gabriel Mance apr_table_addn(e, "REMOTE_IDENT", apr_pstrdup(r->pool, rem_logname));
28b76e08d15cd924bb672449613c410a33361b6eFelix Gabriel Mance /* Apache custom error responses. If we have redirected set two new vars */
28b76e08d15cd924bb672449613c410a33361b6eFelix Gabriel Mance apr_table_addn(e, "REDIRECT_QUERY_STRING", r->prev->args);
28b76e08d15cd924bb672449613c410a33361b6eFelix Gabriel Mance apr_table_addn(e, "REDIRECT_URL", r->prev->uri);
28b76e08d15cd924bb672449613c410a33361b6eFelix Gabriel Mance apr_table_overlap(r->subprocess_env, e, APR_OVERLAP_TABLES_SET);
a57de3f6b0a0c904523910869eedc7428e5e17a3Felix Gabriel Mance/* This "cute" little function comes about because the path info on
a57de3f6b0a0c904523910869eedc7428e5e17a3Felix Gabriel Mance * filenames and URLs aren't always the same. So we take the two,
a57de3f6b0a0c904523910869eedc7428e5e17a3Felix Gabriel Mance * and find as much of the two that match as possible.
a57de3f6b0a0c904523910869eedc7428e5e17a3Felix Gabriel ManceAP_DECLARE(int) ap_find_path_info(const char *uri, const char *path_info)
a57de3f6b0a0c904523910869eedc7428e5e17a3Felix Gabriel Mance while (lu-- && lp-- && uri[lu] == path_info[lp]) {
a57de3f6b0a0c904523910869eedc7428e5e17a3Felix Gabriel Mance while (uri[lu] != '\0' && uri[lu] != '/') {
a57de3f6b0a0c904523910869eedc7428e5e17a3Felix Gabriel Mance/* Obtain the Request-URI from the original request-line, returning
a57de3f6b0a0c904523910869eedc7428e5e17a3Felix Gabriel Mance * a new string from the request pool containing the URI or "".
a57de3f6b0a0c904523910869eedc7428e5e17a3Felix Gabriel Mancestatic char *original_uri(request_rec *r)
61c1a9c658ddc79ef33b01c14ee8cf5039430dd8Felix Gabriel Mance return (char *) apr_pcalloc(r->pool, 1);
61c1a9c658ddc79ef33b01c14ee8cf5039430dd8Felix Gabriel Mance first = r->the_request; /* use the request-line */
61c1a9c658ddc79ef33b01c14ee8cf5039430dd8Felix Gabriel Mance while (*first && !apr_isspace(*first)) {
NULL);
NULL);
#ifdef WIN32
int (*getsfunc) (char *, int, void *),
void *getsfunc_data)
char x[MAX_STRING_LEN];
if (buffer) {
return HTTP_INTERNAL_SERVER_ERROR;
p = strlen(w);
return cond_status;
++maybeEBCDIC;
++maybeASCII;
r->filename);
if (!buffer) {
return HTTP_INTERNAL_SERVER_ERROR;
while (*l && apr_isspace(*l)) {
char *tmp;
return OK;
char *buffer)
int done = 0;
const char *bucket_data;
const char *src;
const char *src_end;
src++;
e = next;
*dst = 0;
char *buffer)
struct vastrs {
int arg;
const char *curpos;
if (t > len)
t = len;
char *buffer,
const char **termch,
int *termarg, ...)
int res;
if (termch)
if (termarg)
return res;