rack-methodoverride-4-all 1.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/Guardfile +6 -0
- data/LICENSE +22 -0
- data/README.md +47 -0
- data/Rakefile +2 -0
- data/lib/rack/methodoverride_4_all.rb +29 -0
- data/lib/rack-methodoverride-4-all.rb +2 -0
- data/rack-methodoverride-4-all.gemspec +22 -0
- data/spec/lib/rack/methodoverride_4_all_spec.rb +50 -0
- data/spec/spec_helper.rb +7 -0
- metadata +143 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 $GIT_AUTHOR_NAME
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
Rack MethodOverride 4 All
|
2
|
+
===============================
|
3
|
+
|
4
|
+
DISCLAIMER: This gem is a rewrite of Rack::MethodOverride. I hacked the original to suit my needs and to provide a nice
|
5
|
+
gem to whoever needs the same. :)
|
6
|
+
|
7
|
+
Rack::MethodOverride checks the "X_HTTP_METHOD_OVERRIDE" header and the query param "_method", however,
|
8
|
+
it only works for POST methods. Rack::MethodOverride4All works for any method (as long you mark it as "overridable"),
|
9
|
+
so, if you GET with a url like http://example.com?_method=post the application will see it as a POST request.
|
10
|
+
|
11
|
+
(I ignobly copied the text below from **dzello** "https://github.com/dzello/rack-methodoverride-with-params", I found
|
12
|
+
the info very useful. Pardon me.)
|
13
|
+
|
14
|
+
If you are building an application that relies on receiving cross domain requests, right now you really have to stick
|
15
|
+
with making GET requests from the client. Even with proper CORS headers, browsers still will not behave properly - the
|
16
|
+
specific problem I ran into is that they will not send cookies in a cross-domain POST - the GET works fine in IE8+,
|
17
|
+
FFox 3.5+, and in Safari/Chrome.
|
18
|
+
|
19
|
+
So, if you want to keep your Rails App RESTful and work around their issue (until "Access-Control-Allow-Credentials"
|
20
|
+
works properly), one solution is to send everything as a GET, and override as necessary.
|
21
|
+
|
22
|
+
See more about CORS at http://http://www.nczonline.net/blog/2010/05/25/cross-domain-ajax-with-cross-origin-resource-sharing/
|
23
|
+
|
24
|
+
To use it with Rack Builder
|
25
|
+
---------------------------
|
26
|
+
|
27
|
+
require 'rack-methodoverride-4-all'
|
28
|
+
#...
|
29
|
+
use Rack::MethodOverride4All
|
30
|
+
|
31
|
+
If you want to allow only "POST" methods to be overridable (as the original Rack::MethodOverride gem), you could:
|
32
|
+
|
33
|
+
use Rack::MethodOverride4All, [:post]
|
34
|
+
|
35
|
+
If you plan to use it to operate an API through the browser, you could:
|
36
|
+
|
37
|
+
use Rack::MethodOverride4All, [:get, :post] # Also, this is default
|
38
|
+
|
39
|
+
Contrib
|
40
|
+
---------------
|
41
|
+
This gem is really tiny, if you want to play with it, clone the thing, run the specs and be happy!
|
42
|
+
|
43
|
+
I definitively won't be actively working on this as it is really simple, but if you need something, you can reach
|
44
|
+
me at github (ruliana) or twitter(@ronie), I'll be glad to help. ^_^
|
45
|
+
|
46
|
+
Also, thx to the original author of Rack::MethodOverride, to [Josh Dzielak](http://github.com/dzello) and
|
47
|
+
[Nick Howard](http://github.com/baroquebobcat) for the code.
|
data/Rakefile
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
module Rack
|
2
|
+
class MethodOverride4All
|
3
|
+
HTTP_METHODS = %w(GET HEAD PUT POST DELETE OPTIONS)
|
4
|
+
METHOD_OVERRIDE_PARAM_KEY = '_method'
|
5
|
+
HTTP_METHOD_OVERRIDE_HEADER = 'HTTP_X_HTTP_METHOD_OVERRIDE'
|
6
|
+
|
7
|
+
def initialize(app, overridable = %w(GET POST))
|
8
|
+
@app = app
|
9
|
+
@overridable = HTTP_METHODS & overridable.map(&:to_s).map(&:upcase)
|
10
|
+
end
|
11
|
+
|
12
|
+
def call(env)
|
13
|
+
override_method(env) if (@overridable.include?(env['REQUEST_METHOD']))
|
14
|
+
@app.call(env)
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
def override_method(env)
|
19
|
+
req = Request.new(env)
|
20
|
+
method = req.params[METHOD_OVERRIDE_PARAM_KEY] || env[HTTP_METHOD_OVERRIDE_HEADER]
|
21
|
+
method = method.to_s.upcase
|
22
|
+
|
23
|
+
if HTTP_METHODS.include?(method)
|
24
|
+
env['rack.methodoverride.original_method'] = env['REQUEST_METHOD']
|
25
|
+
env['REQUEST_METHOD'] = method
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
Gem::Specification.new do |gem|
|
3
|
+
gem.authors = ['Ronie Uliana']
|
4
|
+
gem.email = ['ronie.uliana@gmail.com']
|
5
|
+
gem.description = %q{A replacement for Rack::MethodOverride that allows you to choose which methods are overridable}
|
6
|
+
gem.summary = %q{Rack::MethodOverride checks the "X_HTTP_METHOD_OVERRIDE" and the query param "_method", however, it only works for POST methods. Rack::MethodOverride4All works for any method (as long you mark it as "overridable"), so, if you GET xml with a url like http://example.com/?_method=delete the application will see it as a delete request.}
|
7
|
+
gem.homepage = 'http://github.com/ruliana/rack-methodoverride-4-all'
|
8
|
+
|
9
|
+
gem.files = `git ls-files`.split($\)
|
10
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
11
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
12
|
+
gem.name = "rack-methodoverride-4-all"
|
13
|
+
gem.require_paths = ['lib']
|
14
|
+
gem.version = '1.0.3'
|
15
|
+
|
16
|
+
gem.add_dependency 'rack', '~>1.4.1'
|
17
|
+
|
18
|
+
gem.add_development_dependency 'rack-test', '~>0.6.0'
|
19
|
+
gem.add_development_dependency 'rspec', '~>2.11.0'
|
20
|
+
gem.add_development_dependency 'guard-rspec', '~>2.1.0'
|
21
|
+
gem.add_development_dependency 'rb-inotify', '~>0.8.0'
|
22
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require_relative '../../spec_helper'
|
2
|
+
|
3
|
+
describe Rack::MethodOverride4All do
|
4
|
+
|
5
|
+
let(:mock_app) { double('Mock App', call: [200, { }, ['Mock App']]) }
|
6
|
+
|
7
|
+
context 'default configuration' do
|
8
|
+
let(:app) { Rack::MethodOverride4All.new(mock_app) }
|
9
|
+
|
10
|
+
it 'does NOT change method in "normal" calls (sanity check)' do
|
11
|
+
get '/'
|
12
|
+
expect(last_request.request_method).to eq('GET')
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'overrides method using request parameter' do
|
16
|
+
get '/?_method=post'
|
17
|
+
expect(last_request.request_method).to eq('POST')
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'overrides method using HEADER' do
|
21
|
+
header 'X_HTTP_METHOD_OVERRIDE', 'POST'
|
22
|
+
get '/'
|
23
|
+
expect(last_request.request_method).to eq('POST')
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'keeps the original request in another env var' do
|
27
|
+
get '/?_method=POST'
|
28
|
+
expect(last_request.env['rack.methodoverride.original_method']).to eq('GET')
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'does not override if new method is invalid' do
|
32
|
+
get '/?_method=INVALID'
|
33
|
+
expect(last_request.request_method).to eq('GET')
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context 'custom configuration using "POST" as only overridable method' do
|
38
|
+
let(:app) { Rack::MethodOverride4All.new(mock_app, [:post]) }
|
39
|
+
|
40
|
+
it 'overrides POST' do
|
41
|
+
post '/?_method=HEAD'
|
42
|
+
expect(last_request.request_method).to eq('HEAD')
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'does not override GET' do
|
46
|
+
get '/?_method=HEAD'
|
47
|
+
expect(last_request.request_method).to eq('GET')
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,143 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack-methodoverride-4-all
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.3
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ronie Uliana
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-10-30 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rack
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 1.4.1
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 1.4.1
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rack-test
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 0.6.0
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 0.6.0
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 2.11.0
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 2.11.0
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: guard-rspec
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: 2.1.0
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: 2.1.0
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: rb-inotify
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ~>
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: 0.8.0
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ~>
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: 0.8.0
|
94
|
+
description: A replacement for Rack::MethodOverride that allows you to choose which
|
95
|
+
methods are overridable
|
96
|
+
email:
|
97
|
+
- ronie.uliana@gmail.com
|
98
|
+
executables: []
|
99
|
+
extensions: []
|
100
|
+
extra_rdoc_files: []
|
101
|
+
files:
|
102
|
+
- .gitignore
|
103
|
+
- Gemfile
|
104
|
+
- Guardfile
|
105
|
+
- LICENSE
|
106
|
+
- README.md
|
107
|
+
- Rakefile
|
108
|
+
- lib/rack-methodoverride-4-all.rb
|
109
|
+
- lib/rack/methodoverride_4_all.rb
|
110
|
+
- rack-methodoverride-4-all.gemspec
|
111
|
+
- spec/lib/rack/methodoverride_4_all_spec.rb
|
112
|
+
- spec/spec_helper.rb
|
113
|
+
homepage: http://github.com/ruliana/rack-methodoverride-4-all
|
114
|
+
licenses: []
|
115
|
+
post_install_message:
|
116
|
+
rdoc_options: []
|
117
|
+
require_paths:
|
118
|
+
- lib
|
119
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
120
|
+
none: false
|
121
|
+
requirements:
|
122
|
+
- - ! '>='
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
126
|
+
none: false
|
127
|
+
requirements:
|
128
|
+
- - ! '>='
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: '0'
|
131
|
+
requirements: []
|
132
|
+
rubyforge_project:
|
133
|
+
rubygems_version: 1.8.24
|
134
|
+
signing_key:
|
135
|
+
specification_version: 3
|
136
|
+
summary: Rack::MethodOverride checks the "X_HTTP_METHOD_OVERRIDE" and the query param
|
137
|
+
"_method", however, it only works for POST methods. Rack::MethodOverride4All works
|
138
|
+
for any method (as long you mark it as "overridable"), so, if you GET xml with a
|
139
|
+
url like http://example.com/?_method=delete the application will see it as a delete
|
140
|
+
request.
|
141
|
+
test_files:
|
142
|
+
- spec/lib/rack/methodoverride_4_all_spec.rb
|
143
|
+
- spec/spec_helper.rb
|