gpt4 book ai didi

ruby - 具有基本身份验证和 gzip 的 em-http 流挂起

转载 作者:数据小太阳 更新时间:2023-10-29 07:07:35 26 4
gpt4 key购买 nike

我正在尝试使用 Gnip PowerTrack API,这需要我使用基本身份验证连接到 JSON 的 HTTPS 流。我觉得这应该是相当微不足道的,所以我希望一些比我聪明的 ruby​​ist 可以指出我明显的错误。

这是我的 ruby​​ 1.9.3 代码的相关部分:

require 'eventmachine'
require 'em-http'
require 'json'

usage = "#{$0} <user> <password>"
abort usage unless user = ARGV.shift
abort usage unless password = ARGV.shift
GNIP_STREAMING_URL = 'https://stream.gnip.com:443/foo/bar/prod.json'

http = EM::HttpRequest.new(GNIP_STREAMING_URL)
EventMachine.run do
s = http.get(:head => { 'Authorization' => [user, password], 'accept' => 'application/json', 'Accept-Encoding' => 'gzip,deflate' }, :keepalive => true, :connect_timeout => 0, :inactivity_timeout => 0)

buffer = ""
s.stream do |chunk|
buffer << chunk
while line = buffer.slice!(/.+\r?\n/)
puts JSON.parse(line)
end
end
end

流连接(我的 Gnip 仪表板报告连接)但随后只是缓冲并且从不输出任何东西。事实上,它似乎从未进入 s.stream do.. block 。请注意,这是一个 GZip 编码流。

请注意,这是可行的:

curl --compressed -uusername $GNIP_STREAMING_URL

编辑:我确定这有点含蓄,但我不能给出任何登录凭据或实际 URL,所以不要问 ;)

编辑 #2:如果我能弄清楚如何对 URL 的凭据进行编码,yajl-ruby 可能会起作用(简单的 URL 编码似乎不起作用,因为我无法通过 Gnip 进行身份验证)。

编辑 #3:@rweald 发现 em-http 不支持流式 gzip,我在这里创建了一个 GitHub 问题。

编辑 #4:我已经在 em-http-request 中 fork 并修复了这个问题,你可以指向 my fork如果你想以这种方式使用 em-http。该补丁已合并到维护者的存储库中,并将在下一个版本中运行。

编辑 #5:我的修复已在 em-http-request 1.0.3 中发布,因此这应该不再是一个问题。

最佳答案

问题出在 em-http-request 中。如果你看 https://github.com/igrigorik/em-http-request/blob/master/lib/em-http/decoders.rb

你会注意到 GZIP 解压器不能做流式解压:( https://github.com/igrigorik/em-http-request/blob/master/lib/em-http/decoders.rb#L100

如果您希望能够使用 em-http-request 读取流,则需要修复底层流 gzip 问题

关于ruby - 具有基本身份验证和 gzip 的 em-http 流挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9346848/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com