Skip to content

Commit

Permalink
Merge pull request postrank-labs#58 from stevemohapibanks/master
Browse files Browse the repository at this point in the history
Moving Fiber creation up before middleware chain invoked
  • Loading branch information
dj2 committed Jun 22, 2011
2 parents a4cfd99 + 49ab8c7 commit ed8d202
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 37 deletions.
38 changes: 18 additions & 20 deletions lib/goliath/api.rb
Expand Up @@ -193,30 +193,28 @@ def respond_to_missing?(name, *)
# @param env [Goliath::Env] The request environment
# @return [Goliath::Connection::AsyncResponse] An async response.
def call(env)
Fiber.new {
begin
Thread.current[GOLIATH_ENV] = env
status, headers, body = response(env)
begin
Thread.current[GOLIATH_ENV] = env
status, headers, body = response(env)

if status
if body == Goliath::Response::STREAMING
env[STREAM_START].call(status, headers)
else
env[ASYNC_CALLBACK].call([status, headers, body])
end
if status
if body == Goliath::Response::STREAMING
env[STREAM_START].call(status, headers)
else
env[ASYNC_CALLBACK].call([status, headers, body])
end
end

rescue Goliath::Validation::Error => e
env[RACK_EXCEPTION] = e
env[ASYNC_CALLBACK].call(validation_error(e.status_code, e.message))
rescue Goliath::Validation::Error => e
env[RACK_EXCEPTION] = e
env[ASYNC_CALLBACK].call(validation_error(e.status_code, e.message))

rescue Exception => e
env.logger.error(e.message)
env.logger.error(e.backtrace.join("\n"))
env[RACK_EXCEPTION] = e
env[ASYNC_CALLBACK].call(validation_error(500, e.message))
end
}.resume
rescue Exception => e
env.logger.error(e.message)
env.logger.error(e.backtrace.join("\n"))
env[RACK_EXCEPTION] = e
env[ASYNC_CALLBACK].call(validation_error(500, e.message))
end

Goliath::Connection::AsyncResponse
end
Expand Down
16 changes: 15 additions & 1 deletion lib/goliath/rack/params.rb
Expand Up @@ -58,8 +58,22 @@ def retrieve_params(env)
params.merge!(post_params)
end

params
indifferent_params(params)
end

def indifferent_params(params)
params = indifferent_hash.merge(params)
params.each do |key, value|
next unless value.is_a?(Hash)
params[key] = indifferent_params(value)
end
end

# Creates a Hash with indifferent access.
def indifferent_hash
Hash.new {|hash,key| hash[key.to_s] if Symbol === key }
end

end
end
end
34 changes: 18 additions & 16 deletions lib/goliath/request.rb
Expand Up @@ -128,13 +128,15 @@ def close
#
# @return [Nil]
def process
begin
@state = :finished
@env['rack.input'].rewind if @env['rack.input']
post_process(@app.call(@env))
rescue Exception => e
server_exception(e)
end
Fiber.new {
begin
@state = :finished
@env['rack.input'].rewind if @env['rack.input']
post_process(@app.call(@env))
rescue Exception => e
server_exception(e)
end
}.resume
end

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

@conn.terminate_request(keep_alive)
@conn.terminate_request(keep_alive)
rescue Exception => e
server_exception(e)
end
Expand Down Expand Up @@ -206,13 +208,13 @@ def keep_alive
case @env[HTTP_VERSION]
# HTTP 1.1: all requests are persistent requests, client
# must send a Connection:close header to indicate otherwise
when '1.1' then
(@env[HTTP_PREFIX + CONNECTION].downcase != 'close') rescue true
when '1.1' then
(@env[HTTP_PREFIX + CONNECTION].downcase != 'close') rescue true

# HTTP 1.0: all requests are non keep-alive, client must
# send a Connection: Keep-Alive to indicate otherwise
when '1.0' then
(@env[HTTP_PREFIX + CONNECTION].downcase == 'keep-alive') rescue false
# HTTP 1.0: all requests are non keep-alive, client must
# send a Connection: Keep-Alive to indicate otherwise
when '1.0' then
(@env[HTTP_PREFIX + CONNECTION].downcase == 'keep-alive') rescue false
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions spec/unit/rack/params_spec.rb
Expand Up @@ -19,6 +19,7 @@

ret = @params.retrieve_params(@env)
ret['foo'].should == 'bar'
ret[:foo].should == 'bar'
ret['baz'].should == 'bonkey'
end

Expand All @@ -38,6 +39,8 @@
ret['foo'].should == 'bar'
ret['baz'].should == 'bonkey'
ret['zonk'].should == {'donk' => 'monk'}
ret[:zonk].should == {'donk' => 'monk'}
ret[:zonk][:donk].should == 'monk'
end

it 'parses arrays of data' do
Expand All @@ -47,6 +50,7 @@
ret['foo'].is_a?(Array).should be_true
ret['foo'].length.should == 3
ret['foo'].should == %w(bar baz foos)
ret[:foo].should == %w(bar baz foos)
end

it 'parses multipart data' do
Expand All @@ -67,6 +71,7 @@

ret = @params.retrieve_params(@env)
ret['submit-name'].should == 'Larry'
ret[:"submit-name"].should == 'Larry'
ret['submit-name-with-content'].should == 'Berry'
end

Expand Down Expand Up @@ -140,6 +145,7 @@

ret = @params.retrieve_params(@env)
ret['foo'].should == 'bar'
ret[:foo].should == 'bar'
end

it "handles empty input gracefully on JSON" do
Expand Down

0 comments on commit ed8d202

Please sign in to comment.