UNPKG

4.09 kBJavaScriptView Raw
1(function (Prism) {
2
3 /**
4 * @param {string} name
5 * @returns {RegExp}
6 */
7 function headerValueOf(name) {
8 return RegExp('(^(?:' + name + '):[ \t]*(?![ \t]))[^]+', 'i');
9 }
10
11 Prism.languages.http = {
12 'request-line': {
13 pattern: /^(?:CONNECT|DELETE|GET|HEAD|OPTIONS|PATCH|POST|PRI|PUT|SEARCH|TRACE)\s(?:https?:\/\/|\/)\S*\sHTTP\/[\d.]+/m,
14 inside: {
15 // HTTP Method
16 'method': {
17 pattern: /^[A-Z]+\b/,
18 alias: 'property'
19 },
20 // Request Target e.g. http://example.com, /path/to/file
21 'request-target': {
22 pattern: /^(\s)(?:https?:\/\/|\/)\S*(?=\s)/,
23 lookbehind: true,
24 alias: 'url',
25 inside: Prism.languages.uri
26 },
27 // HTTP Version
28 'http-version': {
29 pattern: /^(\s)HTTP\/[\d.]+/,
30 lookbehind: true,
31 alias: 'property'
32 },
33 }
34 },
35 'response-status': {
36 pattern: /^HTTP\/[\d.]+ \d+ .+/m,
37 inside: {
38 // HTTP Version
39 'http-version': {
40 pattern: /^HTTP\/[\d.]+/,
41 alias: 'property'
42 },
43 // Status Code
44 'status-code': {
45 pattern: /^(\s)\d+(?=\s)/,
46 lookbehind: true,
47 alias: 'number'
48 },
49 // Reason Phrase
50 'reason-phrase': {
51 pattern: /^(\s).+/,
52 lookbehind: true,
53 alias: 'string'
54 }
55 }
56 },
57 'header': {
58 pattern: /^[\w-]+:.+(?:(?:\r\n?|\n)[ \t].+)*/m,
59 inside: {
60 'header-value': [
61 {
62 pattern: headerValueOf(/Content-Security-Policy/.source),
63 lookbehind: true,
64 alias: ['csp', 'languages-csp'],
65 inside: Prism.languages.csp
66 },
67 {
68 pattern: headerValueOf(/Public-Key-Pins(?:-Report-Only)?/.source),
69 lookbehind: true,
70 alias: ['hpkp', 'languages-hpkp'],
71 inside: Prism.languages.hpkp
72 },
73 {
74 pattern: headerValueOf(/Strict-Transport-Security/.source),
75 lookbehind: true,
76 alias: ['hsts', 'languages-hsts'],
77 inside: Prism.languages.hsts
78 },
79 {
80 pattern: headerValueOf(/[^:]+/.source),
81 lookbehind: true
82 }
83 ],
84 'header-name': {
85 pattern: /^[^:]+/,
86 alias: 'keyword'
87 },
88 'punctuation': /^:/
89 }
90 }
91 };
92
93 // Create a mapping of Content-Type headers to language definitions
94 var langs = Prism.languages;
95 var httpLanguages = {
96 'application/javascript': langs.javascript,
97 'application/json': langs.json || langs.javascript,
98 'application/xml': langs.xml,
99 'text/xml': langs.xml,
100 'text/html': langs.html,
101 'text/css': langs.css,
102 'text/plain': langs.plain
103 };
104
105 // Declare which types can also be suffixes
106 var suffixTypes = {
107 'application/json': true,
108 'application/xml': true
109 };
110
111 /**
112 * Returns a pattern for the given content type which matches it and any type which has it as a suffix.
113 *
114 * @param {string} contentType
115 * @returns {string}
116 */
117 function getSuffixPattern(contentType) {
118 var suffix = contentType.replace(/^[a-z]+\//, '');
119 var suffixPattern = '\\w+/(?:[\\w.-]+\\+)+' + suffix + '(?![+\\w.-])';
120 return '(?:' + contentType + '|' + suffixPattern + ')';
121 }
122
123 // Insert each content type parser that has its associated language
124 // currently loaded.
125 var options;
126 for (var contentType in httpLanguages) {
127 if (httpLanguages[contentType]) {
128 options = options || {};
129
130 var pattern = suffixTypes[contentType] ? getSuffixPattern(contentType) : contentType;
131 options[contentType.replace(/\//g, '-')] = {
132 pattern: RegExp(
133 '(' + /content-type:\s*/.source + pattern + /(?:(?:\r\n?|\n)[\w-].*)*(?:\r(?:\n|(?!\n))|\n)/.source + ')' +
134 // This is a little interesting:
135 // The HTTP format spec required 1 empty line before the body to make everything unambiguous.
136 // However, when writing code by hand (e.g. to display on a website) people can forget about this,
137 // so we want to be liberal here. We will allow the empty line to be omitted if the first line of
138 // the body does not start with a [\w-] character (as headers do).
139 /[^ \t\w-][\s\S]*/.source,
140 'i'
141 ),
142 lookbehind: true,
143 inside: httpLanguages[contentType]
144 };
145 }
146 }
147 if (options) {
148 Prism.languages.insertBefore('http', 'header', options);
149 }
150
151}(Prism));