-
Notifications
You must be signed in to change notification settings - Fork 1
/
hijack.go
54 lines (46 loc) · 1.22 KB
/
hijack.go
1
2
3
4
5
6
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package dcgi
import (
"fmt"
"io"
"log"
"github.com/docker/docker/pkg/stdcopy"
"github.com/docker/engine-api/types"
)
// holdHijackedConnection handles copying input to and output from streams to
// the connection. Copied from github.com/docker/docker/api/client.
func holdHijackedConnection(inputStream io.ReadCloser, outputStream, errorStream io.Writer, resp types.HijackedResponse) error {
var err error
receiveStdout := make(chan error, 1)
if outputStream != nil || errorStream != nil {
go func() {
_, err = stdcopy.StdCopy(outputStream, errorStream, resp.Reader)
// log.Printf("[hijack] End of stdout")
receiveStdout <- err
}()
}
stdinDone := make(chan struct{})
go func() {
if inputStream != nil {
io.Copy(resp.Conn, inputStream)
// log.Printf("[hijack] End of stdin")
}
if err := resp.CloseWrite(); err != nil {
log.Printf("cgi: couldn't send EOF: %s", err)
}
close(stdinDone)
}()
select {
case err := <-receiveStdout:
if err != nil {
return fmt.Errorf("Error receiveStdout: %s", err)
}
case <-stdinDone:
if outputStream != nil || errorStream != nil {
err := <-receiveStdout
if err != nil {
return fmt.Errorf("Error receiveStdout: %s", err)
}
}
}
return nil
}