Apache module development/C Language extension Apache (3: A very simple Apache module)

Source: Internet
Author: User
Apache module development/C Language extension Apache (3: A very simple Apache module)
This article from: http://www.loveopensource.com /? P = 18 Original article: Apache module development/C Language extension Apache (3: A very simple Apache module)
By linux_prog
With the basics of the above articles, you should try to write some simple modules,
The following is a simple Apache module. Let's take a look at it.
$ CD/usr/local/apache2.2.4
$ VI mod_c.c

#include <time.h>
#include <stdlib.h>
#include "apr.h"
#include "apr_lib.h"
#include "apr_strings.h"

#define APR_WANT_STRFUNC
#include "apr_want.h"

#include "httpd.h"
#include "http_config.h"
#include "http_core.h"
#include "http_request.h"

module AP_MODULE_DECLARE_DATA c_module;

static int c_handler(request_rec *r)
{
 r->content_type="text/plain";
 ap_rprintf(r,"handler:%s/n",r->handler);
 ap_rprintf(r,"query string:%s/n",r->args);
 ap_rprintf(r,"filename:%s/n",r->filename);
 return OK;
}
static void register_hooks(apr_pool_t *p)
{
 ap_hook_handler(c_handler, NULL, NULL, APR_HOOK_MIDDLE);
}

/* module structure */
module AP_MODULE_DECLARE_DATA c_module = {
    STANDARD20_MODULE_STUFF,
    NULL, /* dir config creater */
    NULL, /* dir merger — default is to override */
    NULL, /* server config */
    NULL, /* merge server configs */
    NULL, /* command apr_table_t */
    register_hooks /* register hooks */
};

Compile and install this module (apxs provided by Apache is very good ):
$./Bin/apxs-C./mod_c.c
$./Bin/apxs-a-I-n C mod_c.la
At this time, apxs will automatically help us install the compiled mod_c.so to the modules/directory, and the module has been loaded in httpd. conf:
[Root @ cn-weblog apache2.2.4] # grep mod_c CONF/httpd. conf
Loadmodule c_module modules/mod_c.so

Test this module:
$./Bin/apachectl stop
$./Bin/apachectl start

Access http: // myhostname/index.html query = YY in IE
IE:
Handler: text/html
Query string: query = YY
Filename:/usr/local/apache2.2.4/htdocs/index.html
The module runs successfully.

Briefly explain the preceding module.

All Apache modules must be the struct, which defines the content.
/* Module structure */
Module ap_module_declare_data c_module = {
Standard20_module_stuff,
Null,/* dir config creater */
Null,/* dir merger-default is to override */
Null,/* server config */
Null,/* merge server configs */
// The above four items define the role of commands in httpd. conf.
Null,/* command apr_table_t * // defines the commands added in httpd. conf and the processing functions of each command
Register_hooks/* Register hooks * // hooks, which defines when to execute the related functions of our module.
};

Ap_hook_handler (c_handler, null, null, apr_hook_middle );
It indicates that our function-c_handler is called when processing the content request.

Related Article

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.