mirror of
				https://github.com/caddyserver/caddy.git
				synced 2025-10-31 02:27:19 -04:00 
			
		
		
		
	* Feature #1282 - Support pre-gzipped files * Fix broken test cases * Support brotli encoding as well * Fix for #1276 - support integers and floats as metadata in markdown (#1278) * Fix for #1276 * Use strconv.Format * Use map[string]interface{} as variables * One more file * Always run all tests before commit * Get rid of DocFlags * Fix syntax in caddy.conf * Update to Go 1.7.4 * Add send_timeout property to fastcgi directive. * Convert rwc field on FCGIClient from type io.ReadWriteCloser to net.Conn. * Return HTTP 504 to the client when a timeout occurs. * In Handler.ServeHTTP(), close the connection before returning an HTTP 502/504. * Refactor tests and add coverage. * Return HTTP 504 when FastCGI connect times out. * test: add unit test for #1283 (#1288) * After review fixes * Limit the number of restarts with systemd * Prevent fd leak * Prevent fd leak * Refactor loops * gofmt
		
			
				
	
	
		
			94 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			94 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package gzip
 | |
| 
 | |
| import (
 | |
| 	"compress/gzip"
 | |
| 	"net/http"
 | |
| 	"strconv"
 | |
| )
 | |
| 
 | |
| // ResponseFilter determines if the response should be gzipped.
 | |
| type ResponseFilter interface {
 | |
| 	ShouldCompress(http.ResponseWriter) bool
 | |
| }
 | |
| 
 | |
| // LengthFilter is ResponseFilter for minimum content length.
 | |
| type LengthFilter int64
 | |
| 
 | |
| // ShouldCompress returns if content length is greater than or
 | |
| // equals to minimum length.
 | |
| func (l LengthFilter) ShouldCompress(w http.ResponseWriter) bool {
 | |
| 	contentLength := w.Header().Get("Content-Length")
 | |
| 	length, err := strconv.ParseInt(contentLength, 10, 64)
 | |
| 	if err != nil || length == 0 {
 | |
| 		return false
 | |
| 	}
 | |
| 	return l != 0 && int64(l) <= length
 | |
| }
 | |
| 
 | |
| // SkipCompressedFilter is ResponseFilter that will discard already compressed responses
 | |
| type SkipCompressedFilter struct{}
 | |
| 
 | |
| // ShouldCompress returns true if served file is not already compressed
 | |
| // encodings via https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Encoding
 | |
| func (n SkipCompressedFilter) ShouldCompress(w http.ResponseWriter) bool {
 | |
| 	switch w.Header().Get("Content-Encoding") {
 | |
| 	case "gzip", "compress", "deflate", "br":
 | |
| 		return false
 | |
| 	default:
 | |
| 		return true
 | |
| 	}
 | |
| }
 | |
| 
 | |
| // ResponseFilterWriter validates ResponseFilters. It writes
 | |
| // gzip compressed data if ResponseFilters are satisfied or
 | |
| // uncompressed data otherwise.
 | |
| type ResponseFilterWriter struct {
 | |
| 	filters           []ResponseFilter
 | |
| 	shouldCompress    bool
 | |
| 	statusCodeWritten bool
 | |
| 	*gzipResponseWriter
 | |
| }
 | |
| 
 | |
| // NewResponseFilterWriter creates and initializes a new ResponseFilterWriter.
 | |
| func NewResponseFilterWriter(filters []ResponseFilter, gz *gzipResponseWriter) *ResponseFilterWriter {
 | |
| 	return &ResponseFilterWriter{filters: filters, gzipResponseWriter: gz}
 | |
| }
 | |
| 
 | |
| // WriteHeader wraps underlying WriteHeader method and
 | |
| // compresses if filters are satisfied.
 | |
| func (r *ResponseFilterWriter) WriteHeader(code int) {
 | |
| 	// Determine if compression should be used or not.
 | |
| 	r.shouldCompress = true
 | |
| 	for _, filter := range r.filters {
 | |
| 		if !filter.ShouldCompress(r) {
 | |
| 			r.shouldCompress = false
 | |
| 			break
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	if r.shouldCompress {
 | |
| 		// replace discard writer with ResponseWriter
 | |
| 		if gzWriter, ok := r.gzipResponseWriter.Writer.(*gzip.Writer); ok {
 | |
| 			gzWriter.Reset(r.ResponseWriter)
 | |
| 		}
 | |
| 		// use gzip WriteHeader to include and delete
 | |
| 		// necessary headers
 | |
| 		r.gzipResponseWriter.WriteHeader(code)
 | |
| 	} else {
 | |
| 		r.ResponseWriter.WriteHeader(code)
 | |
| 	}
 | |
| 	r.statusCodeWritten = true
 | |
| }
 | |
| 
 | |
| // Write wraps underlying Write method and compresses if filters
 | |
| // are satisfied
 | |
| func (r *ResponseFilterWriter) Write(b []byte) (int, error) {
 | |
| 	if !r.statusCodeWritten {
 | |
| 		r.WriteHeader(http.StatusOK)
 | |
| 	}
 | |
| 	if r.shouldCompress {
 | |
| 		return r.gzipResponseWriter.Write(b)
 | |
| 	}
 | |
| 	return r.ResponseWriter.Write(b)
 | |
| }
 |