| 1 | package memoriatest |
| 2 | |
| 3 | import ( |
| 4 | "html" |
| 5 | "net/url" |
| 6 | "regexp" |
| 7 | ) |
| 8 | |
| 9 | // sentLocation reports whether location is the door's Sent as rastrillo |
| 10 | // answers it with the smart door's SigninScreen on: /signin?sent=1 with an |
| 11 | // attempt= naming this browser's attempt, and nothing else but a |
| 12 | // return_to. Begin answers an owner so, and beginGuard answers a stranger |
| 13 | // with rastrillo's AnswerAsSent, which is the same answer without the mail. |
| 14 | func sentLocation(location string) bool { |
| 15 | u, err := url.Parse(location) |
| 16 | if err != nil || u.Path != "/signin" || u.Host != "" { |
| 17 | return false |
| 18 | } |
| 19 | q := u.Query() |
| 20 | if q.Get("sent") != "1" || len(q["attempt"]) != 1 || q.Get("attempt") == "" { |
| 21 | return false |
| 22 | } |
| 23 | for k := range q { |
| 24 | if k != "sent" && k != "attempt" && k != "return_to" { |
| 25 | return false |
| 26 | } |
| 27 | } |
| 28 | return true |
| 29 | } |
| 30 | |
| 31 | // sentShape is location with its attempt id masked: two answers alike in |
| 32 | // every byte but the attempt's own random id have the same shape. |
| 33 | func sentShape(location string) string { |
| 34 | u, err := url.Parse(location) |
| 35 | if err != nil { |
| 36 | return location |
| 37 | } |
| 38 | q := u.Query() |
| 39 | if q.Has("attempt") { |
| 40 | q.Set("attempt", "…") |
| 41 | } |
| 42 | u.RawQuery = q.Encode() |
| 43 | return u.String() |
| 44 | } |
| 45 | |
| 46 | // doorForm is the hidden fields of the first door form on page posting to |
| 47 | // action, as a browser would submit them, or nil when there is none. |
| 48 | func doorForm(page, action string) url.Values { |
| 49 | m := regexp.MustCompile(`(?s)<form rst-signin-form method="post" action="` + regexp.QuoteMeta(action) + `">(.*?)</form>`).FindStringSubmatch(page) |
| 50 | if m == nil { |
| 51 | return nil |
| 52 | } |
| 53 | out := url.Values{} |
| 54 | for _, in := range regexp.MustCompile(`<input type="hidden" name="([^"]+)" value="([^"]*)">`).FindAllStringSubmatch(m[1], -1) { |
| 55 | out.Add(in[1], html.UnescapeString(in[2])) |
| 56 | } |
| 57 | return out |
| 58 | } |
| 59 | |