Module: HTTP::Connection::Internals

Included in:
HTTP::Connection
Defined in:
lib/http/connection/internals.rb,
sig/http.rbs

Overview

Internal private methods for Connection

Defined Under Namespace

Modules: _InternalsHost

Instance Method Summary collapse

Instance Method Details

#body_framed?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Check if the response body has a known framing mechanism

Examples:

body_framed?

Returns:

  • (Boolean)


117
118
119
120
# File 'lib/http/connection/internals.rb', line 117

def body_framed?
  @parser.headers.include?(Headers::TRANSFER_ENCODING) ||
    @parser.headers.include?(Headers::CONTENT_LENGTH)
end

#flush_or_close_response(response) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Flush the response or close if the body exceeds the size limit

Parameters:



28
29
30
31
32
33
34
35
# File 'lib/http/connection/internals.rb', line 28

def flush_or_close_response(response)
  content_length = response.content_length
  if content_length && content_length > MAX_FLUSH_SIZE
    close
  else
    response.flush
  end
end

#flush_pending_responsevoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Flush the pending response body so the connection can be reused



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/http/connection/internals.rb', line 12

def flush_pending_response
  response = @pending_response
  unless response.respond_to?(:flush)
    close
    return
  end

  flush_or_close_response(response)
rescue
  close
end

#handle_proxy_connect_responsevoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Process the proxy connect response



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/http/connection/internals.rb', line 74

def handle_proxy_connect_response
  @proxy_response_headers = @parser.headers

  if @parser.status_code != 200
    @failed_proxy_connect = true
    return
  end

  @parser.reset
  @pending_response = false
end

#read_more(size) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Feeds some more data into parser

Parameters:

  • size (Integer)

Raises:



126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/http/connection/internals.rb', line 126

def read_more(size)
  return if @parser.finished?

  value = @socket.readpartial(size, @buffer)
  if value == :eof
    @parser << ""
    :eof
  elsif value
    @parser << value
  end
rescue IOError, SocketError, SystemCallError => e
  raise SocketReadError, "error reading from socket: #{e}", e.backtrace
end

#reset_timervoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Resets expiration of persistent connection



89
90
91
# File 'lib/http/connection/internals.rb', line 89

def reset_timer
  @conn_expires_at = Time.now + @keep_alive_timeout if @persistent
end

#send_proxy_connect_request(req) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Open tunnel through proxy

Parameters:



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/http/connection/internals.rb', line 57

def send_proxy_connect_request(req)
  return unless req.uri.https? && req.using_proxy?

  @pending_request = true

  req.connect_using_proxy @socket

  @pending_request  = false
  @pending_response = true

  read_headers!
  handle_proxy_connect_response
end

#set_keep_alivevoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Store keep-alive state from parser



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/http/connection/internals.rb', line 96

def set_keep_alive
  return @keep_alive = false unless @persistent

  @keep_alive =
    case @parser.http_version
    when HTTP_1_0 # HTTP/1.0 requires opt in for Keep Alive
      @parser.headers[Headers::CONNECTION] == KEEP_ALIVE
    when HTTP_1_1 # HTTP/1.1 is opt-out
      @parser.headers[Headers::CONNECTION] != CLOSE
    else # Anything else we assume doesn't support it
      false
    end
end

#start_tls(req, options) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Sets up SSL context and starts TLS if needed

Parameters:



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/http/connection/internals.rb', line 41

def start_tls(req, options)
  return unless req.uri.https? && !failed_proxy_connect?

  ssl_context = options.ssl_context

  unless ssl_context
    ssl_context = OpenSSL::SSL::SSLContext.new
    ssl_context.set_params(options.ssl || {})
  end

  @socket.start_tls(req.uri.host, options.ssl_socket_class, ssl_context)
end