1
0
mirror of https://github.com/alecthomas/chroma.git synced 2025-01-28 03:29:41 +02:00

support HTTP/2 and HTTP/3 (#774)

This commit is contained in:
ifNil 2023-03-26 06:08:11 +08:00 committed by GitHub
parent 357f0cbe40
commit 4f02bc5f68
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 68 additions and 4 deletions

View File

@ -22,8 +22,8 @@ var HTTP = Register(httpBodyContentTypeLexer(MustNewLexer(
func httpRules() Rules {
return Rules{
"root": {
{`(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")},
{`(GET|POST|PUT|DELETE|HEAD|OPTIONS|TRACE|PATCH|CONNECT)( +)([^ ]+)( +)(HTTP)(/)([123](?:\.[01])?)(\r?\n|\Z)`, ByGroups(NameFunction, Text, NameNamespace, Text, KeywordReserved, Operator, LiteralNumber, Text), Push("headers")},
{`(HTTP)(/)([123](?:\.[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},

5
lexers/testdata/http.2.0.actual vendored Normal file
View File

@ -0,0 +1,5 @@
GET /foo HTTP/2.0
Content-Type: application/json
User-Agent: foo
{"hello": "world"}

27
lexers/testdata/http.2.0.expected vendored Normal file
View File

@ -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"}
]

View File

@ -1,4 +1,4 @@
GET /foo HTTP/2.0
GET /foo HTTP/2
Content-Type: application/json
User-Agent: foo

View File

@ -5,7 +5,7 @@
{"type":"Text","value":" "},
{"type":"KeywordReserved","value":"HTTP"},
{"type":"Operator","value":"/"},
{"type":"LiteralNumber","value":"2.0"},
{"type":"LiteralNumber","value":"2"},
{"type":"Text","value":"\n"},
{"type":"Name","value":"Content-Type"},
{"type":"Operator","value":":"},

5
lexers/testdata/http.3.actual vendored Normal file
View File

@ -0,0 +1,5 @@
GET /foo HTTP/3
Content-Type: application/json
User-Agent: foo
{"hello": "world"}

27
lexers/testdata/http.3.expected vendored Normal file
View File

@ -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":"3"},
{"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"}
]