随着web分析越来越受重视,cookie的使用也就越来越多。
为此开发了一个种cookie的 apache module。
见以下代码:
/*
mod_AliCookie for apache2.x.
Author:Seamus Dean Email:deally.wu@gmail.com
*/
/* include head files */
#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"
/* our define */
#define ENV_SERVER_NAME "ali_server_name"
#define ENV_SERVER_IP "ali_server_ip"
#define ENABLE_SIMPLE_COOKIE 0
/* enable this module? */
#define ALI_COOKIE_ENABLED 0
/* ali_apache_id cookie */
#define FOREVER_COOKIE_NAME "ali_apache_id"
#define FOREVER_COOKIE_PATH "/"
#define FOREVER_COOKIE_DOMAIN ". oguoguo.com"
#define FOREVER_COOKIE_EXPIRE "Wed, 30-Nov-2084 01:01:01 GMT"
/* ali_apache_sid cookie */
#define SESSION_COOKIE_NAME "ali_apache_sid"
#define SESSION_COOKIE_PATH "/"
#define SESSION_COOKIE_DOMAIN ".oguoguo.com"
#define SESSION_COOKIE_LIVETIME (60*30) /* session cookie timeout */
/* ali_apache_agent cookie*/
#define AGENT_COOKIE_NAME "ali_apache_aid"
#define AGENT_COOKIE_PATH "/"
#define AGENT_COOKIE_DOMAIN ". oguoguo.com"
/*ali_apache_track cookie*/
//#define TRACK_COOKIE_NAME
#define TRACKE_COOKIE_DOMAIN ". oguoguo.com"
/* our HTTP header entry */
#define HEADER_TRACE_NAME "Resin-Trace"
/* cookie max length */
#define ALI_COOKIE_LENGTH 32
module AP_MODULE_DECLARE_DATA AliCookie_module;
typedef struct{
//utma
//char *ip;
char *unique_id;
char *ftime;
char *ltime;
char *stime;
int session_count;
//utmb
char * session_unique_id;
char * session_cookie;
int page_count;
//char * session_cookie2;
long timeout;
//utmc
char * agent_cookie;
//utmt
char **track_cookie;
}ALIUTM;
typedef struct {
char *track_cookie_name;
char *track_cookie_value;
char *track_cookie_expire;
void * nextTrackCookie;
} TrackCookieNode;
typedef struct{
int ali_cookie_enabled;
char *forever_cookie_name;
char *forever_cookie_value;
char *forever_cookie_path;
char *forever_cookie_domain;
char *forever_cookie_expire;
char *session_cookie_name;
char *session_cookie_value;
char *session_cookie_path;
char *session_cookie_domain;
int session_cookie_livetime;
/*add by wujianwei*/
char *agent_cookie_name;
char *agent_cookie_value;
char *agent_cookie_path;
char *agent_cookie_domain;
/*track_cookie*/
char *track_cookie_path;
char *track_cookie_domain;
TrackCookieNode *track_cookie;
} AliCookie;
/* init */
static void *make_cookie_dir(apr_pool_t *p, char *d)
{
AliCookie *cookie;
cookie = (AliCookie *)apr_pcalloc(p, sizeof(AliCookie));
if(!cookie) return NULL;
cookie->ali_cookie_enabled = ALI_COOKIE_ENABLED;
cookie->forever_cookie_name = FOREVER_COOKIE_NAME;
cookie->forever_cookie_path = FOREVER_COOKIE_PATH;
cookie->forever_cookie_domain = FOREVER_COOKIE_DOMAIN;
cookie->forever_cookie_expire = FOREVER_COOKIE_EXPIRE;
cookie->session_cookie_name = SESSION_COOKIE_NAME;
cookie->session_cookie_path = SESSION_COOKIE_PATH;
cookie->session_cookie_domain = SESSION_COOKIE_DOMAIN;
cookie->session_cookie_livetime= SESSION_COOKIE_LIVETIME;
cookie->agent_cookie_name = AGENT_COOKIE_NAME;
cookie->agent_cookie_path = AGENT_COOKIE_PATH;
cookie->agent_cookie_domain = AGENT_COOKIE_DOMAIN;
//cookie->track_cookie_name = NULL;
cookie->track_cookie_domain=TRACKE_COOKIE_DOMAIN;
//cookie->track_cookie_value =NULL;
cookie->track_cookie_path ="/";
// cookie->track_cookie_expire=NULL;
cookie->track_cookie = NULL;
return cookie;
}
//将营销活动数据记录到cookie中
static int make_track_cookie(request_rec *r,AliCookie *cookie)
{
if(cookie->track_cookie == NULL)return 0;
char *parameter = apr_pstrdup(r->pool,r->args);
//char *parameter = ap_strchr_c(url,'?');
TrackCookieNode *p = cookie->track_cookie;
char *c=NULL;
char *c2=NULL;
if(parameter==NULL) return 0;
while(p!=NULL)
{
c = NULL;
c = ap_strstr_c(parameter,p->track_cookie_name);
if(c!=NULL)
{
c += strlen(p->track_cookie_name)+1;
c2 =c;
while(*c)
{
if(*c=='&')
{
*c='/0';
p->track_cookie_value = apr_pstrdup(r->pool,c2);
*c='&';
c++;
break;
}
c++;
}
if(*c=='/0')
{
p->track_cookie_value = apr_pstrdup(r->pool,c2);
}
// p->track_cookie_value =c2;;
}
p=p->nextTrackCookie;
}
return 1;
}
//创建永久cookie
static char * make_forever_cookie(request_rec *r)
{
struct timeval request_time;
long sec;
long msec;
int randnumber;
char *buffer ;
gettimeofday(&request_time, NULL);
sec = (long)request_time.tv_sec; //精度:0.001s
msec = (long)(request_time.tv_usec ); //精度:0.001s
srand((int)time(0));
randnumber = (int)( (double) rand() / (RAND_MAX + 1.0)*2147483647);//10位的随机数字
buffer= apr_psprintf(r->pool,"%d.%d%d.%d%d.%d%d.%d",randnumber,(int)sec, (int)msec,(int)sec, (int)msec,(int)sec, (int)msec,1);
return buffer;
}
//更新永久cookie
static char *change_forever_cookie(request_rec *r,ALIUTM *pUtm)
{
struct timeval request_time;
long sec;
long msec;
int randnumber;
char *buffer;
gettimeofday(&request_time, NULL);
sec = (long)request_time.tv_sec; //精度:0.001s
msec = (long)(request_time.tv_usec ); //精度:0.001s
buffer= apr_psprintf(r->pool,"%s.%s.%s.%d%d.%d",pUtm->unique_id,pUtm->ftime, pUtm->stime,(int)sec, (int)msec,++pUtm->session_count);
return buffer;
}
//创建session cookie
static char * make_session_cookie(request_rec *r)
{
struct timeval request_time;
long sec;
long msec;
int randnumber;
char *buffer ;
gettimeofday(&request_time, NULL);
sec = (long)request_time.tv_sec; //精度:0.001s
msec = (long)(request_time.tv_usec ); //精度:0.001s
srand((int)time(0));
randnumber = (int)( (double) rand() / (RAND_MAX + 1.0)*2147483647);//10位的随机数字
buffer= apr_psprintf(r->pool,"%d.%d%d.%d|%d",randnumber,(int)sec, (int)msec,1,(int)((int)sec+SESSION_COOKIE_LIVETIME));
return buffer;
}
//更新sesssion_cookie
static char * change_session_cookie(request_rec *r,ALIUTM *pUtm)
{
struct timeval request_time;
long sec;
long msec;
int randnumber;
char *buffer ;
gettimeofday(&request_time, NULL);
sec = (long)request_time.tv_sec; //精度:0.001s
msec = (long)(request_time.tv_usec ); //精度:0.001s
if(pUtm->timeout >sec)//meiyou超时
{
buffer= apr_psprintf(r->pool,"%s.%s.%d|%d",pUtm->session_unique_id,pUtm->session_cookie,++(pUtm->page_count),(int)((int)sec+SESSION_COOKIE_LIVETIME));
}
else
{
randnumber = (int)( (double) rand() / (RAND_MAX + 1.0)*2147483647);//10位的随机数字
buffer= apr_psprintf(r->pool,"%d.%d%d.%d|%d",randnumber,(int)sec, (int)msec,1,(int)((int)sec+SESSION_COOKIE_LIVETIME));
}
return buffer;
}
//创建 agent cookie
static char * make_agent_cookie(request_rec *r)
{
struct timeval request_time;
long sec;
long msec;
// int randnumber;
char *buffer ;
gettimeofday(&request_time, NULL);
sec = (long)request_time.tv_sec; //精度:0.001s
msec = (long)(request_time.tv_usec ); //精度:0.001s
//srand((int)time(0));
//randnumber = (int)( (double) rand() / (RAND_MAX + 1.0)*2147483647);//10位的随机数字
buffer= apr_psprintf(r->pool,"%d%d",(int)sec, (int)msec);
return buffer;
}
static char * get_cookie_value(request_rec *r ,const char *cookieName, char * cookieString)
{
char *p;
//char v[1024];
char *buffer;
int value_len=0;
if(NULL==cookieName)return NULL;
if((p=strstr(cookieString,cookieName))!=NULL)
{
p += strlen(cookieName) + 1;
buffer = p;
while(*p)
{
if(*p == ';')
{
break;
}
*p++;
value_len++;
}
// buffer = apr_pcalloc(r->pool, sizeof(char)*value_len);
buffer = apr_pstrndup(r->pool,buffer,value_len);
return buffer;
}
return NULL;
}
/*解析cookie字符串*/
static ALIUTM * parse_cookie(request_rec *r,AliCookie *cookie)
{
char * old_cookie=NULL;
ALIUTM *pUmt =NULL;
char * p;
/*AliCookie *cookie = ap_get_module_config(r->per_dir_config,
&AliCookie_module);*/
if((old_cookie = (char*)apr_table_get(r->headers_in, "Cookie"))!=NULL)
{
pUmt= (ALIUTM *)apr_pcalloc(r->pool, sizeof(ALIUTM));
//forever cookie
pUmt->unique_id=get_cookie_value(r,cookie->forever_cookie_name,old_cookie);
p= pUmt->unique_id;
while(*p)
{
if(*p == '.')
{
*p = '/0';
p++;
break;
}
*p++;
}
pUmt->ftime =p;
while(*p)
{
if(*p == '.')
{
*p = '/0';
p++;
break;
}
*p++;
}
pUmt->ltime =p;
while(*p)
{
if(*p == '.')
{
*p = '/0';
p++;
break;
}
*p++;
}
pUmt->stime =p;
while(*p)
{
if(*p == '.')
{
*p = '/0';
p++;
break;
}
*p++;
}
pUmt->session_count =atoi(p);;
//session cookie
p = get_cookie_value(r,cookie->session_cookie_name,old_cookie);
pUmt->session_unique_id =p;
if(p!=NULL)
{
while(*p)
{
if(*p == '.')
{
*p = '/0';
p++;
break;
}
*p++;
}
pUmt->session_cookie =p;
while(*p)
{
if(*p == '.')
{
*p = '/0';
p++;
break;
}
*p++;
}
char * tmp =p;
//pUmt->page_count =p;
while(*p)
{
if(*p == '|')
{
*p = '/0';
p++;
break;
}
*p++;
}
pUmt->page_count =atoi(tmp);
pUmt->timeout=atol(p);
}
// agent cookie
pUmt->agent_cookie=get_cookie_value(r,cookie->agent_cookie_name,old_cookie);
}
return pUmt;
}
/* do alibaba cookie logic */
static int do_cookie(request_rec *r)
{
char *ali_server_name;
char *ali_server_ip;
char *forever_cookie ;
char *session_cookie;
char *agent_cookie;
/*const char *rname = ap_get_remote_host(r->connection,
r->per_dir_config,
REMOTE_NAME,
NULL);*/
AliCookie *cookie = ap_get_module_config(r->per_dir_config,
&AliCookie_module);
if(r->main != NULL) return OK;
ali_server_name = getenv(ENV_SERVER_NAME);
ali_server_ip = getenv(ENV_SERVER_IP);
if(ali_server_name)
{
apr_table_setn(r->notes, ENV_SERVER_NAME,
apr_psprintf(r->pool, "%s", ali_server_name));
}
if(ali_server_ip)
{
apr_table_setn(r->notes, ENV_SERVER_IP,
apr_psprintf(r->pool, "%s", ali_server_ip));
}
if(!cookie->ali_cookie_enabled)
{
return OK;
}
//解析cookie
ALIUTM *pUtm= parse_cookie(r,cookie);
if(pUtm==NULL)//没有cookie
{
forever_cookie= make_forever_cookie(r);
session_cookie= make_session_cookie(r);
agent_cookie= make_agent_cookie(r);
//make_track_cookie(r,cookie);
}
else //有cookie
{
if(pUtm->agent_cookie==NULL||((char*)pUtm->agent_cookie)=="")//没有浏览器cookie
{
//创建新的agent cookie
agent_cookie= make_agent_cookie(r);
//更新 session cookie
session_cookie= change_session_cookie(r,pUtm);
//更新 forever cookie
forever_cookie= change_forever_cookie(r,pUtm);
}
else
{
agent_cookie = pUtm->agent_cookie;
forever_cookie = apr_psprintf(r->pool,"%s.%s.%s.%s.%d",pUtm->unique_id,pUtm->ftime, pUtm->ltime,pUtm->stime,pUtm->session_count);
//更新 session cookie
session_cookie= change_session_cookie(r,pUtm);
}
}
make_track_cookie(r,cookie);
//写入缓存
//forever
apr_table_add(r->headers_out, "Set-Cookie",
apr_psprintf(r->pool,"%s=%s; path=%s; domain=%s; expires=%s",cookie->forever_cookie_name,
forever_cookie,cookie->forever_cookie_path,cookie->forever_cookie_domain,cookie->forever_cookie_expire));
apr_table_setn(r->notes, cookie->forever_cookie_name,
apr_psprintf(r->pool, "%s", forever_cookie));
//session
apr_table_add(r->headers_out, "Set-Cookie",
apr_psprintf(r->pool, "%s=%s; path=%s; domain=%s", cookie->session_cookie_name,
session_cookie, cookie->session_cookie_path, cookie->session_cookie_domain));
apr_table_setn(r->notes, cookie->session_cookie_name,
apr_psprintf(r->pool, "%s", session_cookie));
//agent
apr_table_add(r->headers_out, "Set-Cookie",
apr_psprintf(r->pool, "%s=%s; path=%s; domain=%s", cookie->agent_cookie_name,
agent_cookie, cookie->agent_cookie_path, cookie->agent_cookie_domain));
apr_table_setn(r->notes, cookie->agent_cookie_name,
apr_psprintf(r->pool, "%s", agent_cookie));
//track
TrackCookieNode *tmp = cookie->track_cookie;
while(tmp!=NULL && tmp->track_cookie_value!=NULL && tmp->track_cookie_value!='/0')
{
if(tmp->track_cookie_expire!=NULL)
{
apr_time_exp_t tms;
apr_time_exp_gmt(&tms, r->request_time + apr_time_from_sec(atoi(tmp->track_cookie_expire)));
apr_table_add(r->headers_out, "Set-Cookie",
apr_psprintf(r->pool, "%s=%s; path=%s; domain=%s; expires=%s,%.2d-%s-%.2d %.2d:%.2d:%.2d GMT",
tmp->track_cookie_name,
tmp->track_cookie_value,
cookie->track_cookie_path, cookie->track_cookie_domain,
apr_day_snames[tms.tm_wday],tms.tm_mday, apr_month_snames[tms.tm_mon],
tms.tm_year + 1900, tms.tm_hour, tms.tm_min));
}
else
{
apr_table_add(r->headers_out, "Set-Cookie",
apr_psprintf(r->pool, "%s=%s; path=%s; domain=%s;", tmp->track_cookie_name,
tmp->track_cookie_value, cookie->track_cookie_path, cookie->track_cookie_domain));
}
apr_table_setn(r->notes, tmp->track_cookie_name,
apr_psprintf(r->pool, "%s", tmp->track_cookie_value));
tmp = tmp->nextTrackCookie;
}
return OK;
}
/*
hold response
catch the cookie or resin-trace sended by resin or weblogic.
*/
static int hold_response(request_rec *r)
{
int flag = 0;
// alibaba notes name
char *notes_name = NULL;
// alibaba notes value
char *notes_value = NULL;
// a pointer when extract the cookie structure and Resin-Trace structure.
char *point = NULL;
// uses to export cookie or resin-trace
char *notes = NULL;
apr_array_header_t *resphdrs_arr;
apr_table_entry_t *resphdrs_elts;
int i = 0;
AliCookie *cookie = ap_get_module_config(r->per_dir_config, &AliCookie_module);
if(!cookie->ali_cookie_enabled)
{
return OK;
}
resphdrs_arr = (apr_array_header_t *)apr_table_elts(r->headers_out);
resphdrs_elts = (apr_table_entry_t *)resphdrs_arr->elts;
for (i = 0; i < resphdrs_arr->nelts; i++)
{
if (resphdrs_elts[i].key == NULL || resphdrs_elts[i].val == NULL) continue;
if((strcasecmp(resphdrs_elts[i].key,"Set-Cookie") == 0) ||
(strcasecmp(resphdrs_elts[i].key, HEADER_TRACE_NAME) == 0))
{
//this is a cookie or Resin-Trace,We need to export it by Apache module notes!
//Copy the data by alloac memory from Apache's memory pool.
point = apr_pstrdup(r->pool, resphdrs_elts[i].val);
notes_name = point;
/*
Then we need to extract the cookie or Resin-Trace,we only need name and value.
*/
while(*point)
{
if((flag == 0) && (*point == '='))
{
flag = 1;
*point = '/0';
point++;
notes_value = point;
continue;
}
if((flag == 1) && (*point == ';'))
{
*point = '/0';
break;
}
point++;
}
flag = 0;
/* export the cookie or Resin-Trace */
if(strlen(notes_value) > 0 &&
strcasecmp(notes_name, cookie->session_cookie_name) != 0)
{
notes = apr_psprintf(r->pool, "%s", notes_value);
apr_table_setn(r->notes, notes_name, notes);
}
}
}
return OK;
}
/* enable this module or not */
static const char *set_ali_cookie_enable(cmd_parms *cmd, void *mconfig, int arg)
{
AliCookie *cookie = (AliCookie *)mconfig;
cookie->ali_cookie_enabled = arg;
return NULL;
}
/* set session cookie name */
static const char *set_session_cookie_name(cmd_parms *cmd, void *mconfig, const char *name)
{
AliCookie *cookie = (AliCookie *) mconfig;
if(strlen(name) == 0)
{
return "AliSessionCookieName values may not be null";
}
cookie->session_cookie_name = apr_pstrdup(cmd->pool, name);
return NULL;
}
/* set forever cookie name */
static const char *set_forever_cookie_name(cmd_parms *cmd, void *mconfig, const char *name)
{
AliCookie *cookie = (AliCookie *) mconfig;
if(strlen(name) == 0)
{
return "AliForeverCookieName values may not be null";
}
cookie->forever_cookie_name = apr_pstrdup(cmd->pool, name);
return NULL;
}
/* set session cookie domain */
static const char *set_session_cookie_domain(cmd_parms *cmd, void *mconfig, const char *name)
{
AliCookie *cookie;
cookie = (AliCookie *) mconfig;
if (strlen(name) == 0) {
return "AliSessionCookieDomain values may not be null";
}
if (name[0] != '.') {
return "AliSessionCookieDomain values must begin with a dot";
}
if (strchr(&name[1], '.') == NULL) {
return "AliSessionCookieDomain values must contain at least one embedded dot";
}
cookie->session_cookie_domain = apr_pstrdup(cmd->pool, name);
return NULL;
}
/* set forever cookie domain */
static const char *set_forever_cookie_domain(cmd_parms *cmd, void *mconfig, const char *name)
{
AliCookie *cookie;
cookie = (AliCookie *) mconfig;
if (strlen(name) == 0) {
return "AliForeverCookieDomain values may not be null";
}
if (name[0] != '.') {
return "AliForeverCookieDomain values must begin with a dot";
}
if (strchr(&name[1], '.') == NULL) {
return "AliForeverCookieDomain values must contain at least one embedded dot";
}
cookie->forever_cookie_domain = apr_pstrdup(cmd->pool, name);
return NULL;
}
/* set session cookie path */
static const char *set_session_cookie_path(cmd_parms *cmd, void *mconfig, const char *path)
{
AliCookie *cookie;
cookie = (AliCookie *) mconfig;
if (strlen(path) == 0) {
return "AliSessionCookiePath values may not be null";
}
if (path[0] != '/') {
return "AliSessionCookiePath values must begin with /";
}
cookie->session_cookie_path = apr_pstrdup(cmd->pool, path);
return NULL;
}
/* set forever cookie path */
static const char *set_forever_cookie_path(cmd_parms *cmd, void *mconfig, const char *path)
{
AliCookie *cookie;
cookie = (AliCookie *) mconfig;
if (strlen(path) == 0) {
return "AliForeverCookiePath values may not be null";
}
if (path[0] != '/') {
return "AliForeverCookiePath values must begin with /";
}
cookie->forever_cookie_path = apr_pstrdup(cmd->pool, path);
return NULL;
}
/* set forever cookie expire time */
static const char *set_forever_cookie_expire(cmd_parms *cmd, void *mconfig, const char *expire)
{
AliCookie *cookie;
cookie = (AliCookie *) mconfig;
if (strlen(expire) == 0) {
return "AliCookieExpire values may not be null";
}
cookie->forever_cookie_expire = apr_pstrdup(cmd->pool, expire);
return NULL;
}
/* set session cookie timeout */
static const char *set_session_cookie_livetime(cmd_parms *cmd, void *mconfig, const char *livetime)
{
AliCookie *cookie;
char *p;
cookie = (AliCookie *) mconfig;
p = apr_pstrdup(cmd->pool,livetime);
while(*p)
{
if(!isdigit(*p))
{
return "AliSessionCookieLivetime must be a integer as seconds.";
}
p++;
}
cookie->session_cookie_livetime = atoi(livetime);
return NULL;
}
//static const char *
/*set track cookie*/
static const char *set_track_cookie(cmd_parms *cmd, void *mconfig,
const char *in_str,const char *sec_str)
{
AliCookie *cookie;
cookie = (AliCookie *)mconfig;
char *p = apr_pstrdup(cmd->pool,in_str);
char *track_cookie_name = p;
char *track_cookie_expire=NULL;
if (p==NULL)return NULL;
if(sec_str!=NULL)
{
// atol(sec_str);
track_cookie_expire = apr_pstrdup(cmd->pool,sec_str);
}
if( cookie->track_cookie ==NULL)//the first track cookie
{
cookie->track_cookie = (TrackCookieNode *) apr_pcalloc(cmd->pool, sizeof(TrackCookieNode));
cookie->track_cookie->track_cookie_name= track_cookie_name;
cookie->track_cookie->track_cookie_expire= track_cookie_expire;
cookie->track_cookie->nextTrackCookie = NULL;
}
else
{
TrackCookieNode *pt = cookie->track_cookie;
while(pt->nextTrackCookie != NULL)
{
pt = pt->nextTrackCookie;
}
TrackCookieNode *p2 = (TrackCookieNode *) apr_pcalloc(cmd->pool, sizeof(TrackCookieNode));
p2->track_cookie_name= track_cookie_name;
p2->track_cookie_expire= track_cookie_expire;
p2->nextTrackCookie = NULL;
pt->nextTrackCookie = p2;
}
return NULL;
}
static apr_status_t init_module(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *tp, server_rec *s)
{
ap_add_version_component(p,"mod_AliCookie(for apache2.x)/1.1");
return APR_SUCCESS;
}
static const command_rec cookie_cmds[]={
AP_INIT_FLAG("EnableAliCookie", set_ali_cookie_enable, NULL, OR_FILEINFO,
"whether or not to enable alibaba cookie"),
AP_INIT_TAKE1("AliForeverCookieName", set_forever_cookie_name, NULL, OR_FILEINFO,
"set alibaba forever cookie name"),
AP_INIT_TAKE1("AliSessionCookieName", set_session_cookie_name, NULL, OR_FILEINFO,
"set alibaba session cookie name"),
AP_INIT_TAKE1("AliForeverCookiePath", set_forever_cookie_path, NULL, OR_FILEINFO,
"set alibaba forever cookie path"),
AP_INIT_TAKE1("AliSessionCookiePath", set_session_cookie_path, NULL, OR_FILEINFO,
"set alibaba session cookie path"),
AP_INIT_TAKE1("AliForeverCookieDomain", set_forever_cookie_domain, NULL, OR_FILEINFO,
"set alibaba forever cookie domain"),
AP_INIT_TAKE1("AliSessionCookieDomain", set_session_cookie_domain, NULL, OR_FILEINFO,
"set alibaba session cookie domain"),
AP_INIT_TAKE1("AliSessionCookieLivetime", set_session_cookie_livetime, NULL, OR_FILEINFO,
"set alibaba session cookie expire time"),
AP_INIT_TAKE1("AliForeverCookieExpire", set_forever_cookie_expire, NULL, OR_FILEINFO,
"set alibaba forever cookie expire time"),
AP_INIT_TAKE12("AliTrackCookie",set_track_cookie,NULL,OR_FILEINFO,
"set trackcookie cookieName,expire time"),
{NULL}
};
static void register_hooks(apr_pool_t *p)
{
ap_hook_post_config(init_module, NULL, NULL, APR_HOOK_MIDDLE);
ap_hook_fixups(do_cookie, NULL, NULL, APR_HOOK_MIDDLE);
ap_hook_log_transaction(hold_response, NULL, NULL, APR_HOOK_FIRST);
}
/* module structure */
module AP_MODULE_DECLARE_DATA AliCookie_module = {
STANDARD20_MODULE_STUFF,
make_cookie_dir, /* dir config creater */
NULL, /* dir merger --- default is to override */
NULL, /* server config */
NULL, /* merge server configs */
cookie_cmds, /* command apr_table_t */
register_hooks /* register hooks */
};
将以上mod_AliCookie.c 放到 usr/local/apache2/modules下
1,编译
gcc -fpic -DSHARED_MODULE -I /usr/local/apache2/include -g -c mod_AliCookie.c
2,链接
ld -Bshareable -g -o mod_AliCookie.so mod_AliCookie.o
3,修改httpd.conf文件
LoadModule AliCookie_module modules/mod_AliCookie.so
<location "/">
EnableAliCookie On
AliTrackCookie _u
AliTrackCookie _b 1800
</location>
4,调试
# gdb bin/httpd
GNU gdb Red Hat Linux (6.3.0.0-1.153.el4rh)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu"...Using host libthread_db library "/lib64/tls/libthread_db.so.1".
5,设置断点
(gdb) b ap_process_request
6,运行
(gdb) r -X -d /usr/local/apache2/
7,在浏览器中请求页面
8,设置断点
(gdb) b /usr/local/apache2/modules/mod_AliCookie.c:do_cookie
Breakpoint 2 at 0x2a958cb585: file mod_AliCookie.c, line 358.
(gdb) c
Continuing.