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
761a32a0
Commit
761a32a0
authored
Apr 24, 2017
by
Tw
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
context: add Push action
Signed-off-by:
Tw
<
tw19881113@gmail.com
>
parent
aa7ecb02
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
66 additions
and
7 deletions
+66
-7
caddyhttp/httpserver/context.go
caddyhttp/httpserver/context.go
+23
-0
caddyhttp/httpserver/context_test.go
caddyhttp/httpserver/context_test.go
+35
-1
caddyhttp/markdown/markdown.go
caddyhttp/markdown/markdown.go
+4
-5
caddyhttp/templates/templates.go
caddyhttp/templates/templates.go
+4
-1
No files found.
caddyhttp/httpserver/context.go
View file @
761a32a0
...
...
@@ -29,6 +29,20 @@ type Context struct {
Req
*
http
.
Request
URL
*
url
.
URL
Args
[]
interface
{}
// defined by arguments to .Include
// just used for adding preload links for server push
responseHeader
http
.
Header
}
// NewContextWithHeader creates a context with given response header.
//
// To plugin developer:
// The returned context's exported fileds remain empty,
// you should then initialize them if you want.
func
NewContextWithHeader
(
rh
http
.
Header
)
Context
{
return
Context
{
responseHeader
:
rh
,
}
}
// Include returns the contents of filename relative to the site root.
...
...
@@ -410,6 +424,15 @@ func (c Context) RandomString(minLen, maxLen int) string {
return
string
(
result
)
}
// Push adds a preload link in response header for server push
func
(
c
Context
)
Push
(
link
string
)
string
{
if
c
.
responseHeader
==
nil
{
return
""
}
c
.
responseHeader
.
Add
(
"Link"
,
"<"
+
link
+
">; rel=preload"
)
return
""
}
// buffer pool for .Include context actions
var
includeBufs
=
sync
.
Pool
{
New
:
func
()
interface
{}
{
...
...
caddyhttp/httpserver/context_test.go
View file @
761a32a0
...
...
@@ -6,6 +6,7 @@ import (
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
"net/url"
"os"
"path/filepath"
...
...
@@ -731,8 +732,9 @@ func initTestContext() (Context, error) {
if
err
!=
nil
{
return
Context
{},
err
}
res
:=
httptest
.
NewRecorder
()
return
Context
{
Root
:
http
.
Dir
(
os
.
TempDir
()),
Req
:
request
},
nil
return
Context
{
Root
:
http
.
Dir
(
os
.
TempDir
()),
responseHeader
:
res
.
Header
(),
Req
:
request
},
nil
}
func
getContextOrFail
(
t
*
testing
.
T
)
Context
{
...
...
@@ -874,3 +876,35 @@ func TestFiles(t *testing.T) {
}
}
}
func
TestPush
(
t
*
testing
.
T
)
{
for
name
,
c
:=
range
map
[
string
]
struct
{
input
string
expectLinks
[]
string
}{
"oneLink"
:
{
input
:
`{{.Push "/test.css"}}`
,
expectLinks
:
[]
string
{
"</test.css>; rel=preload"
},
},
"multipleLinks"
:
{
input
:
`{{.Push "/test1.css"}} {{.Push "/test2.css"}}`
,
expectLinks
:
[]
string
{
"</test1.css>; rel=preload"
,
"</test2.css>; rel=preload"
},
},
}
{
c
:=
c
t
.
Run
(
name
,
func
(
t
*
testing
.
T
)
{
ctx
:=
getContextOrFail
(
t
)
tmpl
,
err
:=
template
.
New
(
""
)
.
Parse
(
c
.
input
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
err
=
tmpl
.
Execute
(
ioutil
.
Discard
,
ctx
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
got
:=
ctx
.
responseHeader
[
"Link"
];
!
reflect
.
DeepEqual
(
got
,
c
.
expectLinks
)
{
t
.
Errorf
(
"Result not match: expect %v, but got %v"
,
c
.
expectLinks
,
got
)
}
})
}
}
caddyhttp/markdown/markdown.go
View file @
761a32a0
...
...
@@ -133,11 +133,10 @@ func (md Markdown) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error
}
lastModTime
=
latest
(
lastModTime
,
fs
.
ModTime
())
ctx
:=
httpserver
.
Context
{
Root
:
md
.
FileSys
,
Req
:
r
,
URL
:
r
.
URL
,
}
ctx
:=
httpserver
.
NewContextWithHeader
(
w
.
Header
())
ctx
.
Root
=
md
.
FileSys
ctx
.
Req
=
r
ctx
.
URL
=
r
.
URL
html
,
err
:=
cfg
.
Markdown
(
title
(
fpath
),
f
,
dirents
,
ctx
)
if
err
!=
nil
{
return
http
.
StatusInternalServerError
,
err
...
...
caddyhttp/templates/templates.go
View file @
761a32a0
...
...
@@ -34,7 +34,10 @@ func (t Templates) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error
for
_
,
ext
:=
range
rule
.
Extensions
{
if
reqExt
==
ext
{
// Create execution context
ctx
:=
httpserver
.
Context
{
Root
:
t
.
FileSys
,
Req
:
r
,
URL
:
r
.
URL
}
ctx
:=
httpserver
.
NewContextWithHeader
(
w
.
Header
())
ctx
.
Root
=
t
.
FileSys
ctx
.
Req
=
r
ctx
.
URL
=
r
.
URL
// New template
templateName
:=
filepath
.
Base
(
fpath
)
...
...
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