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

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

E-mail: