Chinaunix首页 | 论坛 | 博客
  • 博客访问: 928865
  • 博文数量: 177
  • 博客积分: 8613
  • 博客等级: 中将
  • 技术积分: 2835
  • 用 户 组: 普通用户
  • 注册时间: 2006-03-12 04:16
文章分类
文章存档

2012年(12)

2011年(24)

2010年(24)

2009年(75)

2008年(42)

我的朋友

分类: C/C++

2008-12-06 22:34:09

  用了整整一天的时间,明白了什么是cookie、apache模块编程的基础知识,然后,用今天一整天的时间,写出来一个设置cookie的apache模块,测试一切正常,加了简单的出错控制,如在url里面不传domain或者其他参数的时候,不至于apache报错:
[Sat Dec  6 21:51:06 2008] [notice] child pid 3454 exit signal Segmentation fault (11)

   今天调试出来这个模块,应该是我的处女作了吧,呵呵,还有很多需要继续完善的,特别是出错控制,以及数组大小设置多大合适上,明天继续弄,争取明天搞定它,达到“完美”呵呵,以下是全部的代码,希望能给做这方面的同学节省时间。

如需要转载,请注明出处:刘峰blog,谢谢!

 

/*
 **  mod_setcookie.c -- Apache sample setcookie module
 **  [Autogenerated via ``apxs -n setcookie -g'']
 **
 **  To play with this sample module, first compile it into a
 **  DSO file and install it into Apache's libexec directory
 **  by running:
 **
 **    $ apxs -c -i mod_setcookie.c
 **
 **  Then activate it in Apache's httpd.conf file, for instance
 **  for the URL /setcookie, as follows:
 **
 **    #   httpd.conf
 **    LoadModule setcookie_module libexec/mod_setcookie.so
 **   
 **    SetHandler setcookie
 **   

 **
 **  Then after restarting Apache via
 **
 **    $ apachectl restart
 **
 **  you immediately can request the URL /%NAME and watch for the
 **  output of this module. This can be achieved for instance via:
 **
 **    $ lynx -mime_header
 **
 **  The output should be similar to the following one:
 **
 **    HTTP/1.1 200 OK
 **    Date: Tue, 31 Mar 1998 14:42:22 GMT
 **    Server: Apache/1.3.4 (Unix)
 **    Connection: close
 **    Content-Type: text/html
 ** 
 **    The sample page from mod_setcookie.c
 */

#include "httpd.h"
#include "http_config.h"
#include "http_protocol.h"
#include "ap_config.h"

/* The sample content handler */
static int setcookie_handler(request_rec *r)
{
 r->content_type = "text/html";     
 if (!r->header_only)
  //    ap_rputs("The sample page from mod_setcookie.c\n", r);

 {
  char cookie[300] = "SINA_SESS=";
  char URL[100];
  char URL2[100];
  strcpy(URL,r->args);
  strcpy(URL2,r->args);

  char *p;
  char *cookie_name;
  char *domain_name;

  p = strtok(URL, "&");
  while (p)
  {
   if(cookie_name = strstr(p,"SNCookie"))
   {
    cookie_name = strtok(cookie_name,"=");
    cookie_name = strtok(NULL,"=");    
    //printf("cookie_name:%s\n", cookie_name);      
   }
   p = strtok(NULL, "&");
  }

  p = strtok(URL2, "&");
  while (p)
  {
   if(domain_name = strstr(p,"domain"))
   {
    domain_name = strtok(domain_name,"=");
    domain_name = strtok(NULL,"=");
    //printf("domain_name:%s\n", domain_name);
   }
   p = strtok(NULL, "&");
  }


  if(cookie_name)
  {
   strcat(cookie,cookie_name);
   strcat(cookie,"; domain=.");
  }
  if(domain_name)
  {
   strcat(cookie,domain_name);
   strcat(cookie,"; path=/");
  }

  if(cookie)
   ap_table_add(r->headers_out,"Set-Cookie",cookie);

  ap_send_http_header(r);
 }  


 return OK;
}

/* Dispatch list of content handlers */
static const handler_rec setcookie_handlers[] = {
 { "setcookie", setcookie_handler },
 { NULL, NULL }
};

/* Dispatch list for API hooks */
module MODULE_VAR_EXPORT setcookie_module = {
 STANDARD_MODULE_STUFF,
 NULL,                  /* module initializer                  */
 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       */
 setcookie_handlers,       /* [#8] MIME-typed-dispatched handlers */
 NULL,                  /* [#1] URI to filename translation    */
 NULL,                  /* [#4] validate user id from request  */
 NULL,                  /* [#5] check if the user is ok _here_ */
 NULL,                  /* [#3] check access by host address   */
 NULL,                  /* [#6] determine MIME type            */
 NULL,                  /* [#7] pre-run fixups                 */
 NULL,                  /* [#9] log a transaction              */
 NULL,                  /* [#2] header parser                  */
 NULL,                  /* child_init                          */
 NULL,                  /* child_exit                          */
 NULL                   /* [#0] post read-request              */
};

 

 


测试方法为:
1、hosts指向开发服务器

2、浏览器输入:

3、如何判断成功?

在http头的输出中会received:


   
SINA_SESS Sent ayaheihei..first..login   
SINA_SESS Received ayaheihei..second..login / .sina.com 


多测试几次,cookie头发送、接收都没有问题,初步搞定!

 

附:apache模块编译的大体过程和编译方法

建立工作项目

/home/liufeng/webadm_dev/bin/apxs -g -n setcookie

快速编译并部署:

/home/liufeng/webadm_dev/bin/apxs -c -a -i -I/home/liufeng/webadm_dev/apache_.1.3.34_src/apache_1.3.34/src/include/ mod_setcookie.c
/home/liufeng/webadm_dev/bin/apachectl stop
sleep 2
/home/liufeng/webadm_dev/bin/apachectl start

 

 

 


 

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