Go's regular expressions don't allow you to create a capturing group
named "*", which previously made using SubRouter with regular expression
patterns impossible. This change introduces the alternate key "_", which
happens to be a legal capturing group name.
Fixes#98.
In particular, these started failing when running tests under the race
detector in Go 1.4 [0], probably due to some kind of (GC?) hijinks
clearing out the sync.Pool.
[0]: these tests might have also failed in 1.3, but I didn't check
"util" is a really bad name for a package since it isn't very
descriptive and so often collides with other names.
Unfortunately, this is a breaking change, but it's both very easy to fix
and perhaps more importantly also better to do now than later.
Previously, a route like "/:a.png" would match "/foo/bar/baz.png".
This was incorrect, as all matches should be restricted to path segments
(or smaller, as dictated by a break character).
This bug was introduced in 1a390aba1c.
Fixes#75.
This middleware makes it much easier to write sub-routes, allowing you
to make the sub-router ignorant of its parent router's matched prefix.
The example app has also been modified to use this functionality for its
admin pages.
Fixes#65.
This is a breaking API change that changes how wildcard patterns are
treated. In particular, wildcards are no longer allowed to appear at
arbitrary places in the URL, and are only allowed to appear immediately
after a path separator. This change effectively changes the wildcard
sigil from "*" to "/*".
Users who use wildcard routes like "/hello*" will have to switch to
regular expression based routes to preserve the old semantics.
The motivation for this change is that it allows the router to publish a
special "tail" key which represents the unmatched portion of the URL.
This is placed into URLParams under the key "*", and includes a leading
"/" to make it easier to write sub-routers.
This allows you to match "/a/cat.gif" with patterns like "/a/:b.:c".
Thanks to @Minecrell for an early patch implementing this functionality.
Fixes#75.
Fixes#48.
Make the bytecode runner return the route that we're going to use. It's
up to the router itself to dispatch to that route.
Besides feeling a teensy bit cleaner, this refactoring is to prepare for
a "Router" middleware, which will allow application developers to
control when in the middleware stack routing occurs.
Instead of using struct embedding to build web.Mux, start moving towards
explicit mappings. This doesn't actually change the public API of
web.Mux, but feels a little cleaner to me.
The longer-term thing here is to get rid of the functions defined on
Muxes in the public documentation that are defined on "rt *Mux", which
is just plain ugly.
It turns out WriterProxy is pretty generally useful, especially when
defining custom http loggers. Expose it in a util package so that other
packages can use it.
If you're manipulating your middleware stack concurrently with active
requests you're probably doing something wrong, and it's not worth
either the complexity or runtime cost to support you hitting yourself.
We can probably take this principle a bit further and disallow mutating
the middleware stack after any requests have been made (which will
eliminate even more complexity) but that can be a project for another
day.
App Engine disallows package unsafe. As a workaround for the (unsafe)
RCU atomic pointer shenanigans we pull in order to avoid taking a lock
in the hot routing path, let's just grab the lock. Honestly, I doubt
anyone will notice anyways, especially considering the fact that App
Engine is single-threaded anyways.
Fixes#52.
Previously, a state machine invalidation could have raced against an
in-flight routing attempt: if the invalidation occured after the routing
attempt had already completed its nil-check (choosing not to compile a
new state machine) but before the state machine was atomically loaded to
perform routing, the routing goroutine would begin to panic from
dereferencing nil.
The meat of this change is that we now return the state machine that we
compiled (while still holding the lock), and we only ever interact with
the state machine through atomic pointer loads.
Many common panic values, e.g. nil pointer dereferences, don't print
very well under "%#v", emitting something like
"runtime.errorCString{cstr:0x54b2a4}" or similar.
If WriteHeader is called multiple times on a http.ResponseWriter, the
first status is the one that is used, not the last. Fix the wrapped
writer to reflect this fact.
For whatever reason, Go insisted on loading rm.sm[i] in several chunks,
even though it could be loaded in a single 64-bit block. Instead, let's
reorder our loads to minimize the amount of memory we're uselessly
moving around.
This gives us about a 15% perf boost in
github.com/julienschmidt/go-http-routing-benchmark's
BenchmarkGoji_StaticAll, and questionable benefits (i.e., not
distinguishable from noise but certainly no worse) on Goji's own
benchmarks.
Change the per-process nonce part of the request ID from 8 characters to
10, and wrap the entire thing in a retry loop so you can never get an
"unlucky" panic. I know this will "never" happen in practice, but it
doesn't hurt to make sure we never, ever have any collisions, and never,
ever have any runtime panics.
It's also worth documenting the math ("math") I used to calculate the
numbers here.
Previously, we would keep the URLParams / Env associated with a cStack
around until the next request flushed them. However, this might cause
either of these maps to stick around for much longer than they ought to,
potentially keeping references to many, many objects.
Instead, clear out the saved context on every release.