browser: parse http content-type

This commit is contained in:
Pierre Tachoire
2023-12-22 16:21:39 +01:00
parent c94528dbd9
commit 20b2bfa00e
2 changed files with 24 additions and 9 deletions

View File

@@ -2,6 +2,7 @@ const std = @import("std");
const parser = @import("../netsurf.zig");
const Loader = @import("loader.zig").Loader;
const Mime = @import("mime.zig");
const jsruntime = @import("jsruntime");
const Loop = jsruntime.Loop;
@@ -166,9 +167,19 @@ pub const Page = struct {
// TODO handle charset
// https://html.spec.whatwg.org/#content-type
// TODO check content-type
try self.loadHTMLDoc(&result);
const ct = result.headers.getFirstValue("Content-Type") orelse {
// no content type in HTTP headers.
// TODO try to sniff mime type from the body.
log.info("no content-type HTTP header", .{});
return;
};
const mime = try Mime.parse(ct);
if (mime.eql(Mime.HTML)) {
// TODO check content-type
try self.loadHTMLDoc(&result);
} else {
log.info("none HTML document: {s}", .{ct});
}
}
// https://html.spec.whatwg.org/#read-html

View File

@@ -14,6 +14,9 @@ mtype: []const u8,
msubtype: []const u8,
params: []const u8,
pub const HTML = Self{ .mtype = "text", .msubtype = "html", .params = "" };
pub const Javascript = Self{ .mtype = "application", .msubtype = "javascript", .params = "" };
const reader = struct {
s: []const u8,
i: usize = 0,
@@ -125,12 +128,7 @@ pub fn parse(s: []const u8) Self.MimeError!Self {
// limit input size
if (ln > 255) return MimeError.TooBig;
var res = Self{
.mtype = "",
.msubtype = "",
.params = "",
};
var res = Self{ .mtype = "", .msubtype = "", .params = "" };
var r = reader{ .s = s };
res.mtype = trim(r.until('/'));
@@ -185,3 +183,9 @@ test "parse invalid" {
try testing.expect(false);
}
}
// Compare type and subtype.
pub fn eql(self: Self, b: Self) bool {
if (!std.mem.eql(u8, self.mtype, b.mtype)) return false;
return std.mem.eql(u8, self.msubtype, b.msubtype);
}