forked from devfeel/dotweb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
feature.go
86 lines (73 loc) · 2.38 KB
/
feature.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
package dotweb
import (
"compress/gzip"
"github.com/devfeel/dotweb/feature"
"io"
"net/http"
"net/url"
"strconv"
)
type xFeatureTools struct{}
var FeatureTools *xFeatureTools
func init() {
FeatureTools = new(xFeatureTools)
}
//set CROS config on HttpContext
func (f *xFeatureTools) SetCROSConfig(ctx *HttpContext, c *feature.CROSConfig) {
ctx.Response().SetHeader(HeaderAccessControlAllowOrigin, c.AllowedOrigins)
ctx.Response().SetHeader(HeaderAccessControlAllowMethods, c.AllowedMethods)
ctx.Response().SetHeader(HeaderAccessControlAllowHeaders, c.AllowedHeaders)
ctx.Response().SetHeader(HeaderAccessControlAllowCredentials, strconv.FormatBool(c.AllowCredentials))
ctx.Response().SetHeader(HeaderP3P, c.AllowedP3P)
}
//set CROS config on HttpContext
func (f *xFeatureTools) SetSession(httpCtx *HttpContext) {
sessionId, err := httpCtx.HttpServer().GetSessionManager().GetClientSessionID(httpCtx.Request().Request)
if err == nil && sessionId != "" {
httpCtx.sessionID = sessionId
} else {
httpCtx.sessionID = httpCtx.HttpServer().GetSessionManager().NewSessionID()
cookie := &http.Cookie{
Name: httpCtx.HttpServer().sessionManager.CookieName,
Value: url.QueryEscape(httpCtx.SessionID()),
Path: "/",
}
httpCtx.SetCookie(cookie)
}
}
func (f *xFeatureTools) SetGzip(httpCtx *HttpContext) {
gw, err := gzip.NewWriterLevel(httpCtx.Response().Writer(), DefaultGzipLevel)
if err != nil {
panic("use gzip error -> " + err.Error())
}
grw := &gzipResponseWriter{Writer: gw, ResponseWriter: httpCtx.Response().Writer()}
httpCtx.Response().reset(grw)
httpCtx.Response().SetHeader(HeaderContentEncoding, gzipScheme)
}
// doFeatures do features...
func (f *xFeatureTools) InitFeatures(server *HttpServer, httpCtx *HttpContext) {
//gzip
if server.ServerConfig().EnabledGzip {
FeatureTools.SetGzip(httpCtx)
}
//session
//if exists client-sessionid, use it
//if not exists client-sessionid, new one
if server.SessionConfig().EnabledSession {
FeatureTools.SetSession(httpCtx)
}
//处理 cros feature
if server.Features.CROSConfig != nil {
c := server.Features.CROSConfig
if c.EnabledCROS {
FeatureTools.SetCROSConfig(httpCtx, c)
}
}
}
func (f *xFeatureTools) ReleaseFeatures(server *HttpServer, httpCtx *HttpContext) {
if server.ServerConfig().EnabledGzip {
var w io.Writer
w = httpCtx.Response().Writer().(*gzipResponseWriter).Writer
w.(*gzip.Writer).Close()
}
}