2015-01-13 14:43:45 -05:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2015-01-19 01:11:21 -05:00
|
|
|
"os"
|
2015-01-13 14:43:45 -05:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// parser is a type which can parse config files.
|
|
|
|
type parser struct {
|
2015-01-19 01:11:21 -05:00
|
|
|
filename string // the name of the file that we're parsing
|
|
|
|
lexer lexer // the lexer that is giving us tokens from the raw input
|
|
|
|
cfg Config // each server gets one Config; this is the one we're currently building
|
|
|
|
other map[string]*dispenser // tokens to be parsed later by others (middleware generators)
|
|
|
|
unused bool // sometimes the token won't be immediately consumed
|
|
|
|
}
|
|
|
|
|
|
|
|
// newParser makes a new parser and prepares it for parsing, given
|
|
|
|
// the input to parse.
|
2015-01-21 15:19:25 -05:00
|
|
|
func newParser(file *os.File) (*parser, error) {
|
|
|
|
stat, err := file.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
p := &parser{filename: stat.Name()}
|
2015-01-19 01:11:21 -05:00
|
|
|
p.lexer.load(file)
|
2015-01-21 15:19:25 -05:00
|
|
|
|
|
|
|
return p, nil
|
2015-01-13 14:43:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse parses the configuration file. It produces a slice of Config
|
|
|
|
// structs which can be used to create and configure server instances.
|
2015-01-19 01:11:21 -05:00
|
|
|
func (p *parser) parse() ([]Config, error) {
|
2015-01-13 14:43:45 -05:00
|
|
|
var configs []Config
|
|
|
|
|
2015-01-19 01:11:21 -05:00
|
|
|
for p.lexer.next() {
|
|
|
|
err := p.parseOne()
|
2015-01-13 14:43:45 -05:00
|
|
|
if err != nil {
|
2015-01-19 01:11:21 -05:00
|
|
|
return nil, err
|
2015-01-13 14:43:45 -05:00
|
|
|
}
|
|
|
|
configs = append(configs, p.cfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
return configs, nil
|
|
|
|
}
|
|
|
|
|
2015-01-19 01:11:21 -05:00
|
|
|
// nextArg loads the next token if it is on the same line.
|
|
|
|
// Returns true if a token was loaded; false otherwise.
|
|
|
|
func (p *parser) nextArg() bool {
|
|
|
|
if p.unused {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
line := p.line()
|
|
|
|
if p.next() {
|
|
|
|
if p.line() > line {
|
|
|
|
p.unused = true
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// next loads the next token and returns true if a token
|
|
|
|
// was loaded; false otherwise.
|
|
|
|
func (p *parser) next() bool {
|
|
|
|
if p.unused {
|
|
|
|
p.unused = false
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
return p.lexer.next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseOne parses the contents of a configuration
|
|
|
|
// file for a single Config object (each server or
|
|
|
|
// virtualhost instance gets their own Config struct),
|
|
|
|
// which is until the next address/server block.
|
|
|
|
// Call this only after you know that the lexer has another
|
|
|
|
// another token and you're not in the middle of a server
|
|
|
|
// block already.
|
|
|
|
func (p *parser) parseOne() error {
|
|
|
|
p.cfg = Config{}
|
|
|
|
|
|
|
|
p.other = make(map[string]*dispenser)
|
|
|
|
|
|
|
|
err := p.begin()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = p.unwrap()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// unwrap gets the middleware generators from the middleware
|
|
|
|
// package in the order in which they are registered, and
|
|
|
|
// executes the top-level functions (the generator function)
|
|
|
|
// to expose the second layers which is the actual middleware.
|
|
|
|
// This function should be called only after p has filled out
|
|
|
|
// p.other and that the entire server block has been consumed.
|
|
|
|
func (p *parser) unwrap() error {
|
2015-01-30 00:02:17 -05:00
|
|
|
for directive := range registry.directiveMap {
|
2015-01-19 01:11:21 -05:00
|
|
|
if disp, ok := p.other[directive]; ok {
|
2015-01-30 00:02:17 -05:00
|
|
|
if generator, ok := registry.directiveMap[directive]; ok {
|
|
|
|
mid, err := generator(disp)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-01-19 01:11:21 -05:00
|
|
|
}
|
|
|
|
p.cfg.Middleware = append(p.cfg.Middleware, mid)
|
|
|
|
} else {
|
|
|
|
return errors.New("No middleware bound to directive '" + directive + "'")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-01-13 14:43:45 -05:00
|
|
|
// tkn is shorthand to get the text/value of the current token.
|
|
|
|
func (p *parser) tkn() string {
|
|
|
|
return p.lexer.token.text
|
|
|
|
}
|
|
|
|
|
|
|
|
// line is shorthand to get the line number of the current token.
|
|
|
|
func (p *parser) line() int {
|
|
|
|
return p.lexer.token.line
|
|
|
|
}
|
|
|
|
|
|
|
|
// syntaxErr creates a syntax error which explains what was
|
|
|
|
// found and expected.
|
|
|
|
func (p *parser) syntaxErr(expected string) error {
|
|
|
|
return p.err("Syntax", fmt.Sprintf("Unexpected token '%s', expecting '%s'", p.tkn(), expected))
|
|
|
|
}
|
|
|
|
|
|
|
|
// syntaxErr creates a syntax error that explains that there
|
|
|
|
// weren't enough arguments on the line.
|
|
|
|
func (p *parser) argErr() error {
|
|
|
|
return p.err("Syntax", "Unexpected line break after '"+p.tkn()+"' (missing arguments?)")
|
|
|
|
}
|
|
|
|
|
|
|
|
// eofErr creates a syntax error describing an unexpected EOF.
|
|
|
|
func (p *parser) eofErr() error {
|
|
|
|
return p.err("Syntax", "Unexpected EOF")
|
|
|
|
}
|
|
|
|
|
2015-01-19 01:11:21 -05:00
|
|
|
// err creates an error with a custom message msg: "{{kind}} error: {{msg}}". The
|
2015-01-13 14:43:45 -05:00
|
|
|
// file name and line number are included in the error message.
|
|
|
|
func (p *parser) err(kind, msg string) error {
|
2015-01-19 01:11:21 -05:00
|
|
|
msg = fmt.Sprintf("%s:%d - %s error: %s", p.filename, p.line(), kind, msg)
|
2015-01-13 14:43:45 -05:00
|
|
|
return errors.New(msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseAddress takes a host:port string (val), and returns the host
|
2015-01-13 19:25:55 -05:00
|
|
|
// and port as separate values. Empty strings can be returned if
|
|
|
|
// either is missing.
|
|
|
|
func parseAddress(val string) (string, string) {
|
2015-01-13 14:43:45 -05:00
|
|
|
parts := strings.SplitN(val, ":", 3)
|
2015-01-13 19:25:55 -05:00
|
|
|
if len(parts) == 1 {
|
|
|
|
return parts[0], ""
|
2015-01-13 14:43:45 -05:00
|
|
|
} else {
|
|
|
|
return parts[0], parts[1]
|
|
|
|
}
|
|
|
|
}
|