Sha256: d781004c4f29e46bb3109436f632d0ff020da5d4afca4d605fdb570fe327f06e

Contents?: true

Size: 1.24 KB

Versions: 10

Compression:

Stored size: 1.24 KB

Contents

require "delegate"

module Immunio
  # Rack middleware running at the very end of the stack to finish HTTP requests.
  class HTTPFinisher
    def initialize(app)
      @app = app
    end

    def call(env)
      status, headers, body = @app.call(env)
      if Request.current
        Immunio.logger.debug "Finishing request in HTTPFinisher"
        [status, headers, BodyWrapper.new(body)]
      else
        [status, headers, body]
      end
    end
  end

  class BodyWrapper < SimpleDelegator
    def initialize(body)
      super body
      @body = body
    end

    def each
      begin
        @body.each do |chunk|
          Immunio.run_hook "http_finisher", "http_response_body_chunk",
                            chunk: chunk
          yield chunk
        end
      rescue RequestBlocked
        # We need to catch this exception here as access to static files is
        # deferred. If one of our hooks, such as file_io throws when the Rake
        # stack has unwound this far it will kill the request with a 500
        yield "Request Blocked"
      end
      Immunio.run_hook "http_finisher", "http_request_finish"

      # Reset current request
      Immunio.finish_request
    end

    def respond_to?(*args)
      @body.respond_to?(*args)
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
immunio-1.0.4 lib/immunio/plugins/http_finisher.rb
immunio-1.0.3 lib/immunio/plugins/http_finisher.rb
immunio-1.0.2 lib/immunio/plugins/http_finisher.rb
immunio-1.0.1 lib/immunio/plugins/http_finisher.rb
immunio-1.0.0 lib/immunio/plugins/http_finisher.rb
immunio-0.16.1 lib/immunio/plugins/http_finisher.rb
immunio-0.16.0 lib/immunio/plugins/http_finisher.rb
immunio-0.15.4 lib/immunio/plugins/http_finisher.rb
immunio-0.15.3 lib/immunio/plugins/http_finisher.rb
immunio-0.15.2 lib/immunio/plugins/http_finisher.rb