Second-To-Last-Fixadoo

This commit is contained in:
Hannah-Dagemark 2024-05-23 14:47:42 +02:00
commit 2ce8a9c28b
6 changed files with 62 additions and 29 deletions

0
lib/response.rb Normal file
View file

View file

@ -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

24
main.rb
View file

@ -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

View file

@ -1,4 +1,4 @@
li {
color: green;
background-color: blueviolet;
color: whitesmoke;
background-color: slategray;
}

View file

@ -13,5 +13,7 @@
<li><h1>WEBSITE YOU HAVE EVER SEEEN</h1></li>
<ol><h3>Cuz we got hotdogs :D</h3></ol>
</ol>
<h2>Also here's my cat:</h2>
<img src="/image/cat.jpg" alt="No Cat Found :C">
</body>
</html>

BIN
public/image/cat.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 325 KiB