twins-upstream/serve_fcgi.go
2020-11-05 10:21:13 -08:00

62 lines
1.1 KiB
Go

package main
import (
"bytes"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"net/url"
"github.com/yookoala/gofast"
)
type responseWriter struct {
io.WriteCloser
header http.Header
}
func newResponseWriter(out io.WriteCloser) *responseWriter {
return &responseWriter{
WriteCloser: out,
header: make(http.Header),
}
}
func (w *responseWriter) Header() http.Header {
return w.header
}
func (w *responseWriter) WriteHeader(statusCode int) {
// Do nothing
}
func serveFastCGI(c net.Conn, connFactory gofast.ConnFactory, u *url.URL, filePath string) {
header := map[string][]string{
"Accept": {"*/*"},
"Host": {u.Hostname()},
}
r := &http.Request{
Method: "GET",
URL: u,
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Header: header,
Body: ioutil.NopCloser(bytes.NewReader(nil)),
Host: u.Host,
}
gofast.
NewHandler(
gofast.NewFileEndpoint(filePath)(gofast.BasicSession),
gofast.SimpleClientFactory(connFactory, 0),
).
ServeHTTP(newResponseWriter(c), r)
if verbose {
log.Printf("< exec %s\n", filePath)
}
}