Second-To-Last-Fixadoo
This commit is contained in:
parent
d48cfbcf11
commit
2ce8a9c28b
6 changed files with 62 additions and 29 deletions
0
lib/response.rb
Normal file
0
lib/response.rb
Normal file
|
|
@ -4,30 +4,14 @@ require_relative 'route'
|
||||||
|
|
||||||
class HTTPServer
|
class HTTPServer
|
||||||
|
|
||||||
def initialize(port)
|
def initialize(port, router)
|
||||||
@port = port
|
@port = port
|
||||||
|
@router = router
|
||||||
end
|
end
|
||||||
|
|
||||||
def start
|
def start
|
||||||
server = TCPServer.new(@port)
|
server = TCPServer.new(@port)
|
||||||
puts "Listening on #{@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
|
while session = server.accept
|
||||||
data = ""
|
data = ""
|
||||||
|
|
@ -42,7 +26,11 @@ class HTTPServer
|
||||||
request = Request.new(data)
|
request = Request.new(data)
|
||||||
pp request
|
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 = {}
|
outputContent = {}
|
||||||
if routeReturn[1] == 200
|
if routeReturn[1] == 200
|
||||||
if routeReturn[0][:block] != nil
|
if routeReturn[0][:block] != nil
|
||||||
|
|
@ -57,9 +45,12 @@ class HTTPServer
|
||||||
elsif routeReturn[1] == 404
|
elsif routeReturn[1] == 404
|
||||||
print("HTML route not found, searching for target file...\n")
|
print("HTML route not found, searching for target file...\n")
|
||||||
if File.file?("./public/#{routeReturn[0].resource}")
|
if File.file?("./public/#{routeReturn[0].resource}")
|
||||||
outputContent[:info] = File.read("./public#{routeReturn[0].resource}")
|
print("Found file: ./public/#{routeReturn[0].resource}\n")
|
||||||
print("Successfully opened file \"./public#{routeReturn[0].resource}\" with contents:\n#{outputContent[:info]}\n ")
|
outputContent[:info] = File.open("./public#{routeReturn[0].resource}", "rb")
|
||||||
outputContent[:type] = "text/css"
|
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
|
status = 200
|
||||||
else
|
else
|
||||||
print("Could not find file: \"./public/#{routeReturn[0].resource}\"\n")
|
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
|
# 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 "HTTP/1.1 #{status}\r\n"
|
||||||
session.print "Content-Type: #{outputContent[:type]}\r\n"
|
if status != 404
|
||||||
print("Reading content type: #{outputContent[:type]}\n")
|
session.print "Content-Type: #{outputContent[:type]}\r\n"
|
||||||
session.print "\r\n"
|
print("\nReading content type: #{outputContent[:type]}\n")
|
||||||
if outputContent[:info]; session.print outputContent[:info] end
|
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
|
session.close
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
24
main.rb
24
main.rb
|
|
@ -1,4 +1,26 @@
|
||||||
require_relative 'lib/tcp_server'
|
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
|
server.start
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
li {
|
li {
|
||||||
color: green;
|
color: whitesmoke;
|
||||||
background-color: blueviolet;
|
background-color: slategray;
|
||||||
}
|
}
|
||||||
|
|
@ -13,5 +13,7 @@
|
||||||
<li><h1>WEBSITE YOU HAVE EVER SEEEN</h1></li>
|
<li><h1>WEBSITE YOU HAVE EVER SEEEN</h1></li>
|
||||||
<ol><h3>Cuz we got hotdogs :D</h3></ol>
|
<ol><h3>Cuz we got hotdogs :D</h3></ol>
|
||||||
</ol>
|
</ol>
|
||||||
|
<h2>Also here's my cat:</h2>
|
||||||
|
<img src="/image/cat.jpg" alt="No Cat Found :C">
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
BIN
public/image/cat.jpg
Normal file
BIN
public/image/cat.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 325 KiB |
Loading…
Add table
Add a link
Reference in a new issue