Finished routing, simple website now works yay

This commit is contained in:
Hannah-Dagemark 2024-04-18 16:01:30 +02:00
commit 7a112697ec
11 changed files with 148 additions and 15 deletions

View file

@ -1,3 +1,4 @@
source 'https://rubygems.org'
gem 'minitest'
gem 'minitest'
gem 'yard'

View file

@ -2,12 +2,14 @@ GEM
remote: https://rubygems.org/
specs:
minitest (5.20.0)
yard (0.9.34)
PLATFORMS
x64-mingw-ucrt
DEPENDENCIES
minitest
yard
BUNDLED WITH
2.4.19

4
doc.txt Normal file
View file

@ -0,0 +1,4 @@
Regexp
(w+)
rubular

View file

@ -2,14 +2,13 @@ class Request
attr_reader :method, :resource, :version, :headers, :params
def initialize(input)
@input = input
variable_definer(@input)
variable_definer(input)
end
private
def variable_definer(input) ##Defines required class variables by splitting up the input text
rows = input.split(/\n/)
rows = input.split(/\r\n/)
@method, @resource, @version = rows[0].split(' ')
@headers, @params = {}, {}
if rows.find_index("") != nil then

View file

@ -1,20 +1,34 @@
class Route
require 'debug'
class Router
attr_reader :routes
def initialize()
@routes = {}
@routes = []
end
def add_route(input)
@routes.merge!(input)
def add_route(method, input, &block)
routeHash = {method: method, resource: input, block: block}
@routes << routeHash
p "added successfully, @routes are: #{@routes}"
end
def match_route(input)
resource = input.resource
if @routes[resource]
return @routes[resource]
def match_route(request)
#spec ["3" "bostongurka"]
match = @routes.find {|route| (route[:method] == request.method) && (request.resource.match?(route[:resource]))}
if match != nil
print("\nMatch: ", match, "\n")
return 200
else
p "failed to find: #{resource} route"
if request.resource.match?(@routes[0][:resource]) != true
p "failed to find: #{request.resource} resource.\nExpected: #{String(@routes[0][:resource])} resource"
elsif @routes[0][:method] != request.method
p "failed to find #{request.method} method.\nExpected: #{@routes[0][:method]} method"
else
p "Failed to match, got no reason, just kinda didn't feel like it"
end
return 404
end
end

57
lib/tcp_server.rb Normal file
View file

@ -0,0 +1,57 @@
require 'socket'
require_relative 'request'
require_relative 'route'
class HTTPServer
def initialize(port)
@port = port
end
def start
server = TCPServer.new(@port)
puts "Listening on #{@port}"
router = Router.new
router.add_route("GET",/\/grillkorv\/\d/) do |id, senap|
puts "woot #{senap}"
end
router.add_route("GET","/favicon.ico")
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)
if routeReturn == 200
html = "<h1>Hello, World!</h1>"
status = 200
elsif routeReturn == 404
html = "<h1>Not found!</h1>"
status = 404
end
#Sen kolla om resursen (filen finns)
# Nedanstående bör göras i er Response-klass
session.print "HTTP/1.1 #{status}\r\n"
session.print "Content-Type: text/html\r\n"
session.print "\r\n"
session.print html
session.close
end
end
end

4
main.rb Normal file
View file

@ -0,0 +1,4 @@
require_relative 'lib/tcp_server'
server = HTTPServer.new(4567)
server.start

View file

@ -2,5 +2,4 @@ GET /examples HTTP/1.1
Host: example.com
User-Agent: ExampleBrowser/1.0
Accept-Encoding: gzip, deflate
Accept: */*
Accept: */*

View file

@ -0,0 +1,5 @@
GET /examples/bananas HTTP/1.1
Host: example.com
User-Agent: ExampleBrowser/1.0
Accept-Encoding: gzip, deflate
Accept: */*

42
spec/http_router_spec.rb Normal file
View file

@ -0,0 +1,42 @@
require_relative 'spec_helper'
require_relative '../lib/route'
describe 'Route' do
describe 'Simple get-route' do
it 'matches a simple route' do
@route = Route.new()
@route.add_route({"/examples" => "puts grillkorv"})
request_string = File.read('spec/example_requests/get-examples.request.txt')
request = Request.new(request_string)
_(@route.match_route(request)).must_equal "puts grillkorv"
end
it 'matches a double route' do
@route = Route.new()
@route.add_route({"/examples/banan" => "does a backflip"})
request_string = File.read('spec/example_requests/get-subexamples.request.txt')
request = Request.new(request_string)
_(@route.match_route(request)).must_equal "does a backflip"
end
it 'matches a variable route' do
@route = Route.new()
@route.add_route({"/examples/:id" => "puts id"})
request_string = File.read('spec/example_requests/get-subexamples.request.txt')
request = Request.new(request_string)
_(@route.match_route(request)).must_equal "puts id"
end
it 'does not match' do
@route = Route.new()
@route.add_route({"/grillkorv" => "puts grillkorv"})
request_string = File.read('spec/example_requests/get-examples.request.txt')
request = Request.new(request_string)
_(@route.match_route(request)).must_equal 404
end
end
end

6
test.rb Normal file
View file

@ -0,0 +1,6 @@
route = {"hello/:hi" => "goodbye"}
p route["hello:hi"]
if route.keys[0].include?(":")
route = {route.keys[0].split(":")[0].concat("") => route.values[0]}
end
p route