--- parser3/src/main/pa_http.C 2013/03/10 23:32:56 1.56 +++ parser3/src/main/pa_http.C 2015/04/02 22:18:26 1.63 @@ -13,7 +13,13 @@ #include "pa_vfile.h" #include "pa_random.h" -volatile const char * IDENT_PA_HTTP_C="$Id: pa_http.C,v 1.56 2013/03/10 23:32:56 misha Exp $" IDENT_PA_HTTP_H; +volatile const char * IDENT_PA_HTTP_C="$Id: pa_http.C,v 1.63 2015/04/02 22:18:26 moko Exp $" IDENT_PA_HTTP_H; + +#ifdef _MSC_VER +#include +#else +#define closesocket close +#endif // defines @@ -22,11 +28,9 @@ volatile const char * IDENT_PA_HTTP_C="$ #define HTTP_BODY_NAME "body" #define HTTP_TIMEOUT_NAME "timeout" #define HTTP_HEADERS_NAME "headers" -#define HTTP_COOKIES_NAME "cookies" #define HTTP_FORM_ENCTYPE_NAME "enctype" #define HTTP_ANY_STATUS_NAME "any-status" -#define HTTP_OMIT_POST_CHARSET_NAME "omit-post-charset" // ^file::load[...;http://...;$.form[...]$.method[post]] - // by default add charset to content-type +#define HTTP_OMIT_POST_CHARSET_NAME "omit-post-charset" // ^file::load[...;http://...;$.method[post]] by default adds charset to content-type #define HTTP_TABLES_NAME "tables" @@ -35,9 +39,9 @@ volatile const char * IDENT_PA_HTTP_C="$ #define DEFAULT_USER_AGENT "parser3" -# ifndef INADDR_NONE -# define INADDR_NONE ((ulong) -1) -# endif +#ifndef INADDR_NONE +#define INADDR_NONE ((ulong) -1) +#endif #undef CRLF #define CRLF "\r\n" @@ -343,7 +347,7 @@ static void http_pass_header(HashStringV *info->user_agent_specified=true; if(strcasecmp(name_cstr, HTTP_CONTENT_TYPE)==0){ *info->content_type_specified=true; - *info->content_type_url_encoded=StrStartFromNC(value.cstr(), HTTP_CONTENT_TYPE_FORM_URLENCODED); + *info->content_type_url_encoded=pa_strncasecmp(value.cstr(), HTTP_CONTENT_TYPE_FORM_URLENCODED)==0; } } @@ -406,7 +410,7 @@ static void form_value2string( } else throw Exception(PARSER_RUNTIME, new String(key, String::L_TAINTED), - "is %s, "HTTP_FORM_NAME" option value can be string or table only (file is allowed for $."HTTP_METHOD_NAME"[POST] + $."HTTP_FORM_ENCTYPE_NAME"["HTTP_CONTENT_TYPE_MULTIPART_FORMDATA"])", value->type()); + "is %s, " HTTP_FORM_NAME " option value can be string or table only (file is allowed for $." HTTP_METHOD_NAME "[POST] + $." HTTP_FORM_ENCTYPE_NAME "[" HTTP_CONTENT_TYPE_MULTIPART_FORMDATA "])", value->type()); } const char* pa_form2string(HashStringValue& form, Request_charsets& charsets) { @@ -512,7 +516,7 @@ static void form_value2part( } else throw Exception(PARSER_RUNTIME, new String(key, String::L_TAINTED), - "is %s, "HTTP_FORM_NAME" option value can be string, table or file only", value->type()); + "is %s, " HTTP_FORM_NAME " option value can be string, table or file only", value->type()); } const char* pa_form2string_multipart(HashStringValue& form, Request& r, const char* boundary, size_t& post_size){ @@ -638,6 +642,7 @@ File_read_http_result pa_internal_file_r Value* vcookies=0; Value* vbody=0; Charset *asked_remote_charset=0; + Charset* real_remote_charset=0; const char* user_cstr=0; const char* password_cstr=0; const char* encode=0; @@ -681,8 +686,11 @@ File_read_http_result pa_internal_file_r 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(r.charsets.source(), String::CC_UPPER)); + asked_remote_charset=&charsets.get(vcharset_name->as_string().change_case(r.charsets.source(), String::CC_UPPER)); + } + if(Value* vresponse_charset_name=options->get(PA_RESPONSE_CHARSET_NAME)) { + valid_options++; + real_remote_charset=&charsets.get(vresponse_charset_name->as_string().change_case(r.charsets.source(), String::CC_UPPER)); } if(Value* vuser=options->get(HTTP_USER)) { valid_options++; @@ -703,26 +711,26 @@ File_read_http_result pa_internal_file_r if(method_is_get) throw Exception(PARSER_RUNTIME, 0, - "you can not use $."HTTP_FORM_ENCTYPE_NAME" option with method GET"); + "you can not use $." HTTP_FORM_ENCTYPE_NAME " option with method GET"); multipart=strcasecmp(encode, HTTP_CONTENT_TYPE_MULTIPART_FORMDATA)==0; if(!multipart && strcasecmp(encode, HTTP_CONTENT_TYPE_FORM_URLENCODED)!=0) throw Exception(PARSER_RUNTIME, 0, - "$."HTTP_FORM_ENCTYPE_NAME" option value can be "HTTP_CONTENT_TYPE_FORM_URLENCODED" or "HTTP_CONTENT_TYPE_MULTIPART_FORMDATA" only"); + "$." HTTP_FORM_ENCTYPE_NAME " option value can be " HTTP_CONTENT_TYPE_FORM_URLENCODED " or " HTTP_CONTENT_TYPE_MULTIPART_FORMDATA " only"); } if(vbody){ if(method_is_get) throw Exception(PARSER_RUNTIME, 0, - "you can not use $."HTTP_BODY_NAME" option with method GET"); + "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"); + "you can not use options $." HTTP_BODY_NAME " and $." HTTP_FORM_NAME " together"); } //preparing request @@ -896,7 +904,6 @@ File_read_http_result pa_internal_file_r result.headers=new HashStringValue; VHash* vtables=new VHash; result.headers->put(HTTP_TABLES_NAME, vtables); - Charset* real_remote_charset=0; // undetected, yet if(headers_end_at) { *headers_end_at=0; @@ -919,7 +926,7 @@ File_read_http_result pa_internal_file_r "bad response from host - bad header \"%s\"", line.cstr()); const String::Body HEADER_NAME=line.mid(0, pos).change_case(r.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_UPPER) + if(as_text && HEADER_NAME==HTTP_CONTENT_TYPE_UPPER && !real_remote_charset) real_remote_charset=detect_charset(HEADER_VALUE.cstr()); // tables