class Supersimplehttp::Server

Public Class Methods

new(addr, port, &block) click to toggle source
# File lib/supersimplehttp.rb, line 9
def initialize(addr, port, &block)
  @server = TCPServer.new(addr, port)
  if block
    puts block.call()
  end
  run
end

Public Instance Methods

run() click to toggle source
# File lib/supersimplehttp.rb, line 17
def run
  loop do
    Thread.start(@server.accept) do |client|
      request = client.readpartial(2048)
      
      request = RequestParser.new(request).parse
      response = ResponsePreparer.new.prepare_response(request)
      puts "[#{Time.now.strftime("%H:%M:%S")}] #{client.peeraddr[3]} #{request.fetch(:path)} - #{response.code}"
      # Pretty print request
      # STDERR.puts PP.pp(request, $>, 40)
      response.send(client)
      client.close 
    end
  end
end