Skip to content

Commit 97aaa3d

Browse files
committed
refactor: move redundant security check.
Avoid repetition, use only in function `send<Method, T>()`.
1 parent c747f8c commit 97aaa3d

File tree

2 files changed

+7
-21
lines changed

2 files changed

+7
-21
lines changed

src/AsyncHttpClient.cpp

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ template <HttpMethod Method, class T>
2727
void AsyncHttpClient::send(const HttpCallback& p_callback,
2828
const Red::CString& p_url, const T& p_body,
2929
const Red::Optional<HttpHeaders>& p_headers) {
30+
if (!HttpClient::is_secure(p_url)) {
31+
p_callback({});
32+
return;
33+
}
3034
if constexpr (std::is_same<T, Red::CString>()) {
3135
send_body<Method>(p_callback, p_url, p_body, p_headers);
3236
} else if constexpr (std::is_same<T, HttpPairs>()) {
@@ -41,10 +45,6 @@ void AsyncHttpClient::send_body(const HttpCallback& p_callback,
4145
const Red::CString& p_url,
4246
const Red::CString& p_body,
4347
const Red::Optional<HttpHeaders>& p_headers) {
44-
if (!HttpClient::is_secure(p_url)) {
45-
p_callback({});
46-
return;
47-
}
4848
cpr::Header request_headers = HttpClient::build_headers(p_headers.value);
4949

5050
if (!request_headers.contains("Content-Type")) {
@@ -79,10 +79,6 @@ void AsyncHttpClient::send_form(const HttpCallback& p_callback,
7979
const Red::CString& p_url,
8080
const HttpPairs& p_form,
8181
const Red::Optional<HttpHeaders>& p_headers) {
82-
if (!HttpClient::is_secure(p_url)) {
83-
p_callback({});
84-
return;
85-
}
8682
std::vector<cpr::Pair> values;
8783

8884
for (const auto& pair : p_form) {
@@ -122,10 +118,6 @@ void AsyncHttpClient::send_multipart(
122118
const HttpCallback& p_callback, const Red::CString& p_url,
123119
const Red::Handle<HttpMultipart>& p_form,
124120
const Red::Optional<HttpHeaders>& p_headers) {
125-
if (!HttpClient::is_secure(p_url)) {
126-
p_callback({});
127-
return;
128-
}
129121
cpr::Header request_headers = HttpClient::build_headers(p_headers.value);
130122

131123
plugin->log_request(Method, p_url, p_form, request_headers);

src/HttpClient.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ template <HttpMethod Method, class T>
4040
Red::Handle<HttpResponse> HttpClient::send(
4141
const Red::CString& p_url, const T& p_body,
4242
const Red::Optional<HttpHeaders>& p_headers) {
43+
if (!is_secure(p_url)) {
44+
return {};
45+
}
4346
if constexpr (std::is_same<T, Red::CString>()) {
4447
return send_body<Method>(p_url, p_body, p_headers);
4548
} else if constexpr (std::is_same<T, HttpPairs>()) {
@@ -54,9 +57,6 @@ template <HttpMethod Method>
5457
Red::Handle<HttpResponse> HttpClient::send_body(
5558
const Red::CString& p_url, const Red::CString& p_body,
5659
const Red::Optional<HttpHeaders>& p_headers) {
57-
if (!is_secure(p_url)) {
58-
return {};
59-
}
6060
cpr::Header request_headers = build_headers(p_headers.value);
6161

6262
if (!request_headers.contains("Content-Type")) {
@@ -89,9 +89,6 @@ template <HttpMethod Method>
8989
Red::Handle<HttpResponse> HttpClient::send_form(
9090
const Red::CString& p_url, const HttpPairs& p_form,
9191
const Red::Optional<HttpHeaders>& p_headers) {
92-
if (!is_secure(p_url)) {
93-
return {};
94-
}
9592
std::vector<cpr::Pair> values;
9693

9794
for (const auto& pair : p_form) {
@@ -130,9 +127,6 @@ template <HttpMethod Method>
130127
Red::Handle<HttpResponse> HttpClient::send_multipart(
131128
const Red::CString& p_url, const Red::Handle<HttpMultipart>& p_form,
132129
const Red::Optional<HttpHeaders>& p_headers) {
133-
if (!is_secure(p_url)) {
134-
return {};
135-
}
136130
cpr::Header request_headers = build_headers(p_headers.value);
137131

138132
plugin->log_request(Method, p_url, p_form, request_headers);

0 commit comments

Comments
 (0)