レスポンスヘッダのContent-typeがtext/html; charset=ISO-8859-1でなにをどうがんばっても日本語を提供できない回
Conversation
Notices
-
Embed this notice
zunda (zundan@mastodon.zunda.ninja)'s status on Sunday, 09-Feb-2025 10:36:49 JST zunda
-
Embed this notice
tateisu :force::r_9a: (tateisu@mastodon.juggler.jp)'s status on Sunday, 09-Feb-2025 14:07:41 JST tateisu :force::r_9a:
@zundan HTMLなら https://e-words.jp/w/%E6%96%87%E5%AD%97%E5%8F%82%E7%85%A7.html とかで書けるはず
-
Embed this notice
zunda (zundan@mastodon.zunda.ninja)'s status on Sunday, 09-Feb-2025 14:07:41 JST zunda
@tateisu なるほど〜。下記のようなサーバで試してみたら、少なくともSafariとChromeではユニコードからの数値参照の場合にレスポンスヘッダのcharsetを無視する感じになりそうですね。
require "sinatra"
def entity_reference(str, encoding)
str.encode(encoding).codepoints.map{|c| "&##{c};"}.join
endget "/euc-jp" do
headers "Content-Type" => "text/html; charset=ISO-8859-1"
"<html><body><p>#{entity_reference("こんにちは", "EUC-JP")}</p></body></html>\n"
endget "/utf-8" do
headers "Content-Type" => "text/html; charset=ISO-8859-1"
"<html><body><p>#{entity_reference("こんにちは", "UTF-8")}</p></body></html>\n"
endIn conversation permalink -
Embed this notice
tateisu :force::r_9a: (tateisu@mastodon.juggler.jp)'s status on Sunday, 09-Feb-2025 14:35:21 JST tateisu :force::r_9a:
@zundan その出力に含まれるのは7bit ASCIIのテキストなのでiso-8859-1でバイト列から文字列に変換できて、次のHTMLパースの段階で文字参照はUnicode文字になります。レスポンスヘッダの文字コード指定は前者の方でちゃんと適用されています。
In conversation permalink -
Embed this notice
zunda (zundan@mastodon.zunda.ninja)'s status on Sunday, 09-Feb-2025 14:35:21 JST zunda
@tateisu なるほどそれじゃあmataタグでもう一度文字コードを…と思ったのですがユニコード以外は今は規格外なのかもですね
require "sinatra"
def entity_reference(str, encoding)
str.encode(encoding).codepoints.map{|c| "&##{c};"}.join
endget "/euc-jp-equiv" do
headers "Content-Type" => "text/html; charset=ISO-8859-1"
<<"_HTML"
<html>
<meta http-equiv="content-type" content="text/html; charset=EUC-JP">
<body><p>#{entity_reference("こんにちは", "EUC-JP")}</p></body>
</html>
_HTML
endIn conversation permalink Attachments
-
Embed this notice