lib/webmachine/adapters/reel.rb in webmachine-1.2.2 vs lib/webmachine/adapters/reel.rb in webmachine-1.3.0

- old
+ new

@@ -1,42 +1,43 @@ +require 'webmachine/adapter' +require 'webmachine/constants' +require 'set' require 'reel' -require 'webmachine/version' require 'webmachine/headers' require 'webmachine/request' require 'webmachine/response' -require 'webmachine/dispatcher' -require 'set' module Webmachine module Adapters class Reel < Adapter # Used to override default Reel server options (useful in testing) DEFAULT_OPTIONS = {} - + def run @options = DEFAULT_OPTIONS.merge({ - :port => configuration.port, - :host => configuration.ip - }).merge(configuration.adapter_options) + :port => application.configuration.port, + :host => application.configuration.ip + }).merge(application.configuration.adapter_options) - if extra_verbs = configuration.adapter_options[:extra_verbs] + if extra_verbs = application.configuration.adapter_options[:extra_verbs] @extra_verbs = Set.new(extra_verbs.map(&:to_s).map(&:upcase)) else @extra_verbs = Set.new end - @server = ::Reel::Server.supervise(@options[:host], @options[:port], &method(:process)) + if @options[:ssl] + unless @options[:ssl][:cert] && @options[:ssl][:key] + raise ArgumentError, 'Certificate or Private key missing for HTTPS Server' + end + @server = ::Reel::Server::HTTPS.supervise(@options[:host], @options[:port], @options[:ssl], &method(:process)) + else + @server = ::Reel::Server::HTTP.supervise(@options[:host], @options[:port], &method(:process)) + end - # FIXME: this will no longer work on Ruby 2.0. We need Celluloid.trap - trap("INT") { @server.terminate; exit 0 } Celluloid::Actor.join(@server) end - def shutdown - @server.terminate! if @server - end - def process(connection) connection.each_request do |request| # Users of the adapter can configure a custom WebSocket handler if request.websocket? if handler = @options[:websocket_handler] @@ -52,22 +53,23 @@ # Optional support for e.g. WebDAV verbs not included in Webmachine's # state machine. Do the "Railsy" thing and handle them like POSTs # with a magical parameter if @extra_verbs.include?(request.method) - method = "POST" + method = POST_METHOD param = "_method=#{request.method}" uri = request_uri(request.url, request.headers, param) else method = request.method uri = request_uri(request.url, request.headers) end wm_headers = Webmachine::Headers[request.headers.dup] wm_request = Webmachine::Request.new(method, uri, wm_headers, request.body) + wm_response = Webmachine::Response.new - @dispatcher.dispatch(wm_request, wm_response) + application.dispatcher.dispatch(wm_request, wm_response) fixup_headers(wm_response) fixup_callable_encoder(wm_response) request.respond ::Reel::Response.new(wm_response.code, @@ -75,16 +77,12 @@ wm_response.body) end end def request_uri(path, headers, extra_query_params = nil) - host_parts = headers.fetch('Host').split(':') path_parts = path.split('?') - - uri_hash = {host: host_parts.first, path: path_parts.first} - - uri_hash[:port] = host_parts.last.to_i if host_parts.length == 2 - uri_hash[:query] = path_parts.last if path_parts.length == 2 + uri_hash = {path: path_parts.first} + uri_hash[:query] = path_parts.last if path_parts.length == 2 if extra_query_params if uri_hash[:query] uri_hash[:query] << "&#{extra_query_params}" else