package http
import (
)
type fileTransport struct {
fh fileHandler
}
func ( FileSystem) RoundTripper {
return fileTransport{fileHandler{}}
}
func ( fileTransport) ( *Request) ( *Response, error) {
, := newPopulateResponseWriter()
go func() {
.fh.ServeHTTP(, )
.finish()
}()
return <-, nil
}
func () (*populateResponse, <-chan *Response) {
, := io.Pipe()
:= &populateResponse{
ch: make(chan *Response),
pw: ,
res: &Response{
Proto: "HTTP/1.0",
ProtoMajor: 1,
Header: make(Header),
Close: true,
Body: ,
},
}
return , .ch
}
type populateResponse struct {
res *Response
ch chan *Response
wroteHeader bool
hasContent bool
sentResponse bool
pw *io.PipeWriter
}
func ( *populateResponse) () {
if !.wroteHeader {
.WriteHeader(500)
}
if !.sentResponse {
.sendResponse()
}
.pw.Close()
}
func ( *populateResponse) () {
if .sentResponse {
return
}
.sentResponse = true
if .hasContent {
.res.ContentLength = -1
}
.ch <- .res
}
func ( *populateResponse) () Header {
return .res.Header
}
func ( *populateResponse) ( int) {
if .wroteHeader {
return
}
.wroteHeader = true
.res.StatusCode =
.res.Status = fmt.Sprintf("%d %s", , StatusText())
}
func ( *populateResponse) ( []byte) ( int, error) {
if !.wroteHeader {
.WriteHeader(StatusOK)
}
.hasContent = true
if !.sentResponse {
.sendResponse()
}
return .pw.Write()
}