The parser module provides standard methods for accessing the headers and content of a response that are shared across pluggable parsers.
Special filenames that must be escaped
Access HTTP header by name
# File lib/mechanize/parser.rb, line 61 61: def_delegator :header, :[], :[]
Set HTTP header to value
# File lib/mechanize/parser.rb, line 68 68: def_delegator :header, :[]=, :[]=
Enumerate HTTP headers
# File lib/mechanize/parser.rb, line 82 82: def_delegator :header, :each, :each
Enumerate HTTP headers in capitalized (canonical) form
# File lib/mechanize/parser.rb, line 89 89: def_delegator :header, :canonical_each, :canonical_each
Extracts the filename from a Content-Disposition header in the # 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.
# File lib/mechanize/parser.rb, line 97 97: def extract_filename full_path = @full_path 98: handled = false 99: 100: if @uri then 101: uri = @uri 102: uri += 'index.html' if uri.path.end_with? '/' 103: 104: path = uri.path.split(/\//) 105: filename = path.pop || 'index.html' 106: else 107: path = [] 108: filename = 'index.html' 109: end 110: 111: # Set the filename 112: if disposition = @response['content-disposition'] then 113: content_disposition = 114: Mechanize::HTTP::ContentDispositionParser.parse disposition 115: 116: if content_disposition && content_disposition.filename then 117: filename = content_disposition.filename 118: filename = filename.split(/[\\\/]/).last 119: handled = true 120: end 121: end 122: 123: if not handled and @uri then 124: filename << '.html' unless filename =~ /\./ 125: filename << "?#{@uri.query}" if @uri.query 126: end 127: 128: if SPECIAL_FILENAMES =~ filename then 129: filename = "_#{filename}" 130: end 131: 132: filename = filename.tr "\x00-\x20<>:\"/\\|?*", '_' 133: 134: @filename = if full_path then 135: File.join @uri.host, path, filename 136: else 137: filename 138: end 139: end
Creates a Mechanize::Header from the Net::HTTPResponse response.
This allows the Net::HTTPResponse to be garbage collected sooner.
# File lib/mechanize/parser.rb, line 146 146: def fill_header response 147: @response = Mechanize::Headers.new 148: 149: response.each { |k,v| 150: @response[k] = v 151: } if response 152: 153: @response 154: end
Finds a free filename based on filename, but is not race-free
# File lib/mechanize/parser.rb, line 159 159: def find_free_name filename 160: base_filename = filename ||= @filename 161: 162: number = 1 163: 164: while File.exist? filename do 165: filename = "#{base_filename}.#{number}" 166: number += 1 167: end 168: 169: filename 170: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.