Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
caddy
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
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Łukasz Nowak
caddy
Commits
84845a66
Commit
84845a66
authored
Mar 11, 2016
by
Abiola Ibrahim
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix broken build.
parent
f3a183ec
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
33 additions
and
9 deletions
+33
-9
middleware/fileserver.go
middleware/fileserver.go
+33
-9
No files found.
middleware/fileserver.go
View file @
84845a66
...
...
@@ -44,12 +44,19 @@ func (fh *fileHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, e
upath
=
"/"
+
upath
r
.
URL
.
Path
=
upath
}
return
fh
.
serveFile
(
w
,
r
,
file
path
.
Clean
(
upath
))
return
fh
.
serveFile
(
w
,
r
,
path
.
Clean
(
upath
))
}
// serveFile writes the specified file to the HTTP response.
// name is '/'-separated, not filepath.Separator.
func
(
fh
*
fileHandler
)
serveFile
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
,
name
string
)
(
int
,
error
)
{
// Prevent absolute path access on Windows.
// TODO remove when stdlib http.Dir fixes this.
if
runtimeGoos
==
"windows"
{
if
filepath
.
IsAbs
(
name
[
1
:
])
{
return
http
.
StatusNotFound
,
nil
}
}
f
,
err
:=
fh
.
root
.
Open
(
name
)
if
err
!=
nil
{
if
os
.
IsNotExist
(
err
)
{
...
...
@@ -114,6 +121,28 @@ func (fh *fileHandler) serveFile(w http.ResponseWriter, r *http.Request, name st
return
http
.
StatusNotFound
,
nil
}
// If file is on hide list.
if
fh
.
isHidden
(
d
.
Name
())
{
return
http
.
StatusNotFound
,
nil
}
// Note: Errors generated by ServeContent are written immediately
// to the response. This usually only happens if seeking fails (rare).
http
.
ServeContent
(
w
,
r
,
d
.
Name
(),
d
.
ModTime
(),
f
)
return
http
.
StatusOK
,
nil
}
// isHidden checks if file with name is on hide list.
func
(
fh
fileHandler
)
isHidden
(
name
string
)
bool
{
// Clean up on Windows.
// Remove trailing dots and trim whitespaces.
if
runtimeGoos
==
"windows"
{
name
=
strings
.
TrimSpace
(
name
)
for
strings
.
HasSuffix
(
name
,
"."
)
{
name
=
name
[
:
len
(
name
)
-
1
]
}
}
// If the file is supposed to be hidden, return a 404
// (TODO: If the slice gets large, a set may be faster)
for
_
,
hiddenPath
:=
range
fh
.
hide
{
...
...
@@ -123,16 +152,11 @@ func (fh *fileHandler) serveFile(w http.ResponseWriter, r *http.Request, name st
// TODO: This matches file NAME only, regardless of path. In other
// words, trying to serve another file with the same name as the
// active config file will result in a 404 when it shouldn't.
if
strings
.
EqualFold
(
d
.
Name
(),
path
.
Base
(
hiddenPath
))
{
return
http
.
StatusNotFound
,
nil
if
strings
.
EqualFold
(
name
,
file
path
.
Base
(
hiddenPath
))
{
return
true
}
}
// Note: Errors generated by ServeContent are written immediately
// to the response. This usually only happens if seeking fails (rare).
http
.
ServeContent
(
w
,
r
,
d
.
Name
(),
d
.
ModTime
(),
f
)
return
http
.
StatusOK
,
nil
return
false
}
// redirect is taken from http.localRedirect of the std lib. It
...
...
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