mirror of
https://github.com/kataras/iris.git
synced 2025-12-18 10:27:06 +00:00
New DirOptions.Cache field for in-memory caching and pre-compression for the fastest possible static file server
Read HISTORY.md it contains a breaking change, second parameter of HandleDir should be iris.Dir(...) instead of just a string relative to: https://github.com/kataras/iris/issues/1556#issuecomment-661057446 Former-commit-id: 14b48a06fb3b99287dff543932be2937a64233b9
This commit is contained in:
@@ -41,6 +41,26 @@ var (
|
||||
ErrNotSupportedCompression = errors.New("compress: unsupported compression")
|
||||
)
|
||||
|
||||
// AllEncodings is a slice of default content encodings.
|
||||
// See `AcquireCompressResponseWriter`.
|
||||
var AllEncodings = []string{GZIP, DEFLATE, BROTLI, SNAPPY}
|
||||
|
||||
// GetEncoding extracts the best available encoding from the request.
|
||||
func GetEncoding(r *http.Request, offers []string) (string, error) {
|
||||
acceptEncoding := r.Header[AcceptEncodingHeaderKey]
|
||||
|
||||
if len(acceptEncoding) == 0 {
|
||||
return "", ErrResponseNotCompressed
|
||||
}
|
||||
|
||||
encoding := negotiateAcceptHeader(acceptEncoding, offers, IDENTITY)
|
||||
if encoding == "" {
|
||||
return "", fmt.Errorf("%w: %s", ErrNotSupportedCompression, encoding)
|
||||
}
|
||||
|
||||
return encoding, nil
|
||||
}
|
||||
|
||||
type (
|
||||
noOpWriter struct{}
|
||||
|
||||
@@ -182,15 +202,9 @@ var _ ResponseWriter = (*CompressResponseWriter)(nil)
|
||||
// It returns the best candidate among "gzip", "defate", "br", "snappy" and "s2"
|
||||
// based on the request's "Accept-Encoding" header value.
|
||||
func AcquireCompressResponseWriter(w ResponseWriter, r *http.Request, level int) (*CompressResponseWriter, error) {
|
||||
// acceptEncoding := r.Header.Values(AcceptEncodingHeaderKey)
|
||||
acceptEncoding := r.Header[AcceptEncodingHeaderKey]
|
||||
if len(acceptEncoding) == 0 {
|
||||
return nil, ErrResponseNotCompressed
|
||||
}
|
||||
|
||||
encoding := negotiateAcceptHeader(acceptEncoding, []string{GZIP, DEFLATE, BROTLI, SNAPPY, S2}, IDENTITY)
|
||||
if encoding == "" {
|
||||
return nil, fmt.Errorf("%w: %s", ErrNotSupportedCompression, encoding)
|
||||
encoding, err := GetEncoding(r, AllEncodings)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
v := compressWritersPool.Get().(*CompressResponseWriter)
|
||||
|
||||
Reference in New Issue
Block a user