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