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

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.80    ! misha       8: static const char * const IDENT_VCOOKIE_C="$Date: 2009-07-06 12:10:09 $";
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.80    ! misha      78: const VJunction* VCookie::put_element(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.79      misha     161:        *result << String(aname, String::L_HTTP_COOKIE) << "=";
1.74      misha     162: 
1.56      paf       163:        Value* lmeaning;
1.1       paf       164:        // figure out 'meaning'
1.61      paf       165:        // Set-Cookie: (attribute)=(value); path=/
                    166:        HashStringValue *hash;
                    167:        double default_expires_days=adelete?-DEFAULT_EXPIRES_DAYS:+DEFAULT_EXPIRES_DAYS;
                    168:        if((hash=ameaning->get_hash())) { // ...[hash value]
                    169:                // clone to safely change it
1.74      misha     170:                lmeaning=new VHash(*hash);
                    171:                hash=lmeaning->get_hash();
                    172: 
1.61      paf       173:                // $expires
                    174:                if(Value* expires=hash->get(expires_name)) {
                    175:                        const String* string;
                    176:                        if(expires->is_string() && (string=expires->get_string()) && (*string==SESSION_NAME))  {
                    177:                                // $expires[session]
                    178:                                hash->remove(expires_name);
                    179:                        } else {
1.80    ! misha     180:                                if(Value* vdate=expires->as(VDATE_TYPE))
1.61      paf       181:                                        hash->put(expires_name, vdate); // $expires[DATE]
                    182:                                else if(double days_till_expire=expires->as_double())
                    183:                                        hash->put(expires_name, &expires_vdate(days_till_expire)); // $expires(days)
                    184:                                else
                    185:                                        hash->remove(expires_name); // $expires(0)
                    186:                        }
                    187:                } else // $expires not assigned, defaulting
                    188:                        hash->put(expires_name, &expires_vdate(default_expires_days));
                    189:        } else { // ...[string value]
                    190:                Value* wrap_meaning=new VHash;
                    191:                hash=wrap_meaning->get_hash();
                    192:                // wrapping lmeaning into hash
                    193:                hash->put(value_name, ameaning);
                    194:                // string = $expires not assigned, defaulting
                    195:                hash->put(expires_name, &expires_vdate(default_expires_days));
                    196:                // replacing lmeaning with hash-wrapped one
                    197:                lmeaning=wrap_meaning;
                    198:        }
                    199: 
                    200:        if(adelete) {// removing value
1.25      parser    201:                /*
1.73      misha     202:                        http://wp.netscape.com/newsref/std/cookie_spec.html
1.25      parser    203:                        to delete a cookie, it can do so by returning a cookie with the same name, 
                    204:                        and an expires time which is in the past
                    205:                */
                    206: 
1.1       paf       207:                // Set-Cookie: (attribute)=; path=/
1.61      paf       208:                lmeaning->get_hash()->remove(value_name);
1.1       paf       209:        }
1.74      misha     210: 
1.1       paf       211:        // defaulting path
1.56      paf       212:        if(!lmeaning->get_hash()->get(path_name))
1.74      misha     213:                lmeaning->get_hash()->put(path_name, new VString(path_value_default));
1.1       paf       214: 
1.56      paf       215:        // append lmeaning
1.74      misha     216:        *result << attributed_meaning_to_string(*lmeaning, String::L_HTTP_COOKIE, true, true /* allow bool attr */);
1.1       paf       217: 
1.74      misha     218:        return result;
1.1       paf       219: }
1.74      misha     220: 
                    221: 
                    222: struct Cookie_pass_info {
                    223:        SAPI_Info* sapi_info;
                    224:        Request_charsets* charsets;
                    225: };
                    226: 
                    227: void output_set_cookie_header(
                    228:                        HashStringValue::key_type aattribute, 
                    229:                        HashStringValue::value_type ameaning,
                    230:                        bool adelete,
                    231:                        Cookie_pass_info& cookie_info
                    232:        ){
                    233:                SAPI::add_header_attribute(*cookie_info.sapi_info, "set-cookie",
1.79      misha     234:                        output_set_cookie_value(aattribute, ameaning, adelete)->untaint_cstr(String::L_AS_IS, 0, cookie_info.charsets));
1.56      paf       235: }
1.74      misha     236: 
                    237: void output_after(
                    238:                        HashStringValue::key_type aattribute, 
                    239:                        HashStringValue::value_type ameaning,
                    240:                        Cookie_pass_info& cookie_info
                    241:        ){
                    242:                output_set_cookie_header(aattribute, ameaning, false, cookie_info);
                    243: }
                    244: 
                    245: void output_deleted(
                    246:                        HashStringValue::key_type aattribute, 
                    247:                        HashStringValue::value_type ameaning, 
                    248:                        Cookie_pass_info& cookie_info
                    249:        ){
                    250:                if(ameaning)
                    251:                        output_set_cookie_header(aattribute, ameaning, true, cookie_info);
                    252: }
                    253: 
1.56      paf       254: void VCookie::output_result(SAPI_Info& sapi_info) {
1.74      misha     255:        Cookie_pass_info cookie_info={&sapi_info, &fcharsets};
                    256: 
                    257:        after.for_each<Cookie_pass_info&>(output_after, cookie_info);
                    258:        deleted.for_each<Cookie_pass_info&>(output_deleted, cookie_info);
                    259: }
                    260: 
                    261: bool VCookie::should_refill(){
                    262:        return !(
                    263:                &fcharsets.source()==filled_source
                    264:                && &fcharsets.client()==filled_client
                    265:        );
                    266: }
                    267: 
                    268: //#include <stdio.h>
                    269: void VCookie::refill(){
                    270:        //request_info.cookie="test-session=value%3D5; test-default1=value%3D1; test-default2=value%3D2; test-tomorrow=value%3D3";
                    271:        //request_info.cookie="enabled=yes; auth.uid=196325308053599810; enabled=yes; msnames; msuri"; // mdm 
                    272:        if(!frequest_info.cookie)
                    273:                return;
                    274: /*
                    275:        FILE *f=fopen("c:\\temp\\a", "wt");
                    276:        fprintf(f, "cookie=%s", request_info.cookie);
                    277:        fclose(f);
                    278: */
                    279:        char *cookies=strdup(frequest_info.cookie);
                    280:        char *current=cookies;
                    281:        //_asm int 3;
                    282:        do {
                    283:                if(char *attribute=search_stop(current, '='))
                    284:                        if(char *meaning=search_stop(current, ';')) {
                    285:                                const String& sattribute=
1.78      misha     286:                                        *new String(unescape_chars(attribute, strlen(attribute), &fcharsets.source(), true/*don't convert '"' to space*/), String::L_TAINTED);
1.74      misha     287:                                const String& smeaning=
1.78      misha     288:                                        *new String(unescape_chars(meaning, strlen(meaning), &fcharsets.source(), true/*don't convert '"' to space*/), String::L_TAINTED);
1.74      misha     289:                                before.put(sattribute, new VString(smeaning));
                    290: 
                    291:                                //if(sattribute == "test_js") throw Exception(0, 0, "'%s' '%s'", meaning, smeaning.cstr());
                    292:                        }
                    293:        } while(current);
                    294: 
                    295:        filled_source=&fcharsets.source();
                    296:        filled_client=&fcharsets.client();
1.1       paf       297: }
1.74      misha     298: 

E-mail: