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
21b2e5a0
Commit
21b2e5a0
authored
Mar 28, 2015
by
Matthew Holt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactored file server to return errors
parent
878ae7ea
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
20 additions
and
12 deletions
+20
-12
server/fileserver.go
server/fileserver.go
+20
-12
No files found.
server/fileserver.go
View file @
21b2e5a0
...
...
@@ -2,9 +2,11 @@ package server
import
(
"net/http"
"os"
"path"
"strings"
"github.com/mholt/caddy/middleware"
"github.com/mholt/caddy/middleware/browse"
)
...
...
@@ -16,7 +18,7 @@ import (
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
func
FileServer
(
root
http
.
FileSystem
)
http
.
Handler
{
func
FileServer
(
root
http
.
FileSystem
)
middleware
.
Handler
{
return
&
fileHandler
{
root
}
}
...
...
@@ -24,30 +26,32 @@ type fileHandler struct {
root
http
.
FileSystem
}
func
(
f
*
fileHandler
)
ServeHTTP
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
func
(
f
*
fileHandler
)
ServeHTTP
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
(
int
,
error
)
{
upath
:=
r
.
URL
.
Path
if
!
strings
.
HasPrefix
(
upath
,
"/"
)
{
upath
=
"/"
+
upath
r
.
URL
.
Path
=
upath
}
serveFile
(
w
,
r
,
f
.
root
,
path
.
Clean
(
upath
),
true
)
return
serveFile
(
w
,
r
,
f
.
root
,
path
.
Clean
(
upath
)
)
}
// name is '/'-separated, not filepath.Separator.
func
serveFile
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
,
fs
http
.
FileSystem
,
name
string
,
redirect
bool
)
{
func
serveFile
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
,
fs
http
.
FileSystem
,
name
string
)
(
int
,
error
)
{
f
,
err
:=
fs
.
Open
(
name
)
if
err
!=
nil
{
// TODO expose actual error?
http
.
NotFound
(
w
,
r
)
return
if
os
.
IsPermission
(
err
)
{
return
http
.
StatusForbidden
,
err
}
return
http
.
StatusNotFound
,
nil
}
defer
f
.
Close
()
d
,
err1
:=
f
.
Stat
()
if
err1
!=
nil
{
// TODO expose actual error?
http
.
NotFound
(
w
,
r
)
return
if
os
.
IsPermission
(
err
)
{
return
http
.
StatusForbidden
,
err
}
return
http
.
StatusNotFound
,
nil
}
// use contents of an index file, if present, for directory
...
...
@@ -69,10 +73,14 @@ func serveFile(w http.ResponseWriter, r *http.Request, fs http.FileSystem, name
}
// Still a directory? (we didn't find an index file)
// Return 404 to hide the fact that the folder exists
if
d
.
IsDir
()
{
http
.
NotFound
(
w
,
r
)
// 404 instead of 403 to hide the fact that the folder exists
return
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
}
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