diff --git a/Gemfile b/Gemfile index 939c93f..589e952 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,4 @@ source 'https://rubygems.org' -gem 'minitest' \ No newline at end of file +gem 'minitest' +gem 'yard' \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index 8169cd9..c92dd69 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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 diff --git a/doc.txt b/doc.txt new file mode 100644 index 0000000..8291e7e --- /dev/null +++ b/doc.txt @@ -0,0 +1,4 @@ +Regexp +(w+) + +rubular \ No newline at end of file diff --git a/lib/Request.rb b/lib/Request.rb index 2476327..700bf5c 100644 --- a/lib/Request.rb +++ b/lib/Request.rb @@ -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 diff --git a/lib/route.rb b/lib/route.rb index be5bee4..b2ee72e 100644 --- a/lib/route.rb +++ b/lib/route.rb @@ -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 diff --git a/lib/tcp_server.rb b/lib/tcp_server.rb new file mode 100644 index 0000000..ba61e87 --- /dev/null +++ b/lib/tcp_server.rb @@ -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 = "

Hello, World!

" + status = 200 + elsif routeReturn == 404 + html = "

Not found!

" + 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 \ No newline at end of file diff --git a/main.rb b/main.rb new file mode 100644 index 0000000..90b3183 --- /dev/null +++ b/main.rb @@ -0,0 +1,4 @@ +require_relative 'lib/tcp_server' + +server = HTTPServer.new(4567) +server.start \ No newline at end of file diff --git a/spec/example_requests/get-examples.request.txt b/spec/example_requests/get-examples.request.txt index eedd945..2759b43 100644 --- a/spec/example_requests/get-examples.request.txt +++ b/spec/example_requests/get-examples.request.txt @@ -2,5 +2,4 @@ GET /examples HTTP/1.1 Host: example.com User-Agent: ExampleBrowser/1.0 Accept-Encoding: gzip, deflate -Accept: */* - +Accept: */* \ No newline at end of file diff --git a/spec/example_requests/get-subexamples.request.txt b/spec/example_requests/get-subexamples.request.txt new file mode 100644 index 0000000..174c77c --- /dev/null +++ b/spec/example_requests/get-subexamples.request.txt @@ -0,0 +1,5 @@ +GET /examples/bananas HTTP/1.1 +Host: example.com +User-Agent: ExampleBrowser/1.0 +Accept-Encoding: gzip, deflate +Accept: */* \ No newline at end of file diff --git a/spec/http_router_spec.rb b/spec/http_router_spec.rb new file mode 100644 index 0000000..1b48d18 --- /dev/null +++ b/spec/http_router_spec.rb @@ -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 \ No newline at end of file diff --git a/test.rb b/test.rb new file mode 100644 index 0000000..68a2499 --- /dev/null +++ b/test.rb @@ -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 \ No newline at end of file