Annotation of parser3/src/types/pa_vcookie.C, revision 1.55.2.4
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.4! paf 8: static const char* IDENT_VCOOKIE_C="$Date: 2003/01/31 12:34:42 $";
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.55.2.4! paf 141: #ifndef DOXYGEN
! 142: struct Output_set_cookie_info {
! 143: Pool *pool;
! 144: SAPI_Info *sapi_info;
! 145: };
! 146: #endif
! 147:
1.25 parser 148: /*
1.49 paf 149: @todo
150: http://www.netscape.com/newsref/std/cookie_spec.html
1.25 parser 151: When sending cookies to a server,
152: all cookies with a more specific path mapping should be sent before cookies
153: with less specific path mappings.
154: For example, a cookie "name1=foo" with a path mapping of "/" should be sent after
155: a cookie "name1=foo2" with a path mapping of "/bar" if they are both to be sent.
156:
157: There are limitations on the number of cookies that a client can store at any one time.
158: This is a specification of the minimum number of cookies that a client should be prepared
159: to receive and store.
160: 300 total cookies
161: 4 kilobytes per cookie, where the name and the OPAQUE_STRING combine
162: to form the 4 kilobyte limit.
163: 20 cookies per server or domain. (note that completely specified hosts
164: and domains are treated as separate entities and have a 20 cookie limitation
165: for each, not combined)
166: */
1.55.2.1 paf 167: static void output_set_cookie(
168: HashStringValue::key_type aattribute,
169: HashStringValue::value_type ameaning,
1.55.2.4! paf 170: Output_set_cookie_info info) {
1.55.2.1 paf 171: String string;
1.1 paf 172: // attribute
1.55.2.1 paf 173: string.append(*aattribute, String::UL_HTTP_HEADER, true);
1.1 paf 174: // attribute=
1.19 paf 175: string << "=";
1.55.2.1 paf 176: ValuePtr lmeaning;
1.1 paf 177: // figure out 'meaning'
178: if(ameaning) { // assigning value
179: // Set-Cookie: (attribute)=(value); path=/
1.55.2.1 paf 180: lmeaning=ameaning;
181: if(HashStringValue *hash=lmeaning->get_hash(aattribute)) { // ...[hash value]
1.1 paf 182: // $expires
1.55.2.1 paf 183: if(ValuePtr expires=hash->get(expires_name)) {
1.55.2.2 paf 184: StringPtr string;
1.55.2.1 paf 185: if((string=expires->get_string(0)) && (*string==SESSION_NAME)) {
1.1 paf 186: // $expires[session]
1.55.2.1 paf 187: hash->remove(expires_name);
1.1 paf 188: } else {
189: // $expires(days)
1.41 paf 190: if(double days_till_expire=expires->as_double())
1.55.2.1 paf 191: hash->put(expires_name, expires_vdate(aattribute, days_till_expire));
1.41 paf 192: else // $expires(0)
1.55.2.1 paf 193: hash->remove(expires_name);
1.1 paf 194: }
195: } else // $expires not assigned, defaulting
1.55.2.1 paf 196: hash->put(expires_name, expires_vdate(aattribute, DEFAULT_EXPIRES_DAYS));
1.1 paf 197: } else { // ...[string value]
1.55.2.1 paf 198: ValuePtr wrap_meaning(new VHash);
199: HashStringValue& hash=*wrap_meaning->get_hash(aattribute);
200: // wrapping lmeaning into hash
201: hash.put(value_name, lmeaning);
1.1 paf 202: // string = $expires not assigned, defaulting
1.55.2.1 paf 203: hash.put(expires_name, expires_vdate(aattribute, DEFAULT_EXPIRES_DAYS));
204: // replacing lmeaning with hash-wrapped one
205: lmeaning=wrap_meaning;
1.1 paf 206: }
207: } else {// removing value
1.25 parser 208: /*
209: http://www.netscape.com/newsref/std/cookie_spec.html
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.55.2.1 paf 215: lmeaning=ValuePtr(new VHash);
216: lmeaning->get_hash(aattribute)->put(expires_name,
217: expires_vdate(aattribute, -DEFAULT_EXPIRES_DAYS));
1.1 paf 218: }
219: // defaulting path
1.55.2.1 paf 220: if(!lmeaning->get_hash(aattribute)->get(path_name))
221: lmeaning->get_hash(aattribute)->put(path_name,
222: ValuePtr(new VString(path_value_default)));
1.1 paf 223:
1.55.2.1 paf 224: // append lmeaning
1.55.2.4! paf 225: string << attributed_meaning_to_string(*info.pool, lmeaning, String::UL_HTTP_HEADER, true);
1.1 paf 226:
227: // output
1.55.2.4! paf 228: SAPI::add_header_attribute(*info.sapi_info, "set-cookie", string.cstr(String::UL_UNSPECIFIED));
1.1 paf 229: }
1.55.2.1 paf 230: static void output_after(
231: HashStringValue::key_type aattribute,
232: HashStringValue::value_type ameaning,
1.55.2.4! paf 233: Output_set_cookie_info *info) {
! 234: output_set_cookie(aattribute, ameaning, *info);
1.55.2.1 paf 235: }
236: static void output_deleted(
237: HashStringValue::key_type aattribute,
238: HashStringValue::value_type ameaning,
1.55.2.4! paf 239: Output_set_cookie_info* info) {
! 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: