Allow processing of HTTP headers at WS creation

This commit is contained in:
George Grozdev
2021-10-14 09:48:46 +01:00
parent f157dbe9d3
commit 77ac0b8e58
5 changed files with 21 additions and 9 deletions

View File

@@ -3,6 +3,7 @@ package localcommand
import (
"os"
"os/exec"
"strings"
"syscall"
"time"
@@ -27,11 +28,22 @@ type LocalCommand struct {
ptyClosed chan struct{}
}
func New(command string, argv []string, options ...Option) (*LocalCommand, error) {
func New(command string, argv []string, headers map[string][]string, options ...Option) (*LocalCommand, error) {
cmd := exec.Command(command, argv...)
cmd.Env = append(os.Environ(), "TERM=xterm-256color")
// Combine headers into key=value pairs to set as env vars
// Prefix the headers with "http_" so we don't overwrite any other env vars
// which potentially has the same name and to bring these closer to what
// a (F)CGI server would proxy to a backend service
// Replace hyphen with underscore and make them all upper case
for key, values := range headers {
h := "HTTP_" + strings.Replace(strings.ToUpper(key), "-", "_", -1) + "=" + strings.Join(values, ",")
// log.Printf("Adding header: %s", h)
cmd.Env = append(cmd.Env, h)
}
pty, err := pty.Start(cmd)
if err != nil {
// todo close cmd?