mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-23 22:27:38 -05:00
Refactored fastcgi middleware
This commit is contained in:
parent
40bf7c5285
commit
113b175db7
2 changed files with 105 additions and 86 deletions
|
@ -1,6 +1,6 @@
|
||||||
// Package fastcgi has middleware that acts as a FastCGI client. Requests
|
// Package fastcgi has middleware that acts as a FastCGI client. Requests
|
||||||
// that get forwarded to FastCGI stop the middleware execution chain.
|
// that get forwarded to FastCGI stop the middleware execution chain.
|
||||||
// The most common use for this layer is to serve PHP websites via php-fpm.
|
// The most common use for this package is to serve PHP websites via php-fpm.
|
||||||
package fastcgi
|
package fastcgi
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -17,31 +17,38 @@ import (
|
||||||
func New(c middleware.Controller) (middleware.Middleware, error) {
|
func New(c middleware.Controller) (middleware.Middleware, error) {
|
||||||
root := c.Root()
|
root := c.Root()
|
||||||
|
|
||||||
var rules []fastCgi
|
rules, err := parse(c)
|
||||||
for c.Next() {
|
if err != nil {
|
||||||
rule := fastCgi{}
|
return nil, err
|
||||||
if !c.Args(&rule.path, &rule.address) {
|
|
||||||
return nil, c.ArgErr()
|
|
||||||
}
|
|
||||||
rules = append(rules, rule)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return func(next middleware.Handler) middleware.Handler {
|
return func(next middleware.Handler) middleware.Handler {
|
||||||
return middleware.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
|
return Handler{Next: next, Rules: rules, Root: root}
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handler is a middleware type that can handle requests as a FastCGI client.
|
||||||
|
type Handler struct {
|
||||||
|
Next middleware.Handler
|
||||||
|
Root string
|
||||||
|
Rules []Rule
|
||||||
|
}
|
||||||
|
|
||||||
|
// ServeHTTP satisfies the middleware.Handler interface.
|
||||||
|
func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||||
servedFcgi := false
|
servedFcgi := false
|
||||||
for _, rule := range rules {
|
for _, rule := range h.Rules {
|
||||||
if middleware.Path(r.URL.Path).Matches(rule.path) {
|
if middleware.Path(r.URL.Path).Matches(rule.Path) {
|
||||||
servedFcgi = true
|
servedFcgi = true
|
||||||
|
|
||||||
// Get absolute file paths
|
// Get absolute file paths
|
||||||
absPath, err := filepath.Abs(root + r.URL.Path)
|
absPath, err := filepath.Abs(h.Root + r.URL.Path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return http.StatusInternalServerError, err
|
return http.StatusInternalServerError, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get absolute file path to website root
|
// Get absolute file path to website root
|
||||||
absRootPath, err := filepath.Abs(root)
|
absRootPath, err := filepath.Abs(h.Root)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return http.StatusInternalServerError, err
|
return http.StatusInternalServerError, err
|
||||||
}
|
}
|
||||||
|
@ -68,7 +75,7 @@ func New(c middleware.Controller) (middleware.Middleware, error) {
|
||||||
env["DOCUMENT_URI"] = r.URL.Path
|
env["DOCUMENT_URI"] = r.URL.Path
|
||||||
env["DOCUMENT_ROOT"] = absRootPath
|
env["DOCUMENT_ROOT"] = absRootPath
|
||||||
|
|
||||||
fcgi, err := Dial("tcp", rule.address)
|
fcgi, err := Dial("tcp", rule.Address)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return http.StatusBadGateway, err
|
return http.StatusBadGateway, err
|
||||||
}
|
}
|
||||||
|
@ -97,15 +104,27 @@ func New(c middleware.Controller) (middleware.Middleware, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if !servedFcgi {
|
if !servedFcgi {
|
||||||
return next.ServeHTTP(w, r)
|
return h.Next.ServeHTTP(w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0, nil
|
return 0, nil
|
||||||
})
|
|
||||||
}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type fastCgi struct {
|
func parse(c middleware.Controller) ([]Rule, error) {
|
||||||
path string
|
var rules []Rule
|
||||||
address string
|
|
||||||
|
for c.Next() {
|
||||||
|
var rule Rule
|
||||||
|
if !c.Args(&rule.Path, &rule.Address) {
|
||||||
|
return rules, c.ArgErr()
|
||||||
|
}
|
||||||
|
rules = append(rules, rule)
|
||||||
|
}
|
||||||
|
|
||||||
|
return rules, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rule represents a FastCGI handling rule.
|
||||||
|
type Rule struct {
|
||||||
|
Path, Address string
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,7 @@ import (
|
||||||
"github.com/mholt/caddy/middleware"
|
"github.com/mholt/caddy/middleware"
|
||||||
)
|
)
|
||||||
|
|
||||||
// New instantiates a new Rewrites middleware.
|
// New instantiates a new Redirect middleware.
|
||||||
func New(c middleware.Controller) (middleware.Middleware, error) {
|
func New(c middleware.Controller) (middleware.Middleware, error) {
|
||||||
rules, err := parse(c)
|
rules, err := parse(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in a new issue