Sha256: f34f4f2031d3b2a56ce6edfcd1c5ccae2229180e2cfe9d889904197253fd265b

Contents?: true

Size: 994 Bytes

Versions: 5

Compression:

Stored size: 994 Bytes

Contents

require 'sitehub/cookie_rewriting'
require 'sitehub/location_rewriters'
require 'sitehub/request_mapping'
require 'sitehub/constants'

class SiteHub
  module Middleware
    class ReverseProxy
      include CookieRewriting, Constants::HttpHeaderKeys

      attr_reader :path_directives

      def initialize(app, directives)
        @app = app
        @path_directives = LocationRewriters.new(directives)
      end

      def call(env)
        status, headers, body = @app.call(env).to_a
        mapping = env[REQUEST].mapping

        headers[LOCATION_HEADER] = location(headers, mapping.source_url) if headers[LOCATION_HEADER]

        rewrite_cookies(headers, substitute_domain: mapping.host) if headers[SET_COOKIE]

        [status, HttpHeaders.new(headers), body]
      end

      private

      def location(headers, source_url)
        location_header = headers[LOCATION_HEADER]
        path_directives.find(location_header).apply(location_header, source_url)
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
sitehub-0.4.10 lib/sitehub/middleware/reverse_proxy.rb
sitehub-0.4.9 lib/sitehub/middleware/reverse_proxy.rb
sitehub-0.4.8 lib/sitehub/middleware/reverse_proxy.rb
sitehub-0.4.7 lib/sitehub/middleware/reverse_proxy.rb
sitehub-0.4.6 lib/sitehub/middleware/reverse_proxy.rb