mirror of
https://github.com/caddyserver/caddy.git
synced 2026-07-01 06:11:35 +00:00
context: add Push action
Signed-off-by: Tw <tw19881113@gmail.com>
This commit is contained in:
parent
aa7ecb02af
commit
761a32a080
4 changed files with 66 additions and 7 deletions
|
|
@ -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{} {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue