-
Notifications
You must be signed in to change notification settings - Fork 1
/
http_data_type.go
143 lines (129 loc) · 2.93 KB
/
http_data_type.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
package config
import (
"net/url"
"net/http"
"log"
"time"
"fmt"
"strings"
"regexp"
)
type Method string
const (
POST Method = "POST"
GET Method = "GET"
)
type Scheme string
const (
HTTP Scheme = "http://"
HTTPS Scheme = "https://"
)
// TODO: setup proxy service
type Link struct{
Protocol Scheme
URL *url.URL
// this link retry times for http request
RetryTimes int
// "GET" or "POST"
Type Method
// form data for "POST" request
FormData url.Values
Header http.Header
Cookies []*http.Cookie
// Dialer timeout and client timeout.
Timeout time.Duration
// whether this Link is available. such as http://javascript_xxx is not available.
Available bool
UnavailableAddr string
}
// note: only handle http or https, others will be setup unavailable. such as mailto:xxx.com
func NewLink(addr string) *Link{
var err error
link := new(Link)
link.RetryTimes = 3
link.Protocol = HTTPS
link.Available = true
reg := regexp.MustCompile("\\s+")
addr = reg.ReplaceAllString(addr, "")
if addr == ""{
log.Printf("url is \"\"\n", addr)
return &Link{Available: false, UnavailableAddr: "no addr"}
}
n := strings.Index(addr, string(HTTPS))
if n >= 0{
addr = addr[n:]
}else{
n = strings.Index(addr, string(HTTP))
if n >= 0{
addr = addr[n:]
}else{
items := strings.Split(addr, ":")
tmp := false
if len(items) == 2{
tmp, _ = regexp.MatchString("\\d+", items[1])
if !tmp{
return &Link{Available: false, UnavailableAddr: addr}
}
}
if len(items) == 1 || tmp{
addr = fmt.Sprintf("%s%s", string(link.Protocol), addr)
}
if len(items) > 2{
return &Link{Available: false, UnavailableAddr: addr}
}
}
}
link.URL, err = url.Parse(addr)
if err != nil{
log.Printf("parse url %s failed!\n", addr)
return &Link{Available: false, UnavailableAddr: addr}
}
link.Type = GET
link.FormData = make(url.Values)
link.Header = make(http.Header)
// link.AddHeader("User-Agent", "xxx")
link.Cookies = make([]*http.Cookie, 0, 16)
link.Timeout = 30 * time.Second
return link
}
func (this *Link) AddHeader(key, value string){
this.Header.Add(key, value)
}
func (this *Link) AddCookie(cookie *http.Cookie){
this.Cookies = append(this.Cookies, cookie)
}
func (this *Link) AddReqParam(key, value string){
this.FormData.Add(key, value)
}
type RawData struct{
LINK *Link
Data string
// if LINK is not available, the RawData should be the same.
Available bool
}
func NewRawData(link *Link, data string) *RawData{
if link == nil || !link.Available{
return &RawData{Available: false, LINK: link}
}
rd := new(RawData)
rd.Data = data
rd.LINK = link
rd.Available = true
return rd
}
type Text struct{
LINK *Link
Data string
// if LINK is not available, the Text should be the same.
Available bool
}
func NewText(link *Link, data string) *Text{
if link == nil || !link.Available{
return &Text{Available: false, LINK: link}
}
txt := new(Text)
txt.Data = data
txt.LINK = link
txt.Available = true
return txt
}