apache.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package axslogparser
  2. import (
  3. "bytes"
  4. "fmt"
  5. "regexp"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "github.com/pkg/errors"
  10. )
  11. // Apache log parser
  12. type Apache struct {
  13. }
  14. var logRe = regexp.MustCompile(
  15. `^(?:(\S+)\s)?` + // %v(The canonical ServerName/virtual host)
  16. `(\S+)\s` + // %h(Remote Hostname) $remote_addr
  17. `(\S+)\s` + // %l(Remote Logname)
  18. `(\S+)\s` + // $remote_user
  19. `\[(\d{2}/\w{3}/\d{2}(?:\d{2}:){3}\d{2} [-+]\d{4})\]\s` + // $time_local
  20. `(.*)`)
  21. // Parse for Parser interface
  22. func (ap *Apache) Parse(line string) (*Log, error) {
  23. matches := logRe.FindStringSubmatch(line)
  24. if len(matches) < 1 {
  25. return nil, fmt.Errorf("failed to parse apachelog (not matched): %s", line)
  26. }
  27. l := &Log{
  28. VirtualHost: matches[1],
  29. Host: matches[2],
  30. RemoteUser: matches[3],
  31. User: matches[4],
  32. }
  33. l.Time, _ = time.Parse(clfTimeLayout, matches[5])
  34. var rest string
  35. l.Request, rest = takeQuoted(matches[6])
  36. if err := l.breakdownRequest(); err != nil {
  37. return nil, errors.Wrapf(err, "failed to parse apachelog (invalid request): %s", line)
  38. }
  39. matches = strings.Fields(rest)
  40. if len(matches) < 2 {
  41. return nil, fmt.Errorf("failed to parse apachelog (invalid status or size): %s", line)
  42. }
  43. l.Status, _ = strconv.Atoi(matches[0])
  44. if l.Status < 100 || 600 <= l.Status {
  45. return nil, fmt.Errorf("failed to parse apachelog (invalid status: %s): %s", matches[0], line)
  46. }
  47. l.Size, _ = strconv.ParseUint(matches[1], 10, 64)
  48. l.Referer, rest = takeQuoted(rest)
  49. l.UserAgent, _ = takeQuoted(rest)
  50. return l, nil
  51. }
  52. func takeQuoted(line string) (string, string) {
  53. if line == "" {
  54. return "", ""
  55. }
  56. i := 0
  57. for ; i < len(line); i++ {
  58. if line[i] == '"' {
  59. i++
  60. break
  61. }
  62. }
  63. if i == len(line) {
  64. return "", ""
  65. }
  66. buf := &bytes.Buffer{}
  67. escaped := false
  68. for ; i < len(line); i++ {
  69. c := line[i]
  70. if !escaped {
  71. if c == '"' {
  72. break
  73. }
  74. if c == '\\' {
  75. escaped = true
  76. continue
  77. }
  78. buf.WriteByte(c)
  79. continue
  80. }
  81. escaped = false
  82. switch c {
  83. case 'n':
  84. buf.WriteByte('\n')
  85. case 't':
  86. buf.WriteByte('\t')
  87. case '\\':
  88. buf.WriteByte('\\')
  89. case '"':
  90. buf.WriteByte('"')
  91. default:
  92. buf.WriteByte(c)
  93. }
  94. }
  95. return buf.String(), line[i+1:]
  96. }