Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(134)

Side by Side Diff: util/net/http_transport_win.cc

Issue 852213004: win: Add implementation of HTTPTransport based on WinHTTP (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@multiproc-impl
Patch Set: %z fix, now all http_transport_test pass Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « util/net/http_transport_test_server.py ('k') | util/util.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 <winhttp.h>
19
20 #include "base/logging.h"
21 #include "base/scoped_generic.h"
22 #include "base/strings/stringprintf.h"
23 #include "base/strings/utf_string_conversions.h"
24 #include "util/net/http_body.h"
25 #include "package.h"
26
27 namespace crashpad {
28
29 namespace {
30
31 // PLOG doesn't work for messages from WinHTTP, so we need to use
32 // FORMAT_MESSAGE_FROM_HMODULE + the dll name manually here.
33 void LogErrorWinHttpMessage(const char* extra) {
34 DWORD error_code = GetLastError();
35 char msgbuf[256];
36 DWORD flags = FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS |
37 FORMAT_MESSAGE_MAX_WIDTH_MASK | FORMAT_MESSAGE_FROM_HMODULE;
38 DWORD len = FormatMessageA(flags,
39 GetModuleHandle(L"winhttp.dll"),
40 error_code,
41 0,
42 msgbuf,
43 arraysize(msgbuf),
44 NULL);
45 if (len) {
46 LOG(ERROR) << extra << ": " << msgbuf
47 << base::StringPrintf(" (0x%X)", error_code);
48 } else {
49 LOG(ERROR) << base::StringPrintf(
50 "Error (0x%X) while retrieving error. (0x%X)",
51 GetLastError(),
52 error_code);
53 }
54 }
55
56 struct ScopedHINTERNETTraits {
57 static HINTERNET InvalidValue() {
58 return nullptr;
59 }
60 static void Free(HINTERNET handle) {
61 if (handle) {
62 if (!WinHttpCloseHandle(handle)) {
63 LogErrorWinHttpMessage("WinHttpCloseHandle");
64 }
65 }
66 }
67 };
68
69 using ScopedHINTERNET = base::ScopedGeneric<HINTERNET, ScopedHINTERNETTraits>;
70
71 class HTTPTransportWin final : public HTTPTransport {
72 public:
73 HTTPTransportWin();
74 ~HTTPTransportWin() override;
75
76 bool ExecuteSynchronously() override;
77
78 private:
79 DISALLOW_COPY_AND_ASSIGN(HTTPTransportWin);
80 };
81
82 HTTPTransportWin::HTTPTransportWin() : HTTPTransport() {
83 }
84
85 HTTPTransportWin::~HTTPTransportWin() {
86 }
87
88 bool HTTPTransportWin::ExecuteSynchronously() {
89 ScopedHINTERNET session(
90 WinHttpOpen(base::UTF8ToUTF16(PACKAGE_NAME "/" PACKAGE_VERSION).c_str(),
91 WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
92 WINHTTP_NO_PROXY_NAME,
93 WINHTTP_NO_PROXY_BYPASS,
94 0));
95 if (!session.get()) {
96 LogErrorWinHttpMessage("WinHttpOpen");
97 return false;
98 }
99
100 URL_COMPONENTS url_components = {0};
101 url_components.dwStructSize = sizeof(URL_COMPONENTS);
102 url_components.dwHostNameLength = 1;
103 url_components.dwUrlPathLength = 1;
104 url_components.dwExtraInfoLength = 1;
105 std::wstring url_wide(base::UTF8ToUTF16(url()));
106 if (!WinHttpCrackUrl(
107 url_wide.c_str(), 0, ICU_REJECT_USERPWD, &url_components)) {
108 LogErrorWinHttpMessage("WinHttpCrackUrl");
109 return false;
110 }
111 std::wstring host_name(url_components.lpszHostName,
112 url_components.dwHostNameLength);
113 std::wstring url_path(url_components.lpszUrlPath,
114 url_components.dwUrlPathLength);
115 std::wstring extra_info(url_components.lpszExtraInfo,
116 url_components.dwExtraInfoLength);
117
118 ScopedHINTERNET connect(WinHttpConnect(
119 session.get(), host_name.c_str(), url_components.nPort, 0));
120 if (!connect.get()) {
121 LogErrorWinHttpMessage("WinHttpConnect");
122 return false;
123 }
124
125 ScopedHINTERNET request(
126 WinHttpOpenRequest(connect.get(),
127 base::UTF8ToUTF16(method()).c_str(),
128 url_path.c_str(),
129 nullptr,
130 WINHTTP_NO_REFERER,
131 WINHTTP_DEFAULT_ACCEPT_TYPES,
132 0));
133 if (!request.get()) {
134 LogErrorWinHttpMessage("WinHttpOpenRequest");
135 return false;
136 }
137
138 // Add headers to the request.
139 for (const auto& pair : headers()) {
140 std::wstring header_string =
141 base::UTF8ToUTF16(pair.first) + L": " + base::UTF8ToUTF16(pair.second);
142 if (!WinHttpAddRequestHeaders(request.get(),
143 header_string.c_str(),
144 header_string.size(),
145 WINHTTP_ADDREQ_FLAG_ADD)) {
146 LogErrorWinHttpMessage("WinHttpAddRequestHeaders");
147 return false;
148 }
149 }
150
151 // 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.
152 // the interface to not require this, and then use WinHttpWriteData after
153 // WinHttpSendRequest.
154 std::vector<uint8_t> post_data;
155
156 // Write the body of a POST if any.
157 for (;;) {
158 uint8_t buffer[4096];
159 ssize_t bytes_to_write =
160 body_stream()->GetBytesBuffer(buffer, sizeof(buffer));
161 if (bytes_to_write == 0)
162 break;
163 post_data.insert(post_data.end(), buffer, buffer + bytes_to_write);
164 }
165
166 if (!WinHttpSendRequest(request.get(),
167 WINHTTP_NO_ADDITIONAL_HEADERS,
168 0,
169 &post_data[0],
170 post_data.size(),
171 post_data.size(),
172 0)) {
173 LogErrorWinHttpMessage("WinHttpSendRequest");
174 return false;
175 }
176
177 if (!WinHttpReceiveResponse(request.get(), nullptr)) {
178 LogErrorWinHttpMessage("WinHttpReceiveResponse");
179 return false;
180 }
181
182 DWORD status_code = 0;
183 DWORD sizeof_status_code = sizeof(status_code);
184
185 if (!WinHttpQueryHeaders(
186 request.get(),
187 WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER,
188 WINHTTP_HEADER_NAME_BY_INDEX,
189 &status_code,
190 &sizeof_status_code,
191 WINHTTP_NO_HEADER_INDEX)) {
192 LogErrorWinHttpMessage("WinHttpQueryHeaders");
193 return false;
194 }
195
196 if (status_code != 200) {
197 LOG(ERROR) << base::StringPrintf("HTTP status %d", status_code);
198 return false;
199 }
200
201 // TODO(scottmg): Retrieve body of response if necessary with
202 // WinHttpQueryDataAvailable and WinHttpReadData.
203
204 return true;
205 }
206
207 } // namespace
208
209 // static
210 scoped_ptr<HTTPTransport> HTTPTransport::Create() {
211 return scoped_ptr<HTTPTransportWin>(new HTTPTransportWin);
212 }
213
214 } // namespace crashpad
OLDNEW
« no previous file with comments | « util/net/http_transport_test_server.py ('k') | util/util.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698