-
Notifications
You must be signed in to change notification settings - Fork 3
/
html.go
58 lines (47 loc) · 1.03 KB
/
html.go
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package testutil
import (
"errors"
"io"
"golang.org/x/net/html"
"golang.org/x/net/html/atom"
)
func findInputs(parent *html.Node, inputs map[string]string) {
for node := parent.FirstChild; node != nil; node = node.NextSibling {
if node.Type == html.ElementNode {
if node.DataAtom == atom.Input {
name := ""
value := ""
for _, attr := range node.Attr {
switch attr.Key {
case "name":
name = attr.Val
case "value":
value = attr.Val
}
}
inputs[name] = value
}
findInputs(node, inputs)
}
}
}
func FindInputsByHTML(body io.Reader) (map[string]string, error) {
nodes, err := html.Parse(body)
if err != nil {
return nil, err
}
inputs := make(map[string]string)
findInputs(nodes, inputs)
return inputs, nil
}
func FindRequestObjectByHTML(body io.Reader) (string, error) {
inputs, err := FindInputsByHTML(body)
if err != nil {
return "", err
}
if req := inputs["request"]; req == "" {
return "", errors.New("request is not in the form")
} else {
return req, nil
}
}