mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-29 23:23:28 +00:00
browser: parse http content-type
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user