diff --git a/src/http/multipart.zig b/src/http/multipart.zig index c52363b..8ba2f90 100644 --- a/src/http/multipart.zig +++ b/src/http/multipart.zig @@ -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 \\ diff --git a/src/http/request/test_parser.zig b/src/http/request/test_parser.zig index 55a66d6..b715528 100644 --- a/src/http/request/test_parser.zig +++ b/src/http/request/test_parser.zig @@ -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 @@ -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 diff --git a/src/http/server/response.zig b/src/http/server/response.zig index fdbe9cc..384677d 100644 --- a/src/http/server/response.zig +++ b/src/http/server/response.zig @@ -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" { diff --git a/src/util/lib.zig b/src/util/lib.zig index 1958922..7d2ef80 100644 --- a/src/util/lib.zig +++ b/src/util/lib.zig @@ -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);