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
nexedi
caddy
Commits
bdb61f4a
Commit
bdb61f4a
authored
Feb 15, 2017
by
Matt Holt
Committed by
GitHub
Feb 15, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1409 from mastercactapus/not_a_directory
return 404 for "not a directory" errors
parents
1183d91c
18edf586
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
40 additions
and
0 deletions
+40
-0
caddyhttp/staticfiles/fileserver.go
caddyhttp/staticfiles/fileserver.go
+35
-0
caddyhttp/staticfiles/fileserver_test.go
caddyhttp/staticfiles/fileserver_test.go
+5
-0
No files found.
caddyhttp/staticfiles/fileserver.go
View file @
bdb61f4a
...
...
@@ -56,6 +56,9 @@ func (fs FileServer) serveFile(w http.ResponseWriter, r *http.Request, name stri
f
,
err
:=
fs
.
Root
.
Open
(
name
)
if
err
!=
nil
{
// TODO: remove when http.Dir handles this
// Go issue #18984
err
=
mapFSRootOpenErr
(
err
)
if
os
.
IsNotExist
(
err
)
{
return
http
.
StatusNotFound
,
nil
}
else
if
os
.
IsPermission
(
err
)
{
...
...
@@ -230,3 +233,35 @@ var staticEncodingPriority = []string{
"br"
,
"gzip"
,
}
// mapFSRootOpenErr maps the provided non-nil error
// to a possibly better non-nil error. In particular, it turns OS-specific errors
// about opening files in non-directories into os.ErrNotExist.
//
// TODO: remove when http.Dir handles this
// Go issue #18984
func
mapFSRootOpenErr
(
originalErr
error
)
error
{
if
os
.
IsNotExist
(
originalErr
)
||
os
.
IsPermission
(
originalErr
)
{
return
originalErr
}
perr
,
ok
:=
originalErr
.
(
*
os
.
PathError
)
if
!
ok
{
return
originalErr
}
name
:=
perr
.
Path
parts
:=
strings
.
Split
(
name
,
string
(
filepath
.
Separator
))
for
i
:=
range
parts
{
if
parts
[
i
]
==
""
{
continue
}
fi
,
err
:=
os
.
Stat
(
strings
.
Join
(
parts
[
:
i
+
1
],
string
(
filepath
.
Separator
)))
if
err
!=
nil
{
return
originalErr
}
if
!
fi
.
IsDir
()
{
return
os
.
ErrNotExist
}
}
return
originalErr
}
caddyhttp/staticfiles/fileserver_test.go
View file @
bdb61f4a
...
...
@@ -178,6 +178,11 @@ func TestServeHTTP(t *testing.T) {
expectedBodyContent
:
testFiles
[
filepath
.
Join
(
"webroot"
,
"sub"
,
"brotli.html.br"
)],
expectedEtag
:
`W/"1e240-e"`
,
},
// Test 20 - treat existing file as a directory.
{
url
:
"https://foo/file1.html/other"
,
expectedStatus
:
http
.
StatusNotFound
,
},
}
for
i
,
test
:=
range
tests
{
...
...
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