From ea42b410d0664f81d59e0bf8c556ec7d122d87c7 Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Tue, 25 Jun 2013 01:05:05 -0700 Subject: [PATCH] Initial commit --- .gitignore | 17 +++++++ .rspec | 4 ++ .travis.yml | 15 ++++++ Gemfile | 12 +++++ LICENSE.txt | 22 +++++++++ README.md | 25 ++++++++++ Rakefile | 4 ++ lib/rack/handler/reel.rb | 41 ++++++++++++++++ lib/reel/rack.rb | 2 + lib/reel/rack/server.rb | 49 +++++++++++++++++++ lib/reel/rack/version.rb | 5 ++ log/.gitignore | 1 + reel-rack.gemspec | 26 ++++++++++ spec/reel/rack/server_spec.rb | 22 +++++++++ spec/spec_helper.rb | 91 +++++++++++++++++++++++++++++++++++ tasks/rspec.rake | 7 +++ 16 files changed, 343 insertions(+) create mode 100644 .gitignore create mode 100644 .rspec create mode 100644 .travis.yml create mode 100644 Gemfile create mode 100644 LICENSE.txt create mode 100644 README.md create mode 100644 Rakefile create mode 100644 lib/rack/handler/reel.rb create mode 100644 lib/reel/rack.rb create mode 100644 lib/reel/rack/server.rb create mode 100644 lib/reel/rack/version.rb create mode 100644 log/.gitignore create mode 100644 reel-rack.gemspec create mode 100644 spec/reel/rack/server_spec.rb create mode 100644 spec/spec_helper.rb create mode 100644 tasks/rspec.rake diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d87d4be --- /dev/null +++ b/.gitignore @@ -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 diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..270de17 --- /dev/null +++ b/.rspec @@ -0,0 +1,4 @@ +--color +--format documentation +--backtrace +--default_path spec diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..d17ccfd --- /dev/null +++ b/.travis.yml @@ -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" diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..382a862 --- /dev/null +++ b/Gemfile @@ -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 diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..f41d519 --- /dev/null +++ b/LICENSE.txt @@ -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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..642edc6 --- /dev/null +++ b/README.md @@ -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 diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..dbbf624 --- /dev/null +++ b/Rakefile @@ -0,0 +1,4 @@ +require "bundler/gem_tasks" +Dir[File.expand_path("../tasks/**/*.rake", __FILE__)].each { |task| load task } + +task :default => :spec diff --git a/lib/rack/handler/reel.rb b/lib/rack/handler/reel.rb new file mode 100644 index 0000000..c11ab71 --- /dev/null +++ b/lib/rack/handler/reel.rb @@ -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 diff --git a/lib/reel/rack.rb b/lib/reel/rack.rb new file mode 100644 index 0000000..e6e14f6 --- /dev/null +++ b/lib/reel/rack.rb @@ -0,0 +1,2 @@ +require 'reel/rack/version' +require 'rack/handler/reel' diff --git a/lib/reel/rack/server.rb b/lib/reel/rack/server.rb new file mode 100644 index 0000000..d459406 --- /dev/null +++ b/lib/reel/rack/server.rb @@ -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 \ No newline at end of file diff --git a/lib/reel/rack/version.rb b/lib/reel/rack/version.rb new file mode 100644 index 0000000..2ad8546 --- /dev/null +++ b/lib/reel/rack/version.rb @@ -0,0 +1,5 @@ +module Reel + module Rack + VERSION = "0.0.1" + end +end diff --git a/log/.gitignore b/log/.gitignore new file mode 100644 index 0000000..397b4a7 --- /dev/null +++ b/log/.gitignore @@ -0,0 +1 @@ +*.log diff --git a/reel-rack.gemspec b/reel-rack.gemspec new file mode 100644 index 0000000..3852dce --- /dev/null +++ b/reel-rack.gemspec @@ -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 = ["tony.arcieri@gmail.com"] + 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 diff --git a/spec/reel/rack/server_spec.rb b/spec/reel/rack/server_spec.rb new file mode 100644 index 0000000..e9587dd --- /dev/null +++ b/spec/reel/rack/server_spec.rb @@ -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 \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..8e374a2 --- /dev/null +++ b/spec/spec_helper.rb @@ -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 diff --git a/tasks/rspec.rake b/tasks/rspec.rake new file mode 100644 index 0000000..bd2a9d8 --- /dev/null +++ b/tasks/rspec.rake @@ -0,0 +1,7 @@ +require 'rspec/core/rake_task' + +RSpec::Core::RakeTask.new + +RSpec::Core::RakeTask.new(:rcov) do |task| + task.rcov = true +end