Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
galene
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
nexedi
galene
Commits
6e84b641
Commit
6e84b641
authored
Sep 18, 2020
by
Juliusz Chroboczek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improve cachability of the web server.
parent
4d79d10d
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
58 additions
and
2 deletions
+58
-2
webserver/webserver.go
webserver/webserver.go
+58
-2
No files found.
webserver/webserver.go
View file @
6e84b641
...
...
@@ -42,7 +42,7 @@ func Serve(address string, dataDir string) {
http
.
HandleFunc
(
"/ice-servers.json"
,
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
mungeHeader
(
w
)
http
.
S
erveFile
(
w
,
r
,
s
erveFile
(
w
,
r
,
filepath
.
Join
(
dataDir
,
"ice-servers.json"
))
})
http
.
HandleFunc
(
"/public-groups.json"
,
publicHandler
)
...
...
@@ -113,6 +113,21 @@ type fileHandler struct {
root
http
.
FileSystem
}
func
makeEtag
(
d
os
.
FileInfo
)
string
{
return
fmt
.
Sprintf
(
"
\"
%v-%v
\"
"
,
d
.
Size
(),
d
.
ModTime
()
.
UnixNano
())
}
const
(
normalCacheControl
=
"max-age=1800"
veryCachableCacheControl
=
"max-age=86400"
)
func
isVeryCachable
(
p
string
)
bool
{
return
strings
.
HasPrefix
(
p
,
"/fonts/"
)
||
strings
.
HasPrefix
(
p
,
"/scripts/"
)
||
strings
.
HasPrefix
(
p
,
"/css/"
)
}
func
(
fh
*
fileHandler
)
ServeHTTP
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
mungeHeader
(
w
)
p
:=
r
.
URL
.
Path
...
...
@@ -165,6 +180,47 @@ func (fh *fileHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
p
=
index
}
etag
:=
makeEtag
(
d
)
if
etag
!=
""
{
w
.
Header
()
.
Add
(
"ETag"
,
etag
)
}
cc
:=
normalCacheControl
if
isVeryCachable
(
p
)
{
cc
=
veryCachableCacheControl
}
w
.
Header
()
.
Add
(
"Cache-Control"
,
cc
)
http
.
ServeContent
(
w
,
r
,
d
.
Name
(),
d
.
ModTime
(),
f
)
}
// serveFile is similar to http.ServeFile, except that it doesn't check
// for .. and adds cachability headers.
func
serveFile
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
,
p
string
)
{
f
,
err
:=
os
.
Open
(
p
)
if
err
!=
nil
{
httpError
(
w
,
err
)
return
}
defer
f
.
Close
()
d
,
err
:=
f
.
Stat
()
if
err
!=
nil
{
httpError
(
w
,
err
)
return
}
if
d
.
IsDir
()
{
httpError
(
w
,
ErrIsDirectory
)
return
}
etag
:=
makeEtag
(
d
)
if
etag
!=
""
{
w
.
Header
()
.
Add
(
"ETag"
,
etag
)
}
w
.
Header
()
.
Add
(
"Cache-Control"
,
normalCacheControl
)
http
.
ServeContent
(
w
,
r
,
d
.
Name
(),
d
.
ModTime
(),
f
)
}
...
...
@@ -210,7 +266,7 @@ func groupHandler(w http.ResponseWriter, r *http.Request) {
return
}
http
.
S
erveFile
(
w
,
r
,
filepath
.
Join
(
StaticRoot
,
"sfu.html"
))
s
erveFile
(
w
,
r
,
filepath
.
Join
(
StaticRoot
,
"sfu.html"
))
}
func
publicHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment