Sha256: bf61b6ff3394141d4fdc0264d071f44a99f668bb5001db1f3f97ecb54313b9b7

Contents?: true

Size: 1.76 KB

Versions: 5

Compression:

Stored size: 1.76 KB

Contents

module WWW
  class Mechanize
    class Chain
      class URIResolver
        include WWW::Handler

        def initialize(scheme_handlers)
          @scheme_handlers = scheme_handlers
        end

        def handle(ctx, params)
          raise ArgumentError.new('uri must be specified') unless params[:uri]
          uri     = params[:uri]
          referer = params[:referer]
          unless uri.is_a?(URI)
            uri = uri.to_s.strip.gsub(/[^#{0.chr}-#{126.chr}]/) { |match|
              sprintf('%%%X', match.unpack($KCODE == 'UTF8' ? 'U' : 'c')[0])
            }

            uri = URI.parse(
                    Util.html_unescape(
                      uri.split(/(?:%[0-9A-Fa-f]{2})+|#/).zip(
                        uri.scan(/(?:%[0-9A-Fa-f]{2})+|#/)
                      ).map { |x,y|
                        "#{URI.escape(x)}#{y}"
                      }.join('')
                    )
                  )
          end
          uri = @scheme_handlers[
            uri.relative? ? 'relative' : uri.scheme.downcase
          ].call(uri, params[:referer])
          uri.path = '/' if uri.path.length == 0

          if uri.relative?
            raise 'need absolute URL' unless referer && referer.uri
            base = referer.respond_to?(:bases) ? referer.bases.last : nil
            uri = ((base && base.uri && base.uri.absolute?) ?
                    base.uri :
                    referer.uri) + uri
            uri = referer.uri + uri
            # Strip initial "/.." bits from the path
            uri.path.sub!(/^(\/\.\.)+(?=\/)/, '')
          end

          unless ['http', 'https', 'file'].include?(uri.scheme.downcase)
            raise "unsupported scheme: #{uri.scheme}"
          end
          params[:uri] = uri

          super
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
mechanize-0.8.0 lib/www/mechanize/chain/uri_resolver.rb
mechanize-0.8.4 lib/www/mechanize/chain/uri_resolver.rb
mechanize-0.8.3 lib/www/mechanize/chain/uri_resolver.rb
mechanize-0.8.1 lib/www/mechanize/chain/uri_resolver.rb
mechanize-0.8.2 lib/www/mechanize/chain/uri_resolver.rb