summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMitja Felicijan <mitja.felicijan@gmail.com>2024-09-16 18:44:51 +0200
committerMitja Felicijan <mitja.felicijan@gmail.com>2024-09-16 18:44:51 +0200
commit135321620a433778be784ed8811af40a8cfb2875 (patch)
treeb65c6499aa483707240e7956b95c5a97ac21741e
parenta72805786a3444726eb7bf9d1fd22714ffc5fac3 (diff)
downloadprobe-135321620a433778be784ed8811af40a8cfb2875.tar.gz
Added basic HTTP 1.1 server primer
-rw-r--r--zig-http/Makefile2
-rw-r--r--zig-http/main.zig31
2 files changed, 33 insertions, 0 deletions
diff --git a/zig-http/Makefile b/zig-http/Makefile
new file mode 100644
index 0000000..b962fd1
--- /dev/null
+++ b/zig-http/Makefile
@@ -0,0 +1,2 @@
+default:
+ zig run main.zig
diff --git a/zig-http/main.zig b/zig-http/main.zig
new file mode 100644
index 0000000..5859766
--- /dev/null
+++ b/zig-http/main.zig
@@ -0,0 +1,31 @@
+// https://www.rfc-editor.org/rfc/rfc2616
+
+// NOTE: This is just a barebones example and it has a bunch of bugs there
+// so do not use this for anything seriously.
+
+const std = @import("std");
+
+pub fn main() anyerror!void {
+ const self_addr = try std.net.Address.resolveIp("127.0.0.1", 6969);
+ var listener = std.net.StreamServer.init(.{});
+ try (&listener).listen(self_addr);
+
+ std.log.info("Listening on {}; press Ctrl-C to exit...", .{self_addr});
+
+ while ((&listener).accept()) |conn| {
+ std.log.info("Accepted Connection from: {}", .{conn.address});
+
+ // Basic HTTP 1.1 header.
+ _ = try conn.stream.write("HTTP/1.1 200 OK\r\n");
+ _ = try conn.stream.write("Connection: close\r\n");
+ _ = try conn.stream.write("Content-Type: text/html; charset=utf-8\r\n");
+ _ = try conn.stream.write("\r\n");
+
+ // Actual response.
+ _ = try conn.stream.write("<h1>Oh, hi Mark!</h1><p>It’s not true! It’s bullshit! I did not hit her! I did not!</p>\r\n");
+
+ conn.stream.close();
+ } else |err| {
+ return err;
+ }
+}