Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/smanonr..../opt/alt/ruby31/share/ruby
File: open-uri.rb
# frozen_string_literal: true
[0] Fix | Delete
require 'uri'
[1] Fix | Delete
require 'stringio'
[2] Fix | Delete
require 'time'
[3] Fix | Delete
[4] Fix | Delete
module URI
[5] Fix | Delete
# Allows the opening of various resources including URIs.
[6] Fix | Delete
#
[7] Fix | Delete
# If the first argument responds to the 'open' method, 'open' is called on
[8] Fix | Delete
# it with the rest of the arguments.
[9] Fix | Delete
#
[10] Fix | Delete
# If the first argument is a string that begins with <code>(protocol)://<code>, it is parsed by
[11] Fix | Delete
# URI.parse. If the parsed object responds to the 'open' method,
[12] Fix | Delete
# 'open' is called on it with the rest of the arguments.
[13] Fix | Delete
#
[14] Fix | Delete
# Otherwise, Kernel#open is called.
[15] Fix | Delete
#
[16] Fix | Delete
# OpenURI::OpenRead#open provides URI::HTTP#open, URI::HTTPS#open and
[17] Fix | Delete
# URI::FTP#open, Kernel#open.
[18] Fix | Delete
#
[19] Fix | Delete
# We can accept URIs and strings that begin with http://, https:// and
[20] Fix | Delete
# ftp://. In these cases, the opened file object is extended by OpenURI::Meta.
[21] Fix | Delete
def self.open(name, *rest, &block)
[22] Fix | Delete
if name.respond_to?(:open)
[23] Fix | Delete
name.open(*rest, &block)
[24] Fix | Delete
elsif name.respond_to?(:to_str) &&
[25] Fix | Delete
%r{\A[A-Za-z][A-Za-z0-9+\-\.]*://} =~ name &&
[26] Fix | Delete
(uri = URI.parse(name)).respond_to?(:open)
[27] Fix | Delete
uri.open(*rest, &block)
[28] Fix | Delete
else
[29] Fix | Delete
super
[30] Fix | Delete
end
[31] Fix | Delete
end
[32] Fix | Delete
end
[33] Fix | Delete
[34] Fix | Delete
# OpenURI is an easy-to-use wrapper for Net::HTTP, Net::HTTPS and Net::FTP.
[35] Fix | Delete
#
[36] Fix | Delete
# == Example
[37] Fix | Delete
#
[38] Fix | Delete
# It is possible to open an http, https or ftp URL as though it were a file:
[39] Fix | Delete
#
[40] Fix | Delete
# URI.open("http://www.ruby-lang.org/") {|f|
[41] Fix | Delete
# f.each_line {|line| p line}
[42] Fix | Delete
# }
[43] Fix | Delete
#
[44] Fix | Delete
# The opened file has several getter methods for its meta-information, as
[45] Fix | Delete
# follows, since it is extended by OpenURI::Meta.
[46] Fix | Delete
#
[47] Fix | Delete
# URI.open("http://www.ruby-lang.org/en") {|f|
[48] Fix | Delete
# f.each_line {|line| p line}
[49] Fix | Delete
# p f.base_uri # <URI::HTTP:0x40e6ef2 URL:http://www.ruby-lang.org/en/>
[50] Fix | Delete
# p f.content_type # "text/html"
[51] Fix | Delete
# p f.charset # "iso-8859-1"
[52] Fix | Delete
# p f.content_encoding # []
[53] Fix | Delete
# p f.last_modified # Thu Dec 05 02:45:02 UTC 2002
[54] Fix | Delete
# }
[55] Fix | Delete
#
[56] Fix | Delete
# Additional header fields can be specified by an optional hash argument.
[57] Fix | Delete
#
[58] Fix | Delete
# URI.open("http://www.ruby-lang.org/en/",
[59] Fix | Delete
# "User-Agent" => "Ruby/#{RUBY_VERSION}",
[60] Fix | Delete
# "From" => "foo@bar.invalid",
[61] Fix | Delete
# "Referer" => "http://www.ruby-lang.org/") {|f|
[62] Fix | Delete
# # ...
[63] Fix | Delete
# }
[64] Fix | Delete
#
[65] Fix | Delete
# The environment variables such as http_proxy, https_proxy and ftp_proxy
[66] Fix | Delete
# are in effect by default. Here we disable proxy:
[67] Fix | Delete
#
[68] Fix | Delete
# URI.open("http://www.ruby-lang.org/en/", :proxy => nil) {|f|
[69] Fix | Delete
# # ...
[70] Fix | Delete
# }
[71] Fix | Delete
#
[72] Fix | Delete
# See OpenURI::OpenRead.open and URI.open for more on available options.
[73] Fix | Delete
#
[74] Fix | Delete
# URI objects can be opened in a similar way.
[75] Fix | Delete
#
[76] Fix | Delete
# uri = URI.parse("http://www.ruby-lang.org/en/")
[77] Fix | Delete
# uri.open {|f|
[78] Fix | Delete
# # ...
[79] Fix | Delete
# }
[80] Fix | Delete
#
[81] Fix | Delete
# URI objects can be read directly. The returned string is also extended by
[82] Fix | Delete
# OpenURI::Meta.
[83] Fix | Delete
#
[84] Fix | Delete
# str = uri.read
[85] Fix | Delete
# p str.base_uri
[86] Fix | Delete
#
[87] Fix | Delete
# Author:: Tanaka Akira <akr@m17n.org>
[88] Fix | Delete
[89] Fix | Delete
module OpenURI
[90] Fix | Delete
Options = {
[91] Fix | Delete
:proxy => true,
[92] Fix | Delete
:proxy_http_basic_authentication => true,
[93] Fix | Delete
:progress_proc => true,
[94] Fix | Delete
:content_length_proc => true,
[95] Fix | Delete
:http_basic_authentication => true,
[96] Fix | Delete
:read_timeout => true,
[97] Fix | Delete
:open_timeout => true,
[98] Fix | Delete
:ssl_ca_cert => nil,
[99] Fix | Delete
:ssl_verify_mode => nil,
[100] Fix | Delete
:ftp_active_mode => false,
[101] Fix | Delete
:redirect => true,
[102] Fix | Delete
:encoding => nil,
[103] Fix | Delete
}
[104] Fix | Delete
[105] Fix | Delete
def OpenURI.check_options(options) # :nodoc:
[106] Fix | Delete
options.each {|k, v|
[107] Fix | Delete
next unless Symbol === k
[108] Fix | Delete
unless Options.include? k
[109] Fix | Delete
raise ArgumentError, "unrecognized option: #{k}"
[110] Fix | Delete
end
[111] Fix | Delete
}
[112] Fix | Delete
end
[113] Fix | Delete
[114] Fix | Delete
def OpenURI.scan_open_optional_arguments(*rest) # :nodoc:
[115] Fix | Delete
if !rest.empty? && (String === rest.first || Integer === rest.first)
[116] Fix | Delete
mode = rest.shift
[117] Fix | Delete
if !rest.empty? && Integer === rest.first
[118] Fix | Delete
perm = rest.shift
[119] Fix | Delete
end
[120] Fix | Delete
end
[121] Fix | Delete
return mode, perm, rest
[122] Fix | Delete
end
[123] Fix | Delete
[124] Fix | Delete
def OpenURI.open_uri(name, *rest) # :nodoc:
[125] Fix | Delete
uri = URI::Generic === name ? name : URI.parse(name)
[126] Fix | Delete
mode, _, rest = OpenURI.scan_open_optional_arguments(*rest)
[127] Fix | Delete
options = rest.shift if !rest.empty? && Hash === rest.first
[128] Fix | Delete
raise ArgumentError.new("extra arguments") if !rest.empty?
[129] Fix | Delete
options ||= {}
[130] Fix | Delete
OpenURI.check_options(options)
[131] Fix | Delete
[132] Fix | Delete
if /\Arb?(?:\Z|:([^:]+))/ =~ mode
[133] Fix | Delete
encoding, = $1,Encoding.find($1) if $1
[134] Fix | Delete
mode = nil
[135] Fix | Delete
end
[136] Fix | Delete
if options.has_key? :encoding
[137] Fix | Delete
if !encoding.nil?
[138] Fix | Delete
raise ArgumentError, "encoding specified twice"
[139] Fix | Delete
end
[140] Fix | Delete
encoding = Encoding.find(options[:encoding])
[141] Fix | Delete
end
[142] Fix | Delete
[143] Fix | Delete
unless mode == nil ||
[144] Fix | Delete
mode == 'r' || mode == 'rb' ||
[145] Fix | Delete
mode == File::RDONLY
[146] Fix | Delete
raise ArgumentError.new("invalid access mode #{mode} (#{uri.class} resource is read only.)")
[147] Fix | Delete
end
[148] Fix | Delete
[149] Fix | Delete
io = open_loop(uri, options)
[150] Fix | Delete
io.set_encoding(encoding) if encoding
[151] Fix | Delete
if block_given?
[152] Fix | Delete
begin
[153] Fix | Delete
yield io
[154] Fix | Delete
ensure
[155] Fix | Delete
if io.respond_to? :close!
[156] Fix | Delete
io.close! # Tempfile
[157] Fix | Delete
else
[158] Fix | Delete
io.close if !io.closed?
[159] Fix | Delete
end
[160] Fix | Delete
end
[161] Fix | Delete
else
[162] Fix | Delete
io
[163] Fix | Delete
end
[164] Fix | Delete
end
[165] Fix | Delete
[166] Fix | Delete
def OpenURI.open_loop(uri, options) # :nodoc:
[167] Fix | Delete
proxy_opts = []
[168] Fix | Delete
proxy_opts << :proxy_http_basic_authentication if options.include? :proxy_http_basic_authentication
[169] Fix | Delete
proxy_opts << :proxy if options.include? :proxy
[170] Fix | Delete
proxy_opts.compact!
[171] Fix | Delete
if 1 < proxy_opts.length
[172] Fix | Delete
raise ArgumentError, "multiple proxy options specified"
[173] Fix | Delete
end
[174] Fix | Delete
case proxy_opts.first
[175] Fix | Delete
when :proxy_http_basic_authentication
[176] Fix | Delete
opt_proxy, proxy_user, proxy_pass = options.fetch(:proxy_http_basic_authentication)
[177] Fix | Delete
proxy_user = proxy_user.to_str
[178] Fix | Delete
proxy_pass = proxy_pass.to_str
[179] Fix | Delete
if opt_proxy == true
[180] Fix | Delete
raise ArgumentError.new("Invalid authenticated proxy option: #{options[:proxy_http_basic_authentication].inspect}")
[181] Fix | Delete
end
[182] Fix | Delete
when :proxy
[183] Fix | Delete
opt_proxy = options.fetch(:proxy)
[184] Fix | Delete
proxy_user = nil
[185] Fix | Delete
proxy_pass = nil
[186] Fix | Delete
when nil
[187] Fix | Delete
opt_proxy = true
[188] Fix | Delete
proxy_user = nil
[189] Fix | Delete
proxy_pass = nil
[190] Fix | Delete
end
[191] Fix | Delete
case opt_proxy
[192] Fix | Delete
when true
[193] Fix | Delete
find_proxy = lambda {|u| pxy = u.find_proxy; pxy ? [pxy, nil, nil] : nil}
[194] Fix | Delete
when nil, false
[195] Fix | Delete
find_proxy = lambda {|u| nil}
[196] Fix | Delete
when String
[197] Fix | Delete
opt_proxy = URI.parse(opt_proxy)
[198] Fix | Delete
find_proxy = lambda {|u| [opt_proxy, proxy_user, proxy_pass]}
[199] Fix | Delete
when URI::Generic
[200] Fix | Delete
find_proxy = lambda {|u| [opt_proxy, proxy_user, proxy_pass]}
[201] Fix | Delete
else
[202] Fix | Delete
raise ArgumentError.new("Invalid proxy option: #{opt_proxy}")
[203] Fix | Delete
end
[204] Fix | Delete
[205] Fix | Delete
uri_set = {}
[206] Fix | Delete
buf = nil
[207] Fix | Delete
while true
[208] Fix | Delete
redirect = catch(:open_uri_redirect) {
[209] Fix | Delete
buf = Buffer.new
[210] Fix | Delete
uri.buffer_open(buf, find_proxy.call(uri), options)
[211] Fix | Delete
nil
[212] Fix | Delete
}
[213] Fix | Delete
if redirect
[214] Fix | Delete
if redirect.relative?
[215] Fix | Delete
# Although it violates RFC2616, Location: field may have relative
[216] Fix | Delete
# URI. It is converted to absolute URI using uri as a base URI.
[217] Fix | Delete
redirect = uri + redirect
[218] Fix | Delete
end
[219] Fix | Delete
if !options.fetch(:redirect, true)
[220] Fix | Delete
raise HTTPRedirect.new(buf.io.status.join(' '), buf.io, redirect)
[221] Fix | Delete
end
[222] Fix | Delete
unless OpenURI.redirectable?(uri, redirect)
[223] Fix | Delete
raise "redirection forbidden: #{uri} -> #{redirect}"
[224] Fix | Delete
end
[225] Fix | Delete
if options.include? :http_basic_authentication
[226] Fix | Delete
# send authentication only for the URI directly specified.
[227] Fix | Delete
options = options.dup
[228] Fix | Delete
options.delete :http_basic_authentication
[229] Fix | Delete
end
[230] Fix | Delete
uri = redirect
[231] Fix | Delete
raise "HTTP redirection loop: #{uri}" if uri_set.include? uri.to_s
[232] Fix | Delete
uri_set[uri.to_s] = true
[233] Fix | Delete
else
[234] Fix | Delete
break
[235] Fix | Delete
end
[236] Fix | Delete
end
[237] Fix | Delete
io = buf.io
[238] Fix | Delete
io.base_uri = uri
[239] Fix | Delete
io
[240] Fix | Delete
end
[241] Fix | Delete
[242] Fix | Delete
def OpenURI.redirectable?(uri1, uri2) # :nodoc:
[243] Fix | Delete
# This test is intended to forbid a redirection from http://... to
[244] Fix | Delete
# file:///etc/passwd, file:///dev/zero, etc. CVE-2011-1521
[245] Fix | Delete
# https to http redirect is also forbidden intentionally.
[246] Fix | Delete
# It avoids sending secure cookie or referer by non-secure HTTP protocol.
[247] Fix | Delete
# (RFC 2109 4.3.1, RFC 2965 3.3, RFC 2616 15.1.3)
[248] Fix | Delete
# However this is ad hoc. It should be extensible/configurable.
[249] Fix | Delete
uri1.scheme.downcase == uri2.scheme.downcase ||
[250] Fix | Delete
(/\A(?:http|ftp)\z/i =~ uri1.scheme && /\A(?:https?|ftp)\z/i =~ uri2.scheme)
[251] Fix | Delete
end
[252] Fix | Delete
[253] Fix | Delete
def OpenURI.open_http(buf, target, proxy, options) # :nodoc:
[254] Fix | Delete
if proxy
[255] Fix | Delete
proxy_uri, proxy_user, proxy_pass = proxy
[256] Fix | Delete
raise "Non-HTTP proxy URI: #{proxy_uri}" if proxy_uri.class != URI::HTTP
[257] Fix | Delete
end
[258] Fix | Delete
[259] Fix | Delete
if target.userinfo
[260] Fix | Delete
raise ArgumentError, "userinfo not supported. [RFC3986]"
[261] Fix | Delete
end
[262] Fix | Delete
[263] Fix | Delete
header = {}
[264] Fix | Delete
options.each {|k, v| header[k] = v if String === k }
[265] Fix | Delete
[266] Fix | Delete
require 'net/http'
[267] Fix | Delete
klass = Net::HTTP
[268] Fix | Delete
if URI::HTTP === target
[269] Fix | Delete
# HTTP or HTTPS
[270] Fix | Delete
if proxy
[271] Fix | Delete
unless proxy_user && proxy_pass
[272] Fix | Delete
proxy_user, proxy_pass = proxy_uri.userinfo.split(':') if proxy_uri.userinfo
[273] Fix | Delete
end
[274] Fix | Delete
if proxy_user && proxy_pass
[275] Fix | Delete
klass = Net::HTTP::Proxy(proxy_uri.hostname, proxy_uri.port, proxy_user, proxy_pass)
[276] Fix | Delete
else
[277] Fix | Delete
klass = Net::HTTP::Proxy(proxy_uri.hostname, proxy_uri.port)
[278] Fix | Delete
end
[279] Fix | Delete
end
[280] Fix | Delete
target_host = target.hostname
[281] Fix | Delete
target_port = target.port
[282] Fix | Delete
request_uri = target.request_uri
[283] Fix | Delete
else
[284] Fix | Delete
# FTP over HTTP proxy
[285] Fix | Delete
target_host = proxy_uri.hostname
[286] Fix | Delete
target_port = proxy_uri.port
[287] Fix | Delete
request_uri = target.to_s
[288] Fix | Delete
if proxy_user && proxy_pass
[289] Fix | Delete
header["Proxy-Authorization"] =
[290] Fix | Delete
'Basic ' + ["#{proxy_user}:#{proxy_pass}"].pack('m0')
[291] Fix | Delete
end
[292] Fix | Delete
end
[293] Fix | Delete
[294] Fix | Delete
http = proxy ? klass.new(target_host, target_port) : klass.new(target_host, target_port, nil)
[295] Fix | Delete
if target.class == URI::HTTPS
[296] Fix | Delete
require 'net/https'
[297] Fix | Delete
http.use_ssl = true
[298] Fix | Delete
http.verify_mode = options[:ssl_verify_mode] || OpenSSL::SSL::VERIFY_PEER
[299] Fix | Delete
store = OpenSSL::X509::Store.new
[300] Fix | Delete
if options[:ssl_ca_cert]
[301] Fix | Delete
Array(options[:ssl_ca_cert]).each do |cert|
[302] Fix | Delete
if File.directory? cert
[303] Fix | Delete
store.add_path cert
[304] Fix | Delete
else
[305] Fix | Delete
store.add_file cert
[306] Fix | Delete
end
[307] Fix | Delete
end
[308] Fix | Delete
else
[309] Fix | Delete
store.set_default_paths
[310] Fix | Delete
end
[311] Fix | Delete
http.cert_store = store
[312] Fix | Delete
end
[313] Fix | Delete
if options.include? :read_timeout
[314] Fix | Delete
http.read_timeout = options[:read_timeout]
[315] Fix | Delete
end
[316] Fix | Delete
if options.include? :open_timeout
[317] Fix | Delete
http.open_timeout = options[:open_timeout]
[318] Fix | Delete
end
[319] Fix | Delete
[320] Fix | Delete
resp = nil
[321] Fix | Delete
http.start {
[322] Fix | Delete
req = Net::HTTP::Get.new(request_uri, header)
[323] Fix | Delete
if options.include? :http_basic_authentication
[324] Fix | Delete
user, pass = options[:http_basic_authentication]
[325] Fix | Delete
req.basic_auth user, pass
[326] Fix | Delete
end
[327] Fix | Delete
http.request(req) {|response|
[328] Fix | Delete
resp = response
[329] Fix | Delete
if options[:content_length_proc] && Net::HTTPSuccess === resp
[330] Fix | Delete
if resp.key?('Content-Length')
[331] Fix | Delete
options[:content_length_proc].call(resp['Content-Length'].to_i)
[332] Fix | Delete
else
[333] Fix | Delete
options[:content_length_proc].call(nil)
[334] Fix | Delete
end
[335] Fix | Delete
end
[336] Fix | Delete
resp.read_body {|str|
[337] Fix | Delete
buf << str
[338] Fix | Delete
if options[:progress_proc] && Net::HTTPSuccess === resp
[339] Fix | Delete
options[:progress_proc].call(buf.size)
[340] Fix | Delete
end
[341] Fix | Delete
str.clear
[342] Fix | Delete
}
[343] Fix | Delete
}
[344] Fix | Delete
}
[345] Fix | Delete
io = buf.io
[346] Fix | Delete
io.rewind
[347] Fix | Delete
io.status = [resp.code, resp.message]
[348] Fix | Delete
resp.each_name {|name| buf.io.meta_add_field2 name, resp.get_fields(name) }
[349] Fix | Delete
case resp
[350] Fix | Delete
when Net::HTTPSuccess
[351] Fix | Delete
when Net::HTTPMovedPermanently, # 301
[352] Fix | Delete
Net::HTTPFound, # 302
[353] Fix | Delete
Net::HTTPSeeOther, # 303
[354] Fix | Delete
Net::HTTPTemporaryRedirect # 307
[355] Fix | Delete
begin
[356] Fix | Delete
loc_uri = URI.parse(resp['location'])
[357] Fix | Delete
rescue URI::InvalidURIError
[358] Fix | Delete
raise OpenURI::HTTPError.new(io.status.join(' ') + ' (Invalid Location URI)', io)
[359] Fix | Delete
end
[360] Fix | Delete
throw :open_uri_redirect, loc_uri
[361] Fix | Delete
else
[362] Fix | Delete
raise OpenURI::HTTPError.new(io.status.join(' '), io)
[363] Fix | Delete
end
[364] Fix | Delete
end
[365] Fix | Delete
[366] Fix | Delete
class HTTPError < StandardError
[367] Fix | Delete
def initialize(message, io)
[368] Fix | Delete
super(message)
[369] Fix | Delete
@io = io
[370] Fix | Delete
end
[371] Fix | Delete
attr_reader :io
[372] Fix | Delete
end
[373] Fix | Delete
[374] Fix | Delete
# Raised on redirection,
[375] Fix | Delete
# only occurs when +redirect+ option for HTTP is +false+.
[376] Fix | Delete
class HTTPRedirect < HTTPError
[377] Fix | Delete
def initialize(message, io, uri)
[378] Fix | Delete
super(message, io)
[379] Fix | Delete
@uri = uri
[380] Fix | Delete
end
[381] Fix | Delete
attr_reader :uri
[382] Fix | Delete
end
[383] Fix | Delete
[384] Fix | Delete
class Buffer # :nodoc: all
[385] Fix | Delete
def initialize
[386] Fix | Delete
@io = StringIO.new
[387] Fix | Delete
@size = 0
[388] Fix | Delete
end
[389] Fix | Delete
attr_reader :size
[390] Fix | Delete
[391] Fix | Delete
StringMax = 10240
[392] Fix | Delete
def <<(str)
[393] Fix | Delete
@io << str
[394] Fix | Delete
@size += str.length
[395] Fix | Delete
if StringIO === @io && StringMax < @size
[396] Fix | Delete
require 'tempfile'
[397] Fix | Delete
io = Tempfile.new('open-uri')
[398] Fix | Delete
io.binmode
[399] Fix | Delete
Meta.init io, @io if Meta === @io
[400] Fix | Delete
io << @io.string
[401] Fix | Delete
@io = io
[402] Fix | Delete
end
[403] Fix | Delete
end
[404] Fix | Delete
[405] Fix | Delete
def io
[406] Fix | Delete
Meta.init @io unless Meta === @io
[407] Fix | Delete
@io
[408] Fix | Delete
end
[409] Fix | Delete
end
[410] Fix | Delete
[411] Fix | Delete
# Mixin for holding meta-information.
[412] Fix | Delete
module Meta
[413] Fix | Delete
def Meta.init(obj, src=nil) # :nodoc:
[414] Fix | Delete
obj.extend Meta
[415] Fix | Delete
obj.instance_eval {
[416] Fix | Delete
@base_uri = nil
[417] Fix | Delete
@meta = {} # name to string. legacy.
[418] Fix | Delete
@metas = {} # name to array of strings.
[419] Fix | Delete
}
[420] Fix | Delete
if src
[421] Fix | Delete
obj.status = src.status
[422] Fix | Delete
obj.base_uri = src.base_uri
[423] Fix | Delete
src.metas.each {|name, values|
[424] Fix | Delete
obj.meta_add_field2(name, values)
[425] Fix | Delete
}
[426] Fix | Delete
end
[427] Fix | Delete
end
[428] Fix | Delete
[429] Fix | Delete
# returns an Array that consists of status code and message.
[430] Fix | Delete
attr_accessor :status
[431] Fix | Delete
[432] Fix | Delete
# returns a URI that is the base of relative URIs in the data.
[433] Fix | Delete
# It may differ from the URI supplied by a user due to redirection.
[434] Fix | Delete
attr_accessor :base_uri
[435] Fix | Delete
[436] Fix | Delete
# returns a Hash that represents header fields.
[437] Fix | Delete
# The Hash keys are downcased for canonicalization.
[438] Fix | Delete
# The Hash values are a field body.
[439] Fix | Delete
# If there are multiple field with same field name,
[440] Fix | Delete
# the field values are concatenated with a comma.
[441] Fix | Delete
attr_reader :meta
[442] Fix | Delete
[443] Fix | Delete
# returns a Hash that represents header fields.
[444] Fix | Delete
# The Hash keys are downcased for canonicalization.
[445] Fix | Delete
# The Hash value are an array of field values.
[446] Fix | Delete
attr_reader :metas
[447] Fix | Delete
[448] Fix | Delete
def meta_setup_encoding # :nodoc:
[449] Fix | Delete
charset = self.charset
[450] Fix | Delete
enc = nil
[451] Fix | Delete
if charset
[452] Fix | Delete
begin
[453] Fix | Delete
enc = Encoding.find(charset)
[454] Fix | Delete
rescue ArgumentError
[455] Fix | Delete
end
[456] Fix | Delete
end
[457] Fix | Delete
enc = Encoding::ASCII_8BIT unless enc
[458] Fix | Delete
if self.respond_to? :force_encoding
[459] Fix | Delete
self.force_encoding(enc)
[460] Fix | Delete
elsif self.respond_to? :string
[461] Fix | Delete
self.string.force_encoding(enc)
[462] Fix | Delete
else # Tempfile
[463] Fix | Delete
self.set_encoding enc
[464] Fix | Delete
end
[465] Fix | Delete
end
[466] Fix | Delete
[467] Fix | Delete
def meta_add_field2(name, values) # :nodoc:
[468] Fix | Delete
name = name.downcase
[469] Fix | Delete
@metas[name] = values
[470] Fix | Delete
@meta[name] = values.join(', ')
[471] Fix | Delete
meta_setup_encoding if name == 'content-type'
[472] Fix | Delete
end
[473] Fix | Delete
[474] Fix | Delete
def meta_add_field(name, value) # :nodoc:
[475] Fix | Delete
meta_add_field2(name, [value])
[476] Fix | Delete
end
[477] Fix | Delete
[478] Fix | Delete
# returns a Time that represents the Last-Modified field.
[479] Fix | Delete
def last_modified
[480] Fix | Delete
if vs = @metas['last-modified']
[481] Fix | Delete
v = vs.join(', ')
[482] Fix | Delete
Time.httpdate(v)
[483] Fix | Delete
else
[484] Fix | Delete
nil
[485] Fix | Delete
end
[486] Fix | Delete
end
[487] Fix | Delete
[488] Fix | Delete
# :stopdoc:
[489] Fix | Delete
RE_LWS = /[\r\n\t ]+/n
[490] Fix | Delete
RE_TOKEN = %r{[^\x00- ()<>@,;:\\"/\[\]?={}\x7f]+}n
[491] Fix | Delete
RE_QUOTED_STRING = %r{"(?:[\r\n\t !#-\[\]-~\x80-\xff]|\\[\x00-\x7f])*"}n
[492] Fix | Delete
RE_PARAMETERS = %r{(?:;#{RE_LWS}?#{RE_TOKEN}#{RE_LWS}?=#{RE_LWS}?(?:#{RE_TOKEN}|#{RE_QUOTED_STRING})#{RE_LWS}?)*}n
[493] Fix | Delete
# :startdoc:
[494] Fix | Delete
[495] Fix | Delete
def content_type_parse # :nodoc:
[496] Fix | Delete
vs = @metas['content-type']
[497] Fix | Delete
# The last (?:;#{RE_LWS}?)? matches extra ";" which violates RFC2045.
[498] Fix | Delete
if vs && %r{\A#{RE_LWS}?(#{RE_TOKEN})#{RE_LWS}?/(#{RE_TOKEN})#{RE_LWS}?(#{RE_PARAMETERS})(?:;#{RE_LWS}?)?\z}no =~ vs.join(', ')
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function