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

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

E-mail: