Annotation of parser3/src/types/pa_vcookie.C, revision 1.24
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.24 ! parser 7: $Id: $
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.6 paf 49: //#include <stdio.h>
1.1 paf 50: void VCookie::fill_fields(Request& request) {
1.19 paf 51: //request.info.cookie="test-session=value%3D5; test-default1=value%3D1; test-default2=value%3D2; test-tomorrow=value%3D3";
1.20 paf 52: //request.info.cookie="\"вот\"=\"прислал \\\"browser\\\"\"";
1.1 paf 53: if(!request.info.cookie)
54: return;
55: /*
56: FILE *f=fopen("c:\\temp\\a", "wt");
57: fprintf(f, "cookie=%s", request.info.cookie);
58: fclose(f);*/
59: char *cookies=(char *)malloc(strlen(request.info.cookie)+1);
60: strcpy(cookies, request.info.cookie);
61: char *current=cookies;
1.22 parser 62: uint line=0;
1.1 paf 63: do {
1.19 paf 64: if(char *attribute=unquote(current, '='))
65: if(char *meaning=unquote(current, ';')) {
1.22 parser 66: String& sattribute=*NEW String(pool());
67: String& smeaning=*NEW String(pool());
68: sattribute.APPEND_TAINTED(attribute, 0, "cookie_name", line);
69: smeaning.APPEND_TAINTED(meaning, 0, "cookie_value", line);
1.19 paf 70: before.put(sattribute, NEW VString(smeaning));
1.22 parser 71: line++;
1.19 paf 72: }
1.1 paf 73: } while(current);
74: }
75:
76: static VString *expires_timestamp(Pool& pool, double days_till_expire) {
77: const char month_names[12][4]={
78: "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
79: const char days[7][4]={
80: "Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
81:
82: time_t when=time(NULL)+(time_t)(60*60*24*days_till_expire);
83: struct tm *tms=gmtime(&when);
84: char *buf=(char *)pool.malloc(MAX_STRING);
85: snprintf(buf, MAX_STRING, "%s, %.2d-%s-%.4d %.2d:%.2d:%.2d GMT",
86: days[tms->tm_wday],
87: tms->tm_mday,month_names[tms->tm_mon],tms->tm_year+1900,
88: tms->tm_hour,tms->tm_min,tms->tm_sec);
89: return new(pool) VString(*new(pool) String(pool, buf));
90: }
91:
1.4 paf 92: static void output_set_cookie(const Hash::Key& aattribute, Hash::Val *ameaning) {
1.1 paf 93: Pool& pool=aattribute.pool();
94: String string(pool);
95: // attribute
1.16 paf 96: string.append(aattribute, String::UL_HTTP_HEADER, true);
1.1 paf 97: // attribute=
1.19 paf 98: string << "=";
1.1 paf 99: Value *meaning;
100: // figure out 'meaning'
101: if(ameaning) { // assigning value
102: // Set-Cookie: (attribute)=(value); path=/
103: meaning=static_cast<Value *>(ameaning);
104: if(Hash *hash=meaning->get_hash()) { // ...[hash value]
105: // $expires
106: if(Value *expires=static_cast<Value *>(hash->get(*expires_name))) {
107: const String *string;
108: if((string=expires->get_string()) && (*string==SESSION_NAME)) {
109: // $expires[session]
110: hash->put(*expires_name, 0);
111: } else {
112: // $expires(days)
113: hash->put(*expires_name,
1.14 paf 114: expires_timestamp(pool, expires->as_double()));
1.1 paf 115: }
116: } else // $expires not assigned, defaulting
117: hash->put(*expires_name, expires_timestamp(pool, DEFAULT_EXPIRES_DAYS));
118: } else { // ...[string value]
119: Value *wrap_meaning=new(pool) VHash(pool);
120: // wrapping meaning into hash
121: wrap_meaning->get_hash()->put(*value_name, meaning);
122: // string = $expires not assigned, defaulting
123: wrap_meaning->get_hash()->put(*expires_name,
124: expires_timestamp(pool, DEFAULT_EXPIRES_DAYS));
125: // replacing meaning with hash-wrapped one
126: meaning=wrap_meaning;
127: }
128: } else {// removing value
129: // Set-Cookie: (attribute)=; path=/
130: meaning=new(pool) VHash(pool);
131: }
132: // defaulting path
133: if(!meaning->get_hash()->get(*path_name))
134: meaning->get_hash()->put(*path_name,
135: new(pool) VString(*new(pool) String(pool, "/")));
136:
137: // append meaning
1.19 paf 138: string << attributed_meaning_to_string(*meaning, String::UL_HTTP_HEADER);
1.1 paf 139:
140: // output
1.19 paf 141: SAPI::add_header_attribute(pool, "set-cookie", string.cstr());
1.1 paf 142: }
1.4 paf 143: static void output_after(const Hash::Key& aattribute, Hash::Val *ameaning, void *) {
1.1 paf 144: output_set_cookie(aattribute, ameaning);
145: }
1.4 paf 146: static void output_deleted(const Hash::Key& aattribute, Hash::Val *ameaning, void *) {
1.1 paf 147: output_set_cookie(aattribute, 0);
148: }
149: void VCookie::output_result() {
1.12 paf 150: after.for_each(output_after, this);
151: deleted.for_each(output_deleted, this);
1.1 paf 152: }
E-mail: