forked from mrlauer/gofcgisrv
-
Notifications
You must be signed in to change notification settings - Fork 3
/
scgi.go
66 lines (59 loc) · 1.45 KB
/
scgi.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
package gofcgisrv
import (
"bytes"
"fmt"
"io"
"net"
"strings"
)
type SCGIRequester struct {
applicationAddr string
}
func (sr *SCGIRequester) Request(env []string, stdin io.Reader, stdout io.Writer, stderr io.Writer) error {
// Make a connection
conn, err := net.Dial("tcp", sr.applicationAddr)
if err != nil {
return err
}
// Send the environment
header := bytes.NewBuffer(nil)
// Content length has to go first.
for _, envstring := range env {
splits := strings.SplitN(envstring, "=", 2)
if len(splits) == 2 && splits[0] == "CONTENT_LENGTH" {
fmt.Fprintf(header, "%s\000%s\000", splits[0], splits[1])
}
}
io.WriteString(header, "SCGI:\x001\x00")
for _, envstring := range env {
splits := strings.SplitN(envstring, "=", 2)
if len(splits) == 2 && splits[0] != "CONTENT_LENGTH" {
fmt.Fprintf(header, "%s\000%s\000", splits[0], splits[1])
}
}
_, err = fmt.Fprintf(conn, "%d:%s,", header.Len(), header.Bytes())
if err != nil {
conn.Close()
return err
}
_, err = io.Copy(conn, stdin)
if err != nil {
conn.Close()
return err
}
// Flup needs the write side closed. I don't think that's right, but there it is.
if cw, ok := conn.(interface {
CloseWrite() error
}); ok {
cw.CloseWrite()
}
_, err = io.Copy(stdout, conn)
// If we have an error, just log it to stderr.
if err != nil {
stderr.Write([]byte(err.Error()))
}
return nil
}
func NewSCGI(addr string) *SCGIRequester {
return &SCGIRequester{applicationAddr: addr}
}