faraday_json 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +38 -0
- data/.travis.yml +25 -0
- data/Gemfile +18 -0
- data/Gemfile-0.7.rb +3 -0
- data/Gemfile-0.7.rb.lock +57 -0
- data/Gemfile-0.8.rb +3 -0
- data/Gemfile-0.8.rb.lock +53 -0
- data/Gemfile.lock +52 -0
- data/LICENSE +30 -0
- data/README.md +30 -0
- data/Rakefile +28 -0
- data/faraday_json.gemspec +25 -0
- data/lib/faraday_json.rb +22 -0
- data/lib/faraday_json/encode_json.rb +94 -0
- data/lib/faraday_json/encoding.rb +219 -0
- data/lib/faraday_json/parse_json.rb +165 -0
- data/lib/faraday_json/version.rb +10 -0
- data/spec/data/iso8859-15_file.json +1 -0
- data/spec/data/utf16be_file.json +0 -0
- data/spec/data/utf16le_file.json +0 -0
- data/spec/data/utf8_file.json +1 -0
- data/spec/encode_json_spec.rb +376 -0
- data/spec/helper.rb +74 -0
- data/spec/parse_json_spec.rb +217 -0
- metadata +123 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 797f339a60927cd80ecb7e6b6a61d992402e21cc
|
4
|
+
data.tar.gz: 181e9fbcff6c9a45754dd287bb54d71d09b695fb
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: dd2b02b134d0cc13fcca62b60669c62a5d9e5596337b1c06f0dc31d2a73ef7e9603e3a886467aa4995bfa336ff20b0f150b5bfe9c61fe28c7e56a17da6bd2118
|
7
|
+
data.tar.gz: 1d42a2368bc4d7525a000e32ffffda78a88332aadc771d1406d8cd4c61fc68a1ac0964a1e1c8782c119ff44e22215fdb11d569c7b59d54a9f8371ea4033316f5
|
data/.gitignore
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
/.config
|
4
|
+
/coverage/
|
5
|
+
/InstalledFiles
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/test/tmp/
|
9
|
+
/test/version_tmp/
|
10
|
+
/tmp/
|
11
|
+
|
12
|
+
## Specific to RubyMotion:
|
13
|
+
.dat*
|
14
|
+
.repl_history
|
15
|
+
build/
|
16
|
+
|
17
|
+
## Documentation cache and generated files:
|
18
|
+
/.yardoc/
|
19
|
+
/_yardoc/
|
20
|
+
/doc/
|
21
|
+
/rdoc/
|
22
|
+
|
23
|
+
## Environment normalisation:
|
24
|
+
/.bundle/
|
25
|
+
/vendor/bundle
|
26
|
+
/lib/bundler/man/
|
27
|
+
|
28
|
+
# for a library or gem, you might want to ignore these files since the code is
|
29
|
+
# intended to run in multiple environments; otherwise, check them in:
|
30
|
+
# Gemfile.lock
|
31
|
+
.ruby-version
|
32
|
+
.ruby-gemset
|
33
|
+
|
34
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
35
|
+
.rvmrc
|
36
|
+
|
37
|
+
# Editor files
|
38
|
+
.*.sw*
|
data/.travis.yml
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
bundler_args: --without development
|
2
|
+
language: ruby
|
3
|
+
rvm:
|
4
|
+
- 1.8.7
|
5
|
+
- 1.9.2
|
6
|
+
- 1.9.3
|
7
|
+
- 2.0.0
|
8
|
+
- 2.1.6
|
9
|
+
- jruby-head
|
10
|
+
- rbx-2
|
11
|
+
- ruby-head
|
12
|
+
gemfile:
|
13
|
+
- Gemfile
|
14
|
+
- Gemfile-0.7.rb
|
15
|
+
- Gemfile-0.8.rb
|
16
|
+
env:
|
17
|
+
- LC_ALL=C LANG=C
|
18
|
+
- LC_ALL=en_US.UTF-8 LANG=C
|
19
|
+
matrix:
|
20
|
+
allow_failures:
|
21
|
+
- rvm: jruby-head
|
22
|
+
- rvm: rbx-2
|
23
|
+
- rvm: ruby-head
|
24
|
+
- rvm: 2.2.2
|
25
|
+
fast_finish: true
|
data/Gemfile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
gem 'json', :platforms => [:jruby, :rbx, :ruby_18]
|
4
|
+
gem 'rake', '>= 0.9'
|
5
|
+
|
6
|
+
group :test do
|
7
|
+
gem 'parallel', '= 1.3.3', :platforms => [:mri_19]
|
8
|
+
gem 'cane', '>= 2.2.2', :platforms => [:mri_19, :mri_20, :mri_21, :mri_22]
|
9
|
+
gem 'rspec', '>= 3'
|
10
|
+
gem 'simplecov'
|
11
|
+
# gem 'webmock'
|
12
|
+
end
|
13
|
+
|
14
|
+
platforms :ruby_18 do
|
15
|
+
gem 'iconv'
|
16
|
+
end
|
17
|
+
|
18
|
+
gemspec
|
data/Gemfile-0.7.rb
ADDED
data/Gemfile-0.7.rb.lock
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
faraday_json (0.1.0)
|
5
|
+
faraday (>= 0.7.4, < 0.10)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
addressable (2.3.8)
|
11
|
+
cane (2.6.2)
|
12
|
+
parallel
|
13
|
+
diff-lcs (1.2.5)
|
14
|
+
docile (1.1.5)
|
15
|
+
faraday (0.7.6)
|
16
|
+
addressable (~> 2.2)
|
17
|
+
multipart-post (~> 1.1)
|
18
|
+
rack (~> 1.1)
|
19
|
+
iconv (1.0.4)
|
20
|
+
json (1.8.2)
|
21
|
+
multipart-post (1.2.0)
|
22
|
+
parallel (1.3.3)
|
23
|
+
rack (1.6.1)
|
24
|
+
rake (10.4.2)
|
25
|
+
rspec (3.2.0)
|
26
|
+
rspec-core (~> 3.2.0)
|
27
|
+
rspec-expectations (~> 3.2.0)
|
28
|
+
rspec-mocks (~> 3.2.0)
|
29
|
+
rspec-core (3.2.3)
|
30
|
+
rspec-support (~> 3.2.0)
|
31
|
+
rspec-expectations (3.2.1)
|
32
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
33
|
+
rspec-support (~> 3.2.0)
|
34
|
+
rspec-mocks (3.2.1)
|
35
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
36
|
+
rspec-support (~> 3.2.0)
|
37
|
+
rspec-support (3.2.2)
|
38
|
+
simplecov (0.10.0)
|
39
|
+
docile (~> 1.1.0)
|
40
|
+
json (~> 1.8)
|
41
|
+
simplecov-html (~> 0.10.0)
|
42
|
+
simplecov-html (0.10.0)
|
43
|
+
|
44
|
+
PLATFORMS
|
45
|
+
ruby
|
46
|
+
|
47
|
+
DEPENDENCIES
|
48
|
+
bundler (~> 1.6)
|
49
|
+
cane (>= 2.2.2)
|
50
|
+
faraday (~> 0.7.4)
|
51
|
+
faraday_json!
|
52
|
+
iconv
|
53
|
+
json
|
54
|
+
parallel (= 1.3.3)
|
55
|
+
rake (>= 0.9)
|
56
|
+
rspec (>= 3)
|
57
|
+
simplecov
|
data/Gemfile-0.8.rb
ADDED
data/Gemfile-0.8.rb.lock
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
faraday_json (0.1.0)
|
5
|
+
faraday (>= 0.7.4, < 0.10)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
cane (2.6.2)
|
11
|
+
parallel
|
12
|
+
diff-lcs (1.2.5)
|
13
|
+
docile (1.1.5)
|
14
|
+
faraday (0.8.9)
|
15
|
+
multipart-post (~> 1.2.0)
|
16
|
+
iconv (1.0.4)
|
17
|
+
json (1.8.2)
|
18
|
+
multipart-post (1.2.0)
|
19
|
+
parallel (1.3.3)
|
20
|
+
rake (10.4.2)
|
21
|
+
rspec (3.2.0)
|
22
|
+
rspec-core (~> 3.2.0)
|
23
|
+
rspec-expectations (~> 3.2.0)
|
24
|
+
rspec-mocks (~> 3.2.0)
|
25
|
+
rspec-core (3.2.3)
|
26
|
+
rspec-support (~> 3.2.0)
|
27
|
+
rspec-expectations (3.2.1)
|
28
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
29
|
+
rspec-support (~> 3.2.0)
|
30
|
+
rspec-mocks (3.2.1)
|
31
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
32
|
+
rspec-support (~> 3.2.0)
|
33
|
+
rspec-support (3.2.2)
|
34
|
+
simplecov (0.10.0)
|
35
|
+
docile (~> 1.1.0)
|
36
|
+
json (~> 1.8)
|
37
|
+
simplecov-html (~> 0.10.0)
|
38
|
+
simplecov-html (0.10.0)
|
39
|
+
|
40
|
+
PLATFORMS
|
41
|
+
ruby
|
42
|
+
|
43
|
+
DEPENDENCIES
|
44
|
+
bundler (~> 1.6)
|
45
|
+
cane (>= 2.2.2)
|
46
|
+
faraday (~> 0.8.8)
|
47
|
+
faraday_json!
|
48
|
+
iconv
|
49
|
+
json
|
50
|
+
parallel (= 1.3.3)
|
51
|
+
rake (>= 0.9)
|
52
|
+
rspec (>= 3)
|
53
|
+
simplecov
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
faraday_json (0.1.0)
|
5
|
+
faraday (>= 0.7.4, < 0.10)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
cane (2.6.2)
|
11
|
+
parallel
|
12
|
+
diff-lcs (1.2.5)
|
13
|
+
docile (1.1.5)
|
14
|
+
faraday (0.9.1)
|
15
|
+
multipart-post (>= 1.2, < 3)
|
16
|
+
iconv (1.0.4)
|
17
|
+
json (1.8.2)
|
18
|
+
multipart-post (2.0.0)
|
19
|
+
parallel (1.3.3)
|
20
|
+
rake (10.4.2)
|
21
|
+
rspec (3.2.0)
|
22
|
+
rspec-core (~> 3.2.0)
|
23
|
+
rspec-expectations (~> 3.2.0)
|
24
|
+
rspec-mocks (~> 3.2.0)
|
25
|
+
rspec-core (3.2.3)
|
26
|
+
rspec-support (~> 3.2.0)
|
27
|
+
rspec-expectations (3.2.1)
|
28
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
29
|
+
rspec-support (~> 3.2.0)
|
30
|
+
rspec-mocks (3.2.1)
|
31
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
32
|
+
rspec-support (~> 3.2.0)
|
33
|
+
rspec-support (3.2.2)
|
34
|
+
simplecov (0.10.0)
|
35
|
+
docile (~> 1.1.0)
|
36
|
+
json (~> 1.8)
|
37
|
+
simplecov-html (~> 0.10.0)
|
38
|
+
simplecov-html (0.10.0)
|
39
|
+
|
40
|
+
PLATFORMS
|
41
|
+
ruby
|
42
|
+
|
43
|
+
DEPENDENCIES
|
44
|
+
bundler (~> 1.6)
|
45
|
+
cane (>= 2.2.2)
|
46
|
+
faraday_json!
|
47
|
+
iconv
|
48
|
+
json
|
49
|
+
parallel (= 1.3.3)
|
50
|
+
rake (>= 0.9)
|
51
|
+
rspec (>= 3)
|
52
|
+
simplecov
|
data/LICENSE
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
Copyright spriteCloud B.V. (http://www.spritecloud.com/) and other faraday_json
|
2
|
+
contributors. All rights not covered below are reserved.
|
3
|
+
|
4
|
+
MIT +no-false-attribs License
|
5
|
+
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
7
|
+
of this software and associated documentation files (the "Software"), to
|
8
|
+
deal in the Software without restriction, including without limitation the
|
9
|
+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
10
|
+
sell copies of the Software, and to permit persons to whom the Software is
|
11
|
+
furnished to do so, subject to the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be included in
|
14
|
+
all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
Distributions of all or part of the Software intended to be used by the
|
17
|
+
recipients as they would use the unmodified Software, containing modifications
|
18
|
+
that substantially alter, remove, or disable functionality of the Software,
|
19
|
+
outside of the documented configuration mechanisms provided by the Software,
|
20
|
+
shall be modified such that the Original Author's bug reporting email addresses
|
21
|
+
and urls are either replaced with the contact information of the parties
|
22
|
+
responsible for the changes, or removed entirely.
|
23
|
+
|
24
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
25
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
26
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
27
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
28
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
29
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
30
|
+
IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# Introduction
|
2
|
+
This is a character encoding-aware JSON middleware for Faraday.
|
3
|
+
|
4
|
+
[![Build Status](https://travis-ci.org/spriteCloud/faraday_json.svg)](https://travis-ci.org/spriteCloud/faraday_json)
|
5
|
+
|
6
|
+
The default JSON middleware from lostisland/faraday_middleware is not character
|
7
|
+
encoding aware. This is problematic because by default, Ruby performs I/O based
|
8
|
+
on locale settings. If your locale specifies one character encoding, but the
|
9
|
+
request or response body is encoded in another, you will end up with a Ruby
|
10
|
+
string that pretends to be one encoding, but cannot correctly be interpreted as
|
11
|
+
such.
|
12
|
+
|
13
|
+
Worse, Ruby 1.8 and later Ruby versions will not behave consistently, as 1.8
|
14
|
+
does not yet understand character encodings internally. There is a lengthy
|
15
|
+
discussion about this at https://bugs.ruby-lang.org/issues/2567
|
16
|
+
|
17
|
+
Still worse, JSON used to specify UTF-8 encoding as mandatory, but now loosened
|
18
|
+
this to any Unicode character set. The result is that some JSON libraries still
|
19
|
+
only understand UTF-8.
|
20
|
+
|
21
|
+
This middleware fixes these issues rather brute-force by:
|
22
|
+
|
23
|
+
- Accepting the Content-Type charset value as the correct encoding, and
|
24
|
+
converting the body from Faraday adapters to this encoding.
|
25
|
+
- Only sending UTF-8 encoded JSON, converting raw data if necessary.
|
26
|
+
- Always setting the UTF-8 charset when sending this UTF-8 encoded JSON.
|
27
|
+
|
28
|
+
When no charset is provided, all bets are off. The correct default would be to
|
29
|
+
assume US-ASCII, but that may break some code. This middleware lets you override
|
30
|
+
this default.
|
data/Rakefile
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
ruby_19 = RUBY_VERSION > '1.9'
|
2
|
+
ruby_mri = !defined?(RUBY_ENGINE) || 'ruby' == RUBY_ENGINE
|
3
|
+
default_gemfile = ENV['BUNDLE_GEMFILE'] =~ /Gemfile$/
|
4
|
+
|
5
|
+
if ruby_19 && ruby_mri && default_gemfile
|
6
|
+
task :default => [:enable_coverage, :spec, :quality]
|
7
|
+
else
|
8
|
+
task :default => [:spec]
|
9
|
+
end
|
10
|
+
|
11
|
+
require 'bundler'
|
12
|
+
Bundler::GemHelper.install_tasks
|
13
|
+
|
14
|
+
require 'rspec/core/rake_task'
|
15
|
+
RSpec::Core::RakeTask.new(:spec)
|
16
|
+
|
17
|
+
task :enable_coverage do
|
18
|
+
ENV['COVERAGE'] = 'yes'
|
19
|
+
end
|
20
|
+
|
21
|
+
desc %(Check code quality metrics with Cane)
|
22
|
+
task :quality do
|
23
|
+
sh 'cane',
|
24
|
+
'--abc-max=15',
|
25
|
+
'--style-measure=110',
|
26
|
+
'--gte=coverage/covered_percent,88.89',
|
27
|
+
'--max-violations=0'
|
28
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'faraday_json/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "faraday_json"
|
8
|
+
spec.version = FaradayJSON::VERSION
|
9
|
+
spec.authors = ["Jens Finkhaeuser"]
|
10
|
+
spec.email = ["foss@spritecloud.com"]
|
11
|
+
spec.description = %q{This is a character encoding-aware JSON middleware for Faraday.}
|
12
|
+
spec.summary = %q{This is a character encoding-aware JSON middleware for Faraday.}
|
13
|
+
spec.homepage = "https://github.com/spriteCloud/faraday_json"
|
14
|
+
spec.license = "MITNFA"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
|
24
|
+
spec.add_dependency 'faraday', ['>= 0.7.4', '< 0.10']
|
25
|
+
end
|
data/lib/faraday_json.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
#
|
2
|
+
# FaradayJSON
|
3
|
+
# https://github.com/spriteCloud/faraday_json
|
4
|
+
#
|
5
|
+
# Copyright (c) 2015 spriteCloud B.V. and other FaradayJSON contributors.
|
6
|
+
# All rights reserved.
|
7
|
+
#
|
8
|
+
|
9
|
+
require 'faraday'
|
10
|
+
|
11
|
+
module FaradayJSON
|
12
|
+
autoload :ParseJson, 'faraday_json/parse_json'
|
13
|
+
|
14
|
+
if Faraday::Middleware.respond_to? :register_middleware
|
15
|
+
Faraday::Request.register_middleware \
|
16
|
+
:json => lambda { EncodeJson }
|
17
|
+
|
18
|
+
Faraday::Response.register_middleware \
|
19
|
+
:json => lambda { ParseJson },
|
20
|
+
:json_fix => lambda { ParseJsonMimeTypeFix }
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,94 @@
|
|
1
|
+
#
|
2
|
+
# FaradayJSON
|
3
|
+
# https://github.com/spriteCloud/faraday_json
|
4
|
+
#
|
5
|
+
# Copyright (c) 2015 spriteCloud B.V. and other FaradayJSON contributors.
|
6
|
+
# All rights reserved.
|
7
|
+
#
|
8
|
+
|
9
|
+
require 'faraday'
|
10
|
+
require 'faraday_json/encoding'
|
11
|
+
|
12
|
+
module FaradayJSON
|
13
|
+
# Request middleware that encodes the body as JSON.
|
14
|
+
#
|
15
|
+
# Processes only requests with matching Content-type or those without a type.
|
16
|
+
# If a request doesn't have a type but has a body, it sets the Content-type
|
17
|
+
# to JSON MIME-type.
|
18
|
+
#
|
19
|
+
# Doesn't try to encode bodies that already are in string form.
|
20
|
+
class EncodeJson < Faraday::Middleware
|
21
|
+
CONTENT_TYPE = 'Content-Type'.freeze
|
22
|
+
CONTENT_LENGTH = 'Content-Length'.freeze
|
23
|
+
MIME_TYPE = 'application/json'.freeze
|
24
|
+
MIME_TYPE_UTF8 = 'application/json; charset=utf-8'.freeze
|
25
|
+
|
26
|
+
include ::FaradayJSON::Encoding
|
27
|
+
|
28
|
+
dependency do
|
29
|
+
require 'json' unless defined?(::JSON)
|
30
|
+
end
|
31
|
+
|
32
|
+
def call(env)
|
33
|
+
if process_request?(env)
|
34
|
+
body = env[:body]
|
35
|
+
|
36
|
+
# Detect and honour input charset. Basically, all requests without a
|
37
|
+
# charset should be considered malformed, but we can make a best guess.
|
38
|
+
# Whether the body is a string or another data structure does not
|
39
|
+
# matter: all strings *contained* within it must be encoded properly.
|
40
|
+
charset = request_charset(env)
|
41
|
+
|
42
|
+
# Strip BOM, if any
|
43
|
+
body = strip_bom(body, charset, { 'default_encoding' => 'us-ascii' })
|
44
|
+
|
45
|
+
# Transcode to UTF-8
|
46
|
+
body = to_utf8(body, charset, { 'force_input_charset' => true })
|
47
|
+
|
48
|
+
# If the body is a stirng, we assume it's already JSON. No further
|
49
|
+
# processing is necessary.
|
50
|
+
# XXX Is :to_str really a good indicator for Strings? Taken from old
|
51
|
+
# code.
|
52
|
+
if not body.respond_to?(:to_str)
|
53
|
+
# If body isn't a string yet, we need to encode it. We also know it's
|
54
|
+
# then going to be UTF-8, because JSON defaults to that.
|
55
|
+
# Thanks to to_utf8 above, JSON.dump should not have any issues here.
|
56
|
+
body = ::JSON.dump(body)
|
57
|
+
end
|
58
|
+
|
59
|
+
env[:body] = body
|
60
|
+
|
61
|
+
# We'll add a content length, because otherwise we're relying on every
|
62
|
+
# component down the line properly interpreting UTF-8 - that can fail.
|
63
|
+
env[:request_headers][CONTENT_LENGTH] ||= env[:body].bytesize
|
64
|
+
|
65
|
+
# Always base the encoding we're sending in the content type header on
|
66
|
+
# the string encoding.
|
67
|
+
env[:request_headers][CONTENT_TYPE] = MIME_TYPE_UTF8
|
68
|
+
end
|
69
|
+
@app.call env
|
70
|
+
end
|
71
|
+
|
72
|
+
def process_request?(env)
|
73
|
+
type = request_type(env)
|
74
|
+
has_body?(env) and (type.empty? or type == MIME_TYPE)
|
75
|
+
end
|
76
|
+
|
77
|
+
def request_charset(env)
|
78
|
+
enc = env[:request_headers][CONTENT_TYPE].to_s
|
79
|
+
enc = enc.split(';', 2).last if enc.index(';')
|
80
|
+
enc = enc.split('=', 2).last if enc.index('=')
|
81
|
+
return enc
|
82
|
+
end
|
83
|
+
|
84
|
+
def has_body?(env)
|
85
|
+
body = env[:body] and !(body.respond_to?(:to_str) and body.empty?)
|
86
|
+
end
|
87
|
+
|
88
|
+
def request_type(env)
|
89
|
+
type = env[:request_headers][CONTENT_TYPE].to_s
|
90
|
+
type = type.split(';', 2).first if type.index(';')
|
91
|
+
type
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|