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

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

E-mail: