Annotation of parser3/src/types/pa_vcookie.C, revision 1.4

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.4     ! paf         6:        $Id: pa_vcookie.C,v 1.3 2001/03/19 16:06:16 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:        // $CLASS,$BASE,$method
                     24:        if(Value *result=VStateless_class::get_element(aname))
                     25:                return result;
                     26:        
                     27:        // $cookie
                     28: 
                     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: 
                     55: #include <stdio.h>
                     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 {
                     68:                char *value=lsplit(&current,';');
                     69:                char *fname=lsplit(&value, '=');
                     70:                if(value) {
                     71:                        while(*fname==' ')
                     72:                                fname++;
                     73:                        rsplit(value,' ');
                     74:                        String& string=*NEW String(pool(), 
                     75:                                unescape_chars(pool(), value, strlen(value)), true);
                     76:                        before.put(String(pool(), fname), NEW VString(string));
                     77:                }
                     78:        } while(current);
                     79: }
                     80: 
                     81: static VString *expires_timestamp(Pool& pool, double days_till_expire) {
                     82:     const char month_names[12][4]={
                     83:                "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
                     84:     const char days[7][4]={
                     85:                "Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
                     86:        
                     87:        time_t when=time(NULL)+(time_t)(60*60*24*days_till_expire);
                     88:        struct tm *tms=gmtime(&when);
                     89:        char *buf=(char *)pool.malloc(MAX_STRING);
                     90:        snprintf(buf, MAX_STRING, "%s, %.2d-%s-%.4d %.2d:%.2d:%.2d GMT", 
                     91:                days[tms->tm_wday],
                     92:                tms->tm_mday,month_names[tms->tm_mon],tms->tm_year+1900,
                     93:                tms->tm_hour,tms->tm_min,tms->tm_sec);
                     94:        return new(pool) VString(*new(pool) String(pool, buf));
                     95: }
                     96: 
1.4     ! paf        97: static void output_set_cookie(const Hash::Key& aattribute, Hash::Val *ameaning) {
1.1       paf        98:        Pool& pool=aattribute.pool();
                     99:        String string(pool);
                    100:        // attribute
                    101:        string.append(aattribute, String::Untaint_lang::HEADER, true);
                    102:        // attribute=
                    103:        string.APPEND_CONST("=");
                    104:        Value *meaning;
                    105:        // figure out 'meaning'
                    106:        if(ameaning) { // assigning value
                    107:                // Set-Cookie: (attribute)=(value); path=/
                    108:                meaning=static_cast<Value *>(ameaning);
                    109:                if(Hash *hash=meaning->get_hash()) { // ...[hash value]
                    110:                        // $expires
                    111:                        if(Value *expires=static_cast<Value *>(hash->get(*expires_name))) {
                    112:                                const String *string;
                    113:                                if((string=expires->get_string()) && (*string==SESSION_NAME))  {
                    114:                                        // $expires[session]
                    115:                                        hash->put(*expires_name, 0);
                    116:                                } else {
                    117:                                        // $expires(days)
                    118:                                        hash->put(*expires_name, 
                    119:                                                expires_timestamp(pool, expires->get_double()));
                    120:                                }
                    121:                        } else // $expires not assigned, defaulting
                    122:                                hash->put(*expires_name, expires_timestamp(pool, DEFAULT_EXPIRES_DAYS));
                    123:                } else { // ...[string value]
                    124:                        Value *wrap_meaning=new(pool) VHash(pool);
                    125:                        // wrapping meaning into hash
                    126:                        wrap_meaning->get_hash()->put(*value_name, meaning);
                    127:                        // string = $expires not assigned, defaulting
                    128:                        wrap_meaning->get_hash()->put(*expires_name, 
                    129:                                expires_timestamp(pool, DEFAULT_EXPIRES_DAYS));
                    130:                        // replacing meaning with hash-wrapped one
                    131:                        meaning=wrap_meaning;
                    132:                }
                    133:        } else {// removing value
                    134:                // Set-Cookie: (attribute)=; path=/
                    135:                meaning=new(pool) VHash(pool);
                    136:        }
                    137:        // defaulting path
                    138:        if(!meaning->get_hash()->get(*path_name))
                    139:                meaning->get_hash()->put(*path_name, 
                    140:                        new(pool) VString(*new(pool) String(pool, "/")));
                    141: 
                    142:        // append meaning
                    143:        string.append(attributed_meaning_string(meaning), 
                    144:        String::Untaint_lang::PASS_APPENDED);
                    145: 
                    146:        // output
                    147:        (*service_funcs.output_header_attribute)(
                    148:                "set-cookie", 
                    149:                string.cstr());
                    150: }
1.4     ! paf       151: static void output_after(const Hash::Key& aattribute, Hash::Val *ameaning, void *) {
1.1       paf       152:        output_set_cookie(aattribute, ameaning);
                    153: }
1.4     ! paf       154: static void output_deleted(const Hash::Key& aattribute, Hash::Val *ameaning, void *) {
1.1       paf       155:        output_set_cookie(aattribute, 0);
                    156: }
                    157: void VCookie::output_result() {
                    158:        after.foreach(output_after, this);
                    159:        deleted.foreach(output_deleted, this);
                    160: }

E-mail: