--- parser3/src/main/pa_http.C 2015/10/13 21:27:57 1.68 +++ parser3/src/main/pa_http.C 2016/07/26 15:22:36 1.73 @@ -1,7 +1,7 @@ /** @file Parser: http support functions. - Copyright (c) 2001-2012 Art. Lebedev Studio (http://www.artlebedev.com) + Copyright (c) 2001-2015 Art. Lebedev Studio (http://www.artlebedev.com) Author: Alexandr Petrosian (http://paf.design.ru) */ @@ -13,7 +13,7 @@ #include "pa_vfile.h" #include "pa_random.h" -volatile const char * IDENT_PA_HTTP_C="$Id: pa_http.C,v 1.68 2015/10/13 21:27:57 moko Exp $" IDENT_PA_HTTP_H; +volatile const char * IDENT_PA_HTTP_C="$Id: pa_http.C,v 1.73 2016/07/26 15:22:36 moko Exp $" IDENT_PA_HTTP_H; #ifdef _MSC_VER #include @@ -32,11 +32,10 @@ volatile const char * IDENT_PA_HTTP_C="$ #define HTTP_ANY_STATUS_NAME "any-status" #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" - #define HTTP_USER "user" #define HTTP_PASSWORD "password" +#define HTTP_USER_AGENT "user-agent" #define DEFAULT_USER_AGENT "parser3" #ifndef INADDR_NONE @@ -109,9 +108,7 @@ static int http_read_response(char*& res goto done; if(received_size<0) { if(int no=pa_socks_errno()) - throw Exception("http.timeout", - 0, - "error receiving response header: %s (%d)", pa_socks_strerr(no), no); + throw Exception("http.timeout", 0, "error receiving response header: %s (%d)", pa_socks_strerr(no), no); goto done; } // terminator [helps futher string searches] @@ -126,9 +123,7 @@ static int http_read_response(char*& res result=status_code.as_int(); if(fail_on_status_ne_200 && result!=200) - throw Exception("http.status", - &status_code, - "invalid HTTP response status"); + throw Exception("http.status", &status_code, "invalid HTTP response status"); } // detecting response_size { @@ -163,9 +158,7 @@ static int http_read_response(char*& res } if(received_size<0) { if(int no=pa_socks_errno()) - throw Exception("http.timeout", - 0, - "error receiving response body: %s (%d)", pa_socks_strerr(no), no); + throw Exception("http.timeout", 0, "error receiving response body: %s (%d)", pa_socks_strerr(no), no); break; } // they've touched the terminator? @@ -191,9 +184,7 @@ done: return result; } else - throw Exception("http.response", - 0, - "bad response from host - no status found (size=%u)", response_size); + throw Exception("http.response", 0, "bad response from host - no status found (size=%u)", response_size); } /* ********************** request *************************** */ @@ -215,9 +206,7 @@ static int http_request(char*& response, int timeout_secs, bool fail_on_status_ne_200) { if(!host) - throw Exception("http.host", - 0, - "zero hostname"); //never + throw Exception("http.host", 0, "zero hostname"); //never volatile // to prevent makeing it register variable, because it will be clobbered by longjmp [thanks gcc warning] int sock=-1; @@ -231,9 +220,7 @@ static int http_request(char*& response, // rewritten simplier [athough duplicating closesocket code] if(sock>=0) closesocket(sock); - throw Exception("http.timeout", - 0, - "timeout occured while retrieving document"); + throw Exception("http.timeout", 0, "timeout occured while retrieving document"); return 0; // never } else { alarm(timeout_secs); @@ -243,15 +230,11 @@ static int http_request(char*& response, struct sockaddr_in dest; if(!set_addr(&dest, host, port)) - throw Exception("http.host", - 0, - "can not resolve hostname \"%s\"", host); + throw Exception("http.host", 0, "can not resolve hostname \"%s\"", host); if((sock=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP/*0*/))<0) { int no=pa_socks_errno(); - throw Exception("http.connect", - 0, - "can not make socket: %s (%d)", pa_socks_strerr(no), no); + throw Exception("http.connect", 0, "can not make socket: %s (%d)", pa_socks_strerr(no), no); } // To enable SO_DONTLINGER (that is, disable SO_LINGER) @@ -270,16 +253,12 @@ static int http_request(char*& response, if(connect(sock, (struct sockaddr *)&dest, sizeof(dest))) { int no=pa_socks_errno(); - throw Exception("http.connect", - 0, - "can not connect to host \"%s\": %s (%d)", host, pa_socks_strerr(no), no); + throw Exception("http.connect", 0, "can not connect to host \"%s\": %s (%d)", host, pa_socks_strerr(no), no); } if(send(sock, request, request_size, 0)!=(ssize_t)request_size) { int no=pa_socks_errno(); - throw Exception("http.timeout", - 0, - "error sending request: %s (%d)", pa_socks_strerr(no), no); + throw Exception("http.timeout", 0, "error sending request: %s (%d)", pa_socks_strerr(no), no); } result=http_read_response(response, response_size, sock, fail_on_status_ne_200); @@ -371,11 +350,7 @@ static const String* basic_authorization return result; } -static void form_string_value2string( - HashStringValue::key_type key, - const String& value, - String& result) -{ +static void form_string_value2string(HashStringValue::key_type key, const String& value, String& result) { result << String(key, String::L_URI) << "=" << String(value, String::L_URI) << "&"; } @@ -391,19 +366,15 @@ struct Form_table_value2string_info { static void form_table_value2string(Table::element_type row, Form_table_value2string_info* info) { form_string_value2string(info->key, *row->get(0), info->result); } -static void form_value2string( - HashStringValue::key_type key, - HashStringValue::value_type value, - String* result) -{ + +static void form_value2string(HashStringValue::key_type key, HashStringValue::value_type value, String* result) { if(const String* svalue=value->get_string()) form_string_value2string(key, *svalue, *result); else if(Table* tvalue=value->get_table()) { Form_table_value2string_info info(key, *result); tvalue->for_each(form_table_value2string, &info); } else - throw Exception(PARSER_RUNTIME, - new String(key, String::L_TAINTED), + 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()); } @@ -456,11 +427,8 @@ struct FormPart { } }; -static void form_part_boundary_header(FormPart& part, String::Body name, const char* file_name=0){ - *part.string << "--" << part.boundary - << CRLF CONTENT_DISPOSITION_CAPITALIZED ": form-data; name=\"" - << name - << "\""; +static void form_part_boundary_header(FormPart& part, String::Body name, const char* file_name=0) { + *part.string << "--" << part.boundary << CRLF CONTENT_DISPOSITION_CAPITALIZED ": form-data; name=\"" << name << "\""; if(file_name){ if(strcmp(file_name, NONAME_DAT)!=0) *part.string << "; filename=\"" << file_name << "\""; @@ -469,20 +437,12 @@ static void form_part_boundary_header(Fo *part.string << CRLF CRLF; } -static void form_string_value2part( - HashStringValue::key_type key, - const String& value, - FormPart& part) -{ +static void form_string_value2part(HashStringValue::key_type key, const String& value, FormPart& part) { form_part_boundary_header(part, key); *part.string << value << CRLF; } -static void form_file_value2part( - HashStringValue::key_type key, - VFile& vfile, - FormPart& part) -{ +static void form_file_value2part(HashStringValue::key_type key, VFile& vfile, FormPart& part) { form_part_boundary_header(part, key, vfile.fields().get(name_name)->as_string().cstr()); part.blocks+=FormPart::BinaryBlock(part.string, part.r); part.blocks+=FormPart::BinaryBlock(vfile.value_ptr(), vfile.value_size()); @@ -494,11 +454,7 @@ static void form_table_value2part(Table: form_string_value2part(part->info->key, *row->get(0), *part); } -static void form_value2part( - HashStringValue::key_type key, - HashStringValue::value_type value, - FormPart& part) -{ +static void form_value2part(HashStringValue::key_type key, HashStringValue::value_type value, FormPart& part) { if(const String* svalue=value->get_string()) form_string_value2part(key, *svalue, part); else if(Table* tvalue=value->get_table()) { @@ -508,9 +464,7 @@ static void form_value2part( } else if(VFile* vfile=static_cast(value->as("file"))){ form_file_value2part(key, *vfile, part); } 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()); + 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()); } const char* pa_form2string_multipart(HashStringValue& form, Request& r, const char* boundary, size_t& post_size){ @@ -521,10 +475,7 @@ const char* pa_form2string_multipart(Has return formpart.post(post_size); } -static void find_headers_end(char* p, - char*& headers_end_at, - char*& raw_body) -{ +static void find_headers_end(char* p, char*& headers_end_at, char*& raw_body) { raw_body=p; // \n\n // \r\n\r\n @@ -616,12 +567,26 @@ Table* parse_cookies(Request& r, Table * return &result; } +void *tables_update(HashStringValue& tables, const String::Body name, const String& value){ + Table *table; + if(Value *valready=tables.get(name)) { + // second+ appearence + table=valready->get_table(); + } else { + // first appearence + Table::columns_type columns=new ArrayString(1); + *columns+=new String("value"); + table=new Table(columns); + tables.put(name, new VTable(table)); + } + // this string becomes next row + ArrayString& row=*new ArrayString(1); + row+=&value; + *table+=&row; +} + /// @todo build .cookies field. use ^file.tables.SET-COOKIES.menu{ for now -File_read_http_result pa_internal_file_read_http(Request& r, - const String& file_spec, - bool as_text, - HashStringValue *options, - bool transcode_text_result) { +File_read_http_result pa_internal_file_read_http(Request& r, const String& file_spec, bool as_text, HashStringValue *options, bool transcode_text_result) { File_read_http_result result; char host[MAX_STRING]; const char *idna_host; @@ -636,7 +601,7 @@ File_read_http_result pa_internal_file_r Value* vheaders=0; Value* vcookies=0; Value* vbody=0; - Charset *asked_remote_charset=0; + Charset* asked_remote_charset=0; Charset* real_remote_charset=0; const char* user_cstr=0; const char* password_cstr=0; @@ -704,28 +669,20 @@ File_read_http_result pa_internal_file_r if(encode){ if(method_is_get) - throw Exception(PARSER_RUNTIME, - 0, - "you can not use $." HTTP_FORM_ENCTYPE_NAME " option with method GET"); + throw Exception(PARSER_RUNTIME, 0, "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"); + 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"); } if(vbody){ if(method_is_get) - throw Exception(PARSER_RUNTIME, - 0, - "you can not use $." HTTP_BODY_NAME " option with method 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"); + throw Exception(PARSER_RUNTIME, 0, "you can not use options $." HTTP_BODY_NAME " and $." HTTP_FORM_NAME " together"); } //preparing request @@ -741,9 +698,7 @@ File_read_http_result pa_internal_file_r const char* current=connect_string_cstr; if(strncmp(current, "http://", 7)!=0) - throw Exception(PARSER_RUNTIME, - &connect_string, - "does not start with http://"); //never + throw Exception(PARSER_RUNTIME, &connect_string, "does not start with http://"); //never current+=7; strncpy(host, current, sizeof(host)-1); host[sizeof(host)-1]=0; @@ -771,19 +726,7 @@ File_read_http_result pa_internal_file_r head << ":" << port_cstr; head << CRLF; - char* boundary=0; - - if(multipart){ - uuid uuid=get_uuid(); - const int boundary_bufsize=10+32+1/*for zero-teminator*/+1/*for faulty snprintfs*/; - boundary=new(PointerFreeGC) char[boundary_bufsize]; - snprintf(boundary, boundary_bufsize, - "----------%08X%04X%04X%02X%02X%02X%02X%02X%02X%02X%02X", - uuid.time_low, uuid.time_mid, uuid.time_hi_and_version, - uuid.clock_seq >> 8, uuid.clock_seq & 0xFF, - uuid.node[0], uuid.node[1], uuid.node[2], - uuid.node[3], uuid.node[4], uuid.node[5]); - } + char* boundary= multipart ? get_uuid_boundary() : 0; String user_headers; bool user_agent_specified=false; @@ -799,9 +742,7 @@ File_read_http_result pa_internal_file_r &content_type_url_encoded}; headers->for_each(http_pass_header, &info); } else - throw Exception(PARSER_RUNTIME, - 0, - "headers param must be hash"); + throw Exception(PARSER_RUNTIME, 0, "headers param must be hash"); }; const char* request_body=0; @@ -827,11 +768,8 @@ File_read_http_result pa_internal_file_r request_body=vbody->as_string().untaint_and_transcode_cstr(String::L_URI, &(r.charsets)); } else { // content-type != application/x-www-form-urlencoded -> transcode only, don't url-encode! - request_body=Charset::transcode( - String::C(vbody->as_string().cstr(), vbody->as_string().length()), - r.charsets.source(), - *asked_remote_charset - ).str; + const String &sbody=vbody->as_string(); + request_body=Charset::transcode(String::C(sbody.cstr(), sbody.length()), r.charsets.source(), *asked_remote_charset).str; } post_size=strlen(request_body); } @@ -846,9 +784,7 @@ File_read_http_result pa_internal_file_r 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, - 0, - "$.content-type can't be specified with method POST"); + throw Exception(PARSER_RUNTIME, 0, "$.content-type can't be specified with method POST"); if(vcookies && !vcookies->is_string()){ // allow empty if(HashStringValue* cookies=vcookies->get_hash()) { @@ -857,9 +793,7 @@ File_read_http_result pa_internal_file_r cookies->for_each(http_pass_cookie, &info); head << CRLF; } else - throw Exception(PARSER_RUNTIME, - 0, - "cookies param must be hash"); + throw Exception(PARSER_RUNTIME, 0, "cookies param must be hash"); } if(request_body) @@ -882,95 +816,71 @@ File_read_http_result pa_internal_file_r } } - char* response; + char* response_str; size_t response_size; // sending request - int status_code=http_request(response, response_size, - idna_host, port, request, request_size, - timeout_secs, fail_on_status_ne_200); + int status_code=http_request(response_str, response_size, idna_host, port, request, request_size, timeout_secs, fail_on_status_ne_200); - // processing results + // processing results char* raw_body; size_t raw_body_size; char* headers_end_at; - find_headers_end(response, - headers_end_at, - raw_body); - raw_body_size=response_size-(raw_body-response); + find_headers_end(response_str, headers_end_at, raw_body); + raw_body_size=response_size-(raw_body-response_str); result.headers=new HashStringValue; VHash* vtables=new VHash; - result.headers->put(HTTP_TABLES_NAME, vtables); + result.headers->put("tables", vtables); + + ResponseHeaders response; if(headers_end_at) { *headers_end_at=0; - const String header_block(String::C(response, headers_end_at-response), String::L_TAINTED); + const String header_block(String::C(response_str, headers_end_at-response_str), String::L_TAINTED); ArrayString aheaders; - HashStringValue& tables=vtables->hash(); size_t pos_after=0; header_block.split(aheaders, pos_after, "\n"); - - // processing headers - size_t aheaders_count=aheaders.count(); - for(size_t i=1; iget_table(); - } else { - // first appearence - 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; - *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)); + Array_iterator i(aheaders); + i.next(); // skipping status + for(;i.has_next();){ + const char *line=i.next()->cstr(); + if(!response.add_header(line)) + throw Exception("http.response", &connect_string, "bad response from host - bad header \"%s\"", line); } - - // filling $.cookies - if(Value *vcookies=(Value *)tables.get("SET-COOKIE")) - result.headers->put(HTTP_COOKIES_NAME, new VTable(parse_cookies(r, vcookies->get_table()))); } - if(as_text){ + if (!real_remote_charset && !response.content_type.is_empty()) + real_remote_charset= detect_charset(response.content_type.cstr()); + + if(as_text) real_remote_charset=charsets.checkBOM(raw_body, raw_body_size, real_remote_charset); + + if (!real_remote_charset) + real_remote_charset=asked_remote_charset; // never null + + for(Array_iterator i(response.headers); i.has_next(); ){ + ResponseHeaders::Header header=i.next(); + + header.transcode(*real_remote_charset, r.charsets.source()); + + String &header_value=*new String(header.value, String::L_TAINTED); + + tables_update(vtables->hash(), header.name, header_value); + result.headers->put(header.name, new VString(header_value)); } + // filling $.cookies + if(Value *vcookies=vtables->hash().get("SET-COOKIE")) + result.headers->put(HTTP_COOKIES_NAME, new VTable(parse_cookies(r, vcookies->get_table()))); + // output response String::C real_body=String::C(raw_body, raw_body_size); 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, r.charsets.source()); - } result.str=const_cast(real_body.str); // hacking a little