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

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.18    ! paf         8:        $Id: pa_vcookie.C,v 1.17.2.1 2001/04/16 11:26:57 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.15      paf        21: /// @test change urlencode here and in untaint.C to HTTP standard's " and \" mech
1.1       paf        22: 
                     23: // VCookie
                     24: 
                     25: Value *VCookie::get_element(const String& aname) {
                     26:        // $cookie
                     27:        if(deleted.get(aname)) // deleted?
                     28:                return 0;
                     29:        
1.2       paf        30:        if(Value *after_meaning=static_cast<Value *>(after.get(aname))) // assigned 'after'?
                     31:                if(Hash *hash=after_meaning->get_hash())
                     32:                        return static_cast<Value *>(hash->get(*value_name));
                     33:                else
                     34:                        return after_meaning;
1.1       paf        35:        
                     36:        // neither deleted nor assigned 
                     37:        // return any value it had 'before'
                     38:        return static_cast<Value *>(before.get(aname));
                     39: }
                     40: 
                     41: void VCookie::put_element(const String& aname, Value *avalue) {
                     42:        // $cookie
                     43:        bool remove;
                     44:        if(Hash *hash=avalue->get_hash())
                     45:                remove=hash->size()==0;
                     46:        else
                     47:                remove=avalue->as_string().size()==0;
                     48: 
                     49:        (remove?deleted:after).put(aname, avalue);
                     50:        (remove?after:deleted).put(aname, 0);
                     51: }
                     52: 
1.6       paf        53: //#include <stdio.h>
1.1       paf        54: void VCookie::fill_fields(Request& request) {
                     55: //     request.info.cookie="test-session=value%3D5; test-default1=value%3D1; test-default2=value%3D2; test-tomorrow=value%3D3";
                     56:        if(!request.info.cookie)
                     57:                return;
                     58: /*
                     59:        FILE *f=fopen("c:\\temp\\a", "wt");
                     60:        fprintf(f, "cookie=%s", request.info.cookie);
                     61:        fclose(f);*/
                     62:        char *cookies=(char *)malloc(strlen(request.info.cookie)+1);
                     63:        strcpy(cookies, request.info.cookie);
                     64:     char *current=cookies;
                     65:     do {
1.8       paf        66:                char *meaning=lsplit(&current,';');
                     67:                char *attribute=lsplit(&meaning, '=');
                     68:                if(meaning) {
                     69:                        while(*attribute==' ')
                     70:                                attribute++;
                     71:                        rsplit(meaning,' ');
                     72:                        String& smeaning=*NEW String(pool(), 
1.17      paf        73:                                unescape_chars(pool(), meaning, strlen(meaning)), 0, true);
1.8       paf        74:                        String& sattribute=*NEW String(pool(), 
1.17      paf        75:                                unescape_chars(pool(), attribute, strlen(attribute)), 0, true);
1.8       paf        76:                        before.put(sattribute, NEW VString(smeaning));
1.1       paf        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
1.16      paf       101:        string.append(aattribute, String::UL_HTTP_HEADER, true);
1.1       paf       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, 
1.14      paf       119:                                                expires_timestamp(pool, expires->as_double()));
1.1       paf       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
1.16      paf       143:        string.append(attributed_meaning_to_string(*meaning, String::UL_HTTP_HEADER), 
                    144:                String::UL_PASS_APPENDED);
1.1       paf       145: 
                    146:        // output
1.11      paf       147:        SAPI::add_header_attribute(pool,
1.1       paf       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() {
1.12      paf       158:        after.for_each(output_after, this);
                    159:        deleted.for_each(output_deleted, this);
1.1       paf       160: }

E-mail: