--- parser3/src/main/pa_http.C 2013/04/21 20:37:55 1.58 +++ parser3/src/main/pa_http.C 2016/07/21 17:05:37 1.71 @@ -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,13 @@ #include "pa_vfile.h" #include "pa_random.h" -volatile const char * IDENT_PA_HTTP_C="$Id: pa_http.C,v 1.58 2013/04/21 20:37:55 moko Exp $" IDENT_PA_HTTP_H; +volatile const char * IDENT_PA_HTTP_C="$Id: pa_http.C,v 1.71 2016/07/21 17:05:37 moko Exp $" IDENT_PA_HTTP_H; + +#ifdef _MSC_VER +#include +#else +#define closesocket close +#endif // defines @@ -24,19 +30,19 @@ volatile const char * IDENT_PA_HTTP_C="$ #define HTTP_HEADERS_NAME "headers" #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" #define HTTP_USER "user" #define HTTP_PASSWORD "password" +#define HTTP_USER_AGENT "user-agent" #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" @@ -63,19 +69,13 @@ static bool set_addr(struct sockaddr_in addr->sin_family=AF_INET; addr->sin_port=htons(port); if(host) { - ulong packed_ip=inet_addr(host); - if(packed_ip!=INADDR_NONE) - memcpy(&addr->sin_addr, &packed_ip, sizeof(packed_ip)); - else { - struct hostent *hostIP=gethostbyname(host); - if(hostIP) - memcpy(&addr->sin_addr, hostIP->h_addr, hostIP->h_length); - else - return false; - } - } else - addr->sin_addr.s_addr=INADDR_ANY; - return true; + struct hostent *hostIP=gethostbyname(host); + if(hostIP && hostIP->h_addrtype == AF_INET){ + memcpy(&addr->sin_addr, hostIP->h_addr, hostIP->h_length); + return true; + } + } + return false; } size_t guess_content_length(char* buf) { @@ -342,7 +342,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; } } @@ -405,7 +405,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) { @@ -511,7 +511,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){ @@ -543,7 +543,7 @@ static void find_headers_end(char* p, // Set-Cookie: name=value; Domain=docs.foo.com; Path=/accounts; Expires=Wed, 13-Jan-2021 22:23:01 GMT; Secure; HttpOnly static ArrayString* parse_cookie(Request& r, const String& cookie) { - char *current=strdup(cookie.cstr()); + char *current=pa_strdup(cookie.cstr()); const String* name=0; const String* value=&String::Empty; @@ -625,6 +625,7 @@ File_read_http_result pa_internal_file_r bool transcode_text_result) { File_read_http_result result; char host[MAX_STRING]; + const char *idna_host; const char* uri; short port=80; const char* method="GET"; @@ -684,6 +685,7 @@ File_read_http_result pa_internal_file_r 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)) { @@ -705,26 +707,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 @@ -757,30 +759,20 @@ File_read_http_result pa_internal_file_r throw Exception(PARSER_RUNTIME, &connect_string, "invalid port number '%s'", port_cstr); } + idna_host=pa_idna_encode(host, r.charsets.source()); + // making request head String head; head << method << " " << uri; if(method_is_get && form) head << (strchr(uri, '?')!=0?"&":"?") << pa_form2string(*form, r.charsets); - head <<" HTTP/1.0" CRLF "Host: "<< host; + head <<" HTTP/1.0" CRLF "Host: "<< idna_host; if (port != 80) 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; @@ -828,7 +820,7 @@ File_read_http_result pa_internal_file_r String::C(vbody->as_string().cstr(), vbody->as_string().length()), r.charsets.source(), *asked_remote_charset - ); + ).str; } post_size=strlen(request_body); } @@ -884,7 +876,7 @@ File_read_http_result pa_internal_file_r // sending request int status_code=http_request(response, response_size, - host, port, request, request_size, + idna_host, port, request, request_size, timeout_secs, fail_on_status_ne_200); // processing results @@ -954,12 +946,8 @@ File_read_http_result pa_internal_file_r result.headers->put(HTTP_COOKIES_NAME, new VTable(parse_cookies(r, vcookies->get_table()))); } - if(as_text && raw_body_size>=3 && strncmp(raw_body, "\xEF\xBB\xBF", 3)==0){ - // skip UTF-8 signature (BOM code) - raw_body+=3; - raw_body_size-=3; - if(!real_remote_charset) - real_remote_charset=&UTF8_charset; + if(as_text){ + real_remote_charset=charsets.checkBOM(raw_body, raw_body_size, real_remote_charset); } // output response