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

1.6       paf         1: /** @file
                      2:        Parser: cookie class.
                      3: 
1.1       paf         4:        Copyright(c) 2001 ArtLebedev Group(http://www.artlebedev.com)
1.24      parser      5:        Author: Alexander Petrosyan <paf@design.ru>(http://design.ru/paf)
1.6       paf         6: 
1.25    ! parser      7:        $Id: pa_vcookie.C,v 1.24 2001/09/26 10:32:26 parser Exp $
1.1       paf         8: */
                      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"
                     14: #include "pa_request.h"
                     15: 
                     16: #define SESSION_NAME "session"
                     17: #define DEFAULT_EXPIRES_DAYS 90
1.9       paf        18: 
1.1       paf        19: // VCookie
                     20: 
                     21: Value *VCookie::get_element(const String& aname) {
                     22:        // $cookie
                     23:        if(deleted.get(aname)) // deleted?
                     24:                return 0;
                     25:        
1.2       paf        26:        if(Value *after_meaning=static_cast<Value *>(after.get(aname))) // assigned 'after'?
                     27:                if(Hash *hash=after_meaning->get_hash())
                     28:                        return static_cast<Value *>(hash->get(*value_name));
                     29:                else
                     30:                        return after_meaning;
1.1       paf        31:        
                     32:        // neither deleted nor assigned 
                     33:        // return any value it had 'before'
                     34:        return static_cast<Value *>(before.get(aname));
                     35: }
                     36: 
                     37: void VCookie::put_element(const String& aname, Value *avalue) {
                     38:        // $cookie
                     39:        bool remove;
                     40:        if(Hash *hash=avalue->get_hash())
                     41:                remove=hash->size()==0;
                     42:        else
                     43:                remove=avalue->as_string().size()==0;
                     44: 
                     45:        (remove?deleted:after).put(aname, avalue);
                     46:        (remove?after:deleted).put(aname, 0);
                     47: }
                     48: 
