Skip to content

Commit c0ee503

Browse files
committed
more work
1 parent 8cc417b commit c0ee503

File tree

5 files changed

+42
-50
lines changed

5 files changed

+42
-50
lines changed

include/http/HTTPClientState.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ namespace OpenShock::HTTP {
2828
}
2929

3030
struct [[nodiscard]] StartRequestResult {
31-
int statusCode;
31+
uint32_t statusCode;
3232
bool isChunked;
3333
uint32_t contentLength;
3434
};

include/http/HTTPResponse.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ namespace OpenShock::HTTP {
3636
, m_contentLength(0)
3737
{
3838
}
39-
39+
4040
inline bool Ok() const { return m_error == HTTPError::None && !m_state.expired(); }
4141
inline HTTPError Error() const { return m_error; }
42-
inline int StatusCode() const { return m_statusCode; }
42+
inline uint32_t StatusCode() const { return m_statusCode; }
4343
inline uint32_t ContentLength() const { return m_contentLength; }
4444

4545
inline ReadResult<uint32_t> ReadStream(DownloadCallback downloadCallback) {
@@ -67,7 +67,7 @@ namespace OpenShock::HTTP {
6767
private:
6868
std::weak_ptr<HTTPClientState> m_state;
6969
HTTPError m_error;
70-
int m_statusCode;
70+
uint32_t m_statusCode;
7171
uint32_t m_contentLength;
7272
};
7373
} // namespace OpenShock::HTTP

src/http/HTTPClientState.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,9 @@ std::variant<HTTP::HTTPClientState::StartRequestResult, HTTP::HTTPError> HTTP::H
6464
}
6565

6666
int code = esp_http_client_get_status_code(m_handle);
67+
if (code < 0) code = 0;
6768

68-
return StartRequestResult {code, isChunked, static_cast<uint32_t>(contentLength)};
69+
return StartRequestResult {static_cast<uint32_t>(code), isChunked, static_cast<uint32_t>(contentLength)};
6970
}
7071

7172
HTTP::ReadResult<uint32_t> HTTP::HTTPClientState::ReadStreamImpl(DownloadCallback cb)

src/serial/command_handlers/domain.cpp

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#include "serial/command_handlers/common.h"
22

33
#include "config/Config.h"
4-
#include "serialization/JsonAPI.h"
4+
#include "http/HTTPClient.h"
5+
#include "http/JsonAPI.h"
56

67
#include <esp_system.h>
78

