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
f7a70266
Commit
f7a70266
authored
Oct 30, 2017
by
Mohammed Al Sahaf
Committed by
Toby Allen
Oct 29, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement per-site index (#1906)
parent
fc75527e
Changes
13
Show whitespace changes
Inline
Side-by-side
Showing
13 changed files
with
110 additions
and
24 deletions
+110
-24
caddyhttp/browse/browse.go
caddyhttp/browse/browse.go
+1
-1
caddyhttp/browse/setup.go
caddyhttp/browse/setup.go
+3
-2
caddyhttp/httpserver/plugin.go
caddyhttp/httpserver/plugin.go
+3
-1
caddyhttp/httpserver/server.go
caddyhttp/httpserver/server.go
+1
-1
caddyhttp/httpserver/siteconfig.go
caddyhttp/httpserver/siteconfig.go
+3
-0
caddyhttp/index/index.go
caddyhttp/index/index.go
+4
-2
caddyhttp/index/index_test.go
caddyhttp/index/index_test.go
+76
-4
caddyhttp/push/handler.go
caddyhttp/push/handler.go
+1
-2
caddyhttp/push/handler_test.go
caddyhttp/push/handler_test.go
+2
-1
caddyhttp/push/push.go
caddyhttp/push/push.go
+4
-3
caddyhttp/push/setup.go
caddyhttp/push/setup.go
+1
-1
caddyhttp/staticfiles/fileserver.go
caddyhttp/staticfiles/fileserver.go
+8
-4
caddyhttp/staticfiles/fileserver_test.go
caddyhttp/staticfiles/fileserver_test.go
+3
-2
No files found.
caddyhttp/browse/browse.go
View file @
f7a70266
...
...
@@ -252,7 +252,7 @@ func directoryListing(files []os.FileInfo, canGoUp bool, urlPath string, config
for
_
,
f
:=
range
files
{
name
:=
f
.
Name
()
for
_
,
indexName
:=
range
staticfile
s
.
IndexPages
{
for
_
,
indexName
:=
range
config
.
F
s
.
IndexPages
{
if
name
==
indexName
{
hasIndexFile
=
true
break
...
...
caddyhttp/browse/setup.go
View file @
f7a70266
...
...
@@ -80,6 +80,7 @@ func browseParse(c *caddy.Controller) ([]Config, error) {
bc
.
Fs
=
staticfiles
.
FileServer
{
Root
:
http
.
Dir
(
cfg
.
Root
),
Hide
:
cfg
.
HiddenFiles
,
IndexPages
:
cfg
.
IndexPages
,
}
// Second argument would be the template file to use
...
...
caddyhttp/httpserver/plugin.go
View file @
f7a70266
...
...
@@ -27,6 +27,7 @@ import (
"github.com/mholt/caddy"
"github.com/mholt/caddy/caddyfile"
"github.com/mholt/caddy/caddyhttp/staticfiles"
"github.com/mholt/caddy/caddytls"
)
...
...
@@ -155,6 +156,7 @@ func (h *httpContext) InspectServerBlocks(sourceFile string, serverBlocks []cadd
AltTLSSNIPort
:
altTLSSNIPort
,
},
originCaddyfile
:
sourceFile
,
IndexPages
:
staticfiles
.
DefaultIndexPages
,
}
h
.
saveConfig
(
key
,
cfg
)
}
...
...
@@ -234,7 +236,7 @@ func GetConfig(c *caddy.Controller) *SiteConfig {
// we should only get here during tests because directive
// actions typically skip the server blocks where we make
// the configs
cfg
:=
&
SiteConfig
{
Root
:
Root
,
TLS
:
new
(
caddytls
.
Config
)}
cfg
:=
&
SiteConfig
{
Root
:
Root
,
TLS
:
new
(
caddytls
.
Config
)
,
IndexPages
:
staticfiles
.
DefaultIndexPages
}
ctx
.
saveConfig
(
key
,
cfg
)
return
cfg
}
...
...
caddyhttp/httpserver/server.go
View file @
f7a70266
...
...
@@ -142,7 +142,7 @@ func NewServer(addr string, group []*SiteConfig) (*Server, error) {
// Compile custom middleware for every site (enables virtual hosting)
for
_
,
site
:=
range
group
{
stack
:=
Handler
(
staticfiles
.
FileServer
{
Root
:
http
.
Dir
(
site
.
Root
),
Hide
:
site
.
HiddenFiles
})
stack
:=
Handler
(
staticfiles
.
FileServer
{
Root
:
http
.
Dir
(
site
.
Root
),
Hide
:
site
.
HiddenFiles
,
IndexPages
:
site
.
IndexPages
})
for
i
:=
len
(
site
.
middleware
)
-
1
;
i
>=
0
;
i
--
{
stack
=
site
.
middleware
[
i
](
stack
)
}
...
...
caddyhttp/httpserver/siteconfig.go
View file @
f7a70266
...
...
@@ -26,6 +26,9 @@ type SiteConfig struct {
// The address of the site
Addr
Address
// The list of viable index page names of the site
IndexPages
[]
string
// The hostname to bind listener to;
// defaults to Addr.Host
ListenHost
string
...
...
caddyhttp/index/index.go
View file @
f7a70266
...
...
@@ -16,7 +16,7 @@ package index
import
(
"github.com/mholt/caddy"
"github.com/mholt/caddy/caddyhttp/
staticfiles
"
"github.com/mholt/caddy/caddyhttp/
httpserver
"
)
func
init
()
{
...
...
@@ -29,6 +29,8 @@ func init() {
func
setupIndex
(
c
*
caddy
.
Controller
)
error
{
var
index
[]
string
cfg
:=
httpserver
.
GetConfig
(
c
)
for
c
.
Next
()
{
args
:=
c
.
RemainingArgs
()
...
...
@@ -40,7 +42,7 @@ func setupIndex(c *caddy.Controller) error {
index
=
append
(
index
,
in
)
}
staticfiles
.
IndexPages
=
index
cfg
.
IndexPages
=
index
}
return
nil
...
...
caddyhttp/index/index_test.go
View file @
f7a70266
...
...
@@ -18,6 +18,7 @@ import (
"testing"
"github.com/mholt/caddy"
"github.com/mholt/caddy/caddyhttp/httpserver"
"github.com/mholt/caddy/caddyhttp/staticfiles"
)
...
...
@@ -31,7 +32,7 @@ func TestIndexIncompleteParams(t *testing.T) {
}
func
TestIndex
(
t
*
testing
.
T
)
{
c
:=
caddy
.
NewTestController
(
""
,
"index a.html b.html c.html"
)
c
:=
caddy
.
NewTestController
(
"
http
"
,
"index a.html b.html c.html"
)
err
:=
setupIndex
(
c
)
if
err
!=
nil
{
...
...
@@ -40,14 +41,85 @@ func TestIndex(t *testing.T) {
expectedIndex
:=
[]
string
{
"a.html"
,
"b.html"
,
"c.html"
}
if
len
(
staticfiles
.
IndexPages
)
!=
3
{
t
.
Errorf
(
"Expected 3 values, got %v"
,
len
(
staticfiles
.
IndexPages
))
siteConfig
:=
httpserver
.
GetConfig
(
c
)
if
len
(
siteConfig
.
IndexPages
)
!=
len
(
expectedIndex
)
{
t
.
Errorf
(
"Expected 3 values, got %v"
,
len
(
siteConfig
.
IndexPages
))
}
// Ensure ordering is correct
for
i
,
actual
:=
range
s
taticfiles
.
IndexPages
{
for
i
,
actual
:=
range
s
iteConfig
.
IndexPages
{
if
actual
!=
expectedIndex
[
i
]
{
t
.
Errorf
(
"Expected value in position %d to be %v, got %v"
,
i
,
expectedIndex
[
i
],
actual
)
}
}
}
func
TestMultiSiteIndexWithEitherHasDefault
(
t
*
testing
.
T
)
{
// TestIndex already covers the correctness of the directive
// when used on a single controller, so no need to verify test setupIndex again.
// This sets the stage for the actual verification.
customIndex
:=
caddy
.
NewTestController
(
"http"
,
"index a.html b.html"
)
// setupIndex against customIdx should not pollute the
// index list for other controllers.
err
:=
setupIndex
(
customIndex
)
if
err
!=
nil
{
t
.
Errorf
(
"Expected no errors, got: %v"
,
err
)
}
// Represents a virtual host with no index directive.
defaultIndex
:=
caddy
.
NewTestController
(
"http"
,
""
)
// Not calling setupIndex because it guards against lack of arguments,
// and we need to ensure the site gets the default set of index pages.
siteConfig
:=
httpserver
.
GetConfig
(
defaultIndex
)
// In case the index directive is not used, the virtual host
// should receive staticfiles.DefaultIndexPages slice. The length, as checked here,
// and the values, as checked in the upcoming loop, should match.
if
len
(
siteConfig
.
IndexPages
)
!=
len
(
staticfiles
.
DefaultIndexPages
)
{
t
.
Errorf
(
"Expected %d values, got %d"
,
len
(
staticfiles
.
DefaultIndexPages
),
len
(
siteConfig
.
IndexPages
))
}
// Ensure values match the expected default index pages
for
i
,
actual
:=
range
siteConfig
.
IndexPages
{
if
actual
!=
staticfiles
.
DefaultIndexPages
[
i
]
{
t
.
Errorf
(
"Expected value in position %d to be %v, got %v"
,
i
,
staticfiles
.
DefaultIndexPages
[
i
],
actual
)
}
}
}
func
TestPerSiteIndexPageIsolation
(
t
*
testing
.
T
)
{
firstIndex
:=
"first.html"
secondIndex
:=
"second.html"
// Create two sites with different index page configurations
firstSite
:=
caddy
.
NewTestController
(
"http"
,
"index first.html"
)
err
:=
setupIndex
(
firstSite
)
if
err
!=
nil
{
t
.
Errorf
(
"Expected no errors, got: %v"
,
err
)
}
secondSite
:=
caddy
.
NewTestController
(
"http"
,
"index second.html"
)
err
=
setupIndex
(
secondSite
)
if
err
!=
nil
{
t
.
Errorf
(
"Expected no errors, got: %v"
,
err
)
}
firstSiteConfig
:=
httpserver
.
GetConfig
(
firstSite
)
if
firstSiteConfig
.
IndexPages
[
0
]
!=
firstIndex
{
t
.
Errorf
(
"Expected index for first site as %s, received %s"
,
firstIndex
,
firstSiteConfig
.
IndexPages
[
0
])
}
secondSiteConfig
:=
httpserver
.
GetConfig
(
secondSite
)
if
secondSiteConfig
.
IndexPages
[
0
]
!=
secondIndex
{
t
.
Errorf
(
"Expected index for second site as %s, received %s"
,
secondIndex
,
secondSiteConfig
.
IndexPages
[
0
])
}
// They should have different index pages, as per the provided config.
if
firstSiteConfig
.
IndexPages
[
0
]
==
secondSiteConfig
.
IndexPages
[
0
]
{
t
.
Errorf
(
"Expected different index pages for both sites, got %s for first and %s for second"
,
firstSiteConfig
.
IndexPages
[
0
],
secondSiteConfig
.
IndexPages
[
0
])
}
}
caddyhttp/push/handler.go
View file @
f7a70266
...
...
@@ -19,7 +19,6 @@ import (
"strings"
"github.com/mholt/caddy/caddyhttp/httpserver"
"github.com/mholt/caddy/caddyhttp/staticfiles"
)
func
(
h
Middleware
)
ServeHTTP
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
(
int
,
error
)
{
...
...
@@ -44,7 +43,7 @@ outer:
matches
:=
httpserver
.
Path
(
urlPath
)
.
Matches
(
rule
.
Path
)
// Also check IndexPages when requesting a directory
if
!
matches
{
indexFile
,
isIndexFile
:=
httpserver
.
IndexFile
(
h
.
Root
,
urlPath
,
staticfiles
.
I
ndexPages
)
indexFile
,
isIndexFile
:=
httpserver
.
IndexFile
(
h
.
Root
,
urlPath
,
h
.
i
ndexPages
)
if
isIndexFile
{
matches
=
httpserver
.
Path
(
indexFile
)
.
Matches
(
rule
.
Path
)
}
...
...
caddyhttp/push/handler_test.go
View file @
f7a70266
...
...
@@ -394,6 +394,7 @@ func TestMiddlewareShouldPushIndexFile(t *testing.T) {
}},
},
Root
:
http
.
Dir
(
root
),
indexPages
:
[]
string
{
indexFile
},
}
indexFilePath
:=
filepath
.
Join
(
root
,
indexFile
)
...
...
caddyhttp/push/push.go
View file @
f7a70266
...
...
@@ -39,6 +39,7 @@ type (
Next
httpserver
.
Handler
Rules
[]
Rule
Root
http
.
FileSystem
indexPages
[]
string
// will be injected from SiteConfig on setup
}
ruleOp
func
([]
Resource
)
...
...
caddyhttp/push/setup.go
View file @
f7a70266
...
...
@@ -50,7 +50,7 @@ func setup(c *caddy.Controller) error {
cfg
:=
httpserver
.
GetConfig
(
c
)
cfg
.
AddMiddleware
(
func
(
next
httpserver
.
Handler
)
httpserver
.
Handler
{
return
Middleware
{
Next
:
next
,
Rules
:
rules
,
Root
:
http
.
Dir
(
cfg
.
Root
)}
return
Middleware
{
Next
:
next
,
Rules
:
rules
,
Root
:
http
.
Dir
(
cfg
.
Root
)
,
indexPages
:
cfg
.
IndexPages
}
})
return
nil
...
...
caddyhttp/staticfiles/fileserver.go
View file @
f7a70266
...
...
@@ -45,6 +45,10 @@ import (
type
FileServer
struct
{
Root
http
.
FileSystem
// jailed access to the file system
Hide
[]
string
// list of files for which to respond with "Not Found"
// A list of pages that may be understood as the "index" files to directories.
// Injected from *SiteConfig.
IndexPages
[]
string
}
// ServeHTTP serves static files for r according to fs's configuration.
...
...
@@ -118,7 +122,7 @@ func (fs FileServer) serveFile(w http.ResponseWriter, r *http.Request) (int, err
// if an index file was explicitly requested, strip file name from the request
// ("/foo/index.html" -> "/foo/")
var
requestPage
=
path
.
Base
(
urlCopy
.
Path
)
for
_
,
indexPage
:=
range
IndexPages
{
for
_
,
indexPage
:=
range
fs
.
IndexPages
{
if
requestPage
==
indexPage
{
urlCopy
.
Path
=
urlCopy
.
Path
[
:
len
(
urlCopy
.
Path
)
-
len
(
indexPage
)]
redir
=
true
...
...
@@ -134,7 +138,7 @@ func (fs FileServer) serveFile(w http.ResponseWriter, r *http.Request) (int, err
// use contents of an index file, if present, for directory requests
if
d
.
IsDir
()
{
for
_
,
indexPage
:=
range
IndexPages
{
for
_
,
indexPage
:=
range
fs
.
IndexPages
{
indexPath
:=
path
.
Join
(
reqPath
,
indexPage
)
indexFile
,
err
:=
fs
.
Root
.
Open
(
indexPath
)
if
err
!=
nil
{
...
...
@@ -253,9 +257,9 @@ func calculateEtag(d os.FileInfo) string {
return
`"`
+
t
+
s
+
`"`
}
// IndexPages is a list of pages that may be understood as
//
Default
IndexPages is a list of pages that may be understood as
// the "index" files to directories.
var
IndexPages
=
[]
string
{
var
Default
IndexPages
=
[]
string
{
"index.html"
,
"index.htm"
,
"index.txt"
,
...
...
caddyhttp/staticfiles/fileserver_test.go
View file @
f7a70266
...
...
@@ -38,6 +38,7 @@ func TestServeHTTP(t *testing.T) {
fileserver
:=
FileServer
{
Root
:
http
.
Dir
(
filepath
.
Join
(
tmpWebRootDir
,
webrootName
)),
Hide
:
[]
string
{
"dir/hidden.html"
},
IndexPages
:
DefaultIndexPages
,
}
movedPermanently
:=
"Moved Permanently"
...
...
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