--- parser3/src/main/pa_http.C 2008/02/22 17:29:38 1.11 +++ parser3/src/main/pa_http.C 2008/06/06 17:25:44 1.13 @@ -5,7 +5,7 @@ Author: Alexandr Petrosian (http://paf.design.ru) */ -static const char * const IDENT_HTTP_C="$Date: 2008/02/22 17:29:38 $"; +static const char * const IDENT_HTTP_C="$Date: 2008/06/06 17:25:44 $"; #include "pa_http.h" #include "pa_common.h" @@ -21,8 +21,11 @@ static const char * const IDENT_HTTP_C=" #define HTTP_HEADERS_NAME "headers" #define HTTP_COOKIES_NAME "cookies" #define HTTP_ANY_STATUS_NAME "any-status" -// #define HTTP_CHARSET_NAME "charset" +#define HTTP_OMIT_POST_CHARSET "omit-post-charset" // ^file::load[...;http://...;$.form[...]$.method[post]] + // by default add charset to content-type + #define HTTP_TABLES_NAME "tables" + #define HTTP_USER "user" #define HTTP_PASSWORD "password" @@ -284,6 +287,7 @@ struct Http_pass_header_info { Request_charsets* charsets; String* request; bool user_agent_specified; + bool content_type_specified; }; #endif static void http_pass_header(HashStringValue::key_type name, @@ -296,8 +300,11 @@ static void http_pass_header(HashStringV << attributed_meaning_to_string(*value, String::L_URI, false) << CRLF; - if(aname.change_case(info->charsets->source(), String::CC_UPPER)=="USER-AGENT") + const String::Body name_upper=aname.change_case(info->charsets->source(), String::CC_UPPER); + if(name_upper==HTTP_USER_AGENT) info->user_agent_specified=true; + if(name_upper==HTTP_CONTENT_TYPE) + info->content_type_specified=true; } static void http_pass_cookie(HashStringValue::key_type name, @@ -310,38 +317,6 @@ static void http_pass_cookie(HashStringV } -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; @@ -431,6 +406,7 @@ File_read_http_result pa_internal_file_r const char* body_cstr=0; int timeout_secs=2; bool fail_on_status_ne_200=true; + bool omit_post_charset=false; Value* vheaders=0; Value* vcookies=0; Value* vbody=0; @@ -465,7 +441,11 @@ File_read_http_result pa_internal_file_r if(Value* vany_status=options->get(HTTP_ANY_STATUS_NAME)) { valid_options++; fail_on_status_ne_200=!vany_status->as_bool(); - } + } + if(Value* vomit_post_charset=options->get(HTTP_OMIT_POST_CHARSET)){ + valid_options++; + omit_post_charset=vomit_post_charset->as_bool(); + } if(Value* vcharset_name=options->get(PA_CHARSET_NAME)) { asked_remote_charset=&::charsets.get(vcharset_name->as_string(). change_case(charsets.source(), String::CC_UPPER)); @@ -536,8 +516,11 @@ File_read_http_result pa_internal_file_r head <<" HTTP/1.0" CRLF "host: "<< host << CRLF; - if(form && !method_is_get) { - head << "content-type: application/x-www-form-urlencoded" CRLF; + if(form && !method_is_get) { // POST + head << "content-type: " << HTTP_CONTENT_TYPE_FORM_URLENCODED; + if(!omit_post_charset) + head << "; charset=" << asked_remote_charset->NAME_CSTR() << ";"; + head << CRLF; body_cstr=pa_form2string(*form, charsets); } else if (vbody) { body_cstr=vbody->as_string().cstr(String::L_UNSPECIFIED, 0, &charsets); @@ -554,11 +537,13 @@ File_read_http_result pa_internal_file_r head<<"authorization: "<<*authorization_field_value<is_string()) { // allow empty if(HashStringValue *headers=vheaders->get_hash()) { Http_pass_header_info info={&charsets, &head, false}; headers->for_each(http_pass_header, &info); user_agent_specified=info.user_agent_specified; + content_type_specified=info.content_type_specified; } else throw Exception(PARSER_RUNTIME, &connect_string, @@ -567,6 +552,11 @@ File_read_http_result pa_internal_file_r if(!user_agent_specified) // defaulting head << "user-agent: " DEFAULT_USER_AGENT CRLF; + if(form && !method_is_get && content_type_specified) // POST + form + content-type was specified + throw Exception(PARSER_RUNTIME, + &connect_string, + "$.content-type can't be specified with method POST"); + if(vcookies && !vcookies->is_string()){ // allow empty if(HashStringValue* cookies=vcookies->get_hash()) { head << "cookie: "; @@ -635,8 +625,8 @@ File_read_http_result pa_internal_file_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=="CONTENT-TYPE") - real_remote_charset=detect_charset(charsets.source(), header_value); + if(as_text && HEADER_NAME==HTTP_CONTENT_TYPE) + real_remote_charset=detect_charset(charsets, header_value); // tables {