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