remove crlf from http headers

This commit is contained in:
matcool 2024-02-03 18:55:03 -03:00
parent 342487993e
commit 17153a4f68
2 changed files with 20 additions and 15 deletions

View file

@ -280,6 +280,7 @@ void updater::downloadLoaderUpdate(std::string const& url) {
LoaderUpdateEvent(UpdateFinished()).post();
})
.expect([](std::string const& info) {
log::error("Failed to download latest update {}", info);
LoaderUpdateEvent(
UpdateFailed("Unable to download update: " + info)
).post();
@ -334,6 +335,7 @@ void updater::checkForLoaderUpdates() {
).post();
},
[](std::string const& info) {
log::error("Failed to fetch updates {}", info);
LoaderUpdateEvent(
UpdateFailed("Unable to check for updates: " + info)
).post();

View file

@ -336,21 +336,19 @@ SentAsyncWebRequest::Impl::Impl(SentAsyncWebRequest* self, AsyncWebRequest const
curl_easy_setopt(curl, CURLOPT_HEADERDATA, &data);
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, +[](char* buffer, size_t size, size_t nitems, void* ptr){
auto data = static_cast<ProgressData*>(ptr);
std::string header;
header.append(buffer, size * nitems);
// send the header to the response header callback
Loader::get()->queueInMainThread([self = data->self, header]() {
std::unordered_map<std::string, std::string> headers;
std::string line;
std::stringstream ss(header);
std::stringstream ss(std::string(buffer, size * nitems));
while (std::getline(ss, line)) {
auto colon = line.find(':');
if (colon == std::string::npos) continue;
auto key = line.substr(0, colon);
auto value = line.substr(colon + 2);
self->m_responseHeader[key] = value;
if (value.ends_with('\r')) {
value = value.substr(0, value.size() - 1);
}
data->self->m_responseHeader[key] = value;
}
});
return size * nitems;
});
@ -572,7 +570,12 @@ AsyncWebRequest& AsyncWebRequest::timeout(std::chrono::seconds seconds) {
}
AsyncWebRequest& AsyncWebRequest::header(std::string_view const header) {
m_impl->m_httpHeaders.push_back(std::string(header));
std::string str(header);
// remove \r and \n
str.erase(std::remove_if(str.begin(), str.end(), [](char c) {
return c == '\r' || c == '\n';
}), str.end());
m_impl->m_httpHeaders.push_back(str);
return *this;
}