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

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

E-mail: