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