--- parser3/src/main/pa_http.C 2009/08/05 08:57:08 1.32 +++ parser3/src/main/pa_http.C 2009/09/10 10:01:45 1.42 @@ -5,7 +5,7 @@ Author: Alexandr Petrosian (http://paf.design.ru) */ -static const char * const IDENT_HTTP_C="$Date: 2009/08/05 08:57:08 $"; +static const char * const IDENT_HTTP_C="$Date: 2009/09/10 10:01:45 $"; #include "pa_http.h" #include "pa_common.h" @@ -66,7 +66,7 @@ size_t guess_content_length(char* buf) { char* ptr; if((ptr=strstr(buf, "Content-Length:"))) // Apache goto found; - if((ptr=strstr(buf, "content-length:"))) // Parser 3 + if((ptr=strstr(buf, "content-length:"))) // Parser 3 before 3.4.0 goto found; if((ptr=strstr(buf, "Content-length:"))) // maybe 1 goto found; @@ -75,7 +75,7 @@ size_t guess_content_length(char* buf) { return 0; found: char *error_pos; - size_t result=(size_t)strtol(ptr+15/*strlen("CONTENT-LENGTH:")*/, &error_pos, 0); + size_t result=(size_t)strtol(ptr+15/*strlen("Content-Length:")*/, &error_pos, 0); const size_t reasonable_initial_max=0x400*0x400*10 /*10M*/; if(result>reasonable_initial_max) // sanity check @@ -85,8 +85,8 @@ found: static int http_read_response(char*& response, size_t& response_size, int sock, bool fail_on_status_ne_200) { int result=0; - // fetching some to local buffer, guessing on possible content-length - response_size=0x400*20; // initial size if content-length could not be determined + // fetching some to local buffer, guessing on possible Content-Length + response_size=0x400*20; // initial size if Content-Length could not be determined const size_t preview_size=0x400*20; char preview_buf[preview_size+1/*terminator*/]; // 20K buffer to preview headers ssize_t received_size=recv(sock, preview_buf, preview_size, 0); @@ -135,9 +135,9 @@ static int http_read_response(char*& res // we use terminator byte for two purposes here: // 1. we return there zero always, not knowing: maybe they would want to create String form $file.body? // invariant: all Strings should have zero-terminated buffers - // 2. we use that out-of-size byte to detect if our content-length guess was wrong + // 2. we use that out-of-size byte to detect if our Content-Length guess was wrong // when recv gets more than we expected - // a) we know that the content-length guess was wrong + // a) we know that the Content-Length guess was wrong // b) we have space to put the first byte of extra data // c) we use less code to detect normal situation: on last while-cycle recv expected to just return 0 while(true) { @@ -313,25 +313,32 @@ static int http_request(char*& response, struct Http_pass_header_info { Request_charsets* charsets; String* request; - bool user_agent_specified; - bool content_type_specified; + bool* user_agent_specified; + bool* content_type_specified; + bool* content_type_url_encoded; }; #endif -static void http_pass_header(HashStringValue::key_type name, - HashStringValue::value_type value, +static void http_pass_header(HashStringValue::key_type aname, + HashStringValue::value_type avalue, Http_pass_header_info *info) { - String aname=String(name, String::L_URI); + const char* name_cstr=aname.cstr(); + + String name=String(capitalize(name_cstr), String::L_URI); + + if(strcasecmp(name_cstr, HTTP_CONTENT_LENGTH)==0) + return; + + String value=attributed_meaning_to_string(*avalue, String::L_URI, true); - *info->request << aname << ": " - << attributed_meaning_to_string(*value, String::L_URI, false) - << CRLF; + *info->request << name << ": " << value << CRLF; - const String::Body name_upper=aname.change_case(info->charsets->source(), String::CC_UPPER); - if(name_upper==HTTP_USER_AGENT_UPPER) - info->user_agent_specified=true; - if(name_upper==HTTP_CONTENT_TYPE_UPPER) - info->content_type_specified=true; + if(strcasecmp(name_cstr, HTTP_USER_AGENT)==0) + *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); + } } static void http_pass_cookie(HashStringValue::key_type name, @@ -399,7 +406,7 @@ static void form_value2string( const char* pa_form2string(HashStringValue& form, Request_charsets& charsets) { String string; form.for_each(form_value2string, &string); - return string.untaint_cstr(String::L_AS_IS, 0, &charsets); + return string.transcode_and_untaint_cstr(String::L_URI, &charsets); } struct FormPart { @@ -411,13 +418,13 @@ struct FormPart { static void form_part_boundary_header(FormPart& part, String::Body name, const char* file_name=0){ part.string << "--" << part.boundary - << CRLF HTTP_CONTENT_DISPOSITION ": form-data; name=\"" + << CRLF CONTENT_DISPOSITION_CAPITALIZED ": form-data; name=\"" << Charset::transcode(name, part.r->charsets.source(), part.r->charsets.client()) << "\""; if(file_name){ if(strcmp(file_name, NONAME_DAT)!=0) part.string << "; filename=\"" << file_name << "\""; - part.string << CRLF HTTP_CONTENT_TYPE ": " << part.r->mime_type_of(file_name); + part.string << CRLF HTTP_CONTENT_TYPE_CAPITALIZED ": " << part.r->mime_type_of(file_name); } part.string << CRLF CRLF; } @@ -456,7 +463,7 @@ static void form_value2part( Form_table_value2string_info info(key, part.string); part.info = &info; tvalue->for_each(form_table_value2part, &part); - } else if(VFile* vfile=static_cast(value->as("file", false))){ + } else if(VFile* vfile=static_cast(value->as("file"))){ form_file_value2part(key, *vfile, part); } else throw Exception(PARSER_RUNTIME, @@ -573,7 +580,7 @@ File_read_http_result pa_internal_file_r if(valid_options!=options->count()) throw Exception(PARSER_RUNTIME, 0, - "invalid option passed"); + INVALID_OPTION_PASSED); } if(!asked_remote_charset) // defaulting to $request:charset asked_remote_charset=&(r.charsets).source(); @@ -612,7 +619,7 @@ File_read_http_result pa_internal_file_r // influence URLencoding of tainted pieces to String::L_URI lang Temp_client_charset temp(r.charsets, *asked_remote_charset); - const char* connect_string_cstr=connect_string.untaint_cstr(String::L_URI, 0, &(r.charsets)); + const char* connect_string_cstr=connect_string.transcode_and_untaint_cstr(String::L_URI, &(r.charsets)); const char* current=connect_string_cstr; if(strncmp(current, "http://", 7)!=0) @@ -622,9 +629,9 @@ File_read_http_result pa_internal_file_r current+=7; strncpy(host, current, sizeof(host)-1); host[sizeof(host)-1]=0; - char* host_uri=lsplit(host, '/'); - uri=host_uri?current+(host_uri-1-host):"/"; - char* port_cstr=lsplit(host, ':'); + char* host_uri=lsplit(host, '/'); + uri=host_uri?current+(host_uri-1-host):"/"; + char* port_cstr=lsplit(host, ':'); char* error_pos=0; port=port_cstr?(short)strtol(port_cstr, &error_pos, 0):80; @@ -634,7 +641,7 @@ File_read_http_result pa_internal_file_r if(method_is_get && form) head << (strchr(uri, '?')!=0?"&":"?") << pa_form2string(*form, r.charsets); - head <<" HTTP/1.0" CRLF "host: "<< host << CRLF; + head <<" HTTP/1.0" CRLF "Host: "<< host << CRLF; char* boundary=0; @@ -650,9 +657,28 @@ File_read_http_result pa_internal_file_r uuid.node[3], uuid.node[4], uuid.node[5]); } + String user_headers; + bool user_agent_specified=false; + bool content_type_specified=false; + bool content_type_url_encoded=false; + if(vheaders && !vheaders->is_string()) { // allow empty + if(HashStringValue *headers=vheaders->get_hash()) { + Http_pass_header_info info={ + &(r.charsets), + &user_headers, + &user_agent_specified, + &content_type_specified, + &content_type_url_encoded}; + headers->for_each(http_pass_header, &info); + } else + throw Exception(PARSER_RUNTIME, + 0, + "headers param must be hash"); + }; + size_t post_size=0; if(form && !method_is_get) { - head << HTTP_CONTENT_TYPE ": " << (multipart ? HTTP_CONTENT_TYPE_MULTIPART_FORMDATA : HTTP_CONTENT_TYPE_FORM_URLENCODED); + head << "Content-Type: " << (multipart ? HTTP_CONTENT_TYPE_MULTIPART_FORMDATA : HTTP_CONTENT_TYPE_FORM_URLENCODED); if(!omit_post_charset) head << "; charset=" << asked_remote_charset->NAME_CSTR(); @@ -665,62 +691,53 @@ File_read_http_result pa_internal_file_r post_size=strlen(body_cstr); } head << CRLF; - } else if (vbody) { - // transcode tainted pieces and then URI-encode them - body_cstr=vbody->as_string().untaint_cstr(String::L_AS_IS, 0, &(r.charsets)); - - // now transcode is needed only if own content-type was specified _and_ clean chars with code>127 are in the body - // @todo: I don't like the current behaviour - body_cstr=Charset::transcode( - String::C(body_cstr, strlen(body_cstr)), - r.charsets.source(), - *asked_remote_charset - ); + } else if(vbody) { + // $.body was specified + if(content_type_url_encoded){ + // transcode + url-encode + body_cstr=vbody->as_string().transcode_and_untaint_cstr(String::L_URI, &(r.charsets)); + } else { + // content-type != application/x-www-form-urlencoded -> transcode only, don't url-encode! + body_cstr=Charset::transcode( + String::C(vbody->as_string().cstr(), vbody->as_string().length()), + r.charsets.source(), + *asked_remote_charset + ); + } post_size=strlen(body_cstr); } // http://www.ietf.org/rfc/rfc2617.txt if(const String* authorization_field_value=basic_authorization_field(user_cstr, password_cstr)) - head<<"authorization: "<<*authorization_field_value<is_string()) { // allow empty - if(HashStringValue *headers=vheaders->get_hash()) { - Http_pass_header_info info={&(r.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, - "headers param must be hash"); - }; if(!user_agent_specified) // defaulting - head << HTTP_USER_AGENT ": " DEFAULT_USER_AGENT CRLF; + 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, + 0, "$.content-type can't be specified with method POST"); if(vcookies && !vcookies->is_string()){ // allow empty if(HashStringValue* cookies=vcookies->get_hash()) { - head << "cookie: "; - Http_pass_header_info info={&(r.charsets), &head, false}; + head << "Cookie: "; + Http_pass_header_info info={&(r.charsets), &head, 0, 0, 0}; cookies->for_each(http_pass_cookie, &info); head << CRLF; } else throw Exception(PARSER_RUNTIME, - &connect_string, + 0, "cookies param must be hash"); } if(body_cstr) - head << "content-length: " << format(post_size, "%u") << CRLF; + head << "Content-Length: " << format(post_size, "%u") << CRLF; // head + end of header - request_head_and_body << head.untaint_cstr(String::L_AS_IS, 0, &(r.charsets)) << CRLF; + request_head_and_body << head.transcode_and_untaint_cstr(String::L_URI, &(r.charsets)) << CRLF; // body if(body_cstr)