2019-08-10 02:05:47 +08: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-10 02:19:56 +08:00
|
|
|
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
|
2019-08-22 00:46:35 +08:00
|
|
|
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
|
2019-08-10 02:05:47 +08:00
|
|
|
)
|
|
|
|
|
2019-08-22 00:46:35 +08:00
|
|
|
func init() {
|
2020-01-23 00:33:53 +08:00
|
|
|
httpcaddyfile.RegisterHandlerDirective("header", parseCaddyfile)
|
2019-09-12 08:48:37 +08:00
|
|
|
httpcaddyfile.RegisterHandlerDirective("request_header", parseReqHdrCaddyfile)
|
2019-08-22 00:46:35 +08:00
|
|
|
}
|
|
|
|
|
2019-09-12 08:48:37 +08:00
|
|
|
// parseCaddyfile sets up the handler for response headers from
|
|
|
|
// Caddyfile tokens. Syntax:
|
2019-08-10 02:05:47 +08:00
|
|
|
//
|
2020-01-23 00:33:53 +08:00
|
|
|
// header [<matcher>] [[+|-]<field> [<value|regexp>] [<replacement>]] {
|
2019-09-21 03:13:49 +08:00
|
|
|
// [+]<field> [<value|regexp> [<replacement>]]
|
|
|
|
// -<field>
|
2020-02-05 02:05:32 +08:00
|
|
|
// [defer]
|
2019-08-10 02:05:47 +08:00
|
|
|
// }
|
|
|
|
//
|
|
|
|
// Either a block can be opened or a single header field can be configured
|
2020-02-05 02:05:32 +08: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-22 00:46:35 +08:00
|
|
|
func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
|
2019-09-14 13:38:52 +08:00
|
|
|
hdr := new(Handler)
|
2019-09-21 03:13:49 +08:00
|
|
|
|
|
|
|
makeResponseOps := func() {
|
|
|
|
if hdr.Response == nil {
|
|
|
|
hdr.Response = &RespHeaderOps{
|
|
|
|
HeaderOps: new(HeaderOps),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-22 00:46:35 +08:00
|
|
|
for h.Next() {
|
2019-08-10 02:05:47 +08:00
|
|
|
// first see if headers are in the initial line
|
|
|
|
var hasArgs bool
|
2019-08-22 00:46:35 +08:00
|
|
|
if h.NextArg() {
|
2019-08-10 02:05:47 +08:00
|
|
|
hasArgs = true
|
2019-08-22 00:46:35 +08:00
|
|
|
field := h.Val()
|
2019-09-21 03:13:49 +08:00
|
|
|
var value, replacement string
|
2019-09-12 08:48:37 +08:00
|
|
|
if h.NextArg() {
|
|
|
|
value = h.Val()
|
|
|
|
}
|
2019-09-21 03:13:49 +08:00
|
|
|
if h.NextArg() {
|
|
|
|
replacement = h.Val()
|
|
|
|
}
|
|
|
|
makeResponseOps()
|
|
|
|
CaddyfileHeaderOp(hdr.Response.HeaderOps, field, value, replacement)
|
2020-02-05 02:05:32 +08:00
|
|
|
if len(hdr.Response.HeaderOps.Delete) > 0 {
|
|
|
|
hdr.Response.Deferred = true
|
|
|
|
}
|
2019-08-10 02:05:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// if not, they should be in a block
|
2019-09-11 09:21:52 +08:00
|
|
|
for h.NextBlock(0) {
|
2020-02-05 02:05:32 +08:00
|
|
|
field := h.Val()
|
|
|
|
if field == "defer" {
|
|
|
|
hdr.Response.Deferred = true
|
|
|
|
continue
|
|
|
|
}
|
2019-08-10 02:05:47 +08:00
|
|
|
if hasArgs {
|
2019-08-22 00:46:35 +08:00
|
|
|
return nil, h.Err("cannot specify headers in both arguments and block")
|
2019-08-10 02:05:47 +08:00
|
|
|
}
|
2019-09-21 03:13:49 +08:00
|
|
|
var value, replacement string
|
2019-08-22 00:46:35 +08:00
|
|
|
if h.NextArg() {
|
|
|
|
value = h.Val()
|
2019-08-10 02:05:47 +08:00
|
|
|
}
|
2019-09-21 03:13:49 +08:00
|
|
|
if h.NextArg() {
|
|
|
|
replacement = h.Val()
|
|
|
|
}
|
|
|
|
makeResponseOps()
|
|
|
|
CaddyfileHeaderOp(hdr.Response.HeaderOps, field, value, replacement)
|
2020-02-05 02:05:32 +08:00
|
|
|
if len(hdr.Response.HeaderOps.Delete) > 0 {
|
|
|
|
hdr.Response.Deferred = true
|
|
|
|
}
|
2019-08-10 02:05:47 +08:00
|
|
|
}
|
|
|
|
}
|
2019-09-21 03:13:49 +08:00
|
|
|
|
2019-08-22 00:46:35 +08:00
|
|
|
return hdr, nil
|
2019-08-10 02:05:47 +08:00
|
|
|
}
|
|
|
|
|
2019-09-12 08:48:37 +08:00
|
|
|
// parseReqHdrCaddyfile sets up the handler for request headers
|
|
|
|
// from Caddyfile tokens. Syntax:
|
|
|
|
//
|
2019-09-21 03:13:49 +08:00
|
|
|
// request_header [<matcher>] [[+|-]<field> [<value|regexp>] [<replacement>]]
|
2019-09-12 08:48:37 +08:00
|
|
|
//
|
|
|
|
func parseReqHdrCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
|
2019-09-14 13:38:52 +08:00
|
|
|
hdr := new(Handler)
|
2019-09-12 08:48:37 +08:00
|
|
|
for h.Next() {
|
|
|
|
if !h.NextArg() {
|
|
|
|
return nil, h.ArgErr()
|
|
|
|
}
|
|
|
|
field := h.Val()
|
2020-03-31 01:52:11 +08:00
|
|
|
|
|
|
|
// sometimes it is habitual for users to suffix a field name with a colon,
|
|
|
|
// as if they were writing a curl command or something; see
|
|
|
|
// https://caddy.community/t/v2-reverse-proxy-please-add-cors-example-to-the-docs/7349
|
|
|
|
field = strings.TrimSuffix(field, ":")
|
|
|
|
|
2019-09-21 03:13:49 +08:00
|
|
|
var value, replacement string
|
2019-09-12 08:48:37 +08:00
|
|
|
if h.NextArg() {
|
|
|
|
value = h.Val()
|
|
|
|
}
|
2019-09-21 03:13:49 +08:00
|
|
|
if h.NextArg() {
|
|
|
|
replacement = h.Val()
|
2020-01-06 23:10:20 +08:00
|
|
|
if h.NextArg() {
|
|
|
|
return nil, h.ArgErr()
|
|
|
|
}
|
2019-09-21 03:13:49 +08:00
|
|
|
}
|
2019-09-12 08:48:37 +08:00
|
|
|
|
|
|
|
if hdr.Request == nil {
|
|
|
|
hdr.Request = new(HeaderOps)
|
|
|
|
}
|
2019-09-21 03:13:49 +08:00
|
|
|
CaddyfileHeaderOp(hdr.Request, field, value, replacement)
|
2019-09-12 08:48:37 +08:00
|
|
|
|
|
|
|
if h.NextArg() {
|
|
|
|
return nil, h.ArgErr()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return hdr, nil
|
|
|
|
}
|
|
|
|
|
2019-09-21 03:13:49 +08: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-12 08:48:37 +08:00
|
|
|
if strings.HasPrefix(field, "+") {
|
2019-09-21 03:13:49 +08:00
|
|
|
if ops.Add == nil {
|
|
|
|
ops.Add = make(http.Header)
|
2019-08-10 02:05:47 +08:00
|
|
|
}
|
2019-09-21 03:13:49 +08:00
|
|
|
ops.Add.Set(field[1:], value)
|
2019-08-10 02:05:47 +08:00
|
|
|
} else if strings.HasPrefix(field, "-") {
|
2019-09-21 03:13:49 +08:00
|
|
|
ops.Delete = append(ops.Delete, field[1:])
|
2019-08-10 02:05:47 +08:00
|
|
|
} else {
|
2019-09-21 03:13:49 +08: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-10 02:05:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|