mirror of
https://code.rocketnine.space/tslocum/twins.git
synced 2024-11-27 11:38:14 +01:00
43 lines
750 B
Go
43 lines
750 B
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"log"
|
|
"net"
|
|
"net/url"
|
|
"os/exec"
|
|
"strings"
|
|
)
|
|
|
|
func serveCommand(c net.Conn, request *url.URL, command []string) {
|
|
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 {
|
|
writeStatus(c, statusBadRequest)
|
|
return
|
|
}
|
|
cmd.Stdin = strings.NewReader(requestQuery + "\n")
|
|
}
|
|
cmd.Stdout = &buf
|
|
cmd.Stderr = &buf
|
|
|
|
err := cmd.Run()
|
|
if err != nil {
|
|
writeStatus(c, statusProxyError)
|
|
return
|
|
}
|
|
|
|
writeHeader(c, statusSuccess, "text/gemini; charset=utf-8")
|
|
c.Write(buf.Bytes())
|
|
|
|
if verbose {
|
|
log.Printf("< %s\n", command)
|
|
}
|
|
}
|