1.6       paf        49: //#include <stdio.h>
1.1       paf        50: void VCookie::fill_fields(Request& request) {
1.19      paf        51:        //request.info.cookie="test-session=value%3D5; test-default1=value%3D1; test-default2=value%3D2; test-tomorrow=value%3D3";
1.20      paf        52:        //request.info.cookie="\"вот\"=\"прислал \\\"browser\\\"\"";
1.1       paf        53:        if(!request.info.cookie)
                     54:                return;
                     55: /*
                     56:        FILE *f=fopen("c:\\temp\\a", "wt");
                     57:        fprintf(f, "cookie=%s", request.info.cookie);
                     58:        fclose(f);*/
                     59:        char *cookies=(char *)malloc(strlen(request.info.cookie)+1);
                     60:        strcpy(cookies, request.info.cookie);
                     61:     char *current=cookies;
1.22      parser     62:        uint line=0;
1.1       paf        63:     do {
1.19      paf        64:                if(char *attribute=unquote(current, '='))
                     65:                        if(char *meaning=unquote(current, ';')) {
1.22      parser     66:                                String& sattribute=*NEW String(pool());
                     67:                                String& smeaning=*NEW String(pool());
                     68:                                sattribute.APPEND_TAINTED(attribute, 0, "cookie_name", line);
                     69:                                smeaning.APPEND_TAINTED(meaning, 0, "cookie_value", line);
1.19      paf        70:                                before.put(sattribute, NEW VString(smeaning));
1.22      parser     71:                                line++;
1.19      paf        72:                        }
1.1       paf        73:        } while(current);
                     74: }
                     75: 
                     76: static VString *expires_timestamp(Pool& pool, double days_till_expire) {
                     77:     const char month_names[12][4]={
                     78:                "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
                     79:     const char days[7][4]={
                     80:                "Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
                     81:        
                     82:        time_t when=time(NULL)+(time_t)(60*60*24*days_till_expire);
                     83:        struct tm *tms=gmtime(&when);
                     84:        char *buf=(char *)pool.malloc(MAX_STRING);
                     85:        snprintf(buf, MAX_STRING, "%s, %.2d-%s-%.4d %.2d:%.2d:%.2d GMT", 
                     86:                days[tms->tm_wday],
                     87:                tms->tm_mday,month_names[tms->tm_mon],tms->tm_year+1900,
                     88:                tms->tm_hour,tms->tm_min,tms->tm_sec);
                     89:        return new(pool) VString(*new(pool) String(pool, buf));
                     90: }
                     91: 
1.25    ! parser     92: /*
        !            93:        @test http://www.netscape.com/newsref/std/cookie_spec.html
        !            94:        When sending cookies to a server, 
        !            95:        all cookies with a more specific path mapping should be sent before cookies 
        !            96:        with less specific path mappings. 
        !            97:        For example, a cookie "name1=foo" with a path mapping of "/" should be sent after 
        !            98:        a cookie "name1=foo2" with a path mapping of "/bar" if they are both to be sent. 
        !            99: 
        !           100:   There are limitations on the number of cookies that a client can store at any one time. 
        !           101:   This is a specification of the minimum number of cookies that a client should be prepared 
        !           102:   to receive and store. 
        !           103:        300 total cookies 
        !           104:        4 kilobytes per cookie, where the name and the OPAQUE_STRING combine 
        !           105:                to form the 4 kilobyte limit. 
        !           106:        20 cookies per server or domain. (note that completely specified hosts 
        !           107:                and domains are treated as separate entities and have a 20 cookie limitation 
        !           108:                for each, not combined) 
        !           109: */
1.4       paf       110: static void output_set_cookie(const Hash::Key& aattribute, Hash::Val *ameaning) {
1.1       paf       111:        Pool& pool=aattribute.pool();
                    112:        String string(pool);
                    113:        // attribute
1.16      paf       114:        string.append(aattribute, String::UL_HTTP_HEADER, true);
1.1       paf       115:        // attribute=
1.19      paf       116:        string << "=";
1.1       paf       117:        Value *meaning;
                    118:        // figure out 'meaning'
                    119:        if(ameaning) { // assigning value
                    120:                // Set-Cookie: (attribute)=(value); path=/
                    121:                meaning=static_cast<Value *>(ameaning);
                    122:                if(Hash *hash=meaning->get_hash()) { // ...[hash value]
                    123:                        // $expires
                    124:                        if(Value *expires=static_cast<Value *>(hash->get(*expires_name))) {
                    125:                                const String *string;
                    126:                                if((string=expires->get_string()) && (*string==SESSION_NAME))  {
                    127:                                        // $expires[session]
                    128:                                        hash->put(*expires_name, 0);
                    129:                                } else {
                    130:                                        // $expires(days)
                    131:                                        hash->put(*expires_name, 
1.14      paf       132:                                                expires_timestamp(pool, expires->as_double()));
1.1       paf       133:                                }
                    134:                        } else // $expires not assigned, defaulting
                    135:                                hash->put(*expires_name, expires_timestamp(pool, DEFAULT_EXPIRES_DAYS));
                    136:                } else { // ...[string value]
                    137:                        Value *wrap_meaning=new(pool) VHash(pool);
                    138:                        // wrapping meaning into hash
                    139:                        wrap_meaning->get_hash()->put(*value_name, meaning);
                    140:                        // string = $expires not assigned, defaulting
                    141:                        wrap_meaning->get_hash()->put(*expires_name, 
                    142:                                expires_timestamp(pool, DEFAULT_EXPIRES_DAYS));
                    143:                        // replacing meaning with hash-wrapped one
                    144:                        meaning=wrap_meaning;
                    145:                }
                    146:        } else {// removing value
1.25    ! parser    147:                /*
        !           148:                        http://www.netscape.com/newsref/std/cookie_spec.html
        !           149:                        to delete a cookie, it can do so by returning a cookie with the same name, 
        !           150:                        and an expires time which is in the past
        !           151:                */
        !           152: 
1.1       paf       153:                // Set-Cookie: (attribute)=; path=/
                    154:                meaning=new(pool) VHash(pool);
1.25    ! parser    155:                wrap_meaning->get_hash()->put(*expires_name, 
        !           156:                        expires_timestamp(pool, -DEFAULT_EXPIRES_DAYS));
1.1       paf       157:        }
                    158:        // defaulting path
                    159:        if(!meaning->get_hash()->get(*path_name))
                    160:                meaning->get_hash()->put(*path_name, 
                    161:                        new(pool) VString(*new(pool) String(pool, "/")));
                    162: 
                    163:        // append meaning
1.19      paf       164:        string << attributed_meaning_to_string(*meaning, String::UL_HTTP_HEADER);
1.1       paf       165: 
                    166:        // output
1.19      paf       167:        SAPI::add_header_attribute(pool, "set-cookie", string.cstr());
1.1       paf       168: }
1.4       paf       169: static void output_after(const Hash::Key& aattribute, Hash::Val *ameaning, void *) {
1.1       paf       170:        output_set_cookie(aattribute, ameaning);
                    171: }
1.4       paf       172: static void output_deleted(const Hash::Key& aattribute, Hash::Val *ameaning, void *) {
1.1       paf       173:        output_set_cookie(aattribute, 0);
                    174: }
                    175: void VCookie::output_result() {
1.12      paf       176:        after.for_each(output_after, this);
                    177:        deleted.for_each(output_deleted, this);
1.1       paf       178: }

E-mail: