Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

完成issue #374

Merged
merged 2 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@ on:
jobs:

build:
name: Build
runs-on: ubuntu-latest
strategy:
matrix:
go: ['1.15', '1.16', '1.17', '1.18', '1.19', '1.20']
name: Go ${{ matrix.go }} sample

steps:

- name: Set up Go 1.14
- name: Set up Go 1.13
uses: actions/setup-go@v1
with:
go-version: 1.14
go-version: ${{ matrix.go }}
id: go

- name: Check out code into the Go module directory
Expand Down
63 changes: 59 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,11 @@ gout 是go写的http 客户端,为提高工作效率而开发
- [Using chunked data format](#Using-chunked-data-format)
- [NewWithOpt](#NewWithOpt)
- [Insecure skip verify](#insecure-skip-verify)
- [Turn off 3xx status code automatic jump](#Turn-off-3xx-status-code-automatic-jump)
- [NewWithOpt set timeout](#NewWithOpt-set-timeout)
- [Turn off 3xx status code automatic jump](#turn-off-3xx-status-code-automatic-jump)
- [NewWithOpt set timeout](#new-with-opt-set-timeout)
- [NewWithOpt unix sock](#new-with-opt-unix-socket)
- [NewWithOpt proxy](#new-with-opt-proxy)
- [NewWithOpt socks5](#new-with-opt-socks5)
- [Global configuration](#Global-configuration)
- [set timeout](#set-timeout)
- [set debug](#set-debug)
Expand Down Expand Up @@ -2103,7 +2106,7 @@ func main() {
}
}
```
## Turn off 3xx status code automatic jump
## turn off 3xx status code automatic jump
golang client库默认遇到301的状态码会自动跳转重新发起新请求, 你希望关闭这种默认形为, 那就使用下面的功能
```go
import (
Expand All @@ -2120,7 +2123,7 @@ func main() {
}
}
```
## NewWithOpt set timeout
## new with opt set timeout
```gout.WithTimeout``` 为了让大家少用```gout.SetTimeout```而设计
```go
import (
Expand All @@ -2138,6 +2141,58 @@ func main() {
}
```

## new with opt unix socket
```gout.WithUnixSocket``` 为了让大家少用```.UnixSocket ```而设计
```go
import (
"github.com/guonaihong/gout"
)

func main() {
// globalWithOpt里面包含连接池, 这是一个全局可复用的对象, 一个进程里面可能只需创建1个, 如果有多个不同的unixsocket,可以创建多个
globalWithOpt := gout.NewWithOpt(gout.WithUnixSocket("/tmp/test.socket"))
err := globalWithOpt.GET("url").Do()
if err != nil {
fmt.Printf("err = %v\n" ,err)
return
}
}
```
## new with opt proxy
```gout.WithProxy``` 为了让大家少用```.SetProxy ```而设计
```go
import (
"github.com/guonaihong/gout"
)

func main() {
// globalWithOpt里面包含连接池, 这是一个全局可复用的对象, 一个进程里面可能只需创建1个, 如果有多个不同的proxy,可以创建多个
globalWithOpt := gout.NewWithOpt(gout.WithProxy("http://127.0.0.1:7000"))
err := globalWithOpt.GET("url").Do()
if err != nil {
fmt.Printf("err = %v\n" ,err)
return
}
}
```

## new with opt socks5
```gout.WithSocks5``` 为了让大家少用```.SetSOCKS5```而设计
```go
import (
"github.com/guonaihong/gout"
)

func main() {
// globalWithOpt里面包含连接池, 这是一个全局可复用的对象, 一个进程里面可能只需创建1个, 如果有多个不同的socks5,可以创建多个
globalWithOpt := gout.NewWithOpt(gout.WithSocks5("127.0.0.1:7000"))
err := globalWithOpt.GET("url").Do()
if err != nil {
fmt.Printf("err = %v\n" ,err)
return
}
}
```
# Global configuration
## set timeout

Expand Down
9 changes: 6 additions & 3 deletions dataflow/dataflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ func (df *DataFlow) SetRequest(req *http.Request) *DataFlow {

// SetBody set the data to the http body, Support string/bytes/io.Reader
func (df *DataFlow) SetBody(obj interface{}) *DataFlow {

df.Req.bodyEncoder = encode.NewBodyEncode(obj)
return df
}
Expand Down Expand Up @@ -240,8 +239,9 @@ func (df *DataFlow) getTransport() (*http.Transport, bool) {
}

// UnixSocket 函数会修改Transport, 请像对待全局变量一样对待UnixSocket
// 对于全局变量的解释可看下面的链接
// https://github.com/guonaihong/gout/issues/373
func (df *DataFlow) UnixSocket(path string) *DataFlow {

df.initTransport()

transport, ok := df.getTransport()
Expand All @@ -258,6 +258,8 @@ func (df *DataFlow) UnixSocket(path string) *DataFlow {
}

// SetProxy 函数会修改Transport,请像对待全局变量一样对待SetProxy
// 对于全局变量的解释可看下面的链接
// https://github.com/guonaihong/gout/issues/373
func (df *DataFlow) SetProxy(proxyURL string) *DataFlow {
proxy, err := url.Parse(modifyURL(proxyURL))
if err != nil {
Expand All @@ -279,6 +281,8 @@ func (df *DataFlow) SetProxy(proxyURL string) *DataFlow {
}

// SetSOCKS5 函数会修改Transport,请像对待全局变量一样对待SetSOCKS5
// 对于全局变量的解释可看下面的链接
// https://github.com/guonaihong/gout/issues/373
func (df *DataFlow) SetSOCKS5(addr string) *DataFlow {
dialer, err := proxy.SOCKS5("tcp", addr, nil, proxy.Direct)
if err != nil {
Expand Down Expand Up @@ -331,7 +335,6 @@ func (df *DataFlow) BindJSON(obj interface{}) *DataFlow {
df.out.RspBodyType = "json"
df.Req.bodyDecoder = append(df.Req.bodyDecoder, decode.NewJSONDecode(obj))
return df

}

// BindYAML parse the yaml string in http body to obj.
Expand Down
19 changes: 6 additions & 13 deletions dataflow/req.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type Req struct {

callback func(*Context) error

//cookie
// cookie
cookies []*http.Cookie

ctxIndex int
Expand Down Expand Up @@ -120,7 +120,6 @@ func (r *Req) addDefDebug() {
r.ReqBodyType = "yaml"
}
}

}

func (r *Req) addContextType(req *http.Request) {
Expand All @@ -138,7 +137,6 @@ func (r *Req) addContextType(req *http.Request) {
req.Header.Add("Content-Type", "application/x-yaml")
}
}

}

func (r *Req) selectRequest(body *bytes.Buffer) (req *http.Request, err error) {
Expand Down Expand Up @@ -336,7 +334,7 @@ func (r *Req) Request() (req *http.Request, err error) {
if r.userName != nil && r.password != nil {
req.SetBasicAuth(*r.userName, *r.password)
}
//运行请求中间件
// 运行请求中间件
for _, reqModify := range r.reqModify {
if err = reqModify.ModifyRequest(req); err != nil {
return nil, err
Expand Down Expand Up @@ -377,15 +375,14 @@ func (r *Req) GetContext() context.Context {

// TODO 优化代码,每个decode都有自己的指针偏移直接指向流,减少大body的内存使用
func (r *Req) decodeBody(req *http.Request, resp *http.Response) (err error) {

if r.bodyDecoder != nil {
var all []byte
if len(r.bodyDecoder) > 1 {
all, err = ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
//已经取走数据,直接关闭body
// 已经取走数据,直接关闭body
resp.Body.Close()
}

Expand Down Expand Up @@ -432,7 +429,7 @@ func (r *Req) decode(req *http.Request, resp *http.Response, openDebug bool) (er
return err
}
}
//运行响应中间件。放到debug打印后面,避免混淆请求返回内容
// 运行响应中间件。放到debug打印后面,避免混淆请求返回内容
for _, modify := range r.responseModify {
err = modify.ModifyResponse(resp)
if err != nil {
Expand Down Expand Up @@ -461,7 +458,6 @@ func clearBody(resp *http.Response) error {
}

func (r *Req) Bind(req *http.Request, resp *http.Response) (err error) {

if err = r.decode(req, resp, r.Setting.Debug); err != nil {
return err
}
Expand All @@ -485,7 +481,6 @@ func (r *Req) Bind(req *http.Request, resp *http.Response) (err error) {
}

return nil

}

func (r *Req) Client() *http.Client {
Expand Down Expand Up @@ -528,11 +523,10 @@ func (r *Req) getReqAndRsp() (req *http.Request, rsp *http.Response, err error)
// 如果调用Chunked()接口, 就使用chunked的数据包
r.maybeUseChunked(req)

//resp, err := r.Client().Do(req)
//TODO r.Client() 返回Do接口
// resp, err := r.Client().Do(req)
// TODO r.Client() 返回Do接口
rsp, err = opt.StartTrace(opt, r.canTrace(), req, r.Client())
return

}

// Response 获取原始http.Response数据结构
Expand All @@ -544,7 +538,6 @@ func (r *Req) Response() (rsp *http.Response, err error) {

// Do Send function
func (r *Req) Do() (err error) {

req, resp, err := r.getReqAndRsp()
if resp != nil {
defer resp.Body.Close()
Expand Down
47 changes: 47 additions & 0 deletions gout_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import (
"net/http"
"time"

"github.com/guonaihong/gout/hcutil"
"github.com/guonaihong/gout/setting"
)

type options struct {
hc *http.Client
setting.Setting
err error
}

type Option interface {
Expand Down Expand Up @@ -72,3 +74,48 @@ func WithTimeout(t time.Duration) Option {
func (t *timeout) apply(opts *options) {
opts.SetTimeout(time.Duration(*t))
}

// 5. 设置代理
type proxy string

func WithProxy(p string) Option {
return (*proxy)(&p)
}

func (p *proxy) apply(opts *options) {
if opts.hc == nil {
opts.hc = &http.Client{}
}

opts.err = hcutil.SetProxy(opts.hc, string(*p))
}

// 6. 设置socks5代理
type socks5 string

func WithSocks5(s string) Option {
return (*socks5)(&s)
}

func (s *socks5) apply(opts *options) {
if opts.hc == nil {
opts.hc = &http.Client{}
}

opts.err = hcutil.SetSOCKS5(opts.hc, string(*s))
}

// 7. 设置unix socket
type unixSocket string

func WithUnixSocket(u string) Option {
return (*unixSocket)(&u)
}

func (u *unixSocket) apply(opts *options) {
if opts.hc == nil {
opts.hc = &http.Client{}
}

opts.err = hcutil.UnixSocket(opts.hc, string(*u))
}
2 changes: 2 additions & 0 deletions hcutil/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
##
本目录存入http.Client的辅助函数
Loading
Loading