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