2019-08-09 13:05:47 -05:00
|
|
|
// Copyright 2015 Matthew Holt and The Caddy Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package headers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
2019-08-09 13:19:56 -05:00
|
|
|
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
|
2019-08-21 11:46:35 -05:00
|
|
|
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
|
2019-08-09 13:05:47 -05:00
|
|
|
)
|
|
|
|
|
2019-08-21 11:46:35 -05:00
|
|
|
func init() {
|
2020-01-22 11:33:53 -05:00
|
|
|
httpcaddyfile.RegisterHandlerDirective("header", parseCaddyfile)
|
2019-09-11 19:48:37 -05:00
|
|
|
httpcaddyfile.RegisterHandlerDirective("request_header", parseReqHdrCaddyfile)
|
2019-08-21 11:46:35 -05:00
|
|
|
}
|
|
|
|
|
2019-09-11 19:48:37 -05:00
|
|
|
// parseCaddyfile sets up the handler for response headers from
|
|
|
|
// Caddyfile tokens. Syntax:
|
2019-08-09 13:05:47 -05:00
|
|
|
//
|
2020-01-22 11:33:53 -05:00
|
|
|
// header [<matcher>] [[+|-]<field> [<value|regexp>] [<replacement>]] {
|
2019-09-20 14:13:49 -05:00
|
|
|
// [+]<field> [<value|regexp> [<replacement>]]
|
|
|
|
// -<field>
|
2020-02-04 13:05:32 -05:00
|
|
|
// [defer]
|
2019-08-09 13:05:47 -05:00
|
|
|
// }
|
|
|
|
//
|
|
|
|
// Either a block can be opened or a single header field can be configured
|
2020-02-04 13:05:32 -05:00
|
|
|
// in the first line, but not both in the same directive. Header operations
|
|
|
|
// are deferred to write-time if any headers are being deleted or if the
|
|
|
|
// 'defer' subdirective is used.
|
2019-08-21 11:46:35 -05:00
|
|
|
func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
|
2019-09-14 00:38:52 -05:00
|
|
|
hdr := new(Handler)
|
2019-09-20 14:13:49 -05:00
|
|
|
|
|
|
|
makeResponseOps := func() {
|
|
|
|
if hdr.Response == nil {
|
|
|
|
hdr.Response = &RespHeaderOps{
|
|
|
|
HeaderOps: new(HeaderOps),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-21 11:46:35 -05:00
|
|
|
for h.Next() {
|
2019-08-09 13:05:47 -05:00
|
|
|
// first see if headers are in the initial line
|
|
|
|
var hasArgs bool
|
2019-08-21 11:46:35 -05:00
|
|
|
if h.NextArg() {
|
2019-08-09 13:05:47 -05:00
|
|
|
hasArgs = true
|
2019-08-21 11:46:35 -05:00
|
|
|
field := h.Val()
|
2019-09-20 14:13:49 -05:00
|
|
|
var value, replacement string
|
2019-09-11 19:48:37 -05:00
|
|
|
if h.NextArg() {
|
|
|
|
value = h.Val()
|
|
|
|
}
|
2019-09-20 14:13:49 -05:00
|
|
|
if h.NextArg() {
|
|
|
|
replacement = h.Val()
|
|
|
|
}
|
|
|
|
makeResponseOps()
|
|
|
|
CaddyfileHeaderOp(hdr.Response.HeaderOps, field, value, replacement)
|
2020-02-04 13:05:32 -05:00
|
|
|
if len(hdr.Response.HeaderOps.Delete) > 0 {
|
|
|
|
hdr.Response.Deferred = true
|
|
|
|
}
|
2019-08-09 13:05:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// if not, they should be in a block
|
2019-09-10 20:21:52 -05:00
|
|
|
for h.NextBlock(0) {
|
2020-02-04 13:05:32 -05:00
|
|
|
field := h.Val()
|
|
|
|
if field == "defer" {
|
|
|
|
hdr.Response.Deferred = true
|
|
|
|
continue
|
|
|
|
}
|
2019-08-09 13:05:47 -05:00
|
|
|
if hasArgs {
|
2019-08-21 11:46:35 -05:00
|
|
|
return nil, h.Err("cannot specify headers in both arguments and block")
|
2019-08-09 13:05:47 -05:00
|
|
|
}
|
2019-09-20 14:13:49 -05:00
|
|
|
var value, replacement string
|
2019-08-21 11:46:35 -05:00
|
|
|
if h.NextArg() {
|
|
|
|
value = h.Val()
|
2019-08-09 13:05:47 -05:00
|
|
|
}
|
2019-09-20 14:13:49 -05:00
|
|
|
if h.NextArg() {
|
|
|
|
replacement = h.Val()
|
|
|
|
}
|
|
|
|
makeResponseOps()
|
|
|
|
CaddyfileHeaderOp(hdr.Response.HeaderOps, field, value, replacement)
|
2020-02-04 13:05:32 -05:00
|
|
|
if len(hdr.Response.HeaderOps.Delete) > 0 {
|
|
|
|
hdr.Response.Deferred = true
|
|
|
|
}
|
2019-08-09 13:05:47 -05:00
|
|
|
}
|
|
|
|
}
|
2019-09-20 14:13:49 -05:00
|
|
|
|
2019-08-21 11:46:35 -05:00
|
|
|
return hdr, nil
|
2019-08-09 13:05:47 -05:00
|
|
|
}
|
|
|
|
|
2019-09-11 19:48:37 -05:00
|
|
|
// parseReqHdrCaddyfile sets up the handler for request headers
|
|
|
|
// from Caddyfile tokens. Syntax:
|
|
|
|
//
|
2019-09-20 14:13:49 -05:00
|
|
|
// request_header [<matcher>] [[+|-]<field> [<value|regexp>] [<replacement>]]
|
2019-09-11 19:48:37 -05:00
|
|
|
//
|
|
|
|
func parseReqHdrCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
|
2019-09-14 00:38:52 -05:00
|
|
|
hdr := new(Handler)
|
2019-09-11 19:48:37 -05:00
|
|
|
for h.Next() {
|
|
|
|
if !h.NextArg() {
|
|
|
|
return nil, h.ArgErr()
|
|
|
|
}
|
|
|
|
field := h.Val()
|
2019-09-20 14:13:49 -05:00
|
|
|
var value, replacement string
|
2019-09-11 19:48:37 -05:00
|
|
|
if h.NextArg() {
|
|
|
|
value = h.Val()
|
|
|
|
}
|
2019-09-20 14:13:49 -05:00
|
|
|
if h.NextArg() {
|
|
|
|
replacement = h.Val()
|
2020-01-06 10:10:20 -05:00
|
|
|
if h.NextArg() {
|
|
|
|
return nil, h.ArgErr()
|
|
|
|
}
|
2019-09-20 14:13:49 -05:00
|
|
|
}
|
2019-09-11 19:48:37 -05:00
|
|
|
|
|
|
|
if hdr.Request == nil {
|
|
|
|
hdr.Request = new(HeaderOps)
|
|
|
|
}
|
2019-09-20 14:13:49 -05:00
|
|
|
CaddyfileHeaderOp(hdr.Request, field, value, replacement)
|
2019-09-11 19:48:37 -05:00
|
|
|
|
|
|
|
if h.NextArg() {
|
|
|
|
return nil, h.ArgErr()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return hdr, nil
|
|
|
|
}
|
|
|
|
|
2019-09-20 14:13:49 -05:00
|
|
|
// CaddyfileHeaderOp applies a new header operation according to
|
|
|
|
// field, value, and replacement. The field can be prefixed with
|
|
|
|
// "+" or "-" to specify adding or removing; otherwise, the value
|
|
|
|
// will be set (overriding any previous value). If replacement is
|
|
|
|
// non-empty, value will be treated as a regular expression which
|
|
|
|
// will be used to search and then replacement will be used to
|
|
|
|
// complete the substring replacement; in that case, any + or -
|
|
|
|
// prefix to field will be ignored.
|
|
|
|
func CaddyfileHeaderOp(ops *HeaderOps, field, value, replacement string) {
|
2019-09-11 19:48:37 -05:00
|
|
|
if strings.HasPrefix(field, "+") {
|
2019-09-20 14:13:49 -05:00
|
|
|
if ops.Add == nil {
|
|
|
|
ops.Add = make(http.Header)
|
2019-08-09 13:05:47 -05:00
|
|
|
}
|
2019-09-20 14:13:49 -05:00
|
|
|
ops.Add.Set(field[1:], value)
|
2019-08-09 13:05:47 -05:00
|
|
|
} else if strings.HasPrefix(field, "-") {
|
2019-09-20 14:13:49 -05:00
|
|
|
ops.Delete = append(ops.Delete, field[1:])
|
2019-08-09 13:05:47 -05:00
|
|
|
} else {
|
2019-09-20 14:13:49 -05:00
|
|
|
if replacement == "" {
|
|
|
|
if ops.Set == nil {
|
|
|
|
ops.Set = make(http.Header)
|
|
|
|
}
|
|
|
|
ops.Set.Set(field, value)
|
|
|
|
} else {
|
|
|
|
if ops.Replace == nil {
|
|
|
|
ops.Replace = make(map[string][]Replacement)
|
|
|
|
}
|
|
|
|
field = strings.TrimLeft(field, "+-")
|
|
|
|
ops.Replace[field] = append(
|
|
|
|
ops.Replace[field],
|
|
|
|
Replacement{
|
|
|
|
SearchRegexp: value,
|
|
|
|
Replace: replacement,
|
|
|
|
},
|
|
|
|
)
|
2019-08-09 13:05:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|