Class: PuppetTwitch::BasicHttpServer
- Inherits:
-
Object
- Object
- PuppetTwitch::BasicHttpServer
- Defined in:
- lib/puppet_twitch/basic_http_server.rb
Instance Attribute Summary collapse
-
#logger ⇒ Object
Returns the value of attribute logger.
Instance Method Summary collapse
- #endpoint(endpoint, &block) ⇒ Object
- #form_response(status, body) ⇒ Object
- #get_endpoint_and_params(request) ⇒ Object
-
#initialize(bind, port, multiple_threads = true) ⇒ BasicHttpServer
constructor
A new instance of BasicHttpServer.
- #parse_params(param_string) ⇒ Object
- #process_request(socket) ⇒ Object
- #start ⇒ Object
Constructor Details
#initialize(bind, port, multiple_threads = true) ⇒ BasicHttpServer
Returns a new instance of BasicHttpServer.
11 12 13 14 15 16 17 18 |
# File 'lib/puppet_twitch/basic_http_server.rb', line 11 def initialize(bind, port, multiple_threads = true) @bind = bind @port = port @multiple_threads = multiple_threads @logger = PuppetTwitch::Logger.new() @logger.level = PuppetTwitch::Logger::DEBUG @actions = {} end |
Instance Attribute Details
#logger ⇒ Object
Returns the value of attribute logger.
9 10 11 |
# File 'lib/puppet_twitch/basic_http_server.rb', line 9 def logger @logger end |
Instance Method Details
#endpoint(endpoint, &block) ⇒ Object
60 61 62 |
# File 'lib/puppet_twitch/basic_http_server.rb', line 60 def endpoint(endpoint, &block) @actions[endpoint] = block end |
#form_response(status, body) ⇒ Object
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/puppet_twitch/basic_http_server.rb', line 73 def form_response(status, body) <<-EOF HTTP/1.1 #{status} Content-Type: text/plain Content-Length: #{body.bytesize} Connection: close #{body} EOF end |
#get_endpoint_and_params(request) ⇒ Object
64 65 66 67 |
# File 'lib/puppet_twitch/basic_http_server.rb', line 64 def get_endpoint_and_params(request) match = request.match(/^\s*(?<verb>GET|POST|PUT|DELETE) (?<endpoint>[\w\-\/]*)(\?(?<params>\S*)\s*|\s*)HTTP.*/) return match['endpoint'], parse_params(match['params']) end |
#parse_params(param_string) ⇒ Object
69 70 71 |
# File 'lib/puppet_twitch/basic_http_server.rb', line 69 def parse_params(param_string) PuppetTwitch::ParamParser.parse(param_string.to_s.strip.split('&')) end |
#process_request(socket) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/puppet_twitch/basic_http_server.rb', line 45 def process_request(socket) client_ip = socket.peeraddr[3] client_hostname = socket.peeraddr[2] request = socket.gets endpoint, params = get_endpoint_and_params(request) @logger.info "Connection from: #{client_hostname} (#{client_ip}) | Endpoint: #{endpoint} | Params: #{params}" response = @actions.has_key?(endpoint) ? @actions[endpoint].call(params) : [400, "Unrecognised endpoint: #{endpoint}"] @logger.debug "#{response[0]}: #{response[1]}" socket.puts form_response(response[0], response[1]) @logger.debug "Closing connection from #{client_ip}" end |
#start ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/puppet_twitch/basic_http_server.rb', line 20 def start() @logger.info 'Starting server ...' server = TCPServer.new(@bind, @port) @logger.info "Listening on #{@bind}:#{@port}" server_loop = proc { |socket| begin process_request socket rescue => e @logger.error e.to_s socket.puts form_response(500, 'Unexpected error') ensure socket.close end } loop do if @multiple_threads Thread.fork(server.accept, &server_loop) else server_loop.call server.accept end end end |