Method: Docker::Util.hijack_for

Defined in:
lib/docker/util.rb

.hijack_for(stdin, block, msg_stack, tty) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/docker/util.rb', line 60

def hijack_for(stdin, block, msg_stack, tty)
  attach_block = attach_for(block, msg_stack, tty)

  lambda do |socket|
    debug "hijack: hijacking the HTTP socket"
    threads = []

    debug "hijack: starting stdin copy thread"
    threads << Thread.start do
      debug "hijack: copying stdin => socket"
      IO.copy_stream stdin, socket

      debug "hijack: closing write end of hijacked socket"
      close_write(socket)
    end

    debug "hijack: starting hijacked socket read thread"
    threads << Thread.start do
      debug "hijack: reading from hijacked socket"

      begin
        while chunk = socket.readpartial(512)
          debug "hijack: got #{chunk.bytesize} bytes from hijacked socket"
          attach_block.call chunk, nil, nil
        end
      rescue EOFError
      end

      debug "hijack: killing stdin copy thread"
      threads.first.kill
    end

    threads.each(&:join)
  end
end