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

1.6       paf         1: /** @file
                      2:        Parser: cookie class.
                      3: 
1.56      paf         4:        Copyright(c) 2001-2003 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.57    ! paf         8: static const char* IDENT_VCOOKIE_C="$Date: 2003/07/24 11:31:25 $";
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 EXPIRES_NAME "expires"
                     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.56      paf        26: // statics
                     27: 
                     28: static const String expires_name(EXPIRES_NAME);
                     29: static const String path_name(PATH_NAME);
                     30: static const String path_value_default(PATH_VALUE_DEFAULT);
                     31: 
1.1       paf        32: // VCookie
                     33: 
1.56      paf        34: Value* VCookie::get_element(const String& aname, Value& /*aself*/, bool /*looking_up*/) {
1.28      parser     35:        // $CLASS
1.46      paf        36:        if(aname==CLASS_NAME)
1.28      parser     37:                return this;
                     38: 
1.1       paf        39:        // $cookie
1.46      paf        40:        if(deleted.get(aname)) // deleted?
1.1       paf        41:                return 0;
                     42:        
1.56      paf        43:        if(Value* after_meaning=after.get(aname)) // assigned 'after'?
                     44:                if(HashStringValue *hash=after_meaning->get_hash())
                     45:                        return hash->get(value_name);
1.2       paf        46:                else
                     47:                        return after_meaning;
1.1       paf        48:        
                     49:        // neither deleted nor assigned 
                     50:        // return any value it had 'before'
1.56      paf        51:        return before.get(aname);
1.1       paf        52: }
                     53: 
1.56      paf        54: bool VCookie::put_element(const String& aname, Value* avalue, bool /*replace*/) {
1.1       paf        55:        // $cookie
                     56:        bool remove;
1.56      paf        57:        Value* lvalue;
                     58:        if(HashStringValue *hash=avalue->get_hash())
                     59:                lvalue=hash->get(value_name);
1.1       paf        60:        else
1.50      paf        61:                lvalue=avalue;
1.51      paf        62: 
                     63:        if(lvalue && lvalue->is_string()) {
                     64:                // taint string being assigned
1.56      paf        65:                String& tainted=*new String;
                     66:                tainted.append(*lvalue->get_string(), String::L_TAINTED, true /*forced*/);
                     67:                lvalue=new VString(tainted);
1.51      paf        68:        }
1.1       paf        69: 
1.50      paf        70:        remove=!lvalue || lvalue->as_string().is_empty();
                     71: 
1.54      paf        72:        (remove?deleted:after).put(aname, avalue);
1.46      paf        73:        (remove?after:deleted).put(aname, 0);
                     74:        
                     75:        return true;
1.1       paf        76: }
                     77: 
1.26      parser     78: static char *search_stop(char*& current, char cstop_at) {
1.37      paf        79:        // sanity check
                     80:        if(!current)
                     81:                return 0;
                     82: 
1.26      parser     83:        // skip leading WS
                     84:        while(*current==' ' || *current=='\t')
                     85:                current++;
                     86:        if(!*current)
                     87:                return current=0;
                     88: 
                     89:        char *result=current;
                     90:        if(char *pstop_at=strchr(current, cstop_at)) {
                     91:                *pstop_at=0;
                     92:                current=pstop_at+1;
                     93:        } else
                     94:                current=0;
                     95:        return result;
                     96: }
                     97: 
                     98: 
1.6       paf        99: //#include <stdio.h>
1.56      paf       100: void VCookie::fill_fields(Request_info& request_info) {
                    101:        //request_info.cookie="test-session=value%3D5; test-default1=value%3D1; test-default2=value%3D2; test-tomorrow=value%3D3";
                    102:        //request_info.cookie="enabled=yes; auth.uid=196325308053599810; enabled=yes; msnames; msuri"; // mdm 
                    103:        if(!request_info.cookie)
1.1       paf       104:                return;
                    105: /*
                    106:        FILE *f=fopen("c:\\temp\\a", "wt");
1.56      paf       107:        fprintf(f, "cookie=%s", request_info.cookie);
1.1       paf       108:        fclose(f);*/
1.56      paf       109:        char *cookies=strdup(request_info.cookie);
                    110:        char *current=cookies;
1.26      parser    111:        //_asm int 3;
1.56      paf       112:        do {
1.26      parser    113:                if(char *attribute=search_stop(current, '='))
1.38      paf       114:                        if(char *meaning=search_stop(current, ';')) {
1.56      paf       115:                                const String& sattribute=
                    116:                                        *new String(unescape_chars(attribute, strlen(attribute)), 0, true);
                    117:                                const String& smeaning=
                    118:                                        *new String(unescape_chars(meaning, strlen(meaning)), 0, true);
                    119:                                before.put(sattribute, new VString(smeaning));
1.38      paf       120:                        }
1.1       paf       121:        } while(current);
                    122: }
                    123: 
1.56      paf       124: static Value& expires_vdate(double days_till_expire) {
1.1       paf       125:        time_t when=time(NULL)+(time_t)(60*60*24*days_till_expire);
                    126:        struct tm *tms=gmtime(&when);
1.41      paf       127:        if(!tms)
1.43      paf       128:                throw Exception(0,
1.56      paf       129:                        0,
1.41      paf       130:                        "bad expires time (seconds from epoch=%ld)", when);
1.49      paf       131: 
1.56      paf       132:        return *new VDate(when);
1.1       paf       133: }
                    134: 
