Skip to content

Commit 51a9e5a

Browse files
committed
Implement HTTP gzip/zstd Compression and WebSocket permessage-deflate Support
1 parent 62cd137 commit 51a9e5a

40 files changed

Lines changed: 5446 additions & 72 deletions

.github/workflows/CI.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ jobs:
1919
- name: build
2020
run: |
2121
sudo apt update
22-
sudo apt install libssl-dev libnghttp2-dev
23-
./configure --with-openssl --with-nghttp2 --with-kcp --with-mqtt
22+
sudo apt install libssl-dev libnghttp2-dev zlib1g-dev libzstd-dev
23+
./configure --with-openssl --with-nghttp2 --with-kcp --with-mqtt --with-zlib --with-zstd
2424
make libhv evpp
2525
2626
- name: test

BUILD.bazel

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library")
1+
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_test")
22

33

44
config_setting(
@@ -85,6 +85,16 @@ config_setting(
8585
visibility = [":__subpackages__"]
8686
)
8787

88+
config_setting(
89+
name = "with_http_server_client",
90+
define_values = {
91+
"WITH_EVPP": "ON",
92+
"WITH_HTTP": "ON",
93+
"WITH_HTTP_SERVER": "ON",
94+
"WITH_HTTP_CLIENT": "ON",
95+
},
96+
)
97+
8898
config_setting(
8999
name = "with_evpp_nghttp2",
90100
define_values = {
@@ -120,6 +130,16 @@ config_setting(
120130
define_values = {"WITH_NGHTTP2": "ON"}
121131
)
122132

133+
config_setting(
134+
name = "with_zlib",
135+
define_values = {"WITH_ZLIB": "ON"}
136+
)
137+
138+
config_setting(
139+
name = "with_zstd",
140+
define_values = {"WITH_ZSTD": "ON"}
141+
)
142+
123143
config_setting(
124144
name = "with_openssl",
125145
define_values = {"WITH_OPENSSL": "ON"}
@@ -219,6 +239,12 @@ COPTS = select({
219239
}) + select({
220240
"with_nghttp2": ["-DWITH_NGHTTP2"],
221241
"//conditions:default": [],
242+
}) + select({
243+
"with_zlib": ["-DWITH_ZLIB"],
244+
"//conditions:default": [],
245+
}) + select({
246+
"with_zstd": ["-DWITH_ZSTD"],
247+
"//conditions:default": [],
222248
}) + select({
223249
"with_openssl": ["-DWITH_OPENSSL"],
224250
"//conditions:default": [],
@@ -253,6 +279,12 @@ LINKOPTS = select({
253279
}) + select({
254280
"@bazel_tools//tools/cpp:gcc": ["-lrt"],
255281
"//conditions:default": [],
282+
}) + select({
283+
"with_zlib": ["-lz"],
284+
"//conditions:default": [],
285+
}) + select({
286+
"with_zstd": ["-lzstd"],
287+
"//conditions:default": [],
256288
})
257289

258290
BASE_HEADERS = [
@@ -332,6 +364,7 @@ PROTOCOL_HEADERS = [
332364
HTTP_HEADERS = [
333365
"http/httpdef.h",
334366
"http/wsdef.h",
367+
"http/HttpCompression.h",
335368
"http/http_content.h",
336369
"http/HttpMessage.h",
337370
"http/HttpParser.h",
@@ -449,6 +482,32 @@ cc_library(
449482
visibility = ["//visibility:public"]
450483
)
451484

485+
cc_test(
486+
name = "http_compression_test",
487+
srcs = ["unittest/http_compression_test.cpp"],
488+
copts = COPTS,
489+
deps = [":hv"],
490+
)
491+
492+
cc_test(
493+
name = "websocket_compression_test",
494+
srcs = ["unittest/websocket_compression_test.cpp"],
495+
copts = COPTS,
496+
deps = [":hv"],
497+
)
498+
499+
filegroup(
500+
name = "unittests",
501+
srcs = select({
502+
"with_http_server_client": [
503+
":http_compression_test",
504+
":websocket_compression_test",
505+
],
506+
"//conditions:default": [],
507+
}),
508+
visibility = ["//:__pkg__"],
509+
)
510+
452511
filegroup(
453512
name = "libhv",
454513
srcs = select({
@@ -460,7 +519,8 @@ filegroup(
460519
}) + select({
461520
"build_examples": ["//examples:examples"],
462521
"//conditions:default": [],
522+
}) + select({
523+
"build_unittest": [":unittests"],
524+
"//conditions:default": [],
463525
})
464526
)
465-
466-

BUILD.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,20 @@ bin/httpd -s restart -d
144144
bin/curl -v http://localhost:8080 --http2
145145
```
146146

147+
### compile WITH_ZLIB
148+
```
149+
sudo apt install zlib1g-dev # ubuntu
150+
./configure --with-zlib
151+
make clean && make
152+
```
153+
154+
### compile WITH_ZSTD (Requires libzstd >= 1.5.0)
155+
```
156+
sudo apt install libzstd-dev # ubuntu
157+
./configure --with-zstd
158+
make clean && make
159+
```
160+
147161
### compile WITH_KCP
148162
```
149163
./configure --with-kcp

CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ option(USE_MULTIMAP "MultiMap" OFF)
2323

2424
option(WITH_CURL "with curl library (deprecated)" OFF)
2525
option(WITH_NGHTTP2 "with nghttp2 library" OFF)
26+
option(WITH_ZLIB "with zlib library" OFF)
27+
option(WITH_ZSTD "with zstd library" OFF)
2628

2729
option(WITH_OPENSSL "with openssl library" OFF)
2830
option(WITH_GNUTLS "with gnutls library" OFF)
@@ -152,6 +154,16 @@ if(WITH_NGHTTP2)
152154
set(LIBS ${LIBS} nghttp2)
153155
endif()
154156

157+
if(WITH_ZLIB)
158+
add_definitions(-DWITH_ZLIB)
159+
set(LIBS ${LIBS} z)
160+
endif()
161+
162+
if(WITH_ZSTD)
163+
add_definitions(-DWITH_ZSTD)
164+
set(LIBS ${LIBS} zstd)
165+
endif()
166+
155167
if(WITH_OPENSSL)
156168
add_definitions(-DWITH_OPENSSL)
157169
find_package(OpenSSL)

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,12 @@ unittest: prepare
286286
$(CC) -g -Wall -O0 -std=c99 -I. -Ibase -Iprotocol -o bin/ping unittest/ping_test.c protocol/icmp.c base/hsocket.c base/htime.c -DPRINT_DEBUG
287287
$(CC) -g -Wall -O0 -std=c99 -I. -Ibase -Iprotocol -o bin/ftp unittest/ftp_test.c protocol/ftp.c base/hsocket.c base/htime.c
288288
$(CC) -g -Wall -O0 -std=c99 -I. -Ibase -Iprotocol -Iutil -o bin/sendmail unittest/sendmail_test.c protocol/smtp.c base/hsocket.c base/htime.c util/base64.c
289+
@if [ "$(WITH_HTTP)" = "yes" ] && [ "$(WITH_HTTP_CLIENT)" = "yes" ] && [ "$(WITH_HTTP_SERVER)" = "yes" ]; then \
290+
$(MAKEF) TARGET=http_compression_test SRCDIRS="$(CORE_SRCDIRS) util cpputil evpp http http/client http/server" \
291+
SRCS="unittest/http_compression_test.cpp"; \
292+
$(MAKEF) TARGET=websocket_compression_test SRCDIRS="$(CORE_SRCDIRS) util cpputil evpp http http/client http/server" \
293+
SRCS="unittest/websocket_compression_test.cpp"; \
294+
fi
289295

290296
run-unittest: unittest
291297
bash scripts/unittest.sh

Makefile.in

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,16 @@ ifeq ($(WITH_NGHTTP2), yes)
182182
LDFLAGS += -lnghttp2
183183
endif
184184

185+
ifeq ($(WITH_ZLIB), yes)
186+
CPPFLAGS += -DWITH_ZLIB
187+
LDFLAGS += -lz
188+
endif
189+
190+
ifeq ($(WITH_ZSTD), yes)
191+
CPPFLAGS += -DWITH_ZSTD
192+
LDFLAGS += -lzstd
193+
endif
194+
185195
ifeq ($(WITH_OPENSSL), yes)
186196
CPPFLAGS += -DWITH_OPENSSL
187197
LDFLAGS += -lssl -lcrypto

Makefile.vars

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ PROTOCOL_HEADERS = protocol/icmp.h\
7171

7272
HTTP_HEADERS = http/httpdef.h\
7373
http/wsdef.h\
74+
http/HttpCompression.h\
7475
http/http_content.h\
7576
http/HttpMessage.h\
7677
http/HttpParser.h\

base/herr.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,14 @@
3535
F(1017, INVALID_FMT, "Invalid format") \
3636
F(1018, INVALID_PROTOCOL, "Invalid protocol") \
3737
F(1019, INVALID_PACKAGE, "Invalid package") \
38+
F(1020, UNSUPPORTED_CONTENT_ENCODING, "Unsupported content encoding") \
3839
\
3940
F(1021, OUT_OF_RANGE, "Out of range") \
4041
F(1022, OVER_LIMIT, "Over the limit") \
4142
F(1023, MISMATCH, "Mismatch") \
4243
F(1024, PARSE, "Parse failed") \
44+
F(1025, COMPRESS, "Compress failed") \
45+
F(1026, DECOMPRESS, "Decompress failed")\
4346
\
4447
F(1030, OPEN_FILE, "Open file failed") \
4548
F(1031, SAVE_FILE, "Save file failed") \

cmake/vars.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ set(PROTOCOL_HEADERS
7878
set(HTTP_HEADERS
7979
http/httpdef.h
8080
http/wsdef.h
81+
http/HttpCompression.h
8182
http/http_content.h
8283
http/HttpMessage.h
8384
http/HttpParser.h

config.ini

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ USE_MULTIMAP=no
3131
WITH_CURL=no
3232
# for http2
3333
WITH_NGHTTP2=no
34+
# for http gzip / websocket permessage-deflate
35+
WITH_ZLIB=no
36+
# for http zstd
37+
WITH_ZSTD=no
3438
# for SSL/TLS
3539
WITH_OPENSSL=no
3640
WITH_GNUTLS=no

0 commit comments

Comments
 (0)