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

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.47    ! parser      8:        $Id: file.C,v 1.46 2001/08/06 12:23:11 parser Exp $
1.1       paf         9: */
1.47    ! parser     10: static const char *RCSId="$Id: file.C,v 1.46 2001/08/06 12:23:11 parser Exp $"; 
        !            11: 
        !            12: #include "pa_config_includes.h"
        !            13: 
        !            14: #include "pcre.h"
1.1       paf        15: 
1.35      paf        16: #include "classes.h"
1.1       paf        17: #include "pa_request.h"
                     18: #include "pa_vfile.h"
1.11      paf        19: #include "pa_table.h"
1.21      paf        20: #include "pa_vint.h"
1.24      paf        21: #include "pa_exec.h"
1.40      parser     22: #include "pa_vdate.h"
1.47    ! parser     23: #include "pa_dir.h"
        !            24: #include "pa_vtable.h"
1.1       paf        25: 
1.33      paf        26: // consts
                     27: 
1.32      paf        28: // defines
                     29: 
                     30: #define FILE_CLASS_NAME "file"
                     31: 
                     32: // class
                     33: 
                     34: class MFile : public Methoded {
                     35: public: // VStateless_class
                     36:        
                     37:        Value *create_new_value(Pool& pool) { return new(pool) VFile(pool); }
                     38: 
1.33      paf        39: public: // Methoded
                     40:        bool used_directly() { return true; }
                     41: 
1.32      paf        42: public:
                     43:        MFile(Pool& pool);
1.33      paf        44: 
1.32      paf        45: };
                     46: 
1.9       paf        47: // consts
                     48: 
                     49: const int FIND_MONKEY_MAX_HOPS=10;
                     50: 
1.1       paf        51: // methods
                     52: 
1.26      paf        53: static void _save(Request& r, const String&, MethodParams *params) {
1.39      parser     54:        Value& vfile_name=params->as_no_junction(0, "file name must not be code");
1.4       paf        55: 
1.7       paf        56:        // save
1.18      paf        57:        static_cast<VFile *>(r.self)->save(r.absolute(vfile_name.as_string()));
1.7       paf        58: }
                     59: 
1.26      paf        60: static void _delete(Request& r, const String&, MethodParams *params) {
1.7       paf        61:        Pool& pool=r.pool();
1.39      parser     62:        Value& vfile_name=params->as_no_junction(0, "file name must not be code");
1.7       paf        63: 
                     64:        // unlink
1.18      paf        65:        file_delete(pool, r.absolute(vfile_name.as_string()));
1.1       paf        66: }
                     67: 
1.45      parser     68: static void _move(Request& r, const String&, MethodParams *params) {
                     69:        Pool& pool=r.pool();
                     70:        Value& vfrom_file_name=params->as_no_junction(0, "from file name must not be code");
                     71:        Value& vto_file_name=params->as_no_junction(1, "to file name must not be code");
                     72: 
                     73:        // unlink
                     74:        file_move(pool, 
                     75:                r.absolute(vfrom_file_name.as_string()),
                     76:                r.absolute(vto_file_name.as_string()));
                     77: }
                     78: 
1.26      paf        79: static void _find(Request& r, const String& method_name, MethodParams *params) {
1.8       paf        80:        Pool& pool=r.pool();
1.39      parser     81:        Value& vfile_name=params->as_no_junction(0, "file name must not be code");
1.8       paf        82: 
1.18      paf        83:        const String &lfile_name=vfile_name.as_string();
1.8       paf        84: 
                     85:        // passed file name simply exists in current dir
                     86:        if(file_readable(r.absolute(lfile_name))) {
                     87:                r.write_no_lang(*new(pool) VString(lfile_name));
                     88:                return;
                     89:        }
                     90: 
                     91:        // scan .. dirs for result
1.9       paf        92:        for(int i=0; i<FIND_MONKEY_MAX_HOPS; i++) {
1.8       paf        93:                String test_name(pool);
                     94:                for(int j=0; j<i; j++)
                     95:                        test_name.APPEND_CONST("../");
1.15      paf        96:                test_name.append(lfile_name, String::UL_CLEAN);
1.8       paf        97:                if(file_readable(r.absolute(test_name))) {
                     98:                        r.write_no_lang(*new(pool) VString(*new(pool) String(test_name)));
                     99:                        return;
                    100:                }
                    101:        }
                    102: 
                    103:        // not found
                    104:        if(params->size()==2) {
1.39      parser    105:                Value& not_found_code=params->as_junction(1, "not-found param must be code");
1.8       paf       106:                r.write_pass_lang(r.process(not_found_code));
                    107:        }
                    108: }
                    109: 
1.26      paf       110: static void _load(Request& r, const String& method_name, MethodParams *params) {
1.9       paf       111:        Pool& pool=r.pool();
1.39      parser    112:        Value& vfile_name=params->as_no_junction(0, "file name must not be code");
1.9       paf       113: 
1.18      paf       114:        const String& lfile_name=vfile_name.as_string();
1.9       paf       115: 
1.12      paf       116:        void *data;  size_t size;
1.9       paf       117:        file_read(pool, r.absolute(lfile_name), data, size, false/*binary*/);
                    118: 
1.18      paf       119:        char *user_file_name=params->size()==1?lfile_name.cstr(String::UL_FILE_NAME)
1.26      paf       120:                :params->get(1).as_string().cstr();
1.10      paf       121:        
1.21      paf       122:        static_cast<VFile *>(r.self)->set(true/*tainted*/, data, size, 
1.20      paf       123:                user_file_name, &r.mime_type_of(user_file_name));
1.9       paf       124: }
                    125: 
1.26      paf       126: static void _stat(Request& r, const String& method_name, MethodParams *params) {
1.25      paf       127:        Pool& pool=r.pool();
1.39      parser    128:        Value& vfile_name=params->as_no_junction(0, "file name must not be code");
1.25      paf       129: 
                    130:        const String& lfile_name=vfile_name.as_string();
                    131: 
1.40      parser    132:        size_t size;
                    133:        time_t atime, mtime, ctime;
                    134:        file_stat(r.absolute(lfile_name),
                    135:                size,
                    136:                atime, mtime, ctime);
1.25      paf       137:        
1.40      parser    138:        VFile& vfile=*static_cast<VFile *>(r.self);
                    139:        vfile.set(true/*tainted*/, 0/*no bytes*/, size);
                    140:        Hash& ff=vfile.fields();
                    141:        ff.put(*new(pool) String(pool, "adate"), new(pool) VDate(pool, atime));
                    142:        ff.put(*new(pool) String(pool, "mdate"), new(pool) VDate(pool, mtime));
                    143:        ff.put(*new(pool) String(pool, "cdate"), new(pool) VDate(pool, ctime));
1.25      paf       144: }
                    145: 
1.22      paf       146: static void append_env_pair(const Hash::Key& key, Hash::Val *value, void *info) {
                    147:        Hash& hash=*static_cast<Hash *>(info);
                    148:        hash.put(key, &static_cast<Value *>(value)->as_string());
                    149: }
