Skip to content

Commit a3939d9

Browse files
committed
Supports brotli compression
Adds bortli as a submodules, and compiles the decoder, making it available to libcurl. Some websites appear to sent brotli encoded responses even though we don't advertise support for it (e.g. https://movie.douban.com).
1 parent 432e3c3 commit a3939d9

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,6 @@
3131
[submodule "vendor/curl"]
3232
path = vendor/curl
3333
url = https://github.com/curl/curl.git
34+
[submodule "vendor/brotli"]
35+
path = vendor/brotli
36+
url = https://github.com/google/brotli

build.zig

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ fn addDependencies(b: *Build, mod: *Build.Module, opts: *Build.Step.Options) !vo
245245
mod.addCMacro("HAVE_ASSERT_H", "1");
246246
mod.addCMacro("HAVE_BASENAME", "1");
247247
mod.addCMacro("HAVE_BOOL_T", "1");
248+
mod.addCMacro("HAVE_BROTLI", "1");
248249
mod.addCMacro("HAVE_BUILTIN_AVAILABLE", "1");
249250
mod.addCMacro("HAVE_CLOCK_GETTIME_MONOTONIC", "1");
250251
mod.addCMacro("HAVE_DLFCN_H", "1");
@@ -379,6 +380,7 @@ fn addDependencies(b: *Build, mod: *Build.Module, opts: *Build.Step.Options) !vo
379380
}
380381

381382
try buildZlib(b, mod);
383+
try buildBrotli(b, mod);
382384
try buildMbedtls(b, mod);
383385
try buildNghttp2(b, mod);
384386
try buildCurl(b, mod);
@@ -484,6 +486,30 @@ fn buildZlib(b: *Build, m: *Build.Module) !void {
484486
} });
485487
}
486488

489+
fn buildBrotli(b: *Build, m: *Build.Module) !void {
490+
const brotli = b.addLibrary(.{
491+
.name = "brotli",
492+
.root_module = m,
493+
});
494+
495+
const root = "vendor/brotli/c/";
496+
brotli.installHeader(b.path(root ++ "brotli.h"), "brotli.h");
497+
brotli.addCSourceFiles(.{ .flags = &.{}, .files = &.{
498+
root ++ "common/constants.c",
499+
root ++ "common/context.c",
500+
root ++ "common/dictionary.c",
501+
root ++ "common/platform.c",
502+
root ++ "common/shared_dictionary.c",
503+
root ++ "common/transform.c",
504+
root ++ "dec/bit_reader.c",
505+
root ++ "dec/decode.c",
506+
root ++ "dec/huffman.c",
507+
root ++ "dec/prefix.c",
508+
root ++ "dec/state.c",
509+
root ++ "dec/static_init.c",
510+
} });
511+
}
512+
487513
fn buildMbedtls(b: *Build, m: *Build.Module) !void {
488514
const mbedtls = b.addLibrary(.{
489515
.name = "mbedtls",

src/http/Http.zig

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,13 @@ pub const Connection = struct {
168168
// debug
169169
if (comptime Http.ENABLE_DEBUG) {
170170
try errorCheck(c.curl_easy_setopt(easy, c.CURLOPT_VERBOSE, @as(c_long, 1)));
171+
172+
// Sometimes the default debug output hides some useful data. You can
173+
// uncomment the following line (BUT KEEP THE LIVE ABOVE AS-IS), to
174+
// get more control over the data (specifically, the `CURLINFO_TEXT`
175+
// can include useful data).
176+
177+
// try errorCheck(c.curl_easy_setopt(easy, c.CURLOPT_DEBUGFUNCTION, debugCallback));
171178
}
172179

173180
return .{
@@ -435,3 +442,15 @@ const LineWriter = struct {
435442
self.col = col + remain.len;
436443
}
437444
};
445+
446+
447+
pub fn debugCallback(_: *c.CURL, msg_type: c.curl_infotype, raw: [*c]u8, len: usize, _: *anyopaque) callconv(.c) void {
448+
const data = raw[0..len];
449+
switch (msg_type) {
450+
c.CURLINFO_TEXT => std.debug.print("libcurl [text]: {s}\n", .{data}),
451+
c.CURLINFO_HEADER_OUT => std.debug.print("libcurl [req-h]: {s}\n", .{data}),
452+
c.CURLINFO_HEADER_IN => std.debug.print("libcurl [res-h]: {s}\n", .{data}),
453+
// c.CURLINFO_DATA_IN => std.debug.print("libcurl [res-b]: {s}\n", .{data}),
454+
else => std.debug.print("libcurl ?? {d}\n", .{msg_type}),
455+
}
456+
}

vendor/brotli

Submodule brotli added at f1c8022

0 commit comments

Comments
 (0)