📄 mod_usertrack.c
字号:
}static int spot_cookie(request_rec *r){ cookie_dir_rec *dcfg = ap_get_module_config(r->per_dir_config, &usertrack_module); const char *cookie_header; regmatch_t regm[NUM_SUBS]; if (!dcfg->enabled) { return DECLINED; } if ((cookie_header = ap_table_get(r->headers_in, "Cookie"))) { if (!ap_regexec(dcfg->regexp, cookie_header, NUM_SUBS, regm, 0)) { char *cookieval = NULL; /* Our regexp, * ^cookie_name=([^;]+)|;[ \t]+cookie_name=([^;]+) * only allows for $1 or $2 to be available. ($0 is always * filled with the entire matched expression, not just * the part in parentheses.) So just check for either one * and assign to cookieval if present. */ if (regm[1].rm_so != -1) { cookieval = ap_pregsub(r->pool, "$1", cookie_header, NUM_SUBS, regm); } if (regm[2].rm_so != -1) { cookieval = ap_pregsub(r->pool, "$2", cookie_header, NUM_SUBS, regm); } /* Set the cookie in a note, for logging */ ap_table_setn(r->notes, "cookie", cookieval); return DECLINED; /* There's already a cookie, no new one */ } } make_cookie(r); return OK; /* We set our cookie */}static void *make_cookie_log_state(pool *p, server_rec *s){ cookie_log_state *cls = (cookie_log_state *) ap_palloc(p, sizeof(cookie_log_state)); cls->expires = 0; return (void *) cls;}static void *make_cookie_dir(pool *p, char *d){ cookie_dir_rec *dcfg; dcfg = (cookie_dir_rec *) ap_pcalloc(p, sizeof(cookie_dir_rec)); dcfg->cookie_name = COOKIE_NAME; dcfg->cookie_domain = NULL; dcfg->prefix_string = ""; dcfg->style = CT_UNSET; dcfg->format = CF_NORMAL; dcfg->enabled = 0; /* * In case the user does not use the CookieName directive, * we need to compile the regexp for the default cookie name. */ set_and_comp_regexp(dcfg, p, COOKIE_NAME); return dcfg;}static const char *set_cookie_enable(cmd_parms *cmd, void *mconfig, int arg){ cookie_dir_rec *dcfg = mconfig; dcfg->enabled = arg; return NULL;}static const char *set_cookie_exp(cmd_parms *parms, void *dummy, const char *arg){ cookie_log_state *cls; time_t factor, modifier = 0; time_t num = 0; char *word; cls = ap_get_module_config(parms->server->module_config, &usertrack_module); /* The simple case first - all numbers (we assume) */ if (ap_isdigit(arg[0]) && ap_isdigit(arg[strlen(arg) - 1])) { cls->expires = atol(arg); return NULL; } /* * The harder case - stolen from mod_expires * * CookieExpires "[plus] {<num> <type>}*" */ word = ap_getword_conf(parms->pool, &arg); if (!strncasecmp(word, "plus", 1)) { word = ap_getword_conf(parms->pool, &arg); }; /* {<num> <type>}* */ while (word[0]) { /* <num> */ if (ap_isdigit(word[0])) num = atoi(word); else return "bad expires code, numeric value expected."; /* <type> */ word = ap_getword_conf(parms->pool, &arg); if (!word[0]) return "bad expires code, missing <type>"; factor = 0; if (!strncasecmp(word, "years", 1)) factor = 60 * 60 * 24 * 365; else if (!strncasecmp(word, "months", 2)) factor = 60 * 60 * 24 * 30; else if (!strncasecmp(word, "weeks", 1)) factor = 60 * 60 * 24 * 7; else if (!strncasecmp(word, "days", 1)) factor = 60 * 60 * 24; else if (!strncasecmp(word, "hours", 1)) factor = 60 * 60; else if (!strncasecmp(word, "minutes", 2)) factor = 60; else if (!strncasecmp(word, "seconds", 1)) factor = 1; else return "bad expires code, unrecognized type"; modifier = modifier + factor * num; /* next <num> */ word = ap_getword_conf(parms->pool, &arg); } cls->expires = modifier; return NULL;}static const char *set_cookie_name(cmd_parms *cmd, void *mconfig, char *name){ cookie_dir_rec *dcfg = (cookie_dir_rec *) mconfig; dcfg->cookie_name = ap_pstrdup(cmd->pool, name); set_and_comp_regexp(dcfg, cmd->pool, name); if (dcfg->regexp == NULL) { return "Regular expression could not be compiled."; } if (dcfg->regexp->re_nsub + 1 != NUM_SUBS) { return ap_pstrcat(cmd->pool, "Invalid cookie name \"", name, "\"", NULL); } return NULL;}/* * Set the value for the 'Domain=' attribute. */static const char *set_cookie_domain(cmd_parms *cmd, void *mconfig, char *name){ cookie_dir_rec *dcfg; dcfg = (cookie_dir_rec *) mconfig; /* * Apply the restrictions on cookie domain attributes. */ if (strlen(name) == 0) { return "CookieDomain values may not be null"; } if (name[0] != '.') { return "CookieDomain values must begin with a dot"; } if (strchr(&name[1], '.') == NULL) { return "CookieDomain values must contain at least one embedded dot"; } dcfg->cookie_domain = ap_pstrdup(cmd->pool, name); return NULL;}/* * Make a note of the cookie style we should use. */static const char *set_cookie_style(cmd_parms *cmd, void *mconfig, char *name){ cookie_dir_rec *dcfg; dcfg = (cookie_dir_rec *) mconfig; if (strcasecmp(name, "Netscape") == 0) { dcfg->style = CT_NETSCAPE; } else if ((strcasecmp(name, "Cookie") == 0) || (strcasecmp(name, "RFC2109") == 0)) { dcfg->style = CT_COOKIE; } else if ((strcasecmp(name, "Cookie2") == 0) || (strcasecmp(name, "RFC2965") == 0)) { dcfg->style = CT_COOKIE2; } else { return ap_psprintf(cmd->pool, "Invalid %s keyword: '%s'", cmd->cmd->name, name); } return NULL;}/* * Make a note of the cookie format we should use. */static const char *set_cookie_format(cmd_parms *cmd, void *mconfig, char *name){ cookie_dir_rec *dcfg; dcfg = (cookie_dir_rec *) mconfig; if (strcasecmp(name, "Normal") == 0) { dcfg->format = CF_NORMAL; } else if (strcasecmp(name, "Compact") == 0) { dcfg->format = CF_COMPACT; } else { return ap_psprintf(cmd->pool, "Invalid %s keyword: '%s'", cmd->cmd->name, name); } return NULL;}static const char *set_cookie_prefix(cmd_parms *cmd, void *mconfig, char *name){ cookie_dir_rec *dcfg = (cookie_dir_rec *) mconfig; dcfg->prefix_string = ap_pstrdup(cmd->pool, name); return NULL;}static const command_rec cookie_log_cmds[] = { {"CookieExpires", set_cookie_exp, NULL, OR_FILEINFO, TAKE1, "an expiry date code"}, {"CookieTracking", set_cookie_enable, NULL, OR_FILEINFO, FLAG, "whether or not to enable cookies"}, {"CookieName", set_cookie_name, NULL, OR_FILEINFO, TAKE1, "name of the tracking cookie"}, {"CookieDomain", set_cookie_domain, NULL, OR_FILEINFO, TAKE1, "domain to which this cookie applies"}, {"CookieStyle", set_cookie_style, NULL, OR_FILEINFO, TAKE1, "'Netscape', 'Cookie' (RFC2109), or 'Cookie2' (RFC2965)"}, {"CookieFormat", set_cookie_format, NULL, OR_FILEINFO, TAKE1, "'Normal' or 'Compact'"}, {"CookiePrefix", set_cookie_prefix, NULL, OR_FILEINFO, TAKE1, "String prepended to cookie"}, {NULL}};module MODULE_VAR_EXPORT usertrack_module = { STANDARD_MODULE_STUFF, NULL, /* initializer */ make_cookie_dir, /* dir config creater */ NULL, /* dir merger --- default is to override */ make_cookie_log_state, /* server config */ NULL, /* merge server configs */ cookie_log_cmds, /* command table */ NULL, /* handlers */ NULL, /* filename translation */ NULL, /* check_user_id */ NULL, /* check auth */ NULL, /* check access */ NULL, /* type_checker */ spot_cookie, /* fixups */ NULL, /* logger */ NULL, /* header parser */ NULL, /* child_init */ NULL, /* child_exit */ NULL /* post read-request */};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -