MCPcopy Create free account
hub / github.com/OpenRCT2/OpenRCT2 / Do

Function Do

src/openrct2/core/Http.WinHttp.cpp:157–245  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

155 }
156
157 Response Do(const Request& req)
158 {
159 HINTERNET hSession{}, hConnect{}, hRequest{};
160 try
161 {
162 URL_COMPONENTS url{};
163 url.dwStructSize = sizeof(url);
164 url.dwSchemeLength = static_cast<DWORD>(-1);
165 url.dwHostNameLength = static_cast<DWORD>(-1);
166 url.dwUrlPathLength = static_cast<DWORD>(-1);
167 url.dwExtraInfoLength = static_cast<DWORD>(-1);
168
169 auto wUrl = String::toWideChar(req.url);
170 if (!WinHttpCrackUrl(wUrl.c_str(), 0, 0, &url))
171 throw std::invalid_argument("Unable to parse URI.");
172
173 auto userAgent = String::toWideChar(kOpenRCT2UserAgent);
174 hSession = WinHttpOpen(
175 userAgent.c_str(), WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
176 if (hSession == nullptr)
177 ThrowWin32Exception("WinHttpOpen");
178
179 auto wHostName = std::wstring(url.lpszHostName, url.dwHostNameLength);
180 hConnect = WinHttpConnect(hSession, wHostName.c_str(), url.nPort, 0);
181 if (hConnect == nullptr)
182 ThrowWin32Exception("WinHttpConnect");
183
184 auto dwFlags = 0;
185 if (lstrcmpiW(std::wstring(url.lpszScheme, url.dwSchemeLength).c_str(), L"https") == 0)
186 {
187 dwFlags = WINHTTP_FLAG_SECURE;
188 }
189
190 auto wVerb = GetVerb(req.method);
191 auto wQuery = std::wstring(url.lpszUrlPath, url.dwUrlPathLength);
192 hRequest = WinHttpOpenRequest(
193 hConnect, wVerb, wQuery.c_str(), NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, dwFlags);
194 if (hRequest == nullptr)
195 ThrowWin32Exception("WinHttpOpenRequest");
196
197 for (auto header : req.header)
198 {
199 auto fullHeader = String::toWideChar(header.first) + L": " + String::toWideChar(header.second);
200 if (!WinHttpAddRequestHeaders(hRequest, fullHeader.c_str(), static_cast<ULONG>(-1L), WINHTTP_ADDREQ_FLAG_ADD))
201 ThrowWin32Exception("WinHttpAddRequestHeaders");
202 }
203
204 auto reqBody = reinterpret_cast<LPVOID>(const_cast<char*>(req.body.data()));
205 auto reqBodyLen = static_cast<DWORD>(req.body.size());
206 if (!WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, reqBody, reqBodyLen, reqBodyLen, 0))
207 ThrowWin32Exception("WinHttpSendRequest");
208
209 if (!WinHttpReceiveResponse(hRequest, NULL))
210 ThrowWin32Exception("WinHttpReceiveResponse");
211
212 auto statusCode = ReadStatusCode(hRequest);
213 auto headers = ReadHeaders(hRequest);
214 auto body = ReadBody(hRequest);

Callers

nothing calls this directly

Calls 12

toWideCharFunction · 0.85
ThrowWin32ExceptionFunction · 0.85
GetVerbFunction · 0.85
ReadStatusCodeFunction · 0.85
ReadHeadersFunction · 0.85
ReadBodyFunction · 0.85
WriteLineFunction · 0.85
findMethod · 0.80
endMethod · 0.65
dataMethod · 0.45
sizeMethod · 0.45
whatMethod · 0.45

Tested by

no test coverage detected