@@ -103,6 +103,15 @@ type Service struct {
103103 // /package_name.ServiceName/MethodName
104104 AuthWhitelistPaths []string `long:"authwhitelistpaths" description:"List of regular expressions for paths that don't require authentication'"`
105105
106+ // compiledHostRegexp is the compiled host regex.
107+ compiledHostRegexp * regexp.Regexp
108+
109+ // compiledPathRegexp is the compiled path regex.
110+ compiledPathRegexp * regexp.Regexp
111+
112+ // compiledAuthWhitelistPaths is the compiled auth whitelist paths.
113+ compiledAuthWhitelistPaths []* regexp.Regexp
114+
106115 freebieDB freebie.DB
107116 pricer pricer.Pricer
108117}
@@ -123,8 +132,7 @@ func (s *Service) ResourceName(resourcePath string) string {
123132// AuthRequired determines the auth level required for a given request.
124133func (s * Service ) AuthRequired (r * http.Request ) auth.Level {
125134 // Does the request match any whitelist entry?
126- for _ , pathRegexp := range s .AuthWhitelistPaths {
127- pathRegexp := regexp .MustCompile (pathRegexp )
135+ for _ , pathRegexp := range s .compiledAuthWhitelistPaths {
128136 if pathRegexp .MatchString (r .URL .Path ) {
129137 log .Tracef ("Req path [%s] matches whitelist entry " +
130138 "[%s]." , r .URL .Path , pathRegexp )
@@ -184,15 +192,41 @@ func prepareServices(services []*Service) error {
184192 }
185193 }
186194
195+ // Compile the host regex.
196+ compiledHostRegexp , err := regexp .Compile (service .HostRegexp )
197+ if err != nil {
198+ return fmt .Errorf ("error compiling host regex: %v" , err )
199+ }
200+ service .compiledHostRegexp = compiledHostRegexp
201+
202+ // Compile the path regex.
203+ if service .PathRegexp != "" {
204+ compiledPathRegexp , err := regexp .Compile (
205+ service .PathRegexp ,
206+ )
207+ if err != nil {
208+ return fmt .Errorf ("error compiling path " +
209+ "regex: %v" , err )
210+ }
211+ service .compiledPathRegexp = compiledPathRegexp
212+ }
213+
214+ service .compiledAuthWhitelistPaths = make (
215+ []* regexp.Regexp , 0 , len (service .AuthWhitelistPaths ),
216+ )
217+
187218 // Make sure all whitelist regular expression entries actually
188219 // compile so we run into an eventual panic during startup and
189220 // not only when the request happens.
190221 for _ , entry := range service .AuthWhitelistPaths {
191- _ , err := regexp .Compile (entry )
222+ regExp , err := regexp .Compile (entry )
192223 if err != nil {
193224 return fmt .Errorf ("error validating auth " +
194225 "whitelist: %v" , err )
195226 }
227+ service .compiledAuthWhitelistPaths = append (
228+ service .compiledAuthWhitelistPaths , regExp ,
229+ )
196230 }
197231
198232 // If dynamic prices are enabled then use the provided
0 commit comments