--- parser3/src/main/pa_common.C 2004/08/27 15:49:48 1.191 +++ parser3/src/main/pa_common.C 2005/11/16 14:49:41 1.206 @@ -1,11 +1,11 @@ /** @file Parser: commonly functions. - Copyright(c) 2001-2004 ArtLebedev Group (http://www.artlebedev.com) + Copyright(c) 2001-2005 ArtLebedev Group (http://www.artlebedev.com) Author: Alexandr Petrosian (http://paf.design.ru) */ -static const char * const IDENT_COMMON_C="$Date: 2004/08/27 15:49:48 $"; +static const char * const IDENT_COMMON_C="$Date: 2005/11/16 14:49:41 $"; #include "pa_common.h" #include "pa_exception.h" @@ -21,6 +21,7 @@ static const char * const IDENT_COMMON_C #include "pa_vint.h" #include "pa_vhash.h" #include "pa_vtable.h" +#include "pa_socks.h" #ifdef CYGWIN #define _GNU_H_WINDOWS32_SOCKETS @@ -130,6 +131,21 @@ char* file_read_text(Request_charsets& c return file.success?file.str:0; } +/// these options were handled but not checked elsewhere, now check them +static int get_valid_file_options_count(HashStringValue& options) +{ + int result=0; + if(options.get(PA_SQL_LIMIT_NAME)) + result++; + if(options.get(PA_SQL_OFFSET_NAME)) + result++; + if(options.get(PA_COLUMN_SEPARATOR_NAME)) + result++; + if(options.get(PA_COLUMN_ENCLOSER_NAME)) + result++; + return result; +} + //http request stuff #ifdef PA_HTTP @@ -164,14 +180,21 @@ static int http_read_response(char*& res while(true) { char buf[MAX_STRING*10]; ssize_t received_size=recv(sock, buf, sizeof(buf), 0); - if(received_size<=0) + if(received_size==0) + break; + if(received_size<0) { + if(int no=pa_socks_errno()) + throw Exception("http.timeout", + 0, + "error receiving response: %s (%d)", pa_socks_strerr(no), no); break; + } response=(char*)pa_realloc(response, response_size+received_size+1/*terminator*/); memcpy(response+response_size, buf, received_size); response_size+=received_size; response[response_size]=0; - if(!result && (EOLat=strstr(response, "\r"))) { // checking status in first response + if(!result && (EOLat=strstr(response, "\n"))) { // checking status in first response const String status_line(pa_strdup(response, EOLat-response)); ArrayString astatus; size_t pos_after=0; @@ -201,42 +224,39 @@ static int http_read_response(char*& res #ifdef PA_USE_ALARM static sigjmp_buf timeout_env; -static void timeout_handler(int sig){ +static void timeout_handler(int /*sig*/){ siglongjmp(timeout_env, 1); } #endif static int http_request(char*& response, size_t& response_size, - const char* host, int port, + const char* host, short port, const char* request, - int -#ifdef PA_USE_ALARM - timeout -#endif - , + int timeout_secs, bool fail_on_status_ne_200) { if(!host) throw Exception("http.host", 0, "zero hostname"); //never + volatile // to prevent makeing it register variable, because it will be clobbered by longjmp [thanks gcc warning] + int sock=-1; #ifdef PA_USE_ALARM signal(SIGALRM, timeout_handler); #endif - int sock=-1; #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] + // rewritten simplier [athough duplicating closesocket code] if(sock>=0) closesocket(sock); throw Exception("http.timeout", - origin_string, + 0, "timeout occured while retrieving document"); return 0; // never } else { - alarm(timeout); + alarm(timeout_secs); #endif try { int result; @@ -247,19 +267,40 @@ static int http_request(char*& response, 0, "can not resolve hostname \"%s\"", host); - if((sock=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP/*0*/))<0) + if((sock=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP/*0*/))<0) { + int no=pa_socks_errno(); throw Exception("http.connect", 0, - "can not make socket: %s (%d)", strerror(errno), errno); - if(connect(sock, (struct sockaddr *)&dest, sizeof(dest))) + "can not make socket: %s (%d)", pa_socks_strerr(no), no); + } + + // To enable SO_DONTLINGER (that is, disable SO_LINGER) + // l_onoff should be set to zero and setsockopt should be called + linger dont_linger={0,0}; + setsockopt(sock, SOL_SOCKET, SO_LINGER, (const char *)&dont_linger, sizeof(dont_linger)); + +#ifdef WIN32 +// SO_*TIMEO can be defined in .h but not implemlemented in protocol, +// failing subsequently with Option not supported by protocol (99) message +// could not suppress that, so leaving this only for win32 + int timeout_ms=timeout_secs*1000; + setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (const char*)&timeout_ms, sizeof(timeout_ms)); + setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (const char*)&timeout_ms, sizeof(timeout_ms)); +#endif + + if(connect(sock, (struct sockaddr *)&dest, sizeof(dest))) { + int no=pa_socks_errno(); throw Exception("http.connect", 0, - "can not connect to host \"%s\": %s (%d)", host, strerror(errno), errno); + "can not connect to host \"%s\": %s (%d)", host, pa_socks_strerr(no), no); + } size_t request_size=strlen(request); - if(send(sock, request, request_size, 0)!=(ssize_t)request_size) - throw Exception("http.connect", + if(send(sock, request, request_size, 0)!=(ssize_t)request_size) { + int no=pa_socks_errno(); + throw Exception("http.timeout", 0, - "error sending request: %s (%d)", strerror(errno), errno); + "error sending request: %s (%d)", pa_socks_strerr(no), no); + } result=http_read_response(response, response_size, sock, fail_on_status_ne_200); closesocket(sock); @@ -393,6 +434,25 @@ struct File_read_http_result { HashStringValue* headers; }; #endif +static void find_headers_end(char* p, + char*& headers_end_at, + char*& raw_body) +{ + raw_body=p; + // \n\n + // \r\n\r\n + while((p=strchr(p, '\n'))) { + headers_end_at=++p; // \n>.< + if(*p=='\r') // \r\n>\r?<\n + p++; + if(*p=='\n') { // \r\n\r>\n?< + raw_body=p+1; + return; + } + } + headers_end_at=0; +} + /// @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, @@ -401,11 +461,11 @@ static File_read_http_result file_read_h File_read_http_result result; char host[MAX_STRING]; const char* uri; - int port; + short port; const char* method="GET"; bool method_is_get; HashStringValue* form=0; const char* body_cstr=0; - int timeout=2; + int timeout_secs=2; bool fail_on_status_ne_200=true; Value* vheaders=0; Charset *asked_remote_charset=0; @@ -413,7 +473,8 @@ static File_read_http_result file_read_h const char* password_cstr=0; if(options) { - int valid_options=0; + int valid_options=get_valid_file_options_count(*options); + if(Value* vmethod=options->get(HTTP_METHOD_NAME)) { valid_options++; method=vmethod->as_string().cstr(); @@ -428,7 +489,7 @@ static File_read_http_result file_read_h } if(Value* vtimeout=options->get(HTTP_TIMEOUT_NAME)) { valid_options++; - timeout=vtimeout->as_int(); + timeout_secs=vtimeout->as_int(); } if((vheaders=options->get(HTTP_HEADERS_NAME))) { valid_options++; @@ -489,7 +550,7 @@ static File_read_http_result file_read_h uri=host_uri?current+(host_uri-1-host):"/"; char* port_cstr=lsplit(host, ':'); char* error_pos=0; - port=port_cstr?strtol(port_cstr, &error_pos, 0):80; + port=port_cstr?(short)strtol(port_cstr, &error_pos, 0):80; if(strchr(uri, '?') && form) throw Exception("parser.runtime", @@ -560,92 +621,81 @@ static File_read_http_result file_read_h size_t response_size; int status_code=http_request(response, response_size, host, port, request_head_and_body.cstr(), - timeout, fail_on_status_ne_200); + timeout_secs, fail_on_status_ne_200); //processing results char* raw_body; size_t raw_body_size; - char* headers_end_at=strstr(response, "\n\n" /*change '2' below along!*/); - if(headers_end_at) - raw_body=headers_end_at+2; - else { - headers_end_at=strstr(response, CRLF CRLF /*change '4' below along!*/); - if(headers_end_at) - raw_body=headers_end_at+4; - else { - // yandex web server (http://localhost:17000) - // returns "\n\r\n" - headers_end_at=strstr(response, "\n\r\n" /*change '3' below along!*/); - if(!headers_end_at) - throw Exception("http.response", - &connect_string, - "bad response from host - no headers found"); - - raw_body=headers_end_at+3; - } - } + char* headers_end_at; + find_headers_end(response, + headers_end_at, + raw_body); raw_body_size=response_size-(raw_body-response); - *headers_end_at=0; - const String header_block(response, headers_end_at-response, true); - - ArrayString aheaders; result.headers=new HashStringValue; VHash* vtables=new VHash; result.headers->put(HTTP_TABLES_NAME, vtables); - HashStringValue& tables=vtables->hash(); - - size_t pos_after=0; - header_block.split(aheaders, pos_after, CRLF); - - //processing headers - size_t aheaders_count=aheaders.count(); Charset* real_remote_charset=0; // undetected, yet - for(size_t i=1; iget_table(); - } else { - // first appearence - Table::columns_type columns =new ArrayString(1); - *columns+=new String("value"); - table=new Table(columns); + + if(headers_end_at) { + *headers_end_at=0; + const String header_block(String::C(response, headers_end_at-response), true); + + ArrayString aheaders; + HashStringValue& tables=vtables->hash(); + + size_t pos_after=0; + header_block.split(aheaders, pos_after, "\n"); + + //processing headers + size_t aheaders_count=aheaders.count(); + for(size_t i=1; iget_table(); + } else { + // first appearence + Table::columns_type columns =new ArrayString(1); + *columns+=new String("value"); + table=new Table(columns); + } + // this string becomes next row + ArrayString& row=*new ArrayString(1); + row+=&header_value; + *table+=&row; + // not existed before? add it + if(!existed) + tables.put(HEADER_NAME, new VTable(table)); } - // this string becomes next row - ArrayString& row=*new ArrayString(1); - row+=&header_value; - *table+=&row; - // not existed before? add it - if(!existed) - tables.put(HEADER_NAME, new VTable(table)); - } - result.headers->put(HEADER_NAME, new VString(header_value)); + result.headers->put(HEADER_NAME, new VString(header_value)); + } } - // defaulting to used-asked charset [it's never empty!] - if(!real_remote_charset) - real_remote_charset=asked_remote_charset; // output response String::C real_body=String::C(raw_body, raw_body_size); - if(as_text && raw_body_size) // must be checked because transcode returns CONST string in case length==0, which contradicts hacking few lines below + if(as_text && raw_body_size) { // must be checked because transcode returns CONST string in case length==0, which contradicts hacking few lines below + // defaulting to used-asked charset [it's never empty!] + if(!real_remote_charset) + real_remote_charset=asked_remote_charset; 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; @@ -701,6 +751,11 @@ File_read_result file_read(Request_chars File_read_result result={false, 0, 0, 0}; #ifdef PA_HTTP if(file_spec.starts_with("http://")) { + if(offset || count) + throw Exception("parser.runtime", + 0, + "offset and load options are not supported for HTTP:// file load"); + // fail on read problem File_read_http_result http=file_read_http(charsets, file_spec, as_text, params); result.success=true; @@ -709,10 +764,13 @@ File_read_result file_read(Request_chars result.headers=http.headers; } else { #endif - if(params && params->count()) - throw Exception("parser.runtime", - 0, - "invalid option passed"); + if(params) { + int valid_options=get_valid_file_options_count(*params); + if(valid_options!=params->count()) + throw Exception("parser.runtime", + 0, + "invalid option passed"); + } File_read_action_info info={&result.str, &result.length, buf, offset, count}; @@ -810,7 +868,7 @@ bool file_read_action_under_lock(const S } } -static void create_dir_for_file(const String& file_spec) { +void create_dir_for_file(const String& file_spec) { size_t pos_after=1; size_t pos_before; while((pos_before=file_spec.pos('/', pos_after))!=STRING_NOT_FOUND) { @@ -1092,11 +1150,11 @@ char* unescape_chars(const char* cp, int EscapeFirst, EscapeSecond } escapeState=EscapeRest; - int escapedValue=0; + uchar escapedValue=0; int srcPos=0; int dstPos=0; while(srcPos < len) { - int ch=cp[srcPos]; + uchar ch=(uchar)cp[srcPos]; switch(escapeState) { case EscapeRest: if(ch=='%') { @@ -1108,7 +1166,7 @@ char* unescape_chars(const char* cp, int } break; case EscapeFirst: - escapedValue=hex_value[ch] << 4; + escapedValue=(uchar)(hex_value[ch] << 4); escapeState=EscapeSecond; break; case EscapeSecond: