grape 0.5.0 → 3.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +1223 -164
- data/CONTRIBUTING.md +148 -0
- data/LICENSE +1 -1
- data/README.md +3531 -675
- data/UPGRADING.md +1851 -0
- data/grape.gemspec +26 -32
- data/grape.png +0 -0
- data/lib/grape/api/instance.rb +194 -0
- data/lib/grape/api.rb +110 -485
- data/lib/grape/content_types.rb +28 -0
- data/lib/grape/cookies.rb +32 -25
- data/lib/grape/declared_params_handler.rb +118 -0
- data/lib/grape/dry_types.rb +54 -0
- data/lib/grape/dsl/callbacks.rb +19 -0
- data/lib/grape/dsl/declared.rb +35 -0
- data/lib/grape/dsl/desc.rb +65 -0
- data/lib/grape/dsl/headers.rb +21 -0
- data/lib/grape/dsl/helpers.rb +103 -0
- data/lib/grape/dsl/inside_route.rb +293 -0
- data/lib/grape/dsl/logger.rb +19 -0
- data/lib/grape/dsl/middleware.rb +36 -0
- data/lib/grape/dsl/parameters.rb +239 -0
- data/lib/grape/dsl/request_response.rb +168 -0
- data/lib/grape/dsl/routing.rb +288 -0
- data/lib/grape/dsl/settings.rb +67 -0
- data/lib/grape/dsl/validations.rb +51 -0
- data/lib/grape/endpoint.rb +287 -386
- data/lib/grape/env.rb +20 -0
- data/lib/grape/error_formatter/base.rb +56 -19
- data/lib/grape/error_formatter/json.rb +18 -7
- data/lib/grape/error_formatter/serializable_hash.rb +7 -0
- data/lib/grape/error_formatter/txt.rb +14 -11
- data/lib/grape/error_formatter/xml.rb +5 -11
- data/lib/grape/error_formatter.rb +15 -0
- data/lib/grape/exceptions/base.rb +31 -47
- data/lib/grape/exceptions/incompatible_option_values.rb +11 -0
- data/lib/grape/exceptions/invalid_accept_header.rb +11 -0
- data/lib/grape/exceptions/invalid_formatter.rb +3 -5
- data/lib/grape/exceptions/invalid_message_body.rb +11 -0
- data/lib/grape/exceptions/invalid_response.rb +11 -0
- data/lib/grape/exceptions/invalid_version_header.rb +11 -0
- data/lib/grape/exceptions/invalid_versioner_option.rb +3 -6
- data/lib/grape/exceptions/invalid_with_option_for_represent.rb +3 -7
- data/lib/grape/exceptions/method_not_allowed.rb +11 -0
- data/lib/grape/exceptions/missing_group_type.rb +11 -0
- data/lib/grape/exceptions/missing_mime_type.rb +3 -6
- data/lib/grape/exceptions/missing_vendor_option.rb +3 -5
- data/lib/grape/exceptions/request_error.rb +11 -0
- data/lib/grape/exceptions/unknown_auth_strategy.rb +11 -0
- data/lib/grape/exceptions/unknown_parameter.rb +11 -0
- data/lib/grape/exceptions/unknown_params_builder.rb +11 -0
- data/lib/grape/exceptions/unknown_validator.rb +3 -4
- data/lib/grape/exceptions/unsupported_group_type.rb +11 -0
- data/lib/grape/exceptions/validation.rb +20 -8
- data/lib/grape/exceptions/validation_array_errors.rb +14 -0
- data/lib/grape/exceptions/validation_errors.rb +59 -0
- data/lib/grape/formatter/base.rb +9 -26
- data/lib/grape/formatter/json.rb +6 -7
- data/lib/grape/formatter/serializable_hash.rb +24 -20
- data/lib/grape/formatter/txt.rb +5 -7
- data/lib/grape/formatter/xml.rb +6 -7
- data/lib/grape/formatter.rb +17 -0
- data/lib/grape/json.rb +10 -0
- data/lib/grape/locale/en.yml +47 -20
- data/lib/grape/middleware/auth/base.rb +25 -0
- data/lib/grape/middleware/auth/dsl.rb +38 -0
- data/lib/grape/middleware/auth/strategies.rb +25 -0
- data/lib/grape/middleware/auth/strategy_info.rb +15 -0
- data/lib/grape/middleware/base.rb +73 -19
- data/lib/grape/middleware/error.rb +104 -44
- data/lib/grape/middleware/filter.rb +5 -3
- data/lib/grape/middleware/formatter.rb +115 -105
- data/lib/grape/middleware/globals.rb +14 -0
- data/lib/grape/middleware/stack.rb +110 -0
- data/lib/grape/middleware/versioner/accept_version_header.rb +11 -40
- data/lib/grape/middleware/versioner/base.rb +76 -0
- data/lib/grape/middleware/versioner/header.rb +61 -78
- data/lib/grape/middleware/versioner/param.rb +9 -21
- data/lib/grape/middleware/versioner/path.rb +16 -31
- data/lib/grape/middleware/versioner.rb +10 -15
- data/lib/grape/namespace.rb +39 -10
- data/lib/grape/params_builder/base.rb +20 -0
- data/lib/grape/params_builder/hash.rb +11 -0
- data/lib/grape/params_builder/hash_with_indifferent_access.rb +11 -0
- data/lib/grape/params_builder/hashie_mash.rb +11 -0
- data/lib/grape/params_builder.rb +15 -0
- data/lib/grape/parser/base.rb +9 -24
- data/lib/grape/parser/json.rb +8 -7
- data/lib/grape/parser/xml.rb +8 -7
- data/lib/grape/parser.rb +15 -0
- data/lib/grape/path.rb +76 -0
- data/lib/grape/presenters/presenter.rb +11 -0
- data/lib/grape/railtie.rb +9 -0
- data/lib/grape/request.rb +182 -0
- data/lib/grape/router/base_route.rb +48 -0
- data/lib/grape/router/greedy_route.rb +26 -0
- data/lib/grape/router/pattern.rb +67 -0
- data/lib/grape/router/route.rb +58 -0
- data/lib/grape/router.rb +167 -0
- data/lib/grape/serve_stream/file_body.rb +36 -0
- data/lib/grape/serve_stream/sendfile_response.rb +21 -0
- data/lib/grape/serve_stream/stream_response.rb +23 -0
- data/lib/grape/util/api_description.rb +58 -0
- data/lib/grape/util/base_inheritable.rb +46 -0
- data/lib/grape/util/cache.rb +18 -0
- data/lib/grape/util/deep_freeze.rb +35 -0
- data/lib/grape/util/endpoint_configuration.rb +8 -0
- data/lib/grape/util/header.rb +13 -0
- data/lib/grape/util/inheritable_setting.rb +107 -0
- data/lib/grape/util/inheritable_values.rb +29 -0
- data/lib/grape/util/lazy/block.rb +29 -0
- data/lib/grape/util/lazy/value.rb +38 -0
- data/lib/grape/util/lazy/value_array.rb +21 -0
- data/lib/grape/util/lazy/value_enumerable.rb +34 -0
- data/lib/grape/util/lazy/value_hash.rb +21 -0
- data/lib/grape/util/media_type.rb +70 -0
- data/lib/grape/util/registry.rb +27 -0
- data/lib/grape/util/reverse_stackable_values.rb +15 -0
- data/lib/grape/util/stackable_values.rb +36 -0
- data/lib/grape/util/translation.rb +42 -0
- data/lib/grape/validations/attributes_iterator.rb +77 -0
- data/lib/grape/validations/contract_scope.rb +28 -0
- data/lib/grape/validations/multiple_attributes_iterator.rb +13 -0
- data/lib/grape/validations/param_scope_tracker.rb +57 -0
- data/lib/grape/validations/params_documentation.rb +50 -0
- data/lib/grape/validations/params_scope.rb +529 -0
- data/lib/grape/validations/single_attribute_iterator.rb +26 -0
- data/lib/grape/validations/types/array_coercer.rb +60 -0
- data/lib/grape/validations/types/custom_type_coercer.rb +164 -0
- data/lib/grape/validations/types/custom_type_collection_coercer.rb +56 -0
- data/lib/grape/validations/types/dry_type_coercer.rb +59 -0
- data/lib/grape/validations/types/file.rb +31 -0
- data/lib/grape/validations/types/invalid_value.rb +17 -0
- data/lib/grape/validations/types/json.rb +71 -0
- data/lib/grape/validations/types/multiple_type_coercer.rb +57 -0
- data/lib/grape/validations/types/primitive_coercer.rb +46 -0
- data/lib/grape/validations/types/set_coercer.rb +35 -0
- data/lib/grape/validations/types/variant_collection_coercer.rb +51 -0
- data/lib/grape/validations/types.rb +198 -0
- data/lib/grape/validations/validators/all_or_none_of_validator.rb +19 -0
- data/lib/grape/validations/validators/allow_blank_validator.rb +25 -0
- data/lib/grape/validations/validators/as_validator.rb +14 -0
- data/lib/grape/validations/validators/at_least_one_of_validator.rb +18 -0
- data/lib/grape/validations/validators/base.rb +158 -0
- data/lib/grape/validations/validators/coerce_validator.rb +55 -0
- data/lib/grape/validations/validators/contract_scope_validator.rb +45 -0
- data/lib/grape/validations/validators/default_validator.rb +31 -0
- data/lib/grape/validations/validators/exactly_one_of_validator.rb +24 -0
- data/lib/grape/validations/validators/except_values_validator.rb +33 -0
- data/lib/grape/validations/validators/length_validator.rb +48 -0
- data/lib/grape/validations/validators/multiple_params_base.rb +34 -0
- data/lib/grape/validations/validators/mutually_exclusive_validator.rb +18 -0
- data/lib/grape/validations/validators/presence_validator.rb +17 -0
- data/lib/grape/validations/validators/regexp_validator.rb +24 -0
- data/lib/grape/validations/validators/same_as_validator.rb +20 -0
- data/lib/grape/validations/validators/values_validator.rb +68 -0
- data/lib/grape/validations.rb +10 -226
- data/lib/grape/version.rb +4 -1
- data/lib/grape/xml.rb +10 -0
- data/lib/grape.rb +79 -77
- metadata +160 -306
- data/.gitignore +0 -38
- data/.rspec +0 -2
- data/.travis.yml +0 -9
- data/.yardopts +0 -2
- data/Gemfile +0 -19
- data/Guardfile +0 -15
- data/Rakefile +0 -64
- data/lib/grape/exceptions/missing_option.rb +0 -13
- data/lib/grape/exceptions/unknown_options.rb +0 -14
- data/lib/grape/http/request.rb +0 -28
- data/lib/grape/middleware/auth/basic.rb +0 -30
- data/lib/grape/middleware/auth/digest.rb +0 -30
- data/lib/grape/middleware/auth/oauth2.rb +0 -72
- data/lib/grape/route.rb +0 -23
- data/lib/grape/util/content_types.rb +0 -17
- data/lib/grape/util/deep_merge.rb +0 -23
- data/lib/grape/util/hash_stack.rb +0 -120
- data/lib/grape/validations/coerce.rb +0 -62
- data/lib/grape/validations/default.rb +0 -24
- data/lib/grape/validations/presence.rb +0 -12
- data/lib/grape/validations/regexp.rb +0 -14
- data/spec/grape/api_spec.rb +0 -2129
- data/spec/grape/endpoint_spec.rb +0 -605
- data/spec/grape/entity_spec.rb +0 -281
- data/spec/grape/exceptions/invalid_formatter_spec.rb +0 -18
- data/spec/grape/exceptions/invalid_versioner_option_spec.rb +0 -18
- data/spec/grape/exceptions/missing_mime_type_spec.rb +0 -24
- data/spec/grape/exceptions/missing_option_spec.rb +0 -18
- data/spec/grape/exceptions/unknown_options_spec.rb +0 -18
- data/spec/grape/exceptions/unknown_validator_spec.rb +0 -18
- data/spec/grape/middleware/auth/basic_spec.rb +0 -31
- data/spec/grape/middleware/auth/digest_spec.rb +0 -47
- data/spec/grape/middleware/auth/oauth2_spec.rb +0 -90
- data/spec/grape/middleware/base_spec.rb +0 -63
- data/spec/grape/middleware/error_spec.rb +0 -49
- data/spec/grape/middleware/exception_spec.rb +0 -164
- data/spec/grape/middleware/formatter_spec.rb +0 -244
- data/spec/grape/middleware/versioner/accept_version_header_spec.rb +0 -121
- data/spec/grape/middleware/versioner/header_spec.rb +0 -277
- data/spec/grape/middleware/versioner/param_spec.rb +0 -58
- data/spec/grape/middleware/versioner/path_spec.rb +0 -44
- data/spec/grape/middleware/versioner_spec.rb +0 -22
- data/spec/grape/util/hash_stack_spec.rb +0 -133
- data/spec/grape/validations/coerce_spec.rb +0 -143
- data/spec/grape/validations/default_spec.rb +0 -67
- data/spec/grape/validations/presence_spec.rb +0 -144
- data/spec/grape/validations/regexp_spec.rb +0 -33
- data/spec/grape/validations/zh-CN.yml +0 -9
- data/spec/grape/validations_spec.rb +0 -217
- data/spec/shared/versioning_examples.rb +0 -121
- data/spec/spec_helper.rb +0 -25
- data/spec/support/basic_auth_encode_helpers.rb +0 -4
- data/spec/support/versioned_helpers.rb +0 -48
data/lib/grape/path.rb
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Grape
|
|
4
|
+
# Represents a path to an endpoint.
|
|
5
|
+
class Path
|
|
6
|
+
DEFAULT_FORMAT_SEGMENT = '(/.:format)'
|
|
7
|
+
NO_VERSIONING_WITH_VALID_PATH_FORMAT_SEGMENT = '(.:format)'
|
|
8
|
+
VERSION_SEGMENT = ':version'
|
|
9
|
+
|
|
10
|
+
attr_reader :origin, :suffix
|
|
11
|
+
|
|
12
|
+
def initialize(raw_path, raw_namespace, settings)
|
|
13
|
+
@origin = PartsCache[build_parts(raw_path, raw_namespace, settings)]
|
|
14
|
+
@suffix = build_suffix(raw_path, raw_namespace, settings)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def to_s
|
|
18
|
+
"#{origin}#{suffix}"
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
private
|
|
22
|
+
|
|
23
|
+
def build_suffix(raw_path, raw_namespace, settings)
|
|
24
|
+
if uses_specific_format?(settings)
|
|
25
|
+
"(.#{settings[:format]})"
|
|
26
|
+
elsif !uses_path_versioning?(settings) || (valid_part?(raw_namespace) || valid_part?(raw_path))
|
|
27
|
+
NO_VERSIONING_WITH_VALID_PATH_FORMAT_SEGMENT
|
|
28
|
+
else
|
|
29
|
+
DEFAULT_FORMAT_SEGMENT
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def build_parts(raw_path, raw_namespace, settings)
|
|
34
|
+
[].tap do |parts|
|
|
35
|
+
add_part(parts, settings[:mount_path])
|
|
36
|
+
add_part(parts, settings[:root_prefix])
|
|
37
|
+
parts << VERSION_SEGMENT if uses_path_versioning?(settings)
|
|
38
|
+
add_part(parts, raw_namespace)
|
|
39
|
+
add_part(parts, raw_path)
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def add_part(parts, value)
|
|
44
|
+
parts << value if value && not_slash?(value)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def not_slash?(value)
|
|
48
|
+
value != '/'
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def uses_specific_format?(settings)
|
|
52
|
+
return false unless settings.key?(:format) && settings.key?(:content_types)
|
|
53
|
+
|
|
54
|
+
settings[:format] && Array(settings[:content_types]).size == 1
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def uses_path_versioning?(settings)
|
|
58
|
+
return false unless settings.key?(:version) && settings[:version_options]&.key?(:using)
|
|
59
|
+
|
|
60
|
+
settings[:version] && settings[:version_options][:using] == :path
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def valid_part?(part)
|
|
64
|
+
part&.match?(/^\S/) && not_slash?(part)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
class PartsCache < Grape::Util::Cache
|
|
68
|
+
def initialize
|
|
69
|
+
super
|
|
70
|
+
@cache = Hash.new do |h, parts|
|
|
71
|
+
h[parts] = Grape::Router.normalize_path(parts.join('/'))
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Grape
|
|
4
|
+
class Request < Rack::Request
|
|
5
|
+
# Based on rack 3 KNOWN_HEADERS
|
|
6
|
+
# https://github.com/rack/rack/blob/4f15e7b814922af79605be4b02c5b7c3044ba206/lib/rack/headers.rb#L10
|
|
7
|
+
|
|
8
|
+
KNOWN_HEADERS = %w[
|
|
9
|
+
Accept
|
|
10
|
+
Accept-CH
|
|
11
|
+
Accept-Encoding
|
|
12
|
+
Accept-Language
|
|
13
|
+
Accept-Patch
|
|
14
|
+
Accept-Ranges
|
|
15
|
+
Accept-Version
|
|
16
|
+
Access-Control-Allow-Credentials
|
|
17
|
+
Access-Control-Allow-Headers
|
|
18
|
+
Access-Control-Allow-Methods
|
|
19
|
+
Access-Control-Allow-Origin
|
|
20
|
+
Access-Control-Expose-Headers
|
|
21
|
+
Access-Control-Max-Age
|
|
22
|
+
Age
|
|
23
|
+
Allow
|
|
24
|
+
Alt-Svc
|
|
25
|
+
Authorization
|
|
26
|
+
Cache-Control
|
|
27
|
+
Client-Ip
|
|
28
|
+
Connection
|
|
29
|
+
Content-Disposition
|
|
30
|
+
Content-Encoding
|
|
31
|
+
Content-Language
|
|
32
|
+
Content-Length
|
|
33
|
+
Content-Location
|
|
34
|
+
Content-MD5
|
|
35
|
+
Content-Range
|
|
36
|
+
Content-Security-Policy
|
|
37
|
+
Content-Security-Policy-Report-Only
|
|
38
|
+
Content-Type
|
|
39
|
+
Cookie
|
|
40
|
+
Date
|
|
41
|
+
Delta-Base
|
|
42
|
+
Dnt
|
|
43
|
+
ETag
|
|
44
|
+
Expect-CT
|
|
45
|
+
Expires
|
|
46
|
+
Feature-Policy
|
|
47
|
+
Forwarded
|
|
48
|
+
Host
|
|
49
|
+
If-Modified-Since
|
|
50
|
+
If-None-Match
|
|
51
|
+
IM
|
|
52
|
+
Last-Modified
|
|
53
|
+
Link
|
|
54
|
+
Location
|
|
55
|
+
NEL
|
|
56
|
+
P3P
|
|
57
|
+
Permissions-Policy
|
|
58
|
+
Pragma
|
|
59
|
+
Preference-Applied
|
|
60
|
+
Proxy-Authenticate
|
|
61
|
+
Public-Key-Pins
|
|
62
|
+
Range
|
|
63
|
+
Referer
|
|
64
|
+
Referrer-Policy
|
|
65
|
+
Refresh
|
|
66
|
+
Report-To
|
|
67
|
+
Retry-After
|
|
68
|
+
Sec-Fetch-Dest
|
|
69
|
+
Sec-Fetch-Mode
|
|
70
|
+
Sec-Fetch-Site
|
|
71
|
+
Sec-Fetch-User
|
|
72
|
+
Server
|
|
73
|
+
Set-Cookie
|
|
74
|
+
Status
|
|
75
|
+
Strict-Transport-Security
|
|
76
|
+
Timing-Allow-Origin
|
|
77
|
+
Tk
|
|
78
|
+
Trailer
|
|
79
|
+
Transfer-Encoding
|
|
80
|
+
Upgrade
|
|
81
|
+
Upgrade-Insecure-Requests
|
|
82
|
+
User-Agent
|
|
83
|
+
Vary
|
|
84
|
+
Version
|
|
85
|
+
Via
|
|
86
|
+
Warning
|
|
87
|
+
WWW-Authenticate
|
|
88
|
+
X-Accel-Buffering
|
|
89
|
+
X-Accel-Charset
|
|
90
|
+
X-Accel-Expires
|
|
91
|
+
X-Accel-Limit-Rate
|
|
92
|
+
X-Accel-Mapping
|
|
93
|
+
X-Accel-Redirect
|
|
94
|
+
X-Access-Token
|
|
95
|
+
X-Auth-Request-Access-Token
|
|
96
|
+
X-Auth-Request-Email
|
|
97
|
+
X-Auth-Request-Groups
|
|
98
|
+
X-Auth-Request-Preferred-Username
|
|
99
|
+
X-Auth-Request-Redirect
|
|
100
|
+
X-Auth-Request-Token
|
|
101
|
+
X-Auth-Request-User
|
|
102
|
+
X-Cascade
|
|
103
|
+
X-Client-Ip
|
|
104
|
+
X-Content-Duration
|
|
105
|
+
X-Content-Security-Policy
|
|
106
|
+
X-Content-Type-Options
|
|
107
|
+
X-Correlation-Id
|
|
108
|
+
X-Download-Options
|
|
109
|
+
X-Forwarded-Access-Token
|
|
110
|
+
X-Forwarded-Email
|
|
111
|
+
X-Forwarded-For
|
|
112
|
+
X-Forwarded-Groups
|
|
113
|
+
X-Forwarded-Host
|
|
114
|
+
X-Forwarded-Port
|
|
115
|
+
X-Forwarded-Preferred-Username
|
|
116
|
+
X-Forwarded-Proto
|
|
117
|
+
X-Forwarded-Scheme
|
|
118
|
+
X-Forwarded-Ssl
|
|
119
|
+
X-Forwarded-Uri
|
|
120
|
+
X-Forwarded-User
|
|
121
|
+
X-Frame-Options
|
|
122
|
+
X-HTTP-Method-Override
|
|
123
|
+
X-Permitted-Cross-Domain-Policies
|
|
124
|
+
X-Powered-By
|
|
125
|
+
X-Real-IP
|
|
126
|
+
X-Redirect-By
|
|
127
|
+
X-Request-Id
|
|
128
|
+
X-Requested-With
|
|
129
|
+
X-Runtime
|
|
130
|
+
X-Sendfile
|
|
131
|
+
X-Sendfile-Type
|
|
132
|
+
X-UA-Compatible
|
|
133
|
+
X-WebKit-CS
|
|
134
|
+
X-XSS-Protection
|
|
135
|
+
].each_with_object({}) do |header, response|
|
|
136
|
+
response["HTTP_#{header.upcase.tr('-', '_')}"] = header
|
|
137
|
+
end.freeze
|
|
138
|
+
|
|
139
|
+
alias rack_params params
|
|
140
|
+
alias rack_cookies cookies
|
|
141
|
+
|
|
142
|
+
def initialize(env, build_params_with: nil)
|
|
143
|
+
super(env)
|
|
144
|
+
@params_builder = Grape::ParamsBuilder.params_builder_for(build_params_with || Grape.config.param_builder)
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
def params
|
|
148
|
+
@params ||= make_params
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
def headers
|
|
152
|
+
@headers ||= build_headers
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
def cookies
|
|
156
|
+
@cookies ||= Grape::Cookies.new(-> { rack_cookies })
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
# needs to be public until extensions param_builder are removed
|
|
160
|
+
def grape_routing_args
|
|
161
|
+
# preserve version from query string parameters
|
|
162
|
+
env[Grape::Env::GRAPE_ROUTING_ARGS]&.except(:version, :route_info) || {}
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
private
|
|
166
|
+
|
|
167
|
+
def make_params
|
|
168
|
+
@params_builder.call(rack_params).deep_merge!(grape_routing_args)
|
|
169
|
+
rescue *Grape::RACK_ERRORS
|
|
170
|
+
raise Grape::Exceptions::RequestError
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
def build_headers
|
|
174
|
+
each_header.with_object(Grape::Util::Header.new) do |(k, v), headers|
|
|
175
|
+
next unless k.start_with? 'HTTP_'
|
|
176
|
+
|
|
177
|
+
transformed_header = KNOWN_HEADERS.fetch(k) { -k[5..].tr('_', '-').downcase }
|
|
178
|
+
headers[transformed_header] = v
|
|
179
|
+
end
|
|
180
|
+
end
|
|
181
|
+
end
|
|
182
|
+
end
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Grape
|
|
4
|
+
class Router
|
|
5
|
+
class BaseRoute
|
|
6
|
+
extend Forwardable
|
|
7
|
+
|
|
8
|
+
delegate_missing_to :@options
|
|
9
|
+
|
|
10
|
+
attr_reader :options, :pattern
|
|
11
|
+
|
|
12
|
+
def_delegators :@pattern, :path, :origin
|
|
13
|
+
def_delegators :@options, :description, :version, :requirements, :prefix, :anchor, :settings, :forward_match, *Grape::Util::ApiDescription::DSL_METHODS
|
|
14
|
+
|
|
15
|
+
def initialize(pattern, options = {})
|
|
16
|
+
@pattern = pattern
|
|
17
|
+
@options = options.is_a?(ActiveSupport::OrderedOptions) ? options : ActiveSupport::OrderedOptions.new.update(options)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# see https://github.com/ruby-grape/grape/issues/1348
|
|
21
|
+
def namespace
|
|
22
|
+
@namespace ||= @options[:namespace]
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def regexp_capture_index
|
|
26
|
+
@regexp_capture_index ||= CaptureIndexCache[@index]
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def pattern_regexp
|
|
30
|
+
@pattern.to_regexp
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def to_regexp(index)
|
|
34
|
+
@index = index
|
|
35
|
+
Regexp.new("(?<#{regexp_capture_index}>#{pattern_regexp})")
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
class CaptureIndexCache < Grape::Util::Cache
|
|
39
|
+
def initialize
|
|
40
|
+
super
|
|
41
|
+
@cache = Hash.new do |h, index|
|
|
42
|
+
h[index] = "_#{index}"
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Act like a Grape::Router::Route but for greedy_match
|
|
4
|
+
# see @neutral_map
|
|
5
|
+
|
|
6
|
+
module Grape
|
|
7
|
+
class Router
|
|
8
|
+
class GreedyRoute < BaseRoute
|
|
9
|
+
extend Forwardable
|
|
10
|
+
|
|
11
|
+
def_delegators :@endpoint, :call
|
|
12
|
+
|
|
13
|
+
attr_reader :endpoint, :allow_header
|
|
14
|
+
|
|
15
|
+
def initialize(pattern, endpoint:, allow_header:)
|
|
16
|
+
super(pattern)
|
|
17
|
+
@endpoint = endpoint
|
|
18
|
+
@allow_header = allow_header
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def params(_input = nil)
|
|
22
|
+
nil
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Grape
|
|
4
|
+
class Router
|
|
5
|
+
class Pattern
|
|
6
|
+
extend Forwardable
|
|
7
|
+
|
|
8
|
+
DEFAULT_CAPTURES = %w[format version].freeze
|
|
9
|
+
|
|
10
|
+
attr_reader :origin, :path, :pattern, :to_regexp
|
|
11
|
+
|
|
12
|
+
def_delegators :pattern, :params
|
|
13
|
+
def_delegators :to_regexp, :===
|
|
14
|
+
alias match? ===
|
|
15
|
+
|
|
16
|
+
def initialize(origin:, suffix:, anchor:, params:, format:, version:, requirements:)
|
|
17
|
+
@origin = origin
|
|
18
|
+
@path = PatternCache[[build_path_from_pattern(@origin, anchor), suffix]]
|
|
19
|
+
@pattern = Mustermann::Grape.new(@path, uri_decode: true, params:, capture: extract_capture(format, version, requirements))
|
|
20
|
+
@to_regexp = @pattern.to_regexp
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def captures_default
|
|
24
|
+
to_regexp.names
|
|
25
|
+
.delete_if { |n| DEFAULT_CAPTURES.include?(n) }
|
|
26
|
+
.to_h { |k| [k, ''] }
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
private
|
|
30
|
+
|
|
31
|
+
def extract_capture(format, version, requirements)
|
|
32
|
+
capture = {}
|
|
33
|
+
capture[:format] = map_str(format) if format.present?
|
|
34
|
+
capture[:version] = map_str(version) if version.present?
|
|
35
|
+
|
|
36
|
+
return capture if requirements.blank?
|
|
37
|
+
|
|
38
|
+
requirements.merge(capture)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def build_path_from_pattern(pattern, anchor)
|
|
42
|
+
if pattern.end_with?('*path')
|
|
43
|
+
pattern.dup.insert(pattern.rindex('/') + 1, '?')
|
|
44
|
+
elsif anchor
|
|
45
|
+
pattern
|
|
46
|
+
elsif pattern.end_with?('/')
|
|
47
|
+
"#{pattern}?*path"
|
|
48
|
+
else
|
|
49
|
+
"#{pattern}/?*path"
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def map_str(value)
|
|
54
|
+
Array.wrap(value).map(&:to_s)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
class PatternCache < Grape::Util::Cache
|
|
58
|
+
def initialize
|
|
59
|
+
super
|
|
60
|
+
@cache = Hash.new do |h, (pattern, suffix)|
|
|
61
|
+
h[[pattern, suffix]] = -"#{pattern}#{suffix}"
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Grape
|
|
4
|
+
class Router
|
|
5
|
+
class Route < BaseRoute
|
|
6
|
+
extend Forwardable
|
|
7
|
+
|
|
8
|
+
FORWARD_MATCH_METHOD = ->(input, pattern) { input.start_with?(pattern.origin) }
|
|
9
|
+
NON_FORWARD_MATCH_METHOD = ->(input, pattern) { pattern.match?(input) }
|
|
10
|
+
|
|
11
|
+
attr_reader :app, :request_method, :index
|
|
12
|
+
|
|
13
|
+
def_delegators :@app, :call
|
|
14
|
+
|
|
15
|
+
def initialize(endpoint, method, pattern, options)
|
|
16
|
+
super(pattern, options)
|
|
17
|
+
@app = endpoint
|
|
18
|
+
@request_method = upcase_method(method)
|
|
19
|
+
@match_function = options[:forward_match] ? FORWARD_MATCH_METHOD : NON_FORWARD_MATCH_METHOD
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def convert_to_head_request!
|
|
23
|
+
@request_method = Rack::HEAD
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def apply(app)
|
|
27
|
+
@app = app
|
|
28
|
+
self
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def match?(input)
|
|
32
|
+
return false if input.blank?
|
|
33
|
+
|
|
34
|
+
@match_function.call(input, pattern)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def params(input = nil)
|
|
38
|
+
return params_without_input if input.blank?
|
|
39
|
+
|
|
40
|
+
parsed = pattern.params(input)
|
|
41
|
+
return unless parsed
|
|
42
|
+
|
|
43
|
+
parsed.compact.symbolize_keys
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
private
|
|
47
|
+
|
|
48
|
+
def params_without_input
|
|
49
|
+
@params_without_input ||= pattern.captures_default.merge(options[:params])
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def upcase_method(method)
|
|
53
|
+
method_s = method.to_s
|
|
54
|
+
Grape::HTTP_SUPPORTED_METHODS.detect { |m| m.casecmp(method_s).zero? } || method_s.upcase
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
data/lib/grape/router.rb
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Grape
|
|
4
|
+
class Router
|
|
5
|
+
# Taken from Rails
|
|
6
|
+
# normalize_path("/foo") # => "/foo"
|
|
7
|
+
# normalize_path("/foo/") # => "/foo"
|
|
8
|
+
# normalize_path("foo") # => "/foo"
|
|
9
|
+
# normalize_path("") # => "/"
|
|
10
|
+
# normalize_path("/%ab") # => "/%AB"
|
|
11
|
+
# https://github.com/rails/rails/blob/00cc4ff0259c0185fe08baadaa40e63ea2534f6e/actionpack/lib/action_dispatch/journey/router/utils.rb#L19
|
|
12
|
+
def self.normalize_path(path)
|
|
13
|
+
return '/' unless path
|
|
14
|
+
return path if path == '/'
|
|
15
|
+
|
|
16
|
+
# Fast path for the overwhelming majority of paths that don't need to be normalized
|
|
17
|
+
return path if path.start_with?('/') && !(path.end_with?('/') || path.match?(%r{%|//}))
|
|
18
|
+
|
|
19
|
+
# Slow path
|
|
20
|
+
encoding = path.encoding
|
|
21
|
+
path = "/#{path}"
|
|
22
|
+
path.squeeze!('/')
|
|
23
|
+
|
|
24
|
+
unless path == '/'
|
|
25
|
+
path.delete_suffix!('/')
|
|
26
|
+
path.gsub!(/(%[a-f0-9]{2})/) { ::Regexp.last_match(1).upcase }
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
path.force_encoding(encoding)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def initialize
|
|
33
|
+
@neutral_map = []
|
|
34
|
+
@neutral_regexes = []
|
|
35
|
+
@map = Hash.new { |hash, key| hash[key] = [] }
|
|
36
|
+
@optimized_map = Hash.new { |hash, key| hash[key] = // }
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def compile!
|
|
40
|
+
return if @compiled
|
|
41
|
+
|
|
42
|
+
@union = Regexp.union(@neutral_regexes)
|
|
43
|
+
@neutral_regexes = nil
|
|
44
|
+
(Grape::HTTP_SUPPORTED_METHODS + ['*']).each do |method|
|
|
45
|
+
next unless @map.key?(method)
|
|
46
|
+
|
|
47
|
+
routes = @map[method]
|
|
48
|
+
optimized_map = routes.map.with_index { |route, index| route.to_regexp(index) }
|
|
49
|
+
@optimized_map[method] = Regexp.union(optimized_map)
|
|
50
|
+
end
|
|
51
|
+
@compiled = true
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def append(route)
|
|
55
|
+
@map[route.request_method] << route
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def associate_routes(greedy_route)
|
|
59
|
+
@neutral_regexes << greedy_route.to_regexp(@neutral_map.length)
|
|
60
|
+
@neutral_map << greedy_route
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def call(env)
|
|
64
|
+
with_optimization do
|
|
65
|
+
input = Router.normalize_path(env[Rack::PATH_INFO])
|
|
66
|
+
method = env[Rack::REQUEST_METHOD]
|
|
67
|
+
response, route = identity(input, method, env)
|
|
68
|
+
response || rotation(input, method, env, route)
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def recognize_path(input)
|
|
73
|
+
any = with_optimization { greedy_match?(input) }
|
|
74
|
+
return if any == default_response
|
|
75
|
+
|
|
76
|
+
any.endpoint
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
DEFAULT_RESPONSE_HEADERS = Grape::Util::Header.new.merge('X-Cascade' => 'pass').freeze
|
|
80
|
+
DEFAULT_RESPONSE_BODY = ['404 Not Found'].freeze
|
|
81
|
+
|
|
82
|
+
private
|
|
83
|
+
|
|
84
|
+
def identity(input, method, env)
|
|
85
|
+
route = nil
|
|
86
|
+
response = transaction(input, method, env) do
|
|
87
|
+
route = match?(input, method)
|
|
88
|
+
process_route(route, input, env) if route
|
|
89
|
+
end
|
|
90
|
+
[response, route]
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def rotation(input, method, env, exact_route)
|
|
94
|
+
response = nil
|
|
95
|
+
@map[method].each do |route|
|
|
96
|
+
next if exact_route == route
|
|
97
|
+
next unless route.match?(input)
|
|
98
|
+
|
|
99
|
+
response = process_route(route, input, env)
|
|
100
|
+
break unless cascade?(response)
|
|
101
|
+
end
|
|
102
|
+
response
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def transaction(input, method, env)
|
|
106
|
+
# using a Proc is important since `return` will exit the enclosing function
|
|
107
|
+
cascade_or_return_response = proc do |response|
|
|
108
|
+
if response
|
|
109
|
+
cascade?(response).tap do |cascade|
|
|
110
|
+
return response unless cascade
|
|
111
|
+
|
|
112
|
+
# we need to close the body if possible before dismissing
|
|
113
|
+
response[2].close if response[2].respond_to?(:close)
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
response = yield
|
|
119
|
+
last_response_cascade = cascade_or_return_response.call(response)
|
|
120
|
+
last_neighbor_route = greedy_match?(input)
|
|
121
|
+
|
|
122
|
+
# If last_neighbor_route exists and request method is OPTIONS,
|
|
123
|
+
# return response by using #include_allow_header.
|
|
124
|
+
return process_route(last_neighbor_route, input, env, include_allow_header: true) if !last_response_cascade && method == Rack::OPTIONS && last_neighbor_route
|
|
125
|
+
|
|
126
|
+
route = match?(input, '*')
|
|
127
|
+
|
|
128
|
+
return last_neighbor_route.call(env) if last_neighbor_route && last_response_cascade && route
|
|
129
|
+
|
|
130
|
+
last_response_cascade = cascade_or_return_response.call(process_route(route, input, env)) if route
|
|
131
|
+
|
|
132
|
+
return process_route(last_neighbor_route, input, env, include_allow_header: true) if !last_response_cascade && last_neighbor_route
|
|
133
|
+
|
|
134
|
+
nil
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
def process_route(route, input, env, include_allow_header: false)
|
|
138
|
+
args = env[Grape::Env::GRAPE_ROUTING_ARGS] || { route_info: route }
|
|
139
|
+
route_params = route.params(input)
|
|
140
|
+
routing_args = args.merge(route_params || {})
|
|
141
|
+
env[Grape::Env::GRAPE_ROUTING_ARGS] = routing_args
|
|
142
|
+
env[Grape::Env::GRAPE_ALLOWED_METHODS] = route.allow_header if include_allow_header
|
|
143
|
+
route.call(env)
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
def with_optimization
|
|
147
|
+
compile!
|
|
148
|
+
yield || default_response
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
def default_response
|
|
152
|
+
[404, DEFAULT_RESPONSE_HEADERS.dup, DEFAULT_RESPONSE_BODY.dup]
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
def match?(input, method)
|
|
156
|
+
@optimized_map[method].match(input) { |m| @map[method].detect { |route| m[route.regexp_capture_index] } }
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
def greedy_match?(input)
|
|
160
|
+
@union.match(input) { |m| @neutral_map.detect { |route| m[route.regexp_capture_index] } }
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
def cascade?(response)
|
|
164
|
+
response && response[1]['X-Cascade'] == 'pass'
|
|
165
|
+
end
|
|
166
|
+
end
|
|
167
|
+
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Grape
|
|
4
|
+
module ServeStream
|
|
5
|
+
CHUNK_SIZE = 16_384
|
|
6
|
+
|
|
7
|
+
# Class helps send file through API
|
|
8
|
+
class FileBody
|
|
9
|
+
attr_reader :path
|
|
10
|
+
|
|
11
|
+
# @param path [String]
|
|
12
|
+
def initialize(path)
|
|
13
|
+
@path = path
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# Need for Rack::Sendfile middleware
|
|
17
|
+
#
|
|
18
|
+
# @return [String]
|
|
19
|
+
def to_path
|
|
20
|
+
path
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def each
|
|
24
|
+
File.open(path, 'rb') do |file|
|
|
25
|
+
while (chunk = file.read(CHUNK_SIZE))
|
|
26
|
+
yield chunk
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def ==(other)
|
|
32
|
+
path == other.path
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Grape
|
|
4
|
+
module ServeStream
|
|
5
|
+
# Response should respond to to_path method
|
|
6
|
+
# for using Rack::SendFile middleware
|
|
7
|
+
class SendfileResponse < Rack::Response
|
|
8
|
+
def respond_to?(method_name, include_all = false)
|
|
9
|
+
if method_name == :to_path
|
|
10
|
+
@body.respond_to?(:to_path, include_all)
|
|
11
|
+
else
|
|
12
|
+
super
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def to_path
|
|
17
|
+
@body.to_path
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|