require 'socket' require_relative 'request' require_relative 'route' require_relative 'response' # Main class for managing and talking between sub-classes during request-response cycles. Utalizes the request, route and response classes. class HTTPServer # Initializes the HTTPServer object # # @param port [Int] An integer representing the port number for the http server # @param router [Router] The Router class holding the expected routes for the server def initialize(port, router) @port = port @router = router end # Starts the TCP server and begins reading for requests on the designated port. When a request comes in it turns it into a request object using the request class, and sends that to the router. It then sends the matched information to the response class. def start server = TCPServer.new(@port) puts "Listening on #{@port}" while session = server.accept data = "" while line = session.gets and line !~ /^\s*$/ data += line end puts "RECEIVED REQUEST" puts "-" * 40 puts data puts "-" * 40 request = Request.new(data) pp request routeReturn = @router.match_route(request) response = Response.new(routeReturn) response.print(session) end end end