1.29      paf       150: 
                    151: static void pass_cgi_header_attribute(Array::Item *value, void *info) {
                    152:        String& string=*static_cast<String *>(value);
                    153:        Hash& hash=*static_cast<Hash *>(info);
                    154:        int colon_pos=string.pos(":", 1);
                    155:        if(colon_pos>0)
1.30      paf       156:                hash.put(string.mid(0, colon_pos), 
                    157:                new(string.pool()) VString(string.mid(colon_pos+1, string.size())));
1.29      paf       158: }
1.36      paf       159: /// @todo fix `` in perl - they produced flipping consoles and no output to perl
1.41      parser    160: static void _exec_cgi(Request& r, const String& method_name, MethodParams *params,
                    161:                                          bool cgi) {
1.21      paf       162:        Pool& pool=r.pool();
                    163: 
1.39      parser    164:        Value& vfile_name=params->as_no_junction(0, "file name must not be code");
1.21      paf       165: 
1.23      paf       166:        const String& script_name=r.absolute(vfile_name.as_string());
                    167: 
                    168:        Hash env(pool);
                    169:        #define PASS(key) \
                    170:                String key(pool); \
                    171:                if(const char *value=SAPI::get_env(pool, #key)) { \
                    172:                        key.APPEND_CONST(value); \
                    173:                        env.put(String(pool, #key), &key); \
                    174:                }
                    175:        #define INFO(key, value) \
                    176:                String value(pool); \
                    177:                if(r.info.value) { \
                    178:                        value.APPEND_CONST(r.info.value); \
                    179:                        env.put(String(pool, key), &value); \
                    180:                }
                    181: 
                    182:        // const
                    183:        String gateway_interface(pool, "CGI/1.1");
                    184:        env.put(String(pool, "GATEWAY_INTERFACE"), &gateway_interface);
                    185:        // from Request.info
                    186:        INFO("DOCUMENT_ROOT", document_root);
                    187:        INFO("PATH_TRANSLATED", path_translated);
                    188:        INFO("SERVER_PROTOCOL", method);
                    189:        INFO("QUERY_STRING", query_string);
                    190:        INFO("REQUEST_URI", uri);
                    191:        INFO("CONTENT_TYPE", content_type);
                    192:        char content_length_cstr[MAX_NUMBER];  
                    193:        snprintf(content_length_cstr, MAX_NUMBER, "%u", r.info.content_length);
                    194:        String content_length(pool, content_length_cstr);
                    195:        env.put(String(pool, "CONTENT_LENGTH"), &content_length);
                    196:        INFO("HTTP_COOKIE", cookie);
                    197:        INFO("HTTP_USER_AGENT", user_agent);
                    198:        // passing some SAPI:get_env-s
                    199:        PASS(SERVER_NAME);
                    200:        PASS(SERVER_PORT);
                    201:        PASS(HTTP_REFERER);
                    202:        PASS(REMOTE_ADDR);
                    203:        PASS(REMOTE_HOST);
                    204:        PASS(REMOTE_USER);
                    205:        // SCRIPT_NAME
                    206:        env.put(String(pool, "SCRIPT_NAME"), &script_name);
1.27      paf       207: #ifdef WIN32
                    208:        // WIN32 shell
                    209:        PASS(COMSPEC);
                    210: #endif
1.23      paf       211: 
1.21      paf       212:        if(params->size()>1) {
1.39      parser    213:                Value& venv=params->as_no_junction(1, "env must not be code");
1.23      paf       214:                if(Hash *user_env=venv.get_hash())
                    215:                        user_env->for_each(append_env_pair, &env);
1.21      paf       216:        }
                    217: 
                    218:        Array *argv=0;
                    219:        if(params->size()>2) {
                    220:                argv=new(pool) Array(pool, params->size()-2);
                    221:                for(int i=2; i<params->size(); i++)
1.26      paf       222:                        *argv+=&params->get(i).as_string();
1.21      paf       223:        }
                    224: 
                    225:        const String in(pool, r.post_data, r.post_size);
                    226:        String out(pool);
1.31      paf       227:        //out.APPEND_CONST("content-type:text/plain\nheader:test-header\n\ntest-body");
                    228:        //out<<in;
1.27      paf       229:        String& err=*new(pool) String(pool);
1.42      parser    230:        int status=pa_exec(script_name, &env, argv, in, out, err);
1.21      paf       231: 
                    232:        VFile& self=*static_cast<VFile *>(r.self);
                    233: 
1.41      parser    234:        const String *body=&out; // ^file:exec
                    235:        if(cgi) { // ^file:cgi
                    236:                // construct with 'out' body and header
                    237:                int delim_size;
                    238:                const char *eol_marker="\r\n"; size_t eol_marker_size=2;
                    239:                int pos=out.pos("\r\n\r\n", delim_size=4);
                    240:                if(pos<0) {
                    241:                        eol_marker="\n"; eol_marker_size=1;
                    242:                        pos=out.pos("\n\n", delim_size=2);
                    243:                }
                    244:                if(pos<0) {
                    245:                        delim_size=0; // calm down, compiler
                    246:                        PTHROW(0, 0,
                    247:                                &method_name,
1.46      parser    248:                                "output does not contain CGI header; exit code=%d; outsize=%u; out: \"%s\"; errsize=%u; err: \"%s\"", 
                    249:                                        status, 
                    250:                                        (uint)out.size(), out.cstr(),
                    251:                                        (uint)err.size(), err.cstr());
1.41      parser    252:                }
1.21      paf       253: 
1.41      parser    254:                const String& header=out.mid(0, pos);
                    255:                body=&out.mid(pos+delim_size, out.size());
1.30      paf       256: 
1.41      parser    257:                // header to $fields
                    258:                {
                    259:                        Array rows(pool);
                    260:                        header.split(rows, 0, eol_marker, eol_marker_size, String::UL_CLEAN);
                    261:                        rows.for_each(pass_cgi_header_attribute, &self.fields());
                    262:                }
1.29      paf       263:        }
1.41      parser    264:        // body
                    265:        self.set(false/*not tainted*/, body->cstr(String::UL_AS_IS), body->size());
1.21      paf       266: 
1.42      parser    267:        // $status
1.21      paf       268:        self.fields().put(
1.42      parser    269:                *new(pool) String(pool, "status"),
                    270:                new(pool) VInt(pool, status));
1.21      paf       271:        
                    272:        // $stderr
                    273:        if(err.size()) {
                    274:                self.fields().put(
                    275:                        *new(pool) String(pool, "stderr"),
                    276:                        new(pool) VString(err));
                    277: 
1.44      parser    278:                SAPI::log(pool, "file:%s: %s", cgi?"cgi":"exec", err.cstr());
1.21      paf       279:        }
                    280: }
1.41      parser    281: static void _exec(Request& r, const String& method_name, MethodParams *params) {
                    282:        _exec_cgi(r, method_name, params, false);
                    283: }
                    284: static void _cgi(Request& r, const String& method_name, MethodParams *params) {
                    285:        _exec_cgi(r, method_name, params, true);
                    286: }
                    287: 
1.47    ! parser    288: static void _list(Request& r, const String& method_name, MethodParams *params) {
        !           289:        Pool& pool=r.pool();
        !           290: 
        !           291:        Value& relative_path=params->as_no_junction(0, "path must not be code");
        !           292: 
        !           293:        const String *regexp;
        !           294:        pcre *regexp_code;
        !           295:        int ovecsize;
        !           296:        int *ovector;
        !           297:        if(params->size()>1) {
        !           298:                regexp=&params->as_no_junction(1, "regexp must not be code").as_string();
        !           299: 
        !           300:                const char *pattern=regexp->cstr(String::UL_AS_IS);
        !           301:                const char *errptr;
        !           302:                int erroffset;
        !           303:                regexp_code=pcre_compile(pattern, PCRE_EXTRA | PCRE_DOTALL, 
        !           304:                        &errptr, &erroffset, 
        !           305:                        r.pcre_tables);
        !           306: 
        !           307:                if(!regexp_code)
        !           308:                        PTHROW(0, 0, 
        !           309:                                &regexp->mid(erroffset, regexp->size()), 
        !           310:                                "regular expression syntax error - %s", errptr);
        !           311: 
        !           312:                ovector=(int *)malloc(sizeof(int)*(ovecsize=(1/*match*/)*3));
        !           313:        } else 
        !           314:                regexp_code=0;
        !           315: 
        !           316: 
        !           317:        const char* absolute_path_cstr=r.absolute(relative_path.as_string())
        !           318:                .cstr(String::UL_FILE_NAME);
        !           319: 
        !           320:        Array& columns=*new(pool) Array(pool);
        !           321:        columns+=new(pool) String(pool, "name");        
        !           322:        Table& table=*new(pool) Table(pool, &method_name, &columns);
        !           323: 
        !           324:        LOAD_DIR(absolute_path_cstr, 
        !           325:                size_t file_name_size=strlen(ffblk.ff_name);
        !           326:                bool suits=true;
        !           327:                if(regexp_code) {
        !           328:                        int exec_result=pcre_exec(regexp_code, 0, 
        !           329:                                ffblk.ff_name, file_name_size, 0, 
        !           330:                                0, ovector, ovecsize);
        !           331:                        
        !           332:                        if(exec_result==PCRE_ERROR_NOMATCH)
        !           333:                                suits=false;
        !           334:                        else if(exec_result<0) {
        !           335:                                (*pcre_free)(regexp_code);
        !           336:                                PTHROW(0, 0, 
        !           337:                                        regexp, 
        !           338:                                        "regular expression execute (%d)", 
        !           339:                                                exec_result);
        !           340:                        }
        !           341:                }
        !           342: 
        !           343:                if(suits) {
        !           344:                        char *file_name_cstr=(char *)r.malloc(file_name_size);
        !           345:                        memcpy(file_name_cstr, ffblk.ff_name, file_name_size);
        !           346:                        String &file_name=*new(pool) String(pool);
        !           347:                        file_name.APPEND(file_name_cstr, file_name_size, String::UL_FILE_NAME, 
        !           348:                                method_name.origin().file, method_name.origin().line);
        !           349:                
        !           350:                        Array& row=*new(pool) Array(pool);
        !           351:                        row+=&file_name;
        !           352:                        table+=&row;
        !           353:                }
        !           354:        );
        !           355: 
        !           356:        if(regexp_code)
        !           357:                (*pcre_free)(regexp_code);
        !           358: 
        !           359:        VTable& result=*new(pool) VTable(pool, &table);
        !           360:        result.set_name(method_name);
        !           361:        r.write_no_lang(result);
        !           362: }
1.21      paf       363: 
1.32      paf       364: // constructor
                    365: 
                    366: MFile::MFile(Pool& apool) : Methoded(apool) {
                    367:        set_name(*NEW String(pool(), FILE_CLASS_NAME));
                    368: 
1.1       paf       369: 
                    370:        // ^save[file-name]
1.32      paf       371:        add_native_method("save", Method::CT_DYNAMIC, _save, 1, 1);
1.7       paf       372: 
                    373:        // ^delete[file-name]
1.32      paf       374:        add_native_method("delete", Method::CT_STATIC, _delete, 1, 1);
1.45      parser    375: 
                    376:        // ^move[from-file-name;to-file-name]
                    377:        add_native_method("move", Method::CT_STATIC, _move, 2, 2);
1.8       paf       378: 
                    379:        // ^find[file-name]
                    380:        // ^find[file-name]{when-not-found}
1.32      paf       381:        add_native_method("find", Method::CT_STATIC, _find, 1, 2);
1.9       paf       382: 
                    383:        // ^load[disk-name]
                    384:        // ^load[disk-name;user-name]
1.32      paf       385:        add_native_method("load", Method::CT_DYNAMIC, _load, 1, 2);
1.25      paf       386: 
                    387:        // ^stat[disk-name]
1.32      paf       388:        add_native_method("stat", Method::CT_DYNAMIC, _stat, 1, 1);
1.21      paf       389: 
1.36      paf       390:        // ^cgi[file-name]
                    391:        // ^cgi[file-name;env hash]
                    392:        // ^cgi[file-name;env hash;1cmd;2line;3ar;4g;5s]
1.41      parser    393:        add_native_method("cgi", Method::CT_DYNAMIC, _cgi, 1, 2+10);
                    394: 
                    395:        // ^exec[file-name]
                    396:        // ^exec[file-name;env hash]
                    397:        // ^exec[file-name;env hash;1cmd;2line;3ar;4g;5s]
                    398:        add_native_method("exec", Method::CT_DYNAMIC, _exec, 1, 2+10);
1.47    ! parser    399: 
        !           400:        // ^file:list[path]
        !           401:        // ^file:list[path][regexp]
        !           402:        add_native_method("list", Method::CT_STATIC, _list, 1, 2);
        !           403: 
1.32      paf       404: }
                    405: 
                    406: // global variable
                    407: 
                    408: Methoded *file_class;
                    409: 
                    410: // creator
                    411: 
                    412: Methoded *MFile_create(Pool& pool) {
                    413:        return file_class=new(pool) MFile(pool);
1.1       paf       414: }

E-mail: