GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Handle quoted and case-insensitive charset parameters (#59) · lostisland/faraday-net_http@e85ab61

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib/faraday/adapter
    • net_http.rb
  • spec/faraday/adapter
    • net_http_spec.rb
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/faraday/adapter/net_http.rb‎

Copy file name to clipboardExpand all lines: lib/faraday/adapter/net_http.rb
+4-2Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,10 @@ def ssl_verify_mode(ssl)
212212

213213
def encoded_body(http_response)
214214
body = http_response.body || +''
215-
/\bcharset=([^;]+)/.match(http_response['Content-Type']) do |match|
216-
content_charset = ::Encoding.find(match[1].strip)
215+
/(?:\A|;)\s*charset=([^;]+)/i.match(http_response['Content-Type']) do |match|
216+
charset = match[1].strip
217+
charset = charset[1...-1] if charset.start_with?('"') && charset.end_with?('"')
218+
content_charset = ::Encoding.find(charset)
217219
body = body.dup.force_encoding(content_charset)
218220
rescue ArgumentError
219221
nil
Collapse file

‎spec/faraday/adapter/net_http_spec.rb‎

Copy file name to clipboardExpand all lines: spec/faraday/adapter/net_http_spec.rb
+36Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,42 @@
146146

147147
it { expect(response.body.encoding).to eq(::Encoding::UTF_8) }
148148
end
149+
150+
context 'when Content-Type charset is quoted' do
151+
let(:headers) { { 'Content-Type' => 'text/xml; charset="ISO-8859-1"' } }
152+
153+
it { expect(response.body.encoding).to eq(::Encoding::ISO_8859_1) }
154+
end
155+
156+
context 'when Content-Type charset is quoted and followed by another parameter' do
157+
let(:headers) { { 'Content-Type' => 'text/xml; charset="UTF-8"; boundary=x' } }
158+
159+
it { expect(response.body.encoding).to eq(::Encoding::UTF_8) }
160+
end
161+
162+
context 'when Content-Type charset parameter name is uppercase' do
163+
let(:headers) { { 'Content-Type' => 'text/xml; CHARSET=UTF-8' } }
164+
165+
it { expect(response.body.encoding).to eq(::Encoding::UTF_8) }
166+
end
167+
168+
context 'when Content-Type charset has no space after the semicolon' do
169+
let(:headers) { { 'Content-Type' => 'text/xml;charset=UTF-8' } }
170+
171+
it { expect(response.body.encoding).to eq(::Encoding::UTF_8) }
172+
end
173+
174+
context 'when Content-Type has a parameter merely ending in charset' do
175+
let(:headers) { { 'Content-Type' => 'text/xml; x-charset=UTF-8' } }
176+
177+
it { expect(response.body.encoding).to eq(::Encoding::ASCII_8BIT) }
178+
end
179+
180+
context 'when Content-Type charset is an unterminated quoted value' do
181+
let(:headers) { { 'Content-Type' => 'text/xml; charset="UTF-8' } }
182+
183+
it { expect(response.body.encoding).to eq(::Encoding::ASCII_8BIT) }
184+
end
149185
end
150186

151187
context 'when the response has no body and no Content-Type' do

0 commit comments

Comments
0 (0)
You are visitor 01402605 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.