Annotation of parser3/src/types/pa_vcookie.C, revision 1.21
1.6 paf 1: /** @file
2: Parser: cookie class.
3:
1.1 paf 4: Copyright(c) 2001 ArtLebedev Group(http://www.artlebedev.com)
1.6 paf 5:
1.1 paf 6: Author: Alexander Petrosyan <paf@design.ru>(http://design.ru/paf)
7:
1.21 ! parser 8: $Id: pa_vcookie.C,v 1.20 2001/04/19 16:28:18 paf Exp $
1.1 paf 9: */
10:
1.11 paf 11: #include "pa_sapi.h"
1.3 paf 12: #include "pa_common.h"
1.1 paf 13: #include "pa_vcookie.h"
14: #include "pa_vstring.h"
15: #include "pa_request.h"
16:
17: #define SESSION_NAME "session"
18: #define DEFAULT_EXPIRES_DAYS 90
1.9 paf 19:
1.1 paf 20: // VCookie
21:
22: Value *VCookie::get_element(const String& aname) {
23: // $cookie
24: if(deleted.get(aname)) // deleted?
25: return 0;
26:
1.2 paf 27: if(Value *after_meaning=static_cast<Value *>(after.get(aname))) // assigned 'after'?
28: if(Hash *hash=after_meaning->get_hash())
29: return static_cast<Value *>(hash->get(*value_name));
30: else
31: return after_meaning;
1.1 paf 32:
33: // neither deleted nor assigned
34: // return any value it had 'before'
35: return static_cast<Value *>(before.get(aname));
36: }
37:
38: void VCookie::put_element(const String& aname, Value *avalue) {
39: // $cookie
40: bool remove;
41: if(Hash *hash=avalue->get_hash())
42: remove=hash->size()==0;
43: else
44: remove=avalue->as_string().size()==0;
45:
46: (remove?deleted:after).put(aname, avalue);
47: (remove?after:deleted).put(aname, 0);
48: }
49:
1.6 paf 50: //#include <stdio.h>
1.1 paf 51: void VCookie::fill_fields(Request& request) {
1.19 paf 52: //request.info.cookie="test-session=value%3D5; test-default1=value%3D1; test-default2=value%3D2; test-tomorrow=value%3D3";
1.20 paf 53: //request.info.cookie="\"вот\"=\"прислал \\\"browser\\\"\"";
1.1 paf 54: if(!request.info.cookie)
55: return;
56: /*
57: FILE *f=fopen("c:\\temp\\a", "wt");
58: fprintf(f, "cookie=%s", request.info.cookie);
59: fclose(f);*/
60: char *cookies=(char *)malloc(strlen(request.info.cookie)+1);
61: strcpy(cookies, request.info.cookie);
62: char *current=cookies;
63: do {
1.19 paf 64: if(char *attribute=unquote(current, '='))
65: if(char *meaning=unquote(current, ';')) {
66: String& sattribute=*NEW String(pool(), attribute, 0, true);
67: String& smeaning=*NEW String(pool(), meaning, 0, true);
68: before.put(sattribute, NEW VString(smeaning));
69: }
1.1 paf 70: } while(current);
71: }
72:
73: static VString *expires_timestamp(Pool& pool, double days_till_expire) {
74: const char month_names[12][4]={
75: "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
76: const char days[7][4]={
77: "Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
78:
79: time_t when=time(NULL)+(time_t)(60*60*24*days_till_expire);
80: struct tm *tms=gmtime(&when);
81: char *buf=(char *)pool.malloc(MAX_STRING);
82: snprintf(buf, MAX_STRING, "%s, %.2d-%s-%.4d %.2d:%.2d:%.2d GMT",
83: days[tms->tm_wday],
84: tms->tm_mday,month_names[tms->tm_mon],tms->tm_year+1900,
85: tms->tm_hour,tms->tm_min,tms->tm_sec);
86: return new(pool) VString(*new(pool) String(pool, buf));
87: }
88:
1.4 paf 89: static void output_set_cookie(const Hash::Key& aattribute, Hash::Val *ameaning) {
1.1 paf 90: Pool& pool=aattribute.pool();
91: String string(pool);
92: // attribute
1.16 paf 93: string.append(aattribute, String::UL_HTTP_HEADER, true);
1.1 paf 94: // attribute=
1.19 paf 95: string << "=";
1.1 paf 96: Value *meaning;
97: // figure out 'meaning'
98: if(ameaning) { // assigning value
99: // Set-Cookie: (attribute)=(value); path=/
100: meaning=static_cast<Value *>(ameaning);
101: if(Hash *hash=meaning->get_hash()) { // ...[hash value]
102: // $expires
103: if(Value *expires=static_cast<Value *>(hash->get(*expires_name))) {
104: const String *string;
105: if((string=expires->get_string()) && (*string==SESSION_NAME)) {
106: // $expires[session]
107: hash->put(*expires_name, 0);
108: } else {
109: // $expires(days)
110: hash->put(*expires_name,
1.14 paf 111: expires_timestamp(pool, expires->as_double()));
1.1 paf 112: }
113: } else // $expires not assigned, defaulting
114: hash->put(*expires_name, expires_timestamp(pool, DEFAULT_EXPIRES_DAYS));
115: } else { // ...[string value]
116: Value *wrap_meaning=new(pool) VHash(pool);
117: // wrapping meaning into hash
118: wrap_meaning->get_hash()->put(*value_name, meaning);
119: // string = $expires not assigned, defaulting
120: wrap_meaning->get_hash()->put(*expires_name,
121: expires_timestamp(pool, DEFAULT_EXPIRES_DAYS));
122: // replacing meaning with hash-wrapped one
123: meaning=wrap_meaning;
124: }
125: } else {// removing value
126: // Set-Cookie: (attribute)=; path=/
127: meaning=new(pool) VHash(pool);
128: }
129: // defaulting path
130: if(!meaning->get_hash()->get(*path_name))
131: meaning->get_hash()->put(*path_name,
132: new(pool) VString(*new(pool) String(pool, "/")));
133:
134: // append meaning
1.19 paf 135: string << attributed_meaning_to_string(*meaning, String::UL_HTTP_HEADER);
1.1 paf 136:
137: // output
1.19 paf 138: SAPI::add_header_attribute(pool, "set-cookie", string.cstr());
1.1 paf 139: }
1.4 paf 140: static void output_after(const Hash::Key& aattribute, Hash::Val *ameaning, void *) {
1.1 paf 141: output_set_cookie(aattribute, ameaning);
142: }
1.4 paf 143: static void output_deleted(const Hash::Key& aattribute, Hash::Val *ameaning, void *) {
1.1 paf 144: output_set_cookie(aattribute, 0);
145: }
146: void VCookie::output_result() {
1.12 paf 147: after.for_each(output_after, this);
148: deleted.for_each(output_deleted, this);
1.1 paf 149: }
E-mail: