summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAzul <azul@riseup.net>2016-06-30 11:23:45 +0200
committerAzul <azul@riseup.net>2016-06-30 12:04:07 +0200
commit0491e79c4e5f16d38cf87e53290394e1eccfa2e9 (patch)
tree56d0aff1fc8ca1bbd11b09bc6929f00132467898 /lib
parent4132d6e6db3755334ace352165991487802c6b82 (diff)
Trying to replace EM base server with reel
some tests are still broken. But at least they are running now.
Diffstat (limited to 'lib')
-rw-r--r--lib/nickserver/adapters/em_http.rb24
-rw-r--r--lib/nickserver/em_server.rb51
-rw-r--r--lib/nickserver/hkp/source.rb1
-rw-r--r--lib/nickserver/reel_server.rb24
-rw-r--r--lib/nickserver/request_handler.rb5
-rw-r--r--lib/nickserver/server.rb9
6 files changed, 23 insertions, 91 deletions
diff --git a/lib/nickserver/adapters/em_http.rb b/lib/nickserver/adapters/em_http.rb
deleted file mode 100644
index 16db5ae..0000000
--- a/lib/nickserver/adapters/em_http.rb
+++ /dev/null
@@ -1,24 +0,0 @@
-require 'nickserver/adapters'
-require 'em-http'
-
-module Nickserver::Adapters
- class EmHttp
-
- def initialize
- @timeout = 5
- end
-
- def get(url, options = {})
- get_request(url, options).callback {|http|
- yield http.response_header.status, http.response
- }.errback {|http|
- yield 0, http.error
- }
- end
-
- def get_request(url, options = {})
- @request = EventMachine::HttpRequest.new(url)
- @request.get timeout: @timeout, query: options[:query]
- end
- end
-end
diff --git a/lib/nickserver/em_server.rb b/lib/nickserver/em_server.rb
deleted file mode 100644
index a983249..0000000
--- a/lib/nickserver/em_server.rb
+++ /dev/null
@@ -1,51 +0,0 @@
-require 'eventmachine'
-silence_warnings do
- require 'evma_httpserver'
-end
-require 'nickserver/request_handler'
-
-module Nickserver
- class EmServer < EM::Connection
- include EM::HttpServer
-
- def self.start(options = {})
- EventMachine.run do
- EM.start_server options[:host], options[:port], self
- end
- end
-
- def post_init
- super
- no_environment_strings
- end
-
- def process_http_request
- handler.respond_to params, @http_headers
- end
-
- def respond(status, content)
- response = EM::DelegatedHttpResponse.new(self)
- response.status = status
- response.content_type 'text/plain'
- response.content = content
- silence_warnings do
- response.send_response
- end
- end
-
- private
-
- def handler
- @handler ||= RequestHandler.new(self, Nickserver::Adapters::EmHttp.new)
- end
-
- def params
- if @http_query_string
- CGI.parse(@http_query_string)
- elsif @http_post_content
- CGI.parse(@http_post_content)
- end
- end
-
- end
-end
diff --git a/lib/nickserver/hkp/source.rb b/lib/nickserver/hkp/source.rb
index 8b2a62b..9c79546 100644
--- a/lib/nickserver/hkp/source.rb
+++ b/lib/nickserver/hkp/source.rb
@@ -1,3 +1,4 @@
+require 'nickserver/source'
require 'nickserver/response'
require 'nickserver/hkp/response'
require 'nickserver/hkp/client'
diff --git a/lib/nickserver/reel_server.rb b/lib/nickserver/reel_server.rb
index c12c402..abda57f 100644
--- a/lib/nickserver/reel_server.rb
+++ b/lib/nickserver/reel_server.rb
@@ -4,25 +4,31 @@ require 'nickserver/adapters/celluloid_http'
require 'nickserver/request_handler'
module Nickserver
- class ReelServer
+ class ReelServer < Reel::Server::HTTP
def self.start(options = {})
- Reel::Server::HTTP.run(options[:host], options[:port]) do |connection|
- # Support multiple keep-alive requests per connection
- connection.each_request do |request|
- handler = handler_for(request)
- handler.respond_to params(request), request.headers
- end
+ new(options[:host], options[:port]).run
+ end
+
+ def initialize(host = "127.0.0.1", port = 3000)
+ super(host, port, &method(:on_connection))
+ end
+
+ def on_connection(connection)
+ connection.each_request do |request|
+ handler = handler_for(request)
+ handler.respond_to params(request), request.headers
end
end
+
protected
- def self.handler_for(request)
+ def handler_for(request)
RequestHandler.new(request, Nickserver::Adapters::CelluloidHttp.new)
end
- def self.params(request)
+ def params(request)
if request.query_string
CGI.parse request.query_string
else
diff --git a/lib/nickserver/request_handler.rb b/lib/nickserver/request_handler.rb
index a3fd57d..a54653a 100644
--- a/lib/nickserver/request_handler.rb
+++ b/lib/nickserver/request_handler.rb
@@ -1,3 +1,6 @@
+require 'nickserver/hkp/source'
+require 'nickserver/couch_db/source'
+
module Nickserver
class RequestHandler
@@ -54,7 +57,7 @@ module Nickserver
return uid_domain == Config.domain
else
# no domain configured, use Host header
- host_header = headers.split(/\0/).grep(/^Host: /).first
+ host_header = headers['Host']
if host_header.nil?
send_error("HTTP request must include a Host header.")
else
diff --git a/lib/nickserver/server.rb b/lib/nickserver/server.rb
index 174d6ac..b8f5604 100644
--- a/lib/nickserver/server.rb
+++ b/lib/nickserver/server.rb
@@ -1,9 +1,6 @@
require 'kernel_ext'
require 'json'
-require 'nickserver/em_server'
-require 'nickserver/couch_db/source'
-require 'nickserver/hkp/source'
-require 'nickserver/adapters/em_http'
+require 'nickserver/reel_server'
#
@@ -15,7 +12,7 @@ module Nickserver
class Server
#
- # Starts the Nickserver. Must be run inside an EM.run block.
+ # Starts the Nickserver.
#
# Available options:
#
@@ -33,7 +30,7 @@ module Nickserver
puts "Starting nickserver #{options[:host]}:#{options[:port]}"
end
- Nickserver::EmServer.start(options)
+ Nickserver::ReelServer.start(options)
end