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

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

E-mail: