Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions book-src/05-03-http-server-std.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
## 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: This implementation has very poor performance. If you plan to do more than toy development, consider alternative libraries such as [http.zig](https://github.com/karlseguin/http.zig), [zap](https://github.com/zigzap/zap), [jetzig](https://pismice.github.io/HEIG_ZIG/docs/web/jetzig/), [zzz](https://github.com/mookums/zzz) (note that it is not yet mature), etc.
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!", .{});
}
}