Skip to content

Commit

Permalink
More http code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
hrydgard committed Jan 23, 2025
1 parent 12adad0 commit 493367c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 36 deletions.
44 changes: 15 additions & 29 deletions Common/Net/HTTPRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,24 @@ Request::Request(RequestMethod method, std::string_view url, std::string_view na
};
}

bool RequestManager::IsHttpsUrl(const std::string &url) {
static bool IsHttpsUrl(std::string_view url) {
return startsWith(url, "https:");
}

std::shared_ptr<Request> RequestManager::StartDownload(const std::string &url, const Path &outfile, ProgressBarMode mode, const char *acceptMime) {
std::shared_ptr<Request> dl;
std::shared_ptr<Request> CreateRequest(RequestMethod method, std::string_view url, std::string_view postdata, std::string_view postMime, const Path &outfile, ProgressBarMode mode, std::string_view name) {
if (IsHttpsUrl(url) && System_GetPropertyBool(SYSPROP_SUPPORTS_HTTPS)) {
#ifndef HTTPS_NOT_AVAILABLE
dl.reset(new HTTPSRequest(RequestMethod::GET, url, "", "", outfile, mode));
return std::shared_ptr<Request>(new HTTPSRequest(method, url, postdata, postMime, outfile, mode, name));
#else
return std::shared_ptr<Request>();
#endif
} else {
dl.reset(new HTTPRequest(RequestMethod::GET, url, "", "", outfile, mode));
return std::shared_ptr<Request>(new HTTPRequest(method, url, postdata, postMime, outfile, mode, name));
}
}

std::shared_ptr<Request> RequestManager::StartDownload(std::string_view url, const Path &outfile, ProgressBarMode mode, const char *acceptMime) {
std::shared_ptr<Request> dl = CreateRequest(RequestMethod::GET, url, "", "", outfile, mode, "");

if (!userAgent_.empty())
dl->SetUserAgent(userAgent_);
Expand All @@ -61,22 +64,14 @@ std::shared_ptr<Request> RequestManager::StartDownload(const std::string &url, c
}

std::shared_ptr<Request> RequestManager::StartDownloadWithCallback(
const std::string &url,
std::string_view url,
const Path &outfile,
ProgressBarMode mode,
std::function<void(Request &)> callback,
std::string_view name,
const char *acceptMime) {
std::shared_ptr<Request> dl;
if (IsHttpsUrl(url) && System_GetPropertyBool(SYSPROP_SUPPORTS_HTTPS)) {
#ifndef HTTPS_NOT_AVAILABLE
dl.reset(new HTTPSRequest(RequestMethod::GET, url, "", "", outfile, mode, name));
#else
return std::shared_ptr<Request>();
#endif
} else {
dl.reset(new HTTPRequest(RequestMethod::GET, url, "", "", outfile, mode, name));
}
std::shared_ptr<Request> dl = CreateRequest(RequestMethod::GET, url, "", "", outfile, mode, name);

if (!userAgent_.empty())
dl->SetUserAgent(userAgent_);
if (acceptMime)
Expand All @@ -88,22 +83,13 @@ std::shared_ptr<Request> RequestManager::StartDownloadWithCallback(
}

std::shared_ptr<Request> RequestManager::AsyncPostWithCallback(
const std::string &url,
const std::string &postData,
const std::string &postMime,
std::string_view url,
std::string_view postData,
std::string_view postMime,
ProgressBarMode mode,
std::function<void(Request &)> callback,
std::string_view name) {
std::shared_ptr<Request> dl;
if (IsHttpsUrl(url) && System_GetPropertyBool(SYSPROP_SUPPORTS_HTTPS)) {
#ifndef HTTPS_NOT_AVAILABLE
dl.reset(new HTTPSRequest(RequestMethod::POST, url, postData, postMime, Path(), mode, name));
#else
return std::shared_ptr<Request>();
#endif
} else {
dl.reset(new HTTPRequest(RequestMethod::POST, url, postData, postMime, Path(), mode, name));
}
std::shared_ptr<Request> dl = CreateRequest(RequestMethod::POST, url, postData, postMime, Path(), mode, name);
if (!userAgent_.empty())
dl->SetUserAgent(userAgent_);
dl->SetCallback(callback);
Expand Down
12 changes: 5 additions & 7 deletions Common/Net/HTTPRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,20 +95,20 @@ class RequestManager {
CancelAll();
}

std::shared_ptr<Request> StartDownload(const std::string &url, const Path &outfile, ProgressBarMode mode, const char *acceptMime = nullptr);
std::shared_ptr<Request> StartDownload(std::string_view url, const Path &outfile, ProgressBarMode mode, const char *acceptMime = nullptr);

std::shared_ptr<Request> StartDownloadWithCallback(
const std::string &url,
std::string_view url,
const Path &outfile,
ProgressBarMode mode,
std::function<void(Request &)> callback,
std::string_view name = "",
const char *acceptMime = nullptr);

std::shared_ptr<Request> AsyncPostWithCallback(
const std::string &url,
const std::string &postData,
const std::string &postMime, // Use postMime = "application/x-www-form-urlencoded" for standard form-style posts, such as used by retroachievements. For encoding form data manually we have MultipartFormDataEncoder.
std::string_view url,
std::string_view postData,
std::string_view postMime, // Use postMime = "application/x-www-form-urlencoded" for standard form-style posts, such as used by retroachievements. For encoding form data manually we have MultipartFormDataEncoder.
ProgressBarMode mode,
std::function<void(Request &)> callback,
std::string_view name = "");
Expand All @@ -122,8 +122,6 @@ class RequestManager {
}

private:
static bool IsHttpsUrl(const std::string &url);

std::vector<std::shared_ptr<Request>> downloads_;
// These get copied to downloads_ in Update(). It's so that callbacks can add new downloads
// while running.
Expand Down

0 comments on commit 493367c

Please sign in to comment.