From 1d2bbcd22fcd71e9477bf52bfa6fc82540778101 Mon Sep 17 00:00:00 2001 From: huangsimin Date: Wed, 21 Nov 2018 15:21:01 +0800 Subject: [PATCH] ParseURL -> ParsedURL name; add method Set Get ParsedURL --- base.go | 4 ++-- session.go | 12 +++++++----- workflow.go | 21 ++++++++++++++++----- 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/base.go b/base.go index 0629d00..eb90eb8 100644 --- a/base.go +++ b/base.go @@ -13,7 +13,7 @@ func buildBodyRequest(wf *Workflow) *http.Request { contentType := "" if wf.Body.IOBody() == nil { - req, err = http.NewRequest(wf.Method, wf.GetStringURL(), nil) + req, err = http.NewRequest(wf.Method, wf.GetRawURL(), nil) } else { var bodybuf *bytes.Buffer switch wf.Body.IOBody().(type) { @@ -24,7 +24,7 @@ func buildBodyRequest(wf *Workflow) *http.Request { 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 { diff --git a/session.go b/session.go index 5e4da77..8b5e0ed 100644 --- a/session.go +++ b/session.go @@ -93,11 +93,14 @@ type BasicAuth struct { // Session 的基本方法 type Session struct { + auth *BasicAuth + + body IBody + client *http.Client - transport *http.Transport cookiejar http.CookieJar - body IBody - auth *BasicAuth + + transport *http.Transport Header http.Header Query url.Values @@ -122,7 +125,7 @@ const ( // TypeForm PostForm类型 TypeForm = TypeURLENCODED - // TypeStream application/octet-stream 类型 binary + // TypeStream application/octet-stream 只能提交一个二进制流, 很少用 TypeStream = "application/octet-stream" // TypeFormData 类型 @@ -281,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) } diff --git a/workflow.go b/workflow.go index 1e11136..1f4fb24 100644 --- a/workflow.go +++ b/workflow.go @@ -22,7 +22,7 @@ func NewWorkflow(ses *Session, u string) *Workflow { wf := &Workflow{} wf.SwitchSession(ses) - wf.SetURL(u) + wf.SetRawURL(u) wf.Body = NewBody() wf.Header = make(http.Header) @@ -94,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)