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

1.17      paf         1: /** @file
                      2:        Parser: @b file parser class.
                      3: 
1.107     paf         4:        Copyright (c) 2001, 2003 ArtLebedev Group (http://www.artlebedev.com)
1.72      paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.110   ! paf         6: 
        !             7:        portions by Victor Fedoseev" <vvf_ru@mail.ru> [January 23, 2003]
1.91      paf         8: */
1.17      paf         9: 
1.110   ! paf        10: static const char* IDENT_FILE_C="$Date: 2003/03/21 09:43:48 $";
1.47      parser     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.67      paf        25: #include "pa_charset.h"
1.109     paf        26: #include "pa_charsets.h"
1.1       paf        27: 
1.32      paf        28: // defines
                     29: 
1.48      parser     30: #define TEXT_MODE_NAME "text"
1.90      paf        31: #define STDIN_EXEC_PARAM_NAME "stdin"
1.109     paf        32: #define CHARSET_EXEC_PARAM_NAME "charset"
1.48      parser     33: 
1.83      paf        34: // consts
                     35: 
                     36: /// from apache-1.3|src|support|suexec.c 
                     37: static const char *suexec_safe_env_lst[]={
                     38:     "AUTH_TYPE",
                     39:     "CONTENT_LENGTH",
                     40:     "CONTENT_TYPE",
                     41:     "DATE_GMT",
                     42:     "DATE_LOCAL",
                     43:     "DOCUMENT_NAME",
                     44:     "DOCUMENT_PATH_INFO",
                     45:     "DOCUMENT_ROOT",
                     46:     "DOCUMENT_URI",
                     47:     "FILEPATH_INFO",
                     48:     "GATEWAY_INTERFACE",
                     49:     "LAST_MODIFIED",
                     50:     "PATH_INFO",
                     51:     "PATH_TRANSLATED",
                     52:     "QUERY_STRING",
                     53:     "QUERY_STRING_UNESCAPED",
                     54:     "REMOTE_ADDR",
                     55:     "REMOTE_HOST",
                     56:     "REMOTE_IDENT",
                     57:     "REMOTE_PORT",
                     58:     "REMOTE_USER",
                     59:     "REDIRECT_QUERY_STRING",
                     60:     "REDIRECT_STATUS",
                     61:     "REDIRECT_URL",
                     62:     "REQUEST_METHOD",
                     63:     "REQUEST_URI",
                     64:     "SCRIPT_FILENAME",
                     65:     "SCRIPT_NAME",
                     66:     "SCRIPT_URI",
                     67:     "SCRIPT_URL",
                     68:     "SERVER_ADMIN",
                     69:     "SERVER_NAME",
                     70:     "SERVER_ADDR",
                     71:     "SERVER_PORT",
                     72:     "SERVER_PROTOCOL",
                     73:     "SERVER_SOFTWARE",
                     74:     "UNIQUE_ID",
                     75:     "USER_NAME",
                     76:     "TZ",
                     77:     NULL
                     78: };
                     79: 
1.32      paf        80: // class
                     81: 
                     82: class MFile : public Methoded {
                     83: public: // VStateless_class
                     84:        
                     85:        Value *create_new_value(Pool& pool) { return new(pool) VFile(pool); }
                     86: 
1.33      paf        87: public: // Methoded
                     88:        bool used_directly() { return true; }
                     89: 
1.32      paf        90: public:
                     91:        MFile(Pool& pool);
1.33      paf        92: 
1.32      paf        93: };
                     94: 
1.1       paf        95: // methods
                     96: 
1.26      paf        97: static void _save(Request& r, const String&, MethodParams *params) {
1.48      parser     98:        Value& vmode_name=params-> as_no_junction(0, "mode must not be code");
1.49      parser     99:        Value& vfile_name=params->as_no_junction(1, "file name must not be code");
1.4       paf       100: 
1.7       paf       101:        // save
1.99      paf       102:        static_cast<VFile *>(r.get_self())->save(r.absolute(vfile_name.as_string()),
1.48      parser    103:                vmode_name.as_string()==TEXT_MODE_NAME);
1.7       paf       104: }
                    105: 
1.26      paf       106: static void _delete(Request& r, const String&, MethodParams *params) {
1.39      parser    107:        Value& vfile_name=params->as_no_junction(0, "file name must not be code");
1.7       paf       108: 
                    109:        // unlink
1.68      paf       110:        file_delete(r.absolute(vfile_name.as_string()));
1.1       paf       111: }
                    112: 
1.45      parser    113: static void _move(Request& r, const String&, MethodParams *params) {
                    114:        Value& vfrom_file_name=params->as_no_junction(0, "from file name must not be code");
                    115:        Value& vto_file_name=params->as_no_junction(1, "to file name must not be code");
                    116: 
1.51      parser    117:        // move
1.68      paf       118:        file_move(
1.45      parser    119:                r.absolute(vfrom_file_name.as_string()),
                    120:                r.absolute(vto_file_name.as_string()));
                    121: }
                    122: 
1.105     paf       123: static void _load_pass_param(const Hash::Key& key, Hash::Val *value, void *info) {
                    124:        Hash& dest=*static_cast<Hash *>(info);
                    125:        dest.put(key, value);
                    126: }
1.26      paf       127: static void _load(Request& r, const String& method_name, MethodParams *params) {
1.9       paf       128:        Pool& pool=r.pool();
1.48      parser    129:        Value& vmode_name=params-> as_no_junction(0, "mode must not be code");
1.95      paf       130:        const String& lfile_name=r.absolute(params->as_no_junction(1, "file name must not be code").as_string());
1.104     paf       131:        Value *third_param=params->size()>2?&params->as_no_junction(2, "filename or options must not be code"):0;
                    132:        Hash *third_param_hash=third_param?third_param->get_hash(&method_name):0;
                    133:        int alt_filename_param_index=2;
                    134:        if(third_param_hash)
                    135:                alt_filename_param_index++;
1.9       paf       136: 
1.12      paf       137:        void *data;  size_t size;
1.104     paf       138:        Hash *fields=0;
1.95      paf       139:        file_read(pool, lfile_name, data, size, 
1.104     paf       140:                vmode_name.as_string()==TEXT_MODE_NAME,
                    141:                third_param_hash,
                    142:                &fields
                    143:        );
1.9       paf       144: 
1.104     paf       145:        char *user_file_name=params->size()>alt_filename_param_index?
                    146:                params->as_string(alt_filename_param_index, "filename must be string").cstr(String::UL_FILE_SPEC)
1.53      parser    147:                :lfile_name.cstr(String::UL_FILE_SPEC);
1.104     paf       148: 
                    149:        Value *vcontent_type=0;
                    150:        if(fields)
                    151:                vcontent_type=static_cast<Value *>(fields->get(*content_type_name));
                    152:        if(!vcontent_type)
                    153:                vcontent_type=new(pool) VString(r.mime_type_of(user_file_name));
1.10      paf       154:        
1.104     paf       155:        VFile& self=*static_cast<VFile *>(r.get_self());
                    156:        self.set(true/*tainted*/, data, size, user_file_name, vcontent_type);
1.105     paf       157:        if(fields)
                    158:                fields->for_each(_load_pass_param, &self.fields());
1.9       paf       159: }
                    160: 
1.26      paf       161: static void _stat(Request& r, const String& method_name, MethodParams *params) {
1.25      paf       162:        Pool& pool=r.pool();
1.39      parser    163:        Value& vfile_name=params->as_no_junction(0, "file name must not be code");
1.25      paf       164: 
                    165:        const String& lfile_name=vfile_name.as_string();
                    166: 
1.40      parser    167:        size_t size;
                    168:        time_t atime, mtime, ctime;
                    169:        file_stat(r.absolute(lfile_name),
                    170:                size,
                    171:                atime, mtime, ctime);
1.25      paf       172:        
1.99      paf       173:        VFile& vfile=*static_cast<VFile *>(r.get_self());
1.40      parser    174:        vfile.set(true/*tainted*/, 0/*no bytes*/, size);
                    175:        Hash& ff=vfile.fields();
                    176:        ff.put(*new(pool) String(pool, "adate"), new(pool) VDate(pool, atime));
                    177:        ff.put(*new(pool) String(pool, "mdate"), new(pool) VDate(pool, mtime));
                    178:        ff.put(*new(pool) String(pool, "cdate"), new(pool) VDate(pool, ctime));
1.93      paf       179:        ff.put(*content_type_name, new(pool) VString(r.mime_type_of(lfile_name.cstr(String::UL_FILE_SPEC))));
1.25      paf       180: }
                    181: 
