UNPKG

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