Apply suggestions
This commit is contained in:
parent
bf5c707a88
commit
5fb455425c
@ -63,25 +63,16 @@ const ResultCode ERROR_WRONG_CERT_HANDLE = // 0xD8A0A0C9
|
||||
const ResultCode ERROR_CERT_ALREADY_SET = // 0xD8A0A03D
|
||||
ResultCode(61, ErrorModule::HTTP, ErrorSummary::InvalidState, ErrorLevel::Permanent);
|
||||
|
||||
static ssize_t write_headers(httplib::Stream& strm,
|
||||
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;
|
||||
}
|
||||
struct URLInfo {
|
||||
bool is_https;
|
||||
std::string host;
|
||||
int port;
|
||||
std::string path;
|
||||
};
|
||||
|
||||
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 auto scheme_end = url.find(prefix);
|
||||
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);
|
||||
std::string host;
|
||||
int port = -1;
|
||||
constexpr int default_http_port = 80;
|
||||
constexpr int default_https_port = 443;
|
||||
std::string path;
|
||||
if (path_index == std::string::npos) {
|
||||
// If no path is specified after the host, set it to "/"
|
||||
@ -121,11 +114,79 @@ static std::tuple<std::string, int, std::string, bool> SplitUrl(const std::strin
|
||||
path = url.substr(path_index);
|
||||
}
|
||||
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};
|
||||
}
|
||||
|
||||
static ssize_t WriteHeaders(httplib::Stream& strm,
|
||||
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 size_t HeaderWriteHandler(std::vector<Context::RequestHeader>& pending_headers,
|
||||
httplib::Stream& strm, httplib::Headers& httplib_headers) {
|
||||
std::vector<Context::RequestHeader> final_headers;
|
||||
std::vector<Context::RequestHeader>::iterator it_p;
|
||||
httplib::Headers::iterator it_h;
|
||||
|
||||
auto find_pending_header = [&pending_headers](const std::string& str) {
|
||||
return std::find_if(pending_headers.begin(), pending_headers.end(),
|
||||
[&str](Context::RequestHeader& rh) { return rh.name == str; });
|
||||
};
|
||||
|
||||
// First: Host
|
||||
it_p = find_pending_header("Host");
|
||||
if (it_p != pending_headers.end()) {
|
||||
final_headers.push_back(Context::RequestHeader(it_p->name, it_p->value));
|
||||
pending_headers.erase(it_p);
|
||||
} else {
|
||||
it_h = httplib_headers.find("Host");
|
||||
if (it_h != httplib_headers.end()) {
|
||||
final_headers.push_back(Context::RequestHeader(it_h->first, it_h->second));
|
||||
}
|
||||
}
|
||||
|
||||
// Second: Content-Type (optional, ignore httplib)
|
||||
it_p = find_pending_header("Content-Type");
|
||||
if (it_p != pending_headers.end()) {
|
||||
final_headers.push_back(Context::RequestHeader(it_p->name, it_p->value));
|
||||
pending_headers.erase(it_p);
|
||||
}
|
||||
|
||||
// Third: Content-Length
|
||||
it_p = find_pending_header("Content-Length");
|
||||
if (it_p != pending_headers.end()) {
|
||||
final_headers.push_back(Context::RequestHeader(it_p->name, it_p->value));
|
||||
pending_headers.erase(it_p);
|
||||
} else {
|
||||
it_h = httplib_headers.find("Content-Length");
|
||||
if (it_h != httplib_headers.end()) {
|
||||
final_headers.push_back(Context::RequestHeader(it_h->first, it_h->second));
|
||||
}
|
||||
}
|
||||
|
||||
// Finally, user defined headers
|
||||
for (const auto& header : pending_headers) {
|
||||
final_headers.push_back(header);
|
||||
}
|
||||
|
||||
return WriteHeaders(strm, final_headers);
|
||||
};
|
||||
|
||||
void Context::MakeRequest() {
|
||||
ASSERT(state == RequestState::NotStarted);
|
||||
|
||||
@ -136,13 +197,13 @@ void Context::MakeRequest() {
|
||||
{RequestMethod::PutEmpty, "PUT"},
|
||||
};
|
||||
|
||||
const auto& [host, port, path, is_https] = SplitUrl(url.c_str());
|
||||
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 = path;
|
||||
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
|
||||
@ -151,55 +212,10 @@ void Context::MakeRequest() {
|
||||
return true;
|
||||
};
|
||||
|
||||
// Watch out for header ordering!
|
||||
// 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>::iterator it_p;
|
||||
httplib::Headers::iterator it_h;
|
||||
|
||||
auto find_pending_header = [&pending_headers](const std::string& str) {
|
||||
return std::find_if(pending_headers.begin(), pending_headers.end(),
|
||||
[&str](Context::RequestHeader& rh) { return rh.name == str; });
|
||||
};
|
||||
|
||||
// First: Host
|
||||
it_p = find_pending_header("Host");
|
||||
if (it_p != pending_headers.end()) {
|
||||
final_headers.push_back(Context::RequestHeader(it_p->name, it_p->value));
|
||||
pending_headers.erase(it_p);
|
||||
} else {
|
||||
it_h = httplib_headers.find("Host");
|
||||
if (it_h != httplib_headers.end()) {
|
||||
final_headers.push_back(Context::RequestHeader(it_h->first, it_h->second));
|
||||
}
|
||||
}
|
||||
|
||||
// Second: Content-Type (optional, ignore httplib)
|
||||
it_p = find_pending_header("Content-Type");
|
||||
if (it_p != pending_headers.end()) {
|
||||
final_headers.push_back(Context::RequestHeader(it_p->name, it_p->value));
|
||||
pending_headers.erase(it_p);
|
||||
}
|
||||
|
||||
// Third: Content-Length
|
||||
it_p = find_pending_header("Content-Length");
|
||||
if (it_p != pending_headers.end()) {
|
||||
final_headers.push_back(Context::RequestHeader(it_p->name, it_p->value));
|
||||
pending_headers.erase(it_p);
|
||||
} else {
|
||||
it_h = httplib_headers.find("Content-Length");
|
||||
if (it_h != httplib_headers.end()) {
|
||||
final_headers.push_back(Context::RequestHeader(it_h->first, it_h->second));
|
||||
}
|
||||
}
|
||||
|
||||
// Finally, user defined headers
|
||||
for (const auto& header : pending_headers) {
|
||||
final_headers.push_back(header);
|
||||
}
|
||||
|
||||
return write_headers(strm, final_headers);
|
||||
return HeaderWriteHandler(pending_headers, strm, httplib_headers);
|
||||
};
|
||||
|
||||
if (!post_data.empty()) {
|
||||
@ -220,7 +236,7 @@ void Context::MakeRequest() {
|
||||
state = RequestState::InProgress;
|
||||
|
||||
// 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;
|
||||
EVP_PKEY* key = nullptr;
|
||||
{
|
||||
@ -231,18 +247,18 @@ void Context::MakeRequest() {
|
||||
cert = d2i_X509(nullptr, &tmpCertPtr, (long)clcert_data->certificate.size());
|
||||
key = d2i_PrivateKey(EVP_PKEY_RSA, nullptr, &tmpKeyPtr,
|
||||
(long)clcert_data->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 if (auto client_cert = ssl_config.client_cert_ctx.lock()) {
|
||||
const unsigned char* tmpCertPtr = client_cert->certificate.data();
|
||||
const unsigned char* tmpKeyPtr = client_cert->private_key.data();
|
||||
cert = d2i_X509(nullptr, &tmpCertPtr, (long)client_cert->certificate.size());
|
||||
key = d2i_PrivateKey(EVP_PKEY_RSA, nullptr, &tmpKeyPtr,
|
||||
(long)client_cert->private_key.size());
|
||||
client =
|
||||
std::make_unique<httplib::SSLClient>(url_info.host, url_info.port, cert, key);
|
||||
} else {
|
||||
if (auto client_cert = ssl_config.client_cert_ctx.lock()) {
|
||||
const unsigned char* tmpCertPtr = client_cert->certificate.data();
|
||||
const unsigned char* tmpKeyPtr = client_cert->private_key.data();
|
||||
cert = d2i_X509(nullptr, &tmpCertPtr, (long)client_cert->certificate.size());
|
||||
key = d2i_PrivateKey(EVP_PKEY_RSA, nullptr, &tmpKeyPtr,
|
||||
(long)client_cert->private_key.size());
|
||||
client = std::make_unique<httplib::SSLClient>(host, port, cert, key);
|
||||
} 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
|
||||
@ -268,7 +284,8 @@ void Context::MakeRequest() {
|
||||
EVP_PKEY_free(key);
|
||||
}
|
||||
} 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);
|
||||
|
||||
@ -358,11 +375,11 @@ void HTTP_C::BeginRequest(Kernel::HLERequestContext& ctx) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto itr = contexts.find(context_handle);
|
||||
ASSERT(itr != contexts.end());
|
||||
Context& http_context = GetContext(context_handle);
|
||||
|
||||
// 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);
|
||||
rb.Push(ERROR_STATE_ERROR);
|
||||
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
|
||||
// For now make every request async in it's own thread.
|
||||
|
||||
itr->second.request_future =
|
||||
std::async(std::launch::async, &Context::MakeRequest, std::ref(itr->second));
|
||||
itr->second.current_copied_data = 0;
|
||||
http_context.request_future =
|
||||
std::async(std::launch::async, &Context::MakeRequest, std::ref(http_context));
|
||||
http_context.current_copied_data = 0;
|
||||
|
||||
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
|
||||
rb.Push(RESULT_SUCCESS);
|
||||
@ -393,11 +410,11 @@ void HTTP_C::BeginRequestAsync(Kernel::HLERequestContext& ctx) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto itr = contexts.find(context_handle);
|
||||
ASSERT(itr != contexts.end());
|
||||
Context& http_context = GetContext(context_handle);
|
||||
|
||||
// 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);
|
||||
rb.Push(ERROR_STATE_ERROR);
|
||||
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
|
||||
// For now make every request async in it's own thread.
|
||||
|
||||
itr->second.request_future =
|
||||
std::async(std::launch::async, &Context::MakeRequest, std::ref(itr->second));
|
||||
itr->second.current_copied_data = 0;
|
||||
http_context.request_future =
|
||||
std::async(std::launch::async, &Context::MakeRequest, std::ref(http_context));
|
||||
http_context.current_copied_data = 0;
|
||||
|
||||
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
|
||||
rb.Push(RESULT_SUCCESS);
|
||||
@ -462,10 +479,7 @@ void HTTP_C::ReceiveDataImpl(Kernel::HLERequestContext& ctx, bool timeout) {
|
||||
|
||||
ctx.RunAsync(
|
||||
[async_data](Kernel::HLERequestContext& ctx) {
|
||||
auto itr = async_data->own->contexts.find(async_data->context_handle);
|
||||
ASSERT(itr != async_data->own->contexts.end());
|
||||
|
||||
Context& http_context = itr->second;
|
||||
Context& http_context = async_data->own->GetContext(async_data->context_handle);
|
||||
|
||||
if (async_data->timeout) {
|
||||
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);
|
||||
return;
|
||||
}
|
||||
auto itr = async_data->own->contexts.find(async_data->context_handle);
|
||||
ASSERT(itr != async_data->own->contexts.end());
|
||||
|
||||
Context& http_context = itr->second;
|
||||
Context& http_context = async_data->own->GetContext(async_data->context_handle);
|
||||
|
||||
size_t remaining_data =
|
||||
http_context.response.body.size() - http_context.current_copied_data;
|
||||
@ -638,8 +649,7 @@ void HTTP_C::CancelConnection(Kernel::HLERequestContext& ctx) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto itr = contexts.find(context_handle);
|
||||
ASSERT(itr != contexts.end());
|
||||
[[maybe_unused]] Context& http_context = GetContext(context_handle);
|
||||
|
||||
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
|
||||
rb.Push(RESULT_SUCCESS);
|
||||
@ -667,8 +677,7 @@ void HTTP_C::AddRequestHeader(Kernel::HLERequestContext& ctx) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto itr = contexts.find(context_handle);
|
||||
ASSERT(itr != contexts.end());
|
||||
[[maybe_unused]] Context& http_context = GetContext(context_handle);
|
||||
|
||||
if (itr->second.state != RequestState::NotStarted) {
|
||||
LOG_ERROR(Service_HTTP,
|
||||
@ -709,10 +718,9 @@ void HTTP_C::AddPostDataAscii(Kernel::HLERequestContext& ctx) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto itr = contexts.find(context_handle);
|
||||
ASSERT(itr != contexts.end());
|
||||
Context& http_context = GetContext(context_handle);
|
||||
|
||||
if (itr->second.state != RequestState::NotStarted) {
|
||||
if (http_context.state != RequestState::NotStarted) {
|
||||
LOG_ERROR(Service_HTTP,
|
||||
"Tried to add post data on a context that has already been started.");
|
||||
IPC::RequestBuilder rb = rp.MakeBuilder(1, 2);
|
||||
@ -722,7 +730,7 @@ void HTTP_C::AddPostDataAscii(Kernel::HLERequestContext& ctx) {
|
||||
return;
|
||||
}
|
||||
|
||||
itr->second.post_data.emplace(name, value);
|
||||
http_context.post_data.emplace(name, value);
|
||||
|
||||
IPC::RequestBuilder rb = rp.MakeBuilder(1, 2);
|
||||
rb.Push(RESULT_SUCCESS);
|
||||
@ -741,10 +749,9 @@ void HTTP_C::AddPostDataRaw(Kernel::HLERequestContext& ctx) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto itr = contexts.find(context_handle);
|
||||
ASSERT(itr != contexts.end());
|
||||
Context& http_context = GetContext(context_handle);
|
||||
|
||||
if (itr->second.state != RequestState::NotStarted) {
|
||||
if (http_context.state != RequestState::NotStarted) {
|
||||
LOG_ERROR(Service_HTTP,
|
||||
"Tried to add post data on a context that has already been started.");
|
||||
IPC::RequestBuilder rb = rp.MakeBuilder(1, 2);
|
||||
@ -754,8 +761,8 @@ void HTTP_C::AddPostDataRaw(Kernel::HLERequestContext& ctx) {
|
||||
return;
|
||||
}
|
||||
|
||||
itr->second.post_data_raw.resize(buffer.GetSize());
|
||||
buffer.Read(itr->second.post_data_raw.data(), 0, buffer.GetSize());
|
||||
http_context.post_data_raw.resize(buffer.GetSize());
|
||||
buffer.Read(http_context.post_data_raw.data(), 0, buffer.GetSize());
|
||||
|
||||
IPC::RequestBuilder rb = rp.MakeBuilder(1, 2);
|
||||
rb.Push(RESULT_SUCCESS);
|
||||
@ -770,7 +777,7 @@ void HTTP_C::GetResponseHeader(Kernel::HLERequestContext& ctx) {
|
||||
u32 context_handle;
|
||||
u32 name_len;
|
||||
u32 value_max_len;
|
||||
const std::vector<u8>* header_name;
|
||||
std::span<const u8> header_name;
|
||||
Kernel::MappedBuffer* value_buffer;
|
||||
};
|
||||
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->name_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();
|
||||
|
||||
if (!PerformStateChecks(ctx, rp, async_data->context_handle)) {
|
||||
@ -788,24 +795,21 @@ void HTTP_C::GetResponseHeader(Kernel::HLERequestContext& ctx) {
|
||||
|
||||
ctx.RunAsync(
|
||||
[async_data](Kernel::HLERequestContext& ctx) {
|
||||
auto itr = async_data->own->contexts.find(async_data->context_handle);
|
||||
ASSERT(itr != async_data->own->contexts.end());
|
||||
|
||||
itr->second.request_future.wait();
|
||||
Context& http_context = async_data->own->GetContext(async_data->context_handle);
|
||||
http_context.request_future.wait();
|
||||
return 0;
|
||||
},
|
||||
[async_data](Kernel::HLERequestContext& ctx) {
|
||||
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);
|
||||
while (header_name_str.size() && header_name_str.back() == '\0') {
|
||||
header_name_str.pop_back();
|
||||
}
|
||||
|
||||
auto itr = async_data->own->contexts.find(async_data->context_handle);
|
||||
ASSERT(itr != async_data->own->contexts.end());
|
||||
Context& http_context = async_data->own->GetContext(async_data->context_handle);
|
||||
|
||||
auto& headers = itr->second.response.headers;
|
||||
auto& headers = http_context.response.headers;
|
||||
u32 copied_size = 0;
|
||||
|
||||
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(
|
||||
[async_data](Kernel::HLERequestContext& ctx) {
|
||||
auto itr = async_data->own->contexts.find(async_data->context_handle);
|
||||
ASSERT(itr != async_data->own->contexts.end());
|
||||
Context& http_context = async_data->own->GetContext(async_data->context_handle);
|
||||
|
||||
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));
|
||||
if (wait_res == std::future_status::timeout) {
|
||||
LOG_DEBUG(Service_HTTP, "Status code: {}", "timeout");
|
||||
@ -887,7 +890,7 @@ void HTTP_C::GetResponseStatusCodeImpl(Kernel::HLERequestContext& ctx, bool time
|
||||
ErrorLevel::Permanent);
|
||||
}
|
||||
} else {
|
||||
itr->second.request_future.wait();
|
||||
http_context.request_future.wait();
|
||||
}
|
||||
return 0;
|
||||
},
|
||||
@ -899,10 +902,9 @@ void HTTP_C::GetResponseStatusCodeImpl(Kernel::HLERequestContext& ctx, bool time
|
||||
return;
|
||||
}
|
||||
|
||||
auto itr = async_data->own->contexts.find(async_data->context_handle);
|
||||
ASSERT(itr != async_data->own->contexts.end());
|
||||
Context& http_context = async_data->own->GetContext(async_data->context_handle);
|
||||
|
||||
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);
|
||||
|
||||
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) {
|
||||
IPC::RequestParser rp(ctx);
|
||||
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);
|
||||
|
||||
@ -948,17 +950,16 @@ void HTTP_C::SetDefaultClientCert(Kernel::HLERequestContext& ctx) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto itr = contexts.find(context_handle);
|
||||
ASSERT(itr != contexts.end());
|
||||
Context& http_context = GetContext(context_handle);
|
||||
|
||||
if (client_cert_id != 0x40) {
|
||||
if (client_cert_id != ClientCertID::Default) {
|
||||
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
|
||||
rb.Push(ERROR_WRONG_CERT_ID);
|
||||
return;
|
||||
}
|
||||
|
||||
itr->second.uses_default_client_cert = true;
|
||||
itr->second.clcert_data = &GetClCertA();
|
||||
http_context.uses_default_client_cert = true;
|
||||
http_context.clcert_data = &GetClCertA();
|
||||
|
||||
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
|
||||
rb.Push(RESULT_SUCCESS);
|
||||
@ -976,8 +977,7 @@ void HTTP_C::SetClientCertContext(Kernel::HLERequestContext& ctx) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto http_context_itr = contexts.find(context_handle);
|
||||
ASSERT(http_context_itr != contexts.end());
|
||||
Context& http_context = GetContext(context_handle);
|
||||
|
||||
auto cert_context_itr = client_certs.find(client_cert_handle);
|
||||
if (cert_context_itr == client_certs.end()) {
|
||||
@ -987,7 +987,7 @@ void HTTP_C::SetClientCertContext(Kernel::HLERequestContext& ctx) {
|
||||
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,
|
||||
"Tried to set a client cert to a context that already has a client cert");
|
||||
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
|
||||
@ -995,7 +995,7 @@ void HTTP_C::SetClientCertContext(Kernel::HLERequestContext& ctx) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (http_context_itr->second.state != RequestState::NotStarted) {
|
||||
if (http_context.state != RequestState::NotStarted) {
|
||||
LOG_ERROR(Service_HTTP,
|
||||
"Tried to set a client cert on a context that has already been started.");
|
||||
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
|
||||
@ -1004,7 +1004,7 @@ void HTTP_C::SetClientCertContext(Kernel::HLERequestContext& ctx) {
|
||||
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);
|
||||
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);
|
||||
|
||||
auto http_context_itr = contexts.find(context_handle);
|
||||
ASSERT(http_context_itr != contexts.end());
|
||||
[[maybe_unused]] Context& http_context = GetContext(context_handle);
|
||||
|
||||
IPC::RequestBuilder rb = rp.MakeBuilder(2, 0);
|
||||
rb.Push(RESULT_SUCCESS);
|
||||
@ -1103,7 +1102,7 @@ void HTTP_C::OpenDefaultClientCertContext(Kernel::HLERequestContext& ctx) {
|
||||
return;
|
||||
}
|
||||
|
||||
constexpr u8 default_cert_id = 0x40;
|
||||
constexpr u8 default_cert_id = static_cast<u8>(ClientCertID::Default);
|
||||
if (cert_id != default_cert_id) {
|
||||
LOG_ERROR(Service_HTTP, "called with invalid cert_id {}", cert_id);
|
||||
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
|
||||
@ -1210,28 +1209,27 @@ void HTTP_C::GetDownloadSizeState(Kernel::HLERequestContext& ctx) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto itr = contexts.find(context_handle);
|
||||
ASSERT(itr != contexts.end());
|
||||
Context& http_context = GetContext(context_handle);
|
||||
|
||||
// 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
|
||||
// length if the download is complete and 0 otherwise.
|
||||
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;
|
||||
if (is_complete) {
|
||||
const auto& headers = itr->second.response.headers;
|
||||
const auto& headers = http_context.response.headers;
|
||||
const auto& it = headers.find("Content-Length");
|
||||
if (it != headers.end()) {
|
||||
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);
|
||||
IPC::RequestBuilder rb = rp.MakeBuilder(3, 0);
|
||||
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);
|
||||
}
|
||||
|
||||
|
@ -57,6 +57,10 @@ enum class RequestState : u8 {
|
||||
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.
|
||||
/// There can only be at most one client certificate context attached to an HTTP context at any
|
||||
/// given time.
|
||||
@ -595,6 +599,13 @@ private:
|
||||
/// Global list of HTTP contexts currently opened.
|
||||
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.
|
||||
std::unordered_map<ClientCertContext::Handle, std::shared_ptr<ClientCertContext>> client_certs;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user