-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make Allow middleware thread-safe without duping
- Drop support for passing config options to middleware
- Loading branch information
Showing
8 changed files
with
97 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#!/usr/bin/env ruby | ||
# frozen_string_literal: true | ||
|
||
require "bundler/setup" | ||
require "strong_routes" | ||
|
||
require "benchmark/ips" | ||
|
||
StrongRoutes.config.allowed_routes = [ | ||
"/", | ||
"/bar/sandwich", | ||
"/comments/:id", | ||
"/comments/:id/edit", | ||
"/posts/:id", | ||
"/posts/:id/edit", | ||
"/posts/:post_id/comments", | ||
"/posts/:post_id/comments/new", | ||
"/trading-post", | ||
"/user_posts/:id", | ||
"/users", | ||
"/users/:user_id/posts", | ||
"/users/:user_id/posts/new" | ||
] | ||
StrongRoutes.reload! if StrongRoutes.respond_to?(:reload!) | ||
|
||
app = StrongRoutes::Allow.new(lambda { |env| [200, {"content-type" => "text/plain"}, ["OK"]] }) | ||
|
||
Benchmark.ips do |x| | ||
x.report("Hit") { app.call(Rack::PATH_INFO => "/users/1") } | ||
x.report("Miss") { app.call(Rack::PATH_INFO => "/baz") } | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,23 @@ | ||
module StrongRoutes | ||
class Allow | ||
def initialize(app, options = {}) | ||
def initialize(app) | ||
@app = app | ||
@options = options | ||
end | ||
|
||
def _call(env) | ||
return @app.call(env) unless enabled? | ||
|
||
request = ::Rack::Request.new(env) | ||
def call(env) | ||
return @app.call(env) unless config.enabled? | ||
|
||
if allowed?(request) | ||
if StrongRoutes.allowed?(env[Rack::PATH_INFO].to_s) | ||
@app.call(env) | ||
else | ||
[404, {"Content-Type" => "text/html", "Content-Length" => config.message.length.to_s}, [config.message]] | ||
end | ||
end | ||
|
||
# HACK: This makes this middleware threadsafe, but is not a very good pattern. | ||
# We should rewrite this to use a separate class with instance-level stuff. | ||
def call(env) | ||
dup._call(env) | ||
end | ||
|
||
private | ||
|
||
def allowed_routes | ||
@allowed_routes ||= begin | ||
routes = [config.allowed_routes] | ||
routes.flatten! | ||
routes.compact! | ||
routes.uniq! | ||
routes | ||
end | ||
end | ||
|
||
def allowed?(request) | ||
route_matchers.any? { |route_matcher| route_matcher =~ request.path_info } | ||
end | ||
|
||
def config | ||
@config ||= StrongRoutes.config.merge(@options) | ||
end | ||
|
||
def enabled? | ||
config.enabled? | ||
end | ||
|
||
def route_matchers | ||
@route_matchers ||= allowed_routes.map { |allowed_route| ::StrongRoutes::RouteMatcher.new(allowed_route) } | ||
StrongRoutes.config | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,35 @@ | ||
module StrongRoutes | ||
module Accessorable | ||
# Creates an accessor that simply sets and reads a key in the hash: | ||
# | ||
# class Config < Hash | ||
# hash_accessor :app | ||
# end | ||
# | ||
# config = Config.new | ||
# config.app = Foo | ||
# config[:app] #=> Foo | ||
# | ||
# config[:app] = Bar | ||
# config.app #=> Bar | ||
# | ||
def hash_accessor(*names) # :nodoc: | ||
names.each do |name| | ||
class_eval <<-METHOD, __FILE__, __LINE__ + 1 | ||
def #{name} | ||
self[:#{name}] | ||
end | ||
class Config | ||
attr_accessor :enabled, | ||
:insert_after, | ||
:insert_before, | ||
:message | ||
|
||
def #{name}=(value) | ||
self[:#{name}] = value | ||
end | ||
attr_reader :allowed_routes, :route_matchers | ||
|
||
def #{name}? | ||
!! self[:#{name}] | ||
end | ||
METHOD | ||
end | ||
def initialize | ||
@allowed_routes = Set.new | ||
@enabled = true | ||
@message = "Resource Not Found" | ||
@route_matchers = [] | ||
end | ||
end | ||
|
||
class Config < Hash | ||
extend Accessorable | ||
def allowed_routes=(value) | ||
raise TypeError, "allowed routes must be an Enumerable" unless value.is_a?(Enumerable) | ||
@allowed_routes = value.to_set | ||
@route_matchers = allowed_routes.map { |route| StrongRoutes::RouteMatcher.new(route) } | ||
end | ||
|
||
hash_accessor :allowed_routes, | ||
:enabled, | ||
:insert_after, | ||
:insert_before, | ||
:message | ||
def enabled? | ||
!!enabled | ||
end | ||
|
||
def initialize(*) | ||
super | ||
def insert_after? | ||
!!insert_after | ||
end | ||
|
||
self[:enabled] = true if self[:enabled].nil? | ||
self[:message] = "Resource Not Found" if self[:message].nil? | ||
def insert_before? | ||
!!insert_before | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters