source: node_modules/refractor/lang/http.js

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

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