Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1463827
  • 博文数量: 218
  • 博客积分: 6394
  • 博客等级: 准将
  • 技术积分: 2563
  • 用 户 组: 普通用户
  • 注册时间: 2008-02-08 15:33
个人简介

持之以恒

文章分类

全部博文(218)

文章存档

2013年(8)

2012年(2)

2011年(21)

2010年(55)

2009年(116)

2008年(16)

分类: 网络与安全

2012-12-15 16:13:47

       今天在Ubutun上装上了Apache 服务器和Mysql,实验了一个自定义生成网页的Hook,Hook处理客户端的请求,从Mysql中读取数据,生成网页返回,现在记录过程如下:
(1) 安装Apache和配置
       sudo apt-get install apache2
  安装结束后:
  产生的启动和停止文件是:/etc/init.d/apache2
  启动:sudo apache2ctl -k start
  停止:sudo apache2ctl -k stop
  重新启动:sudo apache2ctl -k restart
    配置文件保存在:/etc/apache2
参考:http://www.blogjava.net/duanzhimin528/archive/2010/03/05/314564.html
(2)Apache Hook modules
原理:
参考:http://blog.csdn.net/hguisu/article/details/7395181
(3)安装MySQL
c 连接mysql

http://blog.sina.com.cn/s/blog_682a588a0100ikh6.html
 sudo cp ./usr/lib/x86_64-linux-gnu/libmysqlclient* ./usr/lib/
test1.cpp

点击(此处)折叠或打开

  1. #include <iostream>
  2. #include <mysql/mysql.h>
  3. #include <string>
  4. using namespace std;
  5. int main()
  6. {
  7.   MYSQL mysql;
  8.   mysql_init(&mysql);
  9.   mysql_real_connect(&mysql, "localhost", "root", "123456", "test", 3306, NULL, 0);
  10.   string sql = "insert into t1 (id, name) values (1, 'java1');";
  11.   int ret = mysql_query(&mysql, sql.c_str());
  12.   mysql_close(&mysql);
  13.   cout<<"test2 finished"<<","<<ret;
  14.   return 0;
  15. }

生成的表

MySQL配置==>(配置密码)
%E5%AE%89%E8%A3%85%E6%8C%87%E5%8D%97

(4)编写Hooks访问数据库
增加对Apache 对C++和MySql的支持
在httpd.conf中增加两个LoadFile分别指向libstdc++.so和libmysqlclient.so

然后重新启动Apache
编写 hooks mod_helloworld.c

点击(此处)折叠或打开

  1. #include "httpd.h"
  2. #include "http_config.h"
  3. #include "http_protocol.h"
  4. #include "ap_config.h"

  5. #include <mysql/mysql.h>

  6. #include <string>
  7. #include <iostream>
  8. #ifdef __cplusplus
  9. using namespace std;

  10. extern "C" {

  11. #endif

  12. //一段代码
  13. static string getDataFromDB();
  14. static int helloworld_handler(request_rec *r);
  15. static void helloworld_register_hooks(apr_pool_t *p);
  16. #ifdef __cplusplus

  17. }

  18. #endif
  19. static string getDataFromDB()
  20. {
  21.   string retString;
  22.   MYSQL mysql;
  23.   MYSQL_RES *result = NULL;
  24.   MYSQL_FIELD *field = NULL;
  25.   mysql_init(&mysql);
  26.   mysql_real_connect(&mysql, "localhost", "root", "123456", "test", 3306, NULL, 0);
  27.   string str = "select id,name from t1;";
  28.   mysql_query(&mysql, str.c_str());
  29.   result = mysql_store_result(&mysql);
  30.   int rowcount = mysql_num_rows(result);
  31.   cout << rowcount << endl;
  32.   int fieldcount = mysql_num_fields(result);
  33.   cout << fieldcount << endl;
  34.   for(int i = 0; i < fieldcount; i++)
  35.   {
  36.    field = mysql_fetch_field_direct(result,i);
  37.    retString+=field->name;
  38.    retString+="\t\t";
  39.   // cout << field->name << "\t\t";
  40.   }
  41. retString+="
    "
    ;
  42.     MYSQL_ROW row = NULL;
  43.   row = mysql_fetch_row(result);
  44.   while(NULL != row)
  45.   {
  46.    for(int i=1; i<fieldcount; i++)
  47.    {
  48.     retString+=row[i];
  49. retString+="\t\t";
  50.    }
  51.    row = mysql_fetch_row(result);
  52.   }
  53. retString+="
    "
    ;
  54.   mysql_close(&mysql);
  55.   return retString;
  56. }

  57. /* The sample content handler */
  58. static int helloworld_handler(request_rec *r)
  59. {
  60.     if (strcmp(r->handler, "helloworld")) {
  61.         return DECLINED;
  62.     }
  63.     r->content_type = "text/html";

  64.     if (!r->header_only)
  65.          //ap_rputs("test", r);
  66.         ap_rputs(getDataFromDB().c_str(), r);
  67.     return OK;
  68. }



  69. static void helloworld_register_hooks(apr_pool_t *p)
  70. {
  71.     ap_hook_handler(helloworld_handler, NULL, NULL, APR_HOOK_MIDDLE);
  72. }
  73. /* Dispatch list for API hooks */
  74. module AP_MODULE_DECLARE_DATA helloworld_module = {
  75.     STANDARD20_MODULE_STUFF, //用于编译后的模块产生版本信息
  76.     NULL, /* 创建目录配置结构*/
  77.     NULL, /* 合并目录配置结构 */
  78.     NULL, /* 创建主机配置结构 */
  79.     NULL, /* 合并主机配置结构 */
  80.     NULL, /* 为模块配置相关指令 */
  81.     helloworld_register_hooks /* 注册模块的钩子函数 */
  82. };
编译

生成
mod_helloworld.so==>copy 到/usr/lib/apache2/modules/目录下
修改httpd.conf

重新启动Apache
通过电脑浏览器访问:

192.168.1.101是本机的IP 地址
通过手机浏览器访问(局域网WIFI)


参考:
Apache模块开发之HelloWorld
http://andrew913.iteye.com/blog/398648
Apache模块编写==>怎么用apxs编译C++ 程序
http://uuchi.iteye.com/blog/1005136
在Apache中增加对C++和SSL 的支持

Mysql中文API
http://dev.mysql.com/doc/refman/5.1/zh/sql-syntax.html

问题解决
Commands out of sync; you can’t run this command now
http://blog.sina.com.cn/s/blog_93b1f3f00100uydl.html


阅读(1511) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~