iTranslated by AI
The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
👏
[Ruby] Sending HTTP/2 frames with TCPSocket
The script processed normally with just the PRI, but it froze when sending Settings frames, ACK frames, etc.
client.rb
require 'socket'
# https://gist.github.com/masakielastic/d1f589ba3aedcaa98dad4593004caf80
require './h2frames.rb'
# h2c test server
# https://zenn.dev/masakielastic/articles/530f9751697ed6
sock = TCPSocket.new 'localhost', 8000
sock.write(h2frames('pri'))
while !sock.closed? && !sock.eof?
data = sock.read_nonblock(128)
ret = data.unpack("H*").first
puts "hex: #{ret}"
puts "size: #{ret.size}"
end
sock.close
Checking with strace reveals that it stops at poll.
> strace ruby client.rb
writev(1, [{iov_base="size: 122", iov_len=9}, {iov_base="\n", iov_len=1}], 2size: 122
) = 10
read(5, 0x56087e2e6800, 8192) = -1 EAGAIN (Resource temporarily unavailable)
poll([{fd=5, events=POLLIN}], 1, -1
Discussion