Apache2.0 MOD module development Step 1

xiaoxiao2021-03-06  46

Apache2.0 MOD module development Step 1

One. purpose

Write an Apache 2.0 MOD module, read the configuration, and process all the suffixes for .hello.

two. step

Create a mod_hello.c file

1. Define a module.

#include "httpd.h"

#include "http_config.h"

Module AP_MODULE_DECLARE_DATA HELLO_MODULE

2. Define the interface.

Module AP_MODULE_DECLARE_DATA HELLO_MODULE =

{

Standard20_module_stuff, // standard stuff; no need to message with this.

Null, // Create Per-Directory Configuration Structures - We do not.

NULL, // Merge Per-Directory - No Need To Merge IF WE Are Not Creating Anything.

CREATE_MODHELLO_CONFIG, // CREATE PER-Server Configuration Structure.

NULL, // Merge Per-Server - HRM - Examples I Have Been Reading Don't Bother with this for trivial caves.

MOD_HELLO_CMDS, // Configuration Directive Handlers

MOD_HELLO_REGISTER_HOOKS, // Request Handlers

}

Description:

Where the CREATE_MODHELLO_CONFIG function is used to allocate space for custom structural assignments, mod_hello_cmds defines a read function of parameter sequences and parameters. MOD_HELLO_REGISTER_HOOKS Defines the request handler

3. Initialization configuration, read configuration.

Definition of configuration structure:

Typedef struct {

Char * Welcome;

INT MAX_PROCESS;

} modhello_config;

Definition of parameters:

Static const command_rec mod_hello_cmds [] =

{

AP_INIT_TAKE1 (

"Welcome",

Set_modhello_string,

NULL,

RSRC_CONF,

"Hello, Apache"

),

AP_INIT_TAKE1 (

"ModuleMaxProcess",

Set_modhello_string,

NULL,

RSRC_CONF,

NULL

),

{Null}

}

The creation of the parameter structure is called by Apache during loading module.

Static void * create_modhello_config (APR_POOL_T * P, Server_Rec * S)

{

Modhello_config * newcfg;

// Allocate Space for the Configuration Structure from the provided pool p.

Newcfg = (MODHELLO_CONFIG *) APR_PCALLOC (p, sizeof (modhello_config);

// Return the New Server Configuration Structure.

Return (void *) newcfg;

}

Parameter read function

Static const char * set_modhello_string (cmd_parms * parms, void * mconfig, const char * arg) {

Modhello_config * s_cfg = ap_get_module_config (PARMS-> Server-> Module_config, & hello_Module);

IF (! Strcmp (Parms-> cmd-> name, "welcome")) {

S_CFG-> Welcome = (char *) arg;

} else if (! Strcmp (Parms-> cmd-> name, "modulemaxprocess")) {

S_CFG-> Max_Process = ATOI (Arg);

}

// SUCCESS

Return NULL;

}

4. Processing request.

Registration request.

Static void mod_hello_register_hooks (APR_POOL_T * P)

{

AP_HOOK_HANDLER (MOD_HELLO_METHOD_HANDLER, NULL, NULL, APR_HOOK_LAST);

}

Request handler

Static int mod_hello_method_handler (Request_rec * R)

{

Modhello_config * s_cfg;

IF ("Hello-script", r-> handler) Return Declined;

S_CFG = AP_GET_MODULE_CONFIG (r-> Server-> module_config, & hello_module);

FPRINTF (stderr, "% s,% s,% d / n", r-> content_type, r-> handler, s_cfg-> max_process);

AP_RPUTS ("Hello, World!", R);

Return 0;

}

three. installation.

Compilation.

Look at Makefile.

All: MOD_HELLO.C

GCC -G -I / Home / WEE / APACHE2 / include / -fpic -o mod_hello.o -c mod_hello.c

GCC -Shared -i / Home / WEE / APACHE2 / include / -o libmodhello.so -lc mod_hello.o

cp * .so / home / wee / apache2 / modules /

Clean:

Rm * .o * .so

2. Configuration.

Modify httpd.conf.

Increase processing:

LoadModule Hello_Module Modules / Libmodhello.so

AddHandler Hello-script .hello

Add parameters:

Welcome "Hello, World"

ModuleMaxProcess 5

3. Installation

GCC -V

Reading

SPECS from /usr/lib/gcc-lib/i386-redhat-linux/2.96/specs

GCC Version 2.96 20000731 (Red Hat Linux 7.3 2.96-110)

Make.

four. test.

Visit http://xxx.xxx.xxx.xxx/a.hello, "Hello, World" is printed on the screen, and there is also print information in the log.

Fives. Reference

1. Http://threebit.net/tutorials/apache2_modules/tut1/tutorial1.html2. Writing.apache modules with perl and c (lincoln stein and doug macechern)

3. Http://apache-modules.com/

4. Http://www.apache.org/

转载请注明原文地址:https://www.9cbs.com/read-81404.html

New Post(0)