1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "util_cookies.h"
21 #include "apr_strings.h"
24 #define LOG_PREFIX "ap_cookie: "
27 * Write an RFC2109 compliant cookie.
29 * @param r The request
30 * @param name The name of the cookie.
31 * @param val The value to place in the cookie.
32 * @param attrs The string containing additional cookie attributes. If NULL, the
33 * DEFAULT_ATTRS will be used.
34 * @param maxage If non zero, a Max-Age header will be added to the cookie.
36 AP_DECLARE(apr_status_t) ap_cookie_write(request_rec * r, const char *name, const char *val,
37 const char *attrs, long maxage)
45 buffer = apr_pstrcat(r->pool, "Max-Age=", apr_ltoa(r->pool, maxage), ";", NULL);
48 /* create RFC2109 compliant cookie */
49 char *rfc2109 = apr_pstrcat(r->pool, name, "=", val, ";",
51 attrs && strlen(attrs) > 0 ?
52 attrs : DEFAULT_ATTRS, NULL);
53 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, LOG_PREFIX
54 "user '%s' set cookie: '%s'", r->user, rfc2109);
55 apr_table_addn(r->headers_out, SET_COOKIE, rfc2109);
62 * Write an RFC2965 compliant cookie.
64 * @param r The request
65 * @param name2 The name of the cookie.
66 * @param val The value to place in the cookie.
67 * @param attrs2 The string containing additional cookie attributes. If NULL, the
68 * DEFAULT_ATTRS will be used.
69 * @param maxage If non zero, a Max-Age header will be added to the cookie.
71 AP_DECLARE(apr_status_t) ap_cookie_write2(request_rec * r, const char *name2, const char *val,
72 const char *attrs2, long maxage)
80 buffer = apr_pstrcat(r->pool, "Max-Age=", apr_ltoa(r->pool, maxage), ";");
83 /* create RFC2965 compliant cookie */
84 char *rfc2965 = apr_pstrcat(r->pool, name2, "=", val, ";",
86 attrs2 && strlen(attrs2) > 0 ?
87 attrs2 : DEFAULT_ATTRS, NULL);
88 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, LOG_PREFIX
89 "user '%s' set cookie2: '%s'", r->user, rfc2965);
90 apr_table_addn(r->headers_out, SET_COOKIE2, rfc2965);
97 * Remove an RFC2109 compliant cookie.
99 * @param r The request
100 * @param name The name of the cookie.
102 AP_DECLARE(apr_status_t) ap_cookie_remove(request_rec * r, const char *name)
105 /* create RFC2109 compliant cookie */
106 char *rfc2109 = apr_pstrcat(r->pool, name, "=;",
108 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, LOG_PREFIX
109 "user '%s' removed cookie: '%s'", r->user, rfc2109);
110 apr_table_addn(r->headers_out, SET_COOKIE, rfc2109);
117 * Remove an RFC2965 compliant cookie.
119 * @param r The request
120 * @param name2 The name of the cookie.
122 AP_DECLARE(apr_status_t) ap_cookie_remove2(request_rec * r, const char *name2)
125 /* create RFC2965 compliant cookie */
126 char *rfc2965 = apr_pstrcat(r->pool, name2, "=;",
128 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, LOG_PREFIX
129 "user '%s' removed cookie2: '%s'", r->user, rfc2965);
130 apr_table_addn(r->headers_out, SET_COOKIE2, rfc2965);
136 /* Iterate through the cookies, isolate our cookie and then remove it.
138 * If our cookie appears two or more times, but with different values,
139 * remove it twice and set the duplicated flag to true. Remove any
140 * $path or other attributes following our cookie if present. If we end
141 * up with an empty cookie, remove the whole header.
143 static int extract_cookie_line(ap_cookie_do * v, const char *key, const char *val)
146 char *cookie = apr_pstrdup(v->r->pool, val);
147 const char *name = apr_pstrcat(v->r->pool, v->name ? v->name : "", "=", NULL);
148 size_t len = strlen(name);
149 char *new_cookie = "";
150 const char *comma = ",";
152 const char *semi = ";";
154 const char *sep = "";
157 /* find the cookie called name */
159 next1 = apr_strtok(cookie, comma, &last1);
161 next2 = apr_strtok(next1, semi, &last2);
164 while (apr_isspace(*trim)) {
167 if (!strncmp(trim, name, len)) {
169 if (strcmp(v->encoded, trim + len)) {
173 v->encoded = apr_pstrdup(v->r->pool, trim + len);
182 new_cookie = apr_pstrcat(v->r->pool, new_cookie, sep, next2, NULL);
185 next2 = apr_strtok(NULL, semi, &last2);
189 next1 = apr_strtok(NULL, comma, &last1);
193 /* any cookies left over? */
195 apr_table_addn(v->new_cookies, key, new_cookie);
202 * Read a cookie called name, placing its value in val.
204 * Both the Cookie and Cookie2 headers are scanned for the cookie.
206 * If the cookie is duplicated, this function returns APR_EGENERAL. If found,
207 * and if remove is non zero, the cookie will be removed from the headers, and
208 * thus kept private from the backend.
210 AP_DECLARE(apr_status_t) ap_cookie_read(request_rec * r, const char *name, const char **val,
217 v.new_cookies = apr_table_make(r->pool, 10);
221 apr_table_do((int (*) (void *, const char *, const char *))
222 extract_cookie_line, (void *) &v, r->headers_in,
223 "Cookie", "Cookie2", NULL);
225 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, LOG_PREFIX
226 "client submitted cookie '%s' more than once: %s", v.name, r->uri);
230 /* remove our cookie(s), and replace them */
232 apr_table_unset(r->headers_in, "Cookie");
233 apr_table_unset(r->headers_in, "Cookie2");
234 r->headers_in = apr_table_overlay(r->pool, r->headers_in, v.new_cookies);
244 * Sanity check a given string that it exists, is not empty,
245 * and does not contain the special characters '=', ';' and '&'.
247 * It is used to sanity check the cookie names.
249 AP_DECLARE(apr_status_t) ap_cookie_check_string(const char *string)
251 if (!string || !*string || ap_strchr_c(string, '=') || ap_strchr_c(string, '&') ||
252 ap_strchr_c(string, ';')) {