Skip to content

Commit

Permalink
docs: add introduce for http-std (#83)
Browse files Browse the repository at this point in the history
Close #72

---------

Co-authored-by: xihale <[email protected]>
Co-authored-by: Jiacai Liu <[email protected]>
  • Loading branch information
3 people authored Feb 8, 2025
1 parent eaaba20 commit 6048579
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
12 changes: 12 additions & 0 deletions book-src/05-03-http-server-std.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
## http.Server - std

Starting with release 0.12.0, a very simple implementation of `http.Server` has been introduced.

```zig
{{#include ../src/05-03.zig }}
```

Note: The std implementation exhibits extremely poor performance. If you're planning beyond basic experimentation, consider utilizing alternative libraries such as:
- <https://github.com/karlseguin/http.zig>
- <https://github.com/zigzap/zap>
- <https://github.com/mookums/zzz>
1 change: 1 addition & 0 deletions book-src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

- [HTTP Get](./05-01-http-get.md)
- [HTTP Post](./05-02-http-post.md)
- [HTTP Server Std](./05-03-http-server-std.md)

- [Algorithms]()

Expand Down
4 changes: 3 additions & 1 deletion build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,11 @@ fn addExample(b: *std.Build, run_all: *std.Build.Step) !void {
// 04-01 start tcp server, and won't stop so we skip it here
// 04-02 is the server's client.
// 04-03 starts udp listener.
// 05-03 starts http server.
if (std.mem.eql(u8, "04-01", name) or
std.mem.eql(u8, "04-02", name) or
std.mem.eql(u8, "04-03", name))
std.mem.eql(u8, "04-03", name) or
std.mem.eql(u8, "05-03", name))
{
continue;
}
Expand Down
22 changes: 22 additions & 0 deletions src/05-03.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// reference: https://pismice.github.io/HEIG_ZIG/docs/web/std-http/
const std = @import("std");

pub fn main() !void {
const addr = try std.net.Address.parseIp("127.0.0.1", 8080);
var server = try std.net.Address.listen(addr, .{ .reuse_address = true });

var buf: [65535]u8 = undefined;
const conn = try server.accept();

var client = std.http.Server.init(conn, &buf);

while (client.state == .ready) {
var request = client.receiveHead() catch |err| switch (err) {
std.http.Server.ReceiveHeadError.HttpConnectionClosing => break,
else => return err,
};
_ = try request.reader();

try request.respond("Hello http.std!", .{});
}
}

0 comments on commit 6048579

Please sign in to comment.