http.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 HEADERS_AND_BODY = [
  33. {
  34. className: 'attribute',
  35. begin: concat('^', HEADER_NAME, '(?=\\:\\s)'),
  36. starts: {
  37. contains: [
  38. {
  39. className: "punctuation",
  40. begin: /: /,
  41. relevance: 0,
  42. starts: {
  43. end: '$',
  44. relevance: 0
  45. }
  46. }
  47. ]
  48. }
  49. },
  50. {
  51. begin: '\\n\\n',
  52. starts: { subLanguage: [], endsWithParent: true }
  53. }
  54. ];
  55. return {
  56. name: 'HTTP',
  57. aliases: ['https'],
  58. illegal: /\S/,
  59. contains: [
  60. // response
  61. {
  62. begin: '^(?=' + VERSION + " \\d{3})",
  63. end: /$/,
  64. contains: [
  65. {
  66. className: "meta",
  67. begin: VERSION
  68. },
  69. {
  70. className: 'number', begin: '\\b\\d{3}\\b'
  71. }
  72. ],
  73. starts: {
  74. end: /\b\B/,
  75. illegal: /\S/,
  76. contains: HEADERS_AND_BODY
  77. }
  78. },
  79. // request
  80. {
  81. begin: '(?=^[A-Z]+ (.*?) ' + VERSION + '$)',
  82. end: /$/,
  83. contains: [
  84. {
  85. className: 'string',
  86. begin: ' ',
  87. end: ' ',
  88. excludeBegin: true,
  89. excludeEnd: true
  90. },
  91. {
  92. className: "meta",
  93. begin: VERSION
  94. },
  95. {
  96. className: 'keyword',
  97. begin: '[A-Z]+'
  98. }
  99. ],
  100. starts: {
  101. end: /\b\B/,
  102. illegal: /\S/,
  103. contains: HEADERS_AND_BODY
  104. }
  105. }
  106. ]
  107. };
  108. }
  109. module.exports = http;