--- parser3/src/main/pa_common.C 2002/12/02 10:07:39 1.136 +++ parser3/src/main/pa_common.C 2003/01/16 08:44:19 1.141 @@ -5,7 +5,7 @@ Author: Alexandr Petrosian (http://paf.design.ru) */ -static const char* IDENT_COMMON_C="$Date: 2002/12/02 10:07:39 $"; +static const char* IDENT_COMMON_C="$Date: 2003/01/16 08:44:19 $"; #include "pa_common.h" #include "pa_exception.h" @@ -29,6 +29,16 @@ static const char* IDENT_COMMON_C="$Date # define _O_BINARY 0 #endif +#ifdef HAVE_FTRUNCATE +# define PA_O_TRUNC 0 +#else +# ifdef _O_TRUNC +# define PA_O_TRUNC _O_TRUNC +# else +# error you must have either ftruncate function or _O_TRUNC bit declared +# endif +#endif + // locking constants #ifdef HAVE_FLOCK @@ -77,28 +87,17 @@ static int unlock(int fd) { FLOCK(F_TLOC #define DEFAULT_USER_AGENT "parser3" - -static char* strnchr(char* buf, size_t size, char c) { - // sanity check - if(!buf) - return 0; - - for(; size-->0; buf++) { - if(*buf==c) - return buf; - } - - return 0; -} - void fix_line_breaks(char* buf, size_t& size) { + if(size==0) + return; + //_asm int 3; const char* const eob=buf+size; char* dest=buf; // fix DOS: \r\n -> \n // fix Macintosh: \r -> \n char* bol=buf; - while(char* eol=strnchr(bol, eob -bol, '\r')) { + while(char* eol=(char*)memchr(bol, '\r', eob -bol)) { size_t len=eol-bol; if(dest!=bol) memcpy(dest, bol, len); @@ -141,7 +140,6 @@ static bool set_addr(struct sockaddr_in } static void http_read_response(String& response, int sock){ - bool check_status=true; ssize_t EOLat=0; while(true) { char *buf=(char *)response.pool().malloc(MAX_STRING); @@ -149,24 +147,7 @@ static void http_read_response(String& r if(size<=0) break; response.APPEND_TAINTED(buf, size, "remote HTTP server response", 0); - if(check_status && (EOLat=response.pos("\r\n", 2))>=0) { // checking status in first response - check_status=false; - - const String& status_line=response.mid(0, (size_t)EOLat); - Array astatus(response.pool()); - size_t pos_after_ref=0; status_line.split(astatus, &pos_after_ref, " ", 1); - const String* status_code=astatus.get_string(1); - - if(!status_code || *status_code!="200") - throw Exception("http.status", - status_code, - "invalid HTTP response status"); - } } - if(check_status) - throw Exception("http.response", - 0, - "bad response from host - no status found (size=%lu)", response.size()); } /* ********************** request *************************** */ @@ -347,11 +328,11 @@ static void file_read_http(Pool& pool, c //processing status code const String *status_line=aheaders.get_string(0); - if(!status_line){ - throw Exception("http.response", - &connect_string, - "bad response from host - no status line "); - } + Array astatus(pool); + pos_after_ref=0; + status_line->split(astatus, &pos_after_ref, " ", 1); + const String *status_code=astatus.get_string(1); + //processing headers for(int i=1;i=0) { + |(do_append?O_APPEND:PA_O_TRUNC), 0664))>=0) { if((do_block?lock_exclusive_blocking(f):lock_exclusive_nonblocking(f))!=0) { Exception e("file.lock", &file_spec, @@ -538,14 +522,18 @@ bool file_write_action_under_lock( try { action(f, context); } catch(...) { +#ifdef HAVE_FTRUNCATE if(!do_append) ftruncate(f, lseek(f, 0, SEEK_CUR)); // one can not use O_TRUNC, read lower +#endif unlock(f);close(f); /*re*/throw; } +#ifdef HAVE_FTRUNCATE 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); return true; } else