Skip to content

Commit

Permalink
feat: support HTTP/2 in HTTP lexer
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgtaylor authored and alecthomas committed May 14, 2020
1 parent 2612dee commit 4065717
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lexers/h/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ var HTTP = internal.Register(httpBodyContentTypeLexer(MustNewLexer(
},
Rules{
"root": {
{`(GET|POST|PUT|DELETE|HEAD|OPTIONS|TRACE|PATCH|CONNECT)( +)([^ ]+)( +)(HTTP)(/)(1\.[01])(\r?\n|\Z)`, ByGroups(NameFunction, Text, NameNamespace, Text, KeywordReserved, Operator, LiteralNumber, Text), Push("headers")},
{`(HTTP)(/)(1\.[01])( +)(\d{3})( +)([^\r\n]+)(\r?\n|\Z)`, ByGroups(KeywordReserved, Operator, LiteralNumber, Text, LiteralNumber, Text, NameException, Text), Push("headers")},
{`(GET|POST|PUT|DELETE|HEAD|OPTIONS|TRACE|PATCH|CONNECT)( +)([^ ]+)( +)(HTTP)(/)([12]\.[01])(\r?\n|\Z)`, ByGroups(NameFunction, Text, NameNamespace, Text, KeywordReserved, Operator, LiteralNumber, Text), Push("headers")},
{`(HTTP)(/)([12]\.[01])( +)(\d{3})( +)([^\r\n]+)(\r?\n|\Z)`, ByGroups(KeywordReserved, Operator, LiteralNumber, Text, LiteralNumber, Text, NameException, Text), Push("headers")},
},
"headers": {
{`([^\s:]+)( *)(:)( *)([^\r\n]+)(\r?\n|\Z)`, EmitterFunc(httpHeaderBlock), nil},
Expand Down
3 changes: 2 additions & 1 deletion lexers/lexers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ func TestLexers(t *testing.T) {
continue
}

lexer := lexers.Get(strings.TrimSuffix(file.Name(), filepath.Ext(file.Name())))
base := strings.Split(strings.TrimSuffix(file.Name(), filepath.Ext(file.Name())), "-")[0]
lexer := lexers.Get(base)
assert.NotNil(t, lexer)

filename := filepath.Join("testdata", file.Name())
Expand Down
5 changes: 5 additions & 0 deletions lexers/testdata/http-2.actual
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
GET /foo HTTP/2.0
Content-Type: application/json
User-Agent: foo

{"hello": "world"}
27 changes: 27 additions & 0 deletions lexers/testdata/http-2.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[
{"type":"NameFunction","value":"GET"},
{"type":"Text","value":" "},
{"type":"NameNamespace","value":"/foo"},
{"type":"Text","value":" "},
{"type":"KeywordReserved","value":"HTTP"},
{"type":"Operator","value":"/"},
{"type":"LiteralNumber","value":"2.0"},
{"type":"Text","value":"\n"},
{"type":"Name","value":"Content-Type"},
{"type":"Operator","value":":"},
{"type":"Text","value":" "},
{"type":"Literal","value":"application/json"},
{"type":"Text","value":"\n"},
{"type":"Name","value":"User-Agent"},
{"type":"Operator","value":":"},
{"type":"Text","value":" "},
{"type":"Literal","value":"foo"},
{"type":"Text","value":"\n\n"},
{"type":"Punctuation","value":"{"},
{"type":"NameTag","value":"\"hello\""},
{"type":"Punctuation","value":":"},
{"type":"Text","value":" "},
{"type":"LiteralStringDouble","value":"\"world\""},
{"type":"Punctuation","value":"}"},
{"type":"Text","value":"\n"}
]

0 comments on commit 4065717

Please sign in to comment.