Annotation of parser3/src/classes/file.C, revision 1.25

1.17      paf         1: /** @file
                      2:        Parser: @b file parser class.
                      3: 
1.1       paf         4:        Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
1.17      paf         5: 
1.1       paf         6:        Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
                      7: 
1.25    ! paf         8:        $Id: file.C,v 1.24 2001/04/09 16:04:45 paf Exp $
1.1       paf         9: */
                     10: 
                     11: #include "pa_request.h"
                     12: #include "_file.h"
                     13: #include "pa_vfile.h"
1.11      paf        14: #include "pa_table.h"
1.21      paf        15: #include "pa_vint.h"
1.24      paf        16: #include "pa_exec.h"
1.1       paf        17: 
1.9       paf        18: // consts
                     19: 
                     20: const int FIND_MONKEY_MAX_HOPS=10;
                     21: 
1.1       paf        22: // global var
                     23: 
1.3       paf        24: VStateless_class *file_class;
1.1       paf        25: 
                     26: // methods
                     27: 
                     28: static void _save(Request& r, const String& method_name, Array *params) {
1.4       paf        29:        Pool& pool=r.pool();
1.8       paf        30:        Value& vfile_name=*static_cast<Value *>(params->get(0));
1.1       paf        31:        // forcing
1.4       paf        32:        // ^save[this body type]
1.8       paf        33:        r.fail_if_junction_(true, vfile_name, 
1.19      paf        34:                method_name, "file name must not be code");
1.4       paf        35: 
1.7       paf        36:        // save
1.18      paf        37:        static_cast<VFile *>(r.self)->save(r.absolute(vfile_name.as_string()));
1.7       paf        38: }
                     39: 
                     40: static void _delete(Request& r, const String& method_name, Array *params) {
                     41:        Pool& pool=r.pool();
1.8       paf        42:        Value& vfile_name=*static_cast<Value *>(params->get(0));
1.7       paf        43:        // forcing
                     44:        // ^delete[this body type]
1.8       paf        45:        r.fail_if_junction_(true, vfile_name, 
1.19      paf        46:                method_name, "file name must not be code");
1.7       paf        47: 
                     48:        // unlink
1.18      paf        49:        file_delete(pool, r.absolute(vfile_name.as_string()));
1.1       paf        50: }
                     51: 
1.8       paf        52: static void _find(Request& r, const String& method_name, Array *params) {
                     53:        Pool& pool=r.pool();
                     54:        Value& vfile_name=*static_cast<Value *>(params->get(0));
                     55:        // forcing
                     56:        // ^delete[this body type]
                     57:        r.fail_if_junction_(true, vfile_name, 
1.19      paf        58:                method_name, "file name must not be code");
1.8       paf        59: 
1.18      paf        60:        const String &lfile_name=vfile_name.as_string();
1.8       paf        61: 
                     62:        // passed file name simply exists in current dir
                     63:        if(file_readable(r.absolute(lfile_name))) {
                     64:                r.write_no_lang(*new(pool) VString(lfile_name));
                     65:                return;
                     66:        }
                     67: 
                     68:        // scan .. dirs for result
1.9       paf        69:        for(int i=0; i<FIND_MONKEY_MAX_HOPS; i++) {
1.8       paf        70:                String test_name(pool);
                     71:                for(int j=0; j<i; j++)
                     72:                        test_name.APPEND_CONST("../");
1.15      paf        73:                test_name.append(lfile_name, String::UL_CLEAN);
1.8       paf        74:                if(file_readable(r.absolute(test_name))) {
                     75:                        r.write_no_lang(*new(pool) VString(*new(pool) String(test_name)));
                     76:                        return;
                     77:                }
                     78:        }
                     79: 
                     80:        // not found
                     81:        if(params->size()==2) {
                     82:                // forcing ..{this body type}
                     83:                Value& not_found_code=*static_cast<Value *>(params->get(1));
                     84:                r.fail_if_junction_(false, not_found_code, 
1.19      paf        85:                        method_name, "not-found param must be code");
1.8       paf        86:                r.write_pass_lang(r.process(not_found_code));
                     87:        }
                     88: }
                     89: 
1.9       paf        90: static void _load(Request& r, const String& method_name, Array *params) {
                     91:        Pool& pool=r.pool();
                     92:        Value& vfile_name=*static_cast<Value *>(params->get(0));
                     93: 
1.10      paf        94:        // forcing ^load[this body type]
1.9       paf        95:        r.fail_if_junction_(true, vfile_name, 
1.19      paf        96:                method_name, "file name must not be code");
1.9       paf        97: 
1.18      paf        98:        const String& lfile_name=vfile_name.as_string();
1.9       paf        99: 
1.12      paf       100:        void *data;  size_t size;
1.9       paf       101:        file_read(pool, r.absolute(lfile_name), data, size, false/*binary*/);
                    102: 
1.18      paf       103:        char *user_file_name=params->size()==1?lfile_name.cstr(String::UL_FILE_NAME)
1.9       paf       104:                :static_cast<Value *>(params->get(1))->as_string().cstr();
1.10      paf       105:        
1.21      paf       106:        static_cast<VFile *>(r.self)->set(true/*tainted*/, data, size, 
1.20      paf       107:                user_file_name, &r.mime_type_of(user_file_name));
1.9       paf       108: }
                    109: 
1.25    ! paf       110: static void _stat(Request& r, const String& method_name, Array *params) {
        !           111:        Pool& pool=r.pool();
        !           112:        Value& vfile_name=*static_cast<Value *>(params->get(0));
        !           113: 
        !           114:        // forcing ^load[this body type]
        !           115:        r.fail_if_junction_(true, vfile_name, 
        !           116:                method_name, "file name must not be code");
        !           117: 
        !           118:        const String& lfile_name=vfile_name.as_string();
        !           119: 
        !           120:        size_t size=file_size(r.absolute(lfile_name));
        !           121:        
        !           122:        static_cast<VFile *>(r.self)->set(true/*tainted*/, 0/*no bytes*/, size);
        !           123: }
        !           124: 
1.22      paf       125: static void append_env_pair(const Hash::Key& key, Hash::Val *value, void *info) {
                    126:        Hash& hash=*static_cast<Hash *>(info);
                    127:        hash.put(key, &static_cast<Value *>(value)->as_string());
                    128: }
1.21      paf       129: /// ^exec[file-name]
                    130: /// ^exec[file-name;env hash]
                    131: /// ^exec[file-name;env hash;cmd;line;arg;s]
                    132: /// @test header to $fields. waits for header '\' tricks
                    133: static void _cgi(Request& r, const String& method_name, Array *params) {
                    134:        Pool& pool=r.pool();
                    135: 
                    136:        Value& vfile_name=*static_cast<Value *>(params->get(0));
                    137:        // forcing [this param type]
                    138:        r.fail_if_junction_(true, vfile_name, 
                    139:                method_name, "file name must not be code");
                    140: 
1.23      paf       141:        const String& script_name=r.absolute(vfile_name.as_string());
                    142: 
                    143:        Hash env(pool);
                    144:        #define PASS(key) \
                    145:                String key(pool); \
                    146:                if(const char *value=SAPI::get_env(pool, #key)) { \
                    147:                        key.APPEND_CONST(value); \
                    148:                        env.put(String(pool, #key), &key); \
                    149:                }
                    150:        #define INFO(key, value) \
                    151:                String value(pool); \
                    152:                if(r.info.value) { \
                    153:                        value.APPEND_CONST(r.info.value); \
                    154:                        env.put(String(pool, key), &value); \
                    155:                }
                    156: 
                    157:        // const
                    158:        String gateway_interface(pool, "CGI/1.1");
                    159:        env.put(String(pool, "GATEWAY_INTERFACE"), &gateway_interface);
                    160:        // from Request.info
                    161:        INFO("DOCUMENT_ROOT", document_root);
                    162:        INFO("PATH_TRANSLATED", path_translated);
                    163:        INFO("SERVER_PROTOCOL", method);
                    164:        INFO("QUERY_STRING", query_string);
                    165:        INFO("REQUEST_URI", uri);
                    166:        INFO("CONTENT_TYPE", content_type);
                    167:        char content_length_cstr[MAX_NUMBER];  
                    168:        snprintf(content_length_cstr, MAX_NUMBER, "%u", r.info.content_length);
                    169:        String content_length(pool, content_length_cstr);
                    170:        env.put(String(pool, "CONTENT_LENGTH"), &content_length);
                    171:        INFO("HTTP_COOKIE", cookie);
                    172:        INFO("HTTP_USER_AGENT", user_agent);
                    173:        // passing some SAPI:get_env-s
                    174:        PASS(SERVER_NAME);
                    175:        PASS(SERVER_PORT);
                    176:        PASS(HTTP_REFERER);
                    177:        PASS(REMOTE_ADDR);
                    178:        PASS(REMOTE_HOST);
                    179:        PASS(REMOTE_USER);
                    180:        // SCRIPT_NAME
                    181:        env.put(String(pool, "SCRIPT_NAME"), &script_name);
                    182: 
1.21      paf       183:        if(params->size()>1) {
                    184:                Value& venv=*static_cast<Value *>(params->get(1));
                    185:                // forcing [this param type]
                    186:                r.fail_if_junction_(true, venv, 
                    187:                        method_name, "env must not be code");
1.23      paf       188:                if(Hash *user_env=venv.get_hash())
                    189:                        user_env->for_each(append_env_pair, &env);
                    190:                else
1.21      paf       191:                        PTHROW(0, 0,
                    192:                                &method_name,
                    193:                                "env must be hash");
                    194:        }
                    195: 
                    196:        Array *argv=0;
                    197:        if(params->size()>2) {
                    198:                argv=new(pool) Array(pool, params->size()-2);
                    199:                for(int i=2; i<params->size(); i++)
                    200:                        *argv+=&static_cast<Value *>(params->get(i))->as_string();
                    201:        }
                    202: 
                    203:        const String in(pool, r.post_data, r.post_size);
                    204:        String out(pool);
                    205:        String err(pool);
1.24      paf       206:        int exit_code=pa_exec(script_name, &env, argv,
1.21      paf       207:                in, out, err);
                    208: 
                    209:        VFile& self=*static_cast<VFile *>(r.self);
                    210:        // construct with 'out' body and header
                    211:        int delim_size;
                    212:        int pos=out.pos("\n\n", delim_size=2);
                    213:        if(pos<0)
                    214:                pos=out.pos("\r\n\r\n", delim_size=4);
                    215:        if(pos<0) {
1.22      paf       216:                delim_size=0; // calm down, compiler
1.21      paf       217:                PTHROW(0, 0,
                    218:                        &method_name,
                    219:                        "output does not contain CGI header");
                    220:        }
                    221: 
                    222:        const String& header=out.mid(0, pos);
                    223:        const String& body=out.mid(pos+delim_size, out.size());
                    224: 
                    225:        // body
                    226:        self.set(false/*not tainted*/, body.cstr(String::UL_AS_IS), body.size());
                    227: 
                    228:        // todo header to $fields. waits for header '\' tricks
                    229: 
                    230:        // $exit-code
                    231:        self.fields().put(
                    232:                *new(pool) String(pool, "exit-code"),
                    233:                new(pool) VInt(pool, exit_code));
                    234:        
                    235:        // $stderr
                    236:        if(err.size()) {
                    237:                self.fields().put(
                    238:                        *new(pool) String(pool, "stderr"),
                    239:                        new(pool) VString(err));
                    240: 
                    241:                SAPI::log(pool, "cgi: %s", err.cstr());
                    242:        }
                    243: }
                    244: 
1.1       paf       245: // initialize
                    246: 
1.3       paf       247: void initialize_file_class(Pool& pool, VStateless_class& vclass) {
1.1       paf       248:        // ^save[file-name]
1.14      paf       249:        vclass.add_native_method("save", Method::CT_DYNAMIC, _save, 1, 1);
1.7       paf       250: 
                    251:        // ^delete[file-name]
1.14      paf       252:        vclass.add_native_method("delete", Method::CT_STATIC, _delete, 1, 1);
1.8       paf       253: 
                    254:        // ^find[file-name]
                    255:        // ^find[file-name]{when-not-found}
1.14      paf       256:        vclass.add_native_method("find", Method::CT_STATIC, _find, 1, 2);
1.9       paf       257: 
                    258:        // ^load[disk-name]
                    259:        // ^load[disk-name;user-name]
1.20      paf       260:        vclass.add_native_method("load", Method::CT_DYNAMIC, _load, 1, 2);
1.25    ! paf       261: 
        !           262:        // ^stat[disk-name]
        !           263:        vclass.add_native_method("stat", Method::CT_DYNAMIC, _stat, 1, 1);
1.21      paf       264: 
                    265:        // ^exec[file-name]
                    266:        // ^exec[file-name;env hash]
                    267:        // ^exec[file-name;env hash;1cmd;2line;3ar;4g;5s]
                    268:        vclass.add_native_method("cgi", Method::CT_DYNAMIC, _cgi, 1, 2+5);
1.1       paf       269: }

E-mail: