Class: Requests::Http2Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/requests/http2.rb

Constant Summary collapse

TYPE_DATA =
0x0
TYPE_HEADERS =
0x1
TYPE_PRIORITY =
0x2
TYPE_RST_STREAM =
0x3
TYPE_SETTINGS =
0x4
TYPE_PUSH_PROMISE =
0x5
TYPE_PING =
0x6
TYPE_GOAWAY =
0x7
TYPE_WINDOW_UPDATE =
0x8
TYPE_CONTINUATION =
0x9
FLAG_END_STREAM =
0x1
FLAG_ACK =
0x1
FLAG_END_HEADERS =
0x4
FLAG_PADDED =
0x8
FLAG_PRIORITY =
0x20
PREFACE =
"PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"
DEFAULT_WINDOW =
65_535

Instance Method Summary collapse

Constructor Details

#initialize(socket) ⇒ Http2Connection

Returns a new instance of Http2Connection.



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/requests/http2.rb', line 67

def initialize(socket)
  @socket = socket
  @next_stream_id = 1
  @encoder = HPACK::Encoder.new
  @decoder = HPACK::Decoder.new
  @send_window = DEFAULT_WINDOW
  @conn_recv_window = DEFAULT_WINDOW
  @peer_max_frame_size = 16_384
  @peer_initial_window = DEFAULT_WINDOW
  @streams = {}
  @closed = false
  handshake!
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/requests/http2.rb', line 80

def alive?
  !@closed && !@socket.closed?
end

#closeObject



83
84
85
86
87
88
89
90
# File 'lib/requests/http2.rb', line 83

def close
  write_frame(TYPE_GOAWAY, 0, 0, [0, 0].pack('NN')) if alive?
rescue StandardError
  nil
ensure
  @closed = true
  @socket.close unless @socket.closed?
end

#request(method, uri, header_list, body, io: nil, progress: nil) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/requests/http2.rb', line 91

def request(method, uri, header_list, body, io: nil, progress: nil)
  raise Requests::ConnectionError, 'http/2 connection is closed' unless alive?
  stream_id = @next_stream_id
  @next_stream_id += 2
  @streams[stream_id] = { send_window: @peer_initial_window, recv_window: DEFAULT_WINDOW }
  end_stream = body.nil? || body.to_s.empty?
  send_headers(stream_id, header_list, end_stream)
  send_body(stream_id, body) unless end_stream
  read_response(stream_id, io: io, progress: progress)
ensure
  @streams.delete(stream_id) if stream_id
end