"Fossies" - the Fresh Open Source Software Archive

Member "mod_relocate-1.0/mod_relocate.c" (17 Jul 2000, 4607 Bytes) of package /linux/www/apache_httpd_modules/old/mod_relocate-1.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /* 
    2 **  mod_relocate.c -- Log relocation hits
    3 */ 
    4 
    5 #include "httpd.h"
    6 #include "http_config.h"
    7 #include "http_protocol.h"
    8 #include "ap_config.h"
    9 #include "http_log.h"
   10 
   11 typedef struct {
   12     char *hostname_key;
   13     char *trigger;
   14 } relocate_conf;
   15 
   16 
   17 static void *create_dir_mconfig(pool *p, char *dir) {
   18     relocate_conf *cfg;
   19     cfg = ap_pcalloc(p, sizeof(relocate_conf));
   20     cfg->hostname_key = ap_pstrdup(p, "_URL");
   21     cfg->trigger = NULL;
   22 
   23     return (void *) cfg;
   24 }
   25 
   26 module MODULE_VAR_EXPORT relocate_module;
   27 
   28 static int include_virtual(request_rec *r, char *uri) {
   29     int status = OK;
   30     request_rec *subr;
   31 
   32     /* Better safe then sorry */
   33     ap_table_set(r->headers_in, "Content-Length", "0");
   34     subr = (request_rec *) ap_sub_req_method_uri("GET", uri, r);
   35     ap_table_set(subr->subprocess_env, "RELOCATE_SCRIPT_NAME", r->uri);
   36     ap_table_set(subr->subprocess_env, "RELOCATE_PATH_INFO", r->path_info);
   37     ap_table_set(subr->subprocess_env, "RELOCATE_QUERY_STRING", r->args);
   38     status = ap_run_sub_req(subr);
   39     ap_destroy_sub_req(subr);
   40 
   41     return status;
   42 }
   43 
   44 
   45 const char *url_rebuild (request_rec *r, char *hostname_key) {
   46     const char *data = NULL;
   47     const char *key = NULL;
   48     const char *url = NULL;
   49     const char *returntosender = NULL;
   50     int key_size = strlen(hostname_key);
   51 
   52     data = ap_pstrdup(r->pool, r->args);
   53     while(*data && (key = ap_getword(r->pool, &data, '&'))){
   54         if(!strncmp(key, hostname_key, key_size)) {
   55             (void)ap_getword(r->pool, &key, '=');
   56             url = (char *)ap_pstrdup(r->pool, key);
   57         } else {
   58             if (returntosender) {
   59                 returntosender = ap_pstrcat(r->pool, returntosender, "&", key, NULL);
   60             } else {
   61                 returntosender = ap_pstrdup(r->pool, key);
   62             }
   63         }
   64     }
   65 
   66     /* Now lets put her back together */
   67     if(!url) {
   68         return NULL;  
   69     }
   70 
   71     if(returntosender) {
   72         if(ap_rind(url, '?') != -1) {
   73             return ap_pstrcat(r->pool, url, "&", returntosender, r->path_info, NULL);  
   74         }else {
   75             return ap_pstrcat(r->pool, url, "?", returntosender, r->path_info, NULL);  
   76         }
   77     } else {
   78         if (r->path_info) {
   79             return ap_pstrcat(r->pool, url, r->path_info, NULL);
   80         } else {
   81             return ap_pstrcat(r->pool, url, NULL);
   82         }
   83     }
   84 }
   85 
   86 static int relocate_handler(request_rec *r) {
   87     const char *url;
   88     int rc;
   89     relocate_conf *cfg = (relocate_conf *)ap_get_module_config(r->per_dir_config, &relocate_module);
   90 
   91     if (r->header_only)
   92         return OK;
   93     if(!(url = (const char *)url_rebuild(r, cfg->hostname_key))) {
   94         return 404;
   95     }
   96 
   97     if (cfg->trigger) {
   98         if((rc = include_virtual(r,cfg->trigger)) != OK) {
   99               ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, r, "The following error occured while processing trigger %s : %d", cfg->trigger, rc);
  100         }
  101 
  102     }
  103 
  104     ap_table_set(r->headers_out, "Location", url);
  105 
  106     return HTTP_MOVED_TEMPORARILY;
  107 }
  108 
  109 static const command_rec relocate_cmds[] = {
  110   {"RelocatURLKey", ap_set_string_slot, (void *) XtOffsetOf(relocate_conf, hostname_key), OR_OPTIONS, TAKE1, "Enter a string which will override the default _URL key."},
  111   {"RelocateTrigger", ap_set_string_slot, (void *) XtOffsetOf(relocate_conf, trigger), OR_OPTIONS, TAKE1, "Enter a uri which will be called whenever a relocation occurs."},
  112   {NULL},
  113 };
  114 
  115 
  116 /* Dispatch list of content handlers */
  117 static const handler_rec relocate_handlers[] = { 
  118     { "relocate", relocate_handler }, 
  119     { NULL, NULL }
  120 };
  121 
  122 /* Dispatch list for API hooks */
  123 module MODULE_VAR_EXPORT relocate_module = {
  124     STANDARD_MODULE_STUFF, 
  125     NULL,                  /* module initializer                  */
  126     create_dir_mconfig,    /* create per-dir    config structures */
  127     NULL,                  /* merge  per-dir    config structures */
  128     NULL,                  /* create per-server config structures */
  129     NULL,                  /* merge  per-server config structures */
  130     relocate_cmds,         /* table of config file commands       */
  131     relocate_handlers,     /* [#8] MIME-typed-dispatched handlers */
  132     NULL,                  /* [#1] URI to filename translation    */
  133     NULL,                  /* [#4] validate user id from request  */
  134     NULL,                  /* [#5] check if the user is ok _here_ */
  135     NULL,                  /* [#3] check access by host address   */
  136     NULL,                  /* [#6] determine MIME type            */
  137     NULL,                  /* [#7] pre-run fixups                 */
  138     NULL,                  /* [#9] log a transaction              */
  139     NULL,                  /* [#2] header parser                  */
  140     NULL,                  /* child_init                          */
  141     NULL,                  /* child_exit                          */
  142     NULL                   /* [#0] post read-request              */
  143 };
  144