C. apache module development (access mysql)

Source: Internet
Author: User
Extended Apache module development most of the tutorials on the Internet are centered on the Perl language memory. The "Writing Apache Modules with Perl and C" by foreigners can be regarded as a classic, but it has always been developed for older versions, in addition, the main language is Perl, And the C language is only slightly introduced. However, using Perl to expand the module functions is faster and easier than using C language. I also applied a part of my work. I mainly wrote two simple dual-level modules on Anti-leech. I can refer to the other two articles I wrote: apache + mod_perl anti-leech protection and apache + mod_perl implement url rewrite. After talking about so many questions, let's go back to the question. Here we only use the C language to implement a simple hello module. The module function is to query all the user tables in the MySQL database.

System Environment:
ArchLinux Apache2.2 MySQL 5.0

Specific development steps:
1. Use Apache to build the hello module with apxs:

[Root # localhost] apxs-g-n hello

Apxs is in path to apache.../apache/bin/apxs.

Such as cd/usr/local/apache

Bin/apxs-g-n hello

The hello folder is created in the/usr/local/apache directory.

In this way, a new file directory of the hello module is created under the current directory. You can see files such as Makefile mod_hello.c modules. mk. You can query the apache/bin directory of the local machine in the specific apxs path.

2. Preview mod_hello.cYou can see that apxs automatically generates a bunch of code for you. All you need is to modify the code section. First, we will briefly introduce the function description in it.
The include part introduces some necessary header files.
Hello_handler: This is the main part of the hello module. All the display and processing requests are here.
Hello_register_hooks hello_module are required by the functions to be exported. You can ignore them and follow the generated parameters.

3. ModifyHello_handler function,You can see that request_rec * r has many functions and variables. For more information, see the document. The ap_rputs in is the output, which can be simply understood as outputting strings to r.
Static int hello_handler (request_rec * r)
{
If (strcmp (r-> handler, "hello") {// you can skip this step if the handler in the apache configuration file is hello or not.
Return DECLINED;
}
R-> content_type = "text/html"; // set content-type
If (! R-> header_only)
Ap_rputs ("The sample page from mod_hello.c \ n", r); // output a text clip.
Return OK; // return the 200 OK status
}

Add # include "mysq. h". This header file is required for query.
For more information about the code, see the end of this article.

4. Compilation Module

[Root # localhost]Apxs-c-a-I-I/usr/include/mysql/mod_hello.c

Other instances:

Cd/usr/local/apache

Bin/apxs-c-a-I-I/usr/local/mysql/include/mysql/hello/mod_hello.c 

 /Usr/include/mysql/It is the header file location of mysql. during actual compilation, select the header file location after mysql installation,

We can see a bunch of compilation commands. Adding-I and-l is required for mysql compilation. After compilation, LoadModule hello_module modules/mod_hello.so is automatically added to httpd. conf.

5. Modify httpd. conf
<Location/hello>
SetHandler hello
</
Location>

6. Restart apache,Access http: // localhost/hello to check whether it is successful.

From http://hi.baidu.com/smallfish_xy/blog/item/1a59fe117afd26f5c2ce79a9.html
========================================================== ========================================================== =

Complete code:
# Include "httpd. h"
# Include "http_config.h"
# Include "http_protocol.h"
# Include "ap_config.h"
/* Header file, which is used in this article
Ap_rprintf function */
# Include "apr. h"
# Include "apr_lib.h"
# Include "apr_strings.h"
# Include "apr_want.h"
# Include "mysql. h"

/* Define mysql Data Variables */
Const char * host = "localhost ";
Const char * user = "root ";
Const char * pass = "smallfish ";
Const char * db = "mysql ";

/* The sample content handler */
Static int hello_handler (request_rec * r)
{
If (strcmp (r-> handler, "hello ")){
Return DECLINED;
}
R-> content_type = "text/html ";
/* Define mysql variables */
MYSQL mysql;
MYSQL_RES * rs;
MYSQL_ROW row;
Mysql_init (& mysql);/* initialization */
If (! Mysql_real_connect (& mysql, host, user, pass, db, 0, NULL, 0) {/* connect to database */
Ap_rprintf (r, "<li> Error: % d % s </li> \ n", mysql_errno (& mysql), mysql_error (& mysql ));
Return OK;
}
Char * SQL = "select host, user from user order by rand ()";
If (mysql_query (& mysql, SQL )! = 0) {/* query */
Ap_rprintf (r, "<li> Error: % d % s </li> \ n", mysql_errno (& mysql), mysql_error (& mysql ));
Return OK;
}
Rs = mysql_store_result (& mysql);/* obtain the query result */
While (row = mysql_fetch_row (rs) {/* Get each row record */
Ap_rprintf (r, "<li> % s-% s </li> \ n", row [0], row [1]);
}
Mysql_free_result (rs);/* release result set */
Mysql_close (& mysql);/* close the connection */
Return OK;
}

Static void hello_register_hooks (apr_pool_t * p)
{
Ap_hook_handler (hello_handler, NULL, NULL, APR_HOOK_MIDDLE );
}

/* Dispatch list for API hooks */
Module AP_MODULE_DECLARE_DATA hello_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 */
Hello_register_hooks/* register hooks */

};

After testing, you can run it successfully.

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.