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

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.24    ! paf         8:        $Id: file.C,v 1.23 2001/04/09 14:01:54 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.22      paf       110: static void append_env_pair(const Hash::Key& key, Hash::Val *value, void *info) {
                    111:        Hash& hash=*static_cast<Hash *>(info);
                    112:        hash.put(key, &static_cast<Value *>(value)->as_string());
                    113: }
1.21      paf       114: /// ^exec[file-name]
                    115: /// ^exec[file-name;env hash]
                    116: /// ^exec[file-name;env hash;cmd;line;arg;s]
                    117: /// @test header to $fields. waits for header '\' tricks
                    118: static void _cgi(Request& r, const String& method_name, Array *params) {
                    119:        Pool& pool=r.pool();
                    120: 
                    121:        Value& vfile_name=*static_cast<Value *>(params->get(0));
                    122:        // forcing [this param type]
                    123:        r.fail_if_junction_(true, vfile_name, 
                    124:                method_name, "file name must not be code");
                    125: 
1.23      paf       126:        const String& script_name=r.absolute(vfile_name.as_string());
                    127: 
                    128:        Hash env(pool);
                    129:        #define PASS(key) \
                    130:                String key(pool); \
                    131:                if(const char *value=SAPI::get_env(pool, #key)) { \
                    132:                        key.APPEND_CONST(value); \
                    133:                        env.put(String(pool, #key), &key); \
                    134:                }
                    135:        #define INFO(key, value) \
                    136:                String value(pool); \
                    137:                if(r.info.value) { \
                    138:                        value.APPEND_CONST(r.info.value); \
                    139:                        env.put(String(pool, key), &value); \
                    140:                }
                    141: 
                    142:        // const
                    143:        String gateway_interface(pool, "CGI/1.1");
                    144:        env.put(String(pool, "GATEWAY_INTERFACE"), &gateway_interface);
                    145:        // from Request.info
                    146:        INFO("DOCUMENT_ROOT", document_root);
                    147:        INFO("PATH_TRANSLATED", path_translated);
                    148:        INFO("SERVER_PROTOCOL", method);
                    149:        INFO("QUERY_STRING", query_string);
                    150:        INFO("REQUEST_URI", uri);
                    151:        INFO("CONTENT_TYPE", content_type);
                    152:        char content_length_cstr[MAX_NUMBER];  
                    153:        snprintf(content_length_cstr, MAX_NUMBER, "%u", r.info.content_length);
                    154:        String content_length(pool, content_length_cstr);
                    155:        env.put(String(pool, "CONTENT_LENGTH"), &content_length);
                    156:        INFO("HTTP_COOKIE", cookie);
                    157:        INFO("HTTP_USER_AGENT", user_agent);
                    158:        // passing some SAPI:get_env-s
                    159:        PASS(SERVER_NAME);
                    160:        PASS(SERVER_PORT);
                    161:        PASS(HTTP_REFERER);
                    162:        PASS(REMOTE_ADDR);
                    163:        PASS(REMOTE_HOST);
                    164:        PASS(REMOTE_USER);
                    165:        // SCRIPT_NAME
                    166:        env.put(String(pool, "SCRIPT_NAME"), &script_name);
                    167: 
1.21      paf       168:        if(params->size()>1) {
                    169:                Value& venv=*static_cast<Value *>(params->get(1));
                    170:                // forcing [this param type]
                    171:                r.fail_if_junction_(true, venv, 
                    172:                        method_name, "env must not be code");
1.23      paf       173:                if(Hash *user_env=venv.get_hash())
                    174:                        user_env->for_each(append_env_pair, &env);
                    175:                else
1.21      paf       176:                        PTHROW(0, 0,
                    177:                                &method_name,
                    178:                                "env must be hash");
                    179:        }
                    180: 
                    181:        Array *argv=0;
                    182:        if(params->size()>2) {
                    183:                argv=new(pool) Array(pool, params->size()-2);
                    184:                for(int i=2; i<params->size(); i++)
                    185:                        *argv+=&static_cast<Value *>(params->get(i))->as_string();
                    186:        }
                    187: 
                    188:        const String in(pool, r.post_data, r.post_size);
                    189:        String out(pool);
                    190:        String err(pool);
1.24    ! paf       191:        int exit_code=pa_exec(script_name, &env, argv,
1.21      paf       192:                in, out, err);
                    193: 
                    194:        VFile& self=*static_cast<VFile *>(r.self);
                    195:        // construct with 'out' body and header
                    196:        int delim_size;
                    197:        int pos=out.pos("\n\n", delim_size=2);
                    198:        if(pos<0)
                    199:                pos=out.pos("\r\n\r\n", delim_size=4);
                    200:        if(pos<0) {
1.22      paf       201:                delim_size=0; // calm down, compiler
1.21      paf       202:                PTHROW(0, 0,
                    203:                        &method_name,
                    204:                        "output does not contain CGI header");
                    205:        }
                    206: 
                    207:        const String& header=out.mid(0, pos);
                    208:        const String& body=out.mid(pos+delim_size, out.size());
                    209: 
                    210:        // body
                    211:        self.set(false/*not tainted*/, body.cstr(String::UL_AS_IS), body.size());
                    212: 
                    213:        // todo header to $fields. waits for header '\' tricks
                    214: 
                    215:        // $exit-code
                    216:        self.fields().put(
                    217:                *new(pool) String(pool, "exit-code"),
                    218:                new(pool) VInt(pool, exit_code));
                    219:        
                    220:        // $stderr
                    221:        if(err.size()) {
                    222:                self.fields().put(
                    223:                        *new(pool) String(pool, "stderr"),
                    224:                        new(pool) VString(err));
                    225: 
                    226:                SAPI::log(pool, "cgi: %s", err.cstr());
                    227:        }
                    228: }
                    229: 
1.1       paf       230: // initialize
                    231: 
1.3       paf       232: void initialize_file_class(Pool& pool, VStateless_class& vclass) {
1.1       paf       233:        // ^save[file-name]
1.14      paf       234:        vclass.add_native_method("save", Method::CT_DYNAMIC, _save, 1, 1);
1.7       paf       235: 
                    236:        // ^delete[file-name]
1.14      paf       237:        vclass.add_native_method("delete", Method::CT_STATIC, _delete, 1, 1);
1.8       paf       238: 
                    239:        // ^find[file-name]
                    240:        // ^find[file-name]{when-not-found}
1.14      paf       241:        vclass.add_native_method("find", Method::CT_STATIC, _find, 1, 2);
1.9       paf       242: 
                    243:        // ^load[disk-name]
                    244:        // ^load[disk-name;user-name]
1.20      paf       245:        vclass.add_native_method("load", Method::CT_DYNAMIC, _load, 1, 2);
1.21      paf       246: 
                    247:        // ^exec[file-name]
                    248:        // ^exec[file-name;env hash]
                    249:        // ^exec[file-name;env hash;1cmd;2line;3ar;4g;5s]
                    250:        vclass.add_native_method("cgi", Method::CT_DYNAMIC, _cgi, 1, 2+5);
1.1       paf       251: }

E-mail: