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

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

E-mail: