Skip to content

Lightpanda: the headless browser designed for AI and automation

`simd.zig` -> `network/header_parser.zig`

Halil Durak2ca2c44faf37
diff --git a/src/browser/webapi/net/FormData.zig b/src/browser/webapi/net/FormData.zig
index f120e7e0e..f784152f4 100644
--- a/src/browser/webapi/net/FormData.zig
+++ b/src/browser/webapi/net/FormData.zig
@@ -28,7 +28,7 @@ const Blob = @import("../Blob.zig");
const File = @import("../File.zig");
const Blob = @import("../Blob.zig");
const KeyValueList = @import("../KeyValueList.zig");
-const simd = @import("../../../simd.zig");
+const header_parser = @import("../../../network/header_parser.zig");
const log = lp.log;
const String = lp.String;
@@ -531,63 +531,64 @@ fn parseMultipart(self: *FormData, page: *Page, bytes: []const u8, boundary: []c
return error.InvalidFormData;
}
// Skip-past boundary.
- var cursor = bytes[2 + boundary.len ..];
+ const slice = bytes[2 + boundary.len ..];
- const double_dash: u16 = @bitCast([_]u8{ '-', '-' });
- const crlf: u16 = @bitCast([_]u8{ '\r', '\n' });
+ var cursor = header_parser.Cursor{
+ .idx = slice.ptr,
+ .end = slice.ptr + slice.len,
+ .start = slice.ptr,
+ };
while (true) {
- if (cursor.len < 2) {
+ if (!cursor.hasLength(2)) {
return error.InvalidFormData;
}
- const prefix: u16 = @bitCast(cursor[0..2].*);
// Check if we've reached the end.
- if (prefix == double_dash) {
+ if (cursor.peek2('-', '-')) {
return;
}
// If we haven't reached the end, CRLF is required.
- if (prefix != crlf) {
+ if (!cursor.peek2('\r', '\n')) {
return error.InvalidFormData;
}
// Consume prefix.
- cursor = cursor[2..];
+ cursor.advance(2);
// Content-Disposition can appear once a part, and is required.
- var disposition: ?simd.Disposition = null;
+ var disposition: ?header_parser.Disposition = null;
// Default Content-Type; can be overwritten while parsing headers.
var content_type: []const u8 = "text/plain";
// Reused for parsing headers.
- var header: simd.HttpHeader = undefined;
+ var header: header_parser.Header = undefined;
// Parse the whole header block; the content starts only after the
// terminating empty line.
while (true) {
- if (cursor.len == 0) {
+ if (cursor.reachedEnd()) {
return error.InvalidFormData;
}
// Check if headers part has finished.
- switch (cursor[0]) {
+ switch (cursor.char()) {
'\n' => {
// End of headers.
- cursor = cursor[1..];
+ cursor.advance(1);
break;
},
'\r' => {
- // We need a `\n` character too.
- if (cursor.len < 2 or cursor[1] != '\n') {
+ // We need an LF too.
+ if (!cursor.hasLength(2) or !cursor.peek2('\r', '\n')) {
return error.InvalidFormData;
}
-
// End of headers.
- cursor = cursor[2..];
+ cursor.advance(2);
break;
},
else => {},
}
- const consumed = try simd.parseHttpHeader(cursor, &header);
- cursor = cursor[consumed..];
+ // Parse an HTTP header.
+ try header.parse(&cursor);
if (std.ascii.eqlIgnoreCase(header.key, "content-type")) {
content_type = header.value;
@@ -596,16 +597,16 @@ fn parseMultipart(self: *FormData, page: *Page, bytes: []const u8, boundary: []c
// Content-Disposition found twice, report an error.
return error.InvalidFormData;
}
- disposition = try simd.parseDisposition(header.value);
+ disposition = try header_parser.parseDisposition(header.value);
}
}
const parsed = disposition orelse return error.InvalidFormData;
const name = try decodeMultipartName(self._arena, parsed.name orelse return error.InvalidFormData);
- const content_end = indexOfBoundary(cursor, boundary) orelse return error.InvalidFormData;
- const content = cursor[0..content_end];
- cursor = cursor[content_end + "\r\n--".len + boundary.len ..];
+ const content_end = indexOfBoundary(cursor.remaining(), boundary) orelse return error.InvalidFormData;
+ const content = cursor.remaining()[0..content_end];
+ cursor.advance(content_end + "\r\n--".len + boundary.len);
// Got a file.
if (parsed.filename) |filename| {
@@ -616,7 +617,7 @@ fn parseMultipart(self: *FormData, page: *Page, bytes: []const u8, boundary: []c
file.* = .{
._proto = blob,
._name = try blob._arena.dupe(u8, try decodeMultipartName(self._arena, filename)),
- ._last_modified = std.time.milliTimestamp(),
+ ._last_modified = std.Io.Clock.now(.real, lp.io).toMilliseconds(),
};
blob._type = .{ .file = file };
diff --git a/src/browser/webapi/net/Request.zig b/src/browser/webapi/net/Request.zig
index 3d4e18854..36e713a02 100644
--- a/src/browser/webapi/net/Request.zig
+++ b/src/browser/webapi/net/Request.zig
@@ -26,8 +26,10 @@ const URL = @import("../URL.zig");
const Page = @import("../../Page.zig");
const Blob = @import("../Blob.zig");
const AbortSignal = @import("../AbortSignal.zig");
+const ContentTypeIterator = @import("../../Mime.zig").ContentTypeIterator;
const Headers = @import("Headers.zig");
+const FormData = @import("FormData.zig");
const body_init = @import("body_init.zig");
const BodyInit = body_init.BodyInit;
@@ -298,21 +300,20 @@ pub fn bytes(self: *Request, exec: *const Execution) !js.Promise {
return local.resolvePromise(js.TypedArray(u8){ .values = self._body orelse "" });
}
-const FormData = @import("FormData.zig");
-const simd = @import("../../../simd.zig");
-
pub fn formData(self: *Request, exec: *const Execution) !js.Promise {
const local = exec.js.local.?;
if (self.consume(local)) |rejected| {
return rejected;
}
- // Should we consider body is consumed at this point?
- const body = self._body orelse return error.InvalidFormData;
+ // Per Fetch, a null body acts as an empty byte sequence.
+ const body = self._body orelse "";
const headers = try self.getHeaders(exec);
- const content_type = try headers.get("content-type", exec) orelse return error.InvalidFormData;
- var it = simd.ContentTypeIterator.init(content_type);
+ const content_type = try headers.get("content-type", exec) orelse {
+ return local.rejectPromise(.{ .type_error = "Failed to parse body as FormData" });
+ };
+ var it = ContentTypeIterator.init(content_type);
const essence = it.essence;
// [RFC7578]
@@ -320,7 +321,9 @@ pub fn formData(self: *Request, exec: *const Execution) !js.Promise {
// per the rules set forth in Returning Values from Forms: multipart/form-data.
if (std.ascii.eqlIgnoreCase(essence, "multipart/form-data")) {
const boundary = it.findBoundary();
- if (boundary.len == 0) return error.InvalidFormData;
+ if (boundary.len == 0) {
+ return local.rejectPromise(.{ .type_error = "Failed to parse body as FormData" });
+ }
const form_data = FormData.initFromMultipart(body, boundary, exec) catch |err| switch (err) {
error.OutOfMemory => return err,
diff --git a/src/browser/webapi/net/Response.zig b/src/browser/webapi/net/Response.zig
index cfdd464fe..55858fbf7 100644
--- a/src/browser/webapi/net/Response.zig
+++ b/src/browser/webapi/net/Response.zig
@@ -26,10 +26,13 @@ const Transfer = @import("../../../network/HttpClient.zig").Transfer;
const Blob = @import("../Blob.zig");
const ReadableStream = @import("../streams/ReadableStream.zig");
+const FormData = @import("FormData.zig");
const Headers = @import("Headers.zig");
const body_init = @import("body_init.zig");
+const ContentTypeIterator = @import("../../Mime.zig").ContentTypeIterator;
+
const Execution = js.Execution;
const Allocator = std.mem.Allocator;
@@ -456,9 +459,6 @@ pub fn bytes(self: *Response, exec: *const Execution) !js.Promise {
return local.resolvePromise(js.TypedArray(u8){ .values = body });
}
-const FormData = @import("FormData.zig");
-const simd = @import("../../../simd.zig");
-
pub fn formData(self: *Response, exec: *const Execution) !js.Promise {
const local = exec.js.local.?;
if (self.consume(local)) |rejected| return rejected;
@@ -468,8 +468,10 @@ pub fn formData(self: *Response, exec: *const Execution) !js.Promise {
.stream => return local.rejectPromise(.{ .type_error = "Cannot read FormData from stream body" }),
};
- const content_type = try self._headers.get("content-type", exec) orelse return error.InvalidFormData;
- var it = simd.ContentTypeIterator.init(content_type);
+ const content_type = try self._headers.get("content-type", exec) orelse {
+ return local.rejectPromise(.{ .type_error = "Failed to parse body as FormData" });
+ };
+ var it = ContentTypeIterator.init(content_type);
const essence = it.essence;
// [RFC7578]
@@ -477,7 +479,9 @@ pub fn formData(self: *Response, exec: *const Execution) !js.Promise {
// per the rules set forth in Returning Values from Forms: multipart/form-data.
if (std.ascii.eqlIgnoreCase(essence, "multipart/form-data")) {
const boundary = it.findBoundary();
- if (boundary.len == 0) return error.InvalidFormData;
+ if (boundary.len == 0) {
+ return local.rejectPromise(.{ .type_error = "Failed to parse body as FormData" });
+ }
const form_data = FormData.initFromMultipart(body, boundary, exec) catch |err| switch (err) {
error.OutOfMemory => return err,
diff --git a/src/network/header_parser.zig b/src/network/header_parser.zig
new file mode 100644
index 000000000..d3fce0a6b
--- /dev/null
+++ b/src/network/header_parser.zig
@@ -0,0 +1,539 @@
+// Copyright (C) 2023-2026 Lightpanda (Selecy SAS)
+//
+// Francis Bouvier <[email protected]>
+// Pierre Tachoire <[email protected]>
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as
+// published by the Free Software Foundation, either version 3 of the
+// License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+const std = @import("std");
+const builtin = @import("builtin");
+
+/// Block size of the CPU.
+const block_size = @sizeOf(usize);
+
+/// Whether a byte is allowed in a header key; controls (including space),
+/// the `:` delimiter and DEL are excluded.
+inline fn isHeaderKeyByte(byte: u8) bool {
+ return switch (byte) {
+ 0...' ', ':', 0x7f => false,
+ else => true,
+ };
+}
+
+/// Whether a byte is allowed in a header value; controls (excluding space)
+/// and DEL are excluded.
+inline fn isHeaderValueByte(byte: u8) bool {
+ return switch (byte) {
+ 0...0x1f, 0x7f => false,
+ else => true,
+ };
+}
+
+/// Returns an integer filled with a given byte.
+inline fn broadcast(comptime T: type, byte: u8) T {
+ comptime {
+ const bits = @ctz(@as(T, 0));
+ const b = @as(T, byte);
+ return switch (bits) {
+ 8 => b * 0x01,
+ 16 => b * 0x01_01,
+ 32 => b * 0x01_01_01_01,
+ 64 => b * 0x01_01_01_01_01_01_01_01,
+ else => @compileError("unexpected broadcast size"),
+ };
+ }
+}
+
+inline fn matchHeaderKey(cursor: *Cursor) void {
+ // Pick a good default for relatively small strings.
+ const vec_size = 16;
+ const use_vectors = comptime if (std.simd.suggestVectorLength(u8)) |recommended|
+ recommended >= vec_size
+ else
+ false;
+
+ if (comptime use_vectors) {
+ const Vec = @Vector(vec_size, u8);
+ const Int = @Int(.unsigned, vec_size);
+
+ while (cursor.hasLength(vec_size)) {
+ const spaces: Vec = @splat(' ');
+ const colons: Vec = @splat(':');
+ const deletes: Vec = @splat(0x7f);
+ const chunk = cursor.asVector(vec_size);
+
+ // Per-lane `isHeaderKeyByte`: a lane is 1 when its byte is valid,
+ // above space (which also rules out the controls) and neither the
+ // `:` delimiter nor DEL. Inverting the mask makes invalid lanes
+ // 1s, so `@ctz` yields the index of the first invalid byte, or
+ // `vec_size` when the whole chunk is valid.
+ const mask = @intFromBool(chunk > spaces) & ~(@intFromBool(chunk == colons) | @intFromBool(chunk == deletes));
+ const advance_by = @ctz(~@as(Int, @bitCast(mask)));
+ // Advance.
+ cursor.advance(advance_by);
+ if (advance_by != vec_size) {
+ return;
+ }
+ }
+ }
+
+ // NOTE: SWAR is not preferred here, this might change in the future
+ // but honestly header keys are not so long.
+
+ // Fallback for len < `vec_size`.
+ while (cursor.end - cursor.idx > 0) : (cursor.advance(1)) {
+ if (isHeaderKeyByte(cursor.char()) == false) {
+ return;
+ }
+ }
+}
+
+inline fn matchHeaderValue(cursor: *Cursor) void {
+ const maybe_vec_size: ?usize = comptime blk: {
+ if (std.simd.suggestVectorLength(u8)) |recommended| {
+ break :blk if (recommended >= 64) 32 else recommended;
+ }
+ break :blk null;
+ };
+
+ if (comptime maybe_vec_size) |vec_size| {
+ const Vec = @Vector(vec_size, u8);
+ const Int = @Int(.unsigned, vec_size);
+
+ while (cursor.hasLength(vec_size)) {
+ // Fill a vector with DEL (127).
+ const deletes: Vec = @splat(0x7f);
+ // Fill a vector with US (31).
+ const full_31: Vec = @splat(0x1f);
+ // Load the next chunk from the buffer.
+ const chunk = cursor.asVector(vec_size);
+
+ // Per-lane `isHeaderValueByte`: a lane is 1 when its byte is
+ // valid, above US (which rules out the controls but keeps space)
+ // and not DEL. Inverting the mask makes invalid lanes 1s, so
+ // `@ctz` yields the index of the first invalid byte, or
+ // `vec_size` when the whole chunk is valid.
+ const mask = @intFromBool(chunk > full_31) & ~@intFromBool(chunk == deletes);
+ const advance_by = @ctz(~@as(Int, @bitCast(mask)));
+ cursor.advance(advance_by);
+ if (advance_by != vec_size) {
+ return;
+ }
+ }
+ }
+
+ // SWAR path.
+ while (cursor.hasLength(block_size)) {
+ const spaces = comptime broadcast(usize, ' ');
+ const ones = comptime broadcast(usize, 0x01);
+ const dels = comptime broadcast(usize, 0x7f);
+ const full_128 = comptime broadcast(usize, 128);
+ const chunk = cursor.asInteger(usize);
+
+ // When a byte is less than a space (32), subtraction will wrap around
+ // and set the high bit; the AND NOT makes sure only bytes below 128
+ // can report so.
+ const lt = (chunk -% spaces) & ~chunk;
+ const xor_dels = chunk ^ dels;
+ const eq_del = (xor_dels -% ones) & ~xor_dels;
+ const advance_by = @ctz((lt | eq_del) & full_128) >> 3;
+
+ cursor.advance(advance_by);
+ if (advance_by != block_size) {
+ return;
+ }
+ }
+
+ while (cursor.end - cursor.idx > 0) : (cursor.advance(1)) {
+ if (isHeaderValueByte(cursor.char()) == false) {
+ return;
+ }
+ }
+}
+
+/// Represents a single HTTP header.
+pub const Header = struct {
+ key: []const u8,
+ value: []const u8,
+
+ pub const ParseError = error{ Incomplete, Invalid };
+
+ pub fn parse(header: *Header, cursor: *Cursor) ParseError!void {
+ const key_start = cursor.current();
+ matchHeaderKey(cursor);
+ const key_end = cursor.current();
+
+ // Buffer has been consumed fully without a delimiter; the caller can
+ // read more data and try to parse again. Checked before `char`, which
+ // reads unchecked.
+ if (cursor.reachedEnd()) {
+ return error.Incomplete;
+ }
+
+ switch (cursor.char()) {
+ ':' => {
+ @branchHint(.likely);
+ // 0 length header key, which is invalid.
+ if (key_end == key_start) {
+ return error.Invalid;
+ }
+ cursor.advance(1);
+ },
+ // Invalid character, so a malformed header. Can't go further.
+ else => return error.Invalid,
+ }
+
+ // Get rid of leading spaces if there are any.
+ cursor.skipSpaces();
+
+ // Found where header value starts.
+ const val_start = cursor.current();
+ matchHeaderValue(cursor);
+ const val_end = cursor.current();
+
+ // Buffer has been consumed fully without a line ending; the caller
+ // can read more data and try to parse again.
+ if (cursor.reachedEnd()) {
+ return error.Incomplete;
+ }
+
+ // Both LF and CRLF indicate the end of the value part.
+ switch (cursor.char()) {
+ '\n' => cursor.advance(1),
+ '\r' => {
+ // We need an LF too.
+ if (!cursor.hasLength(2)) {
+ return error.Incomplete;
+ }
+ if (!cursor.peek2('\r', '\n')) {
+ @branchHint(.unlikely);
+ return error.Invalid;
+ }
+ cursor.advance(2);
+ },
+ // Any other character is invalid.
+ else => return error.Invalid,
+ }
+
+ header.* = .{
+ .key = key_start[0 .. key_end - key_start],
+ .value = val_start[0 .. val_end - val_start],
+ };
+ }
+};
+
+pub const Disposition = struct {
+ name: ?[]const u8 = null,
+ filename: ?[]const u8 = null,
+};
+
+// Parses a part's Content-Disposition value: `form-data` followed by
+// `; key="value"` params. Values are quoted-strings whose CR/LF/" were
+// percent-escaped by the encoder (writeMultipartName), so the next raw '"'
+// always closes the value; a raw ';' inside quotes is legal and preserved.
+// Parameters that are not `key="quoted"` pairs — e.g. RFC 5987
+// `filename*=UTF-8''...`, which real servers emit — are skipped rather than
+// failing the whole parse, matching browser leniency.
+pub fn parseDisposition(value: []const u8) !Disposition {
+ var rest = std.mem.trim(u8, value, " \t");
+ if (!std.ascii.startsWithIgnoreCase(rest, "form-data")) {
+ return error.InvalidFormData;
+ }
+ rest = rest["form-data".len..];
+
+ var disposition = Disposition{};
+ while (true) {
+ rest = std.mem.trimStart(u8, rest, " \t");
+ if (rest.len == 0) {
+ return disposition;
+ }
+ if (rest[0] != ';') {
+ return error.InvalidFormData;
+ }
+ rest = std.mem.trimStart(u8, rest[1..], " \t");
+
+ // An unquoted value cannot contain a `;`, so the next one (or the end
+ // of the header) bounds this parameter when it has to be skipped.
+ const semi = std.mem.indexOfScalar(u8, rest, ';') orelse rest.len;
+ const eq = std.mem.indexOfScalar(u8, rest, '=') orelse rest.len;
+ if (eq >= semi) {
+ // No `=` in this parameter; skip it.
+ rest = rest[semi..];
+ continue;
+ }
+ const key = rest[0..eq];
+ const after_eq = rest[eq + 1 ..];
+
+ if (after_eq.len == 0 or after_eq[0] != '"') {
+ // Unquoted value; skip it.
+ rest = rest[semi..];
+ continue;
+ }
+ const end = std.mem.indexOfScalarPos(u8, after_eq, 1, '"') orelse {
+ // Unterminated quoted value; ignore the rest of the header.
+ return disposition;
+ };
+ const param_value = after_eq[1..end];
+ rest = after_eq[end + 1 ..];
+
+ if (std.ascii.eqlIgnoreCase(key, "name")) {
+ disposition.name = param_value;
+ } else if (std.ascii.eqlIgnoreCase(key, "filename")) {
+ disposition.filename = param_value;
+ }
+ }
+}
+
+/// Helper for wandering around & parsing things along the way.
+pub const Cursor = struct {
+ /// Pointer to current position of cursor.
+ idx: [*]const u8,
+ /// Pointer to end of the buffer.
+ end: [*]const u8,
+ /// Pointer to start of the buffer.
+ start: [*]const u8,
+
+ /// Returns the current position.
+ pub inline fn current(cursor: *const Cursor) [*]const u8 {
+ return cursor.idx;
+ }
+
+ /// Returns the current character.
+ pub inline fn char(cursor: *const Cursor) u8 {
+ return cursor.idx[0];
+ }
+
+ /// Advances the position of the cursor by given value.
+ /// SAFETY: This function doesn't check if out of bounds reachable.
+ pub inline fn advance(cursor: *Cursor, by: usize) void {
+ cursor.idx += by;
+ }
+
+ /// Checks if buffer has `len` length of characters.
+ /// `(cursor.end - cursor.idx >= len)`
+ pub inline fn hasLength(cursor: *const Cursor, len: usize) bool {
+ return cursor.end - cursor.idx >= len;
+ }
+
+ /// Loads a `@Vector(len, u8)` from the current position of cursor without advancing.
+ /// SAFETY: This function doesn't check if out of bounds reachable.
+ pub inline fn asVector(cursor: *const Cursor, len: comptime_int) @Vector(len, u8) {
+ return cursor.idx[0..len].*;
+ }
+
+ /// Creates an integer from the current position of the cursor without advancing.
+ /// SAFETY: This function doesn't check if out of bounds reachable.
+ /// SAFETY: T must be an integer with bit size >= @bitSizeOf(u8).
+ pub inline fn asInteger(cursor: *const Cursor, comptime T: type) T {
+ return @bitCast(cursor.idx[0 .. @bitSizeOf(T) / @bitSizeOf(u8)].*);
+ }
+
+ /// Peek the current and the next but don't advance.
+ /// SAFETY: This function doesn't check if out of bounds reachable.
+ pub inline fn peek2(cursor: *const Cursor, c0: u8, c1: u8) bool {
+ return cursor.asInteger(u16) == @as(u16, @bitCast([2]u8{ c0, c1 }));
+ }
+
+ /// Moves the cursor until no leading spaces there are.
+ pub inline fn skipSpaces(cursor: *Cursor) void {
+ while (cursor.end - cursor.current() > 0 and cursor.char() == ' ') : (cursor.advance(1)) {}
+ }
+
+ /// Returns true if `Cursor` reached end.
+ pub inline fn reachedEnd(cursor: *const Cursor) bool {
+ return cursor.idx == cursor.end;
+ }
+
+ /// Returns the unread portion of the buffer.
+ pub inline fn remaining(cursor: *const Cursor) []const u8 {
+ return cursor.idx[0 .. cursor.end - cursor.idx];
+ }
+};
+
+const testing = @import("../testing.zig");
+
+fn initCursor(bytes: []const u8) Cursor {
+ return .{ .idx = bytes.ptr, .end = bytes.ptr + bytes.len, .start = bytes.ptr };
+}
+
+fn consumed(cursor: *const Cursor) usize {
+ return cursor.idx - cursor.start;
+}
+
+test "header_parser: parse HTTP header" {
+ const bytes = "Content-Disposition: attachment; filename*=UTF-8''file%20name.jpg\r\nrest";
+ var cursor = initCursor(bytes);
+ var header: Header = undefined;
+ try header.parse(&cursor);
+
+ try testing.expectEqual(bytes.len - "rest".len, consumed(&cursor));
+ try testing.expectString("Content-Disposition", header.key);
+ try testing.expectString("attachment; filename*=UTF-8''file%20name.jpg", header.value);
+
+ // Lone `\n` line endings are accepted too; the cursor is left at the next
+ // header, so parsing can continue from there.
+ cursor = initCursor("Host: abc\nHost: def\n");
+ try header.parse(&cursor);
+ try testing.expectEqual(10, consumed(&cursor));
+ try testing.expectString("Host", header.key);
+ try testing.expectString("abc", header.value);
+ try header.parse(&cursor);
+ try testing.expectString("def", header.value);
+ try testing.expectEqual(true, cursor.reachedEnd());
+
+ // Leading whitespaces of the value are skipped, trailing ones are kept.
+ cursor = initCursor("Key: value \r\n");
+ try header.parse(&cursor);
+ try testing.expectString("value ", header.value);
+
+ // 0 length values are fine.
+ cursor = initCursor("Key:\r\n");
+ try header.parse(&cursor);
+ try testing.expectString("Key", header.key);
+ try testing.expectString("", header.value);
+}
+
+test "header_parser: parse HTTP header incomplete" {
+ var header: Header = undefined;
+
+ // Buffer may end anywhere before the line ending is complete.
+ const bytes = "Content-Type: text/plain\r\n";
+ for (0..bytes.len) |len| {
+ var cursor = initCursor(bytes[0..len]);
+ try testing.expectError(error.Incomplete, header.parse(&cursor));
+ }
+ // The lone `\n` variant completes a byte earlier.
+ var cursor = initCursor("Content-Type: text/plain\n");
+ try header.parse(&cursor);
+ try testing.expectEqual(true, cursor.reachedEnd());
+}
+
+test "header_parser: parse HTTP header invalid" {
+ var header: Header = undefined;
+
+ const cases = [_][]const u8{
+ ": value\r\n", // 0 length key
+ "Key\x01: value\r\n", // invalid character in the key
+ "Key name: value\r\n", // space in the key
+ "Key: val\x01ue\r\n", // invalid character in the value
+ "Key: val\x7fue\r\n", // DEL in the value
+ "Key: value\rX\n", // `\r` must be followed by `\n`
+ };
+ for (cases) |case| {
+ var cursor = initCursor(case);
+ try testing.expectError(error.Invalid, header.parse(&cursor));
+ }
+}
+
+test "header_parser: parse Content-Disposition" {
+ var d = try parseDisposition("form-data; name=\"a\"; filename=\"b.txt\"");
+ try testing.expectString("a", d.name.?);
+ try testing.expectString("b.txt", d.filename.?);
+
+ // Type and parameter names are case-insensitive; surrounding whitespace
+ // is tolerated.
+ d = try parseDisposition(" Form-Data; NAME=\"x\" ");
+ try testing.expectString("x", d.name.?);
+ try testing.expectEqual(null, d.filename);
+
+ // No parameters at all is fine; both fields stay null.
+ d = try parseDisposition("form-data");
+ try testing.expectEqual(null, d.name);
+ try testing.expectEqual(null, d.filename);
+
+ // A raw ';' inside a quoted value is preserved; unknown parameters are
+ // skipped.
+ d = try parseDisposition("form-data; foo=\"bar\"; name=\"a;b\"");
+ try testing.expectString("a;b", d.name.?);
+
+ // Parameters without `=` or with an unquoted value — e.g. RFC 5987
+ // `filename*` — are skipped without failing the parse; later well-formed
+ // parameters still apply.
+ d = try parseDisposition("form-data; filename*=UTF-8''file%20name.jpg; name=\"n\"");
+ try testing.expectString("n", d.name.?);
+ try testing.expectEqual(null, d.filename);
+ d = try parseDisposition("form-data; foo; name=bare; filename=\"f.txt\"");
+ try testing.expectEqual(null, d.name);
+ try testing.expectString("f.txt", d.filename.?);
+
+ // An unterminated quoted value ends the parse, keeping what was found.
+ d = try parseDisposition("form-data; name=\"a\"; filename=\"f");
+ try testing.expectString("a", d.name.?);
+ try testing.expectEqual(null, d.filename);
+
+ // Not form-data at all, or a parameter without a `;` separator, is
+ // rejected.
+ try testing.expectError(error.InvalidFormData, parseDisposition("attachment; name=\"a\""));
+ try testing.expectError(error.InvalidFormData, parseDisposition("form-data name=\"a\""));
+}
+
+test "header_parser: cursor" {
+ var cursor = initCursor(" ab\r\ncd");
+ try testing.expectEqual(8, cursor.remaining().len);
+ cursor.skipSpaces();
+ try testing.expectEqual(2, consumed(&cursor));
+ try testing.expectEqual('a', cursor.char());
+ try testing.expectEqual(true, cursor.peek2('a', 'b'));
+ try testing.expectEqual(false, cursor.peek2('a', 'c'));
+ try testing.expectEqual(true, cursor.hasLength(6));
+ try testing.expectEqual(false, cursor.hasLength(7));
+ cursor.advance(6);
+ try testing.expectEqual(true, cursor.reachedEnd());
+ try testing.expectString("", cursor.remaining());
+
+ // Skipping spaces stops at the end of the buffer.
+ cursor = initCursor(" ");
+ cursor.skipSpaces();
+ try testing.expectEqual(true, cursor.reachedEnd());
+}
+
+fn expectMatchesReference(bytes: []const u8) !void {
+ var key_expected: usize = 0;
+ while (key_expected < bytes.len and isHeaderKeyByte(bytes[key_expected])) key_expected += 1;
+ var value_expected: usize = 0;
+ while (value_expected < bytes.len and isHeaderValueByte(bytes[value_expected])) value_expected += 1;
+
+ var cursor = initCursor(bytes);
+ matchHeaderKey(&cursor);
+ try testing.expectEqual(key_expected, consumed(&cursor));
+
+ cursor = initCursor(bytes);
+ matchHeaderValue(&cursor);
+ try testing.expectEqual(value_expected, consumed(&cursor));
+}
+
+test "header_parser: match functions against scalar reference" {
+ // Exhaustive: place every possible byte at every position of an otherwise
+ // valid buffer, across lengths covering the vector, SWAR and scalar paths.
+ var buf: [48]u8 = undefined;
+ for (1..buf.len + 1) |len| {
+ for (0..len) |pos| {
+ for (0..256) |c| {
+ @memset(buf[0..len], 'a');
+ buf[pos] = @intCast(c);
+ try expectMatchesReference(buf[0..len]);
+ }
+ }
+ }
+
+ // Randomized: fully random buffers to exercise multiple invalid bytes per
+ // chunk at once.
+ var prng = std.Random.DefaultPrng.init(0x5eed);
+ const random = prng.random();
+ for (0..20_000) |_| {
+ const len = random.intRangeAtMost(usize, 0, buf.len);
+ random.bytes(buf[0..len]);
+ try expectMatchesReference(buf[0..len]);
+ }
+}
diff --git a/src/simd.zig b/src/simd.zig
deleted file mode 100644
index 1e062be88..000000000
--- a/src/simd.zig
+++ /dev/null
@@ -1,549 +0,0 @@
-// Copyright (C) 2023-2026 Lightpanda (Selecy SAS)
-//
-// Francis Bouvier <[email protected]>
-// Pierre Tachoire <[email protected]>
-//
-// This program is free software: you can redistribute it and/or modify
-// it under the terms of the GNU Affero General Public License as
-// published by the Free Software Foundation, either version 3 of the
-// License, or (at your option) any later version.
-//
-// This program is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU Affero General Public License for more details.
-//
-// You should have received a copy of the GNU Affero General Public License
-// along with this program. If not, see <https://www.gnu.org/licenses/>.
-
-//! We have multiple places that are in need of same parsing requirements
-//! with same efficiency. This file tries to unite them under here.
-
-const std = @import("std");
-const builtin = @import("builtin");
-
-/// Block size of the CPU.
-const block_size = @sizeOf(usize);
-
-const header_key_map = [256]u1{
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0-15
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16-31
- 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 32-47
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, // 48-63
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 64-79
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 80-95
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 96-111
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, // 112-127
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 128-143
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 144-159
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 160-175
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 176-191
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 192-207
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 208-223
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 224-239
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 240-255
-};
-
-const header_value_map = [256]u1{
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0-15
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16-31
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 32-47
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 48-63
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 64-79
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 80-95
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 96-111
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, // 112-127
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 128-143
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 144-159
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 160-175
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 176-191
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 192-207
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 208-223
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 224-239
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 240-255
-};
-
-/// Returns an integer filled with a given byte.
-inline fn broadcast(comptime T: type, byte: u8) T {
- comptime {
- const bits = @ctz(@as(T, 0));
- const b = @as(T, byte);
- return switch (bits) {
- 8 => b * 0x01,
- 16 => b * 0x01_01,
- 32 => b * 0x01_01_01_01,
- 64 => b * 0x01_01_01_01_01_01_01_01,
- else => @compileError("unexpected broadcast size"),
- };
- }
-}
-
-/// Returns how much to move forward for the end of header key; the byte at
-/// the returned index is either an invalid one or the `:` delimiter. If the
-/// returned index is equal to `bytes.len`, the buffer has been consumed fully.
-/// Force this to inline, there's merely a single call-site for it.
-inline fn matchHeaderKey(bytes: []const u8) usize {
- // How much we've moved forward.
- var i: usize = 0;
-
- const use_vectors = if (std.simd.suggestVectorLength(u8)) |recommended|
- recommended >= 16
- else
- false;
-
- if (comptime use_vectors) {
- // Pick a good default for relatively small strings.
- const vec_size = 16;
- const Vec = @Vector(vec_size, u8);
- const Int = std.meta.Int(.unsigned, vec_size);
-
- while (bytes.len - i >= vec_size) {
- const spaces: Vec = @splat(' ');
- const colons: Vec = @splat(':');
- const deletes: Vec = @splat(0x7f);
- const chunk: Vec = bytes[i..][0..vec_size].*;
-
- const bits = @intFromBool(chunk > spaces) & ~(@intFromBool(chunk == colons) | @intFromBool(chunk == deletes));
- // How much to move forward.
- const advance_by = @ctz(~@as(Int, @bitCast(bits)));
- i += advance_by;
- // Found one of the characters we'd like to have.
- if (advance_by != vec_size) {
- return i;
- }
- }
- }
-
- // NOTE: SWAR is not preferred here, this might change in the future
- // but honestly header keys are not so large.
-
- // Fallback for len < sse_vec_size.
- while (i < bytes.len) : (i += 1) {
- if (header_key_map[bytes[i]] == 0) {
- return i;
- }
- }
-
- return i;
-}
-
-/// Returns how much to move forward for the end of header value; the byte at
-/// the returned index is either an invalid one or a line ending. If the
-/// returned index is equal to `bytes.len`, the buffer has been consumed fully.
-/// Force this to inline, there's merely a single call-site for it.
-inline fn matchHeaderValue(bytes: []const u8) usize {
- // How much we've moved forward.
- var i: usize = 0;
-
- const maybe_vec_size: ?usize = comptime blk: {
- if (std.simd.suggestVectorLength(u8)) |recommended| {
- break :blk if (recommended >= 64) 32 else recommended;
- }
- break :blk null;
- };
-
- // Unlike header keys, prefer larger vectors initially when validating
- // header values if possible.
- if (comptime maybe_vec_size) |vec_size| {
- const Vec = @Vector(vec_size, u8);
- const Int = std.meta.Int(.unsigned, vec_size);
-
- while (bytes.len - i >= vec_size) {
- // Fill a vector with DEL (127).
- const deletes: Vec = @splat(0x7f);
- // Fill a vector with US (31).
- const full_31: Vec = @splat(0x1f);
- // Load the next chunk from the buffer.
- const chunk: Vec = bytes[i..][0..vec_size].*;
-
- const bits = @intFromBool(chunk > full_31) & ~@intFromBool(chunk == deletes);
- const advance_by = @ctz(~@as(Int, @bitCast(bits)));
- i += advance_by;
-
- if (advance_by != vec_size) {
- return i;
- }
- }
- }
-
- // SWAR path.
- while (bytes.len - i >= block_size) {
- const spaces = comptime broadcast(usize, ' ');
- const ones = comptime broadcast(usize, 0x01);
- const dels = comptime broadcast(usize, 0x7f);
- const full_128 = comptime broadcast(usize, 128);
- const chunk: usize = @bitCast(bytes[i..][0..block_size].*);
-
- // When a byte is less than a space (32), subtraction will wrap around
- // and set the high bit; the AND NOT makes sure only bytes below 128
- // can report so.
- const lt = (chunk -% spaces) & ~chunk;
- const xor_dels = chunk ^ dels;
- const eq_del = (xor_dels -% ones) & ~xor_dels;
- const advance_by = @ctz((lt | eq_del) & full_128) >> 3;
- i += advance_by;
-
- if (advance_by != block_size) {
- return i;
- }
- }
-
- // Fallback for len < block_size.
- while (i < bytes.len) : (i += 1) {
- if (header_value_map[bytes[i]] == 0) {
- return i;
- }
- }
-
- return i;
-}
-
-/// Represents a single HTTP header.
-pub const HttpHeader = struct {
- key: []const u8,
- value: []const u8,
-};
-
-pub const ParseHttpHeaderError = error{ Incomplete, Invalid };
-
-/// Parses a single HTTP header out of given buffer and returns how many bytes
-/// are consumed, including the line ending. The buffer MAY or MAY NOT have a
-/// line ending; in case it doesn't exist, `error.Incomplete` is returned.
-/// Which allows streaming (or push-style) usage of this function.
-pub fn parseHttpHeader(bytes: []const u8, header: *HttpHeader) ParseHttpHeaderError!usize {
- var cursor = bytes;
-
- const key_end = matchHeaderKey(cursor);
- // Buffer has been consumed fully without a delimiter; the caller can read
- // more data and try to parse again.
- if (key_end == cursor.len) {
- return error.Incomplete;
- }
-
- // Make sure we're at colon.
- switch (cursor[key_end]) {
- ':' => {
- @branchHint(.likely);
- // 0 length headers are invalid.
- if (key_end == 0) {
- return error.Invalid;
- }
- },
- // Invalid character, so a malformed header. Can't go further.
- else => return error.Invalid,
- }
-
- // Found header key.
- const key = cursor[0..key_end];
- // Skip the key and the colon.
- cursor = cursor[key_end + 1 ..];
-
- // Skip leading whitespaces.
- while (cursor.len > 0 and cursor[0] == ' ') : (cursor = cursor[1..]) {}
-
- // We're at where header value starts; find where it ends.
- const value_end = matchHeaderValue(cursor);
- // Buffer has been consumed fully without a line ending; the caller can
- // read more data and try to parse again.
- if (value_end == cursor.len) {
- return error.Incomplete;
- }
-
- // Found header value.
- const value = cursor[0..value_end];
-
- // Both `\n` and `\r\n` indicate the end of the value part.
- switch (cursor[value_end]) {
- '\n' => cursor = cursor[value_end + 1 ..],
- '\r' => {
- // We need a `\n` character too.
- if (value_end + 1 == cursor.len) {
- return error.Incomplete;
- }
- if (cursor[value_end + 1] != '\n') {
- @branchHint(.unlikely);
- return error.Invalid;
- }
-
- cursor = cursor[value_end + 2 ..];
- },
- // Any other character is invalid.
- else => return error.Invalid,
- }
-
- // Header is set.
- header.* = .{ .key = key, .value = value };
-
- // Return the total consumed length to caller.
- return bytes.len - cursor.len;
-}
-
-pub const Disposition = struct {
- name: ?[]const u8 = null,
- filename: ?[]const u8 = null,
-};
-
-// Parses a part's Content-Disposition value: `form-data` followed by
-// `; key="value"` params. Values are quoted-strings whose CR/LF/" were
-// percent-escaped by the encoder (writeMultipartName), so the next raw '"'
-// always closes the value; a raw ';' inside quotes is legal and preserved.
-pub fn parseDisposition(value: []const u8) !Disposition {
- var rest = std.mem.trim(u8, value, " \t");
- if (!std.ascii.startsWithIgnoreCase(rest, "form-data")) {
- return error.InvalidFormData;
- }
- rest = rest["form-data".len..];
-
- var disposition = Disposition{};
- while (true) {
- rest = std.mem.trimLeft(u8, rest, " \t");
- if (rest.len == 0) {
- return disposition;
- }
- if (rest[0] != ';') {
- return error.InvalidFormData;
- }
- rest = std.mem.trimLeft(u8, rest[1..], " \t");
-
- const eq = std.mem.indexOfScalar(u8, rest, '=') orelse return error.InvalidFormData;
- const key = rest[0..eq];
- rest = rest[eq + 1 ..];
- if (rest.len == 0 or rest[0] != '"') {
- return error.InvalidFormData;
- }
- const end = std.mem.indexOfScalarPos(u8, rest, 1, '"') orelse return error.InvalidFormData;
- const param_value = rest[1..end];
- rest = rest[end + 1 ..];
-
- if (std.ascii.eqlIgnoreCase(key, "name")) {
- disposition.name = param_value;
- } else if (std.ascii.eqlIgnoreCase(key, "filename")) {
- disposition.filename = param_value;
- }
- }
-}
-
-pub const ContentTypeIterator = struct {
- /// Header value.
- rest: []const u8,
- /// Always the first.
- essence: []const u8,
-
- /// Initializes a `ContentTypeIterator`.
- pub fn init(content_type: []const u8) ContentTypeIterator {
- // Skip whitespace.
- const trimmed = std.mem.trimStart(u8, content_type, &.{ ' ', '\t' });
- // Find semicolon delimiter; or just use the end position.
- const end = std.mem.indexOfScalar(u8, trimmed, ';') orelse trimmed.len;
- const essence = std.mem.trimEnd(u8, trimmed[0..end], &.{ ' ', '\t' });
-
- // Rest of the parameters.
- const rest = trimmed[end..];
- return .{ .rest = rest, .essence = essence };
- }
-
- pub const Parameter = struct {
- key: []const u8,
- /// `value` can be an empty string ("").
- value: []const u8,
- };
-
- /// Returns the next parameter or null if there aren't any parameters.
- pub fn next(self: *ContentTypeIterator) ?Parameter {
- while (self.rest.len > 0) {
- // `rest` always sits at the `;` that introduced this parameter.
- var param = self.rest[1..];
- const end = std.mem.indexOfScalar(u8, param, ';') orelse param.len;
- self.rest = param[end..];
- param = std.mem.trim(u8, param[0..end], " \t");
-
- // Parameters without `=` are malformed; skip them.
- const eq = std.mem.indexOfScalar(u8, param, '=') orelse continue;
- const key = std.mem.trimEnd(u8, param[0..eq], " \t");
- if (key.len == 0) {
- continue;
- }
-
- var value = std.mem.trimStart(u8, param[eq + 1 ..], " \t");
- if (value.len >= 2 and value[0] == '"' and value[value.len - 1] == '"') {
- value = value[1 .. value.len - 1];
- }
- return .{ .key = key, .value = value };
- }
-
- return null;
- }
-
- /// Returns boundary string; consuming other parameters along the way.
- /// Boundary can be an empty string.
- pub fn findBoundary(self: *ContentTypeIterator) []const u8 {
- while (self.next()) |p| {
- if (std.ascii.eqlIgnoreCase(p.key, "boundary")) {
- return p.value;
- }
- }
- return "";
- }
-};
-
-const testing = @import("testing.zig");
-test "simd: parse HTTP header" {
- const bytes = "Content-Disposition: attachment; filename*=UTF-8''file%20name.jpg\r\nrest";
- var header: HttpHeader = undefined;
- const consumed = try parseHttpHeader(bytes, &header);
-
- try testing.expectEqual(bytes.len - "rest".len, consumed);
- try testing.expectString("Content-Disposition", header.key);
- try testing.expectString("attachment; filename*=UTF-8''file%20name.jpg", header.value);
-
- // Lone `\n` line endings are accepted too.
- try testing.expectEqual(10, try parseHttpHeader("Host: abc\nHost: def\n", &header));
- try testing.expectString("Host", header.key);
- try testing.expectString("abc", header.value);
-
- // Leading whitespaces of the value are skipped, trailing ones are kept.
- _ = try parseHttpHeader("Key: value \r\n", &header);
- try testing.expectString("value ", header.value);
-
- // 0 length values are fine.
- _ = try parseHttpHeader("Key:\r\n", &header);
- try testing.expectString("Key", header.key);
- try testing.expectString("", header.value);
-}
-
-test "simd: parse HTTP header incomplete" {
- var header: HttpHeader = undefined;
-
- // Buffer may end anywhere before the line ending is complete.
- const bytes = "Content-Type: text/plain\r\n";
- for (0..bytes.len) |len| {
- try testing.expectError(error.Incomplete, parseHttpHeader(bytes[0..len], &header));
- }
- // The lone `\n` variant completes a byte earlier.
- const lf_bytes = "Content-Type: text/plain\n";
- try testing.expectEqual(lf_bytes.len, try parseHttpHeader(lf_bytes, &header));
-}
-
-test "simd: parse HTTP header invalid" {
- var header: HttpHeader = undefined;
-
- // 0 length keys.
- try testing.expectError(error.Invalid, parseHttpHeader(": value\r\n", &header));
- // Invalid characters in the key.
- try testing.expectError(error.Invalid, parseHttpHeader("Key\x01: value\r\n", &header));
- try testing.expectError(error.Invalid, parseHttpHeader("Key name: value\r\n", &header));
- // Invalid characters in the value.
- try testing.expectError(error.Invalid, parseHttpHeader("Key: val\x01ue\r\n", &header));
- try testing.expectError(error.Invalid, parseHttpHeader("Key: val\x7fue\r\n", &header));
- // `\r` must be followed by `\n`.
- try testing.expectError(error.Invalid, parseHttpHeader("Key: value\rX\n", &header));
-}
-
-test "simd: Content-Type iterator" {
- // No parameters; essence is trimmed of surrounding whitespace.
- var it = ContentTypeIterator.init(" text/html ");
- try testing.expectString("text/html", it.essence);
- try testing.expectEqual(null, it.next());
-
- // A trailing `;` doesn't produce a parameter.
- it = ContentTypeIterator.init("text/html;");
- try testing.expectString("text/html", it.essence);
- try testing.expectEqual(null, it.next());
-
- // Plain and quoted values; whitespace around keys and values is trimmed.
- it = ContentTypeIterator.init("multipart/form-data; boundary=abc;charset = \"utf-8\" ");
- try testing.expectString("multipart/form-data", it.essence);
- var p = it.next().?;
- try testing.expectString("boundary", p.key);
- try testing.expectString("abc", p.value);
- p = it.next().?;
- try testing.expectString("charset", p.key);
- try testing.expectString("utf-8", p.value);
- try testing.expectEqual(null, it.next());
-
- // Parameters without `=` or without a key are skipped.
- it = ContentTypeIterator.init("text/html; foo; =bar; a=1");
- p = it.next().?;
- try testing.expectString("a", p.key);
- try testing.expectString("1", p.value);
- try testing.expectEqual(null, it.next());
-
- // Values may be empty, unquoted or quoted.
- it = ContentTypeIterator.init("text/html; a=; b=\"\"");
- p = it.next().?;
- try testing.expectString("a", p.key);
- try testing.expectString("", p.value);
- p = it.next().?;
- try testing.expectString("b", p.key);
- try testing.expectString("", p.value);
- try testing.expectEqual(null, it.next());
-}
-
-test "simd: Content-Type find boundary" {
- // The most common shape: single parameter, end of header.
- var it = ContentTypeIterator.init("multipart/form-data; boundary=abc");
- try testing.expectString("abc", it.findBoundary());
- // The iterator is consumed.
- try testing.expectEqual(null, it.next());
-
- // Parameter names are case-insensitive; other parameters are skipped
- // (consumed), the ones after the boundary are still iterable.
- it = ContentTypeIterator.init("multipart/form-data; charset=utf-8; BOUNDARY=\"ab c\"; foo=bar");
- try testing.expectString("ab c", it.findBoundary());
- const p = it.next().?;
- try testing.expectString("foo", p.key);
-
- // The first boundary wins.
- it = ContentTypeIterator.init("multipart/form-data; boundary=first; boundary=second");
- try testing.expectString("first", it.findBoundary());
-
- // Missing boundary is an empty string, which callers treat as invalid.
- it = ContentTypeIterator.init("multipart/form-data");
- try testing.expectString("", it.findBoundary());
- it = ContentTypeIterator.init("multipart/form-data; charset=utf-8");
- try testing.expectString("", it.findBoundary());
- it = ContentTypeIterator.init("multipart/form-data; boundary=");
- try testing.expectString("", it.findBoundary());
- // No false match on names that merely contain "boundary".
- it = ContentTypeIterator.init("multipart/form-data; notboundary=evil; xboundaryx=evil");
- try testing.expectString("", it.findBoundary());
-}
-
-test "simd: match functions against scalar reference" {
- // Exhaustive: place every possible byte at every position of an otherwise
- // valid buffer, across lengths covering the vector, SWAR and scalar paths.
- var buf: [48]u8 = undefined;
- for (1..buf.len + 1) |len| {
- for (0..len) |pos| {
- for (0..256) |c| {
- @memset(buf[0..len], 'a');
- buf[pos] = @intCast(c);
-
- var key_expected: usize = 0;
- while (key_expected < len and header_key_map[buf[key_expected]] != 0) key_expected += 1;
- var value_expected: usize = 0;
- while (value_expected < len and header_value_map[buf[value_expected]] != 0) value_expected += 1;
-
- try testing.expectEqual(key_expected, matchHeaderKey(buf[0..len]));
- try testing.expectEqual(value_expected, matchHeaderValue(buf[0..len]));
- }
- }
- }
-
- // Randomized: fully random buffers to exercise multiple invalid bytes per
- // chunk at once.
- var prng = std.Random.DefaultPrng.init(0x5eed);
- const random = prng.random();
- for (0..20_000) |_| {
- const len = random.intRangeAtMost(usize, 0, buf.len);
- random.bytes(buf[0..len]);
-
- var key_expected: usize = 0;
- while (key_expected < len and header_key_map[buf[key_expected]] != 0) key_expected += 1;
- var value_expected: usize = 0;
- while (value_expected < len and header_value_map[buf[value_expected]] != 0) value_expected += 1;
-
- try testing.expectEqual(key_expected, matchHeaderKey(buf[0..len]));
- try testing.expectEqual(value_expected, matchHeaderValue(buf[0..len]));
- }
-}