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