2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2012-05-22 16:36:21 -04:00
|
|
|
# HTTPGenericRequest is the parent of the HTTPRequest class.
|
|
|
|
# Do not use this directly; use a subclass of HTTPRequest.
|
|
|
|
#
|
|
|
|
# Mixes in the HTTPHeader module to provide easier access to HTTP headers.
|
|
|
|
#
|
|
|
|
class Net::HTTPGenericRequest
|
|
|
|
|
|
|
|
include Net::HTTPHeader
|
|
|
|
|
2012-12-21 15:36:07 -05:00
|
|
|
def initialize(m, reqbody, resbody, uri_or_path, initheader = nil)
|
2012-05-22 16:36:21 -04:00
|
|
|
@method = m
|
|
|
|
@request_has_body = reqbody
|
|
|
|
@response_has_body = resbody
|
2012-12-21 15:36:07 -05:00
|
|
|
|
|
|
|
if URI === uri_or_path then
|
2018-03-08 11:07:54 -05:00
|
|
|
raise ArgumentError, "not an HTTP URI" unless URI::HTTP === uri_or_path
|
|
|
|
raise ArgumentError, "no host component for URI" unless uri_or_path.hostname
|
2012-12-21 15:36:07 -05:00
|
|
|
@uri = uri_or_path.dup
|
2014-08-05 15:09:43 -04:00
|
|
|
host = @uri.hostname.dup
|
|
|
|
host << ":".freeze << @uri.port.to_s if @uri.port != @uri.default_port
|
|
|
|
@path = uri_or_path.request_uri
|
|
|
|
raise ArgumentError, "no HTTP request path given" unless @path
|
2012-12-21 15:36:07 -05:00
|
|
|
else
|
|
|
|
@uri = nil
|
|
|
|
host = nil
|
2014-08-05 15:09:43 -04:00
|
|
|
raise ArgumentError, "no HTTP request path given" unless uri_or_path
|
|
|
|
raise ArgumentError, "HTTP request path is empty" if uri_or_path.empty?
|
|
|
|
@path = uri_or_path.dup
|
2012-12-21 15:36:07 -05:00
|
|
|
end
|
|
|
|
|
2013-02-13 20:08:19 -05:00
|
|
|
@decode_content = false
|
|
|
|
|
2012-07-19 18:43:38 -04:00
|
|
|
if @response_has_body and Net::HTTP::HAVE_ZLIB then
|
|
|
|
if !initheader ||
|
|
|
|
!initheader.keys.any? { |k|
|
|
|
|
%w[accept-encoding range].include? k.downcase
|
|
|
|
} then
|
2013-02-13 20:08:19 -05:00
|
|
|
@decode_content = true
|
2012-07-19 18:43:38 -04:00
|
|
|
initheader = initheader ? initheader.dup : {}
|
|
|
|
initheader["accept-encoding"] =
|
|
|
|
"gzip;q=1.0,deflate;q=0.6,identity;q=0.3"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-05-22 16:36:21 -04:00
|
|
|
initialize_http_header initheader
|
|
|
|
self['Accept'] ||= '*/*'
|
|
|
|
self['User-Agent'] ||= 'Ruby'
|
2014-08-05 15:09:43 -04:00
|
|
|
self['Host'] ||= host if host
|
2012-05-22 16:36:21 -04:00
|
|
|
@body = nil
|
|
|
|
@body_stream = nil
|
|
|
|
@body_data = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :method
|
|
|
|
attr_reader :path
|
2012-12-21 15:36:07 -05:00
|
|
|
attr_reader :uri
|
2012-05-22 16:36:21 -04:00
|
|
|
|
2013-02-13 20:08:19 -05:00
|
|
|
# Automatically set to false if the user sets the Accept-Encoding header.
|
|
|
|
# This indicates they wish to handle Content-encoding in responses
|
|
|
|
# themselves.
|
|
|
|
attr_reader :decode_content
|
|
|
|
|
2012-05-22 16:36:21 -04:00
|
|
|
def inspect
|
|
|
|
"\#<#{self.class} #{@method}>"
|
|
|
|
end
|
|
|
|
|
2013-02-13 20:08:19 -05:00
|
|
|
##
|
|
|
|
# Don't automatically decode response content-encoding if the user indicates
|
|
|
|
# they want to handle it.
|
|
|
|
|
|
|
|
def []=(key, val) # :nodoc:
|
|
|
|
@decode_content = false if key.downcase == 'accept-encoding'
|
|
|
|
|
|
|
|
super key, val
|
|
|
|
end
|
|
|
|
|
2012-05-22 16:36:21 -04:00
|
|
|
def request_body_permitted?
|
|
|
|
@request_has_body
|
|
|
|
end
|
|
|
|
|
|
|
|
def response_body_permitted?
|
|
|
|
@response_has_body
|
|
|
|
end
|
|
|
|
|
|
|
|
def body_exist?
|
2017-12-12 06:56:25 -05:00
|
|
|
warn "Net::HTTPRequest#body_exist? is obsolete; use response_body_permitted?", uplevel: 1 if $VERBOSE
|
2012-05-22 16:36:21 -04:00
|
|
|
response_body_permitted?
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :body
|
|
|
|
|
|
|
|
def body=(str)
|
|
|
|
@body = str
|
|
|
|
@body_stream = nil
|
|
|
|
@body_data = nil
|
|
|
|
str
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :body_stream
|
|
|
|
|
|
|
|
def body_stream=(input)
|
|
|
|
@body = nil
|
|
|
|
@body_stream = input
|
|
|
|
@body_data = nil
|
|
|
|
input
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_body_internal(str) #:nodoc: internal use only
|
|
|
|
raise ArgumentError, "both of body argument and HTTPRequest#body set" if str and (@body or @body_stream)
|
|
|
|
self.body = str if str
|
2012-12-23 14:58:44 -05:00
|
|
|
if @body.nil? && @body_stream.nil? && @body_data.nil? && request_body_permitted?
|
|
|
|
self.body = ''
|
|
|
|
end
|
2012-05-22 16:36:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# write
|
|
|
|
#
|
|
|
|
|
|
|
|
def exec(sock, ver, path) #:nodoc: internal use only
|
|
|
|
if @body
|
|
|
|
send_request_with_body sock, ver, path, @body
|
|
|
|
elsif @body_stream
|
|
|
|
send_request_with_body_stream sock, ver, path, @body_stream
|
|
|
|
elsif @body_data
|
|
|
|
send_request_with_body_data sock, ver, path, @body_data
|
|
|
|
else
|
|
|
|
write_header sock, ver, path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-05 15:10:05 -04:00
|
|
|
def update_uri(addr, port, ssl) # :nodoc: internal use only
|
|
|
|
# reflect the connection and @path to @uri
|
2012-12-21 15:36:07 -05:00
|
|
|
return unless @uri
|
|
|
|
|
2014-08-05 15:10:05 -04:00
|
|
|
if ssl
|
|
|
|
scheme = 'https'.freeze
|
|
|
|
klass = URI::HTTPS
|
|
|
|
else
|
|
|
|
scheme = 'http'.freeze
|
|
|
|
klass = URI::HTTP
|
|
|
|
end
|
2012-12-21 15:36:07 -05:00
|
|
|
|
2014-08-05 15:10:23 -04:00
|
|
|
if host = self['host']
|
2014-08-05 15:10:05 -04:00
|
|
|
host.sub!(/:.*/s, ''.freeze)
|
2014-08-05 15:10:23 -04:00
|
|
|
elsif host = @uri.host
|
2014-08-05 15:10:05 -04:00
|
|
|
else
|
|
|
|
host = addr
|
|
|
|
end
|
2012-12-21 15:36:07 -05:00
|
|
|
# convert the class of the URI
|
2014-08-05 15:10:05 -04:00
|
|
|
if @uri.is_a?(klass)
|
|
|
|
@uri.host = host
|
|
|
|
@uri.port = port
|
|
|
|
else
|
|
|
|
@uri = klass.new(
|
|
|
|
scheme, @uri.userinfo,
|
|
|
|
host, port, nil,
|
|
|
|
@uri.path, nil, @uri.query, nil)
|
2012-12-21 15:36:07 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-05-22 16:36:21 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
class Chunker #:nodoc:
|
|
|
|
def initialize(sock)
|
|
|
|
@sock = sock
|
|
|
|
@prev = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def write(buf)
|
|
|
|
# avoid memcpy() of buf, buf can huge and eat memory bandwidth
|
2018-01-13 21:44:53 -05:00
|
|
|
rv = buf.bytesize
|
|
|
|
@sock.write("#{rv.to_s(16)}\r\n", buf, "\r\n")
|
2012-05-22 16:36:21 -04:00
|
|
|
rv
|
|
|
|
end
|
|
|
|
|
|
|
|
def finish
|
|
|
|
@sock.write("0\r\n\r\n")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def send_request_with_body(sock, ver, path, body)
|
|
|
|
self.content_length = body.bytesize
|
|
|
|
delete 'Transfer-Encoding'
|
|
|
|
supply_default_content_type
|
|
|
|
write_header sock, ver, path
|
|
|
|
wait_for_continue sock, ver if sock.continue_timeout
|
|
|
|
sock.write body
|
|
|
|
end
|
|
|
|
|
|
|
|
def send_request_with_body_stream(sock, ver, path, f)
|
|
|
|
unless content_length() or chunked?
|
|
|
|
raise ArgumentError,
|
|
|
|
"Content-Length not given and Transfer-Encoding is not `chunked'"
|
|
|
|
end
|
|
|
|
supply_default_content_type
|
|
|
|
write_header sock, ver, path
|
|
|
|
wait_for_continue sock, ver if sock.continue_timeout
|
|
|
|
if chunked?
|
|
|
|
chunker = Chunker.new(sock)
|
|
|
|
IO.copy_stream(f, chunker)
|
|
|
|
chunker.finish
|
|
|
|
else
|
|
|
|
# copy_stream can sendfile() to sock.io unless we use SSL.
|
|
|
|
# If sock.io is an SSLSocket, copy_stream will hit SSL_write()
|
|
|
|
IO.copy_stream(f, sock.io)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def send_request_with_body_data(sock, ver, path, params)
|
|
|
|
if /\Amultipart\/form-data\z/i !~ self.content_type
|
|
|
|
self.content_type = 'application/x-www-form-urlencoded'
|
|
|
|
return send_request_with_body(sock, ver, path, URI.encode_www_form(params))
|
|
|
|
end
|
|
|
|
|
|
|
|
opt = @form_option.dup
|
|
|
|
require 'securerandom' unless defined?(SecureRandom)
|
|
|
|
opt[:boundary] ||= SecureRandom.urlsafe_base64(40)
|
|
|
|
self.set_content_type(self.content_type, boundary: opt[:boundary])
|
|
|
|
if chunked?
|
|
|
|
write_header sock, ver, path
|
|
|
|
encode_multipart_form_data(sock, params, opt)
|
|
|
|
else
|
|
|
|
require 'tempfile'
|
|
|
|
file = Tempfile.new('multipart')
|
|
|
|
file.binmode
|
|
|
|
encode_multipart_form_data(file, params, opt)
|
|
|
|
file.rewind
|
|
|
|
self.content_length = file.size
|
|
|
|
write_header sock, ver, path
|
|
|
|
IO.copy_stream(file, sock)
|
2013-01-29 07:41:56 -05:00
|
|
|
file.close(true)
|
2012-05-22 16:36:21 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def encode_multipart_form_data(out, params, opt)
|
|
|
|
charset = opt[:charset]
|
|
|
|
boundary = opt[:boundary]
|
|
|
|
require 'securerandom' unless defined?(SecureRandom)
|
|
|
|
boundary ||= SecureRandom.urlsafe_base64(40)
|
|
|
|
chunked_p = chunked?
|
|
|
|
|
|
|
|
buf = ''
|
|
|
|
params.each do |key, value, h={}|
|
|
|
|
key = quote_string(key, charset)
|
|
|
|
filename =
|
|
|
|
h.key?(:filename) ? h[:filename] :
|
|
|
|
value.respond_to?(:to_path) ? File.basename(value.to_path) :
|
|
|
|
nil
|
|
|
|
|
|
|
|
buf << "--#{boundary}\r\n"
|
|
|
|
if filename
|
|
|
|
filename = quote_string(filename, charset)
|
|
|
|
type = h[:content_type] || 'application/octet-stream'
|
2012-10-25 10:06:31 -04:00
|
|
|
buf << "Content-Disposition: form-data; " \
|
|
|
|
"name=\"#{key}\"; filename=\"#{filename}\"\r\n" \
|
|
|
|
"Content-Type: #{type}\r\n\r\n"
|
2012-05-22 16:36:21 -04:00
|
|
|
if !out.respond_to?(:write) || !value.respond_to?(:read)
|
|
|
|
# if +out+ is not an IO or +value+ is not an IO
|
|
|
|
buf << (value.respond_to?(:read) ? value.read : value)
|
|
|
|
elsif value.respond_to?(:size) && chunked_p
|
|
|
|
# if +out+ is an IO and +value+ is a File, use IO.copy_stream
|
|
|
|
flush_buffer(out, buf, chunked_p)
|
|
|
|
out << "%x\r\n" % value.size if chunked_p
|
|
|
|
IO.copy_stream(value, out)
|
|
|
|
out << "\r\n" if chunked_p
|
|
|
|
else
|
|
|
|
# +out+ is an IO, and +value+ is not a File but an IO
|
|
|
|
flush_buffer(out, buf, chunked_p)
|
|
|
|
1 while flush_buffer(out, value.read(4096), chunked_p)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
# non-file field:
|
|
|
|
# HTML5 says, "The parts of the generated multipart/form-data
|
|
|
|
# resource that correspond to non-file fields must not have a
|
|
|
|
# Content-Type header specified."
|
|
|
|
buf << "Content-Disposition: form-data; name=\"#{key}\"\r\n\r\n"
|
|
|
|
buf << (value.respond_to?(:read) ? value.read : value)
|
|
|
|
end
|
|
|
|
buf << "\r\n"
|
|
|
|
end
|
|
|
|
buf << "--#{boundary}--\r\n"
|
|
|
|
flush_buffer(out, buf, chunked_p)
|
|
|
|
out << "0\r\n\r\n" if chunked_p
|
|
|
|
end
|
|
|
|
|
|
|
|
def quote_string(str, charset)
|
|
|
|
str = str.encode(charset, fallback:->(c){'&#%d;'%c.encode("UTF-8").ord}) if charset
|
2013-03-22 07:57:51 -04:00
|
|
|
str.gsub(/[\\"]/, '\\\\\&')
|
2012-05-22 16:36:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def flush_buffer(out, buf, chunked_p)
|
|
|
|
return unless buf
|
|
|
|
out << "%x\r\n"%buf.bytesize if chunked_p
|
|
|
|
out << buf
|
|
|
|
out << "\r\n" if chunked_p
|
|
|
|
buf.clear
|
|
|
|
end
|
|
|
|
|
|
|
|
def supply_default_content_type
|
|
|
|
return if content_type()
|
2017-12-12 06:56:25 -05:00
|
|
|
warn 'net/http: Content-Type did not set; using application/x-www-form-urlencoded', uplevel: 1 if $VERBOSE
|
2012-05-22 16:36:21 -04:00
|
|
|
set_content_type 'application/x-www-form-urlencoded'
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Waits up to the continue timeout for a response from the server provided
|
|
|
|
# we're speaking HTTP 1.1 and are expecting a 100-continue response.
|
|
|
|
|
|
|
|
def wait_for_continue(sock, ver)
|
|
|
|
if ver >= '1.1' and @header['expect'] and
|
|
|
|
@header['expect'].include?('100-continue')
|
2015-04-15 16:11:23 -04:00
|
|
|
if sock.io.to_io.wait_readable(sock.continue_timeout)
|
2012-05-22 16:36:21 -04:00
|
|
|
res = Net::HTTPResponse.read_new(sock)
|
|
|
|
unless res.kind_of?(Net::HTTPContinue)
|
2013-02-13 20:08:19 -05:00
|
|
|
res.decode_content = @decode_content
|
2012-05-22 16:36:21 -04:00
|
|
|
throw :response, res
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def write_header(sock, ver, path)
|
2016-07-05 20:01:20 -04:00
|
|
|
reqline = "#{@method} #{path} HTTP/#{ver}"
|
|
|
|
if /[\r\n]/ =~ reqline
|
|
|
|
raise ArgumentError, "A Request-Line must not contain CR or LF"
|
|
|
|
end
|
|
|
|
buf = ""
|
|
|
|
buf << reqline << "\r\n"
|
2012-05-22 16:36:21 -04:00
|
|
|
each_capitalized do |k,v|
|
|
|
|
buf << "#{k}: #{v}\r\n"
|
|
|
|
end
|
|
|
|
buf << "\r\n"
|
|
|
|
sock.write buf
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|