-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparse_lookup_req.go
More file actions
43 lines (34 loc) · 1008 Bytes
/
parse_lookup_req.go
File metadata and controls
43 lines (34 loc) · 1008 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package chef
import (
"net/http"
"net/url"
"github.com/sauerbraten/chef/db"
"github.com/sauerbraten/chef/ips"
)
func parseLookupRequest(resp http.ResponseWriter, req *http.Request) (
nameOrIP string,
sorting db.Sorting,
last90DaysOnly, directLookupForced bool,
redirected bool,
) {
nameOrIP = req.FormValue("q")
sorting = db.ByNameFrequency
if req.FormValue("sorting") == db.ByLastSeen.Identifier {
sorting = db.ByLastSeen
}
last90DaysOnly = !(req.FormValue("search_old") == "true")
directLookupForced = req.FormValue("direct") == "true"
// (permanently) redirect partial IP queries
if ips.IsPartialOrFullCIDR(nameOrIP) {
subnet := ips.GetSubnet(nameOrIP)
if nameOrIP != subnet.String() {
u, _ := url.ParseRequestURI(req.RequestURI) // it's safe to assume this will not fail
params := u.Query()
params.Set("q", subnet.String())
u.RawQuery = params.Encode()
http.Redirect(resp, req, u.String(), http.StatusPermanentRedirect)
redirected = true
}
}
return
}