1.83      paf       182: static bool is_safe_env_key(const char *key) {
1.88      paf       183:        if(strncasecmp(key, "HTTP_", 5)==0)
1.83      paf       184:                return true;
1.87      paf       185:        if(strncasecmp(key, "CGI_", 4)==0)
1.83      paf       186:                return true;
                    187:        for(int i=0; suexec_safe_env_lst[i]; i++) {
1.87      paf       188:                if(strcasecmp(key, suexec_safe_env_lst[i])==0)
1.83      paf       189:                        return true;
                    190:        }
                    191:        return false;
                    192: }
1.90      paf       193: #ifndef DOXYGEN
                    194: struct Append_env_pair_info {
                    195:        Hash* hash;
1.100     paf       196:        Value* vstdin;
1.109     paf       197:        Value* vcharset;
1.90      paf       198: };
                    199: #endif
1.100     paf       200: static void append_env_pair(const Hash::Key& key, Hash::Val *avalue, void *info) {
1.90      paf       201:        Append_env_pair_info& pi=*static_cast<Append_env_pair_info *>(info);
1.100     paf       202:        Value& value=*static_cast<Value *>(avalue);
1.90      paf       203: 
                    204:        if(key==STDIN_EXEC_PARAM_NAME) {
1.100     paf       205:                pi.vstdin=&value;
1.109     paf       206:        } else if(key==CHARSET_EXEC_PARAM_NAME) {
                    207:                pi.vcharset=&value;
1.90      paf       208:        } else {
                    209:                if(!is_safe_env_key(key.cstr()))
                    210:                        throw Exception("parser.runtime",
                    211:                                &key,
                    212:                                "not safe environment variable");
1.100     paf       213:                pi.hash->put(key, &value.as_string());
1.90      paf       214:        }
1.22      paf       215: }
1.94      paf       216: #ifndef DOXYGEN
                    217: struct Pass_cgi_header_attribute_info {
                    218:        Hash *hash;
                    219:        Value *content_type;
                    220: };
                    221: #endif
                    222: static void pass_cgi_header_attribute(Array::Item *value, void *ainfo) {
1.29      paf       223:        String& string=*static_cast<String *>(value);
1.94      paf       224:        Pool& pool=string.pool();
                    225:        Pass_cgi_header_attribute_info& info=*static_cast<Pass_cgi_header_attribute_info *>(ainfo);
1.29      paf       226:        int colon_pos=string.pos(":", 1);
1.94      paf       227:        if(colon_pos>0) {
                    228:                const String& key=string.mid(0, colon_pos).change_case(pool, String::CC_UPPER);
                    229:                Value *value=new(pool) VString(string.mid(colon_pos+1, string.size()));
                    230:                info.hash->put(key, value);
                    231:                if(key=="CONTENT-TYPE")
                    232:                        info.content_type=value;
                    233:        }
1.29      paf       234: }
1.90      paf       235: /// @todo fix `` in perl - they produced flipping consoles and no output to perl
1.41      parser    236: static void _exec_cgi(Request& r, const String& method_name, MethodParams *params,
                    237:                                          bool cgi) {
1.21      paf       238:        Pool& pool=r.pool();
                    239: 
1.39      parser    240:        Value& vfile_name=params->as_no_junction(0, "file name must not be code");
1.21      paf       241: 
1.23      paf       242:        const String& script_name=r.absolute(vfile_name.as_string());
                    243: 
                    244:        Hash env(pool);
1.62      paf       245:        #define ECSTR(name, value_cstr) \
                    246:                String name##key(pool, #name); \
                    247:                String name##value(pool); \
                    248:                if(value_cstr) { \
                    249:                        name##value.APPEND_CONST(value_cstr); \
                    250:                        env.put(name##key, &name##value); \
1.23      paf       251:                }
1.82      paf       252:        // passing SAPI::environment
                    253:        if(const char *const *pairs=SAPI::environment(pool)) {
                    254:                while(const char *pair=*pairs++)
                    255:                        if(const char *eq_at=strchr(pair, '=')) {
                    256:                                String& key=*new(pool) String(pool, pair, eq_at-pair);
                    257:                                String& value=*new(pool) String(pool, eq_at+1);
                    258:                                env.put(key, &value);
                    259:                        }
                    260:        }
                    261: 
1.23      paf       262:        // const
1.63      paf       263:        ECSTR(GATEWAY_INTERFACE, "CGI/1.1");
1.23      paf       264:        // from Request.info
1.62      paf       265:        ECSTR(DOCUMENT_ROOT, r.info.document_root);
                    266:        ECSTR(PATH_TRANSLATED, r.info.path_translated);
1.66      paf       267:        ECSTR(REQUEST_METHOD, r.info.method);
1.62      paf       268:        ECSTR(QUERY_STRING, r.info.query_string);
                    269:        ECSTR(REQUEST_URI, r.info.uri);
                    270:        ECSTR(CONTENT_TYPE, r.info.content_type);
1.23      paf       271:        char content_length_cstr[MAX_NUMBER];  
                    272:        snprintf(content_length_cstr, MAX_NUMBER, "%u", r.info.content_length);
                    273:        String content_length(pool, content_length_cstr);
1.62      paf       274:        ECSTR(CONTENT_LENGTH, content_length_cstr);
1.82      paf       275:        // SCRIPT_*
1.63      paf       276:        env.put(*new(pool) String(pool, "SCRIPT_NAME"), &script_name);
1.82      paf       277:        //env.put(*new(pool) String(pool, "SCRIPT_FILENAME"), ??&script_name);
1.23      paf       278: 
1.90      paf       279:        // environment & stdin from param
1.109     paf       280:        String raw_in(pool);
                    281:        Charset *charset=0; // default script works raw_in 'source' charset = no transcoding needed
1.21      paf       282:        if(params->size()>1) {
1.39      parser    283:                Value& venv=params->as_no_junction(1, "env must not be code");
1.90      paf       284:                if(Hash *user_env=venv.get_hash(&method_name)) {
                    285:                        Append_env_pair_info info={&env};
                    286:                        user_env->for_each(append_env_pair, &info);
1.109     paf       287:                        // $.stdin
1.103     paf       288:                        if(info.vstdin) {
                    289:                                if(const String *sstdin=info.vstdin->get_string()) {
1.109     paf       290:                                        raw_in.append(*sstdin, String::UL_CLEAN, true);
1.103     paf       291:                                } else
1.100     paf       292:                                        if(VFile *vfile=static_cast<VFile *>(info.vstdin->as("file", false)))
1.109     paf       293:                                                raw_in.APPEND_TAINTED((const char *)vfile->value_ptr(), vfile->value_size(),
1.100     paf       294:                                                        "$.stdin[assigned]", 0);
                    295:                                        else
                    296:                                                throw Exception("parser.runtime",
                    297:                                                        &method_name,
                    298:                                                        STDIN_EXEC_PARAM_NAME " parameter must be string or file");
1.103     paf       299:                        }
1.109     paf       300:                        // $.charset
                    301:                        if(info.vcharset)
                    302:                                charset=&charsets->get_charset(info.vcharset->as_string());
1.90      paf       303:                }
1.21      paf       304:        }
                    305: 
1.90      paf       306:        // argv from params
1.21      paf       307:        Array *argv=0;
                    308:        if(params->size()>2) {
                    309:                argv=new(pool) Array(pool, params->size()-2);
                    310:                for(int i=2; i<params->size(); i++)
1.58      parser    311:                        *argv+=&params->as_string(i, "parameter must be string");
1.21      paf       312:        }
1.90      paf       313: 
1.109     paf       314:        // transcode if necessary
                    315:        String* real_in=&raw_in;
                    316:        if(charset) {
                    317:                Charset::transcode(pool, pool.get_source_charset(), *charset, env);
                    318:                if(argv)
                    319:                        Charset::transcode(pool, pool.get_source_charset(), *charset, *argv);
                    320:                real_in=&Charset::transcode(pool, pool.get_source_charset(), *charset, raw_in);
                    321:        }
                    322: 
1.90      paf       323:        // exec!
1.109     paf       324:        String raw_out(pool);
                    325:        String& raw_err=*new(pool) String(pool);
1.110   ! paf       326:        int status=0;
        !           327: 
        !           328:        const String *body=0;
        !           329:        const String *header=0;
        !           330:        const char *eol_marker=0; size_t eol_marker_size; int header_break_pos;
1.109     paf       331: 
                    332:        String *real_out=&raw_out;
                    333:        String *real_err=&raw_err;
1.41      parser    334:        if(cgi) { // ^file:cgi
1.110   ! paf       335:                status = pa_exec(false/*forced_allow*/, script_name, &env, argv, *real_in, raw_out, raw_err, &header_break_pos, &eol_marker, &eol_marker_size);
        !           336:                // transcode if necessary
        !           337:                if(charset) {
        !           338:                        real_out=&Charset::transcode(pool, *charset, pool.get_source_charset(), raw_out);
        !           339:                        real_err=&Charset::transcode(pool, *charset, pool.get_source_charset(), raw_err);
        !           340:                }
        !           341:                if(header_break_pos == -1)
1.74      paf       342:                        throw Exception(0,
1.41      parser    343:                                &method_name,
1.90      paf       344:                                "output does not contain CGI header; "
                    345:                                "exit status=%d; stdoutsize=%u; stdout: \"%s\"; stderrsize=%u; stderr: \"%s\"", 
1.46      parser    346:                                        status, 
1.109     paf       347:                                        (uint)real_out->size(), real_out->cstr(),
                    348:                                        (uint)real_err->size(), real_err->cstr());
1.21      paf       349: 
1.109     paf       350:                header=&real_out->mid(0, header_break_pos);
                    351:                body=&real_out->mid(header_break_pos+eol_marker_size*2, real_out->size());
1.110   ! paf       352:        }else{ // ^file:exec
        !           353:                status = pa_exec(false/*forced_allow*/, script_name, &env, argv, *real_in, raw_out, raw_err);
        !           354:                // transcode if necessary
        !           355:                if(charset) {
        !           356:                        real_out=&Charset::transcode(pool, *charset, pool.get_source_charset(), raw_out);
        !           357:                        real_err=&Charset::transcode(pool, *charset, pool.get_source_charset(), raw_err);
        !           358:                }
        !           359:                body=real_out;
1.29      paf       360:        }
1.110   ! paf       361: 
        !           362: 
        !           363: 
        !           364:        VFile& self=*static_cast<VFile *>(r.get_self());
        !           365: 
1.41      parser    366:        // body
1.64      paf       367:        self.set(false/*not tainted*/, body->cstr(), body->size());
1.94      paf       368: 
                    369:        // $fields << header
1.98      paf       370:        if(header && eol_marker) {
1.94      paf       371:                Array rows(pool);
                    372:                header->split(rows, 0, eol_marker, eol_marker_size);
                    373:                Pass_cgi_header_attribute_info info={&self.fields()};
                    374:                rows.for_each(pass_cgi_header_attribute, &info);
                    375:                if(info.content_type)
                    376:                        self.fields().put(*content_type_name, info.content_type);
                    377:        }
1.21      paf       378: 
1.42      parser    379:        // $status
1.21      paf       380:        self.fields().put(
1.104     paf       381:                *file_status_name,
1.42      parser    382:                new(pool) VInt(pool, status));
1.21      paf       383:        
                    384:        // $stderr
1.109     paf       385:        if(real_err->size()) {
1.21      paf       386:                self.fields().put(
                    387:                        *new(pool) String(pool, "stderr"),
1.109     paf       388:                        new(pool) VString(*real_err));
1.21      paf       389:        }
                    390: }
1.41      parser    391: static void _exec(Request& r, const String& method_name, MethodParams *params) {
                    392:        _exec_cgi(r, method_name, params, false);
                    393: }
                    394: static void _cgi(Request& r, const String& method_name, MethodParams *params) {
                    395:        _exec_cgi(r, method_name, params, true);
                    396: }
                    397: 
1.47      parser    398: static void _list(Request& r, const String& method_name, MethodParams *params) {
                    399:        Pool& pool=r.pool();
                    400: 
                    401:        Value& relative_path=params->as_no_junction(0, "path must not be code");
                    402: 
                    403:        const String *regexp;
                    404:        pcre *regexp_code;
1.81      paf       405:        const int ovecsize=(1/*match*/)*3;
                    406:        int ovector[ovecsize];
1.47      parser    407:        if(params->size()>1) {
                    408:                regexp=&params->as_no_junction(1, "regexp must not be code").as_string();
                    409: 
1.64      paf       410:                const char *pattern=regexp->cstr();
1.47      parser    411:                const char *errptr;
                    412:                int erroffset;
                    413:                regexp_code=pcre_compile(pattern, PCRE_EXTRA | PCRE_DOTALL, 
                    414:                        &errptr, &erroffset, 
1.67      paf       415:                        pool.get_client_charset().pcre_tables);
1.47      parser    416: 
                    417:                if(!regexp_code)
1.74      paf       418:                        throw Exception(0, 
1.47      parser    419:                                &regexp->mid(erroffset, regexp->size()), 
                    420:                                "regular expression syntax error - %s", errptr);
                    421:        } else 
                    422:                regexp_code=0;
                    423: 
                    424: 
                    425:        const char* absolute_path_cstr=r.absolute(relative_path.as_string())
1.53      parser    426:                .cstr(String::UL_FILE_SPEC);
1.47      parser    427: 
                    428:        Array& columns=*new(pool) Array(pool);
                    429:        columns+=new(pool) String(pool, "name");        
                    430:        Table& table=*new(pool) Table(pool, &method_name, &columns);
                    431: 
                    432:        LOAD_DIR(absolute_path_cstr, 
                    433:                size_t file_name_size=strlen(ffblk.ff_name);
                    434:                bool suits=true;
                    435:                if(regexp_code) {
                    436:                        int exec_result=pcre_exec(regexp_code, 0, 
                    437:                                ffblk.ff_name, file_name_size, 0, 
                    438:                                0, ovector, ovecsize);
                    439:                        
                    440:                        if(exec_result==PCRE_ERROR_NOMATCH)
                    441:                                suits=false;
                    442:                        else if(exec_result<0) {
                    443:                                (*pcre_free)(regexp_code);
1.74      paf       444:                                throw Exception(0, 
1.47      parser    445:                                        regexp, 
                    446:                                        "regular expression execute (%d)", 
                    447:                                                exec_result);
                    448:                        }
                    449:                }
                    450: 
                    451:                if(suits) {
1.50      parser    452:                        char *file_name_cstr=(char *)pool.malloc(file_name_size);
1.47      parser    453:                        memcpy(file_name_cstr, ffblk.ff_name, file_name_size);
                    454:                        String &file_name=*new(pool) String(pool);
1.101     paf       455:                        file_name.APPEND_TAINTED(file_name_cstr, file_name_size, 
1.47      parser    456:                                method_name.origin().file, method_name.origin().line);
                    457:                
                    458:                        Array& row=*new(pool) Array(pool);
                    459:                        row+=&file_name;
                    460:                        table+=&row;
                    461:                }
                    462:        );
                    463: 
                    464:        if(regexp_code)
                    465:                (*pcre_free)(regexp_code);
                    466: 
1.60      parser    467:        // write out result
1.47      parser    468:        VTable& result=*new(pool) VTable(pool, &table);
                    469:        r.write_no_lang(result);
                    470: }
1.21      paf       471: 
1.69      paf       472: #ifndef DOXYGEN
                    473: struct Lock_execute_body_info {
                    474:        Request *r;
                    475:        Value *body_code;
                    476: };
                    477: #endif
                    478: static void lock_execute_body(int , void *context) {
                    479:        Lock_execute_body_info& info=*static_cast<Lock_execute_body_info *>(context);
                    480: 
                    481:        // execute body
1.78      paf       482:        info.r->write_assign_lang(info.r->process(*info.body_code));
1.69      paf       483: };
                    484: static void _lock(Request& r, const String& method_name, MethodParams *params) {
                    485:        const String& file_spec=r.absolute(params->as_string(0, "file name must be string"));
                    486:        Value& body_code=params->as_junction(1, "body must be code");
                    487: 
                    488:        Lock_execute_body_info info={&r, &body_code};
1.70      paf       489:        file_write_action_under_lock(file_spec, "lock", lock_execute_body, &info);
1.69      paf       490: }
                    491: 
1.89      paf       492: static int lastposafter(const String& s, int after, const char *substr, size_t substr_size, bool beforelast=false) {
                    493:        size_t size;
                    494:        if(beforelast)
                    495:                size=s.size();
                    496:        int at;
                    497:        while((at=s.pos(substr, substr_size, after))>=0) {
                    498:                size_t newafter=at+substr_size/*skip substr*/;
                    499:                if(beforelast && newafter==size)
                    500:                        break;
                    501:                after=newafter;
                    502:        }
                    503: 
                    504:        return after;
                    505: }
                    506: 
1.90      paf       507: static void _find(Request& r, const String& method_name, MethodParams *params) {
                    508:        Pool& pool=r.pool();
                    509:        const String &file_name=params->as_no_junction(0, "file name must not be code").as_string();
                    510:        const String *file_spec;
                    511:        if(file_name.first_char()=='/')
                    512:                file_spec=&file_name;
                    513:        else 
                    514:                file_spec=&r.relative(r.info.uri, file_name);
                    515: 
                    516:        // easy way
                    517:        if(file_readable(r.absolute(*file_spec))) {
1.96      paf       518:                r.write_assign_lang(*file_spec);
1.90      paf       519:                return;
                    520:        }
                    521: 
                    522:        // monkey way
                    523:        int after_base_slash=lastposafter(*file_spec, 0, "/", 1);
                    524:        const String *dirname=&file_spec->mid(0, after_base_slash);
                    525:        const String& basename=file_spec->mid(after_base_slash, file_spec->size());
                    526: 
                    527:        int after_monkey_slash;
                    528:        while((after_monkey_slash=lastposafter(*dirname, 0, "/", 1, true))>0) {
                    529:                String local_test_name(pool);
                    530:                local_test_name<<*(dirname=&dirname->mid(0, after_monkey_slash));
                    531:                local_test_name<<basename;
                    532:                if(file_readable(r.absolute(local_test_name))) {
1.96      paf       533:                        r.write_assign_lang(*new(pool) String(local_test_name));
1.90      paf       534:                        return;
                    535:                }
                    536:        }
                    537: 
                    538:        // no way, not found
                    539:        if(params->size()==2) {
                    540:                Value& not_found_code=params->as_junction(1, "not-found param must be code");
                    541:                r.write_pass_lang(r.process(not_found_code));
                    542:        }
                    543: }
                    544: 
1.89      paf       545: static void _dirname(Request& r, const String& method_name, MethodParams *params) {
                    546:        Pool& pool=r.pool();
                    547:        const String& file_spec=params->as_string(0, "file name must be string");
                    548:     // /a/some.tar.gz > /a
                    549:        // /a/b/ > /a
                    550:        int afterslash=lastposafter(file_spec, 0, "/", 1, true);
                    551:        if(afterslash>0)
                    552:                r.write_assign_lang(file_spec.mid(0, afterslash==1?1:afterslash-1));
                    553:        else
                    554:                r.write_assign_lang(*new(pool) String(pool, ".", 1));
                    555: }
                    556: 
                    557: static void _basename(Request& r, const String& method_name, MethodParams *params) {
                    558:        const String& file_spec=params->as_string(0, "file name must be string");
                    559:     // /a/some.tar.gz > some.tar.gz
                    560:        int afterslash=lastposafter(file_spec, 0, "/", 1);
                    561:        r.write_assign_lang(file_spec.mid(afterslash, file_spec.size()));
                    562: }
                    563: 
                    564: static void _justname(Request& r, const String& method_name, MethodParams *params) {
                    565:        const String& file_spec=params->as_string(0, "file name must be string");
                    566:     // /a/some.tar.gz > some.tar
                    567:        int afterslash=lastposafter(file_spec, 0, "/", 1);
                    568:        int afterdot=lastposafter(file_spec, afterslash, ".", 1);
                    569:        r.write_assign_lang(file_spec.mid(afterslash, afterdot!=afterslash?afterdot-1:file_spec.size()));
                    570: }
                    571: static void _justext(Request& r, const String& method_name, MethodParams *params) {
                    572:        const String& file_spec=params->as_string(0, "file name must be string");
                    573:     // /a/some.tar.gz > gz
                    574:        int afterdot=lastposafter(file_spec, 0, ".", 1);
                    575:        if(afterdot>0)
                    576:                r.write_assign_lang(file_spec.mid(afterdot, file_spec.size()));
                    577: }
                    578: 
1.102     paf       579: static void _fullpath(Request& r, const String& method_name, MethodParams *params) {
                    580:        const String& file_spec=params->as_string(0, "file name must be string");
                    581:        const String *result;
                    582:        if(file_spec.first_char()=='/')
                    583:                result=&file_spec;
                    584:        else {
                    585:                // /some/page.html: ^file:fullpath[a.gif] => /some/a.gif
                    586:                const String& full_disk_path=r.absolute(file_spec);
                    587:                size_t document_root_length=strlen(r.info.document_root);
1.106     paf       588: 
                    589:                if(document_root_length>0) {
                    590:                        char last_char=r.info.document_root[document_root_length-1];
                    591:                        if(last_char == '/' || last_char == '\\')
                    592:                                --document_root_length;
                    593:                }
1.102     paf       594:                result=&full_disk_path.mid(document_root_length,  full_disk_path.size());
                    595:        }
                    596:        r.write_assign_lang(*result);
                    597: }
                    598: 
1.89      paf       599: 
1.32      paf       600: // constructor
                    601: 
1.80      paf       602: MFile::MFile(Pool& apool) : Methoded(apool, "file") {
1.48      parser    603:        // ^save[mode;file-name]
                    604:        add_native_method("save", Method::CT_DYNAMIC, _save, 2, 2);
1.7       paf       605: 
                    606:        // ^delete[file-name]
1.32      paf       607:        add_native_method("delete", Method::CT_STATIC, _delete, 1, 1);
1.45      parser    608: 
                    609:        // ^move[from-file-name;to-file-name]
                    610:        add_native_method("move", Method::CT_STATIC, _move, 2, 2);
1.8       paf       611: 
1.48      parser    612:        // ^load[mode;disk-name]
                    613:        // ^load[mode;disk-name;user-name]
                    614:        add_native_method("load", Method::CT_DYNAMIC, _load, 2, 3);
1.25      paf       615: 
                    616:        // ^stat[disk-name]
1.32      paf       617:        add_native_method("stat", Method::CT_DYNAMIC, _stat, 1, 1);
1.21      paf       618: 
1.36      paf       619:        // ^cgi[file-name]
                    620:        // ^cgi[file-name;env hash]
                    621:        // ^cgi[file-name;env hash;1cmd;2line;3ar;4g;5s]
1.41      parser    622:        add_native_method("cgi", Method::CT_DYNAMIC, _cgi, 1, 2+10);
                    623: 
                    624:        // ^exec[file-name]
                    625:        // ^exec[file-name;env hash]
                    626:        // ^exec[file-name;env hash;1cmd;2line;3ar;4g;5s]
                    627:        add_native_method("exec", Method::CT_DYNAMIC, _exec, 1, 2+10);
1.47      parser    628: 
                    629:        // ^file:list[path]
                    630:        // ^file:list[path][regexp]
                    631:        add_native_method("list", Method::CT_STATIC, _list, 1, 2);
1.69      paf       632: 
                    633:        // ^file:lock[path]{code}
                    634:        add_native_method("lock", Method::CT_STATIC, _lock, 2, 2);
1.90      paf       635: 
                    636:        // ^find[file-name]
                    637:        // ^find[file-name]{when-not-found}
                    638:        add_native_method("find", Method::CT_STATIC, _find, 1, 2);
1.47      parser    639: 
1.89      paf       640:     // ^file:dirname[/a/some.tar.gz]=/a
                    641:        // ^file:dirname[/a/b/]=/a
                    642:        add_native_method("dirname", Method::CT_STATIC, _dirname, 1, 1);
                    643:     // ^file:basename[/a/some.tar.gz]=some.tar.gz
                    644:     add_native_method("basename", Method::CT_STATIC, _basename, 1, 1);
                    645:     // ^file:justname[/a/some.tar.gz]=some.tar
                    646:        add_native_method("justname", Method::CT_STATIC, _justname, 1, 1);
                    647:     // ^file:justext[/a/some.tar.gz]=gz
                    648:        add_native_method("justext", Method::CT_STATIC, _justext, 1, 1);
1.102     paf       649:     // /some/page.html: ^file:fullpath[a.gif] => /some/a.gif
                    650:        add_native_method("fullpath", Method::CT_STATIC, _fullpath, 1, 1);
1.32      paf       651: }
                    652: 
                    653: // global variable
                    654: 
                    655: Methoded *file_class;
                    656: 
                    657: // creator
                    658: 
                    659: Methoded *MFile_create(Pool& pool) {
                    660:        return file_class=new(pool) MFile(pool);
1.1       paf       661: }

E-mail: