regexp.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2020 Alexey Edelev <semlanik@gmail.com>
  5. *
  6. * This file is part of gostfix project https://git.semlanik.org/semlanik/gostfix
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy of this
  9. * software and associated documentation files (the "Software"), to deal in the Software
  10. * without restriction, including without limitation the rights to use, copy, modify,
  11. * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
  12. * to permit persons to whom the Software is furnished to do so, subject to the following
  13. * conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all copies
  16. * or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  19. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  20. * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  21. * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  22. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. package utils
  26. import (
  27. "log"
  28. "regexp"
  29. "sync"
  30. )
  31. const (
  32. HeaderRegExp = "^([\x21-\x7E^:]+):(.*)"
  33. FoldingRegExp = "^\\s+(.*)"
  34. BoundaryStartRegExp = "^--(.*)"
  35. BoundaryEndRegExp = "^--(.*)--$"
  36. BoundaryRegExp = "boundary=\"(.*)\""
  37. )
  38. const (
  39. UserRegExp = "^[a-zA-Z][\\w0-9\\._]*"
  40. )
  41. type RegExpUtils regExpUtils
  42. var (
  43. once sync.Once
  44. instance *regExpUtils
  45. )
  46. func RegExpUtilsInstance() *RegExpUtils {
  47. once.Do(func() {
  48. instance, _ = newRegExpUtils()
  49. })
  50. return (*RegExpUtils)(instance)
  51. }
  52. type regExpUtils struct {
  53. UserChecker *regexp.Regexp
  54. HeaderFinder *regexp.Regexp
  55. FoldingFinder *regexp.Regexp
  56. BoundaryStartFinder *regexp.Regexp
  57. BoundaryEndFinder *regexp.Regexp
  58. BoundaryFinder *regexp.Regexp
  59. }
  60. func newRegExpUtils() (*regExpUtils, error) {
  61. headerFinder, err := regexp.Compile(HeaderRegExp)
  62. if err != nil {
  63. log.Fatalf("Invalid regexp %s\n", err)
  64. return nil, err
  65. }
  66. foldingFinder, err := regexp.Compile(FoldingRegExp)
  67. if err != nil {
  68. log.Fatalf("Invalid regexp %s\n", err)
  69. return nil, err
  70. }
  71. boundaryStartFinder, err := regexp.Compile(BoundaryStartRegExp)
  72. if err != nil {
  73. log.Fatalf("Invalid regexp %s\n", err)
  74. return nil, err
  75. }
  76. boundaryEndFinder, err := regexp.Compile(BoundaryEndRegExp)
  77. if err != nil {
  78. log.Fatalf("Invalid regexp %s\n", err)
  79. return nil, err
  80. }
  81. boundaryFinder, err := regexp.Compile(BoundaryRegExp)
  82. if err != nil {
  83. log.Fatalf("Invalid regexp %s\n", err)
  84. return nil, err
  85. }
  86. userChecker, err := regexp.Compile(UserRegExp)
  87. if err != nil {
  88. log.Fatalf("Invalid regexp %s\n", err)
  89. return nil, err
  90. }
  91. ru := &regExpUtils{
  92. UserChecker: userChecker,
  93. HeaderFinder: headerFinder,
  94. FoldingFinder: foldingFinder,
  95. BoundaryStartFinder: boundaryStartFinder,
  96. BoundaryEndFinder: boundaryEndFinder,
  97. BoundaryFinder: boundaryFinder,
  98. }
  99. return ru, nil
  100. }