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

1.6       paf         1: /** @file
                      2:        Parser: cookie class.
                      3: 
1.93      moko        4:        Copyright (c) 2001-2015 Art. Lebedev Studio (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.11      paf         8: #include "pa_sapi.h"
1.3       paf         9: #include "pa_common.h"
1.1       paf        10: #include "pa_vcookie.h"
                     11: #include "pa_vstring.h"
1.49      paf        12: #include "pa_vdate.h"
1.56      paf        13: #include "pa_vhash.h"
1.74      misha      14: #include "pa_request.h"
1.56      paf        15: 
1.95    ! moko       16: volatile const char * IDENT_PA_VCOOKIE_C="$Id: pa_vcookie.C,v 1.94 2016/04/06 22:11:43 moko Exp $" IDENT_PA_VCOOKIE_H;
1.86      moko       17: 
1.56      paf        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.95    ! moko       41: #ifndef OPTIMIZE_BYTECODE_GET_ELEMENT__SPECIAL
        !            42:        // CLASS, CLASS_NAME
1.94      moko       43:        if(Value* result=VStateless_class::get_element(aname))
                     44:                return result;
1.95    ! moko       45: #endif
1.76      misha      46: 
1.74      misha      47:        // $fields
1.72      misha      48:        if(aname==COOKIE_FIELDS_ELEMENT_NAME){
1.74      misha      49:                if(should_refill())
                     50:                        refill();
                     51: 
                     52:                HashStringValue *result=new HashStringValue(before);
1.72      misha      53:                after.for_each<HashStringValue*>(copy_all_overwrite_to, result);
                     54:                deleted.for_each<HashStringValue*>(remove_key_from, result);
                     55:                return new VHash(*result);
                     56:        }
                     57: 
1.1       paf        58:        // $cookie
1.46      paf        59:        if(deleted.get(aname)) // deleted?
1.1       paf        60:                return 0;
                     61:        
1.90      moko       62:        if(Value* after_meaning=after.get(aname)) { // assigned 'after'?
1.56      paf        63:                if(HashStringValue *hash=after_meaning->get_hash())
                     64:                        return hash->get(value_name);
1.2       paf        65:                else
                     66:                        return after_meaning;
1.90      moko       67:        }
1.1       paf        68:        
1.74      misha      69:        if(should_refill())
                     70:                refill();
                     71: 
1.1       paf        72:        // neither deleted nor assigned 
                     73:        // return any value it had 'before'
1.56      paf        74:        return before.get(aname);
1.1       paf        75: }
                     76: 
1.84      misha      77: time_t expires_sec(double days_till_expire) {
                     78:        time_t result=time(NULL)+(time_t)(60*60*24*days_till_expire);
                     79:        struct tm* tms=gmtime(&result);
                     80:        if(!tms)
                     81:                throw Exception(DATE_RANGE_EXCEPTION_TYPE,
                     82:                        0,
                     83:                        "bad expires time (seconds from epoch=%u)", result);
                     84:        return result;
                     85: }
                     86: 
1.88      moko       87: const VJunction* VCookie::put_element(const String& aname, Value* avalue) {
1.1       paf        88:        // $cookie
1.56      paf        89:        Value* lvalue;
1.84      misha      90:        if(HashStringValue *hash=avalue->get_hash()) {
1.85      misha      91:                if(Value* expires=hash->get(expires_name)){
                     92:                        const String* string;
                     93:                        if(!(expires->is_string() && (string=expires->get_string()) && (*string==SESSION_NAME)))
1.92      moko       94:                                if(!expires->as(VDATE_TYPE))
                     95:                                        if(double days_till_expire=expires->as_double())
                     96:                                                expires_sec(days_till_expire);
1.85      misha      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.56      paf       119: static Value& expires_vdate(double days_till_expire) {
1.91      moko      120:        return *new VDate((pa_time_t)expires_sec(days_till_expire));
1.1       paf       121: }
                    122: 
1.25      parser    123: /*
1.49      paf       124:        @todo 
1.87      misha     125:        http://curl.haxx.se/rfc/cookie_spec.html
                    126:        http://www.w3.org/Protocols/rfc2109/rfc2109
1.25      parser    127:        When sending cookies to a server, 
                    128:        all cookies with a more specific path mapping should be sent before cookies 
                    129:        with less specific path mappings. 
                    130:        For example, a cookie "name1=foo" with a path mapping of "/" should be sent after 
                    131:        a cookie "name1=foo2" with a path mapping of "/bar" if they are both to be sent. 
                    132: 
1.74      misha     133:        There are limitations on the number of cookies that a client can store at any one time. 
                    134:        This is a specification of the minimum number of cookies that a client should be prepared 
                    135:        to receive and store. 
                    136:                300 total cookies 
                    137:                4 kilobytes per cookie, where the name and the OPAQUE_STRING combine 
                    138:                        to form the 4 kilobyte limit. 
                    139:                20 cookies per server or domain. (note that completely specified hosts 
                    140:                        and domains are treated as separate entities and have a 20 cookie limitation 
                    141:                        for each, not combined) 
1.25      parser    142: */
1.74      misha     143: 
                    144: const String* output_set_cookie_value(
                    145:                                        HashStringValue::key_type aname,
                    146:                                        HashStringValue::value_type ameaning,
                    147:                                        bool adelete){
                    148:        String* result=new String();
1.1       paf       149:        // attribute=
1.79      misha     150:        *result << String(aname, String::L_HTTP_COOKIE) << "=";
1.74      misha     151: 
1.56      paf       152:        Value* lmeaning;
1.1       paf       153:        // figure out 'meaning'
1.61      paf       154:        // Set-Cookie: (attribute)=(value); path=/
                    155:        HashStringValue *hash;
                    156:        double default_expires_days=adelete?-DEFAULT_EXPIRES_DAYS:+DEFAULT_EXPIRES_DAYS;
                    157:        if((hash=ameaning->get_hash())) { // ...[hash value]
                    158:                // clone to safely change it
1.74      misha     159:                lmeaning=new VHash(*hash);
                    160:                hash=lmeaning->get_hash();
                    161: 
1.61      paf       162:                // $expires
                    163:                if(Value* expires=hash->get(expires_name)) {
                    164:                        const String* string;
                    165:                        if(expires->is_string() && (string=expires->get_string()) && (*string==SESSION_NAME))  {
                    166:                                // $expires[session]
                    167:                                hash->remove(expires_name);
                    168:                        } else {
1.80      misha     169:                                if(Value* vdate=expires->as(VDATE_TYPE))
1.61      paf       170:                                        hash->put(expires_name, vdate); // $expires[DATE]
                    171:                                else if(double days_till_expire=expires->as_double())
                    172:                                        hash->put(expires_name, &expires_vdate(days_till_expire)); // $expires(days)
                    173:                                else
                    174:                                        hash->remove(expires_name); // $expires(0)
                    175:                        }
                    176:                } else // $expires not assigned, defaulting
                    177:                        hash->put(expires_name, &expires_vdate(default_expires_days));
                    178:        } else { // ...[string value]
                    179:                Value* wrap_meaning=new VHash;
                    180:                hash=wrap_meaning->get_hash();
                    181:                // wrapping lmeaning into hash
                    182:                hash->put(value_name, ameaning);
                    183:                // string = $expires not assigned, defaulting
                    184:                hash->put(expires_name, &expires_vdate(default_expires_days));
                    185:                // replacing lmeaning with hash-wrapped one
                    186:                lmeaning=wrap_meaning;
                    187:        }
                    188: 
                    189:        if(adelete) {// removing value
1.25      parser    190:                /*
1.87      misha     191:                        http://curl.haxx.se/rfc/cookie_spec.html
                    192:                        http://www.w3.org/Protocols/rfc2109/rfc2109
1.25      parser    193:                        to delete a cookie, it can do so by returning a cookie with the same name, 
                    194:                        and an expires time which is in the past
                    195:                */
                    196: 
1.1       paf       197:                // Set-Cookie: (attribute)=; path=/
1.61      paf       198:                lmeaning->get_hash()->remove(value_name);
1.1       paf       199:        }
1.74      misha     200: 
1.1       paf       201:        // defaulting path
1.56      paf       202:        if(!lmeaning->get_hash()->get(path_name))
1.74      misha     203:                lmeaning->get_hash()->put(path_name, new VString(path_value_default));
1.1       paf       204: 
1.56      paf       205:        // append lmeaning
1.74      misha     206:        *result << attributed_meaning_to_string(*lmeaning, String::L_HTTP_COOKIE, true, true /* allow bool attr */);
1.1       paf       207: 
1.74      misha     208:        return result;
1.1       paf       209: }
1.74      misha     210: 
                    211: 
                    212: struct Cookie_pass_info {
                    213:        SAPI_Info* sapi_info;
                    214:        Request_charsets* charsets;
                    215: };
                    216: 
                    217: void output_set_cookie_header(
                    218:                        HashStringValue::key_type aattribute, 
                    219:                        HashStringValue::value_type ameaning,
                    220:                        bool adelete,
                    221:                        Cookie_pass_info& cookie_info
                    222:        ){
1.82      misha     223:                SAPI::add_header_attribute(*cookie_info.sapi_info, "set-cookie",
1.79      misha     224:                        output_set_cookie_value(aattribute, ameaning, adelete)->untaint_cstr(String::L_AS_IS, 0, cookie_info.charsets));
1.56      paf       225: }
1.74      misha     226: 
                    227: void output_after(
                    228:                        HashStringValue::key_type aattribute, 
                    229:                        HashStringValue::value_type ameaning,
                    230:                        Cookie_pass_info& cookie_info
                    231:        ){
                    232:                output_set_cookie_header(aattribute, ameaning, false, cookie_info);
                    233: }
                    234: 
                    235: void output_deleted(
                    236:                        HashStringValue::key_type aattribute, 
                    237:                        HashStringValue::value_type ameaning, 
                    238:                        Cookie_pass_info& cookie_info
                    239:        ){
                    240:                if(ameaning)
                    241:                        output_set_cookie_header(aattribute, ameaning, true, cookie_info);
                    242: }
                    243: 
1.56      paf       244: void VCookie::output_result(SAPI_Info& sapi_info) {
1.74      misha     245:        Cookie_pass_info cookie_info={&sapi_info, &fcharsets};
                    246: 
                    247:        after.for_each<Cookie_pass_info&>(output_after, cookie_info);
                    248:        deleted.for_each<Cookie_pass_info&>(output_deleted, cookie_info);
                    249: }
                    250: 
                    251: bool VCookie::should_refill(){
                    252:        return !(
                    253:                &fcharsets.source()==filled_source
                    254:                && &fcharsets.client()==filled_client
                    255:        );
                    256: }
                    257: 
                    258: //#include <stdio.h>
                    259: void VCookie::refill(){
                    260:        //request_info.cookie="test-session=value%3D5; test-default1=value%3D1; test-default2=value%3D2; test-tomorrow=value%3D3";
                    261:        //request_info.cookie="enabled=yes; auth.uid=196325308053599810; enabled=yes; msnames; msuri"; // mdm 
                    262:        if(!frequest_info.cookie)
                    263:                return;
                    264: /*
                    265:        FILE *f=fopen("c:\\temp\\a", "wt");
                    266:        fprintf(f, "cookie=%s", request_info.cookie);
                    267:        fclose(f);
                    268: */
                    269:        char *cookies=strdup(frequest_info.cookie);
                    270:        char *current=cookies;
                    271:        //_asm int 3;
                    272:        do {
                    273:                if(char *attribute=search_stop(current, '='))
                    274:                        if(char *meaning=search_stop(current, ';')) {
                    275:                                const String& sattribute=
1.83      misha     276:                                        *new String(unescape_chars(attribute, strlen(attribute), &fcharsets.source(), true), String::L_TAINTED);
1.74      misha     277:                                const String& smeaning=
1.83      misha     278:                                        *new String(unescape_chars(meaning, strlen(meaning), &fcharsets.source(), true), String::L_TAINTED);
1.74      misha     279:                                before.put(sattribute, new VString(smeaning));
                    280: 
                    281:                                //if(sattribute == "test_js") throw Exception(0, 0, "'%s' '%s'", meaning, smeaning.cstr());
                    282:                        }
                    283:        } while(current);
                    284: 
                    285:        filled_source=&fcharsets.source();
                    286:        filled_client=&fcharsets.client();
1.1       paf       287: }
1.74      misha     288: 

E-mail: