Create Web Server Filter by apache module

[japanese(日本語)]

You can controll Web server (IIS, apache) with adding module as follows.

 

This is a exsample that display  “overtime” when it was accessed except from 8:00 to 18:00.

 

1. Environment

OS: CentOS7.7

apache: 2.4.6

 

2. Install apxs

yum install httpd-devel

 

3. Create template

cd /tmp/work/

apxs -g -n sample01

 

4. modify source code.

/* The sample content handler */
static int sample01_handler(request_rec *r)
{
  time_t timer;
  struct tm *t_st;
  char buff[256];
  time(&timer);
  t_st = localtime(&timer);

  sprintf(buff,"%02d%02d", t_st->tm_hour, t_st->tm_min);
  if(strcmp(buff,"0800")>=0 && (strcmp(buff,"1800")<=0)){
    return DECLINED;
  }
  else {
    r->content_type = "text/html";
    if(!r->header_only){
      ap_rputs("<html><body>overtime</body></html>", r);
    }
    return OK;
  }
}

static void sample01_register_hooks(apr_pool_t *p)
{
  ap_hook_handler(sample01_handler, NULL, NULL, APR_HOOK_FIRST);
}

 

5. Make

apxs -c mod_sample01.c

 

6. copy module to apache modules directory.

cp .libs/mod_sample01.so  /etc/httpd/modules/

 

7. modify apache config.  order to load the module at first.

#                                                                                                                                   
# Dynamic Shared Object (DSO) Support                                                                                               
#                                                                                                                                   
# To be able to use the functionality of a module which was built as a DSO you                                                      
# have to place corresponding `LoadModule' lines at this location so the                                                            
# directives contained in it are actually available _before_ they are used.                                                         
# Statically compiled modules (those listed by `httpd -l') do not need                                                              
# to be loaded here.                                                                                                                
#                                                                                                                                   
# Example:                                                                                                                          
# LoadModule foo_module modules/mod_foo.so                                                                                          
#                                                                                                                                   

LoadModule sample01_module modules/mod_sample01.so                                                                                 
Include conf.modules.d/*.conf

 

8. restart apache