Skip to content

Commit 6183fd9

Browse files
authoredJan 18, 2021
HTTP: More granular tokenization (#2722)
1 parent 7a790bf commit 6183fd9

File tree

4 files changed

+148
-51
lines changed

4 files changed

+148
-51
lines changed
 

‎components/prism-http.js

+35-10
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,46 @@
11
(function (Prism) {
22
Prism.languages.http = {
33
'request-line': {
4-
pattern: /^(?:POST|GET|PUT|DELETE|OPTIONS|PATCH|TRACE|CONNECT)\s(?:https?:\/\/|\/)\S*\sHTTP\/[0-9.]+/m,
4+
pattern: /^(?:GET|HEAD|POST|PUT|DELETE|CONNECT|OPTIONS|TRACE|PATCH|PRI|SEARCH)\s(?:https?:\/\/|\/)\S*\sHTTP\/[0-9.]+/m,
Has conversations. Original line has conversations.
55
inside: {
6-
// HTTP Verb
7-
'property': /^(?:POST|GET|PUT|DELETE|OPTIONS|PATCH|TRACE|CONNECT)\b/,
8-
// Path or query argument
9-
'attr-name': /:\w+/
6+
// HTTP Method
7+
'method': {
8+
pattern: /^[A-Z]+\b/,
9+
alias: 'property'
10+
},
11+
// Request Target e.g. http://example.com, /path/to/file
12+
'request-target': {
13+
pattern: /^(\s)(?:https?:\/\/|\/)\S*(?=\s)/,
14+
lookbehind: true,
15+
alias: 'url'
16+
},
17+
// HTTP Version
18+
'http-version': {
19+
pattern: /^(\s)HTTP\/[0-9.]+/,
20+
lookbehind: true,
21+
alias: 'property'
22+
},
1023
}
1124
},
1225
'response-status': {
13-
pattern: /^HTTP\/1.[01] \d.*/m,
26+
pattern: /^HTTP\/[0-9.]+ \d+ .+/m,
1427
inside: {
15-
// Status, e.g. 200 OK
16-
'property': {
17-
pattern: /(^HTTP\/1.[01] )\d.*/i,
18-
lookbehind: true
28+
// HTTP Version
29+
'http-version': {
30+
pattern: /^HTTP\/[0-9.]+/,
31+
alias: 'property'
32+
},
33+
// Status Code
34+
'status-code': {
35+
pattern: /^(\s)\d+(?=\s)/,
36+
lookbehind: true,
37+
alias: 'number'
38+
},
39+
// Reason Phrase
40+
'reason-phrase': {
41+
pattern: /^(\s).+/,
42+
lookbehind: true,
43+
alias: 'string'
1944
}
2045
}
2146
},

‎components/prism-http.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎tests/languages/http/request-line_feature.test

+99-31
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,126 @@
1-
POST http://example.com HTTP/1.0
2-
GET http://localhost:9999/foo.html HTTP/1.1
3-
PUT http://www.example.com HTTP/2.0
4-
DELETE https://example.com HTTP/1.1
5-
OPTIONS https://www.example.com HTTP/1.1
6-
PATCH http://example.com HTTP/1.0
7-
TRACE http://example.com HTTP/1.0
8-
CONNECT http://example.com HTTP/1.0
9-
GET /path/to/foo.html HTTP/1.1
1+
GET / HTTP/1.0
102
GET / HTTP/1.1
3+
GET /path/to/file HTTP/1.1
4+
GET /path/to/file?a=1&b=2 HTTP/1.1
5+
GET http://example.com HTTP/1.1
6+
GET https://example.com HTTP/1.1
7+
GET https://example.com/ HTTP/1.1
8+
GET https://example.com/path/to/file?a=1&b=2 HTTP/1.1
9+
GET https://example.com:443/path/to/file?a=1&b=2 HTTP/1.1
10+
GET https://user:pass@example.com:443/path/to/file?a=1&b=2 HTTP/1.1
11+
HEAD / HTTP/1.1
12+
POST / HTTP/1.1
13+
PUT / HTTP/1.1
14+
DELETE / HTTP/1.1
15+
CONNECT / HTTP/1.1
16+
OPTIONS / HTTP/1.1
17+
TRACE / HTTP/1.1
18+
PATCH / HTTP/1.1
19+
PRI / HTTP/1.1
20+
SEARCH / HTTP/1.1
1121

1222
----------------------------------------------------
1323

1424
[
1525
["request-line", [
16-
["property", "POST"],
17-
" http://example.com HTTP/1.0"
26+
["method", "GET"],
27+
["request-target", "/"],
28+
["http-version", "HTTP/1.0"]
1829
]],
1930
["request-line", [
20-
["property", "GET"],
21-
" http://localhost",
22-
["attr-name", ":9999"],
23-
"/foo.html HTTP/1.1"
31+
["method", "GET"],
32+
["request-target", "/"],
33+
["http-version", "HTTP/1.1"]
2434
]],
2535
["request-line", [
26-
["property", "PUT"],
27-
" http://www.example.com HTTP/2.0"
36+
["method", "GET"],
37+
["request-target", "/path/to/file"],
38+
["http-version", "HTTP/1.1"]
2839
]],
2940
["request-line", [
30-
["property", "DELETE"],
31-
" https://example.com HTTP/1.1"
41+
["method", "GET"],
42+
["request-target", "/path/to/file?a=1&b=2"],
43+
["http-version", "HTTP/1.1"]
3244
]],
3345
["request-line", [
34-
["property", "OPTIONS"],
35-
" https://www.example.com HTTP/1.1"
46+
["method", "GET"],
47+
["request-target", "http://example.com"],
48+
["http-version", "HTTP/1.1"]
3649
]],
3750
["request-line", [
38-
["property", "PATCH"],
39-
" http://example.com HTTP/1.0"
51+
["method", "GET"],
52+
["request-target", "https://example.com"],
53+
["http-version", "HTTP/1.1"]
4054
]],
4155
["request-line", [
42-
["property", "TRACE"],
43-
" http://example.com HTTP/1.0"
56+
["method", "GET"],
57+
["request-target", "https://example.com/"],
58+
["http-version", "HTTP/1.1"]
4459
]],
4560
["request-line", [
46-
["property", "CONNECT"],
47-
" http://example.com HTTP/1.0"
61+
["method", "GET"],
62+
["request-target", "https://example.com/path/to/file?a=1&b=2"],
63+
["http-version", "HTTP/1.1"]
4864
]],
4965
["request-line", [
50-
["property", "GET"],
51-
" /path/to/foo.html HTTP/1.1"
66+
["method", "GET"],
67+
["request-target", "https://example.com:443/path/to/file?a=1&b=2"],
68+
["http-version", "HTTP/1.1"]
5269
]],
5370
["request-line", [
54-
["property", "GET"],
55-
" / HTTP/1.1"
71+
["method", "GET"],
72+
["request-target", "https://user:pass@example.com:443/path/to/file?a=1&b=2"],
73+
["http-version", "HTTP/1.1"]
74+
]],
75+
["request-line", [
76+
["method", "HEAD"],
77+
["request-target", "/"],
78+
["http-version", "HTTP/1.1"]
79+
]],
80+
["request-line", [
81+
["method", "POST"],
82+
["request-target", "/"],
83+
["http-version", "HTTP/1.1"]
84+
]],
85+
["request-line", [
86+
["method", "PUT"],
87+
["request-target", "/"],
88+
["http-version", "HTTP/1.1"]
89+
]],
90+
["request-line", [
91+
["method", "DELETE"],
92+
["request-target", "/"],
93+
["http-version", "HTTP/1.1"]
94+
]],
95+
["request-line", [
96+
["method", "CONNECT"],
97+
["request-target", "/"],
98+
["http-version", "HTTP/1.1"]
99+
]],
100+
["request-line", [
101+
["method", "OPTIONS"],
102+
["request-target", "/"],
103+
["http-version", "HTTP/1.1"]
104+
]],
105+
["request-line", [
106+
["method", "TRACE"],
107+
["request-target", "/"],
108+
["http-version", "HTTP/1.1"]
109+
]],
110+
["request-line", [
111+
["method", "PATCH"],
112+
["request-target", "/"],
113+
["http-version", "HTTP/1.1"]
114+
]],
115+
["request-line", [
116+
["method", "PRI"],
117+
["request-target", "/"],
118+
["http-version", "HTTP/1.1"]
119+
]],
120+
["request-line", [
121+
["method", "SEARCH"],
122+
["request-target", "/"],
123+
["http-version", "HTTP/1.1"]
56124
]]
57125
]
58126

‎tests/languages/http/response-status_feature.test

+13-9
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,27 @@ HTTP/1.0 418 I'm a teapot
77

88
[
99
["response-status", [
10-
"HTTP/1.0 ",
11-
["property", "200 OK"]
10+
["http-version", "HTTP/1.0"],
11+
["status-code", "200"],
12+
["reason-phrase", "OK"]
1213
]],
1314
["response-status", [
14-
"HTTP/1.1 ",
15-
["property", "403 Forbidden"]
15+
["http-version", "HTTP/1.1"],
16+
["status-code", "403"],
17+
["reason-phrase", "Forbidden"]
1618
]],
1719
["response-status", [
18-
"HTTP/1.1 ",
19-
["property", "404 Not Found"]
20+
["http-version", "HTTP/1.1"],
21+
["status-code", "404"],
22+
["reason-phrase", "Not Found"]
2023
]],
2124
["response-status", [
22-
"HTTP/1.0 ",
23-
["property", "418 I'm a teapot"]
25+
["http-version", "HTTP/1.0"],
26+
["status-code", "418"],
27+
["reason-phrase", "I'm a teapot"]
2428
]]
2529
]
2630

2731
----------------------------------------------------
2832

29-
Checks for response statuses.
33+
Checks for response statuses.

0 commit comments

Comments
 (0)
Please sign in to comment.