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

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.  9(paf       8:3): static const char* IDENT_FILE_C="$Date: 2003/05/30 09:47:11 $";
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.  3(paf     105:3): static void _save(Request& r, MethodParams& params) {
                    106:3):     Value& vmode_name=params. as_no_junction(0, "mode must not be code");
                    107:3):     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.  3(paf     114:3): static void _delete(Request& r, MethodParams& params) {
                    115:3):     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.  3(paf     121:3): static void _move(Request& r, MethodParams& params) {
                    122:3):     Value& vfrom_file_name=params.as_no_junction(0, "from file name must not be code");
                    123:3):     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.  3(paf     137:3): static void _load(Request& r, MethodParams& params) {
                    138:3):     Value& vmode_name=params. as_no_junction(0, "mode must not be code");
                    139:3):     const String& lfile_name=r.absolute(params.as_no_junction(1, "file name must not be code").as_string());
                    140:3):     Value* third_param=params.count()>2?&params.as_no_junction(2, "filename or options must not be code")
          (paf      141:):              :0;
                    142:):      HashStringValue* third_param_hash=third_param?third_param->get_hash():0;
          1(paf     143:3):     size_t alt_filename_param_index=2;
1.104     paf       144:        if(third_param_hash)
                    145:                alt_filename_param_index++;
1.9       paf       146: 
1.107.2.16.2.  5(paf     147:3):     File_read_result file=file_read(r.charsets, lfile_name,
          (paf      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.  3(paf     152:3):     const char *user_file_name=params.count()>alt_filename_param_index?
                    153:3):             params.as_string(alt_filename_param_index, "filename must be string").cstr()
          (paf      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.  3(paf     168:3): static void _stat(Request& r, MethodParams& params) {
                    169:3):     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(
1.107.2.16.2.  (paf      227:):                                    ArrayString::element_type astring, 
                    228:):                                    Pass_cgi_header_attribute_info* info) {
          0(paf     229:3):     size_t colon_pos=astring->pos(':');
          (paf      230:):      if(colon_pos==STRING_NOT_FOUND) {
                    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.  3(paf     240:3): static void _exec_cgi(Request& r, MethodParams& params,
1.41      parser    241:                                          bool cgi) {
1.21      paf       242: 
1.107.2.16.2.  3(paf     243:3):     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), \
          8(paf     252:3):                             StringBody(value_cstr, 0)); \
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),
          8(paf     259:3):                                     StringBody(eq_at+1, 0));
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.16.2.  3(paf     282:3):     if(params.count()>1) {
                    283:3):             Value& venv=params.as_no_junction(1, "env must not be code");
          (paf      284:):              if(HashStringValue* user_env=venv.get_hash()) {
          6(paf     285:3):                     Append_env_pair_info info={&env};
1.90      paf       286:                        user_env->for_each(append_env_pair, &info);
1.103     paf       287:                        if(info.vstdin) {
                    288:                                stdin_specified=true;
1.107.2.16.2.  (paf      289:):                              if(const String* sstdin=info.vstdin->get_string()) {
                    290:):                                      in.append(*sstdin, String::L_CLEAN, true);
1.103     paf       291:                                } else
1.107.2.16.2.  (paf      292:):                                      if(VFile* vfile=static_cast<VFile *>(info.vstdin->as("file", false)))
          4(paf     293:3):                                             in.append_know_length((const char* )vfile->value_ptr(), vfile->value_size(), String::L_TAINTED);
1.100     paf       294:                                        else
                    295:                                                throw Exception("parser.runtime",
1.107.2.16.2.  (paf      296:):                                                      0,
1.100     paf       297:                                                        STDIN_EXEC_PARAM_NAME " parameter must be string or file");
1.103     paf       298:                        }
1.90      paf       299:                }
1.21      paf       300:        }
                    301: 
1.90      paf       302:        // argv from params
1.107.2.7  paf       303:        ArrayString argv;
1.107.2.16.2.  3(paf     304:3):     if(params.count()>2) {
                    305:3):             for(size_t i=2; i<params.count(); i++)
                    306:3):                     argv+=&params.as_string(i, "parameter must be string");
1.21      paf       307:        }
                    308: 
1.90      paf       309:        // passing POST data
1.107.2.16.2.  2(paf     310:3):     if(!stdin_specified) // if $.stdin[...] not specified  @todo zeroterminate post_data!
          4(paf     311:3):             in.append_know_length(r.request_info.post_data, r.request_info.post_size, String::L_CLEAN);
1.90      paf       312: 
                    313:        // exec!
1.107.2.7  paf       314:        PA_exec_result execution=
1.107.2.16.2.  (paf      315:):              pa_exec(false/*forced_allow*/, script_name, &env, argv, in);
1.21      paf       316: 
1.107.2.7  paf       317:        VFile& self=GET_SELF(r, VFile);
1.21      paf       318: 
1.107.2.16.2.  (paf      319:):      const String* body=&execution.out; // ^file:exec
                    320:):      Value* content_type=0;
1.107.2.3  paf       321:        const char* eol_marker=0; size_t eol_marker_size;
1.107.2.16.2.  (paf      322:):      const String* header=0;
1.41      parser    323:        if(cgi) { // ^file:cgi
                    324:                // construct with 'out' body and header
1.107.2.16.2.  (paf      325:):              size_t dos_pos=execution.out.pos("\r\n\r\n", 4);
                    326:):              size_t unix_pos=execution.out.pos("\n\n", 2);
1.98      paf       327: 
                    328:                bool unix_header_break;
1.107.2.16.2.  (paf      329:):              switch((dos_pos!=STRING_NOT_FOUND?10:00) + (unix_pos!=STRING_NOT_FOUND?01:00)) {
1.98      paf       330:                case 10: // dos
                    331:                        unix_header_break=false;
                    332:                        break;
                    333:                case 01: // unix
                    334:                        unix_header_break=true;
                    335:                        break;
                    336:                case 11: // dos & unix
                    337:                        unix_header_break=unix_pos<dos_pos;
                    338:                        break;
                    339:                default: // 00
                    340:                        unix_header_break=false; // calm down, compiler
1.74      paf       341:                        throw Exception(0,
1.107.2.16.2.  (paf      342:):                              0,
1.90      paf       343:                                "output does not contain CGI header; "
                    344:                                "exit status=%d; stdoutsize=%u; stdout: \"%s\"; stderrsize=%u; stderr: \"%s\"", 
1.107.2.7  paf       345:                                        execution.status, 
1.107.2.16.2.  (paf      346:):                                      (uint)execution.out.length(), execution.out.cstr(),
                    347:):                                      (uint)execution.err.length(), execution.err.cstr());
1.98      paf       348:                        break; //never reached
                    349:                }
                    350: 
                    351:                int header_break_pos;
                    352:                if(unix_header_break) {
                    353:                        header_break_pos=unix_pos;
                    354:                        eol_marker="\n"; eol_marker_size=1;
                    355:                } else {
                    356:                        header_break_pos=dos_pos;
                    357:                        eol_marker="\r\n"; eol_marker_size=2;
1.41      parser    358:                }
1.21      paf       359: 
1.107.2.16.2.  (paf      360:):              header=&execution.out.mid(0, header_break_pos);
                    361:):              body=&execution.out.mid(header_break_pos+eol_marker_size*2, execution.out.length());
1.29      paf       362:        }
1.41      parser    363:        // body
1.107.2.16.2.  (paf      364:):      self.set(false/*not tainted*/, body->cstr(), body->length());
1.94      paf       365: 
                    366:        // $fields << header
1.98      paf       367:        if(header && eol_marker) {
1.107.2.7  paf       368:                ArrayString rows;
1.107.2.16.2.  (paf      369:):              size_t pos_after=0;
                    370:):              header->split(rows, pos_after, eol_marker);
          9(paf     371:3):             Pass_cgi_header_attribute_info info={0};
1.107.2.7  paf       372:                info.charset=&r.charsets.source();
                    373:                info.fields=&self.fields();
1.94      paf       374:                rows.for_each(pass_cgi_header_attribute, &info);
                    375:                if(info.content_type)
1.107.2.7  paf       376:                        self.fields().put(content_type_name, info.content_type);
1.94      paf       377:        }
1.21      paf       378: 
1.42      parser    379:        // $status
1.107.2.16.2.  (paf      380:):      self.fields().put(file_status_name, new VInt(execution.status));
1.21      paf       381:        
                    382:        // $stderr
1.107.2.16.2.  (paf      383:):      if(execution.err.length())
1.21      paf       384:                self.fields().put(
1.107.2.16.2.  (paf      385:):                      StringBody("stderr"),
                    386:):                      new VString(execution.err));
1.21      paf       387: }
1.107.2.16.2.  3(paf     388:3): static void _exec(Request& r, MethodParams& params) {
          (paf      389:):      _exec_cgi(r, params, false);
1.41      parser    390: }
1.107.2.16.2.  3(paf     391:3): static void _cgi(Request& r, MethodParams& params) {
          (paf      392:):      _exec_cgi(r, params, true);
1.41      parser    393: }
                    394: 
1.107.2.16.2.  3(paf     395:3): static void _list(Request& r, MethodParams& params) {
                    396:3):     Value& relative_path=params.as_no_junction(0, "path must not be code");
1.47      parser    397: 
1.107.2.16.2.  (paf      398:):      const String* regexp;
1.47      parser    399:        pcre *regexp_code;
1.81      paf       400:        const int ovecsize=(1/*match*/)*3;
                    401:        int ovector[ovecsize];
1.107.2.16.2.  3(paf     402:3):     if(params.count()>1) {
                    403:3):             regexp=&params.as_no_junction(1, "regexp must not be code").as_string();
1.47      parser    404: 
1.107.2.16.2.  (paf      405:):              const char* pattern=regexp->cstr();
1.107.2.3  paf       406:                const char* errptr;
1.47      parser    407:                int erroffset;
                    408:                regexp_code=pcre_compile(pattern, PCRE_EXTRA | PCRE_DOTALL, 
                    409:                        &errptr, &erroffset, 
1.107.2.7  paf       410:                        r.charsets.source().pcre_tables);
1.47      parser    411: 
                    412:                if(!regexp_code)
1.74      paf       413:                        throw Exception(0, 
1.107.2.16.2.  (paf      414:):                              &regexp->mid(erroffset, regexp->length()), 
1.47      parser    415:                                "regular expression syntax error - %s", errptr);
                    416:        } else 
                    417:                regexp_code=0;
                    418: 
                    419: 
1.107.2.16.2.  (paf      420:):      const char* absolute_path_cstr=r.absolute(relative_path.as_string()).cstr(String::L_FILE_SPEC);
1.47      parser    421: 
1.107.2.7  paf       422:        Table::columns_type columns(new ArrayString);
1.107.2.16.2.  (paf      423:):      *columns+=new String("name");
                    424:):      Table& table=*new Table(columns);
1.47      parser    425: 
                    426:        LOAD_DIR(absolute_path_cstr, 
1.107.2.7  paf       427:                const char* file_name_cstr=ffblk.ff_name;
                    428:                size_t file_name_size=strlen(file_name_cstr);
1.47      parser    429:                bool suits=true;
                    430:                if(regexp_code) {
                    431:                        int exec_result=pcre_exec(regexp_code, 0, 
                    432:                                ffblk.ff_name, file_name_size, 0, 
                    433:                                0, ovector, ovecsize);
                    434:                        
                    435:                        if(exec_result==PCRE_ERROR_NOMATCH)
                    436:                                suits=false;
                    437:                        else if(exec_result<0) {
                    438:                                (*pcre_free)(regexp_code);
1.74      paf       439:                                throw Exception(0, 
1.47      parser    440:                                        regexp, 
                    441:                                        "regular expression execute (%d)", 
                    442:                                                exec_result);
                    443:                        }
                    444:                }
                    445: 
                    446:                if(suits) {
1.107.2.7  paf       447:                        Table::element_type row(new ArrayString);
1.107.2.16.2.  (paf      448:):                      *row+=new String(pa_strdup(file_name_cstr, file_name_size), file_name_size, true);
                    449:):                      table+=row;
1.47      parser    450:                }
                    451:        );
                    452: 
                    453:        if(regexp_code)
1.107.2.7  paf       454:                pcre_free(regexp_code);
1.47      parser    455: 
1.60      parser    456:        // write out result
1.107.2.16.2.  (paf      457:):      r.write_no_lang(*new VTable(&table));
1.47      parser    458: }
1.21      paf       459: 
1.69      paf       460: #ifndef DOXYGEN
                    461: struct Lock_execute_body_info {
1.107.2.7  paf       462:        Request* r;
1.107.2.16.2.  (paf      463:):      Value* body_code;
1.69      paf       464: };
                    465: #endif
1.107.2.7  paf       466: static void lock_execute_body(int , void *ainfo) {
                    467:        Lock_execute_body_info& info=*static_cast<Lock_execute_body_info *>(ainfo);
1.69      paf       468:        // execute body
1.107.2.16.2.  (paf      469:):      info.r->write_assign_lang(info.r->process(*info.body_code));
1.69      paf       470: };
1.107.2.16.2.  3(paf     471:3): static void _lock(Request& r, MethodParams& params) {
          9(paf     472:3):     Lock_execute_body_info info={0};
1.107.2.7  paf       473:        info.r=&r;
1.107.2.16.2.  3(paf     474:3):     const String& file_spec=r.absolute(params.as_string(0, "file name must be string"));
                    475:3):     info.body_code=&params.as_junction(1, "body must be code");
1.69      paf       476: 
1.70      paf       477:        file_write_action_under_lock(file_spec, "lock", lock_execute_body, &info);
1.69      paf       478: }
                    479: 
1.107.2.16.2.  (paf      480:): static int lastposafter(const String& s, size_t after, const char* substr, size_t substr_size, bool beforelast=false) {
1.89      paf       481:        size_t size;
                    482:        if(beforelast)
1.107.2.16.2.  (paf      483:):              size=s.length();
1.89      paf       484:        int at;
1.107.2.16.2.  7(paf     485:3):     while((at=s.pos(StringBody(substr, substr_size), after))!=STRING_NOT_FOUND) {
1.89      paf       486:                size_t newafter=at+substr_size/*skip substr*/;
                    487:                if(beforelast && newafter==size)
                    488:                        break;
                    489:                after=newafter;
                    490:        }
                    491: 
                    492:        return after;
                    493: }
                    494: 
1.107.2.16.2.  3(paf     495:3): static void _find(Request& r, MethodParams& params) {
                    496:3):     const String& file_name=params.as_no_junction(0, "file name must not be code").as_string();
          (paf      497:):      const String* file_spec;
                    498:):      if(file_name.first_char()=='/')
                    499:):              file_spec=&file_name;
1.90      paf       500:        else 
1.107.2.16.2.  (paf      501:):              file_spec=&r.relative(r.request_info.uri, file_name);
1.90      paf       502: 
                    503:        // easy way
1.107.2.16.2.  (paf      504:):      if(file_readable(r.absolute(*file_spec))) {
1.96      paf       505:                r.write_assign_lang(*file_spec);
1.90      paf       506:                return;
                    507:        }
                    508: 
                    509:        // monkey way
                    510:        int after_base_slash=lastposafter(*file_spec, 0, "/", 1);
1.107.2.16.2.  (paf      511:):      const String* dirname=&file_spec->mid(0, after_base_slash);
                    512:):      const String& basename=file_spec->mid(after_base_slash, file_spec->length());
1.90      paf       513: 
                    514:        int after_monkey_slash;
                    515:        while((after_monkey_slash=lastposafter(*dirname, 0, "/", 1, true))>0) {
1.107.2.16.2.  (paf      516:):              String test_name;
                    517:):              test_name<<*(dirname=&dirname->mid(0, after_monkey_slash));
                    518:):              test_name<<basename;
1.107.2.7  paf       519:                if(file_readable(r.absolute(test_name))) {
1.107.2.16.2.  (paf      520:):                      r.write_assign_lang(test_name);
1.90      paf       521:                        return;
                    522:                }
                    523:        }
                    524: 
                    525:        // no way, not found
1.107.2.16.2.  3(paf     526:3):     if(params.count()==2) {
                    527:3):             Value& not_found_code=params.as_junction(1, "not-found param must be code");
1.90      paf       528:                r.write_pass_lang(r.process(not_found_code));
                    529:        }
                    530: }
                    531: 
1.107.2.16.2.  3(paf     532:3): static void _dirname(Request& r, MethodParams& params) {
                    533:3):     const String& file_spec=params.as_string(0, "file name must be string");
1.89      paf       534:     // /a/some.tar.gz > /a
                    535:        // /a/b/ > /a
1.107.2.16.2.  (paf      536:):      int afterslash=lastposafter(file_spec, 0, "/", 1, true);
1.89      paf       537:        if(afterslash>0)
1.107.2.16.2.  (paf      538:):              r.write_assign_lang(file_spec.mid(0, afterslash==1?1:afterslash-1));
1.89      paf       539:        else
1.107.2.7  paf       540:                r.write_assign_lang(String(".", 1));
1.89      paf       541: }
                    542: 
1.107.2.16.2.  3(paf     543:3): static void _basename(Request& r, MethodParams& params) {
                    544:3):     const String& file_spec=params.as_string(0, "file name must be string");
1.89      paf       545:     // /a/some.tar.gz > some.tar.gz
1.107.2.16.2.  (paf      546:):      int afterslash=lastposafter(file_spec, 0, "/", 1);
                    547:):      r.write_assign_lang(file_spec.mid(afterslash, file_spec.length()));
1.89      paf       548: }
                    549: 
1.107.2.16.2.  3(paf     550:3): static void _justname(Request& r, MethodParams& params) {
                    551:3):     const String& file_spec=params.as_string(0, "file name must be string");
1.89      paf       552:     // /a/some.tar.gz > some.tar
1.107.2.16.2.  (paf      553:):      int afterslash=lastposafter(file_spec, 0, "/", 1);
                    554:):      int afterdot=lastposafter(file_spec, afterslash, ".", 1);
                    555:):      r.write_assign_lang(file_spec.mid(afterslash, afterdot!=afterslash?afterdot-1:file_spec.length()));
1.89      paf       556: }
1.107.2.16.2.  3(paf     557:3): static void _justext(Request& r, MethodParams& params) {
                    558:3):     const String& file_spec=params.as_string(0, "file name must be string");
1.89      paf       559:     // /a/some.tar.gz > gz
1.107.2.16.2.  (paf      560:):      int afterdot=lastposafter(file_spec, 0, ".", 1);
1.89      paf       561:        if(afterdot>0)
1.107.2.16.2.  (paf      562:):              r.write_assign_lang(file_spec.mid(afterdot, file_spec.length()));
1.89      paf       563: }
                    564: 
1.107.2.16.2.  3(paf     565:3): static void _fullpath(Request& r, MethodParams& params) {
                    566:3):     const String& file_spec=params.as_string(0, "file name must be string");
          (paf      567:):      const String* result;
                    568:):      if(file_spec.first_char()=='/')
                    569:):              result=&file_spec;
