2016-06-05 23:39:23 -05:00
|
|
|
package fastcgi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"net/http"
|
|
|
|
"path/filepath"
|
2016-09-24 00:29:23 -05:00
|
|
|
"strconv"
|
2016-11-16 23:29:43 -05:00
|
|
|
"strings"
|
2016-11-19 11:05:29 -05:00
|
|
|
"time"
|
2016-06-05 23:39:23 -05:00
|
|
|
|
|
|
|
"github.com/mholt/caddy"
|
|
|
|
"github.com/mholt/caddy/caddyhttp/httpserver"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2016-06-06 16:31:03 -05:00
|
|
|
caddy.RegisterPlugin("fastcgi", caddy.Plugin{
|
2016-06-05 23:39:23 -05:00
|
|
|
ServerType: "http",
|
|
|
|
Action: setup,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// setup configures a new FastCGI middleware instance.
|
|
|
|
func setup(c *caddy.Controller) error {
|
2016-06-20 12:44:20 -05:00
|
|
|
cfg := httpserver.GetConfig(c)
|
2016-06-05 23:39:23 -05:00
|
|
|
|
|
|
|
rules, err := fastcgiParse(c)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg.AddMiddleware(func(next httpserver.Handler) httpserver.Handler {
|
|
|
|
return Handler{
|
|
|
|
Next: next,
|
|
|
|
Rules: rules,
|
|
|
|
Root: cfg.Root,
|
|
|
|
FileSys: http.Dir(cfg.Root),
|
|
|
|
SoftwareName: caddy.AppName,
|
|
|
|
SoftwareVersion: caddy.AppVersion,
|
|
|
|
ServerName: cfg.Addr.Host,
|
|
|
|
ServerPort: cfg.Addr.Port,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func fastcgiParse(c *caddy.Controller) ([]Rule, error) {
|
|
|
|
var rules []Rule
|
|
|
|
|
2017-01-17 12:34:17 -05:00
|
|
|
cfg := httpserver.GetConfig(c)
|
|
|
|
absRoot, err := filepath.Abs(cfg.Root)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-06-05 23:39:23 -05:00
|
|
|
for c.Next() {
|
|
|
|
args := c.RemainingArgs()
|
|
|
|
|
2016-11-16 23:29:43 -05:00
|
|
|
if len(args) < 2 || len(args) > 3 {
|
2016-06-05 23:39:23 -05:00
|
|
|
return rules, c.ArgErr()
|
2016-11-16 23:29:43 -05:00
|
|
|
}
|
|
|
|
|
2017-01-17 12:34:17 -05:00
|
|
|
rule := Rule{
|
|
|
|
Root: absRoot,
|
|
|
|
Path: args[0],
|
|
|
|
ReadTimeout: 60 * time.Second,
|
|
|
|
SendTimeout: 60 * time.Second,
|
|
|
|
}
|
2016-11-16 23:29:43 -05:00
|
|
|
upstreams := []string{args[1]}
|
|
|
|
|
|
|
|
if len(args) == 3 {
|
|
|
|
if err := fastcgiPreset(args[2], &rule); err != nil {
|
|
|
|
return rules, err
|
2016-06-05 23:39:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-19 11:05:29 -05:00
|
|
|
var err error
|
|
|
|
var pool int
|
2016-11-25 11:30:51 -05:00
|
|
|
var connectTimeout = 60 * time.Second
|
2016-11-16 23:29:43 -05:00
|
|
|
var dialers []dialer
|
|
|
|
var poolSize = -1
|
2016-09-24 00:29:23 -05:00
|
|
|
|
2016-06-05 23:39:23 -05:00
|
|
|
for c.NextBlock() {
|
|
|
|
switch c.Val() {
|
2017-01-17 12:34:17 -05:00
|
|
|
case "root":
|
|
|
|
if !c.NextArg() {
|
|
|
|
return rules, c.ArgErr()
|
|
|
|
}
|
|
|
|
rule.Root = c.Val()
|
|
|
|
|
2016-06-05 23:39:23 -05:00
|
|
|
case "ext":
|
|
|
|
if !c.NextArg() {
|
|
|
|
return rules, c.ArgErr()
|
|
|
|
}
|
|
|
|
rule.Ext = c.Val()
|
|
|
|
case "split":
|
|
|
|
if !c.NextArg() {
|
|
|
|
return rules, c.ArgErr()
|
|
|
|
}
|
|
|
|
rule.SplitPath = c.Val()
|
|
|
|
case "index":
|
|
|
|
args := c.RemainingArgs()
|
|
|
|
if len(args) == 0 {
|
|
|
|
return rules, c.ArgErr()
|
|
|
|
}
|
|
|
|
rule.IndexFiles = args
|
2016-11-16 23:29:43 -05:00
|
|
|
|
|
|
|
case "upstream":
|
|
|
|
args := c.RemainingArgs()
|
|
|
|
|
|
|
|
if len(args) != 1 {
|
|
|
|
return rules, c.ArgErr()
|
|
|
|
}
|
|
|
|
|
|
|
|
upstreams = append(upstreams, args[0])
|
2016-06-05 23:39:23 -05:00
|
|
|
case "env":
|
|
|
|
envArgs := c.RemainingArgs()
|
|
|
|
if len(envArgs) < 2 {
|
|
|
|
return rules, c.ArgErr()
|
|
|
|
}
|
|
|
|
rule.EnvVars = append(rule.EnvVars, [2]string{envArgs[0], envArgs[1]})
|
|
|
|
case "except":
|
|
|
|
ignoredPaths := c.RemainingArgs()
|
|
|
|
if len(ignoredPaths) == 0 {
|
|
|
|
return rules, c.ArgErr()
|
|
|
|
}
|
|
|
|
rule.IgnoredSubPaths = ignoredPaths
|
2016-11-16 23:29:43 -05:00
|
|
|
|
2016-09-24 00:29:23 -05:00
|
|
|
case "pool":
|
|
|
|
if !c.NextArg() {
|
|
|
|
return rules, c.ArgErr()
|
|
|
|
}
|
2016-11-19 11:05:29 -05:00
|
|
|
pool, err = strconv.Atoi(c.Val())
|
2016-09-24 00:29:23 -05:00
|
|
|
if err != nil {
|
|
|
|
return rules, err
|
|
|
|
}
|
|
|
|
if pool >= 0 {
|
2016-11-16 23:29:43 -05:00
|
|
|
poolSize = pool
|
2016-09-24 00:29:23 -05:00
|
|
|
} else {
|
|
|
|
return rules, c.Errf("positive integer expected, found %d", pool)
|
|
|
|
}
|
2016-11-19 11:05:29 -05:00
|
|
|
case "connect_timeout":
|
|
|
|
if !c.NextArg() {
|
|
|
|
return rules, c.ArgErr()
|
|
|
|
}
|
2016-11-25 11:30:51 -05:00
|
|
|
connectTimeout, err = time.ParseDuration(c.Val())
|
2016-11-19 11:05:29 -05:00
|
|
|
if err != nil {
|
|
|
|
return rules, err
|
|
|
|
}
|
|
|
|
case "read_timeout":
|
|
|
|
if !c.NextArg() {
|
|
|
|
return rules, c.ArgErr()
|
|
|
|
}
|
|
|
|
readTimeout, err := time.ParseDuration(c.Val())
|
|
|
|
if err != nil {
|
|
|
|
return rules, err
|
|
|
|
}
|
|
|
|
rule.ReadTimeout = readTimeout
|
2016-11-29 21:26:51 -05:00
|
|
|
case "send_timeout":
|
|
|
|
if !c.NextArg() {
|
|
|
|
return rules, c.ArgErr()
|
|
|
|
}
|
|
|
|
sendTimeout, err := time.ParseDuration(c.Val())
|
|
|
|
if err != nil {
|
|
|
|
return rules, err
|
|
|
|
}
|
|
|
|
rule.SendTimeout = sendTimeout
|
2016-06-05 23:39:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-16 23:29:43 -05:00
|
|
|
for _, rawAddress := range upstreams {
|
|
|
|
network, address := parseAddress(rawAddress)
|
|
|
|
if poolSize >= 0 {
|
2016-11-25 11:30:51 -05:00
|
|
|
dialers = append(dialers, &persistentDialer{
|
|
|
|
size: poolSize,
|
|
|
|
network: network,
|
|
|
|
address: address,
|
|
|
|
timeout: connectTimeout,
|
|
|
|
})
|
2016-11-16 23:29:43 -05:00
|
|
|
} else {
|
2016-11-25 11:30:51 -05:00
|
|
|
dialers = append(dialers, basicDialer{
|
|
|
|
network: network,
|
|
|
|
address: address,
|
|
|
|
timeout: connectTimeout,
|
|
|
|
})
|
2016-11-16 23:29:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rule.dialer = &loadBalancingDialer{dialers: dialers}
|
|
|
|
rule.Address = strings.Join(upstreams, ",")
|
2016-06-05 23:39:23 -05:00
|
|
|
rules = append(rules, rule)
|
|
|
|
}
|
|
|
|
|
|
|
|
return rules, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// fastcgiPreset configures rule according to name. It returns an error if
|
|
|
|
// name is not a recognized preset name.
|
|
|
|
func fastcgiPreset(name string, rule *Rule) error {
|
|
|
|
switch name {
|
|
|
|
case "php":
|
|
|
|
rule.Ext = ".php"
|
|
|
|
rule.SplitPath = ".php"
|
|
|
|
rule.IndexFiles = []string{"index.php"}
|
|
|
|
default:
|
|
|
|
return errors.New(name + " is not a valid preset name")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|