2022-06-23 07:25:50 +00:00
|
|
|
const std = @import("std");
|
|
|
|
const util = @import("util");
|
|
|
|
const http = @import("./lib.zig");
|
|
|
|
|
2022-07-09 22:43:35 +00:00
|
|
|
const response = @import("./server/response.zig");
|
2022-11-05 10:09:59 +00:00
|
|
|
const request = @import("./request.zig");
|
2022-11-18 02:42:23 +00:00
|
|
|
const os = std.os;
|
2022-07-09 22:43:35 +00:00
|
|
|
|
2022-10-13 09:23:57 +00:00
|
|
|
pub const Response = struct {
|
|
|
|
alloc: std.mem.Allocator,
|
2022-11-18 02:42:23 +00:00
|
|
|
stream: Stream,
|
2022-10-13 09:23:57 +00:00
|
|
|
should_close: bool = false,
|
2022-11-18 02:42:23 +00:00
|
|
|
|
|
|
|
pub const ResponseStream = response.ResponseStream(Stream.Writer);
|
|
|
|
pub fn open(self: *Response, status: http.Status, headers: *const http.Fields) !ResponseStream {
|
2022-10-13 09:23:57 +00:00
|
|
|
if (headers.get("Connection")) |hdr| {
|
|
|
|
if (std.ascii.indexOfIgnoreCase(hdr, "close")) |_| self.should_close = true;
|
|
|
|
}
|
|
|
|
|
2022-11-18 02:42:23 +00:00
|
|
|
return response.open(self.alloc, self.stream.writer(), headers, status);
|
2022-10-13 09:23:57 +00:00
|
|
|
}
|
2022-10-16 12:48:12 +00:00
|
|
|
|
2022-11-18 02:42:23 +00:00
|
|
|
pub fn upgrade(self: *Response, status: http.Status, headers: *const http.Fields) !Stream {
|
2022-10-16 12:48:12 +00:00
|
|
|
try response.writeRequestHeader(self.stream.writer(), headers, status);
|
|
|
|
return self.stream;
|
|
|
|
}
|
2022-10-13 09:23:57 +00:00
|
|
|
};
|
2022-11-18 02:42:23 +00:00
|
|
|
pub const StreamKind = enum {
|
|
|
|
tcp,
|
|
|
|
};
|
|
|
|
|
|
|
|
pub const Stream = struct {
|
|
|
|
kind: StreamKind,
|
|
|
|
|
|
|
|
socket: os.socket_t,
|
|
|
|
|
|
|
|
pub fn close(self: Stream) void {
|
|
|
|
os.closeSocket(self.socket);
|
|
|
|
}
|
2022-06-23 07:25:50 +00:00
|
|
|
|
2022-11-18 02:42:23 +00:00
|
|
|
pub const ReadError = os.RecvFromError;
|
|
|
|
pub const WriteError = os.SendError;
|
2022-11-17 05:11:51 +00:00
|
|
|
|
2022-11-18 02:42:23 +00:00
|
|
|
pub const Reader = std.io.Reader(Stream, ReadError, read);
|
|
|
|
pub const Writer = std.io.Writer(Stream, WriteError, write);
|
2022-11-17 05:11:51 +00:00
|
|
|
|
2022-11-18 02:42:23 +00:00
|
|
|
pub fn read(self: Stream, buffer: []u8) ReadError!usize {
|
|
|
|
if (std.io.is_async) @compileError("TODO: async not supported");
|
|
|
|
if (self.kind != .tcp) @panic("TODO");
|
|
|
|
|
|
|
|
return os.recv(self.socket, buffer, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write(self: Stream, buffer: []const u8) WriteError!usize {
|
2022-11-17 05:11:51 +00:00
|
|
|
if (std.io.is_async) @compileError("TODO: Async not supported yet");
|
2022-11-18 02:42:23 +00:00
|
|
|
if (self.kind != .tcp) @panic("TODO");
|
|
|
|
|
|
|
|
return os.send(self.socket, buffer, os.MSG.NOSIGNAL);
|
2022-11-17 05:11:51 +00:00
|
|
|
}
|
|
|
|
|
2022-11-18 02:42:23 +00:00
|
|
|
pub fn reader(self: Stream) Reader {
|
2022-11-17 05:11:51 +00:00
|
|
|
return .{ .context = self };
|
|
|
|
}
|
|
|
|
|
2022-11-18 02:42:23 +00:00
|
|
|
pub fn writer(self: Stream) Writer {
|
|
|
|
return .{ .context = self };
|
|
|
|
}
|
|
|
|
};
|
2022-06-23 07:25:50 +00:00
|
|
|
|
2022-11-18 02:42:23 +00:00
|
|
|
pub const Server = struct {
|
|
|
|
tcp_server: std.net.StreamServer,
|
2022-07-09 21:18:43 +00:00
|
|
|
|
2022-11-18 02:42:23 +00:00
|
|
|
pub fn init() Server {
|
|
|
|
return Server{
|
|
|
|
.tcp_server = std.net.StreamServer.init(.{ .reuse_address = true }),
|
2022-07-09 21:18:43 +00:00
|
|
|
};
|
2022-11-18 02:42:23 +00:00
|
|
|
}
|
2022-07-09 21:18:43 +00:00
|
|
|
|
2022-11-18 02:42:23 +00:00
|
|
|
pub fn deinit(self: *Server) void {
|
|
|
|
self.tcp_server.deinit();
|
|
|
|
}
|
2022-07-09 21:18:43 +00:00
|
|
|
|
2022-11-18 02:42:23 +00:00
|
|
|
pub fn listen(self: *Server, address: std.net.Address) !void {
|
|
|
|
try self.tcp_server.listen(address);
|
|
|
|
}
|
2022-07-09 21:18:43 +00:00
|
|
|
|
2022-11-18 02:42:23 +00:00
|
|
|
pub const Connection = struct {
|
|
|
|
stream: Stream,
|
|
|
|
address: std.net.Address,
|
|
|
|
};
|
2022-07-09 21:18:43 +00:00
|
|
|
|
2022-11-18 02:42:23 +00:00
|
|
|
pub fn handleLoop(
|
|
|
|
self: *Server,
|
|
|
|
allocator: std.mem.Allocator,
|
|
|
|
ctx: anytype,
|
|
|
|
handler: anytype,
|
|
|
|
) void {
|
|
|
|
while (true) {
|
|
|
|
const conn = self.tcp_server.accept() catch |err| {
|
|
|
|
if (err == error.SocketNotListening) return;
|
|
|
|
|
|
|
|
std.log.err("Error occurred accepting connection: {}", .{err});
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
|
|
|
|
serveConn(
|
|
|
|
allocator,
|
|
|
|
Connection{
|
|
|
|
.stream = Stream{ .kind = .tcp, .socket = conn.stream.handle },
|
|
|
|
.address = conn.address,
|
|
|
|
},
|
|
|
|
ctx,
|
|
|
|
handler,
|
|
|
|
);
|
|
|
|
}
|
2022-07-09 21:18:43 +00:00
|
|
|
}
|
2022-11-18 02:42:23 +00:00
|
|
|
|
|
|
|
fn serveConn(
|
|
|
|
allocator: std.mem.Allocator,
|
|
|
|
conn: Connection,
|
|
|
|
ctx: anytype,
|
|
|
|
handler: anytype,
|
|
|
|
) void {
|
|
|
|
while (true) {
|
|
|
|
var req = request.parse(allocator, conn.stream.reader()) catch |err| {
|
|
|
|
return handleError(conn.stream.writer(), err) catch {};
|
|
|
|
};
|
|
|
|
|
|
|
|
var res = Response{
|
|
|
|
.alloc = allocator,
|
|
|
|
.stream = conn.stream,
|
|
|
|
};
|
|
|
|
|
|
|
|
handler(ctx, &req, &res);
|
|
|
|
|
|
|
|
if (req.headers.get("Connection")) |hdr| {
|
|
|
|
if (std.ascii.indexOfIgnoreCase(hdr, "close")) |_| return;
|
|
|
|
} else if (req.headers.get("Keep-Alive")) |_| {
|
|
|
|
// TODO: Support this
|
|
|
|
return;
|
|
|
|
} else if (req.protocol == .http_1_0) return;
|
|
|
|
if (res.should_close) return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2022-06-23 07:25:50 +00:00
|
|
|
|
2022-10-13 09:23:57 +00:00
|
|
|
/// Writes an error response message and requests closure of the connection
|
2022-06-26 06:35:31 +00:00
|
|
|
fn handleError(writer: anytype, err: anyerror) !void {
|
|
|
|
const status: http.Status = switch (err) {
|
2022-10-13 09:23:57 +00:00
|
|
|
error.EndOfStream => return, // Do nothing, the client closed the connection
|
2022-06-26 06:35:31 +00:00
|
|
|
error.BadRequest => .bad_request,
|
|
|
|
error.UnsupportedMediaType => .unsupported_media_type,
|
|
|
|
error.HttpVersionNotSupported => .http_version_not_supported,
|
|
|
|
|
2022-10-13 09:23:57 +00:00
|
|
|
else => .internal_server_error,
|
2022-06-26 06:35:31 +00:00
|
|
|
};
|
|
|
|
|
2022-10-13 09:23:57 +00:00
|
|
|
try writer.print("HTTP/1.1 {} {?s}\r\nConnection: close\r\n\r\n", .{ @enumToInt(status), status.phrase() });
|
2022-06-26 06:35:31 +00:00
|
|
|
}
|