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