lib/baurets/optionsful/server.rb in optionsful-0.2.3 vs lib/baurets/optionsful/server.rb in optionsful-0.3.0

- old
+ new

@@ -2,51 +2,65 @@ module Optionsful class Server def initialize(app) @app = app - @config = ::Baurets::Optionsful::Config.new + @config = Config.new end def call(env) unless env["REQUEST_METHOD"] == "OPTIONS" @app.call(env) else - extract_options_information(env) + @env = env + build_response end end private - def extract_options_information(env) - allows = extract_allowed_methods(env) - if allows.empty? - [404, {}, "Not found."] + def build_response + allows = extract_options_information + headers = {} + status = 500 + body = "" + unless allows.empty? + headers.merge!({"Allow" => allows}) + status = 204 + if @config.link + headers.merge!({"Link" => build_link_header}) + end else - [204, {"Allow" => allows, "Link" => "\"#{build_help_link(env)}\""}, ""] + status = 404 + body = "Not found" end + [status, headers, body] end - - def extract_allowed_methods(env) - # do routing introspection: - routes = ::Baurets::Optionsful::Introspections.do_routing_introspection - # do request path investigation - path = env["PATH_INFO"] - route_guess = ::Baurets::Optionsful::Introspections.guess_route(routes, path) + + def build_link_header + link = "" + if @config.host == "auto" + server_name = @env["SERVER_NAME"] + server_port = @env["SERVER_PORT"] + link = "\"<http://#{server_name}:#{server_port}" + else + link = "\"<http://#{@config.host}" + end + unless @config.base_path.empty? + link += @config.base_path unless @config.base_path == "/" + end + if @config.propagate == true + link += @env["PATH_INFO"] + end - puts "\nPATH: \n #{path}\n" - puts "\nROUTES: \n #{routes.inspect}\n" - puts "\nGUESS: \n #{route_guess.inspect}\n" - - # do the matches: - allow = ::Baurets::Optionsful::Introspections.do_the_matches(routes, route_guess) + link += ">; type=text/html; rel=help\"" + link end - def build_help_link(env) - server_name = env["SERVER_NAME"] - server_port = env["SERVER_PORT"] - "<http://#{server_name}:#{server_port}" + @config.base_path + "#{env["PATH_INFO"]}>; type=text/html; rel=help" + def extract_options_information + allows = ::Baurets::Optionsful::Introspections.do_the_matches(@env["PATH_INFO"]) end end end -end \ No newline at end of file +end +