Skip to content

Commit bb4d19b

Browse files
committed
Merge pull request #977 from u2/less-objects2
Less allocated objects on each request
2 parents c8fe152 + 37f5e28 commit bb4d19b

File tree

11 files changed

+57
-26
lines changed

11 files changed

+57
-26
lines changed

lib/grape.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ module Grape
4242
autoload :Validations, 'grape/validations'
4343
autoload :Request, 'grape/http/request'
4444

45+
module Http
46+
autoload :Headers, 'grape/http/headers'
47+
end
48+
4549
module Exceptions
4650
autoload :Base, 'grape/exceptions/base'
4751
autoload :Validation, 'grape/exceptions/validation'

lib/grape/api.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def initialize
9696

9797
def call(env)
9898
result = @route_set.call(env)
99-
result[1].delete('X-Cascade') unless cascade?
99+
result[1].delete(Grape::Http::Headers::X_CASCADE) unless cascade?
100100
result
101101
end
102102

@@ -142,12 +142,12 @@ def add_head_not_allowed_methods_and_options_methods
142142
methods_per_path.each do |path, methods|
143143
allowed_methods = methods.dup
144144
unless self.class.namespace_inheritable(:do_not_route_head)
145-
allowed_methods |= ['HEAD'] if allowed_methods.include?('GET')
145+
allowed_methods |= [Grape::Http::Headers::HEAD] if allowed_methods.include?(Grape::Http::Headers::GET)
146146
end
147147

148-
allow_header = (['OPTIONS'] | allowed_methods).join(', ')
148+
allow_header = ([Grape::Http::Headers::OPTIONS] | allowed_methods).join(', ')
149149
unless self.class.namespace_inheritable(:do_not_route_options)
150-
unless allowed_methods.include?('OPTIONS')
150+
unless allowed_methods.include?(Grape::Http::Headers::OPTIONS)
151151
self.class.options(path, {}) do
152152
header 'Allow', allow_header
153153
status 204
@@ -157,7 +157,7 @@ def add_head_not_allowed_methods_and_options_methods
157157
end
158158

159159
not_allowed_methods = %w(GET PUT POST DELETE PATCH HEAD) - allowed_methods
160-
not_allowed_methods << 'OPTIONS' if self.class.namespace_inheritable(:do_not_route_options)
160+
not_allowed_methods << Grape::Http::Headers::OPTIONS if self.class.namespace_inheritable(:do_not_route_options)
161161
self.class.route(not_allowed_methods, path) do
162162
header 'Allow', allow_header
163163
status 405

lib/grape/dsl/inside_route.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def redirect(url, options = {})
8080
if merged_options[:permanent]
8181
status 301
8282
else
83-
if env['HTTP_VERSION'] == 'HTTP/1.1' && request.request_method.to_s.upcase != 'GET'
83+
if env[Grape::Http::Headers::HTTP_VERSION] == 'HTTP/1.1' && request.request_method.to_s.upcase != Grape::Http::Headers::GET
8484
status 303
8585
else
8686
status 302
@@ -106,7 +106,7 @@ def status(status = nil)
106106
when nil
107107
return @status if @status
108108
case request.request_method.to_s.upcase
109-
when 'POST'
109+
when Grape::Http::Headers::POST
110110
201
111111
else
112112
200
@@ -129,9 +129,9 @@ def header(key = nil, val = nil)
129129
# Set response content-type
130130
def content_type(val = nil)
131131
if val
132-
header('Content-Type', val)
132+
header(Grape::Http::Headers::CONTENT_TYPE, val)
133133
else
134-
header['Content-Type']
134+
header[Grape::Http::Headers::CONTENT_TYPE]
135135
end
136136
end
137137

lib/grape/endpoint.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ def mount_in(route_set)
106106

