Skip to content

Instantly share code, notes, and snippets.

@mimosa
Created May 15, 2017 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mimosa/36c41ead7022d97ba98d44a21fb5f0a1 to your computer and use it in GitHub Desktop.
Save mimosa/36c41ead7022d97ba98d44a21fb5f0a1 to your computer and use it in GitHub Desktop.
Rack::Proxy

Gemfile

...
gem 'rack-proxy', require: 'rack/proxy'
...

config.ru

# frozen_string_literal: true

# This file is used by Rack-based servers to start the application.
require_relative 'config/environment'

class NilProxy < Rack::Proxy
  def perform_request(env)
    request = Rack::Request.new(env)

    if request.path =~ %r{\.pdf}
      env['HTTP_HOST'] = 'www.eff.org'
      env['SERVER_PORT'] = '443'
      env['PATH_INFO'] = '/files/filenode/readme.pdf'
      env['rack.url_scheme'] = 'https'

      super(env)
    else
      @app.call(env)
    end
  end

  def rewrite_response(triplet)
    status, headers, body = triplet

    headers['Content-Type'] = 'application/pdf'
    headers['Content-Disposition'] = 'attachment; filename=test.pdf'

    [status, headers, body]
  end
end

use NilProxy, ssl_verify_none: true

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