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

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

E-mail: