Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1d2bbcd22f | ||
|
|
137121ee59 | ||
|
|
23a369677f | ||
|
|
d382505453 | ||
|
|
02bc589ba3 | ||
|
|
0deea9fc82 | ||
|
|
f27a538639 | ||
|
|
f490fa9c04 | ||
|
|
4f88f69899 | ||
|
|
1c6f19a23c |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1 +1,2 @@
|
||||
*.pyc
|
||||
*.vscode
|
||||
|
||||
21
base.go
21
base.go
@@ -12,32 +12,29 @@ func buildBodyRequest(wf *Workflow) *http.Request {
|
||||
var err error
|
||||
contentType := ""
|
||||
|
||||
if wf.Body.IOBody == nil {
|
||||
req, err = http.NewRequest(wf.Method, wf.GetStringURL(), nil)
|
||||
if wf.Body.IOBody() == nil {
|
||||
req, err = http.NewRequest(wf.Method, wf.GetRawURL(), nil)
|
||||
} else {
|
||||
var bodybuf *bytes.Buffer
|
||||
switch wf.Body.IOBody.(type) {
|
||||
switch wf.Body.IOBody().(type) {
|
||||
case []byte:
|
||||
bodybuf = bytes.NewBuffer(wf.Body.IOBody.([]byte))
|
||||
bodybuf = bytes.NewBuffer(wf.Body.IOBody().([]byte))
|
||||
case *bytes.Buffer:
|
||||
bodybuf = bytes.NewBuffer(wf.Body.IOBody.(*bytes.Buffer).Bytes())
|
||||
bodybuf = bytes.NewBuffer(wf.Body.IOBody().(*bytes.Buffer).Bytes())
|
||||
default:
|
||||
panic(errors.New("the type is not exist, type is" + reflect.TypeOf(wf.Body.IOBody).String()))
|
||||
}
|
||||
req, err = http.NewRequest(wf.Method, wf.GetStringURL(), bodybuf)
|
||||
req, err = http.NewRequest(wf.Method, wf.GetRawURL(), bodybuf)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if wf.Body.ContentType != "" {
|
||||
if wf.Body.ContentType == TypeContentEmpty {
|
||||
if wf.Body.ContentType() != "" {
|
||||
contentType = wf.Body.ContentType()
|
||||
} else {
|
||||
contentType = ""
|
||||
} else {
|
||||
contentType = wf.Body.ContentType
|
||||
}
|
||||
} else {
|
||||
if contentType == "" {
|
||||
if wf.Method == "POST" || wf.Method == "PUT" || wf.Method == "PATCH" {
|
||||
contentType = TypeURLENCODED
|
||||
|
||||
@@ -17,7 +17,7 @@ func writeFormUploadFile(mwriter *multipart.Writer, ufile *UploadFile) {
|
||||
io.Copy(part, ufile.FileReaderCloser)
|
||||
}
|
||||
|
||||
func createMultipart(postParams *Body, params []interface{}) {
|
||||
func createMultipart(postParams IBody, params []interface{}) {
|
||||
plen := len(params)
|
||||
|
||||
body := &bytes.Buffer{}
|
||||
@@ -85,6 +85,6 @@ func createMultipart(postParams *Body, params []interface{}) {
|
||||
}
|
||||
}
|
||||
|
||||
postParams.ContentType = mwriter.FormDataContentType()
|
||||
postParams.IOBody = body
|
||||
postParams.AddContentType(mwriter.FormDataContentType())
|
||||
postParams.SetIOBody(body)
|
||||
}
|
||||
|
||||
145
session.go
145
session.go
@@ -8,6 +8,7 @@ import (
|
||||
"net/url"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"golang.org/x/net/publicsuffix"
|
||||
@@ -16,9 +17,70 @@ import (
|
||||
// Body 相关参数结构
|
||||
type Body struct {
|
||||
// Query map[string][]string
|
||||
IOBody interface{}
|
||||
ioBody interface{}
|
||||
// prefix ContentType 前缀
|
||||
prefix string
|
||||
// Files []UploadFile
|
||||
ContentType string
|
||||
contentTypes map[string]int
|
||||
}
|
||||
|
||||
// NewBody new body pointer
|
||||
func NewBody() *Body {
|
||||
b := &Body{}
|
||||
b.contentTypes = make(map[string]int)
|
||||
return b
|
||||
}
|
||||
|
||||
// SetIOBody 设置IOBody的值
|
||||
func (body *Body) SetIOBody(iobody interface{}) {
|
||||
body.ioBody = iobody
|
||||
}
|
||||
|
||||
// IOBody 获取ioBody值
|
||||
func (body *Body) IOBody() interface{} {
|
||||
return body.ioBody
|
||||
}
|
||||
|
||||
// ContentType 获取ContentType
|
||||
func (body *Body) ContentType() string {
|
||||
content := body.prefix
|
||||
for kvalue := range body.contentTypes {
|
||||
content += kvalue + ";"
|
||||
}
|
||||
return strings.TrimRight(content, ";")
|
||||
}
|
||||
|
||||
// SetPrefix SetPrefix 和 AddContentType的顺序会影响到ContentType()的返回结果
|
||||
func (body *Body) SetPrefix(ct string) {
|
||||
body.prefix = strings.TrimRight(ct, ";") + ";"
|
||||
}
|
||||
|
||||
// AddContentType 添加 Add Type类型
|
||||
func (body *Body) AddContentType(ct string) {
|
||||
for _, v := range strings.Split(ct, ";") {
|
||||
v = strings.Trim(v, " ")
|
||||
if v != "" {
|
||||
if body.prefix != v {
|
||||
body.contentTypes[v] = 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// IBody 相关参数结构
|
||||
type IBody interface {
|
||||
|
||||
// Query map[string][]string
|
||||
IOBody() interface{}
|
||||
// SetIOBody
|
||||
SetIOBody(iobody interface{})
|
||||
// Files []UploadFile
|
||||
ContentType() string
|
||||
// AppendContent
|
||||
AddContentType(ct string)
|
||||
// AddPrefix 添加 Prefix
|
||||
SetPrefix(ct string)
|
||||
}
|
||||
|
||||
// BasicAuth 帐号认真结构
|
||||
@@ -31,12 +93,15 @@ type BasicAuth struct {
|
||||
|
||||
// Session 的基本方法
|
||||
type Session struct {
|
||||
client *http.Client
|
||||
transport *http.Transport
|
||||
cookiejar http.CookieJar
|
||||
body *Body
|
||||
auth *BasicAuth
|
||||
|
||||
body IBody
|
||||
|
||||
client *http.Client
|
||||
cookiejar http.CookieJar
|
||||
|
||||
transport *http.Transport
|
||||
|
||||
Header http.Header
|
||||
Query url.Values
|
||||
}
|
||||
@@ -44,20 +109,37 @@ type Session struct {
|
||||
const (
|
||||
// TypeJSON 类型
|
||||
TypeJSON = "application/json"
|
||||
|
||||
// TypeXML 类型
|
||||
TypeXML = "text/xml"
|
||||
|
||||
// TypePlain 类型
|
||||
TypePlain = "text/plain"
|
||||
|
||||
// TypeHTML 类型
|
||||
TypeHTML = "text/html"
|
||||
|
||||
// TypeURLENCODED 类型
|
||||
TypeURLENCODED = "application/x-www-form-urlencoded"
|
||||
|
||||
// TypeForm PostForm类型
|
||||
TypeForm = TypeURLENCODED
|
||||
// TypeContentEmpty 没有Form的类型 Content
|
||||
TypeContentEmpty = "ContentEmpty"
|
||||
|
||||
// TypeStream application/octet-stream 只能提交一个二进制流, 很少用
|
||||
TypeStream = "application/octet-stream"
|
||||
|
||||
// TypeFormData 类型
|
||||
TypeFormData = "multipart/form-data"
|
||||
|
||||
// TypeMixed Mixed类型
|
||||
TypeMixed = "multipart/mixed"
|
||||
|
||||
// HeaderKeyHost Host
|
||||
HeaderKeyHost = "Host"
|
||||
|
||||
// HeaderKeyUA User-Agent
|
||||
HeaderKeyUA = "User-Agent"
|
||||
|
||||
// HeaderKeyContentType Content-Type
|
||||
HeaderKeyContentType = "Content-Type"
|
||||
)
|
||||
@@ -67,26 +149,26 @@ type TypeConfig int
|
||||
|
||||
const (
|
||||
_ TypeConfig = iota
|
||||
// ConfigRequestTimeout request 包括 dial request redirect 总时间超时
|
||||
ConfigRequestTimeout // 支持time.Duration 和 int(秒为单位)
|
||||
// CRequestTimeout request 包括 dial request redirect 总时间超时
|
||||
CRequestTimeout // 支持time.Duration 和 int(秒为单位)
|
||||
|
||||
// ConfigDialTimeout 一个Connect过程的Timeout
|
||||
ConfigDialTimeout // 支持time.Duration 和 int(秒为单位)
|
||||
// CDialTimeout 一个Connect过程的Timeout
|
||||
CDialTimeout // 支持time.Duration 和 int(秒为单位)
|
||||
|
||||
// ConfigProxy 代理链接
|
||||
ConfigProxy // http, https, socks5
|
||||
// CProxy 代理链接
|
||||
CProxy // http, https, socks5
|
||||
|
||||
// ConfigInsecure InsecureSkipVerify
|
||||
ConfigInsecure // true, false
|
||||
// CInsecure InsecureSkipVerify
|
||||
CInsecure // true, false
|
||||
|
||||
// ConfigBasicAuth 帐号认证
|
||||
ConfigBasicAuth // user pwd
|
||||
// CBasicAuth 帐号认证
|
||||
CBasicAuth // user pwd
|
||||
|
||||
// ConfigTLS 帐号认证
|
||||
ConfigTLS // user pwd
|
||||
// CTLS 帐号认证
|
||||
CTLS // user pwd
|
||||
|
||||
// ConfigCookiejar 持久化 CookieJar
|
||||
ConfigCookiejar // true or false ; default = true
|
||||
// CCookiejar 持久化 CookieJar
|
||||
CCookiejar // true or false ; default = true
|
||||
)
|
||||
|
||||
// NewSession 创建Session
|
||||
@@ -101,14 +183,14 @@ func NewSession() *Session {
|
||||
}
|
||||
|
||||
client.Jar = cjar
|
||||
return &Session{client: client, body: &Body{}, transport: transport, auth: nil, cookiejar: client.Jar, Header: make(http.Header)}
|
||||
return &Session{client: client, body: NewBody(), transport: transport, auth: nil, cookiejar: client.Jar, Header: make(http.Header)}
|
||||
}
|
||||
|
||||
// SetConfig 设置配置
|
||||
func (ses *Session) SetConfig(typeConfig TypeConfig, values interface{}) {
|
||||
|
||||
switch typeConfig {
|
||||
case ConfigRequestTimeout:
|
||||
case CRequestTimeout:
|
||||
switch v := values.(type) {
|
||||
case time.Duration:
|
||||
ses.client.Timeout = v
|
||||
@@ -123,9 +205,9 @@ func (ses *Session) SetConfig(typeConfig TypeConfig, values interface{}) {
|
||||
default:
|
||||
panic(errors.New("error type " + reflect.TypeOf(v).String()))
|
||||
}
|
||||
case ConfigDialTimeout:
|
||||
case CDialTimeout:
|
||||
// 没时间实现这些小细节
|
||||
case ConfigCookiejar:
|
||||
case CCookiejar:
|
||||
v := values.(bool)
|
||||
if v {
|
||||
if ses.client.Jar == nil {
|
||||
@@ -134,7 +216,7 @@ func (ses *Session) SetConfig(typeConfig TypeConfig, values interface{}) {
|
||||
} else {
|
||||
ses.client.Jar = nil
|
||||
}
|
||||
case ConfigProxy:
|
||||
case CProxy:
|
||||
switch v := values.(type) {
|
||||
case string:
|
||||
purl, err := (url.Parse(v))
|
||||
@@ -144,12 +226,14 @@ func (ses *Session) SetConfig(typeConfig TypeConfig, values interface{}) {
|
||||
ses.transport.Proxy = http.ProxyURL(purl)
|
||||
case *url.URL:
|
||||
ses.transport.Proxy = http.ProxyURL(v)
|
||||
case nil:
|
||||
ses.transport.Proxy = nil
|
||||
}
|
||||
case ConfigInsecure:
|
||||
case CInsecure:
|
||||
ses.transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: !values.(bool)}
|
||||
case ConfigTLS:
|
||||
case CTLS:
|
||||
ses.transport.TLSClientConfig = values.(*tls.Config)
|
||||
case ConfigBasicAuth:
|
||||
case CBasicAuth:
|
||||
if ses.auth == nil {
|
||||
ses.auth = &BasicAuth{}
|
||||
}
|
||||
@@ -200,7 +284,6 @@ func (ses *Session) SetCookies(u *url.URL, cookies []*http.Cookie) {
|
||||
|
||||
// Cookies 返回 Cookies
|
||||
func (ses *Session) Cookies(u *url.URL) []*http.Cookie {
|
||||
|
||||
return ses.cookiejar.Cookies(u)
|
||||
}
|
||||
|
||||
|
||||
@@ -76,7 +76,7 @@ func TestSession_Post(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ses := NewSession()
|
||||
got, err := ses.Post("http://httpbin.org/post").SetBodyParams(tt.args.params...).Execute()
|
||||
got, err := ses.Post("http://httpbin.org/post").SetBody(tt.args.params...).Execute()
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Metchod error = %v", err)
|
||||
@@ -118,7 +118,7 @@ func TestSession_Setparams(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "test xml",
|
||||
fields: fields{client: &http.Client{}, params: &Body{}},
|
||||
fields: fields{client: &http.Client{}, params: NewBody()},
|
||||
args: args{params: []interface{}{`<request><parameters><password>test</password></parameters></request>`, TypeXML}},
|
||||
want: regexp.MustCompile(`"data": "<request><parameters><password>test</password></parameters></request>"`),
|
||||
},
|
||||
@@ -127,7 +127,7 @@ func TestSession_Setparams(t *testing.T) {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ses := NewSession()
|
||||
|
||||
got, err := ses.Post("http://httpbin.org/post").SetBodyParams(tt.args.params...).Execute()
|
||||
got, err := ses.Post("http://httpbin.org/post").SetBody(tt.args.params...).Execute()
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("Metchod error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
@@ -169,7 +169,7 @@ func TestSession_PostUploadFile(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ses := NewSession()
|
||||
got, err := ses.Post("http://httpbin.org/post").SetBodyParams(tt.args.params, TypeFormData).Execute()
|
||||
got, err := ses.Post("http://httpbin.org/post").SetBody(tt.args.params, TypeFormData).Execute()
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Metchod error = %v", err)
|
||||
@@ -213,7 +213,7 @@ func TestSession_Put(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ses := NewSession()
|
||||
got, err := ses.Put("http://httpbin.org/put").SetBodyParams(tt.args.params, TypeFormData).Execute()
|
||||
got, err := ses.Put("http://httpbin.org/put").SetBody(tt.args.params, TypeFormData).Execute()
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Metchod error = %v", err)
|
||||
@@ -257,7 +257,7 @@ func TestSession_Patch(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ses := NewSession()
|
||||
got, err := ses.Patch("http://httpbin.org/patch").SetBodyParams(tt.args.params, TypeFormData).Execute()
|
||||
got, err := ses.Patch("http://httpbin.org/patch").SetBody(tt.args.params, TypeFormData).Execute()
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Metchod error = %v", err)
|
||||
@@ -285,19 +285,19 @@ func TestSession_SetConfig(t *testing.T) {
|
||||
}{
|
||||
{
|
||||
name: "test timeout",
|
||||
args: args{typeConfig: ConfigRequestTimeout, values: 0.01},
|
||||
args: args{typeConfig: CRequestTimeout, values: 0.01},
|
||||
wantErr: true,
|
||||
},
|
||||
|
||||
{
|
||||
name: "test not timeout",
|
||||
args: args{typeConfig: ConfigRequestTimeout, values: 5},
|
||||
args: args{typeConfig: CRequestTimeout, values: 5},
|
||||
wantErr: false,
|
||||
},
|
||||
|
||||
{
|
||||
name: "test proxy",
|
||||
args: args{typeConfig: ConfigProxy, values: "http://474420502.top:7070"},
|
||||
args: args{typeConfig: CProxy, values: "http://474420502.top:7070"},
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
@@ -319,7 +319,7 @@ func TestSession_SetConfig(t *testing.T) {
|
||||
func TestSession_SetConfigInsecure(t *testing.T) {
|
||||
|
||||
ses := NewSession()
|
||||
ses.SetConfig(ConfigInsecure, false)
|
||||
ses.SetConfig(CInsecure, false)
|
||||
|
||||
for _, badSSL := range []string{
|
||||
"https://self-signed.badssl.com/",
|
||||
|
||||
47
workflow.go
47
workflow.go
@@ -1,7 +1,6 @@
|
||||
package requests
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"regexp"
|
||||
@@ -13,7 +12,7 @@ type Workflow struct {
|
||||
session *Session
|
||||
ParsedURL *url.URL
|
||||
Method string
|
||||
Body *Body
|
||||
Body IBody
|
||||
Header http.Header
|
||||
Cookies map[string]*http.Cookie
|
||||
}
|
||||
@@ -23,9 +22,9 @@ func NewWorkflow(ses *Session, u string) *Workflow {
|
||||
wf := &Workflow{}
|
||||
wf.SwitchSession(ses)
|
||||
|
||||
wf.SetURL(u)
|
||||
wf.SetRawURL(u)
|
||||
|
||||
wf.Body = &Body{}
|
||||
wf.Body = NewBody()
|
||||
wf.Header = make(http.Header)
|
||||
wf.Cookies = make(map[string]*http.Cookie)
|
||||
return wf
|
||||
@@ -95,14 +94,25 @@ func (wf *Workflow) DelCookie(name interface{}) *Workflow {
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetStringURL 获取url的string形式
|
||||
func (wf *Workflow) GetStringURL() string {
|
||||
// GetParsedURL 获取url的string形式
|
||||
func (wf *Workflow) GetParsedURL() *url.URL {
|
||||
return wf.ParsedURL
|
||||
}
|
||||
|
||||
// SetParsedURL 获取url的string形式
|
||||
func (wf *Workflow) SetParsedURL(u *url.URL) *Workflow {
|
||||
wf.ParsedURL = u
|
||||
return wf
|
||||
}
|
||||
|
||||
// GetRawURL 获取url的string形式
|
||||
func (wf *Workflow) GetRawURL() string {
|
||||
u := strings.Split(wf.ParsedURL.String(), "?")[0] + "?" + wf.GetCombineQuery().Encode()
|
||||
return u
|
||||
}
|
||||
|
||||
// SetURL 设置 url
|
||||
func (wf *Workflow) SetURL(srcURL string) *Workflow {
|
||||
// SetRawURL 设置 url
|
||||
func (wf *Workflow) SetRawURL(srcURL string) *Workflow {
|
||||
purl, err := url.ParseRequestURI(srcURL)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
@@ -174,25 +184,24 @@ func (wf *Workflow) SetURLRawPath(path string) *Workflow {
|
||||
return wf
|
||||
}
|
||||
|
||||
// SetBodyParams 参数设置
|
||||
func (wf *Workflow) SetBodyParams(params ...interface{}) *Workflow {
|
||||
if params != nil {
|
||||
// SetBody 参数设置
|
||||
func (wf *Workflow) SetBody(params ...interface{}) *Workflow {
|
||||
|
||||
if params != nil {
|
||||
plen := len(params)
|
||||
defaultContentType := TypeURLENCODED
|
||||
|
||||
if plen >= 2 {
|
||||
t := params[plen-1]
|
||||
defaultContentType = t.(string)
|
||||
wf.Body.ContentType = defaultContentType
|
||||
wf.Body.SetPrefix(defaultContentType)
|
||||
} else {
|
||||
wf.Body.ContentType = defaultContentType
|
||||
wf.Body.SetPrefix(defaultContentType)
|
||||
}
|
||||
|
||||
if defaultContentType == TypeFormData {
|
||||
createMultipart(wf.Body, params)
|
||||
} else {
|
||||
|
||||
var values url.Values
|
||||
switch param := params[0].(type) {
|
||||
case map[string]string:
|
||||
@@ -200,19 +209,18 @@ func (wf *Workflow) SetBodyParams(params ...interface{}) *Workflow {
|
||||
for k, v := range param {
|
||||
values.Set(k, v)
|
||||
}
|
||||
wf.Body.IOBody = []byte(values.Encode())
|
||||
wf.Body.SetIOBody([]byte(values.Encode()))
|
||||
case map[string][]string:
|
||||
values = param
|
||||
wf.Body.IOBody = []byte(values.Encode())
|
||||
wf.Body.SetIOBody([]byte(values.Encode()))
|
||||
case string:
|
||||
wf.Body.IOBody = []byte(param)
|
||||
wf.Body.SetIOBody([]byte(param))
|
||||
case []byte:
|
||||
wf.Body.IOBody = param
|
||||
wf.Body.SetIOBody(param)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return wf
|
||||
}
|
||||
|
||||
@@ -273,7 +281,6 @@ func (wf *Workflow) Execute() (*Response, error) {
|
||||
if wf.session.auth != nil {
|
||||
req.SetBasicAuth(wf.session.auth.User, wf.session.auth.Password)
|
||||
}
|
||||
log.Println(req.Header)
|
||||
|
||||
resp, err := wf.session.client.Do(req)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user