forked from beefsack/webify
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.go
125 lines (112 loc) · 2.84 KB
/
server.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
55
56
57
58
59
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package lib
import (
"io"
"io/ioutil"
"log"
"net/http"
"os/exec"
"sync"
"syscall"
)
// Server is a simple proxy server to pipe HTTP requests to a subprocess' stdin
// and the subprocess' stdout to the HTTP response.
type Server struct {
Opts
}
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Start subprocess
cmd := exec.Command(s.Script[0], s.Script[1:]...)
// Get handles to subprocess stdin, stdout and stderr
stdinPipe, err := cmd.StdinPipe()
if err != nil {
log.Printf("error accessing subprocess stdin: %v", err)
respError(w)
return
}
defer stdinPipe.Close()
stderrPipe, err := cmd.StderrPipe()
if err != nil {
log.Printf("error accessing subprocess stderr: %v", err)
respError(w)
return
}
stdoutPipe, err := cmd.StdoutPipe()
if err != nil {
log.Printf("error accessing subprocess stdout: %v", err)
respError(w)
return
}
// Start the subprocess
err = cmd.Start()
if err != nil {
log.Printf("error starting subprocess: %v", err)
respError(w)
return
}
// We use a WaitGroup to wait for all goroutines to finish
wg := sync.WaitGroup{}
// Write request body to subprocess stdin
wg.Add(1)
go func() {
defer func() {
stdinPipe.Close()
wg.Done()
}()
_, err = io.Copy(stdinPipe, r.Body)
if err != nil {
log.Printf("error writing request body to subprocess stdin: %v", err)
respError(w)
return
}
}()
// Read all stderr and write to parent stderr if not empty
wg.Add(1)
go func() {
defer wg.Done()
stderr, err := ioutil.ReadAll(stderrPipe)
if err != nil {
log.Printf("error reading subprocess stderr: %v", err)
respError(w)
return
}
if len(stderr) > 0 {
log.Print(string(stderr))
}
}()
// Read all stdout, but don't write to the response as we need the exit
// status of the subcommand to know our HTTP response code
wg.Add(1)
var stdout []byte
go func() {
defer wg.Done()
so, err := ioutil.ReadAll(stdoutPipe)
stdout = so
if err != nil {
log.Printf("error reading subprocess stdout: %v", err)
respError(w)
return
}
}()
// We must consume stdout and stderr before `cmd.Wait()` as per
// doc and example at https://golang.org/pkg/os/exec/#Cmd.StdoutPipe
wg.Wait()
// Wait for the subprocess to complete
cmdErr := cmd.Wait()
if cmdErr != nil {
// We don't return here because we also want to try to write stdout if
// there was some output
log.Printf("error running subprocess: %v", err)
respError(w)
}
// Write stdout as the response body
_, err = w.Write(stdout)
// We ignore connection close errors, which appears as `syscall.EPIPE`
if err != nil && err != syscall.EPIPE {
log.Printf("error writing response body: %v", err)
}
}
/// respError sends an error response back to the client. Currently this is just
/// a 500 status code.
func respError(w http.ResponseWriter) {
w.WriteHeader(http.StatusInternalServerError)
}