summaryrefslogtreecommitdiff
path: root/lib/nickserver/em_server.rb
blob: bcec4cd93963429dde4097356226c2275e43d058 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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 send_response(options = {})
      response = EM::DelegatedHttpResponse.new(self)
      response.status = options[:status]
      response.content_type options[:content_type]
      response.content = options[: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