diff --git a/lib/response.rb b/lib/response.rb new file mode 100644 index 0000000..e69de29 diff --git a/lib/tcp_server.rb b/lib/tcp_server.rb index e34267c..fbdad57 100644 --- a/lib/tcp_server.rb +++ b/lib/tcp_server.rb @@ -4,30 +4,14 @@ require_relative 'route' class HTTPServer - def initialize(port) + def initialize(port, router) @port = port + @router = router end def start server = TCPServer.new(@port) puts "Listening on #{@port}" - - router = Router.new - router.add_route("GET",/\/grillkorv\/\d/) do |id, senap| - if File.read("./public/grillkorv.html") != nil - File.read("./public/grillkorv.html") - else - "Html not found" - end - end - router.add_route("GET",/\/grillkorv\/\w.css/) do |id, senap| - if File.read("./public/grillkorv.html") != nil - File.read("./public/grillkorv.html") - else - "Html not found" - end - end - router.add_route("GET","/favicon.ico") while session = server.accept data = "" @@ -42,7 +26,11 @@ class HTTPServer request = Request.new(data) pp request - routeReturn = router.match_route(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 @@ -57,9 +45,12 @@ class HTTPServer elsif routeReturn[1] == 404 print("HTML route not found, searching for target file...\n") if File.file?("./public/#{routeReturn[0].resource}") - outputContent[:info] = File.read("./public#{routeReturn[0].resource}") - print("Successfully opened file \"./public#{routeReturn[0].resource}\" with contents:\n#{outputContent[:info]}\n ") - outputContent[:type] = "text/css" + 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") @@ -69,12 +60,30 @@ class HTTPServer # Nedanstående bör göras i er Response-klass + print("\nStatus: #{status.to_s}\n") session.print "HTTP/1.1 #{status}\r\n" - session.print "Content-Type: #{outputContent[:type]}\r\n" - print("Reading content type: #{outputContent[:type]}\n") - session.print "\r\n" - if outputContent[:info]; session.print outputContent[:info] end + 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 + end + end end \ No newline at end of file diff --git a/main.rb b/main.rb index 90b3183..70dd9e5 100644 --- a/main.rb +++ b/main.rb @@ -1,4 +1,26 @@ require_relative 'lib/tcp_server' -server = HTTPServer.new(4567) +r = Router.new + +r.add_route("GET",/\/grillkorv\/\d/) do |id, senap| + if File.open("./public/grillkorv.html") != nil + File.open("./public/grillkorv.html") + else + "Html not found" + end +end + +# Headache: r.add_route("GET","/grillkorv/:id") do |id, senap| + +r.add_route("GET",/\/grillkorv\/\d/) do |id, senap| + if File.open("./public/grillkorv.html") != nil + File.open("./public/grillkorv.html") + else + "Html not found" + end +end + +#r.add_route("GET","/favicon.ico") + +server = HTTPServer.new(4567, r) server.start \ No newline at end of file diff --git a/public/css/style.css b/public/css/style.css index 95a11ca..e0a602d 100644 --- a/public/css/style.css +++ b/public/css/style.css @@ -1,4 +1,4 @@ li { - color: green; - background-color: blueviolet; + color: whitesmoke; + background-color: slategray; } \ No newline at end of file diff --git a/public/grillkorv.html b/public/grillkorv.html index b19fa95..c4ba6b6 100644 --- a/public/grillkorv.html +++ b/public/grillkorv.html @@ -13,5 +13,7 @@
  • WEBSITE YOU HAVE EVER SEEEN

    1. Cuz we got hotdogs :D

    +

    Also here's my cat:

    + No Cat Found :C \ No newline at end of file diff --git a/public/image/cat.jpg b/public/image/cat.jpg new file mode 100644 index 0000000..7e461a8 Binary files /dev/null and b/public/image/cat.jpg differ