This file contains hidden or 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
| # Rack::Middleware that returns the number of models in the database for any call to a URL like modelname/count. | |
| # | |
| # Examples: | |
| # /users/count | |
| # => returns User.count in plain text | |
| # | |
| # /articles/1/comments/count.xml | |
| # => returns Comment.count as XML, e.g. | |
| # <comments type="integer">5</comments> | |
| # Note: this does not return Articles.find(1).comments.count like it maybe should! |
This file contains hidden or 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
| # Full code is at http://github.com/cwninja/rack-cache-buster | |
| require 'digest/md5' | |
| module Rack | |
| class CacheBuster | |
| def initialize(app, key, target_time = nil) | |
| @app, @target_time = app, target_time | |
| @key = "-"+Digest::MD5.hexdigest(key || "blank-key").freeze | |
| @key_regexp = /#{@key}/.freeze |
This file contains hidden or 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
| require 'nokogiri' | |
| require 'open-uri' | |
| gem 'maca-fork-csspool' | |
| require 'csspool' | |
| module InlineStyle | |
| module Rack | |
| class Middleware | |
| # |
This file contains hidden or 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
| require 'RMagick' | |
| class WatermarkMe | |
| def initialize(app, args) | |
| @app = app | |
| @watermark_text, @mime_types = *args | |
| @mime_types ||= %w[image/jpeg image/png image/gif] | |
| end | |
| def call(env) |
This file contains hidden or 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
| module Rack | |
| class Censor | |
| WORDS = [ 'shit', 'fuck', 'cock', 'cunt', 'cameltoe', 'mooseknuckle' ].map { |w| Regexp.new(w, Regexp::IGNORECASE) }.freeze | |
| attr_reader :options, :request | |
| def initialize(app, options={}) | |
| @app, @options = app, { | |
| :replacement => '*****' |
This file contains hidden or 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
| require 'geoip' | |
| module Rack | |
| # Rack::GeoIPCountry uses the geoip gem and the GeoIP database to lookup the country of a request by its IP address | |
| # The database can be downloaded from: | |
| # http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz | |
| # | |
| # Usage: | |
| # use Rack::GeoIPCountry, :db => "path/to/GeoIP.dat" | |
| # |
This file contains hidden or 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 is actually available as a gem: gem install rack-rewrite | |
| # Full source code including tests is on github: http://github.com/jtrupiano/rack-rewrite | |
| module Rack | |
| # A rack middleware for defining and applying rewrite rules. In many cases you | |
| # can get away with rack-rewrite instead of writing Apache mod_rewrite rules. | |
| class Rewrite | |
| def initialize(app, &rule_block) | |
| @app = app | |
| @rule_set = RuleSet.new |
This file contains hidden or 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
| # BruteForceKilla | |
| # | |
| # A Rack middleware to limit requests by ip address, coded for fun as my first | |
| # middleware, thanks http://coderack.org for giving me a reason :) | |
| # | |
| # For production use, one would want to make a memcache or redis tracker. | |
| # | |
| # options: | |
| # | |
| # :tracker => Class name of the tracker to use (default Memory (all there is for now!)) |
This file contains hidden or 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
| module Rack | |
| # | |
| # The LieServer is a simple Rack middleware app which allows one to spoof | |
| # the +Server+ header in responses for every request, requests to certain | |
| # sub-directories or paths which match a regular expression. | |
| # | |
| # Be deceitful to would be attackers, tell them your running IIS 3.0. | |
| # | |
| # MIT License - Hal Brodigan (postmodern.mod3 at gmail.com) | |
| # |
This file contains hidden or 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
| module Rack | |
| # | |
| # RefererControl is a Rack middleware app which restricts access to paths | |
| # based on the Referer header. Using RefererControl you can make sure | |
| # users follow the intended flow of a website. If a controlled path is | |
| # visited with an unacceptable Referer URI, then a simple 307 Redirect | |
| # response is returned. | |
| # | |
| # RefererControl should also make Cross Site Request Forgery (CSRF) a | |
| # little more difficult to exploit; but not impossible using JavaScript. |