app/controllers/concerns/streaming.rb
# Streams data to the client from a given URL. Request method, request
# headers, response code, and response headers are also set to match.
#
# @param [String] url The URL to stream data from.
Streams data to the client from a given URL. Request method, request
headers, response code, and response headers are also set to match.
Streams data to the client from a given URL . Request method request headers response code and response headers are also set to match .
def stream(url)
    uri = URI.parse(url)
    Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
      request_class = request.head? ? Net::HTTP::Head : Net::HTTP::Get
      outbound_request = request_class.new(uri.request_uri)
      PASS_THROUGH_REQUEST_HEADERS.each do |header|
        outbound_request[header] = request.headers[header]
      end
      http.request(outbound_request) do |outbound_response|
        response.status = outbound_response.code.presence&.to_i
        PASS_THROUGH_RESPONSE_HEADERS.each do |header|
          response.headers[header] = outbound_response[header] if outbound_response[header].present?
        end
        outbound_response.read_body { |chunk| response.stream.write chunk }
      end
    end
  ensure
    response.stream.close
  end
def stream ( url ) uri = URI . parse ( url ) Net :: HTTP . start ( uri . host , uri . port , use_ssl : uri . scheme == 'https' ) do | http | request_class = request . head? ? Net :: HTTP :: Head : Net :: HTTP :: Get outbound_request = request_class . new ( uri . request_uri ) PASS_THROUGH_REQUEST_HEADERS . each do | header | outbound_request [ header ] = request . headers [ header ] end http . request ( outbound_request ) do | outbound_response | response . status = outbound_response . code . presence &. to_i PASS_THROUGH_RESPONSE_HEADERS . each do | header | response . headers [ header ] = outbound_response [ header ] if outbound_response [ header ] . present? end outbound_response . read_body { | chunk | response . stream . write chunk } end end ensure response . stream . close end
Streams data to the client from a given URL.
Streams data to the client from a given URL .
url
<not_specific>
<not_specific>
url The URL to stream data from.
url The URL to stream data from .