Method: Mechanize::Parser#extract_filename

Defined in:
lib/mechanize/parser.rb

#extract_filename(full_path = @full_path) ⇒ Object

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.

[View source]

107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/mechanize/parser.rb', line 107

def extract_filename full_path = @full_path
  handled = false

  if @uri then
    uri = @uri
    uri += '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