home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1999 April / PCO0499.ISO / filesbbs / os2 / apach134.arj / APACH134.ZIP / src / modules / standard / mod_actions.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-01-01  |  7.7 KB  |  233 lines

  1. /* ====================================================================
  2.  * Copyright (c) 1995-1999 The Apache Group.  All rights reserved.
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  *
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer. 
  10.  *
  11.  * 2. Redistributions in binary form must reproduce the above copyright
  12.  *    notice, this list of conditions and the following disclaimer in
  13.  *    the documentation and/or other materials provided with the
  14.  *    distribution.
  15.  *
  16.  * 3. All advertising materials mentioning features or use of this
  17.  *    software must display the following acknowledgment:
  18.  *    "This product includes software developed by the Apache Group
  19.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  20.  *
  21.  * 4. The names "Apache Server" and "Apache Group" must not be used to
  22.  *    endorse or promote products derived from this software without
  23.  *    prior written permission. For written permission, please contact
  24.  *    apache@apache.org.
  25.  *
  26.  * 5. Products derived from this software may not be called "Apache"
  27.  *    nor may "Apache" appear in their names without prior written
  28.  *    permission of the Apache Group.
  29.  *
  30.  * 6. Redistributions of any form whatsoever must retain the following
  31.  *    acknowledgment:
  32.  *    "This product includes software developed by the Apache Group
  33.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  34.  *
  35.  * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
  36.  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  37.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  38.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE APACHE GROUP OR
  39.  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  41.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  42.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  44.  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  45.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  46.  * OF THE POSSIBILITY OF SUCH DAMAGE.
  47.  * ====================================================================
  48.  *
  49.  * This software consists of voluntary contributions made by many
  50.  * individuals on behalf of the Apache Group and was originally based
  51.  * on public domain software written at the National Center for
  52.  * Supercomputing Applications, University of Illinois, Urbana-Champaign.
  53.  * For more information on the Apache Group and the Apache HTTP server
  54.  * project, please see <http://www.apache.org/>.
  55.  *
  56.  */
  57.  
  58. /*
  59.  * mod_actions.c: executes scripts based on MIME type or HTTP method
  60.  *
  61.  * by Alexei Kosut; based on mod_cgi.c, mod_mime.c and mod_includes.c,
  62.  * adapted by rst from original NCSA code by Rob McCool
  63.  *
  64.  * Usage instructions:
  65.  *
  66.  * Action mime/type /cgi-bin/script
  67.  * 
  68.  * will activate /cgi-bin/script when a file of content type mime/type is 
  69.  * requested. It sends the URL and file path of the requested document using 
  70.  * the standard CGI PATH_INFO and PATH_TRANSLATED environment variables.
  71.  *
  72.  * Script PUT /cgi-bin/script
  73.  *
  74.  * will activate /cgi-bin/script when a request is received with the
  75.  * HTTP method "PUT".  The available method names are defined in httpd.h.
  76.  * If the method is GET, the script will only be activated if the requested
  77.  * URI includes query information (stuff after a ?-mark).
  78.  */
  79.  
  80. #include "httpd.h"
  81. #include "http_config.h"
  82. #include "http_request.h"
  83. #include "http_core.h"
  84. #include "http_protocol.h"
  85. #include "http_main.h"
  86. #include "http_log.h"
  87. #include "util_script.h"
  88.  
  89. typedef struct {
  90.     table *action_types;       /* Added with Action... */
  91.     char *scripted[METHODS];   /* Added with Script... */
  92. } action_dir_config;
  93.  
  94. module action_module;
  95.  
  96. static void *create_action_dir_config(pool *p, char *dummy)
  97. {
  98.     action_dir_config *new =
  99.     (action_dir_config *) ap_palloc(p, sizeof(action_dir_config));
  100.  
  101.     new->action_types = ap_make_table(p, 4);
  102.     memset(new->scripted, 0, sizeof(new->scripted));
  103.  
  104.     return new;
  105. }
  106.  
  107. static void *merge_action_dir_configs(pool *p, void *basev, void *addv)
  108. {
  109.     action_dir_config *base = (action_dir_config *) basev;
  110.     action_dir_config *add = (action_dir_config *) addv;
  111.     action_dir_config *new = (action_dir_config *) ap_palloc(p,
  112.                                   sizeof(action_dir_config));
  113.     int i;
  114.  
  115.     new->action_types = ap_overlay_tables(p, add->action_types,
  116.                        base->action_types);
  117.  
  118.     for (i = 0; i < METHODS; ++i) {
  119.         new->scripted[i] = add->scripted[i] ? add->scripted[i]
  120.                                             : base->scripted[i];
  121.     }
  122.     return new;
  123. }
  124.  
  125. static const char *add_action(cmd_parms *cmd, action_dir_config * m, char *type,
  126.                   char *script)
  127. {
  128.     ap_table_setn(m->action_types, type, script);
  129.     return NULL;
  130. }
  131.  
  132. static const char *set_script(cmd_parms *cmd, action_dir_config * m,
  133.                               char *method, char *script)
  134. {
  135.     int methnum;
  136.  
  137.     methnum = ap_method_number_of(method);
  138.     if (methnum == M_TRACE)
  139.         return "TRACE not allowed for Script";
  140.     else if (methnum == M_INVALID)
  141.         return "Unknown method type for Script";
  142.     else
  143.         m->scripted[methnum] = script;
  144.  
  145.     return NULL;
  146. }
  147.  
  148. static const command_rec action_cmds[] =
  149. {
  150.     {"Action", add_action, NULL, OR_FILEINFO, TAKE2,
  151.      "a media type followed by a script name"},
  152.     {"Script", set_script, NULL, ACCESS_CONF | RSRC_CONF, TAKE2,
  153.      "a method followed by a script name"},
  154.     {NULL}
  155. };
  156.  
  157. static int action_handler(request_rec *r)
  158. {
  159.     action_dir_config *conf = (action_dir_config *)
  160.         ap_get_module_config(r->per_dir_config, &action_module);
  161.     const char *t, *action = r->handler ? r->handler : r->content_type;
  162.     const char *script;
  163.     int i;
  164.  
  165.     /* Set allowed stuff */
  166.     for (i = 0; i < METHODS; ++i) {
  167.         if (conf->scripted[i])
  168.             r->allowed |= (1 << i);
  169.     }
  170.  
  171.     /* First, check for the method-handling scripts */
  172.     if (r->method_number == M_GET) {
  173.         if (r->args)
  174.             script = conf->scripted[M_GET];
  175.         else
  176.             script = NULL;
  177.     }
  178.     else {
  179.         script = conf->scripted[r->method_number];
  180.     }
  181.  
  182.     /* Check for looping, which can happen if the CGI script isn't */
  183.     if (script && r->prev && r->prev->prev)
  184.     return DECLINED;
  185.  
  186.     /* Second, check for actions (which override the method scripts) */
  187.     if ((t = ap_table_get(conf->action_types,
  188.                action ? action : ap_default_type(r)))) {
  189.     script = t;
  190.     if (r->finfo.st_mode == 0) {
  191.         ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, r,
  192.             "File does not exist: %s", r->filename);
  193.         return NOT_FOUND;
  194.     }
  195.     }
  196.  
  197.     if (script == NULL)
  198.     return DECLINED;
  199.  
  200.     ap_internal_redirect_handler(ap_pstrcat(r->pool, script, ap_escape_uri(r->pool,
  201.               r->uri), r->args ? "?" : NULL, r->args, NULL), r);
  202.     return OK;
  203. }
  204.  
  205. static const handler_rec action_handlers[] =
  206. {
  207.     {"*/*", action_handler},
  208.     {NULL}
  209. };
  210.  
  211. module action_module =
  212. {
  213.     STANDARD_MODULE_STUFF,
  214.     NULL,            /* initializer */
  215.     create_action_dir_config,    /* dir config creater */
  216.     merge_action_dir_configs,    /* dir merger --- default is to override */
  217.     NULL,            /* server config */
  218.     NULL,            /* merge server config */
  219.     action_cmds,        /* command table */
  220.     action_handlers,        /* handlers */
  221.     NULL,            /* filename translation */
  222.     NULL,            /* check_user_id */
  223.     NULL,            /* check auth */
  224.     NULL,            /* check access */
  225.     NULL,            /* type_checker */
  226.     NULL,            /* fixups */
  227.     NULL,            /* logger */
  228.     NULL,            /* header parser */
  229.     NULL,            /* child_init */
  230.     NULL,            /* child_exit */
  231.     NULL            /* post read-request */
  232. };
  233.