2016-09-20 105 views
2

我試圖使用Ruby gem http-2向Google發送GET請求。Ruby HTTP2 GET請求

我從example代碼直接解除,稍作簡化它:

require 'http/2' 
require 'socket' 
require 'openssl' 
require 'uri' 

uri = URI.parse('http://www.google.com/') 
tcp = TCPSocket.new(uri.host, uri.port) 
sock = tcp 

conn = HTTP2::Client.new 
conn.on(:frame) do |bytes| 
    # puts "Sending bytes: #{bytes.unpack("H*").first}" 
    sock.print bytes 
    sock.flush 
end 
conn.on(:frame_sent) do |frame| 
    puts "Sent frame: #{frame.inspect}" 
end 
conn.on(:frame_received) do |frame| 
    puts "Received frame: #{frame.inspect}" 
end 

stream = conn.new_stream 

stream.on(:close) do 
    puts 'stream closed' 
    sock.close 
end 

stream.on(:half_close) do 
    puts 'closing client-end of the stream' 
end 

stream.on(:headers) do |h| 
    puts "response headers: #{h}" 
end 

stream.on(:data) do |d| 
    puts "response data chunk: <<#{d}>>" 
end 

head = { 
    ':scheme' => uri.scheme, 
    ':method' => 'GET', 
    ':path' => uri.path 
} 

puts 'Sending HTTP 2.0 request' 
stream.headers(head, end_stream: true) 

while !sock.closed? && !sock.eof? 
    data = sock.read_nonblock(1024) 
    # puts "Received bytes: #{data.unpack("H*").first}" 

    begin 
    conn << data 
    rescue => e 
    puts "#{e.class} exception: #{e.message} - closing socket." 
    e.backtrace.each { |l| puts "\t" + l } 
    sock.close 
    end 
end 

輸出是:

Sending HTTP 2.0 request 
Sent frame: {:type=>:settings, :stream=>0, :payload=>[[:settings_max_concurrent_streams, 100]]} 
Sent frame: {:type=>:headers, :flags=>[:end_headers, :end_stream], :payload=>[[":scheme", "http"], [":method", "GET"], [":path", "/"]], :stream=>1} 
closing client-end of the stream 

(注:你得到幾乎相同的輸出通過以上運行實際的示例文件,即ruby client.rb http://www.google.com/

爲什麼沒有顯示響應數據?

回答

5

像google.com這樣的公共服務器不支持明文形式的HTTP/2。

您正在嘗試連接到http://google.com,而您應該真的連接到https://google.com(請注意https方案)。

爲了做到這一點,如果http-2不適合您,您可能需要使用TLS封裝TCP套接字(例如,請參閱here)。

還要注意,HTTP/2需要強大的TLS密碼和ALPN,因此請確保您擁有OpenSSL的更新版本(至少1.0.2)。

假設的http-2作者是一個強大的HTTP/2支持者,我猜測,你唯一的問題是你的努力明文http而非https的事實,我希望TLS加密強度和ALPN採取照顧http-2圖書館。

+0

輝煌,謝謝。是的,圖書館,甚至我上面鏈接的例子都有加密所需的代碼。我只是不知道他們總是有必要的。 只是爲了讀者的緣故:我還必須添加回http標題,我錯誤地從上面鏈接的示例代碼中刪除(':authority'=> [uri.host,uri.port] .join (':'),'accept'=>'*/*',)。 –