Sha256: eb0c855ef9b017a3479df669144d6718cfaf56287958b168959eb0b6578ba2f5

Contents?: true

Size: 1004 Bytes

Versions: 4

Compression:

Stored size: 1004 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, Equality

      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

4 entries across 4 versions & 1 rubygems

Version Path
sitehub-0.5.0.alpha5 lib/sitehub/middleware/reverse_proxy.rb
sitehub-0.5.0.alpha4 lib/sitehub/middleware/reverse_proxy.rb
sitehub-0.5.0.alpha3 lib/sitehub/middleware/reverse_proxy.rb
sitehub-0.5.0.alpha2 lib/sitehub/middleware/reverse_proxy.rb