Don't assume character encoding when loading in fallback mode

This commit is contained in:
Shadowfacts 2021-06-17 22:20:02 -04:00
parent 5d2fb53510
commit de68ecbe4b
2 changed files with 10 additions and 4 deletions

View File

@ -258,8 +258,7 @@ class BrowserWebViewController: UIViewController {
self.loadedFallback = true
// todo: probably shouldn't assume this is UTF-8
self.webView.load(body, mimeType: mimeType, characterEncodingName: "utf-8", baseURL: self.url)
self.webView.load(body, mimeType: mimeType, characterEncodingName: response.encodingName ?? "utf-8", baseURL: self.url)
// When showing an image, the safe area insets seem to be ignored. This isn't perfect
// (there's a little extra space between the bottom of the nav bar and the top of the image),
// but it's better than the image being obscured.

View File

@ -38,10 +38,17 @@ public struct GeminiResponse {
return UTType.types(tag: mimeType, tagClass: .mimeType, conformingTo: nil).first
}
public var encodingName: String? {
guard let parameters = mimeTypeParameters else {
return nil
}
return parameters["charset"]
}
public var bodyText: String? {
guard let body = body, let parameters = mimeTypeParameters else { return nil }
guard let body = body else { return nil }
let encoding: String.Encoding
switch parameters["charset"]?.lowercased() {
switch encodingName?.lowercased() {
case nil, "utf-8":
// The Gemini spec defines UTF-8 to be the default charset.
encoding = .utf8