--- parser3/src/classes/file.C 2017/05/17 14:22:11 1.263 +++ parser3/src/classes/file.C 2019/11/23 23:48:40 1.271 @@ -9,6 +9,7 @@ #include "classes.h" #include "pa_vmethod_frame.h" +#include "pa_base64.h" #include "pa_request.h" #include "pa_vfile.h" @@ -25,7 +26,7 @@ #include "pa_vregex.h" #include "pa_version.h" -volatile const char * IDENT_FILE_C="$Id: file.C,v 1.263 2017/05/17 14:22:11 moko Exp $"; +volatile const char * IDENT_FILE_C="$Id: file.C,v 1.271 2019/11/23 23:48:40 moko Exp $"; // defines @@ -217,6 +218,18 @@ static void _copy(Request& r, MethodPara Value& vfrom_file_name=params.as_no_junction(0, "from file name must not be code"); Value& vto_file_name=params.as_no_junction(1, "to file name must not be code"); + bool append=false; + if(params.count()>2) + if(HashStringValue* options=params.as_hash(2)){ + int valid_options=0; + if(Value* vappend=options->get("append")){ + append=r.process(*vappend).as_bool(); + valid_options++; + } + if(valid_options != options->count()) + throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION); + } + String from_spec = r.absolute(vfrom_file_name.as_string()); const String& to_spec = r.absolute(vto_file_name.as_string()); @@ -224,7 +237,9 @@ static void _copy(Request& r, MethodPara to_spec, "copy", copy_open_target, - &from_spec); + &from_spec, + false /*as text*/, + append); } static void _load_pass_param( @@ -257,22 +272,7 @@ static void _load(Request& r, MethodPara if(!user_file_name) user_file_name=&lfile_name; - size_t offset=0; - size_t limit=0; - - if(options){ - options=new HashStringValue(*options); - if(Value *voffset=(Value *)options->get(sql_offset_name)){ - offset=r.process(*voffset).as_int(); - } - if(Value *vlimit=(Value *)options->get(sql_limit_name)){ - limit=r.process(*vlimit).as_int(); - } - // no check on options count here, see file_read - } - File_read_result file=file_load(r, lfile_name, - as_text, options, true, 0, offset, limit - ); + File_read_result file=file_load(r, lfile_name, as_text, options, true); Value* vcontent_type=0; if(file.headers){ @@ -939,7 +939,6 @@ static void _sql_string(Request& r, Meth #ifndef DOXYGEN class File_sql_event_handlers: public SQL_Driver_query_event_handlers { - const String& statement_string; const char* statement_cstr; int got_columns; int got_cells; public: @@ -947,9 +946,7 @@ public: const String* user_file_name; const String* user_content_type; public: - File_sql_event_handlers( - const String& astatement_string, const char* astatement_cstr): - statement_string(astatement_string), statement_cstr(astatement_cstr), + File_sql_event_handlers(): got_columns(0), got_cells(0), user_file_name(0), @@ -957,7 +954,7 @@ public: bool add_column(SQL_Error& error, const char* /*str*/, size_t /*length*/) { if(got_columns++==3) { - error=SQL_Error(PARSER_RUNTIME, "result must contain not more then 3 columns"); + error=SQL_Error("result must contain not more then 3 columns"); return true; } return false; @@ -979,7 +976,7 @@ public: user_content_type=new String(str, String::L_TAINTED); break; default: - error=SQL_Error(PARSER_RUNTIME, "result must not contain more then one row, three columns"); + error=SQL_Error("result must not contain more then one row, three columns"); return true; } return false; @@ -996,7 +993,7 @@ static void _sql(Request& r, MethodParam const String& statement_string=r.process_to_string(statement); const char* statement_cstr=statement_string.untaint_cstr(String::L_SQL, r.connection()); - File_sql_event_handlers handlers(statement_string, statement_cstr); + File_sql_event_handlers handlers; ulong limit=SQL_NO_LIMIT; ulong offset=0; @@ -1025,12 +1022,7 @@ static void _sql(Request& r, MethodParam } - r.connection()->query( - statement_cstr, - 0, 0, - offset, limit, - handlers, - statement_string); + r.connection()->query(statement_cstr, 0, 0, offset, limit, handlers, statement_string); if(!handlers.value.str) throw Exception(PARSER_RUNTIME, 0, "produced no result"); @@ -1042,44 +1034,61 @@ static void _sql(Request& r, MethodParam , &r); } +extern Base64Options base64_encode_options(Request& r, HashStringValue* options); + +Base64Options base64_decode_options(Request& r, HashStringValue* options, VString** vcontent_type) { + Base64Options result; + if(options) { + int valid_options=0; + for(HashStringValue::Iterator i(*options); i; i.next() ) { + String::Body key=i.key(); + Value* value=i.value(); + if(key == "pad") { + result.pad=r.process(*value).as_bool(); + valid_options++; + } else if(key == "strict") { + result.strict=r.process(*value).as_bool(); + valid_options++; + } else if(key == CONTENT_TYPE_NAME) { + *vcontent_type=new VString(value->as_string()); + valid_options++; + } else if(key == "url-safe") { + if(r.process(*value).as_bool()) + result.set_url_safe_abc(); + valid_options++; + } + } + + if(valid_options != options->count()) + throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION); + } + return result; +} + static void _base64(Request& r, MethodParams& params) { bool dynamic=!(&r.get_self() == file_class); if(dynamic) { VFile& self=GET_SELF(r, VFile); - if(params.count()) { + if(params.count()>1 || params.count()==1 && params[0].is_string()) { // decode: // ^file::base64[encoded] // backward // ^file::base64[mode;user-file-name;encoded[;$.content-type[...] $.strict(true|false)]] bool is_text=false; - bool strict=false; - VString* vcontent_type=0; const String* user_file_name=0; + VString* vcontent_type=0; + Base64Options options; + size_t param_index=0; if(params.count() > 1) { if(params.count() < 3) - throw Exception(PARSER_RUNTIME, - 0, - "constructor can not have less then 3 parameters (has %d parameters)", - params.count()); // actually it accepts 1 parameter (backward) + throw Exception(PARSER_RUNTIME, 0, "constructor can not have less then 3 parameters (has %d parameters)", params.count()); // actually it accepts 1 parameter (backward) is_text=VFile::is_text_mode(params.as_string(0, MODE_MUST_NOT_BE_CODE)); user_file_name=¶ms.as_string(1, FILE_NAME_MUST_BE_STRING); if(params.count() == 4) - if(HashStringValue* options=params.as_hash(3)) { - int valid_options=0; - if(Value* value=options->get(CONTENT_TYPE_NAME)) { - vcontent_type=new VString(value->as_string()); - valid_options++; - } - if(Value* vstrict=options->get(BASE64_STRICT_OPTION_NAME)) { - strict=r.process(*vstrict).as_bool(); - valid_options++; - } - if(valid_options!=options->count()) - throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION); - } + options=base64_decode_options(r, params.as_hash(3), &vcontent_type); param_index=2; } @@ -1087,20 +1096,21 @@ static void _base64(Request& r, MethodPa const char* encoded=params.as_string(param_index, PARAMETER_MUST_BE_STRING).cstr(); char* decoded=0; - size_t length=0; - pa_base64_decode(encoded, strlen(encoded), decoded, length, strict); + size_t length=pa_base64_decode(encoded, strlen(encoded), decoded, options); self.set(true/*tainted*/, is_text, decoded, length, user_file_name, vcontent_type, &r); } else { - // encode: ^f.base64[] - const char* encoded=pa_base64_encode(self.value_ptr(), self.value_size()); - r.write(*new String(encoded, String::L_TAINTED/*once ?param=base64(something) was needed**/)); + // encode: ^f.base64[options] + Base64Options options = base64_encode_options(r, params.count() > 0 ? params.as_hash(0) : NULL); + const char* encoded=pa_base64_encode(self.value_ptr(), self.value_size(), options); + r.write(*new String(encoded, String::L_TAINTED /*once ?param=base64(something) was needed**/ )); } } else { - // encode: ^file:base64[filespec] - const String& file_spec=params.as_string(0, FILE_NAME_MUST_BE_STRING); - const char* encoded=pa_base64_encode(r.absolute(file_spec)); - r.write(*new String(encoded, String::L_TAINTED/*once ?param=base64(something) was needed*/)); + // encode: ^file:base64[filespec[;options]] + const String& file_spec = params.as_string(0, FILE_NAME_MUST_BE_STRING); + Base64Options options = base64_encode_options(r, params.count() > 1 ? params.as_hash(1) : NULL); + const char* encoded = pa_base64_encode(r.absolute(file_spec), options); + r.write(*new String(encoded, String::L_TAINTED /*once ?param=base64(something) was needed*/ )); } } @@ -1266,5 +1276,6 @@ MFile::MFile(): Methoded("file") { add_native_method("md5", Method::CT_ANY, _md5, 0, 1); // ^file:copy[from-file-name;to-file-name] - add_native_method("copy", Method::CT_STATIC, _copy, 2, 2); + // ^file:copy[from-file-name;to-file-name;$.append(false)] + add_native_method("copy", Method::CT_STATIC, _copy, 2, 3); }