Repository navigation
Expand file tree
/
Copy pathhttp2Client.zig
More file actions
61 lines (50 loc) · 1.83 KB
/
Copy pathhttp2Client.zig
File metadata and controls
61 lines (50 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//! HTTP/2 Client & Server (h2c) Example.
//!
//! Demonstrates running an HTTP/2 server and performing an HTTP/2 exchange
//! with stream multiplexing and binary framing over TCP.
//! Run with: `zig build run-http2-client`
const std = @import("std");
const httpx = @import("httpx");
fn h2Handler(ctx: *httpx.router.Context) anyerror!httpx.router.Response {
std.debug.print("HTTP/2 Server received: {s} {s}\n", .{ @tagName(ctx.method), ctx.path });
return .{
.status = 200,
.body = "{\"status\":200,\"protocol\":\"HTTP/2\",\"message\":\"Hello from HTTP/2\"}",
.contentType = "application/json",
};
}
pub fn main() !void {
var gpa: std.heap.DebugAllocator(.{}) = .init;
defer _ = gpa.deinit();
const allocator = gpa.allocator();
const io = std.Io.Threaded.global_single_threaded.io();
var server = try httpx.Server.init(allocator, io, .{
.host = "127.0.0.1",
.port = 0,
.enableDocs = false,
.maxConnections = 1,
});
defer server.deinit();
try server.get("/h2", h2Handler);
const port = server.localPort();
const ServerWorker = struct {
fn run(s: *httpx.Server) void {
s.run();
}
};
const t = try std.Thread.spawn(.{}, ServerWorker.run, .{&server});
defer t.join();
// Client side: connect and perform HTTP/2 request with zero manual IO management
var client = httpx.Client.init(allocator, io, .{
.httpVersion = .http2,
});
defer client.deinit();
const url = try std.fmt.allocPrint(allocator, "http://127.0.0.1:{d}/h2", .{port});
defer allocator.free(url);
var resp = try client.get(url, .{
.httpVersion = .http2,
});
defer resp.deinit();
std.debug.print("HTTP/2 response status: {d}\n", .{resp.status});
std.debug.print("body: {s}\n", .{resp.body});
}