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

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

E-mail: