Annotation of parser3/src/types/pa_vcookie.C, revision 1.55.2.2
1.6 paf 1: /** @file
2: Parser: cookie class.
3:
1.55 paf 4: Copyright(c) 2001, 2003 ArtLebedev Group (http://www.artlebedev.com)
1.34 paf 5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.44 paf 6: */
1.6 paf 7:
1.55.2.2! paf 8: static const char* IDENT_VCOOKIE_C="$Date: 2003/01/29 12:57:37 $";
1.1 paf 9:
1.11 paf 10: #include "pa_sapi.h"
1.3 paf 11: #include "pa_common.h"
1.55.2.1 paf 12: #include "pa_value_includes.h"
1.1 paf 13: #include "pa_vcookie.h"
14: #include "pa_vstring.h"
1.49 paf 15: #include "pa_vdate.h"
1.55.2.1 paf 16: #include "pa_vhash.h"
17:
18: // defines
19:
20: #define EXPIRES_NAME "expires"
21: #define PATH_NAME "path"
22: #define PATH_VALUE_DEFAULT "/"
1.1 paf 23:
24: #define SESSION_NAME "session"
25: #define DEFAULT_EXPIRES_DAYS 90
1.9 paf 26:
1.55.2.1 paf 27: // statics
28:
29: static StringPtr expires_name(new String(EXPIRES_NAME));
30: static StringPtr path_name(new String(PATH_NAME));
31: static StringPtr path_value_default(new String(PATH_VALUE_DEFAULT));
32:
1.1 paf 33: // VCookie
34:
1.55.2.2! paf 35: ValuePtr VCookie::get_element(StringPtr aname, Value& /*aself*/, bool /*looking_up*/) {
1.28 parser 36: // $CLASS
1.55.2.1 paf 37: if(*aname==CLASS_NAME)
1.28 parser 38: return this;
39:
1.1 paf 40: // $cookie
1.46 paf 41: if(deleted.get(aname)) // deleted?
1.1 paf 42: return 0;
43:
1.55.2.1 paf 44: if(ValuePtr after_meaning=after.get(aname)) // assigned 'after'?
45: if(HashStringValue *hash=after_meaning->get_hash(aname))
46: return hash->get(value_name);
1.2 paf 47: else
48: return after_meaning;
1.1 paf 49:
50: // neither deleted nor assigned
51: // return any value it had 'before'
1.55.2.1 paf 52: return before.get(aname);
1.1 paf 53: }
54:
1.55.2.2! paf 55: bool VCookie::put_element(StringPtr aname, ValuePtr avalue, bool /*replace*/) {
1.1 paf 56: // $cookie
57: bool remove;
1.55.2.1 paf 58: ValuePtr lvalue;
59: if(HashStringValue *hash=avalue->get_hash(aname))
60: lvalue=hash->get(value_name);
1.1 paf 61: else
1.50 paf 62: lvalue=avalue;
1.51 paf 63:
64: if(lvalue && lvalue->is_string()) {
65: // taint string being assigned
1.55.2.1 paf 66: StringPtr tainted(new String);
67: tainted->append(*lvalue->get_string(0), String::UL_TAINTED, true /*forced*/);
68: lvalue=ValuePtr(new VString(tainted));
1.51 paf 69: }
1.1 paf 70:
1.55.2.1 paf 71: remove=!lvalue || lvalue->as_string(0)->is_empty();
1.50 paf 72:
1.54 paf 73: (remove?deleted:after).put(aname, avalue);
1.55.2.1 paf 74: (remove?after:deleted).put(aname, ValuePtr(0));
1.46 paf 75:
76: return true;
1.1 paf 77: }
78:
1.26 parser 79: static char *search_stop(char*& current, char cstop_at) {
1.37 paf 80: // sanity check
81: if(!current)
82: return 0;
83:
1.26 parser 84: // skip leading WS
85: while(*current==' ' || *current=='\t')
86: current++;
87: if(!*current)
88: return current=0;
89:
90: char *result=current;
91: if(char *pstop_at=strchr(current, cstop_at)) {
92: *pstop_at=0;
93: current=pstop_at+1;
94: } else
95: current=0;
96: return result;
97: }
98:
99:
1.6 paf 100: //#include <stdio.h>
1.55.2.1 paf 101: void VCookie::fill_fields(Pool& pool, Request_info& request_info) {
102: //request_info.cookie="test-session=value%3D5; test-default1=value%3D1; test-default2=value%3D2; test-tomorrow=value%3D3";
103: //request_info.cookie="enabled=yes; auth.uid=196325308053599810; enabled=yes; msnames; msuri"; // mdm
104: if(!request_info.cookie)
1.1 paf 105: return;
106: /*
107: FILE *f=fopen("c:\\temp\\a", "wt");
1.55.2.1 paf 108: fprintf(f, "cookie=%s", request_info.cookie);
1.1 paf 109: fclose(f);*/
1.55.2.1 paf 110: char *cookies=(char *)malloc(strlen(request_info.cookie)+1);
111: strcpy(cookies, request_info.cookie);
1.1 paf 112: char *current=cookies;
1.22 parser 113: uint line=0;
1.26 parser 114: //_asm int 3;
1.1 paf 115: do {
1.26 parser 116: if(char *attribute=search_stop(current, '='))
1.38 paf 117: if(char *meaning=search_stop(current, ';')) {
1.55.2.1 paf 118: StringPtr sattribute(new String);
119: StringPtr smeaning(new String);
120: sattribute->APPEND_TAINTED(unescape_chars(pool, attribute, strlen(attribute)), 0,
1.38 paf 121: "cookie_name", line);
1.55.2.1 paf 122: smeaning->APPEND_TAINTED(unescape_chars(pool, meaning, strlen(meaning)), 0,
1.38 paf 123: "cookie_value", line);
1.55.2.1 paf 124: before.put(sattribute, ValuePtr(new VString(smeaning)));
1.38 paf 125: line++;
126: }
1.1 paf 127: } while(current);
128: }
129:
1.55.2.2! paf 130: static ValuePtr expires_vdate(StringPtr source, double days_till_expire) {
1.1 paf 131: time_t when=time(NULL)+(time_t)(60*60*24*days_till_expire);
132: struct tm *tms=gmtime(&when);
1.41 paf 133: if(!tms)
1.55.2.1 paf 134: throw Exception(Exception::undefined_type,
1.42 paf 135: source,
1.41 paf 136: "bad expires time (seconds from epoch=%ld)", when);
1.49 paf 137:
1.55.2.1 paf 138: return ValuePtr(new VDate(when));
1.1 paf 139: }
140:
1.25 parser 141: /*
1.49 paf 142: @todo
143: http://www.netscape.com/newsref/std/cookie_spec.html
1.25 parser 144: When sending cookies to a server,
145: all cookies with a more specific path mapping should be sent before cookies
146: with less specific path mappings.
147: For example, a cookie "name1=foo" with a path mapping of "/" should be sent after
148: a cookie "name1=foo2" with a path mapping of "/bar" if they are both to be sent.
149:
150: There are limitations on the number of cookies that a client can store at any one time.
151: This is a specification of the minimum number of cookies that a client should be prepared
152: to receive and store.
153: 300 total cookies
154: 4 kilobytes per cookie, where the name and the OPAQUE_STRING combine
155: to form the 4 kilobyte limit.
156: 20 cookies per server or domain. (note that completely specified hosts
157: and domains are treated as separate entities and have a 20 cookie limitation
158: for each, not combined)
159: */
1.55.2.1 paf 160: static void output_set_cookie(
161: HashStringValue::key_type aattribute,
162: HashStringValue::value_type ameaning,
163: SAPI_Info& sapi_info) {
164: String string;
1.1 paf 165: // attribute
1.55.2.1 paf 166: string.append(*aattribute, String::UL_HTTP_HEADER, true);
1.1 paf 167: // attribute=
1.19 paf 168: string << "=";
1.55.2.1 paf 169: ValuePtr lmeaning;
1.1 paf 170: // figure out 'meaning'
171: if(ameaning) { // assigning value
172: // Set-Cookie: (attribute)=(value); path=/
1.55.2.1 paf 173: lmeaning=ameaning;
174: if(HashStringValue *hash=lmeaning->get_hash(aattribute)) { // ...[hash value]
1.1 paf 175: // $expires
1.55.2.1 paf 176: if(ValuePtr expires=hash->get(expires_name)) {
1.55.2.2! paf 177: StringPtr string;
1.55.2.1 paf 178: if((string=expires->get_string(0)) && (*string==SESSION_NAME)) {
1.1 paf 179: // $expires[session]
1.55.2.1 paf 180: hash->remove(expires_name);
1.1 paf 181: } else {
182: // $expires(days)
1.41 paf 183: if(double days_till_expire=expires->as_double())
1.55.2.1 paf 184: hash->put(expires_name, expires_vdate(aattribute, days_till_expire));
1.41 paf 185: else // $expires(0)
1.55.2.1 paf 186: hash->remove(expires_name);
1.1 paf 187: }
188: } else // $expires not assigned, defaulting
1.55.2.1 paf 189: hash->put(expires_name, expires_vdate(aattribute, DEFAULT_EXPIRES_DAYS));
1.1 paf 190: } else { // ...[string value]
1.55.2.1 paf 191: ValuePtr wrap_meaning(new VHash);
192: HashStringValue& hash=*wrap_meaning->get_hash(aattribute);
193: // wrapping lmeaning into hash
194: hash.put(value_name, lmeaning);
1.1 paf 195: // string = $expires not assigned, defaulting
1.55.2.1 paf 196: hash.put(expires_name, expires_vdate(aattribute, DEFAULT_EXPIRES_DAYS));
197: // replacing lmeaning with hash-wrapped one
198: lmeaning=wrap_meaning;
1.1 paf 199: }
200: } else {// removing value
1.25 parser 201: /*
202: http://www.netscape.com/newsref/std/cookie_spec.html
203: to delete a cookie, it can do so by returning a cookie with the same name,
204: and an expires time which is in the past
205: */
206:
1.1 paf 207: // Set-Cookie: (attribute)=; path=/
1.55.2.1 paf 208: lmeaning=ValuePtr(new VHash);
209: lmeaning->get_hash(aattribute)->put(expires_name,
210: expires_vdate(aattribute, -DEFAULT_EXPIRES_DAYS));
1.1 paf 211: }
212: // defaulting path
1.55.2.1 paf 213: if(!lmeaning->get_hash(aattribute)->get(path_name))
214: lmeaning->get_hash(aattribute)->put(path_name,
215: ValuePtr(new VString(path_value_default)));
1.1 paf 216:
1.55.2.1 paf 217: // append lmeaning
218: string << attributed_meaning_to_string(lmeaning, String::UL_HTTP_HEADER, true);
1.1 paf 219:
220: // output
1.55.2.1 paf 221: SAPI::add_header_attribute(sapi_info, "set-cookie", string.cstr(String::UL_UNSPECIFIED));
1.1 paf 222: }
1.55.2.1 paf 223: static void output_after(
224: HashStringValue::key_type aattribute,
225: HashStringValue::value_type ameaning,
226: SAPI_Info* sapi_info) {
227: output_set_cookie(aattribute, ameaning, *sapi_info);
228: }
229: static void output_deleted(
230: HashStringValue::key_type aattribute,
231: HashStringValue::value_type ameaning,
232: SAPI_Info* sapi_info) {
233: output_set_cookie(aattribute, ValuePtr(0), *sapi_info);
234: }
235: void VCookie::output_result(SAPI_Info& sapi_info) {
236: after.for_each(output_after, &sapi_info);
237: deleted.for_each(output_deleted, &sapi_info);
1.1 paf 238: }
E-mail: