twins/serve_fcgi.go

58 lines
1.0 KiB
Go
Raw Normal View History

2020-11-04 20:48:55 +00:00
package main
import (
"bytes"
"io"
"io/ioutil"
"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
}
2020-11-04 21:07:06 +00:00
func serveFastCGI(c net.Conn, connFactory gofast.ConnFactory, u *url.URL, filePath string) {
header := map[string][]string{
"Accept": {"*/*"},
"Host": {u.Hostname()},
}
2020-11-04 20:48:55 +00:00
r := &http.Request{
Method: "GET",
2020-11-04 21:07:06 +00:00
URL: u,
2020-11-04 20:48:55 +00:00
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
2020-11-04 21:07:06 +00:00
Header: header,
2020-11-04 20:48:55 +00:00
Body: ioutil.NopCloser(bytes.NewReader(nil)),
2020-11-04 21:07:06 +00:00
Host: u.Host,
2020-11-04 20:48:55 +00:00
}
gofast.
NewHandler(
gofast.NewFileEndpoint(filePath)(gofast.BasicSession),
2020-11-04 21:07:06 +00:00
gofast.SimpleClientFactory(connFactory, 0),
2020-11-04 20:48:55 +00:00
).
ServeHTTP(newResponseWriter(c), r)
}