Skip to content

Commit 0019da7

Browse files
committed
In order to make the compress work with the go server engine, it needed to prevent the results from setting the header and triggering a write. Also in the adapter we cannot use the http.ServerContent method if the output stream is using the compressor.
1 parent 61de450 commit 0019da7

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

compress.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type WriteFlusher interface {
4141

4242
type CompressResponseWriter struct {
4343
Header ServerHeader
44+
ControllerResponse *Response
4445
OriginalWriter io.Writer
4546
compressWriter WriteFlusher
4647
compressionType string
@@ -56,11 +57,13 @@ func CompressFilter(c *Controller, fc []Filter) {
5657
if Config.BoolDefault("results.compressed", false) {
5758
if c.Response.Status != http.StatusNoContent && c.Response.Status != http.StatusNotModified {
5859
if found, compressType, compressWriter := detectCompressionType(c.Request, c.Response); found {
59-
writer := CompressResponseWriter{c.Response.Out.Header(), c.Response.Out.GetWriter(), compressWriter, compressType, false, make(chan bool, 1), nil, false}
60+
writer := CompressResponseWriter{c.Response.Out.Header(), c.Response, c.Response.Out.GetWriter(), compressWriter, compressType, false, make(chan bool, 1), nil, false}
6061
if w, ok := c.Response.Out.(http.CloseNotifier); ok {
6162
writer.parentNotify = w.CloseNotify()
6263
}
6364
c.Response.Out.SetWriter(&writer)
65+
// Block the results from writing there own header
66+
c.Response.headerWritten = true
6467
}
6568
} else {
6669
TRACE.Printf("Compression disabled for response status (%d)", c.Response.Status)
@@ -108,8 +111,10 @@ func (c *CompressResponseWriter) WriteHeader(status int) {
108111

109112
func (c *CompressResponseWriter) Close() error {
110113
if c.compressionType != "" {
114+
c.Header.Del("Content-Length")
111115
if err := c.compressWriter.Close(); err != nil {
112-
ERROR.Println("Error closing compress writer", err)
116+
// TODO When writing directly to stream
117+
ERROR.Println("Error closing compress writer",c.compressionType, err)
113118
}
114119
}
115120
// Non-blocking write to the closenotifier, if we for some reason should

engine_adapter_go.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,12 @@ func (r *GOResponse) SetWriter(writer io.Writer) {
223223
}
224224
func (r *GOResponse) WriteStream(name string, contentlen int64, modtime time.Time,reader io.Reader) error {
225225

226-
if rs, ok := reader.(io.ReadSeeker); ok {
227-
// Call ServeContent to serve the content
228-
http.ServeContent(r.Original, r.Request, name, modtime, rs)
226+
// Check to see if the output stream is modified, if not send it using the
227+
// Native writer
228+
if _,ok:=r.Writer.(http.ResponseWriter);ok {
229+
if rs, ok := reader.(io.ReadSeeker); ok {
230+
http.ServeContent(r.Original, r.Request, name, modtime, rs)
231+
}
229232
} else {
230233
// Else, do a simple io.Copy.
231234
ius := r.Request.Header.Get("If-Unmodified-Since")

results.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,9 @@ func (r *RenderTemplateResult) Apply(req *Request, resp *Response) {
214214
}
215215

216216
if !chunked {
217-
resp.Out.Header().Set("Content-Length", strconv.Itoa(b.Len()))
217+
if !resp.headerWritten {
218+
resp.Out.Header().Set("ContentBeater-Length", strconv.Itoa(b.Len()))
219+
}
218220
}
219221
resp.WriteHeader(http.StatusOK, "text/html; charset=utf-8")
220222
if _, err := b.WriteTo(out); err != nil {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy