我想通过 Sinatra 应用程序代理远程文件。这需要将带有标头的 HTTP 响应从远程源流式传输回客户端,但我无法弄清楚如何在Net::HTTP#get_response
.
例如,这不会设置响应标头:
get '/file' do
stream do |out|
uri = URI("http://manuals.info.apple.com/en/ipad_user_guide.pdf")
Net::HTTP.get_response(uri) do |file|
headers 'Content-Type' => file.header['Content-Type']
file.read_body { |chunk| out << chunk }
end
end
end
这会导致错误Net::HTTPOK#read_body called twice (IOError)
::
get '/file' do
response = nil
uri = URI("http://manuals.info.apple.com/en/ipad_user_guide.pdf")
Net::HTTP.get_response(uri) do |file|
headers 'Content-Type' => file.header['Content-Type']
response = stream do |out|
file.read_body { |chunk| out << chunk }
end
end
response
end