Skip to content

Commit e23d456

Browse files
committed
Improve handling of statuscodes in HTTP requests
1 parent 703b24f commit e23d456

8 files changed

Lines changed: 34 additions & 34 deletions

File tree

include/http/HTTPRequestManager.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
#include <functional>
88
#include <map>
99
#include <string_view>
10-
#include <vector>
10+
11+
#include "span.h"
1112

1213
namespace OpenShock::HTTP {
1314
enum class RequestResult : uint8_t {
@@ -34,11 +35,11 @@ namespace OpenShock::HTTP {
3435
using GotContentLengthCallback = std::function<bool(int contentLength)>;
3536
using DownloadCallback = std::function<bool(std::size_t offset, const uint8_t* data, std::size_t len)>;
3637

37-
Response<std::size_t> Download(std::string_view url, const std::map<String, String>& headers, GotContentLengthCallback contentLengthCallback, DownloadCallback downloadCallback, const std::vector<int>& acceptedCodes = {200}, uint32_t timeoutMs = 10'000);
38-
Response<std::string> GetString(std::string_view url, const std::map<String, String>& headers, const std::vector<int>& acceptedCodes = {200}, uint32_t timeoutMs = 10'000);
38+
Response<std::size_t> Download(std::string_view url, const std::map<String, String>& headers, GotContentLengthCallback contentLengthCallback, DownloadCallback downloadCallback, tcb::span<const uint16_t> acceptedCodes, uint32_t timeoutMs = 10'000);
39+
Response<std::string> GetString(std::string_view url, const std::map<String, String>& headers, tcb::span<const uint16_t> acceptedCodes, uint32_t timeoutMs = 10'000);
3940

4041
template<typename T>
41-
Response<T> GetJSON(std::string_view url, const std::map<String, String>& headers, JsonParser<T> jsonParser, const std::vector<int>& acceptedCodes = {200}, uint32_t timeoutMs = 10'000) {
42+
Response<T> GetJSON(std::string_view url, const std::map<String, String>& headers, JsonParser<T> jsonParser, tcb::span<const uint16_t> acceptedCodes, uint32_t timeoutMs = 10'000) {
4243
auto response = GetString(url, headers, acceptedCodes, timeoutMs);
4344
if (response.result != RequestResult::Success) {
4445
return {response.result, response.code, {}};
@@ -56,6 +57,6 @@ namespace OpenShock::HTTP {
5657

5758
cJSON_Delete(json);
5859

59-
return {response.result, response.code, data};
60+
return {response.result, response.code, std::move(data)};
6061
}
6162
} // namespace OpenShock::HTTP

src/GatewayConnectionManager.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ AccountLinkResultCode GatewayConnectionManager::Link(std::string_view linkCode)
9696

9797
auto response = HTTP::JsonAPI::LinkAccount(linkCode);
9898

99+
if (response.code == 404) {
100+
return AccountLinkResultCode::InvalidCode;
101+
}
102+
99103
if (response.result == HTTP::RequestResult::RateLimited) {
100104
OS_LOGW(TAG, "Account Link request got ratelimited");
101105
return AccountLinkResultCode::RateLimited;
@@ -106,10 +110,6 @@ AccountLinkResultCode GatewayConnectionManager::Link(std::string_view linkCode)
106110
return AccountLinkResultCode::InternalError;
107111
}
108112

109-
if (response.code == 404) {
110-
return AccountLinkResultCode::InvalidCode;
111-
}
112-
113113
if (response.code != 200) {
114114
OS_LOGE(TAG, "Unexpected response code: %d", response.code);
115115
return AccountLinkResultCode::InternalError;
@@ -166,6 +166,12 @@ bool FetchHubInfo(std::string_view authToken)
166166

167167
auto response = HTTP::JsonAPI::GetHubInfo(authToken);
168168

169+
if (response.code == 401) {
170+
OS_LOGD(TAG, "Auth token is invalid, clearing it");
171+
Config::ClearBackendAuthToken();
172+
return false;
173+
}
174+
169175
if (response.result == HTTP::RequestResult::RateLimited) {
170176
return false; // Just return false, don't spam the console with errors
171177
}
@@ -174,12 +180,6 @@ bool FetchHubInfo(std::string_view authToken)
174180
return false;
175181
}
176182

177-
if (response.code == 401) {
178-
OS_LOGD(TAG, "Auth token is invalid, clearing it");
179-
Config::ClearBackendAuthToken();
180-
return false;
181-
}
182-
183183
if (response.code != 200) {
184184
OS_LOGE(TAG, "Unexpected response code: %d", response.code);
185185
return false;
@@ -241,6 +241,12 @@ bool StartConnectingToLCG()
241241

242242
auto response = HTTP::JsonAPI::AssignLcg(authToken);
243243

244+
if (response.code == 401) {
245+
OS_LOGD(TAG, "Auth token is invalid, clearing it");
246+
Config::ClearBackendAuthToken();
247+
return false;
248+
}
249+
244250
if (response.result == HTTP::RequestResult::RateLimited) {
245251
return false; // Just return false, don't spam the console with errors
246252
}
@@ -249,12 +255,6 @@ bool StartConnectingToLCG()
249255
return false;
250256
}
251257

252-
if (response.code == 401) {
253-
OS_LOGD(TAG, "Auth token is invalid, clearing it");
254-
Config::ClearBackendAuthToken();
255-
return false;
256-
}
257-
258258
if (response.code != 200) {
259259
OS_LOGE(TAG, "Unexpected response code: %d", response.code);
260260
return false;

src/OtaUpdateManager.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ static bool _tryGetStringList(std::string_view url, std::vector<std::string>& li
429429
{
430430
{"Accept", "text/plain"}
431431
},
432-
{200, 304}
432+
std::array<uint16_t, 2> {200, 304}
433433
);
434434
if (response.result != OpenShock::HTTP::RequestResult::Success) {
435435
OS_LOGE(TAG, "Failed to fetch list: [%u] %s", response.code, response.data.c_str());
@@ -548,7 +548,7 @@ bool OtaUpdateManager::TryGetFirmwareVersion(OtaUpdateChannel channel, OpenShock
548548
{
549549
{"Accept", "text/plain"}
550550
},
551-
{200, 304}
551+
std::array<uint16_t, 2> {200, 304}
552552
);
553553
if (response.result != OpenShock::HTTP::RequestResult::Success) {
554554
OS_LOGE(TAG, "Failed to fetch firmware version: [%u] %s", response.code, response.data.c_str());
@@ -618,7 +618,7 @@ bool OtaUpdateManager::TryGetFirmwareRelease(const OpenShock::SemVer& version, F
618618
{
619619
{"Accept", "text/plain"}
620620
},
621-
{200, 304}
621+
std::array<uint16_t, 2> {200, 304}
622622
);
623623
if (sha256HashesResponse.result != OpenShock::HTTP::RequestResult::Success) {
624624
OS_LOGE(TAG, "Failed to fetch hashes: [%u] %s", sha256HashesResponse.code, sha256HashesResponse.data.c_str());

src/http/HTTPRequestManager.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ const char* const TAG = "HTTPRequestManager";
1717
#include <numeric>
1818
#include <string_view>
1919
#include <unordered_map>
20-
#include <vector>
2120

2221
using namespace std::string_view_literals;
2322

@@ -358,7 +357,7 @@ HTTP::Response<std::size_t> _doGetStream(
358357
HTTPClient& client,
359358
std::string_view url,
360359
const std::map<String, String>& headers,
361-
const std::vector<int>& acceptedCodes,
360+
tcb::span<const uint16_t> acceptedCodes,
362361
std::shared_ptr<OpenShock::RateLimiter> rateLimiter,
363362
HTTP::GotContentLengthCallback contentLengthCallback,
364363
HTTP::DownloadCallback downloadCallback,
@@ -448,7 +447,7 @@ HTTP::Response<std::size_t> _doGetStream(
448447
}
449448

450449
HTTP::Response<std::size_t>
451-
HTTP::Download(std::string_view url, const std::map<String, String>& headers, HTTP::GotContentLengthCallback contentLengthCallback, HTTP::DownloadCallback downloadCallback, const std::vector<int>& acceptedCodes, uint32_t timeoutMs)
450+
HTTP::Download(std::string_view url, const std::map<String, String>& headers, HTTP::GotContentLengthCallback contentLengthCallback, HTTP::DownloadCallback downloadCallback, tcb::span<const uint16_t> acceptedCodes, uint32_t timeoutMs)
452451
{
453452
std::shared_ptr<OpenShock::RateLimiter> rateLimiter = _getRateLimiter(url);
454453
if (rateLimiter == nullptr) {
@@ -465,7 +464,7 @@ HTTP::Response<std::size_t>
465464
return _doGetStream(client, url, headers, acceptedCodes, rateLimiter, contentLengthCallback, downloadCallback, timeoutMs);
466465
}
467466

468-
HTTP::Response<std::string> HTTP::GetString(std::string_view url, const std::map<String, String>& headers, const std::vector<int>& acceptedCodes, uint32_t timeoutMs)
467+
HTTP::Response<std::string> HTTP::GetString(std::string_view url, const std::map<String, String>& headers, tcb::span<const uint16_t> acceptedCodes, uint32_t timeoutMs)
469468
{
470469
std::string result;
471470

src/http/JsonAPI.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ HTTP::Response<Serialization::JsonAPI::AccountLinkResponse> HTTP::JsonAPI::LinkA
2222
{"Accept", "application/json"}
2323
},
2424
Serialization::JsonAPI::ParseAccountLinkJsonResponse,
25-
{200, 404}
25+
std::array<uint16_t, 2> {200}
2626
);
2727
}
2828

@@ -43,7 +43,7 @@ HTTP::Response<Serialization::JsonAPI::HubInfoResponse> HTTP::JsonAPI::GetHubInf
4343
{"DeviceToken", OpenShock::StringToArduinoString(hubToken)}
4444
},
4545
Serialization::JsonAPI::ParseHubInfoJsonResponse,
46-
{200, 401}
46+
std::array<uint16_t, 2> {200}
4747
);
4848
}
4949

@@ -64,6 +64,6 @@ HTTP::Response<Serialization::JsonAPI::AssignLcgResponse> HTTP::JsonAPI::AssignL
6464
{"DeviceToken", OpenShock::StringToArduinoString(hubToken)}
6565
},
6666
Serialization::JsonAPI::ParseAssignLcgJsonResponse,
67-
{200, 401}
67+
std::array<uint16_t, 2> {200}
6868
);
6969
}

src/serial/command_handlers/domain.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ void _handleDomainCommand(std::string_view arg, bool isAutomated) {
3939
{"Accept", "application/json"}
4040
},
4141
OpenShock::Serialization::JsonAPI::ParseBackendVersionJsonResponse,
42-
{200}
42+
std::array<uint16_t, 2> {200}
4343
);
4444

4545
if (resp.result != OpenShock::HTTP::RequestResult::Success) {

src/serial/command_handlers/lcgoverride.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ void _handleLcgOverrideCommand(std::string_view arg, bool isAutomated)
5858
{"Accept", "application/json"}
5959
},
6060
OpenShock::Serialization::JsonAPI::ParseLcgInstanceDetailsJsonResponse,
61-
{200}
61+
std::array<uint16_t, 2> {200}
6262
);
6363

6464
if (resp.result != OpenShock::HTTP::RequestResult::Success) {

src/util/ParitionUtils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ bool OpenShock::FlashPartitionFromUrl(const esp_partition_t* partition, std::str
8282
},
8383
sizeValidator,
8484
dataWriter,
85-
{200, 304},
85+
std::array<uint16_t, 2> {200, 304},
8686
180'000
8787
); // 3 minutes
8888
if (appBinaryResponse.result != OpenShock::HTTP::RequestResult::Success) {

0 commit comments

Comments
 (0)