cuba-api 0.5.0 → 0.5.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/cuba_api/accept_content.rb +1 -1
- data/lib/cuba_api/accept_content.rb~ +1 -1
- data/lib/cuba_api/cors.rb +91 -87
- data/lib/cuba_api/current_user.rb~ +1 -1
- data/lib/cuba_api/guard.rb~ +1 -1
- data/lib/cuba_api/reponse_status.rb~ +1 -1
- data/lib/cuba_api/serializer.rb~ +1 -1
- data/lib/cuba_api/write_aspect.rb +1 -1
- data/lib/cuba_api/write_aspects.rb~ +1 -1
- data/spec/cors_with_config_spec.rb +1 -1
- metadata +2 -2
data/lib/cuba_api/cors.rb
CHANGED
@@ -1,113 +1,117 @@
|
|
1
|
-
|
1
|
+
require 'uri'
|
2
|
+
module CubaApi
|
3
|
+
class CORS
|
2
4
|
|
3
|
-
|
5
|
+
DEFAULT_METHODS = [ 'GET', 'HEAD', 'POST', 'PUT', 'DELETE' ]
|
4
6
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
7
|
+
def initialize( options, &block )
|
8
|
+
@options = options
|
9
|
+
# only for inspect
|
10
|
+
@config = options.config
|
11
|
+
# set default max_ago
|
12
|
+
self.max_age = 60 * 60 * 24 # one day
|
13
|
+
block.call self if block
|
14
|
+
end
|
13
15
|
|
14
|
-
|
15
|
-
|
16
|
-
|
16
|
+
def config
|
17
|
+
@config
|
18
|
+
end
|
17
19
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
20
|
+
def method_missing( method, *args )
|
21
|
+
m = method.to_s
|
22
|
+
if m.match /^_/
|
23
|
+
if m =~ /=$/
|
24
|
+
@options[ "cors_#{m[1..-2]}".to_sym ] = args.flatten
|
25
|
+
else
|
26
|
+
@options[ "cors_#{m[1..-1]}".to_sym ]
|
27
|
+
end
|
23
28
|
else
|
24
|
-
|
29
|
+
super
|
25
30
|
end
|
26
|
-
else
|
27
|
-
super
|
28
31
|
end
|
29
|
-
end
|
30
|
-
|
31
|
-
def max_age=( max )
|
32
|
-
@options[ :cors_max_age ] = max
|
33
|
-
end
|
34
|
-
|
35
|
-
def expose=( expose )
|
36
|
-
self._expose = expose
|
37
|
-
end
|
38
32
|
|
39
|
-
|
40
|
-
|
41
|
-
end
|
42
|
-
|
43
|
-
def origins( domain )
|
44
|
-
origins = self._origins
|
45
|
-
if origins == [ '*' ] || origins.nil? || origins.member?( domain )
|
46
|
-
domain
|
33
|
+
def max_age=( max )
|
34
|
+
@options[ :cors_max_age ] = max
|
47
35
|
end
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
self._methods = [ methods ].flatten.collect{ |h| h.to_s.upcase }
|
52
|
-
end
|
53
|
-
|
54
|
-
def methods( method, methods = nil )
|
55
|
-
methods = methods.collect { |m| m.to_s.upcase } if methods
|
56
|
-
if (methods || self._methods || DEFAULT_METHODS).member?( method.to_s.upcase )
|
57
|
-
methods || self._methods || DEFAULT_METHODS
|
36
|
+
|
37
|
+
def expose=( expose )
|
38
|
+
self._expose = expose
|
58
39
|
end
|
59
|
-
end
|
60
40
|
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
41
|
+
def origins=( *origins )
|
42
|
+
self._origins = [ origins ].flatten
|
43
|
+
end
|
44
|
+
|
45
|
+
def origins( domain )
|
46
|
+
if domain
|
47
|
+
host = URI.parse( domain ).host
|
48
|
+
origins = self._origins
|
49
|
+
if origins == [ '*' ] || origins.nil? || origins.member?( host )
|
50
|
+
domain
|
51
|
+
end
|
52
|
+
end
|
67
53
|
end
|
68
|
-
end
|
69
54
|
|
70
|
-
|
71
|
-
|
72
|
-
|
55
|
+
def methods=( *methods )
|
56
|
+
self._methods = [ methods ].flatten.collect{ |h| h.to_s.upcase }
|
57
|
+
end
|
58
|
+
|
59
|
+
def methods( method, methods = nil )
|
60
|
+
methods = methods.collect { |m| m.to_s.upcase } if methods
|
61
|
+
if (methods || self._methods || DEFAULT_METHODS).member?( method.to_s.upcase )
|
62
|
+
methods || self._methods || DEFAULT_METHODS
|
63
|
+
end
|
64
|
+
end
|
73
65
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
result = nil if result.empty?
|
82
|
-
result
|
83
|
-
else
|
84
|
-
headers
|
66
|
+
def allowed?( methods )
|
67
|
+
if methods
|
68
|
+
methods = methods.collect { |m| m.to_s.upcase }
|
69
|
+
( ( self._methods || DEFAULT_METHODS ) & methods ).size > 0
|
70
|
+
else
|
71
|
+
true
|
72
|
+
end
|
85
73
|
end
|
86
|
-
end
|
87
74
|
|
88
|
-
|
89
|
-
|
90
|
-
res[ 'Access-Control-Allow-Origin' ] = orig
|
75
|
+
def headers=( *headers )
|
76
|
+
self._headers = [ headers ].flatten.collect{ |h| h.to_s.upcase }
|
91
77
|
end
|
92
|
-
end
|
93
78
|
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
79
|
+
def headers( headers )
|
80
|
+
# return headers as they come in when not configured
|
81
|
+
headers = headers.split( /,\s+/ ) if headers
|
82
|
+
if self._headers && headers
|
83
|
+
given = headers.collect{ |h| h.to_s.upcase }
|
84
|
+
# give back the allowed subset of the given headers
|
85
|
+
result = given & self._headers
|
86
|
+
result = nil if result.empty?
|
87
|
+
result
|
88
|
+
else
|
89
|
+
headers
|
90
|
+
end
|
99
91
|
end
|
100
|
-
|
101
|
-
|
92
|
+
|
93
|
+
def allow_origin( req, res )
|
94
|
+
if orig = origins( req[ 'HTTP_ORIGIN' ] )
|
95
|
+
res[ 'Access-Control-Allow-Origin' ] = orig
|
96
|
+
end
|
102
97
|
end
|
103
|
-
|
104
|
-
|
98
|
+
|
99
|
+
def process( req, res, methods )
|
100
|
+
allow_origin( req, res )
|
101
|
+
res[ 'Access-Control-Max-Age' ] = _max_age.to_s if _max_age
|
102
|
+
if m = methods( req[ 'HTTP_ACCESS_CONTROL_REQUEST_METHOD' ], methods )
|
103
|
+
res[ 'Access-Control-Allow-Methods' ] = m.join( ', ' )
|
104
|
+
end
|
105
|
+
if h = headers( req[ 'HTTP_ACCESS_CONTROL_REQUEST_HEADERS' ] )
|
106
|
+
res[ 'Access-Control-Allow-Headers' ] = h.join( ', ' )
|
107
|
+
end
|
108
|
+
unless _expose.nil? || _expose.empty?
|
109
|
+
res[ 'Access-Control-Expose-Headers' ] = _expose.join( ', ' )
|
110
|
+
end
|
111
|
+
res.status = 200
|
105
112
|
end
|
106
|
-
res.status = 200
|
107
113
|
end
|
108
|
-
end
|
109
114
|
|
110
|
-
module CubaApi
|
111
115
|
module Cors
|
112
116
|
module ClassMethods
|
113
117
|
|
data/lib/cuba_api/guard.rb~
CHANGED
data/lib/cuba_api/serializer.rb~
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cuba-api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-10-01 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: cuba
|