Annotation of parser3/src/types/pa_vcookie.C, revision 1.5
1.1 paf 1: /*
2: Parser
3: Copyright(c) 2001 ArtLebedev Group(http://www.artlebedev.com)
4: Author: Alexander Petrosyan <paf@design.ru>(http://design.ru/paf)
5:
1.5 ! paf 6: $Id: pa_vcookie.C,v 1.4 2001/03/19 17:56:29 paf Exp $
1.1 paf 7: */
8:
9: #include <string.h>
10: #include <time.h>
11:
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
19:
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:
50: #include <stdio.h>
51: void VCookie::fill_fields(Request& request) {
52: // request.info.cookie="test-session=value%3D5; test-default1=value%3D1; test-default2=value%3D2; test-tomorrow=value%3D3";
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;
62: do {
63: char *value=lsplit(¤t,';');
64: char *fname=lsplit(&value, '=');
65: if(value) {
66: while(*fname==' ')
67: fname++;
68: rsplit(value,' ');
69: String& string=*NEW String(pool(),
70: unescape_chars(pool(), value, strlen(value)), true);
71: before.put(String(pool(), fname), NEW VString(string));
72: }
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
96: string.append(aattribute, String::Untaint_lang::HEADER, true);
97: // attribute=
98: string.APPEND_CONST("=");
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,
114: expires_timestamp(pool, expires->get_double()));
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
138: string.append(attributed_meaning_string(meaning),
139: String::Untaint_lang::PASS_APPENDED);
140:
141: // output
142: (*service_funcs.output_header_attribute)(
143: "set-cookie",
144: string.cstr());
145: }
1.4 paf 146: static void output_after(const Hash::Key& aattribute, Hash::Val *ameaning, void *) {
1.1 paf 147: output_set_cookie(aattribute, ameaning);
148: }
1.4 paf 149: static void output_deleted(const Hash::Key& aattribute, Hash::Val *ameaning, void *) {
1.1 paf 150: output_set_cookie(aattribute, 0);
151: }
152: void VCookie::output_result() {
153: after.foreach(output_after, this);
154: deleted.foreach(output_deleted, this);
155: }
E-mail: