Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTP: Relax pattern for body #3169

Merged
merged 2 commits into from Nov 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion components/prism-http.js
Expand Up @@ -90,7 +90,16 @@

var pattern = suffixTypes[contentType] ? getSuffixPattern(contentType) : contentType;
options[contentType.replace(/\//g, '-')] = {
pattern: RegExp('(content-type:\\s*' + pattern + '(?:(?:\\r\\n?|\\n).+)*)(?:\\r?\\n|\\r){2}[\\s\\S]*', 'i'),
pattern: RegExp(
'(' + /content-type:\s*/.source + pattern + /(?:(?:\r\n?|\n)[\w-].*)*(?:\r(?:\n|(?!\n))|\n)/.source + ')' +
// This is a little interesting:
// The HTTP format spec required 1 empty line before the body to make everything unambiguous.
// However, when writing code by hand (e.g. to display on a website) people can forget about this,
// so we want to be liberal here. We will allow the empty line to be omitted if the first line of
// the body does not start with a [\w-] character (as headers do).
/[^\w-][\s\S]*/.source,
'i'
),
lookbehind: true,
inside: httpLanguages[contentType]
};
Expand Down
2 changes: 1 addition & 1 deletion components/prism-http.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion tests/languages/css+http/css_inclusion.test
Expand Up @@ -8,14 +8,17 @@ a.link:hover {

[
["header-name", "Content-type:"],
" text/css",
" text/css\r\n",

["text-css", [
["selector", "a.link:hover"],
["punctuation", "{"],

["property", "color"],
["punctuation", ":"],
" red",
["punctuation", ";"],

["punctuation", "}"]
]]
]
Expand Down
3 changes: 2 additions & 1 deletion tests/languages/javascript+http/issue2733.test
Expand Up @@ -43,7 +43,8 @@ transfer-encoding: chunked
" timeout=60\r\n",

["header-name", "transfer-encoding:"],
" chunked",
" chunked\r\n",

["application-json", [
["punctuation", "{"],

Expand Down
3 changes: 2 additions & 1 deletion tests/languages/javascript+http/javascript_inclusion.test
Expand Up @@ -6,7 +6,8 @@ var a = true;

[
["header-name", "Content-type:"],
" application/javascript",
" application/javascript\r\n",

["application-javascript", [
["keyword", "var"],
" a ",
Expand Down
5 changes: 3 additions & 2 deletions tests/languages/json+http/issue2733.test
Expand Up @@ -43,7 +43,8 @@ transfer-encoding: chunked
" timeout=60\r\n",

["header-name", "transfer-encoding:"],
" chunked",
" chunked\r\n",

["application-json", [
["punctuation", "{"],

Expand Down Expand Up @@ -109,4 +110,4 @@ transfer-encoding: chunked

["punctuation", "}"]
]]
]
]
145 changes: 145 additions & 0 deletions tests/languages/json+http/issue3168.test
@@ -0,0 +1,145 @@
HTTP/1.1 200 OK
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Length: 523
Content-Type: application/json
Server: gunicorn/19.9.0
X-Kong-Proxy-Latency: 1
X-Kong-Upstream-Latency: 1
{
"args": {},
"data": "",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Connection": "close",
"Host": "httpbin.org",
"User-Agent": "HTTPie/1.0.2",
"X-Forwarded-Host": "example.com"
},
"json": null,
"method": "GET",
"origin": "172.19.0.1, 52.201.239.166",
"url": "http://example.com/anything"
}

----------------------------------------------------

[
["response-status", [
["http-version", "HTTP/1.1"],
["status-code", "200"],
["reason-phrase", "OK"]
]],

["header-name", "Access-Control-Allow-Credentials:"],
" true\r\n",

["header-name", "Access-Control-Allow-Origin:"],
" *\r\n",

["header-name", "Connection:"],
" keep-alive\r\n",

["header-name", "Content-Length:"],
" 523\r\n",

["header-name", "Content-Type:"],
" application/json\r\n",

["header-name", "Server:"],
" gunicorn/19.9.0\r\n",

["header-name", "X-Kong-Proxy-Latency:"],
" 1\r\n",

["header-name", "X-Kong-Upstream-Latency:"],
" 1\r\n",

["application-json", [
["punctuation", "{"],

["property", "\"args\""],
["operator", ":"],
["punctuation", "{"],
["punctuation", "}"],
["punctuation", ","],

["property", "\"data\""],
["operator", ":"],
["string", "\"\""],
["punctuation", ","],

["property", "\"files\""],
["operator", ":"],
["punctuation", "{"],
["punctuation", "}"],
["punctuation", ","],

["property", "\"form\""],
["operator", ":"],
["punctuation", "{"],
["punctuation", "}"],
["punctuation", ","],

["property", "\"headers\""],
["operator", ":"],
["punctuation", "{"],

["property", "\"Accept\""],
["operator", ":"],
["string", "\"*/*\""],
["punctuation", ","],

["property", "\"Accept-Encoding\""],
["operator", ":"],
["string", "\"gzip, deflate\""],
["punctuation", ","],

["property", "\"Connection\""],
["operator", ":"],
["string", "\"close\""],
["punctuation", ","],

["property", "\"Host\""],
["operator", ":"],
["string", "\"httpbin.org\""],
["punctuation", ","],

["property", "\"User-Agent\""],
["operator", ":"],
["string", "\"HTTPie/1.0.2\""],
["punctuation", ","],

["property", "\"X-Forwarded-Host\""],
["operator", ":"],
["string", "\"example.com\""],

["punctuation", "}"],
["punctuation", ","],

["property", "\"json\""],
["operator", ":"],
["null", "null"],
["punctuation", ","],

["property", "\"method\""],
["operator", ":"],
["string", "\"GET\""],
["punctuation", ","],

["property", "\"origin\""],
["operator", ":"],
["string", "\"172.19.0.1, 52.201.239.166\""],
["punctuation", ","],

["property", "\"url\""],
["operator", ":"],
["string", "\"http://example.com/anything\""],

["punctuation", "}"]
]]
]
3 changes: 2 additions & 1 deletion tests/languages/json+http/json-suffix_inclusion.test
Expand Up @@ -6,7 +6,8 @@ Content-type: application/x.foo+bar+json

[
["header-name", "Content-type:"],
" application/x.foo+bar+json",
" application/x.foo+bar+json\r\n",

["application-json", [
["punctuation", "{"],
["property", "\"foo\""],
Expand Down
3 changes: 2 additions & 1 deletion tests/languages/json+http/json_inclusion.test
Expand Up @@ -6,7 +6,8 @@ Content-type: application/json

[
["header-name", "Content-type:"],
" application/json",
" application/json\r\n",

["application-json", [
["punctuation", "{"],
["property", "\"foo\""],
Expand Down
3 changes: 2 additions & 1 deletion tests/languages/markup+http/html_inclusion.test
Expand Up @@ -6,7 +6,8 @@ Content-type: text/html

[
["header-name", "Content-type:"],
" text/html",
" text/html\r\n",

["text-html", [
["tag", [
["tag", [
Expand Down
5 changes: 3 additions & 2 deletions tests/languages/markup+http/issue2733.test
Expand Up @@ -32,7 +32,8 @@ transfer-encoding: chunked
" timeout=60\r\n",

["header-name", "transfer-encoding:"],
" chunked",
" chunked\r\n",

["application-xml", [
["tag", [
["tag", [
Expand Down Expand Up @@ -82,4 +83,4 @@ transfer-encoding: chunked
["punctuation", ">"]
]]
]]
]
]
3 changes: 2 additions & 1 deletion tests/languages/markup+http/text-xml_inclusion.test
Expand Up @@ -6,7 +6,8 @@ Content-type: text/xml

[
["header-name", "Content-type:"],
" text/xml",
" text/xml\r\n",

["text-xml", [
["tag", [
["tag", [
Expand Down
3 changes: 2 additions & 1 deletion tests/languages/markup+http/xml-suffix_inclusion.test
Expand Up @@ -6,7 +6,8 @@ Content-type: text/x.anything+something-else+xml

[
["header-name", "Content-type:"],
" text/x.anything+something-else+xml",
" text/x.anything+something-else+xml\r\n",

["application-xml", [
["tag", [
["tag", [
Expand Down
3 changes: 2 additions & 1 deletion tests/languages/markup+http/xml_inclusion.test
Expand Up @@ -6,7 +6,8 @@ Content-type: application/xml

[
["header-name", "Content-type:"],
" application/xml",
" application/xml\r\n",

["application-xml", [
["tag", [
["tag", [
Expand Down