Skip to content
This repository has been archived by the owner on Dec 7, 2018. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tarcieri committed Sep 6, 2013
0 parents commit ea42b41
Show file tree
Hide file tree
Showing 16 changed files with 343 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
*.gem
*.rbc
.bundle
.config
.yardoc
Gemfile.lock
InstalledFiles
_yardoc
coverage
doc/
lib/bundler/man
pkg
rdoc
spec/reports
test/tmp
test/version_tmp
tmp
4 changes: 4 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
--color
--format documentation
--backtrace
--default_path spec
15 changes: 15 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
rvm:
- 1.9.3
- 2.0.0
- ruby-head
- jruby-19mode
- jruby-head
- rbx-19mode

matrix:
allow_failures:
- rvm: ruby-head
- rvm: jruby-head

notifications:
irc: "irc.freenode.org#celluloid"
12 changes: 12 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
source 'https://rubygems.org'

gem 'celluloid', github: 'celluloid/celluloid', branch: 'master'
gem 'celluloid-io', github: 'celluloid/celluloid-io', branch: 'master'

gem 'reel', github: 'celluloid/reel'
gem 'http', github: 'tarcieri/http'

gem 'coveralls', require: false

# Specify your gem's dependencies in reel-app.gemspec
gemspec
22 changes: 22 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright (c) 2013 Tony Arcieri

MIT License

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Reel::Rack
==========

A Rack adapter for [Reel][reel], the [Celluloid::IO][celluloidio] web server.

[reel]: https://github.com/celluloid/reel
[celluloidio]: https://github.com/celluloid/celluloid-io

## Installation

Add this line to your application's Gemfile:

gem 'reel-rack'

And then execute:

$ bundle

Or install it yourself as:

$ gem install reel-rack

## Usage

TODO: Write usage instructions here
4 changes: 4 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require "bundler/gem_tasks"
Dir[File.expand_path("../tasks/**/*.rake", __FILE__)].each { |task| load task }

task :default => :spec
41 changes: 41 additions & 0 deletions lib/rack/handler/reel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'reel/rack/server'

module Rack
module Handler
class Reel
DEFAULT_OPTIONS = {
:host => "0.0.0.0",
:port => 3000,
:quiet => false
}

def self.run(app, options = {})
options = DEFAULT_OPTIONS.merge(options)

unless options[:quiet]
app = Rack::CommonLogger.new(app, STDOUT)
end

if options[:environment]
ENV['RACK_ENV'] = options[:environment].to_s
end

Celluloid.logger.info "A Reel good HTTP server! (Codename #{Reel::CODENAME})"
Celluloid.logger.info "Listening on #{options[:host]}:#{options[:port]}"

supervisor = ::Reel::Rack::Server.supervise_as(:reel_rack_server, app, options)

begin
sleep
rescue Interrupt
Celluloid.logger.info "Interrupt received... shutting down"
supervisor.terminate
Celluloid.join(supervisor)
Celluloid.logger.info "That's all, folks!"
end
end
end

register :reel, Reel
end
end
2 changes: 2 additions & 0 deletions lib/reel/rack.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require 'reel/rack/version'
require 'rack/handler/reel'
49 changes: 49 additions & 0 deletions lib/reel/rack/server.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@

# Adapted from code orinially Copyright (c) 2013 Jonathan Stott

require 'reel'
require 'rack'

module Reel
module Rack
class Server < Server
attr_reader :app
def initialize(app, options)
raise ArgumentError, "no host given" unless options[:host]
raise ArgumentError, "no port given" unless options[:port]

super(options[:host], options[:port], &method(:on_connection))
@app = app
end

def on_connection(connection)
connection.each_request do |request|
if request.websocket?
request.respond :bad_request, "WebSockets not supported"
else
route_request request
end
end
end

def route_request(request)
options = {
:method => request.method,
:input => request.body.to_s,
"REMOTE_ADDR" => request.remote_addr
}.merge(convert_headers(request.headers))

status, headers, body = app.call ::Rack::MockRequest.env_for(request.url, options)
request.respond status_symbol(status), headers, body
end

def convert_headers(headers)
Hash[headers.map { |key, value| ['HTTP_' + key.upcase.gsub('-','_'),value ] }]
end

def status_symbol(status)
status.is_a?(Fixnum) ? Http::Response::STATUS_CODES[status].downcase.gsub(/\s|-/, '_').to_sym : status.to_sym
end
end
end
end
5 changes: 5 additions & 0 deletions lib/reel/rack/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Reel
module Rack
VERSION = "0.0.1"
end
end
1 change: 1 addition & 0 deletions log/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.log
26 changes: 26 additions & 0 deletions reel-rack.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'reel/rack/version'

Gem::Specification.new do |spec|
spec.name = "reel-rack"
spec.version = Reel::Rack::VERSION
spec.authors = ["Tony Arcieri", "Jonathan Stott"]
spec.email = ["[email protected]"]
spec.description = "Rack adapter for Reel"
spec.summary = "Rack adapter for Reel, a Celluloid::IO web server"
spec.homepage = "https://github.com/celluloid/reel-rack"
spec.license = "MIT"

spec.files = `git ls-files`.split($/)
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]

spec.add_runtime_dependency "reel", ">= 0.4.0.pre2"

spec.add_development_dependency "bundler", "~> 1.3"
spec.add_development_dependency "rake"
spec.add_development_dependency "rspec"
end
22 changes: 22 additions & 0 deletions spec/reel/rack/server_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'reel/rack/server'
require 'net/http'

describe Reel::Rack::Server do
let(:host) { "127.0.0.1" }
let(:port) { 30000 }
let(:body) { "hello world" }

subject do
app = proc { [200, {"Content-Type" => "text/plain"}, body] }
described_class.new(app, :host => host, :port => port)
end

it "runs a basic Hello World app" do
# Hax to wait for server to be started
subject.inspect

expect(Net::HTTP.get(URI("http://#{host}:#{port}"))).to eq body

subject.terminate
end
end
91 changes: 91 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
require 'bundler/setup'
require 'reel/app'

logfile = File.open(File.expand_path("../../log/test.log", __FILE__), 'a')
Celluloid.logger = Logger.new(logfile)

def example_addr; '127.0.0.1'; end
def example_port; 1234; end
def example_path; "/example"; end
def example_url; "http://#{example_addr}:#{example_port}#{example_path}"; end

def with_reel(handler)
server = Reel::Server.new(example_addr, example_port, &handler)
yield server
ensure
server.terminate if server && server.alive?
end

def with_socket_pair
host = '127.0.0.1'
port = 10101

server = TCPServer.new(host, port)
client = TCPSocket.new(host, port)
peer = server.accept

begin
connection = Reel::Connection.new(peer)
yield client, connection
ensure
server.close rescue nil
client.close rescue nil
peer.close rescue nil
end
end

class ExampleRequest
extend Forwardable
def_delegators :@headers, :[], :[]=
attr_accessor :method, :path, :version, :body

def initialize(method = :get, path = "/", version = "1.1", headers = {}, body = nil)
@method = method.to_s.upcase
@path = path
@version = "1.1"
@headers = {
'Host' => 'www.example.com',
'Connection' => 'keep-alive',
'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.78 S',
'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Encoding' => 'gzip,deflate,sdch',
'Accept-Language' => 'en-US,en;q=0.8',
'Accept-Charset' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.3'
}.merge(headers)

@body = nil
end

def to_s
if @body && !@headers['Content-Length']
@headers['Content-Length'] = @body.length
end

"#{@method} #{@path} HTTP/#{@version}\r\n" <<
@headers.map { |k, v| "#{k}: #{v}" }.join("\r\n") << "\r\n\r\n" <<
(@body ? @body : '')
end
end

module WebSocketHelpers
def self.included(spec)
spec.instance_eval do
let(:example_host) { "www.example.com" }
let(:example_path) { "/example"}
let(:example_url) { "ws://#{example_host}#{example_path}" }
let :handshake_headers do
{
"Host" => example_host,
"Upgrade" => "websocket",
"Connection" => "Upgrade",
"Sec-WebSocket-Key" => "dGhlIHNhbXBsZSBub25jZQ==",
"Origin" => "http://example.com",
"Sec-WebSocket-Protocol" => "chat, superchat",
"Sec-WebSocket-Version" => "13"
}
end

let(:handshake) { WebSocket::ClientHandshake.new(:get, example_url, handshake_headers) }
end
end
end
7 changes: 7 additions & 0 deletions tasks/rspec.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new

RSpec::Core::RakeTask.new(:rcov) do |task|
task.rcov = true
end

0 comments on commit ea42b41

Please sign in to comment.