--- parser3/src/main/pa_common.C 2002/12/05 12:53:48 1.137 +++ parser3/src/main/pa_common.C 2003/01/24 08:19:09 1.143.2.2 @@ -1,18 +1,16 @@ /** @file Parser: commonly functions. - Copyright(c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com) + Copyright(c) 2001, 2003 ArtLebedev Group (http://www.artlebedev.com) Author: Alexandr Petrosian (http://paf.design.ru) */ -static const char* IDENT_COMMON_C="$Date: 2002/12/05 12:53:48 $"; +static const char* IDENT_COMMON_C="$Date: 2003/01/24 08:19:09 $"; #include "pa_common.h" #include "pa_exception.h" #include "pa_globals.h" #include "pa_hash.h" -#include "pa_vstring.h" -#include "pa_vdate.h" #ifdef WIN32 # include @@ -29,6 +27,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 @@ -78,6 +86,9 @@ static int unlock(int fd) { FLOCK(F_TLOC 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; @@ -102,11 +113,13 @@ void fix_line_breaks(char* buf, size_t& memcpy(dest, bol, eob-bol); } -char* file_read_text(Pool& pool, const String& file_spec, +char* file_read_text(Pool& pool, + ConstStringPtr file_spec, bool fail_on_read_problem, - Hash *params, Hash** out_fields) { + StringHash *params, StringHash** out_fields) { void *result; size_t size; - return file_read(pool, file_spec, result, size, true, params, out_fields, fail_on_read_problem)?(char *)result:0; + return file_read(pool, file_spec, result, size, true, params, out_fields, fail_on_read_problem)? + (char *)result:0; } //http request stuff @@ -126,30 +139,30 @@ static bool set_addr(struct sockaddr_in return true; } -static void http_read_response(String& response, int sock){ - bool check_status=true; +static int http_read_response(Pool& pool, String& response, int sock, bool fail_on_status_ne_200){ + int result=0; ssize_t EOLat=0; while(true) { - char *buf=(char *)response.pool().malloc(MAX_STRING); + char *buf=(char *)pool.malloc(MAX_STRING); ssize_t size=recv(sock, buf, MAX_STRING, 0); 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; - + if(!result && (EOLat=response.pos("\r\n", 2))>=0) { // checking status in first response 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); + result=astatus.get_string(1)->as_int(); - if(!status_code || *status_code!="200") + if(fail_on_status_ne_200 && *status_code!="200") throw Exception("http.status", status_code, "invalid HTTP response status"); } - } - if(check_status) + } + if(result) + return result; + else throw Exception("http.response", 0, "bad response from host - no status found (size=%lu)", response.size()); @@ -168,11 +181,12 @@ static void timeout_handler(int sig){ } #endif -static void http_request(String& response, +static int http_request(String& response, const String *origin_string, const char* host, int port, const char* request, - int timeout){ + int timeout, + bool fail_on_status_ne_200){ if(!host) throw Exception("http.host", origin_string, @@ -183,6 +197,7 @@ static void http_request(String& respons #endif int sock=-1; try { + int result; #ifdef WE_CAN_USE_ALARM if(sigsetjmp(timeout_env, 1)) throw Exception("http.timeout", @@ -212,12 +227,13 @@ static void http_request(String& respons origin_string, "error sending request: %s (%d)", strerror(errno), errno); - http_read_response(response, sock); - if(sock>=0) - closesocket(sock); + result=http_read_response(response, sock, fail_on_status_ne_200); + closesocket(sock); #ifdef WE_CAN_USE_ALARM + alarm(0); } #endif + return result; } catch(...) { if(sock>=0) closesocket(sock); @@ -254,6 +270,7 @@ static void file_read_http(Pool& pool, c int port; const char* method="GET"; int timeout=2; + bool fail_on_status_ne_200=true; Value *vheaders=0; String connect_string(pool); @@ -287,6 +304,11 @@ static void file_read_http(Pool& pool, c if(vheaders=static_cast(options->get(*http_headers_name))) { valid_options++; } + if(Value *vany_status=static_cast(options->get(*http_any_status_name))) { + valid_options++; + fail_on_status_ne_200=!vany_status->as_bool(); + } + if(valid_options!=options->size()) throw Exception("parser.runtime", 0, @@ -313,8 +335,9 @@ static void file_read_http(Pool& pool, c //sending request String response(pool); - http_request(response, - &connect_string, host, port, request.cstr(String::UL_UNSPECIFIED), timeout); + int status_code=http_request(response, + &connect_string, host, port, request.cstr(String::UL_UNSPECIFIED), + timeout, fail_on_status_ne_200); //processing results int pos=response.pos("\r\n\r\n", 4); @@ -331,13 +354,6 @@ static void file_read_http(Pool& pool, c size_t pos_after_ref=0; header_block.split(aheaders, &pos_after_ref, "\r\n", 2); - //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 "); - } //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, @@ -524,14 +543,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 @@ -722,7 +745,7 @@ char* rsplit(char* string, char delim) { } /// @todo less stupid type detection -char* format(Pool& pool, double value, char* fmt) { +char* format(double value, char* fmt) { char local_buf[MAX_NUMBER]; size_t size; @@ -758,7 +781,7 @@ size_t stdout_write(const void *buf, siz #endif } -char* unescape_chars(Pool& pool, const char* cp, int len) { +char* unescape_chars(const char* cp, int len) { char* s=(char *)pool.malloc(len + 1, 5); enum EscapeState { EscapeRest, @@ -930,74 +953,3 @@ int pa_sleep(unsigned long secs, unsigne } -// attributed meaning - -static size_t date_attribute(const VDate& vdate, char *buf, size_t buf_size) { - const char month_names[12][4]={ - "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"}; - const char days[7][4]={ - "Sun","Mon","Tue","Wed","Thu","Fri","Sat"}; - - time_t when=vdate.get_time(); - struct tm *tms=gmtime(&when); - if(!tms) - throw Exception(0, - 0, - "bad time in attribute value (seconds from epoch=%ld)", when); - return snprintf(buf, MAX_STRING, "%s, %.2d-%s-%.4d %.2d:%.2d:%.2d GMT", - days[tms->tm_wday], - tms->tm_mday,month_names[tms->tm_mon],tms->tm_year+1900, - tms->tm_hour,tms->tm_min,tms->tm_sec); -} -static void append_attribute_meaning(String& result, - Value& value, String::Untaint_lang lang, bool forced) { - if(const String *string=value.get_string()) - result.append(string->join_chains(result.pool(), 0), lang, forced); - else - if(Value *vdate=value.as(VDATE_TYPE, false)) { - char *buf=(char *)result.malloc(MAX_STRING); - size_t size=date_attribute(*static_cast(vdate), - buf, MAX_STRING); - - result.APPEND_CLEAN(buf, size, "converted from date", 0); - } else - throw Exception("parser.runtime", - &result, - "trying to append here neither string nor date (%s)", - value.type()); -} -#ifndef DOXYGEN -struct Attributed_meaning_info { - String *header; // header line being constructed - String::Untaint_lang lang; // language in which to append to that line - bool forced; // do they force that lang? -}; -#endif -static void append_attribute_subattribute(const Hash::Key& akey, Hash::Val *avalue, - void *info) { - if(akey==VALUE_NAME) - return; - - Attributed_meaning_info& ami=*static_cast(info); - - // ...; charset=windows1251 - *ami.header << "; "; - ami.header->append(akey, ami.lang, ami.forced); - *ami.header << "="; - append_attribute_meaning(*ami.header, *static_cast(avalue), ami.lang, ami.forced); -} -const String& attributed_meaning_to_string(Value& meaning, - String::Untaint_lang lang, bool forced) { - String &result=*new(meaning.pool()) String(meaning.pool()); - if(Hash *hash=meaning.get_hash(0)) { - // $value(value) $subattribute(subattribute value) - if(Value *value=static_cast(hash->get(*value_name))) - append_attribute_meaning(result, *value, lang, forced); - - Attributed_meaning_info attributed_meaning_info={&result, lang}; - hash->for_each(append_attribute_subattribute, &attributed_meaning_info); - } else // result value - append_attribute_meaning(result, meaning, lang, forced); - - return result; -}