I want to proxy deleted files through the Sinatra app. This requires streaming an HTTP response with headers from a remote source back to the client, but I cannot figure out how to configure response headers when using the streaming API inside the block provided Net::HTTP#get_response.
For example, this will not set response headers:
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
And this leads to an error 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
source
share