// SPDX-License-Identifier: GPL-3.0-or-later // Package header reads the header of a page or a news file: lines of // "Key: value" ended by a line that holds a single "-", then the body. package header import ( "fmt" "strings" ) // terminator is the line that ends a header. const terminator = "-" // Header is the ordered set of keys and values of one file. type Header struct { keys []string values map[string]string } // Parse splits src into its header and its body. The name is used in error // messages, which also carry the line number. A leading byte order mark and // CRLF line endings are accepted; the body is returned with LF endings and // without the blank lines that follow the terminator. func Parse(name string, src []byte) (Header, string, error) { text := strings.TrimPrefix(string(src), "\xef\xbb\xbf") text = strings.ReplaceAll(text, "\r\n", "\n") lines := strings.Split(text, "\n") h := Header{values: make(map[string]string)} for i, line := range lines { number := i + 1 if strings.TrimRight(line, " \t") == terminator { body := strings.TrimLeft(strings.Join(lines[i+1:], "\n"), "\n") return h, body, nil } if strings.TrimSpace(line) == "" { continue } key, value, err := splitLine(line) if err != nil { return Header{}, "", fmt.Errorf("%s:%d: %w", name, number, err) } if _, dup := h.values[key]; dup { return Header{}, "", fmt.Errorf("%s:%d: duplicate key %q", name, number, key) } h.keys = append(h.keys, key) h.values[key] = value } return Header{}, "", fmt.Errorf("%s:%d: the header is not ended by a line with a single %q", name, len(lines), terminator) } // splitLine splits "Key: value" at the first colon followed by a space, or at // a colon that ends the line (an empty value). func splitLine(line string) (key, value string, err error) { if i := strings.Index(line, ": "); i >= 0 { key, value = line[:i], line[i+2:] } else if strings.HasSuffix(line, ":") { key = strings.TrimSuffix(line, ":") } else { return "", "", fmt.Errorf("line %q is not of the form \"Key: value\"", line) } key = strings.TrimSpace(key) if key == "" { return "", "", fmt.Errorf("empty key in line %q", line) } return key, strings.TrimSpace(value), nil } // Get returns the value of key, or an empty string when the key is absent. func (h Header) Get(key string) string { return h.values[key] } // Has reports whether key is present, even with an empty value. func (h Header) Has(key string) bool { _, ok := h.values[key] return ok } // Require returns an error naming name and the first key of keys that is // missing or has an empty value. func (h Header) Require(name string, keys ...string) error { for _, k := range keys { if !h.Has(k) { return fmt.Errorf("%s: missing key %q", name, k) } if h.Get(k) == "" { return fmt.Errorf("%s: the key %q is empty", name, k) } } return nil } // Only returns an error naming name and the first key that is not in allowed. func (h Header) Only(name string, allowed ...string) error { ok := make(map[string]bool, len(allowed)) for _, k := range allowed { ok[k] = true } for _, k := range h.keys { if !ok[k] { return fmt.Errorf("%s: unknown key %q (allowed: %s)", name, k, strings.Join(allowed, ", ")) } } return nil }