/
service.go
72 lines (57 loc) · 1.41 KB
/
service.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
package main
import (
"net/http"
"golang.org/x/net/html"
"log"
"net/url"
"io/ioutil"
"bytes"
"launchpad.net/xmlpath"
"errors"
)
func main() {
// http.Handle("/authenticate", authenticateHandler)
http.ListenAndServe(":7890", nil)
}
//func authenticateHandler(w http.ResponseWriter, r *http.Request) {
//
//}
func authenticate(username, password string) {
data := url.Values{}
data.Set("UserName", username)
data.Set("Password", password)
response, err := http.PostForm("http://www.simunomics.com/Login.php", data)
panicError(err)
found, err := loadXpath(response, "//*[@id=\"BadPassInner\"]/text()[1]")
panicError(err)
found = bytes.TrimSpace(found)
log.Print(string(found))
}
func loadXpath(response *http.Response, xpath string) ([]byte, error) {
body, err := ioutil.ReadAll(response.Body)
panicError(err)
// Parse body to see if login worked
// reader := strings.NewReader(body)
root, err := html.Parse(bytes.NewBuffer(body))
if err != nil {
return nil, err
}
var b bytes.Buffer
html.Render(&b, root)
fixedHtml := b
// body = bytes.NewReader(fixedHtml)
xmlroot, xmlerr := xmlpath.ParseHTML(bytes.NewReader(fixedHtml.Bytes()))
if xmlerr != nil {
return nil, xmlerr
}
path := xmlpath.MustCompile(xpath)
if value, ok := path.Bytes(xmlroot); ok {
return value, nil
}
return nil, errors.New("Could not find xpath")
}
func panicError(err error) {
if (err != nil) {
log.Panic(err)
}
}