#include "util_cookies.h"
#include "apr_lib.h"
#include "apr_strings.h"
+#include "http_config.h"
+#include "http_core.h"
#include "http_log.h"
#define LOG_PREFIX "ap_cookie: "
+/* we know core's module_index is 0 */
+#undef APLOG_MODULE_INDEX
+#define APLOG_MODULE_INDEX AP_CORE_MODULE_INDEX
+
/**
* Write an RFC2109 compliant cookie.
*
* @param maxage If non zero, a Max-Age header will be added to the cookie.
*/
AP_DECLARE(apr_status_t) ap_cookie_write(request_rec * r, const char *name, const char *val,
- const char *attrs, long maxage)
+ const char *attrs, long maxage, ...)
{
- char *buffer;
- char *rfc2109;
+ const char *buffer;
+ const char *rfc2109;
+ apr_table_t *t;
+ va_list vp;
/* handle expiry */
buffer = "";
}
/* create RFC2109 compliant cookie */
- rfc2109 = apr_pstrcat(r->pool, name, "=", val, ";",
- buffer,
- attrs && strlen(attrs) > 0 ?
- attrs : DEFAULT_ATTRS, NULL);
- ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, LOG_PREFIX
+ rfc2109 = apr_pstrcat(r->pool, name, "=", val, ";", buffer,
+ attrs && *attrs ? attrs : DEFAULT_ATTRS, NULL);
+ ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00007) LOG_PREFIX
"user '%s' set cookie: '%s'", r->user, rfc2109);
- apr_table_addn(r->headers_out, SET_COOKIE, rfc2109);
+
+ /* write the cookie to the header table(s) provided */
+ va_start(vp, maxage);
+ while ((t = va_arg(vp, apr_table_t *))) {
+ apr_table_addn(t, SET_COOKIE, rfc2109);
+ }
+ va_end(vp);
return APR_SUCCESS;
* @param maxage If non zero, a Max-Age header will be added to the cookie.
*/
AP_DECLARE(apr_status_t) ap_cookie_write2(request_rec * r, const char *name2, const char *val,
- const char *attrs2, long maxage)
+ const char *attrs2, long maxage, ...)
{
- char *buffer;
- char *rfc2965;
+ const char *buffer;
+ const char *rfc2965;
+ apr_table_t *t;
+ va_list vp;
/* handle expiry */
buffer = "";
}
/* create RFC2965 compliant cookie */
- rfc2965 = apr_pstrcat(r->pool, name2, "=", val, ";",
- buffer,
- attrs2 && strlen(attrs2) > 0 ?
- attrs2 : DEFAULT_ATTRS, NULL);
- ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, LOG_PREFIX
+ rfc2965 = apr_pstrcat(r->pool, name2, "=", val, ";", buffer,
+ attrs2 && *attrs2 ? attrs2 : DEFAULT_ATTRS, NULL);
+ ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00008) LOG_PREFIX
"user '%s' set cookie2: '%s'", r->user, rfc2965);
- apr_table_addn(r->headers_out, SET_COOKIE2, rfc2965);
+
+ /* write the cookie to the header table(s) provided */
+ va_start(vp, maxage);
+ while ((t = va_arg(vp, apr_table_t *))) {
+ apr_table_addn(t, SET_COOKIE2, rfc2965);
+ }
+ va_end(vp);
return APR_SUCCESS;
* @param r The request
* @param name The name of the cookie.
*/
-AP_DECLARE(apr_status_t) ap_cookie_remove(request_rec * r, const char *name)
+AP_DECLARE(apr_status_t) ap_cookie_remove(request_rec * r, const char *name, const char *attrs, ...)
{
+ apr_table_t *t;
+ va_list vp;
/* create RFC2109 compliant cookie */
- char *rfc2109 = apr_pstrcat(r->pool, name, "=;",
- CLEAR_ATTRS, NULL);
- ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, LOG_PREFIX
+ const char *rfc2109 = apr_pstrcat(r->pool, name, "=;Max-Age=0;",
+ attrs ? attrs : CLEAR_ATTRS, NULL);
+ ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00009) LOG_PREFIX
"user '%s' removed cookie: '%s'", r->user, rfc2109);
- apr_table_addn(r->headers_out, SET_COOKIE, rfc2109);
+
+ /* write the cookie to the header table(s) provided */
+ va_start(vp, attrs);
+ while ((t = va_arg(vp, apr_table_t *))) {
+ apr_table_addn(t, SET_COOKIE, rfc2109);
+ }
+ va_end(vp);
return APR_SUCCESS;
* @param r The request
* @param name2 The name of the cookie.
*/
-AP_DECLARE(apr_status_t) ap_cookie_remove2(request_rec * r, const char *name2)
+AP_DECLARE(apr_status_t) ap_cookie_remove2(request_rec * r, const char *name2, const char *attrs2, ...)
{
+ apr_table_t *t;
+ va_list vp;
/* create RFC2965 compliant cookie */
- char *rfc2965 = apr_pstrcat(r->pool, name2, "=;",
- CLEAR_ATTRS, NULL);
- ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, LOG_PREFIX
+ const char *rfc2965 = apr_pstrcat(r->pool, name2, "=;Max-Age=0;",
+ attrs2 ? attrs2 : CLEAR_ATTRS, NULL);
+ ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00010) LOG_PREFIX
"user '%s' removed cookie2: '%s'", r->user, rfc2965);
- apr_table_addn(r->headers_out, SET_COOKIE2, rfc2965);
+
+ /* write the cookie to the header table(s) provided */
+ va_start(vp, attrs2);
+ while ((t = va_arg(vp, apr_table_t *))) {
+ apr_table_addn(t, SET_COOKIE2, rfc2965);
+ }
+ va_end(vp);
return APR_SUCCESS;
* $path or other attributes following our cookie if present. If we end
* up with an empty cookie, remove the whole header.
*/
-static int extract_cookie_line(ap_cookie_do * v, const char *key, const char *val)
+static int extract_cookie_line(void *varg, const char *key, const char *val)
{
+ ap_cookie_do *v = varg;
char *last1, *last2;
char *cookie = apr_pstrdup(v->r->pool, val);
const char *name = apr_pstrcat(v->r->pool, v->name ? v->name : "", "=", NULL);
- size_t len = strlen(name);
- char *new_cookie = "";
+ apr_size_t len = strlen(name);
+ const char *new_cookie = "";
const char *comma = ",";
char *next1;
const char *semi = ";";
v.duplicated = 0;
v.name = name;
- apr_table_do((int (*) (void *, const char *, const char *))
- extract_cookie_line, (void *) &v, r->headers_in,
+ apr_table_do(extract_cookie_line, &v, r->headers_in,
"Cookie", "Cookie2", NULL);
if (v.duplicated) {
- ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, LOG_PREFIX
+ ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00011) LOG_PREFIX
"client submitted cookie '%s' more than once: %s", v.name, r->uri);
return APR_EGENERAL;
}