Guest User

Untitled

a guest
Jul 18th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. require 'rack'
  2. require 'rack/request'
  3.  
  4. module Rack
  5. class HSTS
  6. def initialize(app, options = {})
  7. @app, @options = app, { :expires => 600, :port => 443 }.merge(options)
  8. @hsts = { 'Strict-Transport-Security' => "max-age=#{@options[:expires]}" }
  9. end
  10.  
  11. def call(env)
  12. if env['rack.url_scheme'] == 'http'
  13. env['rack.url_scheme'] = 'https'
  14. env['SERVER_PORT'] = "#{@options[:port]}"
  15. [301, { 'Location' => Request.new(env).url }, []]
  16. else
  17. status, headers, body = @app.call(env)
  18. headers = @hsts.merge(headers) if env['rack.url_scheme'] == 'https'
  19. [status, headers, body]
  20. end
  21. end
  22. end
  23. end
Add Comment
Please, Sign In to add comment