accesslog.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. * Any of the passed expresssions may match
  24. *
  25. * Creates a huge this | this | that | that match
  26. * @param {(RegExp | string)[] } args
  27. * @returns {string}
  28. */
  29. function either(...args) {
  30. const joined = '(' + args.map((x) => source(x)).join("|") + ")";
  31. return joined;
  32. }
  33. /*
  34. Language: Apache Access Log
  35. Author: Oleg Efimov <efimovov@gmail.com>
  36. Description: Apache/Nginx Access Logs
  37. Website: https://httpd.apache.org/docs/2.4/logs.html#accesslog
  38. Audit: 2020
  39. */
  40. /** @type LanguageFn */
  41. function accesslog(_hljs) {
  42. // https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods
  43. const HTTP_VERBS = [
  44. "GET",
  45. "POST",
  46. "HEAD",
  47. "PUT",
  48. "DELETE",
  49. "CONNECT",
  50. "OPTIONS",
  51. "PATCH",
  52. "TRACE"
  53. ];
  54. return {
  55. name: 'Apache Access Log',
  56. contains: [
  57. // IP
  58. {
  59. className: 'number',
  60. begin: /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(:\d{1,5})?\b/,
  61. relevance: 5
  62. },
  63. // Other numbers
  64. {
  65. className: 'number',
  66. begin: /\b\d+\b/,
  67. relevance: 0
  68. },
  69. // Requests
  70. {
  71. className: 'string',
  72. begin: concat(/"/, either(...HTTP_VERBS)),
  73. end: /"/,
  74. keywords: HTTP_VERBS,
  75. illegal: /\n/,
  76. relevance: 5,
  77. contains: [
  78. {
  79. begin: /HTTP\/[12]\.\d'/,
  80. relevance: 5
  81. }
  82. ]
  83. },
  84. // Dates
  85. {
  86. className: 'string',
  87. // dates must have a certain length, this prevents matching
  88. // simple array accesses a[123] and [] and other common patterns
  89. // found in other languages
  90. begin: /\[\d[^\]\n]{8,}\]/,
  91. illegal: /\n/,
  92. relevance: 1
  93. },
  94. {
  95. className: 'string',
  96. begin: /\[/,
  97. end: /\]/,
  98. illegal: /\n/,
  99. relevance: 0
  100. },
  101. // User agent / relevance boost
  102. {
  103. className: 'string',
  104. begin: /"Mozilla\/\d\.\d \(/,
  105. end: /"/,
  106. illegal: /\n/,
  107. relevance: 3
  108. },
  109. // Strings
  110. {
  111. className: 'string',
  112. begin: /"/,
  113. end: /"/,
  114. illegal: /\n/,
  115. relevance: 0
  116. }
  117. ]
  118. };
  119. }
  120. module.exports = accesslog;