Index: util/net/http_transport_win.cc |
diff --git a/util/net/http_transport_win.cc b/util/net/http_transport_win.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..8e1cef31ca401db45f1bbb149ee20e0fa65a3f4e |
--- /dev/null |
+++ b/util/net/http_transport_win.cc |
@@ -0,0 +1,214 @@ |
+// Copyright 2015 The Crashpad Authors. All rights reserved. |
+// |
+// Licensed under the Apache License, Version 2.0 (the "License"); |
+// you may not use this file except in compliance with the License. |
+// You may obtain a copy of the License at |
+// |
+// http://www.apache.org/licenses/LICENSE-2.0 |
+// |
+// Unless required by applicable law or agreed to in writing, software |
+// distributed under the License is distributed on an "AS IS" BASIS, |
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
+// See the License for the specific language governing permissions and |
+// limitations under the License. |
+ |
+#include "util/net/http_transport.h" |
+ |
+#include <windows.h> |
+#include <winhttp.h> |
+ |
+#include "base/logging.h" |
+#include "base/scoped_generic.h" |
+#include "base/strings/stringprintf.h" |
+#include "base/strings/utf_string_conversions.h" |
+#include "util/net/http_body.h" |
+#include "package.h" |
+ |
+namespace crashpad { |
+ |
+namespace { |
+ |
+// PLOG doesn't work for messages from WinHTTP, so we need to use |
+// FORMAT_MESSAGE_FROM_HMODULE + the dll name manually here. |
+void LogErrorWinHttpMessage(const char* extra) { |
+ DWORD error_code = GetLastError(); |
+ char msgbuf[256]; |
+ DWORD flags = FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | |
+ FORMAT_MESSAGE_MAX_WIDTH_MASK | FORMAT_MESSAGE_FROM_HMODULE; |
+ DWORD len = FormatMessageA(flags, |
+ GetModuleHandle(L"winhttp.dll"), |
+ error_code, |
+ 0, |
+ msgbuf, |
+ arraysize(msgbuf), |
+ NULL); |
+ if (len) { |
+ LOG(ERROR) << extra << ": " << msgbuf |
+ << base::StringPrintf(" (0x%X)", error_code); |
+ } else { |
+ LOG(ERROR) << base::StringPrintf( |
+ "Error (0x%X) while retrieving error. (0x%X)", |
+ GetLastError(), |
+ error_code); |
+ } |
+} |
+ |
+struct ScopedHINTERNETTraits { |
+ static HINTERNET InvalidValue() { |
+ return nullptr; |
+ } |
+ static void Free(HINTERNET handle) { |
+ if (handle) { |
+ if (!WinHttpCloseHandle(handle)) { |
+ LogErrorWinHttpMessage("WinHttpCloseHandle"); |
+ } |
+ } |
+ } |
+}; |
+ |
+using ScopedHINTERNET = base::ScopedGeneric<HINTERNET, ScopedHINTERNETTraits>; |
+ |
+class HTTPTransportWin final : public HTTPTransport { |
+ public: |
+ HTTPTransportWin(); |
+ ~HTTPTransportWin() override; |
+ |
+ bool ExecuteSynchronously() override; |
+ |
+ private: |
+ DISALLOW_COPY_AND_ASSIGN(HTTPTransportWin); |
+}; |
+ |
+HTTPTransportWin::HTTPTransportWin() : HTTPTransport() { |
+} |
+ |
+HTTPTransportWin::~HTTPTransportWin() { |
+} |
+ |
+bool HTTPTransportWin::ExecuteSynchronously() { |
+ ScopedHINTERNET session( |
+ WinHttpOpen(base::UTF8ToUTF16(PACKAGE_NAME "/" PACKAGE_VERSION).c_str(), |
+ WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, |
+ WINHTTP_NO_PROXY_NAME, |
+ WINHTTP_NO_PROXY_BYPASS, |
+ 0)); |
+ if (!session.get()) { |
+ LogErrorWinHttpMessage("WinHttpOpen"); |
+ return false; |
+ } |
+ |
+ URL_COMPONENTS url_components = {0}; |
+ url_components.dwStructSize = sizeof(URL_COMPONENTS); |
+ url_components.dwHostNameLength = 1; |
+ url_components.dwUrlPathLength = 1; |
+ url_components.dwExtraInfoLength = 1; |
+ std::wstring url_wide(base::UTF8ToUTF16(url())); |
+ if (!WinHttpCrackUrl( |
+ url_wide.c_str(), 0, ICU_REJECT_USERPWD, &url_components)) { |
+ LogErrorWinHttpMessage("WinHttpCrackUrl"); |
+ return false; |
+ } |
+ std::wstring host_name(url_components.lpszHostName, |
+ url_components.dwHostNameLength); |
+ std::wstring url_path(url_components.lpszUrlPath, |
+ url_components.dwUrlPathLength); |
+ std::wstring extra_info(url_components.lpszExtraInfo, |
+ url_components.dwExtraInfoLength); |
+ |
+ ScopedHINTERNET connect(WinHttpConnect( |
+ session.get(), host_name.c_str(), url_components.nPort, 0)); |
+ if (!connect.get()) { |
+ LogErrorWinHttpMessage("WinHttpConnect"); |
+ return false; |
+ } |
+ |
+ ScopedHINTERNET request( |
+ WinHttpOpenRequest(connect.get(), |
+ base::UTF8ToUTF16(method()).c_str(), |
+ url_path.c_str(), |
+ nullptr, |
+ WINHTTP_NO_REFERER, |
+ WINHTTP_DEFAULT_ACCEPT_TYPES, |
+ 0)); |
+ if (!request.get()) { |
+ LogErrorWinHttpMessage("WinHttpOpenRequest"); |
+ return false; |
+ } |
+ |
+ // Add headers to the request. |
+ for (const auto& pair : headers()) { |
+ std::wstring header_string = |
+ base::UTF8ToUTF16(pair.first) + L": " + base::UTF8ToUTF16(pair.second); |
+ if (!WinHttpAddRequestHeaders(request.get(), |
+ header_string.c_str(), |
+ header_string.size(), |
+ WINHTTP_ADDREQ_FLAG_ADD)) { |
+ LogErrorWinHttpMessage("WinHttpAddRequestHeaders"); |
+ return false; |
+ } |
+ } |
+ |
+ // We need the Content-Length up front, so buffer in memory. We should modify |
Robert Sesek
2015/01/26 21:17:09
This would be helpful on OS X too, and then we can
scottmg
2015/01/26 21:22:34
OK, I'll take a look at doing that as a follow up.
|
+ // the interface to not require this, and then use WinHttpWriteData after |
+ // WinHttpSendRequest. |
+ std::vector<uint8_t> post_data; |
+ |
+ // Write the body of a POST if any. |
+ for (;;) { |
+ uint8_t buffer[4096]; |
+ ssize_t bytes_to_write = |
+ body_stream()->GetBytesBuffer(buffer, sizeof(buffer)); |
+ if (bytes_to_write == 0) |
+ break; |
+ post_data.insert(post_data.end(), buffer, buffer + bytes_to_write); |
+ } |
+ |
+ if (!WinHttpSendRequest(request.get(), |
+ WINHTTP_NO_ADDITIONAL_HEADERS, |
+ 0, |
+ &post_data[0], |
+ post_data.size(), |
+ post_data.size(), |
+ 0)) { |
+ LogErrorWinHttpMessage("WinHttpSendRequest"); |
+ return false; |
+ } |
+ |
+ if (!WinHttpReceiveResponse(request.get(), nullptr)) { |
+ LogErrorWinHttpMessage("WinHttpReceiveResponse"); |
+ return false; |
+ } |
+ |
+ DWORD status_code = 0; |
+ DWORD sizeof_status_code = sizeof(status_code); |
+ |
+ if (!WinHttpQueryHeaders( |
+ request.get(), |
+ WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER, |
+ WINHTTP_HEADER_NAME_BY_INDEX, |
+ &status_code, |
+ &sizeof_status_code, |
+ WINHTTP_NO_HEADER_INDEX)) { |
+ LogErrorWinHttpMessage("WinHttpQueryHeaders"); |
+ return false; |
+ } |
+ |
+ if (status_code != 200) { |
+ LOG(ERROR) << base::StringPrintf("HTTP status %d", status_code); |
+ return false; |
+ } |
+ |
+ // TODO(scottmg): Retrieve body of response if necessary with |
+ // WinHttpQueryDataAvailable and WinHttpReadData. |
+ |
+ return true; |
+} |
+ |
+} // namespace |
+ |
+// static |
+scoped_ptr<HTTPTransport> HTTPTransport::Create() { |
+ return scoped_ptr<HTTPTransportWin>(new HTTPTransportWin); |
+} |
+ |
+} // namespace crashpad |