Debian Codegraph @

scrape/forms.go

raw
1 // Copyright 2013 The go-github AUTHORS. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file.
5
6 // forms.go contains logic for parsing and submitting HTML forms. None of this
7 // is specific to go-github in any way, and could easily be pulled out into a
8 // general purpose scraping library in the future.
9
10 package scrape
11
12 import (
13 "fmt"
14 "net/http"
15 "net/url"
16 "strings"
17
18 "github.com/PuerkitoBio/goquery"
19 "golang.org/x/net/html"
20 )
21
22 // htmlForm represents the basic elements of an HTML Form.
23 type htmlForm struct {
24 // Action is the URL where the form will be submitted
25 Action string
26 // Method is the HTTP method to use when submitting the form
27 Method string
28 // Values contains form values to be submitted
29 Values url.Values
30 }
31
32 // parseForms parses and returns all form elements beneath node. Form values
33 // include all input and textarea elements within the form. The values of radio
34 // and checkbox inputs are included only if they are checked.
35 //
36 // In the future, we might want to allow a custom selector to be passed in to
37 // further restrict what forms will be returned.
38 func parseForms(node *html.Node) (forms []htmlForm) {
39 if node == nil {
40 return nil
41 }
42
43 doc := goquery.NewDocumentFromNode(node)
44 doc.Find("form").Each(func(_ int, s *goquery.Selection) {
45 form := htmlForm{Values: url.Values{}}
46 form.Action, _ = s.Attr("action")
47 form.Method, _ = s.Attr("method")
48
49 s.Find("input").Each(func(_ int, s *goquery.Selection) {
50 name, _ := s.Attr("name")
51 if name == "" {
52 return
53 }
54
55 typ, _ := s.Attr("type")
56 typ = strings.ToLower(typ)
57 _, checked := s.Attr("checked")
58 if (typ == "radio" || typ == "checkbox") && !checked {
59 return
60 }
61
62 value, _ := s.Attr("value")
63 form.Values.Add(name, value)
64 })
65 s.Find("textarea").Each(func(_ int, s *goquery.Selection) {
66 name, _ := s.Attr("name")
67 if name == "" {
68 return
69 }
70
71 value := s.Text()
72 form.Values.Add(name, value)
73 })
74 forms = append(forms, form)
75 })
76
77 return forms
78 }
79
80 // fetchAndSubmitForm will fetch the page at urlStr, then parse and submit the first form found.
81 // setValues will be called with the parsed form values, allowing the caller to set any custom
82 // form values. Form submission will always use the POST method, regardless of the value of the
83 // method attribute in the form. The response from submitting the parsed form is returned.
84 func fetchAndSubmitForm(client *http.Client, urlStr string, setValues func(url.Values)) (*http.Response, error) {
85 resp, err := client.Get(urlStr)
86 if err != nil {
87 return nil, fmt.Errorf("error fetching url %q: %v", urlStr, err)
88 }
89
90 defer resp.Body.Close()
91 root, err := html.Parse(resp.Body)
92 if err != nil {
93 return nil, fmt.Errorf("error parsing response: %v", err)
94 }
95
96 forms := parseForms(root)
97 if len(forms) == 0 {
98 return nil, fmt.Errorf("no forms found at %q", urlStr)
99 }
100 form := forms[0]
101
102 actionURL, err := url.Parse(form.Action)
103 if err != nil {
104 return nil, fmt.Errorf("error parsing form action URL %q: %v", form.Action, err)
105 }
106 actionURL = resp.Request.URL.ResolveReference(actionURL)
107
108 // allow caller to fill out the form
109 if setValues != nil {
110 setValues(form.Values)
111 }
112
113 resp, err = client.PostForm(actionURL.String(), form.Values)
114 if err != nil {
115 return nil, fmt.Errorf("error posting form: %v", err)
116 }
117
118 return resp, nil
119 }