--- parser3/src/main/pa_http.C 2005/11/28 11:32:47 1.2 +++ parser3/src/main/pa_http.C 2008/02/22 17:29:38 1.11 @@ -5,7 +5,7 @@ Author: Alexandr Petrosian (http://paf.design.ru) */ -static const char * const IDENT_HTTP_C="$Date: 2005/11/28 11:32:47 $"; +static const char * const IDENT_HTTP_C="$Date: 2008/02/22 17:29:38 $"; #include "pa_http.h" #include "pa_common.h" @@ -19,8 +19,9 @@ static const char * const IDENT_HTTP_C=" #define HTTP_BODY_NAME "body" #define HTTP_TIMEOUT_NAME "timeout" #define HTTP_HEADERS_NAME "headers" +#define HTTP_COOKIES_NAME "cookies" #define HTTP_ANY_STATUS_NAME "any-status" -#define HTTP_CHARSET_NAME "charset" +// #define HTTP_CHARSET_NAME "charset" #define HTTP_TABLES_NAME "tables" #define HTTP_USER "user" #define HTTP_PASSWORD "password" @@ -285,17 +286,29 @@ struct Http_pass_header_info { bool user_agent_specified; }; #endif -static void http_pass_header(HashStringValue::key_type key, +static void http_pass_header(HashStringValue::key_type name, HashStringValue::value_type value, Http_pass_header_info *info) { - *info->request <request <charsets->source(), String::CC_UPPER)=="USER-AGENT") - info->user_agent_specified=true; + if(aname.change_case(info->charsets->source(), String::CC_UPPER)=="USER-AGENT") + info->user_agent_specified=true; } +static void http_pass_cookie(HashStringValue::key_type name, + 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) + << "; "; + +} static Charset* detect_charset(Charset& source_charset, const String& content_type_value) { const String::Body CONTENT_TYPE_VALUE= @@ -305,7 +318,7 @@ static Charset* detect_charset(Charset& // 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 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) @@ -321,7 +334,7 @@ static Charset* detect_charset(Charset& charset_end=delim_pos; } const String::Body CHARSET_NAME_BODY= - CONTENT_TYPE_VALUE.mid(charset_begin, charset_end); + CONTENT_TYPE_VALUE.mid(charset_begin, charset_end - charset_begin); return &charsets.get(CHARSET_NAME_BODY); } @@ -380,10 +393,10 @@ static void form_value2string( new String(key, String::L_TAINTED), "is %s, "HTTP_FORM_NAME" option value must either string or table", value->type()); } -const char* pa_form2string(HashStringValue& form) { +const char* pa_form2string(HashStringValue& form, Request_charsets& charsets) { String string; - form.for_each(form_value2string, &string); - return string.cstr(String::L_UNSPECIFIED); + form.for_each(form_value2string, &string); + return string.cstr(String::L_UNSPECIFIED, 0, &charsets); } static void find_headers_end(char* p, char*& headers_end_at, @@ -413,12 +426,14 @@ File_read_http_result pa_internal_file_r char host[MAX_STRING]; const char* uri; short port; - const char* method="GET"; bool method_is_get; + const char* method="GET"; HashStringValue* form=0; const char* body_cstr=0; int timeout_secs=2; bool fail_on_status_ne_200=true; Value* vheaders=0; + Value* vcookies=0; + Value* vbody=0; Charset *asked_remote_charset=0; const char* user_cstr=0; const char* password_cstr=0; @@ -434,23 +449,24 @@ File_read_http_result pa_internal_file_r valid_options++; form=vform->get_hash(); } - if(Value* vbody=options->get(HTTP_BODY_NAME)) { + if(vbody=options->get(HTTP_BODY_NAME)) { valid_options++; - body_cstr=vbody->as_string().cstr(String::L_UNSPECIFIED); } if(Value* vtimeout=options->get(HTTP_TIMEOUT_NAME)) { valid_options++; timeout_secs=vtimeout->as_int(); } - if((vheaders=options->get(HTTP_HEADERS_NAME))) { + if(vheaders=options->get(HTTP_HEADERS_NAME)) { + valid_options++; + } + if(vcookies=options->get(HTTP_COOKIES_NAME)) { valid_options++; } if(Value* vany_status=options->get(HTTP_ANY_STATUS_NAME)) { valid_options++; fail_on_status_ne_200=!vany_status->as_bool(); } - if(Value* vcharset_name=options->get(HTTP_CHARSET_NAME)) { - valid_options++; + 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)); } @@ -464,18 +480,25 @@ File_read_http_result pa_internal_file_r } if(valid_options!=options->count()) - throw Exception("parser.runtime", + throw Exception(PARSER_RUNTIME, 0, "invalid option passed"); } if(!asked_remote_charset) // defaulting to $request:charset asked_remote_charset=&charsets.source(); - method_is_get=strcmp(method, "GET")==0; - if(method_is_get && body_cstr) - throw Exception("parser.runtime", - 0, - "you can not use $."HTTP_BODY_NAME" option with method GET"); + bool method_is_get=strcmp(method, "GET")==0; + if(vbody){ + if(method_is_get) + throw Exception(PARSER_RUNTIME, + 0, + "you can not use $."HTTP_BODY_NAME" option with method GET"); + + if(form) + throw Exception(PARSER_RUNTIME, + 0, + "you can not use options $."HTTP_BODY_NAME" and $."HTTP_FORM_NAME" together"); + } //preparing request String& connect_string=*new String; @@ -487,7 +510,7 @@ File_read_http_result pa_internal_file_r // influence URLencoding of tainted pieces to String::L_URI lang Temp_client_charset temp(charsets, *asked_remote_charset); - const char* connect_string_cstr=connect_string.cstr(String::L_UNSPECIFIED); + const char* connect_string_cstr=connect_string.cstr(String::L_UNSPECIFIED, 0, &charsets); const char* current=connect_string_cstr; if(strncmp(current, "http://", 7)!=0) @@ -505,18 +528,25 @@ File_read_http_result pa_internal_file_r bool uri_has_query_string=strchr(uri, '?')!=0; - //making request head + // making request head String head; - head << method; - head << " " << uri; - if(form) - if(method_is_get) - head << (uri_has_query_string?"&":"?") << pa_form2string(*form); - head <<" HTTP/1.0" CRLF - "host: "<< host << CRLF; + head << method << " " << uri; + if(form && method_is_get) + head << (uri_has_query_string?"&":"?") << pa_form2string(*form, charsets); + + head <<" HTTP/1.0" CRLF "host: "<< host << CRLF; + if(form && !method_is_get) { head << "content-type: application/x-www-form-urlencoded" CRLF; - body_cstr = pa_form2string(*form); + body_cstr=pa_form2string(*form, charsets); + } else if (vbody) { + body_cstr=vbody->as_string().cstr(String::L_UNSPECIFIED, 0, &charsets); + // needed for transcoded $.body[] first of all + body_cstr=Charset::transcode( + String::C(body_cstr, strlen(body_cstr)), + charsets.source(), + *asked_remote_charset + ); } // http://www.ietf.org/rfc/rfc2617.txt @@ -527,38 +557,37 @@ File_read_http_result pa_internal_file_r if(vheaders && !vheaders->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); + headers->for_each(http_pass_header, &info); user_agent_specified=info.user_agent_specified; } else - throw Exception("parser.runtime", + throw Exception(PARSER_RUNTIME, &connect_string, "headers param must be hash"); }; if(!user_agent_specified) // defaulting head << "user-agent: " DEFAULT_USER_AGENT CRLF; - if(body_cstr) { - // recode those pieces which are not in String::L_URI lang - // [those violating HTTP standard, but widly used] - body_cstr=Charset::transcode( - String::C(body_cstr, strlen(body_cstr)), - charsets.source(), - *asked_remote_charset); + if(vcookies && !vcookies->is_string()){ // allow empty + if(HashStringValue* cookies=vcookies->get_hash()) { + head << "cookie: "; + Http_pass_header_info info={&charsets, &head, false}; + cookies->for_each(http_pass_cookie, &info); + head << CRLF; + } else + throw Exception(PARSER_RUNTIME, + &connect_string, + "cookies param must be hash"); + } + if(body_cstr) { head << "content-length: " << format(strlen(body_cstr), "%u") << CRLF; } - const char* head_cstr=head.cstr(String::L_UNSPECIFIED); - - // recode those pieces which are not in String::L_URI lang - // [those violating HTTP standard, but widly used] - head_cstr=Charset::transcode( - String::C(head_cstr, strlen(head_cstr)), - charsets.source(), - *asked_remote_charset); + const char* head_cstr=head.cstr(String::L_UNSPECIFIED, 0, &charsets); // head + end of header request_head_and_body << head_cstr << CRLF; + // body if(body_cstr) request_head_and_body << body_cstr;