@marcel91200 / caddy Mirror · read-only
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 11, 02:15 UTC

master
go 98 lines 2.56 KB
Raw
1 // Copyright 2015 Matthew Holt and The Caddy Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package caddygzip
16
17 import (
18 "fmt"
19 "strconv"
20
21 "github.com/klauspost/compress/gzip"
22
23 "github.com/caddyserver/caddy/v2"
24 "github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
25 "github.com/caddyserver/caddy/v2/modules/caddyhttp/encode"
26 )
27
28 func init() {
29 caddy.RegisterModule(Gzip{})
30 }
31
32 // Gzip can create gzip encoders.
33 type Gzip struct {
34 Level int `json:"level,omitempty"`
35 }
36
37 // CaddyModule returns the Caddy module information.
38 func (Gzip) CaddyModule() caddy.ModuleInfo {
39 return caddy.ModuleInfo{
40 ID: "http.encoders.gzip",
41 New: func() caddy.Module { return new(Gzip) },
42 }
43 }
44
45 // UnmarshalCaddyfile sets up the handler from Caddyfile tokens.
46 func (g *Gzip) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
47 d.Next() // consume option name
48 if !d.NextArg() {
49 return nil
50 }
51 levelStr := d.Val()
52 level, err := strconv.Atoi(levelStr)
53 if err != nil {
54 return err
55 }
56 g.Level = level
57 return nil
58 }
59
60 // Provision provisions g's configuration.
61 func (g *Gzip) Provision(ctx caddy.Context) error {
62 if g.Level == 0 {
63 g.Level = defaultGzipLevel
64 }
65 return nil
66 }
67
68 // Validate validates g's configuration.
69 func (g Gzip) Validate() error {
70 if g.Level < gzip.StatelessCompression {
71 return fmt.Errorf("quality too low; must be >= %d", gzip.StatelessCompression)
72 }
73 if g.Level > gzip.BestCompression {
74 return fmt.Errorf("quality too high; must be <= %d", gzip.BestCompression)
75 }
76 return nil
77 }
78
79 // AcceptEncoding returns the name of the encoding as
80 // used in the Accept-Encoding request headers.
81 func (Gzip) AcceptEncoding() string { return "gzip" }
82
83 // NewEncoder returns a new gzip writer.
84 func (g Gzip) NewEncoder() encode.Encoder {
85 writer, _ := gzip.NewWriterLevel(nil, g.Level)
86 return writer
87 }
88
89 // Informed from http://blog.klauspost.com/gzip-performance-for-go-webservers/
90 var defaultGzipLevel = 5
91
92 // Interface guards
93 var (
94 _ encode.Encoding = (*Gzip)(nil)
95 _ caddy.Provisioner = (*Gzip)(nil)
96 _ caddy.Validator = (*Gzip)(nil)
97 _ caddyfile.Unmarshaler = (*Gzip)(nil)
98 )