@marcel91200 / caddy Mirror · read-only / commits / a458544d
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 14, 05:15 UTC

Minor enhancements/fixes to rewrite directive and template virt req's

Matthew Holt committed Oct 16, 2019 at 15:18 UTC a458544d9f6e6aaf72aeab0454acfa482880d3d6
3 files changed +27 -7
README.md
+7 -4
@@ -25,7 +25,7 @@ Please file issues to propose new features and report bugs, and after the bug or
25 Requirements:
26
27 - [Go 1.13 or newer](https://golang.org/dl/)
28 -- Make sure you do not disable [Go modules](https://github.com/golang/go/wiki/Modules) (`export GO111MODULE=auto`)
28 +- Do NOT disable [Go modules](https://github.com/golang/go/wiki/Modules) (`export GO111MODULE=auto`)
29
30 Download the `v2` source code:
31
@@ -120,10 +120,10 @@ Caddy 2 can be configured with a Caddyfile, much like in v1, for example:
120 ```plain
121 example.com
122
123 -templates
124 -encode gzip zstd
123 try_files {path}.html {path}
126 -reverse_proxy /api localhost:9005
124 +encode gzip zstd
125 +reverse_proxy /api localhost:9005
126 +php_fastcgi /blog unix//path/to/socket
127 file_server
128 ```
129
@@ -199,6 +199,7 @@ The following is a non-comprehensive list of significant improvements over Caddy
199 - TLS Session Ticket Ephemeral Keys (STEKs) can be rotated in a cluster for increased performance (no other web server does this either!)
200 - Ability to select a specific certificate per ClientHello given multiple qualifying certificates
201 - Provide TLS certificates without persisting them to disk; keep private keys entirely in memory
202 + - Certificate management at startup is now asynchronous and much easier to use through machine reboots and in unsupervised settings
203 - All-new HTTP server core
204 - Listeners can be configured for any network type, address, and port range
205 - Customizable TLS connection policies
@@ -218,6 +219,8 @@ The following is a non-comprehensive list of significant improvements over Caddy
219 - Done away with URL-rewriting hacks often needed in Caddy 1
220 - Highly descriptive/traceable errors
221 - Very flexible error handling, with the ability to specify a whole list of routes just for error cases
222 + - The proxy has numerous improvements, including dynamic backends and more configurable health checks
223 + - FastCGI support integrated with the reverse proxy
224 - More control over automatic HTTPS: disable entirely, disable only HTTP->HTTPS redirects, disable only cert management, and for certain names, etc.
225 - Use Starlark to build custom, dynamic HTTP handlers at request-time
226 - We are finding that -- on average -- Caddy 2's Starlark handlers are ~1.25-2x faster than NGINX+Lua.
modules/caddyhttp/rewrite/caddyfile.go
+3
@@ -31,6 +31,9 @@ func init() {
31 func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
32 var rewr Rewrite
33 for h.Next() {
34 + if !h.NextArg() {
35 + return nil, h.ArgErr()
36 + }
37 rewr.URI = h.Val()
38 if h.NextArg() {
39 return nil, h.ArgErr()
modules/caddyhttp/templates/tplcontext.go
+17 -3
@@ -22,6 +22,7 @@ import (
22 "net"
23 "net/http"
24 "path"
25 + "strconv"
26 "strings"
27 "sync"
28
@@ -79,8 +80,18 @@ func (c templateContext) Include(filename string, args ...interface{}) (template
80 // are NOT escaped, so you should only include trusted resources.
81 // If it is not trusted, be sure to use escaping functions yourself.
82 func (c templateContext) HTTPInclude(uri string) (template.HTML, error) {
82 - if c.Req.Header.Get(recursionPreventionHeader) == "1" {
83 - return "", fmt.Errorf("virtual request cycle")
83 + // prevent virtual request loops by counting how many levels
84 + // deep we are; and if we get too deep, return an error
85 + recursionCount := 1
86 + if numStr := c.Req.Header.Get(recursionPreventionHeader); numStr != "" {
87 + num, err := strconv.Atoi(numStr)
88 + if err != nil {
89 + return "", fmt.Errorf("parsing %s: %v", recursionPreventionHeader, err)
90 + }
91 + if num >= 3 {
92 + return "", fmt.Errorf("virtual request cycle")
93 + }
94 + recursionCount = num + 1
95 }
96
97 buf := bufPool.Get().(*bytes.Buffer)
@@ -91,7 +102,10 @@ func (c templateContext) HTTPInclude(uri string) (template.HTML, error) {
102 if err != nil {
103 return "", err
104 }
94 - virtReq.Header.Set(recursionPreventionHeader, "1")
105 + virtReq.Host = c.Req.Host
106 + virtReq.Header = c.Req.Header.Clone()
107 + virtReq.Trailer = c.Req.Trailer.Clone()
108 + virtReq.Header.Set(recursionPreventionHeader, strconv.Itoa(recursionCount))
109
110 vrw := &virtualResponseWriter{body: buf, header: make(http.Header)}
111 server := c.Req.Context().Value(caddyhttp.ServerCtxKey).(http.Handler)