|
|
| version 1.181, 2004/03/09 14:49:46 | version 1.192, 2004/08/30 09:33:13 |
|---|---|
| Line 142 static bool set_addr(struct sockaddr_in | Line 142 static bool set_addr(struct sockaddr_in |
| addr->sin_port=htons(port); | addr->sin_port=htons(port); |
| if(host) { | if(host) { |
| ulong packed_ip=inet_addr(host); | ulong packed_ip=inet_addr(host); |
| struct hostent *hostIP = packed_ip==INADDR_NONE? | if(packed_ip!=INADDR_NONE) |
| gethostbyname(host) | memcpy(&addr->sin_addr, &packed_ip, sizeof(packed_ip)); |
| : gethostbyaddr((char *)&packed_ip, sizeof(packed_ip), AF_INET); | else { |
| if(hostIP) | struct hostent *hostIP=gethostbyname(host); |
| memcpy(&addr->sin_addr, hostIP->h_addr, hostIP->h_length); | if(hostIP) |
| else | memcpy(&addr->sin_addr, hostIP->h_addr, hostIP->h_length); |
| return false; | else |
| return false; | |
| } | |
| } else | } else |
| addr->sin_addr.s_addr=INADDR_ANY; | addr->sin_addr.s_addr=INADDR_ANY; |
| return true; | return true; |
| Line 169 static int http_read_response(char*& res | Line 171 static int http_read_response(char*& res |
| response_size+=received_size; | response_size+=received_size; |
| response[response_size]=0; | response[response_size]=0; |
| if(!result && (EOLat=strstr(response, CRLF))) { // 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)); | const String status_line(pa_strdup(response, EOLat-response)); |
| ArrayString astatus; | ArrayString astatus; |
| size_t pos_after=0; | size_t pos_after=0; |
| status_line.split(astatus, pos_after, " "); | status_line.split(astatus, pos_after, " "); |
| const String& status_code=*astatus.get(1); | const String& status_code=*astatus.get(astatus.count()>1?1:0); |
| result=status_code.as_int(); | result=status_code.as_int(); |
| if(fail_on_status_ne_200 && result!=200) | if(fail_on_status_ne_200 && result!=200) |
| Line 562 static File_read_http_result file_read_h | Line 564 static File_read_http_result file_read_h |
| //processing results | //processing results |
| char* raw_body; size_t raw_body_size; | char* raw_body; size_t raw_body_size; |
| char* headers_end_at=strstr(response, CRLF CRLF /*change '4' below along!*/); | char* headers_end_at=strstr(response, "\n\n" /*change '2' below along!*/); |
| if(headers_end_at) { | if(headers_end_at) |
| raw_body=headers_end_at+4; | raw_body=headers_end_at+2; |
| raw_body_size=response_size-(raw_body-response); | else { |
| } else | headers_end_at=strstr(response, CRLF CRLF /*change '4' below along!*/); |
| throw Exception("http.response", | if(headers_end_at) |
| &connect_string, | raw_body=headers_end_at+4; |
| "bad response from host - no headers found"); | 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; | *headers_end_at=0; |
| const String header_block(response, headers_end_at-response, true); | const String header_block(response, headers_end_at-response, true); |
| Line 596 static File_read_http_result file_read_h | Line 610 static File_read_http_result file_read_h |
| const String::Body HEADER_NAME= | const String::Body HEADER_NAME= |
| line.mid(0, pos).change_case(charsets.source(), String::CC_UPPER); | line.mid(0, pos).change_case(charsets.source(), String::CC_UPPER); |
| const String& header_value=line.mid(pos+2, line.length()); | const String& header_value=line.mid(pos+2, line.length()); |
| if(HEADER_NAME=="CONTENT-TYPE") | if(as_text && HEADER_NAME=="CONTENT-TYPE") |
| real_remote_charset=detect_charset(charsets.source(), header_value); | real_remote_charset=detect_charset(charsets.source(), header_value); |
| // tables | // tables |
| Line 624 static File_read_http_result file_read_h | Line 638 static File_read_http_result file_read_h |
| 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 | // output response |
| String::C real_body=String::C(raw_body, raw_body_size); | 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()); | real_body=Charset::transcode(real_body, *real_remote_charset, charsets.source()); |
| } | |
| result.str=const_cast<char *>(real_body.str); // hacking a little | result.str=const_cast<char *>(real_body.str); // hacking a little |
| result.length=real_body.length; | result.length=real_body.length; |
| Line 644 static File_read_http_result file_read_h | Line 659 static File_read_http_result file_read_h |
| #ifndef DOXYGEN | #ifndef DOXYGEN |
| struct File_read_action_info { | struct File_read_action_info { |
| char **data; size_t *data_size; | char **data; size_t *data_size; |
| char* buf; size_t offset; size_t count; | |
| }; | }; |
| #endif | #endif |
| static void file_read_action( | static void file_read_action( |
| Line 652 static void file_read_action( | Line 668 static void file_read_action( |
| const String& file_spec, const char* /*fname*/, bool as_text, | const String& file_spec, const char* /*fname*/, bool as_text, |
| void *context) { | void *context) { |
| File_read_action_info& info=*static_cast<File_read_action_info *>(context); | File_read_action_info& info=*static_cast<File_read_action_info *>(context); |
| if(size_t to_read_size=(size_t)finfo.st_size) { | size_t to_read_size=info.count; |
| *info.data=new(PointerFreeGC) char[to_read_size+(as_text?1:0)]; | 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); | *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) | if(ssize_t(*info.data_size)<0 || *info.data_size>to_read_size) |
| Line 673 static void file_read_action( | Line 697 static void file_read_action( |
| } | } |
| File_read_result file_read(Request_charsets& charsets, const String& file_spec, | File_read_result file_read(Request_charsets& charsets, const String& file_spec, |
| bool as_text, HashStringValue *params, | 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}; | File_read_result result={false, 0, 0, 0}; |
| #ifdef PA_HTTP | #ifdef PA_HTTP |
| if(file_spec.starts_with("http://")) { | if(file_spec.starts_with("http://")) { |
| Line 690 File_read_result file_read(Request_chars | Line 715 File_read_result file_read(Request_chars |
| 0, | 0, |
| "invalid option passed"); | "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, | result.success=file_read_action_under_lock(file_spec, |
| "read", file_read_action, &info, | "read", file_read_action, &info, |
| as_text, fail_on_read_problem); | as_text, fail_on_read_problem); |
| Line 1045 const char* format(double value, char* f | Line 1071 const char* format(double value, char* f |
| size_t stdout_write(const void *buf, size_t size) { | size_t stdout_write(const void *buf, size_t size) { |
| #ifdef WIN32 | #ifdef WIN32 |
| size_t to_write = size; | |
| do{ | do{ |
| int chunk_written=fwrite(buf, 1, min((size_t)8*0x400, size), stdout); | int chunk_written=fwrite(buf, 1, min((size_t)8*0x400, size), stdout); |
| if(chunk_written<=0) | if(chunk_written<=0) |
| Line 1053 size_t stdout_write(const void *buf, siz | Line 1080 size_t stdout_write(const void *buf, siz |
| buf=((const char*)buf)+chunk_written; | buf=((const char*)buf)+chunk_written; |
| } while(size>0); | } while(size>0); |
| return size; | return to_write-size; |
| #else | #else |
| return fwrite(buf, 1, size, stdout); | return fwrite(buf, 1, size, stdout); |
| #endif | #endif |
| Line 1123 bool StrEqNc(const char* s1, const char* | Line 1150 bool StrEqNc(const char* s1, const char* |
| return !strict; | return !strict; |
| } else if(!(*s2)) | } else if(!(*s2)) |
| return !strict; | return !strict; |
| if(isalpha(*s1)) { | if(isalpha((unsigned char)*s1)) { |
| if(tolower(*s1) !=tolower(*s2)) | if(tolower((unsigned char)*s1) !=tolower((unsigned char)*s2)) |
| return false; | return false; |
| } else if((*s1) !=(*s2)) | } else if((*s1) !=(*s2)) |
| return false; | return false; |
| Line 1264 static char *base64_alphabet = | Line 1291 static char *base64_alphabet = |
| static size_t | static size_t |
| g_mime_utils_base64_encode_step (const unsigned char *in, size_t inlen, unsigned char *out, int *state, int *save) | 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; | register unsigned char *outptr; |
| if (inlen <= 0) | if (inlen <= 0) |
| Line 1387 char* pa_base64(const char *in, size_t l | Line 1414 char* pa_base64(const char *in, size_t l |
| char* result=new(PointerFreeGC) char[len * 2 + 6]; | char* result=new(PointerFreeGC) char[len * 2 + 6]; |
| int state=0; | int state=0; |
| int save=0; | int save=0; |
| size_t filled=g_mime_utils_base64_encode_close ((const unsigned char*)in, len, | #ifndef NDEBUG |
| size_t filled= | |
| #endif | |
| g_mime_utils_base64_encode_close ((const unsigned char*)in, len, | |
| (unsigned char*)result, &state, &save); | (unsigned char*)result, &state, &save); |
| assert(filled <= len * 2 + 6); | assert(filled <= len * 2 + 6); |