Skip to content

Commit

Permalink
Remove frozen allow_header. Now Dynamic
Browse files Browse the repository at this point in the history
  • Loading branch information
ericproulx committed Nov 11, 2024
1 parent f3e08bf commit bb7db5f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
10 changes: 6 additions & 4 deletions lib/grape/api/instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def inherit_settings(other_settings)
# this API into a usable form.
def initialize
@router = Router.new
add_head_and_options_methods
add_head_not_allowed_methods_and_options_methods
self.class.endpoints.each do |endpoint|
endpoint.mount_in(@router)
end
Expand Down Expand Up @@ -190,8 +190,10 @@ def cascade?
private

# For every resource add a 'OPTIONS' route that returns an HTTP 204 response
# with a list of HTTP methods that can be called.
def add_head_and_options_methods
# with a list of HTTP methods that can be called. Also add a route that
# will return an HTTP 405 response for any HTTP method that the resource
# cannot handle.
def add_head_not_allowed_methods_and_options_methods
# The paths we collected are prepared (cf. Path#prepare), so they
# contain already versioning information when using path versioning.
all_routes = self.class.endpoints.map(&:routes).flatten
Expand All @@ -217,7 +219,7 @@ def collect_route_config_per_pattern(all_routes)

@router.associate_routes(last_route.pattern, {
endpoint: last_route.app,
allow_header: allow_header.join(', ').freeze
allow_header: allow_header
})
end
end
Expand Down
5 changes: 3 additions & 2 deletions lib/grape/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,10 @@ def run
run_filters befores, :before

if (allowed_methods = env[Grape::Env::GRAPE_ALLOWED_METHODS])
raise Grape::Exceptions::MethodNotAllowed.new(header.merge('Allow' => allowed_methods)) unless options?
allow_header_value = allowed_methods.join(', ')
raise Grape::Exceptions::MethodNotAllowed.new(header.merge('Allow' => allow_header_value)) unless options?

header Grape::Http::Headers::ALLOW, allowed_methods
header Grape::Http::Headers::ALLOW, allow_header_value
response_object = ''
status 204
else
Expand Down
10 changes: 3 additions & 7 deletions lib/grape/middleware/versioner/header.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,10 @@ def match_best_quality_media_type!
if media_type
yield media_type
else
fail!(allowed_methods)
fail!
end
end

def allowed_methods
env[Grape::Env::GRAPE_ALLOWED_METHODS]
end

def accept_header
env[Grape::Http::Headers::HTTP_ACCEPT]
end
Expand Down Expand Up @@ -93,8 +89,8 @@ def invalid_version_header!(message)
raise Grape::Exceptions::InvalidVersionHeader.new(message, error_headers)
end

def fail!(grape_allowed_methods)
return grape_allowed_methods if grape_allowed_methods.present?
def fail!
return if env[Grape::Env::GRAPE_ALLOWED_METHODS].present?

media_types = q_values_mime_types.map { |mime_type| Grape::Util::MediaType.parse(mime_type) }
vendor_not_found!(media_types) || version_not_found!(media_types)
Expand Down

0 comments on commit bb7db5f

Please sign in to comment.