module Mechanize::Parser
The parser module provides standard methods for accessing the headers and content of a response that are shared across pluggable parsers.
Constants
- SPECIAL_FILENAMES
-
Special filenames that must be escaped
Attributes
The Mechanize::Headers for this file
The Mechanize::Headers for this file
The URI this file was retrieved from
Public Instance Methods
Source
# File lib/mechanize/parser.rb, line 65 def_delegator :header, :[], :[]
Access HTTP header by name
Source
# File lib/mechanize/parser.rb, line 75 def_delegator :header, :[]=, :[]=
Set HTTP header to value
Source
# File lib/mechanize/parser.rb, line 92 def_delegator :header, :each, :each
Enumerate HTTP headers
Source
# File lib/mechanize/parser.rb, line 188 def ensure_within directory, path directory = File.join File.expand_path(directory), '' return if File.expand_path(path).start_with? directory raise Mechanize::Error, "refusing to save #{path} outside #{directory}" end
Raises Mechanize::Error unless path is inside directory.
Source
# File lib/mechanize/parser.rb, line 107 def extract_filename full_path = @full_path handled = false if @uri then uri_path = remove_dot_segments @uri.path uri_path += 'index.html' if uri_path.end_with? '/' path = uri_path.split(/\//) filename = path.pop || 'index.html' else path = [] filename = 'index.html' end # Set the filename if (disposition = @response['content-disposition']) content_disposition = Mechanize::HTTP::ContentDispositionParser.parse disposition if content_disposition && content_disposition.filename && content_disposition.filename != '' filename = content_disposition.filename filename = filename.rpartition(/[\\\/]/).last handled = true end end if not handled and @uri then filename << '.html' unless filename =~ /\./ filename << "?#{@uri.query}" if @uri.query end if SPECIAL_FILENAMES =~ filename then filename = "_#{filename}" end filename = filename.tr "\x00-\x20<>:\"/\\|?*", '_' @filename = if full_path then File.join @uri.host, path, filename else filename end end
Extracts the filename from a Content-Disposition header in the response or from the URI. If full_path is true the filename will include the host name and path to the resource, otherwise a filename in the current directory is given.
Source
# File lib/mechanize/parser.rb, line 156 def fill_header response @response = Mechanize::Headers.new response.each { |k,v| @response[k] = v } if response @response end
Creates a Mechanize::Header from the Net::HTTPResponse response.
This allows the Net::HTTPResponse to be garbage collected sooner.
Source
# File lib/mechanize/parser.rb, line 199 def find_free_name filename base_filename = filename ||= @filename number = 1 while File.exist? filename do filename = "#{base_filename}.#{number}" number += 1 end filename end
Finds a free filename based on filename, but is not race-free
Source
# File lib/mechanize/parser.rb, line 85 def_delegator :header, :key?, :key?
Is the named header present?
Source
# File lib/mechanize/parser.rb, line 170 def remove_dot_segments path segments = path.split '/', -1 resolved = segments.each_with_object [] do |segment, result| case segment when '.' then next when '..' then result.pop unless result == [''] else result << segment end end resolved << '' if ['.', '..'].include? segments.last resolved.join '/' end
Resolves “.” and “..” segments in the absolute path as RFC 3986 section 5.2.4 does, without reparsing it as a URI.