From 1de8bed899cc08b3453753132d137b8a953a4e8d Mon Sep 17 00:00:00 2001 From: mmentovai Date: Mon, 2 Apr 2007 18:24:16 +0000 Subject: Windows http_upload should allow response without Content-Length header (#144). Patch by Ted Mielczarek. r=me http://groups.google.com/group/google-breakpad-dev/browse_thread/thread/a401434f639e5b2f git-svn-id: http://google-breakpad.googlecode.com/svn/trunk@136 4c0a9323-5329-0410-9bdc-e9ce6186880e --- src/common/windows/http_upload.cc | 50 ++++++++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 17 deletions(-) (limited to 'src/common') diff --git a/src/common/windows/http_upload.cc b/src/common/windows/http_upload.cc index 86ecf37a..aef046ad 100644 --- a/src/common/windows/http_upload.cc +++ b/src/common/windows/http_upload.cc @@ -174,31 +174,47 @@ bool HTTPUpload::SendRequest(const wstring &url, // static bool HTTPUpload::ReadResponse(HINTERNET request, wstring *response) { + bool has_content_length_header = false; wchar_t content_length[32]; DWORD content_length_size = sizeof(content_length); - if (!HttpQueryInfo(request, HTTP_QUERY_CONTENT_LENGTH, - static_cast(&content_length), &content_length_size, - 0)) { - return false; + DWORD claimed_size; + string response_body; + + if (HttpQueryInfo(request, HTTP_QUERY_CONTENT_LENGTH, + static_cast(&content_length), + &content_length_size, 0)) { + has_content_length_header = true; + claimed_size = wcstol(content_length, NULL, 10); + response_body.reserve(claimed_size); } - DWORD claimed_size = wcstol(content_length, NULL, 10); - char *response_buffer = new char[claimed_size]; - DWORD size_read; + + DWORD bytes_available; DWORD total_read = 0; - BOOL read_result; - do { - read_result = InternetReadFile(request, response_buffer + total_read, - claimed_size - total_read, &size_read); - total_read += size_read; - } while (read_result && (size_read != 0) && (total_read < claimed_size)); - - bool succeeded = (total_read == claimed_size); + bool return_code; + + while ((return_code = InternetQueryDataAvailable(request, &bytes_available, + 0, 0) != 0) && + bytes_available > 0) { + vector response_buffer(bytes_available); + DWORD size_read; + + if ((return_code = InternetReadFile(request, &response_buffer[0], + bytes_available, &size_read) != 0) && + size_read > 0) { + total_read += size_read; + response_body.append(&response_buffer[0], size_read); + } else { + break; + } + } + + bool succeeded = return_code && (!has_content_length_header || + (total_read == claimed_size)); if (succeeded && response) { - *response = UTF8ToWide(string(response_buffer, total_read)); + *response = UTF8ToWide(response_body); } - delete[] response_buffer; return succeeded; } -- cgit v1.2.1