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
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: lib/faraday/adapter/net_http.rb +4-2Lines changed: 4 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -212,8 +212,10 @@ def ssl_verify_mode(ssl) |
212 | 212 |
|
213 | 213 | def encoded_body(http_response) |
214 | 214 | 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) |
217 | 219 | body = body.dup.force_encoding(content_charset) |
218 | 220 | rescue ArgumentError |
219 | 221 | nil |
|
Collapse file
Copy file name to clipboardExpand all lines: spec/faraday/adapter/net_http_spec.rb +36Lines changed: 36 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
146 | 146 |
|
147 | 147 | it { expect(response.body.encoding).to eq(::Encoding::UTF_8) } |
148 | 148 | 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 |
149 | 185 | end |
150 | 186 |
|
151 | 187 | context 'when the response has no body and no Content-Type' do |
|