-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathhttp.go
More file actions
89 lines (77 loc) · 2.23 KB
/
http.go
File metadata and controls
89 lines (77 loc) · 2.23 KB
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
package main
import (
"fmt"
"io"
"net/http"
"strings"
)
func requestPage(args *GlobalOptions, host string, url string) (string, error) {
return doHttpRequestAndReadResponse(args, http.MethodGet, host, url, "")
}
func postPage(args *GlobalOptions, host string, url string, requestBody string) (string, error) {
return doHttpRequestAndReadResponse(args, http.MethodPost, host, url, requestBody)
}
func doHttpRequestAndReadResponse(args *GlobalOptions, httpMethod string, host string, requestUrl string, requestBody string) (string, error) {
model, token, err := readTokenAndModel2GlobalOptions(args, host)
if err != nil {
return "", err
}
if args.Verbose {
fmt.Println(fmt.Sprintf("send HTTP %s request to: %s", httpMethod, requestUrl))
}
if isModel316(model) {
if strings.Contains(requestUrl, "?") {
splits := strings.Split(requestUrl, "?")
requestUrl = splits[0] + "?Gambit=" + token + "&" + splits[1]
} else {
requestUrl = requestUrl + "?Gambit=" + token
}
}
req, err := http.NewRequest(httpMethod, requestUrl, strings.NewReader(requestBody))
if err != nil {
return "", err
}
if isModel30x(model) {
req.Header.Set("Cookie", "SID="+token)
} else if isModel316(model) {
req.Header.Set("Cookie", "gambitCookie="+token)
} else {
panic("model not supported")
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
if args.Verbose {
fmt.Println(resp.Status)
}
bytes, err := io.ReadAll(resp.Body)
return string(bytes), err
}
func doUnauthenticatedHttpRequestAndReadResponse(args *GlobalOptions, httpMethod string, requestUrl string, requestBody string) (string, error) {
if args.Verbose {
fmt.Println("Fetching data from: " + requestUrl)
}
req, err := http.NewRequest(httpMethod, requestUrl, strings.NewReader(requestBody))
if err != nil {
return "", err
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
if args.Verbose {
fmt.Println(resp.Status)
for name, values := range resp.Header {
for _, value := range values {
fmt.Println(fmt.Sprintf("Response header: '%s' -- '%s'", name, value))
}
}
}
bytes, err := io.ReadAll(resp.Body)
return string(bytes), err
}