Add http.Context

This commit is contained in:
jaina heartles 2022-07-09 12:39:36 -07:00
parent 8ba0ebd84c
commit 053de35d33
3 changed files with 21 additions and 7 deletions

View file

@ -2,6 +2,8 @@ const std = @import("std");
pub const Connection = struct {
pub const Id = u64;
pub const Writer = std.net.Stream.Writer;
pub const Reader = std.net.Stream.Reader;
id: Id,
address: std.net.Address,

View file

@ -29,6 +29,18 @@ pub const Request = struct {
body: ?[]const u8 = null,
};
pub const Context = struct {
arena_alloc: std.mem.Allocator,
request: *const Request,
connection: *const Connection,
const Writer = ResponseStream(Connection.Writer);
pub fn openResponse(self: *Context, headers: *const Headers, status: Status) !Writer {
return try response_stream.open(self.arena_alloc, self.connection.stream.writer(), headers, status);
}
};
test {
_ = conn;
_ = response_stream;

View file

@ -5,7 +5,7 @@ const Status = http.Status;
const Headers = http.Headers;
const chunk_size = 16 * 1024;
pub fn openResponse(
pub fn open(
alloc: std.mem.Allocator,
writer: anytype,
headers: *const Headers,
@ -176,7 +176,7 @@ const _tests = struct {
defer headers.deinit();
{
var stream = try openResponse(
var stream = try open(
std.testing.allocator,
test_stream.writer(),
&headers,
@ -206,7 +206,7 @@ const _tests = struct {
try headers.put("Content-Type", "text/plain");
{
var stream = try openResponse(
var stream = try open(
std.testing.allocator,
test_stream.writer(),
&headers,
@ -237,7 +237,7 @@ const _tests = struct {
try headers.put("Content-Type", "text/plain");
{
var stream = try openResponse(
var stream = try open(
std.testing.allocator,
test_stream.writer(),
&headers,
@ -267,7 +267,7 @@ const _tests = struct {
try headers.put("Content-Type", "text/plain");
{
var stream = try openResponse(
var stream = try open(
std.testing.allocator,
test_stream.writer(),
&headers,
@ -301,7 +301,7 @@ const _tests = struct {
try headers.put("Content-Type", "text/plain");
{
var stream = try openResponse(
var stream = try open(
std.testing.allocator,
test_stream.writer(),
&headers,
@ -342,7 +342,7 @@ const _tests = struct {
try headers.put("Content-Type", "text/plain");
{
var stream = try openResponse(
var stream = try open(
std.testing.allocator,
test_stream.writer(),
&headers,