accesslog.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. Language: Apache Access Log
  3. Author: Oleg Efimov <efimovov@gmail.com>
  4. Description: Apache/Nginx Access Logs
  5. Website: https://httpd.apache.org/docs/2.4/logs.html#accesslog
  6. Category: web, logs
  7. Audit: 2020
  8. */
  9. /** @type LanguageFn */
  10. function accesslog(hljs) {
  11. const regex = hljs.regex;
  12. // https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods
  13. const HTTP_VERBS = [
  14. "GET",
  15. "POST",
  16. "HEAD",
  17. "PUT",
  18. "DELETE",
  19. "CONNECT",
  20. "OPTIONS",
  21. "PATCH",
  22. "TRACE"
  23. ];
  24. return {
  25. name: 'Apache Access Log',
  26. contains: [
  27. // IP
  28. {
  29. className: 'number',
  30. begin: /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(:\d{1,5})?\b/,
  31. relevance: 5
  32. },
  33. // Other numbers
  34. {
  35. className: 'number',
  36. begin: /\b\d+\b/,
  37. relevance: 0
  38. },
  39. // Requests
  40. {
  41. className: 'string',
  42. begin: regex.concat(/"/, regex.either(...HTTP_VERBS)),
  43. end: /"/,
  44. keywords: HTTP_VERBS,
  45. illegal: /\n/,
  46. relevance: 5,
  47. contains: [
  48. {
  49. begin: /HTTP\/[12]\.\d'/,
  50. relevance: 5
  51. }
  52. ]
  53. },
  54. // Dates
  55. {
  56. className: 'string',
  57. // dates must have a certain length, this prevents matching
  58. // simple array accesses a[123] and [] and other common patterns
  59. // found in other languages
  60. begin: /\[\d[^\]\n]{8,}\]/,
  61. illegal: /\n/,
  62. relevance: 1
  63. },
  64. {
  65. className: 'string',
  66. begin: /\[/,
  67. end: /\]/,
  68. illegal: /\n/,
  69. relevance: 0
  70. },
  71. // User agent / relevance boost
  72. {
  73. className: 'string',
  74. begin: /"Mozilla\/\d\.\d \(/,
  75. end: /"/,
  76. illegal: /\n/,
  77. relevance: 3
  78. },
  79. // Strings
  80. {
  81. className: 'string',
  82. begin: /"/,
  83. end: /"/,
  84. illegal: /\n/,
  85. relevance: 0
  86. }
  87. ]
  88. };
  89. }
  90. export { accesslog as default };