--- parser3/src/main/pa_common.C 2003/01/16 08:44:19 1.141 +++ parser3/src/main/pa_common.C 2003/06/20 09:56:01 1.152 @@ -1,18 +1,21 @@ /** @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: 2003/01/16 08:44:19 $"; +static const char* IDENT_COMMON_C="$Date: 2003/06/20 09:56:01 $"; #include "pa_common.h" #include "pa_exception.h" #include "pa_globals.h" #include "pa_hash.h" +#include "pa_table.h" #include "pa_vstring.h" #include "pa_vdate.h" +#include "pa_vhash.h" +#include "pa_vtable.h" #ifdef WIN32 # include @@ -139,7 +142,8 @@ static bool set_addr(struct sockaddr_in return true; } -static void http_read_response(String& response, int sock){ +static int http_read_response(String& response, int sock, bool fail_on_status_ne_200){ + const String* status_code=0; ssize_t EOLat=0; while(true) { char *buf=(char *)response.pool().malloc(MAX_STRING); @@ -147,48 +151,73 @@ static void http_read_response(String& r if(size<=0) break; response.APPEND_TAINTED(buf, size, "remote HTTP server response", 0); - } + if(!status_code && (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); + status_code=astatus.get_string(1); + + if(fail_on_status_ne_200 && *status_code!="200") + throw Exception("http.status", + status_code, + "invalid HTTP response status"); + } + } + if(status_code) + return status_code->as_int(); + else + throw Exception("http.response", + 0, + "bad response from host - no status found (size=%lu)", response.size()); } /* ********************** request *************************** */ #if defined(SIGALRM) && defined(HAVE_SIGSETJMP) && defined(HAVE_SIGLONGJMP) -# define WE_CAN_USE_ALARM +# define PA_USE_ALARM #endif -#ifdef WE_CAN_USE_ALARM +#ifdef PA_USE_ALARM static sigjmp_buf timeout_env; static void timeout_handler(int sig){ siglongjmp(timeout_env, 1); } #endif -static void http_request(String& response, - const String *origin_string, - const char* host, int port, - const char* request, - int timeout){ +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) { if(!host) throw Exception("http.host", origin_string, "zero hostname"); //never -#ifdef WE_CAN_USE_ALARM - signal(SIGALRM, timeout_handler); +#ifdef PA_USE_ALARM + signal(SIGALRM, timeout_handler); #endif int sock=-1; - try { -#ifdef WE_CAN_USE_ALARM - if(sigsetjmp(timeout_env, 1)) - throw Exception("http.timeout", - origin_string, - "timeout occured while retrieving document"); - else { - alarm(timeout); +#ifdef PA_USE_ALARM + if(sigsetjmp(timeout_env, 1)) { + // stupid gcc [2.95.4] generated bad code + // which failed to handle sigsetjmp+throw: crashed inside of pre-throw code. + // rewritten simplier [though duplicating closesocket code] + if(sock>=0) + closesocket(sock); + throw Exception("http.timeout", + origin_string, + "timeout occured while retrieving document"); + return 0; // never + } else { + alarm(timeout); #endif + try { + int result; struct sockaddr_in dest; - - if(!set_addr(&dest, host, port)) + + if(!set_addr(&dest, host, port)) throw Exception("http.host", origin_string, "can not resolve hostname \"%s\"", host); @@ -207,20 +236,23 @@ static void http_request(String& respons origin_string, "error sending request: %s (%d)", strerror(errno), errno); - http_read_response(response, sock); + result=http_read_response(response, sock, fail_on_status_ne_200); + closesocket(sock); +#ifdef PA_USE_ALARM + alarm(0); +#endif + return result; + } catch(...) { +#ifdef PA_USE_ALARM + alarm(0); +#endif if(sock>=0) closesocket(sock); -#ifdef WE_CAN_USE_ALARM + /*re*/throw; } -#endif - } catch(...) { - if(sock>=0) - closesocket(sock); -#ifdef WE_CAN_USE_ALARM - alarm(0); -#endif - /*re*/throw; +#ifdef PA_USE_ALARM } +#endif } #ifndef DOXYGEN @@ -241,6 +273,7 @@ static void http_pass_header(const Hash: if(key.change_case(pool, String::CC_UPPER)=="USER-AGENT") i.user_agent_specified=true; } +/// @todo build .cookies field. use ^file.tables.SET-COOKIES.menu{ for now static void file_read_http(Pool& pool, const String& file_spec, void*& data, size_t& data_size, Hash *options=0, Hash** out_fields=0) { @@ -249,9 +282,10 @@ 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); + String& connect_string=*new(pool) String(pool); // must not be local [exception may be reported outside] // not in ^sql{... UL_SQL ...} spirit, but closer to ^file::load one connect_string.append(file_spec, String::UL_URI); // tainted pieces -> URI pieces @@ -282,6 +316,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, @@ -308,8 +347,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); @@ -323,16 +363,13 @@ static void file_read_http(Pool& pool, c Array aheaders(pool); Hash& headers=*new(pool) Hash(pool); + VHash* vtables=new(pool) VHash(pool); + headers.put(*http_tables_name, vtables); + Hash& tables=vtables->hash(0); + 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); - 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;icstr()); - - headers.put( - line->mid(0, pos).change_case(pool, String::CC_UPPER), - new(pool) VString(line->mid(pos+2, line->size()))); + + const String& sname=line->mid(0, pos).change_case(pool, String::CC_UPPER); + const String& string=line->mid(pos+2, line->size()); + + // tables + { + Value *valready=(Value *)tables.get(sname); + bool existed=valready!=0; + Table *table; + if(existed) { + // second+ appearence + table=valready->get_table(); + } else { + // first appearence + Array& columns=*new(pool) Array(pool, 1); + columns+=new(pool) String(pool, "value"); + table=new(pool) Table(pool, 0, &columns); + } + // this string becomes next row + Array& row=*new(pool) Array(pool, 1); + row+=&string; + *table+=&row; + // not existed before? add it + if(!existed) + tables.put(sname, new(pool) VTable(pool, table)); + } + headers.put(sname, new(pool) VString(string)); } else throw Exception("http.response", &connect_string, @@ -354,7 +414,7 @@ static void file_read_http(Pool& pool, c // output response data=body.cstr(); data_size=body.size(); if(out_fields) { - headers.put(*file_status_name, new(pool) VString(*status_code)); + headers.put(*file_status_name, new(pool) VInt(pool, status_code)); *out_fields=&headers; } } @@ -423,6 +483,23 @@ bool file_read(Pool& pool, const String& return result; } +#ifdef PA_SAFE_MODE +void check_safe_mode(struct stat finfo, const String& file_spec, const char* fname) { + if(finfo.st_uid/*foreign?*/!=geteuid() + && finfo.st_gid/*foreign?*/!=getegid()) + throw Exception("parser.runtime", + &file_spec, + "parser is in safe mode: " + "reading files of foreign group and user disabled " + "[recompile parser with --disable-safe-mode configure option], " + "actual filename '%s', " + "fuid(%d)!=euid(%d) or fgid(%d)!=egid(%d)", + fname, + finfo.st_uid, geteuid(), + finfo.st_gid, getegid()); +} +#endif + bool file_read_action_under_lock(Pool& pool, const String& file_spec, const char* action_name, File_read_action action, void *context, bool as_text, @@ -455,12 +532,7 @@ bool file_read_action_under_lock(Pool& p strerror(errno), errno, fname); #ifdef PA_SAFE_MODE - if(finfo.st_uid/*foreign?*/!=geteuid() - && finfo.st_gid/*foreign?*/!=getegid()) - throw Exception("parser.runtime", - &file_spec, - "parser is in safe mode: reading files of foreign group and user disabled [recompile parser with --disable-safe-mode configure option], actual filename '%s'", - fname); + check_safe_mode(finfo, file_spec, fname); #endif action(pool, finfo, f, file_spec, fname, as_text, context); @@ -934,6 +1006,7 @@ int pa_sleep(unsigned long secs, unsigne // attributed meaning +/// http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3 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"}; @@ -946,7 +1019,7 @@ static size_t date_attribute(const VDate 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", + 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);