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

1.17      paf         1: /** @file
                      2:        Parser: @b file parser class.
                      3: 
1.185     misha       4:        Copyright (c) 2001-2009 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.200   ! misha       8: static const char * const IDENT_FILE_C="$Date: 2009-08-08 13:30:20 $";
1.47      parser      9: 
                     10: #include "pa_config_includes.h"
                     11: 
1.35      paf        12: #include "classes.h"
1.111     paf        13: #include "pa_vmethod_frame.h"
                     14: 
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.121     paf        25: #include "pa_sql_connection.h"
1.147     misha      26: #include "pa_md5.h"
1.184     misha      27: #include "pa_vregex.h"
1.1       paf        28: 
1.32      paf        29: // defines
                     30: 
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.131     paf        34: #define NAME_NAME "name"
                     35: 
1.132     paf        36: // externs
                     37: 
                     38: extern String sql_limit_name;
                     39: extern String sql_offset_name;
                     40: 
1.111     paf        41: // class
                     42: 
                     43: class MFile: public Methoded {
                     44: public: // VStateless_class
                     45:        
1.199     misha      46:        Value* create_new_value(Pool&) { return new VFile(); }
1.111     paf        47: 
                     48: public: // Methoded
                     49:        bool used_directly() { return true; }
                     50: 
                     51: public:
                     52:        MFile();
                     53: 
                     54: };
                     55: 
                     56: // global variable
                     57: 
                     58: DECLARE_CLASS_VAR(file, new MFile, 0);
                     59: 
1.83      paf        60: // consts
                     61: 
                     62: /// from apache-1.3|src|support|suexec.c 
1.111     paf        63: static const char* suexec_safe_env_lst[]={
1.83      paf        64:     "AUTH_TYPE",
                     65:     "CONTENT_LENGTH",
                     66:     "CONTENT_TYPE",
                     67:     "DATE_GMT",
                     68:     "DATE_LOCAL",
                     69:     "DOCUMENT_NAME",
                     70:     "DOCUMENT_PATH_INFO",
                     71:     "DOCUMENT_ROOT",
                     72:     "DOCUMENT_URI",
                     73:     "FILEPATH_INFO",
                     74:     "GATEWAY_INTERFACE",
                     75:     "LAST_MODIFIED",
                     76:     "PATH_INFO",
                     77:     "PATH_TRANSLATED",
                     78:     "QUERY_STRING",
                     79:     "QUERY_STRING_UNESCAPED",
                     80:     "REMOTE_ADDR",
                     81:     "REMOTE_HOST",
                     82:     "REMOTE_IDENT",
                     83:     "REMOTE_PORT",
                     84:     "REMOTE_USER",
                     85:     "REDIRECT_QUERY_STRING",
                     86:     "REDIRECT_STATUS",
                     87:     "REDIRECT_URL",
                     88:     "REQUEST_METHOD",
                     89:     "REQUEST_URI",
                     90:     "SCRIPT_FILENAME",
                     91:     "SCRIPT_NAME",
                     92:     "SCRIPT_URI",
                     93:     "SCRIPT_URL",
                     94:     "SERVER_ADMIN",
                     95:     "SERVER_NAME",
                     96:     "SERVER_ADDR",
                     97:     "SERVER_PORT",
                     98:     "SERVER_PROTOCOL",
                     99:     "SERVER_SOFTWARE",
                    100:     "UNIQUE_ID",
                    101:     "USER_NAME",
                    102:     "TZ",
                    103:     NULL
                    104: };
                    105: 
1.111     paf       106: // statics
1.33      paf       107: 
1.112     paf       108: static const String::Body adate_name("adate");
                    109: static const String::Body mdate_name("mdate");
                    110: static const String::Body cdate_name("cdate");
1.32      paf       111: 
1.1       paf       112: // methods
                    113: 
1.161     misha     114: static bool is_valid_mode (const String& mode) {
1.194     misha     115:        return (mode==text_mode_name || mode==binary_mode_name);
1.161     misha     116: }
                    117: 
1.125     paf       118: static bool is_text_mode(const String& mode) {
1.194     misha     119:        if(mode==text_mode_name)
1.125     paf       120:                return true;
1.194     misha     121:        if(mode==binary_mode_name)
1.125     paf       122:                return false;
1.156     misha     123:        throw Exception(PARSER_RUNTIME,
1.125     paf       124:                &mode,
                    125:                "is invalid mode, must be either '"TEXT_MODE_NAME"' or '"BINARY_MODE_NAME"'");
                    126: }
                    127: 
1.111     paf       128: static void _save(Request& r, MethodParams& params) {
1.161     misha     129:        Value& vmode_name=params.as_no_junction(0, MODE_MUST_NOT_BE_CODE);
1.152     misha     130:        Value& vfile_name=params.as_no_junction(1, FILE_NAME_MUST_NOT_BE_CODE);
1.4       paf       131: 
1.7       paf       132:        // save
1.111     paf       133:        GET_SELF(r, VFile).save(r.absolute(vfile_name.as_string()),
1.125     paf       134:                is_text_mode(vmode_name.as_string()));
1.7       paf       135: }
                    136: 
1.111     paf       137: static void _delete(Request& r, MethodParams& params) {
1.152     misha     138:        Value& vfile_name=params.as_no_junction(0, FILE_NAME_MUST_NOT_BE_CODE);
1.7       paf       139: 
                    140:        // unlink
1.68      paf       141:        file_delete(r.absolute(vfile_name.as_string()));
1.1       paf       142: }
                    143: 
1.111     paf       144: static void _move(Request& r, MethodParams& params) {
                    145:        Value& vfrom_file_name=params.as_no_junction(0, "from file name must not be code");
                    146:        Value& vto_file_name=params.as_no_junction(1, "to file name must not be code");
1.45      parser    147: 
1.51      parser    148:        // move
1.68      paf       149:        file_move(
1.45      parser    150:                r.absolute(vfrom_file_name.as_string()),
                    151:                r.absolute(vto_file_name.as_string()));
                    152: }
                    153: 
1.148     misha     154: static void copy_process_source(
1.180     misha     155:                                struct stat& , 
                    156:                                int from_file, 
                    157:                                const String& , const char* /*fname*/, bool, 
                    158:                                void *context) {
1.148     misha     159:        int& to_file=*static_cast<int *>(context);
                    160: 
                    161:        int nCount=0;
                    162:        do {
                    163:                unsigned char buffer[FILE_BUFFER_SIZE];
1.150     misha     164:                nCount = file_block_read(from_file, buffer, sizeof(buffer));
1.148     misha     165:                int written=write(to_file, buffer, nCount); 
                    166:                if( written < 0 )
1.179     misha     167:                        throw Exception("file.access", 
1.148     misha     168:                                0, 
                    169:                                "write failed: %s (%d)",  strerror(errno), errno); 
                    170:                
                    171:        } while(nCount > 0);
                    172: }
                    173: 
                    174: static void copy_open_target(int f, void *from_spec) {
                    175:        String& file_spec=*static_cast<String *>(from_spec);
                    176:        file_read_action_under_lock(file_spec, "copy", copy_process_source, &f);
                    177: };
                    178: 
                    179: static void _copy(Request& r, MethodParams& params) {
                    180:        Value& vfrom_file_name=params.as_no_junction(0, "from file name must not be code");
                    181:        Value& vto_file_name=params.as_no_junction(1, "to file name must not be code");
                    182: 
                    183:        String from_spec = r.absolute(vfrom_file_name.as_string());
                    184:        const String& to_spec = r.absolute(vto_file_name.as_string());
                    185:        
                    186:        file_write_action_under_lock(
                    187:                        to_spec,
                    188:                        "copy",
                    189:                        copy_open_target,
1.149     misha     190:                        &from_spec);
1.148     misha     191: }
                    192: 
1.111     paf       193: static void _load_pass_param(
1.180     misha     194:                                HashStringValue::key_type key, 
                    195:                                HashStringValue::value_type value, 
                    196:                                HashStringValue *dest) {
1.111     paf       197:        dest->put(key, value);
                    198: }
1.180     misha     199: 
1.111     paf       200: static void _load(Request& r, MethodParams& params) {
1.180     misha     201:        bool as_text=is_text_mode(params.as_no_junction(0, MODE_MUST_NOT_BE_CODE).as_string());
1.152     misha     202:        const String& lfile_name=r.absolute(params.as_no_junction(1, FILE_NAME_MUST_NOT_BE_CODE).as_string());
1.9       paf       203: 
1.180     misha     204:        size_t param_index=params.count()-1;
1.183     misha     205:        Value* param_value=param_index>1?&params.as_no_junction(param_index, "filename or options must not be code"):0;
1.180     misha     206: 
1.183     misha     207:        HashStringValue* options=0;
                    208:        const char *user_file_name=0;
                    209: 
                    210:        if(param_value){
                    211:                options=param_value->get_hash();
                    212:                if(options || param_index>2)
                    213:                        param_index--;
                    214:                if(param_index>1){
                    215:                        const String& luser_file_name=params.as_string(param_index, FILE_NAME_MUST_BE_STRING);
                    216:                        if(!luser_file_name.is_empty())
1.196     misha     217:                                user_file_name=luser_file_name.taint_cstr(String::L_FILE_SPEC);
1.183     misha     218:                }
                    219:        }
                    220:        if(!user_file_name)
1.196     misha     221:                user_file_name=lfile_name.taint_cstr(String::L_FILE_SPEC);
1.180     misha     222: 
1.132     paf       223:        size_t offset=0;
                    224:        size_t limit=0;
1.183     misha     225: 
1.180     misha     226:        if(options){
1.132     paf       227:                options=new HashStringValue(*options);
1.180     misha     228:                if(Value *voffset=(Value *)options->get(sql_offset_name)){
1.132     paf       229:                        offset=r.process_to_value(*voffset).as_int();
                    230:                }
1.180     misha     231:                if(Value *vlimit=(Value *)options->get(sql_limit_name)){
1.132     paf       232:                        limit=r.process_to_value(*vlimit).as_int();
                    233:                }
                    234:                // no check on options count here, see file_read
                    235:        }
1.182     misha     236:        File_read_result file=file_load(r, lfile_name,
1.180     misha     237:                as_text, options, true, 0, offset, limit
1.104     paf       238:        );
1.9       paf       239: 
1.111     paf       240:        Value* vcontent_type=0;
1.168     misha     241:        if(file.headers){
1.181     misha     242:                if(Value* remote_content_type=file.headers->get(HTTP_CONTENT_TYPE_UPPER))
1.129     paf       243:                        vcontent_type=new VString(*new String(remote_content_type->as_string().cstr()));
                    244:        } 
1.104     paf       245:        if(!vcontent_type)
1.111     paf       246:                vcontent_type=new VString(r.mime_type_of(user_file_name));
1.10      paf       247:        
1.111     paf       248:        VFile& self=GET_SELF(r, VFile);
                    249:        self.set(true/*tainted*/, file.str, file.length, user_file_name, vcontent_type);
1.168     misha     250: 
1.194     misha     251:        self.set_mode(as_text);
                    252: 
1.168     misha     253:        if(file.headers){
1.143     paf       254:                file.headers->for_each<HashStringValue*>(_load_pass_param, &self.fields());
1.168     misha     255:        } else {
                    256:                size_t size;
                    257:                time_t atime, mtime, ctime;
                    258: 
1.169     misha     259:                file_stat(lfile_name, size, atime, mtime, ctime);
1.168     misha     260:        
                    261:                HashStringValue& ff=self.fields();
                    262:                ff.put(adate_name, new VDate(atime));
                    263:                ff.put(mdate_name, new VDate(mtime));
                    264:                ff.put(cdate_name, new VDate(ctime));
                    265:        }
1.9       paf       266: }
                    267: 
1.138     paf       268: static void _create(Request& r, MethodParams& params) {
1.194     misha     269:        const String& mode_name=params.as_no_junction(0, MODE_MUST_NOT_BE_CODE).as_string();
                    270:        if(!is_text_mode(mode_name))
1.156     misha     271:                throw Exception(PARSER_RUNTIME,
1.138     paf       272:                        0,
                    273:                        "only text mode is currently supported");
                    274: 
                    275:        const char* user_file_name_cstr=r.absolute(
1.196     misha     276:                params.as_no_junction(1, FILE_NAME_MUST_NOT_BE_CODE).as_string()).taint_cstr(String::L_FILE_SPEC);
1.138     paf       277: 
                    278:        const String& content=params.as_string(2, "content must be string");
1.198     misha     279:        const String::Body content_body=content.cstr_to_string_body_untaint(String::L_AS_IS); // explode content, honor tainting changes
1.138     paf       280: 
                    281:        VString* vcontent_type=new VString(r.mime_type_of(user_file_name_cstr));
                    282:        
                    283:        VFile& self=GET_SELF(r, VFile);
1.197     misha     284:        self.set(true/*tainted*/, content_body.cstr(), content_body.length(), user_file_name_cstr, vcontent_type);
1.194     misha     285: 
                    286:        self.set_mode(true/*as_text*/);
1.138     paf       287: }
                    288: 
1.111     paf       289: static void _stat(Request& r, MethodParams& params) {
1.152     misha     290:        Value& vfile_name=params.as_no_junction(0, FILE_NAME_MUST_NOT_BE_CODE);
1.25      paf       291: 
                    292:        const String& lfile_name=vfile_name.as_string();
                    293: 
1.40      parser    294:        size_t size;
                    295:        time_t atime, mtime, ctime;
                    296:        file_stat(r.absolute(lfile_name),
                    297:                size,
                    298:                atime, mtime, ctime);
1.25      paf       299:        
1.196     misha     300:        const char* user_file_name=lfile_name.taint_cstr(String::L_FILE_SPEC);
1.167     misha     301: 
1.111     paf       302:        VFile& self=GET_SELF(r, VFile);
1.167     misha     303: 
                    304:        self.set(true/*tainted*/, 0/*no bytes*/, size, user_file_name, new VString(r.mime_type_of(user_file_name)));
1.111     paf       305:        HashStringValue& ff=self.fields();
                    306:        ff.put(adate_name, new VDate(atime));
                    307:        ff.put(mdate_name, new VDate(mtime));
                    308:        ff.put(cdate_name, new VDate(ctime));
1.25      paf       309: }
                    310: 
1.111     paf       311: static bool is_safe_env_key(const char* key) {
                    312:        for(const char* validator=key; *validator; validator++) {
                    313:                char c=*validator;
                    314:                if(!(c>='A' && c<='Z' || c>='0' && c<='9' || c=='_' || c=='-'))
                    315:                        return false;
                    316:        }
1.88      paf       317:        if(strncasecmp(key, "HTTP_", 5)==0)
1.83      paf       318:                return true;
1.87      paf       319:        if(strncasecmp(key, "CGI_", 4)==0)
1.83      paf       320:                return true;
                    321:        for(int i=0; suexec_safe_env_lst[i]; i++) {
1.87      paf       322:                if(strcasecmp(key, suexec_safe_env_lst[i])==0)
1.83      paf       323:                        return true;
                    324:        }
                    325:        return false;
                    326: }
1.90      paf       327: #ifndef DOXYGEN
                    328: struct Append_env_pair_info {
1.141     paf       329:        Request_charsets* charsets;
1.111     paf       330:        HashStringString* env;
1.100     paf       331:        Value* vstdin;
1.90      paf       332: };
                    333: #endif
1.111     paf       334: static void append_env_pair(
1.180     misha     335:                                HashStringValue::key_type akey, 
                    336:                                HashStringValue::value_type avalue, 
                    337:                                Append_env_pair_info *info) {
1.111     paf       338:        if(akey==STDIN_EXEC_PARAM_NAME) {
                    339:                info->vstdin=avalue;
                    340:        } else if(akey==CHARSET_EXEC_PARAM_NAME) {
1.141     paf       341:                // ignore, already processed
1.90      paf       342:        } else {
1.111     paf       343:                if(!is_safe_env_key(akey.cstr()))
1.156     misha     344:                        throw Exception(PARSER_RUNTIME,
1.111     paf       345:                                new String(akey, String::L_TAINTED),
1.90      paf       346:                                "not safe environment variable");
1.196     misha     347:                info->env->put(akey, avalue->as_string().cstr_to_string_body_untaint(String::L_AS_IS, 0, info->charsets));
1.90      paf       348:        }
1.22      paf       349: }
1.94      paf       350: #ifndef DOXYGEN
                    351: struct Pass_cgi_header_attribute_info {
1.111     paf       352:        Charset* charset;
                    353:        HashStringValue* fields;
                    354:        Value* content_type;
1.94      paf       355: };
                    356: #endif
1.111     paf       357: static void pass_cgi_header_attribute(
1.180     misha     358:                                        ArrayString::element_type astring, 
                    359:                                        Pass_cgi_header_attribute_info* info) {
1.111     paf       360:        size_t colon_pos=astring->pos(':');
1.130     paf       361:        if(colon_pos!=STRING_NOT_FOUND) {
1.111     paf       362:                const String& key=astring->mid(0, colon_pos).change_case(
                    363:                        *info->charset, String::CC_UPPER);
1.130     paf       364:                Value* value=new VString(astring->mid(colon_pos+1, astring->length()).trim());
1.111     paf       365:                info->fields->put(key, value);
1.181     misha     366:                if(key==HTTP_CONTENT_TYPE_UPPER)
1.111     paf       367:                        info->content_type=value;
1.94      paf       368:        }
1.29      paf       369: }
1.155     misha     370: 
                    371: static void append_to_argv(Request& r, ArrayString& argv, const String* str){
1.187     misha     372:        if(!str->is_empty())
1.196     misha     373:                argv+=new String(str->cstr_to_string_body_untaint(String::L_AS_IS, 0, &r.charsets), String::L_AS_IS);
1.155     misha     374: }
                    375: 
