mirror of
https://github.com/caddyserver/caddy.git
synced 2026-05-25 16:22:36 -04:00
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 319bbba402 |
-18
@@ -1,18 +0,0 @@
|
||||
_gitignore/
|
||||
*.log
|
||||
Caddyfile
|
||||
!caddyfile/
|
||||
|
||||
# artifacts from pprof tooling
|
||||
*.prof
|
||||
*.test
|
||||
|
||||
# build artifacts
|
||||
cmd/caddy/caddy
|
||||
cmd/caddy/caddy.exe
|
||||
|
||||
# mac specific
|
||||
.DS_Store
|
||||
|
||||
# go modules
|
||||
vendor
|
||||
@@ -1,49 +0,0 @@
|
||||
linters-settings:
|
||||
errcheck:
|
||||
ignore: fmt:.*,io/ioutil:^Read.*,github.com/caddyserver/caddy/v2/caddyconfig:RegisterAdapter,github.com/caddyserver/caddy/v2:RegisterModule
|
||||
ignoretests: true
|
||||
misspell:
|
||||
locale: US
|
||||
|
||||
linters:
|
||||
enable:
|
||||
- bodyclose
|
||||
- errcheck
|
||||
- gofmt
|
||||
- goimports
|
||||
- gosec
|
||||
- ineffassign
|
||||
- misspell
|
||||
|
||||
run:
|
||||
# default concurrency is a available CPU number.
|
||||
# concurrency: 4 # explicitly omit this value to fully utilize available resources.
|
||||
deadline: 5m
|
||||
issues-exit-code: 1
|
||||
tests: false
|
||||
|
||||
# output configuration options
|
||||
output:
|
||||
format: 'colored-line-number'
|
||||
print-issued-lines: true
|
||||
print-linter-name: true
|
||||
|
||||
issues:
|
||||
exclude-rules:
|
||||
# we aren't calling unknown URL
|
||||
- text: "G107" # G107: Url provided to HTTP request as taint input
|
||||
linters:
|
||||
- gosec
|
||||
# as a web server that's expected to handle any template, this is totally in the hands of the user.
|
||||
- text: "G203" # G203: Use of unescaped data in HTML templates
|
||||
linters:
|
||||
- gosec
|
||||
# we're shelling out to known commands, not relying on user-defined input.
|
||||
- text: "G204" # G204: Audit use of command execution
|
||||
linters:
|
||||
- gosec
|
||||
# the choice of weakrand is deliberate, hence the named import "weakrand"
|
||||
- path: modules/caddyhttp/reverseproxy/selectionpolicies.go
|
||||
text: "G404" # G404: Insecure random number source (rand)
|
||||
linters:
|
||||
- gosec
|
||||
@@ -1,10 +0,0 @@
|
||||
# This is the official list of Caddy Authors for copyright purposes.
|
||||
# Authors may be either individual people or legal entities.
|
||||
#
|
||||
# Not all individual contributors are authors. For the full list of
|
||||
# contributors, refer to the project's page on GitHub or the repo's
|
||||
# commit history.
|
||||
|
||||
Matthew Holt <Matthew.Holt@gmail.com>
|
||||
Light Code Labs <sales@lightcodelabs.com>
|
||||
Ardan Labs <info@ardanlabs.com>
|
||||
@@ -1,202 +0,0 @@
|
||||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
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.
|
||||
@@ -1,131 +0,0 @@
|
||||
Caddy 2
|
||||
=======
|
||||
|
||||
This is the development branch for Caddy 2, the web server of the Go community.
|
||||
|
||||
**Caddy 2 is production-ready, but there may be breaking changes before the stable 2.0 release.** Please test it and deploy it as much as you are able, and submit your feedback!
|
||||
|
||||
---
|
||||
|
||||
<p align="center">
|
||||
<a href="https://caddyserver.com"><img src="https://user-images.githubusercontent.com/1128849/36338535-05fb646a-136f-11e8-987b-e6901e717d5a.png" alt="Caddy" width="450"></a>
|
||||
</p>
|
||||
<h3 align="center">Every site on HTTPS</h3>
|
||||
<p align="center">Caddy is an extensible server platform that uses TLS by default.</p>
|
||||
<p align="center">
|
||||
<a href="https://dev.azure.com/mholt-dev/Caddy/_build/latest?definitionId=5&branchName=v2"><img src="https://dev.azure.com/mholt-dev/Caddy/_apis/build/status/Multiplatform%20Tests?branchName=v2"></a>
|
||||
<a href="https://pkg.go.dev/github.com/caddyserver/caddy/v2"><img src="https://img.shields.io/badge/godoc-reference-blue.svg"></a>
|
||||
<a href="https://app.fuzzit.dev/orgs/caddyserver-gh/dashboard"><img src="https://app.fuzzit.dev/badge?org_id=caddyserver-gh"></a>
|
||||
<br>
|
||||
<a href="https://twitter.com/caddyserver" title="@caddyserver on Twitter"><img src="https://img.shields.io/badge/twitter-@caddyserver-55acee.svg" alt="@caddyserver on Twitter"></a>
|
||||
<a href="https://caddy.community" title="Caddy Forum"><img src="https://img.shields.io/badge/community-forum-ff69b4.svg" alt="Caddy Forum"></a>
|
||||
<a href="https://sourcegraph.com/github.com/caddyserver/caddy?badge" title="Caddy on Sourcegraph"><img src="https://sourcegraph.com/github.com/caddyserver/caddy/-/badge.svg" alt="Caddy on Sourcegraph"></a>
|
||||
</p>
|
||||
<p align="center">
|
||||
<a href="https://github.com/caddyserver/caddy/releases">Download</a> ·
|
||||
<a href="https://caddyserver.com/docs/">Documentation</a> ·
|
||||
<a href="https://caddy.community">Community</a>
|
||||
</p>
|
||||
|
||||
|
||||
|
||||
### Menu
|
||||
|
||||
- [Build from source](#build-from-source)
|
||||
- [For development](#for-development)
|
||||
- [With version information and/or plugins](#with-version-information-andor-plugins)
|
||||
- [Getting started](#getting-started)
|
||||
- [Overview](#overview)
|
||||
- [Full documentation](#full-documentation)
|
||||
- [Getting help](#getting-help)
|
||||
- [About](#about)
|
||||
|
||||
<p align="center">
|
||||
<b>Powered by</b>
|
||||
<br>
|
||||
<a href="https://github.com/caddyserver/certmagic"><img src="https://user-images.githubusercontent.com/1128849/49704830-49d37200-fbd5-11e8-8385-767e0cd033c3.png" alt="CertMagic" width="250"></a>
|
||||
</p>
|
||||
|
||||
## Build from source
|
||||
|
||||
Requirements:
|
||||
|
||||
- [Go 1.14 or newer](https://golang.org/dl/)
|
||||
- Do NOT disable [Go modules](https://github.com/golang/go/wiki/Modules) (`export GO111MODULE=on`)
|
||||
|
||||
### For development
|
||||
|
||||
_**Note:** These steps [will not embed proper version information](https://github.com/golang/go/issues/29228). For that, please follow the instructions below._
|
||||
|
||||
```bash
|
||||
$ git clone -b v2 "https://github.com/caddyserver/caddy.git"
|
||||
$ cd caddy/cmd/caddy/
|
||||
$ go build
|
||||
```
|
||||
|
||||
### With version information and/or plugins
|
||||
|
||||
1. Create a new folder: `mkdir caddy`
|
||||
2. Change into it: `cd caddy`
|
||||
3. Copy [Caddy's main.go](https://github.com/caddyserver/caddy/blob/v2/cmd/caddy/main.go) into the empty folder. Add imports for any custom plugins you want to add.
|
||||
4. Initialize a Go module: `go mod init caddy`
|
||||
5. Pin Caddy version: `go get github.com/caddyserver/caddy/v2@TAG` replacing `TAG` with a git tag or commit.
|
||||
6. Compile: `go build`
|
||||
|
||||
|
||||
|
||||
|
||||
## Quick start
|
||||
|
||||
The [Caddy website](https://caddyserver.com/docs/) has documentation that includes tutorials, quick-start guides, reference, and more.
|
||||
|
||||
**We recommend that all users do our [Getting Started](https://caddyserver.com/docs/getting-started) guide to become familiar with using Caddy.**
|
||||
|
||||
If you've only got a minute, [the website has several quick-start tutorials](https://caddyserver.com/docs/quick-starts) to choose from! However, after finishing a quick-start tutorial, please read more documentation to understand how the software works. 🙂
|
||||
|
||||
|
||||
|
||||
|
||||
## Overview
|
||||
|
||||
Caddy is most often used as an HTTPS server, but it is suitable for any long-running Go program. First and foremost, it is a platform to run Go applications. Caddy "apps" are just Go programs that are implemented as Caddy modules. Two apps -- `tls` and `http` -- ship standard with Caddy.
|
||||
|
||||
Caddy apps instantly benefit from [automated documentation](https://caddyserver.com/docs/json/), graceful on-line [config changes via API](https://caddyserver.com/docs/api), and unification with other Caddy apps.
|
||||
|
||||
Although [JSON](https://caddyserver.com/docs/json/) is Caddy's native config language, Caddy can accept input from [config adapters](https://caddyserver.com/docs/config-adapters) which can essentially convert any config format of your choice into JSON: Caddyfile, JSON 5, YAML, TOML, NGINX config, and more.
|
||||
|
||||
The primary way to configure Caddy is through [its API](https://caddyserver.com/docs/api), but if you prefer config files, the [command-line interface](https://caddyserver.com/docs/command-line) supports those too.
|
||||
|
||||
Caddy exposes an unprecedented level of control compared to any web server in existence. In Caddy, you are usually setting the actual values of the initialized types in memory that power everything from your HTTP handlers and TLS handshakes to your storage medium. Caddy is also ridiculously extensible, with a powerful plugin system that makes vast improvements over other web servers.
|
||||
|
||||
To wield the power of this design, you need to know how the config document is structured. Please see the [our documentation site](https://caddyserver.com/docs/) for details about [Caddy's config structure](https://caddyserver.com/docs/json/).
|
||||
|
||||
Nearly all of Caddy's configuration is contained in a single config document, rather than being scattered across CLI flags and env variables and a configuration file as with other web servers. This makes managing your server config more straightforward and reduces hidden variables/factors.
|
||||
|
||||
|
||||
## Full documentation
|
||||
|
||||
Our website has complete documentation:
|
||||
|
||||
**https://caddyserver.com/docs/**
|
||||
|
||||
The docs are also open source. You can contribute to them here: https://github.com/caddyserver/website
|
||||
|
||||
|
||||
|
||||
## Getting help
|
||||
|
||||
- We **strongly recommend** that all professionals or companies using Caddy get a support contract through [Ardan Labs](https://www.ardanlabs.com/my/contact-us?dd=caddy) before help is needed.
|
||||
|
||||
- Individuals can exchange help for free on our community forum at https://caddy.community. Remember that people give help out of their spare time and good will. The best way to get help is to give it first!
|
||||
|
||||
Please use our [issue tracker](/caddyserver/caddy/issues) only for bug reports and feature requests, i.e. actionable development items (support questions will usually be referred to the forums).
|
||||
|
||||
|
||||
|
||||
## About
|
||||
|
||||
**The name "Caddy" is trademarked.** The name of the software is "Caddy", not "Caddy Server" or "CaddyServer". Please call it "Caddy" or, if you wish to clarify, "the Caddy web server". Caddy is a registered trademark of Light Code Labs, LLC.
|
||||
|
||||
- _Project on Twitter: [@caddyserver](https://twitter.com/caddyserver)_
|
||||
- _Author on Twitter: [@mholt6](https://twitter.com/mholt6)_
|
||||
@@ -1,875 +0,0 @@
|
||||
// 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 caddy
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"expvar"
|
||||
"fmt"
|
||||
"io"
|
||||
"mime"
|
||||
"net/http"
|
||||
"net/http/pprof"
|
||||
"net/url"
|
||||
"os"
|
||||
"path"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/caddyserver/caddy/v2/caddyconfig"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// TODO: is there a way to make the admin endpoint so that it can be plugged into the HTTP app? see issue #2833
|
||||
|
||||
// AdminConfig configures Caddy's API endpoint, which is used
|
||||
// to manage Caddy while it is running.
|
||||
type AdminConfig struct {
|
||||
// If true, the admin endpoint will be completely disabled.
|
||||
// Note that this makes any runtime changes to the config
|
||||
// impossible, since the interface to do so is through the
|
||||
// admin endpoint.
|
||||
Disabled bool `json:"disabled,omitempty"`
|
||||
|
||||
// The address to which the admin endpoint's listener should
|
||||
// bind itself. Can be any single network address that can be
|
||||
// parsed by Caddy. Default: localhost:2019
|
||||
Listen string `json:"listen,omitempty"`
|
||||
|
||||
// If true, CORS headers will be emitted, and requests to the
|
||||
// API will be rejected if their `Host` and `Origin` headers
|
||||
// do not match the expected value(s). Use `origins` to
|
||||
// customize which origins/hosts are allowed.If `origins` is
|
||||
// not set, the listen address is the only value allowed by
|
||||
// default.
|
||||
EnforceOrigin bool `json:"enforce_origin,omitempty"`
|
||||
|
||||
// The list of allowed origins for API requests. Only used if
|
||||
// `enforce_origin` is true. If not set, the listener address
|
||||
// will be the default value. If set but empty, no origins will
|
||||
// be allowed.
|
||||
Origins []string `json:"origins,omitempty"`
|
||||
|
||||
// Options related to configuration management.
|
||||
Config *ConfigSettings `json:"config,omitempty"`
|
||||
}
|
||||
|
||||
// ConfigSettings configures the, uh, configuration... and
|
||||
// management thereof.
|
||||
type ConfigSettings struct {
|
||||
// Whether to keep a copy of the active config on disk. Default is true.
|
||||
Persist *bool `json:"persist,omitempty"`
|
||||
}
|
||||
|
||||
// listenAddr extracts a singular listen address from ac.Listen,
|
||||
// returning the network and the address of the listener.
|
||||
func (admin AdminConfig) listenAddr() (string, string, error) {
|
||||
input := admin.Listen
|
||||
if input == "" {
|
||||
input = DefaultAdminListen
|
||||
}
|
||||
listenAddr, err := ParseNetworkAddress(input)
|
||||
if err != nil {
|
||||
return "", "", fmt.Errorf("parsing admin listener address: %v", err)
|
||||
}
|
||||
if listenAddr.PortRangeSize() != 1 {
|
||||
return "", "", fmt.Errorf("admin endpoint must have exactly one address; cannot listen on %v", listenAddr)
|
||||
}
|
||||
return listenAddr.Network, listenAddr.JoinHostPort(0), nil
|
||||
}
|
||||
|
||||
// newAdminHandler reads admin's config and returns an http.Handler suitable
|
||||
// for use in an admin endpoint server, which will be listening on listenAddr.
|
||||
func (admin AdminConfig) newAdminHandler(listenAddr string) adminHandler {
|
||||
muxWrap := adminHandler{
|
||||
enforceOrigin: admin.EnforceOrigin,
|
||||
allowedOrigins: admin.allowedOrigins(listenAddr),
|
||||
mux: http.NewServeMux(),
|
||||
}
|
||||
|
||||
// addRoute just calls muxWrap.mux.Handle after
|
||||
// wrapping the handler with error handling
|
||||
addRoute := func(pattern string, h AdminHandler) {
|
||||
wrapper := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
err := h.ServeHTTP(w, r)
|
||||
muxWrap.handleError(w, r, err)
|
||||
})
|
||||
muxWrap.mux.Handle(pattern, wrapper)
|
||||
}
|
||||
|
||||
// register standard config control endpoints
|
||||
addRoute("/load", AdminHandlerFunc(handleLoad))
|
||||
addRoute("/"+rawConfigKey+"/", AdminHandlerFunc(handleConfig))
|
||||
addRoute("/id/", AdminHandlerFunc(handleConfigID))
|
||||
addRoute("/stop", AdminHandlerFunc(handleStop))
|
||||
|
||||
// register debugging endpoints
|
||||
muxWrap.mux.HandleFunc("/debug/pprof/", pprof.Index)
|
||||
muxWrap.mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
|
||||
muxWrap.mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
|
||||
muxWrap.mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
|
||||
muxWrap.mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
|
||||
muxWrap.mux.Handle("/debug/vars", expvar.Handler())
|
||||
|
||||
// register third-party module endpoints
|
||||
for _, m := range GetModules("admin.api") {
|
||||
router := m.New().(AdminRouter)
|
||||
for _, route := range router.Routes() {
|
||||
addRoute(route.Pattern, route.Handler)
|
||||
}
|
||||
}
|
||||
|
||||
return muxWrap
|
||||
}
|
||||
|
||||
// allowedOrigins returns a list of origins that are allowed.
|
||||
// If admin.Origins is nil (null), the provided listen address
|
||||
// will be used as the default origin. If admin.Origins is
|
||||
// empty, no origins will be allowed, effectively bricking the
|
||||
// endpoint, but whatever.
|
||||
func (admin AdminConfig) allowedOrigins(listen string) []string {
|
||||
uniqueOrigins := make(map[string]struct{})
|
||||
for _, o := range admin.Origins {
|
||||
uniqueOrigins[o] = struct{}{}
|
||||
}
|
||||
if admin.Origins == nil {
|
||||
uniqueOrigins[listen] = struct{}{}
|
||||
}
|
||||
var allowed []string
|
||||
for origin := range uniqueOrigins {
|
||||
allowed = append(allowed, origin)
|
||||
}
|
||||
return allowed
|
||||
}
|
||||
|
||||
// replaceAdmin replaces the running admin server according
|
||||
// to the relevant configuration in cfg. If no configuration
|
||||
// for the admin endpoint exists in cfg, a default one is
|
||||
// used, so that there is always an admin server (unless it
|
||||
// is explicitly configured to be disabled).
|
||||
func replaceAdmin(cfg *Config) error {
|
||||
// always be sure to close down the old admin endpoint
|
||||
// as gracefully as possible, even if the new one is
|
||||
// disabled -- careful to use reference to the current
|
||||
// (old) admin endpoint since it will be different
|
||||
// when the function returns
|
||||
oldAdminServer := adminServer
|
||||
defer func() {
|
||||
// do the shutdown asynchronously so that any
|
||||
// current API request gets a response; this
|
||||
// goroutine may last a few seconds
|
||||
if oldAdminServer != nil {
|
||||
go func(oldAdminServer *http.Server) {
|
||||
err := stopAdminServer(oldAdminServer)
|
||||
if err != nil {
|
||||
Log().Named("admin").Error("stopping current admin endpoint", zap.Error(err))
|
||||
}
|
||||
}(oldAdminServer)
|
||||
}
|
||||
}()
|
||||
|
||||
// always get a valid admin config
|
||||
adminConfig := DefaultAdminConfig
|
||||
if cfg != nil && cfg.Admin != nil {
|
||||
adminConfig = cfg.Admin
|
||||
}
|
||||
|
||||
// if new admin endpoint is to be disabled, we're done
|
||||
if adminConfig.Disabled {
|
||||
Log().Named("admin").Warn("admin endpoint disabled")
|
||||
return nil
|
||||
}
|
||||
|
||||
// extract a singular listener address
|
||||
netw, addr, err := adminConfig.listenAddr()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
handler := adminConfig.newAdminHandler(addr)
|
||||
|
||||
ln, err := Listen(netw, addr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
adminServer = &http.Server{
|
||||
Handler: handler,
|
||||
ReadTimeout: 10 * time.Second,
|
||||
ReadHeaderTimeout: 5 * time.Second,
|
||||
IdleTimeout: 60 * time.Second,
|
||||
MaxHeaderBytes: 1024 * 64,
|
||||
}
|
||||
|
||||
go adminServer.Serve(ln)
|
||||
|
||||
Log().Named("admin").Info(
|
||||
"admin endpoint started",
|
||||
zap.String("address", addr),
|
||||
zap.Bool("enforce_origin", adminConfig.EnforceOrigin),
|
||||
zap.Strings("origins", handler.allowedOrigins),
|
||||
)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func stopAdminServer(srv *http.Server) error {
|
||||
if srv == nil {
|
||||
return fmt.Errorf("no admin server")
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
err := srv.Shutdown(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("shutting down admin server: %v", err)
|
||||
}
|
||||
Log().Named("admin").Info("stopped previous server")
|
||||
return nil
|
||||
}
|
||||
|
||||
// AdminRouter is a type which can return routes for the admin API.
|
||||
type AdminRouter interface {
|
||||
Routes() []AdminRoute
|
||||
}
|
||||
|
||||
// AdminRoute represents a route for the admin endpoint.
|
||||
type AdminRoute struct {
|
||||
Pattern string
|
||||
Handler AdminHandler
|
||||
}
|
||||
|
||||
type adminHandler struct {
|
||||
enforceOrigin bool
|
||||
allowedOrigins []string
|
||||
mux *http.ServeMux
|
||||
}
|
||||
|
||||
// ServeHTTP is the external entry point for API requests.
|
||||
// It will only be called once per request.
|
||||
func (h adminHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
Log().Named("admin.api").Info("received request",
|
||||
zap.String("method", r.Method),
|
||||
zap.String("uri", r.RequestURI),
|
||||
zap.String("remote_addr", r.RemoteAddr),
|
||||
zap.Reflect("headers", r.Header),
|
||||
)
|
||||
h.serveHTTP(w, r)
|
||||
}
|
||||
|
||||
// serveHTTP is the internal entry point for API requests. It may
|
||||
// be called more than once per request, for example if a request
|
||||
// is rewritten (i.e. internal redirect).
|
||||
func (h adminHandler) serveHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
if h.enforceOrigin {
|
||||
// DNS rebinding mitigation
|
||||
err := h.checkHost(r)
|
||||
if err != nil {
|
||||
h.handleError(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
// cross-site mitigation
|
||||
origin, err := h.checkOrigin(r)
|
||||
if err != nil {
|
||||
h.handleError(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
if r.Method == http.MethodOptions {
|
||||
w.Header().Set("Access-Control-Allow-Methods", "OPTIONS, GET, POST, PUT, PATCH, DELETE")
|
||||
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Cache-Control")
|
||||
w.Header().Set("Access-Control-Allow-Credentials", "true")
|
||||
}
|
||||
w.Header().Set("Access-Control-Allow-Origin", origin)
|
||||
}
|
||||
|
||||
// TODO: authentication & authorization, if configured
|
||||
|
||||
h.mux.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
func (h adminHandler) handleError(w http.ResponseWriter, r *http.Request, err error) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
if err == ErrInternalRedir {
|
||||
h.serveHTTP(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
apiErr, ok := err.(APIError)
|
||||
if !ok {
|
||||
apiErr = APIError{
|
||||
Code: http.StatusInternalServerError,
|
||||
Err: err,
|
||||
}
|
||||
}
|
||||
if apiErr.Code == 0 {
|
||||
apiErr.Code = http.StatusInternalServerError
|
||||
}
|
||||
if apiErr.Message == "" && apiErr.Err != nil {
|
||||
apiErr.Message = apiErr.Err.Error()
|
||||
}
|
||||
|
||||
Log().Named("admin.api").Error("request error",
|
||||
zap.Error(err),
|
||||
zap.Int("status_code", apiErr.Code),
|
||||
)
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(apiErr.Code)
|
||||
json.NewEncoder(w).Encode(apiErr)
|
||||
}
|
||||
|
||||
// checkHost returns a handler that wraps next such that
|
||||
// it will only be called if the request's Host header matches
|
||||
// a trustworthy/expected value. This helps to mitigate DNS
|
||||
// rebinding attacks.
|
||||
func (h adminHandler) checkHost(r *http.Request) error {
|
||||
var allowed bool
|
||||
for _, allowedHost := range h.allowedOrigins {
|
||||
if r.Host == allowedHost {
|
||||
allowed = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !allowed {
|
||||
return APIError{
|
||||
Code: http.StatusForbidden,
|
||||
Err: fmt.Errorf("host not allowed: %s", r.Host),
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// checkOrigin ensures that the Origin header, if
|
||||
// set, matches the intended target; prevents arbitrary
|
||||
// sites from issuing requests to our listener. It
|
||||
// returns the origin that was obtained from r.
|
||||
func (h adminHandler) checkOrigin(r *http.Request) (string, error) {
|
||||
origin := h.getOriginHost(r)
|
||||
if origin == "" {
|
||||
return origin, APIError{
|
||||
Code: http.StatusForbidden,
|
||||
Err: fmt.Errorf("missing required Origin header"),
|
||||
}
|
||||
}
|
||||
if !h.originAllowed(origin) {
|
||||
return origin, APIError{
|
||||
Code: http.StatusForbidden,
|
||||
Err: fmt.Errorf("client is not allowed to access from origin %s", origin),
|
||||
}
|
||||
}
|
||||
return origin, nil
|
||||
}
|
||||
|
||||
func (h adminHandler) getOriginHost(r *http.Request) string {
|
||||
origin := r.Header.Get("Origin")
|
||||
if origin == "" {
|
||||
origin = r.Header.Get("Referer")
|
||||
}
|
||||
originURL, err := url.Parse(origin)
|
||||
if err == nil && originURL.Host != "" {
|
||||
origin = originURL.Host
|
||||
}
|
||||
return origin
|
||||
}
|
||||
|
||||
func (h adminHandler) originAllowed(origin string) bool {
|
||||
for _, allowedOrigin := range h.allowedOrigins {
|
||||
originCopy := origin
|
||||
if !strings.Contains(allowedOrigin, "://") {
|
||||
// no scheme specified, so allow both
|
||||
originCopy = strings.TrimPrefix(originCopy, "http://")
|
||||
originCopy = strings.TrimPrefix(originCopy, "https://")
|
||||
}
|
||||
if originCopy == allowedOrigin {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func handleLoad(w http.ResponseWriter, r *http.Request) error {
|
||||
if r.Method != http.MethodPost {
|
||||
return APIError{
|
||||
Code: http.StatusMethodNotAllowed,
|
||||
Err: fmt.Errorf("method not allowed"),
|
||||
}
|
||||
}
|
||||
|
||||
buf := bufPool.Get().(*bytes.Buffer)
|
||||
buf.Reset()
|
||||
defer bufPool.Put(buf)
|
||||
|
||||
_, err := io.Copy(buf, r.Body)
|
||||
if err != nil {
|
||||
return APIError{
|
||||
Code: http.StatusBadRequest,
|
||||
Err: fmt.Errorf("reading request body: %v", err),
|
||||
}
|
||||
}
|
||||
body := buf.Bytes()
|
||||
|
||||
// if the config is formatted other than Caddy's native
|
||||
// JSON, we need to adapt it before loading it
|
||||
if ctHeader := r.Header.Get("Content-Type"); ctHeader != "" {
|
||||
ct, _, err := mime.ParseMediaType(ctHeader)
|
||||
if err != nil {
|
||||
return APIError{
|
||||
Code: http.StatusBadRequest,
|
||||
Err: fmt.Errorf("invalid Content-Type: %v", err),
|
||||
}
|
||||
}
|
||||
if !strings.HasSuffix(ct, "/json") {
|
||||
slashIdx := strings.Index(ct, "/")
|
||||
if slashIdx < 0 {
|
||||
return APIError{
|
||||
Code: http.StatusBadRequest,
|
||||
Err: fmt.Errorf("malformed Content-Type"),
|
||||
}
|
||||
}
|
||||
adapterName := ct[slashIdx+1:]
|
||||
cfgAdapter := caddyconfig.GetAdapter(adapterName)
|
||||
if cfgAdapter == nil {
|
||||
return APIError{
|
||||
Code: http.StatusBadRequest,
|
||||
Err: fmt.Errorf("unrecognized config adapter '%s'", adapterName),
|
||||
}
|
||||
}
|
||||
result, warnings, err := cfgAdapter.Adapt(body, nil)
|
||||
if err != nil {
|
||||
return APIError{
|
||||
Code: http.StatusBadRequest,
|
||||
Err: fmt.Errorf("adapting config using %s adapter: %v", adapterName, err),
|
||||
}
|
||||
}
|
||||
if len(warnings) > 0 {
|
||||
respBody, err := json.Marshal(warnings)
|
||||
if err != nil {
|
||||
Log().Named("admin.api.load").Error(err.Error())
|
||||
}
|
||||
w.Write(respBody)
|
||||
}
|
||||
body = result
|
||||
}
|
||||
}
|
||||
|
||||
forceReload := r.Header.Get("Cache-Control") == "must-revalidate"
|
||||
|
||||
err = Load(body, forceReload)
|
||||
if err != nil {
|
||||
return APIError{
|
||||
Code: http.StatusBadRequest,
|
||||
Err: fmt.Errorf("loading config: %v", err),
|
||||
}
|
||||
}
|
||||
|
||||
Log().Named("admin.api").Info("load complete")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func handleConfig(w http.ResponseWriter, r *http.Request) error {
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
err := readConfig(r.URL.Path, w)
|
||||
if err != nil {
|
||||
return APIError{Code: http.StatusBadRequest, Err: err}
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
case http.MethodPost,
|
||||
http.MethodPut,
|
||||
http.MethodPatch,
|
||||
http.MethodDelete:
|
||||
|
||||
// DELETE does not use a body, but the others do
|
||||
var body []byte
|
||||
if r.Method != http.MethodDelete {
|
||||
if ct := r.Header.Get("Content-Type"); !strings.Contains(ct, "/json") {
|
||||
return APIError{
|
||||
Code: http.StatusBadRequest,
|
||||
Err: fmt.Errorf("unacceptable content-type: %v; 'application/json' required", ct),
|
||||
}
|
||||
}
|
||||
|
||||
buf := bufPool.Get().(*bytes.Buffer)
|
||||
buf.Reset()
|
||||
defer bufPool.Put(buf)
|
||||
|
||||
_, err := io.Copy(buf, r.Body)
|
||||
if err != nil {
|
||||
return APIError{
|
||||
Code: http.StatusBadRequest,
|
||||
Err: fmt.Errorf("reading request body: %v", err),
|
||||
}
|
||||
}
|
||||
body = buf.Bytes()
|
||||
}
|
||||
|
||||
forceReload := r.Header.Get("Cache-Control") == "must-revalidate"
|
||||
|
||||
err := changeConfig(r.Method, r.URL.Path, body, forceReload)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
default:
|
||||
return APIError{
|
||||
Code: http.StatusMethodNotAllowed,
|
||||
Err: fmt.Errorf("method %s not allowed", r.Method),
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func handleConfigID(w http.ResponseWriter, r *http.Request) error {
|
||||
idPath := r.URL.Path
|
||||
|
||||
parts := strings.Split(idPath, "/")
|
||||
if len(parts) < 3 || parts[2] == "" {
|
||||
return fmt.Errorf("request path is missing object ID")
|
||||
}
|
||||
if parts[0] != "" || parts[1] != "id" {
|
||||
return fmt.Errorf("malformed object path")
|
||||
}
|
||||
id := parts[2]
|
||||
|
||||
// map the ID to the expanded path
|
||||
currentCfgMu.RLock()
|
||||
expanded, ok := rawCfgIndex[id]
|
||||
defer currentCfgMu.RUnlock()
|
||||
if !ok {
|
||||
return fmt.Errorf("unknown object ID '%s'", id)
|
||||
}
|
||||
|
||||
// piece the full URL path back together
|
||||
parts = append([]string{expanded}, parts[3:]...)
|
||||
r.URL.Path = path.Join(parts...)
|
||||
|
||||
return ErrInternalRedir
|
||||
}
|
||||
|
||||
func handleStop(w http.ResponseWriter, r *http.Request) error {
|
||||
err := handleUnload(w, r)
|
||||
if err != nil {
|
||||
Log().Named("admin.api").Error("unload error", zap.Error(err))
|
||||
}
|
||||
go func() {
|
||||
err := stopAdminServer(adminServer)
|
||||
var exitCode int
|
||||
if err != nil {
|
||||
exitCode = ExitCodeFailedQuit
|
||||
Log().Named("admin.api").Error("failed to stop admin server gracefully", zap.Error(err))
|
||||
}
|
||||
Log().Named("admin.api").Info("stopping now, bye!! 👋")
|
||||
os.Exit(exitCode)
|
||||
}()
|
||||
return nil
|
||||
}
|
||||
|
||||
// handleUnload stops the current configuration that is running.
|
||||
// Note that doing this can also be accomplished with DELETE /config/
|
||||
// but we leave this function because handleStop uses it.
|
||||
func handleUnload(w http.ResponseWriter, r *http.Request) error {
|
||||
if r.Method != http.MethodPost {
|
||||
return APIError{
|
||||
Code: http.StatusMethodNotAllowed,
|
||||
Err: fmt.Errorf("method not allowed"),
|
||||
}
|
||||
}
|
||||
currentCfgMu.RLock()
|
||||
hasCfg := currentCfg != nil
|
||||
currentCfgMu.RUnlock()
|
||||
if !hasCfg {
|
||||
Log().Named("admin.api").Info("nothing to unload")
|
||||
return nil
|
||||
}
|
||||
Log().Named("admin.api").Info("unloading")
|
||||
if err := stopAndCleanup(); err != nil {
|
||||
Log().Named("admin.api").Error("error unloading", zap.Error(err))
|
||||
} else {
|
||||
Log().Named("admin.api").Info("unloading completed")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// unsyncedConfigAccess traverses into the current config and performs
|
||||
// the operation at path according to method, using body and out as
|
||||
// needed. This is a low-level, unsynchronized function; most callers
|
||||
// will want to use changeConfig or readConfig instead. This requires a
|
||||
// read or write lock on currentCfgMu, depending on method (GET needs
|
||||
// only a read lock; all others need a write lock).
|
||||
func unsyncedConfigAccess(method, path string, body []byte, out io.Writer) error {
|
||||
var err error
|
||||
var val interface{}
|
||||
|
||||
// if there is a request body, decode it into the
|
||||
// variable that will be set in the config according
|
||||
// to method and path
|
||||
if len(body) > 0 {
|
||||
err = json.Unmarshal(body, &val)
|
||||
if err != nil {
|
||||
return fmt.Errorf("decoding request body: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
enc := json.NewEncoder(out)
|
||||
|
||||
cleanPath := strings.Trim(path, "/")
|
||||
if cleanPath == "" {
|
||||
return fmt.Errorf("no traversable path")
|
||||
}
|
||||
|
||||
parts := strings.Split(cleanPath, "/")
|
||||
if len(parts) == 0 {
|
||||
return fmt.Errorf("path missing")
|
||||
}
|
||||
|
||||
// A path that ends with "..." implies:
|
||||
// 1) the part before it is an array
|
||||
// 2) the payload is an array
|
||||
// and means that the user wants to expand the elements
|
||||
// in the payload array and append each one into the
|
||||
// destination array, like so:
|
||||
// array = append(array, elems...)
|
||||
// This special case is handled below.
|
||||
ellipses := parts[len(parts)-1] == "..."
|
||||
if ellipses {
|
||||
parts = parts[:len(parts)-1]
|
||||
}
|
||||
|
||||
var ptr interface{} = rawCfg
|
||||
|
||||
traverseLoop:
|
||||
for i, part := range parts {
|
||||
switch v := ptr.(type) {
|
||||
case map[string]interface{}:
|
||||
// if the next part enters a slice, and the slice is our destination,
|
||||
// handle it specially (because appending to the slice copies the slice
|
||||
// header, which does not replace the original one like we want)
|
||||
if arr, ok := v[part].([]interface{}); ok && i == len(parts)-2 {
|
||||
var idx int
|
||||
if method != http.MethodPost {
|
||||
idxStr := parts[len(parts)-1]
|
||||
idx, err = strconv.Atoi(idxStr)
|
||||
if err != nil {
|
||||
return fmt.Errorf("[%s] invalid array index '%s': %v",
|
||||
path, idxStr, err)
|
||||
}
|
||||
if idx < 0 || idx >= len(arr) {
|
||||
return fmt.Errorf("[%s] array index out of bounds: %s", path, idxStr)
|
||||
}
|
||||
}
|
||||
|
||||
switch method {
|
||||
case http.MethodGet:
|
||||
err = enc.Encode(arr[idx])
|
||||
if err != nil {
|
||||
return fmt.Errorf("encoding config: %v", err)
|
||||
}
|
||||
case http.MethodPost:
|
||||
if ellipses {
|
||||
valArray, ok := val.([]interface{})
|
||||
if !ok {
|
||||
return fmt.Errorf("final element is not an array")
|
||||
}
|
||||
v[part] = append(arr, valArray...)
|
||||
} else {
|
||||
v[part] = append(arr, val)
|
||||
}
|
||||
case http.MethodPut:
|
||||
// avoid creation of new slice and a second copy (see
|
||||
// https://github.com/golang/go/wiki/SliceTricks#insert)
|
||||
arr = append(arr, nil)
|
||||
copy(arr[idx+1:], arr[idx:])
|
||||
arr[idx] = val
|
||||
v[part] = arr
|
||||
case http.MethodPatch:
|
||||
arr[idx] = val
|
||||
case http.MethodDelete:
|
||||
v[part] = append(arr[:idx], arr[idx+1:]...)
|
||||
default:
|
||||
return fmt.Errorf("unrecognized method %s", method)
|
||||
}
|
||||
break traverseLoop
|
||||
}
|
||||
|
||||
if i == len(parts)-1 {
|
||||
switch method {
|
||||
case http.MethodGet:
|
||||
err = enc.Encode(v[part])
|
||||
if err != nil {
|
||||
return fmt.Errorf("encoding config: %v", err)
|
||||
}
|
||||
case http.MethodPost:
|
||||
// if the part is an existing list, POST appends to
|
||||
// it, otherwise it just sets or creates the value
|
||||
if arr, ok := v[part].([]interface{}); ok {
|
||||
if ellipses {
|
||||
valArray, ok := val.([]interface{})
|
||||
if !ok {
|
||||
return fmt.Errorf("final element is not an array")
|
||||
}
|
||||
v[part] = append(arr, valArray...)
|
||||
} else {
|
||||
v[part] = append(arr, val)
|
||||
}
|
||||
} else {
|
||||
v[part] = val
|
||||
}
|
||||
case http.MethodPut:
|
||||
if _, ok := v[part]; ok {
|
||||
return fmt.Errorf("[%s] key already exists: %s", path, part)
|
||||
}
|
||||
v[part] = val
|
||||
case http.MethodPatch:
|
||||
if _, ok := v[part]; !ok {
|
||||
return fmt.Errorf("[%s] key does not exist: %s", path, part)
|
||||
}
|
||||
v[part] = val
|
||||
case http.MethodDelete:
|
||||
delete(v, part)
|
||||
default:
|
||||
return fmt.Errorf("unrecognized method %s", method)
|
||||
}
|
||||
} else {
|
||||
// if we are "PUTting" a new resource, the key(s) in its path
|
||||
// might not exist yet; that's OK but we need to make them as
|
||||
// we go, while we still have a pointer from the level above
|
||||
if v[part] == nil && method == http.MethodPut {
|
||||
v[part] = make(map[string]interface{})
|
||||
}
|
||||
ptr = v[part]
|
||||
}
|
||||
|
||||
case []interface{}:
|
||||
partInt, err := strconv.Atoi(part)
|
||||
if err != nil {
|
||||
return fmt.Errorf("[/%s] invalid array index '%s': %v",
|
||||
strings.Join(parts[:i+1], "/"), part, err)
|
||||
}
|
||||
if partInt < 0 || partInt >= len(v) {
|
||||
return fmt.Errorf("[/%s] array index out of bounds: %s",
|
||||
strings.Join(parts[:i+1], "/"), part)
|
||||
}
|
||||
ptr = v[partInt]
|
||||
|
||||
default:
|
||||
return fmt.Errorf("invalid traversal path at: %s", strings.Join(parts[:i+1], "/"))
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// RemoveMetaFields removes meta fields like "@id" from a JSON message
|
||||
// by using a simple regular expression. (An alternate way to do this
|
||||
// would be to delete them from the raw, map[string]interface{}
|
||||
// representation as they are indexed, then iterate the index we made
|
||||
// and add them back after encoding as JSON, but this is simpler.)
|
||||
func RemoveMetaFields(rawJSON []byte) []byte {
|
||||
return idRegexp.ReplaceAllFunc(rawJSON, func(in []byte) []byte {
|
||||
// matches with a comma on both sides (when "@id" property is
|
||||
// not the first or last in the object) need to keep exactly
|
||||
// one comma for correct JSON syntax
|
||||
comma := []byte{','}
|
||||
if bytes.HasPrefix(in, comma) && bytes.HasSuffix(in, comma) {
|
||||
return comma
|
||||
}
|
||||
return []byte{}
|
||||
})
|
||||
}
|
||||
|
||||
// AdminHandler is like http.Handler except ServeHTTP may return an error.
|
||||
//
|
||||
// If any handler encounters an error, it should be returned for proper
|
||||
// handling.
|
||||
type AdminHandler interface {
|
||||
ServeHTTP(http.ResponseWriter, *http.Request) error
|
||||
}
|
||||
|
||||
// AdminHandlerFunc is a convenience type like http.HandlerFunc.
|
||||
type AdminHandlerFunc func(http.ResponseWriter, *http.Request) error
|
||||
|
||||
// ServeHTTP implements the Handler interface.
|
||||
func (f AdminHandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request) error {
|
||||
return f(w, r)
|
||||
}
|
||||
|
||||
// APIError is a structured error that every API
|
||||
// handler should return for consistency in logging
|
||||
// and client responses. If Message is unset, then
|
||||
// Err.Error() will be serialized in its place.
|
||||
type APIError struct {
|
||||
Code int `json:"-"`
|
||||
Err error `json:"-"`
|
||||
Message string `json:"error"`
|
||||
}
|
||||
|
||||
func (e APIError) Error() string {
|
||||
if e.Err != nil {
|
||||
return e.Err.Error()
|
||||
}
|
||||
return e.Message
|
||||
}
|
||||
|
||||
var (
|
||||
// DefaultAdminListen is the address for the admin
|
||||
// listener, if none is specified at startup.
|
||||
DefaultAdminListen = "localhost:2019"
|
||||
|
||||
// ErrInternalRedir indicates an internal redirect
|
||||
// and is useful when admin API handlers rewrite
|
||||
// the request; in that case, authentication and
|
||||
// authorization needs to happen again for the
|
||||
// rewritten request.
|
||||
ErrInternalRedir = fmt.Errorf("internal redirect; re-authorization required")
|
||||
|
||||
// DefaultAdminConfig is the default configuration
|
||||
// for the administration endpoint.
|
||||
DefaultAdminConfig = &AdminConfig{
|
||||
Listen: DefaultAdminListen,
|
||||
}
|
||||
)
|
||||
|
||||
// idRegexp is used to match ID fields and their associated values
|
||||
// in the config. It also matches adjacent commas so that syntax
|
||||
// can be preserved no matter where in the object the field appears.
|
||||
// It supports string and most numeric values.
|
||||
var idRegexp = regexp.MustCompile(`(?m),?\s*"` + idKey + `":\s?(-?[0-9]+(\.[0-9]+)?|(?U)".*")\s*,?`)
|
||||
|
||||
const (
|
||||
rawConfigKey = "config"
|
||||
idKey = "@id"
|
||||
)
|
||||
|
||||
var bufPool = sync.Pool{
|
||||
New: func() interface{} {
|
||||
return new(bytes.Buffer)
|
||||
},
|
||||
}
|
||||
|
||||
var adminServer *http.Server
|
||||
-132
@@ -1,132 +0,0 @@
|
||||
// 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 caddy
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestUnsyncedConfigAccess(t *testing.T) {
|
||||
// each test is performed in sequence, so
|
||||
// each change builds on the previous ones;
|
||||
// the config is not reset between tests
|
||||
for i, tc := range []struct {
|
||||
method string
|
||||
path string // rawConfigKey will be prepended
|
||||
payload string
|
||||
expect string // JSON representation of what the whole config is expected to be after the request
|
||||
shouldErr bool
|
||||
}{
|
||||
{
|
||||
method: "POST",
|
||||
path: "",
|
||||
payload: `{"foo": "bar", "list": ["a", "b", "c"]}`, // starting value
|
||||
expect: `{"foo": "bar", "list": ["a", "b", "c"]}`,
|
||||
},
|
||||
{
|
||||
method: "POST",
|
||||
path: "/foo",
|
||||
payload: `"jet"`,
|
||||
expect: `{"foo": "jet", "list": ["a", "b", "c"]}`,
|
||||
},
|
||||
{
|
||||
method: "POST",
|
||||
path: "/bar",
|
||||
payload: `{"aa": "bb", "qq": "zz"}`,
|
||||
expect: `{"foo": "jet", "bar": {"aa": "bb", "qq": "zz"}, "list": ["a", "b", "c"]}`,
|
||||
},
|
||||
{
|
||||
method: "DELETE",
|
||||
path: "/bar/qq",
|
||||
expect: `{"foo": "jet", "bar": {"aa": "bb"}, "list": ["a", "b", "c"]}`,
|
||||
},
|
||||
{
|
||||
method: "POST",
|
||||
path: "/list",
|
||||
payload: `"e"`,
|
||||
expect: `{"foo": "jet", "bar": {"aa": "bb"}, "list": ["a", "b", "c", "e"]}`,
|
||||
},
|
||||
{
|
||||
method: "PUT",
|
||||
path: "/list/3",
|
||||
payload: `"d"`,
|
||||
expect: `{"foo": "jet", "bar": {"aa": "bb"}, "list": ["a", "b", "c", "d", "e"]}`,
|
||||
},
|
||||
{
|
||||
method: "DELETE",
|
||||
path: "/list/3",
|
||||
expect: `{"foo": "jet", "bar": {"aa": "bb"}, "list": ["a", "b", "c", "e"]}`,
|
||||
},
|
||||
{
|
||||
method: "PATCH",
|
||||
path: "/list/3",
|
||||
payload: `"d"`,
|
||||
expect: `{"foo": "jet", "bar": {"aa": "bb"}, "list": ["a", "b", "c", "d"]}`,
|
||||
},
|
||||
{
|
||||
method: "POST",
|
||||
path: "/list/...",
|
||||
payload: `["e", "f", "g"]`,
|
||||
expect: `{"foo": "jet", "bar": {"aa": "bb"}, "list": ["a", "b", "c", "d", "e", "f", "g"]}`,
|
||||
},
|
||||
} {
|
||||
err := unsyncedConfigAccess(tc.method, rawConfigKey+tc.path, []byte(tc.payload), nil)
|
||||
|
||||
if tc.shouldErr && err == nil {
|
||||
t.Fatalf("Test %d: Expected error return value, but got: %v", i, err)
|
||||
}
|
||||
if !tc.shouldErr && err != nil {
|
||||
t.Fatalf("Test %d: Should not have had error return value, but got: %v", i, err)
|
||||
}
|
||||
|
||||
// decode the expected config so we can do a convenient DeepEqual
|
||||
var expectedDecoded interface{}
|
||||
err = json.Unmarshal([]byte(tc.expect), &expectedDecoded)
|
||||
if err != nil {
|
||||
t.Fatalf("Test %d: Unmarshaling expected config: %v", i, err)
|
||||
}
|
||||
|
||||
// make sure the resulting config is as we expect it
|
||||
if !reflect.DeepEqual(rawCfg[rawConfigKey], expectedDecoded) {
|
||||
t.Fatalf("Test %d:\nExpected:\n\t%#v\nActual:\n\t%#v",
|
||||
i, expectedDecoded, rawCfg[rawConfigKey])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkLoad(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
cfg := []byte(`{
|
||||
"apps": {
|
||||
"http": {
|
||||
"servers": {
|
||||
"myserver": {
|
||||
"listen": ["tcp/localhost:8080-8084"],
|
||||
"read_timeout": "30s"
|
||||
},
|
||||
"yourserver": {
|
||||
"listen": ["127.0.0.1:5000"],
|
||||
"read_header_timeout": "15s"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
`)
|
||||
Load(cfg, true)
|
||||
}
|
||||
}
|
||||
@@ -1,263 +0,0 @@
|
||||
# Mutilated beyond recognition from the example at:
|
||||
# https://docs.microsoft.com/azure/devops/pipelines/languages/go
|
||||
|
||||
trigger:
|
||||
- v2
|
||||
|
||||
schedules:
|
||||
- cron: "0 0 * * *"
|
||||
displayName: Daily midnight fuzzing
|
||||
branches:
|
||||
include:
|
||||
- v2
|
||||
always: true
|
||||
|
||||
variables:
|
||||
GOROOT: $(gorootDir)/go
|
||||
GOPATH: $(system.defaultWorkingDirectory)/gopath
|
||||
GOBIN: $(GOPATH)/bin
|
||||
modulePath: '$(GOPATH)/src/github.com/$(build.repository.name)'
|
||||
|
||||
jobs:
|
||||
- job: crossPlatformTest
|
||||
displayName: "Cross-Platform Tests"
|
||||
strategy:
|
||||
matrix:
|
||||
linux:
|
||||
imageName: ubuntu-16.04
|
||||
gorootDir: /usr/local
|
||||
mac:
|
||||
imageName: macos-10.14
|
||||
gorootDir: /usr/local
|
||||
windows:
|
||||
imageName: windows-2019
|
||||
gorootDir: C:\
|
||||
pool:
|
||||
vmImage: $(imageName)
|
||||
|
||||
steps:
|
||||
- bash: |
|
||||
latestGo=$(curl "https://golang.org/VERSION?m=text")
|
||||
echo "##vso[task.setvariable variable=LATEST_GO]$latestGo"
|
||||
echo "Latest Go version: $latestGo"
|
||||
displayName: "Get latest Go version"
|
||||
|
||||
- bash: |
|
||||
sudo rm -f $(which go)
|
||||
echo '##vso[task.prependpath]$(GOBIN)'
|
||||
echo '##vso[task.prependpath]$(GOROOT)/bin'
|
||||
mkdir -p '$(modulePath)'
|
||||
shopt -s extglob
|
||||
shopt -s dotglob
|
||||
mv !(gopath) '$(modulePath)'
|
||||
displayName: Remove old Go, set GOBIN/GOROOT, and move project into GOPATH
|
||||
|
||||
# Install Go (this varies by platform)
|
||||
- bash: |
|
||||
wget "https://dl.google.com/go/$(LATEST_GO).linux-amd64.tar.gz"
|
||||
sudo tar -C $(gorootDir) -xzf "$(LATEST_GO).linux-amd64.tar.gz"
|
||||
condition: eq( variables['Agent.OS'], 'Linux' )
|
||||
displayName: Install Go on Linux
|
||||
|
||||
- bash: |
|
||||
wget "https://dl.google.com/go/$(LATEST_GO).darwin-amd64.tar.gz"
|
||||
sudo tar -C $(gorootDir) -xzf "$(LATEST_GO).darwin-amd64.tar.gz"
|
||||
condition: eq( variables['Agent.OS'], 'Darwin' )
|
||||
displayName: Install Go on macOS
|
||||
|
||||
# The low performance is partly due to PowerShell's attempt to update the progress bar. Disabling it speeds up the process.
|
||||
# Reference: https://github.com/PowerShell/PowerShell/issues/2138
|
||||
- powershell: |
|
||||
$ProgressPreference = 'SilentlyContinue'
|
||||
Write-Host "Downloading Go..."
|
||||
(New-Object System.Net.WebClient).DownloadFile("https://dl.google.com/go/$(LATEST_GO).windows-amd64.zip", "$(LATEST_GO).windows-amd64.zip")
|
||||
Write-Host "Extracting Go... (I'm slow too)"
|
||||
7z x "$(LATEST_GO).windows-amd64.zip" -o"$(gorootDir)"
|
||||
condition: eq( variables['Agent.OS'], 'Windows_NT' )
|
||||
displayName: Install Go on Windows
|
||||
|
||||
- bash: curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.23.6
|
||||
displayName: Install golangci-lint
|
||||
|
||||
- script: |
|
||||
go get github.com/axw/gocov/gocov
|
||||
go get github.com/AlekSi/gocov-xml
|
||||
go get -u github.com/jstemmer/go-junit-report
|
||||
displayName: Install test and coverage analysis tools
|
||||
|
||||
- bash: |
|
||||
printf "Using go at: $(which go)\n"
|
||||
printf "Go version: $(go version)\n"
|
||||
printf "\n\nGo environment:\n\n"
|
||||
go env
|
||||
printf "\n\nSystem environment:\n\n"
|
||||
env
|
||||
displayName: Print Go version and environment
|
||||
|
||||
- script: |
|
||||
go get -v -t -d ./...
|
||||
mkdir test-results
|
||||
workingDirectory: '$(modulePath)'
|
||||
displayName: Get dependencies
|
||||
|
||||
- bash: CGO_ENABLED=0 go build -trimpath -a -ldflags="-w -s" -v
|
||||
workingDirectory: '$(modulePath)/cmd/caddy'
|
||||
displayName: Build Caddy
|
||||
|
||||
- task: PublishBuildArtifacts@1
|
||||
condition: eq( variables['Agent.OS'], 'Windows_NT' )
|
||||
inputs:
|
||||
pathtoPublish: '$(modulePath)/cmd/caddy/caddy.exe'
|
||||
artifactName: caddy_v2.exe
|
||||
|
||||
- task: PublishBuildArtifacts@1
|
||||
condition: ne( variables['Agent.OS'], 'Windows_NT' )
|
||||
inputs:
|
||||
pathtoPublish: '$(modulePath)/cmd/caddy/caddy'
|
||||
artifactName: 'caddy_v2_$(Agent.OS)'
|
||||
|
||||
# its behavior is governed by .golangci.yml
|
||||
- script: |
|
||||
(golangci-lint run --out-format junit-xml) > test-results/lint-result.xml
|
||||
exit 0
|
||||
workingDirectory: '$(modulePath)'
|
||||
continueOnError: true
|
||||
displayName: Run lint check
|
||||
|
||||
- script: |
|
||||
(go test -v -coverprofile=cover-profile.out -race ./... 2>&1) > test-results/test-result.out
|
||||
workingDirectory: '$(modulePath)'
|
||||
continueOnError: true
|
||||
displayName: Run tests
|
||||
|
||||
- script: |
|
||||
set -e
|
||||
cmd/caddy/caddy start
|
||||
go test -v -count=1 ./caddytest/...
|
||||
cmd/caddy/caddy stop
|
||||
workingDirectory: '$(modulePath)'
|
||||
continueOnError: false
|
||||
displayName: Run Integration tests
|
||||
|
||||
- script: |
|
||||
mkdir coverage
|
||||
gocov convert cover-profile.out > coverage/coverage.json
|
||||
# Because Windows doesn't work with input redirection like *nix, but output redirection works.
|
||||
(cat ./coverage/coverage.json | gocov-xml) > coverage/coverage.xml
|
||||
workingDirectory: '$(modulePath)'
|
||||
displayName: Prepare coverage reports
|
||||
|
||||
- script: |
|
||||
(cat ./test-results/test-result.out | go-junit-report) > test-results/test-result.xml
|
||||
workingDirectory: '$(modulePath)'
|
||||
displayName: Prepare test report
|
||||
|
||||
- task: PublishCodeCoverageResults@1
|
||||
displayName: Publish test coverage report
|
||||
inputs:
|
||||
codeCoverageTool: Cobertura
|
||||
summaryFileLocation: $(modulePath)/coverage/coverage.xml
|
||||
|
||||
- task: PublishTestResults@2
|
||||
displayName: Publish unit test
|
||||
inputs:
|
||||
testResultsFormat: 'JUnit'
|
||||
testResultsFiles: $(modulePath)/test-results/test-result.xml
|
||||
testRunTitle: $(agent.OS) Unit Test
|
||||
mergeTestResults: false
|
||||
|
||||
- task: PublishTestResults@2
|
||||
displayName: Publish lint results
|
||||
inputs:
|
||||
testResultsFormat: 'JUnit'
|
||||
testResultsFiles: $(modulePath)/test-results/lint-result.xml
|
||||
testRunTitle: $(agent.OS) Lint
|
||||
mergeTestResults: false
|
||||
|
||||
- bash: |
|
||||
exit 1
|
||||
condition: eq(variables['Agent.JobStatus'], 'SucceededWithIssues')
|
||||
displayName: Coerce correct build result
|
||||
|
||||
- job: fuzzing
|
||||
displayName: 'Fuzzing'
|
||||
# Only run this job on schedules or PRs for non-forks.
|
||||
condition: or(eq(variables['System.PullRequest.IsFork'], 'False'), eq(variables['Build.Reason'], 'Schedule') )
|
||||
strategy:
|
||||
matrix:
|
||||
linux:
|
||||
imageName: ubuntu-16.04
|
||||
gorootDir: /usr/local
|
||||
pool:
|
||||
vmImage: $(imageName)
|
||||
|
||||
steps:
|
||||
- bash: |
|
||||
latestGo=$(curl "https://golang.org/VERSION?m=text")
|
||||
echo "##vso[task.setvariable variable=LATEST_GO]$latestGo"
|
||||
echo "Latest Go version: $latestGo"
|
||||
displayName: "Get latest Go version"
|
||||
|
||||
- bash: |
|
||||
sudo rm -f $(which go)
|
||||
echo '##vso[task.prependpath]$(GOBIN)'
|
||||
echo '##vso[task.prependpath]$(GOROOT)/bin'
|
||||
mkdir -p '$(modulePath)'
|
||||
shopt -s extglob
|
||||
shopt -s dotglob
|
||||
mv !(gopath) '$(modulePath)'
|
||||
displayName: Remove old Go, set GOBIN/GOROOT, and move project into GOPATH
|
||||
|
||||
- bash: |
|
||||
wget "https://dl.google.com/go/$(LATEST_GO).linux-amd64.tar.gz"
|
||||
sudo tar -C $(gorootDir) -xzf "$(LATEST_GO).linux-amd64.tar.gz"
|
||||
condition: eq( variables['Agent.OS'], 'Linux' )
|
||||
displayName: Install Go on Linux
|
||||
|
||||
- bash: |
|
||||
# Install Clang-7.0 because other versions seem to be missing the file libclang_rt.fuzzer-x86_64.a
|
||||
sudo add-apt-repository "deb http://apt.llvm.org/xenial/ llvm-toolchain-xenial-7 main"
|
||||
wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add -
|
||||
sudo apt update && sudo apt install -y clang-7 lldb-7 lld-7
|
||||
|
||||
go get -v github.com/dvyukov/go-fuzz/go-fuzz github.com/dvyukov/go-fuzz/go-fuzz-build
|
||||
wget -q -O fuzzit https://github.com/fuzzitdev/fuzzit/releases/download/v2.4.77/fuzzit_Linux_x86_64
|
||||
chmod a+x fuzzit
|
||||
mv fuzzit $(GOBIN)
|
||||
displayName: Download go-fuzz tools and the Fuzzit CLI, and move Fuzzit CLI to GOBIN
|
||||
condition: and(eq(variables['System.PullRequest.IsFork'], 'False') , eq( variables['Agent.OS'], 'Linux' ))
|
||||
|
||||
- bash: |
|
||||
declare -A fuzzers_funcs=(\
|
||||
["./caddyconfig/httpcaddyfile/addresses_fuzz.go"]="FuzzParseAddress" \
|
||||
["./caddyconfig/caddyfile/parse_fuzz.go"]="FuzzParseCaddyfile" \
|
||||
["./listeners_fuzz.go"]="FuzzParseNetworkAddress" \
|
||||
["./replacer_fuzz.go"]="FuzzReplacer" \
|
||||
)
|
||||
|
||||
declare -A fuzzers_targets=(\
|
||||
["./caddyconfig/httpcaddyfile/addresses_fuzz.go"]="parse-address" \
|
||||
["./caddyconfig/caddyfile/parse_fuzz.go"]="parse-caddyfile" \
|
||||
["./listeners_fuzz.go"]="parse-network-address" \
|
||||
["./replacer_fuzz.go"]="replacer" \
|
||||
)
|
||||
|
||||
fuzz_type="local-regression"
|
||||
if [[ $(Build.Reason) == "Schedule" ]]; then
|
||||
fuzz_type="fuzzing"
|
||||
fi
|
||||
echo "Fuzzing type: $fuzz_type"
|
||||
|
||||
for f in $(find . -name \*_fuzz.go); do
|
||||
FUZZER_DIRECTORY=$(dirname $f)
|
||||
echo "go-fuzz-build func ${fuzzers_funcs[$f]} residing in $f"
|
||||
go-fuzz-build -func "${fuzzers_funcs[$f]}" -libfuzzer -o "$FUZZER_DIRECTORY/${fuzzers_targets[$f]}.a" $FUZZER_DIRECTORY
|
||||
echo "Generating fuzzer binary of func ${fuzzers_funcs[$f]} which resides in $f"
|
||||
clang-7 -fsanitize=fuzzer "$FUZZER_DIRECTORY/${fuzzers_targets[$f]}.a" -o "$FUZZER_DIRECTORY/${fuzzers_targets[$f]}"
|
||||
fuzzit create job caddyserver/${fuzzers_targets[$f]} $FUZZER_DIRECTORY/${fuzzers_targets[$f]} --api-key ${FUZZIT_API_KEY} --type "${fuzz_type}" --branch "${SYSTEM_PULLREQUEST_SOURCEBRANCH}" --revision "${BUILD_SOURCEVERSION}"
|
||||
echo "Completed $f"
|
||||
done
|
||||
env:
|
||||
FUZZIT_API_KEY: $(FUZZIT_API_KEY)
|
||||
workingDirectory: '$(modulePath)'
|
||||
displayName: Generate fuzzers & submit them to Fuzzit
|
||||
@@ -1,570 +0,0 @@
|
||||
// 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 caddy
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"runtime/debug"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/caddyserver/certmagic"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// Config is the top (or beginning) of the Caddy configuration structure.
|
||||
// Caddy config is expressed natively as a JSON document. If you prefer
|
||||
// not to work with JSON directly, there are [many config adapters](/docs/config-adapters)
|
||||
// available that can convert various inputs into Caddy JSON.
|
||||
//
|
||||
// Many parts of this config are extensible through the use of Caddy modules.
|
||||
// Fields which have a json.RawMessage type and which appear as dots (•••) in
|
||||
// the online docs can be fulfilled by modules in a certain module
|
||||
// namespace. The docs show which modules can be used in a given place.
|
||||
//
|
||||
// Whenever a module is used, its name must be given either inline as part of
|
||||
// the module, or as the key to the module's value. The docs will make it clear
|
||||
// which to use.
|
||||
//
|
||||
// Generally, all config settings are optional, as it is Caddy convention to
|
||||
// have good, documented default values. If a parameter is required, the docs
|
||||
// should say so.
|
||||
//
|
||||
// Go programs which are directly building a Config struct value should take
|
||||
// care to populate the JSON-encodable fields of the struct (i.e. the fields
|
||||
// with `json` struct tags) if employing the module lifecycle (e.g. Provision
|
||||
// method calls).
|
||||
type Config struct {
|
||||
Admin *AdminConfig `json:"admin,omitempty"`
|
||||
Logging *Logging `json:"logging,omitempty"`
|
||||
|
||||
// StorageRaw is a storage module that defines how/where Caddy
|
||||
// stores assets (such as TLS certificates). The default storage
|
||||
// module is `caddy.storage.file_system` (the local file system),
|
||||
// and the default path
|
||||
// [depends on the OS and environment](/docs/conventions#data-directory).
|
||||
StorageRaw json.RawMessage `json:"storage,omitempty" caddy:"namespace=caddy.storage inline_key=module"`
|
||||
|
||||
// AppsRaw are the apps that Caddy will load and run. The
|
||||
// app module name is the key, and the app's config is the
|
||||
// associated value.
|
||||
AppsRaw ModuleMap `json:"apps,omitempty" caddy:"namespace="`
|
||||
|
||||
apps map[string]App
|
||||
storage certmagic.Storage
|
||||
|
||||
cancelFunc context.CancelFunc
|
||||
}
|
||||
|
||||
// App is a thing that Caddy runs.
|
||||
type App interface {
|
||||
Start() error
|
||||
Stop() error
|
||||
}
|
||||
|
||||
// Run runs the given config, replacing any existing config.
|
||||
func Run(cfg *Config) error {
|
||||
cfgJSON, err := json.Marshal(cfg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return Load(cfgJSON, true)
|
||||
}
|
||||
|
||||
// Load loads the given config JSON and runs it only
|
||||
// if it is different from the current config or
|
||||
// forceReload is true.
|
||||
func Load(cfgJSON []byte, forceReload bool) error {
|
||||
return changeConfig(http.MethodPost, "/"+rawConfigKey, cfgJSON, forceReload)
|
||||
}
|
||||
|
||||
// changeConfig changes the current config (rawCfg) according to the
|
||||
// method, traversed via the given path, and uses the given input as
|
||||
// the new value (if applicable; i.e. "DELETE" doesn't have an input).
|
||||
// If the resulting config is the same as the previous, no reload will
|
||||
// occur unless forceReload is true. This function is safe for
|
||||
// concurrent use.
|
||||
func changeConfig(method, path string, input []byte, forceReload bool) error {
|
||||
switch method {
|
||||
case http.MethodGet,
|
||||
http.MethodHead,
|
||||
http.MethodOptions,
|
||||
http.MethodConnect,
|
||||
http.MethodTrace:
|
||||
return fmt.Errorf("method not allowed")
|
||||
}
|
||||
|
||||
currentCfgMu.Lock()
|
||||
defer currentCfgMu.Unlock()
|
||||
|
||||
err := unsyncedConfigAccess(method, path, input, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// the mutation is complete, so encode the entire config as JSON
|
||||
newCfg, err := json.Marshal(rawCfg[rawConfigKey])
|
||||
if err != nil {
|
||||
return APIError{
|
||||
Code: http.StatusBadRequest,
|
||||
Err: fmt.Errorf("encoding new config: %v", err),
|
||||
}
|
||||
}
|
||||
|
||||
// if nothing changed, no need to do a whole reload unless the client forces it
|
||||
if !forceReload && bytes.Equal(rawCfgJSON, newCfg) {
|
||||
Log().Named("admin.api").Info("config is unchanged")
|
||||
return nil
|
||||
}
|
||||
|
||||
// find any IDs in this config and index them
|
||||
idx := make(map[string]string)
|
||||
err = indexConfigObjects(rawCfg[rawConfigKey], "/"+rawConfigKey, idx)
|
||||
if err != nil {
|
||||
return APIError{
|
||||
Code: http.StatusInternalServerError,
|
||||
Err: fmt.Errorf("indexing config: %v", err),
|
||||
}
|
||||
}
|
||||
|
||||
// load this new config; if it fails, we need to revert to
|
||||
// our old representation of caddy's actual config
|
||||
err = unsyncedDecodeAndRun(newCfg)
|
||||
if err != nil {
|
||||
if len(rawCfgJSON) > 0 {
|
||||
// restore old config state to keep it consistent
|
||||
// with what caddy is still running; we need to
|
||||
// unmarshal it again because it's likely that
|
||||
// pointers deep in our rawCfg map were modified
|
||||
var oldCfg interface{}
|
||||
err2 := json.Unmarshal(rawCfgJSON, &oldCfg)
|
||||
if err2 != nil {
|
||||
err = fmt.Errorf("%v; additionally, restoring old config: %v", err, err2)
|
||||
}
|
||||
rawCfg[rawConfigKey] = oldCfg
|
||||
}
|
||||
|
||||
return fmt.Errorf("loading new config: %v", err)
|
||||
}
|
||||
|
||||
// success, so update our stored copy of the encoded
|
||||
// config to keep it consistent with what caddy is now
|
||||
// running (storing an encoded copy is not strictly
|
||||
// necessary, but avoids an extra json.Marshal for
|
||||
// each config change)
|
||||
rawCfgJSON = newCfg
|
||||
rawCfgIndex = idx
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// readConfig traverses the current config to path
|
||||
// and writes its JSON encoding to out.
|
||||
func readConfig(path string, out io.Writer) error {
|
||||
currentCfgMu.RLock()
|
||||
defer currentCfgMu.RUnlock()
|
||||
return unsyncedConfigAccess(http.MethodGet, path, nil, out)
|
||||
}
|
||||
|
||||
// indexConfigObjects recursively searches ptr for object fields named
|
||||
// "@id" and maps that ID value to the full configPath in the index.
|
||||
// This function is NOT safe for concurrent access; obtain a write lock
|
||||
// on currentCfgMu.
|
||||
func indexConfigObjects(ptr interface{}, configPath string, index map[string]string) error {
|
||||
switch val := ptr.(type) {
|
||||
case map[string]interface{}:
|
||||
for k, v := range val {
|
||||
if k == idKey {
|
||||
switch idVal := v.(type) {
|
||||
case string:
|
||||
index[idVal] = configPath
|
||||
case float64: // all JSON numbers decode as float64
|
||||
index[fmt.Sprintf("%v", idVal)] = configPath
|
||||
default:
|
||||
return fmt.Errorf("%s: %s field must be a string or number", configPath, idKey)
|
||||
}
|
||||
continue
|
||||
}
|
||||
// traverse this object property recursively
|
||||
err := indexConfigObjects(val[k], path.Join(configPath, k), index)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
case []interface{}:
|
||||
// traverse each element of the array recursively
|
||||
for i := range val {
|
||||
err := indexConfigObjects(val[i], path.Join(configPath, strconv.Itoa(i)), index)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// unsyncedDecodeAndRun removes any meta fields (like @id tags)
|
||||
// from cfgJSON, decodes the result into a *Config, and runs
|
||||
// it as the new config, replacing any other current config.
|
||||
// It does NOT update the raw config state, as this is a
|
||||
// lower-level function; most callers will want to use Load
|
||||
// instead. A write lock on currentCfgMu is required!
|
||||
func unsyncedDecodeAndRun(cfgJSON []byte) error {
|
||||
// remove any @id fields from the JSON, which would cause
|
||||
// loading to break since the field wouldn't be recognized
|
||||
strippedCfgJSON := RemoveMetaFields(cfgJSON)
|
||||
|
||||
var newCfg *Config
|
||||
err := strictUnmarshalJSON(strippedCfgJSON, &newCfg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// run the new config and start all its apps
|
||||
err = run(newCfg, true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// swap old config with the new one
|
||||
oldCfg := currentCfg
|
||||
currentCfg = newCfg
|
||||
|
||||
// Stop, Cleanup each old app
|
||||
unsyncedStop(oldCfg)
|
||||
|
||||
// autosave a non-nil config, if not disabled
|
||||
if newCfg != nil &&
|
||||
(newCfg.Admin == nil ||
|
||||
newCfg.Admin.Config == nil ||
|
||||
newCfg.Admin.Config.Persist == nil ||
|
||||
*newCfg.Admin.Config.Persist) {
|
||||
dir := filepath.Dir(ConfigAutosavePath)
|
||||
err := os.MkdirAll(dir, 0700)
|
||||
if err != nil {
|
||||
Log().Error("unable to create folder for config autosave",
|
||||
zap.String("dir", dir),
|
||||
zap.Error(err))
|
||||
} else {
|
||||
err := ioutil.WriteFile(ConfigAutosavePath, cfgJSON, 0600)
|
||||
if err == nil {
|
||||
Log().Info("autosaved config", zap.String("file", ConfigAutosavePath))
|
||||
} else {
|
||||
Log().Error("unable to autosave config",
|
||||
zap.String("file", ConfigAutosavePath),
|
||||
zap.Error(err))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// run runs newCfg and starts all its apps if
|
||||
// start is true. If any errors happen, cleanup
|
||||
// is performed if any modules were provisioned;
|
||||
// apps that were started already will be stopped,
|
||||
// so this function should not leak resources if
|
||||
// an error is returned. However, if no error is
|
||||
// returned and start == false, you should cancel
|
||||
// the config if you are not going to start it,
|
||||
// so that each provisioned module will be
|
||||
// cleaned up.
|
||||
//
|
||||
// This is a low-level function; most callers
|
||||
// will want to use Run instead, which also
|
||||
// updates the config's raw state.
|
||||
func run(newCfg *Config, start bool) error {
|
||||
// because we will need to roll back any state
|
||||
// modifications if this function errors, we
|
||||
// keep a single error value and scope all
|
||||
// sub-operations to their own functions to
|
||||
// ensure this error value does not get
|
||||
// overridden or missed when it should have
|
||||
// been set by a short assignment
|
||||
var err error
|
||||
|
||||
// start the admin endpoint (and stop any prior one)
|
||||
if start {
|
||||
err = replaceAdmin(newCfg)
|
||||
if err != nil {
|
||||
return fmt.Errorf("starting caddy administration endpoint: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if newCfg == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// prepare the new config for use
|
||||
newCfg.apps = make(map[string]App)
|
||||
|
||||
// create a context within which to load
|
||||
// modules - essentially our new config's
|
||||
// execution environment; be sure that
|
||||
// cleanup occurs when we return if there
|
||||
// was an error; if no error, it will get
|
||||
// cleaned up on next config cycle
|
||||
ctx, cancel := NewContext(Context{Context: context.Background(), cfg: newCfg})
|
||||
defer func() {
|
||||
if err != nil {
|
||||
// if there were any errors during startup,
|
||||
// we should cancel the new context we created
|
||||
// since the associated config won't be used;
|
||||
// this will cause all modules that were newly
|
||||
// provisioned to clean themselves up
|
||||
cancel()
|
||||
|
||||
// also undo any other state changes we made
|
||||
if currentCfg != nil {
|
||||
certmagic.Default.Storage = currentCfg.storage
|
||||
}
|
||||
}
|
||||
}()
|
||||
newCfg.cancelFunc = cancel // clean up later
|
||||
|
||||
// set up logging before anything bad happens
|
||||
if newCfg.Logging == nil {
|
||||
newCfg.Logging = new(Logging)
|
||||
}
|
||||
err = newCfg.Logging.openLogs(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// set up global storage and make it CertMagic's default storage, too
|
||||
err = func() error {
|
||||
if newCfg.StorageRaw != nil {
|
||||
val, err := ctx.LoadModule(newCfg, "StorageRaw")
|
||||
if err != nil {
|
||||
return fmt.Errorf("loading storage module: %v", err)
|
||||
}
|
||||
stor, err := val.(StorageConverter).CertMagicStorage()
|
||||
if err != nil {
|
||||
return fmt.Errorf("creating storage value: %v", err)
|
||||
}
|
||||
newCfg.storage = stor
|
||||
}
|
||||
|
||||
if newCfg.storage == nil {
|
||||
newCfg.storage = &certmagic.FileStorage{Path: AppDataDir()}
|
||||
}
|
||||
certmagic.Default.Storage = newCfg.storage
|
||||
|
||||
return nil
|
||||
}()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Load and Provision each app and their submodules
|
||||
err = func() error {
|
||||
for appName := range newCfg.AppsRaw {
|
||||
if _, err := ctx.App(appName); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !start {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Start
|
||||
return func() error {
|
||||
var started []string
|
||||
for name, a := range newCfg.apps {
|
||||
err := a.Start()
|
||||
if err != nil {
|
||||
// an app failed to start, so we need to stop
|
||||
// all other apps that were already started
|
||||
for _, otherAppName := range started {
|
||||
err2 := newCfg.apps[otherAppName].Stop()
|
||||
if err2 != nil {
|
||||
err = fmt.Errorf("%v; additionally, aborting app %s: %v",
|
||||
err, otherAppName, err2)
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("%s app module: start: %v", name, err)
|
||||
}
|
||||
started = append(started, name)
|
||||
}
|
||||
return nil
|
||||
}()
|
||||
}
|
||||
|
||||
// Stop stops running the current configuration.
|
||||
// It is the antithesis of Run(). This function
|
||||
// will log any errors that occur during the
|
||||
// stopping of individual apps and continue to
|
||||
// stop the others. Stop should only be called
|
||||
// if not replacing with a new config.
|
||||
func Stop() error {
|
||||
currentCfgMu.Lock()
|
||||
defer currentCfgMu.Unlock()
|
||||
unsyncedStop(currentCfg)
|
||||
currentCfg = nil
|
||||
rawCfgJSON = nil
|
||||
rawCfgIndex = nil
|
||||
rawCfg[rawConfigKey] = nil
|
||||
return nil
|
||||
}
|
||||
|
||||
// unsyncedStop stops cfg from running, but has
|
||||
// no locking around cfg. It is a no-op if cfg is
|
||||
// nil. If any app returns an error when stopping,
|
||||
// it is logged and the function continues stopping
|
||||
// the next app. This function assumes all apps in
|
||||
// cfg were successfully started first.
|
||||
func unsyncedStop(cfg *Config) {
|
||||
if cfg == nil {
|
||||
return
|
||||
}
|
||||
|
||||
// stop each app
|
||||
for name, a := range cfg.apps {
|
||||
err := a.Stop()
|
||||
if err != nil {
|
||||
log.Printf("[ERROR] stop %s: %v", name, err)
|
||||
}
|
||||
}
|
||||
|
||||
// clean up all modules
|
||||
cfg.cancelFunc()
|
||||
}
|
||||
|
||||
// stopAndCleanup calls stop and cleans up anything
|
||||
// else that is expedient. This should only be used
|
||||
// when stopping and not replacing with a new config.
|
||||
func stopAndCleanup() error {
|
||||
if err := Stop(); err != nil {
|
||||
return err
|
||||
}
|
||||
certmagic.CleanUpOwnLocks()
|
||||
return nil
|
||||
}
|
||||
|
||||
// Validate loads, provisions, and validates
|
||||
// cfg, but does not start running it.
|
||||
func Validate(cfg *Config) error {
|
||||
err := run(cfg, false)
|
||||
if err == nil {
|
||||
cfg.cancelFunc() // call Cleanup on all modules
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// Duration can be an integer or a string. An integer is
|
||||
// interpreted as nanoseconds. If a string, it is a Go
|
||||
// time.Duration value such as `300ms`, `1.5h`, or `2h45m`;
|
||||
// valid units are `ns`, `us`/`µs`, `ms`, `s`, `m`, and `h`.
|
||||
type Duration time.Duration
|
||||
|
||||
// UnmarshalJSON satisfies json.Unmarshaler.
|
||||
func (d *Duration) UnmarshalJSON(b []byte) error {
|
||||
if len(b) == 0 {
|
||||
return io.EOF
|
||||
}
|
||||
var dur time.Duration
|
||||
var err error
|
||||
if b[0] == byte('"') && b[len(b)-1] == byte('"') {
|
||||
dur, err = time.ParseDuration(strings.Trim(string(b), `"`))
|
||||
} else {
|
||||
err = json.Unmarshal(b, &dur)
|
||||
}
|
||||
*d = Duration(dur)
|
||||
return err
|
||||
}
|
||||
|
||||
// GoModule returns the build info of this Caddy
|
||||
// build from debug.BuildInfo (requires Go modules).
|
||||
// If no version information is available, a non-nil
|
||||
// value will still be returned, but with an
|
||||
// unknown version.
|
||||
func GoModule() *debug.Module {
|
||||
var mod debug.Module
|
||||
return goModule(&mod)
|
||||
}
|
||||
|
||||
// goModule holds the actual implementation of GoModule.
|
||||
// Allocating debug.Module in GoModule() and passing a
|
||||
// reference to goModule enables mid-stack inlining.
|
||||
func goModule(mod *debug.Module) *debug.Module {
|
||||
mod.Version = "unknown"
|
||||
bi, ok := debug.ReadBuildInfo()
|
||||
if ok {
|
||||
mod.Path = bi.Main.Path
|
||||
// The recommended way to build Caddy involves
|
||||
// creating a separate main module, which
|
||||
// TODO: track related Go issue: https://github.com/golang/go/issues/29228
|
||||
// once that issue is fixed, we should just be able to use bi.Main... hopefully.
|
||||
for _, dep := range bi.Deps {
|
||||
if dep.Path == ImportPath {
|
||||
return dep
|
||||
}
|
||||
}
|
||||
return &bi.Main
|
||||
}
|
||||
return mod
|
||||
}
|
||||
|
||||
// CtxKey is a value type for use with context.WithValue.
|
||||
type CtxKey string
|
||||
|
||||
// This group of variables pertains to the current configuration.
|
||||
var (
|
||||
// currentCfgMu protects everything in this var block.
|
||||
currentCfgMu sync.RWMutex
|
||||
|
||||
// currentCfg is the currently-running configuration.
|
||||
currentCfg *Config
|
||||
|
||||
// rawCfg is the current, generic-decoded configuration;
|
||||
// we initialize it as a map with one field ("config")
|
||||
// to maintain parity with the API endpoint and to avoid
|
||||
// the special case of having to access/mutate the variable
|
||||
// directly without traversing into it.
|
||||
rawCfg = map[string]interface{}{
|
||||
rawConfigKey: nil,
|
||||
}
|
||||
|
||||
// rawCfgJSON is the JSON-encoded form of rawCfg. Keeping
|
||||
// this around avoids an extra Marshal call during changes.
|
||||
rawCfgJSON []byte
|
||||
|
||||
// rawCfgIndex is the map of user-assigned ID to expanded
|
||||
// path, for converting /id/ paths to /config/ paths.
|
||||
rawCfgIndex map[string]string
|
||||
)
|
||||
|
||||
// ImportPath is the package import path for Caddy core.
|
||||
const ImportPath = "github.com/caddyserver/caddy/v2"
|
||||
@@ -1,86 +0,0 @@
|
||||
// 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 caddyfile
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/caddyserver/caddy/v2"
|
||||
"github.com/caddyserver/caddy/v2/caddyconfig"
|
||||
)
|
||||
|
||||
// Adapter adapts Caddyfile to Caddy JSON.
|
||||
type Adapter struct {
|
||||
ServerType ServerType
|
||||
}
|
||||
|
||||
// Adapt converts the Caddyfile config in body to Caddy JSON.
|
||||
func (a Adapter) Adapt(body []byte, options map[string]interface{}) ([]byte, []caddyconfig.Warning, error) {
|
||||
if a.ServerType == nil {
|
||||
return nil, nil, fmt.Errorf("no server type")
|
||||
}
|
||||
if options == nil {
|
||||
options = make(map[string]interface{})
|
||||
}
|
||||
|
||||
filename, _ := options["filename"].(string)
|
||||
if filename == "" {
|
||||
filename = "Caddyfile"
|
||||
}
|
||||
|
||||
serverBlocks, err := Parse(filename, body)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
cfg, warnings, err := a.ServerType.Setup(serverBlocks, options)
|
||||
if err != nil {
|
||||
return nil, warnings, err
|
||||
}
|
||||
|
||||
marshalFunc := json.Marshal
|
||||
if options["pretty"] == "true" {
|
||||
marshalFunc = caddyconfig.JSONIndent
|
||||
}
|
||||
result, err := marshalFunc(cfg)
|
||||
|
||||
return result, warnings, err
|
||||
}
|
||||
|
||||
// Unmarshaler is a type that can unmarshal
|
||||
// Caddyfile tokens to set itself up for a
|
||||
// JSON encoding. The goal of an unmarshaler
|
||||
// is not to set itself up for actual use,
|
||||
// but to set itself up for being marshaled
|
||||
// into JSON. Caddyfile-unmarshaled values
|
||||
// will not be used directly; they will be
|
||||
// encoded as JSON and then used from that.
|
||||
type Unmarshaler interface {
|
||||
UnmarshalCaddyfile(d *Dispenser) error
|
||||
}
|
||||
|
||||
// ServerType is a type that can evaluate a Caddyfile and set up a caddy config.
|
||||
type ServerType interface {
|
||||
// Setup takes the server blocks which
|
||||
// contain tokens, as well as options
|
||||
// (e.g. CLI flags) and creates a Caddy
|
||||
// config, along with any warnings or
|
||||
// an error.
|
||||
Setup([]ServerBlock, map[string]interface{}) (*caddy.Config, []caddyconfig.Warning, error)
|
||||
}
|
||||
|
||||
// Interface guard
|
||||
var _ caddyconfig.Adapter = (*Adapter)(nil)
|
||||
@@ -1,384 +0,0 @@
|
||||
// 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 caddyfile
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Dispenser is a type that dispenses tokens, similarly to a lexer,
|
||||
// except that it can do so with some notion of structure. An empty
|
||||
// Dispenser is invalid; call NewDispenser to make a proper instance.
|
||||
type Dispenser struct {
|
||||
tokens []Token
|
||||
cursor int
|
||||
nesting int
|
||||
}
|
||||
|
||||
// NewDispenser returns a Dispenser filled with the given tokens.
|
||||
func NewDispenser(tokens []Token) *Dispenser {
|
||||
return &Dispenser{
|
||||
tokens: tokens,
|
||||
cursor: -1,
|
||||
}
|
||||
}
|
||||
|
||||
// Next loads the next token. Returns true if a token
|
||||
// was loaded; false otherwise. If false, all tokens
|
||||
// have been consumed.
|
||||
func (d *Dispenser) Next() bool {
|
||||
if d.cursor < len(d.tokens)-1 {
|
||||
d.cursor++
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Prev moves to the previous token. It does the inverse
|
||||
// of Next(), except this function may decrement the cursor
|
||||
// to -1 so that the next call to Next() points to the
|
||||
// first token; this allows dispensing to "start over". This
|
||||
// method returns true if the cursor ends up pointing to a
|
||||
// valid token.
|
||||
func (d *Dispenser) Prev() bool {
|
||||
if d.cursor > -1 {
|
||||
d.cursor--
|
||||
return d.cursor > -1
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// NextArg loads the next token if it is on the same
|
||||
// line and if it is not a block opening (open curly
|
||||
// brace). Returns true if an argument token was
|
||||
// loaded; false otherwise. If false, all tokens on
|
||||
// the line have been consumed except for potentially
|
||||
// a block opening. It handles imported tokens
|
||||
// correctly.
|
||||
func (d *Dispenser) NextArg() bool {
|
||||
if !d.nextOnSameLine() {
|
||||
return false
|
||||
}
|
||||
if d.Val() == "{" {
|
||||
// roll back; a block opening is not an argument
|
||||
d.cursor--
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// nextOnSameLine advances the cursor if the next
|
||||
// token is on the same line of the same file.
|
||||
func (d *Dispenser) nextOnSameLine() bool {
|
||||
if d.cursor < 0 {
|
||||
d.cursor++
|
||||
return true
|
||||
}
|
||||
if d.cursor >= len(d.tokens) {
|
||||
return false
|
||||
}
|
||||
if d.cursor < len(d.tokens)-1 &&
|
||||
d.tokens[d.cursor].File == d.tokens[d.cursor+1].File &&
|
||||
d.tokens[d.cursor].Line+d.numLineBreaks(d.cursor) == d.tokens[d.cursor+1].Line {
|
||||
d.cursor++
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// NextLine loads the next token only if it is not on the same
|
||||
// line as the current token, and returns true if a token was
|
||||
// loaded; false otherwise. If false, there is not another token
|
||||
// or it is on the same line. It handles imported tokens correctly.
|
||||
func (d *Dispenser) NextLine() bool {
|
||||
if d.cursor < 0 {
|
||||
d.cursor++
|
||||
return true
|
||||
}
|
||||
if d.cursor >= len(d.tokens) {
|
||||
return false
|
||||
}
|
||||
if d.cursor < len(d.tokens)-1 &&
|
||||
(d.tokens[d.cursor].File != d.tokens[d.cursor+1].File ||
|
||||
d.tokens[d.cursor].Line+d.numLineBreaks(d.cursor) < d.tokens[d.cursor+1].Line) {
|
||||
d.cursor++
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// NextBlock can be used as the condition of a for loop
|
||||
// to load the next token as long as it opens a block or
|
||||
// is already in a block nested more than initialNestingLevel.
|
||||
// In other words, a loop over NextBlock() will iterate
|
||||
// all tokens in the block assuming the next token is an
|
||||
// open curly brace, until the matching closing brace.
|
||||
// The open and closing brace tokens for the outer-most
|
||||
// block will be consumed internally and omitted from
|
||||
// the iteration.
|
||||
//
|
||||
// Proper use of this method looks like this:
|
||||
//
|
||||
// for nesting := d.Nesting(); d.NextBlock(nesting); {
|
||||
// }
|
||||
//
|
||||
// However, in simple cases where it is known that the
|
||||
// Dispenser is new and has not already traversed state
|
||||
// by a loop over NextBlock(), this will do:
|
||||
//
|
||||
// for d.NextBlock(0) {
|
||||
// }
|
||||
//
|
||||
// As with other token parsing logic, a loop over
|
||||
// NextBlock() should be contained within a loop over
|
||||
// Next(), as it is usually prudent to skip the initial
|
||||
// token.
|
||||
func (d *Dispenser) NextBlock(initialNestingLevel int) bool {
|
||||
if d.nesting > initialNestingLevel {
|
||||
if !d.Next() {
|
||||
return false // should be EOF error
|
||||
}
|
||||
if d.Val() == "}" && !d.nextOnSameLine() {
|
||||
d.nesting--
|
||||
} else if d.Val() == "{" && !d.nextOnSameLine() {
|
||||
d.nesting++
|
||||
}
|
||||
return d.nesting > initialNestingLevel
|
||||
}
|
||||
if !d.nextOnSameLine() { // block must open on same line
|
||||
return false
|
||||
}
|
||||
if d.Val() != "{" {
|
||||
d.cursor-- // roll back if not opening brace
|
||||
return false
|
||||
}
|
||||
d.Next() // consume open curly brace
|
||||
if d.Val() == "}" {
|
||||
return false // open and then closed right away
|
||||
}
|
||||
d.nesting++
|
||||
return true
|
||||
}
|
||||
|
||||
// Nesting returns the current nesting level. Necessary
|
||||
// if using NextBlock()
|
||||
func (d *Dispenser) Nesting() int {
|
||||
return d.nesting
|
||||
}
|
||||
|
||||
// Val gets the text of the current token. If there is no token
|
||||
// loaded, it returns empty string.
|
||||
func (d *Dispenser) Val() string {
|
||||
if d.cursor < 0 || d.cursor >= len(d.tokens) {
|
||||
return ""
|
||||
}
|
||||
return d.tokens[d.cursor].Text
|
||||
}
|
||||
|
||||
// Line gets the line number of the current token.
|
||||
// If there is no token loaded, it returns 0.
|
||||
func (d *Dispenser) Line() int {
|
||||
if d.cursor < 0 || d.cursor >= len(d.tokens) {
|
||||
return 0
|
||||
}
|
||||
return d.tokens[d.cursor].Line
|
||||
}
|
||||
|
||||
// File gets the filename where the current token originated.
|
||||
func (d *Dispenser) File() string {
|
||||
if d.cursor < 0 || d.cursor >= len(d.tokens) {
|
||||
return ""
|
||||
}
|
||||
return d.tokens[d.cursor].File
|
||||
}
|
||||
|
||||
// Args is a convenience function that loads the next arguments
|
||||
// (tokens on the same line) into an arbitrary number of strings
|
||||
// pointed to in targets. If there are not enough argument tokens
|
||||
// available to fill targets, false is returned and the remaining
|
||||
// targets are left unchanged. If all the targets are filled,
|
||||
// then true is returned.
|
||||
func (d *Dispenser) Args(targets ...*string) bool {
|
||||
for i := 0; i < len(targets); i++ {
|
||||
if !d.NextArg() {
|
||||
return false
|
||||
}
|
||||
*targets[i] = d.Val()
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// AllArgs is like Args, but if there are more argument tokens
|
||||
// available than there are targets, false is returned. The
|
||||
// number of available argument tokens must match the number of
|
||||
// targets exactly to return true.
|
||||
func (d *Dispenser) AllArgs(targets ...*string) bool {
|
||||
if !d.Args(targets...) {
|
||||
return false
|
||||
}
|
||||
if d.NextArg() {
|
||||
d.Prev()
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// RemainingArgs loads any more arguments (tokens on the same line)
|
||||
// into a slice and returns them. Open curly brace tokens also indicate
|
||||
// the end of arguments, and the curly brace is not included in
|
||||
// the return value nor is it loaded.
|
||||
func (d *Dispenser) RemainingArgs() []string {
|
||||
var args []string
|
||||
for d.NextArg() {
|
||||
args = append(args, d.Val())
|
||||
}
|
||||
return args
|
||||
}
|
||||
|
||||
// NewFromNextSegment returns a new dispenser with a copy of
|
||||
// the tokens from the current token until the end of the
|
||||
// "directive" whether that be to the end of the line or
|
||||
// the end of a block that starts at the end of the line;
|
||||
// in other words, until the end of the segment.
|
||||
func (d *Dispenser) NewFromNextSegment() *Dispenser {
|
||||
return NewDispenser(d.NextSegment())
|
||||
}
|
||||
|
||||
// NextSegment returns a copy of the tokens from the current
|
||||
// token until the end of the line or block that starts at
|
||||
// the end of the line.
|
||||
func (d *Dispenser) NextSegment() Segment {
|
||||
tkns := Segment{d.Token()}
|
||||
for d.NextArg() {
|
||||
tkns = append(tkns, d.Token())
|
||||
}
|
||||
var openedBlock bool
|
||||
for nesting := d.Nesting(); d.NextBlock(nesting); {
|
||||
if !openedBlock {
|
||||
// because NextBlock() consumes the initial open
|
||||
// curly brace, we rewind here to append it, since
|
||||
// our case is special in that we want the new
|
||||
// dispenser to have all the tokens including
|
||||
// surrounding curly braces
|
||||
d.Prev()
|
||||
tkns = append(tkns, d.Token())
|
||||
d.Next()
|
||||
openedBlock = true
|
||||
}
|
||||
tkns = append(tkns, d.Token())
|
||||
}
|
||||
if openedBlock {
|
||||
// include closing brace
|
||||
tkns = append(tkns, d.Token())
|
||||
|
||||
// do not consume the closing curly brace; the
|
||||
// next iteration of the enclosing loop will
|
||||
// call Next() and consume it
|
||||
}
|
||||
return tkns
|
||||
}
|
||||
|
||||
// Token returns the current token.
|
||||
func (d *Dispenser) Token() Token {
|
||||
if d.cursor < 0 || d.cursor >= len(d.tokens) {
|
||||
return Token{}
|
||||
}
|
||||
return d.tokens[d.cursor]
|
||||
}
|
||||
|
||||
// Reset sets d's cursor to the beginning, as
|
||||
// if this was a new and unused dispenser.
|
||||
func (d *Dispenser) Reset() {
|
||||
d.cursor = -1
|
||||
d.nesting = 0
|
||||
}
|
||||
|
||||
// ArgErr returns an argument error, meaning that another
|
||||
// argument was expected but not found. In other words,
|
||||
// a line break or open curly brace was encountered instead of
|
||||
// an argument.
|
||||
func (d *Dispenser) ArgErr() error {
|
||||
if d.Val() == "{" {
|
||||
return d.Err("Unexpected token '{', expecting argument")
|
||||
}
|
||||
return d.Errf("Wrong argument count or unexpected line ending after '%s'", d.Val())
|
||||
}
|
||||
|
||||
// SyntaxErr creates a generic syntax error which explains what was
|
||||
// found and what was expected.
|
||||
func (d *Dispenser) SyntaxErr(expected string) error {
|
||||
msg := fmt.Sprintf("%s:%d - Syntax error: Unexpected token '%s', expecting '%s'", d.File(), d.Line(), d.Val(), expected)
|
||||
return errors.New(msg)
|
||||
}
|
||||
|
||||
// EOFErr returns an error indicating that the dispenser reached
|
||||
// the end of the input when searching for the next token.
|
||||
func (d *Dispenser) EOFErr() error {
|
||||
return d.Errf("Unexpected EOF")
|
||||
}
|
||||
|
||||
// Err generates a custom parse-time error with a message of msg.
|
||||
func (d *Dispenser) Err(msg string) error {
|
||||
msg = fmt.Sprintf("%s:%d - Error during parsing: %s", d.File(), d.Line(), msg)
|
||||
return errors.New(msg)
|
||||
}
|
||||
|
||||
// Errf is like Err, but for formatted error messages
|
||||
func (d *Dispenser) Errf(format string, args ...interface{}) error {
|
||||
return d.Err(fmt.Sprintf(format, args...))
|
||||
}
|
||||
|
||||
// Delete deletes the current token and returns the updated slice
|
||||
// of tokens. The cursor is not advanced to the next token.
|
||||
// Because deletion modifies the underlying slice, this method
|
||||
// should only be called if you have access to the original slice
|
||||
// of tokens and/or are using the slice of tokens outside this
|
||||
// Dispenser instance. If you do not re-assign the slice with the
|
||||
// return value of this method, inconsistencies in the token
|
||||
// array will become apparent (or worse, hide from you like they
|
||||
// did me for 3 and a half freaking hours late one night).
|
||||
func (d *Dispenser) Delete() []Token {
|
||||
if d.cursor >= 0 && d.cursor <= len(d.tokens)-1 {
|
||||
d.tokens = append(d.tokens[:d.cursor], d.tokens[d.cursor+1:]...)
|
||||
d.cursor--
|
||||
}
|
||||
return d.tokens
|
||||
}
|
||||
|
||||
// numLineBreaks counts how many line breaks are in the token
|
||||
// value given by the token index tknIdx. It returns 0 if the
|
||||
// token does not exist or there are no line breaks.
|
||||
func (d *Dispenser) numLineBreaks(tknIdx int) int {
|
||||
if tknIdx < 0 || tknIdx >= len(d.tokens) {
|
||||
return 0
|
||||
}
|
||||
return strings.Count(d.tokens[tknIdx].Text, "\n")
|
||||
}
|
||||
|
||||
// isNewLine determines whether the current token is on a different
|
||||
// line (higher line number) than the previous token. It handles imported
|
||||
// tokens correctly. If there isn't a previous token, it returns true.
|
||||
func (d *Dispenser) isNewLine() bool {
|
||||
if d.cursor < 1 {
|
||||
return true
|
||||
}
|
||||
if d.cursor > len(d.tokens)-1 {
|
||||
return false
|
||||
}
|
||||
return d.tokens[d.cursor-1].File != d.tokens[d.cursor].File ||
|
||||
d.tokens[d.cursor-1].Line+d.numLineBreaks(d.cursor-1) < d.tokens[d.cursor].Line
|
||||
}
|
||||
@@ -1,316 +0,0 @@
|
||||
// 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 caddyfile
|
||||
|
||||
import (
|
||||
"io"
|
||||
"log"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDispenser_Val_Next(t *testing.T) {
|
||||
input := `host:port
|
||||
dir1 arg1
|
||||
dir2 arg2 arg3
|
||||
dir3`
|
||||
d := newTestDispenser(input)
|
||||
|
||||
if val := d.Val(); val != "" {
|
||||
t.Fatalf("Val(): Should return empty string when no token loaded; got '%s'", val)
|
||||
}
|
||||
|
||||
assertNext := func(shouldLoad bool, expectedCursor int, expectedVal string) {
|
||||
if loaded := d.Next(); loaded != shouldLoad {
|
||||
t.Errorf("Next(): Expected %v but got %v instead (val '%s')", shouldLoad, loaded, d.Val())
|
||||
}
|
||||
if d.cursor != expectedCursor {
|
||||
t.Errorf("Expected cursor to be %d, but was %d", expectedCursor, d.cursor)
|
||||
}
|
||||
if d.nesting != 0 {
|
||||
t.Errorf("Nesting should be 0, was %d instead", d.nesting)
|
||||
}
|
||||
if val := d.Val(); val != expectedVal {
|
||||
t.Errorf("Val(): Expected '%s' but got '%s'", expectedVal, val)
|
||||
}
|
||||
}
|
||||
|
||||
assertNext(true, 0, "host:port")
|
||||
assertNext(true, 1, "dir1")
|
||||
assertNext(true, 2, "arg1")
|
||||
assertNext(true, 3, "dir2")
|
||||
assertNext(true, 4, "arg2")
|
||||
assertNext(true, 5, "arg3")
|
||||
assertNext(true, 6, "dir3")
|
||||
// Note: This next test simply asserts existing behavior.
|
||||
// If desired, we may wish to empty the token value after
|
||||
// reading past the EOF. Open an issue if you want this change.
|
||||
assertNext(false, 6, "dir3")
|
||||
}
|
||||
|
||||
func TestDispenser_NextArg(t *testing.T) {
|
||||
input := `dir1 arg1
|
||||
dir2 arg2 arg3
|
||||
dir3`
|
||||
d := newTestDispenser(input)
|
||||
|
||||
assertNext := func(shouldLoad bool, expectedVal string, expectedCursor int) {
|
||||
if d.Next() != shouldLoad {
|
||||
t.Errorf("Next(): Should load token but got false instead (val: '%s')", d.Val())
|
||||
}
|
||||
if d.cursor != expectedCursor {
|
||||
t.Errorf("Next(): Expected cursor to be at %d, but it was %d", expectedCursor, d.cursor)
|
||||
}
|
||||
if val := d.Val(); val != expectedVal {
|
||||
t.Errorf("Val(): Expected '%s' but got '%s'", expectedVal, val)
|
||||
}
|
||||
}
|
||||
|
||||
assertNextArg := func(expectedVal string, loadAnother bool, expectedCursor int) {
|
||||
if !d.NextArg() {
|
||||
t.Error("NextArg(): Should load next argument but got false instead")
|
||||
}
|
||||
if d.cursor != expectedCursor {
|
||||
t.Errorf("NextArg(): Expected cursor to be at %d, but it was %d", expectedCursor, d.cursor)
|
||||
}
|
||||
if val := d.Val(); val != expectedVal {
|
||||
t.Errorf("Val(): Expected '%s' but got '%s'", expectedVal, val)
|
||||
}
|
||||
if !loadAnother {
|
||||
if d.NextArg() {
|
||||
t.Fatalf("NextArg(): Should NOT load another argument, but got true instead (val: '%s')", d.Val())
|
||||
}
|
||||
if d.cursor != expectedCursor {
|
||||
t.Errorf("NextArg(): Expected cursor to remain at %d, but it was %d", expectedCursor, d.cursor)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
assertNext(true, "dir1", 0)
|
||||
assertNextArg("arg1", false, 1)
|
||||
assertNext(true, "dir2", 2)
|
||||
assertNextArg("arg2", true, 3)
|
||||
assertNextArg("arg3", false, 4)
|
||||
assertNext(true, "dir3", 5)
|
||||
assertNext(false, "dir3", 5)
|
||||
}
|
||||
|
||||
func TestDispenser_NextLine(t *testing.T) {
|
||||
input := `host:port
|
||||
dir1 arg1
|
||||
dir2 arg2 arg3`
|
||||
d := newTestDispenser(input)
|
||||
|
||||
assertNextLine := func(shouldLoad bool, expectedVal string, expectedCursor int) {
|
||||
if d.NextLine() != shouldLoad {
|
||||
t.Errorf("NextLine(): Should load token but got false instead (val: '%s')", d.Val())
|
||||
}
|
||||
if d.cursor != expectedCursor {
|
||||
t.Errorf("NextLine(): Expected cursor to be %d, instead was %d", expectedCursor, d.cursor)
|
||||
}
|
||||
if val := d.Val(); val != expectedVal {
|
||||
t.Errorf("Val(): Expected '%s' but got '%s'", expectedVal, val)
|
||||
}
|
||||
}
|
||||
|
||||
assertNextLine(true, "host:port", 0)
|
||||
assertNextLine(true, "dir1", 1)
|
||||
assertNextLine(false, "dir1", 1)
|
||||
d.Next() // arg1
|
||||
assertNextLine(true, "dir2", 3)
|
||||
assertNextLine(false, "dir2", 3)
|
||||
d.Next() // arg2
|
||||
assertNextLine(false, "arg2", 4)
|
||||
d.Next() // arg3
|
||||
assertNextLine(false, "arg3", 5)
|
||||
}
|
||||
|
||||
func TestDispenser_NextBlock(t *testing.T) {
|
||||
input := `foobar1 {
|
||||
sub1 arg1
|
||||
sub2
|
||||
}
|
||||
foobar2 {
|
||||
}`
|
||||
d := newTestDispenser(input)
|
||||
|
||||
assertNextBlock := func(shouldLoad bool, expectedCursor, expectedNesting int) {
|
||||
if loaded := d.NextBlock(0); loaded != shouldLoad {
|
||||
t.Errorf("NextBlock(): Should return %v but got %v", shouldLoad, loaded)
|
||||
}
|
||||
if d.cursor != expectedCursor {
|
||||
t.Errorf("NextBlock(): Expected cursor to be %d, was %d", expectedCursor, d.cursor)
|
||||
}
|
||||
if d.nesting != expectedNesting {
|
||||
t.Errorf("NextBlock(): Nesting should be %d, not %d", expectedNesting, d.nesting)
|
||||
}
|
||||
}
|
||||
|
||||
assertNextBlock(false, -1, 0)
|
||||
d.Next() // foobar1
|
||||
assertNextBlock(true, 2, 1)
|
||||
assertNextBlock(true, 3, 1)
|
||||
assertNextBlock(true, 4, 1)
|
||||
assertNextBlock(false, 5, 0)
|
||||
d.Next() // foobar2
|
||||
assertNextBlock(false, 8, 0) // empty block is as if it didn't exist
|
||||
}
|
||||
|
||||
func TestDispenser_Args(t *testing.T) {
|
||||
var s1, s2, s3 string
|
||||
input := `dir1 arg1 arg2 arg3
|
||||
dir2 arg4 arg5
|
||||
dir3 arg6 arg7
|
||||
dir4`
|
||||
d := newTestDispenser(input)
|
||||
|
||||
d.Next() // dir1
|
||||
|
||||
// As many strings as arguments
|
||||
if all := d.Args(&s1, &s2, &s3); !all {
|
||||
t.Error("Args(): Expected true, got false")
|
||||
}
|
||||
if s1 != "arg1" {
|
||||
t.Errorf("Args(): Expected s1 to be 'arg1', got '%s'", s1)
|
||||
}
|
||||
if s2 != "arg2" {
|
||||
t.Errorf("Args(): Expected s2 to be 'arg2', got '%s'", s2)
|
||||
}
|
||||
if s3 != "arg3" {
|
||||
t.Errorf("Args(): Expected s3 to be 'arg3', got '%s'", s3)
|
||||
}
|
||||
|
||||
d.Next() // dir2
|
||||
|
||||
// More strings than arguments
|
||||
if all := d.Args(&s1, &s2, &s3); all {
|
||||
t.Error("Args(): Expected false, got true")
|
||||
}
|
||||
if s1 != "arg4" {
|
||||
t.Errorf("Args(): Expected s1 to be 'arg4', got '%s'", s1)
|
||||
}
|
||||
if s2 != "arg5" {
|
||||
t.Errorf("Args(): Expected s2 to be 'arg5', got '%s'", s2)
|
||||
}
|
||||
if s3 != "arg3" {
|
||||
t.Errorf("Args(): Expected s3 to be unchanged ('arg3'), instead got '%s'", s3)
|
||||
}
|
||||
|
||||
// (quick cursor check just for kicks and giggles)
|
||||
if d.cursor != 6 {
|
||||
t.Errorf("Cursor should be 6, but is %d", d.cursor)
|
||||
}
|
||||
|
||||
d.Next() // dir3
|
||||
|
||||
// More arguments than strings
|
||||
if all := d.Args(&s1); !all {
|
||||
t.Error("Args(): Expected true, got false")
|
||||
}
|
||||
if s1 != "arg6" {
|
||||
t.Errorf("Args(): Expected s1 to be 'arg6', got '%s'", s1)
|
||||
}
|
||||
|
||||
d.Next() // dir4
|
||||
|
||||
// No arguments or strings
|
||||
if all := d.Args(); !all {
|
||||
t.Error("Args(): Expected true, got false")
|
||||
}
|
||||
|
||||
// No arguments but at least one string
|
||||
if all := d.Args(&s1); all {
|
||||
t.Error("Args(): Expected false, got true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDispenser_RemainingArgs(t *testing.T) {
|
||||
input := `dir1 arg1 arg2 arg3
|
||||
dir2 arg4 arg5
|
||||
dir3 arg6 { arg7
|
||||
dir4`
|
||||
d := newTestDispenser(input)
|
||||
|
||||
d.Next() // dir1
|
||||
|
||||
args := d.RemainingArgs()
|
||||
if expected := []string{"arg1", "arg2", "arg3"}; !reflect.DeepEqual(args, expected) {
|
||||
t.Errorf("RemainingArgs(): Expected %v, got %v", expected, args)
|
||||
}
|
||||
|
||||
d.Next() // dir2
|
||||
|
||||
args = d.RemainingArgs()
|
||||
if expected := []string{"arg4", "arg5"}; !reflect.DeepEqual(args, expected) {
|
||||
t.Errorf("RemainingArgs(): Expected %v, got %v", expected, args)
|
||||
}
|
||||
|
||||
d.Next() // dir3
|
||||
|
||||
args = d.RemainingArgs()
|
||||
if expected := []string{"arg6"}; !reflect.DeepEqual(args, expected) {
|
||||
t.Errorf("RemainingArgs(): Expected %v, got %v", expected, args)
|
||||
}
|
||||
|
||||
d.Next() // {
|
||||
d.Next() // arg7
|
||||
d.Next() // dir4
|
||||
|
||||
args = d.RemainingArgs()
|
||||
if len(args) != 0 {
|
||||
t.Errorf("RemainingArgs(): Expected %v, got %v", []string{}, args)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDispenser_ArgErr_Err(t *testing.T) {
|
||||
input := `dir1 {
|
||||
}
|
||||
dir2 arg1 arg2`
|
||||
d := newTestDispenser(input)
|
||||
|
||||
d.cursor = 1 // {
|
||||
|
||||
if err := d.ArgErr(); err == nil || !strings.Contains(err.Error(), "{") {
|
||||
t.Errorf("ArgErr(): Expected an error message with { in it, but got '%v'", err)
|
||||
}
|
||||
|
||||
d.cursor = 5 // arg2
|
||||
|
||||
if err := d.ArgErr(); err == nil || !strings.Contains(err.Error(), "arg2") {
|
||||
t.Errorf("ArgErr(): Expected an error message with 'arg2' in it; got '%v'", err)
|
||||
}
|
||||
|
||||
err := d.Err("foobar")
|
||||
if err == nil {
|
||||
t.Fatalf("Err(): Expected an error, got nil")
|
||||
}
|
||||
|
||||
if !strings.Contains(err.Error(), "Testfile:3") {
|
||||
t.Errorf("Expected error message with filename:line in it; got '%v'", err)
|
||||
}
|
||||
|
||||
if !strings.Contains(err.Error(), "foobar") {
|
||||
t.Errorf("Expected error message with custom message in it ('foobar'); got '%v'", err)
|
||||
}
|
||||
}
|
||||
|
||||
func newTestDispenser(input string) *Dispenser {
|
||||
tokens, err := allTokens("Testfile", []byte(input))
|
||||
if err != nil && err != io.EOF {
|
||||
log.Fatalf("getting all tokens from input: %v", err)
|
||||
}
|
||||
return NewDispenser(tokens)
|
||||
}
|
||||
@@ -1,148 +0,0 @@
|
||||
// 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 caddyfile
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
// Format formats a Caddyfile to conventional standards.
|
||||
func Format(body []byte) []byte {
|
||||
reader := bytes.NewReader(body)
|
||||
result := new(bytes.Buffer)
|
||||
|
||||
var (
|
||||
commented,
|
||||
quoted,
|
||||
escaped,
|
||||
environ,
|
||||
lineBegin bool
|
||||
|
||||
firstIteration = true
|
||||
|
||||
indentation = 0
|
||||
|
||||
prev,
|
||||
curr,
|
||||
next rune
|
||||
|
||||
err error
|
||||
)
|
||||
|
||||
insertTabs := func(num int) {
|
||||
for tabs := num; tabs > 0; tabs-- {
|
||||
result.WriteRune('\t')
|
||||
}
|
||||
}
|
||||
|
||||
for {
|
||||
prev = curr
|
||||
curr = next
|
||||
|
||||
if curr < 0 {
|
||||
break
|
||||
}
|
||||
|
||||
next, _, err = reader.ReadRune()
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
next = -1
|
||||
} else {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
if firstIteration {
|
||||
firstIteration = false
|
||||
lineBegin = true
|
||||
continue
|
||||
}
|
||||
|
||||
if quoted {
|
||||
if escaped {
|
||||
escaped = false
|
||||
} else {
|
||||
if curr == '\\' {
|
||||
escaped = true
|
||||
}
|
||||
if curr == '"' {
|
||||
quoted = false
|
||||
}
|
||||
}
|
||||
if curr == '\n' {
|
||||
quoted = false
|
||||
}
|
||||
} else if commented {
|
||||
if curr == '\n' {
|
||||
commented = false
|
||||
}
|
||||
} else {
|
||||
if curr == '"' {
|
||||
quoted = true
|
||||
}
|
||||
if curr == '#' {
|
||||
commented = true
|
||||
}
|
||||
if curr == '}' {
|
||||
if environ {
|
||||
environ = false
|
||||
} else if indentation > 0 {
|
||||
indentation--
|
||||
}
|
||||
}
|
||||
if curr == '{' {
|
||||
if unicode.IsSpace(next) {
|
||||
indentation++
|
||||
|
||||
if !unicode.IsSpace(prev) && !lineBegin {
|
||||
result.WriteRune(' ')
|
||||
}
|
||||
} else {
|
||||
environ = true
|
||||
}
|
||||
}
|
||||
if lineBegin {
|
||||
if curr == ' ' || curr == '\t' {
|
||||
continue
|
||||
} else {
|
||||
lineBegin = false
|
||||
if curr == '{' && unicode.IsSpace(next) {
|
||||
// If the block is global, i.e., starts with '{'
|
||||
// One less indentation for these blocks.
|
||||
insertTabs(indentation - 1)
|
||||
} else {
|
||||
insertTabs(indentation)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if prev == '{' &&
|
||||
(curr == ' ' || curr == '\t') &&
|
||||
(next != '\n' && next != '\r') {
|
||||
curr = '\n'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if curr == '\n' {
|
||||
lineBegin = true
|
||||
}
|
||||
|
||||
result.WriteRune(curr)
|
||||
}
|
||||
|
||||
return result.Bytes()
|
||||
}
|
||||
@@ -1,228 +0,0 @@
|
||||
// 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 caddyfile
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFormatBasicIndentation(t *testing.T) {
|
||||
input := []byte(`
|
||||
a
|
||||
b
|
||||
|
||||
c {
|
||||
d
|
||||
}
|
||||
|
||||
e { f
|
||||
}
|
||||
|
||||
g {
|
||||
h {
|
||||
i
|
||||
}
|
||||
}
|
||||
|
||||
j { k {
|
||||
l
|
||||
}
|
||||
}
|
||||
|
||||
m {
|
||||
n { o
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
p
|
||||
}
|
||||
|
||||
{ q
|
||||
}
|
||||
|
||||
{
|
||||
{ r
|
||||
}
|
||||
}
|
||||
`)
|
||||
expected := []byte(`
|
||||
a
|
||||
b
|
||||
|
||||
c {
|
||||
d
|
||||
}
|
||||
|
||||
e {
|
||||
f
|
||||
}
|
||||
|
||||
g {
|
||||
h {
|
||||
i
|
||||
}
|
||||
}
|
||||
|
||||
j {
|
||||
k {
|
||||
l
|
||||
}
|
||||
}
|
||||
|
||||
m {
|
||||
n {
|
||||
o
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
p
|
||||
}
|
||||
|
||||
{
|
||||
q
|
||||
}
|
||||
|
||||
{
|
||||
{
|
||||
r
|
||||
}
|
||||
}
|
||||
`)
|
||||
testFormat(t, input, expected)
|
||||
}
|
||||
|
||||
func TestFormatBasicSpacing(t *testing.T) {
|
||||
input := []byte(`
|
||||
a{
|
||||
b
|
||||
}
|
||||
|
||||
c{ d
|
||||
}
|
||||
`)
|
||||
expected := []byte(`
|
||||
a {
|
||||
b
|
||||
}
|
||||
|
||||
c {
|
||||
d
|
||||
}
|
||||
`)
|
||||
testFormat(t, input, expected)
|
||||
}
|
||||
|
||||
func TestFormatEnvironmentVariable(t *testing.T) {
|
||||
input := []byte(`
|
||||
{$A}
|
||||
|
||||
b {
|
||||
{$C}
|
||||
}
|
||||
|
||||
d { {$E}
|
||||
}
|
||||
|
||||
{ {$F}
|
||||
}
|
||||
`)
|
||||
expected := []byte(`
|
||||
{$A}
|
||||
|
||||
b {
|
||||
{$C}
|
||||
}
|
||||
|
||||
d {
|
||||
{$E}
|
||||
}
|
||||
|
||||
{
|
||||
{$F}
|
||||
}
|
||||
`)
|
||||
testFormat(t, input, expected)
|
||||
}
|
||||
|
||||
func TestFormatComments(t *testing.T) {
|
||||
input := []byte(`
|
||||
# a "\n"
|
||||
|
||||
# b {
|
||||
c
|
||||
}
|
||||
|
||||
d {
|
||||
e # f
|
||||
# g
|
||||
}
|
||||
|
||||
h { # i
|
||||
}
|
||||
`)
|
||||
expected := []byte(`
|
||||
# a "\n"
|
||||
|
||||
# b {
|
||||
c
|
||||
}
|
||||
|
||||
d {
|
||||
e # f
|
||||
# g
|
||||
}
|
||||
|
||||
h {
|
||||
# i
|
||||
}
|
||||
`)
|
||||
testFormat(t, input, expected)
|
||||
}
|
||||
|
||||
func TestFormatQuotesAndEscapes(t *testing.T) {
|
||||
input := []byte(`
|
||||
"a \"b\" #c
|
||||
d
|
||||
|
||||
e {
|
||||
"f"
|
||||
}
|
||||
|
||||
g { "h"
|
||||
}
|
||||
`)
|
||||
expected := []byte(`
|
||||
"a \"b\" #c
|
||||
d
|
||||
|
||||
e {
|
||||
"f"
|
||||
}
|
||||
|
||||
g {
|
||||
"h"
|
||||
}
|
||||
`)
|
||||
testFormat(t, input, expected)
|
||||
}
|
||||
|
||||
func testFormat(t *testing.T, input, expected []byte) {
|
||||
output := Format(input)
|
||||
if string(output) != string(expected) {
|
||||
t.Errorf("Expected:\n%s\ngot:\n%s", string(expected), string(output))
|
||||
}
|
||||
}
|
||||
@@ -1,163 +0,0 @@
|
||||
// Copyright 2015 Light Code Labs, LLC
|
||||
//
|
||||
// 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 caddyfile
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"io"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
type (
|
||||
// lexer is a utility which can get values, token by
|
||||
// token, from a Reader. A token is a word, and tokens
|
||||
// are separated by whitespace. A word can be enclosed
|
||||
// in quotes if it contains whitespace.
|
||||
lexer struct {
|
||||
reader *bufio.Reader
|
||||
token Token
|
||||
line int
|
||||
skippedLines int
|
||||
}
|
||||
|
||||
// Token represents a single parsable unit.
|
||||
Token struct {
|
||||
File string
|
||||
Line int
|
||||
Text string
|
||||
}
|
||||
)
|
||||
|
||||
// load prepares the lexer to scan an input for tokens.
|
||||
// It discards any leading byte order mark.
|
||||
func (l *lexer) load(input io.Reader) error {
|
||||
l.reader = bufio.NewReader(input)
|
||||
l.line = 1
|
||||
|
||||
// discard byte order mark, if present
|
||||
firstCh, _, err := l.reader.ReadRune()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if firstCh != 0xFEFF {
|
||||
err := l.reader.UnreadRune()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// next loads the next token into the lexer.
|
||||
// A token is delimited by whitespace, unless
|
||||
// the token starts with a quotes character (")
|
||||
// in which case the token goes until the closing
|
||||
// quotes (the enclosing quotes are not included).
|
||||
// Inside quoted strings, quotes may be escaped
|
||||
// with a preceding \ character. No other chars
|
||||
// may be escaped. The rest of the line is skipped
|
||||
// if a "#" character is read in. Returns true if
|
||||
// a token was loaded; false otherwise.
|
||||
func (l *lexer) next() bool {
|
||||
var val []rune
|
||||
var comment, quoted, escaped bool
|
||||
|
||||
makeToken := func() bool {
|
||||
l.token.Text = string(val)
|
||||
return true
|
||||
}
|
||||
|
||||
for {
|
||||
ch, _, err := l.reader.ReadRune()
|
||||
if err != nil {
|
||||
if len(val) > 0 {
|
||||
return makeToken()
|
||||
}
|
||||
if err == io.EOF {
|
||||
return false
|
||||
}
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if !escaped && ch == '\\' {
|
||||
escaped = true
|
||||
continue
|
||||
}
|
||||
|
||||
if quoted {
|
||||
if escaped {
|
||||
// all is literal in quoted area,
|
||||
// so only escape quotes
|
||||
if ch != '"' {
|
||||
val = append(val, '\\')
|
||||
}
|
||||
escaped = false
|
||||
} else {
|
||||
if ch == '"' {
|
||||
return makeToken()
|
||||
}
|
||||
}
|
||||
if ch == '\n' {
|
||||
l.line += 1 + l.skippedLines
|
||||
l.skippedLines = 0
|
||||
}
|
||||
val = append(val, ch)
|
||||
continue
|
||||
}
|
||||
|
||||
if unicode.IsSpace(ch) {
|
||||
if ch == '\r' {
|
||||
continue
|
||||
}
|
||||
if ch == '\n' {
|
||||
if escaped {
|
||||
l.skippedLines++
|
||||
escaped = false
|
||||
} else {
|
||||
l.line += 1 + l.skippedLines
|
||||
l.skippedLines = 0
|
||||
}
|
||||
comment = false
|
||||
}
|
||||
if len(val) > 0 {
|
||||
return makeToken()
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
if ch == '#' {
|
||||
comment = true
|
||||
}
|
||||
if comment {
|
||||
continue
|
||||
}
|
||||
|
||||
if len(val) == 0 {
|
||||
l.token = Token{Line: l.line}
|
||||
if ch == '"' {
|
||||
quoted = true
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
if escaped {
|
||||
val = append(val, '\\')
|
||||
escaped = false
|
||||
}
|
||||
|
||||
val = append(val, ch)
|
||||
}
|
||||
}
|
||||
@@ -1,238 +0,0 @@
|
||||
// Copyright 2015 Light Code Labs, LLC
|
||||
//
|
||||
// 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 caddyfile
|
||||
|
||||
import (
|
||||
"log"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type lexerTestCase struct {
|
||||
input string
|
||||
expected []Token
|
||||
}
|
||||
|
||||
func TestLexer(t *testing.T) {
|
||||
testCases := []lexerTestCase{
|
||||
{
|
||||
input: `host:123`,
|
||||
expected: []Token{
|
||||
{Line: 1, Text: "host:123"},
|
||||
},
|
||||
},
|
||||
{
|
||||
input: `host:123
|
||||
|
||||
directive`,
|
||||
expected: []Token{
|
||||
{Line: 1, Text: "host:123"},
|
||||
{Line: 3, Text: "directive"},
|
||||
},
|
||||
},
|
||||
{
|
||||
input: `host:123 {
|
||||
directive
|
||||
}`,
|
||||
expected: []Token{
|
||||
{Line: 1, Text: "host:123"},
|
||||
{Line: 1, Text: "{"},
|
||||
{Line: 2, Text: "directive"},
|
||||
{Line: 3, Text: "}"},
|
||||
},
|
||||
},
|
||||
{
|
||||
input: `host:123 { directive }`,
|
||||
expected: []Token{
|
||||
{Line: 1, Text: "host:123"},
|
||||
{Line: 1, Text: "{"},
|
||||
{Line: 1, Text: "directive"},
|
||||
{Line: 1, Text: "}"},
|
||||
},
|
||||
},
|
||||
{
|
||||
input: `host:123 {
|
||||
#comment
|
||||
directive
|
||||
# comment
|
||||
foobar # another comment
|
||||
}`,
|
||||
expected: []Token{
|
||||
{Line: 1, Text: "host:123"},
|
||||
{Line: 1, Text: "{"},
|
||||
{Line: 3, Text: "directive"},
|
||||
{Line: 5, Text: "foobar"},
|
||||
{Line: 6, Text: "}"},
|
||||
},
|
||||
},
|
||||
{
|
||||
input: `a "quoted value" b
|
||||
foobar`,
|
||||
expected: []Token{
|
||||
{Line: 1, Text: "a"},
|
||||
{Line: 1, Text: "quoted value"},
|
||||
{Line: 1, Text: "b"},
|
||||
{Line: 2, Text: "foobar"},
|
||||
},
|
||||
},
|
||||
{
|
||||
input: `A "quoted \"value\" inside" B`,
|
||||
expected: []Token{
|
||||
{Line: 1, Text: "A"},
|
||||
{Line: 1, Text: `quoted "value" inside`},
|
||||
{Line: 1, Text: "B"},
|
||||
},
|
||||
},
|
||||
{
|
||||
input: "An escaped \"newline\\\ninside\" quotes",
|
||||
expected: []Token{
|
||||
{Line: 1, Text: "An"},
|
||||
{Line: 1, Text: "escaped"},
|
||||
{Line: 1, Text: "newline\\\ninside"},
|
||||
{Line: 2, Text: "quotes"},
|
||||
},
|
||||
},
|
||||
{
|
||||
input: "An escaped newline\\\noutside quotes",
|
||||
expected: []Token{
|
||||
{Line: 1, Text: "An"},
|
||||
{Line: 1, Text: "escaped"},
|
||||
{Line: 1, Text: "newline"},
|
||||
{Line: 1, Text: "outside"},
|
||||
{Line: 1, Text: "quotes"},
|
||||
},
|
||||
},
|
||||
{
|
||||
input: "line1\\\nescaped\nline2\nline3",
|
||||
expected: []Token{
|
||||
{Line: 1, Text: "line1"},
|
||||
{Line: 1, Text: "escaped"},
|
||||
{Line: 3, Text: "line2"},
|
||||
{Line: 4, Text: "line3"},
|
||||
},
|
||||
},
|
||||
{
|
||||
input: "line1\\\nescaped1\\\nescaped2\nline4\nline5",
|
||||
expected: []Token{
|
||||
{Line: 1, Text: "line1"},
|
||||
{Line: 1, Text: "escaped1"},
|
||||
{Line: 1, Text: "escaped2"},
|
||||
{Line: 4, Text: "line4"},
|
||||
{Line: 5, Text: "line5"},
|
||||
},
|
||||
},
|
||||
{
|
||||
input: `"unescapable\ in quotes"`,
|
||||
expected: []Token{
|
||||
{Line: 1, Text: `unescapable\ in quotes`},
|
||||
},
|
||||
},
|
||||
{
|
||||
input: `"don't\escape"`,
|
||||
expected: []Token{
|
||||
{Line: 1, Text: `don't\escape`},
|
||||
},
|
||||
},
|
||||
{
|
||||
input: `"don't\\escape"`,
|
||||
expected: []Token{
|
||||
{Line: 1, Text: `don't\\escape`},
|
||||
},
|
||||
},
|
||||
{
|
||||
input: `un\escapable`,
|
||||
expected: []Token{
|
||||
{Line: 1, Text: `un\escapable`},
|
||||
},
|
||||
},
|
||||
{
|
||||
input: `A "quoted value with line
|
||||
break inside" {
|
||||
foobar
|
||||
}`,
|
||||
expected: []Token{
|
||||
{Line: 1, Text: "A"},
|
||||
{Line: 1, Text: "quoted value with line\n\t\t\t\t\tbreak inside"},
|
||||
{Line: 2, Text: "{"},
|
||||
{Line: 3, Text: "foobar"},
|
||||
{Line: 4, Text: "}"},
|
||||
},
|
||||
},
|
||||
{
|
||||
input: `"C:\php\php-cgi.exe"`,
|
||||
expected: []Token{
|
||||
{Line: 1, Text: `C:\php\php-cgi.exe`},
|
||||
},
|
||||
},
|
||||
{
|
||||
input: `empty "" string`,
|
||||
expected: []Token{
|
||||
{Line: 1, Text: `empty`},
|
||||
{Line: 1, Text: ``},
|
||||
{Line: 1, Text: `string`},
|
||||
},
|
||||
},
|
||||
{
|
||||
input: "skip those\r\nCR characters",
|
||||
expected: []Token{
|
||||
{Line: 1, Text: "skip"},
|
||||
{Line: 1, Text: "those"},
|
||||
{Line: 2, Text: "CR"},
|
||||
{Line: 2, Text: "characters"},
|
||||
},
|
||||
},
|
||||
{
|
||||
input: "\xEF\xBB\xBF:8080", // test with leading byte order mark
|
||||
expected: []Token{
|
||||
{Line: 1, Text: ":8080"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for i, testCase := range testCases {
|
||||
actual := tokenize(testCase.input)
|
||||
lexerCompare(t, i, testCase.expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func tokenize(input string) (tokens []Token) {
|
||||
l := lexer{}
|
||||
if err := l.load(strings.NewReader(input)); err != nil {
|
||||
log.Printf("[ERROR] load failed: %v", err)
|
||||
}
|
||||
for l.next() {
|
||||
tokens = append(tokens, l.token)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func lexerCompare(t *testing.T, n int, expected, actual []Token) {
|
||||
if len(expected) != len(actual) {
|
||||
t.Errorf("Test case %d: expected %d token(s) but got %d", n, len(expected), len(actual))
|
||||
}
|
||||
|
||||
for i := 0; i < len(actual) && i < len(expected); i++ {
|
||||
if actual[i].Line != expected[i].Line {
|
||||
t.Errorf("Test case %d token %d ('%s'): expected line %d but was line %d",
|
||||
n, i, expected[i].Text, expected[i].Line, actual[i].Line)
|
||||
break
|
||||
}
|
||||
if actual[i].Text != expected[i].Text {
|
||||
t.Errorf("Test case %d token %d: expected text '%s' but was '%s'",
|
||||
n, i, expected[i].Text, actual[i].Text)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,536 +0,0 @@
|
||||
// Copyright 2015 Light Code Labs, LLC
|
||||
//
|
||||
// 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 caddyfile
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Parse parses the input just enough to group tokens, in
|
||||
// order, by server block. No further parsing is performed.
|
||||
// Server blocks are returned in the order in which they appear.
|
||||
// Directives that do not appear in validDirectives will cause
|
||||
// an error. If you do not want to check for valid directives,
|
||||
// pass in nil instead.
|
||||
//
|
||||
// Environment variables in {$ENVIRONMENT_VARIABLE} notation
|
||||
// will be replaced before parsing begins.
|
||||
func Parse(filename string, input []byte) ([]ServerBlock, error) {
|
||||
tokens, err := allTokens(filename, input)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
p := parser{Dispenser: NewDispenser(tokens)}
|
||||
return p.parseAll()
|
||||
}
|
||||
|
||||
// replaceEnvVars replaces all occurrences of environment variables.
|
||||
func replaceEnvVars(input []byte) ([]byte, error) {
|
||||
var offset int
|
||||
for {
|
||||
begin := bytes.Index(input[offset:], spanOpen)
|
||||
if begin < 0 {
|
||||
break
|
||||
}
|
||||
begin += offset // make beginning relative to input, not offset
|
||||
end := bytes.Index(input[begin+len(spanOpen):], spanClose)
|
||||
if end < 0 {
|
||||
break
|
||||
}
|
||||
end += begin + len(spanOpen) // make end relative to input, not begin
|
||||
|
||||
// get the name; if there is no name, skip it
|
||||
envVarName := input[begin+len(spanOpen) : end]
|
||||
if len(envVarName) == 0 {
|
||||
offset = end + len(spanClose)
|
||||
continue
|
||||
}
|
||||
|
||||
// get the value of the environment variable
|
||||
envVarValue := []byte(os.ExpandEnv(os.Getenv(string(envVarName))))
|
||||
|
||||
// splice in the value
|
||||
input = append(input[:begin],
|
||||
append(envVarValue, input[end+len(spanClose):]...)...)
|
||||
|
||||
// continue at the end of the replacement
|
||||
offset = begin + len(envVarValue)
|
||||
}
|
||||
return input, nil
|
||||
}
|
||||
|
||||
// allTokens lexes the entire input, but does not parse it.
|
||||
// It returns all the tokens from the input, unstructured
|
||||
// and in order.
|
||||
func allTokens(filename string, input []byte) ([]Token, error) {
|
||||
input, err := replaceEnvVars(input)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
l := new(lexer)
|
||||
err = l.load(bytes.NewReader(input))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var tokens []Token
|
||||
for l.next() {
|
||||
l.token.File = filename
|
||||
tokens = append(tokens, l.token)
|
||||
}
|
||||
return tokens, nil
|
||||
}
|
||||
|
||||
type parser struct {
|
||||
*Dispenser
|
||||
block ServerBlock // current server block being parsed
|
||||
eof bool // if we encounter a valid EOF in a hard place
|
||||
definedSnippets map[string][]Token
|
||||
nesting int
|
||||
}
|
||||
|
||||
func (p *parser) parseAll() ([]ServerBlock, error) {
|
||||
var blocks []ServerBlock
|
||||
|
||||
for p.Next() {
|
||||
err := p.parseOne()
|
||||
if err != nil {
|
||||
return blocks, err
|
||||
}
|
||||
if len(p.block.Keys) > 0 || len(p.block.Segments) > 0 {
|
||||
blocks = append(blocks, p.block)
|
||||
}
|
||||
if p.nesting > 0 {
|
||||
return blocks, p.EOFErr()
|
||||
}
|
||||
}
|
||||
|
||||
return blocks, nil
|
||||
}
|
||||
|
||||
func (p *parser) parseOne() error {
|
||||
p.block = ServerBlock{}
|
||||
return p.begin()
|
||||
}
|
||||
|
||||
func (p *parser) begin() error {
|
||||
if len(p.tokens) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
err := p.addresses()
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if p.eof {
|
||||
// this happens if the Caddyfile consists of only
|
||||
// a line of addresses and nothing else
|
||||
return nil
|
||||
}
|
||||
|
||||
if ok, name := p.isSnippet(); ok {
|
||||
if p.definedSnippets == nil {
|
||||
p.definedSnippets = map[string][]Token{}
|
||||
}
|
||||
if _, found := p.definedSnippets[name]; found {
|
||||
return p.Errf("redeclaration of previously declared snippet %s", name)
|
||||
}
|
||||
// consume all tokens til matched close brace
|
||||
tokens, err := p.snippetTokens()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
p.definedSnippets[name] = tokens
|
||||
// empty block keys so we don't save this block as a real server.
|
||||
p.block.Keys = nil
|
||||
return nil
|
||||
}
|
||||
|
||||
return p.blockContents()
|
||||
}
|
||||
|
||||
func (p *parser) addresses() error {
|
||||
var expectingAnother bool
|
||||
|
||||
for {
|
||||
tkn := p.Val()
|
||||
|
||||
// special case: import directive replaces tokens during parse-time
|
||||
if tkn == "import" && p.isNewLine() {
|
||||
err := p.doImport()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
// Open brace definitely indicates end of addresses
|
||||
if tkn == "{" {
|
||||
if expectingAnother {
|
||||
return p.Errf("Expected another address but had '%s' - check for extra comma", tkn)
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
if tkn != "" { // empty token possible if user typed ""
|
||||
// Trailing comma indicates another address will follow, which
|
||||
// may possibly be on the next line
|
||||
if tkn[len(tkn)-1] == ',' {
|
||||
tkn = tkn[:len(tkn)-1]
|
||||
expectingAnother = true
|
||||
} else {
|
||||
expectingAnother = false // but we may still see another one on this line
|
||||
}
|
||||
|
||||
p.block.Keys = append(p.block.Keys, tkn)
|
||||
}
|
||||
|
||||
// Advance token and possibly break out of loop or return error
|
||||
hasNext := p.Next()
|
||||
if expectingAnother && !hasNext {
|
||||
return p.EOFErr()
|
||||
}
|
||||
if !hasNext {
|
||||
p.eof = true
|
||||
break // EOF
|
||||
}
|
||||
if !expectingAnother && p.isNewLine() {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *parser) blockContents() error {
|
||||
errOpenCurlyBrace := p.openCurlyBrace()
|
||||
if errOpenCurlyBrace != nil {
|
||||
// single-server configs don't need curly braces
|
||||
p.cursor--
|
||||
}
|
||||
|
||||
err := p.directives()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// only look for close curly brace if there was an opening
|
||||
if errOpenCurlyBrace == nil {
|
||||
err = p.closeCurlyBrace()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// directives parses through all the lines for directives
|
||||
// and it expects the next token to be the first
|
||||
// directive. It goes until EOF or closing curly brace
|
||||
// which ends the server block.
|
||||
func (p *parser) directives() error {
|
||||
for p.Next() {
|
||||
// end of server block
|
||||
if p.Val() == "}" {
|
||||
// p.nesting has already been decremented
|
||||
break
|
||||
}
|
||||
|
||||
// special case: import directive replaces tokens during parse-time
|
||||
if p.Val() == "import" {
|
||||
err := p.doImport()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
p.cursor-- // cursor is advanced when we continue, so roll back one more
|
||||
continue
|
||||
}
|
||||
|
||||
// normal case: parse a directive as a new segment
|
||||
// (a "segment" is a line which starts with a directive
|
||||
// and which ends at the end of the line or at the end of
|
||||
// the block that is opened at the end of the line)
|
||||
if err := p.directive(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// doImport swaps out the import directive and its argument
|
||||
// (a total of 2 tokens) with the tokens in the specified file
|
||||
// or globbing pattern. When the function returns, the cursor
|
||||
// is on the token before where the import directive was. In
|
||||
// other words, call Next() to access the first token that was
|
||||
// imported.
|
||||
func (p *parser) doImport() error {
|
||||
// syntax checks
|
||||
if !p.NextArg() {
|
||||
return p.ArgErr()
|
||||
}
|
||||
importPattern := p.Val()
|
||||
if importPattern == "" {
|
||||
return p.Err("Import requires a non-empty filepath")
|
||||
}
|
||||
if p.NextArg() {
|
||||
return p.Err("Import takes only one argument (glob pattern or file)")
|
||||
}
|
||||
// splice out the import directive and its argument (2 tokens total)
|
||||
tokensBefore := p.tokens[:p.cursor-1]
|
||||
tokensAfter := p.tokens[p.cursor+1:]
|
||||
var importedTokens []Token
|
||||
|
||||
// first check snippets. That is a simple, non-recursive replacement
|
||||
if p.definedSnippets != nil && p.definedSnippets[importPattern] != nil {
|
||||
importedTokens = p.definedSnippets[importPattern]
|
||||
} else {
|
||||
// make path relative to the file of the _token_ being processed rather
|
||||
// than current working directory (issue #867) and then use glob to get
|
||||
// list of matching filenames
|
||||
absFile, err := filepath.Abs(p.Dispenser.File())
|
||||
if err != nil {
|
||||
return p.Errf("Failed to get absolute path of file: %s: %v", p.Dispenser.File(), err)
|
||||
}
|
||||
|
||||
var matches []string
|
||||
var globPattern string
|
||||
if !filepath.IsAbs(importPattern) {
|
||||
globPattern = filepath.Join(filepath.Dir(absFile), importPattern)
|
||||
} else {
|
||||
globPattern = importPattern
|
||||
}
|
||||
if strings.Count(globPattern, "*") > 1 || strings.Count(globPattern, "?") > 1 ||
|
||||
(strings.Contains(globPattern, "[") && strings.Contains(globPattern, "]")) {
|
||||
// See issue #2096 - a pattern with many glob expansions can hang for too long
|
||||
return p.Errf("Glob pattern may only contain one wildcard (*), but has others: %s", globPattern)
|
||||
}
|
||||
matches, err = filepath.Glob(globPattern)
|
||||
|
||||
if err != nil {
|
||||
return p.Errf("Failed to use import pattern %s: %v", importPattern, err)
|
||||
}
|
||||
if len(matches) == 0 {
|
||||
if strings.ContainsAny(globPattern, "*?[]") {
|
||||
log.Printf("[WARNING] No files matching import glob pattern: %s", importPattern)
|
||||
} else {
|
||||
return p.Errf("File to import not found: %s", importPattern)
|
||||
}
|
||||
}
|
||||
|
||||
// collect all the imported tokens
|
||||
|
||||
for _, importFile := range matches {
|
||||
newTokens, err := p.doSingleImport(importFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
importedTokens = append(importedTokens, newTokens...)
|
||||
}
|
||||
}
|
||||
|
||||
// splice the imported tokens in the place of the import statement
|
||||
// and rewind cursor so Next() will land on first imported token
|
||||
p.tokens = append(tokensBefore, append(importedTokens, tokensAfter...)...)
|
||||
p.cursor--
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// doSingleImport lexes the individual file at importFile and returns
|
||||
// its tokens or an error, if any.
|
||||
func (p *parser) doSingleImport(importFile string) ([]Token, error) {
|
||||
file, err := os.Open(importFile)
|
||||
if err != nil {
|
||||
return nil, p.Errf("Could not import %s: %v", importFile, err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
if info, err := file.Stat(); err != nil {
|
||||
return nil, p.Errf("Could not import %s: %v", importFile, err)
|
||||
} else if info.IsDir() {
|
||||
return nil, p.Errf("Could not import %s: is a directory", importFile)
|
||||
}
|
||||
|
||||
input, err := ioutil.ReadAll(file)
|
||||
if err != nil {
|
||||
return nil, p.Errf("Could not read imported file %s: %v", importFile, err)
|
||||
}
|
||||
|
||||
importedTokens, err := allTokens(importFile, input)
|
||||
if err != nil {
|
||||
return nil, p.Errf("Could not read tokens while importing %s: %v", importFile, err)
|
||||
}
|
||||
|
||||
// Tack the file path onto these tokens so errors show the imported file's name
|
||||
// (we use full, absolute path to avoid bugs: issue #1892)
|
||||
filename, err := filepath.Abs(importFile)
|
||||
if err != nil {
|
||||
return nil, p.Errf("Failed to get absolute path of file: %s: %v", importFile, err)
|
||||
}
|
||||
for i := 0; i < len(importedTokens); i++ {
|
||||
importedTokens[i].File = filename
|
||||
}
|
||||
|
||||
return importedTokens, nil
|
||||
}
|
||||
|
||||
// directive collects tokens until the directive's scope
|
||||
// closes (either end of line or end of curly brace block).
|
||||
// It expects the currently-loaded token to be a directive
|
||||
// (or } that ends a server block). The collected tokens
|
||||
// are loaded into the current server block for later use
|
||||
// by directive setup functions.
|
||||
func (p *parser) directive() error {
|
||||
|
||||
// a segment is a list of tokens associated with this directive
|
||||
var segment Segment
|
||||
|
||||
// the directive itself is appended as a relevant token
|
||||
segment = append(segment, p.Token())
|
||||
|
||||
for p.Next() {
|
||||
if p.Val() == "{" {
|
||||
p.nesting++
|
||||
} else if p.isNewLine() && p.nesting == 0 {
|
||||
p.cursor-- // read too far
|
||||
break
|
||||
} else if p.Val() == "}" && p.nesting > 0 {
|
||||
p.nesting--
|
||||
} else if p.Val() == "}" && p.nesting == 0 {
|
||||
return p.Err("Unexpected '}' because no matching opening brace")
|
||||
} else if p.Val() == "import" && p.isNewLine() {
|
||||
if err := p.doImport(); err != nil {
|
||||
return err
|
||||
}
|
||||
p.cursor-- // cursor is advanced when we continue, so roll back one more
|
||||
continue
|
||||
}
|
||||
|
||||
segment = append(segment, p.Token())
|
||||
}
|
||||
|
||||
p.block.Segments = append(p.block.Segments, segment)
|
||||
|
||||
if p.nesting > 0 {
|
||||
return p.EOFErr()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// openCurlyBrace expects the current token to be an
|
||||
// opening curly brace. This acts like an assertion
|
||||
// because it returns an error if the token is not
|
||||
// a opening curly brace. It does NOT advance the token.
|
||||
func (p *parser) openCurlyBrace() error {
|
||||
if p.Val() != "{" {
|
||||
return p.SyntaxErr("{")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// closeCurlyBrace expects the current token to be
|
||||
// a closing curly brace. This acts like an assertion
|
||||
// because it returns an error if the token is not
|
||||
// a closing curly brace. It does NOT advance the token.
|
||||
func (p *parser) closeCurlyBrace() error {
|
||||
if p.Val() != "}" {
|
||||
return p.SyntaxErr("}")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *parser) isSnippet() (bool, string) {
|
||||
keys := p.block.Keys
|
||||
// A snippet block is a single key with parens. Nothing else qualifies.
|
||||
if len(keys) == 1 && strings.HasPrefix(keys[0], "(") && strings.HasSuffix(keys[0], ")") {
|
||||
return true, strings.TrimSuffix(keys[0][1:], ")")
|
||||
}
|
||||
return false, ""
|
||||
}
|
||||
|
||||
// read and store everything in a block for later replay.
|
||||
func (p *parser) snippetTokens() ([]Token, error) {
|
||||
// snippet must have curlies.
|
||||
err := p.openCurlyBrace()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
nesting := 1 // count our own nesting in snippets
|
||||
tokens := []Token{}
|
||||
for p.Next() {
|
||||
if p.Val() == "}" {
|
||||
nesting--
|
||||
if nesting == 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
if p.Val() == "{" {
|
||||
nesting++
|
||||
}
|
||||
tokens = append(tokens, p.tokens[p.cursor])
|
||||
}
|
||||
// make sure we're matched up
|
||||
if nesting != 0 {
|
||||
return nil, p.SyntaxErr("}")
|
||||
}
|
||||
return tokens, nil
|
||||
}
|
||||
|
||||
// ServerBlock associates any number of keys from the
|
||||
// head of the server block with tokens, which are
|
||||
// grouped by segments.
|
||||
type ServerBlock struct {
|
||||
Keys []string
|
||||
Segments []Segment
|
||||
}
|
||||
|
||||
// DispenseDirective returns a dispenser that contains
|
||||
// all the tokens in the server block.
|
||||
func (sb ServerBlock) DispenseDirective(dir string) *Dispenser {
|
||||
var tokens []Token
|
||||
for _, seg := range sb.Segments {
|
||||
if len(seg) > 0 && seg[0].Text == dir {
|
||||
tokens = append(tokens, seg...)
|
||||
}
|
||||
}
|
||||
return NewDispenser(tokens)
|
||||
}
|
||||
|
||||
// Segment is a list of tokens which begins with a directive
|
||||
// and ends at the end of the directive (either at the end of
|
||||
// the line, or at the end of a block it opens).
|
||||
type Segment []Token
|
||||
|
||||
// Directive returns the directive name for the segment.
|
||||
// The directive name is the text of the first token.
|
||||
func (s Segment) Directive() string {
|
||||
if len(s) > 0 {
|
||||
return s[0].Text
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// spanOpen and spanClose are used to bound spans that
|
||||
// contain the name of an environment variable.
|
||||
var spanOpen, spanClose = []byte{'{', '$'}, []byte{'}'}
|
||||
@@ -1,36 +0,0 @@
|
||||
// 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.
|
||||
|
||||
// +build gofuzz
|
||||
// +build gofuzz_libfuzzer
|
||||
|
||||
package caddyfile
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
)
|
||||
|
||||
func FuzzParseCaddyfile(data []byte) (score int) {
|
||||
sb, err := Parse("Caddyfile", bytes.NewReader(data))
|
||||
if err != nil {
|
||||
// if both an error is received and some ServerBlocks,
|
||||
// then the parse was able to parse partially. Mark this
|
||||
// result as interesting to push the fuzzer further through the parser.
|
||||
if sb != nil && len(sb) > 0 {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
return 1
|
||||
}
|
||||
@@ -1,674 +0,0 @@
|
||||
// Copyright 2015 Light Code Labs, LLC
|
||||
//
|
||||
// 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 caddyfile
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAllTokens(t *testing.T) {
|
||||
input := []byte("a b c\nd e")
|
||||
expected := []string{"a", "b", "c", "d", "e"}
|
||||
tokens, err := allTokens("TestAllTokens", input)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %v", err)
|
||||
}
|
||||
if len(tokens) != len(expected) {
|
||||
t.Fatalf("Expected %d tokens, got %d", len(expected), len(tokens))
|
||||
}
|
||||
|
||||
for i, val := range expected {
|
||||
if tokens[i].Text != val {
|
||||
t.Errorf("Token %d should be '%s' but was '%s'", i, val, tokens[i].Text)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseOneAndImport(t *testing.T) {
|
||||
testParseOne := func(input string) (ServerBlock, error) {
|
||||
p := testParser(input)
|
||||
p.Next() // parseOne doesn't call Next() to start, so we must
|
||||
err := p.parseOne()
|
||||
return p.block, err
|
||||
}
|
||||
|
||||
for i, test := range []struct {
|
||||
input string
|
||||
shouldErr bool
|
||||
keys []string
|
||||
numTokens []int // number of tokens to expect in each segment
|
||||
}{
|
||||
{`localhost`, false, []string{
|
||||
"localhost",
|
||||
}, []int{}},
|
||||
|
||||
{`localhost
|
||||
dir1`, false, []string{
|
||||
"localhost",
|
||||
}, []int{1}},
|
||||
|
||||
{`localhost:1234
|
||||
dir1 foo bar`, false, []string{
|
||||
"localhost:1234",
|
||||
}, []int{3},
|
||||
},
|
||||
|
||||
{`localhost {
|
||||
dir1
|
||||
}`, false, []string{
|
||||
"localhost",
|
||||
}, []int{1}},
|
||||
|
||||
{`localhost:1234 {
|
||||
dir1 foo bar
|
||||
dir2
|
||||
}`, false, []string{
|
||||
"localhost:1234",
|
||||
}, []int{3, 1}},
|
||||
|
||||
{`http://localhost https://localhost
|
||||
dir1 foo bar`, false, []string{
|
||||
"http://localhost",
|
||||
"https://localhost",
|
||||
}, []int{3}},
|
||||
|
||||
{`http://localhost https://localhost {
|
||||
dir1 foo bar
|
||||
}`, false, []string{
|
||||
"http://localhost",
|
||||
"https://localhost",
|
||||
}, []int{3}},
|
||||
|
||||
{`http://localhost, https://localhost {
|
||||
dir1 foo bar
|
||||
}`, false, []string{
|
||||
"http://localhost",
|
||||
"https://localhost",
|
||||
}, []int{3}},
|
||||
|
||||
{`http://localhost, {
|
||||
}`, true, []string{
|
||||
"http://localhost",
|
||||
}, []int{}},
|
||||
|
||||
{`host1:80, http://host2.com
|
||||
dir1 foo bar
|
||||
dir2 baz`, false, []string{
|
||||
"host1:80",
|
||||
"http://host2.com",
|
||||
}, []int{3, 2}},
|
||||
|
||||
{`http://host1.com,
|
||||
http://host2.com,
|
||||
https://host3.com`, false, []string{
|
||||
"http://host1.com",
|
||||
"http://host2.com",
|
||||
"https://host3.com",
|
||||
}, []int{}},
|
||||
|
||||
{`http://host1.com:1234, https://host2.com
|
||||
dir1 foo {
|
||||
bar baz
|
||||
}
|
||||
dir2`, false, []string{
|
||||
"http://host1.com:1234",
|
||||
"https://host2.com",
|
||||
}, []int{6, 1}},
|
||||
|
||||
{`127.0.0.1
|
||||
dir1 {
|
||||
bar baz
|
||||
}
|
||||
dir2 {
|
||||
foo bar
|
||||
}`, false, []string{
|
||||
"127.0.0.1",
|
||||
}, []int{5, 5}},
|
||||
|
||||
{`localhost
|
||||
dir1 {
|
||||
foo`, true, []string{
|
||||
"localhost",
|
||||
}, []int{3}},
|
||||
|
||||
{`localhost
|
||||
dir1 {
|
||||
}`, false, []string{
|
||||
"localhost",
|
||||
}, []int{3}},
|
||||
|
||||
{`localhost
|
||||
dir1 {
|
||||
} }`, true, []string{
|
||||
"localhost",
|
||||
}, []int{}},
|
||||
|
||||
{`localhost
|
||||
dir1 {
|
||||
nested {
|
||||
foo
|
||||
}
|
||||
}
|
||||
dir2 foo bar`, false, []string{
|
||||
"localhost",
|
||||
}, []int{7, 3}},
|
||||
|
||||
{``, false, []string{}, []int{}},
|
||||
|
||||
{`localhost
|
||||
dir1 arg1
|
||||
import testdata/import_test1.txt`, false, []string{
|
||||
"localhost",
|
||||
}, []int{2, 3, 1}},
|
||||
|
||||
{`import testdata/import_test2.txt`, false, []string{
|
||||
"host1",
|
||||
}, []int{1, 2}},
|
||||
|
||||
{`import testdata/import_test1.txt testdata/import_test2.txt`, true, []string{}, []int{}},
|
||||
|
||||
{`import testdata/not_found.txt`, true, []string{}, []int{}},
|
||||
|
||||
{`""`, false, []string{}, []int{}},
|
||||
|
||||
{``, false, []string{}, []int{}},
|
||||
|
||||
// test cases found by fuzzing!
|
||||
{`import }{$"`, true, []string{}, []int{}},
|
||||
{`import /*/*.txt`, true, []string{}, []int{}},
|
||||
{`import /???/?*?o`, true, []string{}, []int{}},
|
||||
{`import /??`, true, []string{}, []int{}},
|
||||
{`import /[a-z]`, true, []string{}, []int{}},
|
||||
{`import {$}`, true, []string{}, []int{}},
|
||||
{`import {%}`, true, []string{}, []int{}},
|
||||
{`import {$$}`, true, []string{}, []int{}},
|
||||
{`import {%%}`, true, []string{}, []int{}},
|
||||
} {
|
||||
result, err := testParseOne(test.input)
|
||||
|
||||
if test.shouldErr && err == nil {
|
||||
t.Errorf("Test %d: Expected an error, but didn't get one", i)
|
||||
}
|
||||
if !test.shouldErr && err != nil {
|
||||
t.Errorf("Test %d: Expected no error, but got: %v", i, err)
|
||||
}
|
||||
|
||||
if len(result.Keys) != len(test.keys) {
|
||||
t.Errorf("Test %d: Expected %d keys, got %d",
|
||||
i, len(test.keys), len(result.Keys))
|
||||
continue
|
||||
}
|
||||
for j, addr := range result.Keys {
|
||||
if addr != test.keys[j] {
|
||||
t.Errorf("Test %d, key %d: Expected '%s', but was '%s'",
|
||||
i, j, test.keys[j], addr)
|
||||
}
|
||||
}
|
||||
|
||||
if len(result.Segments) != len(test.numTokens) {
|
||||
t.Errorf("Test %d: Expected %d segments, had %d",
|
||||
i, len(test.numTokens), len(result.Segments))
|
||||
continue
|
||||
}
|
||||
|
||||
for j, seg := range result.Segments {
|
||||
if len(seg) != test.numTokens[j] {
|
||||
t.Errorf("Test %d, segment %d: Expected %d tokens, counted %d",
|
||||
i, j, test.numTokens[j], len(seg))
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRecursiveImport(t *testing.T) {
|
||||
testParseOne := func(input string) (ServerBlock, error) {
|
||||
p := testParser(input)
|
||||
p.Next() // parseOne doesn't call Next() to start, so we must
|
||||
err := p.parseOne()
|
||||
return p.block, err
|
||||
}
|
||||
|
||||
isExpected := func(got ServerBlock) bool {
|
||||
if len(got.Keys) != 1 || got.Keys[0] != "localhost" {
|
||||
t.Errorf("got keys unexpected: expect localhost, got %v", got.Keys)
|
||||
return false
|
||||
}
|
||||
if len(got.Segments) != 2 {
|
||||
t.Errorf("got wrong number of segments: expect 2, got %d", len(got.Segments))
|
||||
return false
|
||||
}
|
||||
if len(got.Segments[0]) != 1 || len(got.Segments[1]) != 2 {
|
||||
t.Errorf("got unexpected tokens: %v", got.Segments)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
recursiveFile1, err := filepath.Abs("testdata/recursive_import_test1")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
recursiveFile2, err := filepath.Abs("testdata/recursive_import_test2")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// test relative recursive import
|
||||
err = ioutil.WriteFile(recursiveFile1, []byte(
|
||||
`localhost
|
||||
dir1
|
||||
import recursive_import_test2`), 0644)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.Remove(recursiveFile1)
|
||||
|
||||
err = ioutil.WriteFile(recursiveFile2, []byte("dir2 1"), 0644)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.Remove(recursiveFile2)
|
||||
|
||||
// import absolute path
|
||||
result, err := testParseOne("import " + recursiveFile1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !isExpected(result) {
|
||||
t.Error("absolute+relative import failed")
|
||||
}
|
||||
|
||||
// import relative path
|
||||
result, err = testParseOne("import testdata/recursive_import_test1")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !isExpected(result) {
|
||||
t.Error("relative+relative import failed")
|
||||
}
|
||||
|
||||
// test absolute recursive import
|
||||
err = ioutil.WriteFile(recursiveFile1, []byte(
|
||||
`localhost
|
||||
dir1
|
||||
import `+recursiveFile2), 0644)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// import absolute path
|
||||
result, err = testParseOne("import " + recursiveFile1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !isExpected(result) {
|
||||
t.Error("absolute+absolute import failed")
|
||||
}
|
||||
|
||||
// import relative path
|
||||
result, err = testParseOne("import testdata/recursive_import_test1")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !isExpected(result) {
|
||||
t.Error("relative+absolute import failed")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDirectiveImport(t *testing.T) {
|
||||
testParseOne := func(input string) (ServerBlock, error) {
|
||||
p := testParser(input)
|
||||
p.Next() // parseOne doesn't call Next() to start, so we must
|
||||
err := p.parseOne()
|
||||
return p.block, err
|
||||
}
|
||||
|
||||
isExpected := func(got ServerBlock) bool {
|
||||
if len(got.Keys) != 1 || got.Keys[0] != "localhost" {
|
||||
t.Errorf("got keys unexpected: expect localhost, got %v", got.Keys)
|
||||
return false
|
||||
}
|
||||
if len(got.Segments) != 2 {
|
||||
t.Errorf("got wrong number of segments: expect 2, got %d", len(got.Segments))
|
||||
return false
|
||||
}
|
||||
if len(got.Segments[0]) != 1 || len(got.Segments[1]) != 8 {
|
||||
t.Errorf("got unexpected tokens: %v", got.Segments)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
directiveFile, err := filepath.Abs("testdata/directive_import_test")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = ioutil.WriteFile(directiveFile, []byte(`prop1 1
|
||||
prop2 2`), 0644)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.Remove(directiveFile)
|
||||
|
||||
// import from existing file
|
||||
result, err := testParseOne(`localhost
|
||||
dir1
|
||||
proxy {
|
||||
import testdata/directive_import_test
|
||||
transparent
|
||||
}`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !isExpected(result) {
|
||||
t.Error("directive import failed")
|
||||
}
|
||||
|
||||
// import from nonexistent file
|
||||
_, err = testParseOne(`localhost
|
||||
dir1
|
||||
proxy {
|
||||
import testdata/nonexistent_file
|
||||
transparent
|
||||
}`)
|
||||
if err == nil {
|
||||
t.Fatal("expected error when importing a nonexistent file")
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseAll(t *testing.T) {
|
||||
for i, test := range []struct {
|
||||
input string
|
||||
shouldErr bool
|
||||
keys [][]string // keys per server block, in order
|
||||
}{
|
||||
{`localhost`, false, [][]string{
|
||||
{"localhost"},
|
||||
}},
|
||||
|
||||
{`localhost:1234`, false, [][]string{
|
||||
{"localhost:1234"},
|
||||
}},
|
||||
|
||||
{`localhost:1234 {
|
||||
}
|
||||
localhost:2015 {
|
||||
}`, false, [][]string{
|
||||
{"localhost:1234"},
|
||||
{"localhost:2015"},
|
||||
}},
|
||||
|
||||
{`localhost:1234, http://host2`, false, [][]string{
|
||||
{"localhost:1234", "http://host2"},
|
||||
}},
|
||||
|
||||
{`localhost:1234, http://host2,`, true, [][]string{}},
|
||||
|
||||
{`http://host1.com, http://host2.com {
|
||||
}
|
||||
https://host3.com, https://host4.com {
|
||||
}`, false, [][]string{
|
||||
{"http://host1.com", "http://host2.com"},
|
||||
{"https://host3.com", "https://host4.com"},
|
||||
}},
|
||||
|
||||
{`import testdata/import_glob*.txt`, false, [][]string{
|
||||
{"glob0.host0"},
|
||||
{"glob0.host1"},
|
||||
{"glob1.host0"},
|
||||
{"glob2.host0"},
|
||||
}},
|
||||
|
||||
{`import notfound/*`, false, [][]string{}}, // glob needn't error with no matches
|
||||
{`import notfound/file.conf`, true, [][]string{}}, // but a specific file should
|
||||
} {
|
||||
p := testParser(test.input)
|
||||
blocks, err := p.parseAll()
|
||||
|
||||
if test.shouldErr && err == nil {
|
||||
t.Errorf("Test %d: Expected an error, but didn't get one", i)
|
||||
}
|
||||
if !test.shouldErr && err != nil {
|
||||
t.Errorf("Test %d: Expected no error, but got: %v", i, err)
|
||||
}
|
||||
|
||||
if len(blocks) != len(test.keys) {
|
||||
t.Errorf("Test %d: Expected %d server blocks, got %d",
|
||||
i, len(test.keys), len(blocks))
|
||||
continue
|
||||
}
|
||||
for j, block := range blocks {
|
||||
if len(block.Keys) != len(test.keys[j]) {
|
||||
t.Errorf("Test %d: Expected %d keys in block %d, got %d",
|
||||
i, len(test.keys[j]), j, len(block.Keys))
|
||||
continue
|
||||
}
|
||||
for k, addr := range block.Keys {
|
||||
if addr != test.keys[j][k] {
|
||||
t.Errorf("Test %d, block %d, key %d: Expected '%s', but got '%s'",
|
||||
i, j, k, test.keys[j][k], addr)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnvironmentReplacement(t *testing.T) {
|
||||
os.Setenv("FOOBAR", "foobar")
|
||||
|
||||
for i, test := range []struct {
|
||||
input string
|
||||
expect string
|
||||
}{
|
||||
{
|
||||
input: "",
|
||||
expect: "",
|
||||
},
|
||||
{
|
||||
input: "foo",
|
||||
expect: "foo",
|
||||
},
|
||||
{
|
||||
input: "{$NOT_SET}",
|
||||
expect: "",
|
||||
},
|
||||
{
|
||||
input: "foo{$NOT_SET}bar",
|
||||
expect: "foobar",
|
||||
},
|
||||
{
|
||||
input: "{$FOOBAR}",
|
||||
expect: "foobar",
|
||||
},
|
||||
{
|
||||
input: "foo {$FOOBAR} bar",
|
||||
expect: "foo foobar bar",
|
||||
},
|
||||
{
|
||||
input: "foo{$FOOBAR}bar",
|
||||
expect: "foofoobarbar",
|
||||
},
|
||||
{
|
||||
input: "foo\n{$FOOBAR}\nbar",
|
||||
expect: "foo\nfoobar\nbar",
|
||||
},
|
||||
{
|
||||
input: "{$FOOBAR} {$FOOBAR}",
|
||||
expect: "foobar foobar",
|
||||
},
|
||||
{
|
||||
input: "{$FOOBAR}{$FOOBAR}",
|
||||
expect: "foobarfoobar",
|
||||
},
|
||||
{
|
||||
input: "{$FOOBAR",
|
||||
expect: "{$FOOBAR",
|
||||
},
|
||||
{
|
||||
input: "{$LONGER_NAME $FOOBAR}",
|
||||
expect: "",
|
||||
},
|
||||
{
|
||||
input: "{$}",
|
||||
expect: "{$}",
|
||||
},
|
||||
{
|
||||
input: "{$$}",
|
||||
expect: "",
|
||||
},
|
||||
{
|
||||
input: "{$",
|
||||
expect: "{$",
|
||||
},
|
||||
{
|
||||
input: "}{$",
|
||||
expect: "}{$",
|
||||
},
|
||||
} {
|
||||
actual, err := replaceEnvVars([]byte(test.input))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Equal(actual, []byte(test.expect)) {
|
||||
t.Errorf("Test %d: Expected: '%s' but got '%s'", i, test.expect, actual)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSnippets(t *testing.T) {
|
||||
p := testParser(`
|
||||
(common) {
|
||||
gzip foo
|
||||
errors stderr
|
||||
}
|
||||
http://example.com {
|
||||
import common
|
||||
}
|
||||
`)
|
||||
blocks, err := p.parseAll()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, b := range blocks {
|
||||
t.Log(b.Keys)
|
||||
t.Log(b.Segments)
|
||||
}
|
||||
if len(blocks) != 1 {
|
||||
t.Fatalf("Expect exactly one server block. Got %d.", len(blocks))
|
||||
}
|
||||
if actual, expected := blocks[0].Keys[0], "http://example.com"; expected != actual {
|
||||
t.Errorf("Expected server name to be '%s' but was '%s'", expected, actual)
|
||||
}
|
||||
if len(blocks[0].Segments) != 2 {
|
||||
t.Fatalf("Server block should have tokens from import, got: %+v", blocks[0])
|
||||
}
|
||||
if actual, expected := blocks[0].Segments[0][0].Text, "gzip"; expected != actual {
|
||||
t.Errorf("Expected argument to be '%s' but was '%s'", expected, actual)
|
||||
}
|
||||
if actual, expected := blocks[0].Segments[1][1].Text, "stderr"; expected != actual {
|
||||
t.Errorf("Expected argument to be '%s' but was '%s'", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func writeStringToTempFileOrDie(t *testing.T, str string) (pathToFile string) {
|
||||
file, err := ioutil.TempFile("", t.Name())
|
||||
if err != nil {
|
||||
panic(err) // get a stack trace so we know where this was called from.
|
||||
}
|
||||
if _, err := file.WriteString(str); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if err := file.Close(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return file.Name()
|
||||
}
|
||||
|
||||
func TestImportedFilesIgnoreNonDirectiveImportTokens(t *testing.T) {
|
||||
fileName := writeStringToTempFileOrDie(t, `
|
||||
http://example.com {
|
||||
# This isn't an import directive, it's just an arg with value 'import'
|
||||
basicauth / import password
|
||||
}
|
||||
`)
|
||||
// Parse the root file that imports the other one.
|
||||
p := testParser(`import ` + fileName)
|
||||
blocks, err := p.parseAll()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, b := range blocks {
|
||||
t.Log(b.Keys)
|
||||
t.Log(b.Segments)
|
||||
}
|
||||
auth := blocks[0].Segments[0]
|
||||
line := auth[0].Text + " " + auth[1].Text + " " + auth[2].Text + " " + auth[3].Text
|
||||
if line != "basicauth / import password" {
|
||||
// Previously, it would be changed to:
|
||||
// basicauth / import /path/to/test/dir/password
|
||||
// referencing a file that (probably) doesn't exist and changing the
|
||||
// password!
|
||||
t.Errorf("Expected basicauth tokens to be 'basicauth / import password' but got %#q", line)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSnippetAcrossMultipleFiles(t *testing.T) {
|
||||
// Make the derived Caddyfile that expects (common) to be defined.
|
||||
fileName := writeStringToTempFileOrDie(t, `
|
||||
http://example.com {
|
||||
import common
|
||||
}
|
||||
`)
|
||||
|
||||
// Parse the root file that defines (common) and then imports the other one.
|
||||
p := testParser(`
|
||||
(common) {
|
||||
gzip foo
|
||||
}
|
||||
import ` + fileName + `
|
||||
`)
|
||||
|
||||
blocks, err := p.parseAll()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, b := range blocks {
|
||||
t.Log(b.Keys)
|
||||
t.Log(b.Segments)
|
||||
}
|
||||
if len(blocks) != 1 {
|
||||
t.Fatalf("Expect exactly one server block. Got %d.", len(blocks))
|
||||
}
|
||||
if actual, expected := blocks[0].Keys[0], "http://example.com"; expected != actual {
|
||||
t.Errorf("Expected server name to be '%s' but was '%s'", expected, actual)
|
||||
}
|
||||
if len(blocks[0].Segments) != 1 {
|
||||
t.Fatalf("Server block should have tokens from import")
|
||||
}
|
||||
if actual, expected := blocks[0].Segments[0][0].Text, "gzip"; expected != actual {
|
||||
t.Errorf("Expected argument to be '%s' but was '%s'", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func testParser(input string) parser {
|
||||
return parser{Dispenser: newTestDispenser(input)}
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
glob0.host0 {
|
||||
dir2 arg1
|
||||
}
|
||||
|
||||
glob0.host1 {
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
glob1.host0 {
|
||||
dir1
|
||||
dir2 arg1
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
glob2.host0 {
|
||||
dir2 arg1
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
dir2 arg1 arg2
|
||||
dir3
|
||||
@@ -1,4 +0,0 @@
|
||||
host1 {
|
||||
dir1
|
||||
dir2 arg1
|
||||
}
|
||||
@@ -1,117 +0,0 @@
|
||||
// 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 caddyconfig
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// Adapter is a type which can adapt a configuration to Caddy JSON.
|
||||
// It returns the results and any warnings, or an error.
|
||||
type Adapter interface {
|
||||
Adapt(body []byte, options map[string]interface{}) ([]byte, []Warning, error)
|
||||
}
|
||||
|
||||
// Warning represents a warning or notice related to conversion.
|
||||
type Warning struct {
|
||||
File string `json:"file,omitempty"`
|
||||
Line int `json:"line,omitempty"`
|
||||
Directive string `json:"directive,omitempty"`
|
||||
Message string `json:"message,omitempty"`
|
||||
}
|
||||
|
||||
// JSON encodes val as JSON, returning it as a json.RawMessage. Any
|
||||
// marshaling errors (which are highly unlikely with correct code)
|
||||
// are converted to warnings. This is convenient when filling config
|
||||
// structs that require a json.RawMessage, without having to worry
|
||||
// about errors.
|
||||
func JSON(val interface{}, warnings *[]Warning) json.RawMessage {
|
||||
b, err := json.Marshal(val)
|
||||
if err != nil {
|
||||
if warnings != nil {
|
||||
*warnings = append(*warnings, Warning{Message: err.Error()})
|
||||
}
|
||||
return nil
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// JSONModuleObject is like JSON, except it marshals val into a JSON object
|
||||
// and then adds a key to that object named fieldName with the value fieldVal.
|
||||
// This is useful for JSON-encoding module values where the module name has to
|
||||
// be described within the object by a certain key; for example,
|
||||
// "responder": "file_server" for a file server HTTP responder. The val must
|
||||
// encode into a map[string]interface{} (i.e. it must be a struct or map),
|
||||
// and any errors are converted into warnings, so this can be conveniently
|
||||
// used when filling a struct. For correct code, there should be no errors.
|
||||
func JSONModuleObject(val interface{}, fieldName, fieldVal string, warnings *[]Warning) json.RawMessage {
|
||||
// encode to a JSON object first
|
||||
enc, err := json.Marshal(val)
|
||||
if err != nil {
|
||||
if warnings != nil {
|
||||
*warnings = append(*warnings, Warning{Message: err.Error()})
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// then decode the object
|
||||
var tmp map[string]interface{}
|
||||
err = json.Unmarshal(enc, &tmp)
|
||||
if err != nil {
|
||||
if warnings != nil {
|
||||
*warnings = append(*warnings, Warning{Message: err.Error()})
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// so we can easily add the module's field with its appointed value
|
||||
tmp[fieldName] = fieldVal
|
||||
|
||||
// then re-marshal as JSON
|
||||
result, err := json.Marshal(tmp)
|
||||
if err != nil {
|
||||
if warnings != nil {
|
||||
*warnings = append(*warnings, Warning{Message: err.Error()})
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// JSONIndent is used to JSON-marshal the final resulting Caddy
|
||||
// configuration in a consistent, human-readable way.
|
||||
func JSONIndent(val interface{}) ([]byte, error) {
|
||||
return json.MarshalIndent(val, "", "\t")
|
||||
}
|
||||
|
||||
// RegisterAdapter registers a config adapter with the given name.
|
||||
// This should usually be done at init-time.
|
||||
func RegisterAdapter(name string, adapter Adapter) error {
|
||||
if _, ok := configAdapters[name]; ok {
|
||||
return fmt.Errorf("%s: already registered", name)
|
||||
}
|
||||
configAdapters[name] = adapter
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetAdapter returns the adapter with the given name,
|
||||
// or nil if one with that name is not registered.
|
||||
func GetAdapter(name string) Adapter {
|
||||
return configAdapters[name]
|
||||
}
|
||||
|
||||
var configAdapters = make(map[string]Adapter)
|
||||
@@ -1,353 +0,0 @@
|
||||
// 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 httpcaddyfile
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/caddyserver/caddy/v2"
|
||||
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
|
||||
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
|
||||
"github.com/caddyserver/certmagic"
|
||||
)
|
||||
|
||||
// mapAddressToServerBlocks returns a map of listener address to list of server
|
||||
// blocks that will be served on that address. To do this, each server block is
|
||||
// expanded so that each one is considered individually, although keys of a
|
||||
// server block that share the same address stay grouped together so the config
|
||||
// isn't repeated unnecessarily. For example, this Caddyfile:
|
||||
//
|
||||
// example.com {
|
||||
// bind 127.0.0.1
|
||||
// }
|
||||
// www.example.com, example.net/path, localhost:9999 {
|
||||
// bind 127.0.0.1 1.2.3.4
|
||||
// }
|
||||
//
|
||||
// has two server blocks to start with. But expressed in this Caddyfile are
|
||||
// actually 4 listener addresses: 127.0.0.1:443, 1.2.3.4:443, 127.0.0.1:9999,
|
||||
// and 127.0.0.1:9999. This is because the bind directive is applied to each
|
||||
// key of its server block (specifying the host part), and each key may have
|
||||
// a different port. And we definitely need to be sure that a site which is
|
||||
// bound to be served on a specific interface is not served on others just
|
||||
// because that is more convenient: it would be a potential security risk
|
||||
// if the difference between interfaces means private vs. public.
|
||||
//
|
||||
// So what this function does for the example above is iterate each server
|
||||
// block, and for each server block, iterate its keys. For the first, it
|
||||
// finds one key (example.com) and determines its listener address
|
||||
// (127.0.0.1:443 - because of 'bind' and automatic HTTPS). It then adds
|
||||
// the listener address to the map value returned by this function, with
|
||||
// the first server block as one of its associations.
|
||||
//
|
||||
// It then iterates each key on the second server block and associates them
|
||||
// with one or more listener addresses. Indeed, each key in this block has
|
||||
// two listener addresses because of the 'bind' directive. Once we know
|
||||
// which addresses serve which keys, we can create a new server block for
|
||||
// each address containing the contents of the server block and only those
|
||||
// specific keys of the server block which use that address.
|
||||
//
|
||||
// It is possible and even likely that some keys in the returned map have
|
||||
// the exact same list of server blocks (i.e. they are identical). This
|
||||
// happens when multiple hosts are declared with a 'bind' directive and
|
||||
// the resulting listener addresses are not shared by any other server
|
||||
// block (or the other server blocks are exactly identical in their token
|
||||
// contents). This happens with our example above because 1.2.3.4:443
|
||||
// and 1.2.3.4:9999 are used exclusively with the second server block. This
|
||||
// repetition may be undesirable, so call consolidateAddrMappings() to map
|
||||
// multiple addresses to the same lists of server blocks (a many:many mapping).
|
||||
// (Doing this is essentially a map-reduce technique.)
|
||||
func (st *ServerType) mapAddressToServerBlocks(originalServerBlocks []serverBlock,
|
||||
options map[string]interface{}) (map[string][]serverBlock, error) {
|
||||
sbmap := make(map[string][]serverBlock)
|
||||
|
||||
for i, sblock := range originalServerBlocks {
|
||||
// within a server block, we need to map all the listener addresses
|
||||
// implied by the server block to the keys of the server block which
|
||||
// will be served by them; this has the effect of treating each
|
||||
// key of a server block as its own, but without having to repeat its
|
||||
// contents in cases where multiple keys really can be served together
|
||||
addrToKeys := make(map[string][]string)
|
||||
for j, key := range sblock.block.Keys {
|
||||
// a key can have multiple listener addresses if there are multiple
|
||||
// arguments to the 'bind' directive (although they will all have
|
||||
// the same port, since the port is defined by the key or is implicit
|
||||
// through automatic HTTPS)
|
||||
addrs, err := st.listenerAddrsForServerBlockKey(sblock, key, options)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("server block %d, key %d (%s): determining listener address: %v", i, j, key, err)
|
||||
}
|
||||
|
||||
// associate this key with each listener address it is served on
|
||||
for _, addr := range addrs {
|
||||
addrToKeys[addr] = append(addrToKeys[addr], key)
|
||||
}
|
||||
}
|
||||
|
||||
// now that we know which addresses serve which keys of this
|
||||
// server block, we iterate that mapping and create a list of
|
||||
// new server blocks for each address where the keys of the
|
||||
// server block are only the ones which use the address; but
|
||||
// the contents (tokens) are of course the same
|
||||
for addr, keys := range addrToKeys {
|
||||
sbmap[addr] = append(sbmap[addr], serverBlock{
|
||||
block: caddyfile.ServerBlock{
|
||||
Keys: keys,
|
||||
Segments: sblock.block.Segments,
|
||||
},
|
||||
pile: sblock.pile,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return sbmap, nil
|
||||
}
|
||||
|
||||
// consolidateAddrMappings eliminates repetition of identical server blocks in a mapping of
|
||||
// single listener addresses to lists of server blocks. Since multiple addresses may serve
|
||||
// identical sites (server block contents), this function turns a 1:many mapping into a
|
||||
// many:many mapping. Server block contents (tokens) must be exactly identical so that
|
||||
// reflect.DeepEqual returns true in order for the addresses to be combined. Identical
|
||||
// entries are deleted from the addrToServerBlocks map. Essentially, each pairing (each
|
||||
// association from multiple addresses to multiple server blocks; i.e. each element of
|
||||
// the returned slice) becomes a server definition in the output JSON.
|
||||
func (st *ServerType) consolidateAddrMappings(addrToServerBlocks map[string][]serverBlock) []sbAddrAssociation {
|
||||
var sbaddrs []sbAddrAssociation
|
||||
for addr, sblocks := range addrToServerBlocks {
|
||||
// we start with knowing that at least this address
|
||||
// maps to these server blocks
|
||||
a := sbAddrAssociation{
|
||||
addresses: []string{addr},
|
||||
serverBlocks: sblocks,
|
||||
}
|
||||
|
||||
// now find other addresses that map to identical
|
||||
// server blocks and add them to our list of
|
||||
// addresses, while removing them from the map
|
||||
for otherAddr, otherSblocks := range addrToServerBlocks {
|
||||
if addr == otherAddr {
|
||||
continue
|
||||
}
|
||||
if reflect.DeepEqual(sblocks, otherSblocks) {
|
||||
a.addresses = append(a.addresses, otherAddr)
|
||||
delete(addrToServerBlocks, otherAddr)
|
||||
}
|
||||
}
|
||||
|
||||
sbaddrs = append(sbaddrs, a)
|
||||
}
|
||||
return sbaddrs
|
||||
}
|
||||
|
||||
func (st *ServerType) listenerAddrsForServerBlockKey(sblock serverBlock, key string,
|
||||
options map[string]interface{}) ([]string, error) {
|
||||
addr, err := ParseAddress(key)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parsing key: %v", err)
|
||||
}
|
||||
addr = addr.Normalize()
|
||||
|
||||
// figure out the HTTP and HTTPS ports; either
|
||||
// use defaults, or override with user config
|
||||
httpPort, httpsPort := strconv.Itoa(certmagic.HTTPPort), strconv.Itoa(certmagic.HTTPSPort)
|
||||
if hport, ok := options["http_port"]; ok {
|
||||
httpPort = strconv.Itoa(hport.(int))
|
||||
}
|
||||
if hsport, ok := options["https_port"]; ok {
|
||||
httpsPort = strconv.Itoa(hsport.(int))
|
||||
}
|
||||
|
||||
// default port is the HTTPS port
|
||||
lnPort := httpsPort
|
||||
if addr.Port != "" {
|
||||
// port explicitly defined
|
||||
lnPort = addr.Port
|
||||
} else if addr.Scheme == "http" {
|
||||
// port inferred from scheme
|
||||
lnPort = httpPort
|
||||
}
|
||||
|
||||
// error if scheme and port combination violate convention
|
||||
if (addr.Scheme == "http" && lnPort == httpsPort) || (addr.Scheme == "https" && lnPort == httpPort) {
|
||||
return nil, fmt.Errorf("[%s] scheme and port violate convention", key)
|
||||
}
|
||||
|
||||
// the bind directive specifies hosts, but is optional
|
||||
var lnHosts []string
|
||||
for _, cfgVal := range sblock.pile["bind"] {
|
||||
lnHosts = append(lnHosts, cfgVal.Value.([]string)...)
|
||||
}
|
||||
if len(lnHosts) == 0 {
|
||||
lnHosts = []string{""}
|
||||
}
|
||||
|
||||
// use a map to prevent duplication
|
||||
listeners := make(map[string]struct{})
|
||||
for _, host := range lnHosts {
|
||||
addr, err := caddy.ParseNetworkAddress(host)
|
||||
if err == nil && addr.IsUnixNetwork() {
|
||||
listeners[host] = struct{}{}
|
||||
} else {
|
||||
listeners[net.JoinHostPort(host, lnPort)] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
// now turn map into list
|
||||
var listenersList []string
|
||||
for lnStr := range listeners {
|
||||
listenersList = append(listenersList, lnStr)
|
||||
}
|
||||
|
||||
return listenersList, nil
|
||||
}
|
||||
|
||||
// Address represents a site address. It contains
|
||||
// the original input value, and the component
|
||||
// parts of an address. The component parts may be
|
||||
// updated to the correct values as setup proceeds,
|
||||
// but the original value should never be changed.
|
||||
//
|
||||
// The Host field must be in a normalized form.
|
||||
type Address struct {
|
||||
Original, Scheme, Host, Port, Path string
|
||||
}
|
||||
|
||||
// ParseAddress parses an address string into a structured format with separate
|
||||
// scheme, host, port, and path portions, as well as the original input string.
|
||||
func ParseAddress(str string) (Address, error) {
|
||||
const maxLen = 4096
|
||||
if len(str) > maxLen {
|
||||
str = str[:maxLen]
|
||||
}
|
||||
remaining := strings.TrimSpace(str)
|
||||
a := Address{Original: remaining}
|
||||
|
||||
// extract scheme
|
||||
splitScheme := strings.SplitN(remaining, "://", 2)
|
||||
switch len(splitScheme) {
|
||||
case 0:
|
||||
return a, nil
|
||||
case 1:
|
||||
remaining = splitScheme[0]
|
||||
case 2:
|
||||
a.Scheme = splitScheme[0]
|
||||
remaining = splitScheme[1]
|
||||
}
|
||||
|
||||
// extract host and port
|
||||
hostSplit := strings.SplitN(remaining, "/", 2)
|
||||
if len(hostSplit) > 0 {
|
||||
host, port, err := net.SplitHostPort(hostSplit[0])
|
||||
if err != nil {
|
||||
host, port, err = net.SplitHostPort(hostSplit[0] + ":")
|
||||
if err != nil {
|
||||
host = hostSplit[0]
|
||||
}
|
||||
}
|
||||
a.Host = host
|
||||
a.Port = port
|
||||
}
|
||||
if len(hostSplit) == 2 {
|
||||
// all that remains is the path
|
||||
a.Path = "/" + hostSplit[1]
|
||||
}
|
||||
|
||||
// make sure port is valid
|
||||
if a.Port != "" {
|
||||
if portNum, err := strconv.Atoi(a.Port); err != nil {
|
||||
return Address{}, fmt.Errorf("invalid port '%s': %v", a.Port, err)
|
||||
} else if portNum < 0 || portNum > 65535 {
|
||||
return Address{}, fmt.Errorf("port %d is out of range", portNum)
|
||||
}
|
||||
}
|
||||
|
||||
return a, nil
|
||||
}
|
||||
|
||||
// String returns a human-readable form of a. It will
|
||||
// be a cleaned-up and filled-out URL string.
|
||||
func (a Address) String() string {
|
||||
if a.Host == "" && a.Port == "" {
|
||||
return ""
|
||||
}
|
||||
scheme := a.Scheme
|
||||
if scheme == "" {
|
||||
if a.Port == strconv.Itoa(certmagic.HTTPSPort) {
|
||||
scheme = "https"
|
||||
} else {
|
||||
scheme = "http"
|
||||
}
|
||||
}
|
||||
s := scheme
|
||||
if s != "" {
|
||||
s += "://"
|
||||
}
|
||||
if a.Port != "" &&
|
||||
((scheme == "https" && a.Port != strconv.Itoa(caddyhttp.DefaultHTTPSPort)) ||
|
||||
(scheme == "http" && a.Port != strconv.Itoa(caddyhttp.DefaultHTTPPort))) {
|
||||
s += net.JoinHostPort(a.Host, a.Port)
|
||||
} else {
|
||||
s += a.Host
|
||||
}
|
||||
if a.Path != "" {
|
||||
s += a.Path
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// Normalize returns a normalized version of a.
|
||||
func (a Address) Normalize() Address {
|
||||
path := a.Path
|
||||
|
||||
// ensure host is normalized if it's an IP address
|
||||
host := strings.TrimSpace(a.Host)
|
||||
if ip := net.ParseIP(host); ip != nil {
|
||||
host = ip.String()
|
||||
}
|
||||
|
||||
return Address{
|
||||
Original: a.Original,
|
||||
Scheme: strings.ToLower(a.Scheme),
|
||||
Host: strings.ToLower(host),
|
||||
Port: a.Port,
|
||||
Path: path,
|
||||
}
|
||||
}
|
||||
|
||||
// Key returns a string form of a, much like String() does, but this
|
||||
// method doesn't add anything default that wasn't in the original.
|
||||
func (a Address) Key() string {
|
||||
res := ""
|
||||
if a.Scheme != "" {
|
||||
res += a.Scheme + "://"
|
||||
}
|
||||
if a.Host != "" {
|
||||
res += a.Host
|
||||
}
|
||||
// insert port only if the original has its own explicit port
|
||||
if a.Port != "" &&
|
||||
len(a.Original) >= len(res) &&
|
||||
strings.HasPrefix(a.Original[len(res):], ":"+a.Port) {
|
||||
res += ":" + a.Port
|
||||
}
|
||||
if a.Path != "" {
|
||||
res += a.Path
|
||||
}
|
||||
return res
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
// 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.
|
||||
|
||||
// +build gofuzz
|
||||
// +build gofuzz_libfuzzer
|
||||
|
||||
package httpcaddyfile
|
||||
|
||||
func FuzzParseAddress(data []byte) int {
|
||||
addr, err := ParseAddress(string(data))
|
||||
if err != nil {
|
||||
if addr == (Address{}) {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
return 1
|
||||
}
|
||||
@@ -1,163 +0,0 @@
|
||||
package httpcaddyfile
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestParseAddress(t *testing.T) {
|
||||
for i, test := range []struct {
|
||||
input string
|
||||
scheme, host, port, path string
|
||||
shouldErr bool
|
||||
}{
|
||||
{``, "", "", "", "", false},
|
||||
{`localhost`, "", "localhost", "", "", false},
|
||||
{`localhost:1234`, "", "localhost", "1234", "", false},
|
||||
{`localhost:`, "", "localhost", "", "", false},
|
||||
{`0.0.0.0`, "", "0.0.0.0", "", "", false},
|
||||
{`127.0.0.1:1234`, "", "127.0.0.1", "1234", "", false},
|
||||
{`:1234`, "", "", "1234", "", false},
|
||||
{`[::1]`, "", "::1", "", "", false},
|
||||
{`[::1]:1234`, "", "::1", "1234", "", false},
|
||||
{`:`, "", "", "", "", false},
|
||||
{`:http`, "", "", "", "", true},
|
||||
{`:https`, "", "", "", "", true},
|
||||
{`localhost:http`, "", "", "", "", true}, // using service name in port is verboten, as of Go 1.12.8
|
||||
{`localhost:https`, "", "", "", "", true},
|
||||
{`http://localhost:https`, "", "", "", "", true}, // conflict
|
||||
{`http://localhost:http`, "", "", "", "", true}, // repeated scheme
|
||||
{`host:https/path`, "", "", "", "", true},
|
||||
{`http://localhost:443`, "http", "localhost", "443", "", false}, // NOTE: not conventional
|
||||
{`https://localhost:80`, "https", "localhost", "80", "", false}, // NOTE: not conventional
|
||||
{`http://localhost`, "http", "localhost", "", "", false},
|
||||
{`https://localhost`, "https", "localhost", "", "", false},
|
||||
{`http://{env.APP_DOMAIN}`, "http", "{env.APP_DOMAIN}", "", "", false},
|
||||
{`{env.APP_DOMAIN}:80`, "", "{env.APP_DOMAIN}", "80", "", false},
|
||||
{`{env.APP_DOMAIN}/path`, "", "{env.APP_DOMAIN}", "", "/path", false},
|
||||
{`example.com/{env.APP_PATH}`, "", "example.com", "", "/{env.APP_PATH}", false},
|
||||
{`http://127.0.0.1`, "http", "127.0.0.1", "", "", false},
|
||||
{`https://127.0.0.1`, "https", "127.0.0.1", "", "", false},
|
||||
{`http://[::1]`, "http", "::1", "", "", false},
|
||||
{`http://localhost:1234`, "http", "localhost", "1234", "", false},
|
||||
{`https://127.0.0.1:1234`, "https", "127.0.0.1", "1234", "", false},
|
||||
{`http://[::1]:1234`, "http", "::1", "1234", "", false},
|
||||
{``, "", "", "", "", false},
|
||||
{`::1`, "", "::1", "", "", false},
|
||||
{`localhost::`, "", "localhost::", "", "", false},
|
||||
{`#$%@`, "", "#$%@", "", "", false}, // don't want to presume what the hostname could be
|
||||
{`host/path`, "", "host", "", "/path", false},
|
||||
{`http://host/`, "http", "host", "", "/", false},
|
||||
{`//asdf`, "", "", "", "//asdf", false},
|
||||
{`:1234/asdf`, "", "", "1234", "/asdf", false},
|
||||
{`http://host/path`, "http", "host", "", "/path", false},
|
||||
{`https://host:443/path/foo`, "https", "host", "443", "/path/foo", false},
|
||||
{`host:80/path`, "", "host", "80", "/path", false},
|
||||
{`/path`, "", "", "", "/path", false},
|
||||
} {
|
||||
actual, err := ParseAddress(test.input)
|
||||
|
||||
if err != nil && !test.shouldErr {
|
||||
t.Errorf("Test %d (%s): Expected no error, but had error: %v", i, test.input, err)
|
||||
}
|
||||
if err == nil && test.shouldErr {
|
||||
t.Errorf("Test %d (%s): Expected error, but had none (%#v)", i, test.input, actual)
|
||||
}
|
||||
|
||||
if !test.shouldErr && actual.Original != test.input {
|
||||
t.Errorf("Test %d (%s): Expected original '%s', got '%s'", i, test.input, test.input, actual.Original)
|
||||
}
|
||||
if actual.Scheme != test.scheme {
|
||||
t.Errorf("Test %d (%s): Expected scheme '%s', got '%s'", i, test.input, test.scheme, actual.Scheme)
|
||||
}
|
||||
if actual.Host != test.host {
|
||||
t.Errorf("Test %d (%s): Expected host '%s', got '%s'", i, test.input, test.host, actual.Host)
|
||||
}
|
||||
if actual.Port != test.port {
|
||||
t.Errorf("Test %d (%s): Expected port '%s', got '%s'", i, test.input, test.port, actual.Port)
|
||||
}
|
||||
if actual.Path != test.path {
|
||||
t.Errorf("Test %d (%s): Expected path '%s', got '%s'", i, test.input, test.path, actual.Path)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddressString(t *testing.T) {
|
||||
for i, test := range []struct {
|
||||
addr Address
|
||||
expected string
|
||||
}{
|
||||
{Address{Scheme: "http", Host: "host", Port: "1234", Path: "/path"}, "http://host:1234/path"},
|
||||
{Address{Scheme: "", Host: "host", Port: "", Path: ""}, "http://host"},
|
||||
{Address{Scheme: "", Host: "host", Port: "80", Path: ""}, "http://host"},
|
||||
{Address{Scheme: "", Host: "host", Port: "443", Path: ""}, "https://host"},
|
||||
{Address{Scheme: "https", Host: "host", Port: "443", Path: ""}, "https://host"},
|
||||
{Address{Scheme: "https", Host: "host", Port: "", Path: ""}, "https://host"},
|
||||
{Address{Scheme: "", Host: "host", Port: "80", Path: "/path"}, "http://host/path"},
|
||||
{Address{Scheme: "http", Host: "", Port: "1234", Path: ""}, "http://:1234"},
|
||||
{Address{Scheme: "", Host: "", Port: "", Path: ""}, ""},
|
||||
} {
|
||||
actual := test.addr.String()
|
||||
if actual != test.expected {
|
||||
t.Errorf("Test %d: expected '%s' but got '%s'", i, test.expected, actual)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestKeyNormalization(t *testing.T) {
|
||||
testCases := []struct {
|
||||
input string
|
||||
expect string
|
||||
}{
|
||||
{
|
||||
input: "http://host:1234/path",
|
||||
expect: "http://host:1234/path",
|
||||
},
|
||||
{
|
||||
input: "HTTP://A/ABCDEF",
|
||||
expect: "http://a/ABCDEF",
|
||||
},
|
||||
{
|
||||
input: "A/ABCDEF",
|
||||
expect: "a/ABCDEF",
|
||||
},
|
||||
{
|
||||
input: "A:2015/Path",
|
||||
expect: "a:2015/Path",
|
||||
},
|
||||
{
|
||||
input: ":80",
|
||||
expect: ":80",
|
||||
},
|
||||
{
|
||||
input: ":443",
|
||||
expect: ":443",
|
||||
},
|
||||
{
|
||||
input: ":1234",
|
||||
expect: ":1234",
|
||||
},
|
||||
{
|
||||
input: "",
|
||||
expect: "",
|
||||
},
|
||||
{
|
||||
input: ":",
|
||||
expect: "",
|
||||
},
|
||||
{
|
||||
input: "[::]",
|
||||
expect: "::",
|
||||
},
|
||||
}
|
||||
for i, tc := range testCases {
|
||||
addr, err := ParseAddress(tc.input)
|
||||
if err != nil {
|
||||
t.Errorf("Test %d: Parsing address '%s': %v", i, tc.input, err)
|
||||
continue
|
||||
}
|
||||
if actual := addr.Normalize().Key(); actual != tc.expect {
|
||||
t.Errorf("Test %d: Normalized key for address '%s' was '%s' but expected '%s'", i, tc.input, actual, tc.expect)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,570 +0,0 @@
|
||||
// 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 httpcaddyfile
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"html"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"github.com/caddyserver/caddy/v2"
|
||||
"github.com/caddyserver/caddy/v2/caddyconfig"
|
||||
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
|
||||
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
|
||||
"github.com/caddyserver/caddy/v2/modules/caddytls"
|
||||
"go.uber.org/zap/zapcore"
|
||||
)
|
||||
|
||||
func init() {
|
||||
RegisterDirective("bind", parseBind)
|
||||
RegisterDirective("root", parseRoot) // TODO: isn't this a handler directive?
|
||||
RegisterDirective("tls", parseTLS)
|
||||
RegisterHandlerDirective("redir", parseRedir)
|
||||
RegisterHandlerDirective("respond", parseRespond)
|
||||
RegisterHandlerDirective("route", parseRoute)
|
||||
RegisterHandlerDirective("handle", parseHandle)
|
||||
RegisterDirective("handle_errors", parseHandleErrors)
|
||||
RegisterDirective("log", parseLog)
|
||||
}
|
||||
|
||||
// parseBind parses the bind directive. Syntax:
|
||||
//
|
||||
// bind <addresses...>
|
||||
//
|
||||
func parseBind(h Helper) ([]ConfigValue, error) {
|
||||
var lnHosts []string
|
||||
for h.Next() {
|
||||
lnHosts = append(lnHosts, h.RemainingArgs()...)
|
||||
}
|
||||
return h.NewBindAddresses(lnHosts), nil
|
||||
}
|
||||
|
||||
// parseRoot parses the root directive. Syntax:
|
||||
//
|
||||
// root [<matcher>] <path>
|
||||
//
|
||||
func parseRoot(h Helper) ([]ConfigValue, error) {
|
||||
if !h.Next() {
|
||||
return nil, h.ArgErr()
|
||||
}
|
||||
|
||||
matcherSet, ok, err := h.MatcherToken()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !ok {
|
||||
// no matcher token; oops
|
||||
h.Dispenser.Prev()
|
||||
}
|
||||
|
||||
if !h.NextArg() {
|
||||
return nil, h.ArgErr()
|
||||
}
|
||||
root := h.Val()
|
||||
if h.NextArg() {
|
||||
return nil, h.ArgErr()
|
||||
}
|
||||
|
||||
varsHandler := caddyhttp.VarsMiddleware{"root": root}
|
||||
route := caddyhttp.Route{
|
||||
HandlersRaw: []json.RawMessage{
|
||||
caddyconfig.JSONModuleObject(varsHandler, "handler", "vars", nil),
|
||||
},
|
||||
}
|
||||
if matcherSet != nil {
|
||||
route.MatcherSetsRaw = []caddy.ModuleMap{matcherSet}
|
||||
}
|
||||
|
||||
return []ConfigValue{{Class: "route", Value: route}}, nil
|
||||
}
|
||||
|
||||
// parseTLS parses the tls directive. Syntax:
|
||||
//
|
||||
// tls [<email>|internal]|[<cert_file> <key_file>] {
|
||||
// protocols <min> [<max>]
|
||||
// ciphers <cipher_suites...>
|
||||
// curves <curves...>
|
||||
// alpn <values...>
|
||||
// load <paths...>
|
||||
// ca <acme_ca_endpoint>
|
||||
// ca_root <pem_file>
|
||||
// dns <provider_name>
|
||||
// on_demand
|
||||
// }
|
||||
//
|
||||
func parseTLS(h Helper) ([]ConfigValue, error) {
|
||||
cp := new(caddytls.ConnectionPolicy)
|
||||
var fileLoader caddytls.FileLoader
|
||||
var folderLoader caddytls.FolderLoader
|
||||
var acmeIssuer *caddytls.ACMEIssuer
|
||||
var internalIssuer *caddytls.InternalIssuer
|
||||
var onDemand bool
|
||||
|
||||
for h.Next() {
|
||||
// file certificate loader
|
||||
firstLine := h.RemainingArgs()
|
||||
switch len(firstLine) {
|
||||
case 0:
|
||||
case 1:
|
||||
if firstLine[0] == "internal" {
|
||||
internalIssuer = new(caddytls.InternalIssuer)
|
||||
} else if !strings.Contains(firstLine[0], "@") {
|
||||
return nil, h.Err("single argument must either be 'internal' or an email address")
|
||||
} else {
|
||||
if acmeIssuer == nil {
|
||||
acmeIssuer = new(caddytls.ACMEIssuer)
|
||||
}
|
||||
acmeIssuer.Email = firstLine[0]
|
||||
}
|
||||
|
||||
case 2:
|
||||
certFilename := firstLine[0]
|
||||
keyFilename := firstLine[1]
|
||||
|
||||
// tag this certificate so if multiple certs match, specifically
|
||||
// this one that the user has provided will be used, see #2588:
|
||||
// https://github.com/caddyserver/caddy/issues/2588 ... but we
|
||||
// must be careful about how we do this; being careless will
|
||||
// lead to failed handshakes
|
||||
//
|
||||
// we need to remember which cert files we've seen, since we
|
||||
// must load each cert only once; otherwise, they each get a
|
||||
// different tag... since a cert loaded twice has the same
|
||||
// bytes, it will overwrite the first one in the cache, and
|
||||
// only the last cert (and its tag) will survive, so a any conn
|
||||
// policy that is looking for any tag but the last one to be
|
||||
// loaded won't find it, and TLS handshakes will fail (see end)
|
||||
// of issue #3004)
|
||||
//
|
||||
// tlsCertTags maps certificate filenames to their tag.
|
||||
// This is used to remember which tag is used for each
|
||||
// certificate files, since we need to avoid loading
|
||||
// the same certificate files more than once, overwriting
|
||||
// previous tags
|
||||
tlsCertTags, ok := h.State["tlsCertTags"].(map[string]string)
|
||||
if !ok {
|
||||
tlsCertTags = make(map[string]string)
|
||||
h.State["tlsCertTags"] = tlsCertTags
|
||||
}
|
||||
|
||||
tag, ok := tlsCertTags[certFilename]
|
||||
if !ok {
|
||||
// haven't seen this cert file yet, let's give it a tag
|
||||
// and add a loader for it
|
||||
tag = fmt.Sprintf("cert%d", len(tlsCertTags))
|
||||
fileLoader = append(fileLoader, caddytls.CertKeyFilePair{
|
||||
Certificate: certFilename,
|
||||
Key: keyFilename,
|
||||
Tags: []string{tag},
|
||||
})
|
||||
// remember this for next time we see this cert file
|
||||
tlsCertTags[certFilename] = tag
|
||||
}
|
||||
certSelector := caddytls.CustomCertSelectionPolicy{Tag: tag}
|
||||
cp.CertSelection = caddyconfig.JSONModuleObject(certSelector, "policy", "custom", h.warnings)
|
||||
default:
|
||||
return nil, h.ArgErr()
|
||||
}
|
||||
|
||||
var hasBlock bool
|
||||
for h.NextBlock(0) {
|
||||
hasBlock = true
|
||||
|
||||
switch h.Val() {
|
||||
case "protocols":
|
||||
args := h.RemainingArgs()
|
||||
if len(args) == 0 {
|
||||
return nil, h.SyntaxErr("one or two protocols")
|
||||
}
|
||||
if len(args) > 0 {
|
||||
if _, ok := caddytls.SupportedProtocols[args[0]]; !ok {
|
||||
return nil, h.Errf("Wrong protocol name or protocol not supported: '%s'", args[0])
|
||||
}
|
||||
cp.ProtocolMin = args[0]
|
||||
}
|
||||
if len(args) > 1 {
|
||||
if _, ok := caddytls.SupportedProtocols[args[1]]; !ok {
|
||||
return nil, h.Errf("Wrong protocol name or protocol not supported: '%s'", args[1])
|
||||
}
|
||||
cp.ProtocolMax = args[1]
|
||||
}
|
||||
|
||||
case "ciphers":
|
||||
for h.NextArg() {
|
||||
if _, ok := caddytls.SupportedCipherSuites[h.Val()]; !ok {
|
||||
return nil, h.Errf("Wrong cipher suite name or cipher suite not supported: '%s'", h.Val())
|
||||
}
|
||||
cp.CipherSuites = append(cp.CipherSuites, h.Val())
|
||||
}
|
||||
|
||||
case "curves":
|
||||
for h.NextArg() {
|
||||
if _, ok := caddytls.SupportedCurves[h.Val()]; !ok {
|
||||
return nil, h.Errf("Wrong curve name or curve not supported: '%s'", h.Val())
|
||||
}
|
||||
cp.Curves = append(cp.Curves, h.Val())
|
||||
}
|
||||
|
||||
case "alpn":
|
||||
args := h.RemainingArgs()
|
||||
if len(args) == 0 {
|
||||
return nil, h.ArgErr()
|
||||
}
|
||||
cp.ALPN = args
|
||||
|
||||
case "load":
|
||||
folderLoader = append(folderLoader, h.RemainingArgs()...)
|
||||
|
||||
case "ca":
|
||||
arg := h.RemainingArgs()
|
||||
if len(arg) != 1 {
|
||||
return nil, h.ArgErr()
|
||||
}
|
||||
if acmeIssuer == nil {
|
||||
acmeIssuer = new(caddytls.ACMEIssuer)
|
||||
}
|
||||
acmeIssuer.CA = arg[0]
|
||||
|
||||
case "dns":
|
||||
if !h.Next() {
|
||||
return nil, h.ArgErr()
|
||||
}
|
||||
if acmeIssuer == nil {
|
||||
acmeIssuer = new(caddytls.ACMEIssuer)
|
||||
}
|
||||
provName := h.Val()
|
||||
if acmeIssuer.Challenges == nil {
|
||||
acmeIssuer.Challenges = new(caddytls.ChallengesConfig)
|
||||
}
|
||||
dnsProvModule, err := caddy.GetModule("tls.dns." + provName)
|
||||
if err != nil {
|
||||
return nil, h.Errf("getting DNS provider module named '%s': %v", provName, err)
|
||||
}
|
||||
acmeIssuer.Challenges.DNSRaw = caddyconfig.JSONModuleObject(dnsProvModule.New(), "provider", provName, h.warnings)
|
||||
|
||||
case "ca_root":
|
||||
arg := h.RemainingArgs()
|
||||
if len(arg) != 1 {
|
||||
return nil, h.ArgErr()
|
||||
}
|
||||
if acmeIssuer == nil {
|
||||
acmeIssuer = new(caddytls.ACMEIssuer)
|
||||
}
|
||||
acmeIssuer.TrustedRootsPEMFiles = append(acmeIssuer.TrustedRootsPEMFiles, arg[0])
|
||||
|
||||
case "on_demand":
|
||||
if h.NextArg() {
|
||||
return nil, h.ArgErr()
|
||||
}
|
||||
onDemand = true
|
||||
|
||||
default:
|
||||
return nil, h.Errf("unknown subdirective: %s", h.Val())
|
||||
}
|
||||
}
|
||||
|
||||
// a naked tls directive is not allowed
|
||||
if len(firstLine) == 0 && !hasBlock {
|
||||
return nil, h.ArgErr()
|
||||
}
|
||||
}
|
||||
|
||||
// begin building the final config values
|
||||
var configVals []ConfigValue
|
||||
|
||||
// certificate loaders
|
||||
if len(fileLoader) > 0 {
|
||||
configVals = append(configVals, ConfigValue{
|
||||
Class: "tls.certificate_loader",
|
||||
Value: fileLoader,
|
||||
})
|
||||
}
|
||||
if len(folderLoader) > 0 {
|
||||
configVals = append(configVals, ConfigValue{
|
||||
Class: "tls.certificate_loader",
|
||||
Value: folderLoader,
|
||||
})
|
||||
}
|
||||
|
||||
// issuer
|
||||
if acmeIssuer != nil && internalIssuer != nil {
|
||||
// the logic to support this would be complex
|
||||
return nil, h.Err("cannot use both ACME and internal issuers in same server block")
|
||||
}
|
||||
if acmeIssuer != nil {
|
||||
// fill in global defaults, if configured
|
||||
if email := h.Option("email"); email != nil && acmeIssuer.Email == "" {
|
||||
acmeIssuer.Email = email.(string)
|
||||
}
|
||||
if acmeCA := h.Option("acme_ca"); acmeCA != nil && acmeIssuer.CA == "" {
|
||||
acmeIssuer.CA = acmeCA.(string)
|
||||
}
|
||||
if caPemFile := h.Option("acme_ca_root"); caPemFile != nil {
|
||||
acmeIssuer.TrustedRootsPEMFiles = append(acmeIssuer.TrustedRootsPEMFiles, caPemFile.(string))
|
||||
}
|
||||
|
||||
configVals = append(configVals, ConfigValue{
|
||||
Class: "tls.cert_issuer",
|
||||
Value: acmeIssuer,
|
||||
})
|
||||
} else if internalIssuer != nil {
|
||||
configVals = append(configVals, ConfigValue{
|
||||
Class: "tls.cert_issuer",
|
||||
Value: internalIssuer,
|
||||
})
|
||||
}
|
||||
|
||||
// on-demand TLS
|
||||
if onDemand {
|
||||
configVals = append(configVals, ConfigValue{
|
||||
Class: "tls.on_demand",
|
||||
Value: true,
|
||||
})
|
||||
}
|
||||
|
||||
// connection policy -- always add one, to ensure that TLS
|
||||
// is enabled, because this directive was used (this is
|
||||
// needed, for instance, when a site block has a key of
|
||||
// just ":5000" - i.e. no hostname, and only on-demand TLS
|
||||
// is enabled)
|
||||
configVals = append(configVals, ConfigValue{
|
||||
Class: "tls.connection_policy",
|
||||
Value: cp,
|
||||
})
|
||||
|
||||
return configVals, nil
|
||||
}
|
||||
|
||||
// parseRedir parses the redir directive. Syntax:
|
||||
//
|
||||
// redir [<matcher>] <to> [<code>]
|
||||
//
|
||||
func parseRedir(h Helper) (caddyhttp.MiddlewareHandler, error) {
|
||||
if !h.Next() {
|
||||
return nil, h.ArgErr()
|
||||
}
|
||||
|
||||
if !h.NextArg() {
|
||||
return nil, h.ArgErr()
|
||||
}
|
||||
to := h.Val()
|
||||
|
||||
var code string
|
||||
if h.NextArg() {
|
||||
code = h.Val()
|
||||
}
|
||||
if code == "permanent" {
|
||||
code = "301"
|
||||
}
|
||||
if code == "temporary" || code == "" {
|
||||
code = "302"
|
||||
}
|
||||
var body string
|
||||
if code == "html" {
|
||||
// Script tag comes first since that will better imitate a redirect in the browser's
|
||||
// history, but the meta tag is a fallback for most non-JS clients.
|
||||
const metaRedir = `<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Redirecting...</title>
|
||||
<script>window.location.replace("%s");</script>
|
||||
<meta http-equiv="refresh" content="0; URL='%s'">
|
||||
</head>
|
||||
<body>Redirecting to <a href="%s">%s</a>...</body>
|
||||
</html>
|
||||
`
|
||||
safeTo := html.EscapeString(to)
|
||||
body = fmt.Sprintf(metaRedir, safeTo, safeTo, safeTo, safeTo)
|
||||
}
|
||||
|
||||
return caddyhttp.StaticResponse{
|
||||
StatusCode: caddyhttp.WeakString(code),
|
||||
Headers: http.Header{"Location": []string{to}},
|
||||
Body: body,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// parseRespond parses the respond directive.
|
||||
func parseRespond(h Helper) (caddyhttp.MiddlewareHandler, error) {
|
||||
sr := new(caddyhttp.StaticResponse)
|
||||
err := sr.UnmarshalCaddyfile(h.Dispenser)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return sr, nil
|
||||
}
|
||||
|
||||
// parseRoute parses the route directive.
|
||||
func parseRoute(h Helper) (caddyhttp.MiddlewareHandler, error) {
|
||||
sr := new(caddyhttp.Subroute)
|
||||
|
||||
for h.Next() {
|
||||
for nesting := h.Nesting(); h.NextBlock(nesting); {
|
||||
dir := h.Val()
|
||||
|
||||
dirFunc, ok := registeredDirectives[dir]
|
||||
if !ok {
|
||||
return nil, h.Errf("unrecognized directive: %s", dir)
|
||||
}
|
||||
|
||||
subHelper := h
|
||||
subHelper.Dispenser = h.NewFromNextSegment()
|
||||
|
||||
results, err := dirFunc(subHelper)
|
||||
if err != nil {
|
||||
return nil, h.Errf("parsing caddyfile tokens for '%s': %v", dir, err)
|
||||
}
|
||||
for _, result := range results {
|
||||
handler, ok := result.Value.(caddyhttp.Route)
|
||||
if !ok {
|
||||
return nil, h.Errf("%s directive returned something other than an HTTP route: %#v (only handler directives can be used in routes)", dir, result.Value)
|
||||
}
|
||||
sr.Routes = append(sr.Routes, handler)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return sr, nil
|
||||
}
|
||||
|
||||
func parseHandle(h Helper) (caddyhttp.MiddlewareHandler, error) {
|
||||
return parseSegmentAsSubroute(h)
|
||||
}
|
||||
|
||||
func parseHandleErrors(h Helper) ([]ConfigValue, error) {
|
||||
subroute, err := parseSegmentAsSubroute(h)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return []ConfigValue{
|
||||
{
|
||||
Class: "error_route",
|
||||
Value: subroute,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// parseLog parses the log directive. Syntax:
|
||||
//
|
||||
// log {
|
||||
// output <writer_module> ...
|
||||
// format <encoder_module> ...
|
||||
// level <level>
|
||||
// }
|
||||
//
|
||||
func parseLog(h Helper) ([]ConfigValue, error) {
|
||||
var configValues []ConfigValue
|
||||
for h.Next() {
|
||||
cl := new(caddy.CustomLog)
|
||||
|
||||
for h.NextBlock(0) {
|
||||
switch h.Val() {
|
||||
case "output":
|
||||
if !h.NextArg() {
|
||||
return nil, h.ArgErr()
|
||||
}
|
||||
moduleName := h.Val()
|
||||
|
||||
// can't use the usual caddyfile.Unmarshaler flow with the
|
||||
// standard writers because they are in the caddy package
|
||||
// (because they are the default) and implementing that
|
||||
// interface there would unfortunately create circular import
|
||||
var wo caddy.WriterOpener
|
||||
switch moduleName {
|
||||
case "stdout":
|
||||
wo = caddy.StdoutWriter{}
|
||||
case "stderr":
|
||||
wo = caddy.StderrWriter{}
|
||||
case "discard":
|
||||
wo = caddy.DiscardWriter{}
|
||||
default:
|
||||
mod, err := caddy.GetModule("caddy.logging.writers." + moduleName)
|
||||
if err != nil {
|
||||
return nil, h.Errf("getting log writer module named '%s': %v", moduleName, err)
|
||||
}
|
||||
unm, ok := mod.New().(caddyfile.Unmarshaler)
|
||||
if !ok {
|
||||
return nil, h.Errf("log writer module '%s' is not a Caddyfile unmarshaler", mod)
|
||||
}
|
||||
err = unm.UnmarshalCaddyfile(h.NewFromNextSegment())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
wo, ok = unm.(caddy.WriterOpener)
|
||||
if !ok {
|
||||
return nil, h.Errf("module %s is not a WriterOpener", mod)
|
||||
}
|
||||
}
|
||||
cl.WriterRaw = caddyconfig.JSONModuleObject(wo, "output", moduleName, h.warnings)
|
||||
|
||||
case "format":
|
||||
if !h.NextArg() {
|
||||
return nil, h.ArgErr()
|
||||
}
|
||||
moduleName := h.Val()
|
||||
mod, err := caddy.GetModule("caddy.logging.encoders." + moduleName)
|
||||
if err != nil {
|
||||
return nil, h.Errf("getting log encoder module named '%s': %v", moduleName, err)
|
||||
}
|
||||
unm, ok := mod.New().(caddyfile.Unmarshaler)
|
||||
if !ok {
|
||||
return nil, h.Errf("log encoder module '%s' is not a Caddyfile unmarshaler", mod)
|
||||
}
|
||||
err = unm.UnmarshalCaddyfile(h.NewFromNextSegment())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
enc, ok := unm.(zapcore.Encoder)
|
||||
if !ok {
|
||||
return nil, h.Errf("module %s is not a zapcore.Encoder", mod)
|
||||
}
|
||||
cl.EncoderRaw = caddyconfig.JSONModuleObject(enc, "format", moduleName, h.warnings)
|
||||
|
||||
case "level":
|
||||
if !h.NextArg() {
|
||||
return nil, h.ArgErr()
|
||||
}
|
||||
cl.Level = h.Val()
|
||||
if h.NextArg() {
|
||||
return nil, h.ArgErr()
|
||||
}
|
||||
|
||||
default:
|
||||
return nil, h.Errf("unrecognized subdirective: %s", h.Val())
|
||||
}
|
||||
}
|
||||
|
||||
var val namedCustomLog
|
||||
if !reflect.DeepEqual(cl, new(caddy.CustomLog)) {
|
||||
logCounter, ok := h.State["logCounter"].(int)
|
||||
if !ok {
|
||||
logCounter = 0
|
||||
}
|
||||
cl.Include = []string{"http.log.access"}
|
||||
val.name = fmt.Sprintf("log%d", logCounter)
|
||||
val.log = cl
|
||||
logCounter++
|
||||
h.State["logCounter"] = logCounter
|
||||
}
|
||||
configValues = append(configValues, ConfigValue{
|
||||
Class: "custom_log",
|
||||
Value: val,
|
||||
})
|
||||
}
|
||||
return configValues, nil
|
||||
}
|
||||
@@ -1,400 +0,0 @@
|
||||
// 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 httpcaddyfile
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/caddyserver/caddy/v2"
|
||||
"github.com/caddyserver/caddy/v2/caddyconfig"
|
||||
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
|
||||
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
|
||||
)
|
||||
|
||||
// directiveOrder specifies the order
|
||||
// to apply directives in HTTP routes.
|
||||
var directiveOrder = []string{
|
||||
"redir",
|
||||
"rewrite",
|
||||
|
||||
"root",
|
||||
|
||||
"uri",
|
||||
"try_files",
|
||||
|
||||
// middleware handlers that typically wrap responses
|
||||
"basicauth",
|
||||
"header",
|
||||
"request_header",
|
||||
"encode",
|
||||
"templates",
|
||||
|
||||
"handle",
|
||||
"route",
|
||||
|
||||
// handlers that typically respond to requests
|
||||
"respond",
|
||||
"reverse_proxy",
|
||||
"php_fastcgi",
|
||||
"file_server",
|
||||
}
|
||||
|
||||
// directiveIsOrdered returns true if dir is
|
||||
// a known, ordered (sorted) directive.
|
||||
func directiveIsOrdered(dir string) bool {
|
||||
for _, d := range directiveOrder {
|
||||
if d == dir {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// RegisterDirective registers a unique directive dir with an
|
||||
// associated unmarshaling (setup) function. When directive dir
|
||||
// is encountered in a Caddyfile, setupFunc will be called to
|
||||
// unmarshal its tokens.
|
||||
func RegisterDirective(dir string, setupFunc UnmarshalFunc) {
|
||||
if _, ok := registeredDirectives[dir]; ok {
|
||||
panic("directive " + dir + " already registered")
|
||||
}
|
||||
registeredDirectives[dir] = setupFunc
|
||||
}
|
||||
|
||||
// RegisterHandlerDirective is like RegisterDirective, but for
|
||||
// directives which specifically output only an HTTP handler.
|
||||
// Directives registered with this function will always have
|
||||
// an optional matcher token as the first argument.
|
||||
func RegisterHandlerDirective(dir string, setupFunc UnmarshalHandlerFunc) {
|
||||
RegisterDirective(dir, func(h Helper) ([]ConfigValue, error) {
|
||||
if !h.Next() {
|
||||
return nil, h.ArgErr()
|
||||
}
|
||||
|
||||
matcherSet, ok, err := h.MatcherToken()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if ok {
|
||||
// strip matcher token; we don't need to
|
||||
// use the return value here because a
|
||||
// new dispenser should have been made
|
||||
// solely for this directive's tokens,
|
||||
// with no other uses of same slice
|
||||
h.Dispenser.Delete()
|
||||
}
|
||||
|
||||
h.Dispenser.Reset() // pretend this lookahead never happened
|
||||
val, err := setupFunc(h)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return h.NewRoute(matcherSet, val), nil
|
||||
})
|
||||
}
|
||||
|
||||
// Helper is a type which helps setup a value from
|
||||
// Caddyfile tokens.
|
||||
type Helper struct {
|
||||
*caddyfile.Dispenser
|
||||
// State stores intermediate variables during caddyfile adaptation.
|
||||
State map[string]interface{}
|
||||
options map[string]interface{}
|
||||
warnings *[]caddyconfig.Warning
|
||||
matcherDefs map[string]caddy.ModuleMap
|
||||
parentBlock caddyfile.ServerBlock
|
||||
groupCounter counter
|
||||
}
|
||||
|
||||
// Option gets the option keyed by name.
|
||||
func (h Helper) Option(name string) interface{} {
|
||||
return h.options[name]
|
||||
}
|
||||
|
||||
// Caddyfiles returns the list of config files from
|
||||
// which tokens in the current server block were loaded.
|
||||
func (h Helper) Caddyfiles() []string {
|
||||
// first obtain set of names of files involved
|
||||
// in this server block, without duplicates
|
||||
files := make(map[string]struct{})
|
||||
for _, segment := range h.parentBlock.Segments {
|
||||
for _, token := range segment {
|
||||
files[token.File] = struct{}{}
|
||||
}
|
||||
}
|
||||
// then convert the set into a slice
|
||||
filesSlice := make([]string, 0, len(files))
|
||||
for file := range files {
|
||||
filesSlice = append(filesSlice, file)
|
||||
}
|
||||
return filesSlice
|
||||
}
|
||||
|
||||
// JSON converts val into JSON. Any errors are added to warnings.
|
||||
func (h Helper) JSON(val interface{}) json.RawMessage {
|
||||
return caddyconfig.JSON(val, h.warnings)
|
||||
}
|
||||
|
||||
// MatcherToken assumes the next argument token is (possibly) a matcher,
|
||||
// and if so, returns the matcher set along with a true value. If the next
|
||||
// token is not a matcher, nil and false is returned. Note that a true
|
||||
// value may be returned with a nil matcher set if it is a catch-all.
|
||||
func (h Helper) MatcherToken() (caddy.ModuleMap, bool, error) {
|
||||
if !h.NextArg() {
|
||||
return nil, false, nil
|
||||
}
|
||||
return matcherSetFromMatcherToken(h.Dispenser.Token(), h.matcherDefs, h.warnings)
|
||||
}
|
||||
|
||||
// ExtractMatcherSet is like MatcherToken, except this is a higher-level
|
||||
// method that returns the matcher set described by the matcher token,
|
||||
// or nil if there is none, and deletes the matcher token from the
|
||||
// dispenser and resets it as if this look-ahead never happened. Useful
|
||||
// when wrapping a route (one or more handlers) in a user-defined matcher.
|
||||
func (h Helper) ExtractMatcherSet() (caddy.ModuleMap, error) {
|
||||
matcherSet, hasMatcher, err := h.MatcherToken()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if hasMatcher {
|
||||
h.Dispenser.Delete() // strip matcher token
|
||||
}
|
||||
h.Dispenser.Reset() // pretend this lookahead never happened
|
||||
return matcherSet, nil
|
||||
}
|
||||
|
||||
// NewRoute returns config values relevant to creating a new HTTP route.
|
||||
func (h Helper) NewRoute(matcherSet caddy.ModuleMap,
|
||||
handler caddyhttp.MiddlewareHandler) []ConfigValue {
|
||||
mod, err := caddy.GetModule(caddy.GetModuleID(handler))
|
||||
if err != nil {
|
||||
*h.warnings = append(*h.warnings, caddyconfig.Warning{
|
||||
File: h.File(),
|
||||
Line: h.Line(),
|
||||
Message: err.Error(),
|
||||
})
|
||||
return nil
|
||||
}
|
||||
var matcherSetsRaw []caddy.ModuleMap
|
||||
if matcherSet != nil {
|
||||
matcherSetsRaw = append(matcherSetsRaw, matcherSet)
|
||||
}
|
||||
return []ConfigValue{
|
||||
{
|
||||
Class: "route",
|
||||
Value: caddyhttp.Route{
|
||||
MatcherSetsRaw: matcherSetsRaw,
|
||||
HandlersRaw: []json.RawMessage{caddyconfig.JSONModuleObject(handler, "handler", mod.ID.Name(), h.warnings)},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// GroupRoutes adds the routes (caddyhttp.Route type) in vals to the
|
||||
// same group, if there is more than one route in vals.
|
||||
func (h Helper) GroupRoutes(vals []ConfigValue) {
|
||||
// ensure there's at least two routes; group of one is pointless
|
||||
var count int
|
||||
for _, v := range vals {
|
||||
if _, ok := v.Value.(caddyhttp.Route); ok {
|
||||
count++
|
||||
if count > 1 {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if count < 2 {
|
||||
return
|
||||
}
|
||||
|
||||
// now that we know the group will have some effect, do it
|
||||
groupName := h.groupCounter.nextGroup()
|
||||
for i := range vals {
|
||||
if route, ok := vals[i].Value.(caddyhttp.Route); ok {
|
||||
route.Group = groupName
|
||||
vals[i].Value = route
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// NewBindAddresses returns config values relevant to adding
|
||||
// listener bind addresses to the config.
|
||||
func (h Helper) NewBindAddresses(addrs []string) []ConfigValue {
|
||||
return []ConfigValue{{Class: "bind", Value: addrs}}
|
||||
}
|
||||
|
||||
// ConfigValue represents a value to be added to the final
|
||||
// configuration, or a value to be consulted when building
|
||||
// the final configuration.
|
||||
type ConfigValue struct {
|
||||
// The kind of value this is. As the config is
|
||||
// being built, the adapter will look in the
|
||||
// "pile" for values belonging to a certain
|
||||
// class when it is setting up a certain part
|
||||
// of the config. The associated value will be
|
||||
// type-asserted and placed accordingly.
|
||||
Class string
|
||||
|
||||
// The value to be used when building the config.
|
||||
// Generally its type is associated with the
|
||||
// name of the Class.
|
||||
Value interface{}
|
||||
|
||||
directive string
|
||||
}
|
||||
|
||||
func sortRoutes(routes []ConfigValue) {
|
||||
dirPositions := make(map[string]int)
|
||||
for i, dir := range directiveOrder {
|
||||
dirPositions[dir] = i
|
||||
}
|
||||
|
||||
// while we are sorting, we will need to decode a route's path matcher
|
||||
// in order to sub-sort by path length; we can amortize this operation
|
||||
// for efficiency by storing the decoded matchers in a slice
|
||||
decodedMatchers := make([]caddyhttp.MatchPath, len(routes))
|
||||
|
||||
sort.SliceStable(routes, func(i, j int) bool {
|
||||
iDir, jDir := routes[i].directive, routes[j].directive
|
||||
if iDir == jDir {
|
||||
// directives are the same; sub-sort by path matcher length
|
||||
// if there's only one matcher set and one path (common case)
|
||||
iRoute, ok := routes[i].Value.(caddyhttp.Route)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
jRoute, ok := routes[j].Value.(caddyhttp.Route)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
// use already-decoded matcher, or decode if it's the first time seeing it
|
||||
iPM, jPM := decodedMatchers[i], decodedMatchers[j]
|
||||
if iPM == nil && len(iRoute.MatcherSetsRaw) == 1 {
|
||||
var pathMatcher caddyhttp.MatchPath
|
||||
_ = json.Unmarshal(iRoute.MatcherSetsRaw[0]["path"], &pathMatcher)
|
||||
decodedMatchers[i] = pathMatcher
|
||||
iPM = pathMatcher
|
||||
}
|
||||
if jPM == nil && len(jRoute.MatcherSetsRaw) == 1 {
|
||||
var pathMatcher caddyhttp.MatchPath
|
||||
_ = json.Unmarshal(jRoute.MatcherSetsRaw[0]["path"], &pathMatcher)
|
||||
decodedMatchers[j] = pathMatcher
|
||||
jPM = pathMatcher
|
||||
}
|
||||
|
||||
// sort by longer path (more specific) first; missing
|
||||
// path matchers are treated as zero-length paths
|
||||
var iPathLen, jPathLen int
|
||||
if iPM != nil {
|
||||
iPathLen = len(iPM[0])
|
||||
}
|
||||
if jPM != nil {
|
||||
jPathLen = len(jPM[0])
|
||||
}
|
||||
return iPathLen > jPathLen
|
||||
}
|
||||
|
||||
return dirPositions[iDir] < dirPositions[jDir]
|
||||
})
|
||||
}
|
||||
|
||||
// parseSegmentAsSubroute parses the segment such that its subdirectives
|
||||
// are themselves treated as directives, from which a subroute is built
|
||||
// and returned.
|
||||
func parseSegmentAsSubroute(h Helper) (caddyhttp.MiddlewareHandler, error) {
|
||||
var allResults []ConfigValue
|
||||
|
||||
for h.Next() {
|
||||
// slice the linear list of tokens into top-level segments
|
||||
var segments []caddyfile.Segment
|
||||
for nesting := h.Nesting(); h.NextBlock(nesting); {
|
||||
segments = append(segments, h.NextSegment())
|
||||
}
|
||||
|
||||
// copy existing matcher definitions so we can augment
|
||||
// new ones that are defined only in this scope
|
||||
matcherDefs := make(map[string]caddy.ModuleMap, len(h.matcherDefs))
|
||||
for key, val := range h.matcherDefs {
|
||||
matcherDefs[key] = val
|
||||
}
|
||||
|
||||
// find and extract any embedded matcher definitions in this scope
|
||||
for i, seg := range segments {
|
||||
if strings.HasPrefix(seg.Directive(), matcherPrefix) {
|
||||
err := parseMatcherDefinitions(caddyfile.NewDispenser(seg), matcherDefs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
segments = append(segments[:i], segments[i+1:]...)
|
||||
}
|
||||
}
|
||||
|
||||
// with matchers ready to go, evaluate each directive's segment
|
||||
for _, seg := range segments {
|
||||
dir := seg.Directive()
|
||||
dirFunc, ok := registeredDirectives[dir]
|
||||
if !ok {
|
||||
return nil, h.Errf("unrecognized directive: %s", dir)
|
||||
}
|
||||
|
||||
subHelper := h
|
||||
subHelper.Dispenser = caddyfile.NewDispenser(seg)
|
||||
subHelper.matcherDefs = matcherDefs
|
||||
|
||||
results, err := dirFunc(subHelper)
|
||||
if err != nil {
|
||||
return nil, h.Errf("parsing caddyfile tokens for '%s': %v", dir, err)
|
||||
}
|
||||
for _, result := range results {
|
||||
result.directive = dir
|
||||
allResults = append(allResults, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return buildSubroute(allResults, h.groupCounter)
|
||||
}
|
||||
|
||||
// serverBlock pairs a Caddyfile server block
|
||||
// with a "pile" of config values, keyed by class
|
||||
// name.
|
||||
type serverBlock struct {
|
||||
block caddyfile.ServerBlock
|
||||
pile map[string][]ConfigValue // config values obtained from directives
|
||||
}
|
||||
|
||||
type (
|
||||
// UnmarshalFunc is a function which can unmarshal Caddyfile
|
||||
// tokens into zero or more config values using a Helper type.
|
||||
// These are passed in a call to RegisterDirective.
|
||||
UnmarshalFunc func(h Helper) ([]ConfigValue, error)
|
||||
|
||||
// UnmarshalHandlerFunc is like UnmarshalFunc, except the
|
||||
// output of the unmarshaling is an HTTP handler. This
|
||||
// function does not need to deal with HTTP request matching
|
||||
// which is abstracted away. Since writing HTTP handlers
|
||||
// with Caddyfile support is very common, this is a more
|
||||
// convenient way to add a handler to the chain since a lot
|
||||
// of the details common to HTTP handlers are taken care of
|
||||
// for you. These are passed to a call to
|
||||
// RegisterHandlerDirective.
|
||||
UnmarshalHandlerFunc func(h Helper) (caddyhttp.MiddlewareHandler, error)
|
||||
)
|
||||
|
||||
var registeredDirectives = make(map[string]UnmarshalFunc)
|
||||
@@ -1,938 +0,0 @@
|
||||
// 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 httpcaddyfile
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/caddyserver/caddy/v2"
|
||||
"github.com/caddyserver/caddy/v2/caddyconfig"
|
||||
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
|
||||
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
|
||||
"github.com/caddyserver/caddy/v2/modules/caddytls"
|
||||
)
|
||||
|
||||
func init() {
|
||||
caddyconfig.RegisterAdapter("caddyfile", caddyfile.Adapter{ServerType: ServerType{}})
|
||||
}
|
||||
|
||||
// ServerType can set up a config from an HTTP Caddyfile.
|
||||
type ServerType struct {
|
||||
}
|
||||
|
||||
// Setup makes a config from the tokens.
|
||||
func (st ServerType) Setup(originalServerBlocks []caddyfile.ServerBlock,
|
||||
options map[string]interface{}) (*caddy.Config, []caddyconfig.Warning, error) {
|
||||
var warnings []caddyconfig.Warning
|
||||
gc := counter{new(int)}
|
||||
state := make(map[string]interface{})
|
||||
|
||||
// load all the server blocks and associate them with a "pile"
|
||||
// of config values; also prohibit duplicate keys because they
|
||||
// can make a config confusing if more than one server block is
|
||||
// chosen to handle a request - we actually will make each
|
||||
// server block's route terminal so that only one will run
|
||||
sbKeys := make(map[string]struct{})
|
||||
var serverBlocks []serverBlock
|
||||
for i, sblock := range originalServerBlocks {
|
||||
for j, k := range sblock.Keys {
|
||||
if _, ok := sbKeys[k]; ok {
|
||||
return nil, warnings, fmt.Errorf("duplicate site address not allowed: '%s' in %v (site block %d, key %d)", k, sblock.Keys, i, j)
|
||||
}
|
||||
sbKeys[k] = struct{}{}
|
||||
}
|
||||
serverBlocks = append(serverBlocks, serverBlock{
|
||||
block: sblock,
|
||||
pile: make(map[string][]ConfigValue),
|
||||
})
|
||||
}
|
||||
|
||||
// apply any global options
|
||||
var err error
|
||||
serverBlocks, err = st.evaluateGlobalOptionsBlock(serverBlocks, options)
|
||||
if err != nil {
|
||||
return nil, warnings, err
|
||||
}
|
||||
|
||||
for _, sb := range serverBlocks {
|
||||
// replace shorthand placeholders (which are
|
||||
// convenient when writing a Caddyfile) with
|
||||
// their actual placeholder identifiers or
|
||||
// variable names
|
||||
replacer := strings.NewReplacer(
|
||||
"{dir}", "{http.request.uri.path.dir}",
|
||||
"{file}", "{http.request.uri.path.file}",
|
||||
"{host}", "{http.request.host}",
|
||||
"{hostport}", "{http.request.hostport}",
|
||||
"{method}", "{http.request.method}",
|
||||
"{path}", "{http.request.uri.path}",
|
||||
"{query}", "{http.request.uri.query}",
|
||||
"{remote}", "{http.request.remote}",
|
||||
"{remote_host}", "{http.request.remote.host}",
|
||||
"{remote_port}", "{http.request.remote.port}",
|
||||
"{scheme}", "{http.request.scheme}",
|
||||
"{uri}", "{http.request.uri}",
|
||||
"{tls_cipher}", "{http.request.tls.cipher_suite}",
|
||||
"{tls_version}", "{http.request.tls.version}",
|
||||
"{tls_client_fingerprint}", "{http.request.tls.client.fingerprint}",
|
||||
"{tls_client_issuer}", "{http.request.tls.client.issuer}",
|
||||
"{tls_client_serial}", "{http.request.tls.client.serial}",
|
||||
"{tls_client_subject}", "{http.request.tls.client.subject}",
|
||||
)
|
||||
for _, segment := range sb.block.Segments {
|
||||
for i := 0; i < len(segment); i++ {
|
||||
segment[i].Text = replacer.Replace(segment[i].Text)
|
||||
}
|
||||
}
|
||||
|
||||
if len(sb.block.Keys) == 0 {
|
||||
return nil, warnings, fmt.Errorf("server block without any key is global configuration, and if used, it must be first")
|
||||
}
|
||||
|
||||
// extract matcher definitions
|
||||
matcherDefs := make(map[string]caddy.ModuleMap)
|
||||
for _, segment := range sb.block.Segments {
|
||||
if dir := segment.Directive(); strings.HasPrefix(dir, matcherPrefix) {
|
||||
d := sb.block.DispenseDirective(dir)
|
||||
err := parseMatcherDefinitions(d, matcherDefs)
|
||||
if err != nil {
|
||||
return nil, warnings, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// evaluate each directive ("segment") in this block
|
||||
for _, segment := range sb.block.Segments {
|
||||
dir := segment.Directive()
|
||||
|
||||
if strings.HasPrefix(dir, matcherPrefix) {
|
||||
// matcher definitions were pre-processed
|
||||
continue
|
||||
}
|
||||
|
||||
dirFunc, ok := registeredDirectives[dir]
|
||||
if !ok {
|
||||
tkn := segment[0]
|
||||
return nil, warnings, fmt.Errorf("%s:%d: unrecognized directive: %s", tkn.File, tkn.Line, dir)
|
||||
}
|
||||
|
||||
h := Helper{
|
||||
Dispenser: caddyfile.NewDispenser(segment),
|
||||
options: options,
|
||||
warnings: &warnings,
|
||||
matcherDefs: matcherDefs,
|
||||
parentBlock: sb.block,
|
||||
groupCounter: gc,
|
||||
State: state,
|
||||
}
|
||||
|
||||
results, err := dirFunc(h)
|
||||
if err != nil {
|
||||
return nil, warnings, fmt.Errorf("parsing caddyfile tokens for '%s': %v", dir, err)
|
||||
}
|
||||
for _, result := range results {
|
||||
result.directive = dir
|
||||
sb.pile[result.Class] = append(sb.pile[result.Class], result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// map
|
||||
sbmap, err := st.mapAddressToServerBlocks(serverBlocks, options)
|
||||
if err != nil {
|
||||
return nil, warnings, err
|
||||
}
|
||||
|
||||
// reduce
|
||||
pairings := st.consolidateAddrMappings(sbmap)
|
||||
|
||||
// each pairing of listener addresses to list of server
|
||||
// blocks is basically a server definition
|
||||
servers, err := st.serversFromPairings(pairings, options, &warnings, gc)
|
||||
if err != nil {
|
||||
return nil, warnings, err
|
||||
}
|
||||
|
||||
// now that each server is configured, make the HTTP app
|
||||
httpApp := caddyhttp.App{
|
||||
HTTPPort: tryInt(options["http_port"], &warnings),
|
||||
HTTPSPort: tryInt(options["https_port"], &warnings),
|
||||
Servers: servers,
|
||||
}
|
||||
|
||||
// then make the TLS app
|
||||
tlsApp, warnings, err := st.buildTLSApp(pairings, options, warnings)
|
||||
if err != nil {
|
||||
return nil, warnings, err
|
||||
}
|
||||
|
||||
// if experimental HTTP/3 is enabled, enable it on each server
|
||||
if enableH3, ok := options["experimental_http3"].(bool); ok && enableH3 {
|
||||
for _, srv := range httpApp.Servers {
|
||||
srv.ExperimentalHTTP3 = true
|
||||
}
|
||||
}
|
||||
|
||||
// extract any custom logs, and enforce configured levels
|
||||
var customLogs []namedCustomLog
|
||||
var hasDefaultLog bool
|
||||
for _, sb := range serverBlocks {
|
||||
for _, clVal := range sb.pile["custom_log"] {
|
||||
ncl := clVal.Value.(namedCustomLog)
|
||||
if ncl.name == "" {
|
||||
continue
|
||||
}
|
||||
if ncl.name == "default" {
|
||||
hasDefaultLog = true
|
||||
}
|
||||
if _, ok := options["debug"]; ok && ncl.log.Level == "" {
|
||||
ncl.log.Level = "DEBUG"
|
||||
}
|
||||
customLogs = append(customLogs, ncl)
|
||||
}
|
||||
}
|
||||
if !hasDefaultLog {
|
||||
// if the default log was not customized, ensure we
|
||||
// configure it with any applicable options
|
||||
if _, ok := options["debug"]; ok {
|
||||
customLogs = append(customLogs, namedCustomLog{
|
||||
name: "default",
|
||||
log: &caddy.CustomLog{Level: "DEBUG"},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// annnd the top-level config, then we're done!
|
||||
cfg := &caddy.Config{AppsRaw: make(caddy.ModuleMap)}
|
||||
if len(httpApp.Servers) > 0 {
|
||||
cfg.AppsRaw["http"] = caddyconfig.JSON(httpApp, &warnings)
|
||||
}
|
||||
if !reflect.DeepEqual(tlsApp, &caddytls.TLS{CertificatesRaw: make(caddy.ModuleMap)}) {
|
||||
cfg.AppsRaw["tls"] = caddyconfig.JSON(tlsApp, &warnings)
|
||||
}
|
||||
if storageCvtr, ok := options["storage"].(caddy.StorageConverter); ok {
|
||||
cfg.StorageRaw = caddyconfig.JSONModuleObject(storageCvtr,
|
||||
"module",
|
||||
storageCvtr.(caddy.Module).CaddyModule().ID.Name(),
|
||||
&warnings)
|
||||
}
|
||||
if adminConfig, ok := options["admin"].(string); ok && adminConfig != "" {
|
||||
if adminConfig == "off" {
|
||||
cfg.Admin = &caddy.AdminConfig{Disabled: true}
|
||||
} else {
|
||||
cfg.Admin = &caddy.AdminConfig{Listen: adminConfig}
|
||||
}
|
||||
}
|
||||
if len(customLogs) > 0 {
|
||||
if cfg.Logging == nil {
|
||||
cfg.Logging = &caddy.Logging{
|
||||
Logs: make(map[string]*caddy.CustomLog),
|
||||
}
|
||||
}
|
||||
for _, ncl := range customLogs {
|
||||
if ncl.name != "" {
|
||||
cfg.Logging.Logs[ncl.name] = ncl.log
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(customLogs) > 0 {
|
||||
if cfg.Logging == nil {
|
||||
cfg.Logging = &caddy.Logging{
|
||||
Logs: make(map[string]*caddy.CustomLog),
|
||||
}
|
||||
}
|
||||
for _, ncl := range customLogs {
|
||||
if ncl.name != "" {
|
||||
cfg.Logging.Logs[ncl.name] = ncl.log
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return cfg, warnings, nil
|
||||
}
|
||||
|
||||
// evaluateGlobalOptionsBlock evaluates the global options block,
|
||||
// which is expected to be the first server block if it has zero
|
||||
// keys. It returns the updated list of server blocks with the
|
||||
// global options block removed, and updates options accordingly.
|
||||
func (ServerType) evaluateGlobalOptionsBlock(serverBlocks []serverBlock, options map[string]interface{}) ([]serverBlock, error) {
|
||||
if len(serverBlocks) == 0 || len(serverBlocks[0].block.Keys) > 0 {
|
||||
return serverBlocks, nil
|
||||
}
|
||||
|
||||
for _, segment := range serverBlocks[0].block.Segments {
|
||||
dir := segment.Directive()
|
||||
var val interface{}
|
||||
var err error
|
||||
disp := caddyfile.NewDispenser(segment)
|
||||
switch dir {
|
||||
case "debug":
|
||||
val = true
|
||||
case "http_port":
|
||||
val, err = parseOptHTTPPort(disp)
|
||||
case "https_port":
|
||||
val, err = parseOptHTTPSPort(disp)
|
||||
case "default_sni":
|
||||
val, err = parseOptSingleString(disp)
|
||||
case "order":
|
||||
val, err = parseOptOrder(disp)
|
||||
case "experimental_http3":
|
||||
val, err = parseOptExperimentalHTTP3(disp)
|
||||
case "storage":
|
||||
val, err = parseOptStorage(disp)
|
||||
case "acme_ca", "acme_dns", "acme_ca_root":
|
||||
val, err = parseOptSingleString(disp)
|
||||
case "email":
|
||||
val, err = parseOptSingleString(disp)
|
||||
case "admin":
|
||||
val, err = parseOptAdmin(disp)
|
||||
case "on_demand_tls":
|
||||
val, err = parseOptOnDemand(disp)
|
||||
case "local_certs":
|
||||
val = true
|
||||
default:
|
||||
return nil, fmt.Errorf("unrecognized parameter name: %s", dir)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s: %v", dir, err)
|
||||
}
|
||||
options[dir] = val
|
||||
}
|
||||
|
||||
return serverBlocks[1:], nil
|
||||
}
|
||||
|
||||
// hostsFromServerBlockKeys returns a list of all the non-empty hostnames
|
||||
// found in the keys of the server block sb. If sb has a key that omits
|
||||
// the hostname (i.e. is a catch-all/empty host), then the returned list
|
||||
// is empty, because the server block effectively matches ALL hosts.
|
||||
// The list may not be in a consistent order.
|
||||
func (st *ServerType) hostsFromServerBlockKeys(sb caddyfile.ServerBlock) ([]string, error) {
|
||||
// first get each unique hostname
|
||||
hostMap := make(map[string]struct{})
|
||||
for _, sblockKey := range sb.Keys {
|
||||
addr, err := ParseAddress(sblockKey)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parsing server block key: %v", err)
|
||||
}
|
||||
addr = addr.Normalize()
|
||||
if addr.Host == "" {
|
||||
// server block contains a key like ":443", i.e. the host portion
|
||||
// is empty / catch-all, which means to match all hosts
|
||||
return []string{}, nil
|
||||
}
|
||||
hostMap[addr.Host] = struct{}{}
|
||||
}
|
||||
|
||||
// convert map to slice
|
||||
sblockHosts := make([]string, 0, len(hostMap))
|
||||
for host := range hostMap {
|
||||
sblockHosts = append(sblockHosts, host)
|
||||
}
|
||||
|
||||
return sblockHosts, nil
|
||||
}
|
||||
|
||||
// serversFromPairings creates the servers for each pairing of addresses
|
||||
// to server blocks. Each pairing is essentially a server definition.
|
||||
func (st *ServerType) serversFromPairings(
|
||||
pairings []sbAddrAssociation,
|
||||
options map[string]interface{},
|
||||
warnings *[]caddyconfig.Warning,
|
||||
groupCounter counter,
|
||||
) (map[string]*caddyhttp.Server, error) {
|
||||
servers := make(map[string]*caddyhttp.Server)
|
||||
defaultSNI := tryString(options["default_sni"], warnings)
|
||||
|
||||
for i, p := range pairings {
|
||||
srv := &caddyhttp.Server{
|
||||
Listen: p.addresses,
|
||||
}
|
||||
|
||||
// sort server blocks by their keys; this is important because
|
||||
// only the first matching site should be evaluated, and we should
|
||||
// attempt to match most specific site first (host and path), in
|
||||
// case their matchers overlap; we do this somewhat naively by
|
||||
// descending sort by length of host then path
|
||||
sort.SliceStable(p.serverBlocks, func(i, j int) bool {
|
||||
// TODO: we could pre-process the specificities for efficiency,
|
||||
// but I don't expect many blocks will have SO many keys...
|
||||
var iLongestPath, jLongestPath string
|
||||
var iLongestHost, jLongestHost string
|
||||
for _, key := range p.serverBlocks[i].block.Keys {
|
||||
addr, _ := ParseAddress(key)
|
||||
if specificity(addr.Host) > specificity(iLongestHost) {
|
||||
iLongestHost = addr.Host
|
||||
}
|
||||
if specificity(addr.Path) > specificity(iLongestPath) {
|
||||
iLongestPath = addr.Path
|
||||
}
|
||||
}
|
||||
for _, key := range p.serverBlocks[j].block.Keys {
|
||||
addr, _ := ParseAddress(key)
|
||||
if specificity(addr.Host) > specificity(jLongestHost) {
|
||||
jLongestHost = addr.Host
|
||||
}
|
||||
if specificity(addr.Path) > specificity(jLongestPath) {
|
||||
jLongestPath = addr.Path
|
||||
}
|
||||
}
|
||||
if specificity(iLongestHost) == specificity(jLongestHost) {
|
||||
return len(iLongestPath) > len(jLongestPath)
|
||||
}
|
||||
return specificity(iLongestHost) > specificity(jLongestHost)
|
||||
})
|
||||
|
||||
var hasCatchAllTLSConnPolicy bool
|
||||
|
||||
// create a subroute for each site in the server block
|
||||
for _, sblock := range p.serverBlocks {
|
||||
matcherSetsEnc, err := st.compileEncodedMatcherSets(sblock.block)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("server block %v: compiling matcher sets: %v", sblock.block.Keys, err)
|
||||
}
|
||||
|
||||
hosts, err := st.hostsFromServerBlockKeys(sblock.block)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// tls: connection policies
|
||||
if cpVals, ok := sblock.pile["tls.connection_policy"]; ok {
|
||||
// tls connection policies
|
||||
for _, cpVal := range cpVals {
|
||||
cp := cpVal.Value.(*caddytls.ConnectionPolicy)
|
||||
|
||||
// make sure the policy covers all hostnames from the block
|
||||
for _, h := range hosts {
|
||||
if h == defaultSNI {
|
||||
hosts = append(hosts, "")
|
||||
cp.DefaultSNI = defaultSNI
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if len(hosts) > 0 {
|
||||
cp.MatchersRaw = caddy.ModuleMap{
|
||||
"sni": caddyconfig.JSON(hosts, warnings), // make sure to match all hosts, not just auto-HTTPS-qualified ones
|
||||
}
|
||||
} else {
|
||||
cp.DefaultSNI = defaultSNI
|
||||
hasCatchAllTLSConnPolicy = true
|
||||
}
|
||||
|
||||
srv.TLSConnPolicies = append(srv.TLSConnPolicies, cp)
|
||||
}
|
||||
}
|
||||
|
||||
// exclude any hosts that were defined explicitly with
|
||||
// "http://" in the key from automated cert management (issue #2998)
|
||||
for _, key := range sblock.block.Keys {
|
||||
addr, err := ParseAddress(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
addr = addr.Normalize()
|
||||
if addr.Scheme == "http" && addr.Host != "" {
|
||||
if srv.AutoHTTPS == nil {
|
||||
srv.AutoHTTPS = new(caddyhttp.AutoHTTPSConfig)
|
||||
}
|
||||
if !sliceContains(srv.AutoHTTPS.Skip, addr.Host) {
|
||||
srv.AutoHTTPS.Skip = append(srv.AutoHTTPS.Skip, addr.Host)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// set up each handler directive, making sure to honor directive order
|
||||
dirRoutes := sblock.pile["route"]
|
||||
siteSubroute, err := buildSubroute(dirRoutes, groupCounter)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// add the site block's route(s) to the server
|
||||
srv.Routes = appendSubrouteToRouteList(srv.Routes, siteSubroute, matcherSetsEnc, p, warnings)
|
||||
|
||||
// if error routes are defined, add those too
|
||||
if errorSubrouteVals, ok := sblock.pile["error_route"]; ok {
|
||||
if srv.Errors == nil {
|
||||
srv.Errors = new(caddyhttp.HTTPErrorConfig)
|
||||
}
|
||||
for _, val := range errorSubrouteVals {
|
||||
sr := val.Value.(*caddyhttp.Subroute)
|
||||
srv.Errors.Routes = appendSubrouteToRouteList(srv.Errors.Routes, sr, matcherSetsEnc, p, warnings)
|
||||
}
|
||||
}
|
||||
|
||||
// add log associations
|
||||
for _, cval := range sblock.pile["custom_log"] {
|
||||
ncl := cval.Value.(namedCustomLog)
|
||||
if srv.Logs == nil {
|
||||
srv.Logs = &caddyhttp.ServerLogConfig{
|
||||
LoggerNames: make(map[string]string),
|
||||
}
|
||||
}
|
||||
hosts, err := st.hostsFromServerBlockKeys(sblock.block)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, h := range hosts {
|
||||
if ncl.name != "" {
|
||||
srv.Logs.LoggerNames[h] = ncl.name
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// a catch-all TLS conn policy is necessary to ensure TLS can
|
||||
// be offered to all hostnames of the server; even though only
|
||||
// one policy is needed to enable TLS for the server, that
|
||||
// policy might apply to only certain TLS handshakes; but when
|
||||
// using the Caddyfile, user would expect all handshakes to at
|
||||
// least have a matching connection policy, so here we append a
|
||||
// catch-all/default policy if there isn't one already (it's
|
||||
// important that it goes at the end) - see issue #3004:
|
||||
// https://github.com/caddyserver/caddy/issues/3004
|
||||
// TODO: maybe a smarter way to handle this might be to just make the
|
||||
// auto-HTTPS logic at provision-time detect if there is any connection
|
||||
// policy missing for any HTTPS-enabled hosts, if so, add it... maybe?
|
||||
if !hasCatchAllTLSConnPolicy && (len(srv.TLSConnPolicies) > 0 || defaultSNI != "") {
|
||||
srv.TLSConnPolicies = append(srv.TLSConnPolicies, &caddytls.ConnectionPolicy{DefaultSNI: defaultSNI})
|
||||
}
|
||||
|
||||
// tidy things up a bit
|
||||
srv.TLSConnPolicies = consolidateConnPolicies(srv.TLSConnPolicies)
|
||||
srv.Routes = consolidateRoutes(srv.Routes)
|
||||
|
||||
servers[fmt.Sprintf("srv%d", i)] = srv
|
||||
}
|
||||
|
||||
return servers, nil
|
||||
}
|
||||
|
||||
// consolidateConnPolicies combines TLS connection policies that are the same,
|
||||
// for a cleaner overall output.
|
||||
func consolidateConnPolicies(cps caddytls.ConnectionPolicies) caddytls.ConnectionPolicies {
|
||||
for i := 0; i < len(cps); i++ {
|
||||
for j := 0; j < len(cps); j++ {
|
||||
if j == i {
|
||||
continue
|
||||
}
|
||||
|
||||
// if they're exactly equal in every way, just keep one of them
|
||||
if reflect.DeepEqual(cps[i], cps[j]) {
|
||||
cps = append(cps[:j], cps[j+1:]...)
|
||||
i--
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return cps
|
||||
}
|
||||
|
||||
// appendSubrouteToRouteList appends the routes in subroute
|
||||
// to the routeList, optionally qualified by matchers.
|
||||
func appendSubrouteToRouteList(routeList caddyhttp.RouteList,
|
||||
subroute *caddyhttp.Subroute,
|
||||
matcherSetsEnc []caddy.ModuleMap,
|
||||
p sbAddrAssociation,
|
||||
warnings *[]caddyconfig.Warning) caddyhttp.RouteList {
|
||||
if len(matcherSetsEnc) == 0 && len(p.serverBlocks) == 1 {
|
||||
// no need to wrap the handlers in a subroute if this is
|
||||
// the only server block and there is no matcher for it
|
||||
routeList = append(routeList, subroute.Routes...)
|
||||
} else {
|
||||
routeList = append(routeList, caddyhttp.Route{
|
||||
MatcherSetsRaw: matcherSetsEnc,
|
||||
HandlersRaw: []json.RawMessage{
|
||||
caddyconfig.JSONModuleObject(subroute, "handler", "subroute", warnings),
|
||||
},
|
||||
Terminal: true, // only first matching site block should be evaluated
|
||||
})
|
||||
}
|
||||
return routeList
|
||||
}
|
||||
|
||||
// buildSubroute turns the config values, which are expected to be routes
|
||||
// into a clean and orderly subroute that has all the routes within it.
|
||||
func buildSubroute(routes []ConfigValue, groupCounter counter) (*caddyhttp.Subroute, error) {
|
||||
for _, val := range routes {
|
||||
if !directiveIsOrdered(val.directive) {
|
||||
return nil, fmt.Errorf("directive '%s' is not ordered, so it cannot be used here", val.directive)
|
||||
}
|
||||
}
|
||||
|
||||
sortRoutes(routes)
|
||||
|
||||
subroute := new(caddyhttp.Subroute)
|
||||
|
||||
// some directives are mutually exclusive (only first matching
|
||||
// instance should be evaluated); this is done by putting their
|
||||
// routes in the same group
|
||||
mutuallyExclusiveDirs := map[string]*struct {
|
||||
count int
|
||||
groupName string
|
||||
}{
|
||||
// as a special case, group rewrite directives so that they are mutually exclusive;
|
||||
// this means that only the first matching rewrite will be evaluated, and that's
|
||||
// probably a good thing, since there should never be a need to do more than one
|
||||
// rewrite (I think?), and cascading rewrites smell bad... imagine these rewrites:
|
||||
// rewrite /docs/json/* /docs/json/index.html
|
||||
// rewrite /docs/* /docs/index.html
|
||||
// (We use this on the Caddy website, or at least we did once.) The first rewrite's
|
||||
// result is also matched by the second rewrite, making the first rewrite pointless.
|
||||
// See issue #2959.
|
||||
"rewrite": {},
|
||||
|
||||
// handle blocks are also mutually exclusive by definition
|
||||
"handle": {},
|
||||
|
||||
// root just sets a variable, so if it was not mutually exclusive, intersecting
|
||||
// root directives would overwrite previously-matched ones; they should not cascade
|
||||
"root": {},
|
||||
}
|
||||
for meDir, info := range mutuallyExclusiveDirs {
|
||||
// see how many instances of the directive there are
|
||||
for _, r := range routes {
|
||||
if r.directive == meDir {
|
||||
info.count++
|
||||
if info.count > 1 {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
// if there is more than one, put them in a group
|
||||
// (special case: "rewrite" directive must always be in
|
||||
// its own group--even if there is only one--because we
|
||||
// do not want a rewrite to be consolidated into other
|
||||
// adjacent routes that happen to have the same matcher,
|
||||
// see caddyserver/caddy#3108 - because the implied
|
||||
// intent of rewrite is to do an internal redirect,
|
||||
// we can't assume that the request will continue to
|
||||
// match the same matcher; anyway, giving a route a
|
||||
// unique group name should keep it from consolidating)
|
||||
if info.count > 1 || meDir == "rewrite" {
|
||||
info.groupName = groupCounter.nextGroup()
|
||||
}
|
||||
}
|
||||
|
||||
// add all the routes piled in from directives
|
||||
for _, r := range routes {
|
||||
// put this route into a group if it is mutually exclusive
|
||||
if info, ok := mutuallyExclusiveDirs[r.directive]; ok {
|
||||
route := r.Value.(caddyhttp.Route)
|
||||
route.Group = info.groupName
|
||||
r.Value = route
|
||||
}
|
||||
|
||||
switch route := r.Value.(type) {
|
||||
case caddyhttp.Subroute:
|
||||
// if a route-class config value is actually a Subroute handler
|
||||
// with nothing but a list of routes, then it is the intention
|
||||
// of the directive to keep these handlers together and in this
|
||||
// same order, but not necessarily in a subroute (if it wanted
|
||||
// to keep them in a subroute, the directive would have returned
|
||||
// a route with a Subroute as its handler); this is useful to
|
||||
// keep multiple handlers/routes together and in the same order
|
||||
// so that the sorting procedure we did above doesn't reorder them
|
||||
if route.Errors != nil {
|
||||
// if error handlers are also set, this is confusing; it's
|
||||
// probably supposed to be wrapped in a Route and encoded
|
||||
// as a regular handler route... programmer error.
|
||||
panic("found subroute with more than just routes; perhaps it should have been wrapped in a route?")
|
||||
}
|
||||
subroute.Routes = append(subroute.Routes, route.Routes...)
|
||||
case caddyhttp.Route:
|
||||
subroute.Routes = append(subroute.Routes, route)
|
||||
}
|
||||
}
|
||||
|
||||
subroute.Routes = consolidateRoutes(subroute.Routes)
|
||||
|
||||
return subroute, nil
|
||||
}
|
||||
|
||||
// consolidateRoutes combines routes with the same properties
|
||||
// (same matchers, same Terminal and Group settings) for a
|
||||
// cleaner overall output.
|
||||
func consolidateRoutes(routes caddyhttp.RouteList) caddyhttp.RouteList {
|
||||
for i := 0; i < len(routes)-1; i++ {
|
||||
if reflect.DeepEqual(routes[i].MatcherSetsRaw, routes[i+1].MatcherSetsRaw) &&
|
||||
routes[i].Terminal == routes[i+1].Terminal &&
|
||||
routes[i].Group == routes[i+1].Group {
|
||||
// keep the handlers in the same order, then splice out repetitive route
|
||||
routes[i].HandlersRaw = append(routes[i].HandlersRaw, routes[i+1].HandlersRaw...)
|
||||
routes = append(routes[:i+1], routes[i+2:]...)
|
||||
i--
|
||||
}
|
||||
}
|
||||
return routes
|
||||
}
|
||||
|
||||
func matcherSetFromMatcherToken(
|
||||
tkn caddyfile.Token,
|
||||
matcherDefs map[string]caddy.ModuleMap,
|
||||
warnings *[]caddyconfig.Warning,
|
||||
) (caddy.ModuleMap, bool, error) {
|
||||
// matcher tokens can be wildcards, simple path matchers,
|
||||
// or refer to a pre-defined matcher by some name
|
||||
if tkn.Text == "*" {
|
||||
// match all requests == no matchers, so nothing to do
|
||||
return nil, true, nil
|
||||
} else if strings.HasPrefix(tkn.Text, "/") {
|
||||
// convenient way to specify a single path match
|
||||
return caddy.ModuleMap{
|
||||
"path": caddyconfig.JSON(caddyhttp.MatchPath{tkn.Text}, warnings),
|
||||
}, true, nil
|
||||
} else if strings.HasPrefix(tkn.Text, matcherPrefix) {
|
||||
// pre-defined matcher
|
||||
m, ok := matcherDefs[tkn.Text]
|
||||
if !ok {
|
||||
return nil, false, fmt.Errorf("unrecognized matcher name: %+v", tkn.Text)
|
||||
}
|
||||
return m, true, nil
|
||||
}
|
||||
return nil, false, nil
|
||||
}
|
||||
|
||||
func (st *ServerType) compileEncodedMatcherSets(sblock caddyfile.ServerBlock) ([]caddy.ModuleMap, error) {
|
||||
type hostPathPair struct {
|
||||
hostm caddyhttp.MatchHost
|
||||
pathm caddyhttp.MatchPath
|
||||
}
|
||||
|
||||
// keep routes with common host and path matchers together
|
||||
var matcherPairs []*hostPathPair
|
||||
|
||||
var catchAllHosts bool
|
||||
for _, key := range sblock.Keys {
|
||||
addr, err := ParseAddress(key)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("server block %v: parsing and standardizing address '%s': %v", sblock.Keys, key, err)
|
||||
}
|
||||
addr = addr.Normalize()
|
||||
|
||||
// choose a matcher pair that should be shared by this
|
||||
// server block; if none exists yet, create one
|
||||
var chosenMatcherPair *hostPathPair
|
||||
for _, mp := range matcherPairs {
|
||||
if (len(mp.pathm) == 0 && addr.Path == "") ||
|
||||
(len(mp.pathm) == 1 && mp.pathm[0] == addr.Path) {
|
||||
chosenMatcherPair = mp
|
||||
break
|
||||
}
|
||||
}
|
||||
if chosenMatcherPair == nil {
|
||||
chosenMatcherPair = new(hostPathPair)
|
||||
if addr.Path != "" {
|
||||
chosenMatcherPair.pathm = []string{addr.Path}
|
||||
}
|
||||
matcherPairs = append(matcherPairs, chosenMatcherPair)
|
||||
}
|
||||
|
||||
// if one of the keys has no host (i.e. is a catch-all for
|
||||
// any hostname), then we need to null out the host matcher
|
||||
// entirely so that it matches all hosts
|
||||
if addr.Host == "" && !catchAllHosts {
|
||||
chosenMatcherPair.hostm = nil
|
||||
catchAllHosts = true
|
||||
}
|
||||
if catchAllHosts {
|
||||
continue
|
||||
}
|
||||
|
||||
// add this server block's keys to the matcher
|
||||
// pair if it doesn't already exist
|
||||
if addr.Host != "" {
|
||||
var found bool
|
||||
for _, h := range chosenMatcherPair.hostm {
|
||||
if h == addr.Host {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
chosenMatcherPair.hostm = append(chosenMatcherPair.hostm, addr.Host)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// iterate each pairing of host and path matchers and
|
||||
// put them into a map for JSON encoding
|
||||
var matcherSets []map[string]caddyhttp.RequestMatcher
|
||||
for _, mp := range matcherPairs {
|
||||
matcherSet := make(map[string]caddyhttp.RequestMatcher)
|
||||
if len(mp.hostm) > 0 {
|
||||
matcherSet["host"] = mp.hostm
|
||||
}
|
||||
if len(mp.pathm) > 0 {
|
||||
matcherSet["path"] = mp.pathm
|
||||
}
|
||||
if len(matcherSet) > 0 {
|
||||
matcherSets = append(matcherSets, matcherSet)
|
||||
}
|
||||
}
|
||||
|
||||
// finally, encode each of the matcher sets
|
||||
var matcherSetsEnc []caddy.ModuleMap
|
||||
for _, ms := range matcherSets {
|
||||
msEncoded, err := encodeMatcherSet(ms)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("server block %v: %v", sblock.Keys, err)
|
||||
}
|
||||
matcherSetsEnc = append(matcherSetsEnc, msEncoded)
|
||||
}
|
||||
|
||||
return matcherSetsEnc, nil
|
||||
}
|
||||
|
||||
func parseMatcherDefinitions(d *caddyfile.Dispenser, matchers map[string]caddy.ModuleMap) error {
|
||||
for d.Next() {
|
||||
definitionName := d.Val()
|
||||
|
||||
if _, ok := matchers[definitionName]; ok {
|
||||
return fmt.Errorf("matcher is defined more than once: %s", definitionName)
|
||||
}
|
||||
matchers[definitionName] = make(caddy.ModuleMap)
|
||||
|
||||
// in case there are multiple instances of the same matcher, concatenate
|
||||
// their tokens (we expect that UnmarshalCaddyfile should be able to
|
||||
// handle more than one segment); otherwise, we'd overwrite other
|
||||
// instances of the matcher in this set
|
||||
tokensByMatcherName := make(map[string][]caddyfile.Token)
|
||||
for nesting := d.Nesting(); d.NextBlock(nesting); {
|
||||
matcherName := d.Val()
|
||||
tokensByMatcherName[matcherName] = append(tokensByMatcherName[matcherName], d.NextSegment()...)
|
||||
}
|
||||
for matcherName, tokens := range tokensByMatcherName {
|
||||
mod, err := caddy.GetModule("http.matchers." + matcherName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("getting matcher module '%s': %v", matcherName, err)
|
||||
}
|
||||
unm, ok := mod.New().(caddyfile.Unmarshaler)
|
||||
if !ok {
|
||||
return fmt.Errorf("matcher module '%s' is not a Caddyfile unmarshaler", matcherName)
|
||||
}
|
||||
err = unm.UnmarshalCaddyfile(caddyfile.NewDispenser(tokens))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rm, ok := unm.(caddyhttp.RequestMatcher)
|
||||
if !ok {
|
||||
return fmt.Errorf("matcher module '%s' is not a request matcher", matcherName)
|
||||
}
|
||||
matchers[definitionName][matcherName] = caddyconfig.JSON(rm, nil)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func encodeMatcherSet(matchers map[string]caddyhttp.RequestMatcher) (caddy.ModuleMap, error) {
|
||||
msEncoded := make(caddy.ModuleMap)
|
||||
for matcherName, val := range matchers {
|
||||
jsonBytes, err := json.Marshal(val)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("marshaling matcher set %#v: %v", matchers, err)
|
||||
}
|
||||
msEncoded[matcherName] = jsonBytes
|
||||
}
|
||||
return msEncoded, nil
|
||||
}
|
||||
|
||||
// tryInt tries to convert val to an integer. If it fails,
|
||||
// it downgrades the error to a warning and returns 0.
|
||||
func tryInt(val interface{}, warnings *[]caddyconfig.Warning) int {
|
||||
intVal, ok := val.(int)
|
||||
if val != nil && !ok && warnings != nil {
|
||||
*warnings = append(*warnings, caddyconfig.Warning{Message: "not an integer type"})
|
||||
}
|
||||
return intVal
|
||||
}
|
||||
|
||||
func tryString(val interface{}, warnings *[]caddyconfig.Warning) string {
|
||||
stringVal, ok := val.(string)
|
||||
if val != nil && !ok && warnings != nil {
|
||||
*warnings = append(*warnings, caddyconfig.Warning{Message: "not a string type"})
|
||||
}
|
||||
return stringVal
|
||||
}
|
||||
|
||||
// sliceContains returns true if needle is in haystack.
|
||||
func sliceContains(haystack []string, needle string) bool {
|
||||
for _, s := range haystack {
|
||||
if s == needle {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// specificity returns len(s) minus any wildcards (*) and
|
||||
// placeholders ({...}). Basically, it's a length count
|
||||
// that penalizes the use of wildcards and placeholders.
|
||||
// This is useful for comparing hostnames and paths.
|
||||
// However, wildcards in paths are not a sure answer to
|
||||
// the question of specificity. For example,
|
||||
// '*.example.com' is clearly less specific than
|
||||
// 'a.example.com', but is '/a' more or less specific
|
||||
// than '/a*'?
|
||||
func specificity(s string) int {
|
||||
l := len(s) - strings.Count(s, "*")
|
||||
for len(s) > 0 {
|
||||
start := strings.Index(s, "{")
|
||||
if start < 0 {
|
||||
return l
|
||||
}
|
||||
end := strings.Index(s[start:], "}") + start + 1
|
||||
if end <= start {
|
||||
return l
|
||||
}
|
||||
l -= end - start
|
||||
s = s[end:]
|
||||
}
|
||||
return l
|
||||
}
|
||||
|
||||
type counter struct {
|
||||
n *int
|
||||
}
|
||||
|
||||
func (c counter) nextGroup() string {
|
||||
name := fmt.Sprintf("group%d", *c.n)
|
||||
*c.n++
|
||||
return name
|
||||
}
|
||||
|
||||
type namedCustomLog struct {
|
||||
name string
|
||||
log *caddy.CustomLog
|
||||
}
|
||||
|
||||
// sbAddrAssociation is a mapping from a list of
|
||||
// addresses to a list of server blocks that are
|
||||
// served on those addresses.
|
||||
type sbAddrAssociation struct {
|
||||
addresses []string
|
||||
serverBlocks []serverBlock
|
||||
}
|
||||
|
||||
const matcherPrefix = "@"
|
||||
|
||||
// Interface guard
|
||||
var _ caddyfile.ServerType = (*ServerType)(nil)
|
||||
@@ -1,149 +0,0 @@
|
||||
package httpcaddyfile
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
|
||||
)
|
||||
|
||||
func TestServerType(t *testing.T) {
|
||||
for i, tc := range []struct {
|
||||
input string
|
||||
expectWarn bool
|
||||
expectError bool
|
||||
}{
|
||||
{
|
||||
input: `http://localhost
|
||||
@debug {
|
||||
query showdebug=1
|
||||
}
|
||||
`,
|
||||
expectWarn: false,
|
||||
expectError: false,
|
||||
},
|
||||
{
|
||||
input: `http://localhost
|
||||
@debug {
|
||||
query bad format
|
||||
}
|
||||
`,
|
||||
expectWarn: false,
|
||||
expectError: true,
|
||||
},
|
||||
} {
|
||||
|
||||
adapter := caddyfile.Adapter{
|
||||
ServerType: ServerType{},
|
||||
}
|
||||
|
||||
_, warnings, err := adapter.Adapt([]byte(tc.input), nil)
|
||||
|
||||
if len(warnings) > 0 != tc.expectWarn {
|
||||
t.Errorf("Test %d warning expectation failed Expected: %v, got %v", i, tc.expectWarn, warnings)
|
||||
continue
|
||||
}
|
||||
|
||||
if err != nil != tc.expectError {
|
||||
t.Errorf("Test %d error expectation failed Expected: %v, got %s", i, tc.expectError, err)
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSpecificity(t *testing.T) {
|
||||
for i, tc := range []struct {
|
||||
input string
|
||||
expect int
|
||||
}{
|
||||
{"", 0},
|
||||
{"*", 0},
|
||||
{"*.*", 1},
|
||||
{"{placeholder}", 0},
|
||||
{"/{placeholder}", 1},
|
||||
{"foo", 3},
|
||||
{"example.com", 11},
|
||||
{"a.example.com", 13},
|
||||
{"*.example.com", 12},
|
||||
{"/foo", 4},
|
||||
{"/foo*", 4},
|
||||
{"{placeholder}.example.com", 12},
|
||||
{"{placeholder.example.com", 24},
|
||||
{"}.", 2},
|
||||
{"}{", 2},
|
||||
{"{}", 0},
|
||||
{"{{{}}", 1},
|
||||
} {
|
||||
actual := specificity(tc.input)
|
||||
if actual != tc.expect {
|
||||
t.Errorf("Test %d (%s): Expected %d but got %d", i, tc.input, tc.expect, actual)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestGlobalOptions(t *testing.T) {
|
||||
for i, tc := range []struct {
|
||||
input string
|
||||
expectWarn bool
|
||||
expectError bool
|
||||
}{
|
||||
{
|
||||
input: `
|
||||
{
|
||||
email test@example.com
|
||||
}
|
||||
:80
|
||||
`,
|
||||
expectWarn: false,
|
||||
expectError: false,
|
||||
},
|
||||
{
|
||||
input: `
|
||||
{
|
||||
admin off
|
||||
}
|
||||
:80
|
||||
`,
|
||||
expectWarn: false,
|
||||
expectError: false,
|
||||
},
|
||||
{
|
||||
input: `
|
||||
{
|
||||
admin 127.0.0.1:2020
|
||||
}
|
||||
:80
|
||||
`,
|
||||
expectWarn: false,
|
||||
expectError: false,
|
||||
},
|
||||
{
|
||||
input: `
|
||||
{
|
||||
admin {
|
||||
disabled false
|
||||
}
|
||||
}
|
||||
:80
|
||||
`,
|
||||
expectWarn: false,
|
||||
expectError: true,
|
||||
},
|
||||
} {
|
||||
|
||||
adapter := caddyfile.Adapter{
|
||||
ServerType: ServerType{},
|
||||
}
|
||||
|
||||
_, warnings, err := adapter.Adapt([]byte(tc.input), nil)
|
||||
|
||||
if len(warnings) > 0 != tc.expectWarn {
|
||||
t.Errorf("Test %d warning expectation failed Expected: %v, got %v", i, tc.expectWarn, warnings)
|
||||
continue
|
||||
}
|
||||
|
||||
if err != nil != tc.expectError {
|
||||
t.Errorf("Test %d error expectation failed Expected: %v, got %s", i, tc.expectError, err)
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,250 +0,0 @@
|
||||
// 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 httpcaddyfile
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/caddyserver/caddy/v2"
|
||||
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
|
||||
"github.com/caddyserver/caddy/v2/modules/caddytls"
|
||||
)
|
||||
|
||||
func parseOptHTTPPort(d *caddyfile.Dispenser) (int, error) {
|
||||
var httpPort int
|
||||
for d.Next() {
|
||||
var httpPortStr string
|
||||
if !d.AllArgs(&httpPortStr) {
|
||||
return 0, d.ArgErr()
|
||||
}
|
||||
var err error
|
||||
httpPort, err = strconv.Atoi(httpPortStr)
|
||||
if err != nil {
|
||||
return 0, d.Errf("converting port '%s' to integer value: %v", httpPortStr, err)
|
||||
}
|
||||
}
|
||||
return httpPort, nil
|
||||
}
|
||||
|
||||
func parseOptHTTPSPort(d *caddyfile.Dispenser) (int, error) {
|
||||
var httpsPort int
|
||||
for d.Next() {
|
||||
var httpsPortStr string
|
||||
if !d.AllArgs(&httpsPortStr) {
|
||||
return 0, d.ArgErr()
|
||||
}
|
||||
var err error
|
||||
httpsPort, err = strconv.Atoi(httpsPortStr)
|
||||
if err != nil {
|
||||
return 0, d.Errf("converting port '%s' to integer value: %v", httpsPortStr, err)
|
||||
}
|
||||
}
|
||||
return httpsPort, nil
|
||||
}
|
||||
|
||||
func parseOptExperimentalHTTP3(d *caddyfile.Dispenser) (bool, error) {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func parseOptOrder(d *caddyfile.Dispenser) ([]string, error) {
|
||||
newOrder := directiveOrder
|
||||
|
||||
for d.Next() {
|
||||
// get directive name
|
||||
if !d.Next() {
|
||||
return nil, d.ArgErr()
|
||||
}
|
||||
dirName := d.Val()
|
||||
if _, ok := registeredDirectives[dirName]; !ok {
|
||||
return nil, d.Errf("%s is not a registered directive", dirName)
|
||||
}
|
||||
|
||||
// get positional token
|
||||
if !d.Next() {
|
||||
return nil, d.ArgErr()
|
||||
}
|
||||
pos := d.Val()
|
||||
|
||||
// if directive exists, first remove it
|
||||
for i, d := range newOrder {
|
||||
if d == dirName {
|
||||
newOrder = append(newOrder[:i], newOrder[i+1:]...)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// act on the positional
|
||||
switch pos {
|
||||
case "first":
|
||||
newOrder = append([]string{dirName}, newOrder...)
|
||||
if d.NextArg() {
|
||||
return nil, d.ArgErr()
|
||||
}
|
||||
directiveOrder = newOrder
|
||||
return newOrder, nil
|
||||
case "last":
|
||||
newOrder = append(newOrder, dirName)
|
||||
if d.NextArg() {
|
||||
return nil, d.ArgErr()
|
||||
}
|
||||
directiveOrder = newOrder
|
||||
return newOrder, nil
|
||||
case "before":
|
||||
case "after":
|
||||
default:
|
||||
return nil, d.Errf("unknown positional '%s'", pos)
|
||||
}
|
||||
|
||||
// get name of other directive
|
||||
if !d.NextArg() {
|
||||
return nil, d.ArgErr()
|
||||
}
|
||||
otherDir := d.Val()
|
||||
if d.NextArg() {
|
||||
return nil, d.ArgErr()
|
||||
}
|
||||
|
||||
// insert directive into proper position
|
||||
for i, d := range newOrder {
|
||||
if d == otherDir {
|
||||
if pos == "before" {
|
||||
newOrder = append(newOrder[:i], append([]string{dirName}, newOrder[i:]...)...)
|
||||
} else if pos == "after" {
|
||||
newOrder = append(newOrder[:i+1], append([]string{dirName}, newOrder[i+1:]...)...)
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
directiveOrder = newOrder
|
||||
|
||||
return newOrder, nil
|
||||
}
|
||||
|
||||
func parseOptStorage(d *caddyfile.Dispenser) (caddy.StorageConverter, error) {
|
||||
if !d.Next() {
|
||||
return nil, d.ArgErr()
|
||||
}
|
||||
args := d.RemainingArgs()
|
||||
if len(args) != 1 {
|
||||
return nil, d.ArgErr()
|
||||
}
|
||||
modName := args[0]
|
||||
mod, err := caddy.GetModule("caddy.storage." + modName)
|
||||
if err != nil {
|
||||
return nil, d.Errf("getting storage module '%s': %v", modName, err)
|
||||
}
|
||||
unm, ok := mod.New().(caddyfile.Unmarshaler)
|
||||
if !ok {
|
||||
return nil, d.Errf("storage module '%s' is not a Caddyfile unmarshaler", mod.ID)
|
||||
}
|
||||
err = unm.UnmarshalCaddyfile(d.NewFromNextSegment())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
storage, ok := unm.(caddy.StorageConverter)
|
||||
if !ok {
|
||||
return nil, d.Errf("module %s is not a StorageConverter", mod.ID)
|
||||
}
|
||||
return storage, nil
|
||||
}
|
||||
|
||||
func parseOptSingleString(d *caddyfile.Dispenser) (string, error) {
|
||||
d.Next() // consume parameter name
|
||||
if !d.Next() {
|
||||
return "", d.ArgErr()
|
||||
}
|
||||
val := d.Val()
|
||||
if d.Next() {
|
||||
return "", d.ArgErr()
|
||||
}
|
||||
return val, nil
|
||||
}
|
||||
|
||||
func parseOptAdmin(d *caddyfile.Dispenser) (string, error) {
|
||||
if d.Next() {
|
||||
var listenAddress string
|
||||
if !d.AllArgs(&listenAddress) {
|
||||
return "", d.ArgErr()
|
||||
}
|
||||
if listenAddress == "" {
|
||||
listenAddress = caddy.DefaultAdminListen
|
||||
}
|
||||
return listenAddress, nil
|
||||
}
|
||||
return "", nil
|
||||
}
|
||||
|
||||
func parseOptOnDemand(d *caddyfile.Dispenser) (*caddytls.OnDemandConfig, error) {
|
||||
var ond *caddytls.OnDemandConfig
|
||||
for d.Next() {
|
||||
if d.NextArg() {
|
||||
return nil, d.ArgErr()
|
||||
}
|
||||
for nesting := d.Nesting(); d.NextBlock(nesting); {
|
||||
switch d.Val() {
|
||||
case "ask":
|
||||
if !d.NextArg() {
|
||||
return nil, d.ArgErr()
|
||||
}
|
||||
if ond == nil {
|
||||
ond = new(caddytls.OnDemandConfig)
|
||||
}
|
||||
ond.Ask = d.Val()
|
||||
|
||||
case "interval":
|
||||
if !d.NextArg() {
|
||||
return nil, d.ArgErr()
|
||||
}
|
||||
dur, err := time.ParseDuration(d.Val())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if ond == nil {
|
||||
ond = new(caddytls.OnDemandConfig)
|
||||
}
|
||||
if ond.RateLimit == nil {
|
||||
ond.RateLimit = new(caddytls.RateLimit)
|
||||
}
|
||||
ond.RateLimit.Interval = caddy.Duration(dur)
|
||||
|
||||
case "burst":
|
||||
if !d.NextArg() {
|
||||
return nil, d.ArgErr()
|
||||
}
|
||||
burst, err := strconv.Atoi(d.Val())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if ond == nil {
|
||||
ond = new(caddytls.OnDemandConfig)
|
||||
}
|
||||
if ond.RateLimit == nil {
|
||||
ond.RateLimit = new(caddytls.RateLimit)
|
||||
}
|
||||
ond.RateLimit.Burst = burst
|
||||
|
||||
default:
|
||||
return nil, d.Errf("unrecognized parameter '%s'", d.Val())
|
||||
}
|
||||
}
|
||||
}
|
||||
if ond == nil {
|
||||
return nil, d.Err("expected at least one config parameter for on_demand_tls")
|
||||
}
|
||||
return ond, nil
|
||||
}
|
||||
@@ -1,386 +0,0 @@
|
||||
// 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 httpcaddyfile
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"sort"
|
||||
|
||||
"github.com/caddyserver/caddy/v2"
|
||||
"github.com/caddyserver/caddy/v2/caddyconfig"
|
||||
"github.com/caddyserver/caddy/v2/modules/caddytls"
|
||||
"github.com/caddyserver/certmagic"
|
||||
)
|
||||
|
||||
func (st ServerType) buildTLSApp(
|
||||
pairings []sbAddrAssociation,
|
||||
options map[string]interface{},
|
||||
warnings []caddyconfig.Warning,
|
||||
) (*caddytls.TLS, []caddyconfig.Warning, error) {
|
||||
|
||||
tlsApp := &caddytls.TLS{CertificatesRaw: make(caddy.ModuleMap)}
|
||||
var certLoaders []caddytls.CertificateLoader
|
||||
|
||||
// count how many server blocks have a key with no host,
|
||||
// and find all hosts that share a server block with a
|
||||
// hostless key, so that they don't get forgotten/omitted
|
||||
// by auto-HTTPS (since they won't appear in route matchers)
|
||||
var serverBlocksWithHostlessKey int
|
||||
hostsSharedWithHostlessKey := make(map[string]struct{})
|
||||
for _, pair := range pairings {
|
||||
for _, sb := range pair.serverBlocks {
|
||||
for _, key := range sb.block.Keys {
|
||||
addr, err := ParseAddress(key)
|
||||
if err != nil {
|
||||
return nil, warnings, err
|
||||
}
|
||||
addr = addr.Normalize()
|
||||
if addr.Host == "" {
|
||||
serverBlocksWithHostlessKey++
|
||||
// this server block has a hostless key, now
|
||||
// go through and add all the hosts to the set
|
||||
for _, otherKey := range sb.block.Keys {
|
||||
if otherKey == key {
|
||||
continue
|
||||
}
|
||||
addr, err := ParseAddress(otherKey)
|
||||
if err != nil {
|
||||
return nil, warnings, err
|
||||
}
|
||||
addr = addr.Normalize()
|
||||
if addr.Host != "" {
|
||||
hostsSharedWithHostlessKey[addr.Host] = struct{}{}
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
catchAllAP, err := newBaseAutomationPolicy(options, warnings, false)
|
||||
if err != nil {
|
||||
return nil, warnings, err
|
||||
}
|
||||
|
||||
for _, p := range pairings {
|
||||
for _, sblock := range p.serverBlocks {
|
||||
// get values that populate an automation policy for this block
|
||||
var ap *caddytls.AutomationPolicy
|
||||
|
||||
sblockHosts, err := st.hostsFromServerBlockKeys(sblock.block)
|
||||
if err != nil {
|
||||
return nil, warnings, err
|
||||
}
|
||||
if len(sblockHosts) == 0 {
|
||||
ap = catchAllAP
|
||||
}
|
||||
|
||||
// on-demand tls
|
||||
if _, ok := sblock.pile["tls.on_demand"]; ok {
|
||||
if ap == nil {
|
||||
var err error
|
||||
ap, err = newBaseAutomationPolicy(options, warnings, true)
|
||||
if err != nil {
|
||||
return nil, warnings, err
|
||||
}
|
||||
}
|
||||
ap.OnDemand = true
|
||||
}
|
||||
|
||||
// certificate issuers
|
||||
if issuerVals, ok := sblock.pile["tls.cert_issuer"]; ok {
|
||||
for _, issuerVal := range issuerVals {
|
||||
issuer := issuerVal.Value.(certmagic.Issuer)
|
||||
if ap == nil {
|
||||
var err error
|
||||
ap, err = newBaseAutomationPolicy(options, warnings, true)
|
||||
if err != nil {
|
||||
return nil, warnings, err
|
||||
}
|
||||
}
|
||||
encoded := caddyconfig.JSONModuleObject(issuer, "module", issuer.(caddy.Module).CaddyModule().ID.Name(), &warnings)
|
||||
if ap == catchAllAP && ap.IssuerRaw != nil && !bytes.Equal(ap.IssuerRaw, encoded) {
|
||||
return nil, warnings, fmt.Errorf("conflicting issuer configuration: %s != %s", ap.IssuerRaw, encoded)
|
||||
}
|
||||
ap.IssuerRaw = encoded
|
||||
}
|
||||
}
|
||||
|
||||
if ap != nil {
|
||||
// first make sure this block is allowed to create an automation policy;
|
||||
// doing so is forbidden if it has a key with no host (i.e. ":443")
|
||||
// and if there is a different server block that also has a key with no
|
||||
// host -- since a key with no host matches any host, we need its
|
||||
// associated automation policy to have an empty Subjects list, i.e. no
|
||||
// host filter, which is indistinguishable between the two server blocks
|
||||
// because automation is not done in the context of a particular server...
|
||||
// this is an example of a poor mapping from Caddyfile to JSON but that's
|
||||
// the least-leaky abstraction I could figure out
|
||||
if len(sblockHosts) == 0 {
|
||||
if serverBlocksWithHostlessKey > 1 {
|
||||
// this server block and at least one other has a key with no host,
|
||||
// making the two indistinguishable; it is misleading to define such
|
||||
// a policy within one server block since it actually will apply to
|
||||
// others as well
|
||||
return nil, warnings, fmt.Errorf("cannot make a TLS automation policy from a server block that has a host-less address when there are other server block addresses lacking a host")
|
||||
}
|
||||
if catchAllAP == nil {
|
||||
// this server block has a key with no hosts, but there is not yet
|
||||
// a catch-all automation policy (probably because no global options
|
||||
// were set), so this one becomes it
|
||||
catchAllAP = ap
|
||||
}
|
||||
}
|
||||
|
||||
// associate our new automation policy with this server block's hosts,
|
||||
// unless, of course, the server block has a key with no hosts, in which
|
||||
// case its automation policy becomes or blends with the default/global
|
||||
// automation policy because, of necessity, it applies to all hostnames
|
||||
// (i.e. it has no Subjects filter) -- in that case, we'll append it last
|
||||
if ap != catchAllAP {
|
||||
ap.Subjects = sblockHosts
|
||||
|
||||
// if a combination of public and internal names were given
|
||||
// for this same server block and no issuer was specified, we
|
||||
// need to separate them out in the automation policies so
|
||||
// that the internal names can use the internal issuer and
|
||||
// the other names can use the default/public/ACME issuer
|
||||
var ap2 *caddytls.AutomationPolicy
|
||||
if ap.Issuer == nil {
|
||||
var internal, external []string
|
||||
for _, s := range ap.Subjects {
|
||||
if certmagic.SubjectQualifiesForPublicCert(s) {
|
||||
external = append(external, s)
|
||||
} else {
|
||||
internal = append(internal, s)
|
||||
}
|
||||
}
|
||||
if len(external) > 0 && len(internal) > 0 {
|
||||
ap.Subjects = external
|
||||
apCopy := *ap
|
||||
ap2 = &apCopy
|
||||
ap2.Subjects = internal
|
||||
ap2.IssuerRaw = caddyconfig.JSONModuleObject(caddytls.InternalIssuer{}, "module", "internal", &warnings)
|
||||
}
|
||||
}
|
||||
if tlsApp.Automation == nil {
|
||||
tlsApp.Automation = new(caddytls.AutomationConfig)
|
||||
}
|
||||
tlsApp.Automation.Policies = append(tlsApp.Automation.Policies, ap)
|
||||
if ap2 != nil {
|
||||
tlsApp.Automation.Policies = append(tlsApp.Automation.Policies, ap2)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// certificate loaders
|
||||
if clVals, ok := sblock.pile["tls.certificate_loader"]; ok {
|
||||
for _, clVal := range clVals {
|
||||
certLoaders = append(certLoaders, clVal.Value.(caddytls.CertificateLoader))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// group certificate loaders by module name, then add to config
|
||||
if len(certLoaders) > 0 {
|
||||
loadersByName := make(map[string]caddytls.CertificateLoader)
|
||||
for _, cl := range certLoaders {
|
||||
name := caddy.GetModuleName(cl)
|
||||
// ugh... technically, we may have multiple FileLoader and FolderLoader
|
||||
// modules (because the tls directive returns one per occurrence), but
|
||||
// the config structure expects only one instance of each kind of loader
|
||||
// module, so we have to combine them... instead of enumerating each
|
||||
// possible cert loader module in a type switch, we can use reflection,
|
||||
// which works on any cert loaders that are slice types
|
||||
if reflect.TypeOf(cl).Kind() == reflect.Slice {
|
||||
combined := reflect.ValueOf(loadersByName[name])
|
||||
if !combined.IsValid() {
|
||||
combined = reflect.New(reflect.TypeOf(cl)).Elem()
|
||||
}
|
||||
clVal := reflect.ValueOf(cl)
|
||||
for i := 0; i < clVal.Len(); i++ {
|
||||
combined = reflect.Append(reflect.Value(combined), clVal.Index(i))
|
||||
}
|
||||
loadersByName[name] = combined.Interface().(caddytls.CertificateLoader)
|
||||
}
|
||||
}
|
||||
for certLoaderName, loaders := range loadersByName {
|
||||
tlsApp.CertificatesRaw[certLoaderName] = caddyconfig.JSON(loaders, &warnings)
|
||||
}
|
||||
}
|
||||
|
||||
// set any of the on-demand options, for if/when on-demand TLS is enabled
|
||||
if onDemand, ok := options["on_demand_tls"].(*caddytls.OnDemandConfig); ok {
|
||||
if tlsApp.Automation == nil {
|
||||
tlsApp.Automation = new(caddytls.AutomationConfig)
|
||||
}
|
||||
tlsApp.Automation.OnDemand = onDemand
|
||||
}
|
||||
|
||||
// if there is a global/catch-all automation policy, ensure it goes last
|
||||
if catchAllAP != nil {
|
||||
if tlsApp.Automation == nil {
|
||||
tlsApp.Automation = new(caddytls.AutomationConfig)
|
||||
}
|
||||
tlsApp.Automation.Policies = append(tlsApp.Automation.Policies, catchAllAP)
|
||||
}
|
||||
|
||||
// if any hostnames appear on the same server block as a key with
|
||||
// no host, they will not be used with route matchers because the
|
||||
// hostless key matches all hosts, therefore, it wouldn't be
|
||||
// considered for auto-HTTPS, so we need to make sure those hosts
|
||||
// are manually considered for managed certificates
|
||||
var al caddytls.AutomateLoader
|
||||
for h := range hostsSharedWithHostlessKey {
|
||||
al = append(al, h)
|
||||
}
|
||||
if len(al) > 0 {
|
||||
tlsApp.CertificatesRaw["automate"] = caddyconfig.JSON(al, &warnings)
|
||||
}
|
||||
|
||||
// do a little verification & cleanup
|
||||
if tlsApp.Automation != nil {
|
||||
// ensure automation policies don't overlap subjects (this should be
|
||||
// an error at provision-time as well, but catch it in the adapt phase
|
||||
// for convenience)
|
||||
automationHostSet := make(map[string]struct{})
|
||||
for _, ap := range tlsApp.Automation.Policies {
|
||||
for _, s := range ap.Subjects {
|
||||
if _, ok := automationHostSet[s]; ok {
|
||||
return nil, warnings, fmt.Errorf("hostname appears in more than one automation policy, making certificate management ambiguous: %s", s)
|
||||
}
|
||||
automationHostSet[s] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
// consolidate automation policies that are the exact same
|
||||
tlsApp.Automation.Policies = consolidateAutomationPolicies(tlsApp.Automation.Policies)
|
||||
}
|
||||
|
||||
return tlsApp, warnings, nil
|
||||
}
|
||||
|
||||
// newBaseAutomationPolicy returns a new TLS automation policy that gets
|
||||
// its values from the global options map. It should be used as the base
|
||||
// for any other automation policies. A nil policy (and no error) will be
|
||||
// returned if there are no default/global options. However, if always is
|
||||
// true, a non-nil value will always be returned (unless there is an error).
|
||||
func newBaseAutomationPolicy(options map[string]interface{}, warnings []caddyconfig.Warning, always bool) (*caddytls.AutomationPolicy, error) {
|
||||
acmeCA, hasACMECA := options["acme_ca"]
|
||||
acmeDNS, hasACMEDNS := options["acme_dns"]
|
||||
acmeCARoot, hasACMECARoot := options["acme_ca_root"]
|
||||
email, hasEmail := options["email"]
|
||||
localCerts, hasLocalCerts := options["local_certs"]
|
||||
|
||||
hasGlobalAutomationOpts := hasACMECA || hasACMEDNS || hasACMECARoot || hasEmail || hasLocalCerts
|
||||
|
||||
// if there are no global options related to automation policies
|
||||
// set, then we can just return right away
|
||||
if !hasGlobalAutomationOpts {
|
||||
if always {
|
||||
return new(caddytls.AutomationPolicy), nil
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
ap := new(caddytls.AutomationPolicy)
|
||||
|
||||
if localCerts != nil {
|
||||
// internal issuer enabled trumps any ACME configurations; useful in testing
|
||||
ap.IssuerRaw = caddyconfig.JSONModuleObject(caddytls.InternalIssuer{}, "module", "internal", &warnings)
|
||||
} else {
|
||||
if acmeCA == nil {
|
||||
acmeCA = ""
|
||||
}
|
||||
if email == nil {
|
||||
email = ""
|
||||
}
|
||||
mgr := caddytls.ACMEIssuer{
|
||||
CA: acmeCA.(string),
|
||||
Email: email.(string),
|
||||
}
|
||||
if acmeDNS != nil {
|
||||
provName := acmeDNS.(string)
|
||||
dnsProvModule, err := caddy.GetModule("tls.dns." + provName)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("getting DNS provider module named '%s': %v", provName, err)
|
||||
}
|
||||
mgr.Challenges = &caddytls.ChallengesConfig{
|
||||
DNSRaw: caddyconfig.JSONModuleObject(dnsProvModule.New(), "provider", provName, &warnings),
|
||||
}
|
||||
}
|
||||
if acmeCARoot != nil {
|
||||
mgr.TrustedRootsPEMFiles = []string{acmeCARoot.(string)}
|
||||
}
|
||||
ap.IssuerRaw = caddyconfig.JSONModuleObject(mgr, "module", "acme", &warnings)
|
||||
}
|
||||
|
||||
return ap, nil
|
||||
}
|
||||
|
||||
// consolidateAutomationPolicies combines automation policies that are the same,
|
||||
// for a cleaner overall output.
|
||||
func consolidateAutomationPolicies(aps []*caddytls.AutomationPolicy) []*caddytls.AutomationPolicy {
|
||||
for i := 0; i < len(aps); i++ {
|
||||
for j := 0; j < len(aps); j++ {
|
||||
if j == i {
|
||||
continue
|
||||
}
|
||||
|
||||
// if they're exactly equal in every way, just keep one of them
|
||||
if reflect.DeepEqual(aps[i], aps[j]) {
|
||||
aps = append(aps[:j], aps[j+1:]...)
|
||||
i--
|
||||
break
|
||||
}
|
||||
|
||||
// if the policy is the same, we can keep just one, but we have
|
||||
// to be careful which one we keep; if only one has any hostnames
|
||||
// defined, then we need to keep the one without any hostnames,
|
||||
// otherwise the one without any subjects (a catch-all) would be
|
||||
// eaten up by the one with subjects; and if both have subjects, we
|
||||
// need to combine their lists
|
||||
if bytes.Equal(aps[i].IssuerRaw, aps[j].IssuerRaw) &&
|
||||
bytes.Equal(aps[i].StorageRaw, aps[j].StorageRaw) &&
|
||||
aps[i].MustStaple == aps[j].MustStaple &&
|
||||
aps[i].KeyType == aps[j].KeyType &&
|
||||
aps[i].OnDemand == aps[j].OnDemand &&
|
||||
aps[i].RenewalWindowRatio == aps[j].RenewalWindowRatio &&
|
||||
aps[i].ManageSync == aps[j].ManageSync {
|
||||
if len(aps[i].Subjects) == 0 && len(aps[j].Subjects) > 0 {
|
||||
aps = append(aps[:j], aps[j+1:]...)
|
||||
} else if len(aps[i].Subjects) > 0 && len(aps[j].Subjects) == 0 {
|
||||
aps = append(aps[:i], aps[i+1:]...)
|
||||
} else {
|
||||
aps[i].Subjects = append(aps[i].Subjects, aps[j].Subjects...)
|
||||
aps = append(aps[:j], aps[j+1:]...)
|
||||
}
|
||||
i--
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ensure any catch-all policies go last
|
||||
sort.SliceStable(aps, func(i, j int) bool {
|
||||
return len(aps[i].Subjects) > len(aps[j].Subjects)
|
||||
})
|
||||
|
||||
return aps
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
// 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 json5adapter
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/caddyserver/caddy/v2/caddyconfig"
|
||||
"github.com/ilibs/json5"
|
||||
)
|
||||
|
||||
func init() {
|
||||
caddyconfig.RegisterAdapter("json5", Adapter{})
|
||||
}
|
||||
|
||||
// Adapter adapts JSON5 to Caddy JSON.
|
||||
type Adapter struct{}
|
||||
|
||||
// Adapt converts the JSON5 config in body to Caddy JSON.
|
||||
func (a Adapter) Adapt(body []byte, options map[string]interface{}) (result []byte, warnings []caddyconfig.Warning, err error) {
|
||||
var decoded interface{}
|
||||
err = json5.Unmarshal(body, &decoded)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
result, err = json.Marshal(decoded)
|
||||
return
|
||||
}
|
||||
|
||||
// Interface guard
|
||||
var _ caddyconfig.Adapter = (*Adapter)(nil)
|
||||
@@ -1,49 +0,0 @@
|
||||
// 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 jsoncadapter
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/caddyserver/caddy/v2/caddyconfig"
|
||||
"github.com/muhammadmuzzammil1998/jsonc"
|
||||
)
|
||||
|
||||
func init() {
|
||||
caddyconfig.RegisterAdapter("jsonc", Adapter{})
|
||||
}
|
||||
|
||||
// Adapter adapts JSON-C to Caddy JSON.
|
||||
type Adapter struct{}
|
||||
|
||||
// Adapt converts the JSON-C config in body to Caddy JSON.
|
||||
func (a Adapter) Adapt(body []byte, options map[string]interface{}) (result []byte, warnings []caddyconfig.Warning, err error) {
|
||||
result = jsonc.ToJSON(body)
|
||||
|
||||
// any errors in the JSON will be
|
||||
// reported during config load, but
|
||||
// we can at least warn here that
|
||||
// it is not valid JSON
|
||||
if !json.Valid(result) {
|
||||
warnings = append(warnings, caddyconfig.Warning{
|
||||
Message: "Resulting JSON is invalid.",
|
||||
})
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Interface guard
|
||||
var _ caddyconfig.Adapter = (*Adapter)(nil)
|
||||
@@ -1,23 +0,0 @@
|
||||
-----BEGIN CERTIFICATE-----
|
||||
MIID5zCCAs8CFG4+w/pqR5AZQ+aVB330uRRRKMF0MA0GCSqGSIb3DQEBCwUAMIGv
|
||||
MQswCQYDVQQGEwJVUzELMAkGA1UECAwCTlkxGzAZBgNVBAoMEkxvY2FsIERldmVs
|
||||
b3BlbWVudDEbMBkGA1UEBwwSTG9jYWwgRGV2ZWxvcGVtZW50MRowGAYDVQQDDBFh
|
||||
LmNhZGR5LmxvY2FsaG9zdDEbMBkGA1UECwwSTG9jYWwgRGV2ZWxvcGVtZW50MSAw
|
||||
HgYJKoZIhvcNAQkBFhFhZG1pbkBjYWRkeS5sb2NhbDAeFw0yMDAzMTMxODUwMTda
|
||||
Fw0zMDAzMTExODUwMTdaMIGvMQswCQYDVQQGEwJVUzELMAkGA1UECAwCTlkxGzAZ
|
||||
BgNVBAoMEkxvY2FsIERldmVsb3BlbWVudDEbMBkGA1UEBwwSTG9jYWwgRGV2ZWxv
|
||||
cGVtZW50MRowGAYDVQQDDBFhLmNhZGR5LmxvY2FsaG9zdDEbMBkGA1UECwwSTG9j
|
||||
YWwgRGV2ZWxvcGVtZW50MSAwHgYJKoZIhvcNAQkBFhFhZG1pbkBjYWRkeS5sb2Nh
|
||||
bDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAMd9pC9wF7j0459FndPs
|
||||
Deud/rq41jEZFsVOVtjQgjS1A5ct6NfeMmSlq8i1F7uaTMPZjbOHzY6y6hzLc9+y
|
||||
/VWNgyUC543HjXnNTnp9Xug6tBBxOxvRMw5mv2nAyzjBGDePPgN84xKhOXG2Wj3u
|
||||
fOZ+VPVISefRNvjKfN87WLJ0B0HI9wplG5ASVdPQsWDY1cndrZgt2sxQ/3fjIno4
|
||||
VvrgRWC9Penizgps/a0ZcFZMD/6HJoX/mSZVa1LjopwbMTXvyHCpXkth21E+rBt6
|
||||
I9DMHerdioVQcX25CqPmAwePxPZSNGEQo/Qu32kzcmscmYxTtYBhDa+yLuHgGggI
|
||||
j7ECAwEAATANBgkqhkiG9w0BAQsFAAOCAQEAP/94KPtkpYtkWADnhtzDmgQ6Q1pH
|
||||
SubTUZdCwQtm6/LrvpT+uFNsOj4L3Mv3TVUnIQDmKd5VvR42W2MRBiTN2LQptgEn
|
||||
C7g9BB+UA9kjL3DPk1pJMjzxLHohh0uNLi7eh4mAj8eNvjz9Z4qMWPQoVS0y7/ZK
|
||||
cCBRKh2GkIqKm34ih6pX7xmMpPEQsFoTVPRHYJfYD1SZ8Iui+EN+7WqLuJWPsPXw
|
||||
JM1HuZKn7pZmJU2MZZBsrupHGUvNMbBg2mFJcxt4D1VvU+p+a67PSjpFQ6dJG2re
|
||||
pZoF+N1vMGAFkxe6UqhcC/bXDX+ILVQHJ+RNhzDO6DcWf8dRrC2LaJk3WA==
|
||||
-----END CERTIFICATE-----
|
||||
@@ -1,27 +0,0 @@
|
||||
-----BEGIN RSA PRIVATE KEY-----
|
||||
MIIEpAIBAAKCAQEAx32kL3AXuPTjn0Wd0+wN653+urjWMRkWxU5W2NCCNLUDly3o
|
||||
194yZKWryLUXu5pMw9mNs4fNjrLqHMtz37L9VY2DJQLnjceNec1Oen1e6Dq0EHE7
|
||||
G9EzDma/acDLOMEYN48+A3zjEqE5cbZaPe585n5U9UhJ59E2+Mp83ztYsnQHQcj3
|
||||
CmUbkBJV09CxYNjVyd2tmC3azFD/d+MiejhW+uBFYL096eLOCmz9rRlwVkwP/ocm
|
||||
hf+ZJlVrUuOinBsxNe/IcKleS2HbUT6sG3oj0Mwd6t2KhVBxfbkKo+YDB4/E9lI0
|
||||
YRCj9C7faTNyaxyZjFO1gGENr7Iu4eAaCAiPsQIDAQABAoIBAQDD/YFIBeWYlifn
|
||||
e9risQDAIrp3sk7lb9O6Rwv1+Wxi4hBEABvJsYhq74VFK/3EF4UhyWR5JIvkjYyK
|
||||
e6w887oGyoA05ZSe65XoO7fFidSrbbkoikZbPv3dQT7/ZCWEfdkQBNAVVyY0UGeC
|
||||
e3hPbjYRsb5AOSQ694X9idqC6uhqcOrBDjITFrctUoP4S6l9A6a+mLSUIwiICcuh
|
||||
mrNl+j0lzy7DMXRp/Z5Hyo5kuUlrC0dCLa1UHqtrrK7MR55AVEOihSNp1w+OC+vw
|
||||
f0VjE4JUtO7RQEQUmD1tDfLXwNfMFeWaobB2W0WMvRg0IqoitiqPxsPHRm56OxfM
|
||||
SRo/Q7QBAoGBAP8DapzBMuaIcJ7cE8Yl07ZGndWWf8buIKIItGF8rkEO3BXhrIke
|
||||
EmpOi+ELtpbMOG0APhORZyQ58f4ZOVrqZfneNKtDiEZV4mJZaYUESm1pU+2Y6+y5
|
||||
g4bpQSVKN0ow0xR+MH7qDYtSlsmBU7qAOz775L7BmMA1Bnu72aN/H1JBAoGBAMhD
|
||||
OzqCSakHOjUbEd22rPwqWmcIyVyo04gaSmcVVT2dHbqR4/t0gX5a9D9U2qwyO6xi
|
||||
/R+PXyMd32xIeVR2D/7SQ0x6dK68HXICLV8ofHZ5UQcHbxy5og4v/YxSZVTkN374
|
||||
cEsUeyB0s/UPOHLktFU5hpIlON72/Rp7b+pNIwFxAoGAczpq+Qu/YTWzlcSh1r4O
|
||||
7OT5uqI3eH7vFehTAV3iKxl4zxZa7NY+wfRd9kFhrr/2myIp6pOgBFl+hC+HoBIc
|
||||
JAyIxf5M3GNAWOpH6MfojYmzV7/qktu8l8BcJGplk0t+hVsDtMUze4nFAqZCXBpH
|
||||
Kw2M7bjyuZ78H/rgu6TcVUECgYEAo1M5ldE2U/VCApeuLX1TfWDpU8i1uK0zv3d5
|
||||
oLKkT1i5KzTak3SEO9HgC1qf8PoS8tfUio26UICHe99rnHehOfivzEq+qNdgyF+A
|
||||
M3BoeZMdgzcL5oh640k+Zte4LtDlddcWdhUhCepD7iPYrNNbQ3pkBwL2a9lRuOxc
|
||||
7OC2IPECgYBH8f3OrwXjDltIG1dDvuDPNljxLZbFEFbQyVzMePYNftgZknAyGEdh
|
||||
NW/LuWeTzstnmz/s6RE3jN5ZrrMa4sW77VA9+yU9QW2dkHqFyukQ4sfuNg6kDDNZ
|
||||
+lqZYMCLw0M5P9fIbmnIYwey7tXkHfmzoCpnYHGQDN6hL0Bh0zGwmg==
|
||||
-----END RSA PRIVATE KEY-----
|
||||
@@ -1,23 +0,0 @@
|
||||
-----BEGIN CERTIFICATE-----
|
||||
MIID5zCCAs8CFFmAAFKV79uhzxc5qXbUw3oBNsYXMA0GCSqGSIb3DQEBCwUAMIGv
|
||||
MQswCQYDVQQGEwJVUzELMAkGA1UECAwCTlkxGzAZBgNVBAoMEkxvY2FsIERldmVs
|
||||
b3BlbWVudDEbMBkGA1UEBwwSTG9jYWwgRGV2ZWxvcGVtZW50MRowGAYDVQQDDBEq
|
||||
LmNhZGR5LmxvY2FsaG9zdDEbMBkGA1UECwwSTG9jYWwgRGV2ZWxvcGVtZW50MSAw
|
||||
HgYJKoZIhvcNAQkBFhFhZG1pbkBjYWRkeS5sb2NhbDAeFw0yMDAzMDIwODAxMTZa
|
||||
Fw0zMDAyMjgwODAxMTZaMIGvMQswCQYDVQQGEwJVUzELMAkGA1UECAwCTlkxGzAZ
|
||||
BgNVBAoMEkxvY2FsIERldmVsb3BlbWVudDEbMBkGA1UEBwwSTG9jYWwgRGV2ZWxv
|
||||
cGVtZW50MRowGAYDVQQDDBEqLmNhZGR5LmxvY2FsaG9zdDEbMBkGA1UECwwSTG9j
|
||||
YWwgRGV2ZWxvcGVtZW50MSAwHgYJKoZIhvcNAQkBFhFhZG1pbkBjYWRkeS5sb2Nh
|
||||
bDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAJngfeirQkWaU8ihgIC5
|
||||
SKpRQX/3koRjljDK/oCbhLs+wg592kIwVv06l7+mn7NSaNBloabjuA1GqyLRsNLL
|
||||
ptrv0HvXa5qLx28+icsb2Ny3dJnQaj9w9PwjxQ1qZqEJfWRH1D8Vz9AmB+QSV/Gu
|
||||
8e8alGFewlYZVfH1kbxoTT6QorF37TeA3bh1fgKFtzsGYKswcaZNdDBBHzLunCKZ
|
||||
HU6U6L45hm+yLADj3mmDLafUeiVOt6MRLLoSD1eLRVSXGrNo+brJ87zkZntI9+W1
|
||||
JxOBoXtZCwka7k2DlAtLihsrmBZA2ZC9yVeu/SQy3qb3iCNnTFTCyAnWeTCr6Tcq
|
||||
6w8CAwEAATANBgkqhkiG9w0BAQsFAAOCAQEAOWfXqpAmD4C3wGiMeZAeaaS4hDAR
|
||||
+JmN+avPDA6F6Bq7DB4NJuIwVUlaDL2s07w5VJJtW52aZVKoBlgHR5yG/XUli6J7
|
||||
YUJRmdQJvHUSu26cmKvyoOaTrEYbmvtGICWtZc8uTlMf9wQZbJA4KyxTgEQJDXsZ
|
||||
B2XFe+wVdhAgEpobYDROi+l/p8TL5z3U24LpwVTcJy5sEZVv7Wfs886IyxU8ORt8
|
||||
VZNcDiH6V53OIGeiufIhia/mPe6jbLntfGZfIFxtCcow4IA/lTy1ned7K5fmvNNb
|
||||
ZilxOQUk+wVK8genjdrZVAnAxsYLHJIb5yf9O7rr6fWciVMF3a0k5uNK1w==
|
||||
-----END CERTIFICATE-----
|
||||
@@ -1,27 +0,0 @@
|
||||
-----BEGIN RSA PRIVATE KEY-----
|
||||
MIIEogIBAAKCAQEAmeB96KtCRZpTyKGAgLlIqlFBf/eShGOWMMr+gJuEuz7CDn3a
|
||||
QjBW/TqXv6afs1Jo0GWhpuO4DUarItGw0sum2u/Qe9drmovHbz6JyxvY3Ld0mdBq
|
||||
P3D0/CPFDWpmoQl9ZEfUPxXP0CYH5BJX8a7x7xqUYV7CVhlV8fWRvGhNPpCisXft
|
||||
N4DduHV+AoW3OwZgqzBxpk10MEEfMu6cIpkdTpTovjmGb7IsAOPeaYMtp9R6JU63
|
||||
oxEsuhIPV4tFVJcas2j5usnzvORme0j35bUnE4Ghe1kLCRruTYOUC0uKGyuYFkDZ
|
||||
kL3JV679JDLepveII2dMVMLICdZ5MKvpNyrrDwIDAQABAoIBAFcPK01zb6hfm12c
|
||||
+k5aBiHOnUdgc/YRPg1XHEz5MEycQkDetZjTLrRQ7UBSbnKPgpu9lIsOtbhVLkgh
|
||||
6XAqJroiCou2oruqr+hhsqZGmBiwdvj7cNF6ADGTr05az7v22YneFdinZ481pStF
|
||||
sZocx+bm2+KHMV5zMSwXKyA0xtdJLxs2yklniDBxSZRppgppq1pDPprP5DkgKPfe
|
||||
3ekUmbQd5bHmivhW8ItbJLuf82XSsMBZ9ZhKiKIlWlbKAgiSV3SqnUQb5fi7l8hG
|
||||
yYZxbuCUIGFwKmEpUBBt/nyxrOlMiNtDh9JhrPmijTV3slq70pCLwLL/Ai2aeear
|
||||
EVA5VhkCgYEAyAmxfPqc2P7BsDAp67/sA7OEPso9qM4WyuWiVdlX2gb9TLNLYbPX
|
||||
Kk/UmpAIVzpoTAGY5Zp3wkvdD/ou8uUQsE8ioNn4S1a4G9XURH1wVhcEbUiAKI1S
|
||||
QVBH9B/Pj3eIp5OTKwob0Wj7DNdxoH7ed/Eok0EaTWzOA8pCWADKv/MCgYEAxOzY
|
||||
YsX7Nl+eyZr2+9unKyeAK/D1DCT/o99UUAHx72/xaBVP/06cfzpvKBNcF9iYc+fq
|
||||
R1yIUIrDRoSmYKBq+Kb3+nOg1nrqih/NBTokbTiI4Q+/30OQt0Al1e7y9iNKqV8H
|
||||
jYZItzluGNrWKedZbATwBwbVCY2jnNl6RMDnS3UCgYBxj3cwQUHLuoyQjjcuO80r
|
||||
qLzZvIxWiXDNDKIk5HcIMlGYOmz/8U2kGp/SgxQJGQJeq8V2C0QTjGfaCyieAcaA
|
||||
oNxCvptDgd6RBsoze5bLeNOtiqwe2WOp6n5+q5R0mOJ+Z7vzghCayGNFPgWmnH+F
|
||||
TeW/+wSIkc0+v5L8TK7NWwKBgBrlWlyLO9deUfqpHqihhICBYaEexOlGuF+yZfqT
|
||||
eW7BdFBJ8OYm33sFCR+JHV/oZlIWT8o1Wizd9vPPtEWoQ1P4wg/D8Si6GwSIeWEI
|
||||
YudD/HX4x7T/rmlI6qIAg9CYW18sqoRq3c2gm2fro6qPfYgiWIItLbWjUcBfd7Ki
|
||||
QjTtAoGARKdRv3jMWL84rlEx1nBRgL3pe9Dt+Uxzde2xT3ZeF+5Hp9NfU01qE6M6
|
||||
1I6H64smqpetlsXmCEVKwBemP3pJa6avLKgIYiQvHAD/v4rs9mqgy1RTqtYyGNhR
|
||||
1A/6dKkbiZ6wzePLLPasXVZxSKEviXf5gJooqumQVSVhCswyCZ0=
|
||||
-----END RSA PRIVATE KEY-----
|
||||
@@ -1,281 +0,0 @@
|
||||
package caddytest
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Defaults store any configuration required to make the tests run
|
||||
type Defaults struct {
|
||||
// Port we expect caddy to listening on
|
||||
AdminPort int
|
||||
// Certificates we expect to be loaded before attempting to run the tests
|
||||
Certifcates []string
|
||||
}
|
||||
|
||||
// Default testing values
|
||||
var Default = Defaults{
|
||||
AdminPort: 2019,
|
||||
Certifcates: []string{"/caddy.localhost.crt", "/caddy.localhost.key"},
|
||||
}
|
||||
|
||||
var (
|
||||
matchKey = regexp.MustCompile(`(/[\w\d\.]+\.key)`)
|
||||
matchCert = regexp.MustCompile(`(/[\w\d\.]+\.crt)`)
|
||||
)
|
||||
|
||||
type configLoadError struct {
|
||||
Response string
|
||||
}
|
||||
|
||||
func (e configLoadError) Error() string { return e.Response }
|
||||
|
||||
func timeElapsed(start time.Time, name string) {
|
||||
elapsed := time.Since(start)
|
||||
log.Printf("%s took %s", name, elapsed)
|
||||
}
|
||||
|
||||
// InitServer this will configure the server with a configurion of a specific
|
||||
// type. The configType must be either "json" or the adapter type.
|
||||
func InitServer(t *testing.T, rawConfig string, configType string) {
|
||||
if err := initServer(t, rawConfig, configType); errors.Is(err, &configLoadError{}) {
|
||||
t.Logf("failed to load config: %s", err)
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
// InitServer this will configure the server with a configurion of a specific
|
||||
// type. The configType must be either "json" or the adapter type.
|
||||
func initServer(t *testing.T, rawConfig string, configType string) error {
|
||||
|
||||
err := validateTestPrerequisites()
|
||||
if err != nil {
|
||||
t.Skipf("skipping tests as failed integration prerequisites. %s", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
t.Cleanup(func() {
|
||||
if t.Failed() {
|
||||
res, err := http.Get(fmt.Sprintf("http://localhost:%d/config/", Default.AdminPort))
|
||||
if err != nil {
|
||||
t.Log("unable to read the current config")
|
||||
}
|
||||
defer res.Body.Close()
|
||||
body, err := ioutil.ReadAll(res.Body)
|
||||
|
||||
var out bytes.Buffer
|
||||
json.Indent(&out, body, "", " ")
|
||||
t.Logf("----------- failed with config -----------\n%s", out.String())
|
||||
}
|
||||
})
|
||||
|
||||
rawConfig = prependCaddyFilePath(rawConfig)
|
||||
client := &http.Client{
|
||||
Timeout: time.Second * 2,
|
||||
}
|
||||
|
||||
start := time.Now()
|
||||
req, err := http.NewRequest("POST", fmt.Sprintf("http://localhost:%d/load", Default.AdminPort), strings.NewReader(rawConfig))
|
||||
if err != nil {
|
||||
t.Errorf("failed to create request. %s", err)
|
||||
return err
|
||||
}
|
||||
|
||||
if configType == "json" {
|
||||
req.Header.Add("Content-Type", "application/json")
|
||||
} else {
|
||||
req.Header.Add("Content-Type", "text/"+configType)
|
||||
}
|
||||
|
||||
res, err := client.Do(req)
|
||||
if err != nil {
|
||||
t.Errorf("unable to contact caddy server. %s", err)
|
||||
return err
|
||||
}
|
||||
timeElapsed(start, "caddytest: config load time")
|
||||
|
||||
defer res.Body.Close()
|
||||
body, err := ioutil.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
t.Errorf("unable to read response. %s", err)
|
||||
return err
|
||||
}
|
||||
|
||||
if res.StatusCode != 200 {
|
||||
return configLoadError{Response: string(body)}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
var hasValidated bool
|
||||
var arePrerequisitesValid bool
|
||||
|
||||
func validateTestPrerequisites() error {
|
||||
|
||||
if hasValidated {
|
||||
if !arePrerequisitesValid {
|
||||
return errors.New("caddy integration prerequisites failed. see first error")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
hasValidated = true
|
||||
arePrerequisitesValid = false
|
||||
|
||||
// check certificates are found
|
||||
for _, certName := range Default.Certifcates {
|
||||
if _, err := os.Stat(getIntegrationDir() + certName); os.IsNotExist(err) {
|
||||
return fmt.Errorf("caddy integration test certificates (%s) not found", certName)
|
||||
}
|
||||
}
|
||||
|
||||
// assert that caddy is running
|
||||
client := &http.Client{
|
||||
Timeout: time.Second * 2,
|
||||
}
|
||||
_, err := client.Get(fmt.Sprintf("http://localhost:%d/config/", Default.AdminPort))
|
||||
if err != nil {
|
||||
return errors.New("caddy integration test caddy server not running. Expected to be listening on localhost:2019")
|
||||
}
|
||||
|
||||
arePrerequisitesValid = true
|
||||
return nil
|
||||
}
|
||||
|
||||
func getIntegrationDir() string {
|
||||
|
||||
_, filename, _, ok := runtime.Caller(1)
|
||||
if !ok {
|
||||
panic("unable to determine the current file path")
|
||||
}
|
||||
|
||||
return path.Dir(filename)
|
||||
}
|
||||
|
||||
// use the convention to replace /[certificatename].[crt|key] with the full path
|
||||
// this helps reduce the noise in test configurations and also allow this
|
||||
// to run in any path
|
||||
func prependCaddyFilePath(rawConfig string) string {
|
||||
r := matchKey.ReplaceAllString(rawConfig, getIntegrationDir()+"$1")
|
||||
r = matchCert.ReplaceAllString(r, getIntegrationDir()+"$1")
|
||||
return r
|
||||
}
|
||||
|
||||
// creates a testing transport that forces call dialing connections to happen locally
|
||||
func createTestingTransport() *http.Transport {
|
||||
|
||||
dialer := net.Dialer{
|
||||
Timeout: 5 * time.Second,
|
||||
KeepAlive: 5 * time.Second,
|
||||
DualStack: true,
|
||||
}
|
||||
|
||||
dialContext := func(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||
parts := strings.Split(addr, ":")
|
||||
destAddr := fmt.Sprintf("127.0.0.1:%s", parts[1])
|
||||
log.Printf("caddytest: redirecting the dialer from %s to %s", addr, destAddr)
|
||||
return dialer.DialContext(ctx, network, destAddr)
|
||||
}
|
||||
|
||||
return &http.Transport{
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
DialContext: dialContext,
|
||||
ForceAttemptHTTP2: true,
|
||||
MaxIdleConns: 100,
|
||||
IdleConnTimeout: 90 * time.Second,
|
||||
TLSHandshakeTimeout: 5 * time.Second,
|
||||
ExpectContinueTimeout: 1 * time.Second,
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||
}
|
||||
}
|
||||
|
||||
// AssertLoadError will load a config and expect an error
|
||||
func AssertLoadError(t *testing.T, rawConfig string, configType string, expectedError string) {
|
||||
err := initServer(t, rawConfig, configType)
|
||||
if !strings.Contains(err.Error(), expectedError) {
|
||||
t.Errorf("expected error \"%s\" but got \"%s\"", expectedError, err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// AssertGetResponse request a URI and assert the status code and the body contains a string
|
||||
func AssertGetResponse(t *testing.T, requestURI string, statusCode int, expectedBody string) (*http.Response, string) {
|
||||
resp, body := AssertGetResponseBody(t, requestURI, statusCode)
|
||||
if !strings.Contains(body, expectedBody) {
|
||||
t.Errorf("requesting \"%s\" expected response body \"%s\" but got \"%s\"", requestURI, expectedBody, body)
|
||||
}
|
||||
return resp, string(body)
|
||||
}
|
||||
|
||||
// AssertGetResponseBody request a URI and assert the status code matches
|
||||
func AssertGetResponseBody(t *testing.T, requestURI string, expectedStatusCode int) (*http.Response, string) {
|
||||
|
||||
client := &http.Client{
|
||||
Transport: createTestingTransport(),
|
||||
}
|
||||
|
||||
resp, err := client.Get(requestURI)
|
||||
if err != nil {
|
||||
t.Errorf("failed to call server %s", err)
|
||||
return nil, ""
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
if expectedStatusCode != resp.StatusCode {
|
||||
t.Errorf("requesting \"%s\" expected status code: %d but got %d", requestURI, expectedStatusCode, resp.StatusCode)
|
||||
}
|
||||
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
t.Errorf("unable to read the response body %s", err)
|
||||
return nil, ""
|
||||
}
|
||||
|
||||
return resp, string(body)
|
||||
}
|
||||
|
||||
// AssertRedirect makes a request and asserts the redirection happens
|
||||
func AssertRedirect(t *testing.T, requestURI string, expectedToLocation string, expectedStatusCode int) *http.Response {
|
||||
|
||||
redirectPolicyFunc := func(req *http.Request, via []*http.Request) error {
|
||||
return http.ErrUseLastResponse
|
||||
}
|
||||
|
||||
client := &http.Client{
|
||||
CheckRedirect: redirectPolicyFunc,
|
||||
Transport: createTestingTransport(),
|
||||
}
|
||||
|
||||
resp, err := client.Get(requestURI)
|
||||
if err != nil {
|
||||
t.Errorf("failed to call server %s", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
if expectedStatusCode != resp.StatusCode {
|
||||
t.Errorf("requesting \"%s\" expected status code: %d but got %d", requestURI, expectedStatusCode, resp.StatusCode)
|
||||
}
|
||||
|
||||
loc, err := resp.Location()
|
||||
if expectedToLocation != loc.String() {
|
||||
t.Errorf("requesting \"%s\" expected location: \"%s\" but got \"%s\"", requestURI, expectedToLocation, loc.String())
|
||||
}
|
||||
|
||||
return resp
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
package caddytest
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestReplaceCertificatePaths(t *testing.T) {
|
||||
rawConfig := `a.caddy.localhost:9443 {
|
||||
tls /caddy.localhost.crt /caddy.localhost.key {
|
||||
}
|
||||
|
||||
redir / https://b.caddy.localhost:9443/version 301
|
||||
|
||||
respond /version 200 {
|
||||
body "hello from a.caddy.localhost"
|
||||
}
|
||||
}`
|
||||
|
||||
r := prependCaddyFilePath(rawConfig)
|
||||
|
||||
if !strings.Contains(r, getIntegrationDir()+"/caddy.localhost.crt") {
|
||||
t.Error("expected the /caddy.localhost.crt to be expanded to include the full path")
|
||||
}
|
||||
|
||||
if !strings.Contains(r, getIntegrationDir()+"/caddy.localhost.key") {
|
||||
t.Error("expected the /caddy.localhost.crt to be expanded to include the full path")
|
||||
}
|
||||
|
||||
if !strings.Contains(r, "https://b.caddy.localhost:9443/version") {
|
||||
t.Error("expected redirect uri to be unchanged")
|
||||
}
|
||||
}
|
||||
@@ -1,136 +0,0 @@
|
||||
package integration
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/caddyserver/caddy/v2/caddytest"
|
||||
)
|
||||
|
||||
func TestRespond(t *testing.T) {
|
||||
|
||||
// arrange
|
||||
caddytest.InitServer(t, `
|
||||
{
|
||||
http_port 9080
|
||||
https_port 9443
|
||||
}
|
||||
|
||||
localhost:9080 {
|
||||
respond /version 200 {
|
||||
body "hello from localhost"
|
||||
}
|
||||
}
|
||||
`, "caddyfile")
|
||||
|
||||
// act and assert
|
||||
caddytest.AssertGetResponse(t, "http://localhost:9080/version", 200, "hello from localhost")
|
||||
}
|
||||
|
||||
func TestRedirect(t *testing.T) {
|
||||
|
||||
// arrange
|
||||
caddytest.InitServer(t, `
|
||||
{
|
||||
http_port 9080
|
||||
https_port 9443
|
||||
}
|
||||
|
||||
localhost:9080 {
|
||||
|
||||
redir / http://localhost:9080/hello 301
|
||||
|
||||
respond /hello 200 {
|
||||
body "hello from localhost"
|
||||
}
|
||||
}
|
||||
`, "caddyfile")
|
||||
|
||||
// act and assert
|
||||
caddytest.AssertRedirect(t, "http://localhost:9080/", "http://localhost:9080/hello", 301)
|
||||
|
||||
// follow redirect
|
||||
caddytest.AssertGetResponse(t, "http://localhost:9080/", 200, "hello from localhost")
|
||||
}
|
||||
|
||||
func TestDuplicateHosts(t *testing.T) {
|
||||
|
||||
// act and assert
|
||||
caddytest.AssertLoadError(t,
|
||||
`
|
||||
localhost:9080 {
|
||||
}
|
||||
|
||||
localhost:9080 {
|
||||
}
|
||||
`,
|
||||
"caddyfile",
|
||||
"duplicate site address not allowed")
|
||||
}
|
||||
|
||||
func TestDefaultSNI(t *testing.T) {
|
||||
|
||||
// arrange
|
||||
caddytest.InitServer(t, `
|
||||
{
|
||||
http_port 9080
|
||||
https_port 9443
|
||||
default_sni *.caddy.localhost
|
||||
}
|
||||
|
||||
127.0.0.1:9443 {
|
||||
tls /caddy.localhost.crt /caddy.localhost.key
|
||||
respond /version 200 {
|
||||
body "hello from a"
|
||||
}
|
||||
}
|
||||
`, "caddyfile")
|
||||
|
||||
// act and assert
|
||||
caddytest.AssertGetResponse(t, "https://127.0.0.1:9443/version", 200, "hello from a")
|
||||
}
|
||||
|
||||
func TestDefaultSNIWithNamedHostAndExplicitIP(t *testing.T) {
|
||||
|
||||
// arrange
|
||||
caddytest.InitServer(t, `
|
||||
{
|
||||
http_port 9080
|
||||
https_port 9443
|
||||
default_sni a.caddy.localhost
|
||||
}
|
||||
|
||||
a.caddy.localhost:9443, 127.0.0.1:9443 {
|
||||
tls /a.caddy.localhost.crt /a.caddy.localhost.key
|
||||
respond /version 200 {
|
||||
body "hello from a"
|
||||
}
|
||||
}
|
||||
`, "caddyfile")
|
||||
|
||||
// act and assert
|
||||
// makes a request with no sni
|
||||
caddytest.AssertGetResponse(t, "https://127.0.0.1:9443/version", 200, "hello from a")
|
||||
}
|
||||
|
||||
func TestDefaultSNIWithPortMappingOnly(t *testing.T) {
|
||||
|
||||
// arrange
|
||||
caddytest.InitServer(t, `
|
||||
{
|
||||
http_port 9080
|
||||
https_port 9443
|
||||
default_sni a.caddy.localhost
|
||||
}
|
||||
|
||||
:9443 {
|
||||
tls /a.caddy.localhost.crt /a.caddy.localhost.key
|
||||
respond /version 200 {
|
||||
body "hello from a.caddy.localhost"
|
||||
}
|
||||
}
|
||||
`, "caddyfile")
|
||||
|
||||
// act and assert
|
||||
// makes a request with no sni
|
||||
caddytest.AssertGetResponse(t, "https://127.0.0.1:9443/version", 200, "hello from a")
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
// 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 main is the entry point of the Caddy application.
|
||||
// Most of Caddy's functionality is provided through modules,
|
||||
// which can be plugged in by adding their import below.
|
||||
//
|
||||
// There is no need to modify the Caddy source code to customize your
|
||||
// builds. You can easily build a custom Caddy with these simple steps:
|
||||
//
|
||||
// 1. Copy this file (main.go) into a new folder
|
||||
// 2. Edit the imports below to include the modules you want plugged in
|
||||
// 3. Run `go mod init caddy`
|
||||
// 4. Run `go install` or `go build` - you now have a custom binary!
|
||||
//
|
||||
package main
|
||||
|
||||
import (
|
||||
caddycmd "github.com/caddyserver/caddy/v2/cmd"
|
||||
|
||||
// plug in Caddy modules here
|
||||
_ "github.com/caddyserver/caddy/v2/modules/standard"
|
||||
)
|
||||
|
||||
func main() {
|
||||
caddycmd.Main()
|
||||
}
|
||||
@@ -1,625 +0,0 @@
|
||||
// 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 caddycmd
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/rand"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"runtime/debug"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/caddyserver/caddy/v2"
|
||||
"github.com/caddyserver/caddy/v2/caddyconfig"
|
||||
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
|
||||
"github.com/caddyserver/certmagic"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func cmdStart(fl Flags) (int, error) {
|
||||
startCmdConfigFlag := fl.String("config")
|
||||
startCmdConfigAdapterFlag := fl.String("adapter")
|
||||
|
||||
// open a listener to which the child process will connect when
|
||||
// it is ready to confirm that it has successfully started
|
||||
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
return caddy.ExitCodeFailedStartup,
|
||||
fmt.Errorf("opening listener for success confirmation: %v", err)
|
||||
}
|
||||
defer ln.Close()
|
||||
|
||||
// craft the command with a pingback address and with a
|
||||
// pipe for its stdin, so we can tell it our confirmation
|
||||
// code that we expect so that some random port scan at
|
||||
// the most unfortunate time won't fool us into thinking
|
||||
// the child succeeded (i.e. the alternative is to just
|
||||
// wait for any connection on our listener, but better to
|
||||
// ensure it's the process we're expecting - we can be
|
||||
// sure by giving it some random bytes and having it echo
|
||||
// them back to us)
|
||||
cmd := exec.Command(os.Args[0], "run", "--pingback", ln.Addr().String())
|
||||
if startCmdConfigFlag != "" {
|
||||
cmd.Args = append(cmd.Args, "--config", startCmdConfigFlag)
|
||||
}
|
||||
if startCmdConfigAdapterFlag != "" {
|
||||
cmd.Args = append(cmd.Args, "--adapter", startCmdConfigAdapterFlag)
|
||||
}
|
||||
stdinpipe, err := cmd.StdinPipe()
|
||||
if err != nil {
|
||||
return caddy.ExitCodeFailedStartup,
|
||||
fmt.Errorf("creating stdin pipe: %v", err)
|
||||
}
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
|
||||
// generate the random bytes we'll send to the child process
|
||||
expect := make([]byte, 32)
|
||||
_, err = rand.Read(expect)
|
||||
if err != nil {
|
||||
return caddy.ExitCodeFailedStartup, fmt.Errorf("generating random confirmation bytes: %v", err)
|
||||
}
|
||||
|
||||
// begin writing the confirmation bytes to the child's
|
||||
// stdin; use a goroutine since the child hasn't been
|
||||
// started yet, and writing synchronously would result
|
||||
// in a deadlock
|
||||
go func() {
|
||||
stdinpipe.Write(expect)
|
||||
stdinpipe.Close()
|
||||
}()
|
||||
|
||||
// start the process
|
||||
err = cmd.Start()
|
||||
if err != nil {
|
||||
return caddy.ExitCodeFailedStartup, fmt.Errorf("starting caddy process: %v", err)
|
||||
}
|
||||
|
||||
// there are two ways we know we're done: either
|
||||
// the process will connect to our listener, or
|
||||
// it will exit with an error
|
||||
success, exit := make(chan struct{}), make(chan error)
|
||||
|
||||
// in one goroutine, we await the success of the child process
|
||||
go func() {
|
||||
for {
|
||||
conn, err := ln.Accept()
|
||||
if err != nil {
|
||||
if !strings.Contains(err.Error(), "use of closed network connection") {
|
||||
log.Println(err)
|
||||
}
|
||||
break
|
||||
}
|
||||
err = handlePingbackConn(conn, expect)
|
||||
if err == nil {
|
||||
close(success)
|
||||
break
|
||||
}
|
||||
log.Println(err)
|
||||
}
|
||||
}()
|
||||
|
||||
// in another goroutine, we await the failure of the child process
|
||||
go func() {
|
||||
err := cmd.Wait() // don't send on this line! Wait blocks, but send starts before it unblocks
|
||||
exit <- err // sending on separate line ensures select won't trigger until after Wait unblocks
|
||||
}()
|
||||
|
||||
// when one of the goroutines unblocks, we're done and can exit
|
||||
select {
|
||||
case <-success:
|
||||
fmt.Printf("Successfully started Caddy (pid=%d) - Caddy is running in the background\n", cmd.Process.Pid)
|
||||
case err := <-exit:
|
||||
return caddy.ExitCodeFailedStartup,
|
||||
fmt.Errorf("caddy process exited with error: %v", err)
|
||||
}
|
||||
|
||||
return caddy.ExitCodeSuccess, nil
|
||||
}
|
||||
|
||||
func cmdRun(fl Flags) (int, error) {
|
||||
runCmdConfigFlag := fl.String("config")
|
||||
runCmdConfigAdapterFlag := fl.String("adapter")
|
||||
runCmdResumeFlag := fl.Bool("resume")
|
||||
runCmdPrintEnvFlag := fl.Bool("environ")
|
||||
runCmdPingbackFlag := fl.String("pingback")
|
||||
|
||||
// if we are supposed to print the environment, do that first
|
||||
if runCmdPrintEnvFlag {
|
||||
printEnvironment()
|
||||
}
|
||||
|
||||
// TODO: This is TEMPORARY, until the RCs
|
||||
moveStorage()
|
||||
|
||||
// load the config, depending on flags
|
||||
var config []byte
|
||||
var err error
|
||||
if runCmdResumeFlag {
|
||||
config, err = ioutil.ReadFile(caddy.ConfigAutosavePath)
|
||||
if os.IsNotExist(err) {
|
||||
// not a bad error; just can't resume if autosave file doesn't exist
|
||||
caddy.Log().Info("no autosave file exists", zap.String("autosave_file", caddy.ConfigAutosavePath))
|
||||
runCmdResumeFlag = false
|
||||
} else if err != nil {
|
||||
return caddy.ExitCodeFailedStartup, err
|
||||
} else {
|
||||
caddy.Log().Info("resuming from last configuration", zap.String("autosave_file", caddy.ConfigAutosavePath))
|
||||
}
|
||||
}
|
||||
// we don't use 'else' here since this value might have been changed in 'if' block; i.e. not mutually exclusive
|
||||
if !runCmdResumeFlag {
|
||||
config, _, err = loadConfig(runCmdConfigFlag, runCmdConfigAdapterFlag)
|
||||
if err != nil {
|
||||
return caddy.ExitCodeFailedStartup, err
|
||||
}
|
||||
}
|
||||
|
||||
// set a fitting User-Agent for ACME requests
|
||||
goModule := caddy.GoModule()
|
||||
cleanModVersion := strings.TrimPrefix(goModule.Version, "v")
|
||||
certmagic.UserAgent = "Caddy/" + cleanModVersion
|
||||
|
||||
// run the initial config
|
||||
err = caddy.Load(config, true)
|
||||
if err != nil {
|
||||
return caddy.ExitCodeFailedStartup, fmt.Errorf("loading initial config: %v", err)
|
||||
}
|
||||
caddy.Log().Info("serving initial configuration")
|
||||
|
||||
// if we are to report to another process the successful start
|
||||
// of the server, do so now by echoing back contents of stdin
|
||||
if runCmdPingbackFlag != "" {
|
||||
confirmationBytes, err := ioutil.ReadAll(os.Stdin)
|
||||
if err != nil {
|
||||
return caddy.ExitCodeFailedStartup,
|
||||
fmt.Errorf("reading confirmation bytes from stdin: %v", err)
|
||||
}
|
||||
conn, err := net.Dial("tcp", runCmdPingbackFlag)
|
||||
if err != nil {
|
||||
return caddy.ExitCodeFailedStartup,
|
||||
fmt.Errorf("dialing confirmation address: %v", err)
|
||||
}
|
||||
defer conn.Close()
|
||||
_, err = conn.Write(confirmationBytes)
|
||||
if err != nil {
|
||||
return caddy.ExitCodeFailedStartup,
|
||||
fmt.Errorf("writing confirmation bytes to %s: %v", runCmdPingbackFlag, err)
|
||||
}
|
||||
}
|
||||
|
||||
// warn if the environment does not provide enough information about the disk
|
||||
hasXDG := os.Getenv("XDG_DATA_HOME") != "" &&
|
||||
os.Getenv("XDG_CONFIG_HOME") != "" &&
|
||||
os.Getenv("XDG_CACHE_HOME") != ""
|
||||
switch runtime.GOOS {
|
||||
case "windows":
|
||||
if os.Getenv("HOME") == "" && os.Getenv("USERPROFILE") == "" && !hasXDG {
|
||||
caddy.Log().Warn("neither HOME nor USERPROFILE environment variables are set - please fix; some assets might be stored in ./caddy")
|
||||
}
|
||||
case "plan9":
|
||||
if os.Getenv("home") == "" && !hasXDG {
|
||||
caddy.Log().Warn("$home environment variable is empty - please fix; some assets might be stored in ./caddy")
|
||||
}
|
||||
default:
|
||||
if os.Getenv("HOME") == "" && !hasXDG {
|
||||
caddy.Log().Warn("$HOME environment variable is empty - please fix; some assets might be stored in ./caddy")
|
||||
}
|
||||
}
|
||||
|
||||
select {}
|
||||
}
|
||||
|
||||
func cmdStop(fl Flags) (int, error) {
|
||||
stopCmdAddrFlag := fl.String("address")
|
||||
|
||||
adminAddr := caddy.DefaultAdminListen
|
||||
if stopCmdAddrFlag != "" {
|
||||
adminAddr = stopCmdAddrFlag
|
||||
}
|
||||
stopEndpoint := fmt.Sprintf("http://%s/stop", adminAddr)
|
||||
|
||||
req, err := http.NewRequest(http.MethodPost, stopEndpoint, nil)
|
||||
if err != nil {
|
||||
return caddy.ExitCodeFailedStartup, fmt.Errorf("making request: %v", err)
|
||||
}
|
||||
req.Header.Set("Origin", adminAddr)
|
||||
|
||||
err = apiRequest(req)
|
||||
if err != nil {
|
||||
caddy.Log().Warn("failed using API to stop instance",
|
||||
zap.String("endpoint", stopEndpoint),
|
||||
zap.Error(err),
|
||||
)
|
||||
return caddy.ExitCodeFailedStartup, err
|
||||
}
|
||||
|
||||
return caddy.ExitCodeSuccess, nil
|
||||
}
|
||||
|
||||
func cmdReload(fl Flags) (int, error) {
|
||||
reloadCmdConfigFlag := fl.String("config")
|
||||
reloadCmdConfigAdapterFlag := fl.String("adapter")
|
||||
reloadCmdAddrFlag := fl.String("address")
|
||||
|
||||
// get the config in caddy's native format
|
||||
config, hasConfig, err := loadConfig(reloadCmdConfigFlag, reloadCmdConfigAdapterFlag)
|
||||
if err != nil {
|
||||
return caddy.ExitCodeFailedStartup, err
|
||||
}
|
||||
if !hasConfig {
|
||||
return caddy.ExitCodeFailedStartup, fmt.Errorf("no config file to load")
|
||||
}
|
||||
|
||||
// get the address of the admin listener and craft endpoint URL
|
||||
adminAddr := reloadCmdAddrFlag
|
||||
if adminAddr == "" && len(config) > 0 {
|
||||
var tmpStruct struct {
|
||||
Admin caddy.AdminConfig `json:"admin"`
|
||||
}
|
||||
err = json.Unmarshal(config, &tmpStruct)
|
||||
if err != nil {
|
||||
return caddy.ExitCodeFailedStartup,
|
||||
fmt.Errorf("unmarshaling admin listener address from config: %v", err)
|
||||
}
|
||||
adminAddr = tmpStruct.Admin.Listen
|
||||
}
|
||||
if adminAddr == "" {
|
||||
adminAddr = caddy.DefaultAdminListen
|
||||
}
|
||||
loadEndpoint := fmt.Sprintf("http://%s/load", adminAddr)
|
||||
|
||||
// prepare the request to update the configuration
|
||||
req, err := http.NewRequest(http.MethodPost, loadEndpoint, bytes.NewReader(config))
|
||||
if err != nil {
|
||||
return caddy.ExitCodeFailedStartup, fmt.Errorf("making request: %v", err)
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Origin", adminAddr)
|
||||
|
||||
err = apiRequest(req)
|
||||
if err != nil {
|
||||
return caddy.ExitCodeFailedStartup, fmt.Errorf("sending configuration to instance: %v", err)
|
||||
}
|
||||
|
||||
return caddy.ExitCodeSuccess, nil
|
||||
}
|
||||
|
||||
func cmdVersion(_ Flags) (int, error) {
|
||||
goModule := caddy.GoModule()
|
||||
fmt.Print(goModule.Version)
|
||||
if goModule.Sum != "" {
|
||||
// a build with a known version will also have a checksum
|
||||
fmt.Printf(" %s", goModule.Sum)
|
||||
}
|
||||
if goModule.Replace != nil {
|
||||
fmt.Printf(" => %s", goModule.Replace.Path)
|
||||
if goModule.Replace.Version != "" {
|
||||
fmt.Printf(" %s", goModule.Replace.Version)
|
||||
}
|
||||
}
|
||||
fmt.Println()
|
||||
return caddy.ExitCodeSuccess, nil
|
||||
}
|
||||
|
||||
func cmdBuildInfo(fl Flags) (int, error) {
|
||||
bi, ok := debug.ReadBuildInfo()
|
||||
if !ok {
|
||||
return caddy.ExitCodeFailedStartup, fmt.Errorf("no build information")
|
||||
}
|
||||
|
||||
fmt.Printf("path: %s\n", bi.Path)
|
||||
fmt.Printf("main: %s %s %s\n", bi.Main.Path, bi.Main.Version, bi.Main.Sum)
|
||||
fmt.Println("dependencies:")
|
||||
|
||||
for _, goMod := range bi.Deps {
|
||||
fmt.Printf("%s %s %s", goMod.Path, goMod.Version, goMod.Sum)
|
||||
if goMod.Replace != nil {
|
||||
fmt.Printf(" => %s %s %s", goMod.Replace.Path, goMod.Replace.Version, goMod.Replace.Sum)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
return caddy.ExitCodeSuccess, nil
|
||||
}
|
||||
|
||||
func cmdListModules(fl Flags) (int, error) {
|
||||
versions := fl.Bool("versions")
|
||||
|
||||
bi, ok := debug.ReadBuildInfo()
|
||||
if !ok || !versions {
|
||||
// if there's no build information,
|
||||
// just print out the modules
|
||||
for _, m := range caddy.Modules() {
|
||||
fmt.Println(m)
|
||||
}
|
||||
return caddy.ExitCodeSuccess, nil
|
||||
}
|
||||
|
||||
for _, modID := range caddy.Modules() {
|
||||
modInfo, err := caddy.GetModule(modID)
|
||||
if err != nil {
|
||||
// that's weird
|
||||
fmt.Println(modID)
|
||||
continue
|
||||
}
|
||||
|
||||
// to get the Caddy plugin's version info, we need to know
|
||||
// the package that the Caddy module's value comes from; we
|
||||
// can use reflection but we need a non-pointer value (I'm
|
||||
// not sure why), and since New() should return a pointer
|
||||
// value, we need to dereference it first
|
||||
iface := interface{}(modInfo.New())
|
||||
if rv := reflect.ValueOf(iface); rv.Kind() == reflect.Ptr {
|
||||
iface = reflect.New(reflect.TypeOf(iface).Elem()).Elem().Interface()
|
||||
}
|
||||
modPkgPath := reflect.TypeOf(iface).PkgPath()
|
||||
|
||||
// now we find the Go module that the Caddy module's package
|
||||
// belongs to; we assume the Caddy module package path will
|
||||
// be prefixed by its Go module path, and we will choose the
|
||||
// longest matching prefix in case there are nested modules
|
||||
var matched *debug.Module
|
||||
for _, dep := range bi.Deps {
|
||||
if strings.HasPrefix(modPkgPath, dep.Path) {
|
||||
if matched == nil || len(dep.Path) > len(matched.Path) {
|
||||
matched = dep
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// if we could find no matching module, just print out
|
||||
// the module ID instead
|
||||
if matched == nil {
|
||||
fmt.Println(modID)
|
||||
continue
|
||||
}
|
||||
|
||||
fmt.Printf("%s %s\n", modID, matched.Version)
|
||||
}
|
||||
|
||||
return caddy.ExitCodeSuccess, nil
|
||||
}
|
||||
|
||||
func cmdEnviron(_ Flags) (int, error) {
|
||||
printEnvironment()
|
||||
return caddy.ExitCodeSuccess, nil
|
||||
}
|
||||
|
||||
func cmdAdaptConfig(fl Flags) (int, error) {
|
||||
adaptCmdInputFlag := fl.String("config")
|
||||
adaptCmdAdapterFlag := fl.String("adapter")
|
||||
adaptCmdPrettyFlag := fl.Bool("pretty")
|
||||
adaptCmdValidateFlag := fl.Bool("validate")
|
||||
|
||||
// if no input file was specified, try a default
|
||||
// Caddyfile if the Caddyfile adapter is plugged in
|
||||
if adaptCmdInputFlag == "" && caddyconfig.GetAdapter("caddyfile") != nil {
|
||||
_, err := os.Stat("Caddyfile")
|
||||
if err == nil {
|
||||
// default Caddyfile exists
|
||||
adaptCmdInputFlag = "Caddyfile"
|
||||
caddy.Log().Info("using adjacent Caddyfile")
|
||||
} else if !os.IsNotExist(err) {
|
||||
// default Caddyfile exists, but error accessing it
|
||||
return caddy.ExitCodeFailedStartup, fmt.Errorf("accessing default Caddyfile: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if adaptCmdInputFlag == "" {
|
||||
return caddy.ExitCodeFailedStartup,
|
||||
fmt.Errorf("input file required when there is no Caddyfile in current directory (use --config flag)")
|
||||
}
|
||||
if adaptCmdAdapterFlag == "" {
|
||||
return caddy.ExitCodeFailedStartup,
|
||||
fmt.Errorf("adapter name is required (use --adapt flag or leave unspecified for default)")
|
||||
}
|
||||
|
||||
cfgAdapter := caddyconfig.GetAdapter(adaptCmdAdapterFlag)
|
||||
if cfgAdapter == nil {
|
||||
return caddy.ExitCodeFailedStartup,
|
||||
fmt.Errorf("unrecognized config adapter: %s", adaptCmdAdapterFlag)
|
||||
}
|
||||
|
||||
input, err := ioutil.ReadFile(adaptCmdInputFlag)
|
||||
if err != nil {
|
||||
return caddy.ExitCodeFailedStartup,
|
||||
fmt.Errorf("reading input file: %v", err)
|
||||
}
|
||||
|
||||
opts := make(map[string]interface{})
|
||||
if adaptCmdPrettyFlag {
|
||||
opts["pretty"] = "true"
|
||||
}
|
||||
opts["filename"] = adaptCmdInputFlag
|
||||
|
||||
adaptedConfig, warnings, err := cfgAdapter.Adapt(input, opts)
|
||||
if err != nil {
|
||||
return caddy.ExitCodeFailedStartup, err
|
||||
}
|
||||
|
||||
// print warnings to stderr
|
||||
for _, warn := range warnings {
|
||||
msg := warn.Message
|
||||
if warn.Directive != "" {
|
||||
msg = fmt.Sprintf("%s: %s", warn.Directive, warn.Message)
|
||||
}
|
||||
fmt.Fprintf(os.Stderr, "[WARNING][%s] %s:%d: %s\n", adaptCmdAdapterFlag, warn.File, warn.Line, msg)
|
||||
}
|
||||
|
||||
// print result to stdout
|
||||
fmt.Println(string(adaptedConfig))
|
||||
|
||||
// validate output if requested
|
||||
if adaptCmdValidateFlag {
|
||||
var cfg *caddy.Config
|
||||
err = json.Unmarshal(adaptedConfig, &cfg)
|
||||
if err != nil {
|
||||
return caddy.ExitCodeFailedStartup, fmt.Errorf("decoding config: %v", err)
|
||||
}
|
||||
err = caddy.Validate(cfg)
|
||||
if err != nil {
|
||||
return caddy.ExitCodeFailedStartup, fmt.Errorf("validation: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
return caddy.ExitCodeSuccess, nil
|
||||
}
|
||||
|
||||
func cmdValidateConfig(fl Flags) (int, error) {
|
||||
validateCmdConfigFlag := fl.String("config")
|
||||
validateCmdAdapterFlag := fl.String("adapter")
|
||||
|
||||
input, _, err := loadConfig(validateCmdConfigFlag, validateCmdAdapterFlag)
|
||||
if err != nil {
|
||||
return caddy.ExitCodeFailedStartup, err
|
||||
}
|
||||
input = caddy.RemoveMetaFields(input)
|
||||
|
||||
var cfg *caddy.Config
|
||||
err = json.Unmarshal(input, &cfg)
|
||||
if err != nil {
|
||||
return caddy.ExitCodeFailedStartup, fmt.Errorf("decoding config: %v", err)
|
||||
}
|
||||
|
||||
err = caddy.Validate(cfg)
|
||||
if err != nil {
|
||||
return caddy.ExitCodeFailedStartup, err
|
||||
}
|
||||
|
||||
fmt.Println("Valid configuration")
|
||||
|
||||
return caddy.ExitCodeSuccess, nil
|
||||
}
|
||||
|
||||
func cmdFmt(fl Flags) (int, error) {
|
||||
formatCmdConfigFile := fl.Arg(0)
|
||||
if formatCmdConfigFile == "" {
|
||||
formatCmdConfigFile = "Caddyfile"
|
||||
}
|
||||
overwrite := fl.Bool("overwrite")
|
||||
|
||||
input, err := ioutil.ReadFile(formatCmdConfigFile)
|
||||
if err != nil {
|
||||
return caddy.ExitCodeFailedStartup,
|
||||
fmt.Errorf("reading input file: %v", err)
|
||||
}
|
||||
|
||||
output := caddyfile.Format(input)
|
||||
|
||||
if overwrite {
|
||||
err = ioutil.WriteFile(formatCmdConfigFile, output, 0644)
|
||||
if err != nil {
|
||||
return caddy.ExitCodeFailedStartup, nil
|
||||
}
|
||||
} else {
|
||||
fmt.Print(string(output))
|
||||
}
|
||||
|
||||
return caddy.ExitCodeSuccess, nil
|
||||
}
|
||||
|
||||
func cmdHelp(fl Flags) (int, error) {
|
||||
const fullDocs = `Full documentation is available at:
|
||||
https://caddyserver.com/docs/command-line`
|
||||
|
||||
args := fl.Args()
|
||||
if len(args) == 0 {
|
||||
s := `Caddy is an extensible server platform.
|
||||
|
||||
usage:
|
||||
caddy <command> [<args...>]
|
||||
|
||||
commands:
|
||||
`
|
||||
keys := make([]string, 0, len(commands))
|
||||
for k := range commands {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
for _, k := range keys {
|
||||
cmd := commands[k]
|
||||
short := strings.TrimSuffix(cmd.Short, ".")
|
||||
s += fmt.Sprintf(" %-15s %s\n", cmd.Name, short)
|
||||
}
|
||||
|
||||
s += "\nUse 'caddy help <command>' for more information about a command.\n"
|
||||
s += "\n" + fullDocs + "\n"
|
||||
|
||||
fmt.Print(s)
|
||||
|
||||
return caddy.ExitCodeSuccess, nil
|
||||
} else if len(args) > 1 {
|
||||
return caddy.ExitCodeFailedStartup, fmt.Errorf("can only give help with one command")
|
||||
}
|
||||
|
||||
subcommand, ok := commands[args[0]]
|
||||
if !ok {
|
||||
return caddy.ExitCodeFailedStartup, fmt.Errorf("unknown command: %s", args[0])
|
||||
}
|
||||
|
||||
helpText := strings.TrimSpace(subcommand.Long)
|
||||
if helpText == "" {
|
||||
helpText = subcommand.Short
|
||||
if !strings.HasSuffix(helpText, ".") {
|
||||
helpText += "."
|
||||
}
|
||||
}
|
||||
|
||||
result := fmt.Sprintf("%s\n\nusage:\n caddy %s %s\n",
|
||||
helpText,
|
||||
subcommand.Name,
|
||||
strings.TrimSpace(subcommand.Usage),
|
||||
)
|
||||
|
||||
if help := flagHelp(subcommand.Flags); help != "" {
|
||||
result += fmt.Sprintf("\nflags:\n%s", help)
|
||||
}
|
||||
|
||||
result += "\n" + fullDocs + "\n"
|
||||
|
||||
fmt.Print(result)
|
||||
|
||||
return caddy.ExitCodeSuccess, nil
|
||||
}
|
||||
|
||||
func apiRequest(req *http.Request) error {
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return fmt.Errorf("performing request: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// if it didn't work, let the user know
|
||||
if resp.StatusCode >= 400 {
|
||||
respBody, err := ioutil.ReadAll(io.LimitReader(resp.Body, 1024*10))
|
||||
if err != nil {
|
||||
return fmt.Errorf("HTTP %d: reading error message: %v", resp.StatusCode, err)
|
||||
}
|
||||
return fmt.Errorf("caddy responded with error: HTTP %d: %s", resp.StatusCode, respBody)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
-298
@@ -1,298 +0,0 @@
|
||||
// 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 caddycmd
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
// Command represents a subcommand. Name, Func,
|
||||
// and Short are required.
|
||||
type Command struct {
|
||||
// The name of the subcommand. Must conform to the
|
||||
// format described by the RegisterCommand() godoc.
|
||||
// Required.
|
||||
Name string
|
||||
|
||||
// Run is a function that executes a subcommand using
|
||||
// the parsed flags. It returns an exit code and any
|
||||
// associated error.
|
||||
// Required.
|
||||
Func CommandFunc
|
||||
|
||||
// Usage is a brief message describing the syntax of
|
||||
// the subcommand's flags and args. Use [] to indicate
|
||||
// optional parameters and <> to enclose literal values
|
||||
// intended to be replaced by the user. Do not prefix
|
||||
// the string with "caddy" or the name of the command
|
||||
// since these will be prepended for you; only include
|
||||
// the actual parameters for this command.
|
||||
Usage string
|
||||
|
||||
// Short is a one-line message explaining what the
|
||||
// command does. Should not end with punctuation.
|
||||
// Required.
|
||||
Short string
|
||||
|
||||
// Long is the full help text shown to the user.
|
||||
// Will be trimmed of whitespace on both ends before
|
||||
// being printed.
|
||||
Long string
|
||||
|
||||
// Flags is the flagset for command.
|
||||
Flags *flag.FlagSet
|
||||
}
|
||||
|
||||
// CommandFunc is a command's function. It runs the
|
||||
// command and returns the proper exit code along with
|
||||
// any error that occurred.
|
||||
type CommandFunc func(Flags) (int, error)
|
||||
|
||||
var commands = make(map[string]Command)
|
||||
|
||||
func init() {
|
||||
RegisterCommand(Command{
|
||||
Name: "help",
|
||||
Func: cmdHelp,
|
||||
Usage: "<command>",
|
||||
Short: "Shows help for a Caddy subcommand",
|
||||
})
|
||||
|
||||
RegisterCommand(Command{
|
||||
Name: "start",
|
||||
Func: cmdStart,
|
||||
Usage: "[--config <path> [[--adapter <name>]]",
|
||||
Short: "Starts the Caddy process in the background and then returns",
|
||||
Long: `
|
||||
Starts the Caddy process, optionally bootstrapped with an initial config file.
|
||||
This command unblocks after the server starts running or fails to run.
|
||||
|
||||
On Windows, the spawned child process will remain attached to the terminal, so
|
||||
closing the window will forcefully stop Caddy; to avoid forgetting this, try
|
||||
using 'caddy run' instead to keep it in the foreground.`,
|
||||
Flags: func() *flag.FlagSet {
|
||||
fs := flag.NewFlagSet("start", flag.ExitOnError)
|
||||
fs.String("config", "", "Configuration file")
|
||||
fs.String("adapter", "", "Name of config adapter to apply")
|
||||
return fs
|
||||
}(),
|
||||
})
|
||||
|
||||
RegisterCommand(Command{
|
||||
Name: "run",
|
||||
Func: cmdRun,
|
||||
Usage: "[--config <path> [--adapter <name>]] [--environ]",
|
||||
Short: `Starts the Caddy process and blocks indefinitely`,
|
||||
Long: `
|
||||
Starts the Caddy process, optionally bootstrapped with an initial config file,
|
||||
and blocks indefinitely until the server is stopped; i.e. runs Caddy in
|
||||
"daemon" mode (foreground).
|
||||
|
||||
If a config file is specified, it will be applied immediately after the process
|
||||
is running. If the config file is not in Caddy's native JSON format, you can
|
||||
specify an adapter with --adapter to adapt the given config file to
|
||||
Caddy's native format. The config adapter must be a registered module. Any
|
||||
warnings will be printed to the log, but beware that any adaptation without
|
||||
errors will immediately be used. If you want to review the results of the
|
||||
adaptation first, use the 'adapt' subcommand.
|
||||
|
||||
As a special case, if the current working directory has a file called
|
||||
"Caddyfile" and the caddyfile config adapter is plugged in (default), then
|
||||
that file will be loaded and used to configure Caddy, even without any command
|
||||
line flags.
|
||||
|
||||
If --environ is specified, the environment as seen by the Caddy process will
|
||||
be printed before starting. This is the same as the environ command but does
|
||||
not quit after printing, and can be useful for troubleshooting.
|
||||
|
||||
The --resume flag will override the --config flag if there is a config auto-
|
||||
save file. It is not an error if --resume is used and no autosave file exists.`,
|
||||
Flags: func() *flag.FlagSet {
|
||||
fs := flag.NewFlagSet("run", flag.ExitOnError)
|
||||
fs.String("config", "", "Configuration file")
|
||||
fs.String("adapter", "", "Name of config adapter to apply")
|
||||
fs.Bool("resume", false, "Use saved config, if any (and prefer over --config file)")
|
||||
fs.Bool("environ", false, "Print environment")
|
||||
fs.String("pingback", "", "Echo confirmation bytes to this address on success")
|
||||
return fs
|
||||
}(),
|
||||
})
|
||||
|
||||
RegisterCommand(Command{
|
||||
Name: "stop",
|
||||
Func: cmdStop,
|
||||
Short: "Gracefully stops a started Caddy process",
|
||||
Long: `
|
||||
Stops the background Caddy process as gracefully as possible.
|
||||
|
||||
It requires that the admin API is enabled and accessible, since it will
|
||||
use the API's /stop endpoint. The address of this request can be
|
||||
customized using the --address flag if it is not the default.`,
|
||||
Flags: func() *flag.FlagSet {
|
||||
fs := flag.NewFlagSet("stop", flag.ExitOnError)
|
||||
fs.String("address", "", "The address to use to reach the admin API endpoint, if not the default")
|
||||
return fs
|
||||
}(),
|
||||
})
|
||||
|
||||
RegisterCommand(Command{
|
||||
Name: "reload",
|
||||
Func: cmdReload,
|
||||
Usage: "--config <path> [--adapter <name>] [--address <interface>]",
|
||||
Short: "Changes the config of the running Caddy instance",
|
||||
Long: `
|
||||
Gives the running Caddy instance a new configuration. This has the same effect
|
||||
as POSTing a document to the /load API endpoint, but is convenient for simple
|
||||
workflows revolving around config files.
|
||||
|
||||
Since the admin endpoint is configurable, the endpoint configuration is loaded
|
||||
from the --address flag if specified; otherwise it is loaded from the given
|
||||
config file; otherwise the default is assumed.`,
|
||||
Flags: func() *flag.FlagSet {
|
||||
fs := flag.NewFlagSet("reload", flag.ExitOnError)
|
||||
fs.String("config", "", "Configuration file (required)")
|
||||
fs.String("adapter", "", "Name of config adapter to apply")
|
||||
fs.String("address", "", "Address of the administration listener, if different from config")
|
||||
return fs
|
||||
}(),
|
||||
})
|
||||
|
||||
RegisterCommand(Command{
|
||||
Name: "version",
|
||||
Func: cmdVersion,
|
||||
Short: "Prints the version",
|
||||
})
|
||||
|
||||
RegisterCommand(Command{
|
||||
Name: "list-modules",
|
||||
Func: cmdListModules,
|
||||
Usage: "[--versions]",
|
||||
Short: "Lists the installed Caddy modules",
|
||||
Flags: func() *flag.FlagSet {
|
||||
fs := flag.NewFlagSet("list-modules", flag.ExitOnError)
|
||||
fs.Bool("versions", false, "Print version information")
|
||||
return fs
|
||||
}(),
|
||||
})
|
||||
|
||||
RegisterCommand(Command{
|
||||
Name: "build-info",
|
||||
Func: cmdBuildInfo,
|
||||
Short: "Prints information about this build",
|
||||
})
|
||||
|
||||
RegisterCommand(Command{
|
||||
Name: "environ",
|
||||
Func: cmdEnviron,
|
||||
Short: "Prints the environment",
|
||||
})
|
||||
|
||||
RegisterCommand(Command{
|
||||
Name: "adapt",
|
||||
Func: cmdAdaptConfig,
|
||||
Usage: "--config <path> [--adapter <name>] [--pretty] [--validate]",
|
||||
Short: "Adapts a configuration to Caddy's native JSON",
|
||||
Long: `
|
||||
Adapts a configuration to Caddy's native JSON format and writes the
|
||||
output to stdout, along with any warnings to stderr.
|
||||
|
||||
If --pretty is specified, the output will be formatted with indentation
|
||||
for human readability.
|
||||
|
||||
If --validate is used, the adapted config will be checked for validity.
|
||||
If the config is invalid, an error will be printed to stderr and a non-
|
||||
zero exit status will be returned.`,
|
||||
Flags: func() *flag.FlagSet {
|
||||
fs := flag.NewFlagSet("adapt", flag.ExitOnError)
|
||||
fs.String("config", "", "Configuration file to adapt (required)")
|
||||
fs.String("adapter", "caddyfile", "Name of config adapter")
|
||||
fs.Bool("pretty", false, "Format the output for human readability")
|
||||
fs.Bool("validate", false, "Validate the output")
|
||||
return fs
|
||||
}(),
|
||||
})
|
||||
|
||||
RegisterCommand(Command{
|
||||
Name: "validate",
|
||||
Func: cmdValidateConfig,
|
||||
Usage: "--config <path> [--adapter <name>]",
|
||||
Short: "Tests whether a configuration file is valid",
|
||||
Long: `
|
||||
Loads and provisions the provided config, but does not start running it.
|
||||
This reveals any errors with the configuration through the loading and
|
||||
provisioning stages.`,
|
||||
Flags: func() *flag.FlagSet {
|
||||
fs := flag.NewFlagSet("load", flag.ExitOnError)
|
||||
fs.String("config", "", "Input configuration file")
|
||||
fs.String("adapter", "", "Name of config adapter")
|
||||
return fs
|
||||
}(),
|
||||
})
|
||||
|
||||
RegisterCommand(Command{
|
||||
Name: "fmt",
|
||||
Func: cmdFmt,
|
||||
Usage: "[--overwrite] [<path>]",
|
||||
Short: "Formats a Caddyfile",
|
||||
Long: `
|
||||
Formats the Caddyfile by adding proper indentation and spaces to improve
|
||||
human readability. It prints the result to stdout.
|
||||
|
||||
If --write is specified, the output will be written to the config file
|
||||
directly instead of printing it.`,
|
||||
Flags: func() *flag.FlagSet {
|
||||
fs := flag.NewFlagSet("format", flag.ExitOnError)
|
||||
fs.Bool("overwrite", false, "Overwrite the input file with the results")
|
||||
return fs
|
||||
}(),
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
// RegisterCommand registers the command cmd.
|
||||
// cmd.Name must be unique and conform to the
|
||||
// following format:
|
||||
//
|
||||
// - lowercase
|
||||
// - alphanumeric and hyphen characters only
|
||||
// - cannot start or end with a hyphen
|
||||
// - hyphen cannot be adjacent to another hyphen
|
||||
//
|
||||
// This function panics if the name is already registered,
|
||||
// if the name does not meet the described format, or if
|
||||
// any of the fields are missing from cmd.
|
||||
//
|
||||
// This function should be used in init().
|
||||
func RegisterCommand(cmd Command) {
|
||||
if cmd.Name == "" {
|
||||
panic("command name is required")
|
||||
}
|
||||
if cmd.Func == nil {
|
||||
panic("command function missing")
|
||||
}
|
||||
if cmd.Short == "" {
|
||||
panic("command short string is required")
|
||||
}
|
||||
if _, exists := commands[cmd.Name]; exists {
|
||||
panic("command already registered: " + cmd.Name)
|
||||
}
|
||||
if !commandNameRegex.MatchString(cmd.Name) {
|
||||
panic("invalid command name")
|
||||
}
|
||||
commands[cmd.Name] = cmd
|
||||
}
|
||||
|
||||
var commandNameRegex = regexp.MustCompile(`^[a-z0-9]$|^([a-z0-9]+-?[a-z0-9]*)+[a-z0-9]$`)
|
||||
-331
@@ -1,331 +0,0 @@
|
||||
// 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 caddycmd
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/caddyserver/caddy/v2"
|
||||
"github.com/caddyserver/caddy/v2/caddyconfig"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// Main implements the main function of the caddy command.
|
||||
// Call this if Caddy is to be the main() if your program.
|
||||
func Main() {
|
||||
caddy.TrapSignals()
|
||||
|
||||
switch len(os.Args) {
|
||||
case 0:
|
||||
fmt.Printf("[FATAL] no arguments provided by OS; args[0] must be command\n")
|
||||
os.Exit(caddy.ExitCodeFailedStartup)
|
||||
case 1:
|
||||
os.Args = append(os.Args, "help")
|
||||
}
|
||||
|
||||
subcommandName := os.Args[1]
|
||||
subcommand, ok := commands[subcommandName]
|
||||
if !ok {
|
||||
if strings.HasPrefix(os.Args[1], "-") {
|
||||
// user probably forgot to type the subcommand
|
||||
fmt.Println("[ERROR] first argument must be a subcommand; see 'caddy help'")
|
||||
} else {
|
||||
fmt.Printf("[ERROR] '%s' is not a recognized subcommand; see 'caddy help'\n", os.Args[1])
|
||||
}
|
||||
os.Exit(caddy.ExitCodeFailedStartup)
|
||||
}
|
||||
|
||||
fs := subcommand.Flags
|
||||
if fs == nil {
|
||||
fs = flag.NewFlagSet(subcommand.Name, flag.ExitOnError)
|
||||
}
|
||||
|
||||
err := fs.Parse(os.Args[2:])
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(caddy.ExitCodeFailedStartup)
|
||||
}
|
||||
|
||||
exitCode, err := subcommand.Func(Flags{fs})
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "%s: %v\n", subcommand.Name, err)
|
||||
}
|
||||
|
||||
os.Exit(exitCode)
|
||||
}
|
||||
|
||||
// handlePingbackConn reads from conn and ensures it matches
|
||||
// the bytes in expect, or returns an error if it doesn't.
|
||||
func handlePingbackConn(conn net.Conn, expect []byte) error {
|
||||
defer conn.Close()
|
||||
confirmationBytes, err := ioutil.ReadAll(io.LimitReader(conn, 32))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !bytes.Equal(confirmationBytes, expect) {
|
||||
return fmt.Errorf("wrong confirmation: %x", confirmationBytes)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// loadConfig loads the config from configFile and adapts it
|
||||
// using adapterName. If adapterName is specified, configFile
|
||||
// must be also. If no configFile is specified, it tries
|
||||
// loading a default config file. The lack of a config file is
|
||||
// not treated as an error, but false will be returned if
|
||||
// there is no config available. It prints any warnings to stderr,
|
||||
// and returns the resulting JSON config bytes along with
|
||||
// whether a config file was loaded or not.
|
||||
func loadConfig(configFile, adapterName string) ([]byte, bool, error) {
|
||||
// specifying an adapter without a config file is ambiguous
|
||||
if adapterName != "" && configFile == "" {
|
||||
return nil, false, fmt.Errorf("cannot adapt config without config file (use --config)")
|
||||
}
|
||||
|
||||
// load initial config and adapter
|
||||
var config []byte
|
||||
var cfgAdapter caddyconfig.Adapter
|
||||
var err error
|
||||
if configFile != "" {
|
||||
config, err = ioutil.ReadFile(configFile)
|
||||
if err != nil {
|
||||
return nil, false, fmt.Errorf("reading config file: %v", err)
|
||||
}
|
||||
caddy.Log().Info("using provided configuration",
|
||||
zap.String("config_file", configFile),
|
||||
zap.String("config_adapter", adapterName))
|
||||
} else if adapterName == "" {
|
||||
// as a special case when no config file or adapter
|
||||
// is specified, see if the Caddyfile adapter is
|
||||
// plugged in, and if so, try using a default Caddyfile
|
||||
cfgAdapter = caddyconfig.GetAdapter("caddyfile")
|
||||
if cfgAdapter != nil {
|
||||
config, err = ioutil.ReadFile("Caddyfile")
|
||||
if os.IsNotExist(err) {
|
||||
// okay, no default Caddyfile; pretend like this never happened
|
||||
cfgAdapter = nil
|
||||
} else if err != nil {
|
||||
// default Caddyfile exists, but error reading it
|
||||
return nil, false, fmt.Errorf("reading default Caddyfile: %v", err)
|
||||
} else {
|
||||
// success reading default Caddyfile
|
||||
configFile = "Caddyfile"
|
||||
caddy.Log().Info("using adjacent Caddyfile")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// as a special case, if a config file called "Caddyfile" was
|
||||
// specified, and no adapter is specified, assume caddyfile adapter
|
||||
// for convenience
|
||||
if strings.HasPrefix(filepath.Base(configFile), "Caddyfile") &&
|
||||
filepath.Ext(configFile) != ".json" &&
|
||||
adapterName == "" {
|
||||
adapterName = "caddyfile"
|
||||
}
|
||||
|
||||
// load config adapter
|
||||
if adapterName != "" {
|
||||
cfgAdapter = caddyconfig.GetAdapter(adapterName)
|
||||
if cfgAdapter == nil {
|
||||
return nil, false, fmt.Errorf("unrecognized config adapter: %s", adapterName)
|
||||
}
|
||||
}
|
||||
|
||||
// adapt config
|
||||
if cfgAdapter != nil {
|
||||
adaptedConfig, warnings, err := cfgAdapter.Adapt(config, map[string]interface{}{
|
||||
"filename": configFile,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, false, fmt.Errorf("adapting config using %s: %v", adapterName, err)
|
||||
}
|
||||
for _, warn := range warnings {
|
||||
msg := warn.Message
|
||||
if warn.Directive != "" {
|
||||
msg = fmt.Sprintf("%s: %s", warn.Directive, warn.Message)
|
||||
}
|
||||
fmt.Printf("[WARNING][%s] %s:%d: %s\n", adapterName, warn.File, warn.Line, msg)
|
||||
}
|
||||
config = adaptedConfig
|
||||
}
|
||||
|
||||
return config, configFile != "", nil
|
||||
}
|
||||
|
||||
// Flags wraps a FlagSet so that typed values
|
||||
// from flags can be easily retrieved.
|
||||
type Flags struct {
|
||||
*flag.FlagSet
|
||||
}
|
||||
|
||||
// String returns the string representation of the
|
||||
// flag given by name. It panics if the flag is not
|
||||
// in the flag set.
|
||||
func (f Flags) String(name string) string {
|
||||
return f.FlagSet.Lookup(name).Value.String()
|
||||
}
|
||||
|
||||
// Bool returns the boolean representation of the
|
||||
// flag given by name. It returns false if the flag
|
||||
// is not a boolean type. It panics if the flag is
|
||||
// not in the flag set.
|
||||
func (f Flags) Bool(name string) bool {
|
||||
val, _ := strconv.ParseBool(f.String(name))
|
||||
return val
|
||||
}
|
||||
|
||||
// Int returns the integer representation of the
|
||||
// flag given by name. It returns 0 if the flag
|
||||
// is not an integer type. It panics if the flag is
|
||||
// not in the flag set.
|
||||
func (f Flags) Int(name string) int {
|
||||
val, _ := strconv.ParseInt(f.String(name), 0, strconv.IntSize)
|
||||
return int(val)
|
||||
}
|
||||
|
||||
// Float64 returns the float64 representation of the
|
||||
// flag given by name. It returns false if the flag
|
||||
// is not a float63 type. It panics if the flag is
|
||||
// not in the flag set.
|
||||
func (f Flags) Float64(name string) float64 {
|
||||
val, _ := strconv.ParseFloat(f.String(name), 64)
|
||||
return val
|
||||
}
|
||||
|
||||
// Duration returns the duration representation of the
|
||||
// flag given by name. It returns false if the flag
|
||||
// is not a duration type. It panics if the flag is
|
||||
// not in the flag set.
|
||||
func (f Flags) Duration(name string) time.Duration {
|
||||
val, _ := time.ParseDuration(f.String(name))
|
||||
return val
|
||||
}
|
||||
|
||||
// flagHelp returns the help text for fs.
|
||||
func flagHelp(fs *flag.FlagSet) string {
|
||||
if fs == nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
// temporarily redirect output
|
||||
out := fs.Output()
|
||||
defer fs.SetOutput(out)
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
fs.SetOutput(buf)
|
||||
fs.PrintDefaults()
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func printEnvironment() {
|
||||
fmt.Printf("caddy.HomeDir=%s\n", caddy.HomeDir())
|
||||
fmt.Printf("caddy.AppDataDir=%s\n", caddy.AppDataDir())
|
||||
fmt.Printf("caddy.AppConfigDir=%s\n", caddy.AppConfigDir())
|
||||
fmt.Printf("caddy.ConfigAutosavePath=%s\n", caddy.ConfigAutosavePath)
|
||||
fmt.Printf("runtime.GOOS=%s\n", runtime.GOOS)
|
||||
fmt.Printf("runtime.GOARCH=%s\n", runtime.GOARCH)
|
||||
fmt.Printf("runtime.Compiler=%s\n", runtime.Compiler)
|
||||
fmt.Printf("runtime.NumCPU=%d\n", runtime.NumCPU())
|
||||
fmt.Printf("runtime.GOMAXPROCS=%d\n", runtime.GOMAXPROCS(0))
|
||||
fmt.Printf("runtime.Version=%s\n", runtime.Version())
|
||||
cwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
cwd = fmt.Sprintf("<error: %v>", err)
|
||||
}
|
||||
fmt.Printf("os.Getwd=%s\n\n", cwd)
|
||||
for _, v := range os.Environ() {
|
||||
fmt.Println(v)
|
||||
}
|
||||
}
|
||||
|
||||
// moveStorage moves the old default dataDir to the new default dataDir.
|
||||
// TODO: This is TEMPORARY until the release candidates.
|
||||
func moveStorage() {
|
||||
// get the home directory (the old way)
|
||||
oldHome := os.Getenv("HOME")
|
||||
if oldHome == "" && runtime.GOOS == "windows" {
|
||||
drive := os.Getenv("HOMEDRIVE")
|
||||
path := os.Getenv("HOMEPATH")
|
||||
oldHome = drive + path
|
||||
if drive == "" || path == "" {
|
||||
oldHome = os.Getenv("USERPROFILE")
|
||||
}
|
||||
}
|
||||
if oldHome == "" {
|
||||
oldHome = "."
|
||||
}
|
||||
oldDataDir := filepath.Join(oldHome, ".local", "share", "caddy")
|
||||
|
||||
// nothing to do if old data dir doesn't exist
|
||||
_, err := os.Stat(oldDataDir)
|
||||
if os.IsNotExist(err) {
|
||||
return
|
||||
}
|
||||
|
||||
// nothing to do if the new data dir is the same as the old one
|
||||
newDataDir := caddy.AppDataDir()
|
||||
if oldDataDir == newDataDir {
|
||||
return
|
||||
}
|
||||
|
||||
logger := caddy.Log().Named("automigrate").With(
|
||||
zap.String("old_dir", oldDataDir),
|
||||
zap.String("new_dir", newDataDir))
|
||||
|
||||
logger.Info("beginning one-time data directory migration",
|
||||
zap.String("details", "https://github.com/caddyserver/caddy/issues/2955"))
|
||||
|
||||
// if new data directory exists, avoid auto-migration as a conservative safety measure
|
||||
_, err = os.Stat(newDataDir)
|
||||
if !os.IsNotExist(err) {
|
||||
logger.Error("new data directory already exists; skipping auto-migration as conservative safety measure",
|
||||
zap.Error(err),
|
||||
zap.String("instructions", "https://github.com/caddyserver/caddy/issues/2955#issuecomment-570000333"))
|
||||
return
|
||||
}
|
||||
|
||||
// construct the new data directory's parent folder
|
||||
err = os.MkdirAll(filepath.Dir(newDataDir), 0700)
|
||||
if err != nil {
|
||||
logger.Error("unable to make new datadirectory - follow link for instructions",
|
||||
zap.String("instructions", "https://github.com/caddyserver/caddy/issues/2955#issuecomment-570000333"),
|
||||
zap.Error(err))
|
||||
return
|
||||
}
|
||||
|
||||
// folder structure is same, so just try to rename (move) it;
|
||||
// this fails if the new path is on a separate device
|
||||
err = os.Rename(oldDataDir, newDataDir)
|
||||
if err != nil {
|
||||
logger.Error("new data directory already exists; skipping auto-migration as conservative safety measure - follow link for instructions",
|
||||
zap.String("instructions", "https://github.com/caddyserver/caddy/issues/2955#issuecomment-570000333"),
|
||||
zap.Error(err))
|
||||
}
|
||||
|
||||
logger.Info("successfully completed one-time migration of data directory",
|
||||
zap.String("details", "https://github.com/caddyserver/caddy/issues/2955"))
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
// 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.
|
||||
|
||||
// +build !windows
|
||||
|
||||
package caddycmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
func gracefullyStopProcess(pid int) error {
|
||||
fmt.Print("Graceful stop... ")
|
||||
err := syscall.Kill(pid, syscall.SIGINT)
|
||||
if err != nil {
|
||||
return fmt.Errorf("kill: %v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func getProcessName() string {
|
||||
return filepath.Base(os.Args[0])
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
// 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 caddycmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func gracefullyStopProcess(pid int) error {
|
||||
fmt.Print("Forceful stop... ")
|
||||
// process on windows will not stop unless forced with /f
|
||||
cmd := exec.Command("taskkill", "/pid", strconv.Itoa(pid), "/f")
|
||||
if err := cmd.Run(); err != nil {
|
||||
return fmt.Errorf("taskkill: %v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// On Windows the app name passed in os.Args[0] will match how
|
||||
// caddy was started eg will match caddy or caddy.exe.
|
||||
// So return appname with .exe for consistency
|
||||
func getProcessName() string {
|
||||
base := filepath.Base(os.Args[0])
|
||||
if filepath.Ext(base) == "" {
|
||||
return base + ".exe"
|
||||
}
|
||||
return base
|
||||
}
|
||||
-407
@@ -1,407 +0,0 @@
|
||||
// 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 caddy
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"reflect"
|
||||
|
||||
"github.com/caddyserver/certmagic"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// Context is a type which defines the lifetime of modules that
|
||||
// are loaded and provides access to the parent configuration
|
||||
// that spawned the modules which are loaded. It should be used
|
||||
// with care and wrapped with derivation functions from the
|
||||
// standard context package only if you don't need the Caddy
|
||||
// specific features. These contexts are canceled when the
|
||||
// lifetime of the modules loaded from it is over.
|
||||
//
|
||||
// Use NewContext() to get a valid value (but most modules will
|
||||
// not actually need to do this).
|
||||
type Context struct {
|
||||
context.Context
|
||||
moduleInstances map[string][]interface{}
|
||||
cfg *Config
|
||||
cleanupFuncs []func()
|
||||
}
|
||||
|
||||
// NewContext provides a new context derived from the given
|
||||
// context ctx. Normally, you will not need to call this
|
||||
// function unless you are loading modules which have a
|
||||
// different lifespan than the ones for the context the
|
||||
// module was provisioned with. Be sure to call the cancel
|
||||
// func when the context is to be cleaned up so that
|
||||
// modules which are loaded will be properly unloaded.
|
||||
// See standard library context package's documentation.
|
||||
func NewContext(ctx Context) (Context, context.CancelFunc) {
|
||||
newCtx := Context{moduleInstances: make(map[string][]interface{}), cfg: ctx.cfg}
|
||||
c, cancel := context.WithCancel(ctx.Context)
|
||||
wrappedCancel := func() {
|
||||
cancel()
|
||||
|
||||
for _, f := range ctx.cleanupFuncs {
|
||||
f()
|
||||
}
|
||||
|
||||
for modName, modInstances := range newCtx.moduleInstances {
|
||||
for _, inst := range modInstances {
|
||||
if cu, ok := inst.(CleanerUpper); ok {
|
||||
err := cu.Cleanup()
|
||||
if err != nil {
|
||||
log.Printf("[ERROR] %s (%p): cleanup: %v", modName, inst, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
newCtx.Context = c
|
||||
return newCtx, wrappedCancel
|
||||
}
|
||||
|
||||
// OnCancel executes f when ctx is canceled.
|
||||
func (ctx *Context) OnCancel(f func()) {
|
||||
ctx.cleanupFuncs = append(ctx.cleanupFuncs, f)
|
||||
}
|
||||
|
||||
// LoadModule loads the Caddy module(s) from the specified field of the parent struct
|
||||
// pointer and returns the loaded module(s). The struct pointer and its field name as
|
||||
// a string are necessary so that reflection can be used to read the struct tag on the
|
||||
// field to get the module namespace and inline module name key (if specified).
|
||||
//
|
||||
// The field can be any one of the supported raw module types: json.RawMessage,
|
||||
// []json.RawMessage, map[string]json.RawMessage, or []map[string]json.RawMessage.
|
||||
// ModuleMap may be used in place of map[string]json.RawMessage. The return value's
|
||||
// underlying type mirrors the input field's type:
|
||||
//
|
||||
// json.RawMessage => interface{}
|
||||
// []json.RawMessage => []interface{}
|
||||
// map[string]json.RawMessage => map[string]interface{}
|
||||
// []map[string]json.RawMessage => []map[string]interface{}
|
||||
//
|
||||
// The field must have a "caddy" struct tag in this format:
|
||||
//
|
||||
// caddy:"key1=val1 key2=val2"
|
||||
//
|
||||
// To load modules, a "namespace" key is required. For example, to load modules
|
||||
// in the "http.handlers" namespace, you'd put: `namespace=http.handlers` in the
|
||||
// Caddy struct tag.
|
||||
//
|
||||
// The module name must also be available. If the field type is a map or slice of maps,
|
||||
// then key is assumed to be the module name if an "inline_key" is NOT specified in the
|
||||
// caddy struct tag. In this case, the module name does NOT need to be specified in-line
|
||||
// with the module itself.
|
||||
//
|
||||
// If not a map, or if inline_key is non-empty, then the module name must be embedded
|
||||
// into the values, which must be objects; then there must be a key in those objects
|
||||
// where its associated value is the module name. This is called the "inline key",
|
||||
// meaning the key containing the module's name that is defined inline with the module
|
||||
// itself. You must specify the inline key in a struct tag, along with the namespace:
|
||||
//
|
||||
// caddy:"namespace=http.handlers inline_key=handler"
|
||||
//
|
||||
// This will look for a key/value pair like `"handler": "..."` in the json.RawMessage
|
||||
// in order to know the module name.
|
||||
//
|
||||
// To make use of the loaded module(s) (the return value), you will probably want
|
||||
// to type-assert each interface{} value(s) to the types that are useful to you
|
||||
// and store them on the same struct. Storing them on the same struct makes for
|
||||
// easy garbage collection when your host module is no longer needed.
|
||||
//
|
||||
// Loaded modules have already been provisioned and validated. Upon returning
|
||||
// successfully, this method clears the json.RawMessage(s) in the field since
|
||||
// the raw JSON is no longer needed, and this allows the GC to free up memory.
|
||||
func (ctx Context) LoadModule(structPointer interface{}, fieldName string) (interface{}, error) {
|
||||
val := reflect.ValueOf(structPointer).Elem().FieldByName(fieldName)
|
||||
typ := val.Type()
|
||||
|
||||
field, ok := reflect.TypeOf(structPointer).Elem().FieldByName(fieldName)
|
||||
if !ok {
|
||||
panic(fmt.Sprintf("field %s does not exist in %#v", fieldName, structPointer))
|
||||
}
|
||||
|
||||
opts, err := ParseStructTag(field.Tag.Get("caddy"))
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("malformed tag on field %s: %v", fieldName, err))
|
||||
}
|
||||
|
||||
moduleNamespace, ok := opts["namespace"]
|
||||
if !ok {
|
||||
panic(fmt.Sprintf("missing 'namespace' key in struct tag on field %s", fieldName))
|
||||
}
|
||||
inlineModuleKey := opts["inline_key"]
|
||||
|
||||
var result interface{}
|
||||
|
||||
switch val.Kind() {
|
||||
case reflect.Slice:
|
||||
if isJSONRawMessage(typ) {
|
||||
// val is `json.RawMessage` ([]uint8 under the hood)
|
||||
|
||||
if inlineModuleKey == "" {
|
||||
panic("unable to determine module name without inline_key when type is not a ModuleMap")
|
||||
}
|
||||
val, err := ctx.loadModuleInline(inlineModuleKey, moduleNamespace, val.Interface().(json.RawMessage))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result = val
|
||||
|
||||
} else if isJSONRawMessage(typ.Elem()) {
|
||||
// val is `[]json.RawMessage`
|
||||
|
||||
if inlineModuleKey == "" {
|
||||
panic("unable to determine module name without inline_key because type is not a ModuleMap")
|
||||
}
|
||||
var all []interface{}
|
||||
for i := 0; i < val.Len(); i++ {
|
||||
val, err := ctx.loadModuleInline(inlineModuleKey, moduleNamespace, val.Index(i).Interface().(json.RawMessage))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("position %d: %v", i, err)
|
||||
}
|
||||
all = append(all, val)
|
||||
}
|
||||
result = all
|
||||
|
||||
} else if isModuleMapType(typ.Elem()) {
|
||||
// val is `[]map[string]json.RawMessage`
|
||||
|
||||
var all []map[string]interface{}
|
||||
for i := 0; i < val.Len(); i++ {
|
||||
thisSet, err := ctx.loadModulesFromSomeMap(moduleNamespace, inlineModuleKey, val.Index(i))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
all = append(all, thisSet)
|
||||
}
|
||||
result = all
|
||||
}
|
||||
|
||||
case reflect.Map:
|
||||
// val is a ModuleMap or some other kind of map
|
||||
result, err = ctx.loadModulesFromSomeMap(moduleNamespace, inlineModuleKey, val)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
default:
|
||||
return nil, fmt.Errorf("unrecognized type for module: %s", typ)
|
||||
}
|
||||
|
||||
// we're done with the raw bytes; allow GC to deallocate
|
||||
val.Set(reflect.Zero(typ))
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// loadModulesFromSomeMap loads modules from val, which must be a type of map[string]interface{}.
|
||||
// Depending on inlineModuleKey, it will be interpreted as either a ModuleMap (key is the module
|
||||
// name) or as a regular map (key is not the module name, and module name is defined inline).
|
||||
func (ctx Context) loadModulesFromSomeMap(namespace, inlineModuleKey string, val reflect.Value) (map[string]interface{}, error) {
|
||||
// if no inline_key is specified, then val must be a ModuleMap,
|
||||
// where the key is the module name
|
||||
if inlineModuleKey == "" {
|
||||
if !isModuleMapType(val.Type()) {
|
||||
panic(fmt.Sprintf("expected ModuleMap because inline_key is empty; but we do not recognize this type: %s", val.Type()))
|
||||
}
|
||||
return ctx.loadModuleMap(namespace, val)
|
||||
}
|
||||
|
||||
// otherwise, val is a map with modules, but the module name is
|
||||
// inline with each value (the key means something else)
|
||||
return ctx.loadModulesFromRegularMap(namespace, inlineModuleKey, val)
|
||||
}
|
||||
|
||||
// loadModulesFromRegularMap loads modules from val, where val is a map[string]json.RawMessage.
|
||||
// Map keys are NOT interpreted as module names, so module names are still expected to appear
|
||||
// inline with the objects.
|
||||
func (ctx Context) loadModulesFromRegularMap(namespace, inlineModuleKey string, val reflect.Value) (map[string]interface{}, error) {
|
||||
mods := make(map[string]interface{})
|
||||
iter := val.MapRange()
|
||||
for iter.Next() {
|
||||
k := iter.Key()
|
||||
v := iter.Value()
|
||||
mod, err := ctx.loadModuleInline(inlineModuleKey, namespace, v.Interface().(json.RawMessage))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("key %s: %v", k, err)
|
||||
}
|
||||
mods[k.String()] = mod
|
||||
}
|
||||
return mods, nil
|
||||
}
|
||||
|
||||
// loadModuleMap loads modules from a ModuleMap, i.e. map[string]interface{}, where the key is the
|
||||
// module name. With a module map, module names do not need to be defined inline with their values.
|
||||
func (ctx Context) loadModuleMap(namespace string, val reflect.Value) (map[string]interface{}, error) {
|
||||
all := make(map[string]interface{})
|
||||
iter := val.MapRange()
|
||||
for iter.Next() {
|
||||
k := iter.Key().Interface().(string)
|
||||
v := iter.Value().Interface().(json.RawMessage)
|
||||
moduleName := namespace + "." + k
|
||||
if namespace == "" {
|
||||
moduleName = k
|
||||
}
|
||||
val, err := ctx.LoadModuleByID(moduleName, v)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("module name '%s': %v", k, err)
|
||||
}
|
||||
all[k] = val
|
||||
}
|
||||
return all, nil
|
||||
}
|
||||
|
||||
// LoadModuleByID decodes rawMsg into a new instance of mod and
|
||||
// returns the value. If mod.New is nil, an error is returned.
|
||||
// If the module implements Validator or Provisioner interfaces,
|
||||
// those methods are invoked to ensure the module is fully
|
||||
// configured and valid before being used.
|
||||
//
|
||||
// This is a lower-level method and will usually not be called
|
||||
// directly by most modules. However, this method is useful when
|
||||
// dynamically loading/unloading modules in their own context,
|
||||
// like from embedded scripts, etc.
|
||||
func (ctx Context) LoadModuleByID(id string, rawMsg json.RawMessage) (interface{}, error) {
|
||||
modulesMu.RLock()
|
||||
mod, ok := modules[id]
|
||||
modulesMu.RUnlock()
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unknown module: %s", id)
|
||||
}
|
||||
|
||||
if mod.New == nil {
|
||||
return nil, fmt.Errorf("module '%s' has no constructor", mod.ID)
|
||||
}
|
||||
|
||||
val := mod.New().(interface{})
|
||||
|
||||
// value must be a pointer for unmarshaling into concrete type, even if
|
||||
// the module's concrete type is a slice or map; New() *should* return
|
||||
// a pointer, otherwise unmarshaling errors or panics will occur
|
||||
if rv := reflect.ValueOf(val); rv.Kind() != reflect.Ptr {
|
||||
log.Printf("[WARNING] ModuleInfo.New() for module '%s' did not return a pointer,"+
|
||||
" so we are using reflection to make a pointer instead; please fix this by"+
|
||||
" using new(Type) or &Type notation in your module's New() function.", id)
|
||||
val = reflect.New(rv.Type()).Elem().Addr().Interface().(Module)
|
||||
}
|
||||
|
||||
// fill in its config only if there is a config to fill in
|
||||
if len(rawMsg) > 0 {
|
||||
err := strictUnmarshalJSON(rawMsg, &val)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("decoding module config: %s: %v", mod, err)
|
||||
}
|
||||
}
|
||||
|
||||
if val == nil {
|
||||
// returned module values are almost always type-asserted
|
||||
// before being used, so a nil value would panic; and there
|
||||
// is no good reason to explicitly declare null modules in
|
||||
// a config; it might be because the user is trying to achieve
|
||||
// a result the developer isn't expecting, which is a smell
|
||||
return nil, fmt.Errorf("module value cannot be null")
|
||||
}
|
||||
|
||||
if prov, ok := val.(Provisioner); ok {
|
||||
err := prov.Provision(ctx)
|
||||
if err != nil {
|
||||
// incomplete provisioning could have left state
|
||||
// dangling, so make sure it gets cleaned up
|
||||
if cleanerUpper, ok := val.(CleanerUpper); ok {
|
||||
err2 := cleanerUpper.Cleanup()
|
||||
if err2 != nil {
|
||||
err = fmt.Errorf("%v; additionally, cleanup: %v", err, err2)
|
||||
}
|
||||
}
|
||||
return nil, fmt.Errorf("provision %s: %v", mod, err)
|
||||
}
|
||||
}
|
||||
|
||||
if validator, ok := val.(Validator); ok {
|
||||
err := validator.Validate()
|
||||
if err != nil {
|
||||
// since the module was already provisioned, make sure we clean up
|
||||
if cleanerUpper, ok := val.(CleanerUpper); ok {
|
||||
err2 := cleanerUpper.Cleanup()
|
||||
if err2 != nil {
|
||||
err = fmt.Errorf("%v; additionally, cleanup: %v", err, err2)
|
||||
}
|
||||
}
|
||||
return nil, fmt.Errorf("%s: invalid configuration: %v", mod, err)
|
||||
}
|
||||
}
|
||||
|
||||
ctx.moduleInstances[id] = append(ctx.moduleInstances[id], val)
|
||||
|
||||
return val, nil
|
||||
}
|
||||
|
||||
// loadModuleInline loads a module from a JSON raw message which decodes to
|
||||
// a map[string]interface{}, where one of the object keys is moduleNameKey
|
||||
// and the corresponding value is the module name (as a string) which can
|
||||
// be found in the given scope. In other words, the module name is declared
|
||||
// in-line with the module itself.
|
||||
//
|
||||
// This allows modules to be decoded into their concrete types and used when
|
||||
// their names cannot be the unique key in a map, such as when there are
|
||||
// multiple instances in the map or it appears in an array (where there are
|
||||
// no custom keys). In other words, the key containing the module name is
|
||||
// treated special/separate from all the other keys in the object.
|
||||
func (ctx Context) loadModuleInline(moduleNameKey, moduleScope string, raw json.RawMessage) (interface{}, error) {
|
||||
moduleName, raw, err := getModuleNameInline(moduleNameKey, raw)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
val, err := ctx.LoadModuleByID(moduleScope+"."+moduleName, raw)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("loading module '%s': %v", moduleName, err)
|
||||
}
|
||||
|
||||
return val, nil
|
||||
}
|
||||
|
||||
// App returns the configured app named name. If no app with
|
||||
// that name is currently configured, a new empty one will be
|
||||
// instantiated. (The app module must still be registered.)
|
||||
func (ctx Context) App(name string) (interface{}, error) {
|
||||
if app, ok := ctx.cfg.apps[name]; ok {
|
||||
return app, nil
|
||||
}
|
||||
appRaw := ctx.cfg.AppsRaw[name]
|
||||
modVal, err := ctx.LoadModuleByID(name, appRaw)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("loading %s app module: %v", name, err)
|
||||
}
|
||||
if appRaw != nil {
|
||||
ctx.cfg.AppsRaw[name] = nil // allow GC to deallocate
|
||||
}
|
||||
ctx.cfg.apps[name] = modVal.(App)
|
||||
return modVal, nil
|
||||
}
|
||||
|
||||
// Storage returns the configured Caddy storage implementation.
|
||||
func (ctx Context) Storage() certmagic.Storage {
|
||||
return ctx.cfg.storage
|
||||
}
|
||||
|
||||
// Logger returns a logger that can be used by mod.
|
||||
func (ctx Context) Logger(mod Module) *zap.Logger {
|
||||
return ctx.cfg.Logging.Logger(mod)
|
||||
}
|
||||
-118
@@ -1,118 +0,0 @@
|
||||
// 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 caddy
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
)
|
||||
|
||||
func ExampleContext_LoadModule() {
|
||||
// this whole first part is just setting up for the example;
|
||||
// note the struct tags - very important; we specify inline_key
|
||||
// because that is the only way to know the module name
|
||||
var ctx Context
|
||||
myStruct := &struct {
|
||||
// This godoc comment will appear in module documentation.
|
||||
GuestModuleRaw json.RawMessage `json:"guest_module,omitempty" caddy:"namespace=example inline_key=name"`
|
||||
|
||||
// this is where the decoded module will be stored; in this
|
||||
// example, we pretend we need an io.Writer but it can be
|
||||
// any interface type that is useful to you
|
||||
guestModule io.Writer
|
||||
}{
|
||||
GuestModuleRaw: json.RawMessage(`{"name":"module_name","foo":"bar"}`),
|
||||
}
|
||||
|
||||
// if a guest module is provided, we can load it easily
|
||||
if myStruct.GuestModuleRaw != nil {
|
||||
mod, err := ctx.LoadModule(myStruct, "GuestModuleRaw")
|
||||
if err != nil {
|
||||
// you'd want to actually handle the error here
|
||||
// return fmt.Errorf("loading guest module: %v", err)
|
||||
}
|
||||
// mod contains the loaded and provisioned module,
|
||||
// it is now ready for us to use
|
||||
myStruct.guestModule = mod.(io.Writer)
|
||||
}
|
||||
|
||||
// use myStruct.guestModule from now on
|
||||
}
|
||||
|
||||
func ExampleContext_LoadModule_array() {
|
||||
// this whole first part is just setting up for the example;
|
||||
// note the struct tags - very important; we specify inline_key
|
||||
// because that is the only way to know the module name
|
||||
var ctx Context
|
||||
myStruct := &struct {
|
||||
// This godoc comment will appear in module documentation.
|
||||
GuestModulesRaw []json.RawMessage `json:"guest_modules,omitempty" caddy:"namespace=example inline_key=name"`
|
||||
|
||||
// this is where the decoded module will be stored; in this
|
||||
// example, we pretend we need an io.Writer but it can be
|
||||
// any interface type that is useful to you
|
||||
guestModules []io.Writer
|
||||
}{
|
||||
GuestModulesRaw: []json.RawMessage{
|
||||
json.RawMessage(`{"name":"module1_name","foo":"bar1"}`),
|
||||
json.RawMessage(`{"name":"module2_name","foo":"bar2"}`),
|
||||
},
|
||||
}
|
||||
|
||||
// since our input is []json.RawMessage, the output will be []interface{}
|
||||
mods, err := ctx.LoadModule(myStruct, "GuestModulesRaw")
|
||||
if err != nil {
|
||||
// you'd want to actually handle the error here
|
||||
// return fmt.Errorf("loading guest modules: %v", err)
|
||||
}
|
||||
for _, mod := range mods.([]interface{}) {
|
||||
myStruct.guestModules = append(myStruct.guestModules, mod.(io.Writer))
|
||||
}
|
||||
|
||||
// use myStruct.guestModules from now on
|
||||
}
|
||||
|
||||
func ExampleContext_LoadModule_map() {
|
||||
// this whole first part is just setting up for the example;
|
||||
// note the struct tags - very important; we don't specify
|
||||
// inline_key because the map key is the module name
|
||||
var ctx Context
|
||||
myStruct := &struct {
|
||||
// This godoc comment will appear in module documentation.
|
||||
GuestModulesRaw ModuleMap `json:"guest_modules,omitempty" caddy:"namespace=example"`
|
||||
|
||||
// this is where the decoded module will be stored; in this
|
||||
// example, we pretend we need an io.Writer but it can be
|
||||
// any interface type that is useful to you
|
||||
guestModules map[string]io.Writer
|
||||
}{
|
||||
GuestModulesRaw: ModuleMap{
|
||||
"module1_name": json.RawMessage(`{"foo":"bar1"}`),
|
||||
"module2_name": json.RawMessage(`{"foo":"bar2"}`),
|
||||
},
|
||||
}
|
||||
|
||||
// since our input is map[string]json.RawMessage, the output will be map[string]interface{}
|
||||
mods, err := ctx.LoadModule(myStruct, "GuestModulesRaw")
|
||||
if err != nil {
|
||||
// you'd want to actually handle the error here
|
||||
// return fmt.Errorf("loading guest modules: %v", err)
|
||||
}
|
||||
for modName, mod := range mods.(map[string]interface{}) {
|
||||
myStruct.guestModules[modName] = mod.(io.Writer)
|
||||
}
|
||||
|
||||
// use myStruct.guestModules from now on
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,37 +0,0 @@
|
||||
module github.com/caddyserver/caddy/v2
|
||||
|
||||
go 1.14
|
||||
|
||||
require (
|
||||
github.com/Masterminds/sprig/v3 v3.0.2
|
||||
github.com/alecthomas/chroma v0.7.2-0.20200305040604-4f3623dce67a
|
||||
github.com/andybalholm/brotli v1.0.0
|
||||
github.com/caddyserver/certmagic v0.10.3
|
||||
github.com/dustin/go-humanize v1.0.1-0.20200219035652-afde56e7acac
|
||||
github.com/go-acme/lego/v3 v3.5.0
|
||||
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e
|
||||
github.com/google/cel-go v0.3.2
|
||||
github.com/ilibs/json5 v1.0.1
|
||||
github.com/jsternberg/zap-logfmt v1.2.0
|
||||
github.com/klauspost/compress v1.10.3
|
||||
github.com/klauspost/cpuid v1.2.3
|
||||
github.com/lucas-clemente/quic-go v0.15.2
|
||||
github.com/manifoldco/promptui v0.7.0 // indirect
|
||||
github.com/miekg/dns v1.1.28 // indirect
|
||||
github.com/muhammadmuzzammil1998/jsonc v0.0.0-20200303171503-1e787b591db7
|
||||
github.com/naoina/go-stringutil v0.1.0 // indirect
|
||||
github.com/naoina/toml v0.1.1
|
||||
github.com/smallstep/certificates v0.14.0-rc.5
|
||||
github.com/smallstep/cli v0.14.0-rc.3
|
||||
github.com/smallstep/truststore v0.9.4
|
||||
github.com/vulcand/oxy v1.0.0
|
||||
github.com/yuin/goldmark v1.1.25
|
||||
github.com/yuin/goldmark-highlighting v0.0.0-20200307114337-60d527fdb691
|
||||
go.uber.org/zap v1.14.1
|
||||
golang.org/x/crypto v0.0.0-20200317142112-1b76d66859c6
|
||||
golang.org/x/net v0.0.0-20200301022130-244492dfa37a
|
||||
google.golang.org/genproto v0.0.0-20200305110556-506484158171
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.0.0
|
||||
gopkg.in/square/go-jose.v2 v2.4.1 // indirect
|
||||
gopkg.in/yaml.v2 v2.2.8
|
||||
)
|
||||
-407
@@ -1,407 +0,0 @@
|
||||
// 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 caddy
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Listen returns a listener suitable for use in a Caddy module.
|
||||
// Always be sure to close listeners when you are done with them.
|
||||
func Listen(network, addr string) (net.Listener, error) {
|
||||
lnKey := network + "/" + addr
|
||||
|
||||
listenersMu.Lock()
|
||||
defer listenersMu.Unlock()
|
||||
|
||||
// if listener already exists, increment usage counter, then return listener
|
||||
if lnGlobal, ok := listeners[lnKey]; ok {
|
||||
atomic.AddInt32(&lnGlobal.usage, 1)
|
||||
return &fakeCloseListener{
|
||||
usage: &lnGlobal.usage,
|
||||
deadline: &lnGlobal.deadline,
|
||||
deadlineMu: &lnGlobal.deadlineMu,
|
||||
key: lnKey,
|
||||
Listener: lnGlobal.ln,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// or, create new one and save it
|
||||
ln, err := net.Listen(network, addr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// make sure to start its usage counter at 1
|
||||
lnGlobal := &globalListener{usage: 1, ln: ln}
|
||||
listeners[lnKey] = lnGlobal
|
||||
|
||||
return &fakeCloseListener{
|
||||
usage: &lnGlobal.usage,
|
||||
deadline: &lnGlobal.deadline,
|
||||
deadlineMu: &lnGlobal.deadlineMu,
|
||||
key: lnKey,
|
||||
Listener: ln,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// ListenPacket returns a net.PacketConn suitable for use in a Caddy module.
|
||||
// Always be sure to close the PacketConn when you are done.
|
||||
func ListenPacket(network, addr string) (net.PacketConn, error) {
|
||||
lnKey := network + "/" + addr
|
||||
|
||||
listenersMu.Lock()
|
||||
defer listenersMu.Unlock()
|
||||
|
||||
// if listener already exists, increment usage counter, then return listener
|
||||
if lnGlobal, ok := listeners[lnKey]; ok {
|
||||
atomic.AddInt32(&lnGlobal.usage, 1)
|
||||
log.Printf("[DEBUG] %s: Usage counter should not go above 2 or maybe 3, is now: %d", lnKey, atomic.LoadInt32(&lnGlobal.usage)) // TODO: remove
|
||||
return &fakeClosePacketConn{usage: &lnGlobal.usage, key: lnKey, PacketConn: lnGlobal.pc}, nil
|
||||
}
|
||||
|
||||
// or, create new one and save it
|
||||
pc, err := net.ListenPacket(network, addr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// make sure to start its usage counter at 1
|
||||
lnGlobal := &globalListener{usage: 1, pc: pc}
|
||||
listeners[lnKey] = lnGlobal
|
||||
|
||||
return &fakeClosePacketConn{usage: &lnGlobal.usage, key: lnKey, PacketConn: pc}, nil
|
||||
}
|
||||
|
||||
// fakeCloseListener's Close() method is a no-op. This allows
|
||||
// stopping servers that are using the listener without giving
|
||||
// up the socket; thus, servers become hot-swappable while the
|
||||
// listener remains running. Listeners should be re-wrapped in
|
||||
// a new fakeCloseListener each time the listener is reused.
|
||||
// Other than the 'closed' field (which pertains to this value
|
||||
// only), the other fields in this struct should be pointers to
|
||||
// the associated globalListener's struct fields (except 'key'
|
||||
// which is there for read-only purposes, so it can be a copy).
|
||||
type fakeCloseListener struct {
|
||||
closed int32 // accessed atomically; belongs to this struct only
|
||||
usage *int32 // accessed atomically; global
|
||||
deadline *bool // protected by deadlineMu; global
|
||||
deadlineMu *sync.Mutex // global
|
||||
key string // global, but read-only, so can be copy
|
||||
net.Listener // global
|
||||
}
|
||||
|
||||
// Accept accepts connections until Close() is called.
|
||||
func (fcl *fakeCloseListener) Accept() (net.Conn, error) {
|
||||
// if the listener is already "closed", return error
|
||||
if atomic.LoadInt32(&fcl.closed) == 1 {
|
||||
return nil, fcl.fakeClosedErr()
|
||||
}
|
||||
|
||||
// wrap underlying accept
|
||||
conn, err := fcl.Listener.Accept()
|
||||
if err == nil {
|
||||
return conn, nil
|
||||
}
|
||||
|
||||
// accept returned with error
|
||||
// TODO: This may be better as a condition variable so the deadline is cleared only once?
|
||||
fcl.deadlineMu.Lock()
|
||||
if *fcl.deadline {
|
||||
switch ln := fcl.Listener.(type) {
|
||||
case *net.TCPListener:
|
||||
ln.SetDeadline(time.Time{})
|
||||
case *net.UnixListener:
|
||||
ln.SetDeadline(time.Time{})
|
||||
}
|
||||
*fcl.deadline = false
|
||||
}
|
||||
fcl.deadlineMu.Unlock()
|
||||
|
||||
if atomic.LoadInt32(&fcl.closed) == 1 {
|
||||
// if we canceled the Accept() by setting a deadline
|
||||
// on the listener, we need to make sure any callers of
|
||||
// Accept() think the listener was actually closed;
|
||||
// if we return the timeout error instead, callers might
|
||||
// simply retry, leaking goroutines for longer
|
||||
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
|
||||
return nil, fcl.fakeClosedErr()
|
||||
}
|
||||
}
|
||||
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Close stops accepting new connections without
|
||||
// closing the underlying listener, unless no one
|
||||
// else is using it.
|
||||
func (fcl *fakeCloseListener) Close() error {
|
||||
if atomic.CompareAndSwapInt32(&fcl.closed, 0, 1) {
|
||||
// unfortunately, there is no way to cancel any
|
||||
// currently-blocking calls to Accept() that are
|
||||
// awaiting connections since we're not actually
|
||||
// closing the listener; so we cheat by setting
|
||||
// a deadline in the past, which forces it to
|
||||
// time out; note that this only works for
|
||||
// certain types of listeners...
|
||||
fcl.deadlineMu.Lock()
|
||||
if !*fcl.deadline {
|
||||
switch ln := fcl.Listener.(type) {
|
||||
case *net.TCPListener:
|
||||
ln.SetDeadline(time.Now().Add(-1 * time.Minute))
|
||||
case *net.UnixListener:
|
||||
ln.SetDeadline(time.Now().Add(-1 * time.Minute))
|
||||
}
|
||||
*fcl.deadline = true
|
||||
}
|
||||
fcl.deadlineMu.Unlock()
|
||||
|
||||
// since we're no longer using this listener,
|
||||
// decrement the usage counter and, if no one
|
||||
// else is using it, close underlying listener
|
||||
if atomic.AddInt32(fcl.usage, -1) == 0 {
|
||||
listenersMu.Lock()
|
||||
delete(listeners, fcl.key)
|
||||
listenersMu.Unlock()
|
||||
err := fcl.Listener.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (fcl *fakeCloseListener) fakeClosedErr() error {
|
||||
return &net.OpError{
|
||||
Op: "accept",
|
||||
Net: fcl.Listener.Addr().Network(),
|
||||
Addr: fcl.Listener.Addr(),
|
||||
Err: errFakeClosed,
|
||||
}
|
||||
}
|
||||
|
||||
type fakeClosePacketConn struct {
|
||||
closed int32 // accessed atomically
|
||||
usage *int32 // accessed atomically
|
||||
key string
|
||||
net.PacketConn
|
||||
}
|
||||
|
||||
func (fcpc *fakeClosePacketConn) Close() error {
|
||||
log.Println("[DEBUG] Fake-closing underlying packet conn") // TODO: remove this
|
||||
|
||||
if atomic.CompareAndSwapInt32(&fcpc.closed, 0, 1) {
|
||||
// since we're no longer using this listener,
|
||||
// decrement the usage counter and, if no one
|
||||
// else is using it, close underlying listener
|
||||
if atomic.AddInt32(fcpc.usage, -1) == 0 {
|
||||
listenersMu.Lock()
|
||||
delete(listeners, fcpc.key)
|
||||
listenersMu.Unlock()
|
||||
err := fcpc.PacketConn.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ErrFakeClosed is the underlying error value returned by
|
||||
// fakeCloseListener.Accept() after Close() has been called,
|
||||
// indicating that it is pretending to be closed so that the
|
||||
// server using it can terminate, while the underlying
|
||||
// socket is actually left open.
|
||||
var errFakeClosed = fmt.Errorf("listener 'closed' 😉")
|
||||
|
||||
// globalListener keeps global state for a listener
|
||||
// that may be shared by multiple servers. In other
|
||||
// words, values in this struct exist only once and
|
||||
// all other uses of these values point to the ones
|
||||
// in this struct. In particular, the usage count
|
||||
// (how many callers are using the listener), the
|
||||
// actual listener, and synchronization of the
|
||||
// listener's deadline changes are singular, global
|
||||
// values that must not be copied.
|
||||
type globalListener struct {
|
||||
usage int32 // accessed atomically
|
||||
deadline bool
|
||||
deadlineMu sync.Mutex
|
||||
ln net.Listener
|
||||
pc net.PacketConn
|
||||
}
|
||||
|
||||
// ParsedAddress contains the individual components
|
||||
// for a parsed network address of the form accepted
|
||||
// by ParseNetworkAddress(). Network should be a
|
||||
// network value accepted by Go's net package. Port
|
||||
// ranges are given by [StartPort, EndPort].
|
||||
type ParsedAddress struct {
|
||||
Network string
|
||||
Host string
|
||||
StartPort uint
|
||||
EndPort uint
|
||||
}
|
||||
|
||||
// IsUnixNetwork returns true if pa.Network is
|
||||
// unix, unixgram, or unixpacket.
|
||||
func (pa ParsedAddress) IsUnixNetwork() bool {
|
||||
return isUnixNetwork(pa.Network)
|
||||
}
|
||||
|
||||
// JoinHostPort is like net.JoinHostPort, but where the port
|
||||
// is StartPort + offset.
|
||||
func (pa ParsedAddress) JoinHostPort(offset uint) string {
|
||||
if pa.IsUnixNetwork() {
|
||||
return pa.Host
|
||||
}
|
||||
return net.JoinHostPort(pa.Host, strconv.Itoa(int(pa.StartPort+offset)))
|
||||
}
|
||||
|
||||
// PortRangeSize returns how many ports are in
|
||||
// pa's port range. Port ranges are inclusive,
|
||||
// so the size is the difference of start and
|
||||
// end ports plus one.
|
||||
func (pa ParsedAddress) PortRangeSize() uint {
|
||||
return (pa.EndPort - pa.StartPort) + 1
|
||||
}
|
||||
|
||||
// String reconstructs the address string to the form expected
|
||||
// by ParseNetworkAddress().
|
||||
func (pa ParsedAddress) String() string {
|
||||
port := strconv.FormatUint(uint64(pa.StartPort), 10)
|
||||
if pa.StartPort != pa.EndPort {
|
||||
port += "-" + strconv.FormatUint(uint64(pa.EndPort), 10)
|
||||
}
|
||||
return JoinNetworkAddress(pa.Network, pa.Host, port)
|
||||
}
|
||||
|
||||
func isUnixNetwork(netw string) bool {
|
||||
return netw == "unix" || netw == "unixgram" || netw == "unixpacket"
|
||||
}
|
||||
|
||||
// ParseNetworkAddress parses addr into its individual
|
||||
// components. The input string is expected to be of
|
||||
// the form "network/host:port-range" where any part is
|
||||
// optional. The default network, if unspecified, is tcp.
|
||||
// Port ranges are inclusive.
|
||||
//
|
||||
// Network addresses are distinct from URLs and do not
|
||||
// use URL syntax.
|
||||
func ParseNetworkAddress(addr string) (ParsedAddress, error) {
|
||||
var host, port string
|
||||
network, host, port, err := SplitNetworkAddress(addr)
|
||||
if network == "" {
|
||||
network = "tcp"
|
||||
}
|
||||
if err != nil {
|
||||
return ParsedAddress{}, err
|
||||
}
|
||||
if isUnixNetwork(network) {
|
||||
return ParsedAddress{
|
||||
Network: network,
|
||||
Host: host,
|
||||
}, nil
|
||||
}
|
||||
ports := strings.SplitN(port, "-", 2)
|
||||
if len(ports) == 1 {
|
||||
ports = append(ports, ports[0])
|
||||
}
|
||||
var start, end uint64
|
||||
start, err = strconv.ParseUint(ports[0], 10, 16)
|
||||
if err != nil {
|
||||
return ParsedAddress{}, fmt.Errorf("invalid start port: %v", err)
|
||||
}
|
||||
end, err = strconv.ParseUint(ports[1], 10, 16)
|
||||
if err != nil {
|
||||
return ParsedAddress{}, fmt.Errorf("invalid end port: %v", err)
|
||||
}
|
||||
if end < start {
|
||||
return ParsedAddress{}, fmt.Errorf("end port must not be less than start port")
|
||||
}
|
||||
if (end - start) > maxPortSpan {
|
||||
return ParsedAddress{}, fmt.Errorf("port range exceeds %d ports", maxPortSpan)
|
||||
}
|
||||
return ParsedAddress{
|
||||
Network: network,
|
||||
Host: host,
|
||||
StartPort: uint(start),
|
||||
EndPort: uint(end),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// SplitNetworkAddress splits a into its network, host, and port components.
|
||||
// Note that port may be a port range (:X-Y), or omitted for unix sockets.
|
||||
func SplitNetworkAddress(a string) (network, host, port string, err error) {
|
||||
if idx := strings.Index(a, "/"); idx >= 0 {
|
||||
network = strings.ToLower(strings.TrimSpace(a[:idx]))
|
||||
a = a[idx+1:]
|
||||
}
|
||||
if isUnixNetwork(network) {
|
||||
host = a
|
||||
return
|
||||
}
|
||||
host, port, err = net.SplitHostPort(a)
|
||||
return
|
||||
}
|
||||
|
||||
// JoinNetworkAddress combines network, host, and port into a single
|
||||
// address string of the form accepted by ParseNetworkAddress(). For
|
||||
// unix sockets, the network should be "unix" (or "unixgram" or
|
||||
// "unixpacket") and the path to the socket should be given as the
|
||||
// host parameter.
|
||||
func JoinNetworkAddress(network, host, port string) string {
|
||||
var a string
|
||||
if network != "" {
|
||||
a = network + "/"
|
||||
}
|
||||
if host != "" && port == "" {
|
||||
a += host
|
||||
} else if port != "" {
|
||||
a += net.JoinHostPort(host, port)
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
// ListenerWrapper is a type that wraps a listener
|
||||
// so it can modify the input listener's methods.
|
||||
// Modules that implement this interface are found
|
||||
// in the caddy.listeners namespace. Usually, to
|
||||
// wrap a listener, you will define your own struct
|
||||
// type that embeds the input listener, then
|
||||
// implement your own methods that you want to wrap,
|
||||
// calling the underlying listener's methods where
|
||||
// appropriate.
|
||||
type ListenerWrapper interface {
|
||||
WrapListener(net.Listener) net.Listener
|
||||
}
|
||||
|
||||
var (
|
||||
listeners = make(map[string]*globalListener)
|
||||
listenersMu sync.Mutex
|
||||
)
|
||||
|
||||
const maxPortSpan = 65535
|
||||
@@ -1,26 +0,0 @@
|
||||
// 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.
|
||||
|
||||
// +build gofuzz
|
||||
// +build gofuzz_libfuzzer
|
||||
|
||||
package caddy
|
||||
|
||||
func FuzzParseNetworkAddress(data []byte) int {
|
||||
_, err := ParseNetworkAddress(string(data))
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
return 1
|
||||
}
|
||||
@@ -1,301 +0,0 @@
|
||||
// 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 caddy
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSplitNetworkAddress(t *testing.T) {
|
||||
for i, tc := range []struct {
|
||||
input string
|
||||
expectNetwork string
|
||||
expectHost string
|
||||
expectPort string
|
||||
expectErr bool
|
||||
}{
|
||||
{
|
||||
input: "",
|
||||
expectErr: true,
|
||||
},
|
||||
{
|
||||
input: "foo",
|
||||
expectErr: true,
|
||||
},
|
||||
{
|
||||
input: "foo:1234",
|
||||
expectHost: "foo",
|
||||
expectPort: "1234",
|
||||
},
|
||||
{
|
||||
input: "foo:1234-5678",
|
||||
expectHost: "foo",
|
||||
expectPort: "1234-5678",
|
||||
},
|
||||
{
|
||||
input: "udp/foo:1234",
|
||||
expectNetwork: "udp",
|
||||
expectHost: "foo",
|
||||
expectPort: "1234",
|
||||
},
|
||||
{
|
||||
input: "tcp6/foo:1234-5678",
|
||||
expectNetwork: "tcp6",
|
||||
expectHost: "foo",
|
||||
expectPort: "1234-5678",
|
||||
},
|
||||
{
|
||||
input: "udp/",
|
||||
expectNetwork: "udp",
|
||||
expectErr: true,
|
||||
},
|
||||
{
|
||||
input: "unix//foo/bar",
|
||||
expectNetwork: "unix",
|
||||
expectHost: "/foo/bar",
|
||||
},
|
||||
{
|
||||
input: "unixgram//foo/bar",
|
||||
expectNetwork: "unixgram",
|
||||
expectHost: "/foo/bar",
|
||||
},
|
||||
{
|
||||
input: "unixpacket//foo/bar",
|
||||
expectNetwork: "unixpacket",
|
||||
expectHost: "/foo/bar",
|
||||
},
|
||||
} {
|
||||
actualNetwork, actualHost, actualPort, err := SplitNetworkAddress(tc.input)
|
||||
if tc.expectErr && err == nil {
|
||||
t.Errorf("Test %d: Expected error but got: %v", i, err)
|
||||
}
|
||||
if !tc.expectErr && err != nil {
|
||||
t.Errorf("Test %d: Expected no error but got: %v", i, err)
|
||||
}
|
||||
if actualNetwork != tc.expectNetwork {
|
||||
t.Errorf("Test %d: Expected network '%s' but got '%s'", i, tc.expectNetwork, actualNetwork)
|
||||
}
|
||||
if actualHost != tc.expectHost {
|
||||
t.Errorf("Test %d: Expected host '%s' but got '%s'", i, tc.expectHost, actualHost)
|
||||
}
|
||||
if actualPort != tc.expectPort {
|
||||
t.Errorf("Test %d: Expected port '%s' but got '%s'", i, tc.expectPort, actualPort)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestJoinNetworkAddress(t *testing.T) {
|
||||
for i, tc := range []struct {
|
||||
network, host, port string
|
||||
expect string
|
||||
}{
|
||||
{
|
||||
network: "", host: "", port: "",
|
||||
expect: "",
|
||||
},
|
||||
{
|
||||
network: "tcp", host: "", port: "",
|
||||
expect: "tcp/",
|
||||
},
|
||||
{
|
||||
network: "", host: "foo", port: "",
|
||||
expect: "foo",
|
||||
},
|
||||
{
|
||||
network: "", host: "", port: "1234",
|
||||
expect: ":1234",
|
||||
},
|
||||
{
|
||||
network: "", host: "", port: "1234-5678",
|
||||
expect: ":1234-5678",
|
||||
},
|
||||
{
|
||||
network: "", host: "foo", port: "1234",
|
||||
expect: "foo:1234",
|
||||
},
|
||||
{
|
||||
network: "udp", host: "foo", port: "1234",
|
||||
expect: "udp/foo:1234",
|
||||
},
|
||||
{
|
||||
network: "udp", host: "", port: "1234",
|
||||
expect: "udp/:1234",
|
||||
},
|
||||
{
|
||||
network: "unix", host: "/foo/bar", port: "",
|
||||
expect: "unix//foo/bar",
|
||||
},
|
||||
{
|
||||
network: "", host: "::1", port: "1234",
|
||||
expect: "[::1]:1234",
|
||||
},
|
||||
} {
|
||||
actual := JoinNetworkAddress(tc.network, tc.host, tc.port)
|
||||
if actual != tc.expect {
|
||||
t.Errorf("Test %d: Expected '%s' but got '%s'", i, tc.expect, actual)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseNetworkAddress(t *testing.T) {
|
||||
for i, tc := range []struct {
|
||||
input string
|
||||
expectAddr ParsedAddress
|
||||
expectErr bool
|
||||
}{
|
||||
{
|
||||
input: "",
|
||||
expectErr: true,
|
||||
},
|
||||
{
|
||||
input: ":",
|
||||
expectErr: true,
|
||||
},
|
||||
{
|
||||
input: ":1234",
|
||||
expectAddr: ParsedAddress{
|
||||
Network: "tcp",
|
||||
Host: "",
|
||||
StartPort: 1234,
|
||||
EndPort: 1234,
|
||||
},
|
||||
},
|
||||
{
|
||||
input: "tcp/:1234",
|
||||
expectAddr: ParsedAddress{
|
||||
Network: "tcp",
|
||||
Host: "",
|
||||
StartPort: 1234,
|
||||
EndPort: 1234,
|
||||
},
|
||||
},
|
||||
{
|
||||
input: "tcp6/:1234",
|
||||
expectAddr: ParsedAddress{
|
||||
Network: "tcp6",
|
||||
Host: "",
|
||||
StartPort: 1234,
|
||||
EndPort: 1234,
|
||||
},
|
||||
},
|
||||
{
|
||||
input: "tcp4/localhost:1234",
|
||||
expectAddr: ParsedAddress{
|
||||
Network: "tcp4",
|
||||
Host: "localhost",
|
||||
StartPort: 1234,
|
||||
EndPort: 1234,
|
||||
},
|
||||
},
|
||||
{
|
||||
input: "unix//foo/bar",
|
||||
expectAddr: ParsedAddress{
|
||||
Network: "unix",
|
||||
Host: "/foo/bar",
|
||||
},
|
||||
},
|
||||
{
|
||||
input: "localhost:1234-1234",
|
||||
expectAddr: ParsedAddress{
|
||||
Network: "tcp",
|
||||
Host: "localhost",
|
||||
StartPort: 1234,
|
||||
EndPort: 1234,
|
||||
},
|
||||
},
|
||||
{
|
||||
input: "localhost:2-1",
|
||||
expectErr: true,
|
||||
},
|
||||
{
|
||||
input: "localhost:0",
|
||||
expectAddr: ParsedAddress{
|
||||
Network: "tcp",
|
||||
Host: "localhost",
|
||||
StartPort: 0,
|
||||
EndPort: 0,
|
||||
},
|
||||
},
|
||||
{
|
||||
input: "localhost:1-999999999999",
|
||||
expectErr: true,
|
||||
},
|
||||
} {
|
||||
actualAddr, err := ParseNetworkAddress(tc.input)
|
||||
if tc.expectErr && err == nil {
|
||||
t.Errorf("Test %d: Expected error but got: %v", i, err)
|
||||
}
|
||||
if !tc.expectErr && err != nil {
|
||||
t.Errorf("Test %d: Expected no error but got: %v", i, err)
|
||||
}
|
||||
|
||||
if actualAddr.Network != tc.expectAddr.Network {
|
||||
t.Errorf("Test %d: Expected network '%v' but got '%v'", i, tc.expectAddr, actualAddr)
|
||||
}
|
||||
if !reflect.DeepEqual(tc.expectAddr, actualAddr) {
|
||||
t.Errorf("Test %d: Expected addresses %v but got %v", i, tc.expectAddr, actualAddr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestJoinHostPort(t *testing.T) {
|
||||
for i, tc := range []struct {
|
||||
pa ParsedAddress
|
||||
offset uint
|
||||
expect string
|
||||
}{
|
||||
{
|
||||
pa: ParsedAddress{
|
||||
Network: "tcp",
|
||||
Host: "localhost",
|
||||
StartPort: 1234,
|
||||
EndPort: 1234,
|
||||
},
|
||||
expect: "localhost:1234",
|
||||
},
|
||||
{
|
||||
pa: ParsedAddress{
|
||||
Network: "tcp",
|
||||
Host: "localhost",
|
||||
StartPort: 1234,
|
||||
EndPort: 1235,
|
||||
},
|
||||
expect: "localhost:1234",
|
||||
},
|
||||
{
|
||||
pa: ParsedAddress{
|
||||
Network: "tcp",
|
||||
Host: "localhost",
|
||||
StartPort: 1234,
|
||||
EndPort: 1235,
|
||||
},
|
||||
offset: 1,
|
||||
expect: "localhost:1235",
|
||||
},
|
||||
{
|
||||
pa: ParsedAddress{
|
||||
Network: "unix",
|
||||
Host: "/run/php/php7.3-fpm.sock",
|
||||
},
|
||||
expect: "/run/php/php7.3-fpm.sock",
|
||||
},
|
||||
} {
|
||||
actual := tc.pa.JoinHostPort(tc.offset)
|
||||
if actual != tc.expect {
|
||||
t.Errorf("Test %d: Expected '%s' but got '%s'", i, tc.expect, actual)
|
||||
}
|
||||
}
|
||||
}
|
||||
-696
@@ -1,696 +0,0 @@
|
||||
// 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 caddy
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
"golang.org/x/crypto/ssh/terminal"
|
||||
)
|
||||
|
||||
func init() {
|
||||
RegisterModule(StdoutWriter{})
|
||||
RegisterModule(StderrWriter{})
|
||||
RegisterModule(DiscardWriter{})
|
||||
}
|
||||
|
||||
// Logging facilitates logging within Caddy. The default log is
|
||||
// called "default" and you can customize it. You can also define
|
||||
// additional logs.
|
||||
//
|
||||
// By default, all logs at INFO level and higher are written to
|
||||
// standard error ("stderr" writer) in a human-readable format
|
||||
// ("console" encoder if stdout is an interactive terminal, "json"
|
||||
// encoder otherwise).
|
||||
//
|
||||
// All defined logs accept all log entries by default, but you
|
||||
// can filter by level and module/logger names. A logger's name
|
||||
// is the same as the module's name, but a module may append to
|
||||
// logger names for more specificity. For example, you can
|
||||
// filter logs emitted only by HTTP handlers using the name
|
||||
// "http.handlers", because all HTTP handler module names have
|
||||
// that prefix.
|
||||
//
|
||||
// Caddy logs (except the sink) are zero-allocation, so they are
|
||||
// very high-performing in terms of memory and CPU time. Enabling
|
||||
// sampling can further increase throughput on extremely high-load
|
||||
// servers.
|
||||
type Logging struct {
|
||||
// Sink is the destination for all unstructured logs emitted
|
||||
// from Go's standard library logger. These logs are common
|
||||
// in dependencies that are not designed specifically for use
|
||||
// in Caddy. Because it is global and unstructured, the sink
|
||||
// lacks most advanced features and customizations.
|
||||
Sink *StandardLibLog `json:"sink,omitempty"`
|
||||
|
||||
// Logs are your logs, keyed by an arbitrary name of your
|
||||
// choosing. The default log can be customized by defining
|
||||
// a log called "default". You can further define other logs
|
||||
// and filter what kinds of entries they accept.
|
||||
Logs map[string]*CustomLog `json:"logs,omitempty"`
|
||||
|
||||
// a list of all keys for open writers; all writers
|
||||
// that are opened to provision this logging config
|
||||
// must have their keys added to this list so they
|
||||
// can be closed when cleaning up
|
||||
writerKeys []string
|
||||
}
|
||||
|
||||
// openLogs sets up the config and opens all the configured writers.
|
||||
// It closes its logs when ctx is canceled, so it should clean up
|
||||
// after itself.
|
||||
func (logging *Logging) openLogs(ctx Context) error {
|
||||
// make sure to deallocate resources when context is done
|
||||
ctx.OnCancel(func() {
|
||||
err := logging.closeLogs()
|
||||
if err != nil {
|
||||
Log().Error("closing logs", zap.Error(err))
|
||||
}
|
||||
})
|
||||
|
||||
// set up the "sink" log first (std lib's default global logger)
|
||||
if logging.Sink != nil {
|
||||
err := logging.Sink.provision(ctx, logging)
|
||||
if err != nil {
|
||||
return fmt.Errorf("setting up sink log: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// as a special case, set up the default structured Caddy log next
|
||||
if err := logging.setupNewDefault(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// then set up any other custom logs
|
||||
for name, l := range logging.Logs {
|
||||
// the default log is already set up
|
||||
if name == "default" {
|
||||
continue
|
||||
}
|
||||
|
||||
err := l.provision(ctx, logging)
|
||||
if err != nil {
|
||||
return fmt.Errorf("setting up custom log '%s': %v", name, err)
|
||||
}
|
||||
|
||||
// Any other logs that use the discard writer can be deleted
|
||||
// entirely. This avoids encoding and processing of each
|
||||
// log entry that would just be thrown away anyway. Notably,
|
||||
// we do not reach this point for the default log, which MUST
|
||||
// exist, otherwise core log emissions would panic because
|
||||
// they use the Log() function directly which expects a non-nil
|
||||
// logger. Even if we keep logs with a discard writer, they
|
||||
// have a nop core, and keeping them at all seems unnecessary.
|
||||
if _, ok := l.writerOpener.(*DiscardWriter); ok {
|
||||
delete(logging.Logs, name)
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (logging *Logging) setupNewDefault(ctx Context) error {
|
||||
if logging.Logs == nil {
|
||||
logging.Logs = make(map[string]*CustomLog)
|
||||
}
|
||||
|
||||
// extract the user-defined default log, if any
|
||||
newDefault := new(defaultCustomLog)
|
||||
if userDefault, ok := logging.Logs["default"]; ok {
|
||||
newDefault.CustomLog = userDefault
|
||||
} else {
|
||||
// if none, make one with our own default settings
|
||||
var err error
|
||||
newDefault, err = newDefaultProductionLog()
|
||||
if err != nil {
|
||||
return fmt.Errorf("setting up default Caddy log: %v", err)
|
||||
}
|
||||
logging.Logs["default"] = newDefault.CustomLog
|
||||
}
|
||||
|
||||
// set up this new log
|
||||
err := newDefault.CustomLog.provision(ctx, logging)
|
||||
if err != nil {
|
||||
return fmt.Errorf("setting up default log: %v", err)
|
||||
}
|
||||
newDefault.logger = zap.New(newDefault.CustomLog.core)
|
||||
|
||||
// redirect the default caddy logs
|
||||
defaultLoggerMu.Lock()
|
||||
oldDefault := defaultLogger
|
||||
defaultLogger = newDefault
|
||||
defaultLoggerMu.Unlock()
|
||||
|
||||
// if the new writer is different, indicate it in the logs for convenience
|
||||
var newDefaultLogWriterKey, currentDefaultLogWriterKey string
|
||||
var newDefaultLogWriterStr, currentDefaultLogWriterStr string
|
||||
if newDefault.writerOpener != nil {
|
||||
newDefaultLogWriterKey = newDefault.writerOpener.WriterKey()
|
||||
newDefaultLogWriterStr = newDefault.writerOpener.String()
|
||||
}
|
||||
if oldDefault.writerOpener != nil {
|
||||
currentDefaultLogWriterKey = oldDefault.writerOpener.WriterKey()
|
||||
currentDefaultLogWriterStr = oldDefault.writerOpener.String()
|
||||
}
|
||||
if newDefaultLogWriterKey != currentDefaultLogWriterKey {
|
||||
oldDefault.logger.Info("redirected default logger",
|
||||
zap.String("from", currentDefaultLogWriterStr),
|
||||
zap.String("to", newDefaultLogWriterStr),
|
||||
)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// closeLogs cleans up resources allocated during openLogs.
|
||||
// A successful call to openLogs calls this automatically
|
||||
// when the context is canceled.
|
||||
func (logging *Logging) closeLogs() error {
|
||||
for _, key := range logging.writerKeys {
|
||||
_, err := writers.Delete(key)
|
||||
if err != nil {
|
||||
log.Printf("[ERROR] Closing log writer %v: %v", key, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Logger returns a logger that is ready for the module to use.
|
||||
func (logging *Logging) Logger(mod Module) *zap.Logger {
|
||||
modID := string(mod.CaddyModule().ID)
|
||||
var cores []zapcore.Core
|
||||
|
||||
if logging != nil {
|
||||
for _, l := range logging.Logs {
|
||||
if l.matchesModule(modID) {
|
||||
if len(l.Include) == 0 && len(l.Exclude) == 0 {
|
||||
cores = append(cores, l.core)
|
||||
continue
|
||||
}
|
||||
cores = append(cores, &filteringCore{Core: l.core, cl: l})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
multiCore := zapcore.NewTee(cores...)
|
||||
|
||||
return zap.New(multiCore).Named(string(modID))
|
||||
}
|
||||
|
||||
// openWriter opens a writer using opener, and returns true if
|
||||
// the writer is new, or false if the writer already exists.
|
||||
func (logging *Logging) openWriter(opener WriterOpener) (io.WriteCloser, bool, error) {
|
||||
key := opener.WriterKey()
|
||||
writer, loaded, err := writers.LoadOrNew(key, func() (Destructor, error) {
|
||||
w, err := opener.OpenWriter()
|
||||
return writerDestructor{w}, err
|
||||
})
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
logging.writerKeys = append(logging.writerKeys, key)
|
||||
return writer.(io.WriteCloser), !loaded, nil
|
||||
}
|
||||
|
||||
// WriterOpener is a module that can open a log writer.
|
||||
// It can return a human-readable string representation
|
||||
// of itself so that operators can understand where
|
||||
// the logs are going.
|
||||
type WriterOpener interface {
|
||||
fmt.Stringer
|
||||
|
||||
// WriterKey is a string that uniquely identifies this
|
||||
// writer configuration. It is not shown to humans.
|
||||
WriterKey() string
|
||||
|
||||
// OpenWriter opens a log for writing. The writer
|
||||
// should be safe for concurrent use but need not
|
||||
// be synchronous.
|
||||
OpenWriter() (io.WriteCloser, error)
|
||||
}
|
||||
|
||||
type writerDestructor struct {
|
||||
io.WriteCloser
|
||||
}
|
||||
|
||||
func (wdest writerDestructor) Destruct() error {
|
||||
return wdest.Close()
|
||||
}
|
||||
|
||||
// StandardLibLog configures the default Go standard library
|
||||
// global logger in the log package. This is necessary because
|
||||
// module dependencies which are not built specifically for
|
||||
// Caddy will use the standard logger. This is also known as
|
||||
// the "sink" logger.
|
||||
type StandardLibLog struct {
|
||||
// The module that writes out log entries for the sink.
|
||||
WriterRaw json.RawMessage `json:"writer,omitempty" caddy:"namespace=caddy.logging.writers inline_key=output"`
|
||||
|
||||
writer io.WriteCloser
|
||||
}
|
||||
|
||||
func (sll *StandardLibLog) provision(ctx Context, logging *Logging) error {
|
||||
if sll.WriterRaw != nil {
|
||||
mod, err := ctx.LoadModule(sll, "WriterRaw")
|
||||
if err != nil {
|
||||
return fmt.Errorf("loading sink log writer module: %v", err)
|
||||
}
|
||||
wo := mod.(WriterOpener)
|
||||
|
||||
var isNew bool
|
||||
sll.writer, isNew, err = logging.openWriter(wo)
|
||||
if err != nil {
|
||||
return fmt.Errorf("opening sink log writer %#v: %v", mod, err)
|
||||
}
|
||||
|
||||
if isNew {
|
||||
log.Printf("[INFO] Redirecting sink to: %s", wo)
|
||||
log.SetOutput(sll.writer)
|
||||
log.Printf("[INFO] Redirected sink to here (%s)", wo)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// CustomLog represents a custom logger configuration.
|
||||
//
|
||||
// By default, a log will emit all log entries. Some entries
|
||||
// will be skipped if sampling is enabled. Further, the Include
|
||||
// and Exclude parameters define which loggers (by name) are
|
||||
// allowed or rejected from emitting in this log. If both Include
|
||||
// and Exclude are populated, their values must be mutually
|
||||
// exclusive, and longer namespaces have priority. If neither
|
||||
// are populated, all logs are emitted.
|
||||
type CustomLog struct {
|
||||
// The writer defines where log entries are emitted.
|
||||
WriterRaw json.RawMessage `json:"writer,omitempty" caddy:"namespace=caddy.logging.writers inline_key=output"`
|
||||
|
||||
// The encoder is how the log entries are formatted or encoded.
|
||||
EncoderRaw json.RawMessage `json:"encoder,omitempty" caddy:"namespace=caddy.logging.encoders inline_key=format"`
|
||||
|
||||
// Level is the minimum level to emit, and is inclusive.
|
||||
// Possible levels: DEBUG, INFO, WARN, ERROR, PANIC, and FATAL
|
||||
Level string `json:"level,omitempty"`
|
||||
|
||||
// Sampling configures log entry sampling. If enabled,
|
||||
// only some log entries will be emitted. This is useful
|
||||
// for improving performance on extremely high-pressure
|
||||
// servers.
|
||||
Sampling *LogSampling `json:"sampling,omitempty"`
|
||||
|
||||
// Include defines the names of loggers to emit in this
|
||||
// log. For example, to include only logs emitted by the
|
||||
// admin API, you would include "admin.api".
|
||||
Include []string `json:"include,omitempty"`
|
||||
|
||||
// Exclude defines the names of loggers that should be
|
||||
// skipped by this log. For example, to exclude only
|
||||
// HTTP access logs, you would exclude "http.log.access".
|
||||
Exclude []string `json:"exclude,omitempty"`
|
||||
|
||||
writerOpener WriterOpener
|
||||
writer io.WriteCloser
|
||||
encoder zapcore.Encoder
|
||||
levelEnabler zapcore.LevelEnabler
|
||||
core zapcore.Core
|
||||
}
|
||||
|
||||
func (cl *CustomLog) provision(ctx Context, logging *Logging) error {
|
||||
// Replace placeholder for log level
|
||||
repl := NewReplacer()
|
||||
level, err := repl.ReplaceOrErr(cl.Level, true, true)
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid log level: %v", err)
|
||||
}
|
||||
level = strings.ToLower(level)
|
||||
|
||||
// set up the log level
|
||||
switch level {
|
||||
case "debug":
|
||||
cl.levelEnabler = zapcore.DebugLevel
|
||||
case "", "info":
|
||||
cl.levelEnabler = zapcore.InfoLevel
|
||||
case "warn":
|
||||
cl.levelEnabler = zapcore.WarnLevel
|
||||
case "error":
|
||||
cl.levelEnabler = zapcore.ErrorLevel
|
||||
case "panic":
|
||||
cl.levelEnabler = zapcore.PanicLevel
|
||||
case "fatal":
|
||||
cl.levelEnabler = zapcore.FatalLevel
|
||||
default:
|
||||
return fmt.Errorf("unrecognized log level: %s", cl.Level)
|
||||
}
|
||||
|
||||
// If both Include and Exclude lists are populated, then each item must
|
||||
// be a superspace or subspace of an item in the other list, because
|
||||
// populating both lists means that any given item is either a rule
|
||||
// or an exception to another rule. But if the item is not a super-
|
||||
// or sub-space of any item in the other list, it is neither a rule
|
||||
// nor an exception, and is a contradiction. Ensure, too, that the
|
||||
// sets do not intersect, which is also a contradiction.
|
||||
if len(cl.Include) > 0 && len(cl.Exclude) > 0 {
|
||||
// prevent intersections
|
||||
for _, allow := range cl.Include {
|
||||
for _, deny := range cl.Exclude {
|
||||
if allow == deny {
|
||||
return fmt.Errorf("include and exclude must not intersect, but found %s in both lists", allow)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ensure namespaces are nested
|
||||
outer:
|
||||
for _, allow := range cl.Include {
|
||||
for _, deny := range cl.Exclude {
|
||||
if strings.HasPrefix(allow+".", deny+".") ||
|
||||
strings.HasPrefix(deny+".", allow+".") {
|
||||
continue outer
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("when both include and exclude are populated, each element must be a superspace or subspace of one in the other list; check '%s' in include", allow)
|
||||
}
|
||||
}
|
||||
|
||||
if cl.EncoderRaw != nil {
|
||||
mod, err := ctx.LoadModule(cl, "EncoderRaw")
|
||||
if err != nil {
|
||||
return fmt.Errorf("loading log encoder module: %v", err)
|
||||
}
|
||||
cl.encoder = mod.(zapcore.Encoder)
|
||||
}
|
||||
if cl.encoder == nil {
|
||||
cl.encoder = newDefaultProductionLogEncoder()
|
||||
}
|
||||
|
||||
if cl.WriterRaw != nil {
|
||||
mod, err := ctx.LoadModule(cl, "WriterRaw")
|
||||
if err != nil {
|
||||
return fmt.Errorf("loading log writer module: %v", err)
|
||||
}
|
||||
cl.writerOpener = mod.(WriterOpener)
|
||||
}
|
||||
if cl.writerOpener == nil {
|
||||
cl.writerOpener = StderrWriter{}
|
||||
}
|
||||
|
||||
cl.writer, _, err = logging.openWriter(cl.writerOpener)
|
||||
if err != nil {
|
||||
return fmt.Errorf("opening log writer using %#v: %v", cl.writerOpener, err)
|
||||
}
|
||||
|
||||
cl.buildCore()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cl *CustomLog) buildCore() {
|
||||
// logs which only discard their output don't need
|
||||
// to perform encoding or any other processing steps
|
||||
// at all, so just shorcut to a nop core instead
|
||||
if _, ok := cl.writerOpener.(*DiscardWriter); ok {
|
||||
cl.core = zapcore.NewNopCore()
|
||||
return
|
||||
}
|
||||
c := zapcore.NewCore(
|
||||
cl.encoder,
|
||||
zapcore.AddSync(cl.writer),
|
||||
cl.levelEnabler,
|
||||
)
|
||||
if cl.Sampling != nil {
|
||||
if cl.Sampling.Interval == 0 {
|
||||
cl.Sampling.Interval = 1 * time.Second
|
||||
}
|
||||
if cl.Sampling.First == 0 {
|
||||
cl.Sampling.First = 100
|
||||
}
|
||||
if cl.Sampling.Thereafter == 0 {
|
||||
cl.Sampling.Thereafter = 100
|
||||
}
|
||||
c = zapcore.NewSampler(c, cl.Sampling.Interval,
|
||||
cl.Sampling.First, cl.Sampling.Thereafter)
|
||||
}
|
||||
cl.core = c
|
||||
}
|
||||
|
||||
func (cl *CustomLog) matchesModule(moduleID string) bool {
|
||||
return cl.loggerAllowed(string(moduleID), true)
|
||||
}
|
||||
|
||||
// loggerAllowed returns true if name is allowed to emit
|
||||
// to cl. isModule should be true if name is the name of
|
||||
// a module and you want to see if ANY of that module's
|
||||
// logs would be permitted.
|
||||
func (cl *CustomLog) loggerAllowed(name string, isModule bool) bool {
|
||||
// accept all loggers by default
|
||||
if len(cl.Include) == 0 && len(cl.Exclude) == 0 {
|
||||
return true
|
||||
}
|
||||
|
||||
// append a dot so that partial names don't match
|
||||
// (i.e. we don't want "foo.b" to match "foo.bar"); we
|
||||
// will also have to append a dot when we do HasPrefix
|
||||
// below to compensate for when when namespaces are equal
|
||||
if name != "" && name != "*" && name != "." {
|
||||
name += "."
|
||||
}
|
||||
|
||||
var longestAccept, longestReject int
|
||||
|
||||
if len(cl.Include) > 0 {
|
||||
for _, namespace := range cl.Include {
|
||||
var hasPrefix bool
|
||||
if isModule {
|
||||
hasPrefix = strings.HasPrefix(namespace+".", name)
|
||||
} else {
|
||||
hasPrefix = strings.HasPrefix(name, namespace+".")
|
||||
}
|
||||
if hasPrefix && len(namespace) > longestAccept {
|
||||
longestAccept = len(namespace)
|
||||
}
|
||||
}
|
||||
// the include list was populated, meaning that
|
||||
// a match in this list is absolutely required
|
||||
// if we are to accept the entry
|
||||
if longestAccept == 0 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
if len(cl.Exclude) > 0 {
|
||||
for _, namespace := range cl.Exclude {
|
||||
// * == all logs emitted by modules
|
||||
// . == all logs emitted by core
|
||||
if (namespace == "*" && name != ".") ||
|
||||
(namespace == "." && name == ".") {
|
||||
return false
|
||||
}
|
||||
if strings.HasPrefix(name, namespace+".") &&
|
||||
len(namespace) > longestReject {
|
||||
longestReject = len(namespace)
|
||||
}
|
||||
}
|
||||
// the reject list is populated, so we have to
|
||||
// reject this entry if its match is better
|
||||
// than the best from the accept list
|
||||
if longestReject > longestAccept {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return (longestAccept > longestReject) ||
|
||||
(len(cl.Include) == 0 && longestReject == 0)
|
||||
}
|
||||
|
||||
// filteringCore filters log entries based on logger name,
|
||||
// according to the rules of a CustomLog.
|
||||
type filteringCore struct {
|
||||
zapcore.Core
|
||||
cl *CustomLog
|
||||
}
|
||||
|
||||
// With properly wraps With.
|
||||
func (fc *filteringCore) With(fields []zapcore.Field) zapcore.Core {
|
||||
return &filteringCore{
|
||||
Core: fc.Core.With(fields),
|
||||
cl: fc.cl,
|
||||
}
|
||||
}
|
||||
|
||||
// Check only allows the log entry if its logger name
|
||||
// is allowed from the include/exclude rules of fc.cl.
|
||||
func (fc *filteringCore) Check(e zapcore.Entry, ce *zapcore.CheckedEntry) *zapcore.CheckedEntry {
|
||||
if fc.cl.loggerAllowed(e.LoggerName, false) {
|
||||
return fc.Core.Check(e, ce)
|
||||
}
|
||||
return ce
|
||||
}
|
||||
|
||||
// LogSampling configures log entry sampling.
|
||||
type LogSampling struct {
|
||||
// The window over which to conduct sampling.
|
||||
Interval time.Duration `json:"interval,omitempty"`
|
||||
|
||||
// Log this many entries within a given level and
|
||||
// message for each interval.
|
||||
First int `json:"first,omitempty"`
|
||||
|
||||
// If more entries with the same level and message
|
||||
// are seen during the same interval, keep one in
|
||||
// this many entries until the end of the interval.
|
||||
Thereafter int `json:"thereafter,omitempty"`
|
||||
}
|
||||
|
||||
type (
|
||||
// StdoutWriter writes logs to standard out.
|
||||
StdoutWriter struct{}
|
||||
|
||||
// StderrWriter writes logs to standard error.
|
||||
StderrWriter struct{}
|
||||
|
||||
// DiscardWriter discards all writes.
|
||||
DiscardWriter struct{}
|
||||
)
|
||||
|
||||
// CaddyModule returns the Caddy module information.
|
||||
func (StdoutWriter) CaddyModule() ModuleInfo {
|
||||
return ModuleInfo{
|
||||
ID: "caddy.logging.writers.stdout",
|
||||
New: func() Module { return new(StdoutWriter) },
|
||||
}
|
||||
}
|
||||
|
||||
// CaddyModule returns the Caddy module information.
|
||||
func (StderrWriter) CaddyModule() ModuleInfo {
|
||||
return ModuleInfo{
|
||||
ID: "caddy.logging.writers.stderr",
|
||||
New: func() Module { return new(StderrWriter) },
|
||||
}
|
||||
}
|
||||
|
||||
// CaddyModule returns the Caddy module information.
|
||||
func (DiscardWriter) CaddyModule() ModuleInfo {
|
||||
return ModuleInfo{
|
||||
ID: "caddy.logging.writers.discard",
|
||||
New: func() Module { return new(DiscardWriter) },
|
||||
}
|
||||
}
|
||||
|
||||
func (StdoutWriter) String() string { return "stdout" }
|
||||
func (StderrWriter) String() string { return "stderr" }
|
||||
func (DiscardWriter) String() string { return "discard" }
|
||||
|
||||
// WriterKey returns a unique key representing stdout.
|
||||
func (StdoutWriter) WriterKey() string { return "std:out" }
|
||||
|
||||
// WriterKey returns a unique key representing stderr.
|
||||
func (StderrWriter) WriterKey() string { return "std:err" }
|
||||
|
||||
// WriterKey returns a unique key representing discard.
|
||||
func (DiscardWriter) WriterKey() string { return "discard" }
|
||||
|
||||
// OpenWriter returns os.Stdout that can't be closed.
|
||||
func (StdoutWriter) OpenWriter() (io.WriteCloser, error) {
|
||||
return notClosable{os.Stdout}, nil
|
||||
}
|
||||
|
||||
// OpenWriter returns os.Stderr that can't be closed.
|
||||
func (StderrWriter) OpenWriter() (io.WriteCloser, error) {
|
||||
return notClosable{os.Stderr}, nil
|
||||
}
|
||||
|
||||
// OpenWriter returns ioutil.Discard that can't be closed.
|
||||
func (DiscardWriter) OpenWriter() (io.WriteCloser, error) {
|
||||
return notClosable{ioutil.Discard}, nil
|
||||
}
|
||||
|
||||
// notClosable is an io.WriteCloser that can't be closed.
|
||||
type notClosable struct{ io.Writer }
|
||||
|
||||
func (fc notClosable) Close() error { return nil }
|
||||
|
||||
type defaultCustomLog struct {
|
||||
*CustomLog
|
||||
logger *zap.Logger
|
||||
}
|
||||
|
||||
// newDefaultProductionLog configures a custom log that is
|
||||
// intended for use by default if no other log is specified
|
||||
// in a config. It writes to stderr, uses the console encoder,
|
||||
// and enables INFO-level logs and higher.
|
||||
func newDefaultProductionLog() (*defaultCustomLog, error) {
|
||||
cl := new(CustomLog)
|
||||
cl.writerOpener = StderrWriter{}
|
||||
var err error
|
||||
cl.writer, err = cl.writerOpener.OpenWriter()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cl.encoder = newDefaultProductionLogEncoder()
|
||||
cl.levelEnabler = zapcore.InfoLevel
|
||||
|
||||
cl.buildCore()
|
||||
|
||||
return &defaultCustomLog{
|
||||
CustomLog: cl,
|
||||
logger: zap.New(cl.core),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func newDefaultProductionLogEncoder() zapcore.Encoder {
|
||||
encCfg := zap.NewProductionEncoderConfig()
|
||||
if terminal.IsTerminal(int(os.Stdout.Fd())) {
|
||||
// if interactive terminal, make output more human-readable by default
|
||||
encCfg.EncodeTime = func(ts time.Time, encoder zapcore.PrimitiveArrayEncoder) {
|
||||
encoder.AppendString(ts.UTC().Format("2006/01/02 15:04:05.000"))
|
||||
}
|
||||
encCfg.EncodeLevel = zapcore.CapitalColorLevelEncoder
|
||||
return zapcore.NewConsoleEncoder(encCfg)
|
||||
}
|
||||
return zapcore.NewJSONEncoder(encCfg)
|
||||
}
|
||||
|
||||
// Log returns the current default logger.
|
||||
func Log() *zap.Logger {
|
||||
defaultLoggerMu.RLock()
|
||||
defer defaultLoggerMu.RUnlock()
|
||||
return defaultLogger.logger
|
||||
}
|
||||
|
||||
var (
|
||||
defaultLogger, _ = newDefaultProductionLog()
|
||||
defaultLoggerMu sync.RWMutex
|
||||
)
|
||||
|
||||
var writers = NewUsagePool()
|
||||
|
||||
// Interface guards
|
||||
var (
|
||||
_ io.WriteCloser = (*notClosable)(nil)
|
||||
_ WriterOpener = (*StdoutWriter)(nil)
|
||||
_ WriterOpener = (*StderrWriter)(nil)
|
||||
)
|
||||
-362
@@ -1,362 +0,0 @@
|
||||
// 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 caddy
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Module is a type that is used as a Caddy module. In
|
||||
// addition to this interface, most modules will implement
|
||||
// some interface expected by their host module in order
|
||||
// to be useful. To learn which interface(s) to implement,
|
||||
// see the documentation for the host module. At a bare
|
||||
// minimum, this interface, when implemented, only provides
|
||||
// the module's ID and constructor function.
|
||||
//
|
||||
// Modules will often implement additional interfaces
|
||||
// including Provisioner, Validator, and CleanerUpper.
|
||||
// If a module implements these interfaces, their
|
||||
// methods are called during the module's lifespan.
|
||||
//
|
||||
// When a module is loaded by a host module, the following
|
||||
// happens: 1) ModuleInfo.New() is called to get a new
|
||||
// instance of the module. 2) The module's configuration is
|
||||
// unmarshaled into that instance. 3) If the module is a
|
||||
// Provisioner, the Provision() method is called. 4) If the
|
||||
// module is a Validator, the Validate() method is called.
|
||||
// 5) The module will probably be type-asserted from
|
||||
// interface{} to some other, more useful interface expected
|
||||
// by the host module. For example, HTTP handler modules are
|
||||
// type-asserted as caddyhttp.MiddlewareHandler values.
|
||||
// 6) When a module's containing Context is canceled, if it is
|
||||
// a CleanerUpper, its Cleanup() method is called.
|
||||
type Module interface {
|
||||
// This method indicates that the type is a Caddy
|
||||
// module. The returned ModuleInfo must have both
|
||||
// a name and a constructor function. This method
|
||||
// must not have any side-effects.
|
||||
CaddyModule() ModuleInfo
|
||||
}
|
||||
|
||||
// ModuleInfo represents a registered Caddy module.
|
||||
type ModuleInfo struct {
|
||||
// ID is the "full name" of the module. It
|
||||
// must be unique and properly namespaced.
|
||||
ID ModuleID
|
||||
|
||||
// New returns a pointer to a new, empty
|
||||
// instance of the module's type. This
|
||||
// function must not have any side-effects.
|
||||
New func() Module
|
||||
}
|
||||
|
||||
// ModuleID is a string that uniquely identifies a Caddy module. A
|
||||
// module ID is lightly structured. It consists of dot-separated
|
||||
// labels which form a simple hierarchy from left to right. The last
|
||||
// label is the module name, and the labels before that constitute
|
||||
// the namespace (or scope).
|
||||
//
|
||||
// Thus, a module ID has the form: <namespace>.<name>
|
||||
//
|
||||
// An ID with no dot has the empty namespace, which is appropriate
|
||||
// for app modules (these are "top-level" modules that Caddy core
|
||||
// loads and runs).
|
||||
//
|
||||
// Module IDs should be lowercase and use underscores (_) instead of
|
||||
// spaces.
|
||||
//
|
||||
// Examples of valid IDs:
|
||||
// - http
|
||||
// - http.handlers.file_server
|
||||
// - caddy.logging.encoders.json
|
||||
type ModuleID string
|
||||
|
||||
// Namespace returns the namespace (or scope) portion of a module ID,
|
||||
// which is all but the last label of the ID. If the ID has only one
|
||||
// label, then the namespace is empty.
|
||||
func (id ModuleID) Namespace() string {
|
||||
lastDot := strings.LastIndex(string(id), ".")
|
||||
if lastDot < 0 {
|
||||
return ""
|
||||
}
|
||||
return string(id)[:lastDot]
|
||||
}
|
||||
|
||||
// Name returns the Name (last element) of a module ID.
|
||||
func (id ModuleID) Name() string {
|
||||
if id == "" {
|
||||
return ""
|
||||
}
|
||||
parts := strings.Split(string(id), ".")
|
||||
return parts[len(parts)-1]
|
||||
}
|
||||
|
||||
func (mi ModuleInfo) String() string { return string(mi.ID) }
|
||||
|
||||
// ModuleMap is a map that can contain multiple modules,
|
||||
// where the map key is the module's name. (The namespace
|
||||
// is usually read from an associated field's struct tag.)
|
||||
// Because the module's name is given as the key in a
|
||||
// module map, the name does not have to be given in the
|
||||
// json.RawMessage.
|
||||
type ModuleMap map[string]json.RawMessage
|
||||
|
||||
// RegisterModule registers a module by receiving a
|
||||
// plain/empty value of the module. For registration to
|
||||
// be properly recorded, this should be called in the
|
||||
// init phase of runtime. Typically, the module package
|
||||
// will do this as a side-effect of being imported.
|
||||
// This function returns an error if the module's info
|
||||
// is incomplete or invalid, or if the module is
|
||||
// already registered.
|
||||
func RegisterModule(instance Module) error {
|
||||
mod := instance.CaddyModule()
|
||||
|
||||
if mod.ID == "" {
|
||||
return fmt.Errorf("module ID missing")
|
||||
}
|
||||
if mod.ID == "caddy" || mod.ID == "admin" {
|
||||
return fmt.Errorf("module ID '%s' is reserved", mod.ID)
|
||||
}
|
||||
if mod.New == nil {
|
||||
return fmt.Errorf("missing ModuleInfo.New")
|
||||
}
|
||||
if val := mod.New(); val == nil {
|
||||
return fmt.Errorf("ModuleInfo.New must return a non-nil module instance")
|
||||
}
|
||||
|
||||
modulesMu.Lock()
|
||||
defer modulesMu.Unlock()
|
||||
|
||||
if _, ok := modules[string(mod.ID)]; ok {
|
||||
return fmt.Errorf("module already registered: %s", mod.ID)
|
||||
}
|
||||
modules[string(mod.ID)] = mod
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetModule returns module information from its ID (full name).
|
||||
func GetModule(name string) (ModuleInfo, error) {
|
||||
modulesMu.RLock()
|
||||
defer modulesMu.RUnlock()
|
||||
m, ok := modules[name]
|
||||
if !ok {
|
||||
return ModuleInfo{}, fmt.Errorf("module not registered: %s", name)
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// GetModuleName returns a module's name (the last label of its ID)
|
||||
// from an instance of its value. If the value is not a module, an
|
||||
// empty string will be returned.
|
||||
func GetModuleName(instance interface{}) string {
|
||||
var name string
|
||||
if mod, ok := instance.(Module); ok {
|
||||
name = mod.CaddyModule().ID.Name()
|
||||
}
|
||||
return name
|
||||
}
|
||||
|
||||
// GetModuleID returns a module's ID from an instance of its value.
|
||||
// If the value is not a module, an empty string will be returned.
|
||||
func GetModuleID(instance interface{}) string {
|
||||
var id string
|
||||
if mod, ok := instance.(Module); ok {
|
||||
id = string(mod.CaddyModule().ID)
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
// GetModules returns all modules in the given scope/namespace.
|
||||
// For example, a scope of "foo" returns modules named "foo.bar",
|
||||
// "foo.loo", but not "bar", "foo.bar.loo", etc. An empty scope
|
||||
// returns top-level modules, for example "foo" or "bar". Partial
|
||||
// scopes are not matched (i.e. scope "foo.ba" does not match
|
||||
// name "foo.bar").
|
||||
//
|
||||
// Because modules are registered to a map under the hood, the
|
||||
// returned slice will be sorted to keep it deterministic.
|
||||
func GetModules(scope string) []ModuleInfo {
|
||||
modulesMu.RLock()
|
||||
defer modulesMu.RUnlock()
|
||||
|
||||
scopeParts := strings.Split(scope, ".")
|
||||
|
||||
// handle the special case of an empty scope, which
|
||||
// should match only the top-level modules
|
||||
if scope == "" {
|
||||
scopeParts = []string{}
|
||||
}
|
||||
|
||||
var mods []ModuleInfo
|
||||
iterateModules:
|
||||
for id, m := range modules {
|
||||
modParts := strings.Split(string(id), ".")
|
||||
|
||||
// match only the next level of nesting
|
||||
if len(modParts) != len(scopeParts)+1 {
|
||||
continue
|
||||
}
|
||||
|
||||
// specified parts must be exact matches
|
||||
for i := range scopeParts {
|
||||
if modParts[i] != scopeParts[i] {
|
||||
continue iterateModules
|
||||
}
|
||||
}
|
||||
|
||||
mods = append(mods, m)
|
||||
}
|
||||
|
||||
// make return value deterministic
|
||||
sort.Slice(mods, func(i, j int) bool {
|
||||
return mods[i].ID < mods[j].ID
|
||||
})
|
||||
|
||||
return mods
|
||||
}
|
||||
|
||||
// Modules returns the names of all registered modules
|
||||
// in ascending lexicographical order.
|
||||
func Modules() []string {
|
||||
modulesMu.RLock()
|
||||
defer modulesMu.RUnlock()
|
||||
|
||||
var names []string
|
||||
for name := range modules {
|
||||
names = append(names, string(name))
|
||||
}
|
||||
|
||||
sort.Strings(names)
|
||||
|
||||
return names
|
||||
}
|
||||
|
||||
// getModuleNameInline loads the string value from raw of moduleNameKey,
|
||||
// where raw must be a JSON encoding of a map. It returns that value,
|
||||
// along with the result of removing that key from raw.
|
||||
func getModuleNameInline(moduleNameKey string, raw json.RawMessage) (string, json.RawMessage, error) {
|
||||
var tmp map[string]interface{}
|
||||
err := json.Unmarshal(raw, &tmp)
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
|
||||
moduleName, ok := tmp[moduleNameKey].(string)
|
||||
if !ok || moduleName == "" {
|
||||
return "", nil, fmt.Errorf("module name not specified with key '%s' in %+v", moduleNameKey, tmp)
|
||||
}
|
||||
|
||||
// remove key from the object, otherwise decoding it later
|
||||
// will yield an error because the struct won't recognize it
|
||||
// (this is only needed because we strictly enforce that
|
||||
// all keys are recognized when loading modules)
|
||||
delete(tmp, moduleNameKey)
|
||||
result, err := json.Marshal(tmp)
|
||||
if err != nil {
|
||||
return "", nil, fmt.Errorf("re-encoding module configuration: %v", err)
|
||||
}
|
||||
|
||||
return moduleName, result, nil
|
||||
}
|
||||
|
||||
// Provisioner is implemented by modules which may need to perform
|
||||
// some additional "setup" steps immediately after being loaded.
|
||||
// Provisioning should be fast (imperceptible running time). If
|
||||
// any side-effects result in the execution of this function (e.g.
|
||||
// creating global state, any other allocations which require
|
||||
// garbage collection, opening files, starting goroutines etc.),
|
||||
// be sure to clean up properly by implementing the CleanerUpper
|
||||
// interface to avoid leaking resources.
|
||||
type Provisioner interface {
|
||||
Provision(Context) error
|
||||
}
|
||||
|
||||
// Validator is implemented by modules which can verify that their
|
||||
// configurations are valid. This method will be called after
|
||||
// Provision() (if implemented). Validation should always be fast
|
||||
// (imperceptible running time) and an error must be returned if
|
||||
// the module's configuration is invalid.
|
||||
type Validator interface {
|
||||
Validate() error
|
||||
}
|
||||
|
||||
// CleanerUpper is implemented by modules which may have side-effects
|
||||
// such as opened files, spawned goroutines, or allocated some sort
|
||||
// of non-stack state when they were provisioned. This method should
|
||||
// deallocate/cleanup those resources to prevent memory leaks. Cleanup
|
||||
// should be fast and efficient. Cleanup should work even if Provision
|
||||
// returns an error, to allow cleaning up from partial provisionings.
|
||||
type CleanerUpper interface {
|
||||
Cleanup() error
|
||||
}
|
||||
|
||||
// ParseStructTag parses a caddy struct tag into its keys and values.
|
||||
// It is very simple. The expected syntax is:
|
||||
// `caddy:"key1=val1 key2=val2 ..."`
|
||||
func ParseStructTag(tag string) (map[string]string, error) {
|
||||
results := make(map[string]string)
|
||||
pairs := strings.Split(tag, " ")
|
||||
for i, pair := range pairs {
|
||||
if pair == "" {
|
||||
continue
|
||||
}
|
||||
parts := strings.SplitN(pair, "=", 2)
|
||||
if len(parts) != 2 {
|
||||
return nil, fmt.Errorf("missing key in '%s' (pair %d)", pair, i)
|
||||
}
|
||||
results[parts[0]] = parts[1]
|
||||
}
|
||||
return results, nil
|
||||
}
|
||||
|
||||
// strictUnmarshalJSON is like json.Unmarshal but returns an error
|
||||
// if any of the fields are unrecognized. Useful when decoding
|
||||
// module configurations, where you want to be more sure they're
|
||||
// correct.
|
||||
func strictUnmarshalJSON(data []byte, v interface{}) error {
|
||||
dec := json.NewDecoder(bytes.NewReader(data))
|
||||
dec.DisallowUnknownFields()
|
||||
return dec.Decode(v)
|
||||
}
|
||||
|
||||
// isJSONRawMessage returns true if the type is encoding/json.RawMessage.
|
||||
func isJSONRawMessage(typ reflect.Type) bool {
|
||||
return typ.PkgPath() == "encoding/json" && typ.Name() == "RawMessage"
|
||||
}
|
||||
|
||||
// isModuleMapType returns true if the type is map[string]json.RawMessage.
|
||||
// It assumes that the string key is the module name, but this is not
|
||||
// always the case. To know for sure, this function must return true, but
|
||||
// also the struct tag where this type appears must NOT define an inline_key
|
||||
// attribute, which would mean that the module names appear inline with the
|
||||
// values, not in the key.
|
||||
func isModuleMapType(typ reflect.Type) bool {
|
||||
return typ.Kind() == reflect.Map &&
|
||||
typ.Key().Kind() == reflect.String &&
|
||||
isJSONRawMessage(typ.Elem())
|
||||
}
|
||||
|
||||
var (
|
||||
modules = make(map[string]ModuleInfo)
|
||||
modulesMu sync.RWMutex
|
||||
)
|
||||
@@ -1,433 +0,0 @@
|
||||
// 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 caddyhttp
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/caddyserver/caddy/v2"
|
||||
"github.com/caddyserver/caddy/v2/modules/caddytls"
|
||||
"github.com/lucas-clemente/quic-go/http3"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func init() {
|
||||
err := caddy.RegisterModule(App{})
|
||||
if err != nil {
|
||||
caddy.Log().Fatal(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// App is a robust, production-ready HTTP server.
|
||||
//
|
||||
// HTTPS is enabled by default if host matchers with qualifying names are used
|
||||
// in any of routes; certificates are automatically provisioned and renewed.
|
||||
// Additionally, automatic HTTPS will also enable HTTPS for servers that listen
|
||||
// only on the HTTPS port but which do not have any TLS connection policies
|
||||
// defined by adding a good, default TLS connection policy.
|
||||
//
|
||||
// In HTTP routes, additional placeholders are available (replace any `*`):
|
||||
//
|
||||
// Placeholder | Description
|
||||
// ------------|---------------
|
||||
// `{http.request.cookie.*}` | HTTP request cookie
|
||||
// `{http.request.header.*}` | Specific request header field
|
||||
// `{http.request.host.labels.*}` | Request host labels (0-based from right); e.g. for foo.example.com: 0=com, 1=example, 2=foo
|
||||
// `{http.request.host}` | The host part of the request's Host header
|
||||
// `{http.request.hostport}` | The host and port from the request's Host header
|
||||
// `{http.request.method}` | The request method
|
||||
// `{http.request.orig_method}` | The request's original method
|
||||
// `{http.request.orig_uri.path.dir}` | The request's original directory
|
||||
// `{http.request.orig_uri.path.file}` | The request's original filename
|
||||
// `{http.request.orig_uri.path}` | The request's original path
|
||||
// `{http.request.orig_uri.query}` | The request's original query string (without `?`)
|
||||
// `{http.request.orig_uri}` | The request's original URI
|
||||
// `{http.request.port}` | The port part of the request's Host header
|
||||
// `{http.request.proto}` | The protocol of the request
|
||||
// `{http.request.remote.host}` | The host part of the remote client's address
|
||||
// `{http.request.remote.port}` | The port part of the remote client's address
|
||||
// `{http.request.remote}` | The address of the remote client
|
||||
// `{http.request.scheme}` | The request scheme
|
||||
// `{http.request.tls.version}` | The TLS version name
|
||||
// `{http.request.tls.cipher_suite}` | The TLS cipher suite
|
||||
// `{http.request.tls.resumed}` | The TLS connection resumed a previous connection
|
||||
// `{http.request.tls.proto}` | The negotiated next protocol
|
||||
// `{http.request.tls.proto_mutual}` | The negotiated next protocol was advertised by the server
|
||||
// `{http.request.tls.server_name}` | The server name requested by the client, if any
|
||||
// `{http.request.tls.client.fingerprint}` | The SHA256 checksum of the client certificate
|
||||
// `{http.request.tls.client.issuer}` | The issuer DN of the client certificate
|
||||
// `{http.request.tls.client.serial}` | The serial number of the client certificate
|
||||
// `{http.request.tls.client.subject}` | The subject DN of the client certificate
|
||||
// `{http.request.uri.path.*}` | Parts of the path, split by `/` (0-based from left)
|
||||
// `{http.request.uri.path.dir}` | The directory, excluding leaf filename
|
||||
// `{http.request.uri.path.file}` | The filename of the path, excluding directory
|
||||
// `{http.request.uri.path}` | The path component of the request URI
|
||||
// `{http.request.uri.query.*}` | Individual query string value
|
||||
// `{http.request.uri.query}` | The query string (without `?`)
|
||||
// `{http.request.uri}` | The full request URI
|
||||
// `{http.response.header.*}` | Specific response header field
|
||||
// `{http.vars.*}` | Custom variables in the HTTP handler chain
|
||||
type App struct {
|
||||
// HTTPPort specifies the port to use for HTTP (as opposed to HTTPS),
|
||||
// which is used when setting up HTTP->HTTPS redirects or ACME HTTP
|
||||
// challenge solvers. Default: 80.
|
||||
HTTPPort int `json:"http_port,omitempty"`
|
||||
|
||||
// HTTPSPort specifies the port to use for HTTPS, which is used when
|
||||
// solving the ACME TLS-ALPN challenges, or whenever HTTPS is needed
|
||||
// but no specific port number is given. Default: 443.
|
||||
HTTPSPort int `json:"https_port,omitempty"`
|
||||
|
||||
// GracePeriod is how long to wait for active connections when shutting
|
||||
// down the server. Once the grace period is over, connections will
|
||||
// be forcefully closed.
|
||||
GracePeriod caddy.Duration `json:"grace_period,omitempty"`
|
||||
|
||||
// Servers is the list of servers, keyed by arbitrary names chosen
|
||||
// at your discretion for your own convenience; the keys do not
|
||||
// affect functionality.
|
||||
Servers map[string]*Server `json:"servers,omitempty"`
|
||||
|
||||
servers []*http.Server
|
||||
h3servers []*http3.Server
|
||||
h3listeners []net.PacketConn
|
||||
|
||||
ctx caddy.Context
|
||||
logger *zap.Logger
|
||||
tlsApp *caddytls.TLS
|
||||
|
||||
// used temporarily between phases 1 and 2 of auto HTTPS
|
||||
allCertDomains []string
|
||||
}
|
||||
|
||||
// CaddyModule returns the Caddy module information.
|
||||
func (App) CaddyModule() caddy.ModuleInfo {
|
||||
return caddy.ModuleInfo{
|
||||
ID: "http",
|
||||
New: func() caddy.Module { return new(App) },
|
||||
}
|
||||
}
|
||||
|
||||
// Provision sets up the app.
|
||||
func (app *App) Provision(ctx caddy.Context) error {
|
||||
// store some references
|
||||
tlsAppIface, err := ctx.App("tls")
|
||||
if err != nil {
|
||||
return fmt.Errorf("getting tls app: %v", err)
|
||||
}
|
||||
app.tlsApp = tlsAppIface.(*caddytls.TLS)
|
||||
app.ctx = ctx
|
||||
app.logger = ctx.Logger(app)
|
||||
|
||||
repl := caddy.NewReplacer()
|
||||
|
||||
// this provisions the matchers for each route,
|
||||
// and prepares auto HTTP->HTTPS redirects, and
|
||||
// is required before we provision each server
|
||||
err = app.automaticHTTPSPhase1(ctx, repl)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// prepare each server
|
||||
for srvName, srv := range app.Servers {
|
||||
srv.tlsApp = app.tlsApp
|
||||
srv.logger = app.logger.Named("log")
|
||||
srv.errorLogger = app.logger.Named("log.error")
|
||||
|
||||
// only enable access logs if configured
|
||||
if srv.Logs != nil {
|
||||
srv.accessLogger = app.logger.Named("log.access")
|
||||
}
|
||||
|
||||
// if not explicitly configured by the user, disallow TLS
|
||||
// client auth bypass (domain fronting) which could
|
||||
// otherwise be exploited by sending an unprotected SNI
|
||||
// value during a TLS handshake, then putting a protected
|
||||
// domain in the Host header after establishing connection;
|
||||
// this is a safe default, but we allow users to override
|
||||
// it for example in the case of running a proxy where
|
||||
// domain fronting is desired and access is not restricted
|
||||
// based on hostname
|
||||
if srv.StrictSNIHost == nil && srv.hasTLSClientAuth() {
|
||||
app.logger.Info("enabling strict SNI-Host matching because TLS client auth is configured",
|
||||
zap.String("server_name", srvName),
|
||||
)
|
||||
trueBool := true
|
||||
srv.StrictSNIHost = &trueBool
|
||||
}
|
||||
|
||||
// process each listener address
|
||||
for i := range srv.Listen {
|
||||
lnOut, err := repl.ReplaceOrErr(srv.Listen[i], true, true)
|
||||
if err != nil {
|
||||
return fmt.Errorf("server %s, listener %d: %v",
|
||||
srvName, i, err)
|
||||
}
|
||||
srv.Listen[i] = lnOut
|
||||
}
|
||||
|
||||
// set up each listener modifier
|
||||
if srv.ListenerWrappersRaw != nil {
|
||||
vals, err := ctx.LoadModule(srv, "ListenerWrappersRaw")
|
||||
if err != nil {
|
||||
return fmt.Errorf("loading listener wrapper modules: %v", err)
|
||||
}
|
||||
var hasTLSPlaceholder bool
|
||||
for i, val := range vals.([]interface{}) {
|
||||
if _, ok := val.(*tlsPlaceholderWrapper); ok {
|
||||
if i == 0 {
|
||||
// putting the tls placeholder wrapper first is nonsensical because
|
||||
// that is the default, implicit setting: without it, all wrappers
|
||||
// will go after the TLS listener anyway
|
||||
return fmt.Errorf("it is unnecessary to specify the TLS listener wrapper in the first position because that is the default")
|
||||
}
|
||||
if hasTLSPlaceholder {
|
||||
return fmt.Errorf("TLS listener wrapper can only be specified once")
|
||||
}
|
||||
hasTLSPlaceholder = true
|
||||
}
|
||||
srv.listenerWrappers = append(srv.listenerWrappers, val.(caddy.ListenerWrapper))
|
||||
}
|
||||
// if any wrappers were configured but the TLS placeholder wrapper is
|
||||
// absent, prepend it so all defined wrappers come after the TLS
|
||||
// handshake; this simplifies logic when starting the server, since we
|
||||
// can simply assume the TLS placeholder will always be there
|
||||
if !hasTLSPlaceholder && len(srv.listenerWrappers) > 0 {
|
||||
srv.listenerWrappers = append([]caddy.ListenerWrapper{new(tlsPlaceholderWrapper)}, srv.listenerWrappers...)
|
||||
}
|
||||
}
|
||||
|
||||
// pre-compile the primary handler chain, and be sure to wrap it in our
|
||||
// route handler so that important security checks are done, etc.
|
||||
primaryRoute := emptyHandler
|
||||
if srv.Routes != nil {
|
||||
err := srv.Routes.ProvisionHandlers(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("server %s: setting up route handlers: %v", srvName, err)
|
||||
}
|
||||
primaryRoute = srv.Routes.Compile(emptyHandler)
|
||||
}
|
||||
srv.primaryHandlerChain = srv.wrapPrimaryRoute(primaryRoute)
|
||||
|
||||
// pre-compile the error handler chain
|
||||
if srv.Errors != nil {
|
||||
err := srv.Errors.Routes.Provision(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("server %s: setting up server error handling routes: %v", srvName, err)
|
||||
}
|
||||
srv.errorHandlerChain = srv.Errors.Routes.Compile(errorEmptyHandler)
|
||||
}
|
||||
|
||||
// prepare the TLS connection policies
|
||||
err = srv.TLSConnPolicies.Provision(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("server %s: setting up TLS connection policies: %v", srvName, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Validate ensures the app's configuration is valid.
|
||||
func (app *App) Validate() error {
|
||||
// each server must use distinct listener addresses
|
||||
lnAddrs := make(map[string]string)
|
||||
for srvName, srv := range app.Servers {
|
||||
for _, addr := range srv.Listen {
|
||||
listenAddr, err := caddy.ParseNetworkAddress(addr)
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid listener address '%s': %v", addr, err)
|
||||
}
|
||||
// check that every address in the port range is unique to this server;
|
||||
// we do not use <= here because PortRangeSize() adds 1 to EndPort for us
|
||||
for i := uint(0); i < listenAddr.PortRangeSize(); i++ {
|
||||
addr := caddy.JoinNetworkAddress(listenAddr.Network, listenAddr.Host, strconv.Itoa(int(listenAddr.StartPort+i)))
|
||||
if sn, ok := lnAddrs[addr]; ok {
|
||||
return fmt.Errorf("server %s: listener address repeated: %s (already claimed by server '%s')", srvName, addr, sn)
|
||||
}
|
||||
lnAddrs[addr] = srvName
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Start runs the app. It finishes automatic HTTPS if enabled,
|
||||
// including management of certificates.
|
||||
func (app *App) Start() error {
|
||||
for srvName, srv := range app.Servers {
|
||||
s := &http.Server{
|
||||
ReadTimeout: time.Duration(srv.ReadTimeout),
|
||||
ReadHeaderTimeout: time.Duration(srv.ReadHeaderTimeout),
|
||||
WriteTimeout: time.Duration(srv.WriteTimeout),
|
||||
IdleTimeout: time.Duration(srv.IdleTimeout),
|
||||
MaxHeaderBytes: srv.MaxHeaderBytes,
|
||||
Handler: srv,
|
||||
}
|
||||
|
||||
for _, lnAddr := range srv.Listen {
|
||||
listenAddr, err := caddy.ParseNetworkAddress(lnAddr)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s: parsing listen address '%s': %v", srvName, lnAddr, err)
|
||||
}
|
||||
for portOffset := uint(0); portOffset < listenAddr.PortRangeSize(); portOffset++ {
|
||||
// create the listener for this socket
|
||||
hostport := listenAddr.JoinHostPort(portOffset)
|
||||
ln, err := caddy.Listen(listenAddr.Network, hostport)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s: listening on %s: %v", listenAddr.Network, hostport, err)
|
||||
}
|
||||
|
||||
// wrap listener before TLS (up to the TLS placeholder wrapper)
|
||||
var lnWrapperIdx int
|
||||
for i, lnWrapper := range srv.listenerWrappers {
|
||||
if _, ok := lnWrapper.(*tlsPlaceholderWrapper); ok {
|
||||
lnWrapperIdx = i + 1 // mark the next wrapper's spot
|
||||
break
|
||||
}
|
||||
ln = lnWrapper.WrapListener(ln)
|
||||
}
|
||||
|
||||
// enable TLS if there is a policy and if this is not the HTTP port
|
||||
useTLS := len(srv.TLSConnPolicies) > 0 && int(listenAddr.StartPort+portOffset) != app.httpPort()
|
||||
if useTLS {
|
||||
// create TLS listener
|
||||
tlsCfg := srv.TLSConnPolicies.TLSConfig(app.ctx)
|
||||
ln = tls.NewListener(ln, tlsCfg)
|
||||
|
||||
/////////
|
||||
// TODO: HTTP/3 support is experimental for now
|
||||
if srv.ExperimentalHTTP3 {
|
||||
app.logger.Info("enabling experimental HTTP/3 listener",
|
||||
zap.String("addr", hostport),
|
||||
)
|
||||
h3ln, err := caddy.ListenPacket("udp", hostport)
|
||||
if err != nil {
|
||||
return fmt.Errorf("getting HTTP/3 UDP listener: %v", err)
|
||||
}
|
||||
h3srv := &http3.Server{
|
||||
Server: &http.Server{
|
||||
Addr: hostport,
|
||||
Handler: srv,
|
||||
TLSConfig: tlsCfg,
|
||||
},
|
||||
}
|
||||
go h3srv.Serve(h3ln)
|
||||
app.h3servers = append(app.h3servers, h3srv)
|
||||
app.h3listeners = append(app.h3listeners, h3ln)
|
||||
srv.h3server = h3srv
|
||||
}
|
||||
/////////
|
||||
}
|
||||
|
||||
// finish wrapping listener where we left off before TLS
|
||||
for i := lnWrapperIdx; i < len(srv.listenerWrappers); i++ {
|
||||
ln = srv.listenerWrappers[i].WrapListener(ln)
|
||||
}
|
||||
|
||||
app.logger.Debug("starting server loop",
|
||||
zap.String("address", lnAddr),
|
||||
zap.Bool("http3", srv.ExperimentalHTTP3),
|
||||
zap.Bool("tls", useTLS),
|
||||
)
|
||||
|
||||
go s.Serve(ln)
|
||||
app.servers = append(app.servers, s)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// finish automatic HTTPS by finally beginning
|
||||
// certificate management
|
||||
err := app.automaticHTTPSPhase2()
|
||||
if err != nil {
|
||||
return fmt.Errorf("finalizing automatic HTTPS: %v", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Stop gracefully shuts down the HTTP server.
|
||||
func (app *App) Stop() error {
|
||||
ctx := context.Background()
|
||||
if app.GracePeriod > 0 {
|
||||
var cancel context.CancelFunc
|
||||
ctx, cancel = context.WithTimeout(ctx, time.Duration(app.GracePeriod))
|
||||
defer cancel()
|
||||
}
|
||||
for _, s := range app.servers {
|
||||
err := s.Shutdown(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// close the http3 servers; it's unclear whether the bug reported in
|
||||
// https://github.com/caddyserver/caddy/pull/2727#issuecomment-526856566
|
||||
// was ever truly fixed, since it seemed racey/nondeterministic; but
|
||||
// recent tests in 2020 were unable to replicate the issue again after
|
||||
// repeated attempts (the bug manifested after a config reload; i.e.
|
||||
// reusing a http3 server or listener was problematic), but it seems
|
||||
// to be working fine now
|
||||
for _, s := range app.h3servers {
|
||||
// TODO: CloseGracefully, once implemented upstream
|
||||
// (see https://github.com/lucas-clemente/quic-go/issues/2103)
|
||||
err := s.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// closing an http3.Server does not close their underlying listeners
|
||||
// since apparently the listener can be used both by servers and
|
||||
// clients at the same time; so we need to manually call Close()
|
||||
// on the underlying h3 listeners (see lucas-clemente/quic-go#2103)
|
||||
for _, pc := range app.h3listeners {
|
||||
err := pc.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (app *App) httpPort() int {
|
||||
if app.HTTPPort == 0 {
|
||||
return DefaultHTTPPort
|
||||
}
|
||||
return app.HTTPPort
|
||||
}
|
||||
|
||||
func (app *App) httpsPort() int {
|
||||
if app.HTTPSPort == 0 {
|
||||
return DefaultHTTPSPort
|
||||
}
|
||||
return app.HTTPSPort
|
||||
}
|
||||
|
||||
// Interface guards
|
||||
var (
|
||||
_ caddy.App = (*App)(nil)
|
||||
_ caddy.Provisioner = (*App)(nil)
|
||||
_ caddy.Validator = (*App)(nil)
|
||||
)
|
||||
@@ -1,533 +0,0 @@
|
||||
// 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 caddyhttp
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/caddyserver/caddy/v2"
|
||||
"github.com/caddyserver/caddy/v2/modules/caddytls"
|
||||
"github.com/caddyserver/certmagic"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// AutoHTTPSConfig is used to disable automatic HTTPS
|
||||
// or certain aspects of it for a specific server.
|
||||
// HTTPS is enabled automatically and by default when
|
||||
// qualifying hostnames are available from the config.
|
||||
type AutoHTTPSConfig struct {
|
||||
// If true, automatic HTTPS will be entirely disabled.
|
||||
Disabled bool `json:"disable,omitempty"`
|
||||
|
||||
// If true, only automatic HTTP->HTTPS redirects will
|
||||
// be disabled.
|
||||
DisableRedir bool `json:"disable_redirects,omitempty"`
|
||||
|
||||
// Hosts/domain names listed here will not be included
|
||||
// in automatic HTTPS (they will not have certificates
|
||||
// loaded nor redirects applied).
|
||||
Skip []string `json:"skip,omitempty"`
|
||||
|
||||
// Hosts/domain names listed here will still be enabled
|
||||
// for automatic HTTPS (unless in the Skip list), except
|
||||
// that certificates will not be provisioned and managed
|
||||
// for these names.
|
||||
SkipCerts []string `json:"skip_certificates,omitempty"`
|
||||
|
||||
// By default, automatic HTTPS will obtain and renew
|
||||
// certificates for qualifying hostnames. However, if
|
||||
// a certificate with a matching SAN is already loaded
|
||||
// into the cache, certificate management will not be
|
||||
// enabled. To force automated certificate management
|
||||
// regardless of loaded certificates, set this to true.
|
||||
IgnoreLoadedCerts bool `json:"ignore_loaded_certificates,omitempty"`
|
||||
}
|
||||
|
||||
// Skipped returns true if name is in skipSlice, which
|
||||
// should be either the Skip or SkipCerts field on ahc.
|
||||
func (ahc AutoHTTPSConfig) Skipped(name string, skipSlice []string) bool {
|
||||
for _, n := range skipSlice {
|
||||
if name == n {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// automaticHTTPSPhase1 provisions all route matchers, determines
|
||||
// which domain names found in the routes qualify for automatic
|
||||
// HTTPS, and sets up HTTP->HTTPS redirects. This phase must occur
|
||||
// at the beginning of provisioning, because it may add routes and
|
||||
// even servers to the app, which still need to be set up with the
|
||||
// rest of them during provisioning.
|
||||
func (app *App) automaticHTTPSPhase1(ctx caddy.Context, repl *caddy.Replacer) error {
|
||||
// this map acts as a set to store the domain names
|
||||
// for which we will manage certificates automatically
|
||||
uniqueDomainsForCerts := make(map[string]struct{})
|
||||
|
||||
// this maps domain names for automatic HTTP->HTTPS
|
||||
// redirects to their destination server address
|
||||
redirDomains := make(map[string]caddy.ParsedAddress)
|
||||
|
||||
for srvName, srv := range app.Servers {
|
||||
// as a prerequisite, provision route matchers; this is
|
||||
// required for all routes on all servers, and must be
|
||||
// done before we attempt to do phase 1 of auto HTTPS,
|
||||
// since we have to access the decoded host matchers the
|
||||
// handlers will be provisioned later
|
||||
if srv.Routes != nil {
|
||||
err := srv.Routes.ProvisionMatchers(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("server %s: setting up route matchers: %v", srvName, err)
|
||||
}
|
||||
}
|
||||
|
||||
// prepare for automatic HTTPS
|
||||
if srv.AutoHTTPS == nil {
|
||||
srv.AutoHTTPS = new(AutoHTTPSConfig)
|
||||
}
|
||||
if srv.AutoHTTPS.Disabled {
|
||||
continue
|
||||
}
|
||||
|
||||
// skip if all listeners use the HTTP port
|
||||
if !srv.listenersUseAnyPortOtherThan(app.httpPort()) {
|
||||
app.logger.Info("server is listening only on the HTTP port, so no automatic HTTPS will be applied to this server",
|
||||
zap.String("server_name", srvName),
|
||||
zap.Int("http_port", app.httpPort()),
|
||||
)
|
||||
srv.AutoHTTPS.Disabled = true
|
||||
continue
|
||||
}
|
||||
|
||||
// if all listeners are on the HTTPS port, make sure
|
||||
// there is at least one TLS connection policy; it
|
||||
// should be obvious that they want to use TLS without
|
||||
// needing to specify one empty policy to enable it
|
||||
if srv.TLSConnPolicies == nil &&
|
||||
!srv.listenersUseAnyPortOtherThan(app.httpsPort()) {
|
||||
app.logger.Info("server is listening only on the HTTPS port but has no TLS connection policies; adding one to enable TLS",
|
||||
zap.String("server_name", srvName),
|
||||
zap.Int("https_port", app.httpsPort()),
|
||||
)
|
||||
srv.TLSConnPolicies = caddytls.ConnectionPolicies{new(caddytls.ConnectionPolicy)}
|
||||
}
|
||||
|
||||
// find all qualifying domain names (deduplicated) in this server
|
||||
serverDomainSet := make(map[string]struct{})
|
||||
for routeIdx, route := range srv.Routes {
|
||||
for matcherSetIdx, matcherSet := range route.MatcherSets {
|
||||
for matcherIdx, m := range matcherSet {
|
||||
if hm, ok := m.(*MatchHost); ok {
|
||||
for hostMatcherIdx, d := range *hm {
|
||||
var err error
|
||||
d, err = repl.ReplaceOrErr(d, true, false)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s: route %d, matcher set %d, matcher %d, host matcher %d: %v",
|
||||
srvName, routeIdx, matcherSetIdx, matcherIdx, hostMatcherIdx, err)
|
||||
}
|
||||
if !srv.AutoHTTPS.Skipped(d, srv.AutoHTTPS.Skip) {
|
||||
serverDomainSet[d] = struct{}{}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// nothing more to do here if there are no
|
||||
// domains that qualify for automatic HTTPS
|
||||
if len(serverDomainSet) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
// for all the hostnames we found, filter them so we have
|
||||
// a deduplicated list of names for which to obtain certs
|
||||
for d := range serverDomainSet {
|
||||
if certmagic.SubjectQualifiesForCert(d) &&
|
||||
!srv.AutoHTTPS.Skipped(d, srv.AutoHTTPS.SkipCerts) {
|
||||
// if a certificate for this name is already loaded,
|
||||
// don't obtain another one for it, unless we are
|
||||
// supposed to ignore loaded certificates
|
||||
if !srv.AutoHTTPS.IgnoreLoadedCerts &&
|
||||
len(app.tlsApp.AllMatchingCertificates(d)) > 0 {
|
||||
app.logger.Info("skipping automatic certificate management because one or more matching certificates are already loaded",
|
||||
zap.String("domain", d),
|
||||
zap.String("server_name", srvName),
|
||||
)
|
||||
continue
|
||||
}
|
||||
|
||||
// most clients don't accept wildcards like *.tld... we
|
||||
// can handle that, but as a courtesy, warn the user
|
||||
if strings.Contains(d, "*") &&
|
||||
strings.Count(strings.Trim(d, "."), ".") == 1 {
|
||||
app.logger.Warn("most clients do not trust second-level wildcard certificates (*.tld)",
|
||||
zap.String("domain", d))
|
||||
}
|
||||
|
||||
uniqueDomainsForCerts[d] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
// tell the server to use TLS if it is not already doing so
|
||||
if srv.TLSConnPolicies == nil {
|
||||
srv.TLSConnPolicies = caddytls.ConnectionPolicies{new(caddytls.ConnectionPolicy)}
|
||||
}
|
||||
|
||||
// nothing left to do if auto redirects are disabled
|
||||
if srv.AutoHTTPS.DisableRedir {
|
||||
continue
|
||||
}
|
||||
|
||||
app.logger.Info("enabling automatic HTTP->HTTPS redirects",
|
||||
zap.String("server_name", srvName),
|
||||
)
|
||||
|
||||
// create HTTP->HTTPS redirects
|
||||
for _, addr := range srv.Listen {
|
||||
// figure out the address we will redirect to...
|
||||
addr, err := caddy.ParseNetworkAddress(addr)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s: invalid listener address: %v", srvName, addr)
|
||||
}
|
||||
|
||||
// ...and associate it with each domain in this server
|
||||
for d := range serverDomainSet {
|
||||
// if this domain is used on more than one HTTPS-enabled
|
||||
// port, we'll have to choose one, so prefer the HTTPS port
|
||||
if _, ok := redirDomains[d]; !ok ||
|
||||
addr.StartPort == uint(app.httpsPort()) {
|
||||
redirDomains[d] = addr
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// we now have a list of all the unique names for which we need certs;
|
||||
// turn the set into a slice so that phase 2 can use it
|
||||
app.allCertDomains = make([]string, 0, len(uniqueDomainsForCerts))
|
||||
var internal, external []string
|
||||
uniqueDomainsLoop:
|
||||
for d := range uniqueDomainsForCerts {
|
||||
// whether or not there is already an automation policy for this
|
||||
// name, we should add it to the list to manage a cert for it
|
||||
app.allCertDomains = append(app.allCertDomains, d)
|
||||
|
||||
// some names we've found might already have automation policies
|
||||
// explicitly specified for them; we should exclude those from
|
||||
// our hidden/implicit policy, since applying a name to more than
|
||||
// one automation policy would be confusing and an error
|
||||
if app.tlsApp.Automation != nil {
|
||||
for _, ap := range app.tlsApp.Automation.Policies {
|
||||
for _, apHost := range ap.Subjects {
|
||||
if apHost == d {
|
||||
continue uniqueDomainsLoop
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// if no automation policy exists for the name yet, we
|
||||
// will associate it with an implicit one
|
||||
if certmagic.SubjectQualifiesForPublicCert(d) {
|
||||
external = append(external, d)
|
||||
} else {
|
||||
internal = append(internal, d)
|
||||
}
|
||||
}
|
||||
|
||||
// ensure there is an automation policy to handle these certs
|
||||
err := app.createAutomationPolicies(ctx, external, internal)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// we're done if there are no HTTP->HTTPS redirects to add
|
||||
if len(redirDomains) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
// we need to reduce the mapping, i.e. group domains by address
|
||||
// since new routes are appended to servers by their address
|
||||
domainsByAddr := make(map[string][]string)
|
||||
for domain, addr := range redirDomains {
|
||||
addrStr := addr.String()
|
||||
domainsByAddr[addrStr] = append(domainsByAddr[addrStr], domain)
|
||||
}
|
||||
|
||||
// these keep track of the redirect server address(es)
|
||||
// and the routes for those servers which actually
|
||||
// respond with the redirects
|
||||
redirServerAddrs := make(map[string]struct{})
|
||||
var redirRoutes RouteList
|
||||
|
||||
redirServers := make(map[string][]Route)
|
||||
|
||||
for addrStr, domains := range domainsByAddr {
|
||||
// build the matcher set for this redirect route
|
||||
// (note that we happen to bypass Provision and
|
||||
// Validate steps for these matcher modules)
|
||||
matcherSet := MatcherSet{
|
||||
MatchProtocol("http"),
|
||||
MatchHost(domains),
|
||||
}
|
||||
|
||||
// build the address to which to redirect
|
||||
addr, err := caddy.ParseNetworkAddress(addrStr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
redirTo := "https://{http.request.host}"
|
||||
if addr.StartPort != uint(app.httpsPort()) {
|
||||
redirTo += ":" + strconv.Itoa(int(addr.StartPort))
|
||||
}
|
||||
redirTo += "{http.request.uri}"
|
||||
|
||||
// build the route
|
||||
redirRoute := Route{
|
||||
MatcherSets: []MatcherSet{matcherSet},
|
||||
Handlers: []MiddlewareHandler{
|
||||
StaticResponse{
|
||||
StatusCode: WeakString(strconv.Itoa(http.StatusPermanentRedirect)),
|
||||
Headers: http.Header{
|
||||
"Location": []string{redirTo},
|
||||
"Connection": []string{"close"},
|
||||
},
|
||||
Close: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
// use the network/host information from the address,
|
||||
// but change the port to the HTTP port then rebuild
|
||||
redirAddr := addr
|
||||
redirAddr.StartPort = uint(app.httpPort())
|
||||
redirAddr.EndPort = redirAddr.StartPort
|
||||
redirAddrStr := redirAddr.String()
|
||||
|
||||
redirServers[redirAddrStr] = append(redirServers[redirAddrStr], redirRoute)
|
||||
}
|
||||
|
||||
// on-demand TLS means that hostnames may be used which are not
|
||||
// explicitly defined in the config, and we still need to redirect
|
||||
// those; so we can append a single catch-all route (notice there
|
||||
// is no Host matcher) after the other redirect routes which will
|
||||
// allow us to handle unexpected/new hostnames... however, it's
|
||||
// not entirely clear what the redirect destination should be,
|
||||
// so I'm going to just hard-code the app's HTTPS port and call
|
||||
// it good for now...
|
||||
appendCatchAll := func(routes []Route) []Route {
|
||||
redirTo := "https://{http.request.host}"
|
||||
if app.httpsPort() != DefaultHTTPSPort {
|
||||
redirTo += ":" + strconv.Itoa(app.httpsPort())
|
||||
}
|
||||
redirTo += "{http.request.uri}"
|
||||
routes = append(routes, Route{
|
||||
MatcherSets: []MatcherSet{MatcherSet{MatchProtocol("http")}},
|
||||
Handlers: []MiddlewareHandler{
|
||||
StaticResponse{
|
||||
StatusCode: WeakString(strconv.Itoa(http.StatusPermanentRedirect)),
|
||||
Headers: http.Header{
|
||||
"Location": []string{redirTo},
|
||||
"Connection": []string{"close"},
|
||||
},
|
||||
Close: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
return routes
|
||||
}
|
||||
|
||||
redirServersLoop:
|
||||
for redirServerAddr, routes := range redirServers {
|
||||
// for each redirect listener, see if there's already a
|
||||
// server configured to listen on that exact address; if so,
|
||||
// simply add the redirect route to the end of its route
|
||||
// list; otherwise, we'll create a new server for all the
|
||||
// listener addresses that are unused and serve the
|
||||
// remaining redirects from it
|
||||
for srvName, srv := range app.Servers {
|
||||
if srv.hasListenerAddress(redirServerAddr) {
|
||||
// user has configured a server for the same address
|
||||
// that the redirect runs from; simply append our
|
||||
// redirect route to the existing routes, with a
|
||||
// caveat that their config might override ours
|
||||
app.logger.Warn("user server is listening on same interface as automatic HTTP->HTTPS redirects; user-configured routes might override these redirects",
|
||||
zap.String("server_name", srvName),
|
||||
zap.String("interface", redirServerAddr),
|
||||
)
|
||||
srv.Routes = append(srv.Routes, appendCatchAll(routes)...)
|
||||
continue redirServersLoop
|
||||
}
|
||||
}
|
||||
|
||||
// no server with this listener address exists;
|
||||
// save this address and route for custom server
|
||||
redirServerAddrs[redirServerAddr] = struct{}{}
|
||||
redirRoutes = append(redirRoutes, routes...)
|
||||
}
|
||||
|
||||
// if there are routes remaining which do not belong
|
||||
// in any existing server, make our own to serve the
|
||||
// rest of the redirects
|
||||
if len(redirServerAddrs) > 0 {
|
||||
redirServerAddrsList := make([]string, 0, len(redirServerAddrs))
|
||||
for a := range redirServerAddrs {
|
||||
redirServerAddrsList = append(redirServerAddrsList, a)
|
||||
}
|
||||
app.Servers["remaining_auto_https_redirects"] = &Server{
|
||||
Listen: redirServerAddrsList,
|
||||
Routes: appendCatchAll(redirRoutes),
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// createAutomationPolicy ensures that automated certificates for this
|
||||
// app are managed properly. This adds up to two automation policies:
|
||||
// one for the public names, and one for the internal names. If a catch-all
|
||||
// automation policy exists, it will be shallow-copied and used as the
|
||||
// base for the new ones (this is important for preserving behavior the
|
||||
// user intends to be "defaults").
|
||||
func (app *App) createAutomationPolicies(ctx caddy.Context, publicNames, internalNames []string) error {
|
||||
// nothing to do if no names to manage certs for
|
||||
if len(publicNames) == 0 && len(internalNames) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
// start by finding a base policy that the user may have defined
|
||||
// which should, in theory, apply to any policies derived from it;
|
||||
// typically this would be a "catch-all" policy with no host filter
|
||||
var basePolicy *caddytls.AutomationPolicy
|
||||
if app.tlsApp.Automation != nil {
|
||||
for _, ap := range app.tlsApp.Automation.Policies {
|
||||
// if an existing policy matches (specifically, a catch-all policy),
|
||||
// we should inherit from it, because that is what the user expects;
|
||||
// this is very common for user setting a default issuer, with a
|
||||
// custom CA endpoint, for example - whichever one we choose must
|
||||
// have a host list that is a superset of the policy we make...
|
||||
// the policy with no host filter is guaranteed to qualify
|
||||
if len(ap.Subjects) == 0 {
|
||||
basePolicy = ap
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if basePolicy == nil {
|
||||
basePolicy = new(caddytls.AutomationPolicy)
|
||||
}
|
||||
|
||||
// addPolicy adds an automation policy that uses issuer for hosts.
|
||||
addPolicy := func(issuer certmagic.Issuer, hosts []string) error {
|
||||
// shallow-copy the matching policy; we want to inherit
|
||||
// from it, not replace it... this takes two lines to
|
||||
// overrule compiler optimizations
|
||||
policyCopy := *basePolicy
|
||||
newPolicy := &policyCopy
|
||||
|
||||
// very important to provision it, since we are
|
||||
// bypassing the JSON-unmarshaling step
|
||||
if prov, ok := issuer.(caddy.Provisioner); ok {
|
||||
err := prov.Provision(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
newPolicy.Issuer = issuer
|
||||
newPolicy.Subjects = hosts
|
||||
|
||||
return app.tlsApp.AddAutomationPolicy(newPolicy)
|
||||
}
|
||||
|
||||
if len(publicNames) > 0 {
|
||||
var acmeIssuer *caddytls.ACMEIssuer
|
||||
// if it has an ACME issuer, maybe we can just use that
|
||||
// TODO: we might need a deep copy here, like a Clone() method on ACMEIssuer...
|
||||
acmeIssuer, _ = basePolicy.Issuer.(*caddytls.ACMEIssuer)
|
||||
if acmeIssuer == nil {
|
||||
acmeIssuer = new(caddytls.ACMEIssuer)
|
||||
}
|
||||
if app.HTTPPort > 0 || app.HTTPSPort > 0 {
|
||||
if acmeIssuer.Challenges == nil {
|
||||
acmeIssuer.Challenges = new(caddytls.ChallengesConfig)
|
||||
}
|
||||
}
|
||||
if app.HTTPPort > 0 {
|
||||
if acmeIssuer.Challenges.HTTP == nil {
|
||||
acmeIssuer.Challenges.HTTP = new(caddytls.HTTPChallengeConfig)
|
||||
}
|
||||
// don't overwrite existing explicit config
|
||||
if acmeIssuer.Challenges.HTTP.AlternatePort == 0 {
|
||||
acmeIssuer.Challenges.HTTP.AlternatePort = app.HTTPPort
|
||||
}
|
||||
}
|
||||
if app.HTTPSPort > 0 {
|
||||
if acmeIssuer.Challenges.TLSALPN == nil {
|
||||
acmeIssuer.Challenges.TLSALPN = new(caddytls.TLSALPNChallengeConfig)
|
||||
}
|
||||
// don't overwrite existing explicit config
|
||||
if acmeIssuer.Challenges.TLSALPN.AlternatePort == 0 {
|
||||
acmeIssuer.Challenges.TLSALPN.AlternatePort = app.HTTPSPort
|
||||
}
|
||||
}
|
||||
if err := addPolicy(acmeIssuer, publicNames); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if len(internalNames) > 0 {
|
||||
internalIssuer := new(caddytls.InternalIssuer)
|
||||
if err := addPolicy(internalIssuer, internalNames); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
err := app.tlsApp.Validate()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// automaticHTTPSPhase2 begins certificate management for
|
||||
// all names in the qualifying domain set for each server.
|
||||
// This phase must occur after provisioning and at the end
|
||||
// of app start, after all the servers have been started.
|
||||
// Doing this last ensures that there won't be any race
|
||||
// for listeners on the HTTP or HTTPS ports when management
|
||||
// is async (if CertMagic's solvers bind to those ports
|
||||
// first, then our servers would fail to bind to them,
|
||||
// which would be bad, since CertMagic's bindings are
|
||||
// temporary and don't serve the user's sites!).
|
||||
func (app *App) automaticHTTPSPhase2() error {
|
||||
if len(app.allCertDomains) == 0 {
|
||||
return nil
|
||||
}
|
||||
app.logger.Info("enabling automatic TLS certificate management",
|
||||
zap.Strings("domains", app.allCertDomains),
|
||||
)
|
||||
err := app.tlsApp.Manage(app.allCertDomains)
|
||||
if err != nil {
|
||||
return fmt.Errorf("managing certificates for %v: %s", app.allCertDomains, err)
|
||||
}
|
||||
app.allCertDomains = nil // no longer needed; allow GC to deallocate
|
||||
return nil
|
||||
}
|
||||
@@ -1,173 +0,0 @@
|
||||
// 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 caddyauth
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/caddyserver/caddy/v2"
|
||||
)
|
||||
|
||||
func init() {
|
||||
caddy.RegisterModule(HTTPBasicAuth{})
|
||||
}
|
||||
|
||||
// HTTPBasicAuth facilitates HTTP basic authentication.
|
||||
type HTTPBasicAuth struct {
|
||||
// The algorithm with which the passwords are hashed. Default: bcrypt
|
||||
HashRaw json.RawMessage `json:"hash,omitempty" caddy:"namespace=http.authentication.hashes inline_key=algorithm"`
|
||||
|
||||
// The list of accounts to authenticate.
|
||||
AccountList []Account `json:"accounts,omitempty"`
|
||||
|
||||
// The name of the realm. Default: restricted
|
||||
Realm string `json:"realm,omitempty"`
|
||||
|
||||
Accounts map[string]Account `json:"-"`
|
||||
Hash Comparer `json:"-"`
|
||||
}
|
||||
|
||||
// CaddyModule returns the Caddy module information.
|
||||
func (HTTPBasicAuth) CaddyModule() caddy.ModuleInfo {
|
||||
return caddy.ModuleInfo{
|
||||
ID: "http.authentication.providers.http_basic",
|
||||
New: func() caddy.Module { return new(HTTPBasicAuth) },
|
||||
}
|
||||
}
|
||||
|
||||
// Provision provisions the HTTP basic auth provider.
|
||||
func (hba *HTTPBasicAuth) Provision(ctx caddy.Context) error {
|
||||
if hba.HashRaw == nil {
|
||||
hba.HashRaw = json.RawMessage(`{"algorithm": "bcrypt"}`)
|
||||
}
|
||||
|
||||
// load password hasher
|
||||
hasherIface, err := ctx.LoadModule(hba, "HashRaw")
|
||||
if err != nil {
|
||||
return fmt.Errorf("loading password hasher module: %v", err)
|
||||
}
|
||||
hba.Hash = hasherIface.(Comparer)
|
||||
|
||||
if hba.Hash == nil {
|
||||
return fmt.Errorf("hash is required")
|
||||
}
|
||||
|
||||
repl := caddy.NewReplacer()
|
||||
|
||||
// load account list
|
||||
hba.Accounts = make(map[string]Account)
|
||||
for i, acct := range hba.AccountList {
|
||||
if _, ok := hba.Accounts[acct.Username]; ok {
|
||||
return fmt.Errorf("account %d: username is not unique: %s", i, acct.Username)
|
||||
}
|
||||
|
||||
acct.Username = repl.ReplaceAll(acct.Username, "")
|
||||
acct.Password = repl.ReplaceAll(string(acct.Password), "")
|
||||
acct.Salt = repl.ReplaceAll(string(acct.Salt), "")
|
||||
|
||||
if acct.Username == "" || acct.Password == "" {
|
||||
return fmt.Errorf("account %d: username and password are required", i)
|
||||
}
|
||||
|
||||
acct.password, err = base64.StdEncoding.DecodeString(acct.Password)
|
||||
if err != nil {
|
||||
return fmt.Errorf("base64-decoding password: %v", err)
|
||||
}
|
||||
if acct.Salt != "" {
|
||||
acct.salt, err = base64.StdEncoding.DecodeString(acct.Salt)
|
||||
if err != nil {
|
||||
return fmt.Errorf("base64-decoding salt: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
hba.Accounts[acct.Username] = acct
|
||||
}
|
||||
hba.AccountList = nil // allow GC to deallocate
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Authenticate validates the user credentials in req and returns the user, if valid.
|
||||
func (hba HTTPBasicAuth) Authenticate(w http.ResponseWriter, req *http.Request) (User, bool, error) {
|
||||
username, plaintextPasswordStr, ok := req.BasicAuth()
|
||||
|
||||
// if basic auth is missing or invalid, prompt for credentials
|
||||
if !ok {
|
||||
// browsers show a message that says something like:
|
||||
// "The website says: <realm>"
|
||||
// which is kinda dumb, but whatever.
|
||||
realm := hba.Realm
|
||||
if realm == "" {
|
||||
realm = "restricted"
|
||||
}
|
||||
|
||||
w.Header().Set("WWW-Authenticate", fmt.Sprintf(`Basic realm="%s"`, realm))
|
||||
|
||||
return User{}, false, nil
|
||||
}
|
||||
|
||||
plaintextPassword := []byte(plaintextPasswordStr)
|
||||
|
||||
account, accountExists := hba.Accounts[username]
|
||||
// don't return early if account does not exist; we want
|
||||
// to try to avoid side-channels that leak existence
|
||||
|
||||
same, err := hba.Hash.Compare(account.password, plaintextPassword, account.salt)
|
||||
if err != nil {
|
||||
return User{}, false, err
|
||||
}
|
||||
if !same || !accountExists {
|
||||
return User{}, false, nil
|
||||
}
|
||||
|
||||
return User{ID: username}, true, nil
|
||||
}
|
||||
|
||||
// Comparer is a type that can securely compare
|
||||
// a plaintext password with a hashed password
|
||||
// in constant-time. Comparers should hash the
|
||||
// plaintext password and then use constant-time
|
||||
// comparison.
|
||||
type Comparer interface {
|
||||
// Compare returns true if the result of hashing
|
||||
// plaintextPassword with salt is hashedPassword,
|
||||
// false otherwise. An error is returned only if
|
||||
// there is a technical/configuration error.
|
||||
Compare(hashedPassword, plaintextPassword, salt []byte) (bool, error)
|
||||
}
|
||||
|
||||
// Account contains a username, password, and salt (if applicable).
|
||||
type Account struct {
|
||||
// A user's username.
|
||||
Username string `json:"username"`
|
||||
|
||||
// The user's hashed password, base64-encoded.
|
||||
Password string `json:"password"`
|
||||
|
||||
// The user's password salt, base64-encoded; for
|
||||
// algorithms where external salt is needed.
|
||||
Salt string `json:"salt,omitempty"`
|
||||
|
||||
password, salt []byte
|
||||
}
|
||||
|
||||
// Interface guards
|
||||
var (
|
||||
_ caddy.Provisioner = (*HTTPBasicAuth)(nil)
|
||||
_ Authenticator = (*HTTPBasicAuth)(nil)
|
||||
)
|
||||
@@ -1,102 +0,0 @@
|
||||
// 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 caddyauth
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/caddyserver/caddy/v2"
|
||||
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
|
||||
)
|
||||
|
||||
func init() {
|
||||
caddy.RegisterModule(Authentication{})
|
||||
}
|
||||
|
||||
// Authentication is a middleware which provides user authentication.
|
||||
// Rejects requests with HTTP 401 if the request is not authenticated.
|
||||
type Authentication struct {
|
||||
// A set of authentication providers. If none are specified,
|
||||
// all requests will always be unauthenticated.
|
||||
ProvidersRaw caddy.ModuleMap `json:"providers,omitempty" caddy:"namespace=http.authentication.providers"`
|
||||
|
||||
Providers map[string]Authenticator `json:"-"`
|
||||
}
|
||||
|
||||
// CaddyModule returns the Caddy module information.
|
||||
func (Authentication) CaddyModule() caddy.ModuleInfo {
|
||||
return caddy.ModuleInfo{
|
||||
ID: "http.handlers.authentication",
|
||||
New: func() caddy.Module { return new(Authentication) },
|
||||
}
|
||||
}
|
||||
|
||||
// Provision sets up a.
|
||||
func (a *Authentication) Provision(ctx caddy.Context) error {
|
||||
a.Providers = make(map[string]Authenticator)
|
||||
mods, err := ctx.LoadModule(a, "ProvidersRaw")
|
||||
if err != nil {
|
||||
return fmt.Errorf("loading authentication providers: %v", err)
|
||||
}
|
||||
for modName, modIface := range mods.(map[string]interface{}) {
|
||||
a.Providers[modName] = modIface.(Authenticator)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a Authentication) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
|
||||
var user User
|
||||
var authed bool
|
||||
var err error
|
||||
for provName, prov := range a.Providers {
|
||||
user, authed, err = prov.Authenticate(w, r)
|
||||
if err != nil {
|
||||
log.Printf("[ERROR] Authenticating with %s: %v", provName, err)
|
||||
continue
|
||||
}
|
||||
if authed {
|
||||
break
|
||||
}
|
||||
}
|
||||
if !authed {
|
||||
return caddyhttp.Error(http.StatusUnauthorized, fmt.Errorf("not authenticated"))
|
||||
}
|
||||
|
||||
repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
|
||||
repl.Set("http.authentication.user.id", user.ID)
|
||||
|
||||
return next.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
// Authenticator is a type which can authenticate a request.
|
||||
// If a request was not authenticated, it returns false. An
|
||||
// error is only returned if authenticating the request fails
|
||||
// for a technical reason (not for bad/missing credentials).
|
||||
type Authenticator interface {
|
||||
Authenticate(http.ResponseWriter, *http.Request) (User, bool, error)
|
||||
}
|
||||
|
||||
// User represents an authenticated user.
|
||||
type User struct {
|
||||
ID string
|
||||
}
|
||||
|
||||
// Interface guards
|
||||
var (
|
||||
_ caddy.Provisioner = (*Authentication)(nil)
|
||||
_ caddyhttp.MiddlewareHandler = (*Authentication)(nil)
|
||||
)
|
||||
@@ -1,90 +0,0 @@
|
||||
// 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 caddyauth
|
||||
|
||||
import (
|
||||
"github.com/caddyserver/caddy/v2"
|
||||
"github.com/caddyserver/caddy/v2/caddyconfig"
|
||||
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
|
||||
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
|
||||
)
|
||||
|
||||
func init() {
|
||||
httpcaddyfile.RegisterHandlerDirective("basicauth", parseCaddyfile)
|
||||
}
|
||||
|
||||
// parseCaddyfile sets up the handler from Caddyfile tokens. Syntax:
|
||||
//
|
||||
// basicauth [<matcher>] [<hash_algorithm>] {
|
||||
// <username> <hashed_password_base64> [<salt_base64>]
|
||||
// ...
|
||||
// }
|
||||
//
|
||||
// If no hash algorithm is supplied, bcrypt will be assumed.
|
||||
func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
|
||||
var ba HTTPBasicAuth
|
||||
|
||||
for h.Next() {
|
||||
var cmp Comparer
|
||||
args := h.RemainingArgs()
|
||||
|
||||
var hashName string
|
||||
switch len(args) {
|
||||
case 0:
|
||||
hashName = "bcrypt"
|
||||
case 1:
|
||||
hashName = args[0]
|
||||
default:
|
||||
return nil, h.ArgErr()
|
||||
}
|
||||
|
||||
switch hashName {
|
||||
case "bcrypt":
|
||||
cmp = BcryptHash{}
|
||||
case "scrypt":
|
||||
cmp = ScryptHash{}
|
||||
default:
|
||||
return nil, h.Errf("unrecognized hash algorithm: %s", hashName)
|
||||
}
|
||||
|
||||
ba.HashRaw = caddyconfig.JSONModuleObject(cmp, "algorithm", hashName, nil)
|
||||
|
||||
for h.NextBlock(0) {
|
||||
username := h.Val()
|
||||
|
||||
var b64Pwd, b64Salt string
|
||||
h.Args(&b64Pwd, &b64Salt)
|
||||
if h.NextArg() {
|
||||
return nil, h.ArgErr()
|
||||
}
|
||||
|
||||
if username == "" || b64Pwd == "" {
|
||||
return nil, h.Err("username and password cannot be empty or missing")
|
||||
}
|
||||
|
||||
ba.AccountList = append(ba.AccountList, Account{
|
||||
Username: username,
|
||||
Password: b64Pwd,
|
||||
Salt: b64Salt,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return Authentication{
|
||||
ProvidersRaw: caddy.ModuleMap{
|
||||
"http_basic": caddyconfig.JSON(ba, nil),
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
@@ -1,84 +0,0 @@
|
||||
// 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 caddyauth
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"flag"
|
||||
"fmt"
|
||||
|
||||
"github.com/caddyserver/caddy/v2"
|
||||
caddycmd "github.com/caddyserver/caddy/v2/cmd"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"golang.org/x/crypto/scrypt"
|
||||
)
|
||||
|
||||
func init() {
|
||||
caddycmd.RegisterCommand(caddycmd.Command{
|
||||
Name: "hash-password",
|
||||
Func: cmdHashPassword,
|
||||
Usage: "--plaintext <password> [--salt <string>] [--algorithm <name>]",
|
||||
Short: "Hashes a password and writes base64",
|
||||
Long: `
|
||||
Convenient way to hash a plaintext password. The resulting
|
||||
hash is written to stdout as a base64 string.
|
||||
|
||||
--algorithm may be bcrypt or scrypt. If script, the default
|
||||
parameters are used.
|
||||
|
||||
Use the --salt flag for algorithms which require a salt to
|
||||
be provided (scrypt).
|
||||
`,
|
||||
Flags: func() *flag.FlagSet {
|
||||
fs := flag.NewFlagSet("hash-password", flag.ExitOnError)
|
||||
fs.String("algorithm", "bcrypt", "Name of the hash algorithm")
|
||||
fs.String("plaintext", "", "The plaintext password")
|
||||
fs.String("salt", "", "The password salt")
|
||||
return fs
|
||||
}(),
|
||||
})
|
||||
}
|
||||
|
||||
func cmdHashPassword(fs caddycmd.Flags) (int, error) {
|
||||
algorithm := fs.String("algorithm")
|
||||
plaintext := []byte(fs.String("plaintext"))
|
||||
salt := []byte(fs.String("salt"))
|
||||
|
||||
if len(plaintext) == 0 {
|
||||
return caddy.ExitCodeFailedStartup, fmt.Errorf("password is required")
|
||||
}
|
||||
|
||||
var hash []byte
|
||||
var err error
|
||||
switch algorithm {
|
||||
case "bcrypt":
|
||||
hash, err = bcrypt.GenerateFromPassword(plaintext, bcrypt.DefaultCost)
|
||||
case "scrypt":
|
||||
def := ScryptHash{}
|
||||
def.SetDefaults()
|
||||
hash, err = scrypt.Key(plaintext, salt, def.N, def.R, def.P, def.KeyLength)
|
||||
default:
|
||||
return caddy.ExitCodeFailedStartup, fmt.Errorf("unrecognized hash algorithm: %s", algorithm)
|
||||
}
|
||||
if err != nil {
|
||||
return caddy.ExitCodeFailedStartup, err
|
||||
}
|
||||
|
||||
hashBase64 := base64.StdEncoding.EncodeToString([]byte(hash))
|
||||
|
||||
fmt.Println(hashBase64)
|
||||
|
||||
return 0, nil
|
||||
}
|
||||
@@ -1,125 +0,0 @@
|
||||
// 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 caddyauth
|
||||
|
||||
import (
|
||||
"crypto/subtle"
|
||||
|
||||
"github.com/caddyserver/caddy/v2"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"golang.org/x/crypto/scrypt"
|
||||
)
|
||||
|
||||
func init() {
|
||||
caddy.RegisterModule(BcryptHash{})
|
||||
caddy.RegisterModule(ScryptHash{})
|
||||
}
|
||||
|
||||
// BcryptHash implements the bcrypt hash.
|
||||
type BcryptHash struct{}
|
||||
|
||||
// CaddyModule returns the Caddy module information.
|
||||
func (BcryptHash) CaddyModule() caddy.ModuleInfo {
|
||||
return caddy.ModuleInfo{
|
||||
ID: "http.authentication.hashes.bcrypt",
|
||||
New: func() caddy.Module { return new(BcryptHash) },
|
||||
}
|
||||
}
|
||||
|
||||
// Compare compares passwords.
|
||||
func (BcryptHash) Compare(hashed, plaintext, _ []byte) (bool, error) {
|
||||
err := bcrypt.CompareHashAndPassword(hashed, plaintext)
|
||||
if err == bcrypt.ErrMismatchedHashAndPassword {
|
||||
return false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// ScryptHash implements the scrypt KDF as a hash.
|
||||
type ScryptHash struct {
|
||||
// scrypt's N parameter. If unset or 0, a safe default is used.
|
||||
N int `json:"N,omitempty"`
|
||||
|
||||
// scrypt's r parameter. If unset or 0, a safe default is used.
|
||||
R int `json:"r,omitempty"`
|
||||
|
||||
// scrypt's p parameter. If unset or 0, a safe default is used.
|
||||
P int `json:"p,omitempty"`
|
||||
|
||||
// scrypt's key length parameter (in bytes). If unset or 0, a
|
||||
// safe default is used.
|
||||
KeyLength int `json:"key_length,omitempty"`
|
||||
}
|
||||
|
||||
// CaddyModule returns the Caddy module information.
|
||||
func (ScryptHash) CaddyModule() caddy.ModuleInfo {
|
||||
return caddy.ModuleInfo{
|
||||
ID: "http.authentication.hashes.scrypt",
|
||||
New: func() caddy.Module { return new(ScryptHash) },
|
||||
}
|
||||
}
|
||||
|
||||
// Provision sets up s.
|
||||
func (s *ScryptHash) Provision(_ caddy.Context) error {
|
||||
s.SetDefaults()
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetDefaults sets safe default parameters, but does
|
||||
// not overwrite existing values. Each default parameter
|
||||
// is set independently; it does not check to ensure
|
||||
// that r*p < 2^30. The defaults chosen are those as
|
||||
// recommended in 2019 by
|
||||
// https://godoc.org/golang.org/x/crypto/scrypt.
|
||||
func (s *ScryptHash) SetDefaults() {
|
||||
if s.N == 0 {
|
||||
s.N = 32768
|
||||
}
|
||||
if s.R == 0 {
|
||||
s.R = 8
|
||||
}
|
||||
if s.P == 0 {
|
||||
s.P = 1
|
||||
}
|
||||
if s.KeyLength == 0 {
|
||||
s.KeyLength = 32
|
||||
}
|
||||
}
|
||||
|
||||
// Compare compares passwords.
|
||||
func (s ScryptHash) Compare(hashed, plaintext, salt []byte) (bool, error) {
|
||||
ourHash, err := scrypt.Key(plaintext, salt, s.N, s.R, s.P, s.KeyLength)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if hashesMatch(hashed, ourHash) {
|
||||
return true, nil
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func hashesMatch(pwdHash1, pwdHash2 []byte) bool {
|
||||
return subtle.ConstantTimeCompare(pwdHash1, pwdHash2) == 1
|
||||
}
|
||||
|
||||
// Interface guards
|
||||
var (
|
||||
_ Comparer = (*BcryptHash)(nil)
|
||||
_ Comparer = (*ScryptHash)(nil)
|
||||
_ caddy.Provisioner = (*ScryptHash)(nil)
|
||||
)
|
||||
@@ -1,222 +0,0 @@
|
||||
// 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 caddyhttp
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io"
|
||||
weakrand "math/rand"
|
||||
"net"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/caddyserver/caddy/v2"
|
||||
)
|
||||
|
||||
func init() {
|
||||
weakrand.Seed(time.Now().UnixNano())
|
||||
|
||||
err := caddy.RegisterModule(tlsPlaceholderWrapper{})
|
||||
if err != nil {
|
||||
caddy.Log().Fatal(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// RequestMatcher is a type that can match to a request.
|
||||
// A route matcher MUST NOT modify the request, with the
|
||||
// only exception being its context.
|
||||
type RequestMatcher interface {
|
||||
Match(*http.Request) bool
|
||||
}
|
||||
|
||||
// Handler is like http.Handler except ServeHTTP may return an error.
|
||||
//
|
||||
// If any handler encounters an error, it should be returned for proper
|
||||
// handling. Return values should be propagated down the middleware chain
|
||||
// by returning it unchanged. Returned errors should not be re-wrapped
|
||||
// if they are already HandlerError values.
|
||||
type Handler interface {
|
||||
ServeHTTP(http.ResponseWriter, *http.Request) error
|
||||
}
|
||||
|
||||
// HandlerFunc is a convenience type like http.HandlerFunc.
|
||||
type HandlerFunc func(http.ResponseWriter, *http.Request) error
|
||||
|
||||
// ServeHTTP implements the Handler interface.
|
||||
func (f HandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request) error {
|
||||
return f(w, r)
|
||||
}
|
||||
|
||||
// Middleware chains one Handler to the next by being passed
|
||||
// the next Handler in the chain.
|
||||
type Middleware func(Handler) Handler
|
||||
|
||||
// MiddlewareHandler is like Handler except it takes as a third
|
||||
// argument the next handler in the chain. The next handler will
|
||||
// never be nil, but may be a no-op handler if this is the last
|
||||
// handler in the chain. Handlers which act as middleware should
|
||||
// call the next handler's ServeHTTP method so as to propagate
|
||||
// the request down the chain properly. Handlers which act as
|
||||
// responders (content origins) need not invoke the next handler,
|
||||
// since the last handler in the chain should be the first to
|
||||
// write the response.
|
||||
type MiddlewareHandler interface {
|
||||
ServeHTTP(http.ResponseWriter, *http.Request, Handler) error
|
||||
}
|
||||
|
||||
// emptyHandler is used as a no-op handler.
|
||||
var emptyHandler Handler = HandlerFunc(func(http.ResponseWriter, *http.Request) error { return nil })
|
||||
|
||||
// An implicit suffix middleware that, if reached, sets the StatusCode to the
|
||||
// error stored in the ErrorCtxKey. This is to prevent situations where the
|
||||
// Error chain does not actually handle the error (for instance, it matches only
|
||||
// on some errors). See #3053
|
||||
var errorEmptyHandler Handler = HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
|
||||
httpError := r.Context().Value(ErrorCtxKey)
|
||||
if handlerError, ok := httpError.(HandlerError); ok {
|
||||
w.WriteHeader(handlerError.StatusCode)
|
||||
} else {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
// WeakString is a type that unmarshals any JSON value
|
||||
// as a string literal, with the following exceptions:
|
||||
//
|
||||
// 1. actual string values are decoded as strings; and
|
||||
// 2. null is decoded as empty string;
|
||||
//
|
||||
// and provides methods for getting the value as various
|
||||
// primitive types. However, using this type removes any
|
||||
// type safety as far as deserializing JSON is concerned.
|
||||
type WeakString string
|
||||
|
||||
// UnmarshalJSON satisfies json.Unmarshaler according to
|
||||
// this type's documentation.
|
||||
func (ws *WeakString) UnmarshalJSON(b []byte) error {
|
||||
if len(b) == 0 {
|
||||
return io.EOF
|
||||
}
|
||||
if b[0] == byte('"') && b[len(b)-1] == byte('"') {
|
||||
var s string
|
||||
err := json.Unmarshal(b, &s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*ws = WeakString(s)
|
||||
return nil
|
||||
}
|
||||
if bytes.Equal(b, []byte("null")) {
|
||||
return nil
|
||||
}
|
||||
*ws = WeakString(b)
|
||||
return nil
|
||||
}
|
||||
|
||||
// MarshalJSON marshals was a boolean if true or false,
|
||||
// a number if an integer, or a string otherwise.
|
||||
func (ws WeakString) MarshalJSON() ([]byte, error) {
|
||||
if ws == "true" {
|
||||
return []byte("true"), nil
|
||||
}
|
||||
if ws == "false" {
|
||||
return []byte("false"), nil
|
||||
}
|
||||
if num, err := strconv.Atoi(string(ws)); err == nil {
|
||||
return json.Marshal(num)
|
||||
}
|
||||
return json.Marshal(string(ws))
|
||||
}
|
||||
|
||||
// Int returns ws as an integer. If ws is not an
|
||||
// integer, 0 is returned.
|
||||
func (ws WeakString) Int() int {
|
||||
num, _ := strconv.Atoi(string(ws))
|
||||
return num
|
||||
}
|
||||
|
||||
// Float64 returns ws as a float64. If ws is not a
|
||||
// float value, the zero value is returned.
|
||||
func (ws WeakString) Float64() float64 {
|
||||
num, _ := strconv.ParseFloat(string(ws), 64)
|
||||
return num
|
||||
}
|
||||
|
||||
// Bool returns ws as a boolean. If ws is not a
|
||||
// boolean, false is returned.
|
||||
func (ws WeakString) Bool() bool {
|
||||
return string(ws) == "true"
|
||||
}
|
||||
|
||||
// String returns ws as a string.
|
||||
func (ws WeakString) String() string {
|
||||
return string(ws)
|
||||
}
|
||||
|
||||
// CopyHeader copies HTTP headers by completely
|
||||
// replacing dest with src. (This allows deletions
|
||||
// to be propagated, assuming src started as a
|
||||
// consistent copy of dest.)
|
||||
func CopyHeader(dest, src http.Header) {
|
||||
for field := range dest {
|
||||
delete(dest, field)
|
||||
}
|
||||
for field, val := range src {
|
||||
dest[field] = val
|
||||
}
|
||||
}
|
||||
|
||||
// StatusCodeMatches returns true if a real HTTP status code matches
|
||||
// the configured status code, which may be either a real HTTP status
|
||||
// code or an integer representing a class of codes (e.g. 4 for all
|
||||
// 4xx statuses).
|
||||
func StatusCodeMatches(actual, configured int) bool {
|
||||
if actual == configured {
|
||||
return true
|
||||
}
|
||||
if configured < 100 &&
|
||||
actual >= configured*100 &&
|
||||
actual < (configured+1)*100 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// tlsPlaceholderWrapper is a no-op listener wrapper that marks
|
||||
// where the TLS listener should be in a chain of listener wrappers.
|
||||
type tlsPlaceholderWrapper struct{}
|
||||
|
||||
func (tlsPlaceholderWrapper) CaddyModule() caddy.ModuleInfo {
|
||||
return caddy.ModuleInfo{
|
||||
ID: "caddy.listeners.tls",
|
||||
New: func() caddy.Module { return new(tlsPlaceholderWrapper) },
|
||||
}
|
||||
}
|
||||
|
||||
func (tlsPlaceholderWrapper) WrapListener(ln net.Listener) net.Listener { return ln }
|
||||
|
||||
const (
|
||||
// DefaultHTTPPort is the default port for HTTP.
|
||||
DefaultHTTPPort = 80
|
||||
|
||||
// DefaultHTTPSPort is the default port for HTTPS.
|
||||
DefaultHTTPSPort = 443
|
||||
)
|
||||
|
||||
// Interface guard
|
||||
var _ caddy.ListenerWrapper = (*tlsPlaceholderWrapper)(nil)
|
||||
@@ -1,221 +0,0 @@
|
||||
// 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 caddyhttp
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/caddyserver/caddy/v2"
|
||||
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
|
||||
"github.com/google/cel-go/cel"
|
||||
"github.com/google/cel-go/checker/decls"
|
||||
"github.com/google/cel-go/common/types"
|
||||
"github.com/google/cel-go/common/types/ref"
|
||||
"github.com/google/cel-go/common/types/traits"
|
||||
"github.com/google/cel-go/interpreter/functions"
|
||||
exprpb "google.golang.org/genproto/googleapis/api/expr/v1alpha1"
|
||||
)
|
||||
|
||||
func init() {
|
||||
caddy.RegisterModule(MatchExpression{})
|
||||
}
|
||||
|
||||
// MatchExpression matches requests by evaluating a
|
||||
// [CEL](https://github.com/google/cel-spec) expression.
|
||||
// This enables complex logic to be expressed using a comfortable,
|
||||
// familiar syntax.
|
||||
//
|
||||
// COMPATIBILITY NOTE: This module is still experimental and is not
|
||||
// subject to Caddy's compatibility guarantee.
|
||||
type MatchExpression struct {
|
||||
// The CEL expression to evaluate. Any Caddy placeholders
|
||||
// will be expanded and situated into proper CEL function
|
||||
// calls before evaluating.
|
||||
Expr string
|
||||
|
||||
expandedExpr string
|
||||
prg cel.Program
|
||||
}
|
||||
|
||||
// CaddyModule returns the Caddy module information.
|
||||
func (MatchExpression) CaddyModule() caddy.ModuleInfo {
|
||||
return caddy.ModuleInfo{
|
||||
ID: "http.matchers.expression",
|
||||
New: func() caddy.Module { return new(MatchExpression) },
|
||||
}
|
||||
}
|
||||
|
||||
// MarshalJSON marshals m's expression.
|
||||
func (m MatchExpression) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(m.Expr)
|
||||
}
|
||||
|
||||
// UnmarshalJSON unmarshals m's expression.
|
||||
func (m *MatchExpression) UnmarshalJSON(data []byte) error {
|
||||
return json.Unmarshal(data, &m.Expr)
|
||||
}
|
||||
|
||||
// Provision sets ups m.
|
||||
func (m *MatchExpression) Provision(_ caddy.Context) error {
|
||||
// replace placeholders with a function call - this is just some
|
||||
// light (and possibly naïve) syntactic sugar
|
||||
m.expandedExpr = placeholderRegexp.ReplaceAllString(m.Expr, placeholderExpansion)
|
||||
|
||||
// create the CEL environment
|
||||
env, err := cel.NewEnv(
|
||||
cel.Declarations(
|
||||
decls.NewIdent("request", httpRequestObjectType, nil),
|
||||
decls.NewFunction(placeholderFuncName,
|
||||
decls.NewOverload(placeholderFuncName+"_httpRequest_string",
|
||||
[]*exprpb.Type{httpRequestObjectType, decls.String},
|
||||
decls.String)),
|
||||
),
|
||||
cel.CustomTypeAdapter(celHTTPRequestTypeAdapter{}),
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("setting up CEL environment: %v", err)
|
||||
}
|
||||
|
||||
// parse the expression
|
||||
parsed, issues := env.Parse(m.expandedExpr)
|
||||
if issues != nil && issues.Err() != nil {
|
||||
return fmt.Errorf("parsing CEL program: %s", issues.Err())
|
||||
}
|
||||
|
||||
// type-check it
|
||||
checked, issues := env.Check(parsed)
|
||||
if issues != nil && issues.Err() != nil {
|
||||
return fmt.Errorf("type-checking CEL program: %s", issues.Err())
|
||||
}
|
||||
|
||||
// compile the "program"
|
||||
m.prg, err = env.Program(checked,
|
||||
cel.Functions(
|
||||
&functions.Overload{
|
||||
Operator: placeholderFuncName,
|
||||
Binary: caddyPlaceholderFunc,
|
||||
},
|
||||
),
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("compiling CEL program: %s", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Match returns true if r matches m.
|
||||
func (m MatchExpression) Match(r *http.Request) bool {
|
||||
out, _, _ := m.prg.Eval(map[string]interface{}{
|
||||
"request": celHTTPRequest{r},
|
||||
})
|
||||
if outBool, ok := out.Value().(bool); ok {
|
||||
return outBool
|
||||
}
|
||||
return false
|
||||
|
||||
}
|
||||
|
||||
// UnmarshalCaddyfile implements caddyfile.Unmarshaler.
|
||||
func (m *MatchExpression) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
|
||||
for d.Next() {
|
||||
m.Expr = strings.Join(d.RemainingArgs(), " ")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// httpRequestCELType is the type representation of a native HTTP request.
|
||||
var httpRequestCELType = types.NewTypeValue("http.Request", traits.ReceiverType)
|
||||
|
||||
// cellHTTPRequest wraps an http.Request with
|
||||
// methods to satisfy the ref.Val interface.
|
||||
type celHTTPRequest struct {
|
||||
*http.Request
|
||||
}
|
||||
|
||||
func (cr celHTTPRequest) ConvertToNative(typeDesc reflect.Type) (interface{}, error) {
|
||||
return cr.Request, nil
|
||||
}
|
||||
func (celHTTPRequest) ConvertToType(typeVal ref.Type) ref.Val {
|
||||
panic("not implemented")
|
||||
}
|
||||
func (cr celHTTPRequest) Equal(other ref.Val) ref.Val {
|
||||
if o, ok := other.Value().(celHTTPRequest); ok {
|
||||
return types.Bool(o.Request == cr.Request)
|
||||
}
|
||||
return types.ValOrErr(other, "%v is not comparable type", other)
|
||||
}
|
||||
func (celHTTPRequest) Type() ref.Type { return httpRequestCELType }
|
||||
func (cr celHTTPRequest) Value() interface{} { return cr }
|
||||
|
||||
// celHTTPRequestTypeAdapter can adapt a
|
||||
// celHTTPRequest to a CEL value.
|
||||
type celHTTPRequestTypeAdapter struct{}
|
||||
|
||||
func (celHTTPRequestTypeAdapter) NativeToValue(value interface{}) ref.Val {
|
||||
if celReq, ok := value.(celHTTPRequest); ok {
|
||||
return celReq
|
||||
}
|
||||
return types.DefaultTypeAdapter.NativeToValue(value)
|
||||
}
|
||||
|
||||
// Variables used for replacing Caddy placeholders in CEL
|
||||
// expressions with a proper CEL function call; this is
|
||||
// just for syntactic sugar.
|
||||
var (
|
||||
placeholderRegexp = regexp.MustCompile(`{([\w.-]+)}`)
|
||||
placeholderExpansion = `caddyPlaceholder(request, "${1}")`
|
||||
)
|
||||
|
||||
var httpRequestObjectType = decls.NewObjectType("http.Request")
|
||||
|
||||
// The name of the CEL function which accesses Replacer values.
|
||||
const placeholderFuncName = "caddyPlaceholder"
|
||||
|
||||
// caddyPlaceholderFunc implements the custom CEL function that
|
||||
// accesses the Replacer on a request and gets values from it.
|
||||
func caddyPlaceholderFunc(lhs, rhs ref.Val) ref.Val {
|
||||
celReq, ok := lhs.(celHTTPRequest)
|
||||
if !ok {
|
||||
return types.NewErr(
|
||||
"invalid request of type '%v' to "+placeholderFuncName+"(request, placeholderVarName)",
|
||||
lhs.Type())
|
||||
}
|
||||
phStr, ok := rhs.(types.String)
|
||||
if !ok {
|
||||
return types.NewErr(
|
||||
"invalid placeholder variable name of type '%v' to "+placeholderFuncName+"(request, placeholderVarName)",
|
||||
rhs.Type())
|
||||
}
|
||||
|
||||
repl := celReq.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
|
||||
val, _ := repl.Get(string(phStr))
|
||||
|
||||
return types.String(val)
|
||||
}
|
||||
|
||||
// Interface guards
|
||||
var (
|
||||
_ caddy.Provisioner = (*MatchExpression)(nil)
|
||||
_ RequestMatcher = (*MatchExpression)(nil)
|
||||
_ caddyfile.Unmarshaler = (*MatchExpression)(nil)
|
||||
_ json.Marshaler = (*MatchExpression)(nil)
|
||||
_ json.Unmarshaler = (*MatchExpression)(nil)
|
||||
)
|
||||
@@ -1,95 +0,0 @@
|
||||
// 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 caddybrotli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/andybalholm/brotli"
|
||||
"github.com/caddyserver/caddy/v2"
|
||||
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
|
||||
"github.com/caddyserver/caddy/v2/modules/caddyhttp/encode"
|
||||
)
|
||||
|
||||
func init() {
|
||||
caddy.RegisterModule(Brotli{})
|
||||
}
|
||||
|
||||
// Brotli can create brotli encoders. Note that brotli
|
||||
// is not known for great encoding performance, and
|
||||
// its use during requests is discouraged; instead,
|
||||
// pre-compress the content instead.
|
||||
type Brotli struct {
|
||||
Quality *int `json:"quality,omitempty"`
|
||||
}
|
||||
|
||||
// CaddyModule returns the Caddy module information.
|
||||
func (Brotli) CaddyModule() caddy.ModuleInfo {
|
||||
return caddy.ModuleInfo{
|
||||
ID: "http.encoders.brotli",
|
||||
New: func() caddy.Module { return new(Brotli) },
|
||||
}
|
||||
}
|
||||
|
||||
// UnmarshalCaddyfile sets up the handler from Caddyfile tokens.
|
||||
func (b *Brotli) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
|
||||
for d.Next() {
|
||||
if !d.NextArg() {
|
||||
continue
|
||||
}
|
||||
qualityStr := d.Val()
|
||||
quality, err := strconv.Atoi(qualityStr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
b.Quality = &quality
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Validate validates b's configuration.
|
||||
func (b Brotli) Validate() error {
|
||||
if b.Quality != nil {
|
||||
quality := *b.Quality
|
||||
if quality < brotli.BestSpeed {
|
||||
return fmt.Errorf("quality too low; must be >= %d", brotli.BestSpeed)
|
||||
}
|
||||
if quality > brotli.BestCompression {
|
||||
return fmt.Errorf("quality too high; must be <= %d", brotli.BestCompression)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// AcceptEncoding returns the name of the encoding as
|
||||
// used in the Accept-Encoding request headers.
|
||||
func (Brotli) AcceptEncoding() string { return "br" }
|
||||
|
||||
// NewEncoder returns a new brotli writer.
|
||||
func (b Brotli) NewEncoder() encode.Encoder {
|
||||
quality := brotli.DefaultCompression
|
||||
if b.Quality != nil {
|
||||
quality = *b.Quality
|
||||
}
|
||||
return brotli.NewWriterLevel(nil, quality)
|
||||
}
|
||||
|
||||
// Interface guards
|
||||
var (
|
||||
_ encode.Encoding = (*Brotli)(nil)
|
||||
_ caddy.Validator = (*Brotli)(nil)
|
||||
_ caddyfile.Unmarshaler = (*Brotli)(nil)
|
||||
)
|
||||
@@ -1,95 +0,0 @@
|
||||
// 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 encode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/caddyserver/caddy/v2"
|
||||
"github.com/caddyserver/caddy/v2/caddyconfig"
|
||||
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
|
||||
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
|
||||
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
|
||||
)
|
||||
|
||||
func init() {
|
||||
httpcaddyfile.RegisterHandlerDirective("encode", parseCaddyfile)
|
||||
}
|
||||
|
||||
func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
|
||||
enc := new(Encode)
|
||||
err := enc.UnmarshalCaddyfile(h.Dispenser)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return enc, nil
|
||||
}
|
||||
|
||||
// UnmarshalCaddyfile sets up the handler from Caddyfile tokens. Syntax:
|
||||
//
|
||||
// encode [<matcher>] <formats...> {
|
||||
// gzip [<level>]
|
||||
// zstd
|
||||
// brotli [<quality>]
|
||||
// }
|
||||
//
|
||||
// Specifying the formats on the first line will use those formats' defaults.
|
||||
func (enc *Encode) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
|
||||
for d.Next() {
|
||||
for _, arg := range d.RemainingArgs() {
|
||||
mod, err := caddy.GetModule("http.encoders." + arg)
|
||||
if err != nil {
|
||||
return fmt.Errorf("finding encoder module '%s': %v", mod, err)
|
||||
}
|
||||
encoding, ok := mod.New().(Encoding)
|
||||
if !ok {
|
||||
return fmt.Errorf("module %s is not an HTTP encoding", mod)
|
||||
}
|
||||
if enc.EncodingsRaw == nil {
|
||||
enc.EncodingsRaw = make(caddy.ModuleMap)
|
||||
}
|
||||
enc.EncodingsRaw[arg] = caddyconfig.JSON(encoding, nil)
|
||||
}
|
||||
|
||||
for d.NextBlock(0) {
|
||||
name := d.Val()
|
||||
mod, err := caddy.GetModule("http.encoders." + name)
|
||||
if err != nil {
|
||||
return fmt.Errorf("getting encoder module '%s': %v", name, err)
|
||||
}
|
||||
unm, ok := mod.New().(caddyfile.Unmarshaler)
|
||||
if !ok {
|
||||
return fmt.Errorf("encoder module '%s' is not a Caddyfile unmarshaler", mod)
|
||||
}
|
||||
err = unm.UnmarshalCaddyfile(d.NewFromNextSegment())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
encoding, ok := unm.(Encoding)
|
||||
if !ok {
|
||||
return fmt.Errorf("module %s is not an HTTP encoding", mod)
|
||||
}
|
||||
if enc.EncodingsRaw == nil {
|
||||
enc.EncodingsRaw = make(caddy.ModuleMap)
|
||||
}
|
||||
enc.EncodingsRaw[name] = caddyconfig.JSON(encoding, nil)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Interface guard
|
||||
var _ caddyfile.Unmarshaler = (*Encode)(nil)
|
||||
@@ -1,349 +0,0 @@
|
||||
// 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 encode implements an encoder middleware for Caddy. The initial
|
||||
// enhancements related to Accept-Encoding, minimum content length, and
|
||||
// buffer/writer pools were adapted from https://github.com/xi2/httpgzip
|
||||
// then modified heavily to accommodate modular encoders and fix bugs.
|
||||
// Code borrowed from that repository is Copyright (c) 2015 The Httpgzip Authors.
|
||||
package encode
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/caddyserver/caddy/v2"
|
||||
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
|
||||
)
|
||||
|
||||
func init() {
|
||||
caddy.RegisterModule(Encode{})
|
||||
}
|
||||
|
||||
// Encode is a middleware which can encode responses.
|
||||
type Encode struct {
|
||||
// Selection of compression algorithms to choose from. The best one
|
||||
// will be chosen based on the client's Accept-Encoding header.
|
||||
EncodingsRaw caddy.ModuleMap `json:"encodings,omitempty" caddy:"namespace=http.encoders"`
|
||||
|
||||
// If the client has no strong preference, choose this encoding. TODO: Not yet implemented
|
||||
// Prefer []string `json:"prefer,omitempty"`
|
||||
|
||||
// Only encode responses that are at least this many bytes long.
|
||||
MinLength int `json:"minimum_length,omitempty"`
|
||||
|
||||
writerPools map[string]*sync.Pool // TODO: these pools do not get reused through config reloads...
|
||||
}
|
||||
|
||||
// CaddyModule returns the Caddy module information.
|
||||
func (Encode) CaddyModule() caddy.ModuleInfo {
|
||||
return caddy.ModuleInfo{
|
||||
ID: "http.handlers.encode",
|
||||
New: func() caddy.Module { return new(Encode) },
|
||||
}
|
||||
}
|
||||
|
||||
// Provision provisions enc.
|
||||
func (enc *Encode) Provision(ctx caddy.Context) error {
|
||||
mods, err := ctx.LoadModule(enc, "EncodingsRaw")
|
||||
if err != nil {
|
||||
return fmt.Errorf("loading encoder modules: %v", err)
|
||||
}
|
||||
for modName, modIface := range mods.(map[string]interface{}) {
|
||||
err = enc.addEncoding(modIface.(Encoding))
|
||||
if err != nil {
|
||||
return fmt.Errorf("adding encoding %s: %v", modName, err)
|
||||
}
|
||||
}
|
||||
if enc.MinLength == 0 {
|
||||
enc.MinLength = defaultMinLength
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (enc *Encode) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
|
||||
for _, encName := range acceptedEncodings(r) {
|
||||
if _, ok := enc.writerPools[encName]; !ok {
|
||||
continue // encoding not offered
|
||||
}
|
||||
w = enc.openResponseWriter(encName, w)
|
||||
defer w.(*responseWriter).Close()
|
||||
break
|
||||
}
|
||||
return next.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
func (enc *Encode) addEncoding(e Encoding) error {
|
||||
ae := e.AcceptEncoding()
|
||||
if ae == "" {
|
||||
return fmt.Errorf("encoder does not specify an Accept-Encoding value")
|
||||
}
|
||||
if _, ok := enc.writerPools[ae]; ok {
|
||||
return fmt.Errorf("encoder already added: %s", ae)
|
||||
}
|
||||
if enc.writerPools == nil {
|
||||
enc.writerPools = make(map[string]*sync.Pool)
|
||||
}
|
||||
enc.writerPools[ae] = &sync.Pool{
|
||||
New: func() interface{} {
|
||||
return e.NewEncoder()
|
||||
},
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// openResponseWriter creates a new response writer that may (or may not)
|
||||
// encode the response with encodingName. The returned response writer MUST
|
||||
// be closed after the handler completes.
|
||||
func (enc *Encode) openResponseWriter(encodingName string, w http.ResponseWriter) *responseWriter {
|
||||
var rw responseWriter
|
||||
return enc.initResponseWriter(&rw, encodingName, w)
|
||||
}
|
||||
|
||||
// initResponseWriter initializes the responseWriter instance
|
||||
// allocated in openResponseWriter, enabling mid-stack inlining.
|
||||
func (enc *Encode) initResponseWriter(rw *responseWriter, encodingName string, wrappedRW http.ResponseWriter) *responseWriter {
|
||||
buf := bufPool.Get().(*bytes.Buffer)
|
||||
buf.Reset()
|
||||
|
||||
// The allocation of ResponseWriterWrapper might be optimized as well.
|
||||
rw.ResponseWriterWrapper = &caddyhttp.ResponseWriterWrapper{ResponseWriter: wrappedRW}
|
||||
rw.encodingName = encodingName
|
||||
rw.buf = buf
|
||||
rw.config = enc
|
||||
|
||||
return rw
|
||||
}
|
||||
|
||||
// responseWriter writes to an underlying response writer
|
||||
// using the encoding represented by encodingName and
|
||||
// configured by config.
|
||||
type responseWriter struct {
|
||||
*caddyhttp.ResponseWriterWrapper
|
||||
encodingName string
|
||||
w Encoder
|
||||
buf *bytes.Buffer
|
||||
config *Encode
|
||||
statusCode int
|
||||
}
|
||||
|
||||
// WriteHeader stores the status to write when the time comes
|
||||
// to actually write the header.
|
||||
func (rw *responseWriter) WriteHeader(status int) {
|
||||
rw.statusCode = status
|
||||
}
|
||||
|
||||
// Write writes to the response. If the response qualifies,
|
||||
// it is encoded using the encoder, which is initialized
|
||||
// if not done so already.
|
||||
func (rw *responseWriter) Write(p []byte) (int, error) {
|
||||
var n, written int
|
||||
var err error
|
||||
|
||||
if rw.buf != nil && rw.config.MinLength > 0 {
|
||||
written = rw.buf.Len()
|
||||
_, err := rw.buf.Write(p)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
rw.init()
|
||||
p = rw.buf.Bytes()
|
||||
defer func() {
|
||||
bufPool.Put(rw.buf)
|
||||
rw.buf = nil
|
||||
}()
|
||||
}
|
||||
|
||||
// before we write to the response, we need to make
|
||||
// sure the header is written exactly once; we do
|
||||
// that by checking if a status code has been set,
|
||||
// and if so, that means we haven't written the
|
||||
// header OR the default status code will be written
|
||||
// by the standard library
|
||||
if rw.statusCode > 0 {
|
||||
rw.ResponseWriter.WriteHeader(rw.statusCode)
|
||||
rw.statusCode = 0
|
||||
}
|
||||
|
||||
switch {
|
||||
case rw.w != nil:
|
||||
n, err = rw.w.Write(p)
|
||||
default:
|
||||
n, err = rw.ResponseWriter.Write(p)
|
||||
}
|
||||
n -= written
|
||||
if n < 0 {
|
||||
n = 0
|
||||
}
|
||||
return n, err
|
||||
}
|
||||
|
||||
// Close writes any remaining buffered response and
|
||||
// deallocates any active resources.
|
||||
func (rw *responseWriter) Close() error {
|
||||
var err error
|
||||
// only attempt to write the remaining buffered response
|
||||
// if there are any bytes left to write; otherwise, if
|
||||
// the handler above us returned an error without writing
|
||||
// anything, we'd write to the response when we instead
|
||||
// should simply let the error propagate back down; this
|
||||
// is why the check for rw.buf.Len() > 0 is crucial
|
||||
if rw.buf != nil && rw.buf.Len() > 0 {
|
||||
rw.init()
|
||||
p := rw.buf.Bytes()
|
||||
defer func() {
|
||||
bufPool.Put(rw.buf)
|
||||
rw.buf = nil
|
||||
}()
|
||||
switch {
|
||||
case rw.w != nil:
|
||||
_, err = rw.w.Write(p)
|
||||
default:
|
||||
_, err = rw.ResponseWriter.Write(p)
|
||||
}
|
||||
} else if rw.statusCode != 0 {
|
||||
// it is possible that a body was not written, and
|
||||
// a header was not even written yet, even though
|
||||
// we are closing; ensure the proper status code is
|
||||
// written exactly once, or we risk breaking requests
|
||||
// that rely on If-None-Match, for example
|
||||
rw.ResponseWriter.WriteHeader(rw.statusCode)
|
||||
rw.statusCode = 0
|
||||
}
|
||||
if rw.w != nil {
|
||||
err2 := rw.w.Close()
|
||||
if err2 != nil && err == nil {
|
||||
err = err2
|
||||
}
|
||||
rw.config.writerPools[rw.encodingName].Put(rw.w)
|
||||
rw.w = nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// init should be called before we write a response, if rw.buf has contents.
|
||||
func (rw *responseWriter) init() {
|
||||
if rw.Header().Get("Content-Encoding") == "" && rw.buf.Len() >= rw.config.MinLength {
|
||||
rw.w = rw.config.writerPools[rw.encodingName].Get().(Encoder)
|
||||
rw.w.Reset(rw.ResponseWriter)
|
||||
rw.Header().Del("Content-Length") // https://github.com/golang/go/issues/14975
|
||||
rw.Header().Set("Content-Encoding", rw.encodingName)
|
||||
rw.Header().Add("Vary", "Accept-Encoding")
|
||||
}
|
||||
rw.Header().Del("Accept-Ranges") // we don't know ranges for dynamically-encoded content
|
||||
}
|
||||
|
||||
// acceptedEncodings returns the list of encodings that the
|
||||
// client supports, in descending order of preference. If
|
||||
// the Sec-WebSocket-Key header is present then non-identity
|
||||
// encodings are not considered. See
|
||||
// http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html.
|
||||
func acceptedEncodings(r *http.Request) []string {
|
||||
acceptEncHeader := r.Header.Get("Accept-Encoding")
|
||||
websocketKey := r.Header.Get("Sec-WebSocket-Key")
|
||||
if acceptEncHeader == "" {
|
||||
return []string{}
|
||||
}
|
||||
|
||||
var prefs []encodingPreference
|
||||
|
||||
for _, accepted := range strings.Split(acceptEncHeader, ",") {
|
||||
parts := strings.Split(accepted, ";")
|
||||
encName := strings.ToLower(strings.TrimSpace(parts[0]))
|
||||
|
||||
// determine q-factor
|
||||
qFactor := 1.0
|
||||
if len(parts) > 1 {
|
||||
qFactorStr := strings.ToLower(strings.TrimSpace(parts[1]))
|
||||
if strings.HasPrefix(qFactorStr, "q=") {
|
||||
if qFactorFloat, err := strconv.ParseFloat(qFactorStr[2:], 32); err == nil {
|
||||
if qFactorFloat >= 0 && qFactorFloat <= 1 {
|
||||
qFactor = qFactorFloat
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// encodings with q-factor of 0 are not accepted;
|
||||
// use a small threshold to account for float precision
|
||||
if qFactor < 0.00001 {
|
||||
continue
|
||||
}
|
||||
|
||||
// don't encode WebSocket handshakes
|
||||
if websocketKey != "" && encName != "identity" {
|
||||
continue
|
||||
}
|
||||
|
||||
prefs = append(prefs, encodingPreference{
|
||||
encoding: encName,
|
||||
q: qFactor,
|
||||
})
|
||||
}
|
||||
|
||||
// sort preferences by descending q-factor
|
||||
sort.Slice(prefs, func(i, j int) bool { return prefs[i].q > prefs[j].q })
|
||||
|
||||
// TODO: If no preference, or same pref for all encodings,
|
||||
// and not websocket, use default encoding ordering (enc.Prefer)
|
||||
// for those which are accepted by the client
|
||||
|
||||
prefEncNames := make([]string, len(prefs))
|
||||
for i := range prefs {
|
||||
prefEncNames[i] = prefs[i].encoding
|
||||
}
|
||||
|
||||
return prefEncNames
|
||||
}
|
||||
|
||||
// encodingPreference pairs an encoding with its q-factor.
|
||||
type encodingPreference struct {
|
||||
encoding string
|
||||
q float64
|
||||
}
|
||||
|
||||
// Encoder is a type which can encode a stream of data.
|
||||
type Encoder interface {
|
||||
io.WriteCloser
|
||||
Reset(io.Writer)
|
||||
}
|
||||
|
||||
// Encoding is a type which can create encoders of its kind
|
||||
// and return the name used in the Accept-Encoding header.
|
||||
type Encoding interface {
|
||||
AcceptEncoding() string
|
||||
NewEncoder() Encoder
|
||||
}
|
||||
|
||||
var bufPool = sync.Pool{
|
||||
New: func() interface{} {
|
||||
return new(bytes.Buffer)
|
||||
},
|
||||
}
|
||||
|
||||
// defaultMinLength is the minimum length at which to compress content.
|
||||
const defaultMinLength = 512
|
||||
|
||||
// Interface guards
|
||||
var (
|
||||
_ caddy.Provisioner = (*Encode)(nil)
|
||||
_ caddyhttp.MiddlewareHandler = (*Encode)(nil)
|
||||
_ caddyhttp.HTTPInterfaces = (*responseWriter)(nil)
|
||||
)
|
||||
@@ -1,12 +0,0 @@
|
||||
package encode
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func BenchmarkOpenResponseWriter(b *testing.B) {
|
||||
enc := new(Encode)
|
||||
for n := 0; n < b.N; n++ {
|
||||
enc.openResponseWriter("test", nil)
|
||||
}
|
||||
}
|
||||
@@ -1,99 +0,0 @@
|
||||
// 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 caddygzip
|
||||
|
||||
import (
|
||||
"compress/flate"
|
||||
"compress/gzip" // TODO: consider using https://github.com/klauspost/compress/gzip
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/caddyserver/caddy/v2"
|
||||
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
|
||||
"github.com/caddyserver/caddy/v2/modules/caddyhttp/encode"
|
||||
)
|
||||
|
||||
func init() {
|
||||
caddy.RegisterModule(Gzip{})
|
||||
}
|
||||
|
||||
// Gzip can create gzip encoders.
|
||||
type Gzip struct {
|
||||
Level int `json:"level,omitempty"`
|
||||
}
|
||||
|
||||
// CaddyModule returns the Caddy module information.
|
||||
func (Gzip) CaddyModule() caddy.ModuleInfo {
|
||||
return caddy.ModuleInfo{
|
||||
ID: "http.encoders.gzip",
|
||||
New: func() caddy.Module { return new(Gzip) },
|
||||
}
|
||||
}
|
||||
|
||||
// UnmarshalCaddyfile sets up the handler from Caddyfile tokens.
|
||||
func (g *Gzip) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
|
||||
for d.Next() {
|
||||
if !d.NextArg() {
|
||||
continue
|
||||
}
|
||||
levelStr := d.Val()
|
||||
level, err := strconv.Atoi(levelStr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
g.Level = level
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Provision provisions g's configuration.
|
||||
func (g *Gzip) Provision(ctx caddy.Context) error {
|
||||
if g.Level == 0 {
|
||||
g.Level = defaultGzipLevel
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Validate validates g's configuration.
|
||||
func (g Gzip) Validate() error {
|
||||
if g.Level < flate.NoCompression {
|
||||
return fmt.Errorf("quality too low; must be >= %d", flate.NoCompression)
|
||||
}
|
||||
if g.Level > flate.BestCompression {
|
||||
return fmt.Errorf("quality too high; must be <= %d", flate.BestCompression)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// AcceptEncoding returns the name of the encoding as
|
||||
// used in the Accept-Encoding request headers.
|
||||
func (Gzip) AcceptEncoding() string { return "gzip" }
|
||||
|
||||
// NewEncoder returns a new gzip writer.
|
||||
func (g Gzip) NewEncoder() encode.Encoder {
|
||||
writer, _ := gzip.NewWriterLevel(nil, g.Level)
|
||||
return writer
|
||||
}
|
||||
|
||||
// Informed from http://blog.klauspost.com/gzip-performance-for-go-webservers/
|
||||
var defaultGzipLevel = 5
|
||||
|
||||
// Interface guards
|
||||
var (
|
||||
_ encode.Encoding = (*Gzip)(nil)
|
||||
_ caddy.Provisioner = (*Gzip)(nil)
|
||||
_ caddy.Validator = (*Gzip)(nil)
|
||||
_ caddyfile.Unmarshaler = (*Gzip)(nil)
|
||||
)
|
||||
@@ -1,58 +0,0 @@
|
||||
// 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 caddyzstd
|
||||
|
||||
import (
|
||||
"github.com/caddyserver/caddy/v2"
|
||||
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
|
||||
"github.com/caddyserver/caddy/v2/modules/caddyhttp/encode"
|
||||
"github.com/klauspost/compress/zstd"
|
||||
)
|
||||
|
||||
func init() {
|
||||
caddy.RegisterModule(Zstd{})
|
||||
}
|
||||
|
||||
// Zstd can create Zstandard encoders.
|
||||
type Zstd struct{}
|
||||
|
||||
// CaddyModule returns the Caddy module information.
|
||||
func (Zstd) CaddyModule() caddy.ModuleInfo {
|
||||
return caddy.ModuleInfo{
|
||||
ID: "http.encoders.zstd",
|
||||
New: func() caddy.Module { return new(Zstd) },
|
||||
}
|
||||
}
|
||||
|
||||
// UnmarshalCaddyfile sets up the handler from Caddyfile tokens.
|
||||
func (z *Zstd) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// AcceptEncoding returns the name of the encoding as
|
||||
// used in the Accept-Encoding request headers.
|
||||
func (Zstd) AcceptEncoding() string { return "zstd" }
|
||||
|
||||
// NewEncoder returns a new gzip writer.
|
||||
func (z Zstd) NewEncoder() encode.Encoder {
|
||||
writer, _ := zstd.NewWriter(nil)
|
||||
return writer
|
||||
}
|
||||
|
||||
// Interface guards
|
||||
var (
|
||||
_ encode.Encoding = (*Zstd)(nil)
|
||||
_ caddyfile.Unmarshaler = (*Zstd)(nil)
|
||||
)
|
||||
@@ -1,111 +0,0 @@
|
||||
// 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 caddyhttp
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
mathrand "math/rand"
|
||||
"path"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/caddyserver/caddy/v2"
|
||||
)
|
||||
|
||||
// Error is a convenient way for a Handler to populate the
|
||||
// essential fields of a HandlerError. If err is itself a
|
||||
// HandlerError, then any essential fields that are not
|
||||
// set will be populated.
|
||||
func Error(statusCode int, err error) HandlerError {
|
||||
const idLen = 9
|
||||
if he, ok := err.(HandlerError); ok {
|
||||
if he.ID == "" {
|
||||
he.ID = randString(idLen, true)
|
||||
}
|
||||
if he.Trace == "" {
|
||||
he.Trace = trace()
|
||||
}
|
||||
if he.StatusCode == 0 {
|
||||
he.StatusCode = statusCode
|
||||
}
|
||||
return he
|
||||
}
|
||||
return HandlerError{
|
||||
ID: randString(idLen, true),
|
||||
StatusCode: statusCode,
|
||||
Err: err,
|
||||
Trace: trace(),
|
||||
}
|
||||
}
|
||||
|
||||
// HandlerError is a serializable representation of
|
||||
// an error from within an HTTP handler.
|
||||
type HandlerError struct {
|
||||
Err error // the original error value and message
|
||||
StatusCode int // the HTTP status code to associate with this error
|
||||
|
||||
ID string // generated; for identifying this error in logs
|
||||
Trace string // produced from call stack
|
||||
}
|
||||
|
||||
func (e HandlerError) Error() string {
|
||||
var s string
|
||||
if e.ID != "" {
|
||||
s += fmt.Sprintf("{id=%s}", e.ID)
|
||||
}
|
||||
if e.Trace != "" {
|
||||
s += " " + e.Trace
|
||||
}
|
||||
if e.StatusCode != 0 {
|
||||
s += fmt.Sprintf(": HTTP %d", e.StatusCode)
|
||||
}
|
||||
if e.Err != nil {
|
||||
s += ": " + e.Err.Error()
|
||||
}
|
||||
return strings.TrimSpace(s)
|
||||
}
|
||||
|
||||
// randString returns a string of n random characters.
|
||||
// It is not even remotely secure OR a proper distribution.
|
||||
// But it's good enough for some things. It excludes certain
|
||||
// confusing characters like I, l, 1, 0, O, etc. If sameCase
|
||||
// is true, then uppercase letters are excluded.
|
||||
func randString(n int, sameCase bool) string {
|
||||
if n <= 0 {
|
||||
return ""
|
||||
}
|
||||
dict := []byte("abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRTUVWXY23456789")
|
||||
if sameCase {
|
||||
dict = []byte("abcdefghijkmnpqrstuvwxyz0123456789")
|
||||
}
|
||||
b := make([]byte, n)
|
||||
for i := range b {
|
||||
b[i] = dict[mathrand.Int63()%int64(len(dict))]
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func trace() string {
|
||||
if pc, file, line, ok := runtime.Caller(2); ok {
|
||||
filename := path.Base(file)
|
||||
pkgAndFuncName := path.Base(runtime.FuncForPC(pc).Name())
|
||||
return fmt.Sprintf("%s (%s:%d)", pkgAndFuncName, filename, line)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// ErrorCtxKey is the context key to use when storing
|
||||
// an error (for use with context.Context).
|
||||
const ErrorCtxKey = caddy.CtxKey("handler_chain_error")
|
||||
@@ -1,167 +0,0 @@
|
||||
// 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 fileserver
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/caddyserver/caddy/v2"
|
||||
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
|
||||
)
|
||||
|
||||
// Browse configures directory browsing.
|
||||
type Browse struct {
|
||||
// Use this template file instead of the default browse template.
|
||||
TemplateFile string `json:"template_file,omitempty"`
|
||||
|
||||
template *template.Template
|
||||
}
|
||||
|
||||
func (fsrv *FileServer) serveBrowse(dirPath string, w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
|
||||
// navigation on the client-side gets messed up if the
|
||||
// URL doesn't end in a trailing slash because hrefs like
|
||||
// "/b/c" on a path like "/a" end up going to "/b/c" instead
|
||||
// of "/a/b/c" - so we have to redirect in this case
|
||||
if !strings.HasSuffix(r.URL.Path, "/") {
|
||||
r.URL.Path += "/"
|
||||
http.Redirect(w, r, r.URL.String(), http.StatusMovedPermanently)
|
||||
return nil
|
||||
}
|
||||
|
||||
dir, err := fsrv.openFile(dirPath, w)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer dir.Close()
|
||||
|
||||
repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
|
||||
|
||||
// calling path.Clean here prevents weird breadcrumbs when URL paths are sketchy like /%2e%2e%2f
|
||||
listing, err := fsrv.loadDirectoryContents(dir, path.Clean(r.URL.Path), repl)
|
||||
switch {
|
||||
case os.IsPermission(err):
|
||||
return caddyhttp.Error(http.StatusForbidden, err)
|
||||
case os.IsNotExist(err):
|
||||
return fsrv.notFound(w, r, next)
|
||||
case err != nil:
|
||||
return caddyhttp.Error(http.StatusInternalServerError, err)
|
||||
}
|
||||
|
||||
fsrv.browseApplyQueryParams(w, r, &listing)
|
||||
|
||||
// write response as either JSON or HTML
|
||||
var buf *bytes.Buffer
|
||||
acceptHeader := strings.ToLower(strings.Join(r.Header["Accept"], ","))
|
||||
if strings.Contains(acceptHeader, "application/json") {
|
||||
if buf, err = fsrv.browseWriteJSON(listing); err != nil {
|
||||
return caddyhttp.Error(http.StatusInternalServerError, err)
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
} else {
|
||||
if buf, err = fsrv.browseWriteHTML(listing); err != nil {
|
||||
return caddyhttp.Error(http.StatusInternalServerError, err)
|
||||
}
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
}
|
||||
|
||||
buf.WriteTo(w)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (fsrv *FileServer) loadDirectoryContents(dir *os.File, urlPath string, repl *caddy.Replacer) (browseListing, error) {
|
||||
files, err := dir.Readdir(-1)
|
||||
if err != nil {
|
||||
return browseListing{}, err
|
||||
}
|
||||
|
||||
// determine if user can browse up another folder
|
||||
curPathDir := path.Dir(strings.TrimSuffix(urlPath, "/"))
|
||||
canGoUp := strings.HasPrefix(curPathDir, fsrv.Root)
|
||||
|
||||
return fsrv.directoryListing(files, canGoUp, urlPath, repl), nil
|
||||
}
|
||||
|
||||
// browseApplyQueryParams applies query parameters to the listing.
|
||||
// It mutates the listing and may set cookies.
|
||||
func (fsrv *FileServer) browseApplyQueryParams(w http.ResponseWriter, r *http.Request, listing *browseListing) {
|
||||
sortParam := r.URL.Query().Get("sort")
|
||||
orderParam := r.URL.Query().Get("order")
|
||||
limitParam := r.URL.Query().Get("limit")
|
||||
|
||||
// first figure out what to sort by
|
||||
switch sortParam {
|
||||
case "":
|
||||
sortParam = sortByNameDirFirst
|
||||
if sortCookie, sortErr := r.Cookie("sort"); sortErr == nil {
|
||||
sortParam = sortCookie.Value
|
||||
}
|
||||
case sortByName, sortByNameDirFirst, sortBySize, sortByTime:
|
||||
http.SetCookie(w, &http.Cookie{Name: "sort", Value: sortParam, Secure: r.TLS != nil})
|
||||
}
|
||||
|
||||
// then figure out the order
|
||||
switch orderParam {
|
||||
case "":
|
||||
orderParam = "asc"
|
||||
if orderCookie, orderErr := r.Cookie("order"); orderErr == nil {
|
||||
orderParam = orderCookie.Value
|
||||
}
|
||||
case "asc", "desc":
|
||||
http.SetCookie(w, &http.Cookie{Name: "order", Value: orderParam, Secure: r.TLS != nil})
|
||||
}
|
||||
|
||||
// finally, apply the sorting and limiting
|
||||
listing.applySortAndLimit(sortParam, orderParam, limitParam)
|
||||
}
|
||||
|
||||
func (fsrv *FileServer) browseWriteJSON(listing browseListing) (*bytes.Buffer, error) {
|
||||
buf := bufPool.Get().(*bytes.Buffer)
|
||||
err := json.NewEncoder(buf).Encode(listing.Items)
|
||||
bufPool.Put(buf)
|
||||
return buf, err
|
||||
}
|
||||
|
||||
func (fsrv *FileServer) browseWriteHTML(listing browseListing) (*bytes.Buffer, error) {
|
||||
buf := bufPool.Get().(*bytes.Buffer)
|
||||
err := fsrv.Browse.template.Execute(buf, listing)
|
||||
bufPool.Put(buf)
|
||||
return buf, err
|
||||
}
|
||||
|
||||
// isSymlink return true if f is a symbolic link
|
||||
func isSymlink(f os.FileInfo) bool {
|
||||
return f.Mode()&os.ModeSymlink != 0
|
||||
}
|
||||
|
||||
// isSymlinkTargetDir returns true if f's symbolic link target
|
||||
// is a directory.
|
||||
func isSymlinkTargetDir(f os.FileInfo, root, urlPath string) bool {
|
||||
if !isSymlink(f) {
|
||||
return false
|
||||
}
|
||||
target := sanitizedPathJoin(root, path.Join(urlPath, f.Name()))
|
||||
targetInfo, err := os.Stat(target)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return targetInfo.IsDir()
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
package fileserver
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"testing"
|
||||
|
||||
"github.com/caddyserver/caddy/v2"
|
||||
)
|
||||
|
||||
func BenchmarkBrowseWriteJSON(b *testing.B) {
|
||||
fsrv := new(FileServer)
|
||||
fsrv.Provision(caddy.Context{})
|
||||
listing := browseListing{
|
||||
Name: "test",
|
||||
Path: "test",
|
||||
CanGoUp: false,
|
||||
Items: make([]fileInfo, 100),
|
||||
NumDirs: 42,
|
||||
NumFiles: 420,
|
||||
Sort: "",
|
||||
Order: "",
|
||||
ItemsLimitedTo: 42,
|
||||
}
|
||||
b.ResetTimer()
|
||||
|
||||
for n := 0; n < b.N; n++ {
|
||||
fsrv.browseWriteJSON(listing)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkBrowseWriteHTML(b *testing.B) {
|
||||
fsrv := new(FileServer)
|
||||
fsrv.Provision(caddy.Context{})
|
||||
fsrv.Browse = &Browse{
|
||||
TemplateFile: "",
|
||||
template: template.New("test"),
|
||||
}
|
||||
listing := browseListing{
|
||||
Name: "test",
|
||||
Path: "test",
|
||||
CanGoUp: false,
|
||||
Items: make([]fileInfo, 100),
|
||||
NumDirs: 42,
|
||||
NumFiles: 420,
|
||||
Sort: "",
|
||||
Order: "",
|
||||
ItemsLimitedTo: 42,
|
||||
}
|
||||
b.ResetTimer()
|
||||
|
||||
for n := 0; n < b.N; n++ {
|
||||
fsrv.browseWriteHTML(listing)
|
||||
}
|
||||
}
|
||||
@@ -1,259 +0,0 @@
|
||||
// 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 fileserver
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"os"
|
||||
"path"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/caddyserver/caddy/v2"
|
||||
"github.com/dustin/go-humanize"
|
||||
)
|
||||
|
||||
func (fsrv *FileServer) directoryListing(files []os.FileInfo, canGoUp bool, urlPath string, repl *caddy.Replacer) browseListing {
|
||||
filesToHide := fsrv.transformHidePaths(repl)
|
||||
|
||||
var (
|
||||
fileInfos []fileInfo
|
||||
dirCount, fileCount int
|
||||
)
|
||||
|
||||
for _, f := range files {
|
||||
name := f.Name()
|
||||
|
||||
if fileHidden(name, filesToHide) {
|
||||
continue
|
||||
}
|
||||
|
||||
isDir := f.IsDir() || isSymlinkTargetDir(f, fsrv.Root, urlPath)
|
||||
|
||||
if isDir {
|
||||
name += "/"
|
||||
dirCount++
|
||||
} else {
|
||||
fileCount++
|
||||
}
|
||||
|
||||
u := url.URL{Path: "./" + name} // prepend with "./" to fix paths with ':' in the name
|
||||
|
||||
fileInfos = append(fileInfos, fileInfo{
|
||||
IsDir: isDir,
|
||||
IsSymlink: isSymlink(f),
|
||||
Name: f.Name(),
|
||||
Size: f.Size(),
|
||||
URL: u.String(),
|
||||
ModTime: f.ModTime().UTC(),
|
||||
Mode: f.Mode(),
|
||||
})
|
||||
}
|
||||
|
||||
return browseListing{
|
||||
Name: path.Base(urlPath),
|
||||
Path: urlPath,
|
||||
CanGoUp: canGoUp,
|
||||
Items: fileInfos,
|
||||
NumDirs: dirCount,
|
||||
NumFiles: fileCount,
|
||||
}
|
||||
}
|
||||
|
||||
type browseListing struct {
|
||||
// The name of the directory (the last element of the path).
|
||||
Name string
|
||||
|
||||
// The full path of the request.
|
||||
Path string
|
||||
|
||||
// Whether the parent directory is browseable.
|
||||
CanGoUp bool
|
||||
|
||||
// The items (files and folders) in the path.
|
||||
Items []fileInfo
|
||||
|
||||
// The number of directories in the listing.
|
||||
NumDirs int
|
||||
|
||||
// The number of files (items that aren't directories) in the listing.
|
||||
NumFiles int
|
||||
|
||||
// Sort column used
|
||||
Sort string
|
||||
|
||||
// Sorting order
|
||||
Order string
|
||||
|
||||
// If ≠0 then Items have been limited to that many elements.
|
||||
ItemsLimitedTo int
|
||||
}
|
||||
|
||||
// Breadcrumbs returns l.Path where every element maps
|
||||
// the link to the text to display.
|
||||
func (l browseListing) Breadcrumbs() []crumb {
|
||||
var result []crumb
|
||||
|
||||
if len(l.Path) == 0 {
|
||||
return result
|
||||
}
|
||||
|
||||
// skip trailing slash
|
||||
lpath := l.Path
|
||||
if lpath[len(lpath)-1] == '/' {
|
||||
lpath = lpath[:len(lpath)-1]
|
||||
}
|
||||
|
||||
parts := strings.Split(lpath, "/")
|
||||
for i := range parts {
|
||||
txt := parts[i]
|
||||
if i == 0 && parts[i] == "" {
|
||||
txt = "/"
|
||||
}
|
||||
lnk := strings.Repeat("../", len(parts)-i-1)
|
||||
result = append(result, crumb{Link: lnk, Text: txt})
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func (l *browseListing) applySortAndLimit(sortParam, orderParam, limitParam string) {
|
||||
l.Sort = sortParam
|
||||
l.Order = orderParam
|
||||
|
||||
if l.Order == "desc" {
|
||||
switch l.Sort {
|
||||
case sortByName:
|
||||
sort.Sort(sort.Reverse(byName(*l)))
|
||||
case sortByNameDirFirst:
|
||||
sort.Sort(sort.Reverse(byNameDirFirst(*l)))
|
||||
case sortBySize:
|
||||
sort.Sort(sort.Reverse(bySize(*l)))
|
||||
case sortByTime:
|
||||
sort.Sort(sort.Reverse(byTime(*l)))
|
||||
}
|
||||
} else {
|
||||
switch l.Sort {
|
||||
case sortByName:
|
||||
sort.Sort(byName(*l))
|
||||
case sortByNameDirFirst:
|
||||
sort.Sort(byNameDirFirst(*l))
|
||||
case sortBySize:
|
||||
sort.Sort(bySize(*l))
|
||||
case sortByTime:
|
||||
sort.Sort(byTime(*l))
|
||||
}
|
||||
}
|
||||
|
||||
if limitParam != "" {
|
||||
limit, _ := strconv.Atoi(limitParam)
|
||||
if limit > 0 && limit <= len(l.Items) {
|
||||
l.Items = l.Items[:limit]
|
||||
l.ItemsLimitedTo = limit
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// crumb represents part of a breadcrumb menu,
|
||||
// pairing a link with the text to display.
|
||||
type crumb struct {
|
||||
Link, Text string
|
||||
}
|
||||
|
||||
// fileInfo contains serializable information
|
||||
// about a file or directory.
|
||||
type fileInfo struct {
|
||||
Name string `json:"name"`
|
||||
Size int64 `json:"size"`
|
||||
URL string `json:"url"`
|
||||
ModTime time.Time `json:"mod_time"`
|
||||
Mode os.FileMode `json:"mode"`
|
||||
IsDir bool `json:"is_dir"`
|
||||
IsSymlink bool `json:"is_symlink"`
|
||||
}
|
||||
|
||||
// HumanSize returns the size of the file as a
|
||||
// human-readable string in IEC format (i.e.
|
||||
// power of 2 or base 1024).
|
||||
func (fi fileInfo) HumanSize() string {
|
||||
return humanize.IBytes(uint64(fi.Size))
|
||||
}
|
||||
|
||||
// HumanModTime returns the modified time of the file
|
||||
// as a human-readable string given by format.
|
||||
func (fi fileInfo) HumanModTime(format string) string {
|
||||
return fi.ModTime.Format(format)
|
||||
}
|
||||
|
||||
type byName browseListing
|
||||
type byNameDirFirst browseListing
|
||||
type bySize browseListing
|
||||
type byTime browseListing
|
||||
|
||||
func (l byName) Len() int { return len(l.Items) }
|
||||
func (l byName) Swap(i, j int) { l.Items[i], l.Items[j] = l.Items[j], l.Items[i] }
|
||||
|
||||
func (l byName) Less(i, j int) bool {
|
||||
return strings.ToLower(l.Items[i].Name) < strings.ToLower(l.Items[j].Name)
|
||||
}
|
||||
|
||||
func (l byNameDirFirst) Len() int { return len(l.Items) }
|
||||
func (l byNameDirFirst) Swap(i, j int) { l.Items[i], l.Items[j] = l.Items[j], l.Items[i] }
|
||||
|
||||
func (l byNameDirFirst) Less(i, j int) bool {
|
||||
// sort by name if both are dir or file
|
||||
if l.Items[i].IsDir == l.Items[j].IsDir {
|
||||
return strings.ToLower(l.Items[i].Name) < strings.ToLower(l.Items[j].Name)
|
||||
}
|
||||
// sort dir ahead of file
|
||||
return l.Items[i].IsDir
|
||||
}
|
||||
|
||||
func (l bySize) Len() int { return len(l.Items) }
|
||||
func (l bySize) Swap(i, j int) { l.Items[i], l.Items[j] = l.Items[j], l.Items[i] }
|
||||
|
||||
func (l bySize) Less(i, j int) bool {
|
||||
const directoryOffset = -1 << 31 // = -math.MinInt32
|
||||
|
||||
iSize, jSize := l.Items[i].Size, l.Items[j].Size
|
||||
|
||||
// directory sizes depend on the file system; to
|
||||
// provide a consistent experience, put them up front
|
||||
// and sort them by name
|
||||
if l.Items[i].IsDir {
|
||||
iSize = directoryOffset
|
||||
}
|
||||
if l.Items[j].IsDir {
|
||||
jSize = directoryOffset
|
||||
}
|
||||
if l.Items[i].IsDir && l.Items[j].IsDir {
|
||||
return strings.ToLower(l.Items[i].Name) < strings.ToLower(l.Items[j].Name)
|
||||
}
|
||||
|
||||
return iSize < jSize
|
||||
}
|
||||
|
||||
func (l byTime) Len() int { return len(l.Items) }
|
||||
func (l byTime) Swap(i, j int) { l.Items[i], l.Items[j] = l.Items[j], l.Items[i] }
|
||||
func (l byTime) Less(i, j int) bool { return l.Items[i].ModTime.Before(l.Items[j].ModTime) }
|
||||
|
||||
const (
|
||||
sortByName = "name"
|
||||
sortByNameDirFirst = "name_dir_first"
|
||||
sortBySize = "size"
|
||||
sortByTime = "time"
|
||||
)
|
||||
@@ -1,427 +0,0 @@
|
||||
// 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 fileserver
|
||||
|
||||
const defaultBrowseTemplate = `<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>{{html .Name}}</title>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<style>
|
||||
* { padding: 0; margin: 0; }
|
||||
|
||||
body {
|
||||
font-family: sans-serif;
|
||||
text-rendering: optimizespeed;
|
||||
background-color: #ffffff;
|
||||
}
|
||||
|
||||
a {
|
||||
color: #006ed3;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
a:hover,
|
||||
h1 a:hover {
|
||||
color: #319cff;
|
||||
}
|
||||
|
||||
header,
|
||||
#summary {
|
||||
padding-left: 5%;
|
||||
padding-right: 5%;
|
||||
}
|
||||
|
||||
th:first-child,
|
||||
td:first-child {
|
||||
width: 5%;
|
||||
}
|
||||
|
||||
th:last-child,
|
||||
td:last-child {
|
||||
width: 5%;
|
||||
}
|
||||
|
||||
header {
|
||||
padding-top: 25px;
|
||||
padding-bottom: 15px;
|
||||
background-color: #f2f2f2;
|
||||
}
|
||||
|
||||
h1 {
|
||||
font-size: 20px;
|
||||
font-weight: normal;
|
||||
white-space: nowrap;
|
||||
overflow-x: hidden;
|
||||
text-overflow: ellipsis;
|
||||
color: #999;
|
||||
}
|
||||
|
||||
h1 a {
|
||||
color: #000;
|
||||
margin: 0 4px;
|
||||
}
|
||||
|
||||
h1 a:hover {
|
||||
text-decoration: underline;
|
||||
}
|
||||
|
||||
h1 a:first-child {
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
main {
|
||||
display: block;
|
||||
}
|
||||
|
||||
.meta {
|
||||
font-size: 12px;
|
||||
font-family: Verdana, sans-serif;
|
||||
border-bottom: 1px solid #9C9C9C;
|
||||
padding-top: 10px;
|
||||
padding-bottom: 10px;
|
||||
}
|
||||
|
||||
.meta-item {
|
||||
margin-right: 1em;
|
||||
}
|
||||
|
||||
#filter {
|
||||
padding: 4px;
|
||||
border: 1px solid #CCC;
|
||||
}
|
||||
|
||||
table {
|
||||
width: 100%;
|
||||
border-collapse: collapse;
|
||||
}
|
||||
|
||||
tr {
|
||||
border-bottom: 1px dashed #dadada;
|
||||
}
|
||||
|
||||
tbody tr:hover {
|
||||
background-color: #ffffec;
|
||||
}
|
||||
|
||||
th,
|
||||
td {
|
||||
text-align: left;
|
||||
padding: 10px 0;
|
||||
}
|
||||
|
||||
th {
|
||||
padding-top: 15px;
|
||||
padding-bottom: 15px;
|
||||
font-size: 16px;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
th a {
|
||||
color: black;
|
||||
}
|
||||
|
||||
th svg {
|
||||
vertical-align: middle;
|
||||
}
|
||||
|
||||
td {
|
||||
white-space: nowrap;
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
td:nth-child(2) {
|
||||
width: 80%;
|
||||
}
|
||||
|
||||
td:nth-child(3),
|
||||
th:nth-child(3) {
|
||||
padding: 0 20px 0 20px;
|
||||
}
|
||||
|
||||
th:nth-child(4),
|
||||
td:nth-child(4) {
|
||||
text-align: right;
|
||||
}
|
||||
|
||||
td:nth-child(2) svg {
|
||||
position: absolute;
|
||||
}
|
||||
|
||||
td .name,
|
||||
td .goup {
|
||||
margin-left: 1.75em;
|
||||
word-break: break-all;
|
||||
overflow-wrap: break-word;
|
||||
white-space: pre-wrap;
|
||||
}
|
||||
|
||||
.icon {
|
||||
margin-right: 5px;
|
||||
}
|
||||
|
||||
.icon.sort {
|
||||
display: inline-block;
|
||||
width: 1em;
|
||||
height: 1em;
|
||||
position: relative;
|
||||
top: .2em;
|
||||
}
|
||||
|
||||
.icon.sort .top {
|
||||
position: absolute;
|
||||
left: 0;
|
||||
top: -1px;
|
||||
}
|
||||
|
||||
.icon.sort .bottom {
|
||||
position: absolute;
|
||||
bottom: -1px;
|
||||
left: 0;
|
||||
}
|
||||
|
||||
footer {
|
||||
padding: 40px 20px;
|
||||
font-size: 12px;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
@media (max-width: 600px) {
|
||||
.hideable {
|
||||
display: none;
|
||||
}
|
||||
|
||||
td:nth-child(2) {
|
||||
width: auto;
|
||||
}
|
||||
|
||||
th:nth-child(3),
|
||||
td:nth-child(3) {
|
||||
padding-right: 5%;
|
||||
text-align: right;
|
||||
}
|
||||
|
||||
h1 {
|
||||
color: #000;
|
||||
}
|
||||
|
||||
h1 a {
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
#filter {
|
||||
max-width: 100px;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body onload='initFilter()'>
|
||||
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" height="0" width="0" style="position: absolute;">
|
||||
<defs>
|
||||
<!-- Folder -->
|
||||
<g id="folder" fill-rule="nonzero" fill="none">
|
||||
<path d="M285.22 37.55h-142.6L110.9 0H31.7C14.25 0 0 16.9 0 37.55v75.1h316.92V75.1c0-20.65-14.26-37.55-31.7-37.55z" fill="#FFA000"/>
|
||||
<path d="M285.22 36H31.7C14.25 36 0 50.28 0 67.74v158.7c0 17.47 14.26 31.75 31.7 31.75H285.2c17.44 0 31.7-14.3 31.7-31.75V67.75c0-17.47-14.26-31.75-31.7-31.75z" fill="#FFCA28"/>
|
||||
</g>
|
||||
<g id="folder-shortcut" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
|
||||
<g id="folder-shortcut-group" fill-rule="nonzero">
|
||||
<g id="folder-shortcut-shape">
|
||||
<path d="M285.224876,37.5486902 L142.612438,37.5486902 L110.920785,0 L31.6916529,0 C14.2612438,0 0,16.8969106 0,37.5486902 L0,112.646071 L316.916529,112.646071 L316.916529,75.0973805 C316.916529,54.4456008 302.655285,37.5486902 285.224876,37.5486902 Z" id="Shape" fill="#FFA000"></path>
|
||||
<path d="M285.224876,36 L31.6916529,36 C14.2612438,36 0,50.2838568 0,67.7419039 L0,226.451424 C0,243.909471 14.2612438,258.193328 31.6916529,258.193328 L285.224876,258.193328 C302.655285,258.193328 316.916529,243.909471 316.916529,226.451424 L316.916529,67.7419039 C316.916529,50.2838568 302.655285,36 285.224876,36 Z" id="Shape" fill="#FFCA28"></path>
|
||||
</g>
|
||||
<path d="M126.154134,250.559184 C126.850974,251.883673 127.300549,253.006122 127.772602,254.106122 C128.469442,255.206122 128.919016,256.104082 129.638335,257.002041 C130.559962,258.326531 131.728855,259 133.100057,259 C134.493737,259 135.415364,258.55102 136.112204,257.67551 C136.809044,257.002041 137.258619,255.902041 137.258619,254.577551 C137.258619,253.904082 137.258619,252.804082 137.033832,251.457143 C136.786566,249.908163 136.561779,249.032653 136.561779,248.583673 C136.089726,242.814286 135.864939,237.920408 135.864939,233.273469 C135.864939,225.057143 136.786566,217.514286 138.180246,210.846939 C139.798713,204.202041 141.889234,198.634694 144.429328,193.763265 C147.216689,188.869388 150.678411,184.873469 154.836973,181.326531 C158.995535,177.779592 163.626149,174.883673 168.481552,172.661224 C173.336954,170.438776 179.113983,168.665306 185.587852,167.340816 C192.061722,166.218367 198.760378,165.342857 205.481514,164.669388 C212.18017,164.220408 219.598146,163.995918 228.162535,163.995918 L246.055591,163.995918 L246.055591,195.514286 C246.055591,197.736735 246.752431,199.510204 248.370899,201.059184 C250.214153,202.608163 252.079886,203.506122 254.372715,203.506122 C256.463236,203.506122 258.531277,202.608163 260.172223,201.059184 L326.102289,137.797959 C327.720757,136.24898 328.642384,134.47551 328.642384,132.253061 C328.642384,130.030612 327.720757,128.257143 326.102289,126.708163 L260.172223,63.4469388 C258.553756,61.8979592 256.463236,61 254.395194,61 C252.079886,61 250.236632,61.8979592 248.393377,63.4469388 C246.77491,64.9959184 246.07807,66.7693878 246.07807,68.9918367 L246.07807,100.510204 L228.162535,100.510204 C166.863084,100.510204 129.166282,117.167347 115.274437,150.459184 C110.666301,161.54898 108.350993,175.310204 108.350993,191.742857 C108.350993,205.279592 113.903236,223.912245 124.760454,247.438776 C125.00772,248.112245 125.457294,249.010204 126.154134,250.559184 Z" id="Shape" fill="#FFFFFF" transform="translate(218.496689, 160.000000) scale(-1, 1) translate(-218.496689, -160.000000) "></path>
|
||||
</g>
|
||||
</g>
|
||||
|
||||
<!-- File -->
|
||||
<g id="file" stroke="#000" stroke-width="25" fill="#FFF" fill-rule="evenodd" stroke-linecap="round" stroke-linejoin="round">
|
||||
<path d="M13 24.12v274.76c0 6.16 5.87 11.12 13.17 11.12H239c7.3 0 13.17-4.96 13.17-11.12V136.15S132.6 13 128.37 13H26.17C18.87 13 13 17.96 13 24.12z"/>
|
||||
<path d="M129.37 13L129 113.9c0 10.58 7.26 19.1 16.27 19.1H249L129.37 13z"/>
|
||||
</g>
|
||||
<g id="file-shortcut" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
|
||||
<g id="file-shortcut-group" transform="translate(13.000000, 13.000000)">
|
||||
<g id="file-shortcut-shape" stroke="#000000" stroke-width="25" fill="#FFFFFF" stroke-linecap="round" stroke-linejoin="round">
|
||||
<path d="M0,11.1214886 L0,285.878477 C0,292.039924 5.87498876,296.999983 13.1728373,296.999983 L225.997983,296.999983 C233.295974,296.999983 239.17082,292.039942 239.17082,285.878477 L239.17082,123.145388 C239.17082,123.145388 119.58541,2.84217094e-14 115.369423,2.84217094e-14 L13.1728576,2.84217094e-14 C5.87500907,-1.71479982e-05 0,4.96022995 0,11.1214886 Z" id="rect1171"></path>
|
||||
<path d="M116.37005,0 L116,100.904964 C116,111.483663 123.258008,120 132.273377,120 L236,120 L116.37005,0 L116.37005,0 Z" id="rect1794"></path>
|
||||
</g>
|
||||
<path d="M47.803141,294.093878 C48.4999811,295.177551 48.9495553,296.095918 49.4216083,296.995918 C50.1184484,297.895918 50.5680227,298.630612 51.2873415,299.365306 C52.2089688,300.44898 53.3778619,301 54.7490634,301 C56.1427436,301 57.0643709,300.632653 57.761211,299.916327 C58.4580511,299.365306 58.9076254,298.465306 58.9076254,297.381633 C58.9076254,296.830612 58.9076254,295.930612 58.6828382,294.828571 C58.4355724,293.561224 58.2107852,292.844898 58.2107852,292.477551 C57.7387323,287.757143 57.5139451,283.753061 57.5139451,279.95102 C57.5139451,273.228571 58.4355724,267.057143 59.8292526,261.602041 C61.44772,256.165306 63.5382403,251.610204 66.0783349,247.62449 C68.8656954,243.620408 72.3274172,240.35102 76.4859792,237.44898 C80.6445412,234.546939 85.2751561,232.177551 90.1305582,230.359184 C94.9859603,228.540816 100.76299,227.089796 107.236859,226.006122 C113.710728,225.087755 120.409385,224.371429 127.13052,223.820408 C133.829177,223.453061 141.247152,223.269388 149.811542,223.269388 L167.704598,223.269388 L167.704598,249.057143 C167.704598,250.87551 168.401438,252.326531 170.019905,253.593878 C171.86316,254.861224 173.728893,255.595918 176.021722,255.595918 C178.112242,255.595918 180.180284,254.861224 181.82123,253.593878 L247.751296,201.834694 C249.369763,200.567347 250.291391,199.116327 250.291391,197.297959 C250.291391,195.479592 249.369763,194.028571 247.751296,192.761224 L181.82123,141.002041 C180.202763,139.734694 178.112242,139 176.044201,139 C173.728893,139 171.885639,139.734694 170.042384,141.002041 C168.423917,142.269388 167.727077,143.720408 167.727077,145.538776 L167.727077,171.326531 L149.811542,171.326531 C88.5120908,171.326531 50.8152886,184.955102 36.9234437,212.193878 C32.3153075,221.267347 30,232.526531 30,245.971429 C30,257.046939 35.5522422,272.291837 46.4094607,291.540816 C46.6567266,292.091837 47.1063009,292.826531 47.803141,294.093878 Z" id="Shape-Copy" fill="#000000" fill-rule="nonzero" transform="translate(140.145695, 220.000000) scale(-1, 1) translate(-140.145695, -220.000000) "></path>
|
||||
</g>
|
||||
</g>
|
||||
|
||||
<!-- Up arrow -->
|
||||
<g id="up-arrow" transform="translate(-279.22 -208.12)">
|
||||
<path transform="matrix(.22413 0 0 .12089 335.67 164.35)" stroke-width="0" d="m-194.17 412.01h-28.827-28.827l14.414-24.965 14.414-24.965 14.414 24.965z"/>
|
||||
</g>
|
||||
|
||||
<!-- Down arrow -->
|
||||
<g id="down-arrow" transform="translate(-279.22 -208.12)">
|
||||
<path transform="matrix(.22413 0 0 -.12089 335.67 257.93)" stroke-width="0" d="m-194.17 412.01h-28.827-28.827l14.414-24.965 14.414-24.965 14.414 24.965z"/>
|
||||
</g>
|
||||
</defs>
|
||||
</svg>
|
||||
|
||||
<header>
|
||||
<h1>
|
||||
{{range $i, $crumb := .Breadcrumbs}}<a href="{{html $crumb.Link}}">{{html $crumb.Text}}</a>{{if ne $i 0}}/{{end}}{{end}}
|
||||
</h1>
|
||||
</header>
|
||||
<main>
|
||||
<div class="meta">
|
||||
<div id="summary">
|
||||
<span class="meta-item"><b>{{.NumDirs}}</b> director{{if eq 1 .NumDirs}}y{{else}}ies{{end}}</span>
|
||||
<span class="meta-item"><b>{{.NumFiles}}</b> file{{if ne 1 .NumFiles}}s{{end}}</span>
|
||||
{{- if ne 0 .ItemsLimitedTo}}
|
||||
<span class="meta-item">(of which only <b>{{.ItemsLimitedTo}}</b> are displayed)</span>
|
||||
{{- end}}
|
||||
<span class="meta-item"><input type="text" placeholder="filter" id="filter" onkeyup='filter()'></span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="listing">
|
||||
<table aria-describedby="summary">
|
||||
<thead>
|
||||
<tr>
|
||||
<th></th>
|
||||
<th>
|
||||
{{- if and (eq .Sort "namedirfirst") (ne .Order "desc")}}
|
||||
<a href="?sort=namedirfirst&order=desc{{if ne 0 .ItemsLimitedTo}}&limit={{.ItemsLimitedTo}}{{end}}" class="icon"><svg width="1em" height=".5em" version="1.1" viewBox="0 0 12.922194 6.0358899"><use xlink:href="#up-arrow"></use></svg></a>
|
||||
{{- else if and (eq .Sort "namedirfirst") (ne .Order "asc")}}
|
||||
<a href="?sort=namedirfirst&order=asc{{if ne 0 .ItemsLimitedTo}}&limit={{.ItemsLimitedTo}}{{end}}" class="icon"><svg width="1em" height=".5em" version="1.1" viewBox="0 0 12.922194 6.0358899"><use xlink:href="#down-arrow"></use></svg></a>
|
||||
{{- else}}
|
||||
<a href="?sort=namedirfirst&order=asc{{if ne 0 .ItemsLimitedTo}}&limit={{.ItemsLimitedTo}}{{end}}" class="icon sort"><svg class="top" width="1em" height=".5em" version="1.1" viewBox="0 0 12.922194 6.0358899"><use xlink:href="#up-arrow"></use></svg><svg class="bottom" width="1em" height=".5em" version="1.1" viewBox="0 0 12.922194 6.0358899"><use xlink:href="#down-arrow"></use></svg></a>
|
||||
{{- end}}
|
||||
|
||||
{{- if and (eq .Sort "name") (ne .Order "desc")}}
|
||||
<a href="?sort=name&order=desc{{if ne 0 .ItemsLimitedTo}}&limit={{.ItemsLimitedTo}}{{end}}">Name <svg width="1em" height=".5em" version="1.1" viewBox="0 0 12.922194 6.0358899"><use xlink:href="#up-arrow"></use></svg></a>
|
||||
{{- else if and (eq .Sort "name") (ne .Order "asc")}}
|
||||
<a href="?sort=name&order=asc{{if ne 0 .ItemsLimitedTo}}&limit={{.ItemsLimitedTo}}{{end}}">Name <svg width="1em" height=".5em" version="1.1" viewBox="0 0 12.922194 6.0358899"><use xlink:href="#down-arrow"></use></svg></a>
|
||||
{{- else}}
|
||||
<a href="?sort=name&order=asc{{if ne 0 .ItemsLimitedTo}}&limit={{.ItemsLimitedTo}}{{end}}">Name</a>
|
||||
{{- end}}
|
||||
</th>
|
||||
<th>
|
||||
{{- if and (eq .Sort "size") (ne .Order "desc")}}
|
||||
<a href="?sort=size&order=desc{{if ne 0 .ItemsLimitedTo}}&limit={{.ItemsLimitedTo}}{{end}}">Size <svg width="1em" height=".5em" version="1.1" viewBox="0 0 12.922194 6.0358899"><use xlink:href="#up-arrow"></use></svg></a>
|
||||
{{- else if and (eq .Sort "size") (ne .Order "asc")}}
|
||||
<a href="?sort=size&order=asc{{if ne 0 .ItemsLimitedTo}}&limit={{.ItemsLimitedTo}}{{end}}">Size <svg width="1em" height=".5em" version="1.1" viewBox="0 0 12.922194 6.0358899"><use xlink:href="#down-arrow"></use></svg></a>
|
||||
{{- else}}
|
||||
<a href="?sort=size&order=asc{{if ne 0 .ItemsLimitedTo}}&limit={{.ItemsLimitedTo}}{{end}}">Size</a>
|
||||
{{- end}}
|
||||
</th>
|
||||
<th class="hideable">
|
||||
{{- if and (eq .Sort "time") (ne .Order "desc")}}
|
||||
<a href="?sort=time&order=desc{{if ne 0 .ItemsLimitedTo}}&limit={{.ItemsLimitedTo}}{{end}}">Modified <svg width="1em" height=".5em" version="1.1" viewBox="0 0 12.922194 6.0358899"><use xlink:href="#up-arrow"></use></svg></a>
|
||||
{{- else if and (eq .Sort "time") (ne .Order "asc")}}
|
||||
<a href="?sort=time&order=asc{{if ne 0 .ItemsLimitedTo}}&limit={{.ItemsLimitedTo}}{{end}}">Modified <svg width="1em" height=".5em" version="1.1" viewBox="0 0 12.922194 6.0358899"><use xlink:href="#down-arrow"></use></svg></a>
|
||||
{{- else}}
|
||||
<a href="?sort=time&order=asc{{if ne 0 .ItemsLimitedTo}}&limit={{.ItemsLimitedTo}}{{end}}">Modified</a>
|
||||
{{- end}}
|
||||
</th>
|
||||
<th class="hideable"></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{{- if .CanGoUp}}
|
||||
<tr>
|
||||
<td></td>
|
||||
<td>
|
||||
<a href="..">
|
||||
<span class="goup">Go up</span>
|
||||
</a>
|
||||
</td>
|
||||
<td>—</td>
|
||||
<td class="hideable">—</td>
|
||||
<td class="hideable"></td>
|
||||
</tr>
|
||||
{{- end}}
|
||||
{{- range .Items}}
|
||||
<tr class="file">
|
||||
<td></td>
|
||||
<td>
|
||||
<a href="{{html .URL}}">
|
||||
{{- if .IsDir}}
|
||||
<svg width="1.5em" height="1em" version="1.1" viewBox="0 0 317 259"><use xlink:href="#folder{{if .IsSymlink}}-shortcut{{end}}"></use></svg>
|
||||
{{- else}}
|
||||
<svg width="1.5em" height="1em" version="1.1" viewBox="0 0 265 323"><use xlink:href="#file{{if .IsSymlink}}-shortcut{{end}}"></use></svg>
|
||||
{{- end}}
|
||||
<span class="name">{{html .Name}}</span>
|
||||
</a>
|
||||
</td>
|
||||
{{- if .IsDir}}
|
||||
<td data-order="-1">—</td>
|
||||
{{- else}}
|
||||
<td data-order="{{.Size}}">{{.HumanSize}}</td>
|
||||
{{- end}}
|
||||
<td class="hideable"><time datetime="{{.HumanModTime "2006-01-02T15:04:05Z"}}">{{.HumanModTime "01/02/2006 03:04:05 PM -07:00"}}</time></td>
|
||||
<td class="hideable"></td>
|
||||
</tr>
|
||||
{{- end}}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</main>
|
||||
<footer>
|
||||
Served with <a rel="noopener noreferrer" href="https://caddyserver.com">Caddy</a>
|
||||
</footer>
|
||||
<script>
|
||||
var filterEl = document.getElementById('filter');
|
||||
filterEl.focus();
|
||||
|
||||
function initFilter() {
|
||||
if (!filterEl.value) {
|
||||
var filterParam = new URL(window.location.href).searchParams.get('filter');
|
||||
if (filterParam) {
|
||||
filterEl.value = filterParam;
|
||||
}
|
||||
}
|
||||
filter();
|
||||
}
|
||||
|
||||
function filter() {
|
||||
var q = filterEl.value.trim().toLowerCase();
|
||||
var elems = document.querySelectorAll('tr.file');
|
||||
elems.forEach(function(el) {
|
||||
if (!q) {
|
||||
el.style.display = '';
|
||||
return;
|
||||
}
|
||||
var nameEl = el.querySelector('.name');
|
||||
var nameVal = nameEl.textContent.trim().toLowerCase();
|
||||
if (nameVal.indexOf(q) !== -1) {
|
||||
el.style.display = '';
|
||||
} else {
|
||||
el.style.display = 'none';
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function localizeDatetime(e, index, ar) {
|
||||
if (e.textContent === undefined) {
|
||||
return;
|
||||
}
|
||||
var d = new Date(e.getAttribute('datetime'));
|
||||
if (isNaN(d)) {
|
||||
d = new Date(e.textContent);
|
||||
if (isNaN(d)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
e.textContent = d.toLocaleString([], {day: "2-digit", month: "2-digit", year: "numeric", hour: "2-digit", minute: "2-digit", second: "2-digit"});
|
||||
}
|
||||
var timeList = Array.prototype.slice.call(document.getElementsByTagName("time"));
|
||||
timeList.forEach(localizeDatetime);
|
||||
</script>
|
||||
</body>
|
||||
</html>`
|
||||
@@ -1,172 +0,0 @@
|
||||
// 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 fileserver
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/caddyserver/caddy/v2"
|
||||
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
|
||||
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
|
||||
"github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite"
|
||||
)
|
||||
|
||||
func init() {
|
||||
httpcaddyfile.RegisterHandlerDirective("file_server", parseCaddyfile)
|
||||
httpcaddyfile.RegisterDirective("try_files", parseTryFiles)
|
||||
}
|
||||
|
||||
// parseCaddyfile parses the file_server directive. It enables the static file
|
||||
// server and configures it with this syntax:
|
||||
//
|
||||
// file_server [<matcher>] [browse] {
|
||||
// root <path>
|
||||
// hide <files...>
|
||||
// index <files...>
|
||||
// browse [<template_file>]
|
||||
// }
|
||||
//
|
||||
func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
|
||||
var fsrv FileServer
|
||||
|
||||
for h.Next() {
|
||||
args := h.RemainingArgs()
|
||||
switch len(args) {
|
||||
case 0:
|
||||
case 1:
|
||||
if args[0] != "browse" {
|
||||
return nil, h.ArgErr()
|
||||
}
|
||||
fsrv.Browse = new(Browse)
|
||||
default:
|
||||
return nil, h.ArgErr()
|
||||
}
|
||||
|
||||
for h.NextBlock(0) {
|
||||
switch h.Val() {
|
||||
case "hide":
|
||||
fsrv.Hide = h.RemainingArgs()
|
||||
if len(fsrv.Hide) == 0 {
|
||||
return nil, h.ArgErr()
|
||||
}
|
||||
case "index":
|
||||
fsrv.IndexNames = h.RemainingArgs()
|
||||
if len(fsrv.Hide) == 0 {
|
||||
return nil, h.ArgErr()
|
||||
}
|
||||
case "root":
|
||||
if !h.Args(&fsrv.Root) {
|
||||
return nil, h.ArgErr()
|
||||
}
|
||||
case "browse":
|
||||
if fsrv.Browse != nil {
|
||||
return nil, h.Err("browsing is already configured")
|
||||
}
|
||||
fsrv.Browse = new(Browse)
|
||||
h.Args(&fsrv.Browse.TemplateFile)
|
||||
default:
|
||||
return nil, h.Errf("unknown subdirective '%s'", h.Val())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// hide the Caddyfile (and any imported Caddyfiles)
|
||||
if configFiles := h.Caddyfiles(); len(configFiles) > 0 {
|
||||
for _, file := range configFiles {
|
||||
if !fileHidden(file, fsrv.Hide) {
|
||||
fsrv.Hide = append(fsrv.Hide, file)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return &fsrv, nil
|
||||
}
|
||||
|
||||
// parseTryFiles parses the try_files directive. It combines a file matcher
|
||||
// with a rewrite directive, so this is not a standard handler directive.
|
||||
// A try_files directive has this syntax (notice no matcher tokens accepted):
|
||||
//
|
||||
// try_files <files...>
|
||||
//
|
||||
// and is basically shorthand for:
|
||||
//
|
||||
// @try_files {
|
||||
// file {
|
||||
// try_files <files...>
|
||||
// }
|
||||
// }
|
||||
// rewrite @try_files {http.matchers.file.relative}
|
||||
//
|
||||
// This directive rewrites request paths only, preserving any other part
|
||||
// of the URI, unless the part is explicitly given in the file list. For
|
||||
// example, if any of the files in the list have a query string:
|
||||
//
|
||||
// try_files {path} index.php?{query}&p={path}
|
||||
//
|
||||
// then the query string will not be treated as part of the file name; and
|
||||
// if that file matches, the given query string will replace any query string
|
||||
// that already exists on the request URI.
|
||||
func parseTryFiles(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error) {
|
||||
if !h.Next() {
|
||||
return nil, h.ArgErr()
|
||||
}
|
||||
|
||||
tryFiles := h.RemainingArgs()
|
||||
if len(tryFiles) == 0 {
|
||||
return nil, h.ArgErr()
|
||||
}
|
||||
|
||||
// makeRoute returns a route that tries the files listed in try
|
||||
// and then rewrites to the matched file; userQueryString is
|
||||
// appended to the rewrite rule.
|
||||
makeRoute := func(try []string, userQueryString string) []httpcaddyfile.ConfigValue {
|
||||
handler := rewrite.Rewrite{
|
||||
URI: "{http.matchers.file.relative}" + userQueryString,
|
||||
}
|
||||
matcherSet := caddy.ModuleMap{
|
||||
"file": h.JSON(MatchFile{TryFiles: try}),
|
||||
}
|
||||
return h.NewRoute(matcherSet, handler)
|
||||
}
|
||||
|
||||
var result []httpcaddyfile.ConfigValue
|
||||
|
||||
// if there are query strings in the list, we have to split into
|
||||
// a separate route for each item with a query string, because
|
||||
// the rewrite is different for that item
|
||||
var try []string
|
||||
for _, item := range tryFiles {
|
||||
if idx := strings.Index(item, "?"); idx >= 0 {
|
||||
if len(try) > 0 {
|
||||
result = append(result, makeRoute(try, "")...)
|
||||
try = []string{}
|
||||
}
|
||||
result = append(result, makeRoute([]string{item[:idx]}, item[idx:])...)
|
||||
continue
|
||||
}
|
||||
// accumulate consecutive non-query-string parameters
|
||||
try = append(try, item)
|
||||
}
|
||||
if len(try) > 0 {
|
||||
result = append(result, makeRoute(try, "")...)
|
||||
}
|
||||
|
||||
// ensure that multiple routes (possible if rewrite targets
|
||||
// have query strings, for example) are grouped together
|
||||
// so only the first matching rewrite is performed (#2891)
|
||||
h.GroupRoutes(result)
|
||||
|
||||
return result, nil
|
||||
}
|
||||
@@ -1,128 +0,0 @@
|
||||
// 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 fileserver
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"log"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/caddyserver/caddy/v2"
|
||||
"github.com/caddyserver/caddy/v2/caddyconfig"
|
||||
caddycmd "github.com/caddyserver/caddy/v2/cmd"
|
||||
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
|
||||
caddytpl "github.com/caddyserver/caddy/v2/modules/caddyhttp/templates"
|
||||
"github.com/caddyserver/certmagic"
|
||||
)
|
||||
|
||||
func init() {
|
||||
caddycmd.RegisterCommand(caddycmd.Command{
|
||||
Name: "file-server",
|
||||
Func: cmdFileServer,
|
||||
Usage: "[--domain <example.com>] [--root <path>] [--listen <addr>] [--browse]",
|
||||
Short: "Spins up a production-ready file server",
|
||||
Long: `
|
||||
A simple but production-ready file server. Useful for quick deployments,
|
||||
demos, and development.
|
||||
|
||||
The listener's socket address can be customized with the --listen flag.
|
||||
|
||||
If a domain name is specified with --domain, the default listener address
|
||||
will be changed to the HTTPS port and the server will use HTTPS. If using
|
||||
a public domain, ensure A/AAAA records are properly configured before
|
||||
using this option.
|
||||
|
||||
If --browse is enabled, requests for folders without an index file will
|
||||
respond with a file listing.`,
|
||||
Flags: func() *flag.FlagSet {
|
||||
fs := flag.NewFlagSet("file-server", flag.ExitOnError)
|
||||
fs.String("domain", "", "Domain name at which to serve the files")
|
||||
fs.String("root", "", "The path to the root of the site")
|
||||
fs.String("listen", "", "The address to which to bind the listener")
|
||||
fs.Bool("browse", false, "Enable directory browsing")
|
||||
fs.Bool("templates", false, "Enable template rendering")
|
||||
return fs
|
||||
}(),
|
||||
})
|
||||
}
|
||||
|
||||
func cmdFileServer(fs caddycmd.Flags) (int, error) {
|
||||
domain := fs.String("domain")
|
||||
root := fs.String("root")
|
||||
listen := fs.String("listen")
|
||||
browse := fs.Bool("browse")
|
||||
templates := fs.Bool("templates")
|
||||
|
||||
var handlers []json.RawMessage
|
||||
|
||||
if templates {
|
||||
handler := caddytpl.Templates{FileRoot: root}
|
||||
handlers = append(handlers, caddyconfig.JSONModuleObject(handler, "handler", "templates", nil))
|
||||
}
|
||||
|
||||
handler := FileServer{Root: root}
|
||||
if browse {
|
||||
handler.Browse = new(Browse)
|
||||
}
|
||||
|
||||
handlers = append(handlers, caddyconfig.JSONModuleObject(handler, "handler", "file_server", nil))
|
||||
|
||||
route := caddyhttp.Route{HandlersRaw: handlers}
|
||||
|
||||
if domain != "" {
|
||||
route.MatcherSetsRaw = []caddy.ModuleMap{
|
||||
caddy.ModuleMap{
|
||||
"host": caddyconfig.JSON(caddyhttp.MatchHost{domain}, nil),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
server := &caddyhttp.Server{
|
||||
ReadHeaderTimeout: caddy.Duration(10 * time.Second),
|
||||
IdleTimeout: caddy.Duration(30 * time.Second),
|
||||
MaxHeaderBytes: 1024 * 10,
|
||||
Routes: caddyhttp.RouteList{route},
|
||||
}
|
||||
if listen == "" {
|
||||
if domain == "" {
|
||||
listen = ":80"
|
||||
} else {
|
||||
listen = ":" + strconv.Itoa(certmagic.HTTPSPort)
|
||||
}
|
||||
}
|
||||
server.Listen = []string{listen}
|
||||
|
||||
httpApp := caddyhttp.App{
|
||||
Servers: map[string]*caddyhttp.Server{"static": server},
|
||||
}
|
||||
|
||||
cfg := &caddy.Config{
|
||||
Admin: &caddy.AdminConfig{Disabled: true},
|
||||
AppsRaw: caddy.ModuleMap{
|
||||
"http": caddyconfig.JSON(httpApp, nil),
|
||||
},
|
||||
}
|
||||
|
||||
err := caddy.Run(cfg)
|
||||
if err != nil {
|
||||
return caddy.ExitCodeFailedStartup, err
|
||||
}
|
||||
|
||||
log.Printf("Caddy 2 serving static files on %s", listen)
|
||||
|
||||
select {}
|
||||
}
|
||||
@@ -1,270 +0,0 @@
|
||||
// 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 fileserver
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/caddyserver/caddy/v2"
|
||||
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
|
||||
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
|
||||
)
|
||||
|
||||
func init() {
|
||||
caddy.RegisterModule(MatchFile{})
|
||||
}
|
||||
|
||||
// MatchFile is an HTTP request matcher that can match
|
||||
// requests based upon file existence.
|
||||
//
|
||||
// Upon matching, two new placeholders will be made
|
||||
// available:
|
||||
//
|
||||
// - `{http.matchers.file.relative}` The root-relative
|
||||
// path of the file. This is often useful when rewriting
|
||||
// requests.
|
||||
// - `{http.matchers.file.absolute}` The absolute path
|
||||
// of the matched file.
|
||||
type MatchFile struct {
|
||||
// The root directory, used for creating absolute
|
||||
// file paths, and required when working with
|
||||
// relative paths; if not specified, `{http.vars.root}`
|
||||
// will be used, if set; otherwise, the current
|
||||
// directory is assumed. Accepts placeholders.
|
||||
Root string `json:"root,omitempty"`
|
||||
|
||||
// The list of files to try. Each path here is
|
||||
// considered related to Root. If nil, the request
|
||||
// URL's path will be assumed. Files and
|
||||
// directories are treated distinctly, so to match
|
||||
// a directory, the filepath MUST end in a forward
|
||||
// slash `/`. To match a regular file, there must
|
||||
// be no trailing slash. Accepts placeholders.
|
||||
TryFiles []string `json:"try_files,omitempty"`
|
||||
|
||||
// How to choose a file in TryFiles. Can be:
|
||||
//
|
||||
// - first_exist
|
||||
// - smallest_size
|
||||
// - largest_size
|
||||
// - most_recently_modified
|
||||
//
|
||||
// Default is first_exist.
|
||||
TryPolicy string `json:"try_policy,omitempty"`
|
||||
}
|
||||
|
||||
// CaddyModule returns the Caddy module information.
|
||||
func (MatchFile) CaddyModule() caddy.ModuleInfo {
|
||||
return caddy.ModuleInfo{
|
||||
ID: "http.matchers.file",
|
||||
New: func() caddy.Module { return new(MatchFile) },
|
||||
}
|
||||
}
|
||||
|
||||
// UnmarshalCaddyfile sets up the matcher from Caddyfile tokens. Syntax:
|
||||
//
|
||||
// file {
|
||||
// root <path>
|
||||
// try_files <files...>
|
||||
// try_policy first_exist|smallest_size|largest_size|most_recently_modified
|
||||
// }
|
||||
//
|
||||
func (m *MatchFile) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
|
||||
for d.Next() {
|
||||
for d.NextBlock(0) {
|
||||
switch d.Val() {
|
||||
case "root":
|
||||
if !d.NextArg() {
|
||||
return d.ArgErr()
|
||||
}
|
||||
m.Root = d.Val()
|
||||
case "try_files":
|
||||
m.TryFiles = d.RemainingArgs()
|
||||
if len(m.TryFiles) == 0 {
|
||||
return d.ArgErr()
|
||||
}
|
||||
case "try_policy":
|
||||
if !d.NextArg() {
|
||||
return d.ArgErr()
|
||||
}
|
||||
m.TryPolicy = d.Val()
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Provision sets up m's defaults.
|
||||
func (m *MatchFile) Provision(_ caddy.Context) error {
|
||||
if m.Root == "" {
|
||||
m.Root = "{http.vars.root}"
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Validate ensures m has a valid configuration.
|
||||
func (m MatchFile) Validate() error {
|
||||
switch m.TryPolicy {
|
||||
case "",
|
||||
tryPolicyFirstExist,
|
||||
tryPolicyLargestSize,
|
||||
tryPolicySmallestSize,
|
||||
tryPolicyMostRecentlyMod:
|
||||
default:
|
||||
return fmt.Errorf("unknown try policy %s", m.TryPolicy)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Match returns true if r matches m. Returns true
|
||||
// if a file was matched. If so, two placeholders
|
||||
// will be available:
|
||||
// - http.matchers.file.relative
|
||||
// - http.matchers.file.absolute
|
||||
func (m MatchFile) Match(r *http.Request) bool {
|
||||
repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
|
||||
rel, abs, matched := m.selectFile(r)
|
||||
if matched {
|
||||
repl.Set("http.matchers.file.relative", rel)
|
||||
repl.Set("http.matchers.file.absolute", abs)
|
||||
}
|
||||
return matched
|
||||
}
|
||||
|
||||
// selectFile chooses a file according to m.TryPolicy by appending
|
||||
// the paths in m.TryFiles to m.Root, with placeholder replacements.
|
||||
// It returns the root-relative path to the matched file, the full
|
||||
// or absolute path, and whether a match was made.
|
||||
func (m MatchFile) selectFile(r *http.Request) (rel, abs string, matched bool) {
|
||||
repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
|
||||
|
||||
root := repl.ReplaceAll(m.Root, ".")
|
||||
|
||||
// if list of files to try was omitted entirely,
|
||||
// assume URL path
|
||||
if m.TryFiles == nil {
|
||||
// m is not a pointer, so this is safe
|
||||
m.TryFiles = []string{r.URL.Path}
|
||||
}
|
||||
|
||||
switch m.TryPolicy {
|
||||
case "", tryPolicyFirstExist:
|
||||
for _, f := range m.TryFiles {
|
||||
suffix := path.Clean(repl.ReplaceAll(f, ""))
|
||||
fullpath := sanitizedPathJoin(root, suffix)
|
||||
if strictFileExists(fullpath) {
|
||||
return suffix, fullpath, true
|
||||
}
|
||||
}
|
||||
|
||||
case tryPolicyLargestSize:
|
||||
var largestSize int64
|
||||
var largestFilename string
|
||||
var largestSuffix string
|
||||
for _, f := range m.TryFiles {
|
||||
suffix := path.Clean(repl.ReplaceAll(f, ""))
|
||||
fullpath := sanitizedPathJoin(root, suffix)
|
||||
info, err := os.Stat(fullpath)
|
||||
if err == nil && info.Size() > largestSize {
|
||||
largestSize = info.Size()
|
||||
largestFilename = fullpath
|
||||
largestSuffix = suffix
|
||||
}
|
||||
}
|
||||
return largestSuffix, largestFilename, true
|
||||
|
||||
case tryPolicySmallestSize:
|
||||
var smallestSize int64
|
||||
var smallestFilename string
|
||||
var smallestSuffix string
|
||||
for _, f := range m.TryFiles {
|
||||
suffix := path.Clean(repl.ReplaceAll(f, ""))
|
||||
fullpath := sanitizedPathJoin(root, suffix)
|
||||
info, err := os.Stat(fullpath)
|
||||
if err == nil && (smallestSize == 0 || info.Size() < smallestSize) {
|
||||
smallestSize = info.Size()
|
||||
smallestFilename = fullpath
|
||||
smallestSuffix = suffix
|
||||
}
|
||||
}
|
||||
return smallestSuffix, smallestFilename, true
|
||||
|
||||
case tryPolicyMostRecentlyMod:
|
||||
var recentDate time.Time
|
||||
var recentFilename string
|
||||
var recentSuffix string
|
||||
for _, f := range m.TryFiles {
|
||||
suffix := path.Clean(repl.ReplaceAll(f, ""))
|
||||
fullpath := sanitizedPathJoin(root, suffix)
|
||||
info, err := os.Stat(fullpath)
|
||||
if err == nil &&
|
||||
(recentDate.IsZero() || info.ModTime().After(recentDate)) {
|
||||
recentDate = info.ModTime()
|
||||
recentFilename = fullpath
|
||||
recentSuffix = suffix
|
||||
}
|
||||
}
|
||||
return recentSuffix, recentFilename, true
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// strictFileExists returns true if file exists
|
||||
// and matches the convention of the given file
|
||||
// path. If the path ends in a forward slash,
|
||||
// the file must also be a directory; if it does
|
||||
// NOT end in a forward slash, the file must NOT
|
||||
// be a directory.
|
||||
func strictFileExists(file string) bool {
|
||||
stat, err := os.Stat(file)
|
||||
if err != nil {
|
||||
// in reality, this can be any error
|
||||
// such as permission or even obscure
|
||||
// ones like "is not a directory" (when
|
||||
// trying to stat a file within a file);
|
||||
// in those cases we can't be sure if
|
||||
// the file exists, so we just treat any
|
||||
// error as if it does not exist; see
|
||||
// https://stackoverflow.com/a/12518877/1048862
|
||||
return false
|
||||
}
|
||||
if strings.HasSuffix(file, "/") {
|
||||
// by convention, file paths ending
|
||||
// in a slash must be a directory
|
||||
return stat.IsDir()
|
||||
}
|
||||
// by convention, file paths NOT ending
|
||||
// in a slash must NOT be a directory
|
||||
return !stat.IsDir()
|
||||
}
|
||||
|
||||
const (
|
||||
tryPolicyFirstExist = "first_exist"
|
||||
tryPolicyLargestSize = "largest_size"
|
||||
tryPolicySmallestSize = "smallest_size"
|
||||
tryPolicyMostRecentlyMod = "most_recently_modified"
|
||||
)
|
||||
|
||||
// Interface guards
|
||||
var (
|
||||
_ caddy.Validator = (*MatchFile)(nil)
|
||||
_ caddyhttp.RequestMatcher = (*MatchFile)(nil)
|
||||
)
|
||||
@@ -1,404 +0,0 @@
|
||||
// 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 fileserver
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io"
|
||||
weakrand "math/rand"
|
||||
"mime"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/caddyserver/caddy/v2"
|
||||
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
|
||||
)
|
||||
|
||||
func init() {
|
||||
weakrand.Seed(time.Now().UnixNano())
|
||||
|
||||
caddy.RegisterModule(FileServer{})
|
||||
}
|
||||
|
||||
// FileServer implements a static file server responder for Caddy.
|
||||
type FileServer struct {
|
||||
// The path to the root of the site. Default is `{http.vars.root}` if set,
|
||||
// or current working directory otherwise.
|
||||
Root string `json:"root,omitempty"`
|
||||
|
||||
// A list of files or folders to hide; the file server will pretend as if
|
||||
// they don't exist. Accepts globular patterns like "*.hidden" or "/foo/*/bar".
|
||||
Hide []string `json:"hide,omitempty"`
|
||||
|
||||
// The names of files to try as index files if a folder is requested.
|
||||
IndexNames []string `json:"index_names,omitempty"`
|
||||
|
||||
// Enables file listings if a directory was requested and no index
|
||||
// file is present.
|
||||
Browse *Browse `json:"browse,omitempty"`
|
||||
|
||||
// Use redirects to enforce trailing slashes for directories, or to
|
||||
// remove trailing slash from URIs for files. Default is true.
|
||||
CanonicalURIs *bool `json:"canonical_uris,omitempty"`
|
||||
|
||||
// If pass-thru mode is enabled and a requested file is not found,
|
||||
// it will invoke the next handler in the chain instead of returning
|
||||
// a 404 error. By default, this is false (disabled).
|
||||
PassThru bool `json:"pass_thru,omitempty"`
|
||||
}
|
||||
|
||||
// CaddyModule returns the Caddy module information.
|
||||
func (FileServer) CaddyModule() caddy.ModuleInfo {
|
||||
return caddy.ModuleInfo{
|
||||
ID: "http.handlers.file_server",
|
||||
New: func() caddy.Module { return new(FileServer) },
|
||||
}
|
||||
}
|
||||
|
||||
// Provision sets up the static files responder.
|
||||
func (fsrv *FileServer) Provision(ctx caddy.Context) error {
|
||||
if fsrv.Root == "" {
|
||||
fsrv.Root = "{http.vars.root}"
|
||||
}
|
||||
|
||||
if fsrv.IndexNames == nil {
|
||||
fsrv.IndexNames = defaultIndexNames
|
||||
}
|
||||
|
||||
if fsrv.Browse != nil {
|
||||
var tpl *template.Template
|
||||
var err error
|
||||
if fsrv.Browse.TemplateFile != "" {
|
||||
tpl, err = template.ParseFiles(fsrv.Browse.TemplateFile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("parsing browse template file: %v", err)
|
||||
}
|
||||
} else {
|
||||
tpl, err = template.New("default_listing").Parse(defaultBrowseTemplate)
|
||||
if err != nil {
|
||||
return fmt.Errorf("parsing default browse template: %v", err)
|
||||
}
|
||||
}
|
||||
fsrv.Browse.template = tpl
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
|
||||
repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
|
||||
|
||||
filesToHide := fsrv.transformHidePaths(repl)
|
||||
|
||||
root := repl.ReplaceAll(fsrv.Root, ".")
|
||||
suffix := repl.ReplaceAll(r.URL.Path, "")
|
||||
filename := sanitizedPathJoin(root, suffix)
|
||||
|
||||
// get information about the file
|
||||
info, err := os.Stat(filename)
|
||||
if err != nil {
|
||||
err = mapDirOpenError(err, filename)
|
||||
if os.IsNotExist(err) {
|
||||
return fsrv.notFound(w, r, next)
|
||||
} else if os.IsPermission(err) {
|
||||
return caddyhttp.Error(http.StatusForbidden, err)
|
||||
}
|
||||
// TODO: treat this as resource exhaustion like with os.Open? Or unnecessary here?
|
||||
return caddyhttp.Error(http.StatusInternalServerError, err)
|
||||
}
|
||||
|
||||
// if the request mapped to a directory, see if
|
||||
// there is an index file we can serve
|
||||
var implicitIndexFile bool
|
||||
if info.IsDir() && len(fsrv.IndexNames) > 0 {
|
||||
for _, indexPage := range fsrv.IndexNames {
|
||||
indexPath := sanitizedPathJoin(filename, indexPage)
|
||||
if fileHidden(indexPath, filesToHide) {
|
||||
// pretend this file doesn't exist
|
||||
continue
|
||||
}
|
||||
|
||||
indexInfo, err := os.Stat(indexPath)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
// don't rewrite the request path to append
|
||||
// the index file, because we might need to
|
||||
// do a canonical-URL redirect below based
|
||||
// on the URL as-is
|
||||
|
||||
// we've chosen to use this index file,
|
||||
// so replace the last file info and path
|
||||
// with that of the index file
|
||||
info = indexInfo
|
||||
filename = indexPath
|
||||
implicitIndexFile = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// if still referencing a directory, delegate
|
||||
// to browse or return an error
|
||||
if info.IsDir() {
|
||||
if fsrv.Browse != nil && !fileHidden(filename, filesToHide) {
|
||||
return fsrv.serveBrowse(filename, w, r, next)
|
||||
}
|
||||
return fsrv.notFound(w, r, next)
|
||||
}
|
||||
|
||||
// TODO: content negotiation (brotli sidecar files, etc...)
|
||||
|
||||
// one last check to ensure the file isn't hidden (we might
|
||||
// have changed the filename from when we last checked)
|
||||
if fileHidden(filename, filesToHide) {
|
||||
return fsrv.notFound(w, r, next)
|
||||
}
|
||||
|
||||
// if URL canonicalization is enabled, we need to enforce trailing
|
||||
// slash convention: if a directory, trailing slash; if a file, no
|
||||
// trailing slash - not enforcing this can break relative hrefs
|
||||
// in HTML (see https://github.com/caddyserver/caddy/issues/2741)
|
||||
if fsrv.CanonicalURIs == nil || *fsrv.CanonicalURIs {
|
||||
if implicitIndexFile && !strings.HasSuffix(r.URL.Path, "/") {
|
||||
return redirect(w, r, r.URL.Path+"/")
|
||||
} else if !implicitIndexFile && strings.HasSuffix(r.URL.Path, "/") {
|
||||
return redirect(w, r, r.URL.Path[:len(r.URL.Path)-1])
|
||||
}
|
||||
}
|
||||
|
||||
// open the file
|
||||
file, err := fsrv.openFile(filename, w)
|
||||
if err != nil {
|
||||
if herr, ok := err.(caddyhttp.HandlerError); ok &&
|
||||
herr.StatusCode == http.StatusNotFound {
|
||||
return fsrv.notFound(w, r, next)
|
||||
}
|
||||
return err // error is already structured
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
// set the ETag - note that a conditional If-None-Match request is handled
|
||||
// by http.ServeContent below, which checks against this ETag value
|
||||
w.Header().Set("ETag", calculateEtag(info))
|
||||
|
||||
if w.Header().Get("Content-Type") == "" {
|
||||
mtyp := mime.TypeByExtension(filepath.Ext(filename))
|
||||
if mtyp == "" {
|
||||
// do not allow Go to sniff the content-type; see
|
||||
// https://www.youtube.com/watch?v=8t8JYpt0egE
|
||||
// TODO: If we want a Content-Type, consider writing a default of application/octet-stream - this is secure but violates spec
|
||||
w.Header()["Content-Type"] = nil
|
||||
} else {
|
||||
w.Header().Set("Content-Type", mtyp)
|
||||
}
|
||||
}
|
||||
|
||||
// if this handler exists in an error context (i.e. is
|
||||
// part of a handler chain that is supposed to handle
|
||||
// a previous error), we have to serve the content
|
||||
// manually in order to write the correct status code
|
||||
if reqErr, ok := r.Context().Value(caddyhttp.ErrorCtxKey).(error); ok {
|
||||
statusCode := http.StatusInternalServerError
|
||||
if handlerErr, ok := reqErr.(caddyhttp.HandlerError); ok {
|
||||
if handlerErr.StatusCode > 0 {
|
||||
statusCode = handlerErr.StatusCode
|
||||
}
|
||||
}
|
||||
w.WriteHeader(statusCode)
|
||||
if r.Method != "HEAD" {
|
||||
io.Copy(w, file)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// let the standard library do what it does best; note, however,
|
||||
// that errors generated by ServeContent are written immediately
|
||||
// to the response, so we cannot handle them (but errors there
|
||||
// are rare)
|
||||
http.ServeContent(w, r, info.Name(), info.ModTime(), file)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// openFile opens the file at the given filename. If there was an error,
|
||||
// the response is configured to inform the client how to best handle it
|
||||
// and a well-described handler error is returned (do not wrap the
|
||||
// returned error value).
|
||||
func (fsrv *FileServer) openFile(filename string, w http.ResponseWriter) (*os.File, error) {
|
||||
file, err := os.Open(filename)
|
||||
if err != nil {
|
||||
err = mapDirOpenError(err, filename)
|
||||
if os.IsNotExist(err) {
|
||||
return nil, caddyhttp.Error(http.StatusNotFound, err)
|
||||
} else if os.IsPermission(err) {
|
||||
return nil, caddyhttp.Error(http.StatusForbidden, err)
|
||||
}
|
||||
// maybe the server is under load and ran out of file descriptors?
|
||||
// have client wait arbitrary seconds to help prevent a stampede
|
||||
backoff := weakrand.Intn(maxBackoff-minBackoff) + minBackoff
|
||||
w.Header().Set("Retry-After", strconv.Itoa(backoff))
|
||||
return nil, caddyhttp.Error(http.StatusServiceUnavailable, err)
|
||||
}
|
||||
return file, nil
|
||||
}
|
||||
|
||||
// mapDirOpenError maps the provided non-nil error from opening name
|
||||
// to a possibly better non-nil error. In particular, it turns OS-specific errors
|
||||
// about opening files in non-directories into os.ErrNotExist. See golang/go#18984.
|
||||
// Adapted from the Go standard library; originally written by Nathaniel Caza.
|
||||
// https://go-review.googlesource.com/c/go/+/36635/
|
||||
// https://go-review.googlesource.com/c/go/+/36804/
|
||||
func mapDirOpenError(originalErr error, name string) error {
|
||||
if os.IsNotExist(originalErr) || os.IsPermission(originalErr) {
|
||||
return originalErr
|
||||
}
|
||||
|
||||
parts := strings.Split(name, string(filepath.Separator))
|
||||
for i := range parts {
|
||||
if parts[i] == "" {
|
||||
continue
|
||||
}
|
||||
fi, err := os.Stat(strings.Join(parts[:i+1], string(filepath.Separator)))
|
||||
if err != nil {
|
||||
return originalErr
|
||||
}
|
||||
if !fi.IsDir() {
|
||||
return os.ErrNotExist
|
||||
}
|
||||
}
|
||||
|
||||
return originalErr
|
||||
}
|
||||
|
||||
// transformHidePaths performs replacements for all the elements of
|
||||
// fsrv.Hide and returns a new list of the transformed values.
|
||||
func (fsrv *FileServer) transformHidePaths(repl *caddy.Replacer) []string {
|
||||
hide := make([]string, len(fsrv.Hide))
|
||||
for i := range fsrv.Hide {
|
||||
hide[i] = repl.ReplaceAll(fsrv.Hide[i], "")
|
||||
}
|
||||
return hide
|
||||
}
|
||||
|
||||
// sanitizedPathJoin performs filepath.Join(root, reqPath) that
|
||||
// is safe against directory traversal attacks. It uses logic
|
||||
// similar to that in the Go standard library, specifically
|
||||
// in the implementation of http.Dir. The root is assumed to
|
||||
// be a trusted path, but reqPath is not.
|
||||
func sanitizedPathJoin(root, reqPath string) string {
|
||||
// TODO: Caddy 1 uses this:
|
||||
// prevent absolute path access on Windows, e.g. http://localhost:5000/C:\Windows\notepad.exe
|
||||
// if runtime.GOOS == "windows" && len(reqPath) > 0 && filepath.IsAbs(reqPath[1:]) {
|
||||
// TODO.
|
||||
// }
|
||||
|
||||
// TODO: whereas std lib's http.Dir.Open() uses this:
|
||||
// if filepath.Separator != '/' && strings.ContainsRune(name, filepath.Separator) {
|
||||
// return nil, errors.New("http: invalid character in file path")
|
||||
// }
|
||||
|
||||
// TODO: see https://play.golang.org/p/oh77BiVQFti for another thing to consider
|
||||
|
||||
if root == "" {
|
||||
root = "."
|
||||
}
|
||||
return filepath.Join(root, filepath.FromSlash(path.Clean("/"+reqPath)))
|
||||
}
|
||||
|
||||
// fileHidden returns true if filename is hidden
|
||||
// according to the hide list.
|
||||
func fileHidden(filename string, hide []string) bool {
|
||||
nameOnly := filepath.Base(filename)
|
||||
sep := string(filepath.Separator)
|
||||
|
||||
for _, h := range hide {
|
||||
// assuming h is a glob/shell-like pattern,
|
||||
// use it to compare the whole file path;
|
||||
// but if there is no separator in h, then
|
||||
// just compare against the file's name
|
||||
compare := filename
|
||||
if !strings.Contains(h, sep) {
|
||||
compare = nameOnly
|
||||
}
|
||||
|
||||
hidden, err := filepath.Match(h, compare)
|
||||
if err != nil {
|
||||
// malformed pattern; fallback by checking prefix
|
||||
if strings.HasPrefix(filename, h) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
if hidden {
|
||||
// file name or path matches hide pattern
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// notFound returns a 404 error or, if pass-thru is enabled,
|
||||
// it calls the next handler in the chain.
|
||||
func (fsrv *FileServer) notFound(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
|
||||
if fsrv.PassThru {
|
||||
return next.ServeHTTP(w, r)
|
||||
}
|
||||
return caddyhttp.Error(http.StatusNotFound, nil)
|
||||
}
|
||||
|
||||
// calculateEtag produces a strong etag by default, although, for
|
||||
// efficiency reasons, it does not actually consume the contents
|
||||
// of the file to make a hash of all the bytes. ¯\_(ツ)_/¯
|
||||
// Prefix the etag with "W/" to convert it into a weak etag.
|
||||
// See: https://tools.ietf.org/html/rfc7232#section-2.3
|
||||
func calculateEtag(d os.FileInfo) string {
|
||||
t := strconv.FormatInt(d.ModTime().Unix(), 36)
|
||||
s := strconv.FormatInt(d.Size(), 36)
|
||||
return `"` + t + s + `"`
|
||||
}
|
||||
|
||||
func redirect(w http.ResponseWriter, r *http.Request, to string) error {
|
||||
for strings.HasPrefix(to, "//") {
|
||||
// prevent path-based open redirects
|
||||
to = strings.TrimPrefix(to, "/")
|
||||
}
|
||||
http.Redirect(w, r, to, http.StatusPermanentRedirect)
|
||||
return nil
|
||||
}
|
||||
|
||||
var defaultIndexNames = []string{"index.html", "index.txt"}
|
||||
|
||||
var bufPool = sync.Pool{
|
||||
New: func() interface{} {
|
||||
return new(bytes.Buffer)
|
||||
},
|
||||
}
|
||||
|
||||
const minBackoff, maxBackoff = 2, 5
|
||||
|
||||
// Interface guards
|
||||
var (
|
||||
_ caddy.Provisioner = (*FileServer)(nil)
|
||||
_ caddyhttp.MiddlewareHandler = (*FileServer)(nil)
|
||||
)
|
||||
@@ -1,101 +0,0 @@
|
||||
// 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 fileserver
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSanitizedPathJoin(t *testing.T) {
|
||||
// For easy reference:
|
||||
// %2e = .
|
||||
// %2f = /
|
||||
// %5c = \
|
||||
for i, tc := range []struct {
|
||||
inputRoot string
|
||||
inputPath string
|
||||
expect string
|
||||
}{
|
||||
{
|
||||
inputPath: "",
|
||||
expect: ".",
|
||||
},
|
||||
{
|
||||
inputPath: "/",
|
||||
expect: ".",
|
||||
},
|
||||
{
|
||||
inputPath: "/foo",
|
||||
expect: "foo",
|
||||
},
|
||||
{
|
||||
inputPath: "/foo/bar",
|
||||
expect: filepath.Join("foo", "bar"),
|
||||
},
|
||||
{
|
||||
inputRoot: "/a",
|
||||
inputPath: "/foo/bar",
|
||||
expect: filepath.Join("/", "a", "foo", "bar"),
|
||||
},
|
||||
{
|
||||
inputPath: "/foo/../bar",
|
||||
expect: "bar",
|
||||
},
|
||||
{
|
||||
inputRoot: "/a/b",
|
||||
inputPath: "/foo/../bar",
|
||||
expect: filepath.Join("/", "a", "b", "bar"),
|
||||
},
|
||||
{
|
||||
inputRoot: "/a/b",
|
||||
inputPath: "/..%2fbar",
|
||||
expect: filepath.Join("/", "a", "b", "bar"),
|
||||
},
|
||||
{
|
||||
inputRoot: "/a/b",
|
||||
inputPath: "/%2e%2e%2fbar",
|
||||
expect: filepath.Join("/", "a", "b", "bar"),
|
||||
},
|
||||
{
|
||||
inputRoot: "/a/b",
|
||||
inputPath: "/%2e%2e%2f%2e%2e%2f",
|
||||
expect: filepath.Join("/", "a", "b"),
|
||||
},
|
||||
{
|
||||
inputRoot: "C:\\www",
|
||||
inputPath: "/foo/bar",
|
||||
expect: filepath.Join("C:\\www", "foo", "bar"),
|
||||
},
|
||||
// TODO: test more windows paths... on windows... sigh.
|
||||
} {
|
||||
// we don't *need* to use an actual parsed URL, but it
|
||||
// adds some authenticity to the tests since real-world
|
||||
// values will be coming in from URLs; thus, the test
|
||||
// corpus can contain paths as encoded by clients, which
|
||||
// more closely emulates the actual attack vector
|
||||
u, err := url.Parse("http://test:9999" + tc.inputPath)
|
||||
if err != nil {
|
||||
t.Fatalf("Test %d: invalid URL: %v", i, err)
|
||||
}
|
||||
actual := sanitizedPathJoin(tc.inputRoot, u.Path)
|
||||
if actual != tc.expect {
|
||||
t.Errorf("Test %d: [%s %s] => %s (expected %s)", i, tc.inputRoot, tc.inputPath, actual, tc.expect)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: test fileHidden
|
||||
@@ -1,173 +0,0 @@
|
||||
// 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"
|
||||
|
||||
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
|
||||
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
|
||||
)
|
||||
|
||||
func init() {
|
||||
httpcaddyfile.RegisterHandlerDirective("header", parseCaddyfile)
|
||||
httpcaddyfile.RegisterHandlerDirective("request_header", parseReqHdrCaddyfile)
|
||||
}
|
||||
|
||||
// parseCaddyfile sets up the handler for response headers from
|
||||
// Caddyfile tokens. Syntax:
|
||||
//
|
||||
// header [<matcher>] [[+|-]<field> [<value|regexp>] [<replacement>]] {
|
||||
// [+]<field> [<value|regexp> [<replacement>]]
|
||||
// -<field>
|
||||
// [defer]
|
||||
// }
|
||||
//
|
||||
// Either a block can be opened or a single header field can be configured
|
||||
// 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.
|
||||
func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
|
||||
hdr := new(Handler)
|
||||
|
||||
makeResponseOps := func() {
|
||||
if hdr.Response == nil {
|
||||
hdr.Response = &RespHeaderOps{
|
||||
HeaderOps: new(HeaderOps),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for h.Next() {
|
||||
// first see if headers are in the initial line
|
||||
var hasArgs bool
|
||||
if h.NextArg() {
|
||||
hasArgs = true
|
||||
field := h.Val()
|
||||
var value, replacement string
|
||||
if h.NextArg() {
|
||||
value = h.Val()
|
||||
}
|
||||
if h.NextArg() {
|
||||
replacement = h.Val()
|
||||
}
|
||||
makeResponseOps()
|
||||
CaddyfileHeaderOp(hdr.Response.HeaderOps, field, value, replacement)
|
||||
if len(hdr.Response.HeaderOps.Delete) > 0 {
|
||||
hdr.Response.Deferred = true
|
||||
}
|
||||
}
|
||||
|
||||
// if not, they should be in a block
|
||||
for h.NextBlock(0) {
|
||||
field := h.Val()
|
||||
if field == "defer" {
|
||||
hdr.Response.Deferred = true
|
||||
continue
|
||||
}
|
||||
if hasArgs {
|
||||
return nil, h.Err("cannot specify headers in both arguments and block")
|
||||
}
|
||||
var value, replacement string
|
||||
if h.NextArg() {
|
||||
value = h.Val()
|
||||
}
|
||||
if h.NextArg() {
|
||||
replacement = h.Val()
|
||||
}
|
||||
makeResponseOps()
|
||||
CaddyfileHeaderOp(hdr.Response.HeaderOps, field, value, replacement)
|
||||
if len(hdr.Response.HeaderOps.Delete) > 0 {
|
||||
hdr.Response.Deferred = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return hdr, nil
|
||||
}
|
||||
|
||||
// parseReqHdrCaddyfile sets up the handler for request headers
|
||||
// from Caddyfile tokens. Syntax:
|
||||
//
|
||||
// request_header [<matcher>] [[+|-]<field> [<value|regexp>] [<replacement>]]
|
||||
//
|
||||
func parseReqHdrCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
|
||||
hdr := new(Handler)
|
||||
for h.Next() {
|
||||
if !h.NextArg() {
|
||||
return nil, h.ArgErr()
|
||||
}
|
||||
field := h.Val()
|
||||
var value, replacement string
|
||||
if h.NextArg() {
|
||||
value = h.Val()
|
||||
}
|
||||
if h.NextArg() {
|
||||
replacement = h.Val()
|
||||
if h.NextArg() {
|
||||
return nil, h.ArgErr()
|
||||
}
|
||||
}
|
||||
|
||||
if hdr.Request == nil {
|
||||
hdr.Request = new(HeaderOps)
|
||||
}
|
||||
CaddyfileHeaderOp(hdr.Request, field, value, replacement)
|
||||
|
||||
if h.NextArg() {
|
||||
return nil, h.ArgErr()
|
||||
}
|
||||
}
|
||||
return hdr, nil
|
||||
}
|
||||
|
||||
// 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) {
|
||||
if strings.HasPrefix(field, "+") {
|
||||
if ops.Add == nil {
|
||||
ops.Add = make(http.Header)
|
||||
}
|
||||
ops.Add.Set(field[1:], value)
|
||||
} else if strings.HasPrefix(field, "-") {
|
||||
ops.Delete = append(ops.Delete, field[1:])
|
||||
} else {
|
||||
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,
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,321 +0,0 @@
|
||||
// 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 (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/caddyserver/caddy/v2"
|
||||
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
|
||||
)
|
||||
|
||||
func init() {
|
||||
caddy.RegisterModule(Handler{})
|
||||
}
|
||||
|
||||
// Handler is a middleware which modifies request and response headers.
|
||||
//
|
||||
// Changes to headers are applied immediately, except for the response
|
||||
// headers when Deferred is true or when Required is set. In those cases,
|
||||
// the changes are applied when the headers are written to the response.
|
||||
// Note that deferred changes do not take effect if an error occurs later
|
||||
// in the middleware chain.
|
||||
//
|
||||
// Properties in this module accept placeholders.
|
||||
//
|
||||
// Response header operations can be conditioned upon response status code
|
||||
// and/or other header values.
|
||||
type Handler struct {
|
||||
Request *HeaderOps `json:"request,omitempty"`
|
||||
Response *RespHeaderOps `json:"response,omitempty"`
|
||||
}
|
||||
|
||||
// CaddyModule returns the Caddy module information.
|
||||
func (Handler) CaddyModule() caddy.ModuleInfo {
|
||||
return caddy.ModuleInfo{
|
||||
ID: "http.handlers.headers",
|
||||
New: func() caddy.Module { return new(Handler) },
|
||||
}
|
||||
}
|
||||
|
||||
// Provision sets up h's configuration.
|
||||
func (h *Handler) Provision(_ caddy.Context) error {
|
||||
if h.Request != nil {
|
||||
err := h.Request.provision()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if h.Response != nil {
|
||||
err := h.Response.provision()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Validate ensures h's configuration is valid.
|
||||
func (h Handler) Validate() error {
|
||||
if h.Request != nil {
|
||||
err := h.Request.validate()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if h.Response != nil {
|
||||
err := h.Response.validate()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
|
||||
repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
|
||||
|
||||
if h.Request != nil {
|
||||
h.Request.ApplyToRequest(r)
|
||||
}
|
||||
|
||||
if h.Response != nil {
|
||||
if h.Response.Deferred || h.Response.Require != nil {
|
||||
w = &responseWriterWrapper{
|
||||
ResponseWriterWrapper: &caddyhttp.ResponseWriterWrapper{ResponseWriter: w},
|
||||
replacer: repl,
|
||||
require: h.Response.Require,
|
||||
headerOps: h.Response.HeaderOps,
|
||||
}
|
||||
} else {
|
||||
h.Response.ApplyTo(w.Header(), repl)
|
||||
}
|
||||
}
|
||||
|
||||
return next.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
// HeaderOps defines manipulations for HTTP headers.
|
||||
type HeaderOps struct {
|
||||
// Adds HTTP headers; does not replace any existing header fields.
|
||||
Add http.Header `json:"add,omitempty"`
|
||||
|
||||
// Sets HTTP headers; replaces existing header fields.
|
||||
Set http.Header `json:"set,omitempty"`
|
||||
|
||||
// Names of HTTP header fields to delete.
|
||||
Delete []string `json:"delete,omitempty"`
|
||||
|
||||
// Performs substring replacements of HTTP headers in-situ.
|
||||
Replace map[string][]Replacement `json:"replace,omitempty"`
|
||||
}
|
||||
|
||||
func (ops *HeaderOps) provision() error {
|
||||
for fieldName, replacements := range ops.Replace {
|
||||
for i, r := range replacements {
|
||||
if r.SearchRegexp != "" {
|
||||
re, err := regexp.Compile(r.SearchRegexp)
|
||||
if err != nil {
|
||||
return fmt.Errorf("replacement %d for header field '%s': %v", i, fieldName, err)
|
||||
}
|
||||
replacements[i].re = re
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ops HeaderOps) validate() error {
|
||||
for fieldName, replacements := range ops.Replace {
|
||||
for _, r := range replacements {
|
||||
if r.Search != "" && r.SearchRegexp != "" {
|
||||
return fmt.Errorf("cannot specify both a substring search and a regular expression search for field '%s'", fieldName)
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Replacement describes a string replacement,
|
||||
// either a simple and fast substring search
|
||||
// or a slower but more powerful regex search.
|
||||
type Replacement struct {
|
||||
// The substring to search for.
|
||||
Search string `json:"search,omitempty"`
|
||||
|
||||
// The regular expression to search with.
|
||||
SearchRegexp string `json:"search_regexp,omitempty"`
|
||||
|
||||
// The string with which to replace matches.
|
||||
Replace string `json:"replace,omitempty"`
|
||||
|
||||
re *regexp.Regexp
|
||||
}
|
||||
|
||||
// RespHeaderOps defines manipulations for response headers.
|
||||
type RespHeaderOps struct {
|
||||
*HeaderOps
|
||||
|
||||
// If set, header operations will be deferred until
|
||||
// they are written out and only performed if the
|
||||
// response matches these criteria.
|
||||
Require *caddyhttp.ResponseMatcher `json:"require,omitempty"`
|
||||
|
||||
// If true, header operations will be deferred until
|
||||
// they are written out. Superceded if Require is set.
|
||||
// Usually you will need to set this to true if any
|
||||
// fields are being deleted.
|
||||
Deferred bool `json:"deferred,omitempty"`
|
||||
}
|
||||
|
||||
// ApplyTo applies ops to hdr using repl.
|
||||
func (ops HeaderOps) ApplyTo(hdr http.Header, repl *caddy.Replacer) {
|
||||
// add
|
||||
for fieldName, vals := range ops.Add {
|
||||
fieldName = repl.ReplaceAll(fieldName, "")
|
||||
for _, v := range vals {
|
||||
hdr.Add(fieldName, repl.ReplaceAll(v, ""))
|
||||
}
|
||||
}
|
||||
|
||||
// set
|
||||
for fieldName, vals := range ops.Set {
|
||||
fieldName = repl.ReplaceAll(fieldName, "")
|
||||
var newVals []string
|
||||
for i := range vals {
|
||||
// append to new slice so we don't overwrite
|
||||
// the original values in ops.Set
|
||||
newVals = append(newVals, repl.ReplaceAll(vals[i], ""))
|
||||
}
|
||||
hdr.Set(fieldName, strings.Join(newVals, ","))
|
||||
}
|
||||
|
||||
// delete
|
||||
for _, fieldName := range ops.Delete {
|
||||
hdr.Del(repl.ReplaceAll(fieldName, ""))
|
||||
}
|
||||
|
||||
// replace
|
||||
for fieldName, replacements := range ops.Replace {
|
||||
fieldName = repl.ReplaceAll(fieldName, "")
|
||||
|
||||
// all fields...
|
||||
if fieldName == "*" {
|
||||
for _, r := range replacements {
|
||||
search := repl.ReplaceAll(r.Search, "")
|
||||
replace := repl.ReplaceAll(r.Replace, "")
|
||||
for fieldName, vals := range hdr {
|
||||
for i := range vals {
|
||||
if r.re != nil {
|
||||
hdr[fieldName][i] = r.re.ReplaceAllString(hdr[fieldName][i], replace)
|
||||
} else {
|
||||
hdr[fieldName][i] = strings.ReplaceAll(hdr[fieldName][i], search, replace)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
// ...or only with the named field
|
||||
for _, r := range replacements {
|
||||
search := repl.ReplaceAll(r.Search, "")
|
||||
replace := repl.ReplaceAll(r.Replace, "")
|
||||
for i := range hdr[fieldName] {
|
||||
if r.re != nil {
|
||||
hdr[fieldName][i] = r.re.ReplaceAllString(hdr[fieldName][i], replace)
|
||||
} else {
|
||||
hdr[fieldName][i] = strings.ReplaceAll(hdr[fieldName][i], search, replace)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ApplyToRequest applies ops to r, specially handling the Host
|
||||
// header which the standard library does not include with the
|
||||
// header map with all the others. This method mutates r.Host.
|
||||
func (ops HeaderOps) ApplyToRequest(r *http.Request) {
|
||||
repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
|
||||
|
||||
// capture the current Host header so we can
|
||||
// reset to it when we're done
|
||||
origHost, hadHost := r.Header["Host"]
|
||||
|
||||
// append r.Host; this way, we know that our value
|
||||
// was last in the list, and if an Add operation
|
||||
// appended something else after it, that's probably
|
||||
// fine because it's weird to have multiple Host
|
||||
// headers anyway and presumably the one they added
|
||||
// is the one they wanted
|
||||
r.Header["Host"] = append(r.Header["Host"], r.Host)
|
||||
|
||||
// apply header operations
|
||||
ops.ApplyTo(r.Header, repl)
|
||||
|
||||
// retrieve the last Host value (likely the one we appended)
|
||||
if len(r.Header["Host"]) > 0 {
|
||||
r.Host = r.Header["Host"][len(r.Header["Host"])-1]
|
||||
} else {
|
||||
r.Host = ""
|
||||
}
|
||||
|
||||
// reset the Host header slice
|
||||
if hadHost {
|
||||
r.Header["Host"] = origHost
|
||||
} else {
|
||||
delete(r.Header, "Host")
|
||||
}
|
||||
}
|
||||
|
||||
// responseWriterWrapper defers response header
|
||||
// operations until WriteHeader is called.
|
||||
type responseWriterWrapper struct {
|
||||
*caddyhttp.ResponseWriterWrapper
|
||||
replacer *caddy.Replacer
|
||||
require *caddyhttp.ResponseMatcher
|
||||
headerOps *HeaderOps
|
||||
wroteHeader bool
|
||||
}
|
||||
|
||||
func (rww *responseWriterWrapper) WriteHeader(status int) {
|
||||
if rww.wroteHeader {
|
||||
return
|
||||
}
|
||||
rww.wroteHeader = true
|
||||
if rww.require == nil || rww.require.Match(status, rww.ResponseWriterWrapper.Header()) {
|
||||
if rww.headerOps != nil {
|
||||
rww.headerOps.ApplyTo(rww.ResponseWriterWrapper.Header(), rww.replacer)
|
||||
}
|
||||
}
|
||||
rww.ResponseWriterWrapper.WriteHeader(status)
|
||||
}
|
||||
|
||||
func (rww *responseWriterWrapper) Write(d []byte) (int, error) {
|
||||
if !rww.wroteHeader {
|
||||
rww.WriteHeader(http.StatusOK)
|
||||
}
|
||||
return rww.ResponseWriterWrapper.Write(d)
|
||||
}
|
||||
|
||||
// Interface guards
|
||||
var (
|
||||
_ caddy.Provisioner = (*Handler)(nil)
|
||||
_ caddyhttp.MiddlewareHandler = (*Handler)(nil)
|
||||
_ caddyhttp.HTTPInterfaces = (*responseWriterWrapper)(nil)
|
||||
)
|
||||
@@ -1,21 +0,0 @@
|
||||
// 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 "testing"
|
||||
|
||||
func TestReqHeaders(t *testing.T) {
|
||||
// TODO: write tests
|
||||
}
|
||||
@@ -1,242 +0,0 @@
|
||||
// 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 httpcache
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/gob"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"sync"
|
||||
|
||||
"github.com/caddyserver/caddy/v2"
|
||||
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
|
||||
"github.com/golang/groupcache"
|
||||
)
|
||||
|
||||
func init() {
|
||||
caddy.RegisterModule(Cache{})
|
||||
}
|
||||
|
||||
// Cache implements a simple distributed cache.
|
||||
//
|
||||
// NOTE: This module is a work-in-progress. It is
|
||||
// not finished and is NOT ready for production use.
|
||||
// [We need your help to finish it! Please volunteer
|
||||
// in this issue.](https://github.com/caddyserver/caddy/issues/2820)
|
||||
// Until it is finished, this module is subject to
|
||||
// breaking changes.
|
||||
//
|
||||
// Caches only GET and HEAD requests. Honors the Cache-Control: no-cache header.
|
||||
//
|
||||
// Still TODO:
|
||||
//
|
||||
// - Eviction policies and API
|
||||
// - Use single cache per-process
|
||||
// - Preserve cache through config reloads
|
||||
// - More control over what gets cached
|
||||
type Cache struct {
|
||||
// The network address of this cache instance; required.
|
||||
Self string `json:"self,omitempty"`
|
||||
|
||||
// A list of network addresses of cache instances in the group.
|
||||
Peers []string `json:"peers,omitempty"`
|
||||
|
||||
// Maximum size of the cache, in bytes. Default is 512 MB.
|
||||
MaxSize int64 `json:"max_size,omitempty"`
|
||||
|
||||
group *groupcache.Group
|
||||
}
|
||||
|
||||
// CaddyModule returns the Caddy module information.
|
||||
func (Cache) CaddyModule() caddy.ModuleInfo {
|
||||
return caddy.ModuleInfo{
|
||||
ID: "http.handlers.cache",
|
||||
New: func() caddy.Module { return new(Cache) },
|
||||
}
|
||||
}
|
||||
|
||||
// Provision provisions c.
|
||||
func (c *Cache) Provision(ctx caddy.Context) error {
|
||||
// TODO: use UsagePool so that cache survives config reloads - TODO: a single cache for whole process?
|
||||
maxSize := c.MaxSize
|
||||
if maxSize == 0 {
|
||||
const maxMB = 512
|
||||
maxSize = int64(maxMB << 20)
|
||||
}
|
||||
poolMu.Lock()
|
||||
if pool == nil {
|
||||
pool = groupcache.NewHTTPPool(c.Self)
|
||||
c.group = groupcache.NewGroup(groupName, maxSize, groupcache.GetterFunc(c.getter))
|
||||
} else {
|
||||
c.group = groupcache.GetGroup(groupName)
|
||||
}
|
||||
pool.Set(append(c.Peers, c.Self)...)
|
||||
poolMu.Unlock()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Validate validates c.
|
||||
func (c *Cache) Validate() error {
|
||||
if c.Self == "" {
|
||||
return fmt.Errorf("address of this instance (self) is required")
|
||||
}
|
||||
if c.MaxSize < 0 {
|
||||
return fmt.Errorf("size must be greater than 0")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Cache) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
|
||||
// TODO: proper RFC implementation of cache control headers...
|
||||
if r.Header.Get("Cache-Control") == "no-cache" || (r.Method != "GET" && r.Method != "HEAD") {
|
||||
return next.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
getterCtx := getterContext{w, r, next}
|
||||
ctx := context.WithValue(r.Context(), getterContextCtxKey, getterCtx)
|
||||
|
||||
// TODO: rigorous performance testing
|
||||
|
||||
// TODO: pretty much everything else to handle the nuances of HTTP caching...
|
||||
|
||||
// TODO: groupcache has no explicit cache eviction, so we need to embed
|
||||
// all information related to expiring cache entries into the key; right
|
||||
// now we just use the request URI as a proof-of-concept
|
||||
key := r.RequestURI
|
||||
|
||||
var cachedBytes []byte
|
||||
err := c.group.Get(ctx, key, groupcache.AllocatingByteSliceSink(&cachedBytes))
|
||||
if err == errUncacheable {
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// the cached bytes consists of two parts: first a
|
||||
// gob encoding of the status and header, immediately
|
||||
// followed by the raw bytes of the response body
|
||||
rdr := bytes.NewReader(cachedBytes)
|
||||
|
||||
// read the header and status first
|
||||
var hs headerAndStatus
|
||||
err = gob.NewDecoder(rdr).Decode(&hs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// set and write the cached headers
|
||||
for k, v := range hs.Header {
|
||||
w.Header()[k] = v
|
||||
}
|
||||
w.WriteHeader(hs.Status)
|
||||
|
||||
// write the cached response body
|
||||
io.Copy(w, rdr)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Cache) getter(ctx context.Context, key string, dest groupcache.Sink) error {
|
||||
combo := ctx.Value(getterContextCtxKey).(getterContext)
|
||||
|
||||
// the buffer will store the gob-encoded header, then the body
|
||||
buf := bufPool.Get().(*bytes.Buffer)
|
||||
buf.Reset()
|
||||
defer bufPool.Put(buf)
|
||||
|
||||
// we need to record the response if we are to cache it; only cache if
|
||||
// request is successful (TODO: there's probably much more nuance needed here)
|
||||
rr := caddyhttp.NewResponseRecorder(combo.rw, buf, func(status int, header http.Header) bool {
|
||||
shouldBuf := status < 300
|
||||
|
||||
if shouldBuf {
|
||||
// store the header before the body, so we can efficiently
|
||||
// and conveniently use a single buffer for both; gob
|
||||
// decoder will only read up to end of gob message, and
|
||||
// the rest will be the body, which will be written
|
||||
// implicitly for us by the recorder
|
||||
err := gob.NewEncoder(buf).Encode(headerAndStatus{
|
||||
Header: header,
|
||||
Status: status,
|
||||
})
|
||||
if err != nil {
|
||||
log.Printf("[ERROR] Encoding headers for cache entry: %v; not caching this request", err)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return shouldBuf
|
||||
})
|
||||
|
||||
// execute next handlers in chain
|
||||
err := combo.next.ServeHTTP(rr, combo.req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// if response body was not buffered, response was
|
||||
// already written and we are unable to cache
|
||||
if !rr.Buffered() {
|
||||
return errUncacheable
|
||||
}
|
||||
|
||||
// add to cache
|
||||
dest.SetBytes(buf.Bytes())
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type headerAndStatus struct {
|
||||
Header http.Header
|
||||
Status int
|
||||
}
|
||||
|
||||
type getterContext struct {
|
||||
rw http.ResponseWriter
|
||||
req *http.Request
|
||||
next caddyhttp.Handler
|
||||
}
|
||||
|
||||
var bufPool = sync.Pool{
|
||||
New: func() interface{} {
|
||||
return new(bytes.Buffer)
|
||||
},
|
||||
}
|
||||
|
||||
var (
|
||||
pool *groupcache.HTTPPool
|
||||
poolMu sync.Mutex
|
||||
)
|
||||
|
||||
var errUncacheable = fmt.Errorf("uncacheable")
|
||||
|
||||
const groupName = "http_requests"
|
||||
|
||||
type ctxKey string
|
||||
|
||||
const getterContextCtxKey ctxKey = "getter_context"
|
||||
|
||||
// Interface guards
|
||||
var (
|
||||
_ caddy.Provisioner = (*Cache)(nil)
|
||||
_ caddy.Validator = (*Cache)(nil)
|
||||
_ caddyhttp.MiddlewareHandler = (*Cache)(nil)
|
||||
)
|
||||
@@ -1,89 +0,0 @@
|
||||
// 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 caddyhttp
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"net/http"
|
||||
|
||||
"go.uber.org/zap/zapcore"
|
||||
)
|
||||
|
||||
// LoggableHTTPRequest makes an HTTP request loggable with zap.Object().
|
||||
type LoggableHTTPRequest struct{ *http.Request }
|
||||
|
||||
// MarshalLogObject satisfies the zapcore.ObjectMarshaler interface.
|
||||
func (r LoggableHTTPRequest) MarshalLogObject(enc zapcore.ObjectEncoder) error {
|
||||
enc.AddString("method", r.Method)
|
||||
enc.AddString("uri", r.RequestURI)
|
||||
enc.AddString("proto", r.Proto)
|
||||
enc.AddString("remote_addr", r.RemoteAddr)
|
||||
enc.AddString("host", r.Host)
|
||||
enc.AddObject("headers", LoggableHTTPHeader(r.Header))
|
||||
if r.TLS != nil {
|
||||
enc.AddObject("tls", LoggableTLSConnState(*r.TLS))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// LoggableHTTPHeader makes an HTTP header loggable with zap.Object().
|
||||
type LoggableHTTPHeader http.Header
|
||||
|
||||
// MarshalLogObject satisfies the zapcore.ObjectMarshaler interface.
|
||||
func (h LoggableHTTPHeader) MarshalLogObject(enc zapcore.ObjectEncoder) error {
|
||||
if h == nil {
|
||||
return nil
|
||||
}
|
||||
for key, val := range h {
|
||||
enc.AddArray(key, LoggableStringArray(val))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// LoggableStringArray makes a slice of strings marshalable for logging.
|
||||
type LoggableStringArray []string
|
||||
|
||||
// MarshalLogArray satisfies the zapcore.ArrayMarshaler interface.
|
||||
func (sa LoggableStringArray) MarshalLogArray(enc zapcore.ArrayEncoder) error {
|
||||
if sa == nil {
|
||||
return nil
|
||||
}
|
||||
for _, s := range sa {
|
||||
enc.AppendString(s)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// LoggableTLSConnState makes a TLS connection state loggable with zap.Object().
|
||||
type LoggableTLSConnState tls.ConnectionState
|
||||
|
||||
// MarshalLogObject satisfies the zapcore.ObjectMarshaler interface.
|
||||
func (t LoggableTLSConnState) MarshalLogObject(enc zapcore.ObjectEncoder) error {
|
||||
enc.AddBool("resumed", t.DidResume)
|
||||
enc.AddUint16("version", t.Version)
|
||||
enc.AddUint16("ciphersuite", t.CipherSuite)
|
||||
enc.AddString("proto", t.NegotiatedProtocol)
|
||||
enc.AddBool("proto_mutual", t.NegotiatedProtocolIsMutual)
|
||||
enc.AddString("server_name", t.ServerName)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Interface guards
|
||||
var (
|
||||
_ zapcore.ObjectMarshaler = (*LoggableHTTPRequest)(nil)
|
||||
_ zapcore.ObjectMarshaler = (*LoggableHTTPHeader)(nil)
|
||||
_ zapcore.ArrayMarshaler = (*LoggableStringArray)(nil)
|
||||
_ zapcore.ObjectMarshaler = (*LoggableTLSConnState)(nil)
|
||||
)
|
||||
@@ -1,870 +0,0 @@
|
||||
// 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 caddyhttp
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/textproto"
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/caddyserver/caddy/v2"
|
||||
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
|
||||
)
|
||||
|
||||
type (
|
||||
// MatchHost matches requests by the Host value (case-insensitive).
|
||||
//
|
||||
// When used in a top-level HTTP route,
|
||||
// [qualifying domain names](/docs/automatic-https#hostname-requirements)
|
||||
// may trigger [automatic HTTPS](/docs/automatic-https), which automatically
|
||||
// provisions and renews certificates for you. Before doing this, you
|
||||
// should ensure that DNS records for these domains are properly configured,
|
||||
// especially A/AAAA pointed at your server.
|
||||
//
|
||||
// Automatic HTTPS can be
|
||||
// [customized or disabled](/docs/json/apps/http/servers/automatic_https/).
|
||||
MatchHost []string
|
||||
|
||||
// MatchPath matches requests by the URI's path (case-insensitive). Path
|
||||
// matches are exact, but wildcards may be used:
|
||||
//
|
||||
// - At the end, for a prefix match (`/prefix/*`)
|
||||
// - At the beginning, for a suffix match (`*.suffix`)
|
||||
// - On both sides, for a substring match (`*/contains/*`)
|
||||
// - In the middle, for a globular match (`/accounts/*/info`)
|
||||
//
|
||||
// This matcher is fast, so it does not support regular expressions or
|
||||
// capture groups. For slower but more powerful matching, use the
|
||||
// path_regexp matcher.
|
||||
MatchPath []string
|
||||
|
||||
// MatchPathRE matches requests by a regular expression on the URI's path.
|
||||
//
|
||||
// Upon a match, it adds placeholders to the request: `{http.regexp.name.capture_group}`
|
||||
// where `name` is the regular expression's name, and `capture_group` is either
|
||||
// the named or positional capture group from the expression itself. If no name
|
||||
// is given, then the placeholder omits the name: `{http.regexp.capture_group}`
|
||||
// (potentially leading to collisions).
|
||||
MatchPathRE struct{ MatchRegexp }
|
||||
|
||||
// MatchMethod matches requests by the method.
|
||||
MatchMethod []string
|
||||
|
||||
// MatchQuery matches requests by URI's query string.
|
||||
MatchQuery url.Values
|
||||
|
||||
// MatchHeader matches requests by header fields. It performs fast,
|
||||
// exact string comparisons of the field values. Fast prefix, suffix,
|
||||
// and substring matches can also be done by suffixing, prefixing, or
|
||||
// surrounding the value with the wildcard `*` character, respectively.
|
||||
// If a list is null, the header must not exist. If the list is empty,
|
||||
// the field must simply exist, regardless of its value.
|
||||
MatchHeader http.Header
|
||||
|
||||
// MatchHeaderRE matches requests by a regular expression on header fields.
|
||||
//
|
||||
// Upon a match, it adds placeholders to the request: `{http.regexp.name.capture_group}`
|
||||
// where `name` is the regular expression's name, and `capture_group` is either
|
||||
// the named or positional capture group from the expression itself. If no name
|
||||
// is given, then the placeholder omits the name: `{http.regexp.capture_group}`
|
||||
// (potentially leading to collisions).
|
||||
MatchHeaderRE map[string]*MatchRegexp
|
||||
|
||||
// MatchProtocol matches requests by protocol.
|
||||
MatchProtocol string
|
||||
|
||||
// MatchRemoteIP matches requests by client IP (or CIDR range).
|
||||
MatchRemoteIP struct {
|
||||
Ranges []string `json:"ranges,omitempty"`
|
||||
|
||||
cidrs []*net.IPNet
|
||||
}
|
||||
|
||||
// MatchNegate matches requests by negating its matchers' results.
|
||||
// To use, simply specify a set of matchers like you normally would;
|
||||
// the only difference is that their result will be negated.
|
||||
MatchNegate struct {
|
||||
MatchersRaw caddy.ModuleMap `json:"-" caddy:"namespace=http.matchers"`
|
||||
|
||||
Matchers MatcherSet `json:"-"`
|
||||
}
|
||||
)
|
||||
|
||||
func init() {
|
||||
caddy.RegisterModule(MatchHost{})
|
||||
caddy.RegisterModule(MatchPath{})
|
||||
caddy.RegisterModule(MatchPathRE{})
|
||||
caddy.RegisterModule(MatchMethod{})
|
||||
caddy.RegisterModule(MatchQuery{})
|
||||
caddy.RegisterModule(MatchHeader{})
|
||||
caddy.RegisterModule(MatchHeaderRE{})
|
||||
caddy.RegisterModule(new(MatchProtocol))
|
||||
caddy.RegisterModule(MatchRemoteIP{})
|
||||
caddy.RegisterModule(MatchNegate{})
|
||||
}
|
||||
|
||||
// CaddyModule returns the Caddy module information.
|
||||
func (MatchHost) CaddyModule() caddy.ModuleInfo {
|
||||
return caddy.ModuleInfo{
|
||||
ID: "http.matchers.host",
|
||||
New: func() caddy.Module { return new(MatchHost) },
|
||||
}
|
||||
}
|
||||
|
||||
// UnmarshalCaddyfile implements caddyfile.Unmarshaler.
|
||||
func (m *MatchHost) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
|
||||
for d.Next() {
|
||||
*m = append(*m, d.RemainingArgs()...)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Match returns true if r matches m.
|
||||
func (m MatchHost) Match(r *http.Request) bool {
|
||||
reqHost, _, err := net.SplitHostPort(r.Host)
|
||||
if err != nil {
|
||||
// OK; probably didn't have a port
|
||||
reqHost = r.Host
|
||||
|
||||
// make sure we strip the brackets from IPv6 addresses
|
||||
reqHost = strings.TrimPrefix(reqHost, "[")
|
||||
reqHost = strings.TrimSuffix(reqHost, "]")
|
||||
}
|
||||
|
||||
repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
|
||||
|
||||
outer:
|
||||
for _, host := range m {
|
||||
host = repl.ReplaceAll(host, "")
|
||||
if strings.Contains(host, "*") {
|
||||
patternParts := strings.Split(host, ".")
|
||||
incomingParts := strings.Split(reqHost, ".")
|
||||
if len(patternParts) != len(incomingParts) {
|
||||
continue
|
||||
}
|
||||
for i := range patternParts {
|
||||
if patternParts[i] == "*" {
|
||||
continue
|
||||
}
|
||||
if !strings.EqualFold(patternParts[i], incomingParts[i]) {
|
||||
continue outer
|
||||
}
|
||||
}
|
||||
return true
|
||||
} else if strings.EqualFold(reqHost, host) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// CaddyModule returns the Caddy module information.
|
||||
func (MatchPath) CaddyModule() caddy.ModuleInfo {
|
||||
return caddy.ModuleInfo{
|
||||
ID: "http.matchers.path",
|
||||
New: func() caddy.Module { return new(MatchPath) },
|
||||
}
|
||||
}
|
||||
|
||||
// Provision lower-cases the paths in m to ensure case-insensitive matching.
|
||||
func (m MatchPath) Provision(_ caddy.Context) error {
|
||||
for i := range m {
|
||||
m[i] = strings.ToLower(m[i])
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Match returns true if r matches m.
|
||||
func (m MatchPath) Match(r *http.Request) bool {
|
||||
lowerPath := strings.ToLower(r.URL.Path)
|
||||
|
||||
// see #2917; Windows ignores trailing dots and spaces
|
||||
// when accessing files (sigh), potentially causing a
|
||||
// security risk (cry) if PHP files end up being served
|
||||
// as static files, exposing the source code, instead of
|
||||
// being matched by *.php to be treated as PHP scripts
|
||||
lowerPath = strings.TrimRight(lowerPath, ". ")
|
||||
|
||||
repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
|
||||
|
||||
for _, matchPath := range m {
|
||||
matchPath = repl.ReplaceAll(matchPath, "")
|
||||
|
||||
// special case: whole path is wildcard; this is unnecessary
|
||||
// as it matches all requests, which is the same as no matcher
|
||||
if matchPath == "*" {
|
||||
return true
|
||||
}
|
||||
|
||||
// special case: first and last characters are wildcard,
|
||||
// treat it as a fast substring match
|
||||
if len(matchPath) > 1 &&
|
||||
strings.HasPrefix(matchPath, "*") &&
|
||||
strings.HasSuffix(matchPath, "*") {
|
||||
if strings.Contains(lowerPath, matchPath[1:len(matchPath)-1]) {
|
||||
return true
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
// special case: first character is a wildcard,
|
||||
// treat it as a fast suffix match
|
||||
if strings.HasPrefix(matchPath, "*") {
|
||||
if strings.HasSuffix(lowerPath, matchPath[1:]) {
|
||||
return true
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
// special case: last character is a wildcard,
|
||||
// treat it as a fast prefix match
|
||||
if strings.HasSuffix(matchPath, "*") {
|
||||
if strings.HasPrefix(lowerPath, matchPath[:len(matchPath)-1]) {
|
||||
return true
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
// for everything else, try globular matching, which also
|
||||
// is exact matching if there are no glob/wildcard chars;
|
||||
// can ignore error here because we can't handle it anyway
|
||||
matches, _ := filepath.Match(matchPath, lowerPath)
|
||||
if matches {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// UnmarshalCaddyfile implements caddyfile.Unmarshaler.
|
||||
func (m *MatchPath) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
|
||||
for d.Next() {
|
||||
*m = append(*m, d.RemainingArgs()...)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// CaddyModule returns the Caddy module information.
|
||||
func (MatchPathRE) CaddyModule() caddy.ModuleInfo {
|
||||
return caddy.ModuleInfo{
|
||||
ID: "http.matchers.path_regexp",
|
||||
New: func() caddy.Module { return new(MatchPathRE) },
|
||||
}
|
||||
}
|
||||
|
||||
// Match returns true if r matches m.
|
||||
func (m MatchPathRE) Match(r *http.Request) bool {
|
||||
repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
|
||||
return m.MatchRegexp.Match(r.URL.Path, repl)
|
||||
}
|
||||
|
||||
// CaddyModule returns the Caddy module information.
|
||||
func (MatchMethod) CaddyModule() caddy.ModuleInfo {
|
||||
return caddy.ModuleInfo{
|
||||
ID: "http.matchers.method",
|
||||
New: func() caddy.Module { return new(MatchMethod) },
|
||||
}
|
||||
}
|
||||
|
||||
// UnmarshalCaddyfile implements caddyfile.Unmarshaler.
|
||||
func (m *MatchMethod) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
|
||||
for d.Next() {
|
||||
*m = append(*m, d.RemainingArgs()...)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Match returns true if r matches m.
|
||||
func (m MatchMethod) Match(r *http.Request) bool {
|
||||
for _, method := range m {
|
||||
if r.Method == method {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// CaddyModule returns the Caddy module information.
|
||||
func (MatchQuery) CaddyModule() caddy.ModuleInfo {
|
||||
return caddy.ModuleInfo{
|
||||
ID: "http.matchers.query",
|
||||
New: func() caddy.Module { return new(MatchQuery) },
|
||||
}
|
||||
}
|
||||
|
||||
// UnmarshalCaddyfile implements caddyfile.Unmarshaler.
|
||||
func (m *MatchQuery) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
|
||||
if *m == nil {
|
||||
*m = make(map[string][]string)
|
||||
}
|
||||
|
||||
for d.Next() {
|
||||
var query string
|
||||
if !d.Args(&query) {
|
||||
return d.ArgErr()
|
||||
}
|
||||
parts := strings.SplitN(query, "=", 2)
|
||||
if len(parts) != 2 {
|
||||
return d.Errf("malformed query matcher token: %s; must be in param=val format", d.Val())
|
||||
}
|
||||
url.Values(*m).Set(parts[0], parts[1])
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Match returns true if r matches m.
|
||||
func (m MatchQuery) Match(r *http.Request) bool {
|
||||
for param, vals := range m {
|
||||
paramVal, found := r.URL.Query()[param]
|
||||
if found {
|
||||
for _, v := range vals {
|
||||
if paramVal[0] == v || v == "*" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// CaddyModule returns the Caddy module information.
|
||||
func (MatchHeader) CaddyModule() caddy.ModuleInfo {
|
||||
return caddy.ModuleInfo{
|
||||
ID: "http.matchers.header",
|
||||
New: func() caddy.Module { return new(MatchHeader) },
|
||||
}
|
||||
}
|
||||
|
||||
// UnmarshalCaddyfile implements caddyfile.Unmarshaler.
|
||||
func (m *MatchHeader) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
|
||||
if *m == nil {
|
||||
*m = make(map[string][]string)
|
||||
}
|
||||
for d.Next() {
|
||||
var field, val string
|
||||
if !d.Args(&field, &val) {
|
||||
return d.Errf("expected both field and value")
|
||||
}
|
||||
http.Header(*m).Set(field, val)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Like req.Header.Get(), but that works with Host header.
|
||||
// go's http module swallows "Host" header.
|
||||
func getHeader(r *http.Request, field string) []string {
|
||||
field = textproto.CanonicalMIMEHeaderKey(field)
|
||||
|
||||
if field == "Host" {
|
||||
return []string{r.Host}
|
||||
}
|
||||
|
||||
return r.Header[field]
|
||||
}
|
||||
|
||||
// Match returns true if r matches m.
|
||||
func (m MatchHeader) Match(r *http.Request) bool {
|
||||
for field, allowedFieldVals := range m {
|
||||
actualFieldVals := getHeader(r, field)
|
||||
|
||||
if allowedFieldVals != nil && len(allowedFieldVals) == 0 && actualFieldVals != nil {
|
||||
// a non-nil but empty list of allowed values means
|
||||
// match if the header field exists at all
|
||||
continue
|
||||
}
|
||||
var match bool
|
||||
fieldVals:
|
||||
for _, actualFieldVal := range actualFieldVals {
|
||||
for _, allowedFieldVal := range allowedFieldVals {
|
||||
switch {
|
||||
case allowedFieldVal == "*":
|
||||
match = true
|
||||
case strings.HasPrefix(allowedFieldVal, "*") && strings.HasSuffix(allowedFieldVal, "*"):
|
||||
match = strings.Contains(actualFieldVal, allowedFieldVal[1:len(allowedFieldVal)-1])
|
||||
case strings.HasPrefix(allowedFieldVal, "*"):
|
||||
match = strings.HasSuffix(actualFieldVal, allowedFieldVal[1:])
|
||||
case strings.HasSuffix(allowedFieldVal, "*"):
|
||||
match = strings.HasPrefix(actualFieldVal, allowedFieldVal[:len(allowedFieldVal)-1])
|
||||
default:
|
||||
match = actualFieldVal == allowedFieldVal
|
||||
}
|
||||
if match {
|
||||
break fieldVals
|
||||
}
|
||||
}
|
||||
}
|
||||
if !match {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// CaddyModule returns the Caddy module information.
|
||||
func (MatchHeaderRE) CaddyModule() caddy.ModuleInfo {
|
||||
return caddy.ModuleInfo{
|
||||
ID: "http.matchers.header_regexp",
|
||||
New: func() caddy.Module { return new(MatchHeaderRE) },
|
||||
}
|
||||
}
|
||||
|
||||
// UnmarshalCaddyfile implements caddyfile.Unmarshaler.
|
||||
func (m *MatchHeaderRE) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
|
||||
if *m == nil {
|
||||
*m = make(map[string]*MatchRegexp)
|
||||
}
|
||||
for d.Next() {
|
||||
var first, second, third string
|
||||
if !d.Args(&first, &second) {
|
||||
return d.ArgErr()
|
||||
}
|
||||
|
||||
var name, field, val string
|
||||
if d.Args(&third) {
|
||||
name = first
|
||||
field = second
|
||||
val = third
|
||||
} else {
|
||||
field = first
|
||||
val = second
|
||||
}
|
||||
|
||||
(*m)[field] = &MatchRegexp{Pattern: val, Name: name}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Match returns true if r matches m.
|
||||
func (m MatchHeaderRE) Match(r *http.Request) bool {
|
||||
for field, rm := range m {
|
||||
actualFieldVals := getHeader(r, field)
|
||||
|
||||
match := false
|
||||
fieldVal:
|
||||
for _, actualFieldVal := range actualFieldVals {
|
||||
repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
|
||||
if rm.Match(actualFieldVal, repl) {
|
||||
match = true
|
||||
break fieldVal
|
||||
}
|
||||
}
|
||||
if !match {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// Provision compiles m's regular expressions.
|
||||
func (m MatchHeaderRE) Provision(ctx caddy.Context) error {
|
||||
for _, rm := range m {
|
||||
err := rm.Provision(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Validate validates m's regular expressions.
|
||||
func (m MatchHeaderRE) Validate() error {
|
||||
for _, rm := range m {
|
||||
err := rm.Validate()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// CaddyModule returns the Caddy module information.
|
||||
func (MatchProtocol) CaddyModule() caddy.ModuleInfo {
|
||||
return caddy.ModuleInfo{
|
||||
ID: "http.matchers.protocol",
|
||||
New: func() caddy.Module { return new(MatchProtocol) },
|
||||
}
|
||||
}
|
||||
|
||||
// Match returns true if r matches m.
|
||||
func (m MatchProtocol) Match(r *http.Request) bool {
|
||||
switch string(m) {
|
||||
case "grpc":
|
||||
return r.Header.Get("content-type") == "application/grpc"
|
||||
case "https":
|
||||
return r.TLS != nil
|
||||
case "http":
|
||||
return r.TLS == nil
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// UnmarshalCaddyfile implements caddyfile.Unmarshaler.
|
||||
func (m *MatchProtocol) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
|
||||
for d.Next() {
|
||||
var proto string
|
||||
if !d.Args(&proto) {
|
||||
return d.Err("expected exactly one protocol")
|
||||
}
|
||||
*m = MatchProtocol(proto)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// CaddyModule returns the Caddy module information.
|
||||
func (MatchNegate) CaddyModule() caddy.ModuleInfo {
|
||||
return caddy.ModuleInfo{
|
||||
ID: "http.matchers.not",
|
||||
New: func() caddy.Module { return new(MatchNegate) },
|
||||
}
|
||||
}
|
||||
|
||||
// UnmarshalJSON unmarshals data into m's unexported map field.
|
||||
// This is done because we cannot embed the map directly into
|
||||
// the struct, but we need a struct because we need another
|
||||
// field just for the provisioned modules.
|
||||
func (m *MatchNegate) UnmarshalJSON(data []byte) error {
|
||||
return json.Unmarshal(data, &m.MatchersRaw)
|
||||
}
|
||||
|
||||
// MarshalJSON marshals m's matchers.
|
||||
func (m MatchNegate) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(m.MatchersRaw)
|
||||
}
|
||||
|
||||
// UnmarshalCaddyfile implements caddyfile.Unmarshaler.
|
||||
func (m *MatchNegate) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
|
||||
// first, unmarshal each matcher in the set from its tokens
|
||||
|
||||
matcherMap := make(map[string]RequestMatcher)
|
||||
for d.Next() {
|
||||
for d.NextBlock(0) {
|
||||
matcherName := d.Val()
|
||||
mod, err := caddy.GetModule("http.matchers." + matcherName)
|
||||
if err != nil {
|
||||
return d.Errf("getting matcher module '%s': %v", matcherName, err)
|
||||
}
|
||||
unm, ok := mod.New().(caddyfile.Unmarshaler)
|
||||
if !ok {
|
||||
return d.Errf("matcher module '%s' is not a Caddyfile unmarshaler", matcherName)
|
||||
}
|
||||
err = unm.UnmarshalCaddyfile(d.NewFromNextSegment())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rm := unm.(RequestMatcher)
|
||||
m.Matchers = append(m.Matchers, rm)
|
||||
matcherMap[matcherName] = rm
|
||||
}
|
||||
}
|
||||
|
||||
// we should now be functional, but we also need
|
||||
// to be able to marshal as JSON, otherwise config
|
||||
// adaptation won't work properly
|
||||
m.MatchersRaw = make(caddy.ModuleMap)
|
||||
for name, matchers := range matcherMap {
|
||||
jsonBytes, err := json.Marshal(matchers)
|
||||
if err != nil {
|
||||
return fmt.Errorf("marshaling matcher %s: %v", name, err)
|
||||
}
|
||||
m.MatchersRaw[name] = jsonBytes
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Provision loads the matcher modules to be negated.
|
||||
func (m *MatchNegate) Provision(ctx caddy.Context) error {
|
||||
mods, err := ctx.LoadModule(m, "MatchersRaw")
|
||||
if err != nil {
|
||||
return fmt.Errorf("loading matchers: %v", err)
|
||||
}
|
||||
for _, modIface := range mods.(map[string]interface{}) {
|
||||
m.Matchers = append(m.Matchers, modIface.(RequestMatcher))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Match returns true if r matches m. Since this matcher negates the
|
||||
// embedded matchers, false is returned if any of its matchers match.
|
||||
func (m MatchNegate) Match(r *http.Request) bool {
|
||||
return !m.Matchers.Match(r)
|
||||
}
|
||||
|
||||
// CaddyModule returns the Caddy module information.
|
||||
func (MatchRemoteIP) CaddyModule() caddy.ModuleInfo {
|
||||
return caddy.ModuleInfo{
|
||||
ID: "http.matchers.remote_ip",
|
||||
New: func() caddy.Module { return new(MatchRemoteIP) },
|
||||
}
|
||||
}
|
||||
|
||||
// UnmarshalCaddyfile implements caddyfile.Unmarshaler.
|
||||
func (m *MatchRemoteIP) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
|
||||
for d.Next() {
|
||||
m.Ranges = append(m.Ranges, d.RemainingArgs()...)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Provision parses m's IP ranges, either from IP or CIDR expressions.
|
||||
func (m *MatchRemoteIP) Provision(ctx caddy.Context) error {
|
||||
for _, str := range m.Ranges {
|
||||
if strings.Contains(str, "/") {
|
||||
_, ipNet, err := net.ParseCIDR(str)
|
||||
if err != nil {
|
||||
return fmt.Errorf("parsing CIDR expression: %v", err)
|
||||
}
|
||||
m.cidrs = append(m.cidrs, ipNet)
|
||||
} else {
|
||||
ip := net.ParseIP(str)
|
||||
if ip == nil {
|
||||
return fmt.Errorf("invalid IP address: %s", str)
|
||||
}
|
||||
mask := len(ip) * 8
|
||||
m.cidrs = append(m.cidrs, &net.IPNet{
|
||||
IP: ip,
|
||||
Mask: net.CIDRMask(mask, mask),
|
||||
})
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m MatchRemoteIP) getClientIP(r *http.Request) (net.IP, error) {
|
||||
var remote string
|
||||
if fwdFor := r.Header.Get("X-Forwarded-For"); fwdFor != "" {
|
||||
remote = strings.TrimSpace(strings.Split(fwdFor, ",")[0])
|
||||
}
|
||||
if remote == "" {
|
||||
remote = r.RemoteAddr
|
||||
}
|
||||
|
||||
ipStr, _, err := net.SplitHostPort(remote)
|
||||
if err != nil {
|
||||
ipStr = remote // OK; probably didn't have a port
|
||||
}
|
||||
|
||||
ip := net.ParseIP(ipStr)
|
||||
if ip == nil {
|
||||
return nil, fmt.Errorf("invalid client IP address: %s", ipStr)
|
||||
}
|
||||
|
||||
return ip, nil
|
||||
}
|
||||
|
||||
// Match returns true if r matches m.
|
||||
func (m MatchRemoteIP) Match(r *http.Request) bool {
|
||||
clientIP, err := m.getClientIP(r)
|
||||
if err != nil {
|
||||
log.Printf("[ERROR] remote_ip matcher: %v", err)
|
||||
return false
|
||||
}
|
||||
for _, ipRange := range m.cidrs {
|
||||
if ipRange.Contains(clientIP) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// MatchRegexp is an embedable type for matching
|
||||
// using regular expressions. It adds placeholders
|
||||
// to the request's replacer.
|
||||
type MatchRegexp struct {
|
||||
// A unique name for this regular expression. Optional,
|
||||
// but useful to prevent overwriting captures from other
|
||||
// regexp matchers.
|
||||
Name string `json:"name,omitempty"`
|
||||
|
||||
// The regular expression to evaluate, in RE2 syntax,
|
||||
// which is the same general syntax used by Go, Perl,
|
||||
// and Python. For details, see
|
||||
// [Go's regexp package](https://golang.org/pkg/regexp/).
|
||||
// Captures are accessible via placeholders. Unnamed
|
||||
// capture groups are exposed as their numeric, 1-based
|
||||
// index, while named capture groups are available by
|
||||
// the capture group name.
|
||||
Pattern string `json:"pattern"`
|
||||
|
||||
compiled *regexp.Regexp
|
||||
phPrefix string
|
||||
}
|
||||
|
||||
// Provision compiles the regular expression.
|
||||
func (mre *MatchRegexp) Provision(caddy.Context) error {
|
||||
re, err := regexp.Compile(mre.Pattern)
|
||||
if err != nil {
|
||||
return fmt.Errorf("compiling matcher regexp %s: %v", mre.Pattern, err)
|
||||
}
|
||||
mre.compiled = re
|
||||
mre.phPrefix = regexpPlaceholderPrefix
|
||||
if mre.Name != "" {
|
||||
mre.phPrefix += "." + mre.Name
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Validate ensures mre is set up correctly.
|
||||
func (mre *MatchRegexp) Validate() error {
|
||||
if mre.Name != "" && !wordRE.MatchString(mre.Name) {
|
||||
return fmt.Errorf("invalid regexp name (must contain only word characters): %s", mre.Name)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Match returns true if input matches the compiled regular
|
||||
// expression in mre. It sets values on the replacer repl
|
||||
// associated with capture groups, using the given scope
|
||||
// (namespace).
|
||||
func (mre *MatchRegexp) Match(input string, repl *caddy.Replacer) bool {
|
||||
matches := mre.compiled.FindStringSubmatch(input)
|
||||
if matches == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
// save all capture groups, first by index
|
||||
for i, match := range matches {
|
||||
key := fmt.Sprintf("%s.%d", mre.phPrefix, i)
|
||||
repl.Set(key, match)
|
||||
}
|
||||
|
||||
// then by name
|
||||
for i, name := range mre.compiled.SubexpNames() {
|
||||
if i != 0 && name != "" {
|
||||
key := fmt.Sprintf("%s.%s", mre.phPrefix, name)
|
||||
repl.Set(key, matches[i])
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// UnmarshalCaddyfile implements caddyfile.Unmarshaler.
|
||||
func (mre *MatchRegexp) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
|
||||
for d.Next() {
|
||||
args := d.RemainingArgs()
|
||||
switch len(args) {
|
||||
case 1:
|
||||
mre.Pattern = args[0]
|
||||
case 2:
|
||||
mre.Name = args[0]
|
||||
mre.Pattern = args[1]
|
||||
default:
|
||||
return d.ArgErr()
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ResponseMatcher is a type which can determine if an
|
||||
// HTTP response matches some criteria.
|
||||
type ResponseMatcher struct {
|
||||
// If set, one of these status codes would be required.
|
||||
// A one-digit status can be used to represent all codes
|
||||
// in that class (e.g. 3 for all 3xx codes).
|
||||
StatusCode []int `json:"status_code,omitempty"`
|
||||
|
||||
// If set, each header specified must be one of the specified values.
|
||||
Headers http.Header `json:"headers,omitempty"`
|
||||
}
|
||||
|
||||
// Match returns true if the given statusCode and hdr match rm.
|
||||
func (rm ResponseMatcher) Match(statusCode int, hdr http.Header) bool {
|
||||
if !rm.matchStatusCode(statusCode) {
|
||||
return false
|
||||
}
|
||||
return rm.matchHeaders(hdr)
|
||||
}
|
||||
|
||||
func (rm ResponseMatcher) matchStatusCode(statusCode int) bool {
|
||||
if rm.StatusCode == nil {
|
||||
return true
|
||||
}
|
||||
for _, code := range rm.StatusCode {
|
||||
if StatusCodeMatches(statusCode, code) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (rm ResponseMatcher) matchHeaders(hdr http.Header) bool {
|
||||
for field, allowedFieldVals := range rm.Headers {
|
||||
actualFieldVals, fieldExists := hdr[textproto.CanonicalMIMEHeaderKey(field)]
|
||||
if allowedFieldVals != nil && len(allowedFieldVals) == 0 && fieldExists {
|
||||
// a non-nil but empty list of allowed values means
|
||||
// match if the header field exists at all
|
||||
continue
|
||||
}
|
||||
var match bool
|
||||
fieldVals:
|
||||
for _, actualFieldVal := range actualFieldVals {
|
||||
for _, allowedFieldVal := range allowedFieldVals {
|
||||
if actualFieldVal == allowedFieldVal {
|
||||
match = true
|
||||
break fieldVals
|
||||
}
|
||||
}
|
||||
}
|
||||
if !match {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
var wordRE = regexp.MustCompile(`\w+`)
|
||||
|
||||
const regexpPlaceholderPrefix = "http.regexp"
|
||||
|
||||
// Interface guards
|
||||
var (
|
||||
_ RequestMatcher = (*MatchHost)(nil)
|
||||
_ RequestMatcher = (*MatchPath)(nil)
|
||||
_ RequestMatcher = (*MatchPathRE)(nil)
|
||||
_ caddy.Provisioner = (*MatchPathRE)(nil)
|
||||
_ RequestMatcher = (*MatchMethod)(nil)
|
||||
_ RequestMatcher = (*MatchQuery)(nil)
|
||||
_ RequestMatcher = (*MatchHeader)(nil)
|
||||
_ RequestMatcher = (*MatchHeaderRE)(nil)
|
||||
_ caddy.Provisioner = (*MatchHeaderRE)(nil)
|
||||
_ RequestMatcher = (*MatchProtocol)(nil)
|
||||
_ RequestMatcher = (*MatchRemoteIP)(nil)
|
||||
_ caddy.Provisioner = (*MatchRemoteIP)(nil)
|
||||
_ RequestMatcher = (*MatchNegate)(nil)
|
||||
_ caddy.Provisioner = (*MatchNegate)(nil)
|
||||
_ caddy.Provisioner = (*MatchRegexp)(nil)
|
||||
|
||||
_ caddyfile.Unmarshaler = (*MatchHost)(nil)
|
||||
_ caddyfile.Unmarshaler = (*MatchPath)(nil)
|
||||
_ caddyfile.Unmarshaler = (*MatchPathRE)(nil)
|
||||
_ caddyfile.Unmarshaler = (*MatchMethod)(nil)
|
||||
_ caddyfile.Unmarshaler = (*MatchQuery)(nil)
|
||||
_ caddyfile.Unmarshaler = (*MatchHeader)(nil)
|
||||
_ caddyfile.Unmarshaler = (*MatchHeaderRE)(nil)
|
||||
_ caddyfile.Unmarshaler = (*MatchProtocol)(nil)
|
||||
_ caddyfile.Unmarshaler = (*MatchRemoteIP)(nil)
|
||||
|
||||
_ json.Marshaler = (*MatchNegate)(nil)
|
||||
_ json.Unmarshaler = (*MatchNegate)(nil)
|
||||
)
|
||||
@@ -1,856 +0,0 @@
|
||||
// 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 caddyhttp
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/caddyserver/caddy/v2"
|
||||
)
|
||||
|
||||
func TestHostMatcher(t *testing.T) {
|
||||
err := os.Setenv("GO_BENCHMARK_DOMAIN", "localhost")
|
||||
if err != nil {
|
||||
t.Errorf("error while setting up environment: %v", err)
|
||||
}
|
||||
|
||||
for i, tc := range []struct {
|
||||
match MatchHost
|
||||
input string
|
||||
expect bool
|
||||
}{
|
||||
{
|
||||
match: MatchHost{},
|
||||
input: "example.com",
|
||||
expect: false,
|
||||
},
|
||||
{
|
||||
match: MatchHost{"example.com"},
|
||||
input: "example.com",
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
match: MatchHost{"EXAMPLE.COM"},
|
||||
input: "example.com",
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
match: MatchHost{"example.com"},
|
||||
input: "EXAMPLE.COM",
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
match: MatchHost{"example.com"},
|
||||
input: "foo.example.com",
|
||||
expect: false,
|
||||
},
|
||||
{
|
||||
match: MatchHost{"example.com"},
|
||||
input: "EXAMPLE.COM",
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
match: MatchHost{"foo.example.com"},
|
||||
input: "foo.example.com",
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
match: MatchHost{"foo.example.com"},
|
||||
input: "bar.example.com",
|
||||
expect: false,
|
||||
},
|
||||
{
|
||||
match: MatchHost{"*.example.com"},
|
||||
input: "example.com",
|
||||
expect: false,
|
||||
},
|
||||
{
|
||||
match: MatchHost{"*.example.com"},
|
||||
input: "SUB.EXAMPLE.COM",
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
match: MatchHost{"*.example.com"},
|
||||
input: "foo.example.com",
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
match: MatchHost{"*.example.com"},
|
||||
input: "foo.bar.example.com",
|
||||
expect: false,
|
||||
},
|
||||
{
|
||||
match: MatchHost{"*.example.com", "example.net"},
|
||||
input: "example.net",
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
match: MatchHost{"example.net", "*.example.com"},
|
||||
input: "foo.example.com",
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
match: MatchHost{"*.example.net", "*.*.example.com"},
|
||||
input: "foo.bar.example.com",
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
match: MatchHost{"*.example.net", "sub.*.example.com"},
|
||||
input: "sub.foo.example.com",
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
match: MatchHost{"*.example.net", "sub.*.example.com"},
|
||||
input: "sub.foo.example.net",
|
||||
expect: false,
|
||||
},
|
||||
{
|
||||
match: MatchHost{"example.com"},
|
||||
input: "example.com:5555",
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
match: MatchHost{"{env.GO_BENCHMARK_DOMAIN}"},
|
||||
input: "localhost",
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
match: MatchHost{"{env.GO_NONEXISTENT}"},
|
||||
input: "localhost",
|
||||
expect: false,
|
||||
},
|
||||
} {
|
||||
req := &http.Request{Host: tc.input}
|
||||
repl := caddy.NewReplacer()
|
||||
ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl)
|
||||
req = req.WithContext(ctx)
|
||||
|
||||
actual := tc.match.Match(req)
|
||||
if actual != tc.expect {
|
||||
t.Errorf("Test %d %v: Expected %t, got %t for '%s'", i, tc.match, tc.expect, actual, tc.input)
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPathMatcher(t *testing.T) {
|
||||
for i, tc := range []struct {
|
||||
match MatchPath
|
||||
input string
|
||||
expect bool
|
||||
}{
|
||||
{
|
||||
match: MatchPath{},
|
||||
input: "/",
|
||||
expect: false,
|
||||
},
|
||||
{
|
||||
match: MatchPath{"/"},
|
||||
input: "/",
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
match: MatchPath{"/foo/bar"},
|
||||
input: "/",
|
||||
expect: false,
|
||||
},
|
||||
{
|
||||
match: MatchPath{"/foo/bar"},
|
||||
input: "/foo/bar",
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
match: MatchPath{"/foo/bar/"},
|
||||
input: "/foo/bar",
|
||||
expect: false,
|
||||
},
|
||||
{
|
||||
match: MatchPath{"/foo/bar/"},
|
||||
input: "/foo/bar/",
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
match: MatchPath{"/foo/bar/", "/other"},
|
||||
input: "/other/",
|
||||
expect: false,
|
||||
},
|
||||
{
|
||||
match: MatchPath{"/foo/bar/", "/other"},
|
||||
input: "/other",
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
match: MatchPath{"*.ext"},
|
||||
input: "/foo/bar.ext",
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
match: MatchPath{"*.php"},
|
||||
input: "/index.PHP",
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
match: MatchPath{"*.ext"},
|
||||
input: "/foo/bar.ext",
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
match: MatchPath{"/foo/*/baz"},
|
||||
input: "/foo/bar/baz",
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
match: MatchPath{"/foo/*/baz/bam"},
|
||||
input: "/foo/bar/bam",
|
||||
expect: false,
|
||||
},
|
||||
{
|
||||
match: MatchPath{"*substring*"},
|
||||
input: "/foo/substring/bar.txt",
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
match: MatchPath{"/foo"},
|
||||
input: "/foo/bar",
|
||||
expect: false,
|
||||
},
|
||||
{
|
||||
match: MatchPath{"/foo"},
|
||||
input: "/foo/bar",
|
||||
expect: false,
|
||||
},
|
||||
{
|
||||
match: MatchPath{"/foo"},
|
||||
input: "/FOO",
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
match: MatchPath{"/foo*"},
|
||||
input: "/FOOOO",
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
match: MatchPath{"/foo/bar.txt"},
|
||||
input: "/foo/BAR.txt",
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
match: MatchPath{"*"},
|
||||
input: "/",
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
match: MatchPath{"*"},
|
||||
input: "/foo/bar",
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
match: MatchPath{"**"},
|
||||
input: "/",
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
match: MatchPath{"**"},
|
||||
input: "/foo/bar",
|
||||
expect: true,
|
||||
},
|
||||
} {
|
||||
req := &http.Request{URL: &url.URL{Path: tc.input}}
|
||||
repl := caddy.NewReplacer()
|
||||
ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl)
|
||||
req = req.WithContext(ctx)
|
||||
|
||||
actual := tc.match.Match(req)
|
||||
if actual != tc.expect {
|
||||
t.Errorf("Test %d %v: Expected %t, got %t for '%s'", i, tc.match, tc.expect, actual, tc.input)
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPathMatcherWindows(t *testing.T) {
|
||||
// only Windows has this bug where it will ignore
|
||||
// trailing dots and spaces in a filename, but we
|
||||
// test for it on all platforms to be more consistent
|
||||
|
||||
req := &http.Request{URL: &url.URL{Path: "/index.php . . .."}}
|
||||
repl := caddy.NewReplacer()
|
||||
ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl)
|
||||
req = req.WithContext(ctx)
|
||||
|
||||
match := MatchPath{"*.php"}
|
||||
matched := match.Match(req)
|
||||
if !matched {
|
||||
t.Errorf("Expected to match; should ignore trailing dots and spaces")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPathREMatcher(t *testing.T) {
|
||||
for i, tc := range []struct {
|
||||
match MatchPathRE
|
||||
input string
|
||||
expect bool
|
||||
expectRepl map[string]string
|
||||
}{
|
||||
{
|
||||
match: MatchPathRE{},
|
||||
input: "/",
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
match: MatchPathRE{MatchRegexp{Pattern: "/"}},
|
||||
input: "/",
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
match: MatchPathRE{MatchRegexp{Pattern: "/foo"}},
|
||||
input: "/foo",
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
match: MatchPathRE{MatchRegexp{Pattern: "/foo"}},
|
||||
input: "/foo/",
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
match: MatchPathRE{MatchRegexp{Pattern: "/bar"}},
|
||||
input: "/foo/",
|
||||
expect: false,
|
||||
},
|
||||
{
|
||||
match: MatchPathRE{MatchRegexp{Pattern: "^/bar"}},
|
||||
input: "/foo/bar",
|
||||
expect: false,
|
||||
},
|
||||
{
|
||||
match: MatchPathRE{MatchRegexp{Pattern: "^/foo/(.*)/baz$", Name: "name"}},
|
||||
input: "/foo/bar/baz",
|
||||
expect: true,
|
||||
expectRepl: map[string]string{"name.1": "bar"},
|
||||
},
|
||||
{
|
||||
match: MatchPathRE{MatchRegexp{Pattern: "^/foo/(?P<myparam>.*)/baz$", Name: "name"}},
|
||||
input: "/foo/bar/baz",
|
||||
expect: true,
|
||||
expectRepl: map[string]string{"name.myparam": "bar"},
|
||||
},
|
||||
} {
|
||||
// compile the regexp and validate its name
|
||||
err := tc.match.Provision(caddy.Context{})
|
||||
if err != nil {
|
||||
t.Errorf("Test %d %v: Provisioning: %v", i, tc.match, err)
|
||||
continue
|
||||
}
|
||||
err = tc.match.Validate()
|
||||
if err != nil {
|
||||
t.Errorf("Test %d %v: Validating: %v", i, tc.match, err)
|
||||
continue
|
||||
}
|
||||
|
||||
// set up the fake request and its Replacer
|
||||
req := &http.Request{URL: &url.URL{Path: tc.input}}
|
||||
repl := caddy.NewReplacer()
|
||||
ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl)
|
||||
req = req.WithContext(ctx)
|
||||
addHTTPVarsToReplacer(repl, req, httptest.NewRecorder())
|
||||
|
||||
actual := tc.match.Match(req)
|
||||
if actual != tc.expect {
|
||||
t.Errorf("Test %d [%v]: Expected %t, got %t for input '%s'",
|
||||
i, tc.match.Pattern, tc.expect, actual, tc.input)
|
||||
continue
|
||||
}
|
||||
|
||||
for key, expectVal := range tc.expectRepl {
|
||||
placeholder := fmt.Sprintf("{http.regexp.%s}", key)
|
||||
actualVal := repl.ReplaceAll(placeholder, "<empty>")
|
||||
if actualVal != expectVal {
|
||||
t.Errorf("Test %d [%v]: Expected placeholder {http.regexp.%s} to be '%s' but got '%s'",
|
||||
i, tc.match.Pattern, key, expectVal, actualVal)
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestHeaderMatcher(t *testing.T) {
|
||||
for i, tc := range []struct {
|
||||
match MatchHeader
|
||||
input http.Header // make sure these are canonical cased (std lib will do that in a real request)
|
||||
host string
|
||||
expect bool
|
||||
}{
|
||||
{
|
||||
match: MatchHeader{"Field": []string{"foo"}},
|
||||
input: http.Header{"Field": []string{"foo"}},
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
match: MatchHeader{"Field": []string{"foo", "bar"}},
|
||||
input: http.Header{"Field": []string{"bar"}},
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
match: MatchHeader{"Field": []string{"foo", "bar"}},
|
||||
input: http.Header{"Alakazam": []string{"kapow"}},
|
||||
expect: false,
|
||||
},
|
||||
{
|
||||
match: MatchHeader{"Field": []string{"foo", "bar"}},
|
||||
input: http.Header{"Field": []string{"kapow"}},
|
||||
expect: false,
|
||||
},
|
||||
{
|
||||
match: MatchHeader{"Field": []string{"foo", "bar"}},
|
||||
input: http.Header{"Field": []string{"kapow", "foo"}},
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
match: MatchHeader{"Field1": []string{"foo"}, "Field2": []string{"bar"}},
|
||||
input: http.Header{"Field1": []string{"foo"}, "Field2": []string{"bar"}},
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
match: MatchHeader{"field1": []string{"foo"}, "field2": []string{"bar"}},
|
||||
input: http.Header{"Field1": []string{"foo"}, "Field2": []string{"bar"}},
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
match: MatchHeader{"field1": []string{"foo"}, "field2": []string{"bar"}},
|
||||
input: http.Header{"Field1": []string{"foo"}, "Field2": []string{"kapow"}},
|
||||
expect: false,
|
||||
},
|
||||
{
|
||||
match: MatchHeader{"field1": []string{"*"}},
|
||||
input: http.Header{"Field1": []string{"foo"}},
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
match: MatchHeader{"field1": []string{"*"}},
|
||||
input: http.Header{"Field2": []string{"foo"}},
|
||||
expect: false,
|
||||
},
|
||||
{
|
||||
match: MatchHeader{"host": []string{"localhost"}},
|
||||
input: http.Header{},
|
||||
host: "localhost",
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
match: MatchHeader{"host": []string{"localhost"}},
|
||||
input: http.Header{},
|
||||
host: "caddyserver.com",
|
||||
expect: false,
|
||||
},
|
||||
} {
|
||||
req := &http.Request{Header: tc.input, Host: tc.host}
|
||||
actual := tc.match.Match(req)
|
||||
if actual != tc.expect {
|
||||
t.Errorf("Test %d %v: Expected %t, got %t for '%s'", i, tc.match, tc.expect, actual, tc.input)
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestQueryMatcher(t *testing.T) {
|
||||
for i, tc := range []struct {
|
||||
scenario string
|
||||
match MatchQuery
|
||||
input string
|
||||
expect bool
|
||||
}{
|
||||
{
|
||||
scenario: "non match against a specific value",
|
||||
match: MatchQuery{"debug": []string{"1"}},
|
||||
input: "/",
|
||||
expect: false,
|
||||
},
|
||||
{
|
||||
scenario: "match against a specific value",
|
||||
match: MatchQuery{"debug": []string{"1"}},
|
||||
input: "/?debug=1",
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
scenario: "match against a wildcard",
|
||||
match: MatchQuery{"debug": []string{"*"}},
|
||||
input: "/?debug=something",
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
scenario: "non match against a wildcarded",
|
||||
match: MatchQuery{"debug": []string{"*"}},
|
||||
input: "/?other=something",
|
||||
expect: false,
|
||||
},
|
||||
{
|
||||
scenario: "match against an empty value",
|
||||
match: MatchQuery{"debug": []string{""}},
|
||||
input: "/?debug",
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
scenario: "non match against an empty value",
|
||||
match: MatchQuery{"debug": []string{""}},
|
||||
input: "/?someparam",
|
||||
expect: false,
|
||||
},
|
||||
} {
|
||||
|
||||
u, _ := url.Parse(tc.input)
|
||||
|
||||
req := &http.Request{URL: u}
|
||||
actual := tc.match.Match(req)
|
||||
if actual != tc.expect {
|
||||
t.Errorf("Test %d %v: Expected %t, got %t for '%s'", i, tc.match, tc.expect, actual, tc.input)
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestHeaderREMatcher(t *testing.T) {
|
||||
for i, tc := range []struct {
|
||||
match MatchHeaderRE
|
||||
input http.Header // make sure these are canonical cased (std lib will do that in a real request)
|
||||
host string
|
||||
expect bool
|
||||
expectRepl map[string]string
|
||||
}{
|
||||
{
|
||||
match: MatchHeaderRE{"Field": &MatchRegexp{Pattern: "foo"}},
|
||||
input: http.Header{"Field": []string{"foo"}},
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
match: MatchHeaderRE{"Field": &MatchRegexp{Pattern: "$foo^"}},
|
||||
input: http.Header{"Field": []string{"foobar"}},
|
||||
expect: false,
|
||||
},
|
||||
{
|
||||
match: MatchHeaderRE{"Field": &MatchRegexp{Pattern: "^foo(.*)$", Name: "name"}},
|
||||
input: http.Header{"Field": []string{"foobar"}},
|
||||
expect: true,
|
||||
expectRepl: map[string]string{"name.1": "bar"},
|
||||
},
|
||||
{
|
||||
match: MatchHeaderRE{"Field": &MatchRegexp{Pattern: "^foo.*$", Name: "name"}},
|
||||
input: http.Header{"Field": []string{"barfoo", "foobar"}},
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
match: MatchHeaderRE{"host": &MatchRegexp{Pattern: "^localhost$", Name: "name"}},
|
||||
input: http.Header{},
|
||||
host: "localhost",
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
match: MatchHeaderRE{"host": &MatchRegexp{Pattern: "^local$", Name: "name"}},
|
||||
input: http.Header{},
|
||||
host: "localhost",
|
||||
expect: false,
|
||||
},
|
||||
} {
|
||||
// compile the regexp and validate its name
|
||||
err := tc.match.Provision(caddy.Context{})
|
||||
if err != nil {
|
||||
t.Errorf("Test %d %v: Provisioning: %v", i, tc.match, err)
|
||||
continue
|
||||
}
|
||||
err = tc.match.Validate()
|
||||
if err != nil {
|
||||
t.Errorf("Test %d %v: Validating: %v", i, tc.match, err)
|
||||
continue
|
||||
}
|
||||
|
||||
// set up the fake request and its Replacer
|
||||
req := &http.Request{Header: tc.input, URL: new(url.URL), Host: tc.host}
|
||||
repl := caddy.NewReplacer()
|
||||
ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl)
|
||||
req = req.WithContext(ctx)
|
||||
addHTTPVarsToReplacer(repl, req, httptest.NewRecorder())
|
||||
|
||||
actual := tc.match.Match(req)
|
||||
if actual != tc.expect {
|
||||
t.Errorf("Test %d [%v]: Expected %t, got %t for input '%s'",
|
||||
i, tc.match, tc.expect, actual, tc.input)
|
||||
continue
|
||||
}
|
||||
|
||||
for key, expectVal := range tc.expectRepl {
|
||||
placeholder := fmt.Sprintf("{http.regexp.%s}", key)
|
||||
actualVal := repl.ReplaceAll(placeholder, "<empty>")
|
||||
if actualVal != expectVal {
|
||||
t.Errorf("Test %d [%v]: Expected placeholder {http.regexp.%s} to be '%s' but got '%s'",
|
||||
i, tc.match, key, expectVal, actualVal)
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestVarREMatcher(t *testing.T) {
|
||||
for i, tc := range []struct {
|
||||
desc string
|
||||
match MatchVarsRE
|
||||
input VarsMiddleware
|
||||
expect bool
|
||||
expectRepl map[string]string
|
||||
}{
|
||||
{
|
||||
desc: "match static value within var set by the VarsMiddleware succeeds",
|
||||
match: MatchVarsRE{"Var1": &MatchRegexp{Pattern: "foo"}},
|
||||
input: VarsMiddleware{"Var1": "here is foo val"},
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
desc: "value set by VarsMiddleware not satisfying regexp matcher fails to match",
|
||||
match: MatchVarsRE{"Var1": &MatchRegexp{Pattern: "$foo^"}},
|
||||
input: VarsMiddleware{"Var1": "foobar"},
|
||||
expect: false,
|
||||
},
|
||||
{
|
||||
desc: "successfully matched value is captured and its placeholder is added to replacer",
|
||||
match: MatchVarsRE{"Var1": &MatchRegexp{Pattern: "^foo(.*)$", Name: "name"}},
|
||||
input: VarsMiddleware{"Var1": "foobar"},
|
||||
expect: true,
|
||||
expectRepl: map[string]string{"name.1": "bar"},
|
||||
},
|
||||
{
|
||||
desc: "matching against a value of standard variables succeeds",
|
||||
match: MatchVarsRE{"{http.request.method}": &MatchRegexp{Pattern: "^G.[tT]$"}},
|
||||
input: VarsMiddleware{},
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
desc: "matching against value of var set by the VarsMiddleware and referenced by its placeholder succeeds",
|
||||
match: MatchVarsRE{"{http.vars.Var1}": &MatchRegexp{Pattern: "[vV]ar[0-9]"}},
|
||||
input: VarsMiddleware{"Var1": "var1Value"},
|
||||
expect: true,
|
||||
},
|
||||
} {
|
||||
tc := tc // capture range value
|
||||
t.Run(tc.desc, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
// compile the regexp and validate its name
|
||||
err := tc.match.Provision(caddy.Context{})
|
||||
if err != nil {
|
||||
t.Errorf("Test %d %v: Provisioning: %v", i, tc.match, err)
|
||||
return
|
||||
}
|
||||
err = tc.match.Validate()
|
||||
if err != nil {
|
||||
t.Errorf("Test %d %v: Validating: %v", i, tc.match, err)
|
||||
return
|
||||
}
|
||||
|
||||
// set up the fake request and its Replacer
|
||||
req := &http.Request{URL: new(url.URL), Method: http.MethodGet}
|
||||
repl := caddy.NewReplacer()
|
||||
ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl)
|
||||
ctx = context.WithValue(ctx, VarsCtxKey, make(map[string]interface{}))
|
||||
req = req.WithContext(ctx)
|
||||
|
||||
addHTTPVarsToReplacer(repl, req, httptest.NewRecorder())
|
||||
|
||||
tc.input.ServeHTTP(httptest.NewRecorder(), req, emptyHandler)
|
||||
|
||||
actual := tc.match.Match(req)
|
||||
if actual != tc.expect {
|
||||
t.Errorf("Test %d [%v]: Expected %t, got %t for input '%s'",
|
||||
i, tc.match, tc.expect, actual, tc.input)
|
||||
return
|
||||
}
|
||||
|
||||
for key, expectVal := range tc.expectRepl {
|
||||
placeholder := fmt.Sprintf("{http.regexp.%s}", key)
|
||||
actualVal := repl.ReplaceAll(placeholder, "<empty>")
|
||||
if actualVal != expectVal {
|
||||
t.Errorf("Test %d [%v]: Expected placeholder {http.regexp.%s} to be '%s' but got '%s'",
|
||||
i, tc.match, key, expectVal, actualVal)
|
||||
return
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestResponseMatcher(t *testing.T) {
|
||||
for i, tc := range []struct {
|
||||
require ResponseMatcher
|
||||
status int
|
||||
hdr http.Header // make sure these are canonical cased (std lib will do that in a real request)
|
||||
expect bool
|
||||
}{
|
||||
{
|
||||
require: ResponseMatcher{},
|
||||
status: 200,
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
require: ResponseMatcher{
|
||||
StatusCode: []int{200},
|
||||
},
|
||||
status: 200,
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
require: ResponseMatcher{
|
||||
StatusCode: []int{2},
|
||||
},
|
||||
status: 200,
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
require: ResponseMatcher{
|
||||
StatusCode: []int{201},
|
||||
},
|
||||
status: 200,
|
||||
expect: false,
|
||||
},
|
||||
{
|
||||
require: ResponseMatcher{
|
||||
StatusCode: []int{2},
|
||||
},
|
||||
status: 301,
|
||||
expect: false,
|
||||
},
|
||||
{
|
||||
require: ResponseMatcher{
|
||||
StatusCode: []int{3},
|
||||
},
|
||||
status: 301,
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
require: ResponseMatcher{
|
||||
StatusCode: []int{3},
|
||||
},
|
||||
status: 399,
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
require: ResponseMatcher{
|
||||
StatusCode: []int{3},
|
||||
},
|
||||
status: 400,
|
||||
expect: false,
|
||||
},
|
||||
{
|
||||
require: ResponseMatcher{
|
||||
StatusCode: []int{3, 4},
|
||||
},
|
||||
status: 400,
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
require: ResponseMatcher{
|
||||
StatusCode: []int{3, 401},
|
||||
},
|
||||
status: 401,
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
require: ResponseMatcher{
|
||||
Headers: http.Header{
|
||||
"Foo": []string{"bar"},
|
||||
},
|
||||
},
|
||||
hdr: http.Header{"Foo": []string{"bar"}},
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
require: ResponseMatcher{
|
||||
Headers: http.Header{
|
||||
"Foo2": []string{"bar"},
|
||||
},
|
||||
},
|
||||
hdr: http.Header{"Foo": []string{"bar"}},
|
||||
expect: false,
|
||||
},
|
||||
{
|
||||
require: ResponseMatcher{
|
||||
Headers: http.Header{
|
||||
"Foo": []string{"bar", "baz"},
|
||||
},
|
||||
},
|
||||
hdr: http.Header{"Foo": []string{"baz"}},
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
require: ResponseMatcher{
|
||||
Headers: http.Header{
|
||||
"Foo": []string{"bar"},
|
||||
"Foo2": []string{"baz"},
|
||||
},
|
||||
},
|
||||
hdr: http.Header{"Foo": []string{"baz"}},
|
||||
expect: false,
|
||||
},
|
||||
{
|
||||
require: ResponseMatcher{
|
||||
Headers: http.Header{
|
||||
"Foo": []string{"bar"},
|
||||
"Foo2": []string{"baz"},
|
||||
},
|
||||
},
|
||||
hdr: http.Header{"Foo": []string{"bar"}, "Foo2": []string{"baz"}},
|
||||
expect: true,
|
||||
},
|
||||
} {
|
||||
actual := tc.require.Match(tc.status, tc.hdr)
|
||||
if actual != tc.expect {
|
||||
t.Errorf("Test %d %v: Expected %t, got %t for HTTP %d %v", i, tc.require, tc.expect, actual, tc.status, tc.hdr)
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkHostMatcherWithoutPlaceholder(b *testing.B) {
|
||||
req := &http.Request{Host: "localhost"}
|
||||
repl := caddy.NewReplacer()
|
||||
ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl)
|
||||
req = req.WithContext(ctx)
|
||||
|
||||
match := MatchHost{"localhost"}
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
match.Match(req)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkHostMatcherWithPlaceholder(b *testing.B) {
|
||||
err := os.Setenv("GO_BENCHMARK_DOMAIN", "localhost")
|
||||
if err != nil {
|
||||
b.Errorf("error while setting up environment: %v", err)
|
||||
}
|
||||
|
||||
req := &http.Request{Host: "localhost"}
|
||||
repl := caddy.NewReplacer()
|
||||
ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl)
|
||||
req = req.WithContext(ctx)
|
||||
match := MatchHost{"{env.GO_BENCHMARK_DOMAIN}"}
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
match.Match(req)
|
||||
}
|
||||
}
|
||||
@@ -1,293 +0,0 @@
|
||||
// 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 caddyhttp
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/textproto"
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/caddyserver/caddy/v2"
|
||||
"github.com/caddyserver/caddy/v2/modules/caddytls"
|
||||
)
|
||||
|
||||
func addHTTPVarsToReplacer(repl *caddy.Replacer, req *http.Request, w http.ResponseWriter) {
|
||||
httpVars := func(key string) (string, bool) {
|
||||
if req != nil {
|
||||
// query string parameters
|
||||
if strings.HasPrefix(key, reqURIQueryReplPrefix) {
|
||||
vals := req.URL.Query()[key[len(reqURIQueryReplPrefix):]]
|
||||
// always return true, since the query param might
|
||||
// be present only in some requests
|
||||
return strings.Join(vals, ","), true
|
||||
}
|
||||
|
||||
// request header fields
|
||||
if strings.HasPrefix(key, reqHeaderReplPrefix) {
|
||||
field := key[len(reqHeaderReplPrefix):]
|
||||
vals := req.Header[textproto.CanonicalMIMEHeaderKey(field)]
|
||||
// always return true, since the header field might
|
||||
// be present only in some requests
|
||||
return strings.Join(vals, ","), true
|
||||
}
|
||||
|
||||
// cookies
|
||||
if strings.HasPrefix(key, reqCookieReplPrefix) {
|
||||
name := key[len(reqCookieReplPrefix):]
|
||||
for _, cookie := range req.Cookies() {
|
||||
if strings.EqualFold(name, cookie.Name) {
|
||||
// always return true, since the cookie might
|
||||
// be present only in some requests
|
||||
return cookie.Value, true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// http.request.tls.
|
||||
if strings.HasPrefix(key, reqTLSReplPrefix) {
|
||||
return getReqTLSReplacement(req, key)
|
||||
}
|
||||
|
||||
switch key {
|
||||
case "http.request.method":
|
||||
return req.Method, true
|
||||
case "http.request.scheme":
|
||||
if req.TLS != nil {
|
||||
return "https", true
|
||||
}
|
||||
return "http", true
|
||||
case "http.request.proto":
|
||||
return req.Proto, true
|
||||
case "http.request.host":
|
||||
host, _, err := net.SplitHostPort(req.Host)
|
||||
if err != nil {
|
||||
return req.Host, true // OK; there probably was no port
|
||||
}
|
||||
return host, true
|
||||
case "http.request.port":
|
||||
_, port, _ := net.SplitHostPort(req.Host)
|
||||
return port, true
|
||||
case "http.request.hostport":
|
||||
return req.Host, true
|
||||
case "http.request.remote":
|
||||
return req.RemoteAddr, true
|
||||
case "http.request.remote.host":
|
||||
host, _, err := net.SplitHostPort(req.RemoteAddr)
|
||||
if err != nil {
|
||||
return req.RemoteAddr, true
|
||||
}
|
||||
return host, true
|
||||
case "http.request.remote.port":
|
||||
_, port, _ := net.SplitHostPort(req.RemoteAddr)
|
||||
return port, true
|
||||
|
||||
// current URI, including any internal rewrites
|
||||
case "http.request.uri":
|
||||
return req.URL.RequestURI(), true
|
||||
case "http.request.uri.path":
|
||||
return req.URL.Path, true
|
||||
case "http.request.uri.path.file":
|
||||
_, file := path.Split(req.URL.Path)
|
||||
return file, true
|
||||
case "http.request.uri.path.dir":
|
||||
dir, _ := path.Split(req.URL.Path)
|
||||
return dir, true
|
||||
case "http.request.uri.query":
|
||||
return req.URL.RawQuery, true
|
||||
|
||||
// original request, before any internal changes
|
||||
case "http.request.orig_method":
|
||||
or, _ := req.Context().Value(OriginalRequestCtxKey).(http.Request)
|
||||
return or.Method, true
|
||||
case "http.request.orig_uri":
|
||||
or, _ := req.Context().Value(OriginalRequestCtxKey).(http.Request)
|
||||
return or.RequestURI, true
|
||||
case "http.request.orig_uri.path":
|
||||
or, _ := req.Context().Value(OriginalRequestCtxKey).(http.Request)
|
||||
return or.URL.Path, true
|
||||
case "http.request.orig_uri.path.file":
|
||||
or, _ := req.Context().Value(OriginalRequestCtxKey).(http.Request)
|
||||
_, file := path.Split(or.URL.Path)
|
||||
return file, true
|
||||
case "http.request.orig_uri.path.dir":
|
||||
or, _ := req.Context().Value(OriginalRequestCtxKey).(http.Request)
|
||||
dir, _ := path.Split(or.URL.Path)
|
||||
return dir, true
|
||||
case "http.request.orig_uri.query":
|
||||
or, _ := req.Context().Value(OriginalRequestCtxKey).(http.Request)
|
||||
return or.URL.RawQuery, true
|
||||
}
|
||||
|
||||
// hostname labels
|
||||
if strings.HasPrefix(key, reqHostLabelsReplPrefix) {
|
||||
idxStr := key[len(reqHostLabelsReplPrefix):]
|
||||
idx, err := strconv.Atoi(idxStr)
|
||||
if err != nil {
|
||||
return "", false
|
||||
}
|
||||
reqHost, _, err := net.SplitHostPort(req.Host)
|
||||
if err != nil {
|
||||
reqHost = req.Host // OK; assume there was no port
|
||||
}
|
||||
hostLabels := strings.Split(reqHost, ".")
|
||||
if idx < 0 {
|
||||
return "", false
|
||||
}
|
||||
if idx > len(hostLabels) {
|
||||
return "", true
|
||||
}
|
||||
return hostLabels[len(hostLabels)-idx-1], true
|
||||
}
|
||||
|
||||
// path parts
|
||||
if strings.HasPrefix(key, reqURIPathReplPrefix) {
|
||||
idxStr := key[len(reqURIPathReplPrefix):]
|
||||
idx, err := strconv.Atoi(idxStr)
|
||||
if err != nil {
|
||||
return "", false
|
||||
}
|
||||
pathParts := strings.Split(req.URL.Path, "/")
|
||||
if len(pathParts) > 0 && pathParts[0] == "" {
|
||||
pathParts = pathParts[1:]
|
||||
}
|
||||
if idx < 0 {
|
||||
return "", false
|
||||
}
|
||||
if idx >= len(pathParts) {
|
||||
return "", true
|
||||
}
|
||||
return pathParts[idx], true
|
||||
}
|
||||
|
||||
// middleware variables
|
||||
if strings.HasPrefix(key, varsReplPrefix) {
|
||||
varName := key[len(varsReplPrefix):]
|
||||
tbl := req.Context().Value(VarsCtxKey).(map[string]interface{})
|
||||
raw, ok := tbl[varName]
|
||||
if !ok {
|
||||
// variables can be dynamic, so always return true
|
||||
// even when it may not be set; treat as empty
|
||||
return "", true
|
||||
}
|
||||
// do our best to convert it to a string efficiently
|
||||
switch val := raw.(type) {
|
||||
case string:
|
||||
return val, true
|
||||
case fmt.Stringer:
|
||||
return val.String(), true
|
||||
default:
|
||||
return fmt.Sprintf("%s", val), true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if w != nil {
|
||||
// response header fields
|
||||
if strings.HasPrefix(key, respHeaderReplPrefix) {
|
||||
field := key[len(respHeaderReplPrefix):]
|
||||
vals := w.Header()[textproto.CanonicalMIMEHeaderKey(field)]
|
||||
// always return true, since the header field might
|
||||
// be present only in some responses
|
||||
return strings.Join(vals, ","), true
|
||||
}
|
||||
}
|
||||
|
||||
return "", false
|
||||
}
|
||||
|
||||
repl.Map(httpVars)
|
||||
}
|
||||
|
||||
func getReqTLSReplacement(req *http.Request, key string) (string, bool) {
|
||||
if req == nil || req.TLS == nil {
|
||||
return "", false
|
||||
}
|
||||
|
||||
if len(key) < len(reqTLSReplPrefix) {
|
||||
return "", false
|
||||
}
|
||||
|
||||
field := strings.ToLower(key[len(reqTLSReplPrefix):])
|
||||
|
||||
if strings.HasPrefix(field, "client.") {
|
||||
cert := getTLSPeerCert(req.TLS)
|
||||
if cert == nil {
|
||||
return "", false
|
||||
}
|
||||
|
||||
switch field {
|
||||
case "client.fingerprint":
|
||||
return fmt.Sprintf("%x", sha256.Sum256(cert.Raw)), true
|
||||
case "client.issuer":
|
||||
return cert.Issuer.String(), true
|
||||
case "client.serial":
|
||||
return fmt.Sprintf("%x", cert.SerialNumber), true
|
||||
case "client.subject":
|
||||
return cert.Subject.String(), true
|
||||
default:
|
||||
return "", false
|
||||
}
|
||||
}
|
||||
|
||||
switch field {
|
||||
case "version":
|
||||
return caddytls.ProtocolName(req.TLS.Version), true
|
||||
case "cipher_suite":
|
||||
return tls.CipherSuiteName(req.TLS.CipherSuite), true
|
||||
case "resumed":
|
||||
if req.TLS.DidResume {
|
||||
return "true", true
|
||||
}
|
||||
return "false", true
|
||||
case "proto":
|
||||
return req.TLS.NegotiatedProtocol, true
|
||||
case "proto_mutual":
|
||||
if req.TLS.NegotiatedProtocolIsMutual {
|
||||
return "true", true
|
||||
}
|
||||
return "false", true
|
||||
case "server_name":
|
||||
return req.TLS.ServerName, true
|
||||
default:
|
||||
return "", false
|
||||
}
|
||||
}
|
||||
|
||||
// getTLSPeerCert retrieves the first peer certificate from a TLS session.
|
||||
// Returns nil if no peer cert is in use.
|
||||
func getTLSPeerCert(cs *tls.ConnectionState) *x509.Certificate {
|
||||
if len(cs.PeerCertificates) == 0 {
|
||||
return nil
|
||||
}
|
||||
return cs.PeerCertificates[0]
|
||||
}
|
||||
|
||||
const (
|
||||
reqCookieReplPrefix = "http.request.cookie."
|
||||
reqHeaderReplPrefix = "http.request.header."
|
||||
reqHostLabelsReplPrefix = "http.request.host.labels."
|
||||
reqTLSReplPrefix = "http.request.tls."
|
||||
reqURIPathReplPrefix = "http.request.uri.path."
|
||||
reqURIQueryReplPrefix = "http.request.uri.query."
|
||||
respHeaderReplPrefix = "http.response.header."
|
||||
varsReplPrefix = "http.vars."
|
||||
)
|
||||
@@ -1,157 +0,0 @@
|
||||
// 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 caddyhttp
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"encoding/pem"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/caddyserver/caddy/v2"
|
||||
)
|
||||
|
||||
func TestHTTPVarReplacement(t *testing.T) {
|
||||
req, _ := http.NewRequest("GET", "/", nil)
|
||||
repl := caddy.NewReplacer()
|
||||
ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl)
|
||||
req = req.WithContext(ctx)
|
||||
req.Host = "example.com:80"
|
||||
req.RemoteAddr = "localhost:1234"
|
||||
|
||||
clientCert := []byte(`-----BEGIN CERTIFICATE-----
|
||||
MIIB9jCCAV+gAwIBAgIBAjANBgkqhkiG9w0BAQsFADAYMRYwFAYDVQQDDA1DYWRk
|
||||
eSBUZXN0IENBMB4XDTE4MDcyNDIxMzUwNVoXDTI4MDcyMTIxMzUwNVowHTEbMBkG
|
||||
A1UEAwwSY2xpZW50LmxvY2FsZG9tYWluMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCB
|
||||
iQKBgQDFDEpzF0ew68teT3xDzcUxVFaTII+jXH1ftHXxxP4BEYBU4q90qzeKFneF
|
||||
z83I0nC0WAQ45ZwHfhLMYHFzHPdxr6+jkvKPASf0J2v2HDJuTM1bHBbik5Ls5eq+
|
||||
fVZDP8o/VHKSBKxNs8Goc2NTsr5b07QTIpkRStQK+RJALk4x9QIDAQABo0swSTAJ
|
||||
BgNVHRMEAjAAMAsGA1UdDwQEAwIHgDAaBgNVHREEEzARgglsb2NhbGhvc3SHBH8A
|
||||
AAEwEwYDVR0lBAwwCgYIKwYBBQUHAwIwDQYJKoZIhvcNAQELBQADgYEANSjz2Sk+
|
||||
eqp31wM9il1n+guTNyxJd+FzVAH+hCZE5K+tCgVDdVFUlDEHHbS/wqb2PSIoouLV
|
||||
3Q9fgDkiUod+uIK0IynzIKvw+Cjg+3nx6NQ0IM0zo8c7v398RzB4apbXKZyeeqUH
|
||||
9fNwfEi+OoXR6s+upSKobCmLGLGi9Na5s5g=
|
||||
-----END CERTIFICATE-----`)
|
||||
|
||||
block, _ := pem.Decode(clientCert)
|
||||
if block == nil {
|
||||
t.Fatalf("failed to decode PEM certificate")
|
||||
}
|
||||
|
||||
cert, err := x509.ParseCertificate(block.Bytes)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to decode PEM certificate: %v", err)
|
||||
}
|
||||
|
||||
req.TLS = &tls.ConnectionState{
|
||||
Version: tls.VersionTLS13,
|
||||
HandshakeComplete: true,
|
||||
ServerName: "foo.com",
|
||||
CipherSuite: tls.TLS_AES_256_GCM_SHA384,
|
||||
PeerCertificates: []*x509.Certificate{cert},
|
||||
NegotiatedProtocol: "h2",
|
||||
NegotiatedProtocolIsMutual: true,
|
||||
}
|
||||
|
||||
res := httptest.NewRecorder()
|
||||
addHTTPVarsToReplacer(repl, req, res)
|
||||
|
||||
for i, tc := range []struct {
|
||||
input string
|
||||
expect string
|
||||
}{
|
||||
{
|
||||
input: "{http.request.scheme}",
|
||||
expect: "https",
|
||||
},
|
||||
{
|
||||
input: "{http.request.host}",
|
||||
expect: "example.com",
|
||||
},
|
||||
{
|
||||
input: "{http.request.port}",
|
||||
expect: "80",
|
||||
},
|
||||
{
|
||||
input: "{http.request.hostport}",
|
||||
expect: "example.com:80",
|
||||
},
|
||||
{
|
||||
input: "{http.request.remote.host}",
|
||||
expect: "localhost",
|
||||
},
|
||||
{
|
||||
input: "{http.request.remote.port}",
|
||||
expect: "1234",
|
||||
},
|
||||
{
|
||||
input: "{http.request.host.labels.0}",
|
||||
expect: "com",
|
||||
},
|
||||
{
|
||||
input: "{http.request.host.labels.1}",
|
||||
expect: "example",
|
||||
},
|
||||
{
|
||||
input: "{http.request.tls.cipher_suite}",
|
||||
expect: "TLS_AES_256_GCM_SHA384",
|
||||
},
|
||||
{
|
||||
input: "{http.request.tls.proto}",
|
||||
expect: "h2",
|
||||
},
|
||||
{
|
||||
input: "{http.request.tls.proto_mutual}",
|
||||
expect: "true",
|
||||
},
|
||||
{
|
||||
input: "{http.request.tls.resumed}",
|
||||
expect: "false",
|
||||
},
|
||||
{
|
||||
input: "{http.request.tls.server_name}",
|
||||
expect: "foo.com",
|
||||
},
|
||||
{
|
||||
input: "{http.request.tls.version}",
|
||||
expect: "tls1.3",
|
||||
},
|
||||
{
|
||||
input: "{http.request.tls.client.fingerprint}",
|
||||
expect: "9f57b7b497cceacc5459b76ac1c3afedbc12b300e728071f55f84168ff0f7702",
|
||||
},
|
||||
{
|
||||
input: "{http.request.tls.client.issuer}",
|
||||
expect: "CN=Caddy Test CA",
|
||||
},
|
||||
{
|
||||
input: "{http.request.tls.client.serial}",
|
||||
expect: "2",
|
||||
},
|
||||
{
|
||||
input: "{http.request.tls.client.subject}",
|
||||
expect: "CN=client.localdomain",
|
||||
},
|
||||
} {
|
||||
actual := repl.ReplaceAll(tc.input, "<empty>")
|
||||
if actual != tc.expect {
|
||||
t.Errorf("Test %d: Expected placeholder %s to be '%s' but got '%s'",
|
||||
i, tc.input, tc.expect, actual)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
// 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 requestbody
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/caddyserver/caddy/v2"
|
||||
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
|
||||
)
|
||||
|
||||
func init() {
|
||||
caddy.RegisterModule(RequestBody{})
|
||||
}
|
||||
|
||||
// RequestBody is a middleware for manipulating the request body.
|
||||
type RequestBody struct {
|
||||
// The maximum number of bytes to allow reading from the body by a later handler.
|
||||
MaxSize int64 `json:"max_size,omitempty"`
|
||||
}
|
||||
|
||||
// CaddyModule returns the Caddy module information.
|
||||
func (RequestBody) CaddyModule() caddy.ModuleInfo {
|
||||
return caddy.ModuleInfo{
|
||||
ID: "http.handlers.request_body", // TODO: better name for this?
|
||||
New: func() caddy.Module { return new(RequestBody) },
|
||||
}
|
||||
}
|
||||
|
||||
func (rb RequestBody) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
|
||||
if r.Body == nil {
|
||||
return next.ServeHTTP(w, r)
|
||||
}
|
||||
if rb.MaxSize > 0 {
|
||||
r.Body = http.MaxBytesReader(w, r.Body, rb.MaxSize)
|
||||
}
|
||||
return next.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
// Interface guard
|
||||
var _ caddyhttp.MiddlewareHandler = (*RequestBody)(nil)
|
||||
@@ -1,259 +0,0 @@
|
||||
// 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 caddyhttp
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// ResponseWriterWrapper wraps an underlying ResponseWriter and
|
||||
// promotes its Pusher/Flusher/Hijacker methods as well. To use
|
||||
// this type, embed a pointer to it within your own struct type
|
||||
// that implements the http.ResponseWriter interface, then call
|
||||
// methods on the embedded value. You can make sure your type
|
||||
// wraps correctly by asserting that it implements the
|
||||
// HTTPInterfaces interface.
|
||||
type ResponseWriterWrapper struct {
|
||||
http.ResponseWriter
|
||||
}
|
||||
|
||||
// Hijack implements http.Hijacker. It simply calls the underlying
|
||||
// ResponseWriter's Hijack method if there is one, or returns
|
||||
// ErrNotImplemented otherwise.
|
||||
func (rww *ResponseWriterWrapper) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
||||
if hj, ok := rww.ResponseWriter.(http.Hijacker); ok {
|
||||
return hj.Hijack()
|
||||
}
|
||||
return nil, nil, ErrNotImplemented
|
||||
}
|
||||
|
||||
// Flush implements http.Flusher. It simply calls the underlying
|
||||
// ResponseWriter's Flush method if there is one.
|
||||
func (rww *ResponseWriterWrapper) Flush() {
|
||||
if f, ok := rww.ResponseWriter.(http.Flusher); ok {
|
||||
f.Flush()
|
||||
}
|
||||
}
|
||||
|
||||
// Push implements http.Pusher. It simply calls the underlying
|
||||
// ResponseWriter's Push method if there is one, or returns
|
||||
// ErrNotImplemented otherwise.
|
||||
func (rww *ResponseWriterWrapper) Push(target string, opts *http.PushOptions) error {
|
||||
if pusher, ok := rww.ResponseWriter.(http.Pusher); ok {
|
||||
return pusher.Push(target, opts)
|
||||
}
|
||||
return ErrNotImplemented
|
||||
}
|
||||
|
||||
// HTTPInterfaces mix all the interfaces that middleware ResponseWriters need to support.
|
||||
type HTTPInterfaces interface {
|
||||
http.ResponseWriter
|
||||
http.Pusher
|
||||
http.Flusher
|
||||
http.Hijacker
|
||||
}
|
||||
|
||||
// ErrNotImplemented is returned when an underlying
|
||||
// ResponseWriter does not implement the required method.
|
||||
var ErrNotImplemented = fmt.Errorf("method not implemented")
|
||||
|
||||
type responseRecorder struct {
|
||||
*ResponseWriterWrapper
|
||||
wroteHeader bool
|
||||
statusCode int
|
||||
buf *bytes.Buffer
|
||||
shouldBuffer ShouldBufferFunc
|
||||
stream bool
|
||||
size int
|
||||
header http.Header
|
||||
}
|
||||
|
||||
// NewResponseRecorder returns a new ResponseRecorder that can be
|
||||
// used instead of a standard http.ResponseWriter. The recorder is
|
||||
// useful for middlewares which need to buffer a response and
|
||||
// potentially process its entire body before actually writing the
|
||||
// response to the underlying writer. Of course, buffering the entire
|
||||
// body has a memory overhead, but sometimes there is no way to avoid
|
||||
// buffering the whole response, hence the existence of this type.
|
||||
// Still, if at all practical, handlers should strive to stream
|
||||
// responses by wrapping Write and WriteHeader methods instead of
|
||||
// buffering whole response bodies.
|
||||
//
|
||||
// Buffering is actually optional. The shouldBuffer function will
|
||||
// be called just before the headers are written. If it returns
|
||||
// true, the headers and body will be buffered by this recorder
|
||||
// and not written to the underlying writer; if false, the headers
|
||||
// will be written immediately and the body will be streamed out
|
||||
// directly to the underlying writer. If shouldBuffer is nil,
|
||||
// the response will never be buffered and will always be streamed
|
||||
// directly to the writer.
|
||||
//
|
||||
// You can know if shouldBuffer returned true by calling Buffered().
|
||||
//
|
||||
// The provided buffer buf should be obtained from a pool for best
|
||||
// performance (see the sync.Pool type).
|
||||
//
|
||||
// Proper usage of a recorder looks like this:
|
||||
//
|
||||
// rec := caddyhttp.NewResponseRecorder(w, buf, shouldBuffer)
|
||||
// err := next.ServeHTTP(rec, req)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// if !rec.Buffered() {
|
||||
// return nil
|
||||
// }
|
||||
// // process the buffered response here
|
||||
//
|
||||
// After a response has been buffered, remember that any upstream header
|
||||
// manipulations are only manifest in the recorder's Header(), not the
|
||||
// Header() of the underlying ResponseWriter. Thus if you wish to inspect
|
||||
// or change response headers, you either need to use rec.Header(), or
|
||||
// copy rec.Header() into w.Header() first (see caddyhttp.CopyHeader).
|
||||
//
|
||||
// Once you are ready to write the response, there are two ways you can do
|
||||
// it. The easier way is to have the recorder do it:
|
||||
//
|
||||
// rec.WriteResponse()
|
||||
//
|
||||
// This writes the recorded response headers as well as the buffered body.
|
||||
// Or, you may wish to do it yourself, especially if you manipulated the
|
||||
// buffered body. First you will need to copy the recorded headers, then
|
||||
// write the headers with the recorded status code, then write the body
|
||||
// (this example writes the recorder's body buffer, but you might have
|
||||
// your own body to write instead):
|
||||
//
|
||||
// caddyhttp.CopyHeader(w.Header(), rec.Header())
|
||||
// w.WriteHeader(rec.Status())
|
||||
// io.Copy(w, rec.Buffer())
|
||||
//
|
||||
func NewResponseRecorder(w http.ResponseWriter, buf *bytes.Buffer, shouldBuffer ShouldBufferFunc) ResponseRecorder {
|
||||
// copy the current response header into this buffer so
|
||||
// that any header manipulations on the buffered header
|
||||
// are consistent with what would be written out
|
||||
hdr := make(http.Header)
|
||||
CopyHeader(hdr, w.Header())
|
||||
return &responseRecorder{
|
||||
ResponseWriterWrapper: &ResponseWriterWrapper{ResponseWriter: w},
|
||||
buf: buf,
|
||||
shouldBuffer: shouldBuffer,
|
||||
header: hdr,
|
||||
}
|
||||
}
|
||||
|
||||
func (rr *responseRecorder) Header() http.Header {
|
||||
return rr.header
|
||||
}
|
||||
|
||||
func (rr *responseRecorder) WriteHeader(statusCode int) {
|
||||
if rr.wroteHeader {
|
||||
return
|
||||
}
|
||||
rr.statusCode = statusCode
|
||||
rr.wroteHeader = true
|
||||
|
||||
// decide whether we should buffer the response
|
||||
if rr.shouldBuffer == nil {
|
||||
rr.stream = true
|
||||
} else {
|
||||
rr.stream = !rr.shouldBuffer(rr.statusCode, rr.header)
|
||||
}
|
||||
|
||||
// if not buffered, immediately write header
|
||||
if rr.stream {
|
||||
CopyHeader(rr.ResponseWriterWrapper.Header(), rr.header)
|
||||
rr.ResponseWriterWrapper.WriteHeader(rr.statusCode)
|
||||
}
|
||||
}
|
||||
|
||||
func (rr *responseRecorder) Write(data []byte) (int, error) {
|
||||
rr.WriteHeader(http.StatusOK)
|
||||
var n int
|
||||
var err error
|
||||
if rr.stream {
|
||||
n, err = rr.ResponseWriterWrapper.Write(data)
|
||||
} else {
|
||||
n, err = rr.buf.Write(data)
|
||||
}
|
||||
if err == nil {
|
||||
rr.size += n
|
||||
}
|
||||
return n, err
|
||||
}
|
||||
|
||||
// Status returns the status code that was written, if any.
|
||||
func (rr *responseRecorder) Status() int {
|
||||
return rr.statusCode
|
||||
}
|
||||
|
||||
// Size returns the number of bytes written,
|
||||
// not including the response headers.
|
||||
func (rr *responseRecorder) Size() int {
|
||||
return rr.size
|
||||
}
|
||||
|
||||
// Buffer returns the body buffer that rr was created with.
|
||||
// You should still have your original pointer, though.
|
||||
func (rr *responseRecorder) Buffer() *bytes.Buffer {
|
||||
return rr.buf
|
||||
}
|
||||
|
||||
// Buffered returns whether rr has decided to buffer the response.
|
||||
func (rr *responseRecorder) Buffered() bool {
|
||||
return !rr.stream
|
||||
}
|
||||
|
||||
func (rr *responseRecorder) WriteResponse() error {
|
||||
if rr.stream {
|
||||
return nil
|
||||
}
|
||||
CopyHeader(rr.ResponseWriterWrapper.Header(), rr.header)
|
||||
if rr.statusCode == 0 {
|
||||
// could happen if no handlers actually wrote anything,
|
||||
// and this prevents a panic; status must be > 0
|
||||
rr.statusCode = http.StatusOK
|
||||
}
|
||||
rr.ResponseWriterWrapper.WriteHeader(rr.statusCode)
|
||||
_, err := io.Copy(rr.ResponseWriterWrapper, rr.buf)
|
||||
return err
|
||||
}
|
||||
|
||||
// ResponseRecorder is a http.ResponseWriter that records
|
||||
// responses instead of writing them to the client. See
|
||||
// docs for NewResponseRecorder for proper usage.
|
||||
type ResponseRecorder interface {
|
||||
HTTPInterfaces
|
||||
Status() int
|
||||
Buffer() *bytes.Buffer
|
||||
Buffered() bool
|
||||
Size() int
|
||||
WriteResponse() error
|
||||
}
|
||||
|
||||
// ShouldBufferFunc is a function that returns true if the
|
||||
// response should be buffered, given the pending HTTP status
|
||||
// code and response headers.
|
||||
type ShouldBufferFunc func(status int, header http.Header) bool
|
||||
|
||||
// Interface guards
|
||||
var (
|
||||
_ HTTPInterfaces = (*ResponseWriterWrapper)(nil)
|
||||
_ ResponseRecorder = (*responseRecorder)(nil)
|
||||
)
|
||||
@@ -1,700 +0,0 @@
|
||||
// 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 reverseproxy
|
||||
|
||||
import (
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/caddyserver/caddy/v2"
|
||||
"github.com/caddyserver/caddy/v2/caddyconfig"
|
||||
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
|
||||
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
|
||||
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
|
||||
"github.com/caddyserver/caddy/v2/modules/caddyhttp/headers"
|
||||
"github.com/dustin/go-humanize"
|
||||
)
|
||||
|
||||
func init() {
|
||||
httpcaddyfile.RegisterHandlerDirective("reverse_proxy", parseCaddyfile)
|
||||
}
|
||||
|
||||
func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
|
||||
rp := new(Handler)
|
||||
err := rp.UnmarshalCaddyfile(h.Dispenser)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rp, nil
|
||||
}
|
||||
|
||||
// UnmarshalCaddyfile sets up the handler from Caddyfile tokens. Syntax:
|
||||
//
|
||||
// reverse_proxy [<matcher>] [<upstreams...>] {
|
||||
// # upstreams
|
||||
// to <upstreams...>
|
||||
//
|
||||
// # load balancing
|
||||
// lb_policy <name> [<options...>]
|
||||
// lb_try_duration <duration>
|
||||
// lb_try_interval <interval>
|
||||
//
|
||||
// # active health checking
|
||||
// health_path <path>
|
||||
// health_port <port>
|
||||
// health_interval <interval>
|
||||
// health_timeout <duration>
|
||||
// health_status <status>
|
||||
// health_body <regexp>
|
||||
//
|
||||
// # passive health checking
|
||||
// max_fails <num>
|
||||
// fail_duration <duration>
|
||||
// max_conns <num>
|
||||
// unhealthy_status <status>
|
||||
// unhealthy_latency <duration>
|
||||
//
|
||||
// # streaming
|
||||
// flush_interval <duration>
|
||||
//
|
||||
// # header manipulation
|
||||
// header_up [+|-]<field> [<value|regexp> [<replacement>]]
|
||||
// header_down [+|-]<field> [<value|regexp> [<replacement>]]
|
||||
//
|
||||
// # round trip
|
||||
// transport <name> {
|
||||
// ...
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// Proxy upstream addresses should be network dial addresses such
|
||||
// as `host:port`, or a URL such as `scheme://host:port`. Scheme
|
||||
// and port may be inferred from other parts of the address/URL; if
|
||||
// either are missing, defaults to HTTP.
|
||||
func (h *Handler) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
|
||||
// currently, all backends must use the same scheme/protocol (the
|
||||
// underlying JSON does not yet support per-backend transports)
|
||||
var commonScheme string
|
||||
|
||||
// we'll wait until the very end of parsing before
|
||||
// validating and encoding the transport
|
||||
var transport http.RoundTripper
|
||||
var transportModuleName string
|
||||
|
||||
// TODO: the logic in this function is kind of sensitive, we need
|
||||
// to write tests before making any more changes to it
|
||||
upstreamDialAddress := func(upstreamAddr string) (string, error) {
|
||||
var network, scheme, host, port string
|
||||
|
||||
if strings.Contains(upstreamAddr, "://") {
|
||||
toURL, err := url.Parse(upstreamAddr)
|
||||
if err != nil {
|
||||
return "", d.Errf("parsing upstream URL: %v", err)
|
||||
}
|
||||
|
||||
// there is currently no way to perform a URL rewrite between choosing
|
||||
// a backend and proxying to it, so we cannot allow extra components
|
||||
// in backend URLs
|
||||
if toURL.Path != "" || toURL.RawQuery != "" || toURL.Fragment != "" {
|
||||
return "", d.Err("for now, URLs for proxy upstreams only support scheme, host, and port components")
|
||||
}
|
||||
|
||||
// ensure the port and scheme aren't in conflict
|
||||
urlPort := toURL.Port()
|
||||
if toURL.Scheme == "http" && urlPort == "443" {
|
||||
return "", d.Err("upstream address has conflicting scheme (http://) and port (:443, the HTTPS port)")
|
||||
}
|
||||
if toURL.Scheme == "https" && urlPort == "80" {
|
||||
return "", d.Err("upstream address has conflicting scheme (https://) and port (:80, the HTTP port)")
|
||||
}
|
||||
|
||||
// if port is missing, attempt to infer from scheme
|
||||
if toURL.Port() == "" {
|
||||
var toPort string
|
||||
switch toURL.Scheme {
|
||||
case "", "http":
|
||||
toPort = "80"
|
||||
case "https":
|
||||
toPort = "443"
|
||||
}
|
||||
toURL.Host = net.JoinHostPort(toURL.Hostname(), toPort)
|
||||
}
|
||||
|
||||
scheme, host, port = toURL.Scheme, toURL.Hostname(), toURL.Port()
|
||||
} else {
|
||||
// extract network manually, since caddy.ParseNetworkAddress() will always add one
|
||||
if idx := strings.Index(upstreamAddr, "/"); idx >= 0 {
|
||||
network = strings.ToLower(strings.TrimSpace(upstreamAddr[:idx]))
|
||||
upstreamAddr = upstreamAddr[idx+1:]
|
||||
}
|
||||
var err error
|
||||
host, port, err = net.SplitHostPort(upstreamAddr)
|
||||
if err != nil {
|
||||
host = upstreamAddr
|
||||
}
|
||||
}
|
||||
|
||||
// if scheme is not set, we may be able to infer it from a known port
|
||||
if scheme == "" {
|
||||
if port == "80" {
|
||||
scheme = "http"
|
||||
} else if port == "443" {
|
||||
scheme = "https"
|
||||
}
|
||||
}
|
||||
|
||||
// the underlying JSON does not yet support different
|
||||
// transports (protocols or schemes) to each backend,
|
||||
// so we remember the last one we see and compare them
|
||||
if commonScheme != "" && scheme != commonScheme {
|
||||
return "", d.Errf("for now, all proxy upstreams must use the same scheme (transport protocol); expecting '%s://' but got '%s://'",
|
||||
commonScheme, scheme)
|
||||
}
|
||||
commonScheme = scheme
|
||||
|
||||
// for simplest possible config, we only need to include
|
||||
// the network portion if the user specified one
|
||||
if network != "" {
|
||||
return caddy.JoinNetworkAddress(network, host, port), nil
|
||||
}
|
||||
return net.JoinHostPort(host, port), nil
|
||||
}
|
||||
|
||||
for d.Next() {
|
||||
for _, up := range d.RemainingArgs() {
|
||||
dialAddr, err := upstreamDialAddress(up)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
h.Upstreams = append(h.Upstreams, &Upstream{Dial: dialAddr})
|
||||
}
|
||||
|
||||
for d.NextBlock(0) {
|
||||
switch d.Val() {
|
||||
case "to":
|
||||
args := d.RemainingArgs()
|
||||
if len(args) == 0 {
|
||||
return d.ArgErr()
|
||||
}
|
||||
for _, up := range args {
|
||||
dialAddr, err := upstreamDialAddress(up)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
h.Upstreams = append(h.Upstreams, &Upstream{Dial: dialAddr})
|
||||
}
|
||||
|
||||
case "lb_policy":
|
||||
if !d.NextArg() {
|
||||
return d.ArgErr()
|
||||
}
|
||||
if h.LoadBalancing != nil && h.LoadBalancing.SelectionPolicyRaw != nil {
|
||||
return d.Err("load balancing selection policy already specified")
|
||||
}
|
||||
name := d.Val()
|
||||
mod, err := caddy.GetModule("http.reverse_proxy.selection_policies." + name)
|
||||
if err != nil {
|
||||
return d.Errf("getting load balancing policy module '%s': %v", mod, err)
|
||||
}
|
||||
unm, ok := mod.New().(caddyfile.Unmarshaler)
|
||||
if !ok {
|
||||
return d.Errf("load balancing policy module '%s' is not a Caddyfile unmarshaler", mod)
|
||||
}
|
||||
err = unm.UnmarshalCaddyfile(d.NewFromNextSegment())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sel, ok := unm.(Selector)
|
||||
if !ok {
|
||||
return d.Errf("module %s is not a Selector", mod)
|
||||
}
|
||||
if h.LoadBalancing == nil {
|
||||
h.LoadBalancing = new(LoadBalancing)
|
||||
}
|
||||
h.LoadBalancing.SelectionPolicyRaw = caddyconfig.JSONModuleObject(sel, "policy", name, nil)
|
||||
|
||||
case "lb_try_duration":
|
||||
if !d.NextArg() {
|
||||
return d.ArgErr()
|
||||
}
|
||||
if h.LoadBalancing == nil {
|
||||
h.LoadBalancing = new(LoadBalancing)
|
||||
}
|
||||
dur, err := time.ParseDuration(d.Val())
|
||||
if err != nil {
|
||||
return d.Errf("bad duration value %s: %v", d.Val(), err)
|
||||
}
|
||||
h.LoadBalancing.TryDuration = caddy.Duration(dur)
|
||||
|
||||
case "lb_try_interval":
|
||||
if !d.NextArg() {
|
||||
return d.ArgErr()
|
||||
}
|
||||
if h.LoadBalancing == nil {
|
||||
h.LoadBalancing = new(LoadBalancing)
|
||||
}
|
||||
dur, err := time.ParseDuration(d.Val())
|
||||
if err != nil {
|
||||
return d.Errf("bad interval value '%s': %v", d.Val(), err)
|
||||
}
|
||||
h.LoadBalancing.TryInterval = caddy.Duration(dur)
|
||||
|
||||
case "health_path":
|
||||
if !d.NextArg() {
|
||||
return d.ArgErr()
|
||||
}
|
||||
if h.HealthChecks == nil {
|
||||
h.HealthChecks = new(HealthChecks)
|
||||
}
|
||||
if h.HealthChecks.Active == nil {
|
||||
h.HealthChecks.Active = new(ActiveHealthChecks)
|
||||
}
|
||||
h.HealthChecks.Active.Path = d.Val()
|
||||
|
||||
case "health_port":
|
||||
if !d.NextArg() {
|
||||
return d.ArgErr()
|
||||
}
|
||||
if h.HealthChecks == nil {
|
||||
h.HealthChecks = new(HealthChecks)
|
||||
}
|
||||
if h.HealthChecks.Active == nil {
|
||||
h.HealthChecks.Active = new(ActiveHealthChecks)
|
||||
}
|
||||
portNum, err := strconv.Atoi(d.Val())
|
||||
if err != nil {
|
||||
return d.Errf("bad port number '%s': %v", d.Val(), err)
|
||||
}
|
||||
h.HealthChecks.Active.Port = portNum
|
||||
|
||||
case "health_interval":
|
||||
if !d.NextArg() {
|
||||
return d.ArgErr()
|
||||
}
|
||||
if h.HealthChecks == nil {
|
||||
h.HealthChecks = new(HealthChecks)
|
||||
}
|
||||
if h.HealthChecks.Active == nil {
|
||||
h.HealthChecks.Active = new(ActiveHealthChecks)
|
||||
}
|
||||
dur, err := time.ParseDuration(d.Val())
|
||||
if err != nil {
|
||||
return d.Errf("bad interval value %s: %v", d.Val(), err)
|
||||
}
|
||||
h.HealthChecks.Active.Interval = caddy.Duration(dur)
|
||||
|
||||
case "health_timeout":
|
||||
if !d.NextArg() {
|
||||
return d.ArgErr()
|
||||
}
|
||||
if h.HealthChecks == nil {
|
||||
h.HealthChecks = new(HealthChecks)
|
||||
}
|
||||
if h.HealthChecks.Active == nil {
|
||||
h.HealthChecks.Active = new(ActiveHealthChecks)
|
||||
}
|
||||
dur, err := time.ParseDuration(d.Val())
|
||||
if err != nil {
|
||||
return d.Errf("bad timeout value %s: %v", d.Val(), err)
|
||||
}
|
||||
h.HealthChecks.Active.Timeout = caddy.Duration(dur)
|
||||
|
||||
case "health_status":
|
||||
if !d.NextArg() {
|
||||
return d.ArgErr()
|
||||
}
|
||||
if h.HealthChecks == nil {
|
||||
h.HealthChecks = new(HealthChecks)
|
||||
}
|
||||
if h.HealthChecks.Active == nil {
|
||||
h.HealthChecks.Active = new(ActiveHealthChecks)
|
||||
}
|
||||
val := d.Val()
|
||||
if len(val) == 3 && strings.HasSuffix(val, "xx") {
|
||||
val = val[:1]
|
||||
}
|
||||
statusNum, err := strconv.Atoi(val[:1])
|
||||
if err != nil {
|
||||
return d.Errf("bad status value '%s': %v", d.Val(), err)
|
||||
}
|
||||
h.HealthChecks.Active.ExpectStatus = statusNum
|
||||
|
||||
case "health_body":
|
||||
if !d.NextArg() {
|
||||
return d.ArgErr()
|
||||
}
|
||||
if h.HealthChecks == nil {
|
||||
h.HealthChecks = new(HealthChecks)
|
||||
}
|
||||
if h.HealthChecks.Active == nil {
|
||||
h.HealthChecks.Active = new(ActiveHealthChecks)
|
||||
}
|
||||
h.HealthChecks.Active.ExpectBody = d.Val()
|
||||
|
||||
case "max_fails":
|
||||
if !d.NextArg() {
|
||||
return d.ArgErr()
|
||||
}
|
||||
if h.HealthChecks == nil {
|
||||
h.HealthChecks = new(HealthChecks)
|
||||
}
|
||||
if h.HealthChecks.Passive == nil {
|
||||
h.HealthChecks.Passive = new(PassiveHealthChecks)
|
||||
}
|
||||
maxFails, err := strconv.Atoi(d.Val())
|
||||
if err != nil {
|
||||
return d.Errf("invalid maximum fail count '%s': %v", d.Val(), err)
|
||||
}
|
||||
h.HealthChecks.Passive.MaxFails = maxFails
|
||||
|
||||
case "fail_duration":
|
||||
if !d.NextArg() {
|
||||
return d.ArgErr()
|
||||
}
|
||||
if h.HealthChecks == nil {
|
||||
h.HealthChecks = new(HealthChecks)
|
||||
}
|
||||
if h.HealthChecks.Passive == nil {
|
||||
h.HealthChecks.Passive = new(PassiveHealthChecks)
|
||||
}
|
||||
dur, err := time.ParseDuration(d.Val())
|
||||
if err != nil {
|
||||
return d.Errf("bad duration value '%s': %v", d.Val(), err)
|
||||
}
|
||||
h.HealthChecks.Passive.FailDuration = caddy.Duration(dur)
|
||||
|
||||
case "unhealthy_request_count":
|
||||
if !d.NextArg() {
|
||||
return d.ArgErr()
|
||||
}
|
||||
if h.HealthChecks == nil {
|
||||
h.HealthChecks = new(HealthChecks)
|
||||
}
|
||||
if h.HealthChecks.Passive == nil {
|
||||
h.HealthChecks.Passive = new(PassiveHealthChecks)
|
||||
}
|
||||
maxConns, err := strconv.Atoi(d.Val())
|
||||
if err != nil {
|
||||
return d.Errf("invalid maximum connection count '%s': %v", d.Val(), err)
|
||||
}
|
||||
h.HealthChecks.Passive.UnhealthyRequestCount = maxConns
|
||||
|
||||
case "unhealthy_status":
|
||||
args := d.RemainingArgs()
|
||||
if len(args) == 0 {
|
||||
return d.ArgErr()
|
||||
}
|
||||
if h.HealthChecks == nil {
|
||||
h.HealthChecks = new(HealthChecks)
|
||||
}
|
||||
if h.HealthChecks.Passive == nil {
|
||||
h.HealthChecks.Passive = new(PassiveHealthChecks)
|
||||
}
|
||||
for _, arg := range args {
|
||||
if len(arg) == 3 && strings.HasSuffix(arg, "xx") {
|
||||
arg = arg[:1]
|
||||
}
|
||||
statusNum, err := strconv.Atoi(arg[:1])
|
||||
if err != nil {
|
||||
return d.Errf("bad status value '%s': %v", d.Val(), err)
|
||||
}
|
||||
h.HealthChecks.Passive.UnhealthyStatus = append(h.HealthChecks.Passive.UnhealthyStatus, statusNum)
|
||||
}
|
||||
|
||||
case "unhealthy_latency":
|
||||
if !d.NextArg() {
|
||||
return d.ArgErr()
|
||||
}
|
||||
if h.HealthChecks == nil {
|
||||
h.HealthChecks = new(HealthChecks)
|
||||
}
|
||||
if h.HealthChecks.Passive == nil {
|
||||
h.HealthChecks.Passive = new(PassiveHealthChecks)
|
||||
}
|
||||
dur, err := time.ParseDuration(d.Val())
|
||||
if err != nil {
|
||||
return d.Errf("bad duration value '%s': %v", d.Val(), err)
|
||||
}
|
||||
h.HealthChecks.Passive.UnhealthyLatency = caddy.Duration(dur)
|
||||
|
||||
case "flush_interval":
|
||||
if !d.NextArg() {
|
||||
return d.ArgErr()
|
||||
}
|
||||
if fi, err := strconv.Atoi(d.Val()); err == nil {
|
||||
h.FlushInterval = caddy.Duration(fi)
|
||||
} else {
|
||||
dur, err := time.ParseDuration(d.Val())
|
||||
if err != nil {
|
||||
return d.Errf("bad duration value '%s': %v", d.Val(), err)
|
||||
}
|
||||
h.FlushInterval = caddy.Duration(dur)
|
||||
}
|
||||
|
||||
case "header_up":
|
||||
if h.Headers == nil {
|
||||
h.Headers = new(headers.Handler)
|
||||
}
|
||||
if h.Headers.Request == nil {
|
||||
h.Headers.Request = new(headers.HeaderOps)
|
||||
}
|
||||
args := d.RemainingArgs()
|
||||
switch len(args) {
|
||||
case 1:
|
||||
headers.CaddyfileHeaderOp(h.Headers.Request, args[0], "", "")
|
||||
case 2:
|
||||
headers.CaddyfileHeaderOp(h.Headers.Request, args[0], args[1], "")
|
||||
case 3:
|
||||
headers.CaddyfileHeaderOp(h.Headers.Request, args[0], args[1], args[2])
|
||||
default:
|
||||
return d.ArgErr()
|
||||
}
|
||||
|
||||
case "header_down":
|
||||
if h.Headers == nil {
|
||||
h.Headers = new(headers.Handler)
|
||||
}
|
||||
if h.Headers.Response == nil {
|
||||
h.Headers.Response = &headers.RespHeaderOps{
|
||||
HeaderOps: new(headers.HeaderOps),
|
||||
}
|
||||
}
|
||||
args := d.RemainingArgs()
|
||||
switch len(args) {
|
||||
case 1:
|
||||
headers.CaddyfileHeaderOp(h.Headers.Response.HeaderOps, args[0], "", "")
|
||||
case 2:
|
||||
headers.CaddyfileHeaderOp(h.Headers.Response.HeaderOps, args[0], args[1], "")
|
||||
case 3:
|
||||
headers.CaddyfileHeaderOp(h.Headers.Response.HeaderOps, args[0], args[1], args[2])
|
||||
default:
|
||||
return d.ArgErr()
|
||||
}
|
||||
|
||||
case "transport":
|
||||
if !d.NextArg() {
|
||||
return d.ArgErr()
|
||||
}
|
||||
if h.TransportRaw != nil {
|
||||
return d.Err("transport already specified")
|
||||
}
|
||||
transportModuleName = d.Val()
|
||||
mod, err := caddy.GetModule("http.reverse_proxy.transport." + transportModuleName)
|
||||
if err != nil {
|
||||
return d.Errf("getting transport module '%s': %v", mod, err)
|
||||
}
|
||||
unm, ok := mod.New().(caddyfile.Unmarshaler)
|
||||
if !ok {
|
||||
return d.Errf("transport module '%s' is not a Caddyfile unmarshaler", mod)
|
||||
}
|
||||
err = unm.UnmarshalCaddyfile(d.NewFromNextSegment())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rt, ok := unm.(http.RoundTripper)
|
||||
if !ok {
|
||||
return d.Errf("module %s is not a RoundTripper", mod)
|
||||
}
|
||||
transport = rt
|
||||
|
||||
default:
|
||||
return d.Errf("unrecognized subdirective %s", d.Val())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// if the scheme inferred from the backends' addresses is
|
||||
// HTTPS, we will need a non-nil transport to enable TLS
|
||||
if commonScheme == "https" && transport == nil {
|
||||
transport = new(HTTPTransport)
|
||||
transportModuleName = "http"
|
||||
}
|
||||
|
||||
// verify transport configuration, and finally encode it
|
||||
if transport != nil {
|
||||
// TODO: these two cases are identical, but I don't know how to reuse the code
|
||||
switch ht := transport.(type) {
|
||||
case *HTTPTransport:
|
||||
if commonScheme == "https" && ht.TLS == nil {
|
||||
ht.TLS = new(TLSConfig)
|
||||
}
|
||||
if ht.TLS != nil && commonScheme == "http" {
|
||||
return d.Errf("upstream address scheme is HTTP but transport is configured for HTTP+TLS (HTTPS)")
|
||||
}
|
||||
|
||||
case *NTLMTransport:
|
||||
if commonScheme == "https" && ht.TLS == nil {
|
||||
ht.TLS = new(TLSConfig)
|
||||
}
|
||||
if ht.TLS != nil && commonScheme == "http" {
|
||||
return d.Errf("upstream address scheme is HTTP but transport is configured for HTTP+TLS (HTTPS)")
|
||||
}
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(transport, new(HTTPTransport)) {
|
||||
h.TransportRaw = caddyconfig.JSONModuleObject(transport, "protocol", transportModuleName, nil)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalCaddyfile deserializes Caddyfile tokens into h.
|
||||
//
|
||||
// transport http {
|
||||
// read_buffer <size>
|
||||
// write_buffer <size>
|
||||
// dial_timeout <duration>
|
||||
// tls_client_auth <cert_file> <key_file>
|
||||
// tls_insecure_skip_verify
|
||||
// tls_timeout <duration>
|
||||
// tls_trusted_ca_certs <cert_files...>
|
||||
// keepalive [off|<duration>]
|
||||
// keepalive_idle_conns <max_count>
|
||||
// }
|
||||
//
|
||||
func (h *HTTPTransport) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
|
||||
for d.Next() {
|
||||
for d.NextBlock(0) {
|
||||
switch d.Val() {
|
||||
case "read_buffer":
|
||||
if !d.NextArg() {
|
||||
return d.ArgErr()
|
||||
}
|
||||
size, err := humanize.ParseBytes(d.Val())
|
||||
if err != nil {
|
||||
return d.Errf("invalid read buffer size '%s': %v", d.Val(), err)
|
||||
}
|
||||
h.ReadBufferSize = int(size)
|
||||
|
||||
case "write_buffer":
|
||||
if !d.NextArg() {
|
||||
return d.ArgErr()
|
||||
}
|
||||
size, err := humanize.ParseBytes(d.Val())
|
||||
if err != nil {
|
||||
return d.Errf("invalid write buffer size '%s': %v", d.Val(), err)
|
||||
}
|
||||
h.WriteBufferSize = int(size)
|
||||
|
||||
case "dial_timeout":
|
||||
if !d.NextArg() {
|
||||
return d.ArgErr()
|
||||
}
|
||||
dur, err := time.ParseDuration(d.Val())
|
||||
if err != nil {
|
||||
return d.Errf("bad timeout value '%s': %v", d.Val(), err)
|
||||
}
|
||||
h.DialTimeout = caddy.Duration(dur)
|
||||
|
||||
case "tls_client_auth":
|
||||
args := d.RemainingArgs()
|
||||
if len(args) != 2 {
|
||||
return d.ArgErr()
|
||||
}
|
||||
if h.TLS == nil {
|
||||
h.TLS = new(TLSConfig)
|
||||
}
|
||||
h.TLS.ClientCertificateFile = args[0]
|
||||
h.TLS.ClientCertificateKeyFile = args[1]
|
||||
|
||||
case "tls":
|
||||
if h.TLS == nil {
|
||||
h.TLS = new(TLSConfig)
|
||||
}
|
||||
|
||||
case "tls_insecure_skip_verify":
|
||||
if d.NextArg() {
|
||||
return d.ArgErr()
|
||||
}
|
||||
if h.TLS == nil {
|
||||
h.TLS = new(TLSConfig)
|
||||
}
|
||||
h.TLS.InsecureSkipVerify = true
|
||||
|
||||
case "tls_timeout":
|
||||
if !d.NextArg() {
|
||||
return d.ArgErr()
|
||||
}
|
||||
dur, err := time.ParseDuration(d.Val())
|
||||
if err != nil {
|
||||
return d.Errf("bad timeout value '%s': %v", d.Val(), err)
|
||||
}
|
||||
if h.TLS == nil {
|
||||
h.TLS = new(TLSConfig)
|
||||
}
|
||||
h.TLS.HandshakeTimeout = caddy.Duration(dur)
|
||||
|
||||
case "tls_trusted_ca_certs":
|
||||
args := d.RemainingArgs()
|
||||
if len(args) == 0 {
|
||||
return d.ArgErr()
|
||||
}
|
||||
if h.TLS == nil {
|
||||
h.TLS = new(TLSConfig)
|
||||
}
|
||||
|
||||
h.TLS.RootCAPEMFiles = args
|
||||
|
||||
case "keepalive":
|
||||
if !d.NextArg() {
|
||||
return d.ArgErr()
|
||||
}
|
||||
if h.KeepAlive == nil {
|
||||
h.KeepAlive = new(KeepAlive)
|
||||
}
|
||||
if d.Val() == "off" {
|
||||
var disable bool
|
||||
h.KeepAlive.Enabled = &disable
|
||||
break
|
||||
}
|
||||
dur, err := time.ParseDuration(d.Val())
|
||||
if err != nil {
|
||||
return d.Errf("bad duration value '%s': %v", d.Val(), err)
|
||||
}
|
||||
h.KeepAlive.IdleConnTimeout = caddy.Duration(dur)
|
||||
|
||||
case "keepalive_idle_conns":
|
||||
if !d.NextArg() {
|
||||
return d.ArgErr()
|
||||
}
|
||||
num, err := strconv.Atoi(d.Val())
|
||||
if err != nil {
|
||||
return d.Errf("bad integer value '%s': %v", d.Val(), err)
|
||||
}
|
||||
if h.KeepAlive == nil {
|
||||
h.KeepAlive = new(KeepAlive)
|
||||
}
|
||||
h.KeepAlive.MaxIdleConns = num
|
||||
h.KeepAlive.MaxIdleConnsPerHost = num
|
||||
|
||||
default:
|
||||
return d.Errf("unrecognized subdirective %s", d.Val())
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Interface guards
|
||||
var (
|
||||
_ caddyfile.Unmarshaler = (*Handler)(nil)
|
||||
_ caddyfile.Unmarshaler = (*HTTPTransport)(nil)
|
||||
)
|
||||
@@ -1,157 +0,0 @@
|
||||
// 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 reverseproxy
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/caddyserver/caddy/v2"
|
||||
"github.com/vulcand/oxy/memmetrics"
|
||||
)
|
||||
|
||||
func init() {
|
||||
caddy.RegisterModule(localCircuitBreaker{})
|
||||
}
|
||||
|
||||
// localCircuitBreaker implements circuit breaking functionality
|
||||
// for requests within this process over a sliding time window.
|
||||
type localCircuitBreaker struct {
|
||||
tripped int32
|
||||
cbFactor int32
|
||||
threshold float64
|
||||
metrics *memmetrics.RTMetrics
|
||||
tripTime time.Duration
|
||||
Config
|
||||
}
|
||||
|
||||
// CaddyModule returns the Caddy module information.
|
||||
func (localCircuitBreaker) CaddyModule() caddy.ModuleInfo {
|
||||
return caddy.ModuleInfo{
|
||||
ID: "http.reverse_proxy.circuit_breakers.local",
|
||||
New: func() caddy.Module { return new(localCircuitBreaker) },
|
||||
}
|
||||
}
|
||||
|
||||
// Provision sets up a configured circuit breaker.
|
||||
func (c *localCircuitBreaker) Provision(ctx caddy.Context) error {
|
||||
f, ok := typeCB[c.Factor]
|
||||
if !ok {
|
||||
return fmt.Errorf("type is not defined")
|
||||
}
|
||||
|
||||
if c.TripTime == "" {
|
||||
c.TripTime = defaultTripTime
|
||||
}
|
||||
|
||||
tw, err := time.ParseDuration(c.TripTime)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot parse trip_time duration, %v", err.Error())
|
||||
}
|
||||
|
||||
mt, err := memmetrics.NewRTMetrics()
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot create new metrics: %v", err.Error())
|
||||
}
|
||||
|
||||
c.cbFactor = f
|
||||
c.tripTime = tw
|
||||
c.threshold = c.Threshold
|
||||
c.metrics = mt
|
||||
c.tripped = 0
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Ok returns whether the circuit breaker is tripped or not.
|
||||
func (c *localCircuitBreaker) Ok() bool {
|
||||
tripped := atomic.LoadInt32(&c.tripped)
|
||||
return tripped == 0
|
||||
}
|
||||
|
||||
// RecordMetric records a response status code and execution time of a request. This function should be run in a separate goroutine.
|
||||
func (c *localCircuitBreaker) RecordMetric(statusCode int, latency time.Duration) {
|
||||
c.metrics.Record(statusCode, latency)
|
||||
c.checkAndSet()
|
||||
}
|
||||
|
||||
// Ok checks our metrics to see if we should trip our circuit breaker, or if the fallback duration has completed.
|
||||
func (c *localCircuitBreaker) checkAndSet() {
|
||||
var isTripped bool
|
||||
|
||||
switch c.cbFactor {
|
||||
case factorErrorRatio:
|
||||
// check if amount of network errors exceed threshold over sliding window, threshold for comparison should be < 1.0 i.e. .5 = 50th percentile
|
||||
if c.metrics.NetworkErrorRatio() > c.threshold {
|
||||
isTripped = true
|
||||
}
|
||||
case factorLatency:
|
||||
// check if threshold in milliseconds is reached and trip
|
||||
hist, err := c.metrics.LatencyHistogram()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
l := hist.LatencyAtQuantile(c.threshold)
|
||||
if l.Nanoseconds()/int64(time.Millisecond) > int64(c.threshold) {
|
||||
isTripped = true
|
||||
}
|
||||
case factorStatusCodeRatio:
|
||||
// check ratio of error status codes of sliding window, threshold for comparison should be < 1.0 i.e. .5 = 50th percentile
|
||||
if c.metrics.ResponseCodeRatio(500, 600, 0, 600) > c.threshold {
|
||||
isTripped = true
|
||||
}
|
||||
}
|
||||
|
||||
if isTripped {
|
||||
c.metrics.Reset()
|
||||
atomic.AddInt32(&c.tripped, 1)
|
||||
|
||||
// wait tripTime amount before allowing operations to resume.
|
||||
t := time.NewTimer(c.tripTime)
|
||||
<-t.C
|
||||
|
||||
atomic.AddInt32(&c.tripped, -1)
|
||||
}
|
||||
}
|
||||
|
||||
// Config represents the configuration of a circuit breaker.
|
||||
type Config struct {
|
||||
// The threshold over sliding window that would trip the circuit breaker
|
||||
Threshold float64 `json:"threshold"`
|
||||
// Possible values: latency, error_ratio, and status_ratio. It
|
||||
// defaults to latency.
|
||||
Factor string `json:"factor"`
|
||||
// How long to wait after the circuit is tripped before allowing operations to resume.
|
||||
// The default is 5s.
|
||||
TripTime string `json:"trip_time"`
|
||||
}
|
||||
|
||||
const (
|
||||
factorLatency = iota + 1
|
||||
factorErrorRatio
|
||||
factorStatusCodeRatio
|
||||
defaultTripTime = "5s"
|
||||
)
|
||||
|
||||
var (
|
||||
// typeCB handles converting a Config Factor value to the internal circuit breaker types.
|
||||
typeCB = map[string]int32{
|
||||
"latency": factorLatency,
|
||||
"error_ratio": factorErrorRatio,
|
||||
"status_ratio": factorStatusCodeRatio,
|
||||
}
|
||||
)
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user