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

1.6       paf         1: /** @file
                      2:        Parser: cookie class.
                      3: 
1.76      misha       4:        Copyright(c) 2001-2009 ArtLebedev Group (http://www.artlebedev.com)
1.34      paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.44      paf         6: */
1.6       paf         7: 
1.85    ! misha       8: static const char * const IDENT_VCOOKIE_C="$Date: 2010-10-12 01:31:17 $";
1.1       paf         9: 
1.11      paf        10: #include "pa_sapi.h"
1.3       paf        11: #include "pa_common.h"
1.1       paf        12: #include "pa_vcookie.h"
                     13: #include "pa_vstring.h"
1.49      paf        14: #include "pa_vdate.h"
1.56      paf        15: #include "pa_vhash.h"
1.74      misha      16: #include "pa_request.h"
1.56      paf        17: 
                     18: // defines
                     19: 
                     20: #define PATH_NAME "path"
                     21: #define PATH_VALUE_DEFAULT "/"
1.1       paf        22: 
                     23: #define SESSION_NAME "session"
                     24: #define DEFAULT_EXPIRES_DAYS 90
1.9       paf        25: 
1.72      misha      26: #define COOKIE_FIELDS_ELEMENT_NAME "fields"
                     27: 
1.56      paf        28: // statics
                     29: 
                     30: static const String path_name(PATH_NAME);
                     31: static const String path_value_default(PATH_VALUE_DEFAULT);
                     32: 
1.1       paf        33: // VCookie
                     34: 
1.74      misha      35: VCookie::VCookie(Request_charsets& acharsets, Request_info& arequest_info):
                     36:        fcharsets(acharsets),
                     37:        frequest_info(arequest_info) {
                     38: }
                     39: 
1.80      misha      40: Value* VCookie::get_element(const String& aname) {
1.28      parser     41:        // $CLASS
1.46      paf        42:        if(aname==CLASS_NAME)
1.28      parser     43:                return this;
                     44: 
1.76      misha      45:        // $CLASS_NAME
                     46:        if(aname==CLASS_NAMETEXT)
                     47:                return new VString(cookie_class_name);
                     48: 
1.74      misha      49:        // $fields
1.72      misha      50:        if(aname==COOKIE_FIELDS_ELEMENT_NAME){
1.74      misha      51:                if(should_refill())
                     52:                        refill();
                     53: 
                     54:                HashStringValue *result=new HashStringValue(before);
1.72      misha      55:                after.for_each<HashStringValue*>(copy_all_overwrite_to, result);
                     56:                deleted.for_each<HashStringValue*>(remove_key_from, result);
                     57:                return new VHash(*result);
                     58:        }
                     59: 
1.1       paf        60:        // $cookie
1.46      paf        61:        if(deleted.get(aname)) // deleted?
1.1       paf        62:                return 0;
                     63:        
1.56      paf        64:        if(Value* after_meaning=after.get(aname)) // assigned 'after'?
                     65:                if(HashStringValue *hash=after_meaning->get_hash())
                     66:                        return hash->get(value_name);
1.2       paf        67:                else
                     68:                        return after_meaning;
1.1       paf        69:        
1.74      misha      70:        if(should_refill())
                     71:                refill();
                     72: 
1.1       paf        73:        // neither deleted nor assigned 
                     74:        // return any value it had 'before'
1.56      paf        75:        return before.get(aname);
1.1       paf        76: }
                     77: 
1.84      misha      78: time_t expires_sec(double days_till_expire) {
                     79:        time_t result=time(NULL)+(time_t)(60*60*24*days_till_expire);
                     80:        struct tm* tms=gmtime(&result);
                     81:        if(!tms)
                     82:                throw Exception(DATE_RANGE_EXCEPTION_TYPE,
                     83:                        0,
                     84:                        "bad expires time (seconds from epoch=%u)", result);
                     85:        return result;
                     86: }
                     87: 
1.80      misha      88: const VJunction* VCookie::put_element(const String& aname, Value* avalue, bool /*replace*/) {
1.1       paf        89:        // $cookie
1.56      paf        90:        Value* lvalue;
1.84      misha      91:        if(HashStringValue *hash=avalue->get_hash()) {
1.85    ! misha      92:                if(Value* expires=hash->get(expires_name)){
        !            93:                        const String* string;
        !            94:                        if(!(expires->is_string() && (string=expires->get_string()) && (*string==SESSION_NAME)))
        !            95:                                if(double days_till_expire=expires->as_double())
        !            96:                                        expires_sec(days_till_expire);
        !            97:                }
1.56      paf        98:                lvalue=hash->get(value_name);
1.84      misha      99:        } else
1.50      paf       100:                lvalue=avalue;
1.51      paf       101: 
                    102:        if(lvalue && lvalue->is_string()) {
                    103:                // taint string being assigned
1.56      paf       104:                String& tainted=*new String;
                    105:                tainted.append(*lvalue->get_string(), String::L_TAINTED, true /*forced*/);
                    106:                lvalue=new VString(tainted);
1.51      paf       107:        }
1.1       paf       108: 
1.71      misha     109:        if( !lvalue || lvalue->as_string().is_empty() ) {
                    110:                deleted.put(aname, avalue);
                    111:                after.put(aname, 0);
                    112:        } else {
                    113:                after.put(aname, avalue);
                    114:                deleted.put(aname, 0);
                    115:        }
1.64      paf       116:        return PUT_ELEMENT_REPLACED_ELEMENT;
1.1       paf       117: }
                    118: 
1.26      parser    119: static char *search_stop(char*& current, char cstop_at) {
1.37      paf       120:        // sanity check
                    121:        if(!current)
                    122:                return 0;
                    123: 
1.26      parser    124:        // skip leading WS
                    125:        while(*current==' ' || *current=='\t')
                    126:                current++;
                    127:        if(!*current)
                    128:                return current=0;
                    129: 
                    130:        char *result=current;
                    131:        if(char *pstop_at=strchr(current, cstop_at)) {
                    132:                *pstop_at=0;
                    133:                current=pstop_at+1;
                    134:        } else
                    135:                current=0;
                    136:        return result;
                    137: }
                    138: 
                    139: 
1.56      paf       140: static Value& expires_vdate(double days_till_expire) {
1.84      misha     141:        return *new VDate(expires_sec(days_till_expire));
1.1       paf       142: }
                    143: 
1.25      parser    144: /*
1.49      paf       145:        @todo 
1.73      misha     146:        http://wp.netscape.com/newsref/std/cookie_spec.html
1.25      parser    147:        When sending cookies to a server, 
                    148:        all cookies with a more specific path mapping should be sent before cookies 
                    149:        with less specific path mappings. 
                    150:        For example, a cookie "name1=foo" with a path mapping of "/" should be sent after 
                    151:        a cookie "name1=foo2" with a path mapping of "/bar" if they are both to be sent. 
                    152: 
1.74      misha     153:        There are limitations on the number of cookies that a client can store at any one time. 
                    154:        This is a specification of the minimum number of cookies that a client should be prepared 
                    155:        to receive and store. 
                    156:                300 total cookies 
                    157:                4 kilobytes per cookie, where the name and the OPAQUE_STRING combine 
                    158:                        to form the 4 kilobyte limit. 
                    159:                20 cookies per server or domain. (note that completely specified hosts 
                    160:                        and domains are treated as separate entities and have a 20 cookie limitation 
                    161:                        for each, not combined) 
1.25      parser    162: */
1.74      misha     163: 
                    164: const String* output_set_cookie_value(
                    165:                                        HashStringValue::key_type aname,
                    166:                                        HashStringValue::value_type ameaning,
                    167:                                        bool adelete){
                    168:        String* result=new String();
1.1       paf       169:        // attribute=
1.79      misha     170:        *result << String(aname, String::L_HTTP_COOKIE) << "=";
1.74      misha     171: 
1.56      paf       172:        Value* lmeaning;
1.1       paf       173:        // figure out 'meaning'
1.61      paf       174:        // Set-Cookie: (attribute)=(value); path=/
                    175:        HashStringValue *hash;
                    176:        double default_expires_days=adelete?-DEFAULT_EXPIRES_DAYS:+DEFAULT_EXPIRES_DAYS;
                    177:        if((hash=ameaning->get_hash())) { // ...[hash value]
                    178:                // clone to safely change it
1.74      misha     179:                lmeaning=new VHash(*hash);
                    180:                hash=lmeaning->get_hash();
                    181: 
1.61      paf       182:                // $expires
                    183:                if(Value* expires=hash->get(expires_name)) {
                    184:                        const String* string;
                    185:                        if(expires->is_string() && (string=expires->get_string()) && (*string==SESSION_NAME))  {
                    186:                                // $expires[session]
                    187:                                hash->remove(expires_name);
                    188:                        } else {
1.80      misha     189:                                if(Value* vdate=expires->as(VDATE_TYPE))
1.61      paf       190:                                        hash->put(expires_name, vdate); // $expires[DATE]
                    191:                                else if(double days_till_expire=expires->as_double())
                    192:                                        hash->put(expires_name, &expires_vdate(days_till_expire)); // $expires(days)
                    193:                                else
                    194:                                        hash->remove(expires_name); // $expires(0)
                    195:                        }
                    196:                } else // $expires not assigned, defaulting
                    197:                        hash->put(expires_name, &expires_vdate(default_expires_days));
                    198:        } else { // ...[string value]
                    199:                Value* wrap_meaning=new VHash;
                    200:                hash=wrap_meaning->get_hash();
                    201:                // wrapping lmeaning into hash
                    202:                hash->put(value_name, ameaning);
                    203:                // string = $expires not assigned, defaulting
                    204:                hash->put(expires_name, &expires_vdate(default_expires_days));
                    205:                // replacing lmeaning with hash-wrapped one
                    206:                lmeaning=wrap_meaning;
                    207:        }
                    208: 
                    209:        if(adelete) {// removing value
1.25      parser    210:                /*
1.73      misha     211:                        http://wp.netscape.com/newsref/std/cookie_spec.html
1.25      parser    212:                        to delete a cookie, it can do so by returning a cookie with the same name, 
                    213:                        and an expires time which is in the past
                    214:                */
                    215: 
1.1       paf       216:                // Set-Cookie: (attribute)=; path=/
1.61      paf       217:                lmeaning->get_hash()->remove(value_name);
1.1       paf       218:        }
1.74      misha     219: 
1.1       paf       220:        // defaulting path
1.56      paf       221:        if(!lmeaning->get_hash()->get(path_name))
1.74      misha     222:                lmeaning->get_hash()->put(path_name, new VString(path_value_default));
1.1       paf       223: 
1.56      paf       224:        // append lmeaning
1.74      misha     225:        *result << attributed_meaning_to_string(*lmeaning, String::L_HTTP_COOKIE, true, true /* allow bool attr */);
1.1       paf       226: 
1.74      misha     227:        return result;
1.1       paf       228: }
1.74      misha     229: 
                    230: 
                    231: struct Cookie_pass_info {
                    232:        SAPI_Info* sapi_info;
                    233:        Request_charsets* charsets;
                    234: };
                    235: 
                    236: void output_set_cookie_header(
                    237:                        HashStringValue::key_type aattribute, 
                    238:                        HashStringValue::value_type ameaning,
                    239:                        bool adelete,
                    240:                        Cookie_pass_info& cookie_info
                    241:        ){
1.82      misha     242:                SAPI::add_header_attribute(*cookie_info.sapi_info, "set-cookie",
1.79      misha     243:                        output_set_cookie_value(aattribute, ameaning, adelete)->untaint_cstr(String::L_AS_IS, 0, cookie_info.charsets));
1.56      paf       244: }
1.74      misha     245: 
                    246: void output_after(
                    247:                        HashStringValue::key_type aattribute, 
                    248:                        HashStringValue::value_type ameaning,
                    249:                        Cookie_pass_info& cookie_info
                    250:        ){
                    251:                output_set_cookie_header(aattribute, ameaning, false, cookie_info);
                    252: }
                    253: 
                    254: void output_deleted(
                    255:                        HashStringValue::key_type aattribute, 
                    256:                        HashStringValue::value_type ameaning, 
                    257:                        Cookie_pass_info& cookie_info
                    258:        ){
                    259:                if(ameaning)
                    260:                        output_set_cookie_header(aattribute, ameaning, true, cookie_info);
                    261: }
                    262: 
1.56      paf       263: void VCookie::output_result(SAPI_Info& sapi_info) {
1.74      misha     264:        Cookie_pass_info cookie_info={&sapi_info, &fcharsets};
                    265: 
                    266:        after.for_each<Cookie_pass_info&>(output_after, cookie_info);
                    267:        deleted.for_each<Cookie_pass_info&>(output_deleted, cookie_info);
                    268: }
                    269: 
                    270: bool VCookie::should_refill(){
                    271:        return !(
                    272:                &fcharsets.source()==filled_source
                    273:                && &fcharsets.client()==filled_client
                    274:        );
                    275: }
                    276: 
                    277: //#include <stdio.h>
                    278: void VCookie::refill(){
                    279:        //request_info.cookie="test-session=value%3D5; test-default1=value%3D1; test-default2=value%3D2; test-tomorrow=value%3D3";
                    280:        //request_info.cookie="enabled=yes; auth.uid=196325308053599810; enabled=yes; msnames; msuri"; // mdm 
                    281:        if(!frequest_info.cookie)
                    282:                return;
                    283: /*
                    284:        FILE *f=fopen("c:\\temp\\a", "wt");
                    285:        fprintf(f, "cookie=%s", request_info.cookie);
                    286:        fclose(f);
                    287: */
                    288:        char *cookies=strdup(frequest_info.cookie);
                    289:        char *current=cookies;
                    290:        //_asm int 3;
                    291:        do {
                    292:                if(char *attribute=search_stop(current, '='))
                    293:                        if(char *meaning=search_stop(current, ';')) {
                    294:                                const String& sattribute=
1.83      misha     295:                                        *new String(unescape_chars(attribute, strlen(attribute), &fcharsets.source(), true), String::L_TAINTED);
1.74      misha     296:                                const String& smeaning=
1.83      misha     297:                                        *new String(unescape_chars(meaning, strlen(meaning), &fcharsets.source(), true), String::L_TAINTED);
1.74      misha     298:                                before.put(sattribute, new VString(smeaning));
                    299: 
                    300:                                //if(sattribute == "test_js") throw Exception(0, 0, "'%s' '%s'", meaning, smeaning.cstr());
                    301:                        }
                    302:        } while(current);
                    303: 
                    304:        filled_source=&fcharsets.source();
                    305:        filled_client=&fcharsets.client();
1.1       paf       306: }
1.74      misha     307: 

E-mail: