--- parser3/src/main/pa_common.C 2003/09/19 09:58:26 1.155 +++ parser3/src/main/pa_common.C 2003/12/17 10:12:34 1.170 @@ -5,7 +5,7 @@ Author: Alexandr Petrosian (http://paf.design.ru) */ -static const char* IDENT_COMMON_C="$Date: 2003/09/19 09:58:26 $"; +static const char * const IDENT_COMMON_C="$Date: 2003/12/17 10:12:34 $"; #include "pa_common.h" #include "pa_exception.h" @@ -64,62 +64,6 @@ int PASCAL closesocket(SOCKET); # endif #endif -// locking constants -//#define PA_DEBUG_NO_LOCKING - -#ifdef PA_DEBUG_NO_LOCKING - -#ifdef HAVE_FLOCK - -static int lock_shared_blocking(int fd) { return flock(fd, LOCK_SH); } -static int lock_exclusive_blocking(int fd) { return flock(fd, LOCK_EX); } -static int lock_exclusive_nonblocking(int fd) { return flock(fd, LOCK_EX || LOCK_NB); } -static int unlock(int fd) { return flock(fd, LOCK_UN); } - -#else -#ifdef HAVE__LOCKING - -#define FLOCK(operation) lseek(fd, 0, SEEK_SET); return _locking(fd, operation, 1) -static int lock_shared_blocking(int fd) { FLOCK(_LK_LOCK); } -static int lock_exclusive_blocking(int fd) { FLOCK(_LK_LOCK); } -static int lock_exclusive_nonblocking(int fd) { FLOCK(_LK_NBLCK); } -static int unlock(int fd) { FLOCK(_LK_UNLCK); } - -#else -#ifdef HAVE_FCNTL - -#define FLOCK(cmd, arg) struct flock ls={arg, SEEK_SET}; return fcntl(fd, cmd, &ls) -static int lock_shared_blocking(int fd) { FLOCK(F_SETLKW, F_RDLCK); } -static int lock_exclusive_blocking(int fd) { FLOCK(F_SETLKW, F_WRLCK); } -static int lock_exclusive_nonblocking(int fd) { FLOCK(F_SETLK, F_RDLCK); } -static int unlock(int fd) { FLOCK(F_SETLK, F_UNLCK); } - -#else -#ifdef HAVE_LOCKF - -#define FLOCK(fd, operation) lseek(fd, 0, SEEK_SET); return lockf(fd, operation, 1) -static int lock_shared_blocking(int fd) { FLOCK(F_LOCK); } // on intel solaris man doesn't have doc on shared blocking -static int lock_exclusive_blocking(int fd) { FLOCK(F_LOCK); } -static int lock_exclusive_nonblocking(int fd) { FLOCK(F_TLOCK); } -static int unlock(int fd) { FLOCK(F_TLOCK); } - -#else - -#error unable to find file locking func - -#endif -#endif -#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" @@ -254,7 +198,11 @@ static void timeout_handler(int sig){ static int http_request(String& response, const char* host, int port, const char* request, - int timeout, + int +#ifdef PA_USE_ALARM + timeout +#endif + , bool fail_on_status_ne_200) { if(!host) throw Exception("http.host", @@ -341,7 +289,7 @@ static void http_pass_header(HashStringV static Charset* detect_charset(Charset& source_charset, const String& content_type_value) { - const StringBody CONTENT_TYPE_VALUE= + const String::Body CONTENT_TYPE_VALUE= content_type_value.change_case(source_charset, String::CC_UPPER); // content-type: xxx/xxx; source_charset=WE-NEED-THIS // content-type: xxx/xxx; source_charset="WE-NEED-THIS" @@ -363,7 +311,7 @@ static Charset* detect_charset(Charset& if(delim_pos!=STRING_NOT_FOUND) charset_end=delim_pos; } - const StringBody CHARSET_NAME_BODY= + const String::Body CHARSET_NAME_BODY= CONTENT_TYPE_VALUE.mid(charset_begin, charset_end); return &charsets.get(CHARSET_NAME_BODY); @@ -382,7 +330,8 @@ struct File_read_http_result { /// @todo build .cookies field. use ^file.tables.SET-COOKIES.menu{ for now static File_read_http_result file_read_http(Request_charsets& charsets, const String& file_spec, - HashStringValue *options=0) { + bool as_text, + HashStringValue *options=0) { File_read_http_result result; char host[MAX_STRING]; const char* uri; @@ -403,7 +352,7 @@ static File_read_http_result file_read_h valid_options++; timeout=vtimeout->as_int(); } - if(vheaders=options->get(http_headers_name)) { + if((vheaders=options->get(http_headers_name))) { valid_options++; } if(Value* vany_status=options->get(http_any_status_name)) { @@ -462,7 +411,7 @@ static File_read_http_result file_read_h bool user_agent_specified=false; if(vheaders && !vheaders->is_string()) { // allow empty if(HashStringValue *headers=vheaders->get_hash()) { - Http_pass_header_info info={&charsets, &request}; + Http_pass_header_info info={&charsets, &request, false}; headers->for_each(http_pass_header, &info); user_agent_specified=info.user_agent_specified; } else @@ -518,7 +467,7 @@ static File_read_http_result file_read_h throw Exception("http.response", &connect_string, "bad response from host - bad header \"%s\"", line.cstr()); - const StringBody HEADER_NAME= + const String::Body HEADER_NAME= line.mid(0, pos).change_case(charsets.source(), String::CC_UPPER); const String& header_value=line.mid(pos+2, line.length()); if(HEADER_NAME=="CONTENT-TYPE") @@ -554,10 +503,9 @@ static File_read_http_result file_read_h real_remote_charset=asked_remote_charset; // output response - String::C real_body=Charset::transcode( - String::C(raw_body.cstrm()/*must be modifiable*/, raw_body.length()), - *real_remote_charset, - charsets.source()); + String::C real_body=String::C(raw_body.cstrm()/*must be modifiable*/, raw_body.length()); + if(as_text && raw_body.length()) // must be checked because transcode returns CONST string in case length==0, which contradicts hacking few lines below + real_body=Charset::transcode(real_body, *real_remote_charset, charsets.source()); result.str=const_cast(real_body.str); // hacking a little result.length=real_body.length; @@ -575,7 +523,7 @@ struct File_read_action_info { static void file_read_action( struct stat& finfo, int f, - const String& file_spec, const char* fname, bool as_text, + const String& file_spec, const char* /*fname*/, bool as_text, void *context) { File_read_action_info& info=*static_cast(context); if(size_t to_read_size=(size_t)finfo.st_size) { @@ -585,7 +533,7 @@ static void file_read_action( if(ssize_t(*info.data_size)<0 || *info.data_size>to_read_size) throw Exception(0, &file_spec, - "read failed: actually read %lu bytes count not in [0..%lu] valid range", + "read failed: actually read %l bytes count not in [0..%lu] valid range", *info.data_size, to_read_size); } else { // empty file if(as_text) { @@ -600,17 +548,22 @@ static void file_read_action( File_read_result file_read(Request_charsets& charsets, const String& file_spec, bool as_text, HashStringValue *params, bool fail_on_read_problem) { - File_read_result result={false}; + File_read_result result={false, 0, 0, 0}; #ifdef PA_HTTP if(file_spec.starts_with("http://")) { // fail on read problem - File_read_http_result http=file_read_http(charsets, file_spec, params); + File_read_http_result http=file_read_http(charsets, file_spec, as_text, params); result.success=true; result.str=http.str; result.length=http.length; result.headers=http.headers; } else { #endif + if(params && params->count()) + throw Exception("parser.runtime", + 0, + "invalid option passed"); + File_read_action_info info={&result.str, &result.length}; result.success=file_read_action_under_lock(file_spec, "read", file_read_action, &info, @@ -623,14 +576,12 @@ File_read_result file_read(Request_chars // UTF-8 signature: EF BB BF if(result.length>=3) { char *in=(char *)result.str; - if(strncmp(in, "\xEB\xBB\xBF", 3)==0) { + 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); - // note: after fixing - ((char*&)(result.str))[result.length]=0; } return result; @@ -671,7 +622,7 @@ bool file_read_action_under_lock(const S // later: it seems, even this does not help sometimes if((f=open(fname, O_RDONLY|(as_text?_O_TEXT:_O_BINARY)))>=0) { try { - if(lock_shared_blocking(f)!=0) + if(pa_lock_shared_blocking(f)!=0) throw Exception("file.lock", &file_spec, "shared lock failed: %s (%d), actual filename '%s'", @@ -690,13 +641,13 @@ bool file_read_action_under_lock(const S action(finfo, f, file_spec, fname, as_text, context); } catch(...) { - unlock(f);close(f); + pa_unlock(f);close(f); if(fail_on_read_problem) rethrow; return false; } - unlock(f);close(f); + pa_unlock(f);close(f); return true; } else { if(fail_on_read_problem) @@ -733,7 +684,7 @@ bool file_write_action_under_lock( O_CREAT|O_RDWR |(as_text?_O_TEXT:_O_BINARY) |(do_append?O_APPEND:PA_O_TRUNC), 0664))>=0) { - if((do_block?lock_exclusive_blocking(f):lock_exclusive_nonblocking(f))!=0) { + if((do_block?pa_lock_exclusive_blocking(f):pa_lock_exclusive_nonblocking(f))!=0) { Exception e("file.lock", &file_spec, "shared lock failed: %s (%d), actual filename '%s'", @@ -751,7 +702,7 @@ bool file_write_action_under_lock( if(!do_append) ftruncate(f, lseek(f, 0, SEEK_CUR)); // one can not use O_TRUNC, read lower #endif - unlock(f);close(f); + pa_unlock(f);close(f); rethrow; } @@ -759,7 +710,7 @@ bool file_write_action_under_lock( if(!do_append) ftruncate(f, lseek(f, 0, SEEK_CUR)); // O_TRUNC truncates even exclusevely write-locked file [thanks to Igor Milyakov for discovering] #endif - unlock(f);close(f); + pa_unlock(f);close(f); return true; } else throw Exception(errno==EACCES?"file.access":0, @@ -805,10 +756,10 @@ 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_read_problem) { +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) - if(fail_on_read_problem) + if(fail_on_problem) throw Exception(errno==EACCES?"file.access":errno==ENOENT?"file.missing":0, &file_spec, "unlink failed: %s (%d), actual filename '%s'", @@ -1124,7 +1075,7 @@ int __vsnprintf(char* b, size_t s, const if(r<0) r=0; - else if(r>s) + else if((size_t)r>s) r=s; #endif b[r]=0; @@ -1138,19 +1089,3 @@ int __snprintf(char* b, size_t s, const va_end(l); return r; } - -int pa_sleep(unsigned long secs, unsigned long usecs) { - for (; usecs >= 1000000; ++secs, usecs -= 1000000); - -#ifdef WIN32 - Sleep(secs * 1000 + usecs / 1000); - return 0; -#else - struct timeval t; - t.tv_sec = secs; - t.tv_usec = usecs; - return (select(0, NULL, NULL, NULL, &t) == -1 ? errno : 0); -#endif -} - -