Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emscripten rest client and RestBackend follow-up #358

Merged
merged 2 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions src/client/include/RestClientEmscripten.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ struct SubscriptionPayload : FetchPayload {

void requestNext() {
auto uri = opencmw::URI<opencmw::STRICT>::UriFactory(command.topic).addQueryParameter("LongPollingIdx", (_update == 0) ? "Next" : fmt::format("{}", _update)).build();
fmt::print("URL 1 >>> {}\n", uri.relativeRef());
// fmt::print("URL 1 >>> {}, thread {}\n", uri.relativeRef(), std::this_thread::get_id());
auto preferredHeader = detail::getPreferredContentTypeHeader(command.topic, _mimeType);
std::array<const char *, preferredHeader.size() + 1> preferredHeaderEmscripten;
std::transform(preferredHeader.cbegin(), preferredHeader.cend(), preferredHeaderEmscripten.begin(),
Expand All @@ -162,20 +162,20 @@ struct SubscriptionPayload : FetchPayload {
attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY;
attr.requestHeaders = preferredHeaderEmscripten.data();
attr.onsuccess = [](emscripten_fetch_t *fetch) {
auto payloadIt = getPayloadIt(fetch);
auto &payload = *payloadIt;
std::string finalURL = getFinalURL(fetch->id);
std::string longPollingIdxString = opencmw::URI<>(finalURL).queryParamMap().at("LongPollingIdx").value_or("0");
char *end = longPollingIdxString.data() + longPollingIdxString.size();
std::size_t longPollingIdx = strtoull(longPollingIdxString.data(), &end, 10);
if (longPollingIdx != payload->_update) {
fmt::print("received unexpected update: {}, expected {}\n", longPollingIdx, payload->_update);
return;
}
payload->onsuccess(fetch->status, std::string_view(fetch->data, detail::checkedStringViewSize(fetch->numBytes)));
emscripten_fetch_close(fetch);
payload->_update++;
auto payloadIt = getPayloadIt(fetch);
auto &payload = *payloadIt;
if (payload->_live) {
std::string finalURL = getFinalURL(fetch->id);
std::string longPollingIdxString = opencmw::URI<>(finalURL).queryParamMap().at("LongPollingIdx").value_or("0");
char *end = longPollingIdxString.data() + longPollingIdxString.size();
std::size_t longPollingIdx = strtoull(longPollingIdxString.data(), &end, 10);
if (payload->_update != 0 && longPollingIdx != payload->_update) {
fmt::print("received unexpected update: {}, expected {}\n", longPollingIdx, payload->_update);
return;
}
payload->onsuccess(fetch->status, std::string_view(fetch->data, detail::checkedStringViewSize(fetch->numBytes)));
emscripten_fetch_close(fetch);
payload->_update = longPollingIdx + 1;
payload->requestNext();
} else {
detail::subscriptionPayloads.erase(payloadIt);
Expand Down
3 changes: 1 addition & 2 deletions src/majordomo/include/majordomo/RestBackend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -766,8 +766,7 @@ struct RestBackend<Mode, VirtualFS, Roles...>::RestWorker {
bool respondWithLongPollRedirect(const httplib::Request &request, httplib::Response &response, const mdp::Topic &subscription, detail::PollingIndex redirectLongPollingIdx) {
auto uri = URI<>::factory()
.path(request.path)
.addQueryParameter("LongPollingIdx", std::to_string(redirectLongPollingIdx))
.addQueryParameter("SubscriptionContext", subscription.toMdpTopic().str());
.addQueryParameter("LongPollingIdx", std::to_string(redirectLongPollingIdx));

// copy over the original query parameters
addParameters(request, uri);
Expand Down
2 changes: 1 addition & 1 deletion src/majordomo/test/majordomoworker_rest_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ std::jthread makeLongPollingRequestResponseCheckerThread(const std::string &addr
const std::string url = fmt::format("{}{}LongPollingIdx={}", address, address.contains('?') ? "&" : "?", i == 0 ? "Next" : fmt::format("{}", i));
const auto response = http.Get(url);
if (i == 0) { // check forwarding to the explicit index
REQUIRE(response->location.find("&LongPollingIdx=0") != std::string::npos);
REQUIRE(response->location.find("LongPollingIdx=0") != std::string::npos);
}
requireWithSource(response);
const auto requiredStatusCode = i < requiredStatusCodes.size() ? requiredStatusCodes[i] : 200;
Expand Down
Loading