/
asset.go
231 lines (178 loc) · 4.93 KB
/
asset.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package server
import (
"bytes"
"crypto/sha1" //nolint:gosec // Usage is for collision avoidance not security.
"embed"
"errors"
"fmt"
"io/fs"
"mime"
"net/http"
"path"
"path/filepath"
"strings"
"github.com/valyala/fasthttp"
"github.com/authelia/authelia/v4/internal/handlers"
"github.com/authelia/authelia/v4/internal/middlewares"
"github.com/authelia/authelia/v4/internal/utils"
)
var (
//go:embed public_html
assets embed.FS
//go:embed locales
locales embed.FS
)
func newPublicHTMLEmbeddedHandler() fasthttp.RequestHandler {
etags := map[string][]byte{}
getEmbedETags(assets, assetsRoot, etags)
return func(ctx *fasthttp.RequestCtx) {
p := path.Join(assetsRoot, string(ctx.Path()))
if etag, ok := etags[p]; ok {
ctx.Response.Header.SetBytesKV(headerETag, etag)
ctx.Response.Header.SetBytesKV(headerCacheControl, headerValueCacheControlETaggedAssets)
if bytes.Equal(etag, ctx.Request.Header.PeekBytes(headerIfNoneMatch)) {
ctx.SetStatusCode(fasthttp.StatusNotModified)
return
}
}
var (
data []byte
err error
)
if data, err = assets.ReadFile(p); err != nil {
hfsHandleErr(ctx, err)
return
}
contentType := mime.TypeByExtension(path.Ext(p))
if len(contentType) == 0 {
contentType = http.DetectContentType(data)
}
ctx.SetContentType(contentType)
ctx.SetBody(data)
}
}
func newLocalesPathResolver() func(ctx *fasthttp.RequestCtx) (supported bool, asset string) {
var (
languages, dirs []string
)
entries, err := locales.ReadDir("locales")
if err == nil {
for _, entry := range entries {
if entry.IsDir() {
var lng string
switch len(entry.Name()) {
case 2:
lng = entry.Name()
case 0:
continue
default:
lng = strings.SplitN(entry.Name(), "-", 2)[0]
}
if !utils.IsStringInSlice(entry.Name(), dirs) {
dirs = append(dirs, entry.Name())
}
if utils.IsStringInSlice(lng, languages) {
continue
}
languages = append(languages, lng)
}
}
}
aliases := map[string]string{
"cs": "cs-CZ",
"da": "da-DK",
"el": "el-GR",
"ja": "ja-JP",
"nb": "nb-NO",
"sv": "sv-SE",
"uk": "uk-UA",
"zh": "zh-CN",
}
return func(ctx *fasthttp.RequestCtx) (supported bool, asset string) {
var language, namespace, variant, locale string
language, namespace = ctx.UserValue("language").(string), ctx.UserValue("namespace").(string)
if !utils.IsStringInSlice(language, languages) {
return false, ""
}
if v := ctx.UserValue("variant"); v != nil {
variant = v.(string)
locale = fmt.Sprintf("%s-%s", language, variant)
} else {
locale = language
}
ll := language + "-" + strings.ToUpper(language)
alias, ok := aliases[locale]
switch {
case ok:
return true, fmt.Sprintf("locales/%s/%s.json", alias, namespace)
case utils.IsStringInSlice(locale, dirs):
return true, fmt.Sprintf("locales/%s/%s.json", locale, namespace)
case utils.IsStringInSlice(ll, dirs):
return true, fmt.Sprintf("locales/%s-%s/%s.json", language, strings.ToUpper(language), namespace)
default:
return true, fmt.Sprintf("locales/%s/%s.json", locale, namespace)
}
}
}
func newLocalesEmbeddedHandler() (handler fasthttp.RequestHandler) {
etags := map[string][]byte{}
getEmbedETags(locales, "locales", etags)
getAssetName := newLocalesPathResolver()
return func(ctx *fasthttp.RequestCtx) {
supported, asset := getAssetName(ctx)
if !supported {
handlers.SetStatusCodeResponse(ctx, fasthttp.StatusNotFound)
return
}
if etag, ok := etags[asset]; ok {
ctx.Response.Header.SetBytesKV(headerETag, etag)
ctx.Response.Header.SetBytesKV(headerCacheControl, headerValueCacheControlETaggedAssets)
if bytes.Equal(etag, ctx.Request.Header.PeekBytes(headerIfNoneMatch)) {
ctx.SetStatusCode(fasthttp.StatusNotModified)
return
}
}
var (
data []byte
err error
)
if data, err = locales.ReadFile(asset); err != nil {
data = []byte("{}")
}
middlewares.SetContentTypeApplicationJSON(ctx)
ctx.SetBody(data)
}
}
func getEmbedETags(embedFS embed.FS, root string, etags map[string][]byte) {
var (
err error
entries []fs.DirEntry
)
if entries, err = embedFS.ReadDir(root); err != nil {
return
}
for _, entry := range entries {
if entry.IsDir() {
getEmbedETags(embedFS, filepath.Join(root, entry.Name()), etags)
continue
}
p := filepath.Join(root, entry.Name())
var data []byte
if data, err = embedFS.ReadFile(p); err != nil {
continue
}
sum := sha1.New() //nolint:gosec // Usage is for collision avoidance not security.
sum.Write(data)
etags[p] = []byte(fmt.Sprintf("%x", sum.Sum(nil)))
}
}
func hfsHandleErr(ctx *fasthttp.RequestCtx, err error) {
switch {
case errors.Is(err, fs.ErrNotExist):
handlers.SetStatusCodeResponse(ctx, fasthttp.StatusNotFound)
case errors.Is(err, fs.ErrPermission):
handlers.SetStatusCodeResponse(ctx, fasthttp.StatusForbidden)
default:
handlers.SetStatusCodeResponse(ctx, fasthttp.StatusInternalServerError)
}
}