1.90      paf       376: /// @todo fix `` in perl - they produced flipping consoles and no output to perl
1.194     misha     377: static void _exec_cgi(Request& r, MethodParams& params, bool cgi) {
                    378:        bool as_text=true;
                    379:        size_t param_index=0;
                    380:        const String& mode_name=params.as_no_junction(0, FIRST_ARG_MUST_NOT_BE_CODE).as_string();
                    381:        if(is_valid_mode(mode_name)){
                    382:                as_text=is_text_mode(mode_name);
                    383:                param_index++;
1.162     misha     384:        }
                    385: 
                    386:        if(param_index>=params.count())
                    387:                throw Exception(PARSER_RUNTIME,
                    388:                        0,
                    389:                        "file name must be specified");
                    390: 
                    391: 
                    392:        Value& vfile_name=params.as_no_junction(param_index++, FILE_NAME_MUST_NOT_BE_CODE);
1.21      paf       393: 
1.23      paf       394:        const String& script_name=r.absolute(vfile_name.as_string());
                    395: 
1.111     paf       396:        HashStringString env;
1.62      paf       397:        #define ECSTR(name, value_cstr) \
1.111     paf       398:                if(value_cstr) \
                    399:                        env.put( \
1.112     paf       400:                                String::Body(#name), \
1.192     misha     401:                                String::Body(*value_cstr?value_cstr:0)); \
1.82      paf       402:        // passing SAPI::environment
1.111     paf       403:        if(const char *const *pairs=SAPI::environment(r.sapi_info)) {
                    404:                while(const char* pair=*pairs++)
                    405:                        if(const char* eq_at=strchr(pair, '='))
                    406:                                if(eq_at[1]) // has value
                    407:                                        env.put(
                    408:                                                pa_strdup(pair, eq_at-pair),
                    409:                                                pa_strdup(eq_at+1, 0));
1.82      paf       410:        }
                    411: 
1.23      paf       412:        // const
1.63      paf       413:        ECSTR(GATEWAY_INTERFACE, "CGI/1.1");
1.23      paf       414:        // from Request.info
1.111     paf       415:        ECSTR(DOCUMENT_ROOT, r.request_info.document_root);
                    416:        ECSTR(PATH_TRANSLATED, r.request_info.path_translated);
                    417:        ECSTR(REQUEST_METHOD, r.request_info.method);
                    418:        ECSTR(QUERY_STRING, r.request_info.query_string);
                    419:        ECSTR(REQUEST_URI, r.request_info.uri);
                    420:        ECSTR(CONTENT_TYPE, r.request_info.content_type);
1.200   ! misha     421:        ECSTR(CONTENT_LENGTH, format(r.request_info.content_length, "%u"));
1.82      paf       422:        // SCRIPT_*
1.119     paf       423:        env.put(String::Body("SCRIPT_NAME"), script_name);
                    424:        //env.put(String::Body("SCRIPT_FILENAME"), ??&script_name);
1.23      paf       425: 
1.111     paf       426:        bool stdin_specified=false;
1.90      paf       427:        // environment & stdin from param
1.111     paf       428:        String *in=new String();
1.109     paf       429:        Charset *charset=0; // default script works raw_in 'source' charset = no transcoding needed
1.162     misha     430:        if(param_index < params.count()) {
                    431:                Value& venv=params.as_no_junction(param_index++, "env must not be code");
1.111     paf       432:                if(HashStringValue* user_env=venv.get_hash()) {
1.141     paf       433:                        // $.charset  [previewing to handle URI pieces]
                    434:                        if(Value* vcharset=user_env->get(CHARSET_EXEC_PARAM_NAME))
                    435:                                charset=&charsets.get(vcharset->as_string()
                    436:                                        .change_case(r.charsets.source(), String::CC_UPPER));
                    437: 
                    438:                        // $.others
                    439:                        Append_env_pair_info info={&r.charsets, &env, 0};
                    440:                        {
1.144     paf       441:                                // influence tainting
                    442:                                // main target -- $.QUERY_STRING -- URLencoding of tainted pieces to String::L_URI lang
1.141     paf       443:                                Temp_client_charset temp(r.charsets, charset? *charset: r.charsets.source());
1.143     paf       444:                                user_env->for_each<Append_env_pair_info*>(append_env_pair, &info);
1.141     paf       445:                        }
1.109     paf       446:                        // $.stdin
1.103     paf       447:                        if(info.vstdin) {
1.111     paf       448:                                stdin_specified=true;
                    449:                                if(const String* sstdin=info.vstdin->get_string()) {
                    450:                                        in->append(*sstdin, String::L_CLEAN, true);
1.103     paf       451:                                } else
1.199     misha     452:                                        if(VFile* vfile=static_cast<VFile *>(info.vstdin->as("file")))
1.111     paf       453:                                                in->append_know_length((const char* )vfile->value_ptr(), vfile->value_size(), String::L_TAINTED);
1.100     paf       454:                                        else
1.156     misha     455:                                                throw Exception(PARSER_RUNTIME,
1.111     paf       456:                                                        0,
1.100     paf       457:                                                        STDIN_EXEC_PARAM_NAME " parameter must be string or file");
1.103     paf       458:                        }
1.90      paf       459:                }
1.21      paf       460:        }
                    461: 
1.90      paf       462:        // argv from params
1.111     paf       463:        ArrayString argv;
1.162     misha     464:        if(param_index < params.count()) {
1.180     misha     465:                // influence tainting 
                    466:                Temp_client_charset temp(r.charsets, charset? *charset: r.charsets.source());
1.154     misha     467: 
1.162     misha     468:                for(size_t i=param_index; i<params.count(); i++) {
1.161     misha     469:                        Value& param=params.as_no_junction(i, PARAM_MUST_NOT_BE_CODE);
1.154     misha     470:                        if(param.is_defined()){
                    471:                                if(param.is_string()){
1.155     misha     472:                                        append_to_argv(r, argv, param.get_string());
1.154     misha     473:                                } else {
1.155     misha     474:                                        Table* table=param.get_table();
                    475:                                        if(table){
                    476:                                                for(size_t i=0; i<table->count(); i++) {
                    477:                                                        append_to_argv(r, argv, table->get(i)->get(0));
1.154     misha     478:                                                }
                    479:                                        } else {
1.156     misha     480:                                                throw Exception(PARSER_RUNTIME,
1.154     misha     481:                                                        0,
1.162     misha     482:                                                        "param must be string or table");
1.154     misha     483:                                        }
                    484:                                }
1.145     misha     485:                        }
1.144     paf       486:                }
1.21      paf       487:        }
1.90      paf       488: 
1.109     paf       489:        // transcode if necessary
                    490:        if(charset) {
1.111     paf       491:                Charset::transcode(env, r.charsets.source(), *charset);
                    492:                Charset::transcode(argv, r.charsets.source(), *charset);
                    493:                in=&Charset::transcode(*in, r.charsets.source(), *charset);
                    494:        }
                    495:        // @todo 
                    496:        // ifdef WIN32 do  OEM->ANSI transcode on some(.cmd?) programs to 
                    497:        // match silent conversion in OS
                    498: 
                    499:        // exec!
1.163     misha     500:        PA_exec_result execution=pa_exec(false/*forced_allow*/, script_name, &env, argv, *in);
1.111     paf       501: 
1.162     misha     502:        File_read_result *file_out=&execution.out;
1.111     paf       503:        String *real_err=&execution.err;
1.162     misha     504: 
1.165     misha     505:        // transcode err if necessary (@todo: need fix line breaks in err as well )
                    506:        if(charset)
                    507:                real_err=&Charset::transcode(*real_err, *charset, r.charsets.source());
                    508: 
1.194     misha     509:        if(file_out->length && as_text){
1.162     misha     510:                fix_line_breaks(file_out->str, file_out->length);
                    511:                // treat output as string
1.188     misha     512:                String *real_out = new String(file_out->str);
1.162     misha     513: 
1.165     misha     514:                // transcode out if necessary
                    515:                if(charset)
1.162     misha     516:                        real_out=&Charset::transcode(*real_out, *charset, r.charsets.source());
1.165     misha     517: 
1.162     misha     518:                // FIXME: unsafe cast
1.163     misha     519:                file_out->str=const_cast<char *>(real_out->cstr()); // hacking a little
1.162     misha     520:                file_out->length = real_out->length();
1.109     paf       521:        }
                    522: 
1.111     paf       523:        VFile& self=GET_SELF(r, VFile);
1.109     paf       524: 
1.162     misha     525:        if(cgi) { // ^file::cgi
1.163     misha     526:                const char* eol_marker=0;
                    527:                size_t eol_marker_size;
                    528: 
1.111     paf       529:                // construct with 'out' body and header
1.165     misha     530:                size_t dos_pos=(file_out->length)?strpos(file_out->str, "\r\n\r\n"):STRING_NOT_FOUND;
                    531:                size_t unix_pos=(file_out->length)?strpos(file_out->str, "\n\n"):STRING_NOT_FOUND;
1.111     paf       532: 
                    533:                bool unix_header_break;
                    534:                switch((dos_pos!=STRING_NOT_FOUND?10:00) + (unix_pos!=STRING_NOT_FOUND?01:00)) {
1.166     misha     535:                        case 10: // dos
                    536:                                unix_header_break=false;
                    537:                                break;
                    538:                        case 01: // unix
                    539:                                unix_header_break=true;
                    540:                                break;
                    541:                        case 11: // dos & unix
                    542:                                unix_header_break=unix_pos<dos_pos;
                    543:                                break;
                    544:                        default: // 00
                    545:                                unix_header_break=false; // calm down, compiler
1.179     misha     546:                                throw Exception("file.execute",
1.166     misha     547:                                        0,
                    548:                                        "output does not contain CGI header; "
                    549:                                        "exit status=%d; stdoutsize=%u; stdout: \"%s\"; stderrsize=%u; stderr: \"%s\"", 
                    550:                                                execution.status, 
1.194     misha     551:                                                file_out->length, (file_out->length) ? (file_out->str) : "",
                    552:                                                real_err->length(), real_err->cstr());
1.166     misha     553:                                break; //never reached
1.111     paf       554:                }
                    555: 
1.165     misha     556:                size_t header_break_pos;
1.111     paf       557:                if(unix_header_break) {
                    558:                        header_break_pos=unix_pos;
1.165     misha     559:                        eol_marker="\n";
                    560:                        eol_marker_size=1;
1.111     paf       561:                } else {
                    562:                        header_break_pos=dos_pos;
1.165     misha     563:                        eol_marker="\r\n";
                    564:                        eol_marker_size=2;
1.111     paf       565:                }
1.21      paf       566: 
1.162     misha     567:                file_out->str[header_break_pos] = 0;
1.188     misha     568:                String *header=new String(file_out->str);
1.162     misha     569:                unsigned long headersize = header_break_pos+eol_marker_size*2;
                    570:                file_out->str += headersize;
                    571:                file_out->length -= headersize;
                    572: 
1.164     misha     573:                // $body
                    574:                self.set(false/*not tainted*/, file_out->str, file_out->length);
                    575: 
1.162     misha     576:                // $fields << header
1.194     misha     577:                if(header) {
1.162     misha     578:                        ArrayString rows;
                    579:                        size_t pos_after=0;
                    580:                        header->split(rows, pos_after, eol_marker);
                    581:                        Pass_cgi_header_attribute_info info={0, 0, 0};
                    582:                        info.charset=&r.charsets.source();
                    583:                        info.fields=&self.fields();
                    584:                        rows.for_each(pass_cgi_header_attribute, &info);
                    585:                        if(info.content_type)
                    586:                                self.fields().put(content_type_name, info.content_type);
                    587:                }
1.164     misha     588:        } else { // ^file::exec
1.166     misha     589:                // $body
                    590:                self.set(false/*not tainted*/, file_out->str, file_out->length);
1.164     misha     591:        }
1.163     misha     592: 
1.194     misha     593:        self.set_mode(as_text);
                    594: 
1.42      parser    595:        // $status
1.111     paf       596:        self.fields().put(file_status_name, new VInt(execution.status));
1.21      paf       597:        
                    598:        // $stderr
1.187     misha     599:        if(!real_err->is_empty())
1.21      paf       600:                self.fields().put(
1.119     paf       601:                        String::Body("stderr"),
1.111     paf       602:                        new VString(*real_err));
1.21      paf       603: }
1.111     paf       604: static void _exec(Request& r, MethodParams& params) {
                    605:        _exec_cgi(r, params, false);
1.41      parser    606: }
1.111     paf       607: static void _cgi(Request& r, MethodParams& params) {
                    608:        _exec_cgi(r, params, true);
1.41      parser    609: }
                    610: 
1.111     paf       611: static void _list(Request& r, MethodParams& params) {
                    612:        Value& relative_path=params.as_no_junction(0, "path must not be code");
1.47      parser    613: 
1.191     misha     614:        VRegex* vregex=0;
1.184     misha     615:        VRegexCleaner vrcleaner;
                    616:        if(params.count()>1){
                    617:                Value& regexp=params.as_no_junction(1, "regexp must not be code");
1.191     misha     618:                if(regexp.is_defined()){
1.199     misha     619:                        if(Value* value=regexp.as(VREGEX_TYPE)){
1.191     misha     620:                                vregex=static_cast<VRegex*>(value);
                    621:                        } else {
                    622:                                vregex=new VRegex(r.charsets.source(), &regexp.as_string(), 0/*options*/);
                    623:                                vregex->study();
                    624:                                vrcleaner.vregex=vregex;
                    625:                        }
1.184     misha     626:                }
1.114     paf       627:        }
1.47      parser    628: 
1.196     misha     629:        const char* absolute_path_cstr=r.absolute(relative_path.as_string()).taint_cstr(String::L_FILE_SPEC);
1.47      parser    630: 
1.111     paf       631:        Table::columns_type columns(new ArrayString);
                    632:        *columns+=new String("name");
                    633:        Table& table=*new Table(columns);
1.47      parser    634: 
1.184     misha     635:        const int ovector_size=(1/*match*/)*3;
                    636:        int ovector[ovector_size];
                    637: 
1.47      parser    638:        LOAD_DIR(absolute_path_cstr, 
1.111     paf       639:                const char* file_name_cstr=ffblk.ff_name;
                    640:                size_t file_name_size=strlen(file_name_cstr);
1.47      parser    641: 
1.184     misha     642:                if(!vregex || vregex->exec(ffblk.ff_name, file_name_size, ovector, ovector_size)>=0) {
1.111     paf       643:                        Table::element_type row(new ArrayString);
1.190     misha     644:                        *row+=new String(pa_strdup(file_name_cstr, file_name_size), String::L_TAINTED);
1.111     paf       645:                        table+=row;
1.47      parser    646:                }
                    647:        );
                    648: 
1.60      parser    649:        // write out result
1.111     paf       650:        r.write_no_lang(*new VTable(&table));
1.47      parser    651: }
1.21      paf       652: 
1.69      paf       653: #ifndef DOXYGEN
                    654: struct Lock_execute_body_info {
1.111     paf       655:        Request* r;
                    656:        Value* body_code;
1.69      paf       657: };
                    658: #endif
1.111     paf       659: static void lock_execute_body(int , void *ainfo) {
                    660:        Lock_execute_body_info& info=*static_cast<Lock_execute_body_info *>(ainfo);
1.69      paf       661:        // execute body
1.78      paf       662:        info.r->write_assign_lang(info.r->process(*info.body_code));
1.69      paf       663: };
1.111     paf       664: static void _lock(Request& r, MethodParams& params) {
1.152     misha     665:        const String& file_spec=r.absolute(params.as_string(0, FILE_NAME_MUST_BE_STRING));
1.116     paf       666:        Lock_execute_body_info info={
                    667:                &r, 
1.117     paf       668:                &params.as_junction(1, "body must be code")
1.116     paf       669:        };
1.69      paf       670: 
1.158     misha     671:        file_write_action_under_lock(
                    672:                        file_spec,
                    673:                        "lock",
                    674:                        lock_execute_body,
                    675:                        &info);
1.69      paf       676: }
                    677: 
1.111     paf       678: static int lastposafter(const String& s, size_t after, const char* substr, size_t substr_size, bool beforelast=false) {
1.114     paf       679:        size_t size=0; // just to calm down compiler
1.89      paf       680:        if(beforelast)
1.111     paf       681:                size=s.length();
1.116     paf       682:        size_t at;
1.188     misha     683:        while((at=s.pos(String::Body(substr), after))!=STRING_NOT_FOUND) {
1.89      paf       684:                size_t newafter=at+substr_size/*skip substr*/;
                    685:                if(beforelast && newafter==size)
                    686:                        break;
                    687:                after=newafter;
                    688:        }
                    689: 
                    690:        return after;
                    691: }
                    692: 
1.111     paf       693: static void _find(Request& r, MethodParams& params) {
1.152     misha     694:        const String& file_name=params.as_no_junction(0, FILE_NAME_MUST_NOT_BE_CODE).as_string();
1.111     paf       695:        const String* file_spec;
1.90      paf       696:        if(file_name.first_char()=='/')
                    697:                file_spec=&file_name;
                    698:        else 
1.111     paf       699:                file_spec=&r.relative(r.request_info.uri, file_name);
1.90      paf       700: 
                    701:        // easy way
1.142     paf       702:        if(file_exist(r.absolute(*file_spec))) {
1.96      paf       703:                r.write_assign_lang(*file_spec);
1.90      paf       704:                return;
                    705:        }
                    706: 
                    707:        // monkey way
                    708:        int after_base_slash=lastposafter(*file_spec, 0, "/", 1);
1.111     paf       709:        const String* dirname=&file_spec->mid(0, after_base_slash);
                    710:        const String& basename=file_spec->mid(after_base_slash, file_spec->length());
1.90      paf       711: 
                    712:        int after_monkey_slash;
                    713:        while((after_monkey_slash=lastposafter(*dirname, 0, "/", 1, true))>0) {
1.111     paf       714:                String test_name;
                    715:                test_name<<*(dirname=&dirname->mid(0, after_monkey_slash));
                    716:                test_name<<basename;
1.142     paf       717:                if(file_exist(r.absolute(test_name))) {
1.111     paf       718:                        r.write_assign_lang(test_name);
1.90      paf       719:                        return;
                    720:                }
                    721:        }
                    722: 
                    723:        // no way, not found
1.111     paf       724:        if(params.count()==2) {
                    725:                Value& not_found_code=params.as_junction(1, "not-found param must be code");
1.90      paf       726:                r.write_pass_lang(r.process(not_found_code));
                    727:        }
                    728: }
                    729: 
1.111     paf       730: static void _dirname(Request& r, MethodParams& params) {
1.152     misha     731:        const String& file_spec=params.as_string(0, FILE_NAME_MUST_BE_STRING);
1.180     misha     732:        // /a/some.tar.gz > /a
1.89      paf       733:        // /a/b/ > /a
                    734:        int afterslash=lastposafter(file_spec, 0, "/", 1, true);
                    735:        if(afterslash>0)
                    736:                r.write_assign_lang(file_spec.mid(0, afterslash==1?1:afterslash-1));
                    737:        else
1.189     misha     738:                r.write_assign_lang(String("."));
1.89      paf       739: }
                    740: 
1.111     paf       741: static void _basename(Request& r, MethodParams& params) {
1.152     misha     742:        const String& file_spec=params.as_string(0, FILE_NAME_MUST_BE_STRING);
1.180     misha     743:        // /a/some.tar.gz > some.tar.gz
1.89      paf       744:        int afterslash=lastposafter(file_spec, 0, "/", 1);
1.111     paf       745:        r.write_assign_lang(file_spec.mid(afterslash, file_spec.length()));
1.89      paf       746: }
                    747: 
1.111     paf       748: static void _justname(Request& r, MethodParams& params) {
1.152     misha     749:        const String& file_spec=params.as_string(0, FILE_NAME_MUST_BE_STRING);
1.180     misha     750:        // /a/some.tar.gz > some.tar
1.89      paf       751:        int afterslash=lastposafter(file_spec, 0, "/", 1);
                    752:        int afterdot=lastposafter(file_spec, afterslash, ".", 1);
1.111     paf       753:        r.write_assign_lang(file_spec.mid(afterslash, afterdot!=afterslash?afterdot-1:file_spec.length()));
1.89      paf       754: }
1.111     paf       755: static void _justext(Request& r, MethodParams& params) {
1.152     misha     756:        const String& file_spec=params.as_string(0, FILE_NAME_MUST_BE_STRING);
1.180     misha     757:        // /a/some.tar.gz > gz
1.89      paf       758:        int afterdot=lastposafter(file_spec, 0, ".", 1);
                    759:        if(afterdot>0)
1.111     paf       760:                r.write_assign_lang(file_spec.mid(afterdot, file_spec.length()));
1.89      paf       761: }
                    762: 
1.111     paf       763: static void _fullpath(Request& r, MethodParams& params) {
1.152     misha     764:        const String& file_spec=params.as_string(0, FILE_NAME_MUST_BE_STRING);
1.111     paf       765:        const String* result;
1.102     paf       766:        if(file_spec.first_char()=='/')
                    767:                result=&file_spec;
                    768:        else {
                    769:                // /some/page.html: ^file:fullpath[a.gif] => /some/a.gif
                    770:                const String& full_disk_path=r.absolute(file_spec);
1.111     paf       771:                size_t document_root_length=strlen(r.request_info.document_root);
1.106     paf       772: 
                    773:                if(document_root_length>0) {
1.111     paf       774:                        char last_char=r.request_info.document_root[document_root_length-1];
1.106     paf       775:                        if(last_char == '/' || last_char == '\\')
                    776:                                --document_root_length;
                    777:                }
1.111     paf       778:                result=&full_disk_path.mid(document_root_length,  full_disk_path.length());
1.102     paf       779:        }
                    780:        r.write_assign_lang(*result);
                    781: }
                    782: 
1.121     paf       783: static void _sql_string(Request& r, MethodParams&) {
                    784:        VFile& self=GET_SELF(r, VFile);
                    785: 
                    786:        const char *quoted=r.connection()->quote(self.value_ptr(), self.value_size());
                    787:        r.write_assign_lang(*new String(quoted));
                    788: }
1.89      paf       789: 
1.122     paf       790: #ifndef DOXYGEN
                    791: class File_sql_event_handlers: public SQL_Driver_query_event_handlers {
                    792:        const String& statement_string; const char* statement_cstr;
                    793:        int got_columns;
                    794:        int got_cells;
                    795: public:
                    796:        String::C value;
1.131     paf       797:        const String* user_file_name;
                    798:        const String* user_content_type;
1.122     paf       799: public:
                    800:        File_sql_event_handlers(
                    801:                const String& astatement_string, const char* astatement_cstr):
                    802:                statement_string(astatement_string), statement_cstr(astatement_cstr),
                    803:                got_columns(0),
                    804:                got_cells(0),
                    805:                user_file_name(0),
                    806:                user_content_type(0) {}
                    807: 
                    808:        bool add_column(SQL_Error& error, const char* /*str*/, size_t /*length*/) {
                    809:                if(got_columns++==3) {
1.156     misha     810:                        error=SQL_Error(PARSER_RUNTIME, "result must contain not more then 3 columns");
1.122     paf       811:                        return true;
                    812:                }
                    813:                return false;
                    814:        }
                    815:        bool before_rows(SQL_Error& /*error*/ ) { /* ignore */ return false; }
                    816:        bool add_row(SQL_Error& /*error*/) { /* ignore */ return false; }
                    817:        bool add_row_cell(SQL_Error& error, const char* str, size_t length) {
                    818:                try {
                    819:                        switch(got_cells++) {
                    820:                                case 0:
                    821:                                        value=String::C(str, length); 
                    822:                                        break;
                    823:                                case 1:
1.131     paf       824:                                        if(!user_file_name) // user not specified?
1.190     misha     825:                                                user_file_name=new String(str, String::L_TAINTED);
1.122     paf       826:                                        break;
                    827:                                case 2:
1.131     paf       828:                                        if(!user_content_type) // user not specified?
1.190     misha     829:                                                user_content_type=new String(str, String::L_TAINTED);
1.122     paf       830:                                        break;
                    831:                                default:
1.156     misha     832:                                        error=SQL_Error(PARSER_RUNTIME, "result must not contain more then one row, three rows");
1.122     paf       833:                                        return true;
                    834:                        }
                    835:                        return false;
                    836:                } catch(...) {
                    837:                        error=SQL_Error("exception occured in File_sql_event_handlers::add_row_cell");
                    838:                        return true;
                    839:                }
                    840:        }
                    841: };
                    842: #endif
                    843: static void _sql(Request& r, MethodParams& params) {
1.131     paf       844:        Value& statement=params.as_junction(0, "statement must be code");
1.122     paf       845: 
                    846:        Temp_lang temp_lang(r, String::L_SQL);
                    847:        const String& statement_string=r.process_to_string(statement);
1.197     misha     848:        const char* statement_cstr=statement_string.untaint_cstr(r.flang, r.connection());
1.195     misha     849: 
1.122     paf       850:        File_sql_event_handlers handlers(statement_string, statement_cstr);
1.131     paf       851: 
1.173     misha     852:        ulong limit=SQL_NO_LIMIT;
1.172     misha     853:        ulong offset=0;
                    854: 
1.131     paf       855:        if(params.count()>1)
1.172     misha     856:                if(HashStringValue* options=params.as_no_junction(1, PARAM_MUST_NOT_BE_CODE).get_hash()){
1.131     paf       857:                        int valid_options=0;
                    858:                        if(Value* vfilename=options->get(NAME_NAME)) {
                    859:                                valid_options++;
                    860:                                handlers.user_file_name=&vfilename->as_string();
                    861:                        }
                    862:                        if(Value* vcontent_type=options->get(CONTENT_TYPE_NAME)) {
                    863:                                valid_options++;
                    864:                                handlers.user_content_type=&vcontent_type->as_string();
                    865:                        }
1.173     misha     866:                        if(Value* vlimit=options->get(sql_limit_name)) {
                    867:                                valid_options++;
                    868:                                limit=(ulong)r.process_to_value(*vlimit).as_double();
                    869:                        }
1.172     misha     870:                        if(Value* voffset=options->get(sql_offset_name)) {
                    871:                                valid_options++;
                    872:                                offset=(ulong)r.process_to_value(*voffset).as_double();
                    873:                        }
1.131     paf       874:                        if(valid_options!=options->count())
1.156     misha     875:                                throw Exception(PARSER_RUNTIME,
1.131     paf       876:                                        0,
                    877:                                        "called with invalid option");
                    878:                }
                    879: 
                    880: 
1.122     paf       881:        r.connection()->query(
1.123     paf       882:                statement_cstr, 
                    883:                0, 0,
1.173     misha     884:                offset, limit,
1.122     paf       885:                handlers,
                    886:                statement_string);
                    887: 
                    888:        if(!handlers.value)
1.156     misha     889:                throw Exception(PARSER_RUNTIME,
1.122     paf       890:                        0,
                    891:                        "produced no result");
                    892: 
1.131     paf       893:        const char* user_file_name_cstr=handlers.user_file_name? handlers.user_file_name->cstr(): 0;
1.122     paf       894: 
                    895:        VString* vcontent_type=handlers.user_content_type? 
                    896:                new VString(*handlers.user_content_type)
                    897:                : user_file_name_cstr?
                    898:                        new VString(r.mime_type_of(user_file_name_cstr))
                    899:                        : 0;
                    900:        VFile& self=GET_SELF(r, VFile);
                    901:        self.set(true/*tainted*/, handlers.value.str, handlers.value.length, user_file_name_cstr, vcontent_type);
1.194     misha     902:        self.set_mode(false/*binary*/);
1.122     paf       903: }
1.140     paf       904: 
1.139     paf       905: static void _base64(Request& r, MethodParams& params) {
1.151     misha     906:        bool dynamic = !(&r.get_self() == file_class);
1.180     misha     907:        if(dynamic){
                    908:                VFile& self=GET_SELF(r, VFile);
                    909:                if(params.count()) {
                    910:                        // decode: ^file::base64[encoded]
                    911:                        const char* cstr=params.as_string(0, PARAMETER_MUST_BE_STRING).cstr();
                    912:                        char* decoded=0;
                    913:                        size_t length=0;
                    914:                        pa_base64_decode(cstr, strlen(cstr), decoded, length);
                    915:                        if(decoded && length)
                    916:                                self.set(true/*tainted*/, decoded, length);
                    917:                } else {
                    918:                        // encode: ^f.base64[]
                    919:                        const char* encoded=pa_base64_encode(self.value_ptr(), self.value_size());
1.190     misha     920:                        r.write_assign_lang(*new String(encoded, String::L_TAINTED/*once ?param=base64(something) was needed**/));
1.180     misha     921:                }
1.151     misha     922:        } else {
1.180     misha     923:                // encode: ^file:base64[filespec]
1.152     misha     924:                const String& file_spec=params.as_string(0, FILE_NAME_MUST_BE_STRING);
1.151     misha     925:                const char* encoded=pa_base64_encode(r.absolute(file_spec));
1.190     misha     926:                r.write_assign_lang(*new String(encoded, String::L_TAINTED/*once ?param=base64(something) was needed*/));
1.151     misha     927:        }
1.139     paf       928: }
1.140     paf       929: 
1.146     misha     930: static void _crc32(Request& r, MethodParams& params) {
                    931:        unsigned long crc32 = 0;
                    932:        if(&r.get_self() == file_class) {
                    933:                // ^file:crc32[file-name]
                    934:                if(params.count()) {
1.152     misha     935:                        const String& file_spec=params.as_string(0, FILE_NAME_MUST_BE_STRING);
1.146     misha     936:                        crc32=pa_crc32(r.absolute(file_spec));
                    937:                } else {
1.156     misha     938:                        throw Exception(PARSER_RUNTIME,
1.146     misha     939:                                0,
                    940:                                "file name must be defined");
                    941:                }
                    942:        } else {
                    943:                // ^file.crc32[]
                    944:                VFile& self=GET_SELF(r, VFile);
                    945:                crc32=pa_crc32(self.value_ptr(), self.value_size());
                    946:        }
                    947:        r.write_no_lang(*new VInt(crc32));
                    948: }
                    949: 
                    950: 
1.147     misha     951: static void file_md5_file_action(
1.180     misha     952:                                struct stat& finfo, 
                    953:                                int f, 
                    954:                                const String& , const char* /*fname*/, bool, 
                    955:                                void *context)
1.147     misha     956: {
                    957:        PA_MD5_CTX& md5context=*static_cast<PA_MD5_CTX *>(context);
                    958:        if(finfo.st_size) {
1.148     misha     959:                int nCount=0;
1.147     misha     960:                do {
                    961:                        unsigned char buffer[FILE_BUFFER_SIZE];
1.150     misha     962:                        nCount = file_block_read(f, buffer, sizeof(buffer));
1.147     misha     963:                        if ( nCount ){
                    964:                                pa_MD5Update(&md5context, (const unsigned char*)buffer, nCount);
                    965:                        }
1.148     misha     966:                } while(nCount > 0);
1.147     misha     967:        }
                    968: }
                    969: 
                    970: const char* pa_md5(const String& file_spec)
                    971: {
                    972:        PA_MD5_CTX context;
                    973:        unsigned char digest[16];
                    974:        pa_MD5Init(&context);
                    975:        file_read_action_under_lock(file_spec, "md5", file_md5_file_action, &context);
                    976:        pa_MD5Final(digest, &context);
                    977:        
                    978:        return hex_string(digest, sizeof(digest), false);
                    979: }
                    980: 
                    981: const char* pa_md5(const char *in, size_t in_size)
                    982: {
                    983:        PA_MD5_CTX context;
                    984:        unsigned char digest[16];
                    985:        pa_MD5Init(&context);
                    986:        pa_MD5Update(&context, (const unsigned char*)in, in_size);
                    987:        pa_MD5Final(digest, &context);
                    988:        
                    989:        return hex_string(digest, sizeof(digest), false);
                    990: }
                    991: 
                    992: static void _md5(Request& r, MethodParams& params) {
                    993:        const char* md5;
                    994:        if(&r.get_self() == file_class) {
                    995:                // ^file:md5[file-name]
                    996:                if(params.count()) {
1.152     misha     997:                        const String& file_spec=params.as_string(0, FILE_NAME_MUST_BE_STRING);
1.147     misha     998:                        md5=pa_md5(r.absolute(file_spec));
                    999:                } else {
1.156     misha    1000:                        throw Exception(PARSER_RUNTIME,
1.147     misha    1001:                                0,
                   1002:                                "file name must be defined");
                   1003:                }
                   1004:        } else {
                   1005:                // ^file.md5[]
                   1006:                VFile& self=GET_SELF(r, VFile);
                   1007:                md5=pa_md5(self.value_ptr(), self.value_size());
                   1008: 
                   1009:        }
                   1010:        r.write_no_lang(*new String(md5));
                   1011: }
                   1012: 
1.32      paf      1013: // constructor
                   1014: 
1.111     paf      1015: MFile::MFile(): Methoded("file") {
1.146     misha    1016:        // ^file::create[text;user-name;string]
                   1017:        // ^file::create[binary;user-name;SOMEDAY SOMETHING]
1.138     paf      1018:        add_native_method("create", Method::CT_DYNAMIC, _create, 3, 3);
                   1019: 
1.146     misha    1020:        // ^file.save[mode;file-name]
1.48      parser   1021:        add_native_method("save", Method::CT_DYNAMIC, _save, 2, 2);
1.7       paf      1022: 
1.146     misha    1023:        // ^file:delete[file-name]
1.32      paf      1024:        add_native_method("delete", Method::CT_STATIC, _delete, 1, 1);
1.45      parser   1025: 
1.146     misha    1026:        // ^file:move[from-file-name;to-file-name]
1.45      parser   1027:        add_native_method("move", Method::CT_STATIC, _move, 2, 2);
1.8       paf      1028: 
1.146     misha    1029:        // ^file::load[mode;disk-name]
                   1030:        // ^file::load[mode;disk-name;user-name]
1.180     misha    1031:        add_native_method("load", Method::CT_DYNAMIC, _load, 2, 4);
1.25      paf      1032: 
1.146     misha    1033:        // ^file::stat[disk-name]
1.32      paf      1034:        add_native_method("stat", Method::CT_DYNAMIC, _stat, 1, 1);
1.21      paf      1035: 
1.162     misha    1036:        // ^file::cgi[mode;file-name]
                   1037:        // ^file::cgi[mode;file-name;env hash]
                   1038:        // ^file::cgi[mode;file-name;env hash;1cmd;2line;3ar;4g;5s]
                   1039:        add_native_method("cgi", Method::CT_DYNAMIC, _cgi, 1, 3+50);
                   1040: 
                   1041:        // ^file::exec[mode;file-name]
                   1042:        // ^file::exec[mode;file-name;env hash]
                   1043:        // ^file::exec[mode;file-name;env hash;1cmd;2line;3ar;4g;5s]
                   1044:        add_native_method("exec", Method::CT_DYNAMIC, _exec, 1, 3+50);
1.47      parser   1045: 
                   1046:        // ^file:list[path]
                   1047:        // ^file:list[path][regexp]
                   1048:        add_native_method("list", Method::CT_STATIC, _list, 1, 2);
1.69      paf      1049: 
                   1050:        // ^file:lock[path]{code}
                   1051:        add_native_method("lock", Method::CT_STATIC, _lock, 2, 2);
1.90      paf      1052: 
1.146     misha    1053:        // ^file:find[file-name]
                   1054:        // ^file:find[file-name]{when-not-found}
1.90      paf      1055:        add_native_method("find", Method::CT_STATIC, _find, 1, 2);
1.47      parser   1056: 
1.89      paf      1057:     // ^file:dirname[/a/some.tar.gz]=/a
                   1058:        // ^file:dirname[/a/b/]=/a
                   1059:        add_native_method("dirname", Method::CT_STATIC, _dirname, 1, 1);
                   1060:     // ^file:basename[/a/some.tar.gz]=some.tar.gz
                   1061:     add_native_method("basename", Method::CT_STATIC, _basename, 1, 1);
                   1062:     // ^file:justname[/a/some.tar.gz]=some.tar
                   1063:        add_native_method("justname", Method::CT_STATIC, _justname, 1, 1);
                   1064:     // ^file:justext[/a/some.tar.gz]=gz
                   1065:        add_native_method("justext", Method::CT_STATIC, _justext, 1, 1);
1.102     paf      1066:     // /some/page.html: ^file:fullpath[a.gif] => /some/a.gif
                   1067:        add_native_method("fullpath", Method::CT_STATIC, _fullpath, 1, 1);
1.121     paf      1068: 
                   1069:     // ^file.sql-string[]
                   1070:        add_native_method("sql-string", Method::CT_DYNAMIC, _sql_string, 0, 0);
1.122     paf      1071: 
                   1072:     // ^file::sql[[alt_name]]{}
                   1073:        add_native_method("sql", Method::CT_DYNAMIC, _sql, 1, 2);
1.139     paf      1074: 
1.146     misha    1075:        // ^file::base64[string] << decode
1.139     paf      1076:        // ^file.base64[] << encode
1.151     misha    1077:        // ^file:base64[file-name] << encode
                   1078:        add_native_method("base64", Method::CT_ANY, _base64, 0, 1);
1.146     misha    1079: 
                   1080:        // ^file.crc32[]
                   1081:        // ^file:crc32[file-name]
                   1082:        add_native_method("crc32", Method::CT_ANY, _crc32, 0, 1);
1.147     misha    1083: 
                   1084:        // ^file.md5[]
                   1085:        // ^file:md5[file-name]
                   1086:        add_native_method("md5", Method::CT_ANY, _md5, 0, 1);
                   1087: 
1.148     misha    1088:        // ^file:copy[from-file-name;to-file-name]
                   1089:        add_native_method("copy", Method::CT_STATIC, _copy, 2, 2);
1.1       paf      1090: }

E-mail: