Module: ThreadedProxy::Controller

Defined in:
lib/threaded_proxy/controller.rb

Instance Method Summary collapse

Instance Method Details

#proxy_fetch(origin_url, options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/threaded_proxy/controller.rb', line 7

def proxy_fetch(origin_url, options = {})
  # 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)
    client.start(socket)
  rescue Errno::EPIPE
    # client disconnected before request finished; not an error
  ensure
    socket.close unless socket.closed?
  end

  head :ok
end