Skip to content

Commit

Permalink
tweak(server/http): provide error response headers
Browse files Browse the repository at this point in the history
  • Loading branch information
D4isDAVID committed Dec 17, 2024
1 parent c970038 commit 6fc82e5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 31 deletions.
58 changes: 29 additions & 29 deletions code/components/citizen-server-impl/src/HttpScriptFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,44 +116,44 @@ void PerformHttpRequestInternal(fx::ScriptContext& context)
// run a HTTP request
httpClient->DoMethodRequest(method, url, data, options, [evComponent, token, responseCode, responseHeaders](bool success, const char* data, size_t length)
{
if (!success)
{
evComponent->QueueEvent2("__cfx_internal:httpResponse", {}, token, *responseCode, msgpack::type::nil_t{}, std::map<std::string, std::string>(), std::string{ data, length });
}
else
msgpack::zone mz;
auto responseHeaderData = std::map<std::string, msgpack::object>();
for (auto& [key, value] : *responseHeaders)
{
msgpack::zone mz;
auto responseHeaderData = std::map<std::string, msgpack::object>();
for (auto& [key, value] : *responseHeaders)
if (auto it = responseHeaderData.find(key); it != responseHeaderData.end())
{
if (auto it = responseHeaderData.find(key); it != responseHeaderData.end())
// inefficient, but trivial
std::vector<std::string> newVal;

if (it->second.type != msgpack::type::ARRAY)
{
// inefficient, but trivial
std::vector<std::string> newVal;

if (it->second.type != msgpack::type::ARRAY)
{
newVal = {
it->second.as<std::string>(),
value
};
}
else
{
newVal = it->second.as<std::vector<std::string>>();
newVal.push_back(value);
}

responseHeaderData.erase(it);
responseHeaderData.emplace(key, msgpack::object{ newVal, mz });
newVal = {
it->second.as<std::string>(),
value
};
}
else
{
responseHeaderData.emplace(key, msgpack::object{ value, mz });
newVal = it->second.as<std::vector<std::string>>();
newVal.push_back(value);
}

responseHeaderData.erase(it);
responseHeaderData.emplace(key, msgpack::object{ newVal, mz });
}
else
{
responseHeaderData.emplace(key, msgpack::object{ value, mz });
}
}

evComponent->QueueEvent2("__cfx_internal:httpResponse", {}, token, *responseCode, std::string{ data, length }, responseHeaderData, msgpack::type::nil_t{});
if (!success)
{
evComponent->QueueEvent2("__cfx_internal:httpResponse", {}, token, *responseCode, msgpack::type::nil_t{}, std::map<std::string, std::string>(), std::string{ data, length }, responseHeaderData);
}
else
{
evComponent->QueueEvent2("__cfx_internal:httpResponse", {}, token, *responseCode, std::string{ data, length }, responseHeaderData, msgpack::type::nil_t{}, std::map<std::string, std::string>());
}
});

Expand Down
4 changes: 2 additions & 2 deletions data/shared/citizen/scripting/lua/scheduler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -394,11 +394,11 @@ if isDuplicityVersion then
end

local httpDispatch = {}
AddEventHandler('__cfx_internal:httpResponse', function(token, status, body, headers, errorData)
AddEventHandler('__cfx_internal:httpResponse', function(token, status, body, headers, errorData, errorHeaders)
if httpDispatch[token] then
local userCallback = httpDispatch[token]
httpDispatch[token] = nil
userCallback(status, body, headers, errorData)
userCallback(status, body, headers, errorData, errorHeaders)
end
end)

Expand Down

0 comments on commit 6fc82e5

Please sign in to comment.