Skip to content

Support route pattern quantity match (i.e. curly braces in regex expression) #230

@tapocol

Description

@tapocol

Attempting to use routes that specify the quantity to match in regex does not work. This looks to be related to the way finding curly braces (i.e. { and }) works in pattern segments:

chi/tree.go

Line 628 in 49e7138

ps := strings.Index(pattern, "{")

Here are two routes that fail in different ways from my expectations:

package main

import (
	"fmt"
	"net/http"

	"github.com/go-chi/chi"
)

func handler(w http.ResponseWriter, req *http.Request) {
	w.WriteHeader(200)
	w.Write([]byte("HERE"))
}

func main() {
	r := chi.NewRouter()
	r.MethodFunc("GET", "/{first:[a-z]{4}}", handler)          // Does not panic at runtime, however I except it to match only requests with four a-z characters.
	r.MethodFunc("GET", "/{first:[a-z]{4}}/{second}", handler) // Panics at runtime, slice bounds out of range. I expect it to match only requests where the first URI segment has four a-z characters and some value in the second segment. Comment out to test the first defined route.
	fmt.Println("Listening...")
	http.ListenAndServe(":3000", r)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions