Skip to content

Commit

Permalink
fix(http) avoid recursive sublanguage and tighten rules (#2893)
Browse files Browse the repository at this point in the history
* tweaks, add response markup test
* do not trigger autodetect * unless we are sure it is HTTP
* more specific rules
* add illegal to child modes
  • Loading branch information
joshgoebel authored Dec 11, 2020
1 parent 0f5f591 commit a56d416
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ New Languages:
Language improvements:

- enh(makefile): Add `make` as an alias (#2883) [tripleee][]
- fix(http) avoid recursive sublanguage and tighten rules (#2893) [Josh Goebel][]
- enh(swift) Improved grammar for strings (#2819) [Steven Van Impe][]
- fix(asciidoc): Handle section titles level 5 (#2868) [Vaibhav Chanana][]

Expand Down
78 changes: 60 additions & 18 deletions src/languages/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,85 @@ Category: common, protocols
Website: https://developer.mozilla.org/en-US/docs/Web/HTTP/Overview
*/

import * as regex from '../lib/regex.js';

export default function(hljs) {
var VERSION = 'HTTP/[0-9\\.]+';
const VERSION = 'HTTP/(2|1\\.[01])';
const HEADER_NAME = /[A-Za-z][A-Za-z0-9-]*/;
const HEADERS_AND_BODY = [
{
className: 'attribute',
begin: regex.concat('^', HEADER_NAME, '(?=\\:\\s)'),
starts: {
contains: [
{
className: "punctuation",
begin: /: /,
relevance: 0,
starts: {
end: '$',
relevance: 0
}
}
]
}
},
{
begin: '\\n\\n',
starts: { subLanguage: [], endsWithParent: true }
}
];

return {
name: 'HTTP',
aliases: ['https'],
illegal: '\\S',
illegal: /\S/,
contains: [
// response
{
begin: '^' + VERSION, end: '$',
contains: [{className: 'number', begin: '\\b\\d{3}\\b'}]
begin: '^(?=' + VERSION + " \\d{3})",
end: /$/,
contains: [
{
className: "meta",
begin: VERSION
},
{
className: 'number', begin: '\\b\\d{3}\\b'
}
],
starts: {
end: /\b\B/,
illegal: /\S/,
contains: HEADERS_AND_BODY
}
},
// request
{
begin: '^[A-Z]+ (.*?) ' + VERSION + '$', returnBegin: true, end: '$',
begin: '(?=^[A-Z]+ (.*?) ' + VERSION + '$)',
end: /$/,
contains: [
{
className: 'string',
begin: ' ', end: ' ',
excludeBegin: true, excludeEnd: true
begin: ' ',
end: ' ',
excludeBegin: true,
excludeEnd: true
},
{
className: "meta",
begin: VERSION
},
{
className: 'keyword',
begin: '[A-Z]+'
}
]
},
{
className: 'attribute',
begin: '^\\w', end: ': ', excludeEnd: true,
illegal: '\\n|\\s|=',
starts: {end: '$', relevance: 0}
},
{
begin: '\\n\\n',
starts: {subLanguage: [], endsWithParent: true}
],
starts: {
end: /\b\B/,
illegal: /\S/,
contains: HEADERS_AND_BODY
}
}
]
};
Expand Down
8 changes: 4 additions & 4 deletions test/markup/http/default.expect.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<span class="hljs-keyword">POST</span> <span class="hljs-string">/task?id=1</span> HTTP/1.1
<span class="hljs-attribute">Host</span>: example.org
<span class="hljs-attribute">Content-Type</span>: application/json; charset=utf-8
<span class="hljs-attribute">Content-Length</span>: 19
<span class="hljs-keyword">POST</span> <span class="hljs-string">/task?id=1</span> <span class="hljs-meta">HTTP/1.1</span>
<span class="hljs-attribute">Host</span><span class="hljs-punctuation">: </span>example.org
<span class="hljs-attribute">Content-Type</span><span class="hljs-punctuation">: </span>application/json; charset=utf-8
<span class="hljs-attribute">Content-Length</span><span class="hljs-punctuation">: </span>19

<span class="json">{<span class="hljs-attr">&quot;status&quot;</span>: <span class="hljs-string">&quot;ok&quot;</span>, <span class="hljs-attr">&quot;extended&quot;</span>: <span class="hljs-literal">true</span>}
</span>
11 changes: 11 additions & 0 deletions test/markup/http/response.expect.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<span class="hljs-meta">HTTP/2</span> <span class="hljs-number">301</span>
<span class="hljs-attribute">location</span><span class="hljs-punctuation">: </span>https://www.google.com/
<span class="hljs-attribute">content-type</span><span class="hljs-punctuation">: </span>text/html; charset=UTF-8
<span class="hljs-attribute">date</span><span class="hljs-punctuation">: </span>Sat, 31 Oct 2020 22:47:14 GMT
<span class="hljs-attribute">expires</span><span class="hljs-punctuation">: </span>Mon, 30 Nov 2020 22:47:14 GMT
<span class="hljs-attribute">cache-control</span><span class="hljs-punctuation">: </span>public, max-age=2592000
<span class="hljs-attribute">server</span><span class="hljs-punctuation">: </span>gws
<span class="hljs-attribute">content-length</span><span class="hljs-punctuation">: </span>220
<span class="hljs-attribute">x-xss-protection</span><span class="hljs-punctuation">: </span>0
<span class="hljs-attribute">x-frame-options</span><span class="hljs-punctuation">: </span>SAMEORIGIN
<span class="hljs-attribute">alt-svc</span><span class="hljs-punctuation">: </span>h3-Q050=&quot;:443&quot;; ma=2592000,h3-29=&quot;:443&quot;; ma=2592000,h3-T051=&quot;:443&quot;; ma=2592000,h3-T050=&quot;:443&quot;; ma=2592000,h3-Q046=&quot;:443&quot;; ma=2592000,h3-Q043=&quot;:443&quot;; ma=2592000,quic=&quot;:443&quot;; ma=2592000; v=&quot;46,43&quot;
11 changes: 11 additions & 0 deletions test/markup/http/response.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
HTTP/2 301
location: https://www.google.com/
content-type: text/html; charset=UTF-8
date: Sat, 31 Oct 2020 22:47:14 GMT
expires: Mon, 30 Nov 2020 22:47:14 GMT
cache-control: public, max-age=2592000
server: gws
content-length: 220
x-xss-protection: 0
x-frame-options: SAMEORIGIN
alt-svc: h3-Q050=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-T051=":443"; ma=2592000,h3-T050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"

0 comments on commit a56d416

Please sign in to comment.