http.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. * @param {string} value
  3. * @returns {RegExp}
  4. * */
  5. /**
  6. * @param {RegExp | string } re
  7. * @returns {string}
  8. */
  9. function source(re) {
  10. if (!re) return null;
  11. if (typeof re === "string") return re;
  12. return re.source;
  13. }
  14. /**
  15. * @param {...(RegExp | string) } args
  16. * @returns {string}
  17. */
  18. function concat(...args) {
  19. const joined = args.map((x) => source(x)).join("");
  20. return joined;
  21. }
  22. /*
  23. Language: HTTP
  24. Description: HTTP request and response headers with automatic body highlighting
  25. Author: Ivan Sagalaev <maniac@softwaremaniacs.org>
  26. Category: common, protocols
  27. Website: https://developer.mozilla.org/en-US/docs/Web/HTTP/Overview
  28. */
  29. function http(hljs) {
  30. const VERSION = 'HTTP/(2|1\\.[01])';
  31. const HEADER_NAME = /[A-Za-z][A-Za-z0-9-]*/;
  32. const HEADER = {
  33. className: 'attribute',
  34. begin: concat('^', HEADER_NAME, '(?=\\:\\s)'),
  35. starts: {
  36. contains: [
  37. {
  38. className: "punctuation",
  39. begin: /: /,
  40. relevance: 0,
  41. starts: {
  42. end: '$',
  43. relevance: 0
  44. }
  45. }
  46. ]
  47. }
  48. };
  49. const HEADERS_AND_BODY = [
  50. HEADER,
  51. {
  52. begin: '\\n\\n',
  53. starts: { subLanguage: [], endsWithParent: true }
  54. }
  55. ];
  56. return {
  57. name: 'HTTP',
  58. aliases: ['https'],
  59. illegal: /\S/,
  60. contains: [
  61. // response
  62. {
  63. begin: '^(?=' + VERSION + " \\d{3})",
  64. end: /$/,
  65. contains: [
  66. {
  67. className: "meta",
  68. begin: VERSION
  69. },
  70. {
  71. className: 'number', begin: '\\b\\d{3}\\b'
  72. }
  73. ],
  74. starts: {
  75. end: /\b\B/,
  76. illegal: /\S/,
  77. contains: HEADERS_AND_BODY
  78. }
  79. },
  80. // request
  81. {
  82. begin: '(?=^[A-Z]+ (.*?) ' + VERSION + '$)',
  83. end: /$/,
  84. contains: [
  85. {
  86. className: 'string',
  87. begin: ' ',
  88. end: ' ',
  89. excludeBegin: true,
  90. excludeEnd: true
  91. },
  92. {
  93. className: "meta",
  94. begin: VERSION
  95. },
  96. {
  97. className: 'keyword',
  98. begin: '[A-Z]+'
  99. }
  100. ],
  101. starts: {
  102. end: /\b\B/,
  103. illegal: /\S/,
  104. contains: HEADERS_AND_BODY
  105. }
  106. },
  107. // to allow headers to work even without a preamble
  108. hljs.inherit(HEADER, {
  109. relevance: 0
  110. })
  111. ]
  112. };
  113. }
  114. module.exports = http;