Apache Module Development

Source: Internet
Author: User

First, Introduction

The Apache HTTP server is a modular software that allows managers to select the modules included in the core to crop functionality. You can choose a module that is statically included in the httpd binary image at compile time, or it can be compiled into a dynamic shared object independent of the primary httpd binary image DSO,DSO module can be compiled after the server is compiled, or can be compiled and incremented with the Apache Extension tool (APXS).

The Apache module development mainly uses the hook sub-method to implement the module development, this and the Linux kernel module development somewhat resembles, plainly is adds a callback function.

Second, the installation of Apache APXS

APXS is a tool for compiling and installing extensions for Apache HTTP servers that compiles one or more source programs or target code files into a dynamically shared object that can be loaded into the Apache server at run time with loadmodule instructions provided by MOD_SO.

APXS Reference Document: http://lamp.linux.gov.cn/Apache/ApacheMenu/programs/apxs.html

Check if there is httpd-devel this package, if no need to install

grep httpd          #查看 #yuminstall httpd-devel    #安装

Iii. Examples of development

Program 1:apache module Development Content Generator

Execute the following command to generate a HelloWorld template

APXS-G-N HelloWorld

The generated code is as follows:

/** * MOD_HELLOWORLD.C--Apache sample HelloWorld module** [autogenerated via ' apxs-n helloworld-g ']**** to play With this sample module first compile it to a** DSO file and install it into Apache ' s modules directory * * by running :* * * * $ apxs-c-I mod_helloworld.c**** then activate it in Apache's httpd.conf file for instance** for the Url/hell Oworld in as follows:**** # httpd.conf** LoadModule helloworld_module modules/mod_helloworld.so** <location  /helloworld>** SetHandler helloworld** </location>**** Then after restarting Apache via**** $ apachectl restart**** you immediately can request the Url/helloworld and watch for the** output of this module. This can is achieved for instance via:**** $ lynx-mime_headerHttp://localhost/helloworld* * * The output should is similar to the following one:**** http/1.1, ok** date:tue, 1998 14:42:22 gmt** server:apache/1.3.4 (Unix) * * connection:close** content-type:text/html** * * The sample page from mod _HELLOWORLD.C*/#include"httpd.h"#include"http_config.h"#include"http_protocol.h"#include"ap_config.h"/*The sample content handler*/StaticintHelloworld_handler (REQUEST_REC *R) {    if(strcmp (R->handler,"HelloWorld") {return declined; } R->content_type ="text/html"; if(!r->header_only) ap_rputs ("The sample page from mod_helloworld.c\n", R); return OK;} static void Helloworld_register_hooks (apr_pool_t*p) {Ap_hook_handler (helloworld_handler, NULL, NULL, apr_hook_middle);}/*Dispatch list for API hooks*/Module Ap_module_declare_data helloworld_module={standard20_module_stuff, NULL,/*Create PER-DIR config structures*/NULL,/*Merge Per-dir config structures*/NULL,/*Create Per-server config structures*/NULL,/*Merge Per-server config structures*/NULL,/*Table of config file commands*/Helloworld_register_hooks/*Register Hooks*/};

Compile

Apxs-c MOD_HELLOWORLD.C

Installation

Apxs-i mod_helloworld.la

Modify the configuration file httpd.conf to load the module, mainly add the following content

LoadModule helloworld_module modules/mod_helloworld.so<Location/helloworld>   SetHandler HelloWorld</Location>

Restart the server for testing

Apachectl-k Stop     #停止
Apachectl-k Start    #启动

Enter the URL in the browser to see the effect

http://127.0.0.1/helloworld

Program 2:apache Module Development Output Filter

For filters, there are two types of input filters and output filters, in the following order:

Request--Input filter--content generator--Output filter--response

All requests pass through our filters, so we can operate on them, such as statistical traffic, data compression, and so on. The following example turns all lowercase letters in the page to uppercase.

/**
* @file: MOD_CASEFILTER.C
* @brief: Turn all lowercase letters in the page into capitals
*/
#include"httpd.h"#include"http_config.h"#include"Apr_buckets.h"#include"apr_general.h"#include"Apr_lib.h"#include"Util_filter.h"#include"http_request.h"#include<ctype.h>Static Const Chars_szcasefiltername[]="Casefilter"; module Ap_module_declare_data case_filter_module;typedefstruct{    intbenabled;} Casefilterconfig;Static void*casefiltercreateserverconfig (apr_pool_t *p,server_rec *s) {Casefilterconfig*pconfig=apr_pcalloc (P,sizeof*pconfig); Pconfig->benabled=0; returnPconfig;}Static voidCasefilterinsertfilter (REQUEST_REC *R) {Casefilterconfig*pconfig=ap_get_module_config (r->server->Module_config,&case_filter_module); if(!pconfig->benabled)return; Ap_add_output_filter (S_szcasefiltername,null,r,r-connection);}Staticapr_status_t Casefilteroutfilter (ap_filter_t *F, Apr_bucket_brigade*Pbbin) {Request_rec*r = f->R; Conn_rec*c = r->connection; Apr_bucket*Pbktin; Apr_bucket_brigade*pbbout; Pbbout=apr_brigade_create (R->pool, c->Bucket_alloc);  for(Pbktin =Apr_brigade_first (Pbbin); Pbktin!=Apr_brigade_sentinel (Pbbin); Pbktin=Apr_bucket_next (Pbktin)) {        Const Char*data;        apr_size_t Len; Char*buf;        apr_size_t N; Apr_bucket*pbktout; if(Apr_bucket_is_eos (Pbktin)) {Apr_bucket*pbkteos=apr_bucket_eos_create (c->Bucket_alloc);            Apr_brigade_insert_tail (Pbbout,pbkteos); Continue; }        /*Read*/Apr_bucket_read (Pbktin,&data,&len,apr_block_read); /*Write*/buf= Apr_bucket_alloc (len, c->Bucket_alloc);  for(n=0; n < Len; ++N) buf[n]=Apr_toupper (Data[n]); Pbktout=apr_bucket_heap_create (buf, Len, Apr_bucket_free, C-Bucket_alloc);    Apr_brigade_insert_tail (pbbout,pbktout);    } apr_brigade_cleanup (Pbbin); returnAp_pass_brigade (f->next,pbbout);}Static Const Char*casefilterenable (Cmd_parms *cmd,void*dummy,intArg) {Casefilterconfig*pconfig=ap_get_module_config (cmd->server->Module_config,&case_filter_module); Pconfig->benabled=Arg; returnNULL;}Static ConstCommand_rec casefiltercmds[] ={Ap_init_flag ("Casefilter", Casefilterenable, NULL, rsrc_conf,"Run A case filter on the This host"), {NULL}};Static voidCasefilterregisterhooks (apr_pool_t *p)    {Ap_hook_insert_filter (casefilterinsertfilter,null,null,apr_hook_middle); Ap_register_output_filter (S_szcasefiltername,casefilteroutfilter,null, Ap_ftype_resource);} Module Ap_module_declare_data case_filter_module={standard20_module_stuff, NULL, NULL, casefiltercreateserverconfig, NULL, CASEFILTERCMDS, Casefi Lterregisterhooks};

Compile

Apxs-c mod_filter.c

Installation

Apxs-i mod_filter.la

To configure httpd.conf, add the following:

LoadModule Case_filter_module modules/mod_casefilter.so
Casefilter on

Test

Apache Module Development

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.