1.102     paf       570:        else {
                    571:                // /some/page.html: ^file:fullpath[a.gif] => /some/a.gif
1.107.2.16.2.  (paf      572:):              const String& full_disk_path=r.absolute(file_spec);
1.107.2.7  paf       573:                size_t document_root_length=strlen(r.request_info.document_root);
1.106     paf       574: 
                    575:                if(document_root_length>0) {
1.107.2.7  paf       576:                        char last_char=r.request_info.document_root[document_root_length-1];
1.106     paf       577:                        if(last_char == '/' || last_char == '\\')
                    578:                                --document_root_length;
                    579:                }
1.107.2.16.2.  (paf      580:):              result=&full_disk_path.mid(document_root_length,  full_disk_path.length());
1.102     paf       581:        }
                    582:        r.write_assign_lang(*result);
                    583: }
                    584: 
1.89      paf       585: 
1.32      paf       586: // constructor
                    587: 
1.107.2.7  paf       588: MFile::MFile(): Methoded("file") {
1.48      parser    589:        // ^save[mode;file-name]
                    590:        add_native_method("save", Method::CT_DYNAMIC, _save, 2, 2);
1.7       paf       591: 
                    592:        // ^delete[file-name]
1.32      paf       593:        add_native_method("delete", Method::CT_STATIC, _delete, 1, 1);
1.45      parser    594: 
                    595:        // ^move[from-file-name;to-file-name]
                    596:        add_native_method("move", Method::CT_STATIC, _move, 2, 2);
1.8       paf       597: 
1.48      parser    598:        // ^load[mode;disk-name]
                    599:        // ^load[mode;disk-name;user-name]
                    600:        add_native_method("load", Method::CT_DYNAMIC, _load, 2, 3);
1.25      paf       601: 
                    602:        // ^stat[disk-name]
1.32      paf       603:        add_native_method("stat", Method::CT_DYNAMIC, _stat, 1, 1);
1.21      paf       604: 
1.36      paf       605:        // ^cgi[file-name]
                    606:        // ^cgi[file-name;env hash]
                    607:        // ^cgi[file-name;env hash;1cmd;2line;3ar;4g;5s]
1.41      parser    608:        add_native_method("cgi", Method::CT_DYNAMIC, _cgi, 1, 2+10);
                    609: 
                    610:        // ^exec[file-name]
                    611:        // ^exec[file-name;env hash]
                    612:        // ^exec[file-name;env hash;1cmd;2line;3ar;4g;5s]
                    613:        add_native_method("exec", Method::CT_DYNAMIC, _exec, 1, 2+10);
1.47      parser    614: 
                    615:        // ^file:list[path]
                    616:        // ^file:list[path][regexp]
                    617:        add_native_method("list", Method::CT_STATIC, _list, 1, 2);
1.69      paf       618: 
                    619:        // ^file:lock[path]{code}
                    620:        add_native_method("lock", Method::CT_STATIC, _lock, 2, 2);
1.90      paf       621: 
                    622:        // ^find[file-name]
                    623:        // ^find[file-name]{when-not-found}
                    624:        add_native_method("find", Method::CT_STATIC, _find, 1, 2);
1.47      parser    625: 
1.89      paf       626:     // ^file:dirname[/a/some.tar.gz]=/a
                    627:        // ^file:dirname[/a/b/]=/a
                    628:        add_native_method("dirname", Method::CT_STATIC, _dirname, 1, 1);
                    629:     // ^file:basename[/a/some.tar.gz]=some.tar.gz
                    630:     add_native_method("basename", Method::CT_STATIC, _basename, 1, 1);
                    631:     // ^file:justname[/a/some.tar.gz]=some.tar
                    632:        add_native_method("justname", Method::CT_STATIC, _justname, 1, 1);
                    633:     // ^file:justext[/a/some.tar.gz]=gz
                    634:        add_native_method("justext", Method::CT_STATIC, _justext, 1, 1);
1.102     paf       635:     // /some/page.html: ^file:fullpath[a.gif] => /some/a.gif
                    636:        add_native_method("fullpath", Method::CT_STATIC, _fullpath, 1, 1);
1.1       paf       637: }

E-mail: