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

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.71    ! misha       8: static const char * const IDENT_VCOOKIE_C="$Date: 2006/12/07 18:29:35 $";
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"
                     16: 
                     17: // defines
                     18: 
                     19: #define PATH_NAME "path"
                     20: #define PATH_VALUE_DEFAULT "/"
1.1       paf        21: 
                     22: #define SESSION_NAME "session"
                     23: #define DEFAULT_EXPIRES_DAYS 90
1.9       paf        24: 
1.56      paf        25: // statics
                     26: 
                     27: static const String path_name(PATH_NAME);
                     28: static const String path_value_default(PATH_VALUE_DEFAULT);
                     29: 
1.1       paf        30: // VCookie
                     31: 
1.56      paf        32: Value* VCookie::get_element(const String& aname, Value& /*aself*/, bool /*looking_up*/) {
1.28      parser     33:        // $CLASS
1.46      paf        34:        if(aname==CLASS_NAME)
1.28      parser     35:                return this;
                     36: 
1.1       paf        37:        // $cookie
1.46      paf        38:        if(deleted.get(aname)) // deleted?
1.1       paf        39:                return 0;
                     40:        
1.56      paf        41:        if(Value* after_meaning=after.get(aname)) // assigned 'after'?
                     42:                if(HashStringValue *hash=after_meaning->get_hash())
                     43:                        return hash->get(value_name);
1.2       paf        44:                else
                     45:                        return after_meaning;
1.1       paf        46:        
                     47:        // neither deleted nor assigned 
                     48:        // return any value it had 'before'
1.56      paf        49:        return before.get(aname);
1.1       paf        50: }
                     51: 
1.67      paf        52: const VJunction* VCookie::put_element(Value& /*aself*/, const String& aname, Value* avalue, bool /*replace*/) {
1.1       paf        53:        // $cookie
1.56      paf        54:        Value* lvalue;
                     55:        if(HashStringValue *hash=avalue->get_hash())
                     56:                lvalue=hash->get(value_name);
1.1       paf        57:        else
1.50      paf        58:                lvalue=avalue;
1.51      paf        59: 
                     60:        if(lvalue && lvalue->is_string()) {
                     61:                // taint string being assigned
1.56      paf        62:                String& tainted=*new String;
                     63:                tainted.append(*lvalue->get_string(), String::L_TAINTED, true /*forced*/);
                     64:                lvalue=new VString(tainted);
1.51      paf        65:        }
1.1       paf        66: 
1.71    ! misha      67:        if( !lvalue || lvalue->as_string().is_empty() ) {
        !            68:                deleted.put(aname, avalue);
        !            69:                after.put(aname, 0);
        !            70:        } else {
        !            71:                after.put(aname, avalue);
        !            72:                deleted.put(aname, 0);
        !            73:        }
1.64      paf        74:        return PUT_ELEMENT_REPLACED_ELEMENT;
1.1       paf        75: }
                     76: 
1.26      parser     77: static char *search_stop(char*& current, char cstop_at) {
1.37      paf        78:        // sanity check
                     79:        if(!current)
                     80:                return 0;
                     81: 
1.26      parser     82:        // skip leading WS
                     83:        while(*current==' ' || *current=='\t')
                     84:                current++;
                     85:        if(!*current)
                     86:                return current=0;
                     87: 
                     88:        char *result=current;
                     89:        if(char *pstop_at=strchr(current, cstop_at)) {
                     90:                *pstop_at=0;
                     91:                current=pstop_at+1;
                     92:        } else
                     93:                current=0;
                     94:        return result;
                     95: }
                     96: 
                     97: 
1.6       paf        98: //#include <stdio.h>
1.56      paf        99: void VCookie::fill_fields(Request_info& request_info) {
                    100:        //request_info.cookie="test-session=value%3D5; test-default1=value%3D1; test-default2=value%3D2; test-tomorrow=value%3D3";
                    101:        //request_info.cookie="enabled=yes; auth.uid=196325308053599810; enabled=yes; msnames; msuri"; // mdm 
                    102:        if(!request_info.cookie)
1.1       paf       103:                return;
                    104: /*
                    105:        FILE *f=fopen("c:\\temp\\a", "wt");
1.56      paf       106:        fprintf(f, "cookie=%s", request_info.cookie);
1.1       paf       107:        fclose(f);*/
1.56      paf       108:        char *cookies=strdup(request_info.cookie);
                    109:        char *current=cookies;
1.26      parser    110:        //_asm int 3;
1.56      paf       111:        do {
1.26      parser    112:                if(char *attribute=search_stop(current, '='))
1.38      paf       113:                        if(char *meaning=search_stop(current, ';')) {
1.56      paf       114:                                const String& sattribute=
                    115:                                        *new String(unescape_chars(attribute, strlen(attribute)), 0, true);
                    116:                                const String& smeaning=
                    117:                                        *new String(unescape_chars(meaning, strlen(meaning)), 0, true);
                    118:                                before.put(sattribute, new VString(smeaning));
1.38      paf       119:                        }
1.1       paf       120:        } while(current);
                    121: }
                    122: 
1.56      paf       123: static Value& expires_vdate(double days_till_expire) {
1.1       paf       124:        time_t when=time(NULL)+(time_t)(60*60*24*days_till_expire);
                    125:        struct tm *tms=gmtime(&when);
1.41      paf       126:        if(!tms)
1.43      paf       127:                throw Exception(0,
1.56      paf       128:                        0,
1.62      paf       129:                        "bad expires time (seconds from epoch=%u)", when);
1.49      paf       130: 
1.56      paf       131:        return *new VDate(when);
1.1       paf       132: }
                    133: 
1.25      parser    134: /*
1.49      paf       135:        @todo 
                    136:        http://www.netscape.com/newsref/std/cookie_spec.html
1.25      parser    137:        When sending cookies to a server, 
                    138:        all cookies with a more specific path mapping should be sent before cookies 
                    139:        with less specific path mappings. 
                    140:        For example, a cookie "name1=foo" with a path mapping of "/" should be sent after 
                    141:        a cookie "name1=foo2" with a path mapping of "/bar" if they are both to be sent. 
                    142: 
                    143:   There are limitations on the number of cookies that a client can store at any one time. 
                    144:   This is a specification of the minimum number of cookies that a client should be prepared 
                    145:   to receive and store. 
                    146:        300 total cookies 
                    147:        4 kilobytes per cookie, where the name and the OPAQUE_STRING combine 
                    148:                to form the 4 kilobyte limit. 
                    149:        20 cookies per server or domain. (note that completely specified hosts 
                    150:                and domains are treated as separate entities and have a 20 cookie limitation 
                    151:                for each, not combined) 
                    152: */
1.61      paf       153: static void output_set_cookie_header(
1.56      paf       154:                              HashStringValue::key_type aattribute, 
                    155:                              HashStringValue::value_type ameaning,
1.61      paf       156:                                  bool adelete,
1.56      paf       157:                              SAPI_Info& sapi_info) {
                    158:        String string;
1.1       paf       159:        // attribute
1.56      paf       160:        string.append(String(aattribute, String::L_TAINTED), String::L_HTTP_HEADER, true);
1.1       paf       161:        // attribute=
1.19      paf       162:        string << "=";
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
                    170:                lmeaning=new VHash(*hash);  hash=lmeaning->get_hash();
                    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:                /*
                    200:                        http://www.netscape.com/newsref/std/cookie_spec.html
                    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:        }
                    208:        // defaulting path
1.56      paf       209:        if(!lmeaning->get_hash()->get(path_name))
                    210:                lmeaning->get_hash()->put(path_name, 
                    211:                        new VString(path_value_default));
1.1       paf       212: 
1.56      paf       213:        // append lmeaning
1.70      misha     214:        string << attributed_meaning_to_string(*lmeaning, String::L_HTTP_HEADER, true, true /* allow bool attr */);
1.1       paf       215: 
                    216:        // output
1.56      paf       217:        SAPI::add_header_attribute(sapi_info, "set-cookie", string.cstr(String::L_UNSPECIFIED));
1.1       paf       218: }
1.56      paf       219: static void output_after(
                    220:                                                 HashStringValue::key_type aattribute, 
                    221:                                                 HashStringValue::value_type ameaning,
1.69      paf       222:                                                 SAPI_Info& sapi_info) {
                    223:        output_set_cookie_header(aattribute, ameaning, false, sapi_info);
1.56      paf       224: }
                    225: static void output_deleted(
                    226:                                                   HashStringValue::key_type aattribute, 
                    227:                                                   HashStringValue::value_type ameaning, 
1.69      paf       228:                                                   SAPI_Info& sapi_info) {
1.56      paf       229:        if(ameaning)
1.69      paf       230:                output_set_cookie_header(aattribute, ameaning, true, sapi_info);
1.56      paf       231: }
                    232: void VCookie::output_result(SAPI_Info& sapi_info) {
1.69      paf       233:        after.for_each<SAPI_Info&>(output_after, sapi_info);
                    234:        deleted.for_each<SAPI_Info&>(output_deleted, sapi_info);
1.1       paf       235: }

E-mail: