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