--- parser3/src/main/pa_common.C 2004/04/05 09:23:31 1.182.4.1 +++ parser3/src/main/pa_common.C 2004/08/27 15:49:48 1.191 @@ -5,7 +5,7 @@ Author: Alexandr Petrosian (http://paf.design.ru) */ -static const char * const IDENT_COMMON_C="$Date: 2004/04/05 09:23:31 $"; +static const char * const IDENT_COMMON_C="$Date: 2004/08/27 15:49:48 $"; #include "pa_common.h" #include "pa_exception.h" @@ -142,13 +142,15 @@ static bool set_addr(struct sockaddr_in addr->sin_port=htons(port); if(host) { ulong packed_ip=inet_addr(host); - struct hostent *hostIP = packed_ip==INADDR_NONE? - gethostbyname(host) - : gethostbyaddr((char *)&packed_ip, sizeof(packed_ip), AF_INET); - if(hostIP) - memcpy(&addr->sin_addr, hostIP->h_addr, hostIP->h_length); - else - return false; + if(packed_ip!=INADDR_NONE) + memcpy(&addr->sin_addr, &packed_ip, sizeof(packed_ip)); + else { + struct hostent *hostIP=gethostbyname(host); + if(hostIP) + memcpy(&addr->sin_addr, hostIP->h_addr, hostIP->h_length); + else + return false; + } } else addr->sin_addr.s_addr=INADDR_ANY; return true; @@ -169,7 +171,7 @@ static int http_read_response(char*& res response_size+=received_size; response[response_size]=0; - if(!result && (EOLat=strstr(response, CRLF))) { // checking status in first response + if(!result && (EOLat=strstr(response, "\r"))) { // checking status in first response const String status_line(pa_strdup(response, EOLat-response)); ArrayString astatus; size_t pos_after=0; @@ -562,14 +564,26 @@ static File_read_http_result file_read_h //processing results char* raw_body; size_t raw_body_size; - char* headers_end_at=strstr(response, CRLF CRLF /*change '4' below along!*/); - if(headers_end_at) { - raw_body=headers_end_at+4; - raw_body_size=response_size-(raw_body-response); - } else - throw Exception("http.response", - &connect_string, - "bad response from host - no headers found"); + 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; + } + } + raw_body_size=response_size-(raw_body-response); *headers_end_at=0; const String header_block(response, headers_end_at-response, true); @@ -644,6 +658,7 @@ static File_read_http_result file_read_h #ifndef DOXYGEN struct File_read_action_info { char **data; size_t *data_size; + char* buf; size_t offset; size_t count; }; #endif static void file_read_action( @@ -652,8 +667,16 @@ static void file_read_action( 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) { - *info.data=new(PointerFreeGC) char[to_read_size+(as_text?1:0)]; + size_t to_read_size=info.count; + if(!to_read_size) + to_read_size=(size_t)finfo.st_size; + assert( !(info.buf && as_text) ); + if(to_read_size) { + if(info.offset) + lseek(f, info.offset, SEEK_SET); + *info.data=info.buf + ? info.buf + : new(PointerFreeGC) char[to_read_size+(as_text?1:0)]; *info.data_size=(size_t)read(f, *info.data, to_read_size); if(ssize_t(*info.data_size)<0 || *info.data_size>to_read_size) @@ -673,7 +696,8 @@ 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) { + bool fail_on_read_problem, + char* buf, size_t offset, size_t count) { File_read_result result={false, 0, 0, 0}; #ifdef PA_HTTP if(file_spec.starts_with("http://")) { @@ -690,7 +714,8 @@ File_read_result file_read(Request_chars 0, "invalid option passed"); - File_read_action_info info={&result.str, &result.length}; + File_read_action_info info={&result.str, &result.length, + buf, offset, count}; result.success=file_read_action_under_lock(file_spec, "read", file_read_action, &info, as_text, fail_on_read_problem); @@ -1045,6 +1070,7 @@ const char* format(double value, char* f size_t stdout_write(const void *buf, size_t size) { #ifdef WIN32 + size_t to_write = size; do{ int chunk_written=fwrite(buf, 1, min((size_t)8*0x400, size), stdout); if(chunk_written<=0) @@ -1053,7 +1079,7 @@ size_t stdout_write(const void *buf, siz buf=((const char*)buf)+chunk_written; } while(size>0); - return size; + return to_write-size; #else return fwrite(buf, 1, size, stdout); #endif @@ -1123,8 +1149,8 @@ bool StrEqNc(const char* s1, const char* return !strict; } else if(!(*s2)) return !strict; - if(isalpha(*s1)) { - if(tolower(*s1) !=tolower(*s2)) + if(isalpha((unsigned char)*s1)) { + if(tolower((unsigned char)*s1) !=tolower((unsigned char)*s2)) return false; } else if((*s1) !=(*s2)) return false; @@ -1264,7 +1290,7 @@ static char *base64_alphabet = static size_t g_mime_utils_base64_encode_step (const unsigned char *in, size_t inlen, unsigned char *out, int *state, int *save) { - const register unsigned char *inptr; + register const unsigned char *inptr; register unsigned char *outptr; if (inlen <= 0)