2020-11-04 21:48:55 +01: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 22:07:06 +01: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 21:48:55 +01:00
|
|
|
r := &http.Request{
|
|
|
|
Method: "GET",
|
2020-11-04 22:07:06 +01:00
|
|
|
URL: u,
|
2020-11-04 21:48:55 +01:00
|
|
|
Proto: "HTTP/1.1",
|
|
|
|
ProtoMajor: 1,
|
|
|
|
ProtoMinor: 1,
|
2020-11-04 22:07:06 +01:00
|
|
|
Header: header,
|
2020-11-04 21:48:55 +01:00
|
|
|
Body: ioutil.NopCloser(bytes.NewReader(nil)),
|
2020-11-04 22:07:06 +01:00
|
|
|
Host: u.Host,
|
2020-11-04 21:48:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
gofast.
|
|
|
|
NewHandler(
|
|
|
|
gofast.NewFileEndpoint(filePath)(gofast.BasicSession),
|
2020-11-04 22:07:06 +01:00
|
|
|
gofast.SimpleClientFactory(connFactory, 0),
|
2020-11-04 21:48:55 +01:00
|
|
|
).
|
|
|
|
ServeHTTP(newResponseWriter(c), r)
|
|
|
|
}
|