Sha256: 7b38d66ed46fc533b684fe0e2ca7bd776541220d8b35838f403ed31b9ece5844

Contents?: true

Size: 929 Bytes

Versions: 1

Compression:

Stored size: 929 Bytes

Contents

require 'logger'

module Rack
  class Escapee
    ESCAPED_TAG    = /(<\w+>)/
    ESCAPED_ENTITY = /(&\w+;)/


    def initialize(app, options = {})
      @app = app
      @options = {
        :logfile => false
      }.merge(options)

      if @options[:logfile]
        @logger = ::Logger.new(@options[:logfile])
      else
        @logger = ::Logger.new(STDOUT)
      end
      @logger.datetime_format = "%Y-%m-%d %H:%M:%S"
    end

    def call(env)
      req = Request.new(env)
      path = req.path_info
      status, headers, response = @app.call(env)
      response_body = []
      response.each do |part|
        if part =~ ESCAPED_TAG
          @logger.warn("rendering escaped tag: #{$1} - #{path}")
        elsif part =~ ESCAPED_ENTITY
          @logger.warn("rendering escaped entity: #{$1} - #{path}")
        end
        response_body << part
      end

      [status, headers, response_body]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rack-escapee-0.1.0 lib/rack/escapee.rb