Skip to content

Commit

Permalink
Merge pull request #72 from nlnwa/domain-search
Browse files Browse the repository at this point in the history
Only prefix https if match type is exact
  • Loading branch information
maeb authored Feb 1, 2023
2 parents 7558435 + 54aeb6a commit c3d1f6b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 12 deletions.
29 changes: 17 additions & 12 deletions server/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,28 @@ func Parse(r *http.Request) (*CoreAPI, error) {
// currently the "cdx" does not accept collection as a query or param
coreApi.Collection = "all"

matchType := query.Get("matchType")
if matchType != "" {
if !contains(matchTypes, matchType) {
return nil, fmt.Errorf("matchType must be one of %v, was: %s", matchTypes, matchType)
}
coreApi.MatchType = matchType
} else {
// Default to exact
coreApi.MatchType = MatchTypeExact
}

urls := query["url"]
if len(urls) == 1 && !schemeRegExp.MatchString(urls[0]) {
u := urls[0]
// Add http scheme
urls = []string{
"http://" + u,
"https://" + u,
}
// Add https scheme only for exact match to get results for both http/https
// If match type is prefix, domain or host the scheme part will be stripped so no need.
if coreApi.MatchType == MatchTypeExact {
urls = append(urls, "https://"+u)
}
}
for _, urlStr := range urls {
Expand All @@ -160,17 +176,6 @@ func Parse(r *http.Request) (*CoreAPI, error) {
return nil, err
}

matchType := query.Get("matchType")
if matchType != "" {
if !contains(matchTypes, matchType) {
return nil, fmt.Errorf("matchType must be one of %v, was: %s", matchTypes, matchType)
}
coreApi.MatchType = matchType
} else {
// Default to exact
coreApi.MatchType = MatchTypeExact
}

limit := query.Get("limit")
if limit != "" {
l, err := strconv.Atoi(limit)
Expand Down
39 changes: 39 additions & 0 deletions server/api/api_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package api

import (
"net/http"
"net/url"
"testing"

"github.com/nlnwa/gowarcserver/surt"
)

func TestParse(t *testing.T) {
domains := []string{
"no",
"kommune.no",
"nb.no",
}

for _, domain := range domains {
u, _ := url.Parse("http://example.test/")
values := u.Query()
values.Set("url", domain)
values.Set("matchType", "domain")
u.RawQuery = values.Encode()

r := &http.Request{URL: u}

a, err := Parse(r)
if err != nil {
t.Error(err)
}

got := SearchAPI{a}.Key()
want := MatchType(surt.UrlToSsurt(a.Urls[0]), MatchTypeDomain)
if got != want {
t.Errorf("Got: '%s', Want: '%s'", got, want)
}
}

}

0 comments on commit c3d1f6b

Please sign in to comment.