Skip to content

Commit

Permalink
Splitting sinatra/base.rb
Browse files Browse the repository at this point in the history
  • Loading branch information
Halan Pinheiro committed Jun 23, 2019
1 parent eee711b commit 69c3ddc
Show file tree
Hide file tree
Showing 10 changed files with 885 additions and 860 deletions.
868 changes: 8 additions & 860 deletions lib/sinatra/base.rb

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions lib/sinatra/base/bad_request.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Sinatra
class BadRequest < TypeError #:nodoc:
def http_status; 400 end
end
end
17 changes: 17 additions & 0 deletions lib/sinatra/base/common_logger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Sinatra
# Behaves exactly like Rack::CommonLogger with the notable exception that it does nothing,
# if another CommonLogger is already in the middleware chain.
class CommonLogger < Rack::CommonLogger
def call(env)
env['sinatra.commonlogger'] ? @app.call(env) : super
end

superclass.class_eval do
alias call_without_check call unless method_defined? :call_without_check
def call(env)
env['sinatra.commonlogger'] = true
call_without_check(env)
end
end
end
end
34 changes: 34 additions & 0 deletions lib/sinatra/base/extended_rack.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module Sinatra
# Some Rack handlers (Thin, Rainbows!) implement an extended body object protocol, however,
# some middleware (namely Rack::Lint) will break it by not mirroring the methods in question.
# This middleware will detect an extended body object and will make sure it reaches the
# handler directly. We do this here, so our middleware and middleware set up by the app will
# still be able to run.
class ExtendedRack < Struct.new(:app)
def call(env)
result, callback = app.call(env), env['async.callback']
return result unless callback and async?(*result)
after_response { callback.call result }
setup_close(env, *result)
throw :async
end

private

def setup_close(env, status, headers, body)
return unless body.respond_to? :close and env.include? 'async.close'
env['async.close'].callback { body.close }
env['async.close'].errback { body.close }
end

def after_response(&block)
raise NotImplementedError, "only supports EventMachine at the moment" unless defined? EventMachine
EventMachine.next_tick(&block)
end

def async?(status, headers, body)
return true if status == -1
body.respond_to? :callback and body.respond_to? :errback
end
end
end

0 comments on commit 69c3ddc

Please sign in to comment.