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
b7c8afab
Commit
b7c8afab
authored
Apr 12, 2015
by
Matthew Holt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Respond with 404 if requesting server's config file
parent
6ca475de
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
32 additions
and
10 deletions
+32
-10
config/config.go
config/config.go
+13
-1
server/fileserver.go
server/fileserver.go
+18
-8
server/server.go
server/server.go
+1
-1
No files found.
config/config.go
View file @
b7c8afab
...
@@ -44,6 +44,9 @@ type Config struct {
...
@@ -44,6 +44,9 @@ type Config struct {
// MaxCPU is the maximum number of cores for the whole process to use
// MaxCPU is the maximum number of cores for the whole process to use
MaxCPU
int
MaxCPU
int
// The path to the configuration file from which this was loaded
ConfigFile
string
}
}
// Address returns the host:port of c as a string.
// Address returns the host:port of c as a string.
...
@@ -75,7 +78,16 @@ func Load(filename string) ([]Config, error) {
...
@@ -75,7 +78,16 @@ func Load(filename string) ([]Config, error) {
return
nil
,
err
return
nil
,
err
}
}
return
p
.
parse
()
cfgs
,
err
:=
p
.
parse
()
if
err
!=
nil
{
return
[]
Config
{},
err
}
for
i
:=
0
;
i
<
len
(
cfgs
);
i
++
{
cfgs
[
i
]
.
ConfigFile
=
filename
}
return
cfgs
,
nil
}
}
// IsNotFound returns whether or not the error is
// IsNotFound returns whether or not the error is
...
...
server/fileserver.go
View file @
b7c8afab
...
@@ -10,20 +10,22 @@ import (
...
@@ -10,20 +10,22 @@ import (
"github.com/mholt/caddy/middleware/browse"
"github.com/mholt/caddy/middleware/browse"
)
)
// This FileServer is adapted from the one in net/http
// This FileServer is adapted from the one in net/http by
// by the Go authors. Some modifications have been made.
// the Go authors. Significant modifications have been made.
//
//
//
// License:
// License:
//
//
// Copyright 2009 The Go Authors. All rights reserved.
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// license that can be found in the LICENSE file.
func
FileServer
(
root
http
.
FileSystem
)
middleware
.
Handler
{
func
FileServer
(
root
http
.
FileSystem
,
hide
[]
string
)
middleware
.
Handler
{
return
&
fileHandler
{
root
}
return
&
fileHandler
{
root
:
root
,
hide
:
hide
}
}
}
type
fileHandler
struct
{
type
fileHandler
struct
{
root
http
.
FileSystem
root
http
.
FileSystem
hide
[]
string
// list of files to treat as "Not Found"
}
}
func
(
f
*
fileHandler
)
ServeHTTP
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
(
int
,
error
)
{
func
(
f
*
fileHandler
)
ServeHTTP
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
(
int
,
error
)
{
...
@@ -32,12 +34,12 @@ func (f *fileHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, er
...
@@ -32,12 +34,12 @@ func (f *fileHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, er
upath
=
"/"
+
upath
upath
=
"/"
+
upath
r
.
URL
.
Path
=
upath
r
.
URL
.
Path
=
upath
}
}
return
serveFile
(
w
,
r
,
f
.
root
,
path
.
Clean
(
upath
))
return
f
.
serveFile
(
w
,
r
,
path
.
Clean
(
upath
))
}
}
// name is '/'-separated, not filepath.Separator.
// name is '/'-separated, not filepath.Separator.
func
serveFile
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
,
fs
http
.
FileSystem
,
name
string
)
(
int
,
error
)
{
func
(
fh
*
fileHandler
)
serveFile
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
,
name
string
)
(
int
,
error
)
{
f
,
err
:=
f
s
.
Open
(
name
)
f
,
err
:=
f
h
.
root
.
Open
(
name
)
if
err
!=
nil
{
if
err
!=
nil
{
if
os
.
IsPermission
(
err
)
{
if
os
.
IsPermission
(
err
)
{
return
http
.
StatusForbidden
,
err
return
http
.
StatusForbidden
,
err
...
@@ -58,7 +60,7 @@ func serveFile(w http.ResponseWriter, r *http.Request, fs http.FileSystem, name
...
@@ -58,7 +60,7 @@ func serveFile(w http.ResponseWriter, r *http.Request, fs http.FileSystem, name
if
d
.
IsDir
()
{
if
d
.
IsDir
()
{
for
_
,
indexPage
:=
range
browse
.
IndexPages
{
for
_
,
indexPage
:=
range
browse
.
IndexPages
{
index
:=
strings
.
TrimSuffix
(
name
,
"/"
)
+
"/"
+
indexPage
index
:=
strings
.
TrimSuffix
(
name
,
"/"
)
+
"/"
+
indexPage
ff
,
err
:=
f
s
.
Open
(
index
)
ff
,
err
:=
f
h
.
root
.
Open
(
index
)
if
err
==
nil
{
if
err
==
nil
{
defer
ff
.
Close
()
defer
ff
.
Close
()
dd
,
err
:=
ff
.
Stat
()
dd
,
err
:=
ff
.
Stat
()
...
@@ -78,6 +80,14 @@ func serveFile(w http.ResponseWriter, r *http.Request, fs http.FileSystem, name
...
@@ -78,6 +80,14 @@ func serveFile(w http.ResponseWriter, r *http.Request, fs http.FileSystem, name
return
http
.
StatusNotFound
,
nil
return
http
.
StatusNotFound
,
nil
}
}
// 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
{
if
d
.
Name
()
==
hiddenPath
{
return
http
.
StatusNotFound
,
nil
}
}
// Note: Errors generated by ServeContent are written immediately
// Note: Errors generated by ServeContent are written immediately
// to the response. This usually only happens if seeking fails (rare).
// to the response. This usually only happens if seeking fails (rare).
http
.
ServeContent
(
w
,
r
,
d
.
Name
(),
d
.
ModTime
(),
f
)
http
.
ServeContent
(
w
,
r
,
d
.
Name
(),
d
.
ModTime
(),
f
)
...
...
server/server.go
View file @
b7c8afab
...
@@ -134,7 +134,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
...
@@ -134,7 +134,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// on its config. This method should be called last before
// on its config. This method should be called last before
// ListenAndServe begins.
// ListenAndServe begins.
func
(
s
*
Server
)
buildStack
()
error
{
func
(
s
*
Server
)
buildStack
()
error
{
s
.
fileServer
=
FileServer
(
http
.
Dir
(
s
.
config
.
Root
))
s
.
fileServer
=
FileServer
(
http
.
Dir
(
s
.
config
.
Root
)
,
[]
string
{
s
.
config
.
ConfigFile
}
)
// TODO: We only compile middleware for the "/" scope.
// TODO: We only compile middleware for the "/" scope.
// Partial support for multiple location contexts already
// Partial support for multiple location contexts already
...
...
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