util_cookies.c revision 562e9ce367eaaf4d3ea0ed4eaf3dbf0a644cf4aa
842ae4bd224140319ae7feec1872b93dfd491143fielding/* Licensed to the Apache Software Foundation (ASF) under one or more
842ae4bd224140319ae7feec1872b93dfd491143fielding * contributor license agreements. See the NOTICE file distributed with
842ae4bd224140319ae7feec1872b93dfd491143fielding * this work for additional information regarding copyright ownership.
842ae4bd224140319ae7feec1872b93dfd491143fielding * The ASF licenses this file to You under the Apache License, Version 2.0
842ae4bd224140319ae7feec1872b93dfd491143fielding * (the "License"); you may not use this file except in compliance with
842ae4bd224140319ae7feec1872b93dfd491143fielding * the License. You may obtain a copy of the License at
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * Unless required by applicable law or agreed to in writing, software
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * distributed under the License is distributed on an "AS IS" BASIS,
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * See the License for the specific language governing permissions and
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * limitations under the License.
f5ec9b038bb9db933072ba2c0a8e7bb2a3cedbdagstein * Write an RFC2109 compliant cookie.
cccd31fa4a72fe23cc3249c06db181b274a55a69gstein * @param r The request
cccd31fa4a72fe23cc3249c06db181b274a55a69gstein * @param name The name of the cookie.
cccd31fa4a72fe23cc3249c06db181b274a55a69gstein * @param val The value to place in the cookie.
f4c310fd2555c6faca1f980f00b161eadb089023gstein * @param attrs The string containing additional cookie attributes. If NULL, the
f4c310fd2555c6faca1f980f00b161eadb089023gstein * DEFAULT_ATTRS will be used.
6f15570e3adc0faf87bf55f70857028276fc9e32wrowe * @param maxage If non zero, a Max-Age header will be added to the cookie.
f4c310fd2555c6faca1f980f00b161eadb089023gsteinAP_DECLARE(apr_status_t) ap_cookie_write(request_rec * r, const char *name, const char *val,
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* handle expiry */
f4c310fd2555c6faca1f980f00b161eadb089023gstein buffer = apr_pstrcat(r->pool, "Max-Age=", apr_ltoa(r->pool, maxage), ";", NULL);
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* create RFC2109 compliant cookie */
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, LOG_PREFIX
f4c310fd2555c6faca1f980f00b161eadb089023gstein * Write an RFC2965 compliant cookie.
f4c310fd2555c6faca1f980f00b161eadb089023gstein * @param r The request
707ecf9559338ec06b24334bc9abcca670325cc4gstein * @param name2 The name of the cookie.
707ecf9559338ec06b24334bc9abcca670325cc4gstein * @param val The value to place in the cookie.
707ecf9559338ec06b24334bc9abcca670325cc4gstein * @param attrs2 The string containing additional cookie attributes. If NULL, the
707ecf9559338ec06b24334bc9abcca670325cc4gstein * DEFAULT_ATTRS will be used.
52c1d304b1bd8e05da40a7cded2ecb9f0ba614c5gstein * @param maxage If non zero, a Max-Age header will be added to the cookie.
f4c310fd2555c6faca1f980f00b161eadb089023gsteinAP_DECLARE(apr_status_t) ap_cookie_write2(request_rec * r, const char *name2, const char *val,
707ecf9559338ec06b24334bc9abcca670325cc4gstein /* handle expiry */
707ecf9559338ec06b24334bc9abcca670325cc4gstein buffer = apr_pstrcat(r->pool, "Max-Age=", apr_ltoa(r->pool, maxage), ";", NULL);
707ecf9559338ec06b24334bc9abcca670325cc4gstein /* create RFC2965 compliant cookie */
707ecf9559338ec06b24334bc9abcca670325cc4gstein ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, LOG_PREFIX
707ecf9559338ec06b24334bc9abcca670325cc4gstein apr_table_addn(r->headers_out, SET_COOKIE2, rfc2965);
f4c310fd2555c6faca1f980f00b161eadb089023gstein * Remove an RFC2109 compliant cookie.
f4c310fd2555c6faca1f980f00b161eadb089023gstein * @param r The request
0e8fe062a6ec71b4207d1a125ee6b44b7fd30857gstein * @param name The name of the cookie.
f4c310fd2555c6faca1f980f00b161eadb089023gsteinAP_DECLARE(apr_status_t) ap_cookie_remove(request_rec * r, const char *name)
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* create RFC2109 compliant cookie */
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, LOG_PREFIX
f4c310fd2555c6faca1f980f00b161eadb089023gstein * Remove an RFC2965 compliant cookie.
0b1895a2cd5b4a9450709abdb7ae9974908f9382gstein * @param r The request
0b1895a2cd5b4a9450709abdb7ae9974908f9382gstein * @param name2 The name of the cookie.
c70afa1853413eb72c4909354c2814d89e9b99f7jortonAP_DECLARE(apr_status_t) ap_cookie_remove2(request_rec * r, const char *name2)
0b1895a2cd5b4a9450709abdb7ae9974908f9382gstein /* create RFC2965 compliant cookie */
c70afa1853413eb72c4909354c2814d89e9b99f7jorton ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, LOG_PREFIX
c70afa1853413eb72c4909354c2814d89e9b99f7jorton "user '%s' removed cookie2: '%s'", r->user, rfc2965);
c70afa1853413eb72c4909354c2814d89e9b99f7jorton apr_table_addn(r->headers_out, SET_COOKIE2, rfc2965);
0b1895a2cd5b4a9450709abdb7ae9974908f9382gstein/* Iterate through the cookies, isolate our cookie and then remove it.
0e8fe062a6ec71b4207d1a125ee6b44b7fd30857gstein * If our cookie appears two or more times, but with different values,
f4c310fd2555c6faca1f980f00b161eadb089023gstein * remove it twice and set the duplicated flag to true. Remove any
0e8fe062a6ec71b4207d1a125ee6b44b7fd30857gstein * $path or other attributes following our cookie if present. If we end
f4c310fd2555c6faca1f980f00b161eadb089023gstein * up with an empty cookie, remove the whole header.
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic int extract_cookie_line(ap_cookie_do * v, const char *key, const char *val)
0e8fe062a6ec71b4207d1a125ee6b44b7fd30857gstein const char *name = apr_pstrcat(v->r->pool, v->name ? v->name : "", "=", NULL);
0e8fe062a6ec71b4207d1a125ee6b44b7fd30857gstein /* find the cookie called name */
0e8fe062a6ec71b4207d1a125ee6b44b7fd30857gstein new_cookie = apr_pstrcat(v->r->pool, new_cookie, sep, next2, NULL);
6f15570e3adc0faf87bf55f70857028276fc9e32wrowe /* any cookies left over? */
f94aab38f6ee899f463f0118ea395291f7c5b4cegstein * Read a cookie called name, placing its value in val.
707ecf9559338ec06b24334bc9abcca670325cc4gstein * Both the Cookie and Cookie2 headers are scanned for the cookie.
707ecf9559338ec06b24334bc9abcca670325cc4gstein * If the cookie is duplicated, this function returns APR_EGENERAL. If found,
707ecf9559338ec06b24334bc9abcca670325cc4gstein * and if remove is non zero, the cookie will be removed from the headers, and
f4c310fd2555c6faca1f980f00b161eadb089023gstein * thus kept private from the backend.
f4c310fd2555c6faca1f980f00b161eadb089023gsteinAP_DECLARE(apr_status_t) ap_cookie_read(request_rec * r, const char *name, const char **val,
f4c310fd2555c6faca1f980f00b161eadb089023gstein apr_table_do((int (*) (void *, const char *, const char *))
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, LOG_PREFIX
f4c310fd2555c6faca1f980f00b161eadb089023gstein "client submitted cookie '%s' more than once: %s", v.name, r->uri);
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* remove our cookie(s), and replace them */
f4c310fd2555c6faca1f980f00b161eadb089023gstein r->headers_in = apr_table_overlay(r->pool, r->headers_in, v.new_cookies);
2f40d5ac42ba2e7a8043eca56b5f5d8dce101f94wrowe * Sanity check a given string that it exists, is not empty,
e8f95a682820a599fe41b22977010636be5c2717jim * and does not contain the special characters '=', ';' and '&'.
2f40d5ac42ba2e7a8043eca56b5f5d8dce101f94wrowe * It is used to sanity check the cookie names.
2f40d5ac42ba2e7a8043eca56b5f5d8dce101f94wroweAP_DECLARE(apr_status_t) ap_cookie_check_string(const char *string)
2f40d5ac42ba2e7a8043eca56b5f5d8dce101f94wrowe if (!string || !*string || ap_strchr_c(string, '=') || ap_strchr_c(string, '&') ||