Extract useragent package

Fixes #30
This commit is contained in:
Martin Polden
2017-05-27 15:31:50 +02:00
parent 6fa3c34c19
commit 0cd84ada1e
4 changed files with 82 additions and 6 deletions

View File

@ -6,13 +6,13 @@ import (
"html/template"
"io"
"github.com/mpolden/ipd/useragent"
"github.com/sirupsen/logrus"
"math/big"
"net"
"net/http"
"path/filepath"
"regexp"
"strconv"
"strings"
@ -24,10 +24,6 @@ const (
textMediaType = "text/plain"
)
var userAgentPattern = regexp.MustCompile(
`^(?:curl|Wget|fetch\slibfetch|ddclient|Go-http-client|HTTPie)\/.*|Go\s1\.1\spackage\shttp$`,
)
type API struct {
Template string
IPHeader string
@ -210,7 +206,12 @@ func (a *API) NotFoundHandler(w http.ResponseWriter, r *http.Request) *appError
}
func cliMatcher(r *http.Request, rm *mux.RouteMatch) bool {
return userAgentPattern.MatchString(r.UserAgent())
ua := useragent.Parse(r.UserAgent())
switch ua.Product {
case "curl", "HTTPie", "Wget", "fetch libfetch", "Go", "Go-http-client", "ddclient":
return true
}
return false
}
type appHandler func(http.ResponseWriter, *http.Request) *appError

View File

@ -150,6 +150,7 @@ func TestCLIMatcher(t *testing.T) {
}{
{"curl/7.26.0", true},
{"Wget/1.13.4 (linux-gnu)", true},
{"Wget", true},
{"fetch libfetch/2.0", true},
{"HTTPie/0.9.3", true},
{"Go 1.1 package http", true},