Skip to content

Commit ed8d202

Browse files
committed
Merge pull request postrank-labs#58 from stevemohapibanks/master
Moving Fiber creation up before middleware chain invoked
2 parents a4cfd99 + 49ab8c7 commit ed8d202

File tree

4 files changed

+57
-37
lines changed

4 files changed

+57
-37
lines changed

‎lib/goliath/api.rb‎

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -193,30 +193,28 @@ def respond_to_missing?(name, *)
193193
# @param env [Goliath::Env] The request environment
194194
# @return [Goliath::Connection::AsyncResponse] An async response.
195195
defcall(env)
196-
Fiber.new{
197-
begin
198-
Thread.current[GOLIATH_ENV]=env
199-
status,headers,body=response(env)
196+
begin
197+
Thread.current[GOLIATH_ENV]=env
198+
status,headers,body=response(env)
200199

201-
ifstatus
202-
ifbody == Goliath::Response::STREAMING
203-
env[STREAM_START].call(status,headers)
204-
else
205-
env[ASYNC_CALLBACK].call([status,headers,body])
206-
end
200+
ifstatus
201+
ifbody == Goliath::Response::STREAMING
202+
env[STREAM_START].call(status,headers)
203+
else
204+
env[ASYNC_CALLBACK].call([status,headers,body])
207205
end
206+
end
208207

209-
rescueGoliath::Validation::Error=>e
210-
env[RACK_EXCEPTION]=e
211-
env[ASYNC_CALLBACK].call(validation_error(e.status_code,e.message))
208+
rescueGoliath::Validation::Error=>e
209+
env[RACK_EXCEPTION]=e
210+
env[ASYNC_CALLBACK].call(validation_error(e.status_code,e.message))
212211

213-
rescueException=>e
214-
env.logger.error(e.message)
215-
env.logger.error(e.backtrace.join("\n"))
216-
env[RACK_EXCEPTION]=e
217-
env[ASYNC_CALLBACK].call(validation_error(500,e.message))
218-
end
219-
}.resume
212+
rescueException=>e
213+
env.logger.error(e.message)
214+
env.logger.error(e.backtrace.join("\n"))
215+
env[RACK_EXCEPTION]=e
216+
env[ASYNC_CALLBACK].call(validation_error(500,e.message))
217+
end
220218

221219
Goliath::Connection::AsyncResponse
222220
end

‎lib/goliath/rack/params.rb‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,22 @@ def retrieve_params(env)
5858
params.merge!(post_params)
5959
end
6060

61-
params
61+
indifferent_params(params)
6262
end
63+
64+
defindifferent_params(params)
65+
params=indifferent_hash.merge(params)
66+
params.eachdo |key,value|
67+
nextunlessvalue.is_a?(Hash)
68+
params[key]=indifferent_params(value)
69+
end
70+
end
71+
72+
# Creates a Hash with indifferent access.
73+
defindifferent_hash
74+
Hash.new{|hash,key| hash[key.to_s]ifSymbol === key}
75+
end
76+
6377
end
6478
end
6579
end

‎lib/goliath/request.rb‎

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,15 @@ def close
128128
#
129129
# @return [Nil]
130130
defprocess
131-
begin
132-
@state=:finished
133-
@env['rack.input'].rewindif@env['rack.input']
134-
post_process(@app.call(@env))
135-
rescueException=>e
136-
server_exception(e)
137-
end
131+
Fiber.new{
132+
begin
133+
@state=:finished
134+
@env['rack.input'].rewindif@env['rack.input']
135+
post_process(@app.call(@env))
136+
rescueException=>e
137+
server_exception(e)
138+
end
139+
}.resume
138140
end
139141

140142
# Invoked by the app / middleware once the request
@@ -166,10 +168,10 @@ def post_process(results)
166168
@response.status,@response.headers,@response.body=status,headers,body
167169
@response.each{ |chunk| @conn.send_data(chunk)}
168170
@env[RACK_LOGGER].info("Status: #{@response.status}, " +
169-
"Content-Length: #{@response.headers['Content-Length']}, " +
170-
"Response Time: #{"%.2f" % ((Time.now.to_f - @env[:start_time]) * 1000)}ms")
171+
"Content-Length: #{@response.headers['Content-Length']}, " +
172+
"Response Time: #{"%.2f" % ((Time.now.to_f - @env[:start_time]) * 1000)}ms")
171173

172-
@conn.terminate_request(keep_alive)
174+
@conn.terminate_request(keep_alive)
173175
rescueException=>e
174176
server_exception(e)
175177
end
@@ -206,13 +208,13 @@ def keep_alive
206208
case@env[HTTP_VERSION]
207209
# HTTP 1.1: all requests are persistent requests, client
208210
# must send a Connection:close header to indicate otherwise
209-
when'1.1'then
210-
(@env[HTTP_PREFIX + CONNECTION].downcase != 'close')rescuetrue
211+
when'1.1'then
212+
(@env[HTTP_PREFIX + CONNECTION].downcase != 'close')rescuetrue
211213

212-
# HTTP 1.0: all requests are non keep-alive, client must
213-
# send a Connection: Keep-Alive to indicate otherwise
214-
when'1.0'then
215-
(@env[HTTP_PREFIX + CONNECTION].downcase == 'keep-alive')rescuefalse
214+
# HTTP 1.0: all requests are non keep-alive, client must
215+
# send a Connection: Keep-Alive to indicate otherwise
216+
when'1.0'then
217+
(@env[HTTP_PREFIX + CONNECTION].downcase == 'keep-alive')rescuefalse
216218
end
217219
end
218220
end

‎spec/unit/rack/params_spec.rb‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
ret=@params.retrieve_params(@env)
2121
ret['foo'].should == 'bar'
22+
ret[:foo].should == 'bar'
2223
ret['baz'].should == 'bonkey'
2324
end
2425

@@ -38,6 +39,8 @@
3839
ret['foo'].should == 'bar'
3940
ret['baz'].should == 'bonkey'
4041
ret['zonk'].should == {'donk'=>'monk'}
42+
ret[:zonk].should == {'donk'=>'monk'}
43+
ret[:zonk][:donk].should == 'monk'
4144
end
4245

4346
it'parses arrays of data'do
@@ -47,6 +50,7 @@
4750
ret['foo'].is_a?(Array).shouldbe_true
4851
ret['foo'].length.should == 3
4952
ret['foo'].should == %w(barbazfoos)
53+
ret[:foo].should == %w(barbazfoos)
5054
end
5155

5256
it'parses multipart data'do
@@ -67,6 +71,7 @@
6771

6872
ret=@params.retrieve_params(@env)
6973
ret['submit-name'].should == 'Larry'
74+
ret[:"submit-name"].should == 'Larry'
7075
ret['submit-name-with-content'].should == 'Berry'
7176
end
7277

@@ -140,6 +145,7 @@
140145

141146
ret=@params.retrieve_params(@env)
142147
ret['foo'].should == 'bar'
148+
ret[:foo].should == 'bar'
143149
end
144150

145151
it"handles empty input gracefully on JSON"do

0 commit comments

Comments
(0)