forked from tslocum/twins
1
0
Fork 0
twins/serve_command.go

39 lines
762 B
Go
Raw Permalink Normal View History

2020-11-05 04:18:59 +00:00
package main
import (
"bytes"
"net"
"net/url"
"os/exec"
"strings"
)
2020-11-12 17:56:59 +00:00
func serveCommand(c net.Conn, serve *pathConfig, request *url.URL, command []string) (int, int64) {
2020-11-05 04:18:59 +00:00
var args []string
if len(command) > 0 {
args = command[1:]
}
cmd := exec.Command(command[0], args...)
var buf bytes.Buffer
if request.RawQuery != "" {
requestQuery, err := url.QueryUnescape(request.RawQuery)
if err != nil {
2020-11-12 17:56:59 +00:00
return writeStatus(c, statusBadRequest), -1
2020-11-05 04:18:59 +00:00
}
cmd.Stdin = strings.NewReader(requestQuery + "\n")
}
cmd.Stdout = &buf
cmd.Stderr = &buf
err := cmd.Run()
if err != nil {
2020-11-12 17:56:59 +00:00
return writeStatus(c, statusProxyError), -1
2020-11-05 04:18:59 +00:00
}
2020-11-10 04:10:53 +00:00
writeSuccess(c, serve, geminiType, int64(buf.Len()))
2020-11-05 04:18:59 +00:00
c.Write(buf.Bytes())
2020-11-12 17:56:59 +00:00
return statusSuccess, int64(buf.Len())
2020-11-05 04:18:59 +00:00
}