Annotation of parser3/src/types/pa_vcookie.C, revision 1.84
1.6 paf 1: /** @file
2: Parser: cookie class.
3:
1.76 misha 4: Copyright(c) 2001-2009 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.84 ! misha 8: static const char * const IDENT_VCOOKIE_C="$Date: 2010-05-17 22:57:29 $";
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"
1.74 misha 16: #include "pa_request.h"
1.56 paf 17:
18: // defines
19:
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.72 misha 26: #define COOKIE_FIELDS_ELEMENT_NAME "fields"
27:
1.56 paf 28: // statics
29:
30: static const String path_name(PATH_NAME);
31: static const String path_value_default(PATH_VALUE_DEFAULT);
32:
1.1 paf 33: // VCookie
34:
1.74 misha 35: VCookie::VCookie(Request_charsets& acharsets, Request_info& arequest_info):
36: fcharsets(acharsets),
37: frequest_info(arequest_info) {
38: }
39:
1.80 misha 40: Value* VCookie::get_element(const String& aname) {
1.28 parser 41: // $CLASS
1.46 paf 42: if(aname==CLASS_NAME)
1.28 parser 43: return this;
44:
1.76 misha 45: // $CLASS_NAME
46: if(aname==CLASS_NAMETEXT)
47: return new VString(cookie_class_name);
48:
1.74 misha 49: // $fields
1.72 misha 50: if(aname==COOKIE_FIELDS_ELEMENT_NAME){
1.74 misha 51: if(should_refill())
52: refill();
53:
54: HashStringValue *result=new HashStringValue(before);
1.72 misha 55: after.for_each<HashStringValue*>(copy_all_overwrite_to, result);
56: deleted.for_each<HashStringValue*>(remove_key_from, result);
57: return new VHash(*result);
58: }
59:
1.1 paf 60: // $cookie
1.46 paf 61: if(deleted.get(aname)) // deleted?
1.1 paf 62: return 0;
63:
1.56 paf 64: if(Value* after_meaning=after.get(aname)) // assigned 'after'?
65: if(HashStringValue *hash=after_meaning->get_hash())
66: return hash->get(value_name);
1.2 paf 67: else
68: return after_meaning;
1.1 paf 69:
1.74 misha 70: if(should_refill())
71: refill();
72:
1.1 paf 73: // neither deleted nor assigned
74: // return any value it had 'before'
1.56 paf 75: return before.get(aname);
1.1 paf 76: }
77:
1.84 ! misha 78: time_t expires_sec(double days_till_expire) {
! 79: time_t result=time(NULL)+(time_t)(60*60*24*days_till_expire);
! 80: struct tm* tms=gmtime(&result);
! 81: if(!tms)
! 82: throw Exception(DATE_RANGE_EXCEPTION_TYPE,
! 83: 0,
! 84: "bad expires time (seconds from epoch=%u)", result);
! 85: return result;
! 86: }
! 87:
1.80 misha 88: const VJunction* VCookie::put_element(const String& aname, Value* avalue, bool /*replace*/) {
1.1 paf 89: // $cookie
1.56 paf 90: Value* lvalue;
1.84 ! misha 91: if(HashStringValue *hash=avalue->get_hash()) {
! 92: if(Value* expires=hash->get(expires_name))
! 93: if(double days_till_expire=expires->as_double())
! 94: expires_sec(days_till_expire);
1.56 paf 95: lvalue=hash->get(value_name);
1.84 ! misha 96: } else
1.50 paf 97: lvalue=avalue;
1.51 paf 98:
99: if(lvalue && lvalue->is_string()) {
100: // taint string being assigned
1.56 paf 101: String& tainted=*new String;
102: tainted.append(*lvalue->get_string(), String::L_TAINTED, true /*forced*/);
103: lvalue=new VString(tainted);
1.51 paf 104: }
1.1 paf 105:
1.71 misha 106: if( !lvalue || lvalue->as_string().is_empty() ) {
107: deleted.put(aname, avalue);
108: after.put(aname, 0);
109: } else {
110: after.put(aname, avalue);
111: deleted.put(aname, 0);
112: }
1.64 paf 113: return PUT_ELEMENT_REPLACED_ELEMENT;
1.1 paf 114: }
115:
1.26 parser 116: static char *search_stop(char*& current, char cstop_at) {
1.37 paf 117: // sanity check
118: if(!current)
119: return 0;
120:
1.26 parser 121: // skip leading WS
122: while(*current==' ' || *current=='\t')
123: current++;
124: if(!*current)
125: return current=0;
126:
127: char *result=current;
128: if(char *pstop_at=strchr(current, cstop_at)) {
129: *pstop_at=0;
130: current=pstop_at+1;
131: } else
132: current=0;
133: return result;
134: }
135:
136:
1.56 paf 137: static Value& expires_vdate(double days_till_expire) {
1.84 ! misha 138: return *new VDate(expires_sec(days_till_expire));
1.1 paf 139: }
140:
1.25 parser 141: /*
1.49 paf 142: @todo
1.73 misha 143: http://wp.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:
1.74 misha 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)
1.25 parser 159: */
1.74 misha 160:
161: const String* output_set_cookie_value(
162: HashStringValue::key_type aname,
163: HashStringValue::value_type ameaning,
164: bool adelete){
165: String* result=new String();
1.1 paf 166: // attribute=
1.79 misha 167: *result << String(aname, String::L_HTTP_COOKIE) << "=";
1.74 misha 168:
1.56 paf 169: Value* lmeaning;
1.1 paf 170: // figure out 'meaning'
1.61 paf 171: // Set-Cookie: (attribute)=(value); path=/
172: HashStringValue *hash;
173: double default_expires_days=adelete?-DEFAULT_EXPIRES_DAYS:+DEFAULT_EXPIRES_DAYS;
174: if((hash=ameaning->get_hash())) { // ...[hash value]
175: // clone to safely change it
1.74 misha 176: lmeaning=new VHash(*hash);
177: hash=lmeaning->get_hash();
178:
1.61 paf 179: // $expires
180: if(Value* expires=hash->get(expires_name)) {
181: const String* string;
182: if(expires->is_string() && (string=expires->get_string()) && (*string==SESSION_NAME)) {
183: // $expires[session]
184: hash->remove(expires_name);
185: } else {
1.80 misha 186: if(Value* vdate=expires->as(VDATE_TYPE))
1.61 paf 187: hash->put(expires_name, vdate); // $expires[DATE]
188: else if(double days_till_expire=expires->as_double())
189: hash->put(expires_name, &expires_vdate(days_till_expire)); // $expires(days)
190: else
191: hash->remove(expires_name); // $expires(0)
192: }
193: } else // $expires not assigned, defaulting
194: hash->put(expires_name, &expires_vdate(default_expires_days));
195: } else { // ...[string value]
196: Value* wrap_meaning=new VHash;
197: hash=wrap_meaning->get_hash();
198: // wrapping lmeaning into hash
199: hash->put(value_name, ameaning);
200: // string = $expires not assigned, defaulting
201: hash->put(expires_name, &expires_vdate(default_expires_days));
202: // replacing lmeaning with hash-wrapped one
203: lmeaning=wrap_meaning;
204: }
205:
206: if(adelete) {// removing value
1.25 parser 207: /*
1.73 misha 208: http://wp.netscape.com/newsref/std/cookie_spec.html
1.25 parser 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.61 paf 214: lmeaning->get_hash()->remove(value_name);
1.1 paf 215: }
1.74 misha 216:
1.1 paf 217: // defaulting path
1.56 paf 218: if(!lmeaning->get_hash()->get(path_name))
1.74 misha 219: lmeaning->get_hash()->put(path_name, new VString(path_value_default));
1.1 paf 220:
1.56 paf 221: // append lmeaning
1.74 misha 222: *result << attributed_meaning_to_string(*lmeaning, String::L_HTTP_COOKIE, true, true /* allow bool attr */);
1.1 paf 223:
1.74 misha 224: return result;
1.1 paf 225: }
1.74 misha 226:
227:
228: struct Cookie_pass_info {
229: SAPI_Info* sapi_info;
230: Request_charsets* charsets;
231: };
232:
233: void output_set_cookie_header(
234: HashStringValue::key_type aattribute,
235: HashStringValue::value_type ameaning,
236: bool adelete,
237: Cookie_pass_info& cookie_info
238: ){
1.82 misha 239: SAPI::add_header_attribute(*cookie_info.sapi_info, "set-cookie",
1.79 misha 240: output_set_cookie_value(aattribute, ameaning, adelete)->untaint_cstr(String::L_AS_IS, 0, cookie_info.charsets));
1.56 paf 241: }
1.74 misha 242:
243: void output_after(
244: HashStringValue::key_type aattribute,
245: HashStringValue::value_type ameaning,
246: Cookie_pass_info& cookie_info
247: ){
248: output_set_cookie_header(aattribute, ameaning, false, cookie_info);
249: }
250:
251: void output_deleted(
252: HashStringValue::key_type aattribute,
253: HashStringValue::value_type ameaning,
254: Cookie_pass_info& cookie_info
255: ){
256: if(ameaning)
257: output_set_cookie_header(aattribute, ameaning, true, cookie_info);
258: }
259:
1.56 paf 260: void VCookie::output_result(SAPI_Info& sapi_info) {
1.74 misha 261: Cookie_pass_info cookie_info={&sapi_info, &fcharsets};
262:
263: after.for_each<Cookie_pass_info&>(output_after, cookie_info);
264: deleted.for_each<Cookie_pass_info&>(output_deleted, cookie_info);
265: }
266:
267: bool VCookie::should_refill(){
268: return !(
269: &fcharsets.source()==filled_source
270: && &fcharsets.client()==filled_client
271: );
272: }
273:
274: //#include <stdio.h>
275: void VCookie::refill(){
276: //request_info.cookie="test-session=value%3D5; test-default1=value%3D1; test-default2=value%3D2; test-tomorrow=value%3D3";
277: //request_info.cookie="enabled=yes; auth.uid=196325308053599810; enabled=yes; msnames; msuri"; // mdm
278: if(!frequest_info.cookie)
279: return;
280: /*
281: FILE *f=fopen("c:\\temp\\a", "wt");
282: fprintf(f, "cookie=%s", request_info.cookie);
283: fclose(f);
284: */
285: char *cookies=strdup(frequest_info.cookie);
286: char *current=cookies;
287: //_asm int 3;
288: do {
289: if(char *attribute=search_stop(current, '='))
290: if(char *meaning=search_stop(current, ';')) {
291: const String& sattribute=
1.83 misha 292: *new String(unescape_chars(attribute, strlen(attribute), &fcharsets.source(), true), String::L_TAINTED);
1.74 misha 293: const String& smeaning=
1.83 misha 294: *new String(unescape_chars(meaning, strlen(meaning), &fcharsets.source(), true), String::L_TAINTED);
1.74 misha 295: before.put(sattribute, new VString(smeaning));
296:
297: //if(sattribute == "test_js") throw Exception(0, 0, "'%s' '%s'", meaning, smeaning.cstr());
298: }
299: } while(current);
300:
301: filled_source=&fcharsets.source();
302: filled_client=&fcharsets.client();
1.1 paf 303: }
1.74 misha 304:
E-mail: