Skip to content

Commit a4d7a02

Browse files
authored
Merge pull request lightninglabs#177 from sputn1ck/precompile_regexes
proxy: precompile regexp
2 parents 4e5a50a + 1557c4a commit a4d7a02

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

proxy/proxy.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"net/http"
99
"net/http/httputil"
1010
"os"
11-
"regexp"
1211
"strconv"
1312
"strings"
1413

@@ -366,21 +365,21 @@ func certPool(services []*Service) (*x509.CertPool, error) {
366365
// expression matching the host and path.
367366
func matchService(req *http.Request, services []*Service) (*Service, bool) {
368367
for _, service := range services {
369-
hostRegexp := regexp.MustCompile(service.HostRegexp)
368+
hostRegexp := service.compiledHostRegexp
370369
if !hostRegexp.MatchString(req.Host) {
371370
log.Tracef("Req host [%s] doesn't match [%s].",
372371
req.Host, hostRegexp)
373372
continue
374373
}
375374

376-
if service.PathRegexp == "" {
375+
if service.compiledPathRegexp == nil {
377376
log.Debugf("Host [%s] matched pattern [%s] and path "+
378377
"expression is empty. Using service [%s].",
379378
req.Host, hostRegexp, service.Address)
380379
return service, true
381380
}
382381

383-
pathRegexp := regexp.MustCompile(service.PathRegexp)
382+
pathRegexp := service.compiledPathRegexp
384383
if !pathRegexp.MatchString(req.URL.Path) {
385384
log.Tracef("Req path [%s] doesn't match [%s].",
386385
req.URL.Path, pathRegexp)

proxy/service.go

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
124133
func (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

Comments
 (0)