Skip to content

Instantly share code, notes, and snippets.

@olleolleolle
Created April 10, 2017 13:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olleolleolle/987c83fa880a3f7fe7f7172dff11c122 to your computer and use it in GitHub Desktop.
Save olleolleolle/987c83fa880a3f7fe7f7172dff11c122 to your computer and use it in GitHub Desktop.
263,264c263
< # Net::HTTP.start(uri.host, uri.port,
< # :use_ssl => uri.scheme == 'https') do |http|
---
> # Net::HTTP.start(uri.host, uri.port, :use_ssl => true) do |http|
266d264
< #
269a268,274
> # Or if you simply want to make a GET request, you may pass in an URI
> # object that has a HTTPS URL. Net::HTTP automatically turn on TLS
> # verification if the URI object has a 'https' URI scheme.
> #
> # uri = URI('https://example.com/')
> # Net::HTTP.get(uri) # => String
> #
271c276
< # HTTPS. This is no longer true.
---
> # HTTPS. This is no longer true.
372a378
> # HTTPUnavailableForLegalReasons:: 451
485a492,509
> # Posts data to the specified URI object.
> #
> # Example:
> #
> # require 'net/http'
> # require 'uri'
> #
> # Net::HTTP.post URI('http://www.example.com/api/search'),
> # { "q" => "ruby", "max" => "50" }.to_json,
> # "Content-Type" => "application/json"
> #
> def HTTP.post(url, data, header = nil)
> start(url.hostname, url.port,
> :use_ssl => url.scheme == 'https' ) {|http|
> http.post(url.path, data, header)
> }
> end
>
888a913,928
> if proxy?
> plain_sock = BufferedIO.new(s, read_timeout: @read_timeout,
> continue_timeout: @continue_timeout,
> debug_output: @debug_output)
> buf = "CONNECT #{@address}:#{@port} HTTP/#{HTTPVersion}\r\n"
> buf << "Host: #{@address}:#{@port}\r\n"
> if proxy_user
> credential = ["#{proxy_user}:#{proxy_pass}"].pack('m0')
> buf << "Proxy-Authorization: Basic #{credential}\r\n"
> end
> buf << "\r\n"
> plain_sock.write(buf)
> HTTPResponse.read_new(plain_sock).value
> # assuming nothing left in buffers after successful CONNECT response
> end
>
902,950c942,950
< D "SSL established"
< end
< @socket = BufferedIO.new(s)
< @socket.read_timeout = @read_timeout
< @socket.continue_timeout = @continue_timeout
< @socket.debug_output = @debug_output
< if use_ssl?
< begin
< if proxy?
< buf = "CONNECT #{@address}:#{@port} HTTP/#{HTTPVersion}\r\n"
< buf << "Host: #{@address}:#{@port}\r\n"
< if proxy_user
< credential = ["#{proxy_user}:#{proxy_pass}"].pack('m')
< credential.delete!("\r\n")
< buf << "Proxy-Authorization: Basic #{credential}\r\n"
< end
< buf << "\r\n"
< @socket.write(buf)
< HTTPResponse.read_new(@socket).value
< end
< # Server Name Indication (SNI) RFC 3546
< s.hostname = @address if s.respond_to? :hostname=
< if @ssl_session and
< Process.clock_gettime(Process::CLOCK_REALTIME) < @ssl_session.time.to_f + @ssl_session.timeout
< s.session = @ssl_session if @ssl_session
< end
< if timeout = @open_timeout
< while true
< raise Net::OpenTimeout if timeout <= 0
< start = Process.clock_gettime Process::CLOCK_MONOTONIC
< # to_io is required because SSLSocket doesn't have wait_readable yet
< case s.connect_nonblock(exception: false)
< when :wait_readable; s.to_io.wait_readable(timeout)
< when :wait_writable; s.to_io.wait_writable(timeout)
< else; break
< end
< timeout -= Process.clock_gettime(Process::CLOCK_MONOTONIC) - start
< end
< else
< s.connect
< end
< if @ssl_context.verify_mode != OpenSSL::SSL::VERIFY_NONE
< s.post_connection_check(@address)
< end
< @ssl_session = s.session
< rescue => exception
< D "Conn close because of connect error #{exception}"
< @socket.close if @socket and not @socket.closed?
< raise exception
---
> # Server Name Indication (SNI) RFC 3546
> s.hostname = @address if s.respond_to? :hostname=
> if @ssl_session and
> Process.clock_gettime(Process::CLOCK_REALTIME) < @ssl_session.time.to_f + @ssl_session.timeout
> s.session = @ssl_session if @ssl_session
> end
> ssl_socket_connect(s, @open_timeout)
> if @ssl_context.verify_mode != OpenSSL::SSL::VERIFY_NONE
> s.post_connection_check(@address)
951a952,953
> @ssl_session = s.session
> D "SSL established"
952a955,957
> @socket = BufferedIO.new(s, read_timeout: @read_timeout,
> continue_timeout: @continue_timeout,
> debug_output: @debug_output)
953a959,964
> rescue => exception
> if s
> D "Conn close because of connect error #{exception}"
> s.close
> end
> raise
970c981
< @socket.close if @socket and not @socket.closed?
---
> @socket.close if @socket
1037,1041c1048
< !!if @proxy_from_env then
< proxy_uri
< else
< @proxy_address
< end
---
> !!(@proxy_from_env ? proxy_uri : @proxy_address)
1050a1058
> return if @proxy_uri == false
1053c1061,1062
< ).find_proxy
---
> ).find_proxy || false
> @proxy_uri || nil
1457c1466
< @socket.close if @socket and not @socket.closed?
---
> @socket.close if @socket
1462c1471
< @socket.close if @socket and not @socket.closed?
---
> @socket.close if @socket
1470c1479
< @socket.close if @socket and not @socket.closed?
---
> @socket.close if @socket
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment