teehee :3
This commit is contained in:
parent
2ce8a9c28b
commit
0f893c1d83
27 changed files with 3905 additions and 68 deletions
|
|
@ -1,14 +1,21 @@
|
|||
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}"
|
||||
|
|
@ -27,63 +34,9 @@ class HTTPServer
|
|||
pp request
|
||||
|
||||
routeReturn = @router.match_route(request)
|
||||
# pil nedåt ska in i response-klassen
|
||||
response = Response.new(routeReturn, session)
|
||||
response.print()
|
||||
|
||||
outputContent = {}
|
||||
if routeReturn[1] == 200
|
||||
if routeReturn[0][:block] != nil
|
||||
outputContent[:info] = routeReturn[0][:block].call
|
||||
print("Recieved HTML, enabling as called:\n #{outputContent[:info]}")
|
||||
outputContent[:type] = "text/html"
|
||||
status = 200
|
||||
else
|
||||
outputContent[:info] = nil
|
||||
status = 200
|
||||
end
|
||||
elsif routeReturn[1] == 404
|
||||
print("HTML route not found, searching for target file...\n")
|
||||
if File.file?("./public/#{routeReturn[0].resource}")
|
||||
print("Found file: ./public/#{routeReturn[0].resource}\n")
|
||||
outputContent[:info] = File.open("./public#{routeReturn[0].resource}", "rb")
|
||||
if outputContent[:type].to_s.split("/")[0] == "text"
|
||||
print("Successfully opened file \"./public#{routeReturn[0].resource}\" with contents:\n#{outputContent[:info].read}\n ")
|
||||
end
|
||||
outputContent[:type] = getMime(File.extname("./public/#{routeReturn[0].resource}").to_str)
|
||||
status = 200
|
||||
else
|
||||
print("Could not find file: \"./public/#{routeReturn[0].resource}\"\n")
|
||||
status = 404
|
||||
end
|
||||
end
|
||||
|
||||
# Nedanstående bör göras i er Response-klass
|
||||
|
||||
print("\nStatus: #{status.to_s}\n")
|
||||
session.print "HTTP/1.1 #{status}\r\n"
|
||||
if status != 404
|
||||
session.print "Content-Type: #{outputContent[:type]}\r\n"
|
||||
print("\nReading content type: #{outputContent[:type]}\n")
|
||||
print("\nLoaded is of type: #{outputContent[:info].class}\n")
|
||||
print("\nSize reading: #{File.size(outputContent[:info])}\n")
|
||||
session.print "Content-Length: #{File.size(outputContent[:info])}\r\n"
|
||||
session.print "\r\n"
|
||||
printContent = outputContent[:info].read
|
||||
if printContent; session.print printContent end
|
||||
end
|
||||
session.close
|
||||
end
|
||||
end
|
||||
|
||||
def getMime(extension)
|
||||
mimes = { ".css" => "text/css", ".html" => "text/html", ".ico" => "image/vnd.microsoft.icon", ".jpg" => "image/jpeg", ".png" => "image/png"}
|
||||
if mimes[extension] != nil
|
||||
print("Found #{mimes[extension]} result for extension #{extension}\n")
|
||||
return mimes[extension]
|
||||
else
|
||||
print("Found no result for extension #{extension}\n")
|
||||
return nil
|
||||
response = Response.new(routeReturn)
|
||||
response.print(session)
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue