--- parser3/src/main/pa_common.C 2003/11/03 13:20:30 1.161 +++ 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/11/03 13:20:30 $"; +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", @@ -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 @@ -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) { @@ -600,11 +548,11 @@ 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; @@ -634,8 +582,6 @@ File_read_result file_read(Request_chars } fix_line_breaks((char *)(result.str), result.length); - // note: after fixing - ((char*&)(result.str))[result.length]=0; } return result; @@ -676,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'", @@ -695,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) @@ -738,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'", @@ -756,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; } @@ -764,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, @@ -810,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'", @@ -1129,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; @@ -1143,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 -} - -