home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 25 / CDROM25.iso / Share / linux / apache / contrib / modules / probably_obsolete / mod_counter.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-11  |  7.0 KB  |  239 lines

  1.  
  2. /*-
  3.  * Copyright (c) 1995 The Apache Group. All rights reserved.
  4.  * 
  5.  *
  6.  * Apache httpd license
  7.  * ====================
  8.  * 
  9.  *
  10.  * This is the license for the Apache Server. It covers all the
  11.  * files which come in this distribution, and should never be removed.
  12.  * 
  13.  * The "Apache Group" has based this server, called "Apache", on
  14.  * public domain code distributed under the name "NCSA httpd 1.3".
  15.  * 
  16.  * NCSA httpd 1.3 was placed in the public domain by the National Center 
  17.  * for Supercomputing Applications at the University of Illinois 
  18.  * at Urbana-Champaign.
  19.  * 
  20.  * As requested by NCSA we acknowledge,
  21.  * 
  22.  *  "Portions developed at the National Center for Supercomputing
  23.  *   Applications at the University of Illinois at Urbana-Champaign."
  24.  *
  25.  * Copyright on the sections of code added by the "Apache Group" belong
  26.  * to the "Apache Group" and/or the original authors. The "Apache Group" and
  27.  * authors hereby grant permission for their code, along with the
  28.  * public domain NCSA code, to be distributed under the "Apache" name.
  29.  * 
  30.  * Reuse of "Apache Group" code outside of the Apache distribution should
  31.  * be acknowledged with the following quoted text, to be included with any new
  32.  * work;
  33.  * 
  34.  * "Portions developed by the "Apache Group", taken with permission 
  35.  *  from the Apache Server   http://www.apache.org/apache/   "
  36.  *
  37.  *
  38.  * Permission is hereby granted to anyone to redistribute Apache under
  39.  * the "Apache" name. We do not grant permission for the resale of Apache, but
  40.  * we do grant permission for vendors to bundle Apache free with other software,
  41.  * or to charge a reasonable price for redistribution, provided it is made
  42.  * clear that Apache is free. Permission is also granted for vendors to 
  43.  * sell support for for Apache. We explicitly forbid the redistribution of 
  44.  * Apache under any other name.
  45.  * 
  46.  * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
  47.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  48.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  49.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE
  50.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  51.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  52.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  53.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  54.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  55.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  56.  * SUCH DAMAGE.
  57.  * 
  58.  */
  59.  
  60.  
  61. /* HACK hack HACK
  62.  
  63.    Author: Rob Hartill   hartill@lanl.gov
  64.    
  65.    Module for Apache URL counting
  66.  
  67.    THIS IS JUST AN EXAMPLE OF HOW TO WRITE A COUNTER MODULE IN
  68.    APACHE. I HAVE NO INTENTION OF SUPPORTING THIS. IT'S NOT MEANT
  69.    TO BE EFFICIENT OR WELL DESIGNED. FEEL FREE TO USE IT AS A BASE
  70.    FOR SOMETHING BETTER, THEN HAVE THE BETTER MODULE PUT HERE
  71.    INSTEAD.
  72.    
  73.    CounterLog   is a new config parameter, set it to point to
  74.      a file which contains URL counts.
  75.    The format is
  76.    
  77.    URL-to-matchNNNNNNNNNN\n
  78.    
  79.    e.g.
  80.    
  81.    /index.html0000000000
  82.    /logo.gif0000012345
  83.    /some/subdir/index.gif0000000003
  84.    
  85.    The number at the end is a 10 digit count. Pad with 0s to make it 10 digits
  86.    This module will scan the file for a matching URL, then increment a 
  87.    count. It also creates a "CGI" variable called "URL_COUNTER" for use
  88.    in CGI and includes.
  89.    
  90.    
  91. */
  92.    
  93.     
  94. #define DEFAULT_CNTRLOG "logs/counter_log_default"
  95.  
  96.  
  97. #include "httpd.h"
  98. #include "http_config.h"
  99.  
  100. module counter_module;
  101.  
  102. static int xfer_flags = ( O_RDWR | O_CREAT );
  103. static mode_t xfer_mode = ( S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH );
  104.  
  105. typedef struct {
  106.     char *fname;
  107.     int counter_fd;
  108. } counter_state;
  109.  
  110.  
  111. static int line_read (int fd, char *buffer, int bufsz)
  112. {
  113.     int rv, orig_sz = bufsz;
  114.     char *cptr = buffer;
  115.    
  116.     do {
  117.         rv = read (fd, cptr, 1);
  118.         if (rv > 0) {
  119.         /*fprintf(stderr, "R(%c)",*cptr);*/
  120.             bufsz --;
  121.             if (*cptr++ == '\n') break;
  122.         }
  123.     } while (rv > 0 && bufsz > 0);
  124.     *cptr = '\0';
  125.        
  126.     return rv < 0? rv : orig_sz - bufsz;
  127. }
  128.  
  129. void *make_counter_state (pool *p, server_rec *s)
  130. {
  131.     counter_state *cls =
  132.       (counter_state *)palloc (p, sizeof (counter_state));
  133.  
  134.     cls->fname = DEFAULT_CNTRLOG;
  135.     cls->counter_fd = -1;
  136.  
  137.     return (void *)cls;
  138. }
  139.  
  140. char *set_counter (cmd_parms *parms, void *dummy, char *arg)
  141. {
  142.     counter_state *cls = get_module_config (parms->server->module_config,
  143.                            &counter_module);
  144.   
  145.     cls->fname = arg;
  146.     return NULL;
  147. }
  148.  
  149. command_rec counter_cmds[] = {
  150. { "CounterLog", set_counter, NULL, RSRC_CONF, TAKE1,
  151.     "the filename of the counter log" },
  152. { NULL }
  153. };
  154.  
  155.  
  156. void open_counter (server_rec *s, pool *p)
  157. {
  158.     counter_state *cls = get_module_config (s->module_config,
  159.                            &counter_module);
  160.   
  161.     char *fname = server_root_relative (p, cls->fname);
  162.     
  163.     if (cls->counter_fd > 0) return; /* virtual log shared w/main server */
  164.     
  165.     if((cls->counter_fd = popenf(p, fname, xfer_flags, xfer_mode)) < 0) {
  166.         fprintf(stderr,"httpd: could not open counter log file %s.\n", fname);
  167.         perror("open");
  168.         exit(1);
  169.     }
  170. }
  171.  
  172. void init_counter (server_rec *s, pool *p)
  173. {
  174.     for (; s; s = s->next) open_counter (s, p);
  175. }
  176.  
  177. int look_for_counter_then_increment(request_rec *orig)
  178. {
  179.     counter_state *cls = get_module_config (orig->server->module_config,
  180.                            &counter_module);
  181.   
  182.     char str[HUGE_STRING_LEN];
  183.     request_rec *r;
  184.     int line_len, uri_len;
  185.  
  186.     /* Some comments need to go here */
  187.         
  188.     for (r = orig; r->next; r = r->next)
  189.         continue;
  190.         
  191.     if (!r->the_request) return OK;    
  192.            /* Stops includes incrementing requested URLs counter */
  193.        
  194.     uri_len = strlen(r->uri); 
  195.  
  196.     lseek(cls->counter_fd,0, 0);   /* jump to start of counter log */
  197.  
  198.     /* Search for the URL */
  199.     while (line_read(cls->counter_fd, str, HUGE_STRING_LEN) >0) {
  200.     
  201.        line_len = strlen(str);
  202.        
  203.        if (line_len <12 || (line_len-11) < uri_len) continue;
  204.  
  205.  
  206.        if (!strncmp(str, r->uri, line_len-11)) {
  207.            int count = atoi(str+line_len-10);      
  208.            lseek(cls->counter_fd, -11, 1);   /* reverse 11 bytes */
  209.            sprintf(str, "%010d", ++count);
  210.          
  211.            write(cls->counter_fd, str, 10);
  212.          
  213.            sprintf(str, "%d", count);
  214.            table_set (r->subprocess_env, "URL_COUNTER", str);
  215.        
  216.            return OK;
  217.        }
  218.     }
  219.     return OK;
  220. }
  221.  
  222. module counter_module = {
  223.    STANDARD_MODULE_STUFF,
  224.    init_counter,           /* initializer */
  225.    NULL,               /* create per-dir config */
  226.    NULL,               /* merge per-dir config */
  227.    make_counter_state,               /* server config */
  228.    NULL,               /* merge server config */
  229.    counter_cmds,           /* command table */
  230.    NULL,               /* handlers */
  231.    NULL,               /* filename translation */
  232.    NULL,               /* check_user_id */
  233.    NULL,               /* check auth */
  234.    NULL,               /* check access */
  235.    NULL,               /* type_checker */
  236.    look_for_counter_then_increment,/* fixups */
  237.    NULL                            /* logger */
  238. };
  239.