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

Feat build options #174

Merged
merged 27 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
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
25 changes: 12 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@

LIBNAME = lambda
CFLAGS = -Wall -Werror -std=c++20
LIB_DEPS = $(LIB_CORE_DEPS) $(LIB_EXTRA_DEPS)
EXTERNAL_LIBS = -lz -lbrotlicommon -lbrotlidec -lbrotlienc
LAMBDA_LIBSTATIC = $(LIBNAME).a
LAMBDA_LIBSHARED = $(LIBNAME)$(DLLEXT)
BUILD_TARGET = $(target)

ifeq ($(BUILD_TARGET),prod)
CFLAGS += -s
else
CFLAGS += -g
# lambda main makefile

LIBNAME = lambda
CFLAGS = -Wall -Werror -std=c++20 $(if $(filter $(target),prod),-s,-g)
LIB_DEPS = $(LIB_CORE_DEPS) $(LIB_EXTRA_DEPS)
LAMBDA_LIBSTATIC = $(LIBNAME).a
LAMBDA_LIBSHARED = $(LIBNAME)$(DLLEXT)
BUILD_TARGET = $(target)
BUILD_ENABLE_COMPRESSION = $(if $(filter $(compression),disabled),false,true)

ifeq ($(BUILD_ENABLE_COMPRESSION),true)
EXTERNAL_LIBS += -lz -lbrotlicommon -lbrotlidec -lbrotlienc
endif

ifeq ($(OS),Windows_NT)
Expand Down
13 changes: 13 additions & 0 deletions buildopts.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

#ifndef __LIB_MADDSUA_LAMBDA_BUILD_OPTIONS__
#define __LIB_MADDSUA_LAMBDA_BUILD_OPTIONS__

/**
* You can comment this line if you don't any compression libs to be used.
* This disables both zlib and brotli.
*
* Don't forget to add "compression=disabled" make argument to disable linking with them.
*/
#define LAMBDA_BUILDOPTS_ENABLE_COMPRESSION

#endif
6 changes: 5 additions & 1 deletion core/core.mk
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ include core/websocket/websocket.mk
include core/sse/sse.mk

LIB_CORE = core/core.a
LIB_CORE_DEPS = $(LIB_CORE_POLYFILL_DEPS) $(LIB_CORE_HTTP_DEPS) $(LIB_CORE_ENCODING_DEPS) $(LIB_CORE_NETWORK_DEPS) $(LIB_CORE_COMPRESS_DEPS) $(LIB_CORE_SERVER_DEPS) $(LIB_CORE_CRYPTO_DEPS) $(LIB_CORE_HTML_DEPS) $(LIB_CORE_JSON_DEPS) $(LIB_CORE_WEBSOCKET_DEPS) $(LIB_CORE_UTILS_DEPS) $(LIB_CORE_SSE_DEPS)
LIB_CORE_DEPS = $(LIB_CORE_POLYFILL_DEPS) $(LIB_CORE_HTTP_DEPS) $(LIB_CORE_ENCODING_DEPS) $(LIB_CORE_NETWORK_DEPS) $(LIB_CORE_SERVER_DEPS) $(LIB_CORE_CRYPTO_DEPS) $(LIB_CORE_HTML_DEPS) $(LIB_CORE_JSON_DEPS) $(LIB_CORE_WEBSOCKET_DEPS) $(LIB_CORE_UTILS_DEPS) $(LIB_CORE_SSE_DEPS)

ifeq ($(BUILD_ENABLE_COMPRESSION),true)
LIB_CORE_DEPS += $(LIB_CORE_COMPRESS_DEPS)
endif

# target object
lambda.core: $(LIB_CORE)
Expand Down
80 changes: 45 additions & 35 deletions core/http/transport_v1.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
#include "./transport.hpp"
#include "./transport_impl.hpp"
#include "../polyfill/polyfill.hpp"
#include "../compression/compression.hpp"
#include "../network/network.hpp"
#include "../../buildopts.hpp"

#ifdef LAMBDA_BUILDOPTS_ENABLE_COMPRESSION
#include "../compression/compression.hpp"
#endif

#include <set>
#include <algorithm>
Expand Down Expand Up @@ -226,54 +230,60 @@ void TransportContextV1::respond(const ResponseContext& responsectx) {
}

auto applyEncoding = ContentEncodings::None;

auto responseHeaders = responsectx.response.headers;
const auto& contentTypeHeader = responseHeaders.get("content-type");

if (this->flags.autocompress) {
#ifdef LAMBDA_BUILDOPTS_ENABLE_COMPRESSION

const auto& contentTypeHeader = responseHeaders.get("content-type");

if (this->flags.autocompress) {

if (contentTypeHeader.size()) {
if (contentTypeHeader.size()) {

auto contentTypeNormalized = Strings::toLowerCase(contentTypeHeader);
auto contentTypeNormalized = Strings::toLowerCase(contentTypeHeader);

// when content type is provided, check if it's a text format,
// so that we won't be trying to compress jpegs and stuff
for (const auto& item : compressibleTypes) {
if (Strings::includes(contentTypeNormalized, item)) {
applyEncoding = this->m_compress;
break;
// when content type is provided, check if it's a text format,
// so that we won't be trying to compress jpegs and stuff
for (const auto& item : compressibleTypes) {
if (Strings::includes(contentTypeNormalized, item)) {
applyEncoding = this->m_compress;
break;
}
}
}

} else {
// set content type in case it's not provided in response
// by default, it's assumed to be a html page. works fine with just text too
responseHeaders.set("content-type", "text/html; charset=utf-8");
applyEncoding = this->m_compress;
} else {
// set content type in case it's not provided in response
// by default, it's assumed to be a html page. works fine with just text too
responseHeaders.set("content-type", "text/html; charset=utf-8");
applyEncoding = this->m_compress;
}
}
}

std::vector<uint8_t> responseBodyBuffer;
const auto& responseBody = responsectx.response.body.buffer();
std::vector<uint8_t> responseBodyBuffer;
const auto& responseBody = responsectx.response.body.buffer();

switch (applyEncoding) {
switch (applyEncoding) {

case ContentEncodings::Brotli: {
responseBodyBuffer = Compress::brotliCompressBuffer(responseBody, Compress::Quality::Noice);
} break;
case ContentEncodings::Brotli: {
responseBodyBuffer = Compress::brotliCompressBuffer(responseBody, Compress::Quality::Noice);
} break;

case ContentEncodings::Gzip: {
responseBodyBuffer = Compress::zlibCompressBuffer(responseBody, Compress::Quality::Noice, Compress::ZlibSetHeader::Gzip);
} break;
case ContentEncodings::Gzip: {
responseBodyBuffer = Compress::zlibCompressBuffer(responseBody, Compress::Quality::Noice, Compress::ZlibSetHeader::Gzip);
} break;

case ContentEncodings::Deflate: {
responseBodyBuffer = Compress::zlibCompressBuffer(responseBody, Compress::Quality::Noice, Compress::ZlibSetHeader::Defalte);
} break;
case ContentEncodings::Deflate: {
responseBodyBuffer = Compress::zlibCompressBuffer(responseBody, Compress::Quality::Noice, Compress::ZlibSetHeader::Defalte);
} break;

default: {
responseBodyBuffer = std::move(responseBody);
} break;
}
default: {
responseBodyBuffer = std::move(responseBody);
} break;
}

#else
const auto& responseBodyBuffer = responsectx.response.body.buffer();
#endif

const auto bodyBufferSize = responseBodyBuffer.size();

Expand Down
Loading