http: Make methods of .Data.Res actually work

This commit is contained in:
2024-12-08 01:50:15 -05:00
parent 3c9eb01790
commit 6f635825a9
3 changed files with 41 additions and 74 deletions

View File

@@ -123,9 +123,13 @@ func (this *Handler) serveDocument (
// execute document
data := HTTPData { }
data.Res.Header = recorder.Header()
data.Res.WriteHeader = recorder.WriteHeader
data.Res.Reset = resetRecorder
data.Res = WrappedResponseWriter {
responseWriter: res,
resetFunc: resetRecorder,
Header: WrappedHeader {
Header: recorder.Header(),
},
}
data.Req = req
err = document.Execute(&recorder, step.ExecutionData {
Data: data,

View File

@@ -8,15 +8,44 @@ import "net/http"
// available to templates as they are being executed.
type HTTPData struct {
// Res is like an http.ResponseWriter.
Res struct {
Header http.Header
WriteHeader func (statusCode int)
Reset func ()
}
Res WrappedResponseWriter
// Req is the HTTP request.
Req *http.Request
}
type WrappedResponseWriter struct {
responseWriter http.ResponseWriter
resetFunc func ()
Header WrappedHeader
}
func (this WrappedResponseWriter) WriteHeader (statusCode int) string {
this.responseWriter.WriteHeader(statusCode)
return ""
}
func (this WrappedResponseWriter) Reset () string {
this.resetFunc()
return ""
}
type WrappedHeader struct { http.Header }
func (this WrappedHeader) Add (name, value string) string {
this.Header.Add(name, value)
return ""
}
func (this WrappedHeader) Del (name string) string {
this.Header.Del(name)
return ""
}
func (this WrappedHeader) Set (name, value string) string {
this.Header.Set(name, value)
return ""
}
var _ http.ResponseWriter = new(HTTPResponseRecorder)
// HTTPResponseRecorder is an http.ResponseWriter that can buffer a response to