OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Crashpad Authors. All rights reserved. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 |
| 15 #include "util/net/http_transport.h" |
| 16 |
| 17 #include <windows.h> |
| 18 #include <wininet.h> |
| 19 #pragma comment(lib, "wininet.lib") |
| 20 |
| 21 #include "base/logging.h" |
| 22 #include "base/strings/stringprintf.h" |
| 23 #include "base/strings/utf_string_conversions.h" |
| 24 |
| 25 namespace crashpad { |
| 26 |
| 27 namespace { |
| 28 |
| 29 class HTTPTransportWin final : public HTTPTransport { |
| 30 public: |
| 31 HTTPTransportWin(); |
| 32 ~HTTPTransportWin() override; |
| 33 |
| 34 bool ExecuteSynchronously() override; |
| 35 |
| 36 private: |
| 37 DISALLOW_COPY_AND_ASSIGN(HTTPTransportWin); |
| 38 }; |
| 39 |
| 40 HTTPTransportWin::HTTPTransportWin() : HTTPTransport() { |
| 41 } |
| 42 |
| 43 HTTPTransportWin::~HTTPTransportWin() { |
| 44 } |
| 45 |
| 46 bool HTTPTransportWin::ExecuteSynchronously() { |
| 47 HINTERNET internet = InternetOpen( |
| 48 L"Unknown", INTERNET_OPEN_TYPE_PRECONFIG, nullptr, nullptr, 0); |
| 49 if (!internet) { |
| 50 PLOG(ERROR) << "InternetOpen"; |
| 51 return false; |
| 52 } |
| 53 URL_COMPONENTS url_components = {0}; |
| 54 url_components.dwStructSize = sizeof(URL_COMPONENTS); |
| 55 std::wstring url_wide(base::UTF8ToUTF16(url())); |
| 56 url_components.dwSchemeLength = 1; |
| 57 url_components.dwHostNameLength = 1; |
| 58 url_components.dwUrlPathLength = 1; |
| 59 url_components.dwExtraInfoLength = 1; |
| 60 PCHECK(InternetCrackUrl(url_wide.c_str(), 0, 0, &url_components)); |
| 61 |
| 62 std::wstring scheme(url_components.lpszScheme, url_components.dwSchemeLength); |
| 63 CHECK(scheme == L"http"); |
| 64 std::wstring host_name(url_components.lpszHostName, |
| 65 url_components.dwHostNameLength); |
| 66 std::wstring url_path(url_components.lpszUrlPath, |
| 67 url_components.dwUrlPathLength); |
| 68 std::wstring extra_info(url_components.lpszExtraInfo, |
| 69 url_components.dwExtraInfoLength); |
| 70 |
| 71 HINTERNET session = InternetConnect(internet, |
| 72 host_name.c_str(), |
| 73 INTERNET_DEFAULT_HTTP_PORT, |
| 74 nullptr, |
| 75 nullptr, |
| 76 INTERNET_SERVICE_HTTP, |
| 77 0, |
| 78 0); |
| 79 if (!session) { |
| 80 PLOG(ERROR) << "InternetConnect"; |
| 81 return false; |
| 82 } |
| 83 const wchar_t* accept_types[] = {L"*/*", nullptr}; |
| 84 HINTERNET request = HttpOpenRequest(session, |
| 85 base::UTF8ToUTF16(method()).c_str(), |
| 86 url_path.c_str(), |
| 87 nullptr, |
| 88 nullptr, |
| 89 accept_types, |
| 90 INTERNET_FLAG_NO_UI, |
| 91 0); |
| 92 if (!request) { |
| 93 PLOG(ERROR) << "HttpOpenRequest"; |
| 94 return false; |
| 95 } |
| 96 std::wstring headers_as_string; |
| 97 for (const auto& pair : headers()) { |
| 98 headers_as_string += base::UTF8ToUTF16(pair.first) + L": " + |
| 99 base::UTF8ToUTF16(pair.second) + L"\r\n"; |
| 100 } |
| 101 if (!HttpSendRequest(request, |
| 102 headers_as_string.c_str(), |
| 103 headers_as_string.size(), |
| 104 nullptr, // TODO(scottmg): Support body_stream(). |
| 105 0)) { |
| 106 PLOG(ERROR) << "HttpSendRequest"; |
| 107 return false; |
| 108 } |
| 109 DWORD status_code_buffer = 0; |
| 110 DWORD sizeof_status_code_buffer = sizeof(sizeof_status_code_buffer); |
| 111 DWORD query_index = 0; |
| 112 if (!HttpQueryInfo(request, |
| 113 HTTP_QUERY_STATUS_CODE, |
| 114 &status_code_buffer, |
| 115 &sizeof_status_code_buffer, |
| 116 &query_index)) { |
| 117 PLOG(ERROR) << "HttpQueryInfo"; |
| 118 return false; |
| 119 } |
| 120 if (status_code_buffer != 200) { |
| 121 PLOG(ERROR) << base::StringPrintf("HTTP status %ld", status_code_buffer); |
| 122 return false; |
| 123 } |
| 124 |
| 125 char read_buffer[4 << 10]; |
| 126 DWORD bytes_read = 0; |
| 127 while (InternetReadFile( |
| 128 request, &read_buffer, sizeof(read_buffer), &bytes_read)) { |
| 129 if (bytes_read == 0) |
| 130 break; |
| 131 // TODO(scottmg): Do we need the response? |
| 132 } |
| 133 |
| 134 if (!InternetCloseHandle(request)) { |
| 135 PLOG(ERROR) << "InternetCloseHandle(request)"; |
| 136 return false; |
| 137 } |
| 138 if (!InternetCloseHandle(session)) { |
| 139 PLOG(ERROR) << "InternetCloseHandle(request)"; |
| 140 return false; |
| 141 } |
| 142 if (!InternetCloseHandle(internet)) { |
| 143 PLOG(ERROR) << "InternetCloseHandle(request)"; |
| 144 return false; |
| 145 } |
| 146 |
| 147 return true; |
| 148 } |
| 149 |
| 150 } // namespace |
| 151 |
| 152 // static |
| 153 scoped_ptr<HTTPTransport> HTTPTransport::Create() { |
| 154 return scoped_ptr<HTTPTransportWin>(new HTTPTransportWin); |
| 155 } |
| 156 |
| 157 } // namespace crashpad |
| 158 |
OLD | NEW |