Skip to content

Commit

Permalink
added example for sendFile
Browse files Browse the repository at this point in the history
  • Loading branch information
renerocksai committed May 13, 2023
1 parent 48e30ca commit b988454
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 6 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ Here's what works:
dispatching on the HTTP path
- **[serve](examples/serve/serve.zig)**: the traditional static web
server with optional dynamic request handling
- **[sendfile](examples/sendfile/sendfile.zig)**: simple example of how to send
a file, honoring compression headers, etc.
- **[hello_json](examples/hello_json/hello_json.zig)**: serves you json
dependent on HTTP path
- **[endpoint](examples/endpoint/)**: a simple JSON REST API example featuring
Expand Down
5 changes: 1 addition & 4 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pub fn build(b: *std.build.Builder) !void {
.{ .name = "cookies", .src = "examples/cookies/cookies.zig" },
.{ .name = "websockets", .src = "examples/websockets/websockets.zig" },
.{ .name = "userpass_session", .src = "examples/userpass_session_auth/userpass_session_auth.zig" },
.{ .name = "sendfile", .src = "examples/sendfile/sendfile.zig" },
}) |excfg| {
const ex_name = excfg.name;
const ex_src = excfg.src;
Expand Down Expand Up @@ -148,10 +149,6 @@ pub fn build(b: *std.build.Builder) !void {
sendfile_tests.linkLibrary(facil_dep.artifact("facil.io"));
sendfile_tests.addModule("zap", zap_module);
const run_sendfile_tests = b.addRunArtifact(sendfile_tests);
// TODO: for some reason, tests aren't run more than once unless
// dependencies have changed.
// So, for now, we just force the exe to be built, so in order that
// we can call it again when needed.
const install_sendfile_tests = b.addInstallArtifact(sendfile_tests);

// test commands
Expand Down
46 changes: 46 additions & 0 deletions examples/sendfile/sendfile.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const std = @import("std");
const zap = @import("zap");

var buffer: [1024]u8 = undefined;
var read_len: ?usize = null;

const testfile = @embedFile("testfile.txt");

pub fn on_request(r: zap.SimpleRequest) void {
// Sends a file if present in the filesystem orelse returns an error.
//
// - efficiently sends a file using gzip compression
// - also handles range requests if `Range` or `If-Range` headers are present in the request.
// - sends the response headers and the specified file (the response's body).
//
// On success, the `r.h` handle will be consumed and invalid.
// On error, the handle will still be valid and should be used to send an error response
//
// Important: sets last-modified and cache-control headers with a max-age value of 1 hour!
if (r.sendFile("examples/sendfile/testfile.txt")) {} else |err| {
std.log.err("Unable to send file: {any}", .{err});
}
}

pub fn main() !void {
// setup listener
var listener = zap.SimpleHttpListener.init(
.{
.port = 3000,
.on_request = on_request,
.log = true,
.max_clients = 10,
.max_body_size = 1 * 1024, // careful here
},
);

zap.enableDebugLog();
try listener.listen();

std.debug.print("Visit me on http://127.0.0.1:3000\n", .{});

zap.start(.{
.threads = 1,
.workers = 0,
});
}
1 change: 1 addition & 0 deletions examples/sendfile/testfile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
All your codebase are belong to us!
4 changes: 2 additions & 2 deletions src/tests/test_sendfile.zig
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ test "send file" {
// setup listener
var listener = zap.SimpleHttpListener.init(
.{
.port = 3001,
.port = 3002,
.on_request = on_request,
.log = false,
.max_clients = 10,
Expand All @@ -48,7 +48,7 @@ test "send file" {
zap.enableDebugLog();
try listener.listen();

const thread = try makeRequestThread(allocator, "http://127.0.0.1:3001/?file=src/tests/testfile.txt");
const thread = try makeRequestThread(allocator, "http://127.0.0.1:3002/?file=src/tests/testfile.txt");
defer thread.join();
zap.start(.{
.threads = 1,
Expand Down
1 change: 1 addition & 0 deletions targets.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ pkghash
cookies
websockets
userpass_session
sendfile

0 comments on commit b988454

Please sign in to comment.