/
form.go
394 lines (352 loc) · 9.58 KB
/
form.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
package scraper
import (
"bytes"
"fmt"
"golang.org/x/text/transform"
"net/http"
"net/url"
"sort"
"strconv"
"strings"
)
type FormElementNotFoundError struct {
Name string
}
func (error FormElementNotFoundError) Error() string {
return fmt.Sprintf("form element %v not found", error.Name)
}
// AvailableValue holds an available value and corresponding label to display.
type AvailableValue struct {
Value string
Label string
}
// FormElement holds a form element.
type FormElement struct {
Type string // "select", "hidden", "submit", "text", "email", "password", "button", "checkbox", "radio", "image"
Name string
Value *AvailableValue
AvailableValues []*AvailableValue
}
func (element *FormElement) AddAvailableValue(val *AvailableValue) {
if element.AvailableValues == nil {
element.AvailableValues = make([]*AvailableValue, 0)
}
element.AvailableValues = append(element.AvailableValues, val)
}
func (element *FormElement) GoString() string {
buf := &bytes.Buffer{}
_, _ = fmt.Fprintf(buf, "FormElement{\n")
_, _ = fmt.Fprintf(buf, " Type: %v\n", element.Type)
_, _ = fmt.Fprintf(buf, " Name: %v\n", element.Name)
_, _ = fmt.Fprintf(buf, " Value: %#v\n", element.Value)
if element.AvailableValues != nil {
_, _ = fmt.Fprintf(buf, " AvailableValues: [")
for _, a := range element.AvailableValues {
_, _ = fmt.Fprintf(buf, "%#v, ", a)
}
_, _ = fmt.Fprintf(buf, "]\n")
}
_, _ = fmt.Fprintf(buf, "}\n")
return buf.String()
}
// Form holds form data and submit information
type Form struct {
url *url.URL
baseUrl *url.URL
Action string
Method string
Elements map[string]*FormElement
Logger Logger
}
// Form generates a Form object from a form object identified by selector in the Page
func (page *Page) Form(selector string) (*Form, error) {
form := page.Find(selector)
numForm := form.Length()
if numForm != 1 {
return nil, fmt.Errorf("selector='%v', found %v items. something went wrong", selector, numForm)
}
elements := map[string]*FormElement{}
inputs := form.Find("input")
for i := 0; i < inputs.Length(); i++ {
s := inputs.Eq(i)
name, ok := s.Attr("name")
if !ok {
t, ok := s.Attr("type")
if !ok || t != "submit" {
page.Logger.Printf("an input element without name (%#V) found. ignore.", s)
}
continue
}
t, ok := s.Attr("type")
if !ok {
t = "text"
}
value, _ := s.Attr("value")
element, ok := elements[name]
if !ok {
element = &FormElement{
Type: t,
Name: name,
}
elements[name] = element
}
val := &AvailableValue{
Value: value,
}
if id, ok := s.Attr("id"); ok {
idEscaped := strings.Replace(id, ".", "\\002e", -1)
idEscaped = strings.Replace(idEscaped, ":", "\\003a", -1)
//session.Printf("id %v -> %v\n", id, idEscaped)
label := page.Find(fmt.Sprintf("label[for=%s]", idEscaped))
if label.Length() > 0 {
val.Label = label.Text()
}
}
switch strings.ToLower(t) {
case "submit", "hidden", "button", "text", "email", "password", "image":
element.Value = val
case "checkbox":
element.AvailableValues = []*AvailableValue{val}
if _, checked := s.Attr("checked"); checked {
element.Value = val
}
case "radio":
element.AddAvailableValue(val)
if _, ok := s.Attr("checked"); ok {
element.Value = val
} else if element.Value == nil {
element.Value = val // select first item by default
}
}
}
selects := form.Find("select")
for i := 0; i < selects.Length(); i++ {
s := selects.Eq(i)
name, ok := s.Attr("name")
if !ok {
continue
}
element, ok := elements[name]
if !ok {
element = &FormElement{
Type: "select",
Name: name,
}
elements[name] = element
}
options := s.Find("option")
for j := 0; j < options.Length(); j++ {
o := options.Eq(j)
value, ok := o.Attr("value")
if !ok {
// ignore an option without value
continue
}
val := &AvailableValue{
Value: value,
Label: o.Text(),
}
element.AddAvailableValue(val)
if _, ok := o.Attr("selected"); ok {
element.Value = val
}
if element.Value == nil {
element.Value = val // select first item by default
}
}
}
action, _ := form.Attr("action")
method, ok := form.Attr("method")
if !ok {
method = "get"
}
return &Form{
url: page.Url,
baseUrl: page.BaseUrl,
Action: action,
Method: method,
Elements: elements,
Logger: page.Logger,
}, nil
}
// Set sets a value to the element specified by name.
// if element have AvailableValues(eg. check, radio or select elements), value must be equals one of them.
func (form *Form) Set(name string, value string) error {
element, ok := form.Elements[name]
if !ok {
return FormElementNotFoundError{name}
}
if element.AvailableValues == nil {
element.Value = &AvailableValue{Value: value}
} else {
var found *AvailableValue
for _, val := range element.AvailableValues {
if val.Value == value {
found = val
break
}
}
if found == nil {
return fmt.Errorf("value %v is not available in %#v", value, element.AvailableValues)
}
element.Value = found
}
return nil
}
func (form *Form) SetForce(name string, value string) error {
if _, ok := form.Elements[name]; ok {
return form.Set(name, value)
}
form.Elements[name] = &FormElement{
Type: "hidden",
Name: name,
Value: &AvailableValue{Value: value},
}
return nil
}
// find a Value from element name and its label of available values.
func (form *Form) ValueByLabel(name string, label string) (string, error) {
element, ok := form.Elements[name]
if !ok {
return "", FormElementNotFoundError{name}
}
if element.AvailableValues == nil {
return "", fmt.Errorf("form element %v is not a selection", name)
}
for _, val := range element.AvailableValues {
if val.Label == label {
return val.Value, nil
}
}
// デバッグ用
_ = form.PrintSelection(name)
return "", fmt.Errorf("label %#v is not found in form element %v", label, name)
}
func (form *Form) SetByLabel(name string, label string) error {
value, err := form.ValueByLabel(name, label)
if err != nil {
return err
}
return form.Set(name, value)
}
// PrintSelection shows available values of the element specified by name.
func (form *Form) PrintSelection(name string) error {
element, ok := form.Elements[name]
if !ok {
return FormElementNotFoundError{name}
}
form.Logger.Printf("%v ->\n", name)
if element.AvailableValues == nil {
if element.Value == nil {
form.Logger.Printf(" nil\n")
} else {
form.Logger.Printf(" * %#v\n", element.Value.Label)
}
return nil
}
for _, value := range element.AvailableValues {
mark := " "
if element.Value == value {
mark = "*"
}
form.Logger.Printf(" %v %#v (%#v)\n", mark, value.Label, value.Value)
}
return nil
}
// Unset unset or uncheck the element specified by name.
func (form *Form) Unset(name string) error {
element, ok := form.Elements[name]
if !ok {
return FormElementNotFoundError{name}
}
element.Value = nil
return nil
}
// Check checks the checkbox specified by name.
func (form *Form) Check(name string) error {
return form.Select(name, 0)
}
// Uncheck unchecks the checkbox specified by name.
func (form *Form) Uncheck(name string) error {
return form.Unset(name)
}
// NumSelect returns number of available values of the select element specified by name.
func (form *Form) NumSelect(name string) (int, error) {
element, ok := form.Elements[name]
if !ok {
return 0, FormElementNotFoundError{name}
}
return len(element.AvailableValues), nil
}
// Select sets an answer to the select element specified by name.
func (form *Form) Select(name string, index int) error {
element, ok := form.Elements[name]
if !ok {
return FormElementNotFoundError{name}
}
if index < 0 || index >= len(element.AvailableValues) {
return fmt.Errorf("select out of range %v in %#v", index, element.AvailableValues)
}
element.Value = element.AvailableValues[index]
return nil
}
// SubmitOpt submits a form.
func (session *Session) Submit(form *Form) (*Response, error) {
return session.SubmitOpt(form, "")
}
// SubmitOpt submits a form.
// if imageId is non-empty, specifies "image" element to imitate clicking.
func (session *Session) SubmitOpt(form *Form, imageId string) (*Response, error) {
m := map[string]string{}
for name, element := range form.Elements {
if element.Value != nil {
if element.Type == "image" {
if imageId == "" || element.Name == imageId {
key := func(member string) string {
if name == "" {
return member
}
return name + "." + member
}
m[key("x")] = "0"
m[key("y")] = "0"
}
} else {
m[name] = element.Value.Value
}
}
}
if session.ShowFormPosting {
form.Logger.Printf("Form Posting:{\n")
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
form.Logger.Printf(" %v=%v\n", k, m[k])
}
form.Logger.Printf("}\n")
}
if session.Encoding != nil {
if session.ShowFormPosting {
form.Logger.Printf("converting to %v...\n", session.Encoding)
}
encoder := session.Encoding.NewEncoder()
for k, v := range m {
m[k], _, _ = transform.String(encoder, v)
}
}
data := url.Values{}
for k, v := range m {
data.Set(k, v)
}
reqUrl, _ := form.baseUrl.Parse(form.Action)
encoded := data.Encode()
req, _ := http.NewRequest(strings.ToUpper(form.Method), reqUrl.String(), bytes.NewBufferString(encoded))
req.Header.Set("Content-type", "application/x-www-form-urlencoded")
req.Header.Set("Referer", form.url.String())
req.Header.Set("Content-length", strconv.Itoa(len(encoded)))
//req.Header.Set("Origin", reqUrl.Scheme + "://" + reqUrl.Host)
return session.invoke(req)
}