|
|
| version 1.140.2.1, 2003/02/24 12:38:54 | version 1.153.2.2, 2003/09/02 07:27:03 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: commonly functions. | 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 <paf@design.ru> (http://paf.design.ru) | Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) |
| */ | */ |
| Line 11 static const char* IDENT_COMMON_C="$Date | Line 11 static const char* IDENT_COMMON_C="$Date |
| #include "pa_exception.h" | #include "pa_exception.h" |
| #include "pa_globals.h" | #include "pa_globals.h" |
| #include "pa_hash.h" | #include "pa_hash.h" |
| #include "pa_table.h" | |
| #include "pa_vstring.h" | #include "pa_vstring.h" |
| #include "pa_vdate.h" | #include "pa_vdate.h" |
| #include "pa_vhash.h" | |
| #include "pa_vtable.h" | |
| #ifdef WIN32 | #ifdef WIN32 |
| # include <windows.h> | # include <windows.h> |
| Line 139 static bool set_addr(struct sockaddr_in | Line 142 static bool set_addr(struct sockaddr_in |
| return true; | 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){ |
| bool check_status=true; | const String* status_code=0; |
| ssize_t EOLat=0; | ssize_t EOLat=0; |
| while(true) { | while(true) { |
| char *buf=(char *)response.pool().malloc(MAX_STRING); | char *buf=(char *)response.pool().malloc(MAX_STRING); |
| Line 148 static void http_read_response(String& r | Line 151 static void http_read_response(String& r |
| if(size<=0) | if(size<=0) |
| break; | break; |
| response.APPEND_TAINTED(buf, size, "remote HTTP server response", 0); | 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 | if(!status_code && (EOLat=response.pos("\r\n", 2))>=0) { // checking status in first response |
| check_status=false; | |
| const String& status_line=response.mid(0, (size_t)EOLat); | const String& status_line=response.mid(0, (size_t)EOLat); |
| Array astatus(response.pool()); | Array astatus(response.pool()); |
| size_t pos_after_ref=0; status_line.split(astatus, &pos_after_ref, " ", 1); | size_t pos_after_ref=0; status_line.split(astatus, &pos_after_ref, " ", 1); |
| const String* status_code=astatus.get_string(1); | status_code=astatus.get_string(1); |
| if(!status_code || *status_code!="200") | if(fail_on_status_ne_200 && *status_code!="200") |
| throw Exception("http.status", | throw Exception("http.status", |
| status_code, | status_code, |
| "invalid HTTP response status"); | "invalid HTTP response status"); |
| } | } |
| } | } |
| if(check_status) | if(status_code) |
| return status_code->as_int(); | |
| else | |
| throw Exception("http.response", | throw Exception("http.response", |
| 0, | 0, |
| "bad response from host - no status found (size=%lu)", response.size()); | "bad response from host - no status found (size=%lu)", response.size()); |
| Line 181 static void timeout_handler(int sig){ | Line 184 static void timeout_handler(int sig){ |
| } | } |
| #endif | #endif |
| static void http_request(String& response, | static int http_request(String& response, |
| const String *origin_string, | const String *origin_string, |
| const char* host, int port, | const char* host, int port, |
| const char* request, | const char* request, |
| int timeout){ | int timeout, |
| bool fail_on_status_ne_200) { | |
| if(!host) | if(!host) |
| throw Exception("http.host", | throw Exception("http.host", |
| origin_string, | origin_string, |
| "zero hostname"); //never | "zero hostname"); //never |
| #ifdef PA_USE_ALARM | #ifdef PA_USE_ALARM |
| signal(SIGALRM, timeout_handler); | signal(SIGALRM, timeout_handler); |
| #endif | #endif |
| int sock=-1; | int sock=-1; |
| #ifdef PA_USE_ALARM | #ifdef PA_USE_ALARM |
| Line 205 static void http_request(String& respons | Line 209 static void http_request(String& respons |
| throw Exception("http.timeout", | throw Exception("http.timeout", |
| origin_string, | origin_string, |
| "timeout occured while retrieving document"); | "timeout occured while retrieving document"); |
| return 0; // never | |
| } else { | } else { |
| alarm(timeout); | alarm(timeout); |
| #endif | #endif |
| try { | try { |
| struct sockaddr_in dest; | int result; |
| struct sockaddr_in dest; | |
| if(!set_addr(&dest, host, port)) | |
| throw Exception("http.host", | |
| origin_string, | |
| "can not resolve hostname \"%s\"", host); | |
| if((sock=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP/*0*/))<0) | |
| throw Exception("http.connect", | |
| origin_string, | |
| "can not make socket: %s (%d)", strerror(errno), errno); | |
| if(connect(sock, (struct sockaddr *)&dest, sizeof(dest))) | |
| throw Exception("http.connect", | |
| origin_string, | |
| "can not connect to host \"%s\": %s (%d)", host, strerror(errno), errno); | |
| size_t request_size=strlen(request); | |
| if(send(sock, request, request_size, 0)!=(ssize_t)request_size) | |
| throw Exception("http.connect", | |
| origin_string, | |
| "error sending request: %s (%d)", strerror(errno), errno); | |
| http_read_response(response, sock); | if(!set_addr(&dest, host, port)) |
| closesocket(sock); | throw Exception("http.host", |
| origin_string, | |
| "can not resolve hostname \"%s\"", host); | |
| if((sock=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP/*0*/))<0) | |
| throw Exception("http.connect", | |
| origin_string, | |
| "can not make socket: %s (%d)", strerror(errno), errno); | |
| if(connect(sock, (struct sockaddr *)&dest, sizeof(dest))) | |
| throw Exception("http.connect", | |
| origin_string, | |
| "can not connect to host \"%s\": %s (%d)", host, strerror(errno), errno); | |
| size_t request_size=strlen(request); | |
| if(send(sock, request, request_size, 0)!=(ssize_t)request_size) | |
| throw Exception("http.connect", | |
| origin_string, | |
| "error sending request: %s (%d)", strerror(errno), errno); | |
| result=http_read_response(response, sock, fail_on_status_ne_200); | |
| closesocket(sock); | |
| #ifdef PA_USE_ALARM | #ifdef PA_USE_ALARM |
| alarm(0); | alarm(0); |
| #endif | #endif |
| } catch(...) { | return result; |
| if(sock>=0) | } catch(...) { |
| closesocket(sock); | |
| #ifdef PA_USE_ALARM | #ifdef PA_USE_ALARM |
| alarm(0); | alarm(0); |
| #endif | #endif |
| /*re*/throw; | if(sock>=0) |
| closesocket(sock); | |
| /*re*/throw; | |
| } | |
| #ifdef PA_USE_ALARM | |
| } | } |
| #endif | |
| } | } |
| #undef CRLF | |
| #define CRLF "\r\n" | |
| #ifndef DOXYGEN | #ifndef DOXYGEN |
| struct Http_pass_header_info { | struct Http_pass_header_info { |
| String* request; | String* request; |
| Line 258 static void http_pass_header(const Hash: | Line 271 static void http_pass_header(const Hash: |
| *(i.request)<<key<<": " | *(i.request)<<key<<": " |
| << attributed_meaning_to_string(*static_cast<Value *>(value), String::UL_HTTP_HEADER, false) | << attributed_meaning_to_string(*static_cast<Value *>(value), String::UL_HTTP_HEADER, false) |
| <<"\n"; | << CRLF; |
| if(key.change_case(pool, String::CC_UPPER)=="USER-AGENT") | if(key.change_case(pool, String::CC_UPPER)=="USER-AGENT") |
| i.user_agent_specified=true; | 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, | static void file_read_http(Pool& pool, const String& file_spec, |
| void*& data, size_t& data_size, | void*& data, size_t& data_size, |
| Hash *options=0, Hash** out_fields=0) { | Hash *options=0, Hash** out_fields=0) { |
| Line 271 static void file_read_http(Pool& pool, c | Line 285 static void file_read_http(Pool& pool, c |
| int port; | int port; |
| const char* method="GET"; | const char* method="GET"; |
| int timeout=2; | int timeout=2; |
| bool fail_on_status_ne_200=true; | |
| Value *vheaders=0; | 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 | // not in ^sql{... UL_SQL ...} spirit, but closer to ^file::load one |
| connect_string.append(file_spec, String::UL_URI); // tainted pieces -> URI pieces | connect_string.append(file_spec, String::UL_URI); // tainted pieces -> URI pieces |
| Line 304 static void file_read_http(Pool& pool, c | Line 319 static void file_read_http(Pool& pool, c |
| if(vheaders=static_cast<Value *>(options->get(*http_headers_name))) { | if(vheaders=static_cast<Value *>(options->get(*http_headers_name))) { |
| valid_options++; | valid_options++; |
| } | } |
| if(Value *vany_status=static_cast<Value *>(options->get(*http_any_status_name))) { | |
| valid_options++; | |
| fail_on_status_ne_200=!vany_status->as_bool(); | |
| } | |
| if(valid_options!=options->size()) | if(valid_options!=options->size()) |
| throw Exception("parser.runtime", | throw Exception("parser.runtime", |
| 0, | 0, |
| Line 312 static void file_read_http(Pool& pool, c | Line 332 static void file_read_http(Pool& pool, c |
| //making request | //making request |
| String request(pool); | String request(pool); |
| request<< method <<" "<< uri <<" HTTP/1.0\nHost: "<< host<<"\n"; | request<< method <<" "<< uri <<" HTTP/1.0" CRLF "Host: "<< host<< CRLF; |
| bool user_agent_specified=false; | bool user_agent_specified=false; |
| if(vheaders && !vheaders->is_string()) { // allow empty | if(vheaders && !vheaders->is_string()) { // allow empty |
| if(Hash *headers=vheaders->get_hash(&connect_string)) { | if(Hash *headers=vheaders->get_hash(&connect_string)) { |
| Line 325 static void file_read_http(Pool& pool, c | Line 345 static void file_read_http(Pool& pool, c |
| "headers param must be hash"); | "headers param must be hash"); |
| }; | }; |
| if(!user_agent_specified) // defaulting | if(!user_agent_specified) // defaulting |
| request << "user-agent: " DEFAULT_USER_AGENT "\n"; | request << "user-agent: " DEFAULT_USER_AGENT CRLF; |
| request<<"\n"; | request<<CRLF; |
| //sending request | //sending request |
| String response(pool); | String response(pool); |
| http_request(response, | int status_code=http_request(response, |
| &connect_string, host, port, request.cstr(String::UL_UNSPECIFIED), timeout); | &connect_string, host, port, request.cstr(String::UL_UNSPECIFIED), |
| timeout, fail_on_status_ne_200); | |
| //processing results | //processing results |
| int pos=response.pos("\r\n\r\n", 4); | int pos=response.pos(CRLF CRLF, 4); |
| if(pos<1){ | if(pos<1){ |
| throw Exception("http.response", | throw Exception("http.response", |
| &connect_string, | &connect_string, |
| Line 345 static void file_read_http(Pool& pool, c | Line 366 static void file_read_http(Pool& pool, c |
| Array aheaders(pool); | Array aheaders(pool); |
| Hash& headers=*new(pool) Hash(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; | size_t pos_after_ref=0; |
| header_block.split(aheaders, &pos_after_ref, "\r\n", 2); | header_block.split(aheaders, &pos_after_ref, CRLF, 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 | //processing headers |
| for(int i=1;i<aheaders.size();i++) { | for(int i=1;i<aheaders.size();i++) { |
| if(const String *line=aheaders.get_string(i)) { | if(const String *line=aheaders.get_string(i)) { |
| Line 363 static void file_read_http(Pool& pool, c | Line 381 static void file_read_http(Pool& pool, c |
| throw Exception("http.response", | throw Exception("http.response", |
| &connect_string, | &connect_string, |
| "bad response from host - bad header \"%s\"", line->cstr()); | "bad response from host - bad header \"%s\"", line->cstr()); |
| headers.put( | const String& sname=line->mid(0, pos).change_case(pool, String::CC_UPPER); |
| line->mid(0, pos).change_case(pool, String::CC_UPPER), | const String& string=line->mid(pos+2, line->size()); |
| new(pool) VString(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 | } else |
| throw Exception("http.response", | throw Exception("http.response", |
| &connect_string, | &connect_string, |
| Line 375 static void file_read_http(Pool& pool, c | Line 416 static void file_read_http(Pool& pool, c |
| // output response | // output response |
| data=body.cstr(); data_size=body.size(); | data=body.cstr(); data_size=body.size(); |
| if(out_fields) | if(out_fields) { |
| headers.put(*file_status_name, new(pool) VInt(pool, status_code)); | |
| *out_fields=&headers; | *out_fields=&headers; |
| } | |
| } | } |
| #ifndef DOXYGEN | #ifndef DOXYGEN |
| Line 443 bool file_read(Pool& pool, const String& | Line 486 bool file_read(Pool& pool, const String& |
| return result; | 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, | bool file_read_action_under_lock(Pool& pool, const String& file_spec, |
| const char* action_name, File_read_action action, void *context, | const char* action_name, File_read_action action, void *context, |
| bool as_text, | bool as_text, |
| Line 475 bool file_read_action_under_lock(Pool& p | Line 535 bool file_read_action_under_lock(Pool& p |
| strerror(errno), errno, fname); | strerror(errno), errno, fname); |
| #ifdef PA_SAFE_MODE | #ifdef PA_SAFE_MODE |
| if(finfo.st_uid/*foreign?*/!=geteuid() | check_safe_mode(finfo, file_spec, fname); |
| && 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); | |
| #endif | #endif |
| action(pool, finfo, f, file_spec, fname, as_text, context); | action(pool, finfo, f, file_spec, fname, as_text, context); |
| Line 776 size_t stdout_write(const void *buf, siz | Line 831 size_t stdout_write(const void *buf, siz |
| return size; | return size; |
| #else | #else |
| return fwrite(buf, 1, size, stdout); | return write(1/*handleof(stdout)*/, buf, size); |
| #endif | #endif |
| } | } |
| Line 954 int pa_sleep(unsigned long secs, unsigne | Line 1009 int pa_sleep(unsigned long secs, unsigne |
| // attributed meaning | // 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) { | static size_t date_attribute(const VDate& vdate, char *buf, size_t buf_size) { |
| const char month_names[12][4]={ | const char month_names[12][4]={ |
| "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"}; | "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"}; |
| Line 966 static size_t date_attribute(const VDate | Line 1022 static size_t date_attribute(const VDate |
| throw Exception(0, | throw Exception(0, |
| 0, | 0, |
| "bad time in attribute value (seconds from epoch=%ld)", when); | "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], | days[tms->tm_wday], |
| tms->tm_mday,month_names[tms->tm_mon],tms->tm_year+1900, | tms->tm_mday,month_names[tms->tm_mon],tms->tm_year+1900, |
| tms->tm_hour,tms->tm_min,tms->tm_sec); | tms->tm_hour,tms->tm_min,tms->tm_sec); |