--- parser3/src/classes/file.C 2009/09/08 09:12:47 1.201 +++ parser3/src/classes/file.C 2010/07/05 05:54:46 1.207 @@ -5,7 +5,7 @@ Author: Alexandr Petrosian (http://paf.design.ru) */ -static const char * const IDENT_FILE_C="$Date: 2009/09/08 09:12:47 $"; +static const char * const IDENT_FILE_C="$Date: 2010/07/05 05:54:46 $"; #include "pa_config_includes.h" @@ -115,7 +115,7 @@ static bool is_valid_mode (const String& return (mode==text_mode_name || mode==binary_mode_name); } -static bool is_text_mode(const String& mode) { +bool is_text_mode(const String& mode) { if(mode==text_mode_name) return true; if(mode==binary_mode_name) @@ -132,15 +132,13 @@ static void _save(Request& r, MethodPara Charset* asked_charset=0; if(params.count()>2) if(HashStringValue* options=params.as_no_junction(2, OPTIONS_MUST_NOT_BE_CODE).get_hash()){ - size_t valid_options=0; + int valid_options=0; if(Value* vcharset_name=options->get(PA_CHARSET_NAME)){ asked_charset=&::charsets.get(vcharset_name->as_string().change_case(r.charsets.source(), String::CC_UPPER)); valid_options++; } if(valid_options != options->count()) - throw Exception(PARSER_RUNTIME, - 0, - INVALID_OPTION_PASSED); + throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION); } // save @@ -289,7 +287,24 @@ static void _create(Request& r, MethodPa params.as_no_junction(1, FILE_NAME_MUST_NOT_BE_CODE).as_string()).taint_cstr(String::L_FILE_SPEC); const String& content=params.as_string(2, "content must be string"); - const String::Body content_body=content.cstr_to_string_body_untaint(String::L_AS_IS); // explode content, honor tainting changes + String::Body content_body=content.cstr_to_string_body_untaint(String::L_AS_IS); // explode content, honor tainting changes + + if(params.count()>3){ + Charset* asked_charset=0; + + if(HashStringValue* options=params.as_no_junction(3, OPTIONS_MUST_NOT_BE_CODE).get_hash()){ + int valid_options=0; + if(Value* vcharset_name=options->get(PA_CHARSET_NAME)){ + asked_charset=&::charsets.get(vcharset_name->as_string().change_case(r.charsets.source(), String::CC_UPPER)); + valid_options++; + } + if(valid_options != options->count()) + throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION); + } + + if(asked_charset != 0) + content_body=Charset::transcode(content_body, r.charsets.source(), *asked_charset); + } VString* vcontent_type=new VString(r.mime_type_of(user_file_name_cstr)); @@ -327,6 +342,7 @@ static bool is_safe_env_key(const char* if(!(c>='A' && c<='Z' || c>='0' && c<='9' || c=='_' || c=='-')) return false; } +#ifdef PA_SAFE_MODE if(strncasecmp(key, "HTTP_", 5)==0) return true; if(strncasecmp(key, "CGI_", 4)==0) @@ -336,6 +352,9 @@ static bool is_safe_env_key(const char* return true; } return false; +#else + return true; +#endif } #ifndef DOXYGEN struct Append_env_pair_info { @@ -688,21 +707,6 @@ static void _lock(Request& r, MethodPara &info); } -static int lastposafter(const String& s, size_t after, const char* substr, size_t substr_size, bool beforelast=false) { - size_t size=0; // just to calm down compiler - if(beforelast) - size=s.length(); - size_t at; - while((at=s.pos(String::Body(substr), after))!=STRING_NOT_FOUND) { - size_t newafter=at+substr_size/*skip substr*/; - if(beforelast && newafter==size) - break; - after=newafter; - } - - return after; -} - static void _find(Request& r, MethodParams& params) { const String& file_name=params.as_no_junction(0, FILE_NAME_MUST_NOT_BE_CODE).as_string(); const String* file_spec; @@ -885,9 +889,7 @@ static void _sql(Request& r, MethodParam offset=(ulong)r.process_to_value(*voffset).as_double(); } if(valid_options!=options->count()) - throw Exception(PARSER_RUNTIME, - 0, - "called with invalid option"); + throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION); } @@ -1028,7 +1030,7 @@ static void _md5(Request& r, MethodParam MFile::MFile(): Methoded("file") { // ^file::create[text;user-name;string] // ^file::create[binary;user-name;SOMEDAY SOMETHING] - add_native_method("create", Method::CT_DYNAMIC, _create, 3, 3); + add_native_method("create", Method::CT_DYNAMIC, _create, 3, 4); // ^file.save[mode;file-name] // ^file.save[mode;file-name;$.charset[...]]