Files
@ f2a6ba12fc29
Branch filter:
Location: libtransport.git/3rdparty/cpprestsdk/tests/functional/http/utilities/test_http_client.cpp
f2a6ba12fc29
19.0 KiB
text/x-c++hdr
Slack frontend stub
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 | /***
* ==++==
*
* Copyright (c) Microsoft Corporation. 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.
*
* ==--==
* =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
*
* Defines a test client to handle requests and sending responses.
*
* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
****/
#include "stdafx.h"
#include "cpprest/uri.h"
#include "cpprest/details/http_helpers.h"
#include "test_http_client.h"
#ifdef _WIN32
#include <winhttp.h>
#pragma warning ( push )
#pragma warning ( disable : 4457 )
#include <agents.h>
#pragma warning ( pop )
#endif
using namespace web; using namespace utility;
namespace tests { namespace functional { namespace http { namespace utilities {
// Flatten the http_headers into a name:value pairs separated by a carriage return and line feed.
utility::string_t flatten_http_headers(const std::map<utility::string_t, utility::string_t> &headers)
{
utility::string_t flattened_headers;
for(auto iter = headers.begin(); iter != headers.end(); ++iter)
{
utility::string_t temp((*iter).first + U(":") + (*iter).second + U("\r\n"));
flattened_headers.append(utility::string_t(temp.begin(), temp.end()));
}
return flattened_headers;
}
#ifdef _WIN32
// Helper function to query for the size of header values.
static void query_header_length(HINTERNET request_handle, DWORD header, DWORD &length)
{
WinHttpQueryHeaders(
request_handle,
header,
WINHTTP_HEADER_NAME_BY_INDEX,
WINHTTP_NO_OUTPUT_BUFFER,
&length,
WINHTTP_NO_HEADER_INDEX);
}
// Helper function to get the status code from a WinHTTP response.
static void parse_status_code(HINTERNET request_handle, unsigned short &code)
{
DWORD length = 0;
query_header_length(request_handle, WINHTTP_QUERY_STATUS_CODE, length);
utility::string_t buffer;
buffer.resize(length);
WinHttpQueryHeaders(
request_handle,
WINHTTP_QUERY_STATUS_CODE,
WINHTTP_HEADER_NAME_BY_INDEX,
&buffer[0],
&length,
WINHTTP_NO_HEADER_INDEX);
code = (unsigned short)_wtoi(buffer.c_str());
}
// Helper function to trim leading and trailing null characters from a string.
static void trim_nulls(utility::string_t &str)
{
size_t index;
for(index = 0; index < str.size() && str[index] == 0; ++index);
str.erase(0, index);
index;
for(index = str.size(); index > 0 && str[index - 1] == 0; --index);
str.erase(index);
}
// Helper function to get the reason phrase from a WinHTTP response.
static void parse_reason_phrase(HINTERNET request_handle, utility::string_t &phrase)
{
DWORD length = 0;
query_header_length(request_handle, WINHTTP_QUERY_STATUS_TEXT, length);
phrase.resize(length);
WinHttpQueryHeaders(
request_handle,
WINHTTP_QUERY_STATUS_TEXT,
WINHTTP_HEADER_NAME_BY_INDEX,
&phrase[0],
&length,
WINHTTP_NO_HEADER_INDEX);
// WinHTTP reports back the wrong length, trim any null characters.
trim_nulls(phrase);
}
/// <summary>
/// Parses a string containing Http headers.
/// </summary>
static void parse_winhttp_headers(HINTERNET request_handle, utf16char *headersStr, test_response *p_response)
{
// Status code and reason phrase.
parse_status_code(request_handle, p_response->m_status_code);
parse_reason_phrase(request_handle, p_response->m_reason_phrase);
utf16char *context = nullptr;
utf16char *line = wcstok_s(headersStr, U("\r\n"), &context);
while(line != nullptr)
{
const utility::string_t header_line(line);
const size_t colonIndex = header_line.find_first_of(U(":"));
if(colonIndex != utility::string_t::npos)
{
utility::string_t key = header_line.substr(0, colonIndex);
utility::string_t value = header_line.substr(colonIndex + 1, header_line.length() - colonIndex - 1);
web::http::details::trim_whitespace(key);
web::http::details::trim_whitespace(value);
p_response->m_headers[key] = value;
}
line = wcstok_s(nullptr, U("\r\n"), &context);
}
}
class _test_http_client
{
public:
_test_http_client(const utility::string_t &uri)
: m_uri(uri), m_hSession(nullptr), m_hConnection(nullptr)
{}
unsigned long open()
{
// Open session.
m_hSession = WinHttpOpen(
U("test_http_client"),
WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
WINHTTP_NO_PROXY_NAME,
WINHTTP_NO_PROXY_BYPASS,
WINHTTP_FLAG_ASYNC);
if(!m_hSession)
{
return GetLastError();
}
// Set timeouts.
int multiplier=10;
if(!WinHttpSetTimeouts(m_hSession, 60000*multiplier, 60000*multiplier, 30000*multiplier, 30000*multiplier))
{
return GetLastError();
}
// Set max connection to use per server to 1.
DWORD maxConnections = 1;
if(!WinHttpSetOption(m_hSession, WINHTTP_OPTION_MAX_CONNS_PER_SERVER, &maxConnections, sizeof(maxConnections)))
{
return GetLastError();
}
// Register asynchronous callback.
if(WINHTTP_INVALID_STATUS_CALLBACK == WinHttpSetStatusCallback(
m_hSession,
&_test_http_client::completion_callback,
WINHTTP_CALLBACK_FLAG_ALL_COMPLETIONS,
0))
{
return GetLastError();
}
// Open connection.
::http::uri u(m_uri);
unsigned int port = u.is_port_default() ? INTERNET_DEFAULT_PORT : u.port();
m_hConnection = WinHttpConnect(
m_hSession,
u.host().c_str(),
(INTERNET_PORT)port,
0);
if(m_hConnection == nullptr)
{
return GetLastError();
}
return 0;
}
unsigned long close()
{
// Release memory for each request.
std::for_each(m_responses_memory.begin(), m_responses_memory.end(), [](test_response *p_response)
{
delete p_response;
});
if(m_hConnection != nullptr)
{
if(WinHttpCloseHandle(m_hConnection) == NULL)
{
return GetLastError();
}
}
if(m_hSession != nullptr)
{
// Unregister the callback.
if(!WinHttpSetStatusCallback(
m_hSession,
NULL,
WINHTTP_CALLBACK_FLAG_ALL_NOTIFICATIONS,
NULL))
{
return GetLastError();
}
if(WinHttpCloseHandle(m_hSession) == NULL)
{
return GetLastError();
}
}
return 0;
}
unsigned long request(
const utility::string_t &method,
const utility::string_t &path,
const std::map<utility::string_t, utility::string_t> headers,
void * data,
size_t data_length)
{
HINTERNET request_handle = WinHttpOpenRequest(
m_hConnection,
method.c_str(),
path.c_str(),
NULL,
WINHTTP_NO_REFERER,
WINHTTP_DEFAULT_ACCEPT_TYPES,
0);
if(request_handle == nullptr)
{
return GetLastError();
}
// Add headers.
if(!headers.empty())
{
utility::string_t flattened_headers = flatten_http_headers(headers);
if(!WinHttpAddRequestHeaders(
request_handle,
flattened_headers.c_str(),
(DWORD)flattened_headers.length(),
WINHTTP_ADDREQ_FLAG_ADD))
{
return GetLastError();
}
}
if(!WinHttpSendRequest(
request_handle,
WINHTTP_NO_ADDITIONAL_HEADERS,
0,
data,
(DWORD)data_length,
(DWORD)data_length,
(DWORD_PTR)new test_response(this)))
{
return GetLastError();
}
return 0;
}
test_response * wait_for_response()
{
return wait_for_responses(1)[0];
}
pplx::task<test_response *> next_response()
{
return pplx::create_task([this]() -> test_response *
{
return wait_for_response();
});
}
std::vector<test_response *> wait_for_responses(const size_t count)
{
std::vector<test_response *> m_test_responses;
for(size_t i = 0; i < count; ++i)
{
m_test_responses.push_back(Concurrency::receive(m_responses));
}
return m_test_responses;
}
std::vector<pplx::task<test_response *>> next_responses(const size_t count)
{
std::vector<pplx::task_completion_event<test_response *>> events;
std::vector<pplx::task<test_response *>> responses;
for(size_t i = 0; i < count; ++i)
{
events.push_back(pplx::task_completion_event<test_response *>());
responses.push_back(pplx::create_task(events[i]));
}
pplx::create_task([this, count, events]()
{
for(size_t i = 0; i < count; ++i)
{
events[i].set(wait_for_response());
}
});
return responses;
}
private:
// WinHTTP callback.
static void CALLBACK completion_callback(
HINTERNET hRequestHandle,
DWORD_PTR context,
DWORD statusCode,
void* statusInfo,
DWORD)
{
test_response * p_response = reinterpret_cast<test_response *>(context);
if(p_response != nullptr)
{
if(statusCode == WINHTTP_CALLBACK_STATUS_REQUEST_ERROR)
{
WINHTTP_ASYNC_RESULT *pStatusInfo = static_cast<WINHTTP_ASYNC_RESULT *>(statusInfo);
pStatusInfo;
throw std::exception("Error in WinHTTP callback");
}
else if(statusCode == WINHTTP_CALLBACK_STATUS_SENDREQUEST_COMPLETE)
{
if(!WinHttpReceiveResponse(hRequestHandle, NULL))
{
throw std::exception("Error receiving response");
}
}
else if(statusCode == WINHTTP_CALLBACK_STATUS_HEADERS_AVAILABLE)
{
DWORD headers_length;
WinHttpQueryHeaders(
hRequestHandle,
WINHTTP_QUERY_RAW_HEADERS_CRLF,
WINHTTP_HEADER_NAME_BY_INDEX,
WINHTTP_NO_OUTPUT_BUFFER,
&headers_length,
WINHTTP_NO_HEADER_INDEX);
// Now allocate buffer for headers and query for them.
std::vector<unsigned char> header_raw_buffer;
header_raw_buffer.resize(headers_length);
utf16char * header_buffer = reinterpret_cast<utf16char *>(&header_raw_buffer[0]);
if(!WinHttpQueryHeaders(
hRequestHandle,
WINHTTP_QUERY_RAW_HEADERS_CRLF,
WINHTTP_HEADER_NAME_BY_INDEX,
header_buffer,
&headers_length,
WINHTTP_NO_HEADER_INDEX))
{
throw std::exception("Error querying for headers");
}
parse_winhttp_headers(hRequestHandle, header_buffer, p_response);
// Check to see if the response has a body or not.
if(!WinHttpQueryDataAvailable(hRequestHandle, nullptr))
{
throw std::exception("Error reading response body");
}
}
else if(statusCode == WINHTTP_CALLBACK_STATUS_DATA_AVAILABLE)
{
DWORD num_bytes = *(PDWORD)statusInfo;
if ( num_bytes > 0 )
{
size_t current_size = p_response->m_data.size();
p_response->m_data.resize(current_size+(size_t)num_bytes);
// Actual WinHTTP call to read in body.
if(!WinHttpReadData(
hRequestHandle,
&p_response->m_data[current_size],
(DWORD)num_bytes,
NULL))
{
throw std::exception("Error reading response body");
}
}
else
{
WinHttpCloseHandle(hRequestHandle);
p_response->m_client->m_responses_memory.push_back(p_response);
Concurrency::asend(p_response->m_client->m_responses, p_response);
}
}
else if(statusCode == WINHTTP_CALLBACK_STATUS_READ_COMPLETE)
{
if(!WinHttpQueryDataAvailable(hRequestHandle, nullptr))
{
throw std::exception("Error reading response body");
}
}
}
}
Concurrency::unbounded_buffer<test_response *> m_responses;
// Used to store all requests to simplify memory management.
std::vector<test_response *> m_responses_memory;
const utility::string_t m_uri;
HINTERNET m_hSession;
HINTERNET m_hConnection;
};
#else
class _test_http_client
{
private:
const web::http::uri m_uri;
typename web::http::client::http_client m_client;
std::vector<pplx::task<web::http::http_response>> m_responses;
std::vector<test_response*> m_test_responses;
public:
_test_http_client(utility::string_t uri) : m_uri(web::http::uri::encode_uri(uri)), m_client(m_uri.authority()) {}
unsigned long open() { return 0; }
unsigned long close() { return 0; }
unsigned long request(
const utility::string_t &method,
const utility::string_t &path,
const std::map<utility::string_t, utility::string_t>& headers,
void * data,
size_t data_length)
{
auto localHeaders = headers;
localHeaders["User-Agent"] = "test_http_client";
web::http::http_request request;
request.set_method(method);
request.set_request_uri(web::http::uri_builder(m_uri).append_path(path).to_uri());
auto& hDest = request.headers();
for (auto it = localHeaders.begin(); it != localHeaders.end(); ++it)
{
auto& currentValue = hDest[it->first];
if (currentValue.empty())
currentValue = it->second;
else
currentValue = currentValue + U(", ") + it->second;
}
request.set_body(utility::string_t(reinterpret_cast<const char*>(data), data_length));
m_responses.push_back(m_client.request(request));
return 0;
}
test_response * wait_for_response()
{
return wait_for_responses(1)[0];
}
pplx::task<test_response *> next_response()
{
return pplx::create_task([this]() -> test_response *
{
return wait_for_response();
});
}
std::vector<test_response *> wait_for_responses(const size_t count)
{
if (count > m_responses.size())
throw std::logic_error("count too big");
std::vector<test_response *> m_test_responses;
for(size_t i = 0; i < count; ++i)
{
auto response = m_responses[0].get();
auto tr = new test_response(this);
tr->m_status_code = response.status_code();
tr->m_reason_phrase = response.reason_phrase();
for (auto it = response.headers().begin(); it != response.headers().end(); ++it)
{
tr->m_headers[it->first] = it->second;
}
tr->m_data = response.extract_vector().get();
m_test_responses.push_back(tr);
m_responses.erase(m_responses.begin());
}
return m_test_responses;
}
std::vector<pplx::task<test_response *>> next_responses(const size_t count)
{
std::vector<pplx::task<test_response *>> result;
for (size_t i = 0; i < count; ++i)
{
result.push_back(next_response());
}
return result;
}
};
#endif
test_http_client::test_http_client(const web::http::uri &uri)
{
m_impl = std::unique_ptr<_test_http_client>(new _test_http_client(uri.to_string()));
}
test_http_client::~test_http_client()
{
}
test_http_client::test_http_client(test_http_client &&other) : m_impl(std::move(other.m_impl)) {}
test_http_client & test_http_client::operator=(test_http_client &&other)
{
if(this != &other)
{
this->m_impl = std::move(other.m_impl);
}
return *this;
}
unsigned long test_http_client::open() { return m_impl->open(); }
unsigned long test_http_client::close() { return m_impl->close(); }
unsigned long test_http_client::request(const utility::string_t &method, const utility::string_t &path)
{
return request(method, path, std::map<utility::string_t, utility::string_t>());
}
unsigned long test_http_client::request(
const utility::string_t &method,
const utility::string_t &path,
const std::map<utility::string_t, utility::string_t> &headers)
{
return request(method, path, headers, std::string());
}
unsigned long test_http_client::request(
const utility::string_t &method,
const utility::string_t &path,
const std::string &data)
{
return request(method, path, std::map<utility::string_t, utility::string_t>(), data);
}
unsigned long test_http_client::request(
const utility::string_t &method,
const utility::string_t &path,
const utility::string_t &content_type,
const std::string &data)
{
std::map<utility::string_t, utility::string_t> headers;
headers[U("Content-Type")] = content_type;
return request(method, path, headers, data);
}
unsigned long test_http_client::request(
const utility::string_t &method,
const utility::string_t &path,
const std::map<utility::string_t, utility::string_t> &headers,
const std::string &data)
{
return m_impl->request(method, path, headers, (void *)&data[0], data.size());
}
test_response * test_http_client::wait_for_response() { return m_impl->wait_for_response(); }
pplx::task<test_response *> test_http_client::next_response() { return m_impl->next_response(); }
std::vector<test_response *> test_http_client::wait_for_responses(const size_t count) { return m_impl->wait_for_responses(count); }
std::vector<pplx::task<test_response *>> test_http_client::next_responses(const size_t count) { return m_impl->next_responses(count); }
}}}}
|