-
Notifications
You must be signed in to change notification settings - Fork 1k
/
launcher.go
98 lines (79 loc) · 1.94 KB
/
launcher.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
// Copyright 2013 Joe Walnes and the websocketd team.
// All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package libwebsocketd
import (
"errors"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
)
var ScriptNotFoundError = errors.New("script not found")
type URLInfo struct {
ScriptPath string
PathInfo string
FilePath string
}
func parsePath(path string, config *Config) (*URLInfo, error) {
if !config.UsingScriptDir {
return &URLInfo{"/", path, ""}, nil
}
parts := strings.Split(path[1:], "/")
urlInfo := &URLInfo{}
for i, part := range parts {
urlInfo.ScriptPath = strings.Join([]string{urlInfo.ScriptPath, part}, "/")
urlInfo.FilePath = filepath.Join(config.ScriptDir, urlInfo.ScriptPath)
isLastPart := i == len(parts)-1
statInfo, err := os.Stat(urlInfo.FilePath)
// not a valid path
if err != nil {
return nil, ScriptNotFoundError
}
// at the end of url but is a dir
if isLastPart && statInfo.IsDir() {
return nil, ScriptNotFoundError
}
// we've hit a dir, carry on looking
if statInfo.IsDir() {
continue
}
// no extra args
if isLastPart {
return urlInfo, nil
}
// build path info from extra parts of url
urlInfo.PathInfo = "/" + strings.Join(parts[i+1:], "/")
return urlInfo, nil
}
panic("parsePath")
}
type LaunchedProcess struct {
cmd *exec.Cmd
stdin io.WriteCloser
stdout io.ReadCloser
stderr io.ReadCloser
}
func launchCmd(commandName string, commandArgs []string, env []string) (*LaunchedProcess, error) {
cmd := exec.Command(commandName, commandArgs...)
cmd.Env = env
stdout, err := cmd.StdoutPipe()
if err != nil {
return nil, err
}
stderr, err := cmd.StderrPipe()
if err != nil {
return nil, err
}
stdin, err := cmd.StdinPipe()
if err != nil {
return nil, err
}
err = cmd.Start()
if err != nil {
return nil, err
}
return &LaunchedProcess{cmd, stdin, stdout, stderr}, err
}