Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
update
  • Loading branch information
Albert-Zhan committed Feb 7, 2025
1 parent 91bfadb commit aae63c2
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 141 deletions.
57 changes: 28 additions & 29 deletions body/form.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"os"
"path/filepath"
Expand All @@ -13,37 +12,37 @@ import (
type Form struct {
dataBuf *bytes.Buffer
dataStr string
data *multipart.Writer
data *multipart.Writer
}

func NewFormData() *Form {
bodyBuf:=&bytes.Buffer{}
bodyWriter:=multipart.NewWriter(bodyBuf)
bodyBuf := &bytes.Buffer{}
bodyWriter := multipart.NewWriter(bodyBuf)
return &Form{
dataBuf: bodyBuf,
dataStr: "",
data: bodyWriter,
data: bodyWriter,
}
}

func (this *Form) SetBoundary(boundary string) *Form {
_=this.data.SetBoundary(boundary)
_ = this.data.SetBoundary(boundary)
return this
}

func (this *Form) SetData(name,value string) *Form {
_=this.data.WriteField(name,value)
func (this *Form) SetData(name, value string) *Form {
_ = this.data.WriteField(name, value)
return this
}

func (this *Form) SetFile(name,file string) *Form {
fd,err:=os.Open(file)
func (this *Form) SetFile(name, file string) *Form {
fd, err := os.Open(file)
defer fd.Close()
if err!=nil {
if err != nil {
panic("file does not exist")
}
fileWriter,_:=this.data.CreateFormFile(name,filepath.Base(file))
_,_=io.Copy(fileWriter,fd)
fileWriter, _ := this.data.CreateFormFile(name, filepath.Base(file))
_, _ = io.Copy(fileWriter, fd)
return this
}

Expand All @@ -56,24 +55,24 @@ func (this *Form) GetContentType() string {
}

func (this *Form) Encode() io.Reader {
_=this.data.Close()
_ = this.data.Close()

r:=multipart.NewReader(this.dataBuf,this.data.Boundary())
f,err:=r.ReadForm(0)
if err==nil {
header:="--"+this.data.Boundary()+"\n"
dataStr:=header
foot:="--"+this.data.Boundary()+"--"
for k,v:=range f.Value {
dataStr+=fmt.Sprintf(`Content-Disposition: form-data; name="%s"`,k)+"\n"
dataStr+=v[0]+"\n"
r := multipart.NewReader(this.dataBuf, this.data.Boundary())
f, err := r.ReadForm(0)
if err == nil {
header := "--" + this.data.Boundary() + "\n"
dataStr := header
foot := "--" + this.data.Boundary() + "--"
for k, v := range f.Value {
dataStr += fmt.Sprintf(`Content-Disposition: form-data; name="%s"`, k) + "\n"
dataStr += v[0] + "\n"
}
for fk,fv:=range f.File{
dataStr+=fmt.Sprintf(`Content-Disposition: form-data; name="%s"; filename="%s" Content-Type: application/octet-stream`,fk,fv[0].Filename)+"\n"
dataStr+=fmt.Sprintf("%v",fv)+"\n"
for fk, fv := range f.File {
dataStr += fmt.Sprintf(`Content-Disposition: form-data; name="%s"; filename="%s" Content-Type: application/octet-stream`, fk, fv[0].Filename) + "\n"
dataStr += fmt.Sprintf("%v", fv) + "\n"
}
dataStr+=foot
this.dataStr=dataStr
dataStr += foot
this.dataStr = dataStr
}
return ioutil.NopCloser(this.dataBuf)
return io.NopCloser(this.dataBuf)
}
35 changes: 21 additions & 14 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,47 @@ import (
)

type HttpClient struct {
client *http.Client
client *http.Client
transport *http.Transport
}

func NewHttpClient() *HttpClient {
tr:=&http.Transport{}
tr := &http.Transport{}

client:=&http.Client{
Transport:tr,
Timeout: 30*time.Second,
client := &http.Client{
Transport: tr,
Timeout: 30 * time.Second,
}
return &HttpClient{client:client,transport:tr}
return &HttpClient{client: client, transport: tr}
}

func (this *HttpClient) SetProxy(proxyUrl string) {
func (this *HttpClient) SetProxy(proxyUrl string) *HttpClient {
proxy, _ := url.Parse(proxyUrl)
this.transport.Proxy=http.ProxyURL(proxy)
this.transport.Proxy = http.ProxyURL(proxy)
return this
}

func (this *HttpClient) ClearProxy() *HttpClient {
this.transport.Proxy = nil
return this
}

func (this *HttpClient) SetSkipVerify(isSkipVerify bool) {
this.transport.TLSClientConfig=&tls.Config{InsecureSkipVerify: isSkipVerify}
func (this *HttpClient) SetSkipVerify(isSkipVerify bool) *HttpClient {
this.transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: isSkipVerify}
return this
}

func (this *HttpClient) SetTimeout(t time.Duration) *HttpClient {
this.client.Timeout=t
this.client.Timeout = t
return this
}

func (this *HttpClient) SetCookieJar(j *CookieJar) *HttpClient {
this.client.Jar=j
this.client.Jar = j
return this
}

func (this *HttpClient) SetRedirect(f func(req *http.Request, via []*http.Request) error) *HttpClient {
this.client.CheckRedirect=f
this.client.CheckRedirect = f
return this
}
}
9 changes: 4 additions & 5 deletions cookiejar.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
)

type CookieJar struct {

mu sync.Mutex

entries map[string]map[string]entry
Expand Down Expand Up @@ -43,7 +42,7 @@ type entry struct {
Expires time.Time
Creation time.Time
LastAccess time.Time
seqNum uint64
seqNum uint64
}

func (e *entry) id() string {
Expand Down Expand Up @@ -148,7 +147,7 @@ func (j *CookieJar) cookies(u *url.URL, now time.Time) (cookies []*http.Cookie)
})
for _, e := range selected {
//修复了读取cookie时缺少Domain,造成读取后的cookie请求失效问题
cookies = append(cookies, &http.Cookie{Name: e.Name, Value: e.Value,Domain:e.Domain})
cookies = append(cookies, &http.Cookie{Name: e.Name, Value: e.Value, Domain: e.Domain})
}

return cookies
Expand Down Expand Up @@ -256,7 +255,7 @@ func jarKey(host string) string {
return host
}

i :=strings.LastIndex(host, ".")
i := strings.LastIndex(host, ".")
if i <= 0 {
return host
}
Expand Down Expand Up @@ -495,4 +494,4 @@ func ascii(s string) bool {
}
}
return true
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/Albert-Zhan/httpc

go 1.16
go 1.23

Loading

0 comments on commit aae63c2

Please sign in to comment.