1.25      parser    135: /*
1.49      paf       136:        @todo 
                    137:        http://www.netscape.com/newsref/std/cookie_spec.html
1.25      parser    138:        When sending cookies to a server, 
                    139:        all cookies with a more specific path mapping should be sent before cookies 
                    140:        with less specific path mappings. 
                    141:        For example, a cookie "name1=foo" with a path mapping of "/" should be sent after 
                    142:        a cookie "name1=foo2" with a path mapping of "/bar" if they are both to be sent. 
                    143: 
                    144:   There are limitations on the number of cookies that a client can store at any one time. 
                    145:   This is a specification of the minimum number of cookies that a client should be prepared 
                    146:   to receive and store. 
                    147:        300 total cookies 
                    148:        4 kilobytes per cookie, where the name and the OPAQUE_STRING combine 
                    149:                to form the 4 kilobyte limit. 
                    150:        20 cookies per server or domain. (note that completely specified hosts 
                    151:                and domains are treated as separate entities and have a 20 cookie limitation 
                    152:                for each, not combined) 
                    153: */
1.56      paf       154: static void output_set_cookie(
                    155:                              HashStringValue::key_type aattribute, 
                    156:                              HashStringValue::value_type ameaning,
                    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'
                    165:        if(ameaning) { // assigning value
                    166:                // Set-Cookie: (attribute)=(value); path=/
1.56      paf       167:                lmeaning=ameaning;
1.57    ! paf       168:                HashStringValue *hash;
        !           169:                if(hash=lmeaning->get_hash()) { // ...[hash value]
1.1       paf       170:                        // $expires
1.56      paf       171:                        if(Value* expires=hash->get(expires_name)) {
                    172:                                const String* string;
                    173:                                if(expires->is_string() && (string=expires->get_string()) && (*string==SESSION_NAME))  {
1.1       paf       174:                                        // $expires[session]
1.56      paf       175:                                        hash->remove(expires_name);
1.1       paf       176:                                } else {
1.56      paf       177:                                        if(Value* vdate=expires->as(VDATE_TYPE, false))
                    178:                                                hash->put(expires_name, vdate); // $expires[DATE]
                    179:                                        else if(double days_till_expire=expires->as_double())
                    180:                                                hash->put(expires_name, &expires_vdate(days_till_expire)); // $expires(days)
                    181:                                        else
                    182:                                                hash->remove(expires_name); // $expires(0)
1.1       paf       183:                                }
                    184:                        } else // $expires not assigned, defaulting
1.56      paf       185:                                hash->put(expires_name, &expires_vdate(DEFAULT_EXPIRES_DAYS));
1.1       paf       186:                } else { // ...[string value]
1.56      paf       187:                        Value* wrap_meaning=new VHash;
1.57    ! paf       188:                        hash=wrap_meaning->get_hash();
1.56      paf       189:                        // wrapping lmeaning into hash
1.57    ! paf       190:                        hash->put(value_name, lmeaning);
1.1       paf       191:                        // string = $expires not assigned, defaulting
1.57    ! paf       192:                        hash->put(expires_name, &expires_vdate(DEFAULT_EXPIRES_DAYS));
1.56      paf       193:                        // replacing lmeaning with hash-wrapped one
                    194:                        lmeaning=wrap_meaning;
1.1       paf       195:                }
                    196:        } else {// removing value
1.25      parser    197:                /*
                    198:                        http://www.netscape.com/newsref/std/cookie_spec.html
                    199:                        to delete a cookie, it can do so by returning a cookie with the same name, 
                    200:                        and an expires time which is in the past
                    201:                */
                    202: 
1.1       paf       203:                // Set-Cookie: (attribute)=; path=/
1.56      paf       204:                lmeaning=new VHash;
                    205:                lmeaning->get_hash()->put(expires_name, &expires_vdate(-DEFAULT_EXPIRES_DAYS));
1.1       paf       206:        }
                    207:        // defaulting path
1.56      paf       208:        if(!lmeaning->get_hash()->get(path_name))
                    209:                lmeaning->get_hash()->put(path_name, 
                    210:                        new VString(path_value_default));
1.1       paf       211: 
1.56      paf       212:        // append lmeaning
                    213:        string << attributed_meaning_to_string(*lmeaning, String::L_HTTP_HEADER, true);
1.1       paf       214: 
                    215:        // output
1.56      paf       216:        SAPI::add_header_attribute(sapi_info, "set-cookie", string.cstr(String::L_UNSPECIFIED));
1.1       paf       217: }
1.56      paf       218: static void output_after(
                    219:                                                 HashStringValue::key_type aattribute, 
                    220:                                                 HashStringValue::value_type ameaning,
                    221:                                                 SAPI_Info* sapi_info) {
                    222:        output_set_cookie(aattribute, ameaning, *sapi_info);
                    223: }
                    224: static void output_deleted(
                    225:                                                   HashStringValue::key_type aattribute, 
                    226:                                                   HashStringValue::value_type ameaning, 
                    227:                                                   SAPI_Info* sapi_info) {
                    228:        if(ameaning)
                    229:                output_set_cookie(aattribute, 0, *sapi_info);
                    230: }
                    231: void VCookie::output_result(SAPI_Info& sapi_info) {
                    232:        after.for_each(output_after, &sapi_info);
                    233:        deleted.for_each(output_deleted, &sapi_info);
1.1       paf       234: }

E-mail: