--- parser3/src/main/pa_common.C 2003/03/24 17:00:49 1.143.2.21.2.12 +++ parser3/src/main/pa_common.C 2003/04/01 16:16:23 1.143.2.21.2.19 @@ -5,14 +5,14 @@ Author: Alexandr Petrosian (http://paf.design.ru) */ -static const char* IDENT_COMMON_C="$Date: 2003/03/24 17:00:49 $"; +static const char* IDENT_COMMON_C="$Date: 2003/04/01 16:16:23 $"; #include "pa_common.h" #include "pa_exception.h" #include "pa_hash.h" #include "pa_globals.h" -//#define PA_HTTP +#define PA_HTTP #ifdef PA_HTTP #include "pa_vstring.h" @@ -61,6 +61,9 @@ int PASCAL closesocket(SOCKET); #endif // locking constants +//#define PA_DEBUG_NO_LOCKING + +#ifdef PA_DEBUG_NO_LOCKING #ifdef HAVE_FLOCK @@ -105,6 +108,14 @@ static int unlock(int fd) { FLOCK(F_TLOC #endif #endif +#else +static int lock_shared_blocking(int fd) { return 0; } +static int lock_exclusive_blocking(int fd) { return 0; } +static int lock_exclusive_nonblocking(int fd) { return 0; } +static int unlock(int fd) { return 0; } + +#endif + // defines for globals #define FILE_STATUS_NAME "status" @@ -187,7 +198,7 @@ static bool set_addr(struct sockaddr_in return true; } -static int http_read_responseString& response, int sock, bool fail_on_status_ne_200){ +static int http_read_response(String& response, int sock, bool fail_on_status_ne_200){ int result=0; size_t EOLat=0; while(true) { @@ -195,17 +206,18 @@ static int http_read_responseString& res ssize_t size=recv(sock, buf, MAX_STRING, 0); if(size<=0) break; - response.APPEND_TAINTED(buf, size, "remote HTTP server response", 0); + response.append_strdup(buf, size, String::L_TAINTED); if(!result && (EOLat=response.pos("\r\n", 2))!=STRING_NOT_FOUND) { // checking status in first response const String& status_line=response.mid(0, (size_t)EOLat); ArrayString astatus; - status_line->split(astatus, 0, " ", 1); - const String& status_code=astatus.get(1); - result=status_code->as_int(); + size_t pos_after=0; + status_line.split(astatus, pos_after, " "); + const String& status_code=*astatus.get(1); + result=status_code.as_int(); if(fail_on_status_ne_200 && result!=200) throw Exception("http.status", - status_code, + &status_code, "invalid HTTP response status"); } } @@ -231,15 +243,14 @@ static void timeout_handler(int sig){ #endif static int http_request( - String& response, - const String* origin_string, - const char* host, int port, - const char* request, - int timeout, - bool fail_on_status_ne_200) { + String& response, + const char* host, int port, + const char* request, + int timeout, + bool fail_on_status_ne_200) { if(!host) throw Exception("http.host", - origin_string, + 0, "zero hostname"); //never #ifdef WE_CAN_USE_ALARM @@ -251,7 +262,7 @@ static int http_request( #ifdef WE_CAN_USE_ALARM if(sigsetjmp(timeout_env, 1)) throw Exception("http.timeout", - origin_string, + 0, "timeout occured while retrieving document"); else { alarm(timeout); @@ -260,21 +271,21 @@ static int http_request( if(!set_addr(&dest, host, port)) throw Exception("http.host", - origin_string, + 0, "can not resolve hostname \"%s\"", host); if((sock=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP/*0*/))<0) throw Exception("http.connect", - origin_string, + 0, "can not make socket: %s (%d)", strerror(errno), errno); if(connect(sock, (struct sockaddr *)&dest, sizeof(dest))) throw Exception("http.connect", - origin_string, + 0, "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, + 0, "error sending request: %s (%d)", strerror(errno), errno); result=http_read_response(response, sock, fail_on_status_ne_200); @@ -296,47 +307,46 @@ static int http_request( #ifndef DOXYGEN struct Http_pass_header_info { - ; Charset* charset; String* request; bool user_agent_specified; }; #endif static void http_pass_header(HashStringValue::key_type key, HashStringValue::value_type value, - Http_pass_header_info *info) { - *(info->request)<<*key<<": " - << *attributed_meaning_to_string(*info->value, String::L_HTTP_HEADER, false) - <<"\n"; + Http_pass_header_info *info) { + *info->request <change_case(*info->*info->charset, String::CC_UPPER)=="USER-AGENT") + if(String(key, String::L_TAINTED).change_case(*info->charset, String::CC_UPPER)=="USER-AGENT") info->user_agent_specified=true; } #ifndef DOXYGEN struct File_read_http_result { - char *data; size_t size; + char *str; size_t length; HashStringValue* headers; }; #endif -static File_read_http_result file_read_httpCharset& charset, const String& file_spec, - HashStringValue *options=0) { +static File_read_http_result file_read_http(Charset& charset, const String& file_spec, + HashStringValue *options=0) { File_read_http_result result; char host[MAX_STRING]; const char* uri; int port; - const char* method(0); + const char* method=0; int timeout=2; bool fail_on_status_ne_200=true; - Value* vheaders(0); + Value* vheaders=0; - const String& connect_string(new String()); + String& connect_string=*new String; // not in ^sql{... L_SQL ...} spirit, but closer to ^file::load one - connect_string->append(*file_spec, String::L_URI); // tainted pieces -> URI pieces + connect_string.append(file_spec, String::L_URI); // tainted pieces -> URI pieces - const char* connect_string_cstr=connect_string->cstr(String::L_UNSPECIFIED); + const char* connect_string_cstr=connect_string.cstr(String::L_UNSPECIFIED); const char* current=connect_string_cstr; if(strncmp(current, "http://", 7)!=0) throw Exception(0, - connect_string, + &connect_string, "does not start with http://"); //never current+=7; @@ -380,13 +390,13 @@ static File_read_http_result file_read_h request<< " "<< uri <<" HTTP/1.0\nHost: "<< host<<"\n"; bool user_agent_specified=false; if(vheaders && !vheaders->is_string()) { // allow empty - if(HashStringValue *headers=vheaders->get_hash(connect_string)) { - Http_pass_header_info info={&&charset, &request}; + if(HashStringValue *headers=vheaders->get_hash()) { + Http_pass_header_info info={&charset, &request}; headers->for_each(http_pass_header, &info); user_agent_specified=info.user_agent_specified; } else throw Exception("parser.runtime", - connect_string, + &connect_string, "headers param must be hash"); }; if(!user_agent_specified) // defaulting @@ -397,41 +407,43 @@ static File_read_http_result file_read_h String response; const char* request_cstr=request.cstr(String::L_UNSPECIFIED); int status_code=http_request(response, - connect_string, host, port, request_cstr, + host, port, request_cstr, timeout, fail_on_status_ne_200); //processing results size_t pos=response.pos("\r\n\r\n", 4); if(pos==STRING_NOT_FOUND || pos<1){ throw Exception("http.response", - connect_string, + &connect_string, "bad response from host - no headers found"); } const String& header_block=response.mid(0, pos); const String& body=response.mid(pos+4, response.length()); ArrayString aheaders; - result.headers=HashStringValue* (new HashStringValue); - header_block->split(aheaders, 0, "\r\n", 2); + result.headers=new HashStringValue; + size_t pos_after=0; + header_block.split(aheaders, pos_after, "\r\n"); //processing headers - for(int i=1; ipos(": ", 2); + size_t aheaders_count=aheaders.count(); + for(size_t i=1; icstr().get()); + &connect_string, + "bad response from host - bad header \"%s\"", line.cstr()); result.headers->put( - line->mid(0, pos)->change_case(charset, String::CC_UPPER), - Value*(new VString(line->mid(pos+2, line->length())))); + line.mid(0, pos).change_case(charset, String::CC_UPPER), + new VString(line.mid(pos+2, line.length()))); } // output response - result.str=new(PointerFreeGC) char[1/*as text puts terminating zero later*/+(result.length=body->length())]; - body->store_to(result.str, String::L_AS_IS); - result.headers->put(file_status_name, Value*(new VInt(status_code))); + result.str=body.cstrm(); + result.length=body.length(); + result.headers->put(file_status_name, new VInt(status_code)); return result; } @@ -472,13 +484,13 @@ File_read_result file_read(Charset& char bool fail_on_read_problem) { File_read_result result={false}; #ifdef PA_HTTP - if(file_spec.starts_with("http://", 7)) { + if(file_spec.starts_with("http://")) { // fail on read problem - File_read_http_result http=file_read_http(charset, file_spec, params); + File_read_http_result http;//=file_read_http(charset, file_spec, params); result.success=true; - result.str=http.data; - result.length=http.size; - result.headers=http.headers; + result.str="HELLO";//http.str; + result.length=5;//http.length; + result.headers=new HashStringValue;//http.headers; } else { #endif File_read_action_info info={&result.str, &result.length}; @@ -570,7 +582,7 @@ bool file_read_action_under_lock(const S static 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) { + while((pos_before=file_spec.pos('/', pos_after))!=STRING_NOT_FOUND) { mkdir(file_spec.mid(0, pos_before).cstr(String::L_FILE_SPEC), 0775); pos_after=pos_before+1; } @@ -659,7 +671,7 @@ void file_write( // throws nothing! [this is required in file_move & file_delete] static void rmdir(const String& file_spec, size_t pos_after) { size_t pos_before; - if((pos_before=file_spec.pos("/", pos_after))!=STRING_NOT_FOUND) + if((pos_before=file_spec.pos('/', pos_after))!=STRING_NOT_FOUND) rmdir(file_spec, pos_before+1); rmdir(file_spec.mid(0, pos_after-1/* / */).cstr(String::L_FILE_SPEC)); @@ -749,7 +761,7 @@ bool file_stat(const String& file_spec, time_t& rctime, bool fail_on_read_problem) { const char* fname=file_spec.cstr(String::L_FILE_SPEC); - struct stat finfo; + struct stat finfo; if(stat(fname, &finfo)!=0) if(fail_on_read_problem) throw Exception("file.missing",