@@ -32,21 +33,19 @@ void _handleDomainCommand(std::string_view arg, bool isAutomated) {
3233
char uri[OPENSHOCK_URI_BUFFER_SIZE];
3334
sprintf(uri, "https://%.*s/1", arg.length(), arg.data());
3435

35-
auto resp = OpenShock::HTTP::GetJSON<OpenShock::Serialization::JsonAPI::BackendVersionResponse>(
36-
uri,
37-
{
38-
{"Accept", "application/json"}
39-
},
40-
OpenShock::Serialization::JsonAPI::ParseBackendVersionJsonResponse,
41-
std::array<uint16_t, 2> {200}
42-
);
43-
44-
if (resp.result != OpenShock::HTTP::RequestResult::Success) {
45-
SERPR_ERROR("Tried to connect to \"%.*s\", but failed with status [%d] (%s), refusing to save domain to config", arg.length(), arg.data(), resp.code, resp.ResultToString());
36+
OpenShock::HTTP::HTTPClient client;
37+
auto response = client.GetJson<OpenShock::Serialization::JsonAPI::BackendVersionResponse>(uri, OpenShock::Serialization::JsonAPI::ParseBackendVersionJsonResponse);
38+
if (!response.Ok() || response.StatusCode() != 200) {
39+
SERPR_ERROR("Tried to connect to \"%.*s\", but failed with status [%d] (%s), refusing to save domain to config", arg.length(), arg.data(), response.StatusCode(), OpenShock::HTTP::HTTPErrorToString(response.Error()));
4640
return;
4741
}
4842

49-
OS_LOGI(TAG, "Successfully connected to \"%.*s\", version: %s, commit: %s, current time: %s", arg.length(), arg.data(), resp.data.version.c_str(), resp.data.commit.c_str(), resp.data.currentTime.c_str());
43+
auto content = response.ReadJson();
44+
if (content.error != OpenShock::HTTP::HTTPError::None) {
45+
#error TODO: Handle this
46+
}
47+
48+
OS_LOGI(TAG, "Successfully connected to \"%.*s\", version: %s, commit: %s, current time: %s", arg.length(), arg.data(), content.data.version.c_str(), content.data.commit.c_str(), content.data.currentTime.c_str());
5049

5150
bool result = OpenShock::Config::SetBackendDomain(std::string(arg));
5251

src/util/ParitionUtils.cpp

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const char* const TAG = "PartitionUtils";
44

55
#include "Core.h"
66
#include "Hashing.h"
7+
#include "http/HTTPClient.h"
78
#include "Logging.h"
89
#include "util/HexUtils.h"
910

@@ -34,25 +35,6 @@ bool OpenShock::FlashPartitionFromUrl(const esp_partition_t* partition, const ch
3435
std::size_t contentWritten = 0;
3536
int64_t lastProgress = 0;
3637

37-
auto sizeValidator = [partition, &contentLength, progressCallback, &lastProgress](std::size_t size) -> bool {
38-
if (size > partition->size) {
39-
OS_LOGE(TAG, "Remote partition binary is too large");
40-
return false;
41-
}
42-
43-
// Erase app partition.
44-
if (esp_partition_erase_range(partition, 0, partition->size) != ESP_OK) {
45-
OS_LOGE(TAG, "Failed to erase partition in preparation for update");
46-
return false;
47-
}
48-
49-
contentLength = size;
50-
51-
lastProgress = OpenShock::millis();
52-
progressCallback(0, contentLength, 0.0f);
53-
54-
return true;
55-
};
5638
auto dataWriter = [partition, &sha256, &contentLength, &contentWritten, progressCallback, &lastProgress](std::size_t offset, const uint8_t* data, std::size_t length) -> bool {
5739
if (esp_partition_write(partition, offset, data, length) != ESP_OK) {
5840
OS_LOGE(TAG, "Failed to write to partition");
@@ -76,23 +58,33 @@ bool OpenShock::FlashPartitionFromUrl(const esp_partition_t* partition, const ch
7658
};
7759

7860
// Start streaming binary to app partition.
79-
auto appBinaryResponse = OpenShock::HTTP::Download(
80-
remoteUrl,
81-
{
82-
{"Accept", "application/octet-stream"}
83-
},
84-
sizeValidator,
85-
dataWriter,
86-
std::array<uint16_t, 2> {200, 304},
87-
180'000
88-
); // 3 minutes
89-
if (appBinaryResponse.result != OpenShock::HTTP::RequestResult::Success) {
90-
OS_LOGE(TAG, "Failed to download remote partition binary: [%u]", appBinaryResponse.code);
61+
OpenShock::HTTP::HTTPClient client(180'000); // 3 minutes timeout
62+
auto response = client.Get(remoteUrl);
63+
if (!response.Ok() || response.StatusCode() != 200 || response.StatusCode() != 304) {
64+
OS_LOGE(TAG, "Failed to download remote partition binary: [%u]", response.StatusCode());
9165
return false;
9266
}
9367

68+
if (response.ContentLength() > partition->size) {
69+
OS_LOGE(TAG, "Remote partition binary is too large");
70+
return false;
71+
}
72+
73+
// Erase app partition.
74+
if (esp_partition_erase_range(partition, 0, partition->size) != ESP_OK) {
75+
OS_LOGE(TAG, "Failed to erase partition in preparation for update");
76+
return false;
77+
}
78+
79+
contentLength = response.ContentLength();
80+
81+
lastProgress = OpenShock::millis();
82+
progressCallback(0, contentLength, 0.0f);
83+
84+
contentLength = response.ReadStream(dataWriter);
85+
9486
progressCallback(contentLength, contentLength, 1.0f);
95-
OS_LOGD(TAG, "Wrote %u bytes to partition", appBinaryResponse.data);
87+
OS_LOGD(TAG, "Wrote %u bytes to partition", contentLength);
9688

9789
std::array<uint8_t, 32> localHash;
9890
if (!sha256.finish(localHash)) {

0 commit comments

Comments
 (0)