Class: Excon::Middleware::Hijack

Inherits:
Base
  • Object
show all
Defined in:
lib/excon/middlewares/hijack.rb

Overview

Hijack is an Excon middleware which parses response headers and then yields the underlying TCP socket for raw TCP communication (used to attach to STDIN of containers).

Instance Method Summary collapse

Instance Method Details

#response_call(datum) ⇒ 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
# File 'lib/excon/middlewares/hijack.rb', line 7

def response_call(datum)
  if datum[:hijack_block]
    # Need to process the response headers here rather than in
    # Excon::Middleware::ResponseParser as the response parser will
    # block trying to read the body.
    socket = datum[:connection].send(:socket)

    # c.f. Excon::Response.parse
    until match = /^HTTP\/\d+\.\d+\s(\d{3})\s/.match(socket.readline); end
    status = match[1].to_i

    datum[:response] = {
      :body          => '',
      :headers       => Excon::Headers.new,
      :status        => status,
      :remote_ip     => socket.respond_to?(:remote_ip) &&
                        socket.remote_ip,
      :local_port    => socket.respond_to?(:local_port) &&
                        socket.local_port,
      :local_address => socket.respond_to?(:local_address) &&
                        socket.local_address
    }

    Excon::Response.parse_headers(socket, datum)
    datum[:hijack_block].call socket.instance_variable_get(:@socket)
  end

  @stack.response_call(datum)
end