Skip to content

Commit

Permalink
Move middlewares to root dir.
Browse files Browse the repository at this point in the history
  • Loading branch information
macournoyer committed Feb 2, 2013
1 parent 28b0fde commit daa7c56
Show file tree
Hide file tree
Showing 17 changed files with 221 additions and 234 deletions.
8 changes: 2 additions & 6 deletions examples/async.ru
@@ -1,7 +1,3 @@
require "thin/async"
require "thin/middlewares/async"
require "thin/middlewares/chunked"

class Async
def call(env)
response = Thin::AsyncResponse.new(env)
Expand All @@ -25,8 +21,8 @@ end

use Rack::Chunked

use Thin::Middlewares::Async do
use Thin::Middlewares::Chunked
use Thin::Async do
use Thin::Chunked
use Rack::CommonLogger
end

Expand Down
11 changes: 3 additions & 8 deletions examples/config.ru
@@ -1,8 +1,3 @@
require "thin/middlewares/threaded"
require "thin/middlewares/chunked"
require "thin/middlewares/stream_file"
require "thin/middlewares/streaming"

class RackApp
def call(env)
body = env.inspect
Expand All @@ -17,15 +12,15 @@ class RackApp
end

map '/threaded' do
use Thin::Middlewares::Threaded, 20
use Thin::Threaded
end

map '/man' do
use Thin::Middlewares::StreamFile
use Thin::StreamFile
run Rack::File.new("site/public/man")
end

use Thin::Middlewares::Streaming
use Thin::Streamed
use Rack::CommonLogger

run RackApp.new
26 changes: 23 additions & 3 deletions lib/thin.rb
@@ -1,3 +1,23 @@
require "thin/version"
require "thin/system"
require "thin/server"
module Thin
module Backends
autoload :Prefork, "thin/backends/prefork"
autoload :SingleProcess, "thin/backends/single_process"
end

autoload :Async, "thin/async"
autoload :CatchAsync, "thin/catch_async"
autoload :Chunked, "thin/chunked"
autoload :Configurator, "thin/configurator"
autoload :Connection, "thin/connection"
autoload :FastEnumerator, "thin/fast_enumerator"
autoload :Listener, "thin/listener"
autoload :Request, "thin/request"
autoload :Response, "thin/response"
autoload :Runner, "thin/runner"
autoload :Server, "thin/server"
autoload :StreamFile, "thin/stream_file"
autoload :Streamed, "thin/streamed"
autoload :System, "thin/system"
autoload :Threaded, "thin/threaded"
autoload :Version, "thin/version"
end
42 changes: 42 additions & 0 deletions lib/thin/async.rb
@@ -1,4 +1,46 @@
module Thin
class Async
class Callback
def initialize(method, env)
@method = method
@env = env
end

def call(response)
@method.call(response, @env)
end
end

def initialize(app, &builder)
@app = app
@builder = Rack::Builder.new(&builder)
end

def call(env)
# Connection may be closed unless the App#call response was a [-1, ...]
# It should be noted that connection objects will linger until this
# callback is no longer referenced, so be tidy!
env['async.callback'] = Callback.new(method(:async_call), env)

@app.call(env)
end

def async_call(response, env)
# TODO refactor this to prevent creating a proc on each call
@builder.run(proc { |env| response })
status, headers, body = *@builder.call(env)

connection = env['thin.connection']
reset = connection.method(:reset)
headers['X-Thin-Deferred'] = 'yes'

body.callback(&reset) if body.respond_to?(:callback)
body.errback(&reset) if body.respond_to?(:errback)

connection.send_response [status, headers, body]
end
end

unless defined?(DeferrableBody)
# Based on version from James Tucker <raggi@rubyforge.org>
class DeferrableBody
Expand Down
File renamed without changes.
59 changes: 59 additions & 0 deletions lib/thin/chunked.rb
@@ -0,0 +1,59 @@
module Thin
# Like Rack::Chunk but support async responses
# Make sure it is mounted before any middleware that wraps the body
class Chunked
include Rack::Utils

# Same as Rack::Chunked::Body, but doesn't send the tail automaticaly.
class Body < SimpleDelegator
TERM = "\r\n"
TAIL = "0#{TERM}#{TERM}"

def initialize(body)
super
@body = body
end

def each
term = TERM
@body.each do |chunk|
size = Rack::Utils.bytesize(chunk)
next if size == 0

chunk = chunk.dup.force_encoding(Encoding::BINARY) if chunk.respond_to?(:force_encoding)
yield [size.to_s(16), term, chunk, term].join
end

if @body.respond_to?(:callback)
@body.callback { yield TAIL }
else
yield TAIL
end
end

def close
@body.close if @body.respond_to?(:close)
end
end

def initialize(app)
@app = app
end

def call(env)
status, headers, body = @app.call(env)
headers = HeaderHash.new(headers)

if env['HTTP_VERSION'] == 'HTTP/1.0' ||
STATUS_WITH_NO_ENTITY_BODY.include?(status) ||
headers['Content-Length'] ||
headers['Transfer-Encoding']
[status, headers, body]
else
headers.delete('Content-Length')
headers['Transfer-Encoding'] = 'chunked'
[status, headers, Body.new(body)]
end
end
end
end
3 changes: 0 additions & 3 deletions lib/thin/connection.rb
@@ -1,9 +1,6 @@
require "rack"
require "http/parser"

require "thin/request"
require "thin/response"

module Thin
# EventMachine connection.
# Supports:
Expand Down
45 changes: 0 additions & 45 deletions lib/thin/middlewares/async.rb

This file was deleted.

61 changes: 0 additions & 61 deletions lib/thin/middlewares/chunked.rb

This file was deleted.

50 changes: 0 additions & 50 deletions lib/thin/middlewares/stream_file.rb

This file was deleted.

31 changes: 0 additions & 31 deletions lib/thin/middlewares/streaming.rb

This file was deleted.

0 comments on commit daa7c56

Please sign in to comment.