home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 8 / CDACTUAL8.iso / share / os2 / varios / apache / mod_acti.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-15  |  7.1 KB  |  213 lines

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