-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
793 additions
and
8 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# frozen_string_literal: true | ||
|
||
module Rack | ||
# @api public | ||
# @since 0.1.0 | ||
class BlastWave::BlackList < BlastWave::CheckList | ||
# @param env [Hash] | ||
# @return [Object] | ||
# | ||
# @api private | ||
# @since 0.1.0 | ||
def call(env) | ||
check!(env) ? generate_fail_response! : super | ||
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# frozen_string_literal: true | ||
|
||
module Rack | ||
# @api private | ||
# @since 0.1.0 | ||
class BlastWave::CheckList < BlastWave::Middleware | ||
require_relative 'check_list/filter' | ||
require_relative 'check_list/filter_registry' | ||
require_relative 'check_list/fail_response' | ||
require_relative 'check_list/checkable' | ||
require_relative 'check_list/checker' | ||
require_relative 'check_list/builder' | ||
|
||
class << self | ||
# Creates a clone of the current middleware with it's own initial settings. | ||
# | ||
# @return [Class<BlastWave::CheckList>] | ||
# | ||
# @api public | ||
# @since 0.1.0 | ||
def build | ||
Class.new(self) | ||
end | ||
|
||
# @param child_klass [Class] | ||
# @return [void] | ||
# | ||
# @api private | ||
# @since 0.1.0 | ||
def inherited(child_klass) | ||
BlastWave::CheckList::Builder.build(child_klass) | ||
end | ||
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 |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# frozen_string_literal: true | ||
|
||
module Rack | ||
# @api public | ||
# @since 0.1.0 | ||
module BlastWave::CheckList::Builder | ||
# @return [Proc] | ||
# | ||
# @api private | ||
# @since 0.1.0 | ||
# rubocop:disable Metrics/BlockLength | ||
CLASS_DEFINITIONS = proc do |check_all: false| | ||
# @since 0.1.0 | ||
include Qonfig::Configurable | ||
# @since 0.1.0 | ||
include BlastWave::CheckList::Checkable | ||
|
||
# @since 0.1.0 | ||
configuration do | ||
setting :check_all, check_all | ||
|
||
setting :fail_response do | ||
setting :body, BlastWave::CheckList::FailResponse::DEFAULT_BODY | ||
setting :status, BlastWave::CheckList::FailResponse::DEFAULT_STATUS | ||
setting :headers, BlastWave::CheckList::FailResponse::DEFAULT_HEADERS | ||
end | ||
end | ||
|
||
# @return [Qonfig::Settings] | ||
# | ||
# @api private | ||
# @since 0.1.0 | ||
def options | ||
self.class.config.settings | ||
end | ||
|
||
private | ||
|
||
# @param env [Hash] | ||
# @return [Boolean] | ||
# | ||
# @api private | ||
# @since 0.1.0 | ||
def check!(env) | ||
request = Rack::Request.new(env) | ||
should_check_all = options.check_all? | ||
checker.check!(request, check_all: should_check_all) | ||
end | ||
|
||
# @return [Rack::Response] | ||
# | ||
# @api private | ||
# @since 0.1.0 | ||
def generate_fail_response! | ||
BlastWave::CheckList::FailResponse.build( | ||
status: options.fail_response.status, | ||
body: options.fail_response.body, | ||
headers: options.fail_response.headers | ||
).finish | ||
end | ||
end | ||
# rubocop:enable Metrics/BlockLength | ||
|
||
class << self | ||
# @param empty_middleware_klass [Class<BlastWave::MiddleWare>] | ||
# @return [Class<BlastWave::MiddleWare>] | ||
# | ||
# @api public | ||
# @since 0.1.0 | ||
def build(empty_middleware_klass = Class.new(BlastWave::Middleware), &additionals) | ||
empty_middleware_klass.tap do |middleware_klass| | ||
middleware_klass.class_eval(&CLASS_DEFINITIONS) | ||
middleware_klass.class_eval(&additionals) if block_given? | ||
end | ||
end | ||
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# frozen_string_literal: true | ||
|
||
module Rack | ||
# @api private | ||
# @since 0.1.0 | ||
module BlastWave::CheckList::Checkable | ||
class << self | ||
# @param base_klass [Class] | ||
# @return [void] | ||
# | ||
# @api private | ||
# @since 0.1.0 | ||
def included(base_klass) | ||
base_klass.extend(ClassMethods) | ||
base_klass.include(InstanceMethods) | ||
base_klass.instance_variable_set(:@checker, BlastWave::CheckList::Checker.new) | ||
base_klass.singleton_class.class_eval { attr_reader :checker } | ||
end | ||
end | ||
|
||
# @api private | ||
# @since 0.1.0 | ||
module ClassMethods | ||
# @param block [Proc] | ||
# @return [void] | ||
# | ||
# @api public | ||
# @since 0.1.0 | ||
def filter(name = nil, &block) | ||
block_given? ? checker.register(name || block.object_id, &block) : checker.fetch(name) | ||
end | ||
|
||
# @return [void] | ||
# | ||
# @api public | ||
# @since 0.1.0 | ||
def clear! | ||
checker.clear! | ||
end | ||
end | ||
|
||
# @api private | ||
# @since 0.1.0 | ||
module InstanceMethods | ||
# @return [BlastWave::CheckLis::Checker] | ||
# | ||
# @api private | ||
# @sicne 0.1.0 | ||
def checker | ||
self.class.checker | ||
end | ||
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 |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# frozen_string_literal: true | ||
|
||
module Rack | ||
# @api private | ||
# @since 0.1.0 | ||
class BlastWave::CheckList::Checker | ||
# @return [void] | ||
# | ||
# @api private | ||
# @since 0.1.0 | ||
def initialize | ||
@filter_registry = BlastWave::CheckList::FilterRegistry.new | ||
end | ||
|
||
# @param request [Rack::Request] | ||
# @option check_all [Boolean] | ||
# @return [Boolean] | ||
# | ||
# @api private | ||
# @since 0.1.0 | ||
def check!(request, check_all: false) | ||
check_all ? all?(request) : any?(request) | ||
end | ||
|
||
# @return [void] | ||
# | ||
# @api private | ||
# @since 0.1.0 | ||
def clear! | ||
filter_registry.clear! | ||
end | ||
|
||
# @param block [Proc] | ||
# @return [void] | ||
# | ||
# @api private | ||
# @since 0.1.0 | ||
def register(name, &block) | ||
filter_registry.register(BlastWave::CheckList::Filter.new(name, block)) | ||
end | ||
|
||
# @param name [Object] | ||
# @return [BlastWave::CheckList::Filter] | ||
# | ||
# @api private | ||
# @since 0.1.0 | ||
def fetch(name) | ||
filter_registry.fetch(name) | ||
end | ||
|
||
private | ||
|
||
# @return [BlastWave::CheckList::FilterRegistry] | ||
# | ||
# @api private | ||
# @since 0.1.0 | ||
attr_reader :filter_registry | ||
|
||
# @param request [Rack::Request] | ||
# @return [Boolean] | ||
# | ||
# @api private | ||
# @since 0.1.0 | ||
def any?(request) | ||
filter_registry.any? { |filter| filter.match?(request) } | ||
end | ||
|
||
# @param request [Rack::Request] | ||
# @return [Boolean] | ||
# | ||
# @api private | ||
# @since 0.1.0 | ||
def all?(request) | ||
filter_registry.all? { |filter| filter.match?(request) } | ||
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# frozen_string_literal: true | ||
|
||
module Rack | ||
# @api private | ||
# @since 0.1.0 | ||
class BlastWave::CheckList::FailResponse < Rack::Response | ||
# @return [Array<String>] | ||
# | ||
# @api private | ||
# @since 0.1.0 | ||
DEFAULT_BODY = ["Forbidden\n"].freeze | ||
|
||
# @return [Hash] | ||
# | ||
# @api private | ||
# @since 0.1.0 | ||
DEFAULT_HEADERS = { 'Content-Type' => 'text/plain' }.freeze | ||
|
||
# @return [Integer] | ||
# | ||
# @api private | ||
# @since 0.1.0 | ||
DEFAULT_STATUS = 403 | ||
|
||
class << self | ||
# @option status [Integer] | ||
# @option body [Array<String>] | ||
# @option headers [Hash] | ||
# @return [Rack::Response] | ||
# | ||
# @api private | ||
# @since 0.1.0 | ||
def build(status: DEFAULT_STATUS, body: DEFAULT_BODY, headers: DEFAULT_HEADERS) | ||
new(body, status, headers) | ||
end | ||
end | ||
|
||
# @param body [Array<String>] | ||
# @param status [Integer] | ||
# @param header [Hash] | ||
# @return [void] | ||
# | ||
# @since 0.1.0 | ||
# @api private | ||
def initialize(body = DEFAULT_BODY, status = DEFAULT_STATUS, header = DEFAULT_HEADERS) | ||
super | ||
end | ||
end | ||
end |
Oops, something went wrong.