|
|
| version 1.143.2.6, 2003/01/27 17:17:06 | version 1.143.2.10, 2003/01/31 12:34:36 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: commonly functions. | Parser: commonly functions. |
| Copyright(c) 2001, 2003 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 9 static const char* IDENT_COMMON_C="$Date | Line 9 static const char* IDENT_COMMON_C="$Date |
| #include "pa_common.h" | #include "pa_common.h" |
| #include "pa_exception.h" | #include "pa_exception.h" |
| #include "pa_globals.h" | #include "pa_value_includes.h" |
| #include "pa_hash.h" | #include "pa_hash.h" |
| #include "pa_vstring.h" | #include "pa_vstring.h" |
| #include "pa_vint.h" | #include "pa_vint.h" |
| Line 116 void fix_line_breaks(char* buf, size_t& | Line 116 void fix_line_breaks(char* buf, size_t& |
| } | } |
| char* file_read_text(Pool& pool, Charset& charset, | char* file_read_text(Pool& pool, Charset& charset, |
| ConstStringPtr file_spec, | StringPtr file_spec, |
| bool fail_on_read_problem, | bool fail_on_read_problem, |
| HashStringValue *params, HashStringValue** out_fields) { | HashStringValue *params, HashStringValue** out_fields) { |
| char *result; size_t size; | char *result; size_t size; |
| Line 151 static int http_read_response(Pool& pool | Line 151 static int http_read_response(Pool& pool |
| break; | break; |
| response.APPEND_TAINTED(buf, size, "remote HTTP server response", 0); | response.APPEND_TAINTED(buf, size, "remote HTTP server response", 0); |
| if(!result && (EOLat=response.pos("\r\n", 2))>=0) { // checking status in first response | if(!result && (EOLat=response.pos("\r\n", 2))>=0) { // checking status in first response |
| ConstStringPtr status_line=response.mid(0, (size_t)EOLat); | StringPtr status_line=response.mid(0, (size_t)EOLat); |
| Array<ConstStringPtr> astatus; | Array<StringPtr> astatus; |
| status_line->split(astatus, 0, " ", 1); | status_line->split(astatus, 0, " ", 1); |
| ConstStringPtr status_code=astatus.get(1); | StringPtr status_code=astatus.get(1); |
| 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 186 static void timeout_handler(int sig){ | Line 186 static void timeout_handler(int sig){ |
| static int http_request(Pool& pool, | static int http_request(Pool& pool, |
| String& response, | String& response, |
| ConstStringPtr origin_string, | StringPtr origin_string, |
| const char* host, int port, | const char* host, int port, |
| const char* request, | const char* request, |
| int timeout, | int timeout, |
| Line 259 struct Http_pass_header_info { | Line 259 struct Http_pass_header_info { |
| static void http_pass_header(HashStringValue::key_type key, HashStringValue::value_type value, | static void http_pass_header(HashStringValue::key_type key, HashStringValue::value_type value, |
| Http_pass_header_info *info) { | Http_pass_header_info *info) { |
| *(info->request)<<*key<<": " | *(info->request)<<*key<<": " |
| << attributed_meaning_to_string(value, String::UL_HTTP_HEADER, false) | << *attributed_meaning_to_string(*info->pool, value, String::UL_HTTP_HEADER, false) |
| <<"\n"; | <<"\n"; |
| if(*key->change_case(*info->pool, *info->charset, String::CC_UPPER)=="USER-AGENT") | if(*key->change_case(*info->pool, *info->charset, String::CC_UPPER)=="USER-AGENT") |
| info->user_agent_specified=true; | info->user_agent_specified=true; |
| } | } |
| static HashStringValuePtr file_read_http(Pool& pool, Charset& charset, ConstStringPtr file_spec, | static HashStringValuePtr file_read_http(Pool& pool, Charset& charset, StringPtr file_spec, |
| char*& data, size_t& data_size, | char*& data, size_t& data_size, |
| HashStringValue *options=0, bool return_outfields=false) { | HashStringValue *options=0, bool return_outfields=false) { |
| HashStringValuePtr result(0); | HashStringValuePtr result(0); |
| Line 282 static HashStringValuePtr file_read_http | Line 282 static HashStringValuePtr file_read_http |
| connect_string->append(*file_spec, String::UL_URI); // tainted pieces -> URI pieces | connect_string->append(*file_spec, String::UL_URI); // tainted pieces -> URI pieces |
| CharPtr connect_string_cstr=connect_string->cstr(String::UL_UNSPECIFIED); | CharPtr connect_string_cstr=connect_string->cstr(String::UL_UNSPECIFIED); |
| const char *current=connect_string_cstr; | const char* current=connect_string_cstr; |
| if(strncmp(current, "http://", 7)!=0) | if(strncmp(current, "http://", 7)!=0) |
| throw Exception(0, | throw Exception(0, |
| connect_string, | connect_string, |
| Line 300 static HashStringValuePtr file_read_http | Line 300 static HashStringValuePtr file_read_http |
| int valid_options=0; | int valid_options=0; |
| if(ValuePtr vmethod=options->get(http_method_name)) { | if(ValuePtr vmethod=options->get(http_method_name)) { |
| valid_options++; | valid_options++; |
| method=vmethod->as_string(pool)->cstr(); | method=vmethod->as_string(&pool)->cstr(); |
| } | } |
| if(ValuePtr vtimeout=options->get(http_timeout_name)) { | if(ValuePtr vtimeout=options->get(http_timeout_name)) { |
| valid_options++; | valid_options++; |
| Line 329 static HashStringValuePtr file_read_http | Line 329 static HashStringValuePtr file_read_http |
| request<< " "<< uri <<" HTTP/1.0\nHost: "<< host<<"\n"; | request<< " "<< uri <<" HTTP/1.0\nHost: "<< host<<"\n"; |
| 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(HashStringValuePtr headers=vheaders->get_hash(connect_string)) { | if(HashStringValue *headers=vheaders->get_hash(connect_string)) { |
| Http_pass_header_info info={&pool, &charset, &request}; | Http_pass_header_info info={&pool, &charset, &request}; |
| headers->for_each(http_pass_header, &info); | headers->for_each(http_pass_header, &info); |
| user_agent_specified=info.user_agent_specified; | user_agent_specified=info.user_agent_specified; |
| Line 359 static HashStringValuePtr file_read_http | Line 359 static HashStringValuePtr file_read_http |
| StringPtr header_block=response.mid(0, pos); | StringPtr header_block=response.mid(0, pos); |
| StringPtr body=response.mid(pos+4, response.size()); | StringPtr body=response.mid(pos+4, response.size()); |
| Array<ConstStringPtr> aheaders; | Array<StringPtr> aheaders; |
| HashStringValuePtr headers(new HashStringValue); | HashStringValuePtr headers(new HashStringValue); |
| header_block->split(aheaders, 0, "\r\n", 2); | header_block->split(aheaders, 0, "\r\n", 2); |
| //processing headers | //processing headers |
| for(int i=1; i<aheaders.count(); i++) { | for(int i=1; i<aheaders.count(); i++) { |
| ConstStringPtr line=aheaders.get(i); | StringPtr line=aheaders.get(i); |
| pos=line->pos(": ", 2); | pos=line->pos(": ", 2); |
| if(pos<1) | if(pos<1) |
| throw Exception("http.response", | throw Exception("http.response", |
| Line 395 struct File_read_action_info { | Line 395 struct File_read_action_info { |
| static void file_read_action(Pool& pool, | static void file_read_action(Pool& pool, |
| struct stat& finfo, | struct stat& finfo, |
| int f, | int f, |
| ConstStringPtr file_spec, const char* fname, bool as_text, | StringPtr 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) { | if(size_t to_read_size=(size_t)finfo.st_size) { |
| Line 417 static void file_read_action(Pool& pool, | Line 417 static void file_read_action(Pool& pool, |
| return; | return; |
| } | } |
| } | } |
| File_read_result file_read(Pool& pool, Charset& charset, ConstStringPtr file_spec, | File_read_result file_read(Pool& pool, Charset& charset, StringPtr file_spec, |
| char*& data, size_t& data_size, | char*& data, size_t& data_size, |
| bool as_text, HashStringValue *params, HashStringValue** out_fields, | bool as_text, HashStringValue *params, HashStringValue** out_fields, |
| bool fail_on_read_problem) { | bool fail_on_read_problem) { |
| Line 451 File_read_result file_read(Pool& pool, C | Line 451 File_read_result file_read(Pool& pool, C |
| return result; | return result; |
| } | } |
| bool file_read_action_under_lock(Pool& pool, ConstStringPtr file_spec, | bool file_read_action_under_lock(Pool& pool, StringPtr 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, |
| bool fail_on_read_problem) { | bool fail_on_read_problem) { |
| Line 511 bool file_read_action_under_lock(Pool& p | Line 511 bool file_read_action_under_lock(Pool& p |
| } | } |
| } | } |
| static void create_dir_for_file(ConstStringPtr file_spec) { | static void create_dir_for_file(StringPtr file_spec) { |
| size_t pos_after=1; | size_t pos_after=1; |
| int pos_before; | int pos_before; |
| while((pos_before=file_spec->pos("/", 1, pos_after))>=0) { | while((pos_before=file_spec->pos("/", 1, pos_after))>=0) { |
| Line 521 static void create_dir_for_file(ConstStr | Line 521 static void create_dir_for_file(ConstStr |
| } | } |
| bool file_write_action_under_lock( | bool file_write_action_under_lock( |
| ConstStringPtr file_spec, | StringPtr file_spec, |
| const char* action_name, File_write_action action, void *context, | const char* action_name, File_write_action action, void *context, |
| bool as_text, | bool as_text, |
| bool do_append, | bool do_append, |
| Line 588 static void file_write_action(int f, voi | Line 588 static void file_write_action(int f, voi |
| } | } |
| } | } |
| void file_write( | void file_write( |
| ConstStringPtr file_spec, | StringPtr file_spec, |
| const void *data, size_t size, | const void *data, size_t size, |
| bool as_text, | bool as_text, |
| bool do_append) { | bool do_append) { |
| Line 601 void file_write( | Line 601 void file_write( |
| } | } |
| // throws nothing! [this is required in file_move & file_delete] | // throws nothing! [this is required in file_move & file_delete] |
| static void rmdir(ConstStringPtr file_spec, size_t pos_after) { | static void rmdir(StringPtr file_spec, size_t pos_after) { |
| int pos_before; | int pos_before; |
| if((pos_before=file_spec->pos("/", 1, pos_after))>=0) | if((pos_before=file_spec->pos("/", 1, pos_after))>=0) |
| rmdir(file_spec, pos_before+1); | rmdir(file_spec, pos_before+1); |
| rmdir(file_spec->mid(0, pos_after-1/* / */)->cstr(String::UL_FILE_SPEC)); | rmdir(file_spec->mid(0, pos_after-1/* / */)->cstr(String::UL_FILE_SPEC)); |
| } | } |
| bool file_delete(ConstStringPtr file_spec, bool fail_on_read_problem) { | bool file_delete(StringPtr file_spec, bool fail_on_read_problem) { |
| CharPtr fname=file_spec->cstr(String::UL_FILE_SPEC); | CharPtr fname=file_spec->cstr(String::UL_FILE_SPEC); |
| if(unlink(fname)!=0) | if(unlink(fname)!=0) |
| if(fail_on_read_problem) | if(fail_on_read_problem) |
| Line 622 bool file_delete(ConstStringPtr file_spe | Line 622 bool file_delete(ConstStringPtr file_spe |
| rmdir(file_spec, 1); | rmdir(file_spec, 1); |
| return true; | return true; |
| } | } |
| void file_move(ConstStringPtr old_spec, ConstStringPtr new_spec) { | void file_move(StringPtr old_spec, StringPtr new_spec) { |
| CharPtr old_spec_cstr=old_spec->cstr(String::UL_FILE_SPEC); | CharPtr old_spec_cstr=old_spec->cstr(String::UL_FILE_SPEC); |
| CharPtr new_spec_cstr=new_spec->cstr(String::UL_FILE_SPEC); | CharPtr new_spec_cstr=new_spec->cstr(String::UL_FILE_SPEC); |
| Line 646 bool entry_exists(const char* fname, str | Line 646 bool entry_exists(const char* fname, str |
| return result; | return result; |
| } | } |
| bool entry_exists(ConstStringPtr file_spec) { | bool entry_exists(StringPtr file_spec) { |
| CharPtr fname=file_spec->cstr(String::UL_FILE_SPEC); | CharPtr fname=file_spec->cstr(String::UL_FILE_SPEC); |
| return entry_exists(fname, 0); | return entry_exists(fname, 0); |
| } | } |
| static bool entry_readable(ConstStringPtr file_spec, bool need_dir) { | static bool entry_readable(StringPtr file_spec, bool need_dir) { |
| CharPtr fname=file_spec->cstr(String::UL_FILE_SPEC); | CharPtr fname=file_spec->cstr(String::UL_FILE_SPEC); |
| if(need_dir) { | if(need_dir) { |
| size_t size=strlen(fname); | size_t size=strlen(fname); |
| Line 670 static bool entry_readable(ConstStringPt | Line 670 static bool entry_readable(ConstStringPt |
| } | } |
| return false; | return false; |
| } | } |
| bool file_readable(ConstStringPtr file_spec) { | bool file_readable(StringPtr file_spec) { |
| return entry_readable(file_spec, false); | return entry_readable(file_spec, false); |
| } | } |
| bool dir_readable(ConstStringPtr file_spec) { | bool dir_readable(StringPtr file_spec) { |
| return entry_readable(file_spec, true); | return entry_readable(file_spec, true); |
| } | } |
| ConstStringPtr file_readable(ConstStringPtr path, const String& name) { | StringPtr file_readable(StringPtr path, const String& name) { |
| StringPtr result(new String(*path)); | StringPtr result(new String(*path)); |
| *result << "/"; | *result << "/"; |
| *result << name; | *result << name; |
| return file_readable(result)?result:ConstStringPtrZero; | return file_readable(result)?result:StringPtr(0); |
| } | } |
| bool file_executable(ConstStringPtr file_spec) { | bool file_executable(StringPtr file_spec) { |
| return access(file_spec->cstr(String::UL_FILE_SPEC), X_OK)==0; | return access(file_spec->cstr(String::UL_FILE_SPEC), X_OK)==0; |
| } | } |
| bool file_stat(ConstStringPtr file_spec, | bool file_stat(StringPtr file_spec, |
| size_t& rsize, | size_t& rsize, |
| time_t& ratime, | time_t& ratime, |
| time_t& rmtime, | time_t& rmtime, |
| Line 751 char* rsplit(char* string, char delim) { | Line 751 char* rsplit(char* string, char delim) { |
| } | } |
| /// @todo less stupid type detection | /// @todo less stupid type detection |
| const char *format(Pool& pool, double value, char* fmt) { | const char* format(Pool& pool, double value, char* fmt) { |
| char local_buf[MAX_NUMBER]; | char local_buf[MAX_NUMBER]; |
| size_t size; | size_t size; |
| Line 766 const char *format(Pool& pool, double va | Line 766 const char *format(Pool& pool, double va |
| else | else |
| size=snprintf(local_buf, sizeof(local_buf), "%d", (int)value); | size=snprintf(local_buf, sizeof(local_buf), "%d", (int)value); |
| return pool.malloc(local_buf, size+1); | return pool.copy(local_buf, size+1); |
| } | } |
| size_t stdout_write(const void *buf, size_t size) { | size_t stdout_write(const void *buf, size_t size) { |