Skip to content

Retire superfluous quoting of Content-Type: charset [RFC7230] #189

Description

@forthrin
uri = URI('http://127.0.0.1:8080')
http = Net::HTTP.new(uri.hostname, uri.port)
http.response_body_encoding = true
http.send_request('GET', uri.request_uri)
/opt/homebrew/lib/ruby/gems/4.0.0/gems/net-http-0.9.1/lib/net/http/response.rb:381:in 'String#force_encoding': unknown encoding name - "UTF-8" (ArgumentError)
    @body.force_encoding(enc) if enc
	from /opt/homebrew/lib/ruby/gems/4.0.0/gems/net-http-0.9.1/lib/net/http/response.rb:381:in 'Net::HTTPResponse#read_body'
$ curl -I http://127.0.0.1:8080/
Content-Type: text/html; charset="UTF-8"
Server: WEBrick/1.9.2 (Ruby/4.0.1/2026-01-13)

$ open https://www.rfc-editor.org/rfc/rfc7230#section-3.2.6

[3.2.6](https://www.rfc-editor.org/rfc/rfc7230#section-3.2.6). Field Value Components
quoted-string = DQUOTE *( qdtext / quoted-pair ) DQUOTE
A sender SHOULD NOT generate a quoted-pair in a quoted-string except where necessary

$ open https://www.iana.org/assignments/character-sets/character-sets.xhtml

irb> Encoding.list.flat_map { it.name.delete('0-9A-Za-z').chars }.tally.sort_by(&:first).to_h
=> {"-" => 86, "_" => 1}
$ git diff
diff --git a/lib/webrick/httpservlet/filehandler.rb b/lib/webrick/httpservlet/filehandler.rb
index 4e759d8..2de7785 100644
--- a/lib/webrick/httpservlet/filehandler.rb
+++ b/lib/webrick/httpservlet/filehandler.rb
@@ -489,3 +489,3 @@ module WEBrick
         else
-          type << "; charset=\"#{enc.name}\""
+          type << "; charset=#{enc.name}"
         end
http.send_request('GET', uri.request_uri)
# #<Net::HTTPOK 200 OK readbody=true>

Activity

  1. jeremyevans commented on Jan 16, 2026

    @jeremyevans
    Collaborator

    Thank you for the report. I agree we should make this change.

    FWIW, considering RFC 7230 uses SHOULD NOT and not MUST NOT, net/http should probably handle a quoted charset even if cases where it isn't necessary.

  2. forthrin commented on Jan 16, 2026

    @forthrin
    Author

    Yes, thus simultaneously reported at: ruby/net-http#267

  3. forthrin commented on Sep 15, 2026

    @forthrin
    Author

    @jeremyevans: Any progress here? Seems like quite a simple fix for fundamental compliance.

  4. jeremyevans commented on Sep 17, 2026

    @jeremyevans
    Collaborator

    @forthrin Sorry for the delay. Your inline patch looks fine, so I submitted a pull request for it: #220

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions