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