Sha256: 7252ecfde8b553fce91835c5990d0be37c8d216e4d6502e0910f62a72d88b0f9

Contents?: true

Size: 1006 Bytes

Versions: 6

Compression:

Stored size: 1006 Bytes

Contents

require 'sitehub/cookie/rewriting'
require 'sitehub/location_rewriters'
require 'sitehub/request_mapping'
require 'sitehub/constants'

class SiteHub
  module Middleware
    class ReverseProxy
      include Cookie::Rewriting, 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

6 entries across 6 versions & 1 rubygems

Version Path
sitehub-0.5.0.alpha12 lib/sitehub/middleware/reverse_proxy.rb
sitehub-0.5.0.alpha11 lib/sitehub/middleware/reverse_proxy.rb
sitehub-0.5.0.alpha10 lib/sitehub/middleware/reverse_proxy.rb
sitehub-0.5.0.alpha8 lib/sitehub/middleware/reverse_proxy.rb
sitehub-0.5.0.alpha7 lib/sitehub/middleware/reverse_proxy.rb
sitehub-0.5.0.alpha6 lib/sitehub/middleware/reverse_proxy.rb