Skip to content

Instantly share code, notes, and snippets.

@marcelorxaviers
Last active November 8, 2024 13:54
Show Gist options
  • Save marcelorxaviers/874ec5f3a950ac40b430a01f8946777e to your computer and use it in GitHub Desktop.
Save marcelorxaviers/874ec5f3a950ac40b430a01f8946777e to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
# ...
require_relative "../lib/middleware/marcelo"
# ...
module CoolName
class Application < Rails::Application
#...
# Before anything you get me
# config.middleware.insert_before 0, Marcelo
# Appends it to the end of the middleware chain
config.middleware.use Marcelo
#...
end
end
# frozen_string_literal: true
# I am the man in the middle(ware)
#
# Reading material:
# - https://younes.codes/posts/what-is-rack
# - https://guides.rubyonrails.org/rails_on_rack.html
class Marcelo
def initialize(app)
@app = app
end
def call(env)
return @app.call(env) unless "money".in?(env["REQUEST_PATH"].to_s.downcase)
[
Rack::Utils::SYMBOL_TO_STATUS_CODE[:misdirected_request], # Status code
{ denied: "you'll see no money here" }, # Headers
["Go back where you came from, mercenary."] # Body
]
end
end
@marcelorxaviers
Copy link
Author

marcelorxaviers commented Nov 8, 2024

By doing the following:

curl -i  http://0.0.0.0:3000/show/me/the/money

The result should be:

HTTP/1.1 421 Misdirected Request
denied: you'll see no money here
cache-control: no-cache
content-security-policy:
x-request-id: 985326a1-d9bb-46ec-b423-bb47fb527cef
x-runtime: 0.001031
Server-Timing: total;dur=1.3340000296011567
set-cookie: __profilin=p%3Dt; path=/; httponly; samesite=lax
Content-Length: 39

Go back where you came from, mercenary.%

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment