├── lib ├── ignore_get_content_type.rb ├── ignore_get_content_type │ └── version.rb └── rack │ └── ignore_get_content_type.rb ├── Gemfile ├── spec ├── spec_helper.rb └── rack_spec.rb ├── .gitignore ├── LICENSE.md ├── ignore_get_content_type.gemspec └── README.md /lib/ignore_get_content_type.rb: -------------------------------------------------------------------------------- 1 | require 'rack/ignore_get_content_type' 2 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | # Specify your gem's dependencies in ignore_get_content_type.gemspec 4 | gemspec 5 | -------------------------------------------------------------------------------- /lib/ignore_get_content_type/version.rb: -------------------------------------------------------------------------------- 1 | module Rack 2 | module IgnoreGetContentTypeVersion 3 | VERSION = "0.0.1" 4 | end 5 | end 6 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | require 'ignore_get_content_type' 2 | require 'rack/test' 3 | require 'rspec' 4 | require 'simplecov' 5 | SimpleCov.start 6 | 7 | RSpec.configure do |config| 8 | end 9 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.gem 2 | *.rbc 3 | .bundle 4 | .config 5 | .yardoc 6 | Gemfile.lock 7 | InstalledFiles 8 | _yardoc 9 | coverage 10 | doc/ 11 | lib/bundler/man 12 | pkg 13 | rdoc 14 | spec/reports 15 | test/tmp 16 | test/version_tmp 17 | tmp 18 | *.bundle 19 | *.so 20 | *.o 21 | *.a 22 | mkmf.log 23 | -------------------------------------------------------------------------------- /lib/rack/ignore_get_content_type.rb: -------------------------------------------------------------------------------- 1 | require "ignore_get_content_type/version" 2 | 3 | module Rack 4 | class IgnoreGetContentType 5 | def initialize(app) 6 | @app = app 7 | end 8 | 9 | def call(env) 10 | req = Rack::Request.new(env) 11 | env['CONTENT_TYPE'] = nil if req.content_type && req.request_method == 'GET' 12 | @app.call(env) 13 | end 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /spec/rack_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe Rack::IgnoreGetContentType do 4 | include Rack::Test::Methods 5 | 6 | def app 7 | rack_app = lambda { |env| [200, {}, "app"] } 8 | Rack::IgnoreGetContentType.new(rack_app) 9 | end 10 | 11 | describe 'GET request' do 12 | context 'no Content-Type header is set' do 13 | it 'retains the absence of content type' do 14 | get '/' 15 | expect(last_response).to be_ok 16 | expect(last_request.env['CONTENT_TYPE']).to be_nil 17 | end 18 | end 19 | 20 | context 'a Content-Type header is set' do 21 | it 'ignores the Content Type' do 22 | get '/', {}, { 'CONTENT_TYPE' => 'application/x-www-form-urlencoded' } 23 | expect(last_response).to be_ok 24 | expect(last_request.env['CONTENT_TYPE']).to be_nil 25 | end 26 | end 27 | end 28 | 29 | describe 'POST request' do 30 | it 'retains the Content-Type' do 31 | post '/' 32 | expect(last_response).to be_ok 33 | expect(last_request.env['CONTENT_TYPE']).to eq('application/x-www-form-urlencoded') 34 | end 35 | end 36 | end 37 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014 Medidata Solutions Worldwide 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /ignore_get_content_type.gemspec: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | lib = File.expand_path('../lib', __FILE__) 3 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 4 | require 'ignore_get_content_type/version' 5 | 6 | Gem::Specification.new do |spec| 7 | spec.name = "ignore_get_content_type" 8 | spec.version = Rack::IgnoreGetContentTypeVersion::VERSION 9 | spec.authors = ["Connor Savage", "Purnima Mavinkurve"] 10 | spec.email = ["csavage@mdsol.com", "pmavinkurve@mdsol.com"] 11 | spec.summary = %q{A short piece of middleware to ignore content type set on GET requests} 12 | 13 | spec.files = `git ls-files -z`.split("\x0") 14 | spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } 15 | spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) 16 | spec.require_paths = ["lib"] 17 | 18 | spec.add_development_dependency "bundler", "~> 1.6" 19 | spec.add_development_dependency "rspec" 20 | spec.add_development_dependency "simplecov" 21 | spec.add_development_dependency "rack-test" 22 | spec.add_development_dependency "pry" 23 | spec.add_runtime_dependency "rack" 24 | end 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # IgnoreGetContentType 2 | 3 | 4 | IgnoreGetContentType is a small piece of middleware that enables the consuming application 5 | to ignore Content-Type header on GET requests. 6 | 7 | The Content-Type header field is used to specify the nature of the data in the body of a request. 8 | According to the HTTP spec it is perfectly valid for a GET request to have a body. However, 9 | server semantics require that if there is a body in a GET request, it must have no semantic 10 | meaning to the request. 11 | 12 | In other words, a requester may send a body with a GET request, but the server is forbidden 13 | to do anything with it. Therefore the Content-Type header in a GET request may be safely 14 | ignored, as it specifies the language of a book you are never going to read! 15 | 16 | Additional benefit of ignoring Content-Type header on GETs is that it helps with overcoming 17 | a peculiar limitation of ParamsWrapper's wrap_parameter configuration that unexpectedly adds 18 | an empty resource key in presence of Content-Type for GETs that accept query parameters. 19 | Please see: http://grokbase.com/t/gg/rubyonrails-talk/134jyh7x92/rails-request-with-content-type-application-json-add-extra-parameter 20 | for reference. 21 | 22 | ## Installation 23 | 24 | Add this line to your application's Gemfile: 25 | 26 | gem 'ignore_get_content_type' 27 | 28 | And then execute: 29 | 30 | $ bundle 31 | 32 | Or install it yourself as: 33 | 34 | $ gem install ignore_get_content_type 35 | 36 | ## Usage 37 | 38 | Example: 39 | 40 | ``` 41 | require 'ignore_get_content_type' 42 | config.middleware.use Rack::IgnoreGetContentType 43 | ``` 44 | 45 | ## Contributing 46 | 47 | 1. Fork it ( https://github.com/[my-github-username]/ignore_get_content_type/fork ) 48 | 2. Create your feature branch (`git checkout -b my-new-feature`) 49 | 3. Commit your changes (`git commit -am 'Add some feature'`) 50 | 4. Push to the branch (`git push origin my-new-feature`) 51 | 5. Create a new Pull Request 52 | --------------------------------------------------------------------------------