1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
|
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2015 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package asserts
import (
"bytes"
"fmt"
"regexp"
"sort"
"strings"
"unicode/utf8"
)
var (
nl = []byte("\n")
nlnl = []byte("\n\n")
// for basic validity checking of header names
headerNameValidity = regexp.MustCompile("^[a-z](?:-?[a-z0-9])*$")
)
func parseHeaders(head []byte) (map[string]any, error) {
if !utf8.Valid(head) {
return nil, fmt.Errorf("header is not utf8")
}
headers := make(map[string]any)
lines := strings.Split(string(head), "\n")
for i := 0; i < len(lines); {
entry := lines[i]
nameValueSplit := strings.Index(entry, ":")
if nameValueSplit == -1 {
return nil, fmt.Errorf("header entry missing ':' separator: %q", entry)
}
name := entry[:nameValueSplit]
if !headerNameValidity.MatchString(name) {
return nil, fmt.Errorf("invalid header name: %q", name)
}
consumed := nameValueSplit + 1
var value any
var err error
value, i, err = parseEntry(consumed, i, lines, 0)
if err != nil {
return nil, err
}
if _, ok := headers[name]; ok {
return nil, fmt.Errorf("repeated header: %q", name)
}
headers[name] = value
}
return headers, nil
}
const (
commonPrefix = " "
multilinePrefix = " "
listChar = "-"
listPrefix = commonPrefix + listChar
)
func nestingPrefix(baseIndent int, prefix string) string {
return strings.Repeat(" ", baseIndent) + prefix
}
func parseEntry(consumedByIntro int, first int, lines []string, baseIndent int) (value any, firstAfter int, err error) {
entry := lines[first]
i := first + 1
if consumedByIntro == len(entry) {
// multiline values
basePrefix := nestingPrefix(baseIndent, commonPrefix)
if i < len(lines) && strings.HasPrefix(lines[i], basePrefix) {
rest := lines[i][len(basePrefix):]
if strings.HasPrefix(rest, listChar) {
// list
return parseList(i, lines, baseIndent)
}
if len(rest) > 0 && rest[0] != ' ' {
// map
return parseMap(i, lines, baseIndent)
}
}
return parseMultilineText(i, lines, baseIndent)
}
// simple one-line value
if entry[consumedByIntro] != ' ' {
return nil, -1, fmt.Errorf("header entry should have a space or newline (for multiline) before value: %q", entry)
}
return entry[consumedByIntro+1:], i, nil
}
func parseMultilineText(first int, lines []string, baseIndent int) (value any, firstAfter int, err error) {
size := 0
i := first
j := i
prefix := nestingPrefix(baseIndent, multilinePrefix)
for j < len(lines) {
iline := lines[j]
if !strings.HasPrefix(iline, prefix) {
break
}
size += len(iline) - len(prefix) + 1
j++
}
if j == i {
var cur string
if i == len(lines) {
cur = "EOF"
} else {
cur = fmt.Sprintf("%q", lines[i])
}
return nil, -1, fmt.Errorf("expected %d chars nesting prefix after multiline introduction %q: %s", len(prefix), lines[i-1], cur)
}
valueBuf := bytes.NewBuffer(make([]byte, 0, size-1))
valueBuf.WriteString(lines[i][len(prefix):])
i++
for i < j {
valueBuf.WriteByte('\n')
valueBuf.WriteString(lines[i][len(prefix):])
i++
}
return valueBuf.String(), i, nil
}
func parseList(first int, lines []string, baseIndent int) (value any, firstAfter int, err error) {
lst := []any(nil)
j := first
prefix := nestingPrefix(baseIndent, listPrefix)
for j < len(lines) {
if !strings.HasPrefix(lines[j], prefix) {
return lst, j, nil
}
var v any
var err error
v, j, err = parseEntry(len(prefix), j, lines, baseIndent+len(listPrefix)-1)
if err != nil {
return nil, -1, err
}
lst = append(lst, v)
}
return lst, j, nil
}
func parseMap(first int, lines []string, baseIndent int) (value any, firstAfter int, err error) {
m := make(map[string]any)
j := first
prefix := nestingPrefix(baseIndent, commonPrefix)
for j < len(lines) {
if !strings.HasPrefix(lines[j], prefix) {
return m, j, nil
}
entry := lines[j][len(prefix):]
keyValueSplit := strings.Index(entry, ":")
if keyValueSplit == -1 {
return nil, -1, fmt.Errorf("map entry missing ':' separator: %q", entry)
}
key := entry[:keyValueSplit]
if !headerNameValidity.MatchString(key) {
return nil, -1, fmt.Errorf("invalid map entry key: %q", key)
}
consumed := keyValueSplit + 1
var value any
var err error
value, j, err = parseEntry(len(prefix)+consumed, j, lines, len(prefix))
if err != nil {
return nil, -1, err
}
if _, ok := m[key]; ok {
return nil, -1, fmt.Errorf("repeated map entry: %q", key)
}
m[key] = value
}
return m, j, nil
}
// checkHeader checks that the header values are strings, or nested lists or maps with strings as the only scalars
func checkHeader(v any) error {
switch x := v.(type) {
case string:
return nil
case []any:
for _, elem := range x {
err := checkHeader(elem)
if err != nil {
return err
}
}
return nil
case map[string]any:
for _, elem := range x {
err := checkHeader(elem)
if err != nil {
return err
}
}
return nil
default:
return fmt.Errorf("header values must be strings or nested lists or maps with strings as the only scalars: %v", v)
}
}
// checkHeaders checks that headers are of expected types
func checkHeaders(headers map[string]any) error {
for name, value := range headers {
err := checkHeader(value)
if err != nil {
return fmt.Errorf("header %q: %v", name, err)
}
}
return nil
}
// copyHeader helps deep copying header values to defend against external mutations
func copyHeader(v any) any {
switch x := v.(type) {
case string:
return x
case []any:
res := make([]any, len(x))
for i, elem := range x {
res[i] = copyHeader(elem)
}
return res
case map[string]any:
res := make(map[string]any, len(x))
for name, value := range x {
if value == nil {
continue // normalize nils out
}
res[name] = copyHeader(value)
}
return res
default:
panic(fmt.Sprintf("internal error: encountered unexpected value type copying headers: %v", v))
}
}
// copyHeader helps deep copying headers to defend against external mutations
func copyHeaders(headers map[string]any) map[string]any {
return copyHeader(headers).(map[string]any)
}
func appendEntry(buf *bytes.Buffer, intro string, v any, baseIndent int) {
switch x := v.(type) {
case nil:
return // omit
case string:
buf.WriteByte('\n')
buf.WriteString(intro)
if strings.ContainsRune(x, '\n') {
// multiline value => quote by 4-space indenting
buf.WriteByte('\n')
pfx := nestingPrefix(baseIndent, multilinePrefix)
buf.WriteString(pfx)
x = strings.Replace(x, "\n", "\n"+pfx, -1)
} else {
buf.WriteByte(' ')
}
buf.WriteString(x)
case []any:
if len(x) == 0 {
return // simply omit
}
buf.WriteByte('\n')
buf.WriteString(intro)
pfx := nestingPrefix(baseIndent, listPrefix)
for _, elem := range x {
appendEntry(buf, pfx, elem, baseIndent+len(listPrefix)-1)
}
case map[string]any:
if len(x) == 0 {
return // simply omit
}
buf.WriteByte('\n')
buf.WriteString(intro)
// emit entries sorted by key
keys := make([]string, len(x))
i := 0
for key := range x {
keys[i] = key
i++
}
sort.Strings(keys)
pfx := nestingPrefix(baseIndent, commonPrefix)
for _, key := range keys {
appendEntry(buf, pfx+key+":", x[key], len(pfx))
}
default:
panic(fmt.Sprintf("internal error: encountered unexpected value type formatting headers: %v", v))
}
}
|