package memoriatest import ( "html" "net/url" "regexp" ) // sentLocation reports whether location is the door's Sent as rastrillo // answers it with the smart door's SigninScreen on: /signin?sent=1 with an // attempt= naming this browser's attempt, and nothing else but a // return_to. Begin answers an owner so, and beginGuard answers a stranger // with rastrillo's AnswerAsSent, which is the same answer without the mail. func sentLocation(location string) bool { u, err := url.Parse(location) if err != nil || u.Path != "/signin" || u.Host != "" { return false } q := u.Query() if q.Get("sent") != "1" || len(q["attempt"]) != 1 || q.Get("attempt") == "" { return false } for k := range q { if k != "sent" && k != "attempt" && k != "return_to" { return false } } return true } // sentShape is location with its attempt id masked: two answers alike in // every byte but the attempt's own random id have the same shape. func sentShape(location string) string { u, err := url.Parse(location) if err != nil { return location } q := u.Query() if q.Has("attempt") { q.Set("attempt", "…") } u.RawQuery = q.Encode() return u.String() } // doorForm is the hidden fields of the first door form on page posting to // action, as a browser would submit them, or nil when there is none. func doorForm(page, action string) url.Values { m := regexp.MustCompile(`(?s)
`).FindStringSubmatch(page) if m == nil { return nil } out := url.Values{} for _, in := range regexp.MustCompile(``).FindAllStringSubmatch(m[1], -1) { out.Add(in[1], html.UnescapeString(in[2])) } return out }