From 1e1e69b90f07cb7246e325ed112b49519860e6f6 Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Wed, 27 Jul 2016 12:48:39 -0600 Subject: [PATCH] Discard byte order mark (fix #962) --- caddyfile/lexer.go | 11 +++++++++++ caddyfile/lexer_test.go | 6 ++++++ 2 files changed, 17 insertions(+) diff --git a/caddyfile/lexer.go b/caddyfile/lexer.go index a3004af14..0a738aef3 100644 --- a/caddyfile/lexer.go +++ b/caddyfile/lexer.go @@ -26,9 +26,20 @@ type ( ) // load prepares the lexer to scan an input for tokens. +// It discards any leading byte order mark. func (l *lexer) load(input io.Reader) error { l.reader = bufio.NewReader(input) l.line = 1 + + // discard byte order mark, if present + firstCh, _, err := l.reader.ReadRune() + if err == nil && firstCh != 0xFEFF { + err := l.reader.UnreadRune() + if err != nil { + return err + } + } + return nil } diff --git a/caddyfile/lexer_test.go b/caddyfile/lexer_test.go index 4f8295c42..2a0b175c3 100644 --- a/caddyfile/lexer_test.go +++ b/caddyfile/lexer_test.go @@ -128,6 +128,12 @@ func TestLexer(t *testing.T) { {Line: 2, Text: "characters"}, }, }, + { + input: "\xEF\xBB\xBF:8080", // test with leading byte order mark + expected: []Token{ + {Line: 1, Text: ":8080"}, + }, + }, } for i, testCase := range testCases {