Skip to content

Commit

Permalink
Bug fix: Add mutex locking when checking patterns in route handler
Browse files Browse the repository at this point in the history
  • Loading branch information
thisisaaronland committed Mar 15, 2024
1 parent b37cc61 commit 83ee7c9
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions handler/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ var re_braces = regexp.MustCompile(`\{([^\}]+)\}`)
// not as robust as it could be.
var re_route = regexp.MustCompile(`^(?:(?:(GET|POST|PUT|HEAD|OPTION|DELETE)\s)?([^\/]+)?)?(.*)$`)

var re_mutex = new(sync.RWMutex)

// Regular expression to replace "{label}" style strings with "([^\/]+)".
func not_a_slash(s string) string {
return fmt.Sprintf(`([^\/]+)`)
Expand Down Expand Up @@ -210,6 +212,8 @@ func deriveHandler(req *http.Request, handlers map[string]RouteHandlerFunc, matc
// If there are replace them with a match-up-to-next-forward-slash capture
// and then use the result to build a new regular expression

re_mutex.Lock()

re_wildcard, exists := wildcard_matches[route_path]

if !exists {
Expand All @@ -218,13 +222,16 @@ func deriveHandler(req *http.Request, handlers map[string]RouteHandlerFunc, matc
re, err := regexp.Compile(str_wildcard)

if err != nil {
re_mutex.Unlock()
return nil, fmt.Errorf("Failed to compile wildcard regexp, %w", err)
}

re_wildcard = re
wildcard_matches[route_path] = re_wildcard
}

re_mutex.Unlock()

// Does the current path (like the actual request being processed) match the wildcard?

path_m := re_wildcard.FindStringSubmatch(path)
Expand Down

0 comments on commit 83ee7c9

Please sign in to comment.