--- parser3/src/main/pa_http.C 2008/06/06 13:04:45 1.12 +++ parser3/src/main/pa_http.C 2008/09/04 09:37:48 1.18 @@ -5,7 +5,7 @@ Author: Alexandr Petrosian (http://paf.design.ru) */ -static const char * const IDENT_HTTP_C="$Date: 2008/06/06 13:04:45 $"; +static const char * const IDENT_HTTP_C="$Date: 2008/09/04 09:37:48 $"; #include "pa_http.h" #include "pa_common.h" @@ -311,44 +311,12 @@ static void http_pass_cookie(HashStringV HashStringValue::value_type value, Http_pass_header_info *info) { - *info->request << String(name, String::L_HTTP_HEADER) << "=" - << attributed_meaning_to_string(*value, String::L_HTTP_HEADER, false) + *info->request << String(name, String::L_HTTP_COOKIE) << "=" + << attributed_meaning_to_string(*value, String::L_HTTP_COOKIE, false) << "; "; } -static Charset* detect_charset(Charset& source_charset, const String& content_type_value) { - const String::Body CONTENT_TYPE_VALUE= - content_type_value.change_case(source_charset, String::CC_UPPER); - // content-type: xxx/xxx; source_charset=WE-NEED-THIS - // content-type: xxx/xxx; source_charset="WE-NEED-THIS" - // content-type: xxx/xxx; source_charset="WE-NEED-THIS"; - size_t before_charseteq_pos=CONTENT_TYPE_VALUE.pos("CHARSET="); - if(before_charseteq_pos!=STRING_NOT_FOUND) { - size_t charset_begin=before_charseteq_pos+8/*CHARSET=*/; - size_t open_quote_pos=CONTENT_TYPE_VALUE.pos('"', charset_begin); - bool quoted=open_quote_pos==charset_begin; - if(quoted) - charset_begin++; // skip opening '"' - size_t charset_end=CONTENT_TYPE_VALUE.length(); - if(quoted) { - size_t close_quote_pos=CONTENT_TYPE_VALUE.pos('"', charset_begin); - if(close_quote_pos!=STRING_NOT_FOUND) - charset_end=close_quote_pos; - } else { - size_t delim_pos=CONTENT_TYPE_VALUE.pos(';', charset_begin); - if(delim_pos!=STRING_NOT_FOUND) - charset_end=delim_pos; - } - const String::Body CHARSET_NAME_BODY= - CONTENT_TYPE_VALUE.mid(charset_begin, charset_end - charset_begin); - - return &charsets.get(CHARSET_NAME_BODY); - } - - return 0; -} - static const String* basic_authorization_field(const char* user, const char* pass) { if(!user&& !pass) return 0; @@ -396,7 +364,7 @@ static void form_value2string( Form_table_value2string_info info(key, *result); tvalue->for_each(form_table_value2string, &info); } else - throw Exception(0, + throw Exception(PARSER_RUNTIME, new String(key, String::L_TAINTED), "is %s, "HTTP_FORM_NAME" option value must either string or table", value->type()); } @@ -428,7 +396,8 @@ static void find_headers_end(char* p, File_read_http_result pa_internal_file_read_http(Request_charsets& charsets, const String& file_spec, bool as_text, - HashStringValue *options) { + HashStringValue *options, + bool transcode_text_result) { File_read_http_result result; char host[MAX_STRING]; const char* uri; @@ -526,7 +495,7 @@ File_read_http_result pa_internal_file_r const char* current=connect_string_cstr; if(strncmp(current, "http://", 7)!=0) - throw Exception(0, + throw Exception(PARSER_RUNTIME, &connect_string, "does not start with http://"); //never current+=7; @@ -654,11 +623,10 @@ File_read_http_result pa_internal_file_r throw Exception("http.response", &connect_string, "bad response from host - bad header \"%s\"", line.cstr()); - const String::Body HEADER_NAME= - line.mid(0, pos).change_case(charsets.source(), String::CC_UPPER); - const String& header_value=line.mid(pos+1, line.length()).trim(String::TRIM_BOTH, " \t\r"); + const String::Body HEADER_NAME=line.mid(0, pos).change_case(charsets.source(), String::CC_UPPER); + const String& HEADER_VALUE=line.mid(pos+1, line.length()).trim(String::TRIM_BOTH, " \t\r"); if(as_text && HEADER_NAME==HTTP_CONTENT_TYPE) - real_remote_charset=detect_charset(charsets.source(), header_value); + real_remote_charset=detect_charset(charsets.source(), HEADER_VALUE); // tables { @@ -670,34 +638,45 @@ File_read_http_result pa_internal_file_r table=valready->get_table(); } else { // first appearence - Table::columns_type columns =new ArrayString(1); + Table::columns_type columns=new ArrayString(1); *columns+=new String("value"); table=new Table(columns); } // this string becomes next row ArrayString& row=*new ArrayString(1); - row+=&header_value; + row+=&HEADER_VALUE; *table+=&row; // not existed before? add it if(!existed) tables.put(HEADER_NAME, new VTable(table)); } - result.headers->put(HEADER_NAME, new VString(header_value)); + result.headers->put(HEADER_NAME, new VString(HEADER_VALUE)); } } + if(as_text && raw_body_size>=3 && strncmp(raw_body, "\xEF\xBB\xBF", 3)==0){ + // skip UTF-8 signature: EF BB BF (BOM code) + raw_body+=3; + raw_body_size-=3; + } + // output response 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 && transcode_text_result && raw_body_size) { // 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()); + } result.str=const_cast(real_body.str); // hacking a little result.length=real_body.length; + result.headers->put(file_status_name, new VInt(status_code)); + return result; }