--- parser3/src/main/pa_common.C 2008/06/06 17:24:23 1.232 +++ parser3/src/main/pa_common.C 2009/04/22 04:35:48 1.243 @@ -1,7 +1,7 @@ /** @file Parser: commonly functions. - Copyright(c) 2001-2005 ArtLebedev Group (http://www.artlebedev.com) + Copyright(c) 2001-2009 ArtLebedev Group (http://www.artlebedev.com) Author: Alexandr Petrosian (http://paf.design.ru) * BASE64 part @@ -26,7 +26,7 @@ * */ -static const char * const IDENT_COMMON_C="$Date: 2008/06/06 17:24:23 $"; +static const char * const IDENT_COMMON_C="$Date: 2009/04/22 04:35:48 $"; #include "pa_common.h" #include "pa_exception.h" @@ -35,6 +35,8 @@ static const char * const IDENT_COMMON_C #include "pa_charsets.h" #include "pa_http.h" #include "pa_request_charsets.h" +#include "pcre.h" +#include "pa_request.h" // some maybe-undefined constants @@ -94,15 +96,25 @@ void fix_line_breaks(char *str, size_t& char* file_read_text(Request_charsets& charsets, const String& file_spec, bool fail_on_read_problem, - HashStringValue* params/*, HashStringValue* * out_fields*/) { + HashStringValue* params, + bool transcode_result) { File_read_result file= - file_read(charsets, file_spec, true, params, fail_on_read_problem); + file_read(charsets, file_spec, true, params, fail_on_read_problem, 0, 0, 0, transcode_result); + return file.success?file.str:0; +} + +char* file_load_text(Request& r, + const String& file_spec, + bool fail_on_read_problem, + HashStringValue* params, + bool transcode_result) { + File_read_result file= + file_load(r, file_spec, true, params, fail_on_read_problem, 0, 0, 0, transcode_result); return file.success?file.str:0; } /// these options were handled but not checked elsewhere, now check them -int pa_get_valid_file_options_count(HashStringValue& options) -{ +int pa_get_valid_file_options_count(HashStringValue& options) { int result=0; if(options.get(PA_SQL_LIMIT_NAME)) result++; @@ -142,7 +154,7 @@ static void file_read_action( *info.data_size=(size_t)read(f, *info.data, to_read_size); if(ssize_t(*info.data_size)<0 || *info.data_size>to_read_size) - throw Exception(0, + throw Exception("file.read", &file_spec, "read failed: actually read %u bytes count not in [0..%u] valid range", *info.data_size, to_read_size); @@ -154,10 +166,59 @@ static void file_read_action( return; } } + File_read_result file_read(Request_charsets& charsets, const String& file_spec, bool as_text, HashStringValue *params, bool fail_on_read_problem, - char* buf, size_t offset, size_t count) { + char* buf, size_t offset, size_t count, bool transcode_text_result) { + File_read_result result={false, 0, 0, 0}; + if(params){ + int valid_options=pa_get_valid_file_options_count(*params); + if(valid_options!=params->count()) + throw Exception(PARSER_RUNTIME, + 0, + "invalid option passed"); + } + + File_read_action_info info={&result.str, &result.length, buf, offset, count}; + + result.success=file_read_action_under_lock(file_spec, + "read", file_read_action, &info, + as_text, fail_on_read_problem); + + if(as_text){ + if(result.success){ + if(result.length>=3 && strncmp(result.str, "\xEF\xBB\xBF", 3)==0){ + // skip UTF-8 signature (BOM code) + result.str+=3; + result.length-=3; + } + + if(result.length && transcode_text_result && params){ // must be checked because transcode returns CONST string in case length==0, which contradicts hacking few lines below + if(Value* vcharset_name=params->get(PA_CHARSET_NAME)){ + Charset asked_charset=::charsets.get(vcharset_name->as_string(). + change_case(charsets.source(), String::CC_UPPER)); + + String::C body=String::C(result.str, result.length); + body=Charset::transcode(body, asked_charset, charsets.source()); + + result.str=const_cast(body.str); // hacking a little + result.length=body.length; + } + } + } + if(result.length) + fix_line_breaks(result.str, result.length); + } + + return result; +} + +File_read_result file_load(Request& r, const String& file_spec, + bool as_text, HashStringValue *params, + bool fail_on_read_problem, + char* buf, size_t offset, size_t count, bool transcode_text_result) { + File_read_result result={false, 0, 0, 0}; if(file_spec.starts_with("http://")) { if(offset || count) @@ -166,51 +227,14 @@ File_read_result file_read(Request_chars "offset and load options are not supported for HTTP:// file load"); // fail on read problem - File_read_http_result http=pa_internal_file_read_http(charsets, file_spec, as_text, params); + File_read_http_result http=pa_internal_file_read_http(r, file_spec, as_text, params, transcode_text_result); result.success=true; result.str=http.str; result.length=http.length; result.headers=http.headers; - } else { - if(params) { - int valid_options=pa_get_valid_file_options_count(*params); - if(valid_options!=params->count()) - throw Exception(PARSER_RUNTIME, - 0, - "invalid option passed"); - } - - File_read_action_info info={&result.str, &result.length, - buf, offset, count}; - result.success=file_read_action_under_lock(file_spec, - "read", file_read_action, &info, - as_text, fail_on_read_problem); - - if(result.length && as_text && params) { - if( Value* vcharset_name=params->get(PA_CHARSET_NAME) ) { - Charset asked_charset=::charsets.get(vcharset_name->as_string(). - change_case(charsets.source(), String::CC_UPPER)); - - String::C body=String::C(result.str, result.length); - body=Charset::transcode(body, asked_charset, charsets.source()); - - result.str=const_cast(body.str); // hacking a little - result.length=body.length; - } - } - } - - if(result.success && as_text) { - // UTF-8 signature: EF BB BF - if(result.length>=3) { - char *in=(char *)result.str; - if(strncmp(in, "\xEF\xBB\xBF", 3)==0) { - result.str=in+3; result.length-=3;// skip prefix - } - } - - fix_line_breaks((char *)(result.str), result.length); - } + } else + result= + file_read(r.charsets, file_spec, as_text, params, fail_on_read_problem, buf, offset, count, transcode_text_result); return result; } @@ -360,7 +384,7 @@ static void file_write_action(int f, voi if(info.length) { int written=write(f, info.str, info.length); if(written<0) - throw Exception(0, + throw Exception("file.access", 0, "write failed: %s (%d)", strerror(errno), errno); } @@ -389,6 +413,7 @@ static void rmdir(const String& file_spe rmdir(file_spec.mid(0, pos_after-1/* / */).cstr(String::L_FILE_SPEC)); } + bool file_delete(const String& file_spec, bool fail_on_problem) { const char* fname=file_spec.cstr(String::L_FILE_SPEC); if(unlink(fname)!=0) @@ -403,6 +428,7 @@ bool file_delete(const String& file_spec rmdir(file_spec, 1); return true; } + void file_move(const String& old_spec, const String& new_spec) { const char* old_spec_cstr=old_spec.cstr(String::L_FILE_SPEC); const char* new_spec_cstr=new_spec.cstr(String::L_FILE_SPEC); @@ -451,20 +477,24 @@ static bool entry_readable(const String& } return false; } + bool file_exist(const String& file_spec) { return entry_readable(file_spec, false); } + bool dir_exists(const String& file_spec) { return entry_readable(file_spec, true); } + const String* file_exist(const String& path, const String& name) { String& result=*new String(path); result << "/"; result << name; return file_exist(result)?&result:0; } + bool file_executable(const String& file_spec) { - return access(file_spec.cstr(String::L_FILE_SPEC), X_OK)==0; + return access(file_spec.cstr(String::L_FILE_SPEC), X_OK)==0; } bool file_stat(const String& file_spec, @@ -598,7 +628,7 @@ FormatType format_type(char* fmt){ const char* format(double value, char* fmt) { char local_buf[MAX_NUMBER]; - size_t size; + int size=-1; if(fmt && strlen(fmt)){ switch(format_type(fmt)){ @@ -625,7 +655,7 @@ const char* format(double value, char* f "Error occure white executing snprintf with format string '%s'.", fmt); } - return pa_strdup(local_buf, size); + return pa_strdup(local_buf, (size_t)size); } size_t stdout_write(const void *buf, size_t size) { @@ -651,57 +681,23 @@ enum EscapeState { EscapeSecond, EscapeUnicode }; -/* -char* unescape_chars(const char* cp, int len) { - char* s=new(PointerFreeGC) char[len + 1]; - EscapeState escapeState=EscapeRest; - uint escapedValue=0; - int srcPos=0; - int dstPos=0; - while(srcPos < len) { - uchar ch=(uchar)cp[srcPos]; - switch(escapeState) { - case EscapeRest: - if(ch=='%') { - escapeState=EscapeFirst; - } else if(ch=='+'){ - s[dstPos++]=' '; - } else { - s[dstPos++]=ch; - } - break; - case EscapeFirst: - escapedValue=hex_value[ch] << 4; - escapeState=EscapeSecond; - break; - case EscapeSecond: - escapedValue +=hex_value[ch]; - s[dstPos++]=(char)escapedValue; - escapeState=EscapeRest; - break; - } - srcPos++; - } - s[dstPos]=0; - return s; -} -*/ -char* unescape_chars(const char* cp, int len, Charset* charset){ - char* s=new(PointerFreeGC) char[len + 1]; // enough (%uXXXX==6 bytes, max utf-8 char length==6 bytes) +// @todo prescan for reduce required size (unescaped sting in 1 byte charset requires less memory usually) +char* unescape_chars(const char* cp, int len, Charset* charset, bool ignore_plus){ + char* s=new(PointerFreeGC) char[len+1]; // must be enough (%uXXXX==6 bytes, max utf-8 char length==6 bytes) char* dst=s; EscapeState escapeState=EscapeRest; uint escapedValue=0; int srcPos=0; short int jsCnt=0; - while(srcPos < len) { + while(srcPos(context); if(finfo.st_size) { InitCrc32Table();