Apply suggestions

This commit is contained in:
PabloMK7 2023-10-04 21:59:47 +02:00
parent bf5c707a88
commit 5fb455425c
2 changed files with 161 additions and 152 deletions

View File

@ -63,25 +63,16 @@ const ResultCode ERROR_WRONG_CERT_HANDLE = // 0xD8A0A0C9
const ResultCode ERROR_CERT_ALREADY_SET = // 0xD8A0A03D const ResultCode ERROR_CERT_ALREADY_SET = // 0xD8A0A03D
ResultCode(61, ErrorModule::HTTP, ErrorSummary::InvalidState, ErrorLevel::Permanent); ResultCode(61, ErrorModule::HTTP, ErrorSummary::InvalidState, ErrorLevel::Permanent);
static ssize_t write_headers(httplib::Stream& strm, struct URLInfo {
const std::vector<Context::RequestHeader>& headers) { bool is_https;
ssize_t write_len = 0; std::string host;
for (const auto& x : headers) { int port;
auto len = strm.write_format("%s: %s\r\n", x.name.c_str(), x.value.c_str()); std::string path;
if (len < 0) { };
return len;
}
write_len += len;
}
auto len = strm.write("\r\n");
if (len < 0) {
return len;
}
write_len += len;
return write_len;
}
static std::tuple<std::string, int, std::string, bool> SplitUrl(const std::string& url) { // Splits URL into its components. Example: https://citra-emu.org:443/index.html
// is_https: true; host: citra-emu.org; port: 443; path: /index.html
static URLInfo SplitUrl(const std::string& url) {
const std::string prefix = "://"; const std::string prefix = "://";
const auto scheme_end = url.find(prefix); const auto scheme_end = url.find(prefix);
const auto prefix_end = scheme_end == std::string::npos ? 0 : scheme_end + prefix.length(); const auto prefix_end = scheme_end == std::string::npos ? 0 : scheme_end + prefix.length();
@ -91,6 +82,8 @@ static std::tuple<std::string, int, std::string, bool> SplitUrl(const std::strin
const auto path_index = url.find("/", prefix_end); const auto path_index = url.find("/", prefix_end);
std::string host; std::string host;
int port = -1; int port = -1;
constexpr int default_http_port = 80;
constexpr int default_https_port = 443;
std::string path; std::string path;
if (path_index == std::string::npos) { if (path_index == std::string::npos) {
// If no path is specified after the host, set it to "/" // If no path is specified after the host, set it to "/"
@ -121,39 +114,31 @@ static std::tuple<std::string, int, std::string, bool> SplitUrl(const std::strin
path = url.substr(path_index); path = url.substr(path_index);
} }
if (port == -1) { if (port == -1) {
port = is_https ? 443 : 80; port = is_https ? default_https_port : default_http_port;
} }
return std::make_tuple(host, port, path, is_https); return URLInfo{.is_https = is_https, .host = host, .port = port, .path = path};
} }
void Context::MakeRequest() { static ssize_t WriteHeaders(httplib::Stream& strm,
ASSERT(state == RequestState::NotStarted); const std::vector<Context::RequestHeader>& headers) {
ssize_t write_len = 0;
for (const auto& x : headers) {
auto len = strm.write_format("%s: %s\r\n", x.name.c_str(), x.value.c_str());
if (len < 0) {
return len;
}
write_len += len;
}
auto len = strm.write("\r\n");
if (len < 0) {
return len;
}
write_len += len;
return write_len;
}
static const std::unordered_map<RequestMethod, std::string> request_method_strings{ static size_t HeaderWriteHandler(std::vector<Context::RequestHeader>& pending_headers,
{RequestMethod::Get, "GET"}, {RequestMethod::Post, "POST"}, httplib::Stream& strm, httplib::Headers& httplib_headers) {
{RequestMethod::Head, "HEAD"}, {RequestMethod::Put, "PUT"},
{RequestMethod::Delete, "DELETE"}, {RequestMethod::PostEmpty, "POST"},
{RequestMethod::PutEmpty, "PUT"},
};
const auto& [host, port, path, is_https] = SplitUrl(url.c_str());
httplib::Request request;
httplib::Error error{-1};
std::vector<Context::RequestHeader> pending_headers;
request.method = request_method_strings.at(method);
request.path = path;
request.progress = [this](u64 current, u64 total) -> bool {
// TODO(B3N30): Is there a state that shows response header are available
current_download_size_bytes = current;
total_download_size_bytes = total;
return true;
};
// Watch out for header ordering!
auto header_writter = [&pending_headers](httplib::Stream& strm,
httplib::Headers& httplib_headers) {
std::vector<Context::RequestHeader> final_headers; std::vector<Context::RequestHeader> final_headers;
std::vector<Context::RequestHeader>::iterator it_p; std::vector<Context::RequestHeader>::iterator it_p;
httplib::Headers::iterator it_h; httplib::Headers::iterator it_h;
@ -199,7 +184,38 @@ void Context::MakeRequest() {
final_headers.push_back(header); final_headers.push_back(header);
} }
return write_headers(strm, final_headers); return WriteHeaders(strm, final_headers);
};
void Context::MakeRequest() {
ASSERT(state == RequestState::NotStarted);
static const std::unordered_map<RequestMethod, std::string> request_method_strings{
{RequestMethod::Get, "GET"}, {RequestMethod::Post, "POST"},
{RequestMethod::Head, "HEAD"}, {RequestMethod::Put, "PUT"},
{RequestMethod::Delete, "DELETE"}, {RequestMethod::PostEmpty, "POST"},
{RequestMethod::PutEmpty, "PUT"},
};
URLInfo url_info = SplitUrl(url.c_str());
httplib::Request request;
httplib::Error error{-1};
std::vector<Context::RequestHeader> pending_headers;
request.method = request_method_strings.at(method);
request.path = url_info.path;
request.progress = [this](u64 current, u64 total) -> bool {
// TODO(B3N30): Is there a state that shows response header are available
current_download_size_bytes = current;
total_download_size_bytes = total;
return true;
};
// Watch out for header ordering!!
auto header_writter = [&pending_headers](httplib::Stream& strm,
httplib::Headers& httplib_headers) {
return HeaderWriteHandler(pending_headers, strm, httplib_headers);
}; };
if (!post_data.empty()) { if (!post_data.empty()) {
@ -220,7 +236,7 @@ void Context::MakeRequest() {
state = RequestState::InProgress; state = RequestState::InProgress;
// Sadly, we have to duplicate code, the class hierarchy in httplib is not very useful... // Sadly, we have to duplicate code, the class hierarchy in httplib is not very useful...
if (is_https) { if (url_info.is_https) {
X509* cert = nullptr; X509* cert = nullptr;
EVP_PKEY* key = nullptr; EVP_PKEY* key = nullptr;
{ {
@ -231,18 +247,18 @@ void Context::MakeRequest() {
cert = d2i_X509(nullptr, &tmpCertPtr, (long)clcert_data->certificate.size()); cert = d2i_X509(nullptr, &tmpCertPtr, (long)clcert_data->certificate.size());
key = d2i_PrivateKey(EVP_PKEY_RSA, nullptr, &tmpKeyPtr, key = d2i_PrivateKey(EVP_PKEY_RSA, nullptr, &tmpKeyPtr,
(long)clcert_data->private_key.size()); (long)clcert_data->private_key.size());
client = std::make_unique<httplib::SSLClient>(host, port, cert, key); client =
} else { std::make_unique<httplib::SSLClient>(url_info.host, url_info.port, cert, key);
if (auto client_cert = ssl_config.client_cert_ctx.lock()) { } else if (auto client_cert = ssl_config.client_cert_ctx.lock()) {
const unsigned char* tmpCertPtr = client_cert->certificate.data(); const unsigned char* tmpCertPtr = client_cert->certificate.data();
const unsigned char* tmpKeyPtr = client_cert->private_key.data(); const unsigned char* tmpKeyPtr = client_cert->private_key.data();
cert = d2i_X509(nullptr, &tmpCertPtr, (long)client_cert->certificate.size()); cert = d2i_X509(nullptr, &tmpCertPtr, (long)client_cert->certificate.size());
key = d2i_PrivateKey(EVP_PKEY_RSA, nullptr, &tmpKeyPtr, key = d2i_PrivateKey(EVP_PKEY_RSA, nullptr, &tmpKeyPtr,
(long)client_cert->private_key.size()); (long)client_cert->private_key.size());
client = std::make_unique<httplib::SSLClient>(host, port, cert, key); client =
std::make_unique<httplib::SSLClient>(url_info.host, url_info.port, cert, key);
} else { } else {
client = std::make_unique<httplib::SSLClient>(host, port); client = std::make_unique<httplib::SSLClient>(url_info.host, url_info.port);
}
} }
// TODO(B3N30): Check for SSLOptions-Bits and set the verify method accordingly // TODO(B3N30): Check for SSLOptions-Bits and set the verify method accordingly
@ -268,7 +284,8 @@ void Context::MakeRequest() {
EVP_PKEY_free(key); EVP_PKEY_free(key);
} }
} else { } else {
std::unique_ptr<httplib::Client> client = std::make_unique<httplib::Client>(host, port); std::unique_ptr<httplib::Client> client =
std::make_unique<httplib::Client>(url_info.host, url_info.port);
client->set_header_writer(header_writter); client->set_header_writer(header_writter);
@ -358,11 +375,11 @@ void HTTP_C::BeginRequest(Kernel::HLERequestContext& ctx) {
return; return;
} }
auto itr = contexts.find(context_handle); Context& http_context = GetContext(context_handle);
ASSERT(itr != contexts.end());
// This should never happen in real hardware, but can happen on citra. // This should never happen in real hardware, but can happen on citra.
if (itr->second.uses_default_client_cert && !itr->second.clcert_data->init) { if (http_context.uses_default_client_cert && !http_context.clcert_data->init) {
LOG_ERROR(Service_HTTP, "failed to begin HTTP request: client cert not found.");
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0); IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(ERROR_STATE_ERROR); rb.Push(ERROR_STATE_ERROR);
return; return;
@ -375,9 +392,9 @@ void HTTP_C::BeginRequest(Kernel::HLERequestContext& ctx) {
// Then there are 3? worker threads that pop the requests from the queue and send them // Then there are 3? worker threads that pop the requests from the queue and send them
// For now make every request async in it's own thread. // For now make every request async in it's own thread.
itr->second.request_future = http_context.request_future =
std::async(std::launch::async, &Context::MakeRequest, std::ref(itr->second)); std::async(std::launch::async, &Context::MakeRequest, std::ref(http_context));
itr->second.current_copied_data = 0; http_context.current_copied_data = 0;
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0); IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(RESULT_SUCCESS); rb.Push(RESULT_SUCCESS);
@ -393,11 +410,11 @@ void HTTP_C::BeginRequestAsync(Kernel::HLERequestContext& ctx) {
return; return;
} }
auto itr = contexts.find(context_handle); Context& http_context = GetContext(context_handle);
ASSERT(itr != contexts.end());
// This should never happen in real hardware, but can happen on citra. // This should never happen in real hardware, but can happen on citra.
if (itr->second.uses_default_client_cert && !itr->second.clcert_data->init) { if (http_context.uses_default_client_cert && !http_context.clcert_data->init) {
LOG_ERROR(Service_HTTP, "failed to begin HTTP request: client cert not found.");
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0); IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(ERROR_STATE_ERROR); rb.Push(ERROR_STATE_ERROR);
return; return;
@ -410,9 +427,9 @@ void HTTP_C::BeginRequestAsync(Kernel::HLERequestContext& ctx) {
// Then there are 3? worker threads that pop the requests from the queue and send them // Then there are 3? worker threads that pop the requests from the queue and send them
// For now make every request async in it's own thread. // For now make every request async in it's own thread.
itr->second.request_future = http_context.request_future =
std::async(std::launch::async, &Context::MakeRequest, std::ref(itr->second)); std::async(std::launch::async, &Context::MakeRequest, std::ref(http_context));
itr->second.current_copied_data = 0; http_context.current_copied_data = 0;
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0); IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(RESULT_SUCCESS); rb.Push(RESULT_SUCCESS);
@ -462,10 +479,7 @@ void HTTP_C::ReceiveDataImpl(Kernel::HLERequestContext& ctx, bool timeout) {
ctx.RunAsync( ctx.RunAsync(
[async_data](Kernel::HLERequestContext& ctx) { [async_data](Kernel::HLERequestContext& ctx) {
auto itr = async_data->own->contexts.find(async_data->context_handle); Context& http_context = async_data->own->GetContext(async_data->context_handle);
ASSERT(itr != async_data->own->contexts.end());
Context& http_context = itr->second;
if (async_data->timeout) { if (async_data->timeout) {
auto wait_res = http_context.request_future.wait_for( auto wait_res = http_context.request_future.wait_for(
@ -488,10 +502,7 @@ void HTTP_C::ReceiveDataImpl(Kernel::HLERequestContext& ctx, bool timeout) {
rb.Push(async_data->async_res); rb.Push(async_data->async_res);
return; return;
} }
auto itr = async_data->own->contexts.find(async_data->context_handle); Context& http_context = async_data->own->GetContext(async_data->context_handle);
ASSERT(itr != async_data->own->contexts.end());
Context& http_context = itr->second;
size_t remaining_data = size_t remaining_data =
http_context.response.body.size() - http_context.current_copied_data; http_context.response.body.size() - http_context.current_copied_data;
@ -638,8 +649,7 @@ void HTTP_C::CancelConnection(Kernel::HLERequestContext& ctx) {
return; return;
} }
auto itr = contexts.find(context_handle); [[maybe_unused]] Context& http_context = GetContext(context_handle);
ASSERT(itr != contexts.end());
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0); IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(RESULT_SUCCESS); rb.Push(RESULT_SUCCESS);
@ -667,8 +677,7 @@ void HTTP_C::AddRequestHeader(Kernel::HLERequestContext& ctx) {
return; return;
} }
auto itr = contexts.find(context_handle); [[maybe_unused]] Context& http_context = GetContext(context_handle);
ASSERT(itr != contexts.end());
if (itr->second.state != RequestState::NotStarted) { if (itr->second.state != RequestState::NotStarted) {
LOG_ERROR(Service_HTTP, LOG_ERROR(Service_HTTP,
@ -709,10 +718,9 @@ void HTTP_C::AddPostDataAscii(Kernel::HLERequestContext& ctx) {
return; return;
} }
auto itr = contexts.find(context_handle); Context& http_context = GetContext(context_handle);
ASSERT(itr != contexts.end());
if (itr->second.state != RequestState::NotStarted) { if (http_context.state != RequestState::NotStarted) {
LOG_ERROR(Service_HTTP, LOG_ERROR(Service_HTTP,
"Tried to add post data on a context that has already been started."); "Tried to add post data on a context that has already been started.");
IPC::RequestBuilder rb = rp.MakeBuilder(1, 2); IPC::RequestBuilder rb = rp.MakeBuilder(1, 2);
@ -722,7 +730,7 @@ void HTTP_C::AddPostDataAscii(Kernel::HLERequestContext& ctx) {
return; return;
} }
itr->second.post_data.emplace(name, value); http_context.post_data.emplace(name, value);
IPC::RequestBuilder rb = rp.MakeBuilder(1, 2); IPC::RequestBuilder rb = rp.MakeBuilder(1, 2);
rb.Push(RESULT_SUCCESS); rb.Push(RESULT_SUCCESS);
@ -741,10 +749,9 @@ void HTTP_C::AddPostDataRaw(Kernel::HLERequestContext& ctx) {
return; return;
} }
auto itr = contexts.find(context_handle); Context& http_context = GetContext(context_handle);
ASSERT(itr != contexts.end());
if (itr->second.state != RequestState::NotStarted) { if (http_context.state != RequestState::NotStarted) {
LOG_ERROR(Service_HTTP, LOG_ERROR(Service_HTTP,
"Tried to add post data on a context that has already been started."); "Tried to add post data on a context that has already been started.");
IPC::RequestBuilder rb = rp.MakeBuilder(1, 2); IPC::RequestBuilder rb = rp.MakeBuilder(1, 2);
@ -754,8 +761,8 @@ void HTTP_C::AddPostDataRaw(Kernel::HLERequestContext& ctx) {
return; return;
} }
itr->second.post_data_raw.resize(buffer.GetSize()); http_context.post_data_raw.resize(buffer.GetSize());
buffer.Read(itr->second.post_data_raw.data(), 0, buffer.GetSize()); buffer.Read(http_context.post_data_raw.data(), 0, buffer.GetSize());
IPC::RequestBuilder rb = rp.MakeBuilder(1, 2); IPC::RequestBuilder rb = rp.MakeBuilder(1, 2);
rb.Push(RESULT_SUCCESS); rb.Push(RESULT_SUCCESS);
@ -770,7 +777,7 @@ void HTTP_C::GetResponseHeader(Kernel::HLERequestContext& ctx) {
u32 context_handle; u32 context_handle;
u32 name_len; u32 name_len;
u32 value_max_len; u32 value_max_len;
const std::vector<u8>* header_name; std::span<const u8> header_name;
Kernel::MappedBuffer* value_buffer; Kernel::MappedBuffer* value_buffer;
}; };
std::shared_ptr<AsyncData> async_data = std::make_shared<AsyncData>(); std::shared_ptr<AsyncData> async_data = std::make_shared<AsyncData>();
@ -779,7 +786,7 @@ void HTTP_C::GetResponseHeader(Kernel::HLERequestContext& ctx) {
async_data->context_handle = rp.Pop<u32>(); async_data->context_handle = rp.Pop<u32>();
async_data->name_len = rp.Pop<u32>(); async_data->name_len = rp.Pop<u32>();
async_data->value_max_len = rp.Pop<u32>(); async_data->value_max_len = rp.Pop<u32>();
async_data->header_name = &rp.PopStaticBuffer(); async_data->header_name = std::span<const u8>(rp.PopStaticBuffer());
async_data->value_buffer = &rp.PopMappedBuffer(); async_data->value_buffer = &rp.PopMappedBuffer();
if (!PerformStateChecks(ctx, rp, async_data->context_handle)) { if (!PerformStateChecks(ctx, rp, async_data->context_handle)) {
@ -788,24 +795,21 @@ void HTTP_C::GetResponseHeader(Kernel::HLERequestContext& ctx) {
ctx.RunAsync( ctx.RunAsync(
[async_data](Kernel::HLERequestContext& ctx) { [async_data](Kernel::HLERequestContext& ctx) {
auto itr = async_data->own->contexts.find(async_data->context_handle); Context& http_context = async_data->own->GetContext(async_data->context_handle);
ASSERT(itr != async_data->own->contexts.end()); http_context.request_future.wait();
itr->second.request_future.wait();
return 0; return 0;
}, },
[async_data](Kernel::HLERequestContext& ctx) { [async_data](Kernel::HLERequestContext& ctx) {
std::string header_name_str( std::string header_name_str(
reinterpret_cast<const char*>(async_data->header_name->data()), reinterpret_cast<const char*>(async_data->header_name.data()),
async_data->name_len); async_data->name_len);
while (header_name_str.size() && header_name_str.back() == '\0') { while (header_name_str.size() && header_name_str.back() == '\0') {
header_name_str.pop_back(); header_name_str.pop_back();
} }
auto itr = async_data->own->contexts.find(async_data->context_handle); Context& http_context = async_data->own->GetContext(async_data->context_handle);
ASSERT(itr != async_data->own->contexts.end());
auto& headers = itr->second.response.headers; auto& headers = http_context.response.headers;
u32 copied_size = 0; u32 copied_size = 0;
LOG_DEBUG(Service_HTTP, "header={}, max_len={}", header_name_str, LOG_DEBUG(Service_HTTP, "header={}, max_len={}", header_name_str,
@ -874,11 +878,10 @@ void HTTP_C::GetResponseStatusCodeImpl(Kernel::HLERequestContext& ctx, bool time
ctx.RunAsync( ctx.RunAsync(
[async_data](Kernel::HLERequestContext& ctx) { [async_data](Kernel::HLERequestContext& ctx) {
auto itr = async_data->own->contexts.find(async_data->context_handle); Context& http_context = async_data->own->GetContext(async_data->context_handle);
ASSERT(itr != async_data->own->contexts.end());
if (async_data->timeout) { if (async_data->timeout) {
auto wait_res = itr->second.request_future.wait_for( auto wait_res = http_context.request_future.wait_for(
std::chrono::nanoseconds(async_data->timeout_nanos)); std::chrono::nanoseconds(async_data->timeout_nanos));
if (wait_res == std::future_status::timeout) { if (wait_res == std::future_status::timeout) {
LOG_DEBUG(Service_HTTP, "Status code: {}", "timeout"); LOG_DEBUG(Service_HTTP, "Status code: {}", "timeout");
@ -887,7 +890,7 @@ void HTTP_C::GetResponseStatusCodeImpl(Kernel::HLERequestContext& ctx, bool time
ErrorLevel::Permanent); ErrorLevel::Permanent);
} }
} else { } else {
itr->second.request_future.wait(); http_context.request_future.wait();
} }
return 0; return 0;
}, },
@ -899,10 +902,9 @@ void HTTP_C::GetResponseStatusCodeImpl(Kernel::HLERequestContext& ctx, bool time
return; return;
} }
auto itr = async_data->own->contexts.find(async_data->context_handle); Context& http_context = async_data->own->GetContext(async_data->context_handle);
ASSERT(itr != async_data->own->contexts.end());
const u32 response_code = itr->second.response.status; const u32 response_code = http_context.response.status;
LOG_DEBUG(Service_HTTP, "Status code: {}, response_code={}", "good", response_code); LOG_DEBUG(Service_HTTP, "Status code: {}, response_code={}", "good", response_code);
IPC::RequestBuilder rb(ctx, static_cast<u16>(ctx.CommandHeader().command_id.Value()), 2, IPC::RequestBuilder rb(ctx, static_cast<u16>(ctx.CommandHeader().command_id.Value()), 2,
@ -940,7 +942,7 @@ void HTTP_C::AddDefaultCert(Kernel::HLERequestContext& ctx) {
void HTTP_C::SetDefaultClientCert(Kernel::HLERequestContext& ctx) { void HTTP_C::SetDefaultClientCert(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx); IPC::RequestParser rp(ctx);
const Context::Handle context_handle = rp.Pop<u32>(); const Context::Handle context_handle = rp.Pop<u32>();
const u32 client_cert_id = rp.Pop<u32>(); const ClientCertID client_cert_id = rp.Pop<ClientCertID>();
LOG_DEBUG(Service_HTTP, "client_cert_id={}", client_cert_id); LOG_DEBUG(Service_HTTP, "client_cert_id={}", client_cert_id);
@ -948,17 +950,16 @@ void HTTP_C::SetDefaultClientCert(Kernel::HLERequestContext& ctx) {
return; return;
} }
auto itr = contexts.find(context_handle); Context& http_context = GetContext(context_handle);
ASSERT(itr != contexts.end());
if (client_cert_id != 0x40) { if (client_cert_id != ClientCertID::Default) {
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0); IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(ERROR_WRONG_CERT_ID); rb.Push(ERROR_WRONG_CERT_ID);
return; return;
} }
itr->second.uses_default_client_cert = true; http_context.uses_default_client_cert = true;
itr->second.clcert_data = &GetClCertA(); http_context.clcert_data = &GetClCertA();
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0); IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(RESULT_SUCCESS); rb.Push(RESULT_SUCCESS);
@ -976,8 +977,7 @@ void HTTP_C::SetClientCertContext(Kernel::HLERequestContext& ctx) {
return; return;
} }
auto http_context_itr = contexts.find(context_handle); Context& http_context = GetContext(context_handle);
ASSERT(http_context_itr != contexts.end());
auto cert_context_itr = client_certs.find(client_cert_handle); auto cert_context_itr = client_certs.find(client_cert_handle);
if (cert_context_itr == client_certs.end()) { if (cert_context_itr == client_certs.end()) {
@ -987,7 +987,7 @@ void HTTP_C::SetClientCertContext(Kernel::HLERequestContext& ctx) {
return; return;
} }
if (http_context_itr->second.ssl_config.client_cert_ctx.lock()) { if (http_context.ssl_config.client_cert_ctx.lock()) {
LOG_ERROR(Service_HTTP, LOG_ERROR(Service_HTTP,
"Tried to set a client cert to a context that already has a client cert"); "Tried to set a client cert to a context that already has a client cert");
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0); IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
@ -995,7 +995,7 @@ void HTTP_C::SetClientCertContext(Kernel::HLERequestContext& ctx) {
return; return;
} }
if (http_context_itr->second.state != RequestState::NotStarted) { if (http_context.state != RequestState::NotStarted) {
LOG_ERROR(Service_HTTP, LOG_ERROR(Service_HTTP,
"Tried to set a client cert on a context that has already been started."); "Tried to set a client cert on a context that has already been started.");
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0); IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
@ -1004,7 +1004,7 @@ void HTTP_C::SetClientCertContext(Kernel::HLERequestContext& ctx) {
return; return;
} }
http_context_itr->second.ssl_config.client_cert_ctx = cert_context_itr->second; http_context.ssl_config.client_cert_ctx = cert_context_itr->second;
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0); IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(RESULT_SUCCESS); rb.Push(RESULT_SUCCESS);
} }
@ -1016,8 +1016,7 @@ void HTTP_C::GetSSLError(Kernel::HLERequestContext& ctx) {
LOG_WARNING(Service_HTTP, "(STUBBED) called, context_handle={}, unk={}", context_handle, unk); LOG_WARNING(Service_HTTP, "(STUBBED) called, context_handle={}, unk={}", context_handle, unk);
auto http_context_itr = contexts.find(context_handle); [[maybe_unused]] Context& http_context = GetContext(context_handle);
ASSERT(http_context_itr != contexts.end());
IPC::RequestBuilder rb = rp.MakeBuilder(2, 0); IPC::RequestBuilder rb = rp.MakeBuilder(2, 0);
rb.Push(RESULT_SUCCESS); rb.Push(RESULT_SUCCESS);
@ -1103,7 +1102,7 @@ void HTTP_C::OpenDefaultClientCertContext(Kernel::HLERequestContext& ctx) {
return; return;
} }
constexpr u8 default_cert_id = 0x40; constexpr u8 default_cert_id = static_cast<u8>(ClientCertID::Default);
if (cert_id != default_cert_id) { if (cert_id != default_cert_id) {
LOG_ERROR(Service_HTTP, "called with invalid cert_id {}", cert_id); LOG_ERROR(Service_HTTP, "called with invalid cert_id {}", cert_id);
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0); IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
@ -1210,28 +1209,27 @@ void HTTP_C::GetDownloadSizeState(Kernel::HLERequestContext& ctx) {
return; return;
} }
auto itr = contexts.find(context_handle); Context& http_context = GetContext(context_handle);
ASSERT(itr != contexts.end());
// On the real console, the current downloaded progress and the total size of the content gets // On the real console, the current downloaded progress and the total size of the content gets
// returned. Since we do not support chunked downloads on the host, always return the content // returned. Since we do not support chunked downloads on the host, always return the content
// length if the download is complete and 0 otherwise. // length if the download is complete and 0 otherwise.
u32 content_length = 0; u32 content_length = 0;
const bool is_complete = itr->second.request_future.wait_for(std::chrono::milliseconds(0)) == const bool is_complete = http_context.request_future.wait_for(std::chrono::milliseconds(0)) ==
std::future_status::ready; std::future_status::ready;
if (is_complete) { if (is_complete) {
const auto& headers = itr->second.response.headers; const auto& headers = http_context.response.headers;
const auto& it = headers.find("Content-Length"); const auto& it = headers.find("Content-Length");
if (it != headers.end()) { if (it != headers.end()) {
content_length = std::stoi(it->second); content_length = std::stoi(it->second);
} }
} }
LOG_DEBUG(Service_HTTP, "current={}, total={}", itr->second.current_copied_data, LOG_DEBUG(Service_HTTP, "current={}, total={}", http_context.current_copied_data,
content_length); content_length);
IPC::RequestBuilder rb = rp.MakeBuilder(3, 0); IPC::RequestBuilder rb = rp.MakeBuilder(3, 0);
rb.Push(RESULT_SUCCESS); rb.Push(RESULT_SUCCESS);
rb.Push(static_cast<u32>(itr->second.current_copied_data)); rb.Push(static_cast<u32>(http_context.current_copied_data));
rb.Push(content_length); rb.Push(content_length);
} }

View File

@ -57,6 +57,10 @@ enum class RequestState : u8 {
TimedOut = 0xA, // Request timed out? TimedOut = 0xA, // Request timed out?
}; };
enum class ClientCertID : u32 {
Default = 0x40, // Default client cert
};
/// Represents a client certificate along with its private key, stored as a byte array of DER data. /// Represents a client certificate along with its private key, stored as a byte array of DER data.
/// There can only be at most one client certificate context attached to an HTTP context at any /// There can only be at most one client certificate context attached to an HTTP context at any
/// given time. /// given time.
@ -595,6 +599,13 @@ private:
/// Global list of HTTP contexts currently opened. /// Global list of HTTP contexts currently opened.
std::unordered_map<Context::Handle, Context> contexts; std::unordered_map<Context::Handle, Context> contexts;
// Get context from its handle
inline Context& GetContext(const Context::Handle& handle) {
auto it = contexts.find(handle);
ASSERT(it != contexts.end());
return it->second;
}
/// Global list of ClientCert contexts currently opened. /// Global list of ClientCert contexts currently opened.
std::unordered_map<ClientCertContext::Handle, std::shared_ptr<ClientCertContext>> client_certs; std::unordered_map<ClientCertContext::Handle, std::shared_ptr<ClientCertContext>> client_certs;