Skip to content

Commit

Permalink
Merge pull request #238 from web-patriot/small_refactoring
Browse files Browse the repository at this point in the history
small refactoring
  • Loading branch information
macournoyer committed Jul 4, 2014
2 parents 33a6b76 + 2565cc8 commit 92a33d0
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 55 deletions.
85 changes: 40 additions & 45 deletions lib/rack/adapter/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,27 @@
#
# Based on http://fuzed.rubyforge.org/ Rails adapter
module Rack
module Adapter
module Adapter
class Rails
FILE_METHODS = %w(GET HEAD).freeze
def initialize(options={})
@root = options[:root] || Dir.pwd
@env = options[:environment] || 'development'

def initialize(options = {})
@root = options[:root] || Dir.pwd
@env = options[:environment] || 'development'
@prefix = options[:prefix]

load_application

@rails_app = if self.class.rack_based?
ActionController::Dispatcher.new
else
CgiApp.new
end

@file_app = Rack::File.new(::File.join(RAILS_ROOT, "public"))

@rails_app = self.class.rack_based? ? ActionController::Dispatcher.new : CgiApp.new
@file_app = Rack::File.new(::File.join(RAILS_ROOT, "public"))
end

def load_application
ENV['RAILS_ENV'] = @env

require "#{@root}/config/environment"
require 'dispatcher'

if @prefix
if ActionController::Base.respond_to?(:relative_url_root=)
ActionController::Base.relative_url_root = @prefix # Rails 2.1.1
Expand All @@ -45,17 +40,17 @@ def load_application
end
end
end

def file_exist?(path)
full_path = ::File.join(@file_app.root, Utils.unescape(path))
::File.file?(full_path) && ::File.readable_real?(full_path)
end

def call(env)
path = env['PATH_INFO'].chomp('/')
method = env['REQUEST_METHOD']
cached_path = (path.empty? ? 'index' : path) + ActionController::Base.page_cache_extension

if FILE_METHODS.include?(method)
if file_exist?(path) # Serve the file if it's there
return @file_app.call(env)
Expand All @@ -64,19 +59,19 @@ def call(env)
return @file_app.call(env)
end
end

# No static file, let Rails handle it
@rails_app.call(env)
end

def self.rack_based?
rails_version = ::Rails::VERSION
return false if rails_version::MAJOR < 2
return false if rails_version::MAJOR == 2 && rails_version::MINOR < 2
return false if rails_version::MAJOR == 2 && rails_version::MINOR == 2 && rails_version::TINY < 3
true # >= 2.2.3
end

protected
# For Rails pre Rack (2.3)
class CgiApp
Expand All @@ -91,7 +86,7 @@ def call(env)
response.finish
end
end

class CGIWrapper < ::CGI
def initialize(request, response, *args)
@request = request
Expand All @@ -101,36 +96,36 @@ def initialize(request, response, *args)

super *args
end
def header(options = "text/html")

def header(options = 'text/html')
if options.is_a?(String)
@response['Content-Type'] = options unless @response['Content-Type']
else
@response['Content-Length'] = options.delete('Content-Length').to_s if options['Content-Length']

@response['Content-Type'] = options.delete('type') || "text/html"
@response['Content-Type'] += "; charset=" + options.delete('charset') if options['charset']
@response['Content-Type'] += '; charset=' + options.delete('charset') if options['charset']

@response['Content-Language'] = options.delete('language') if options['language']
@response['Expires'] = options.delete('expires') if options['expires']

@response.status = options.delete('Status') if options['Status']

# Convert 'cookie' header to 'Set-Cookie' headers.
# Because Set-Cookie header can appear more the once in the response body,
# Because Set-Cookie header can appear more the once in the response body,
# we store it in a line break seperated string that will be translated to
# multiple Set-Cookie header by the handler.
if cookie = options.delete('cookie')
cookies = []

case cookie
when Array then cookie.each { |c| cookies << c.to_s }
when Hash then cookie.each { |_, c| cookies << c.to_s }
else cookies << cookie.to_s
end

@output_cookies.each { |c| cookies << c.to_s } if @output_cookies

@response['Set-Cookie'] = [@response['Set-Cookie'], cookies].compact
# See http://groups.google.com/group/rack-devel/browse_thread/thread/e8759b91a82c5a10/a8dbd4574fe97d69?#a8dbd4574fe97d69
if Thin.ruby_18?
Expand All @@ -139,42 +134,42 @@ def header(options = "text/html")
@response['Set-Cookie'] = @response['Set-Cookie'].join("\n")
end
end
options.each { |k,v| @response[k] = v }

options.each { |k, v| @response[k] = v }
end
""

''
end

def params
@params ||= @request.params
end

def cookies
@request.cookies
end

def query_string
@request.query_string
end

# Used to wrap the normal args variable used inside CGI.
def args
@args
end

# Used to wrap the normal env_table variable used inside CGI.
def env_table
@request.env
end

# Used to wrap the normal stdinput variable used inside CGI.
def stdinput
@input
end

def stdoutput
STDERR.puts "stdoutput should not be used."
STDERR.puts 'stdoutput should not be used.'
@response.body
end
end
Expand Down
20 changes: 10 additions & 10 deletions lib/thin/backends/swiftiply_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ module Backends
# Backend to act as a Swiftiply client (http://swiftiply.swiftcore.org).
class SwiftiplyClient < Base
attr_accessor :key

attr_accessor :host, :port
def initialize(host, port, options={})

def initialize(host, port, options = {})
@host = host
@port = port.to_i
@key = options[:swiftiply].to_s
Expand All @@ -26,31 +26,31 @@ def disconnect
def to_s
"#{@host}:#{@port} swiftiply"
end
end
end
end

class SwiftiplyConnection < Connection
def connection_completed
send_data swiftiply_handshake(@backend.key)
end

def persistent?
true
end

def unbind
super
EventMachine.add_timer(rand(2)) { reconnect(@backend.host, @backend.port) } if @backend.running?
end

protected
def swiftiply_handshake(key)
'swiftclient' << host_ip.collect { |x| sprintf('%02x', x.to_i)}.join << sprintf('%04x', @backend.port) << sprintf('%02x', key.length) << key
'swiftclient' << host_ip.collect { |x| sprintf('%02x', x.to_i) }.join << sprintf('%04x', @backend.port) << sprintf('%02x', key.length) << key
end

# For some reason Swiftiply request the current host
def host_ip
Socket.gethostbyname(@backend.host)[3].unpack('CCCC') rescue [0,0,0,0]
Socket.gethostbyname(@backend.host)[3].unpack('CCCC') rescue [0, 0, 0, 0]
end
end
end

0 comments on commit 92a33d0

Please sign in to comment.