Apache HTTPD
util_cookies.c
Go to the documentation of this file.
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17#include "util_cookies.h"
18#include "apr_lib.h"
19#include "apr_strings.h"
20#include "http_config.h"
21#include "http_core.h"
22#include "http_log.h"
23
24#define LOG_PREFIX "ap_cookie: "
25
26/* we know core's module_index is 0 */
27#undef APLOG_MODULE_INDEX
28#define APLOG_MODULE_INDEX AP_CORE_MODULE_INDEX
29
41 const char *attrs, long maxage, ...)
42{
43
44 const char *buffer;
45 const char *rfc2109;
47 va_list vp;
48
49 /* handle expiry */
50 buffer = "";
51 if (maxage) {
52 buffer = apr_pstrcat(r->pool, "Max-Age=", apr_ltoa(r->pool, maxage), ";", NULL);
53 }
54
55 /* create RFC2109 compliant cookie */
56 rfc2109 = apr_pstrcat(r->pool, name, "=", val, ";", buffer,
59 "user '%s' set cookie: '%s'", r->user, rfc2109);
60
61 /* write the cookie to the header table(s) provided */
62 va_start(vp, maxage);
63 while ((t = va_arg(vp, apr_table_t *))) {
65 }
66 va_end(vp);
67
68 return APR_SUCCESS;
69
70}
71
82AP_DECLARE(apr_status_t) ap_cookie_write2(request_rec * r, const char *name2, const char *val,
83 const char *attrs2, long maxage, ...)
84{
85
86 const char *buffer;
87 const char *rfc2965;
89 va_list vp;
90
91 /* handle expiry */
92 buffer = "";
93 if (maxage) {
94 buffer = apr_pstrcat(r->pool, "Max-Age=", apr_ltoa(r->pool, maxage), ";", NULL);
95 }
96
97 /* create RFC2965 compliant cookie */
98 rfc2965 = apr_pstrcat(r->pool, name2, "=", val, ";", buffer,
101 "user '%s' set cookie2: '%s'", r->user, rfc2965);
102
103 /* write the cookie to the header table(s) provided */
104 va_start(vp, maxage);
105 while ((t = va_arg(vp, apr_table_t *))) {
107 }
108 va_end(vp);
109
110 return APR_SUCCESS;
111
112}
113
121{
122 apr_table_t *t;
123 va_list vp;
124
125 /* create RFC2109 compliant cookie */
126 const char *rfc2109 = apr_pstrcat(r->pool, name, "=;Max-Age=0;",
129 "user '%s' removed cookie: '%s'", r->user, rfc2109);
130
131 /* write the cookie to the header table(s) provided */
132 va_start(vp, attrs);
133 while ((t = va_arg(vp, apr_table_t *))) {
135 }
136 va_end(vp);
137
138 return APR_SUCCESS;
139
140}
141
148AP_DECLARE(apr_status_t) ap_cookie_remove2(request_rec * r, const char *name2, const char *attrs2, ...)
149{
150 apr_table_t *t;
151 va_list vp;
152
153 /* create RFC2965 compliant cookie */
154 const char *rfc2965 = apr_pstrcat(r->pool, name2, "=;Max-Age=0;",
157 "user '%s' removed cookie2: '%s'", r->user, rfc2965);
158
159 /* write the cookie to the header table(s) provided */
161 while ((t = va_arg(vp, apr_table_t *))) {
163 }
164 va_end(vp);
165
166 return APR_SUCCESS;
167
168}
169
170/* Iterate through the cookies, isolate our cookie and then remove it.
171 *
172 * If our cookie appears two or more times, but with different values,
173 * remove it twice and set the duplicated flag to true. Remove any
174 * $path or other attributes following our cookie if present. If we end
175 * up with an empty cookie, remove the whole header.
176 */
177static int extract_cookie_line(void *varg, const char *key, const char *val)
178{
179 ap_cookie_do *v = varg;
180 char *last1, *last2;
181 char *cookie = apr_pstrdup(v->r->pool, val);
182 const char *name = apr_pstrcat(v->r->pool, v->name ? v->name : "", "=", NULL);
183 apr_size_t len = strlen(name);
184 const char *new_cookie = "";
185 const char *comma = ",";
186 char *next1;
187 const char *semi = ";";
188 char *next2;
189 const char *sep = "";
190 int cookies = 0;
191
192 /* find the cookie called name */
193 int eat = 0;
194 next1 = apr_strtok(cookie, comma, &last1);
195 while (next1) {
197 while (next2) {
198 char *trim = next2;
199 while (apr_isspace(*trim)) {
200 trim++;
201 }
202 if (!strncmp(trim, name, len)) {
203 if (v->encoded) {
204 if (strcmp(v->encoded, trim + len)) {
205 v->duplicated = 1;
206 }
207 }
208 v->encoded = apr_pstrdup(v->r->pool, trim + len);
209 eat = 1;
210 }
211 else {
212 if (*trim != '$') {
213 cookies++;
214 eat = 0;
215 }
216 if (!eat) {
218 }
219 }
221 sep = semi;
222 }
223
225 sep = comma;
226 }
227
228 /* any cookies left over? */
229 if (cookies) {
231 }
232
233 return 1;
234}
235
246 int remove)
247{
248
249 ap_cookie_do v;
250 v.r = r;
251 v.encoded = NULL;
253 v.duplicated = 0;
254 v.name = name;
255
257 "Cookie", "Cookie2", NULL);
258 if (v.duplicated) {
260 "client submitted cookie '%s' more than once: %s", v.name, r->uri);
261 return APR_EGENERAL;
262 }
263
264 /* remove our cookie(s), and replace them */
265 if (remove) {
266 apr_table_unset(r->headers_in, "Cookie");
267 apr_table_unset(r->headers_in, "Cookie2");
269 }
270
271 *val = v.encoded;
272
273 return APR_SUCCESS;
274
275}
276
284{
285 if (!string || !*string || ap_strchr_c(string, '=') || ap_strchr_c(string, '&') ||
286 ap_strchr_c(string, ';')) {
287 return APR_EGENERAL;
288 }
289 return APR_SUCCESS;
290}
#define AP_DECLARE(type)
Definition ap_config.h:67
const char apr_size_t len
Definition ap_regex.h:187
APR general purpose library routines.
APR Strings library.
request_rec * r
#define APLOGNO(n)
Definition http_log.h:117
#define ap_log_rerror
Definition http_log.h:454
#define APLOG_ERR
Definition http_log.h:67
#define APLOG_MARK
Definition http_log.h:283
#define APLOG_DEBUG
Definition http_log.h:71
#define APR_EGENERAL
Definition apr_errno.h:313
#define ap_strchr_c(s, c)
Definition httpd.h:2353
apr_size_t size
apr_uint32_t val
Definition apr_atomic.h:66
#define apr_isspace(c)
Definition apr_lib.h:225
#define APR_SUCCESS
Definition apr_errno.h:225
int apr_status_t
Definition apr_errno.h:44
const char * key
char * buffer
apr_interval_time_t t
const char * sep
void const apr_table_t void const apr_table_t va_list vp
Definition apr_tables.h:434
Apache Configuration.
CORE HTTP Daemon.
Apache Logging library.
#define trim(line)
Definition mod_macro.c:85
return NULL
Definition mod_so.c:359
char * name
A structure that represents the current request.
Definition httpd.h:845
char * user
Definition httpd.h:1005
char * uri
Definition httpd.h:1016
apr_pool_t * pool
Definition httpd.h:847
apr_table_t * headers_in
Definition httpd.h:976
static int extract_cookie_line(void *varg, const char *key, const char *val)
#define LOG_PREFIX
Apache cookie library.