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