本文整理匯總了Golang中github.com/mholt/caddy.Controller.NextBlock方法的典型用法代碼示例。如果您正苦於以下問題:Golang Controller.NextBlock方法的具體用法?Golang Controller.NextBlock怎麽用?Golang Controller.NextBlock使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/mholt/caddy.Controller
的用法示例。
在下文中一共展示了Controller.NextBlock方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: mimeParse
func mimeParse(c *caddy.Controller) (Config, error) {
configs := Config{}
for c.Next() {
// At least one extension is required
args := c.RemainingArgs()
switch len(args) {
case 2:
if err := validateExt(configs, args[0]); err != nil {
return configs, err
}
configs[args[0]] = args[1]
case 1:
return configs, c.ArgErr()
case 0:
for c.NextBlock() {
ext := c.Val()
if err := validateExt(configs, ext); err != nil {
return configs, err
}
if !c.NextArg() {
return configs, c.ArgErr()
}
configs[ext] = c.Val()
}
}
}
return configs, nil
}
示例2: ParseRoller
// ParseRoller parses roller contents out of c.
func ParseRoller(c *caddy.Controller) (*LogRoller, error) {
var size, age, keep int
// This is kind of a hack to support nested blocks:
// As we are already in a block: either log or errors,
// c.nesting > 0 but, as soon as c meets a }, it thinks
// the block is over and return false for c.NextBlock.
for c.NextBlock() {
what := c.Val()
if !c.NextArg() {
return nil, c.ArgErr()
}
value := c.Val()
var err error
switch what {
case "size":
size, err = strconv.Atoi(value)
case "age":
age, err = strconv.Atoi(value)
case "keep":
keep, err = strconv.Atoi(value)
}
if err != nil {
return nil, err
}
}
return &LogRoller{
MaxSize: size,
MaxAge: age,
MaxBackups: keep,
LocalTime: true,
}, nil
}
示例3: parse
func parse(m *module, c *caddy.Controller) (err error) {
args := c.RemainingArgs()
if len(args) == 1 && args[0] == "cloudflare" {
addCloudflareIps(m)
if c.NextBlock() {
return c.Err("No realip subblocks allowed if using preset.")
}
} else if len(args) != 0 {
return c.ArgErr()
}
for c.NextBlock() {
var err error
switch c.Val() {
case "header":
m.Header, err = StringArg(c)
case "from":
var cidr *net.IPNet
cidr, err = CidrArg(c)
m.From = append(m.From, cidr)
case "strict":
m.Strict, err = BoolArg(c)
default:
return c.Errf("Unknown realip arg: %s", c.Val())
}
if err != nil {
return err
}
}
return nil
}
示例4: statusParse
// statusParse parses status directive
func statusParse(c *caddy.Controller) ([]httpserver.HandlerConfig, error) {
var rules []httpserver.HandlerConfig
for c.Next() {
hadBlock := false
args := c.RemainingArgs()
switch len(args) {
case 1:
status, err := strconv.Atoi(args[0])
if err != nil {
return rules, c.Errf("Expecting a numeric status code, got '%s'", args[0])
}
for c.NextBlock() {
hadBlock = true
basePath := c.Val()
for _, cfg := range rules {
rule := cfg.(*Rule)
if rule.Base == basePath {
return rules, c.Errf("Duplicate path: '%s'", basePath)
}
}
rule := NewRule(basePath, status)
rules = append(rules, rule)
if c.NextArg() {
return rules, c.ArgErr()
}
}
if !hadBlock {
return rules, c.ArgErr()
}
case 2:
status, err := strconv.Atoi(args[0])
if err != nil {
return rules, c.Errf("Expecting a numeric status code, got '%s'", args[0])
}
basePath := args[1]
for _, cfg := range rules {
rule := cfg.(*Rule)
if rule.Base == basePath {
return rules, c.Errf("Duplicate path: '%s'", basePath)
}
}
rule := NewRule(basePath, status)
rules = append(rules, rule)
default:
return rules, c.ArgErr()
}
}
return rules, nil
}
示例5: templatesParse
func templatesParse(c *caddy.Controller) ([]Rule, error) {
var rules []Rule
for c.Next() {
var rule Rule
rule.Path = defaultTemplatePath
rule.Extensions = defaultTemplateExtensions
args := c.RemainingArgs()
switch len(args) {
case 0:
// Optional block
for c.NextBlock() {
switch c.Val() {
case "path":
args := c.RemainingArgs()
if len(args) != 1 {
return nil, c.ArgErr()
}
rule.Path = args[0]
case "ext":
args := c.RemainingArgs()
if len(args) == 0 {
return nil, c.ArgErr()
}
rule.Extensions = args
case "between":
args := c.RemainingArgs()
if len(args) != 2 {
return nil, c.ArgErr()
}
rule.Delims[0] = args[0]
rule.Delims[1] = args[1]
}
}
default:
// First argument would be the path
rule.Path = args[0]
// Any remaining arguments are extensions
rule.Extensions = args[1:]
if len(rule.Extensions) == 0 {
rule.Extensions = defaultTemplateExtensions
}
}
for _, ext := range rule.Extensions {
rule.IndexFiles = append(rule.IndexFiles, "index"+ext)
}
rules = append(rules, rule)
}
return rules, nil
}
示例6: setupMaxRequestBody
func setupMaxRequestBody(c *caddy.Controller) error {
config := httpserver.GetConfig(c)
if !c.Next() {
return c.ArgErr()
}
args := c.RemainingArgs()
argList := []pathLimitUnparsed{}
switch len(args) {
case 0:
// Format: { <path> <limit> ... }
for c.NextBlock() {
path := c.Val()
if !c.NextArg() {
// Uneven pairing of path/limit
return c.ArgErr()
}
argList = append(argList, pathLimitUnparsed{
Path: path,
Limit: c.Val(),
})
}
case 1:
// Format: <limit>
argList = []pathLimitUnparsed{{
Path: "/",
Limit: args[0],
}}
case 2:
// Format: <path> <limit>
argList = []pathLimitUnparsed{{
Path: args[0],
Limit: args[1],
}}
default:
return c.ArgErr()
}
pathLimit, err := parseArguments(argList)
if err != nil {
return c.ArgErr()
}
SortPathLimits(pathLimit)
config.MaxRequestBodySizes = pathLimit
return nil
}
示例7: nlgidsParse
// nlgidsParse will parse the following directives.
// recipients [email protected] [email protected]
// subject [email protected]
// secret /opt/etc/NLgids-fcbeb7928cdb.json
func nlgidsParse(c *caddy.Controller) (*Config, error) {
config := new(Config)
config.Tours = "/opt/www/nlgids.london/tours.json"
for c.Next() {
for c.NextBlock() {
switch c.Val() {
case "recipients":
rcpts := c.RemainingArgs()
if len(rcpts) == 0 {
return nil, c.ArgErr()
}
config.Recipients = append(config.Recipients, rcpts...)
case "subject":
if !c.NextArg() {
return nil, c.ArgErr()
}
config.Subject = c.Val()
if !strings.Contains(config.Subject, "@") {
return nil, fmt.Errorf("nlgids: subject must contain @-sign: %s", c.Val())
}
case "secret":
if !c.NextArg() {
return nil, c.ArgErr()
}
config.Secret = c.Val()
_, err := os.Open(config.Secret)
if err != nil {
return nil, fmt.Errorf("nlgids: secret file must be readable: %s", err)
}
case "template":
if !c.NextArg() {
return nil, c.ArgErr()
}
config.Template = c.Val()
case "tours":
if !c.NextArg() {
return nil, c.ArgErr()
}
config.Tours = c.Val()
}
}
}
return config, nil
}
示例8: markdownParse
func markdownParse(c *caddy.Controller) ([]*Config, error) {
var mdconfigs []*Config
for c.Next() {
md := &Config{
Renderer: blackfriday.HtmlRenderer(0, "", ""),
Extensions: make(map[string]struct{}),
Template: GetDefaultTemplate(),
}
// Get the path scope
args := c.RemainingArgs()
switch len(args) {
case 0:
md.PathScope = "/"
case 1:
md.PathScope = args[0]
default:
return mdconfigs, c.ArgErr()
}
// Load any other configuration parameters
for c.NextBlock() {
if err := loadParams(c, md); err != nil {
return mdconfigs, err
}
}
// If no extensions were specified, assume some defaults
if len(md.Extensions) == 0 {
md.Extensions[".md"] = struct{}{}
md.Extensions[".markdown"] = struct{}{}
md.Extensions[".mdown"] = struct{}{}
}
mdconfigs = append(mdconfigs, md)
}
return mdconfigs, nil
}
示例9: setup
// setup returns a new instance of a pprof handler. It accepts no arguments or options.
func setup(c *caddy.Controller) error {
found := false
for c.Next() {
if found {
return c.Err("pprof can only be specified once")
}
if len(c.RemainingArgs()) != 0 {
return c.ArgErr()
}
if c.NextBlock() {
return c.ArgErr()
}
found = true
}
httpserver.GetConfig(c).AddMiddleware(func(next httpserver.Handler) httpserver.Handler {
return &Handler{Next: next, Mux: NewMux()}
})
return nil
}
示例10: basicAuthParse
func basicAuthParse(c *caddy.Controller) ([]Rule, error) {
var rules []Rule
cfg := httpserver.GetConfig(c)
var err error
for c.Next() {
var rule Rule
args := c.RemainingArgs()
switch len(args) {
case 2:
rule.Username = args[0]
if rule.Password, err = passwordMatcher(rule.Username, args[1], cfg.Root); err != nil {
return rules, c.Errf("Get password matcher from %s: %v", c.Val(), err)
}
for c.NextBlock() {
rule.Resources = append(rule.Resources, c.Val())
if c.NextArg() {
return rules, c.Errf("Expecting only one resource per line (extra '%s')", c.Val())
}
}
case 3:
rule.Resources = append(rule.Resources, args[0])
rule.Username = args[1]
if rule.Password, err = passwordMatcher(rule.Username, args[2], cfg.Root); err != nil {
return rules, c.Errf("Get password matcher from %s: %v", c.Val(), err)
}
default:
return rules, c.ArgErr()
}
rules = append(rules, rule)
}
return rules, nil
}
示例11: logParse
func logParse(c *caddy.Controller) ([]Rule, error) {
var rules []Rule
for c.Next() {
args := c.RemainingArgs()
var logRoller *httpserver.LogRoller
if c.NextBlock() {
if c.Val() == "rotate" {
if c.NextArg() {
if c.Val() == "{" {
var err error
logRoller, err = httpserver.ParseRoller(c)
if err != nil {
return nil, err
}
// This part doesn't allow having something after the rotate block
if c.Next() {
if c.Val() != "}" {
return nil, c.ArgErr()
}
}
}
}
}
}
if len(args) == 0 {
// Nothing specified; use defaults
rules = append(rules, Rule{
PathScope: "/",
OutputFile: DefaultLogFilename,
Format: DefaultLogFormat,
Roller: logRoller,
})
} else if len(args) == 1 {
// Only an output file specified
rules = append(rules, Rule{
PathScope: "/",
OutputFile: args[0],
Format: DefaultLogFormat,
Roller: logRoller,
})
} else {
// Path scope, output file, and maybe a format specified
format := DefaultLogFormat
if len(args) > 2 {
switch args[2] {
case "{common}":
format = CommonLogFormat
case "{combined}":
format = CombinedLogFormat
default:
format = args[2]
}
}
rules = append(rules, Rule{
PathScope: args[0],
OutputFile: args[1],
Format: format,
Roller: logRoller,
})
}
}
return rules, nil
}
示例12: fastcgiParse
func fastcgiParse(c *caddy.Controller) ([]Rule, error) {
var rules []Rule
for c.Next() {
var rule Rule
args := c.RemainingArgs()
switch len(args) {
case 0:
return rules, c.ArgErr()
case 1:
rule.Path = "/"
rule.Address = args[0]
case 2:
rule.Path = args[0]
rule.Address = args[1]
case 3:
rule.Path = args[0]
rule.Address = args[1]
err := fastcgiPreset(args[2], &rule)
if err != nil {
return rules, c.Err("Invalid fastcgi rule preset '" + args[2] + "'")
}
}
network, address := parseAddress(rule.Address)
rule.dialer = basicDialer{network: network, address: address}
for c.NextBlock() {
switch c.Val() {
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
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
case "pool":
if !c.NextArg() {
return rules, c.ArgErr()
}
pool, err := strconv.Atoi(c.Val())
if err != nil {
return rules, err
}
if pool >= 0 {
rule.dialer = &persistentDialer{size: pool, network: network, address: address}
} else {
return rules, c.Errf("positive integer expected, found %d", pool)
}
}
}
rules = append(rules, rule)
}
return rules, nil
}
示例13: redirParse
func redirParse(c *caddy.Controller) ([]Rule, error) {
var redirects []Rule
cfg := httpserver.GetConfig(c.Key)
// setRedirCode sets the redirect code for rule if it can, or returns an error
setRedirCode := func(code string, rule *Rule) error {
if code == "meta" {
rule.Meta = true
} else if codeNumber, ok := httpRedirs[code]; ok {
rule.Code = codeNumber
} else {
return c.Errf("Invalid redirect code '%v'", code)
}
return nil
}
// checkAndSaveRule checks the rule for validity (except the redir code)
// and saves it if it's valid, or returns an error.
checkAndSaveRule := func(rule Rule) error {
if rule.FromPath == rule.To {
return c.Err("'from' and 'to' values of redirect rule cannot be the same")
}
for _, otherRule := range redirects {
if otherRule.FromPath == rule.FromPath {
return c.Errf("rule with duplicate 'from' value: %s -> %s", otherRule.FromPath, otherRule.To)
}
}
redirects = append(redirects, rule)
return nil
}
for c.Next() {
args := c.RemainingArgs()
var hadOptionalBlock bool
for c.NextBlock() {
hadOptionalBlock = true
var rule Rule
if cfg.TLS.Enabled {
rule.FromScheme = "https"
} else {
rule.FromScheme = "http"
}
// Set initial redirect code
// BUG: If the code is specified for a whole block and that code is invalid,
// the line number will appear on the first line inside the block, even if that
// line overwrites the block-level code with a valid redirect code. The program
// still functions correctly, but the line number in the error reporting is
// misleading to the user.
if len(args) == 1 {
err := setRedirCode(args[0], &rule)
if err != nil {
return redirects, err
}
} else {
rule.Code = http.StatusMovedPermanently // default code
}
// RemainingArgs only gets the values after the current token, but in our
// case we want to include the current token to get an accurate count.
insideArgs := append([]string{c.Val()}, c.RemainingArgs()...)
switch len(insideArgs) {
case 1:
// To specified (catch-all redirect)
// Not sure why user is doing this in a table, as it causes all other redirects to be ignored.
// As such, this feature remains undocumented.
rule.FromPath = "/"
rule.To = insideArgs[0]
case 2:
// From and To specified
rule.FromPath = insideArgs[0]
rule.To = insideArgs[1]
case 3:
// From, To, and Code specified
rule.FromPath = insideArgs[0]
rule.To = insideArgs[1]
err := setRedirCode(insideArgs[2], &rule)
if err != nil {
return redirects, err
}
default:
return redirects, c.ArgErr()
}
err := checkAndSaveRule(rule)
if err != nil {
return redirects, err
}
}
if !hadOptionalBlock {
var rule Rule
//.........這裏部分代碼省略.........
示例14: parse
// Parse parses the configuration set by the user so it can be
// used by the middleware
func parse(c *caddy.Controller, root string) (*Config, *filemanager.FileManager, error) {
var (
cfg *Config
fm *filemanager.FileManager
err error
tokens string
)
cfg = new(Config)
if cfg.Hugo, err = exec.LookPath("hugo"); err != nil {
fmt.Println(HugoNotFound)
return cfg, fm, errors.New(HugoNotFound)
}
for c.Next() {
cfg.Public = strings.Replace(root, "./", "", -1)
cfg.BaseURL = "/admin"
cfg.Root = "./"
args := c.RemainingArgs()
if len(args) >= 1 {
cfg.Root = args[0]
cfg.Root = strings.TrimSuffix(cfg.Root, "/")
cfg.Root += "/"
}
if len(args) >= 2 {
cfg.BaseURL = args[1]
cfg.BaseURL = strings.TrimPrefix(cfg.BaseURL, "/")
cfg.BaseURL = "/" + cfg.BaseURL
}
for c.NextBlock() {
switch c.Val() {
case "flag":
if !c.NextArg() {
return cfg, &filemanager.FileManager{}, c.ArgErr()
}
values := strings.Split(c.Val(), " ")
if len(values) == 0 {
return cfg, fm, errors.New("Not enough arguments for 'flag' option.")
}
value := "true"
if len(values) > 1 {
value = values[1]
}
cfg.Args = append(cfg.Args, "--"+values[0]+"="+value)
case "before_publish":
if cfg.BeforePublish, err = config.CommandRunner(c); err != nil {
return cfg, &filemanager.FileManager{}, err
}
case "after_publish":
if cfg.AfterPublish, err = config.CommandRunner(c); err != nil {
return cfg, &filemanager.FileManager{}, err
}
default:
line := "\n\t" + c.Val()
if c.NextArg() {
line += " " + c.Val()
}
tokens += line
}
}
}
tokens = "filemanager " + cfg.BaseURL + " {\n\tshow " + cfg.Root + tokens
tokens += "\n}"
fmConfig, err := config.Parse(caddy.NewTestController("http", tokens))
if err != nil {
return cfg, fm, err
}
fm = &filemanager.FileManager{Configs: fmConfig}
fm.Configs[0].HugoEnabled = true
format := getFrontMatter(cfg)
cfg.WebDavURL = fm.Configs[0].WebDavURL
for _, user := range fm.Configs[0].Users {
user.FrontMatter = format
}
if err != nil {
return cfg, fm, err
}
//.........這裏部分代碼省略.........
示例15: ipfilterParseSingle
// ipfilterParseSingle parses a single ipfilter {} block from the caddy config.
func ipfilterParseSingle(config *IPFConfig, c *caddy.Controller) (IPPath, error) {
var cPath IPPath
// Get PathScopes
cPath.PathScopes = c.RemainingArgs()
if len(cPath.PathScopes) == 0 {
return cPath, c.ArgErr()
}
// Sort PathScopes by length (the longest is always the most specific so should be tested first)
sort.Sort(sort.Reverse(ByLength(cPath.PathScopes)))
for c.NextBlock() {
value := c.Val()
switch value {
case "rule":
if !c.NextArg() {
return cPath, c.ArgErr()
}
rule := c.Val()
if rule == "block" {
cPath.IsBlock = true
} else if rule != "allow" {
return cPath, c.Err("ipfilter: Rule should be 'block' or 'allow'")
}
case "database":
if !c.NextArg() {
return cPath, c.ArgErr()
}
// Check if a database has already been opened
if config.DBHandler != nil {
return cPath, c.Err("ipfilter: A database is already opened")
}
database := c.Val()
// Open the database.
var err error
config.DBHandler, err = maxminddb.Open(database)
if err != nil {
return cPath, c.Err("ipfilter: Can't open database: " + database)
}
case "blockpage":
if !c.NextArg() {
return cPath, c.ArgErr()
}
// check if blockpage exists.
blockpage := c.Val()
if _, err := os.Stat(blockpage); os.IsNotExist(err) {
return cPath, c.Err("ipfilter: No such file: " + blockpage)
}
cPath.BlockPage = blockpage
case "country":
cPath.CountryCodes = c.RemainingArgs()
if len(cPath.CountryCodes) == 0 {
return cPath, c.ArgErr()
}
case "ip":
ips := c.RemainingArgs()
if len(ips) == 0 {
return cPath, c.ArgErr()
}
for _, ip := range ips {
ipRange, err := parseIP(ip)
if err != nil {
return cPath, c.Err("ipfilter: " + err.Error())
}
cPath.Nets = append(cPath.Nets, ipRange...)
}
case "strict":
cPath.Strict = true
}
}
return cPath, nil
}