Note:
Path:/home/xxx/
Tool: apxs
Framework Construction:
1. Preparation: Install httpd-devel corresponding to apache, mainly to install apxs.
2. generate an apache module framework: cd/home/xxx/; apache module: apxs-g-n mytest
Here, mytest is the name of the apache module, but the actual generated so name is mod_mytest.so.
3. Compile apache module: use c ++ language to compile apache module, there is a saying on the Internet that use extern "C" method (see http://hi.baidu.com/zhangsilly/blog/item/a43fa11f869f4efae1fe0bf3.html), but no experiment is successful. Extern "C" has a warning and cannot be compiled ~!
Finally, I made all my background handlers into a liblogic. so, then the apache module mytest loads this liblogic. so, while the apache module mytest only receives the request and passes the parameter to liblogic. so! During mytest module compilation, use the apxs parameter-S to rename the CC, as shown below:
Apxs-c-a-s cc = g ++-I. /src-I. /src/common/-llogic-L. /src/mod_mytest.c-Wl,-rpath =/home/xxx/mytest/src/
Note that even if your mod_mytest.c is written in c ++, you cannot use mod_mytest.cpp to name this file. You must use the suffix of. c. Otherwise, you cannot compile it! The specific cause is unknown. It is to be checked! When using-Wl and-rpath, the application needs to find the actual dynamic library path!
You can use/usr/lib64/apr-1/build/libtool -- silent -- mode = link g ++ ...... here, the silent is removed and the specific compilation command is displayed.-Wl, -- rpath-Wl ,... you can check it out! ~
After the link is compiled successfully, mod_mytest.so will be generated under. libs /.
4. Modify httpd. conf and add:
LoadModule mytest_module/home/xxx/mytest/. libs/mod_mytest.so
<Location/index>
SetHandler mytest
</Location>
The index here represents the request for the index, for example: http://www.baidu.com/index? A = 1 & B = 2 --- the index here will use module mytest to process @!
Restart apache! The above describes how to build the entire apache module, as well as the commands and steps for compiling and linking. The following describes the development of the apache module: the development of the apache module.
A common header file:/usr/include/httpd. h
Where do we start from:
Open the mod_mytest.c file and find: static int mytest_handler (request_rec * r!
This function is the function we need to modify!
It should be noted that all request information is in this r parameter!
First question: Get the parameter http://www.baidu.com/index in the url? A = 1 & B = 2 For example, to obtain the above two parameters a/B, what should we do? In r-> args, the url above, r-> args = "a = 1 & B = 2 ", what we need to do is to get a = 1/B = 2 from this string. Below is a function I wrote to get parameters for reference only! Char * get_args_param (request_rec * r, const char * name) <br/>{/ * {{{ */<br/> const char * args = r-> args; <br/> const char * start_args; <br/> if (NULL! = Args) <br/>{< br/> for (start_args = ap_strstr_c (args, name); start_args; <br/> start_args = ap_strstr_c (start_args + 1, name )) <br/> {<br/> if (start_args = args | start_args [-1] = '&' | isspace (start_args [-1]) <br/>{< br/> start_args + = strlen (name); <br/> while (* start_args & isspace (* start_args )) <br/> ++ start_args; <br/> if (* start_args = '& start_args [1]) <br/>{< br/> char * end_args; <br /> Char * arg; <br/> ++ start_args; <br/> arg = apr_pstrdup (r-> pool, start_args ); <br/> if (end_args = strchr (arg ,'&'))! = NULL) <br/> * end_args = '/0'; <br/> return arg; <br/>}< br/> return NULL; <br/> }/*}}}*/
The compilation of get_cookie_param in the source code is used for reference! As mentioned below! Note that for calling the apr_pstrdup function, the header file # include "apr_strings.h" must be included. Otherwise, a warning is reported and the core is dropped during running! Here, the memory allocated by apr_pstrdup does not need to be explicitly free. It is based on apr_pool_t and is released once after the request end! The second question: how to get a specific cookie here we need to use r-> headers_in, which is a hashmap of the apr_table_t type! The following is a detailed description. Because cookie is similar to: abc = ui230jklsiu; def = uiore0832jhh1; in this case, the source code of the proxy module in apache is used: the get_cookie_param function exists in apache-2.2.11-src/modules/proxy/mod_proxy_balancer.c! Go, google! Char * get_cookie_param (request_rec * r, const char * name) <br/>{/ * {{{ */<br/> const char * cookies; <br/> const char * start_cookie; <br/> if (cookies = apr_table_get (r-> headers_in, "Cookie "))) {<br/> for (start_cookie = ap_strstr_c (cookies, name); start_cookie; <br/> start_cookie = ap_strstr_c (start_cookie + 1, name )) {<br/> if (start_cookie = cookies | <br/> start_cookie [-1] = ';' | <br/> start_cooki E [-1] = ',' | <br/> isspace (start_cookie [-1]) {<br/> start_cookie + = strlen (name ); <br/> while (* start_cookie & isspace (* start_cookie) <br/> + start_cookie; <br/> if (* start_cookie = '& start_cookie [1]) {<br/> char * end_cookie, * cookie; <br/> + start_cookie; <br/> cookie = apr_pstrdup (r-> pool, start_cookie); <br/> if (end_cookie = strchr (cookie ,';'))! = NULL) <br/> * end_cookie = '/0'; <br/> if (end_cookie = strchr (cookie ,','))! = NULL) <br/> * end_cookie = '/0'; <br/> return cookie; <br/>}< br/> return NULL; <br/> }/*}}}*/
There are also many other parameters, such as User-Agent, Referer, Accept-Charset, Keep-Alive, and many other information in this headers_in. the following URL can find what you want: revoke (r-> headers_in, "User-Agent"); the third question is: how to get ipr-> connection-> remote_ip! Fourth Question: How to output:
Apache provides many output functions, all of which use the ap _ header, which can be seen in/usr/include/httpd/http_protocol.h. The following is an excerpt:
AP_DECLARE (int) ap_rputc (int c, request_rec * r );
AP_DECLARE (int) ap_rputs (const char * str, request_rec * r );
AP_DECLARE (int) ap_rwrite (const void * buf, int nbyte, request_rec * r );
AP_DECLARE_NONSTD (int) ap_rvputs (request_rec * r ,...);
AP_DECLARE (int) ap_vrprintf (request_rec * r, const char * fmt, va_list vlist );
......
Note that for ap_rputs, str cannot be NULL, otherwise it will cause core!
Refer:
For more information, see the header file:/usr/include/httpd. h.
Or the following URL will help you:
Some common problems of apache Description: http://blog.sina.com.cn/s/blog_5bf18faf0100aph8.html
An example of helloworld: http://andrew913.javaeye.com/blog/398648
Reqeust_rec Structure Description: http://hi.baidu.com/start_and_end/blog/item/f344224ecadcc9c1d0c86a79.html
Server_rec Structure Description: http://book.51cto.com/art/200805/72067.htm
Apxs tool Introduction: http://lamp.linux.gov.cn/Apache/ApacheMenu/programs/apxs.html
An example of a filter module: http://www.cnblogs.com/ithurricane/archive/2009/01/01/1366312.html
Subsequent supplements:
1. For Apache output compression needs to be considered (use mod_deflate.so), you can refer to the http://www.bestchao.net/archives/134
Important section:
<Ifmodule mod_deflate.c>
Setoutputfilter deflate
</Ifmodule>