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

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

E-mail: