mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-12-16 16:28:58 +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 parser = @import("../netsurf.zig");
|
||||||
const Loader = @import("loader.zig").Loader;
|
const Loader = @import("loader.zig").Loader;
|
||||||
|
const Mime = @import("mime.zig");
|
||||||
|
|
||||||
const jsruntime = @import("jsruntime");
|
const jsruntime = @import("jsruntime");
|
||||||
const Loop = jsruntime.Loop;
|
const Loop = jsruntime.Loop;
|
||||||
@@ -166,9 +167,19 @@ pub const Page = struct {
|
|||||||
|
|
||||||
// TODO handle charset
|
// TODO handle charset
|
||||||
// https://html.spec.whatwg.org/#content-type
|
// https://html.spec.whatwg.org/#content-type
|
||||||
|
const ct = result.headers.getFirstValue("Content-Type") orelse {
|
||||||
// TODO check content-type
|
// no content type in HTTP headers.
|
||||||
try self.loadHTMLDoc(&result);
|
// 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
|
// https://html.spec.whatwg.org/#read-html
|
||||||
|
|||||||
@@ -14,6 +14,9 @@ mtype: []const u8,
|
|||||||
msubtype: []const u8,
|
msubtype: []const u8,
|
||||||
params: []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 {
|
const reader = struct {
|
||||||
s: []const u8,
|
s: []const u8,
|
||||||
i: usize = 0,
|
i: usize = 0,
|
||||||
@@ -125,12 +128,7 @@ pub fn parse(s: []const u8) Self.MimeError!Self {
|
|||||||
// limit input size
|
// limit input size
|
||||||
if (ln > 255) return MimeError.TooBig;
|
if (ln > 255) return MimeError.TooBig;
|
||||||
|
|
||||||
var res = Self{
|
var res = Self{ .mtype = "", .msubtype = "", .params = "" };
|
||||||
.mtype = "",
|
|
||||||
.msubtype = "",
|
|
||||||
.params = "",
|
|
||||||
};
|
|
||||||
|
|
||||||
var r = reader{ .s = s };
|
var r = reader{ .s = s };
|
||||||
|
|
||||||
res.mtype = trim(r.until('/'));
|
res.mtype = trim(r.until('/'));
|
||||||
@@ -185,3 +183,9 @@ test "parse invalid" {
|
|||||||
try testing.expect(false);
|
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