Annotation of parser3/src/types/pa_vcookie.C, revision 1.27
1.6 paf 1: /** @file
2: Parser: cookie class.
3:
1.1 paf 4: Copyright(c) 2001 ArtLebedev Group(http://www.artlebedev.com)
1.24 parser 5: Author: Alexander Petrosyan <paf@design.ru>(http://design.ru/paf)
1.6 paf 6:
1.27 ! parser 7: $Id: pa_vcookie.C,v 1.26 2001/10/08 15:14:07 parser Exp $
1.1 paf 8: */
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"
14: #include "pa_request.h"
15:
16: #define SESSION_NAME "session"
17: #define DEFAULT_EXPIRES_DAYS 90
1.9 paf 18:
1.1 paf 19: // VCookie
20:
21: Value *VCookie::get_element(const String& aname) {
22: // $cookie
23: if(deleted.get(aname)) // deleted?
24: return 0;
25:
1.2 paf 26: if(Value *after_meaning=static_cast<Value *>(after.get(aname))) // assigned 'after'?
27: if(Hash *hash=after_meaning->get_hash())
28: return static_cast<Value *>(hash->get(*value_name));
29: else
30: return after_meaning;
1.1 paf 31:
32: // neither deleted nor assigned
33: // return any value it had 'before'
34: return static_cast<Value *>(before.get(aname));
35: }
36:
37: void VCookie::put_element(const String& aname, Value *avalue) {
38: // $cookie
39: bool remove;
40: if(Hash *hash=avalue->get_hash())
41: remove=hash->size()==0;
42: else
43: remove=avalue->as_string().size()==0;
44:
45: (remove?deleted:after).put(aname, avalue);
46: (remove?after:deleted).put(aname, 0);
47: }
48:
1.26 parser 49: static char *search_stop(char*& current, char cstop_at) {
50: // skip leading WS
51: while(*current==' ' || *current=='\t')
52: current++;
53: if(!*current)
54: return current=0;
55:
56: char *result=current;
57: if(char *pstop_at=strchr(current, cstop_at)) {
58: *pstop_at=0;
59: current=pstop_at+1;
60: } else
61: current=0;
62: return result;
63: }
64:
65:
1.6 paf 66: //#include <stdio.h>
1.1 paf 67: void VCookie::fill_fields(Request& request) {
1.19 paf 68: //request.info.cookie="test-session=value%3D5; test-default1=value%3D1; test-default2=value%3D2; test-tomorrow=value%3D3";
1.1 paf 69: if(!request.info.cookie)
70: return;
71: /*
72: FILE *f=fopen("c:\\temp\\a", "wt");
73: fprintf(f, "cookie=%s", request.info.cookie);
74: fclose(f);*/
75: char *cookies=(char *)malloc(strlen(request.info.cookie)+1);
76: strcpy(cookies, request.info.cookie);
77: char *current=cookies;
1.22 parser 78: uint line=0;
1.26 parser 79: //_asm int 3;
1.1 paf 80: do {
1.26 parser 81: if(char *attribute=search_stop(current, '='))
82: if(char *meaning=search_stop(current, ';')) {
1.22 parser 83: String& sattribute=*NEW String(pool());
84: String& smeaning=*NEW String(pool());
1.26 parser 85: sattribute.APPEND_TAINTED(unescape_chars(pool(), attribute, strlen(attribute)), 0,
86: "cookie_name", line);
87: smeaning.APPEND_TAINTED(unescape_chars(pool(), meaning, strlen(meaning)), 0,
88: "cookie_value", line);
1.19 paf 89: before.put(sattribute, NEW VString(smeaning));
1.22 parser 90: line++;
1.19 paf 91: }
1.1 paf 92: } while(current);
93: }
94:
95: static VString *expires_timestamp(Pool& pool, double days_till_expire) {
96: const char month_names[12][4]={
97: "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
98: const char days[7][4]={
99: "Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
100:
101: time_t when=time(NULL)+(time_t)(60*60*24*days_till_expire);
102: struct tm *tms=gmtime(&when);
103: char *buf=(char *)pool.malloc(MAX_STRING);
104: snprintf(buf, MAX_STRING, "%s, %.2d-%s-%.4d %.2d:%.2d:%.2d GMT",
105: days[tms->tm_wday],
106: tms->tm_mday,month_names[tms->tm_mon],tms->tm_year+1900,
107: tms->tm_hour,tms->tm_min,tms->tm_sec);
108: return new(pool) VString(*new(pool) String(pool, buf));
109: }
110:
1.25 parser 111: /*
1.27 ! parser 112: @todo http://www.netscape.com/newsref/std/cookie_spec.html
1.25 parser 113: When sending cookies to a server,
114: all cookies with a more specific path mapping should be sent before cookies
115: with less specific path mappings.
116: For example, a cookie "name1=foo" with a path mapping of "/" should be sent after
117: a cookie "name1=foo2" with a path mapping of "/bar" if they are both to be sent.
118:
119: There are limitations on the number of cookies that a client can store at any one time.
120: This is a specification of the minimum number of cookies that a client should be prepared
121: to receive and store.
122: 300 total cookies
123: 4 kilobytes per cookie, where the name and the OPAQUE_STRING combine
124: to form the 4 kilobyte limit.
125: 20 cookies per server or domain. (note that completely specified hosts
126: and domains are treated as separate entities and have a 20 cookie limitation
127: for each, not combined)
128: */
1.4 paf 129: static void output_set_cookie(const Hash::Key& aattribute, Hash::Val *ameaning) {
1.1 paf 130: Pool& pool=aattribute.pool();
131: String string(pool);
132: // attribute
1.16 paf 133: string.append(aattribute, String::UL_HTTP_HEADER, true);
1.1 paf 134: // attribute=
1.19 paf 135: string << "=";
1.1 paf 136: Value *meaning;
137: // figure out 'meaning'
138: if(ameaning) { // assigning value
139: // Set-Cookie: (attribute)=(value); path=/
140: meaning=static_cast<Value *>(ameaning);
141: if(Hash *hash=meaning->get_hash()) { // ...[hash value]
142: // $expires
143: if(Value *expires=static_cast<Value *>(hash->get(*expires_name))) {
144: const String *string;
145: if((string=expires->get_string()) && (*string==SESSION_NAME)) {
146: // $expires[session]
147: hash->put(*expires_name, 0);
148: } else {
149: // $expires(days)
150: hash->put(*expires_name,
1.14 paf 151: expires_timestamp(pool, expires->as_double()));
1.1 paf 152: }
153: } else // $expires not assigned, defaulting
154: hash->put(*expires_name, expires_timestamp(pool, DEFAULT_EXPIRES_DAYS));
155: } else { // ...[string value]
156: Value *wrap_meaning=new(pool) VHash(pool);
157: // wrapping meaning into hash
158: wrap_meaning->get_hash()->put(*value_name, meaning);
159: // string = $expires not assigned, defaulting
160: wrap_meaning->get_hash()->put(*expires_name,
161: expires_timestamp(pool, DEFAULT_EXPIRES_DAYS));
162: // replacing meaning with hash-wrapped one
163: meaning=wrap_meaning;
164: }
165: } else {// removing value
1.25 parser 166: /*
167: http://www.netscape.com/newsref/std/cookie_spec.html
168: to delete a cookie, it can do so by returning a cookie with the same name,
169: and an expires time which is in the past
170: */
171:
1.1 paf 172: // Set-Cookie: (attribute)=; path=/
173: meaning=new(pool) VHash(pool);
1.26 parser 174: meaning->get_hash()->put(*expires_name,
1.25 parser 175: expires_timestamp(pool, -DEFAULT_EXPIRES_DAYS));
1.1 paf 176: }
177: // defaulting path
178: if(!meaning->get_hash()->get(*path_name))
179: meaning->get_hash()->put(*path_name,
180: new(pool) VString(*new(pool) String(pool, "/")));
181:
182: // append meaning
1.19 paf 183: string << attributed_meaning_to_string(*meaning, String::UL_HTTP_HEADER);
1.1 paf 184:
185: // output
1.19 paf 186: SAPI::add_header_attribute(pool, "set-cookie", string.cstr());
1.1 paf 187: }
1.4 paf 188: static void output_after(const Hash::Key& aattribute, Hash::Val *ameaning, void *) {
1.1 paf 189: output_set_cookie(aattribute, ameaning);
190: }
1.4 paf 191: static void output_deleted(const Hash::Key& aattribute, Hash::Val *ameaning, void *) {
1.1 paf 192: output_set_cookie(aattribute, 0);
193: }
194: void VCookie::output_result() {
1.12 paf 195: after.for_each(output_after, this);
196: deleted.for_each(output_deleted, this);
1.1 paf 197: }
E-mail: