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

1.6       paf         1: /** @file
                      2:        Parser: cookie class.
                      3: 
1.102     moko        4:        Copyright (c) 2001-2023 Art. Lebedev Studio (http://www.artlebedev.com)
                      5:        Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <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.104   ! moko       16: volatile const char * IDENT_PA_VCOOKIE_C="$Id: pa_vcookie.C,v 1.103 2024/09/13 04:01:23 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):
1.101     moko       36:        filled_source(0), filled_client(0),
                     37:        fcharsets(acharsets), frequest_info(arequest_info) {
1.74      misha      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.96      moko       53:                for(HashStringValue::Iterator i(after); i; i.next())
                     54:                        result->put(i.key(), i.value());
                     55:                for(HashStringValue::Iterator i(deleted); i; i.next())
                     56:                        result->remove(i.key());
1.72      misha      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.90      moko       64:        if(Value* after_meaning=after.get(aname)) { // assigned 'after'?
1.56      paf        65:                if(HashStringValue *hash=after_meaning->get_hash())
                     66:                        return hash->get(value_name);
1.2       paf        67:                else
                     68:                        return after_meaning;
1.90      moko       69:        }
1.1       paf        70:        
1.74      misha      71:        if(should_refill())
                     72:                refill();
                     73: 
1.1       paf        74:        // neither deleted nor assigned 
                     75:        // return any value it had 'before'
1.56      paf        76:        return before.get(aname);
1.1       paf        77: }
                     78: 
1.104   ! moko       79: static Value& expires_vdate(double days_till_expire) {
        !            80:        return *new VDate(time(NULL)+60*60*24*days_till_expire);
1.84      misha      81: }
                     82: 
1.88      moko       83: const VJunction* VCookie::put_element(const String& aname, Value* avalue) {
1.1       paf        84:        // $cookie
1.56      paf        85:        Value* lvalue;
1.84      misha      86:        if(HashStringValue *hash=avalue->get_hash()) {
1.85      misha      87:                if(Value* expires=hash->get(expires_name)){
                     88:                        const String* string;
                     89:                        if(!(expires->is_string() && (string=expires->get_string()) && (*string==SESSION_NAME)))
1.103     moko       90:                                if(!dynamic_cast<VDate*>(expires))
1.92      moko       91:                                        if(double days_till_expire=expires->as_double())
1.104   ! moko       92:                                                expires_vdate(days_till_expire); // checking if date is valid here to avoid failure when sending headers
1.85      misha      93:                }
1.56      paf        94:                lvalue=hash->get(value_name);
1.84      misha      95:        } else
1.50      paf        96:                lvalue=avalue;
1.51      paf        97: 
                     98:        if(lvalue && lvalue->is_string()) {
                     99:                // taint string being assigned
1.56      paf       100:                String& tainted=*new String;
                    101:                tainted.append(*lvalue->get_string(), String::L_TAINTED, true /*forced*/);
                    102:                lvalue=new VString(tainted);
1.51      paf       103:        }
1.1       paf       104: 
1.71      misha     105:        if( !lvalue || lvalue->as_string().is_empty() ) {
                    106:                deleted.put(aname, avalue);
                    107:                after.put(aname, 0);
                    108:        } else {
                    109:                after.put(aname, avalue);
                    110:                deleted.put(aname, 0);
                    111:        }
1.99      moko      112:        return 0;
1.1       paf       113: }
                    114: 
1.25      parser    115: /*
1.49      paf       116:        @todo 
1.87      misha     117:        http://curl.haxx.se/rfc/cookie_spec.html
                    118:        http://www.w3.org/Protocols/rfc2109/rfc2109
1.25      parser    119:        When sending cookies to a server, 
                    120:        all cookies with a more specific path mapping should be sent before cookies 
                    121:        with less specific path mappings. 
                    122:        For example, a cookie "name1=foo" with a path mapping of "/" should be sent after 
                    123:        a cookie "name1=foo2" with a path mapping of "/bar" if they are both to be sent. 
                    124: 
1.74      misha     125:        There are limitations on the number of cookies that a client can store at any one time. 
                    126:        This is a specification of the minimum number of cookies that a client should be prepared 
                    127:        to receive and store. 
                    128:                300 total cookies 
                    129:                4 kilobytes per cookie, where the name and the OPAQUE_STRING combine 
                    130:                        to form the 4 kilobyte limit. 
                    131:                20 cookies per server or domain. (note that completely specified hosts 
                    132:                        and domains are treated as separate entities and have a 20 cookie limitation 
                    133:                        for each, not combined) 
1.25      parser    134: */
1.74      misha     135: 
                    136: const String* output_set_cookie_value(
                    137:                                        HashStringValue::key_type aname,
                    138:                                        HashStringValue::value_type ameaning,
                    139:                                        bool adelete){
                    140:        String* result=new String();
1.1       paf       141:        // attribute=
1.79      misha     142:        *result << String(aname, String::L_HTTP_COOKIE) << "=";
1.74      misha     143: 
1.56      paf       144:        Value* lmeaning;
1.1       paf       145:        // figure out 'meaning'
1.61      paf       146:        // Set-Cookie: (attribute)=(value); path=/
                    147:        HashStringValue *hash;
                    148:        double default_expires_days=adelete?-DEFAULT_EXPIRES_DAYS:+DEFAULT_EXPIRES_DAYS;
                    149:        if((hash=ameaning->get_hash())) { // ...[hash value]
                    150:                // clone to safely change it
1.74      misha     151:                lmeaning=new VHash(*hash);
                    152:                hash=lmeaning->get_hash();
1.104   ! moko      153:  
1.61      paf       154:                // $expires
                    155:                if(Value* expires=hash->get(expires_name)) {
                    156:                        const String* string;
                    157:                        if(expires->is_string() && (string=expires->get_string()) && (*string==SESSION_NAME))  {
                    158:                                // $expires[session]
                    159:                                hash->remove(expires_name);
                    160:                        } else {
1.103     moko      161:                                if(Value* vdate=dynamic_cast<VDate*>(expires))
1.61      paf       162:                                        hash->put(expires_name, vdate); // $expires[DATE]
                    163:                                else if(double days_till_expire=expires->as_double())
                    164:                                        hash->put(expires_name, &expires_vdate(days_till_expire)); // $expires(days)
                    165:                                else
                    166:                                        hash->remove(expires_name); // $expires(0)
                    167:                        }
                    168:                } else // $expires not assigned, defaulting
                    169:                        hash->put(expires_name, &expires_vdate(default_expires_days));
                    170:        } else { // ...[string value]
                    171:                Value* wrap_meaning=new VHash;
                    172:                hash=wrap_meaning->get_hash();
                    173:                // wrapping lmeaning into hash
                    174:                hash->put(value_name, ameaning);
                    175:                // string = $expires not assigned, defaulting
                    176:                hash->put(expires_name, &expires_vdate(default_expires_days));
                    177:                // replacing lmeaning with hash-wrapped one
                    178:                lmeaning=wrap_meaning;
                    179:        }
                    180: 
                    181:        if(adelete) {// removing value
1.25      parser    182:                /*
1.87      misha     183:                        http://curl.haxx.se/rfc/cookie_spec.html
                    184:                        http://www.w3.org/Protocols/rfc2109/rfc2109
1.25      parser    185:                        to delete a cookie, it can do so by returning a cookie with the same name, 
                    186:                        and an expires time which is in the past
                    187:                */
                    188: 
1.1       paf       189:                // Set-Cookie: (attribute)=; path=/
1.61      paf       190:                lmeaning->get_hash()->remove(value_name);
1.1       paf       191:        }
1.74      misha     192: 
1.1       paf       193:        // defaulting path
1.56      paf       194:        if(!lmeaning->get_hash()->get(path_name))
1.74      misha     195:                lmeaning->get_hash()->put(path_name, new VString(path_value_default));
1.1       paf       196: 
1.56      paf       197:        // append lmeaning
1.74      misha     198:        *result << attributed_meaning_to_string(*lmeaning, String::L_HTTP_COOKIE, true, true /* allow bool attr */);
1.1       paf       199: 
1.74      misha     200:        return result;
1.1       paf       201: }
1.74      misha     202: 
                    203: 
                    204: struct Cookie_pass_info {
                    205:        SAPI_Info* sapi_info;
                    206:        Request_charsets* charsets;
                    207: };
                    208: 
                    209: void output_set_cookie_header(
                    210:                        HashStringValue::key_type aattribute, 
                    211:                        HashStringValue::value_type ameaning,
                    212:                        bool adelete,
                    213:                        Cookie_pass_info& cookie_info
                    214:        ){
1.82      misha     215:                SAPI::add_header_attribute(*cookie_info.sapi_info, "set-cookie",
1.79      misha     216:                        output_set_cookie_value(aattribute, ameaning, adelete)->untaint_cstr(String::L_AS_IS, 0, cookie_info.charsets));
1.56      paf       217: }
1.74      misha     218: 
                    219: void output_after(
                    220:                        HashStringValue::key_type aattribute, 
                    221:                        HashStringValue::value_type ameaning,
                    222:                        Cookie_pass_info& cookie_info
                    223:        ){
                    224:                output_set_cookie_header(aattribute, ameaning, false, cookie_info);
                    225: }
                    226: 
                    227: void output_deleted(
                    228:                        HashStringValue::key_type aattribute, 
                    229:                        HashStringValue::value_type ameaning, 
                    230:                        Cookie_pass_info& cookie_info
                    231:        ){
                    232:                if(ameaning)
                    233:                        output_set_cookie_header(aattribute, ameaning, true, cookie_info);
                    234: }
                    235: 
1.56      paf       236: void VCookie::output_result(SAPI_Info& sapi_info) {
1.74      misha     237:        Cookie_pass_info cookie_info={&sapi_info, &fcharsets};
                    238: 
                    239:        after.for_each<Cookie_pass_info&>(output_after, cookie_info);
                    240:        deleted.for_each<Cookie_pass_info&>(output_deleted, cookie_info);
                    241: }
                    242: 
                    243: bool VCookie::should_refill(){
                    244:        return !(
                    245:                &fcharsets.source()==filled_source
                    246:                && &fcharsets.client()==filled_client
                    247:        );
                    248: }
                    249: 
                    250: //#include <stdio.h>
                    251: void VCookie::refill(){
                    252:        //request_info.cookie="test-session=value%3D5; test-default1=value%3D1; test-default2=value%3D2; test-tomorrow=value%3D3";
                    253:        //request_info.cookie="enabled=yes; auth.uid=196325308053599810; enabled=yes; msnames; msuri"; // mdm 
                    254:        if(!frequest_info.cookie)
                    255:                return;
1.97      moko      256: 
1.74      misha     257:        char *cookies=strdup(frequest_info.cookie);
                    258:        char *current=cookies;
                    259:        //_asm int 3;
                    260:        do {
                    261:                if(char *attribute=search_stop(current, '='))
                    262:                        if(char *meaning=search_stop(current, ';')) {
                    263:                                const String& sattribute=
1.83      misha     264:                                        *new String(unescape_chars(attribute, strlen(attribute), &fcharsets.source(), true), String::L_TAINTED);
1.74      misha     265:                                const String& smeaning=
1.83      misha     266:                                        *new String(unescape_chars(meaning, strlen(meaning), &fcharsets.source(), true), String::L_TAINTED);
1.74      misha     267:                                before.put(sattribute, new VString(smeaning));
                    268: 
                    269:                                //if(sattribute == "test_js") throw Exception(0, 0, "'%s' '%s'", meaning, smeaning.cstr());
                    270:                        }
                    271:        } while(current);
                    272: 
                    273:        filled_source=&fcharsets.source();
                    274:        filled_client=&fcharsets.client();
1.1       paf       275: }
1.74      misha     276: 

E-mail: