generated from traefik/plugindemo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
max_age.go
91 lines (81 loc) · 2.23 KB
/
max_age.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Package traefik_session_max_age is a plugin for the Traefik reverse proxy
// that sets cookie's max-age
package traefik_session_max_age
import (
"context"
"net/http"
"net/textproto"
"strconv"
"strings"
)
// Config the plugin configuration.
type Config struct {
CookieName string `json:"cookieName,omitempty"`
MaxAge int `json:"maxAge,omitempty"`
}
// CreateConfig creates the default plugin configuration.
func CreateConfig() *Config {
return &Config{}
}
type responseWriterWrapper struct {
http.ResponseWriter
cookieName string
maxAge int
}
func (rww responseWriterWrapper) Header() http.Header {
return rww.ResponseWriter.Header()
}
func (rww responseWriterWrapper) WriteHeader(code int) {
cookies := rww.ResponseWriter.Header()["Set-Cookie"]
if len(cookies) == 0 {
rww.ResponseWriter.WriteHeader(code)
return
}
for i, line := range cookies {
parts := strings.Split(textproto.TrimString(line), ";")
if len(parts) == 1 && parts[0] == "" {
continue
}
parts[0] = textproto.TrimString(parts[0])
name, _, ok := strings.Cut(parts[0], "=")
if !ok {
continue
}
buf := make([]byte, 0, 19)
name = textproto.TrimString(name)
if name == rww.cookieName {
var b strings.Builder
b.WriteString(line)
if rww.maxAge > 0 {
b.WriteString("; Max-Age=")
b.Write(strconv.AppendInt(buf, int64(rww.maxAge), 10))
} else if rww.maxAge < 0 {
b.WriteString("; Max-Age=0")
}
cookies[i] = b.String()
}
}
rww.ResponseWriter.WriteHeader(code)
}
func (rww responseWriterWrapper) Write(b []byte) (int, error) {
return rww.ResponseWriter.Write(b)
}
// SessionMaxAge is a middleware for traefik middlware plugin to set cookie max age.
type SessionMaxAge struct {
next http.Handler
cookieName string
maxAge int
name string
}
// New return a wrapped http.Handler.
func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
return &SessionMaxAge{
next: next,
name: name,
maxAge: config.MaxAge,
cookieName: config.CookieName,
}, nil
}
func (a *SessionMaxAge) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
a.next.ServeHTTP(responseWriterWrapper{ResponseWriter: rw, cookieName: a.cookieName, maxAge: a.maxAge}, req)
}