107107
routes.each do |route|
108108
methods = [route.route_method]
109-
if !namespace_inheritable(:do_not_route_head) && route.route_method == 'GET'
110-
methods << 'HEAD'
109+
if !namespace_inheritable(:do_not_route_head) && route.route_method == Grape::Http::Headers::GET
110+
methods << Grape::Http::Headers::HEAD
111111
end
112112
methods.each do |method|
113113
route_set.add_route(self, {

lib/grape/http/headers.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module Grape
2+
module Http
3+
module Headers
4+
# https://github.com/rack/rack/blob/master/lib/rack.rb
5+
HTTP_VERSION = 'HTTP_VERSION'.freeze
6+
PATH_INFO = 'PATH_INFO'.freeze
7+
QUERY_STRING = 'QUERY_STRING'.freeze
8+
CONTENT_TYPE = 'Content-Type'.freeze
9+
10+
GET = 'GET'.freeze
11+
POST = 'POST'.freeze
12+
PUT = 'PUT'.freeze
13+
PATCH = 'PATCH'.freeze
14+
DELETE = 'DELETE'.freeze
15+
HEAD = 'HEAD'.freeze
16+
OPTIONS = 'OPTIONS'.freeze
17+
18+
HTTP_ACCEPT_VERSION = 'HTTP_ACCEPT_VERSION'.freeze
19+
X_CASCADE = 'X-Cascade'.freeze
20+
HTTP_TRANSFER_ENCODING = 'HTTP_TRANSFER_ENCODING'.freeze
21+
HTTP_ACCEPT = 'HTTP_ACCEPT'.freeze
22+
23+
ACCEPT = 'accept'.freeze
24+
FORMAT = 'format'.freeze
25+
end
26+
end
27+
end

lib/grape/middleware/error.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def exec_handler(e, &handler)
5959
end
6060

6161
def error!(message, status = options[:default_status], headers = {}, backtrace = [])
62-
headers = { 'Content-Type' => content_type }.merge(headers)
62+
headers = { Grape::Http::Headers::CONTENT_TYPE => content_type }.merge(headers)
6363
rack_response(format_message(message, backtrace), status, headers)
6464
end
6565

@@ -71,13 +71,13 @@ def handle_error(e)
7171
def error_response(error = {})
7272
status = error[:status] || options[:default_status]
7373
message = error[:message] || options[:default_message]
74-
headers = { 'Content-Type' => content_type }
74+
headers = { Grape::Http::Headers::CONTENT_TYPE => content_type }
7575
headers.merge!(error[:headers]) if error[:headers].is_a?(Hash)
7676
backtrace = error[:backtrace] || []
7777
rack_response(format_message(message, backtrace), status, headers)
7878
end
7979

80-
def rack_response(message, status = options[:default_status], headers = { 'Content-Type' => content_type })
80+
def rack_response(message, status = options[:default_status], headers = { Grape::Http::Headers::CONTENT_TYPE => content_type })
8181
Rack::Response.new([message], status, headers).finish
8282
end
8383

lib/grape/middleware/formatter.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def before
2626
def after
2727
status, headers, bodies = *@app_response
2828
# allow content-type to be explicitly overwritten
29-
api_format = mime_types[headers['Content-Type']] || env['api.format']
29+
api_format = mime_types[headers[Grape::Http::Headers::CONTENT_TYPE]] || env['api.format']
3030
formatter = Grape::Formatter::Base.formatter_for api_format, options
3131
begin
3232
bodymap = bodies.collect do |body|
@@ -35,7 +35,7 @@ def after
3535
rescue Grape::Exceptions::InvalidFormatter => e
3636
throw :error, status: 500, message: e.message
3737
end
38-
headers['Content-Type'] = content_type_for(env['api.format']) unless headers['Content-Type']
38+
headers[Grape::Http::Headers::CONTENT_TYPE] = content_type_for(env['api.format']) unless headers[Grape::Http::Headers::CONTENT_TYPE]
3939
Rack::Response.new(bodymap, status, headers).to_a
4040
end
4141

@@ -50,7 +50,7 @@ def read_body_input
5050
if (request.post? || request.put? || request.patch? || request.delete?) &&
5151
(!request.form_data? || !request.media_type) &&
5252
(!request.parseable_data?) &&
53-
(request.content_length.to_i > 0 || request.env['HTTP_TRANSFER_ENCODING'] == 'chunked')
53+
(request.content_length.to_i > 0 || request.env[Grape::Http::Headers::HTTP_TRANSFER_ENCODING] == 'chunked')
5454

5555
if (input = env['rack.input'])
5656
input.rewind
@@ -115,7 +115,7 @@ def format_from_extension
115115
end
116116

117117
def format_from_params
118-
fmt = Rack::Utils.parse_nested_query(env['QUERY_STRING'])['format']
118+
fmt = Rack::Utils.parse_nested_query(env[Grape::Http::Headers::QUERY_STRING])[Grape::Http::Headers::FORMAT]
119119
# avoid symbol memory leak on an unknown format
120120
return fmt.to_sym if content_type_for(fmt)
121121
fmt
@@ -129,7 +129,7 @@ def format_from_header
129129
end
130130

131131
def mime_array
132-
accept = headers['accept']
132+
accept = headers[Grape::Http::Headers::ACCEPT]
133133
return [] unless accept
134134

135135
accept_into_mime_and_quality = %r{

lib/grape/middleware/versioner/accept_version_header.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module Versioner
1818
# route.
1919
class AcceptVersionHeader < Base
2020
def before
21-
potential_version = (env['HTTP_ACCEPT_VERSION'] || '').strip
21+
potential_version = (env[Grape::Http::Headers::HTTP_ACCEPT_VERSION] || '').strip
2222

2323
if strict?
2424
# If no Accept-Version header:
@@ -59,7 +59,7 @@ def cascade?
5959
end
6060

6161
def error_headers
62-
cascade? ? { 'X-Cascade' => 'pass' } : {}
62+
cascade? ? { Grape::Http::Headers::X_CASCADE => 'pass' } : {}
6363
end
6464
end
6565
end

lib/grape/middleware/versioner/header.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def available_media_types
8484
end
8585

8686
def rack_accept_header
87-
Rack::Accept::MediaType.new env['HTTP_ACCEPT']
87+
Rack::Accept::MediaType.new env[Grape::Http::Headers::HTTP_ACCEPT]
8888
rescue RuntimeError => e
8989
raise Grape::Exceptions::InvalidAcceptHeader.new(e.message, error_headers)
9090
end
@@ -113,7 +113,7 @@ def cascade?
113113
end
114114

115115
def error_headers
116-
cascade? ? { 'X-Cascade' => 'pass' } : {}
116+
cascade? ? { Grape::Http::Headers::X_CASCADE => 'pass' } : {}
117117
end
118118

119119
# @param [String] media_type a content type

lib/grape/middleware/versioner/param.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ def default_options
2727

2828
def before
2929
paramkey = options[:parameter]
30-
potential_version = Rack::Utils.parse_nested_query(env['QUERY_STRING'])[paramkey]
30+
potential_version = Rack::Utils.parse_nested_query(env[Grape::Http::Headers::QUERY_STRING])[paramkey]
3131
unless potential_version.nil?
3232
if options[:versions] && !options[:versions].find { |v| v.to_s == potential_version }
33-
throw :error, status: 404, message: '404 API Version Not Found', headers: { 'X-Cascade' => 'pass' }
33+
throw :error, status: 404, message: '404 API Version Not Found', headers: { Grape::Http::Headers::X_CASCADE => 'pass' }
3434
end
3535
env['api.version'] = potential_version
3636
env['rack.request.query_hash'].delete(paramkey) if env.key? 'rack.request.query_hash'

0 commit comments

Comments
 (0)