Skip to content

Commit 42440f1

Browse files
committed
fix mime.charsetString()
1 parent 26827ef commit 42440f1

File tree

4 files changed

+17
-9
lines changed

4 files changed

+17
-9
lines changed

src/browser/mime.zig

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub const Mime = struct {
2424
// IANA defines max. charset value length as 40.
2525
// We keep 41 for null-termination since HTML parser expects in this format.
2626
charset: [41]u8 = default_charset,
27+
charset_len: usize = 5,
2728

2829
/// String "UTF-8" continued by null characters.
2930
pub const default_charset = .{ 'U', 'T', 'F', '-', '8' } ++ .{0} ** 36;
@@ -53,7 +54,7 @@ pub const Mime = struct {
5354
other: struct { type: []const u8, sub_type: []const u8 },
5455
};
5556

56-
pub fn contentTypeString(mime: *const Mime) [:0]const u8 {
57+
pub fn contentTypeString(mime: *const Mime) []const u8 {
5758
return switch (mime.content_type) {
5859
.text_xml => "text/xml",
5960
.text_html => "text/html",
@@ -66,8 +67,12 @@ pub const Mime = struct {
6667
}
6768

6869
/// Returns the null-terminated charset value.
69-
pub fn charsetString(mime: *const Mime) [:0]const u8 {
70-
return @ptrCast(&mime.charset);
70+
pub fn charsetStringZ(mime: *const Mime) [:0]const u8 {
71+
return mime.charset[0..mime.charset_len :0];
72+
}
73+
74+
pub fn charsetString(mime: *const Mime) []const u8 {
75+
return mime.charset[0..mime.charset_len];
7176
}
7277

7378
/// Removes quotes of value if quotes are given.
@@ -111,6 +116,7 @@ pub const Mime = struct {
111116
const params = trimLeft(normalized[type_len..]);
112117

113118
var charset: [41]u8 = undefined;
119+
var charset_len: usize = undefined;
114120

115121
var it = std.mem.splitScalar(u8, params, ';');
116122
while (it.next()) |attr| {
@@ -136,13 +142,15 @@ pub const Mime = struct {
136142
@memcpy(charset[0..attribute_value.len], attribute_value);
137143
// Null-terminate right after attribute value.
138144
charset[attribute_value.len] = 0;
145+
charset_len = attribute_value.len;
139146
},
140147
}
141148
}
142149

143150
return .{
144151
.params = params,
145152
.charset = charset,
153+
.charset_len = charset_len,
146154
.content_type = content_type,
147155
};
148156
}
@@ -523,9 +531,9 @@ fn expect(expected: Expectation, input: []const u8) !void {
523531

524532
if (expected.charset) |ec| {
525533
// We remove the null characters for testing purposes here.
526-
try testing.expectEqual(ec, actual.charsetString()[0..ec.len]);
534+
try testing.expectEqual(ec, actual.charsetString());
527535
} else {
528536
const m: Mime = .unknown;
529-
try testing.expectEqual(m.charsetString(), actual.charsetString());
537+
try testing.expectEqual(m.charsetStringZ(), actual.charsetStringZ());
530538
}
531539
}

src/browser/page.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -740,14 +740,14 @@ pub const Page = struct {
740740
log.debug(.http, "navigate first chunk", .{ .content_type = mime.content_type, .len = data.len });
741741

742742
self.mode = switch (mime.content_type) {
743-
.text_html => .{ .html = try parser.Parser.init(mime.charsetString()) },
743+
.text_html => .{ .html = try parser.Parser.init(mime.charsetStringZ()) },
744744

745745
.application_json,
746746
.text_javascript,
747747
.text_css,
748748
.text_plain,
749749
=> blk: {
750-
var p = try parser.Parser.init(mime.charsetString());
750+
var p = try parser.Parser.init(mime.charsetStringZ());
751751
try p.process("<html><head><meta charset=\"utf-8\"></head><body><pre>");
752752
break :blk .{ .text = p };
753753
},

src/browser/xhr/xhr.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ pub const XMLHttpRequest = struct {
678678
}
679679

680680
var fbs = std.io.fixedBufferStream(self.response_bytes.items);
681-
const doc = parser.documentHTMLParse(fbs.reader(), mime.charsetString()) catch {
681+
const doc = parser.documentHTMLParse(fbs.reader(), mime.charsetStringZ()) catch {
682682
self.response_obj = .{ .Failure = {} };
683683
return;
684684
};

src/cdp/domains/network.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ const TransferAsResponseWriter = struct {
412412
try jws.objectField("mimeType");
413413
try jws.write(mime.contentTypeString());
414414
try jws.objectField("charset");
415-
try jws.write(mime.charsetString()[0..]);
415+
try jws.write(mime.charsetString());
416416
}
417417

418418
{

0 commit comments

Comments
 (0)