-
Notifications
You must be signed in to change notification settings - Fork 4
/
session.go
117 lines (105 loc) · 3.25 KB
/
session.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Package cutil - Content managed by Project Forge, see [projectforge.md] for details.
package cutil
import (
"bytes"
"context"
"io"
"net/http"
"slices"
"strings"
"github.com/mileusna/useragent"
"projectforge.dev/projectforge/app"
"projectforge.dev/projectforge/app/controller/csession"
"projectforge.dev/projectforge/app/lib/telemetry"
"projectforge.dev/projectforge/app/lib/telemetry/httpmetrics"
"projectforge.dev/projectforge/app/lib/user"
"projectforge.dev/projectforge/app/util"
)
var (
initialIcons = []string{"searchbox"}
MaxBodySize = int64(1024 * 1024 * 128) // 128MB
)
// @contextcheck(req_has_ctx).
func LoadPageState(as *app.State, w http.ResponseWriter, r *http.Request, key string, logger util.Logger) *PageState {
parentCtx, logger := httpmetrics.ExtractHeaders(r, logger)
ctx, span, logger := telemetry.StartSpan(parentCtx, "http:"+key, logger)
span.Attribute("path", r.URL.Path)
if !telemetry.SkipControllerMetrics {
httpmetrics.InjectHTTP(200, r, span)
}
session, flashes, prof := loadSession(ctx, as, w, r, logger) //nolint:contextcheck
params := ParamSetFromRequest(r)
ua := useragent.Parse(r.Header.Get("User-Agent"))
os := strings.ToLower(ua.OS)
browser := strings.ToLower(ua.Name)
platform := "unknown"
switch {
case ua.Desktop:
platform = "desktop"
case ua.Tablet:
platform = "tablet"
case ua.Mobile:
platform = "mobile"
case ua.Bot:
platform = "bot"
}
span.Attribute("browser", browser)
span.Attribute("os", os)
b, _ := io.ReadAll(http.MaxBytesReader(w, r.Body, MaxBodySize))
r.Body = io.NopCloser(bytes.NewBuffer(b))
return &PageState{
Action: key, Method: r.Method, URI: r.URL, Flashes: flashes, Session: session,
OS: os, OSVersion: ua.OSVersion, Browser: browser, BrowserVersion: ua.Version, Platform: platform,
Profile: prof, Params: params,
Icons: slices.Clone(initialIcons), Started: util.TimeCurrent(), Logger: logger, Context: ctx, Span: span, RequestBody: b,
}
}
func loadSession(_ context.Context, _ *app.State, w http.ResponseWriter, r *http.Request, logger util.Logger) (util.ValueMap, []string, *user.Profile) {
c, _ := r.Cookie(util.AppKey)
if c == nil || c.Value == "" {
return util.ValueMap{}, nil, user.DefaultProfile.Clone()
}
dec, err := util.DecryptMessage(nil, c.Value, logger)
if err != nil {
logger.Warnf("error decrypting session: %+v", err)
}
session, err := util.FromJSONMap([]byte(dec))
if err != nil {
session = util.ValueMap{}
}
flashes := util.StringSplitAndTrim(session.GetStringOpt(csession.WebFlashKey), ";")
if len(flashes) > 0 {
delete(session, csession.WebFlashKey)
if e := csession.SaveSession(w, session, logger); e != nil {
logger.Warnf("can't save session: %+v", e)
}
}
prof, err := loadProfile(session)
if err != nil {
logger.Warnf("can't load profile: %+v", err)
}
return session, flashes, prof
}
func loadProfile(session util.ValueMap) (*user.Profile, error) {
x, ok := session["profile"]
if !ok {
return user.DefaultProfile.Clone(), nil
}
s, ok := x.(string)
if !ok {
m, ok := x.(map[string]any)
if !ok {
return user.DefaultProfile.Clone(), nil
}
s = util.ToJSON(m)
}
p := &user.Profile{}
err := util.FromJSON([]byte(s), p)
if err != nil {
return nil, err
}
if p.Name == "" {
p.Name = user.DefaultProfile.Name
}
return p, nil
}