From e0b50f3ffac1ff8c6337209b10ca663eab29e3ea Mon Sep 17 00:00:00 2001 From: elotreum <59893399+elotreum@users.noreply.github.com> Date: Sun, 19 Jan 2020 20:33:21 -0700 Subject: [PATCH 1/2] add helper method to retrieve RouteParam by name --- bittorrent/params.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/bittorrent/params.go b/bittorrent/params.go index 2d1baaf..5429894 100644 --- a/bittorrent/params.go +++ b/bittorrent/params.go @@ -70,6 +70,17 @@ type RouteParam struct { // RouteParams is a collection of RouteParam instances. type RouteParams []RouteParam +// ByName returns the value of the first RouteParam that matches the given +// name. If no matching RouteParam is found, an empty string is returned. +func (rp RouteParams) ByName(name string) string { + for _, p := range rp { + if p.Key == name { + return p.Value + } + } + return "" +} + // ParseURLData parses a request URL or UDP URLData as defined in BEP41. // It expects a concatenated string of the request's path and query parts as // defined in RFC 3986. As both the udp: and http: scheme used by BitTorrent From d70d300422f0de0fa4aaf08a2fd05c733be3a316 Mon Sep 17 00:00:00 2001 From: elotreum <59893399+elotreum@users.noreply.github.com> Date: Mon, 20 Jan 2020 11:31:45 -0700 Subject: [PATCH 2/2] add godoc explaining catch-all parameter matching for ByName --- bittorrent/params.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/bittorrent/params.go b/bittorrent/params.go index 5429894..b820108 100644 --- a/bittorrent/params.go +++ b/bittorrent/params.go @@ -72,6 +72,10 @@ type RouteParams []RouteParam // ByName returns the value of the first RouteParam that matches the given // name. If no matching RouteParam is found, an empty string is returned. +// In the event that a "catch-all" parameter is provided on the route and +// no value is matched, an empty string is returned. For example: a route of +// "/announce/*param" matches on "/announce/". However, ByName("param") will +// return an empty string. func (rp RouteParams) ByName(name string) string { for _, p := range rp { if p.Key == name {