Skip to content
Open
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
1 change: 1 addition & 0 deletions code/components/citizen-server-impl/component.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"vendor:eastl",
"vendor:thread-pool-cpp",
"vendor:utfcpp",
"vendor:zlib",
"vendor:breakpad",
"vendor:prometheus-cpp",
"vendor:folly",
Expand Down
75 changes: 72 additions & 3 deletions code/components/citizen-server-impl/src/ClientHttpHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,62 @@

#include <FormData.h>

#include <zlib.h>

using json = nlohmann::json;

static std::shared_ptr<ConVar<bool>> g_threadedHttpVar;
static std::shared_ptr<ConVar<int>> g_maxClientEndpointRequestSize;

namespace
{
bool ClientAcceptsGzip(const fwRefContainer<net::HttpRequest>& request)
{
const auto header = request->GetHeader("accept-encoding");

for (size_t i = 0; i + 4 <= header.size(); ++i)
{
if ((header[i] | 0x20) == 'g' && (header[i + 1] | 0x20) == 'z' &&
(header[i + 2] | 0x20) == 'i' && (header[i + 3] | 0x20) == 'p')
{
return true;
}
}

return false;
}

std::optional<std::string> GzipCompress(std::string_view input)
{
z_stream zs{};

// MAX_WBITS + 16 for the gzip wrapper format
if (deflateInit2(&zs, Z_BEST_SPEED, Z_DEFLATED, MAX_WBITS + 16, 8, Z_DEFAULT_STRATEGY) != Z_OK)
{
return std::nullopt;
}

std::string output;
output.resize(deflateBound(&zs, static_cast<uLong>(input.size())));

zs.next_in = reinterpret_cast<z_const Bytef*>(const_cast<char*>(input.data()));
zs.avail_in = static_cast<uInt>(input.size());
zs.next_out = reinterpret_cast<Bytef*>(output.data());
zs.avail_out = static_cast<uInt>(output.size());

const int rc = deflate(&zs, Z_FINISH);
if (rc != Z_STREAM_END)
{
deflateEnd(&zs);
return std::nullopt;
}

output.resize(zs.total_out);
deflateEnd(&zs);
return output;
}
}

namespace fx
{
auto ClientMethodRegistry::GetHandler(const std::string& method) -> std::optional<std::variant<THandler<TCallback>, THandler<TCallbackFast>>>
Expand Down Expand Up @@ -90,7 +141,7 @@ namespace fx
}
else if (handler->index() == 1)
{
(std::get<1>(*handler))(postMap, request, [response](const rapidjson::Document& data)
(std::get<1>(*handler))(postMap, request, [request, response](const rapidjson::Document& data)
{
if (data.IsNull())
{
Expand All @@ -110,12 +161,30 @@ namespace fx
sb.Put('\r');
sb.Put('\n');

// Compress with gzip when client supports it
constexpr size_t kGzipMinSize = 1024;

std::string compressedHolder;
std::string_view bodyView{ sb.GetString(), sb.GetSize() };

if (bodyView.size() >= kGzipMinSize && ClientAcceptsGzip(request))
{
if (auto compressed = GzipCompress(bodyView))
{
compressedHolder = std::move(*compressed);
bodyView = compressedHolder;
response->SetHeader(net::HeaderString{ "content-encoding" },
net::HeaderString{ "gzip" });
}
}

// for TCP write timeout bits, write this in chunks
constexpr size_t kChunkSize = 16384;

for (size_t i = 0; i < sb.GetLength(); i += kChunkSize)
for (size_t i = 0; i < bodyView.size(); i += kChunkSize)
{
response->Write(std::string{ sb.GetString() + i, std::min(kChunkSize, sb.GetLength() - i) });
response->Write(std::string{ bodyView.data() + i,
std::min(kChunkSize, bodyView.size() - i) });
}
});
}
Expand Down
3 changes: 2 additions & 1 deletion code/components/http-client/src/HttpClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,8 @@ static std::shared_ptr<CurlData> SetupCURLHandle(const std::unique_ptr<HttpClien
curl_easy_setopt(curlHandle, CURLOPT_SSL_VERIFYPEER, 0);
curl_easy_setopt(curlHandle, CURLOPT_SSL_VERIFYHOST, 0);
curl_easy_setopt(curlHandle, CURLOPT_STREAM_WEIGHT, long(options.weight));

curl_easy_setopt(curlHandle, CURLOPT_ACCEPT_ENCODING, "");

if (options.responseHeaders)
{
curl_easy_setopt(curlHandle, CURLOPT_HEADERFUNCTION, CurlHeaderInfo);
Expand Down
10 changes: 9 additions & 1 deletion code/vendor/curl-crt.lua
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,18 @@ return {
add_dependencies 'vendor:nghttp2-crt'
end

-- zlib
if not isCrt then
add_dependencies 'vendor:zlib'
else
add_dependencies 'vendor:zlib-crt'
end

-- all the disables except http/file
defines { 'BUILDING_LIBCURL', 'USE_IPV6', 'CURL_DISABLE_TFTP', 'CURL_DISABLE_FTP', 'CURL_DISABLE_LDAP', 'CURL_DISABLE_TELNET',
'CURL_DISABLE_DICT', 'CURL_DISABLE_RTSP', 'CURL_DISABLE_POP3', 'CURL_DISABLE_IMAP', 'CURL_DISABLE_SMTP',
'CURL_DISABLE_RTMP', 'CURL_DISABLE_GOPHER', 'CURL_DISABLE_SMB', 'USE_IPV6', 'USE_NGHTTP2' }
'CURL_DISABLE_RTMP', 'CURL_DISABLE_GOPHER', 'CURL_DISABLE_SMB', 'USE_IPV6', 'USE_NGHTTP2',
'HAVE_LIBZ', 'HAVE_ZLIB_H' }

if os.istarget('windows') then
defines { 'USE_WINDOWS_SSPI' }
Expand Down
Loading