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 (
2020-11-21 03:38:16 +08:00
"fmt"
2019-08-10 02:05:47 +08:00
"net/http"
2020-11-21 03:38:16 +08:00
"reflect"
2019-08-10 02:05:47 +08:00
"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-11-21 03:38:16 +08:00
httpcaddyfile . RegisterDirective ( "header" , parseCaddyfile )
httpcaddyfile . RegisterDirective ( "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
//
2023-05-16 13:18:13 +08:00
// header [<matcher>] [[+|-|?|>]<field> [<value|regexp>] [<replacement>]] {
// [+]<field> [<value|regexp> [<replacement>]]
// ?<field> <default_value>
// -<field>
// ><field>
// [defer]
2022-09-29 22:19:56 +08:00
// }
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
2020-11-21 03:38:16 +08:00
// 'defer' subdirective is used. + appends a header value, - deletes a field,
2023-05-16 13:18:13 +08:00
// ? conditionally sets a value only if the header field is not already set,
// and > sets a field with defer enabled.
2020-11-21 03:38:16 +08:00
func parseCaddyfile ( h httpcaddyfile . Helper ) ( [ ] httpcaddyfile . ConfigValue , error ) {
2024-01-24 08:36:59 +08:00
h . Next ( ) // consume directive name
2020-11-21 03:38:16 +08:00
matcherSet , err := h . ExtractMatcherSet ( )
if err != nil {
return nil , err
}
2024-01-24 08:36:59 +08:00
h . Next ( ) // consume the directive name again (matcher parsing resets)
2020-11-21 03:38:16 +08:00
makeHandler := func ( ) Handler {
return Handler {
Response : & RespHeaderOps {
HeaderOps : & HeaderOps { } ,
} ,
2019-09-21 03:13:49 +08:00
}
}
2020-11-21 03:38:16 +08:00
handler , handlerWithRequire := makeHandler ( ) , makeHandler ( )
2019-09-21 03:13:49 +08:00
2024-01-24 08:36:59 +08:00
// first see if headers are in the initial line
var hasArgs bool
if h . NextArg ( ) {
hasArgs = true
field := h . Val ( )
2024-03-22 01:29:32 +08:00
var value string
var replacement * string
2024-01-24 08:36:59 +08:00
if h . NextArg ( ) {
value = h . Val ( )
}
if h . NextArg ( ) {
2024-03-22 01:29:32 +08:00
arg := h . Val ( )
replacement = & arg
2024-01-24 08:36:59 +08:00
}
err := applyHeaderOp (
handler . Response . HeaderOps ,
handler . Response ,
field ,
value ,
replacement ,
)
if err != nil {
return nil , h . Err ( err . Error ( ) )
}
if len ( handler . Response . HeaderOps . Delete ) > 0 {
handler . Response . Deferred = true
}
}
// if not, they should be in a block
for h . NextBlock ( 0 ) {
field := h . Val ( )
if field == "defer" {
handler . Response . Deferred = true
continue
}
if hasArgs {
return nil , h . Err ( "cannot specify headers in both arguments and block" ) // because it would be weird
}
// 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/19
field = strings . TrimSuffix ( field , ":" )
2024-03-22 01:29:32 +08:00
var value string
var replacement * string
2024-01-24 08:36:59 +08:00
if h . NextArg ( ) {
value = h . Val ( )
}
2019-08-22 00:46:35 +08:00
if h . NextArg ( ) {
2024-03-22 01:29:32 +08:00
arg := h . Val ( )
replacement = & arg
2024-01-24 08:36:59 +08:00
}
handlerToUse := handler
if strings . HasPrefix ( field , "?" ) {
handlerToUse = handlerWithRequire
2019-08-10 02:05:47 +08:00
}
2024-01-24 08:36:59 +08:00
err := applyHeaderOp (
handlerToUse . Response . HeaderOps ,
handlerToUse . Response ,
field ,
value ,
replacement ,
)
if err != nil {
return nil , h . Err ( err . Error ( ) )
2019-08-10 02:05:47 +08:00
}
}
2019-09-21 03:13:49 +08:00
2020-11-21 03:38:16 +08:00
var configValues [ ] httpcaddyfile . ConfigValue
if ! reflect . DeepEqual ( handler , makeHandler ( ) ) {
configValues = append ( configValues , h . NewRoute ( matcherSet , handler ) ... )
}
if ! reflect . DeepEqual ( handlerWithRequire , makeHandler ( ) ) {
configValues = append ( configValues , h . NewRoute ( matcherSet , handlerWithRequire ) ... )
}
return configValues , 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:
//
2022-09-29 22:19:56 +08:00
// request_header [<matcher>] [[+|-]<field> [<value|regexp>] [<replacement>]]
2020-11-21 03:38:16 +08:00
func parseReqHdrCaddyfile ( h httpcaddyfile . Helper ) ( [ ] httpcaddyfile . ConfigValue , error ) {
2024-01-24 08:36:59 +08:00
h . Next ( ) // consume directive name
2020-11-21 03:38:16 +08:00
matcherSet , err := h . ExtractMatcherSet ( )
if err != nil {
return nil , err
}
2024-01-24 08:36:59 +08:00
h . Next ( ) // consume the directive name again (matcher parsing resets)
2020-11-21 03:38:16 +08:00
configValues := [ ] httpcaddyfile . ConfigValue { }
2024-01-24 08:36:59 +08:00
if ! h . NextArg ( ) {
return nil , h . ArgErr ( )
}
field := h . Val ( )
2020-03-31 01:52:11 +08:00
2024-01-24 08:36:59 +08:00
hdr := Handler {
Request : & HeaderOps { } ,
}
2020-11-21 03:38:16 +08:00
2024-01-24 08:36:59 +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/19
field = strings . TrimSuffix ( field , ":" )
2020-03-31 01:52:11 +08:00
2024-03-22 01:29:32 +08:00
var value string
var replacement * string
2024-01-24 08:36:59 +08:00
if h . NextArg ( ) {
value = h . Val ( )
}
if h . NextArg ( ) {
2024-03-22 01:29:32 +08:00
arg := h . Val ( )
replacement = & arg
2019-09-21 03:13:49 +08:00
if h . NextArg ( ) {
2024-01-24 08:36:59 +08:00
return nil , h . ArgErr ( )
2019-09-21 03:13:49 +08:00
}
2024-01-24 08:36:59 +08:00
}
2019-09-12 08:48:37 +08:00
2024-01-24 08:36:59 +08:00
if hdr . Request == nil {
hdr . Request = new ( HeaderOps )
}
if err := CaddyfileHeaderOp ( hdr . Request , field , value , replacement ) ; err != nil {
return nil , h . Err ( err . Error ( ) )
}
2020-11-21 03:38:16 +08:00
2024-01-24 08:36:59 +08:00
configValues = append ( configValues , h . NewRoute ( matcherSet , hdr ) ... )
2019-09-12 08:48:37 +08:00
2024-01-24 08:36:59 +08:00
if h . NextArg ( ) {
return nil , h . ArgErr ( )
2019-09-12 08:48:37 +08:00
}
2020-11-21 03:38:16 +08:00
return configValues , nil
2019-09-12 08:48:37 +08:00
}
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
2024-03-22 01:29:32 +08:00
// non-nil, value will be treated as a regular expression which
2019-09-21 03:13:49 +08:00
// 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.
2024-03-22 01:29:32 +08:00
func CaddyfileHeaderOp ( ops * HeaderOps , field , value string , replacement * string ) error {
2020-11-21 03:38:16 +08:00
return applyHeaderOp ( ops , nil , field , value , replacement )
}
2024-03-22 01:29:32 +08:00
func applyHeaderOp ( ops * HeaderOps , respHeaderOps * RespHeaderOps , field , value string , replacement * string ) error {
2020-11-21 03:38:16 +08:00
switch {
case strings . HasPrefix ( field , "+" ) : // append
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
}
2022-01-05 01:10:11 +08:00
ops . Add . Add ( field [ 1 : ] , value )
2020-11-21 03:38:16 +08:00
case strings . HasPrefix ( field , "-" ) : // delete
2019-09-21 03:13:49 +08:00
ops . Delete = append ( ops . Delete , field [ 1 : ] )
2020-11-21 03:38:16 +08:00
if respHeaderOps != nil {
respHeaderOps . Deferred = true
}
case strings . HasPrefix ( field , "?" ) : // default (conditional on not existing) - response headers only
if respHeaderOps == nil {
return fmt . Errorf ( "%v: the default header modifier ('?') can only be used on response headers; for conditional manipulation of request headers, use matchers" , field )
}
if respHeaderOps . Require == nil {
respHeaderOps . Require = & caddyhttp . ResponseMatcher {
Headers : make ( http . Header ) ,
2019-09-21 03:13:49 +08:00
}
2019-08-10 02:05:47 +08:00
}
2020-11-21 03:38:16 +08:00
field = strings . TrimPrefix ( field , "?" )
respHeaderOps . Require . Headers [ field ] = nil
if respHeaderOps . Set == nil {
respHeaderOps . Set = make ( http . Header )
}
respHeaderOps . Set . Set ( field , value )
2024-03-22 01:29:32 +08:00
case replacement != nil : // replace
2023-06-16 07:18:55 +08:00
// allow defer shortcut for replace syntax
if strings . HasPrefix ( field , ">" ) && respHeaderOps != nil {
respHeaderOps . Deferred = true
}
2020-11-21 03:38:16 +08:00
if ops . Replace == nil {
ops . Replace = make ( map [ string ] [ ] Replacement )
}
2023-06-16 07:18:55 +08:00
field = strings . TrimLeft ( field , "+-?>" )
2020-11-21 03:38:16 +08:00
ops . Replace [ field ] = append (
ops . Replace [ field ] ,
Replacement {
SearchRegexp : value ,
2024-03-22 01:29:32 +08:00
Replace : * replacement ,
2020-11-21 03:38:16 +08:00
} ,
)
2023-05-16 13:18:13 +08:00
case strings . HasPrefix ( field , ">" ) : // set (overwrite) with defer
if ops . Set == nil {
ops . Set = make ( http . Header )
}
ops . Set . Set ( field [ 1 : ] , value )
if respHeaderOps != nil {
respHeaderOps . Deferred = true
}
2020-11-21 03:38:16 +08:00
default : // set (overwrite)
if ops . Set == nil {
ops . Set = make ( http . Header )
}
ops . Set . Set ( field , value )
2019-08-10 02:05:47 +08:00
}
2020-11-21 03:38:16 +08:00
return nil
2019-08-10 02:05:47 +08:00
}