Sha256: 916c097469d1cf331ca0c3ba280d93a3bebae835ed810ab3226ae0d7be37fedf
Contents?: true
Size: 1.2 KB
Versions: 2
Compression:
Stored size: 1.2 KB
Contents
# frozen_string_literal: true require_relative 'client' module ThreadedProxy module Controller def proxy_fetch(origin_url, options = {}, &block) # hijack the response so we can take it outside of the rack request/response cycle request.env['rack.hijack'].call socket = request.env['rack.hijack_io'] Thread.new do if options[:body] == :rack options[:headers] ||= {} options[:body] = request.body_stream if request.env['HTTP_TRANSFER_ENCODING'] == 'chunked' options[:headers]['Transfer-Encoding'] = 'chunked' elsif request.env['CONTENT_LENGTH'] options[:headers]['content-length'] = request.env['CONTENT_LENGTH'].to_s else raise 'Cannot proxy a non-chunked POST request without content-length' end options[:headers]['Content-Type'] = request.env['CONTENT_TYPE'] if request.env['CONTENT_TYPE'] end client = Client.new(origin_url, options, &block) client.start(socket) rescue Errno::EPIPE # client disconnected before request finished; not an error ensure socket.close unless socket.closed? end head :ok end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
rails-threaded-proxy-0.4.1 | lib/threaded_proxy/controller.rb |
rails-threaded-proxy-0.4.0 | lib/threaded_proxy/controller.rb |