+ All Categories
Home > Technology > Building apache modules

Building apache modules

Date post: 06-May-2015
Category:
Upload: marian-marinov
View: 1,024 times
Download: 1 times
Share this document with a friend
Description:
This presentation outlines the basics about building apache modules.
Popular Tags:
29
Building Apache Modules Building Apache Modules Marian HackMan Marinov Founder and CEO of 1H Ltd. E-Mail: [email protected] Jabber: [email protected]
Transcript
Page 1: Building apache modules

Building Apache ModulesBuilding Apache Modules

Marian HackMan MarinovFounder and CEO of 1H Ltd.

E-Mail: [email protected]: [email protected]

Page 2: Building apache modules

Apache architecture

➢ Request handling

➢ Memory handling

➢ Module architecture

Page 3: Building apache modules

Request handling

1. Just after reading the request (no parsing has been done)

2. Resolve the file name that this URI is trying to access

3. Parse the headers

4. Check access (allow/deny IP/Net/Host)

5. Check the authentication

6. Only if authenticated

7. Check and set the request MIME type

8. Generic fixups

9. Check which (module)function will handle the request

10. Do something before the actual log is called

Page 4: Building apache modules

Request handling

Apache 2.x has two additional hooks:

➢map_to_storage – runs just before the header parser➢ Reads the per-directory configuration

➢ insert_filter – runs just before the handlers➢ Insert content filter

Page 5: Building apache modules

Apache memory pools

➢Apache allocate memory on pools, which are shared across all modules within the same child process/thread.

➢malloc/free is handled by Apache

➢configuration is copied across all child processes

➢misbehaving modules within Apache(mod_php)

➢Apache common functions

➢ Apache 1.3 - palloc()

➢ Apaceh 2.x – palloc(), string handling

Page 6: Building apache modules

Module architecture

➢ Options table

➢ Access table

➢ Authentication table

➢ Handlers table

Page 7: Building apache modules

How to start

➢mod_example➢ Apache 1.3

src/modules/example/mod_example.c➢ Apache 2.x

modules/experimental/mod_example.c

➢ Include files

Page 8: Building apache modules

Structure of a module

➢Includes

➢Module name

➢Module definition

➢Module commands (options)

➢Module configuration

Page 9: Building apache modules

Includes

➢httpd.h - the main include (consider it as, stdio.h)➢ conn_rec, server_rec, request_rec, apache common

functions

➢http_config.h - configuration

➢ module command definitions

➢ additional functions related to the apache configuration

➢http_request.h - request handling functions

➢http_protocol.h - low level direct manipulation of the request

Page 10: Building apache modules

Includes

➢http_core.h - this file gives you everything that is actually provided by mod_core

➢http_main.h - apache server handling

➢http_log.h - logging facilities

➢ For Apache 2.0 and 2.2 ➢ some apache common functions are moved in apr_strings.h

➢ in order to use the request and protocol functionality you would need also http_connection.h

➢ if you need server implementation functions you need ap_mpm.h

➢These are not all includes that you may need in your modules

Page 11: Building apache modules

Module names

#ifdef APACHE2

module AP_MODULE_DECLARE_DATA example_module;

#else

module MODULE_VAR_EXPORT example_module;

#endif

Page 12: Building apache modules

Module definition

➢ Apache 1.3

➢ Apache 2.x

Page 13: Building apache modules

Apache 1.3 module MODULE_VAR_EXPORT example_module = {

STANDARD_MODULE_STUFF,

NULL, /* module initializer */

NULL, /* per-directory config creator */

NULL, /* dir config merger */

NULL, /* server config creator */

NULL, /* server config merger */

NULL, /* command table */

NULL, /* [9] list of handlers */

NULL, /* [2] filename-to-URI translation */

NULL, /* [5] check/validate user_id */

Page 14: Building apache modules

Apache 1.3 NULL, /* [6] check user_id is valid *here* */

NULL, /* [4] check access by host address */

NULL, /* [7] MIME type checker/setter */

NULL, /* [8] fixups */

NULL, /* [10] logger */

NULL, /* [3] header parser */

NULL, /* process initializer */

NULL, /* process exit/cleanup */

NULL /* [1] post read_request handling */

};

Page 15: Building apache modules

Apache 2.x module AP_MODULE_DECLARE_DATA example_module = {

STANDARD20_MODULE_STUFF,

NULL, /* per-directory config creator */

NULL, /* dir config merger */

NULL, /* server config creator */

NULL, /* server config merger */

NULL, /* command table */

NULL, /* set up other request processing hooks */

};

static void register_hooks(apr_pool_t *p) {

static const char * const aszPost[] = { "mod_setenvif.c", NULL };

/* 19 different processing hooks available */

}

Page 16: Building apache modules

Module initialization

➢Apache 1.3 ➢ startup and live

➢ the function is called two times

➢Apache 2.0/2.2 module structure➢ pre-config

➢ post-config

Page 17: Building apache modules

Creating new configuration directives

➢Define them in the module configuration structure

➢Define new module commands(options)

➢Create functions to actually set the options, when found in the config

Page 18: Building apache modules

Module configuration structure

typedef struct {

double dummy_double;

int dummy_int;

char *dummy_char;

array_header *dummy_array;

} example_conf;

Page 19: Building apache modules

Array handling

struct ip_range *net;

char *proxies[] = IP_RANGES;

pool *p;

conf->nets = ap_make_array(p, IP_RANGES_COUNT,

sizeof(struct ip_range));

for (i = 0; i < IP_RANGES_COUNT; i++) {

net = (struct ip_range *) ap_push_array(conf->nets);

parse_ip( p, proxies[ i ], net );

}

Page 20: Building apache modules

Module commands(options)

➢ Apache 1.3

static const command_rec example_cmds[] = {

{

"Example",

cmd_example,

NULL, /* argument to include in call */

OR_OPTIONS,

NO_ARGS,

"Example directive - no arguments"

},

{NULL}

};

Page 21: Building apache modules

Module commands(options)

➢ Apache 2.x

static const command_rec x_cmds[] = {

AP_INIT_NO_ARGS(

"Example",

cmd_example,

NULL, /* argument to include in call */

OR_OPTIONS,

"Example directive - no arguments"

),

{NULL}

};

Page 22: Building apache modules

Possible arguments

➢ FLAG => One of 'On' or 'Off'

➢ NO_ARGS => No args at all, e.g. </Directory>

➢ RAW_ARGS => cmd_func parses command line itself

➢ TAKE1 => one argument only

➢ TAKE2 => two arguments only

➢ ITERATE => one argument, occuring multiple times

➢ ITERATE2 => two arguments, 2nd occurs multiple times

➢ TAKE12 => one or two arguments

➢ TAKE3 => three arguments only

➢ TAKE23 => two or three arguments

➢ TAKE123 => one, two or three arguments

➢ TAKE13 => one or three arguments

Page 23: Building apache modules

Allowed locations for configuration directives

➢ RSRC_CONF => *.conf outside <Directory> or <Location>

➢ ACCESS_CONF=> *.conf inside <Directory> or <Location>

➢ OR_AUTHCFG) => *.conf inside <Directory> or <Location> and .htaccess when AllowOverride AuthConfig

➢ OR_LIMIT) => *.conf inside <Directory> or <Location>

and .htaccess when AllowOverride Limit

➢ OR_OPTIONS) => *.conf anywhere

and .htaccess when AllowOverride Options

➢ OR_FILEINFO) => *.conf anywhere

and .htaccess when AllowOverride FileInfo

➢ OR_INDEXES) => *.conf anywhere

and .htaccess when AllowOverride Indexes

Page 24: Building apache modules

Module command handlers

static const char *set_examplecmd(cmd_parms *cmd, void *vconf, char *arg) { example_conf *conf = (example_conf *) vconf; if (cmd->path == NULL) { conf = (example_conf *) ap_get_module_config(cmd->server->module_config, &example_module);#ifdef DEBUG_OPTIONS fprintf(stderr, "mod_example: (%s:%d)Example (server)\n", cmd->config_file->name, cmd->config_file->line_number); } else { fprintf(stderr, "mod_example: (%s:%d)RelaxPerms (dir)\n", cmd->config_file->name, cmd->config_file->line_number);#endif } conf->dummy_char = ap_pstrdup(cmd->pool, arg); return NULL;}

Page 25: Building apache modules

Handling configuration directives

➢Global vs. Vhost configuration

➢ Per directory/files/location

➢ Merge of the global and per-vhost configuration

➢ Initialization of the configuration

Page 26: Building apache modules

Logging

➢ap_log_error(APLOG_MARK, APLOG_INFO,

r->server, "Some string");

➢ap_log_rerror(APLOG_MARK, APLOG_INFO,

r, "Some string");➢ Apache 2.x

➢ Added ap_log_cerror() for conn_rec handling➢ Added ap_log_perror() for everything that is not server,

record or connection related➢ Added status code between the server and level

Page 27: Building apache modules

Writing portable modules

➢Porting tricks

#ifndef APACHE_RELEASE

#define APACHE2

#endif

➢Apache common functions➢ ap_palloc(1.3) -> apr_palloc(2.x)

Page 28: Building apache modules

Additional resources

http://httpd.apache.org/docs/2.2/developer/

Page 29: Building apache modules

Thank you

??? Questions ???

Thank you


Recommended