Skip to content

Commit

Permalink
optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
gospider007 committed Dec 16, 2023
1 parent b7a39e9 commit 948b2a5
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 14 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ Requests is a fully featured HTTP client library for Golang. Network requests ca
* [Local network card](https://github.com/gospider007/requests/blob/master/test/request/localAddr_test.go)
* [Response](https://github.com/gospider007/requests/tree/master/test/response)
* [Return whether to reuse connections](https://github.com/gospider007/requests/blob/master/test/response/isNewConn_test.go)
* [Return Raw Connection](https://github.com/gospider007/requests/blob/master/test/response/rawConn_test.go)
* [Return Proxy](https://github.com/gospider007/requests/blob/master/test/response/useProxy_test.go)
* [Middleware](https://github.com/gospider007/requests/tree/master/test/middleware)
* [Option Callback Method](https://github.com/gospider007/requests/blob/master/test/middleware/optionltCallBack_test.go)
* [Result Callback Method](https://github.com/gospider007/requests/blob/master/test/middleware/resultCallBack_test.go)
Expand Down
26 changes: 12 additions & 14 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,19 +249,12 @@ func (obj *Response) defaultDecode() bool {
return strings.Contains(obj.ContentType(), "html")
}

func (obj *Response) Read(con []byte) (i int, err error) {
done := make(chan struct{})
go func() {
i, err = obj.response.Body.Read(con)
close(done)
}()
select {
case <-obj.response.Request.Context().Done():
obj.ForceCloseConn()
err = obj.response.Request.Context().Err()
case <-done:
// must stream=true
func (obj *Response) Conn() *connecotr {
if obj.IsStream() {
return obj.rawConn.Conn()
}
return
return nil
}

// return true if response is stream
Expand All @@ -284,12 +277,17 @@ func (obj *Response) ReadBody() (err error) {
bufferPool.Put(bBody)
}()
if obj.bar && obj.ContentLength() > 0 {
err = tools.CopyWitchContext(obj.response.Request.Context(), &barBody{
_, err = io.Copy(&barBody{
bar: bar.NewClient(obj.response.ContentLength),
body: bBody,
}, obj.response.Body)
// err = tools.CopyWitchContext(obj.response.Request.Context(), &barBody{
// bar: bar.NewClient(obj.response.ContentLength),
// body: bBody,
// }, obj.response.Body)
} else {
err = tools.CopyWitchContext(obj.ctx, bBody, obj.response.Body)
_, err = io.Copy(bBody, obj.response.Body)
// err = tools.CopyWitchContext(obj.ctx, bBody, obj.response.Body)
}
if err != nil {
obj.ForceCloseConn()
Expand Down
24 changes: 24 additions & 0 deletions test/response/rawConn_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

import (
"testing"

"github.com/gospider007/requests"
)

func TestRawConn(t *testing.T) {
resp, err := requests.Get(nil, "https://httpbin.org/anything")
if err != nil {
t.Error(err)
}
if resp.Conn() != nil {
t.Error("conn is not nil")
}
resp, err = requests.Get(nil, "https://httpbin.org/anything", requests.RequestOption{Stream: true})
if err != nil {
t.Error(err)
}
if resp.Conn() == nil {
t.Error("conn is nil")
}
}
17 changes: 17 additions & 0 deletions test/response/useProxy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

import (
"testing"

"github.com/gospider007/requests"
)

func TestUseProxy(t *testing.T) {
resp, err := requests.Get(nil, "https://httpbin.org/anything")
if err != nil {
t.Error(err)
}
if resp.Proxy() != "" {
t.Error("proxy error")
}
}

0 comments on commit 948b2a5

Please sign in to comment.