title: Sinatra streaming response with headers
body: <p>I want to proxy remote files through a Sinatra application. This requires streaming an HTTP response with headers from a remote source back to the client, but I can't figure out how to set the headers of the response while using the streaming API inside the block provided by <code>Net::HTTP#get_response</code>.</p>
  
  <p>For example, this will not set response headers:</p>
  
  <pre><code>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' =&gt; file.header['Content-Type']
  
        file.read_body { |chunk| out &lt;&lt; chunk }
      end
    end
  end
  </code></pre>
  
  <p>And this results in the error: <code>Net::HTTPOK#read_body called twice (IOError)</code>:</p>
  
  <pre><code>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' =&gt; file.header['Content-Type']
  
      response = stream do |out|
        file.read_body { |chunk| out &lt;&lt; chunk }
      end
    end
    response
  end
  </code></pre>