--- parser3/src/main/pa_http.C 2009/08/22 14:22:33 1.35 +++ parser3/src/main/pa_http.C 2009/09/08 09:14:02 1.40 @@ -5,7 +5,7 @@ Author: Alexandr Petrosian (http://paf.design.ru) */ -static const char * const IDENT_HTTP_C="$Date: 2009/08/22 14:22:33 $"; +static const char * const IDENT_HTTP_C="$Date: 2009/09/08 09:14:02 $"; #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) { @@ -323,14 +323,18 @@ static void http_pass_header(HashStringV Http_pass_header_info *info) { String name=String(aname, String::L_URI); + const char* name_cstr=capitalize(name.cstr()); + + if(strcasecmp(name_cstr, HTTP_CONTENT_LENGTH)==0) + return; + String value=attributed_meaning_to_string(*avalue, String::L_URI, false); *info->request << name << ": " << value << CRLF; - const String::Body name_upper=name.change_case(info->charsets->source(), String::CC_UPPER); - if(name_upper==HTTP_USER_AGENT_UPPER) + if(strcasecmp(name_cstr, HTTP_USER_AGENT)==0) *info->user_agent_specified=true; - if(name_upper==HTTP_CONTENT_TYPE_UPPER){ + 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); } @@ -413,7 +417,7 @@ 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 ": form-data; name=\"" << Charset::transcode(name, part.r->charsets.source(), part.r->charsets.client()) << "\""; if(file_name){ @@ -575,7 +579,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(); @@ -636,7 +640,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; @@ -673,7 +677,7 @@ File_read_http_result pa_internal_file_r 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(); @@ -687,11 +691,12 @@ File_read_http_result pa_internal_file_r } head << CRLF; } else if(vbody) { + // $.body was specified if(content_type_url_encoded){ - // transcode + url-escape + // 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-escape! + // 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(), @@ -703,12 +708,12 @@ File_read_http_result pa_internal_file_r // 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* cookies=vcookies->get_hash()) { - head << "cookie: "; + head << "Cookie: "; Http_pass_header_info info={&(r.charsets), &head, 0, 0, 0}; cookies->for_each(http_pass_cookie, &info); head << CRLF; @@ -728,7 +733,7 @@ File_read_http_result pa_internal_file_r } 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;