Skip to content

Commit

Permalink
fix closure issue
Browse files Browse the repository at this point in the history
  • Loading branch information
ysmood committed Feb 5, 2020
1 parent f1e64d7 commit da7fab4
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 71 deletions.
144 changes: 74 additions & 70 deletions client/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,27 +58,29 @@ func (c *Client) Serve(addr, overrideHost, scheme string) {
continue
}

go func() {
log.Println("[access log]", kit.C(req.Method, "green"), req.URL.String())
go c.serve(addr, overrideHost, scheme, req, send)
}
}

req.URL.Scheme = scheme
req.URL.Host = addr
if overrideHost != "" {
req.Host = overrideHost
}
func (c *Client) serve(addr, overrideHost, scheme string, req *http.Request, send Send) {
log.Println("[access log]", kit.C(req.Method, "green"), req.URL.String())

httpClient := &http.Client{}
res, err := httpClient.Do(req)
if err != nil {
resErr(send, err.Error())
return
}
req.URL.Scheme = scheme
req.URL.Host = addr
if overrideHost != "" {
req.Host = overrideHost
}

err = send(res.StatusCode, res.Header, res.Body)
if err != nil {
log.Println(err)
}
}()
httpClient := &http.Client{}
res, err := httpClient.Do(req)
if err != nil {
resErr(send, err.Error())
return
}

err = send(res.StatusCode, res.Header, res.Body)
if err != nil {
log.Println(err)
}
}

Expand All @@ -91,65 +93,67 @@ func (c *Client) ServeExec() {
continue
}

go func() {
raw, err := ioutil.ReadAll(req.Body)
if err != nil {
resErr(send, err.Error())
return
}
go c.serveExec(req, send)
}
}

data := kit.JSON(raw)
args := []string{}
if data.IsArray() {
for _, arg := range data.Array() {
args = append(args, arg.String())
}
} else {
args = strings.Split(string(raw), " ")
}
func (c *Client) serveExec(req *http.Request, send Send) {
raw, err := ioutil.ReadAll(req.Body)
if err != nil {
resErr(send, err.Error())
return
}

if len(args) == 0 || args[0] == "" {
resErr(send, "empty args")
return
}
data := kit.JSON(raw)
args := []string{}
if data.IsArray() {
for _, arg := range data.Array() {
args = append(args, arg.String())
}
} else {
args = strings.Split(string(raw), " ")
}

if isBuiltin, fn := c.execBuiltin(args); isBuiltin {
err := fn()
if err != nil {
resErr(send, err.Error())
}
err = send(http.StatusOK, nil, nil)
if err != nil {
resErr(send, err.Error())
}
return
}
if len(args) == 0 || args[0] == "" {
resErr(send, "empty args")
return
}

cmd := exec.Command(args[0], args[1:]...)
if isBuiltin, fn := c.execBuiltin(args); isBuiltin {
err := fn()
if err != nil {
resErr(send, err.Error())
}
err = send(http.StatusOK, nil, nil)
if err != nil {
resErr(send, err.Error())
}
return
}

stdout, err := cmd.StdoutPipe()
if err != nil {
resErr(send, err.Error())
return
}
stderr, err := cmd.StderrPipe()
if err != nil {
resErr(send, err.Error())
return
}
out := io.MultiReader(stdout, stderr)
cmd := exec.Command(args[0], args[1:]...)

err = cmd.Start()
if err != nil {
resErr(send, err.Error())
return
}
stdout, err := cmd.StdoutPipe()
if err != nil {
resErr(send, err.Error())
return
}
stderr, err := cmd.StderrPipe()
if err != nil {
resErr(send, err.Error())
return
}
out := io.MultiReader(stdout, stderr)

err = send(http.StatusOK, nil, out)
if err != nil {
resErr(send, err.Error())
}
}()
err = cmd.Start()
if err != nil {
resErr(send, err.Error())
return
}

err = send(http.StatusOK, nil, out)
if err != nil {
resErr(send, err.Error())
}
}

Expand Down
2 changes: 1 addition & 1 deletion server/version.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package server

// Version ...
const Version = "v1.5.5"
const Version = "v1.5.6"

0 comments on commit da7fab4

Please sign in to comment.