UNPKG

2.68 kBJavaScriptView Raw
1(function (Prism) {
2 Prism.languages.http = {
3 'request-line': {
4 pattern: /^(?:GET|HEAD|POST|PUT|DELETE|CONNECT|OPTIONS|TRACE|PATCH|PRI|SEARCH)\s(?:https?:\/\/|\/)\S*\sHTTP\/[0-9.]+/m,
5 inside: {
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 inside: Prism.languages.uri
17 },
18 // HTTP Version
19 'http-version': {
20 pattern: /^(\s)HTTP\/[0-9.]+/,
21 lookbehind: true,
22 alias: 'property'
23 },
24 }
25 },
26 'response-status': {
27 pattern: /^HTTP\/[0-9.]+ \d+ .+/m,
28 inside: {
29 // HTTP Version
30 'http-version': {
31 pattern: /^HTTP\/[0-9.]+/,
32 alias: 'property'
33 },
34 // Status Code
35 'status-code': {
36 pattern: /^(\s)\d+(?=\s)/,
37 lookbehind: true,
38 alias: 'number'
39 },
40 // Reason Phrase
41 'reason-phrase': {
42 pattern: /^(\s).+/,
43 lookbehind: true,
44 alias: 'string'
45 }
46 }
47 },
48 // HTTP header name
49 'header-name': {
50 pattern: /^[\w-]+:(?=.)/m,
51 alias: 'keyword'
52 }
53 };
54
55 // Create a mapping of Content-Type headers to language definitions
56 var langs = Prism.languages;
57 var httpLanguages = {
58 'application/javascript': langs.javascript,
59 'application/json': langs.json || langs.javascript,
60 'application/xml': langs.xml,
61 'text/xml': langs.xml,
62 'text/html': langs.html,
63 'text/css': langs.css
64 };
65
66 // Declare which types can also be suffixes
67 var suffixTypes = {
68 'application/json': true,
69 'application/xml': true
70 };
71
72 /**
73 * Returns a pattern for the given content type which matches it and any type which has it as a suffix.
74 *
75 * @param {string} contentType
76 * @returns {string}
77 */
78 function getSuffixPattern(contentType) {
79 var suffix = contentType.replace(/^[a-z]+\//, '');
80 var suffixPattern = '\\w+/(?:[\\w.-]+\\+)+' + suffix + '(?![+\\w.-])';
81 return '(?:' + contentType + '|' + suffixPattern + ')';
82 }
83
84 // Insert each content type parser that has its associated language
85 // currently loaded.
86 var options;
87 for (var contentType in httpLanguages) {
88 if (httpLanguages[contentType]) {
89 options = options || {};
90
91 var pattern = suffixTypes[contentType] ? getSuffixPattern(contentType) : contentType;
92 options[contentType.replace(/\//g, '-')] = {
93 pattern: RegExp('(content-type:\\s*' + pattern + '(?:(?:\\r\\n?|\\n).+)*)(?:\\r?\\n|\\r){2}[\\s\\S]*', 'i'),
94 lookbehind: true,
95 inside: httpLanguages[contentType]
96 };
97 }
98 }
99 if (options) {
100 Prism.languages.insertBefore('http', 'header-name', options);
101 }
102
103}(Prism));