--- parser3/src/main/pa_common.C 2003/03/20 08:11:14 1.143.2.21.2.4 +++ parser3/src/main/pa_common.C 2003/04/02 10:10:00 1.143.2.21.2.20 @@ -5,14 +5,14 @@ Author: Alexandr Petrosian (http://paf.design.ru) */ -static const char* IDENT_COMMON_C="$Date: 2003/03/20 08:11:14 $"; +static const char* IDENT_COMMON_C="$Date: 2003/04/02 10:10:00 $"; #include "pa_common.h" #include "pa_exception.h" #include "pa_hash.h" #include "pa_globals.h" -//#define PA_HTTP +#define PA_HTTP #ifdef PA_HTTP #include "pa_vstring.h" @@ -61,6 +61,9 @@ int PASCAL closesocket(SOCKET); #endif // locking constants +//#define PA_DEBUG_NO_LOCKING + +#ifdef PA_DEBUG_NO_LOCKING #ifdef HAVE_FLOCK @@ -105,6 +108,14 @@ static int unlock(int fd) { FLOCK(F_TLOC #endif #endif +#else +static int lock_shared_blocking(int fd) { return 0; } +static int lock_exclusive_blocking(int fd) { return 0; } +static int lock_exclusive_nonblocking(int fd) { return 0; } +static int unlock(int fd) { return 0; } + +#endif + // defines for globals #define FILE_STATUS_NAME "status" @@ -133,16 +144,13 @@ static const String http_any_status_name // functions -void fix_line_breaks(char* buf, size_t& size) { - if(size==0) - return; - +void fix_line_breaks(char *str, size_t& length) { //_asm int 3; - const char* const eob=buf+size; - char* dest=buf; + const char* const eob=str+length; + char* dest=str; // fix DOS: \r\n -> \n // fix Macintosh: \r -> \n - char* bol=buf; + char* bol=str; while(char* eol=(char*)memchr(bol, '\r', eob -bol)) { size_t len=eol-bol; if(dest!=bol) @@ -152,7 +160,7 @@ void fix_line_breaks(char* buf, size_t& if(&eol[1]=0) { // checking status in first response + response.append_strdup(buf, size, String::L_TAINTED); + if(!result && (EOLat=response.pos("\r\n", 2))!=STRING_NOT_FOUND) { // checking status in first response const String& status_line=response.mid(0, (size_t)EOLat); ArrayString astatus; - status_line->split(astatus, 0, " ", 1); - const String& status_code=astatus.get(1); - result=status_code->as_int(); + size_t pos_after=0; + status_line.split(astatus, pos_after, " "); + const String& status_code=*astatus.get(1); + result=status_code.as_int(); if(fail_on_status_ne_200 && result!=200) throw Exception("http.status", - status_code, + &status_code, "invalid HTTP response status"); } } @@ -214,7 +223,7 @@ static int http_read_responseString& res else throw Exception("http.response", 0, - "bad response from host - no status found (size=%lu)", response.size()); + "bad response from host - no status found (size=%lu)", response.length()); } /* ********************** request *************************** */ @@ -231,15 +240,14 @@ static void timeout_handler(int sig){ #endif static int http_request( - String& response, - const String* origin_string, - const char* host, int port, - const char* request, - int timeout, - bool fail_on_status_ne_200) { + String& response, + const char* host, int port, + const char* request, + int timeout, + bool fail_on_status_ne_200) { if(!host) throw Exception("http.host", - origin_string, + 0, "zero hostname"); //never #ifdef WE_CAN_USE_ALARM @@ -251,7 +259,7 @@ static int http_request( #ifdef WE_CAN_USE_ALARM if(sigsetjmp(timeout_env, 1)) throw Exception("http.timeout", - origin_string, + 0, "timeout occured while retrieving document"); else { alarm(timeout); @@ -260,21 +268,21 @@ static int http_request( if(!set_addr(&dest, host, port)) throw Exception("http.host", - origin_string, + 0, "can not resolve hostname \"%s\"", host); if((sock=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP/*0*/))<0) throw Exception("http.connect", - origin_string, + 0, "can not make socket: %s (%d)", strerror(errno), errno); if(connect(sock, (struct sockaddr *)&dest, sizeof(dest))) throw Exception("http.connect", - origin_string, + 0, "can not connect to host \"%s\": %s (%d)", host, strerror(errno), errno); size_t request_size=strlen(request); if(send(sock, request, request_size, 0)!=(ssize_t)request_size) throw Exception("http.connect", - origin_string, + 0, "error sending request: %s (%d)", strerror(errno), errno); result=http_read_response(response, sock, fail_on_status_ne_200); @@ -296,47 +304,46 @@ static int http_request( #ifndef DOXYGEN struct Http_pass_header_info { - ; Charset* charset; String* request; bool user_agent_specified; }; #endif static void http_pass_header(HashStringValue::key_type key, HashStringValue::value_type value, - Http_pass_header_info *info) { - *(info->request)<<*key<<": " - << *attributed_meaning_to_string(*info->value, String::UL_HTTP_HEADER, false) - <<"\n"; + Http_pass_header_info *info) { + *info->request <change_case(*info->*info->charset, String::CC_UPPER)=="USER-AGENT") + if(String(key, String::L_TAINTED).change_case(*info->charset, String::CC_UPPER)=="USER-AGENT") info->user_agent_specified=true; } #ifndef DOXYGEN struct File_read_http_result { - char *data; size_t size; + char *str; size_t length; HashStringValue* headers; }; #endif -static File_read_http_result file_read_httpCharset& charset, const String& file_spec, - HashStringValue *options=0) { +static File_read_http_result file_read_http(Charset& charset, const String& file_spec, + HashStringValue *options=0) { File_read_http_result result; char host[MAX_STRING]; const char* uri; int port; - const char* method(0); + const char* method=0; int timeout=2; bool fail_on_status_ne_200=true; - ValuePtr vheaders(0); + Value* vheaders=0; - const String& connect_string(new String()); - // not in ^sql{... UL_SQL ...} spirit, but closer to ^file::load one - connect_string->append(*file_spec, String::UL_URI); // tainted pieces -> URI pieces + String& connect_string=*new String; + // not in ^sql{... L_SQL ...} spirit, but closer to ^file::load one + connect_string.append(file_spec, String::L_URI); // tainted pieces -> URI pieces - const char* connect_string_cstr=connect_string->cstr(String::UL_UNSPECIFIED); + const char* connect_string_cstr=connect_string.cstr(String::L_UNSPECIFIED); const char* current=connect_string_cstr; if(strncmp(current, "http://", 7)!=0) throw Exception(0, - connect_string, + &connect_string, "does not start with http://"); //never current+=7; @@ -349,18 +356,18 @@ static File_read_http_result file_read_h if(options) { int valid_options=0; - if(ValuePtr vmethod=options->get(http_method_name)) { + if(Value* vmethod=options->get(http_method_name)) { valid_options++; - method=vmethod->as_string(&pool)->cstr(); + method=vmethod->as_string().cstr(); } - if(ValuePtr vtimeout=options->get(http_timeout_name)) { + if(Value* vtimeout=options->get(http_timeout_name)) { valid_options++; timeout=vtimeout->as_int(); } if(vheaders=options->get(http_headers_name)) { valid_options++; } - if(ValuePtr vany_status=options->get(http_any_status_name)) { + if(Value* vany_status=options->get(http_any_status_name)) { valid_options++; fail_on_status_ne_200=!vany_status->as_bool(); } @@ -380,13 +387,13 @@ static File_read_http_result file_read_h request<< " "<< uri <<" HTTP/1.0\nHost: "<< host<<"\n"; bool user_agent_specified=false; if(vheaders && !vheaders->is_string()) { // allow empty - if(HashStringValue *headers=vheaders->get_hash(connect_string)) { - Http_pass_header_info info={&&charset, &request}; + if(HashStringValue *headers=vheaders->get_hash()) { + Http_pass_header_info info={&charset, &request}; headers->for_each(http_pass_header, &info); user_agent_specified=info.user_agent_specified; } else throw Exception("parser.runtime", - connect_string, + &connect_string, "headers param must be hash"); }; if(!user_agent_specified) // defaulting @@ -395,43 +402,45 @@ static File_read_http_result file_read_h //sending request String response; - const char* request_cstr=request.cstr(String::UL_UNSPECIFIED); + const char* request_cstr=request.cstr(String::L_UNSPECIFIED); int status_code=http_request(response, - connect_string, host, port, request_cstr, + host, port, request_cstr, timeout, fail_on_status_ne_200); //processing results - int pos=response.pos("\r\n\r\n", 4); - if(pos<1){ + size_t pos=response.pos("\r\n\r\n", 4); + if(pos==STRING_NOT_FOUND || pos<1){ throw Exception("http.response", - connect_string, + &connect_string, "bad response from host - no headers found"); } const String& header_block=response.mid(0, pos); - const String& body=response.mid(pos+4, response.size()); + const String& body=response.mid(pos+4, response.length()); ArrayString aheaders; - result.headers=HashStringValue* (new HashStringValue); - header_block->split(aheaders, 0, "\r\n", 2); + result.headers=new HashStringValue; + size_t pos_after=0; + header_block.split(aheaders, pos_after, "\r\n"); //processing headers - for(int i=1; ipos(": ", 2); - if(pos<1) + size_t aheaders_count=aheaders.count(); + for(size_t i=1; icstr().get()); + &connect_string, + "bad response from host - bad header \"%s\"", line.cstr()); result.headers->put( - line->mid(0, pos)->change_case(charset, String::CC_UPPER), - ValuePtr(new VString(line->mid(pos+2, line->size())))); + line.mid(0, pos).change_case(charset, String::CC_UPPER), + new VString(line.mid(pos+2, line.length()))); } // output response - result.data=new char[1/*as text puts terminating zero later*/+(result.size=body->size())]; - body->store_to(result.data, String::UL_AS_IS); - result.headers->put(file_status_name, ValuePtr(new VInt(status_code))); + result.str=body.cstrm(); + result.length=body.length(); + result.headers->put(file_status_name, new VInt(status_code)); return result; } @@ -449,7 +458,7 @@ static void file_read_action( void *context) { File_read_action_info& info=*static_cast(context); if(size_t to_read_size=(size_t)finfo.st_size) { - *info.data=new char[to_read_size+(as_text?1:0)]; + *info.data=new(PointerFreeGC) char[to_read_size+(as_text?1:0)]; *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) @@ -459,7 +468,7 @@ static void file_read_action( *info.data_size, to_read_size); } else { // empty file if(as_text) { - *info.data=new char[1]; + *info.data=new(PointerFreeGC) char[1]; *(char*)(*info.data)=0; } else *info.data=0; @@ -470,18 +479,18 @@ static void file_read_action( File_read_result file_read(Charset& charset, const String& file_spec, bool as_text, HashStringValue *params, bool fail_on_read_problem) { - File_read_result result; + File_read_result result={false}; #ifdef PA_HTTP - if(file_spec.starts_with("http://", 7)) { + if(file_spec.starts_with("http://")) { // fail on read problem - File_read_http_result http=file_read_http(charset, file_spec, params); + File_read_http_result http;//=file_read_http(charset, file_spec, params); result.success=true; - result.data=http.data; - result.size=http.size; - result.headers=http.headers; + result.str="HELLO";//http.str; + result.length=5;//http.length; + result.headers=new HashStringValue;//http.headers; } else { #endif - File_read_action_info info={&result.data, &result.size}; + File_read_action_info info={&result.str, &result.length}; result.success=file_read_action_under_lock(file_spec, "read", file_read_action, &info, as_text, fail_on_read_problem); @@ -491,17 +500,17 @@ File_read_result file_read(Charset& char if(result.success && as_text) { // UTF-8 signature: EF BB BF - if(result.size>=3) { - char *in=(char *)result.data; + if(result.length>=3) { + char *in=(char *)result.str; if((in[0] == '\xEF') && (in[1] == '\xBB') && (in[2] == '\xBF')) { - result.data=in+3; result.size-=3;// skip prefix + result.str=in+3; result.length-=3;// skip prefix } } - fix_line_breaks((char *)(result.data), result.size); + fix_line_breaks((char *)(result.str), result.length); // note: after fixing - ((char*&)(result.data))[result.size]=0; + ((char*&)(result.str))[result.length]=0; } return result; @@ -511,7 +520,7 @@ bool file_read_action_under_lock(const S const char* action_name, File_read_action action, void *context, bool as_text, bool fail_on_read_problem) { - const char* fname=file_spec.cstr(String::UL_FILE_SPEC); + const char* fname=file_spec.cstr(String::L_FILE_SPEC); int f; // first open, next stat: @@ -569,9 +578,9 @@ bool file_read_action_under_lock(const S static void create_dir_for_file(const String& file_spec) { size_t pos_after=1; - int pos_before; - while((pos_before=file_spec.pos("/", pos_after))>=0) { - mkdir(file_spec.mid(0, pos_before).cstr(String::UL_FILE_SPEC), 0775); + size_t pos_before; + while((pos_before=file_spec.pos('/', pos_after))!=STRING_NOT_FOUND) { + mkdir(file_spec.mid(0, pos_before).cstr(String::L_FILE_SPEC), 0775); pos_after=pos_before+1; } } @@ -583,7 +592,7 @@ bool file_write_action_under_lock( bool do_append, bool do_block, bool fail_on_lock_problem) { - const char* fname=file_spec.cstr(String::UL_FILE_SPEC); + const char* fname=file_spec.cstr(String::L_FILE_SPEC); int f; if(access(fname, W_OK)!=0) // no create_dir_for_file(file_spec); @@ -630,13 +639,13 @@ bool file_write_action_under_lock( #ifndef DOXYGEN struct File_write_action_info { - const char *data; size_t size; + const char* str; size_t length; }; #endif static void file_write_action(int f, void *context) { File_write_action_info& info=*static_cast(context); - if(info.size) { - int written=write(f, info.data, info.size); + if(info.length) { + int written=write(f, info.str, info.length); if(written<0) throw Exception(0, 0, @@ -645,7 +654,7 @@ static void file_write_action(int f, voi } void file_write( const String& file_spec, - const char *data, size_t size, + const char* data, size_t size, bool as_text, bool do_append) { File_write_action_info info={data, size}; @@ -658,14 +667,14 @@ void file_write( // throws nothing! [this is required in file_move & file_delete] static void rmdir(const String& file_spec, size_t pos_after) { - int pos_before; - if((pos_before=file_spec.pos("/", pos_after))>=0) + size_t pos_before; + if((pos_before=file_spec.pos('/', pos_after))!=STRING_NOT_FOUND) rmdir(file_spec, pos_before+1); - rmdir(file_spec.mid(0, pos_after-1/* / */).cstr(String::UL_FILE_SPEC)); + rmdir(file_spec.mid(0, pos_after-1/* / */).cstr(String::L_FILE_SPEC)); } bool file_delete(const String& file_spec, bool fail_on_read_problem) { - const char* fname=file_spec.cstr(String::UL_FILE_SPEC); + const char* fname=file_spec.cstr(String::L_FILE_SPEC); if(unlink(fname)!=0) if(fail_on_read_problem) throw Exception(errno==EACCES?"file.access":errno==ENOENT?"file.missing":0, @@ -679,8 +688,8 @@ bool file_delete(const String& file_spec return true; } void file_move(const String& old_spec, const String& new_spec) { - const char* old_spec_cstr=old_spec.cstr(String::UL_FILE_SPEC); - const char* new_spec_cstr=new_spec.cstr(String::UL_FILE_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); create_dir_for_file(new_spec); @@ -703,12 +712,12 @@ bool entry_exists(const char* fname, str } bool entry_exists(const String& file_spec) { - const char* fname=file_spec.cstr(String::UL_FILE_SPEC); + const char* fname=file_spec.cstr(String::L_FILE_SPEC); return entry_exists(fname, 0); } static bool entry_readable(const String& file_spec, bool need_dir) { - char* fname=file_spec.cstr(String::UL_FILE_SPEC); + char* fname=file_spec.cstrm(String::L_FILE_SPEC); if(need_dir) { size_t size=strlen(fname); while(size) { @@ -732,14 +741,14 @@ bool file_readable(const String& file_sp bool dir_readable(const String& file_spec) { return entry_readable(file_spec, true); } -const String* file_readable(String& path, String& name) { +const String* file_readable(const String& path, const String& name) { String& result=*new String(path); result << "/"; result << name; return file_readable(result)?&result:0; } bool file_executable(const String& file_spec) { - return access(file_spec.cstr(String::UL_FILE_SPEC), X_OK)==0; + return access(file_spec.cstr(String::L_FILE_SPEC), X_OK)==0; } bool file_stat(const String& file_spec, @@ -748,8 +757,8 @@ bool file_stat(const String& file_spec, time_t& rmtime, time_t& rctime, bool fail_on_read_problem) { - const char* fname=file_spec.cstr(String::UL_FILE_SPEC); - struct stat finfo; + const char* fname=file_spec.cstr(String::L_FILE_SPEC); + struct stat finfo; if(stat(fname, &finfo)!=0) if(fail_on_read_problem) throw Exception("file.missing", @@ -842,7 +851,7 @@ size_t stdout_write(const void *buf, siz } char* unescape_chars(const char* cp, int len) { - char* s=new char[len + 1]; + char* s=new(PointerFreeGC) char[len + 1]; enum EscapeState { EscapeRest, EscapeFirst,