2015-10-27 14:07:22 +08:00
|
|
|
package caddyfile
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2015-10-30 00:13:30 +08:00
|
|
|
"net"
|
2015-11-11 02:49:01 +08:00
|
|
|
"sort"
|
2015-10-27 14:07:22 +08:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/mholt/caddy/caddy/parse"
|
|
|
|
)
|
|
|
|
|
|
|
|
const filename = "Caddyfile"
|
|
|
|
|
|
|
|
// ToJSON converts caddyfile to its JSON representation.
|
|
|
|
func ToJSON(caddyfile []byte) ([]byte, error) {
|
|
|
|
var j Caddyfile
|
|
|
|
|
|
|
|
serverBlocks, err := parse.ServerBlocks(filename, bytes.NewReader(caddyfile), false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, sb := range serverBlocks {
|
2015-11-11 02:49:01 +08:00
|
|
|
block := ServerBlock{Body: [][]interface{}{}}
|
2015-10-27 14:07:22 +08:00
|
|
|
|
2015-11-11 02:49:01 +08:00
|
|
|
// Fill up host list
|
2015-10-27 14:07:22 +08:00
|
|
|
for _, host := range sb.HostList() {
|
2016-01-04 07:46:26 +08:00
|
|
|
block.Hosts = append(block.Hosts, standardizeScheme(host))
|
2015-10-27 14:07:22 +08:00
|
|
|
}
|
|
|
|
|
2015-11-11 02:49:01 +08:00
|
|
|
// Extract directives deterministically by sorting them
|
|
|
|
var directives = make([]string, len(sb.Tokens))
|
|
|
|
for dir := range sb.Tokens {
|
|
|
|
directives = append(directives, dir)
|
2015-10-27 14:07:22 +08:00
|
|
|
}
|
2015-11-11 02:49:01 +08:00
|
|
|
sort.Strings(directives)
|
2015-10-27 14:07:22 +08:00
|
|
|
|
2015-11-11 02:49:01 +08:00
|
|
|
// Convert each directive's tokens into our JSON structure
|
|
|
|
for _, dir := range directives {
|
|
|
|
disp := parse.NewDispenserTokens(filename, sb.Tokens[dir])
|
|
|
|
for disp.Next() {
|
|
|
|
block.Body = append(block.Body, constructLine(&disp))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// tack this block onto the end of the list
|
2015-10-27 14:07:22 +08:00
|
|
|
j = append(j, block)
|
|
|
|
}
|
|
|
|
|
|
|
|
result, err := json.Marshal(j)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// constructLine transforms tokens into a JSON-encodable structure;
|
|
|
|
// but only one line at a time, to be used at the top-level of
|
|
|
|
// a server block only (where the first token on each line is a
|
|
|
|
// directive) - not to be used at any other nesting level.
|
2015-11-11 02:49:01 +08:00
|
|
|
func constructLine(d *parse.Dispenser) []interface{} {
|
2015-10-27 14:07:22 +08:00
|
|
|
var args []interface{}
|
|
|
|
|
2015-11-11 02:49:01 +08:00
|
|
|
args = append(args, d.Val())
|
2015-10-27 14:07:22 +08:00
|
|
|
|
2015-11-11 02:49:01 +08:00
|
|
|
for d.NextArg() {
|
|
|
|
if d.Val() == "{" {
|
|
|
|
args = append(args, constructBlock(d))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
args = append(args, d.Val())
|
2015-10-27 14:07:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return args
|
|
|
|
}
|
|
|
|
|
|
|
|
// constructBlock recursively processes tokens into a
|
2016-01-04 07:46:26 +08:00
|
|
|
// JSON-encodable structure. To be used in a directive's
|
|
|
|
// block. Goes to end of block.
|
2015-11-11 02:49:01 +08:00
|
|
|
func constructBlock(d *parse.Dispenser) [][]interface{} {
|
|
|
|
block := [][]interface{}{}
|
2015-10-27 14:07:22 +08:00
|
|
|
|
|
|
|
for d.Next() {
|
|
|
|
if d.Val() == "}" {
|
|
|
|
break
|
|
|
|
}
|
2015-11-11 02:49:01 +08:00
|
|
|
block = append(block, constructLine(d))
|
2015-10-27 14:07:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return block
|
|
|
|
}
|
|
|
|
|
|
|
|
// FromJSON converts JSON-encoded jsonBytes to Caddyfile text
|
|
|
|
func FromJSON(jsonBytes []byte) ([]byte, error) {
|
|
|
|
var j Caddyfile
|
|
|
|
var result string
|
|
|
|
|
|
|
|
err := json.Unmarshal(jsonBytes, &j)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-11-11 02:49:01 +08:00
|
|
|
for sbPos, sb := range j {
|
|
|
|
if sbPos > 0 {
|
|
|
|
result += "\n\n"
|
|
|
|
}
|
2015-10-27 14:07:22 +08:00
|
|
|
for i, host := range sb.Hosts {
|
|
|
|
if i > 0 {
|
|
|
|
result += ", "
|
|
|
|
}
|
2016-01-04 07:46:26 +08:00
|
|
|
result += standardizeScheme(host)
|
2015-10-27 14:07:22 +08:00
|
|
|
}
|
|
|
|
result += jsonToText(sb.Body, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
return []byte(result), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// jsonToText recursively transforms a scope of JSON into plain
|
|
|
|
// Caddyfile text.
|
|
|
|
func jsonToText(scope interface{}, depth int) string {
|
|
|
|
var result string
|
|
|
|
|
|
|
|
switch val := scope.(type) {
|
|
|
|
case string:
|
2015-10-29 14:22:56 +08:00
|
|
|
if strings.ContainsAny(val, "\" \n\t\r") {
|
2015-11-11 02:49:01 +08:00
|
|
|
result += `"` + strings.Replace(val, "\"", "\\\"", -1) + `"`
|
2015-10-29 14:22:56 +08:00
|
|
|
} else {
|
2015-11-11 02:49:01 +08:00
|
|
|
result += val
|
2015-10-29 14:22:56 +08:00
|
|
|
}
|
2015-10-27 14:07:22 +08:00
|
|
|
case int:
|
2015-11-11 02:49:01 +08:00
|
|
|
result += strconv.Itoa(val)
|
2015-10-27 14:07:22 +08:00
|
|
|
case float64:
|
2015-11-11 02:49:01 +08:00
|
|
|
result += fmt.Sprintf("%v", val)
|
2015-10-27 14:07:22 +08:00
|
|
|
case bool:
|
2015-11-11 02:49:01 +08:00
|
|
|
result += fmt.Sprintf("%t", val)
|
|
|
|
case [][]interface{}:
|
2015-10-27 14:07:22 +08:00
|
|
|
result += " {\n"
|
2015-11-11 02:49:01 +08:00
|
|
|
for _, arg := range val {
|
|
|
|
result += strings.Repeat("\t", depth) + jsonToText(arg, depth+1) + "\n"
|
2015-10-27 14:07:22 +08:00
|
|
|
}
|
|
|
|
result += strings.Repeat("\t", depth-1) + "}"
|
|
|
|
case []interface{}:
|
2015-11-11 02:49:01 +08:00
|
|
|
for i, v := range val {
|
|
|
|
if block, ok := v.([]interface{}); ok {
|
|
|
|
result += "{\n"
|
|
|
|
for _, arg := range block {
|
|
|
|
result += strings.Repeat("\t", depth) + jsonToText(arg, depth+1) + "\n"
|
|
|
|
}
|
|
|
|
result += strings.Repeat("\t", depth-1) + "}"
|
|
|
|
continue
|
|
|
|
}
|
2015-10-27 14:07:22 +08:00
|
|
|
result += jsonToText(v, depth)
|
2015-11-11 02:49:01 +08:00
|
|
|
if i < len(val)-1 {
|
|
|
|
result += " "
|
|
|
|
}
|
2015-10-27 14:07:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2016-01-04 07:46:26 +08:00
|
|
|
// standardizeScheme turns an address like host:https into https://host,
|
|
|
|
// or "host:" into "host".
|
|
|
|
func standardizeScheme(addr string) string {
|
|
|
|
if hostname, port, err := net.SplitHostPort(addr); err == nil {
|
|
|
|
if port == "http" || port == "https" {
|
|
|
|
addr = port + "://" + hostname
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return strings.TrimSuffix(addr, ":")
|
|
|
|
}
|
|
|
|
|
2015-11-03 03:28:50 +08:00
|
|
|
// Caddyfile encapsulates a slice of ServerBlocks.
|
2015-10-27 14:07:22 +08:00
|
|
|
type Caddyfile []ServerBlock
|
|
|
|
|
2015-11-03 03:28:50 +08:00
|
|
|
// ServerBlock represents a server block.
|
2015-10-27 14:07:22 +08:00
|
|
|
type ServerBlock struct {
|
2015-11-11 02:49:01 +08:00
|
|
|
Hosts []string `json:"hosts"`
|
|
|
|
Body [][]interface{} `json:"body"`
|
2015-10-27 14:07:22 +08:00
|
|
|
}
|