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

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.76    ! misha       8: static const char * const IDENT_VCOOKIE_C="$Date: 2008-09-04 09:36:04 $";
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.56      paf        40: Value* VCookie::get_element(const String& aname, Value& /*aself*/, bool /*looking_up*/) {
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.67      paf        78: const VJunction* VCookie::put_element(Value& /*aself*/, const String& aname, Value* avalue, bool /*replace*/) {
1.1       paf        79:        // $cookie
1.56      paf        80:        Value* lvalue;
                     81:        if(HashStringValue *hash=avalue->get_hash())
                     82:                lvalue=hash->get(value_name);
1.1       paf        83:        else
1.50      paf        84:                lvalue=avalue;
1.51      paf        85: 
                     86:        if(lvalue && lvalue->is_string()) {
                     87:                // taint string being assigned
1.56      paf        88:                String& tainted=*new String;
                     89:                tainted.append(*lvalue->get_string(), String::L_TAINTED, true /*forced*/);
                     90:                lvalue=new VString(tainted);
1.51      paf        91:        }
1.1       paf        92: 
1.71      misha      93:        if( !lvalue || lvalue->as_string().is_empty() ) {
                     94:                deleted.put(aname, avalue);
                     95:                after.put(aname, 0);
                     96:        } else {
                     97:                after.put(aname, avalue);
                     98:                deleted.put(aname, 0);
                     99:        }
1.64      paf       100:        return PUT_ELEMENT_REPLACED_ELEMENT;
1.1       paf       101: }
                    102: 
1.26      parser    103: static char *search_stop(char*& current, char cstop_at) {
1.37      paf       104:        // sanity check
                    105:        if(!current)
                    106:                return 0;
                    107: 
1.26      parser    108:        // skip leading WS
                    109:        while(*current==' ' || *current=='\t')
                    110:                current++;
                    111:        if(!*current)
                    112:                return current=0;
                    113: 
                    114:        char *result=current;
                    115:        if(char *pstop_at=strchr(current, cstop_at)) {
                    116:                *pstop_at=0;
                    117:                current=pstop_at+1;
                    118:        } else
                    119:                current=0;
                    120:        return result;
                    121: }
                    122: 
                    123: 
1.56      paf       124: static Value& expires_vdate(double days_till_expire) {
1.1       paf       125:        time_t when=time(NULL)+(time_t)(60*60*24*days_till_expire);
                    126:        struct tm *tms=gmtime(&when);
1.41      paf       127:        if(!tms)
1.75      misha     128:                throw Exception(DATE_RANGE_EXCEPTION_TYPE,
1.56      paf       129:                        0,
1.62      paf       130:                        "bad expires time (seconds from epoch=%u)", when);
1.49      paf       131: 
1.56      paf       132:        return *new VDate(when);
1.1       paf       133: }
                    134: 
1.25      parser    135: /*
1.49      paf       136:        @todo 
1.73      misha     137:        http://wp.netscape.com/newsref/std/cookie_spec.html
1.25      parser    138:        When sending cookies to a server, 
                    139:        all cookies with a more specific path mapping should be sent before cookies 
                    140:        with less specific path mappings. 
                    141:        For example, a cookie "name1=foo" with a path mapping of "/" should be sent after 
                    142:        a cookie "name1=foo2" with a path mapping of "/bar" if they are both to be sent. 
                    143: 
1.74      misha     144:        There are limitations on the number of cookies that a client can store at any one time. 
                    145:        This is a specification of the minimum number of cookies that a client should be prepared 
                    146:        to receive and store. 
                    147:                300 total cookies 
                    148:                4 kilobytes per cookie, where the name and the OPAQUE_STRING combine 
                    149:                        to form the 4 kilobyte limit. 
                    150:                20 cookies per server or domain. (note that completely specified hosts 
                    151:                        and domains are treated as separate entities and have a 20 cookie limitation 
                    152:                        for each, not combined) 
1.25      parser    153: */
1.74      misha     154: 
                    155: const String* output_set_cookie_value(
                    156:                                        HashStringValue::key_type aname,
                    157:                                        HashStringValue::value_type ameaning,
                    158:                                        bool adelete){
                    159:        String* result=new String();
1.1       paf       160:        // attribute
1.74      misha     161:        result->append(String(aname, String::L_TAINTED), String::L_HTTP_COOKIE, true);
1.1       paf       162:        // attribute=
1.74      misha     163:        *result << "=";
                    164: 
1.56      paf       165:        Value* lmeaning;
1.1       paf       166:        // figure out 'meaning'
1.61      paf       167:        // Set-Cookie: (attribute)=(value); path=/
                    168:        HashStringValue *hash;
                    169:        double default_expires_days=adelete?-DEFAULT_EXPIRES_DAYS:+DEFAULT_EXPIRES_DAYS;
                    170:        if((hash=ameaning->get_hash())) { // ...[hash value]
                    171:                // clone to safely change it
1.74      misha     172:                lmeaning=new VHash(*hash);
                    173:                hash=lmeaning->get_hash();
                    174: 
1.61      paf       175:                // $expires
                    176:                if(Value* expires=hash->get(expires_name)) {
                    177:                        const String* string;
                    178:                        if(expires->is_string() && (string=expires->get_string()) && (*string==SESSION_NAME))  {
                    179:                                // $expires[session]
                    180:                                hash->remove(expires_name);
                    181:                        } else {
                    182:                                if(Value* vdate=expires->as(VDATE_TYPE, false))
                    183:                                        hash->put(expires_name, vdate); // $expires[DATE]
                    184:                                else if(double days_till_expire=expires->as_double())
                    185:                                        hash->put(expires_name, &expires_vdate(days_till_expire)); // $expires(days)
                    186:                                else
                    187:                                        hash->remove(expires_name); // $expires(0)
                    188:                        }
                    189:                } else // $expires not assigned, defaulting
                    190:                        hash->put(expires_name, &expires_vdate(default_expires_days));
                    191:        } else { // ...[string value]
                    192:                Value* wrap_meaning=new VHash;
                    193:                hash=wrap_meaning->get_hash();
                    194:                // wrapping lmeaning into hash
                    195:                hash->put(value_name, ameaning);
                    196:                // string = $expires not assigned, defaulting
                    197:                hash->put(expires_name, &expires_vdate(default_expires_days));
                    198:                // replacing lmeaning with hash-wrapped one
                    199:                lmeaning=wrap_meaning;
                    200:        }
                    201: 
                    202:        if(adelete) {// removing value
1.25      parser    203:                /*
1.73      misha     204:                        http://wp.netscape.com/newsref/std/cookie_spec.html
1.25      parser    205:                        to delete a cookie, it can do so by returning a cookie with the same name, 
                    206:                        and an expires time which is in the past
                    207:                */
                    208: 
1.1       paf       209:                // Set-Cookie: (attribute)=; path=/
1.61      paf       210:                lmeaning->get_hash()->remove(value_name);
1.1       paf       211:        }
1.74      misha     212: 
1.1       paf       213:        // defaulting path
1.56      paf       214:        if(!lmeaning->get_hash()->get(path_name))
1.74      misha     215:                lmeaning->get_hash()->put(path_name, new VString(path_value_default));
1.1       paf       216: 
1.56      paf       217:        // append lmeaning
1.74      misha     218:        *result << attributed_meaning_to_string(*lmeaning, String::L_HTTP_COOKIE, true, true /* allow bool attr */);
1.1       paf       219: 
1.74      misha     220:        return result;
1.1       paf       221: }
1.74      misha     222: 
                    223: 
                    224: struct Cookie_pass_info {
                    225:        SAPI_Info* sapi_info;
                    226:        Request_charsets* charsets;
                    227: };
                    228: 
                    229: void output_set_cookie_header(
                    230:                        HashStringValue::key_type aattribute, 
                    231:                        HashStringValue::value_type ameaning,
                    232:                        bool adelete,
                    233:                        Cookie_pass_info& cookie_info
                    234:        ){
                    235:                SAPI::add_header_attribute(*cookie_info.sapi_info, "set-cookie",
                    236:                        output_set_cookie_value(aattribute, ameaning, adelete)->cstr(String::L_UNSPECIFIED, 0, cookie_info.charsets));
1.56      paf       237: }
1.74      misha     238: 
                    239: void output_after(
                    240:                        HashStringValue::key_type aattribute, 
                    241:                        HashStringValue::value_type ameaning,
                    242:                        Cookie_pass_info& cookie_info
                    243:        ){
                    244:                output_set_cookie_header(aattribute, ameaning, false, cookie_info);
                    245: }
                    246: 
                    247: void output_deleted(
                    248:                        HashStringValue::key_type aattribute, 
                    249:                        HashStringValue::value_type ameaning, 
                    250:                        Cookie_pass_info& cookie_info
                    251:        ){
                    252:                if(ameaning)
                    253:                        output_set_cookie_header(aattribute, ameaning, true, cookie_info);
                    254: }
                    255: 
1.56      paf       256: void VCookie::output_result(SAPI_Info& sapi_info) {
1.74      misha     257:        Cookie_pass_info cookie_info={&sapi_info, &fcharsets};
                    258: 
                    259:        after.for_each<Cookie_pass_info&>(output_after, cookie_info);
                    260:        deleted.for_each<Cookie_pass_info&>(output_deleted, cookie_info);
                    261: }
                    262: 
                    263: bool VCookie::should_refill(){
                    264:        return !(
                    265:                &fcharsets.source()==filled_source
                    266:                && &fcharsets.client()==filled_client
                    267:        );
                    268: }
                    269: 
                    270: //#include <stdio.h>
                    271: void VCookie::refill(){
                    272:        //request_info.cookie="test-session=value%3D5; test-default1=value%3D1; test-default2=value%3D2; test-tomorrow=value%3D3";
                    273:        //request_info.cookie="enabled=yes; auth.uid=196325308053599810; enabled=yes; msnames; msuri"; // mdm 
                    274:        if(!frequest_info.cookie)
                    275:                return;
                    276: /*
                    277:        FILE *f=fopen("c:\\temp\\a", "wt");
                    278:        fprintf(f, "cookie=%s", request_info.cookie);
                    279:        fclose(f);
                    280: */
                    281:        char *cookies=strdup(frequest_info.cookie);
                    282:        char *current=cookies;
                    283:        //_asm int 3;
                    284:        do {
                    285:                if(char *attribute=search_stop(current, '='))
                    286:                        if(char *meaning=search_stop(current, ';')) {
                    287:                                const String& sattribute=
                    288:                                        *new String(unescape_chars(attribute, strlen(attribute), &fcharsets.source(), true/*don't convert '"' to space*/), 0, true);
                    289:                                const String& smeaning=
                    290:                                        *new String(unescape_chars(meaning, strlen(meaning), &fcharsets.source(), true/*don't convert '"' to space*/), 0, true);
                    291:                                before.put(sattribute, new VString(smeaning));
                    292: 
                    293:                                //if(sattribute == "test_js") throw Exception(0, 0, "'%s' '%s'", meaning, smeaning.cstr());
                    294:                        }
                    295:        } while(current);
                    296: 
                    297:        filled_source=&fcharsets.source();
                    298:        filled_client=&fcharsets.client();
1.1       paf       299: }
1.74      misha     300: 

E-mail: