Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
stenzek
GitHub Repository: stenzek/duckstation
Path: blob/master/src/duckstation-qt/asynchttprequest.cpp
15701 views
1
// SPDX-FileCopyrightText: 2019-2026 Connor McLaughlin <[email protected]>
2
// SPDX-License-Identifier: CC-BY-NC-ND-4.0
3
4
#include "asynchttprequest.h"
5
6
#include "core/host.h"
7
8
#include "util/http_cache.h"
9
#include "util/http_downloader.h"
10
11
#include "common/error.h"
12
#include "common/log.h"
13
14
#include "moc_asynchttprequest.cpp"
15
16
LOG_CHANNEL(Host);
17
18
AsyncHTTPRequest::AsyncHTTPRequest() : QObject()
19
{
20
}
21
22
AsyncHTTPRequest::~AsyncHTTPRequest() = default;
23
24
void AsyncHTTPRequest::get(std::string url, const void* owner, ProgressCallback* progress /* = nullptr */,
25
HTTPDownloader::HeaderList additional_headers /* = */,
26
std::optional<u16> timeout_seconds /* = */)
27
{
28
HTTPDownloader::CreateRequest(
29
std::move(url), owner,
30
[this](s32 status_code, Error& error, std::string& content_type, HTTPDownloader::RequestData& data) {
31
handleResponse(status_code, error, content_type, data);
32
},
33
progress, additional_headers, timeout_seconds);
34
}
35
36
void AsyncHTTPRequest::post(std::string url, std::string post_data, const void* owner,
37
ProgressCallback* progress /* = nullptr */,
38
HTTPDownloader::HeaderList additional_headers /* = */,
39
std::optional<u16> timeout_seconds /* = */)
40
{
41
HTTPDownloader::CreatePostRequest(
42
std::move(url), std::move(post_data), owner,
43
[this](s32 status_code, Error& error, std::string& content_type, HTTPDownloader::RequestData& data) {
44
handleResponse(status_code, error, content_type, data);
45
},
46
progress, additional_headers, timeout_seconds);
47
}
48
49
ALWAYS_INLINE_RELEASE void AsyncHTTPRequest::handleResponse(s32 status_code, Error& error, std::string& content_type,
50
HTTPDownloader::RequestData& data)
51
{
52
m_status_code = status_code;
53
m_error = std::move(error);
54
m_content_type = std::move(content_type);
55
m_data = std::move(data);
56
QMetaObject::invokeMethod(this, &AsyncHTTPRequest::finishRequest, Qt::QueuedConnection);
57
}
58
59
void AsyncHTTPRequest::finishRequest()
60
{
61
emit requestComplete(m_status_code, m_error, m_content_type, m_data);
62
deleteLater();
63
}
64
65