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
6e0317a7
Commit
6e0317a7
authored
Jul 24, 2017
by
Matt Holt
Committed by
GitHub
Jul 24, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1747 from twdkeule/fix-index-push
Pushes for /index.html work when surfing to /
parents
40b52fb0
20f76a25
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
72 additions
and
3 deletions
+72
-3
caddyhttp/push/handler.go
caddyhttp/push/handler.go
+8
-1
caddyhttp/push/handler_test.go
caddyhttp/push/handler_test.go
+60
-0
caddyhttp/push/push.go
caddyhttp/push/push.go
+1
-0
caddyhttp/push/setup.go
caddyhttp/push/setup.go
+3
-2
No files found.
caddyhttp/push/handler.go
View file @
6e0317a7
...
...
@@ -5,6 +5,7 @@ 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
)
{
...
...
@@ -25,7 +26,13 @@ func (h Middleware) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, erro
// push first
outer
:
for
_
,
rule
:=
range
h
.
Rules
{
if
httpserver
.
Path
(
r
.
URL
.
Path
)
.
Matches
(
rule
.
Path
)
{
urlPath
:=
r
.
URL
.
Path
matches
:=
httpserver
.
Path
(
urlPath
)
.
Matches
(
rule
.
Path
)
// Also check IndexPages when requesting a directory
if
!
matches
{
_
,
matches
=
httpserver
.
IndexFile
(
h
.
Root
,
urlPath
,
staticfiles
.
IndexPages
)
}
if
matches
{
for
_
,
resource
:=
range
rule
.
Resources
{
pushErr
:=
pusher
.
Push
(
resource
.
Path
,
&
http
.
PushOptions
{
Method
:
resource
.
Method
,
...
...
caddyhttp/push/handler_test.go
View file @
6e0317a7
...
...
@@ -2,8 +2,11 @@ package push
import
(
"errors"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"reflect"
"testing"
...
...
@@ -307,6 +310,63 @@ func TestMiddlewareShouldInterceptLinkHeaderPusherError(t *testing.T) {
comparePushedResources
(
t
,
expectedPushedResources
,
pushingWriter
.
pushed
)
}
func
TestMiddlewareShouldPushIndexFile
(
t
*
testing
.
T
)
{
// given
indexFile
:=
"/index.html"
request
,
err
:=
http
.
NewRequest
(
http
.
MethodGet
,
"/"
,
nil
)
// Request root directory, not indexfile itself
if
err
!=
nil
{
t
.
Fatalf
(
"Could not create HTTP request: %v"
,
err
)
}
root
,
err
:=
ioutil
.
TempDir
(
""
,
"caddy"
)
if
err
!=
nil
{
t
.
Fatalf
(
"Could not create temporary directory: %v"
,
err
)
}
defer
os
.
Remove
(
root
)
middleware
:=
Middleware
{
Next
:
httpserver
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
(
int
,
error
)
{
return
0
,
nil
}),
Rules
:
[]
Rule
{
{
Path
:
indexFile
,
Resources
:
[]
Resource
{
{
Path
:
"/index.css"
,
Method
:
http
.
MethodGet
},
}},
},
Root
:
http
.
Dir
(
root
),
}
indexFilePath
:=
filepath
.
Join
(
root
,
indexFile
)
_
,
err
=
os
.
Create
(
indexFilePath
)
if
err
!=
nil
{
t
.
Fatalf
(
"Could not create index file: %s: %v"
,
indexFile
,
err
)
}
defer
os
.
Remove
(
indexFilePath
)
pushingWriter
:=
&
MockedPusher
{
ResponseWriter
:
httptest
.
NewRecorder
(),
returnedError
:
errors
.
New
(
"Cannot push right now"
),
}
// when
_
,
err2
:=
middleware
.
ServeHTTP
(
pushingWriter
,
request
)
// then
if
err2
!=
nil
{
t
.
Error
(
"Should not return error"
)
}
expectedPushedResources
:=
map
[
string
]
*
http
.
PushOptions
{
"/index.css"
:
{
Method
:
http
.
MethodGet
,
Header
:
http
.
Header
{},
},
}
comparePushedResources
(
t
,
expectedPushedResources
,
pushingWriter
.
pushed
)
}
func
comparePushedResources
(
t
*
testing
.
T
,
expected
,
actual
map
[
string
]
*
http
.
PushOptions
)
{
if
len
(
expected
)
!=
len
(
actual
)
{
t
.
Errorf
(
"Expected %d pushed resources, actual: %d"
,
len
(
expected
),
len
(
actual
))
...
...
caddyhttp/push/push.go
View file @
6e0317a7
...
...
@@ -24,6 +24,7 @@ type (
Middleware
struct
{
Next
httpserver
.
Handler
Rules
[]
Rule
Root
http
.
FileSystem
}
ruleOp
func
([]
Resource
)
...
...
caddyhttp/push/setup.go
View file @
6e0317a7
...
...
@@ -34,8 +34,9 @@ func setup(c *caddy.Controller) error {
return
err
}
httpserver
.
GetConfig
(
c
)
.
AddMiddleware
(
func
(
next
httpserver
.
Handler
)
httpserver
.
Handler
{
return
Middleware
{
Next
:
next
,
Rules
:
rules
}
cfg
:=
httpserver
.
GetConfig
(
c
)
cfg
.
AddMiddleware
(
func
(
next
httpserver
.
Handler
)
httpserver
.
Handler
{
return
Middleware
{
Next
:
next
,
Rules
:
rules
,
Root
:
http
.
Dir
(
cfg
.
Root
)}
})
return
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