Annotation of parser3/src/types/pa_vcookie.C, revision 1.1
1.1 ! paf 1: /*
! 2: Parser
! 3: Copyright(c) 2001 ArtLebedev Group(http://www.artlebedev.com)
! 4: Author: Alexander Petrosyan <paf@design.ru>(http://design.ru/paf)
! 5:
! 6: $Id: pa_vcookie.C,v 1.5 2001/03/18 13:38:49 paf Exp $
! 7: */
! 8:
! 9: #include <string.h>
! 10: #include <time.h>
! 11:
! 12: #include "pa_vcookie.h"
! 13: #include "pa_vstring.h"
! 14: #include "pa_common.h"
! 15: #include "pa_request.h"
! 16: #include "pa_common.h"
! 17:
! 18: #define SESSION_NAME "session"
! 19: #define DEFAULT_EXPIRES_DAYS 90
! 20:
! 21: // VCookie
! 22:
! 23: Value *VCookie::get_element(const String& aname) {
! 24: // $CLASS,$BASE,$method
! 25: if(Value *result=VStateless_class::get_element(aname))
! 26: return result;
! 27:
! 28: // $cookie
! 29:
! 30: if(deleted.get(aname)) // deleted?
! 31: return 0;
! 32:
! 33: if(Value *after_value=static_cast<Value *>(after.get(aname))) // assigned 'after'?
! 34: return after_value;
! 35:
! 36: // neither deleted nor assigned
! 37: // return any value it had 'before'
! 38: return static_cast<Value *>(before.get(aname));
! 39: }
! 40:
! 41: void VCookie::put_element(const String& aname, Value *avalue) {
! 42: // $cookie
! 43: bool remove;
! 44: if(Hash *hash=avalue->get_hash())
! 45: remove=hash->size()==0;
! 46: else
! 47: remove=avalue->as_string().size()==0;
! 48:
! 49: (remove?deleted:after).put(aname, avalue);
! 50: (remove?after:deleted).put(aname, 0);
! 51: }
! 52:
! 53: #include <stdio.h>
! 54: void VCookie::fill_fields(Request& request) {
! 55: // request.info.cookie="test-session=value%3D5; test-default1=value%3D1; test-default2=value%3D2; test-tomorrow=value%3D3";
! 56: if(!request.info.cookie)
! 57: return;
! 58: /*
! 59: FILE *f=fopen("c:\\temp\\a", "wt");
! 60: fprintf(f, "cookie=%s", request.info.cookie);
! 61: fclose(f);*/
! 62: char *cookies=(char *)malloc(strlen(request.info.cookie)+1);
! 63: strcpy(cookies, request.info.cookie);
! 64: char *current=cookies;
! 65: do {
! 66: char *value=lsplit(¤t,';');
! 67: char *fname=lsplit(&value, '=');
! 68: if(value) {
! 69: while(*fname==' ')
! 70: fname++;
! 71: rsplit(value,' ');
! 72: String& string=*NEW String(pool(),
! 73: unescape_chars(pool(), value, strlen(value)), true);
! 74: before.put(String(pool(), fname), NEW VString(string));
! 75: }
! 76: } while(current);
! 77: }
! 78:
! 79: static VString *expires_timestamp(Pool& pool, double days_till_expire) {
! 80: const char month_names[12][4]={
! 81: "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
! 82: const char days[7][4]={
! 83: "Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
! 84:
! 85: time_t when=time(NULL)+(time_t)(60*60*24*days_till_expire);
! 86: struct tm *tms=gmtime(&when);
! 87: char *buf=(char *)pool.malloc(MAX_STRING);
! 88: snprintf(buf, MAX_STRING, "%s, %.2d-%s-%.4d %.2d:%.2d:%.2d GMT",
! 89: days[tms->tm_wday],
! 90: tms->tm_mday,month_names[tms->tm_mon],tms->tm_year+1900,
! 91: tms->tm_hour,tms->tm_min,tms->tm_sec);
! 92: return new(pool) VString(*new(pool) String(pool, buf));
! 93: }
! 94:
! 95: static void output_set_cookie(const Hash::Key& aattribute, Hash::Value *ameaning) {
! 96: Pool& pool=aattribute.pool();
! 97: String string(pool);
! 98: // attribute
! 99: string.append(aattribute, String::Untaint_lang::HEADER, true);
! 100: // attribute=
! 101: string.APPEND_CONST("=");
! 102: Value *meaning;
! 103: // figure out 'meaning'
! 104: if(ameaning) { // assigning value
! 105: // Set-Cookie: (attribute)=(value); path=/
! 106: meaning=static_cast<Value *>(ameaning);
! 107: if(Hash *hash=meaning->get_hash()) { // ...[hash value]
! 108: // $expires
! 109: if(Value *expires=static_cast<Value *>(hash->get(*expires_name))) {
! 110: const String *string;
! 111: if((string=expires->get_string()) && (*string==SESSION_NAME)) {
! 112: // $expires[session]
! 113: hash->put(*expires_name, 0);
! 114: } else {
! 115: // $expires(days)
! 116: hash->put(*expires_name,
! 117: expires_timestamp(pool, expires->get_double()));
! 118: }
! 119: } else // $expires not assigned, defaulting
! 120: hash->put(*expires_name, expires_timestamp(pool, DEFAULT_EXPIRES_DAYS));
! 121: } else { // ...[string value]
! 122: Value *wrap_meaning=new(pool) VHash(pool);
! 123: // wrapping meaning into hash
! 124: wrap_meaning->get_hash()->put(*value_name, meaning);
! 125: // string = $expires not assigned, defaulting
! 126: wrap_meaning->get_hash()->put(*expires_name,
! 127: expires_timestamp(pool, DEFAULT_EXPIRES_DAYS));
! 128: // replacing meaning with hash-wrapped one
! 129: meaning=wrap_meaning;
! 130: }
! 131: } else {// removing value
! 132: // Set-Cookie: (attribute)=; path=/
! 133: meaning=new(pool) VHash(pool);
! 134: }
! 135: // defaulting path
! 136: if(!meaning->get_hash()->get(*path_name))
! 137: meaning->get_hash()->put(*path_name,
! 138: new(pool) VString(*new(pool) String(pool, "/")));
! 139:
! 140: // append meaning
! 141: string.append(attributed_meaning_string(meaning),
! 142: String::Untaint_lang::PASS_APPENDED);
! 143:
! 144: // output
! 145: (*service_funcs.output_header_attribute)(
! 146: "set-cookie",
! 147: string.cstr());
! 148: }
! 149: static void output_after(const Hash::Key& aattribute, Hash::Value *ameaning, void *) {
! 150: output_set_cookie(aattribute, ameaning);
! 151: }
! 152: static void output_deleted(const Hash::Key& aattribute, Hash::Value *ameaning, void *) {
! 153: output_set_cookie(aattribute, 0);
! 154: }
! 155: void VCookie::output_result() {
! 156: after.foreach(output_after, this);
! 157: deleted.foreach(output_deleted, this);
! 158: }
E-mail: