add util.comptimeToCrlf

This commit is contained in:
jaina heartles 2022-12-01 19:45:09 -08:00
parent 8400cd74fd
commit 04c593ffdd
4 changed files with 23 additions and 71 deletions

View file

@ -162,30 +162,9 @@ pub fn parseFormData(comptime T: type, boundary: []const u8, reader: anytype, al
return try ds.finish(alloc); return try ds.finish(alloc);
} }
fn toCrlf(comptime str: []const u8) []const u8 {
comptime {
var buf: [str.len * 2]u8 = undefined;
@setEvalBranchQuota(@intCast(u32, str.len * 2)); // TODO: why does this need to be *2
var buf_len: usize = 0;
for (str) |ch| {
if (ch == '\n') {
buf[buf_len] = '\r';
buf_len += 1;
}
buf[buf_len] = ch;
buf_len += 1;
}
return buf[0..buf_len];
}
}
// TODO: Fix these tests // TODO: Fix these tests
test "MultipartStream" { test "MultipartStream" {
const body = toCrlf( const body = util.comptimeToCrlf(
\\--abcd \\--abcd
\\Content-Disposition: form-data; name=first; charset=utf8 \\Content-Disposition: form-data; name=first; charset=utf8
\\ \\
@ -215,7 +194,7 @@ test "MultipartStream" {
} }
test "parseFormData" { test "parseFormData" {
const body = toCrlf( const body = util.comptimeToCrlf(
\\--abcd \\--abcd
\\Content-Disposition: form-data; name=foo \\Content-Disposition: form-data; name=foo
\\ \\

View file

@ -1,4 +1,5 @@
const std = @import("std"); const std = @import("std");
const util = @import("util");
const parser = @import("./parser.zig"); const parser = @import("./parser.zig");
const http = @import("../lib.zig"); const http = @import("../lib.zig");
const t = std.testing; const t = std.testing;
@ -30,30 +31,9 @@ const test_case = struct {
} }
}; };
fn toCrlf(comptime str: []const u8) []const u8 {
comptime {
var buf: [str.len * 2]u8 = undefined;
@setEvalBranchQuota(@intCast(u32, str.len * 2)); // TODO: why does this need to be *2
var buf_len: usize = 0;
for (str) |ch| {
if (ch == '\n') {
buf[buf_len] = '\r';
buf_len += 1;
}
buf[buf_len] = ch;
buf_len += 1;
}
return buf[0..buf_len];
}
}
test "HTTP/1.x parse - No body" { test "HTTP/1.x parse - No body" {
try test_case.parse( try test_case.parse(
toCrlf( util.comptimeToCrlf(
\\GET / HTTP/1.1 \\GET / HTTP/1.1
\\ \\
\\ \\
@ -65,7 +45,7 @@ test "HTTP/1.x parse - No body" {
}, },
); );
try test_case.parse( try test_case.parse(
toCrlf( util.comptimeToCrlf(
\\POST / HTTP/1.1 \\POST / HTTP/1.1
\\ \\
\\ \\
@ -77,7 +57,7 @@ test "HTTP/1.x parse - No body" {
}, },
); );
try test_case.parse( try test_case.parse(
toCrlf( util.comptimeToCrlf(
\\GET /url/abcd HTTP/1.1 \\GET /url/abcd HTTP/1.1
\\ \\
\\ \\
@ -89,7 +69,7 @@ test "HTTP/1.x parse - No body" {
}, },
); );
try test_case.parse( try test_case.parse(
toCrlf( util.comptimeToCrlf(
\\GET / HTTP/1.0 \\GET / HTTP/1.0
\\ \\
\\ \\
@ -101,7 +81,7 @@ test "HTTP/1.x parse - No body" {
}, },
); );
try test_case.parse( try test_case.parse(
toCrlf( util.comptimeToCrlf(
\\GET /url/abcd HTTP/1.1 \\GET /url/abcd HTTP/1.1
\\Content-Type: application/json \\Content-Type: application/json
\\ \\
@ -115,7 +95,7 @@ test "HTTP/1.x parse - No body" {
}, },
); );
try test_case.parse( try test_case.parse(
toCrlf( util.comptimeToCrlf(
\\GET /url/abcd HTTP/1.1 \\GET /url/abcd HTTP/1.1
\\Content-Type: application/json \\Content-Type: application/json
\\Authorization: bearer <token> \\Authorization: bearer <token>
@ -163,7 +143,7 @@ test "HTTP/1.x parse - No body" {
}, },
); );
try test_case.parse( try test_case.parse(
toCrlf( util.comptimeToCrlf(
\\GET / HTTP/1.2 \\GET / HTTP/1.2
\\ \\
\\ \\
@ -265,7 +245,7 @@ test "HTTP/1.x parse - bad requests" {
test "HTTP/1.x parse - Headers" { test "HTTP/1.x parse - Headers" {
try test_case.parse( try test_case.parse(
toCrlf( util.comptimeToCrlf(
\\GET /url/abcd HTTP/1.1 \\GET /url/abcd HTTP/1.1
\\Content-Type: application/json \\Content-Type: application/json
\\Content-Type: application/xml \\Content-Type: application/xml

View file

@ -1,4 +1,5 @@
const std = @import("std"); const std = @import("std");
const util = @import("util");
const http = @import("../lib.zig"); const http = @import("../lib.zig");
const Status = http.Status; const Status = http.Status;
@ -169,25 +170,7 @@ test {
_ = _tests; _ = _tests;
} }
const _tests = struct { const _tests = struct {
fn toCrlf(comptime str: []const u8) []const u8 { const toCrlf = util.comptimeToCrlf;
comptime {
var buf: [str.len * 2]u8 = undefined;
@setEvalBranchQuota(@as(u32, str.len * 2));
var len: usize = 0;
for (str) |ch| {
if (ch == '\n') {
buf[len] = '\r';
len += 1;
}
buf[len] = ch;
len += 1;
}
return buf[0..len];
}
}
const test_buffer_size = chunk_size * 4; const test_buffer_size = chunk_size * 4;
test "ResponseStream no headers empty body" { test "ResponseStream no headers empty body" {

View file

@ -205,6 +205,16 @@ pub fn seedThreadPrng() !void {
prng = std.rand.DefaultPrng.init(@bitCast(u64, buf)); prng = std.rand.DefaultPrng.init(@bitCast(u64, buf));
} }
pub fn comptimeToCrlf(comptime str: []const u8) []const u8 {
comptime {
@setEvalBranchQuota(str.len * 6);
const size = std.mem.replacementSize(u8, str, "\n", "\r\n");
var buf: [size]u8 = undefined;
_ = std.mem.replace(u8, str, "\n", "\r\n", &buf);
return &buf;
}
}
pub const testing = struct { pub const testing = struct {
pub fn expectDeepEqual(expected: anytype, actual: @TypeOf(expected)) !void { pub fn expectDeepEqual(expected: anytype, actual: @TypeOf(expected)) !void {
const T = @TypeOf(expected); const T = @TypeOf(expected);