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);
}
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
test "MultipartStream" {
const body = toCrlf(
const body = util.comptimeToCrlf(
\\--abcd
\\Content-Disposition: form-data; name=first; charset=utf8
\\
@ -215,7 +194,7 @@ test "MultipartStream" {
}
test "parseFormData" {
const body = toCrlf(
const body = util.comptimeToCrlf(
\\--abcd
\\Content-Disposition: form-data; name=foo
\\

View File

@ -1,4 +1,5 @@
const std = @import("std");
const util = @import("util");
const parser = @import("./parser.zig");
const http = @import("../lib.zig");
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" {
try test_case.parse(
toCrlf(
util.comptimeToCrlf(
\\GET / HTTP/1.1
\\
\\
@ -65,7 +45,7 @@ test "HTTP/1.x parse - No body" {
},
);
try test_case.parse(
toCrlf(
util.comptimeToCrlf(
\\POST / HTTP/1.1
\\
\\
@ -77,7 +57,7 @@ test "HTTP/1.x parse - No body" {
},
);
try test_case.parse(
toCrlf(
util.comptimeToCrlf(
\\GET /url/abcd HTTP/1.1
\\
\\
@ -89,7 +69,7 @@ test "HTTP/1.x parse - No body" {
},
);
try test_case.parse(
toCrlf(
util.comptimeToCrlf(
\\GET / HTTP/1.0
\\
\\
@ -101,7 +81,7 @@ test "HTTP/1.x parse - No body" {
},
);
try test_case.parse(
toCrlf(
util.comptimeToCrlf(
\\GET /url/abcd HTTP/1.1
\\Content-Type: application/json
\\
@ -115,7 +95,7 @@ test "HTTP/1.x parse - No body" {
},
);
try test_case.parse(
toCrlf(
util.comptimeToCrlf(
\\GET /url/abcd HTTP/1.1
\\Content-Type: application/json
\\Authorization: bearer <token>
@ -163,7 +143,7 @@ test "HTTP/1.x parse - No body" {
},
);
try test_case.parse(
toCrlf(
util.comptimeToCrlf(
\\GET / HTTP/1.2
\\
\\
@ -265,7 +245,7 @@ test "HTTP/1.x parse - bad requests" {
test "HTTP/1.x parse - Headers" {
try test_case.parse(
toCrlf(
util.comptimeToCrlf(
\\GET /url/abcd HTTP/1.1
\\Content-Type: application/json
\\Content-Type: application/xml

View File

@ -1,4 +1,5 @@
const std = @import("std");
const util = @import("util");
const http = @import("../lib.zig");
const Status = http.Status;
@ -169,25 +170,7 @@ test {
_ = _tests;
}
const _tests = struct {
fn toCrlf(comptime str: []const u8) []const u8 {
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 toCrlf = util.comptimeToCrlf;
const test_buffer_size = chunk_size * 4;
test "ResponseStream no headers empty body" {

View File

@ -205,6 +205,16 @@ pub fn seedThreadPrng() !void {
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 fn expectDeepEqual(expected: anytype, actual: @TypeOf(expected)) !void {
const T = @TypeOf(expected);