-
Notifications
You must be signed in to change notification settings - Fork 0
/
default_http_client.go
119 lines (109 loc) · 3.31 KB
/
default_http_client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
* // Copyright 2020 Insolar Network Ltd.
* // All rights reserved.
* // This material is licensed under the Insolar License version 1.0,
* // available at https://github.com/insolar/assured-ledger/blob/master/LICENSE.md.
*/
package loaderbot
import (
"fmt"
"log"
"net/http"
"net/http/cookiejar"
"net/http/httputil"
"strings"
"time"
jsoniter "github.com/json-iterator/go"
)
// NewLoggintHTTPClient creates new client with debug http
func NewLoggingHTTPClient(debug bool, transportTimeout int) *http.Client {
var transport http.RoundTripper
http.DefaultTransport.(*http.Transport).MaxConnsPerHost = 65535
http.DefaultTransport.(*http.Transport).MaxIdleConns = 65535
http.DefaultTransport.(*http.Transport).MaxIdleConnsPerHost = 65535
http.DefaultTransport.(*http.Transport).DisableCompression = true
http.DefaultTransport.(*http.Transport).ResponseHeaderTimeout = time.Duration(transportTimeout) * time.Second
if debug {
transport = &DumpTransport{
http.DefaultTransport,
}
} else {
transport = http.DefaultTransport
}
cookieJar, _ := cookiejar.New(nil)
return &http.Client{
Transport: transport,
Timeout: time.Duration(transportTimeout) * time.Second,
Jar: cookieJar,
}
}
const (
RequestHeader = "========== REQUEST ==========\n%s\n"
RequestHeaderBody = "========== REQUEST ==========\n%s\n%s\n"
ResponseHeaderBody = "========== RESPONSE ==========\n%s\n%s\n"
ResponseHeader = "========== RESPONSE ==========\n%s\n"
HTTPBodyDelimiter = "\r\n\r\n"
)
// DumpTransport log http request/responses, pprint bodies
type DumpTransport struct {
r http.RoundTripper
}
func (d *DumpTransport) RoundTrip(h *http.Request) (*http.Response, error) {
var respString string
var pprintBody string
dump, _ := httputil.DumpRequestOut(h, true)
if bodyIsJson(h.Header) {
req, pprintBody := d.prettyPrintJsonBody(dump)
fmt.Printf(RequestHeaderBody, req, pprintBody)
} else {
fmt.Printf(RequestHeader, dump)
}
resp, err := d.r.RoundTrip(h)
if err != nil {
return nil, err
}
if resp != nil && resp.Body != nil && bodyIsJson(resp.Header) {
defer resp.Body.Close()
dump, _ = httputil.DumpResponse(resp, true)
respString, pprintBody = d.prettyPrintJsonBody(dump)
fmt.Printf(ResponseHeaderBody, respString, pprintBody)
return resp, err
}
dump, _ = httputil.DumpResponse(resp, true)
fmt.Printf(ResponseHeader, dump)
return resp, err
}
// prettyPrintJsonBody returns http format request and pretty printed json body
func (d *DumpTransport) prettyPrintJsonBody(b []byte) (string, string) {
var pprintBody []byte
s := string(b)
sp := strings.Split(s, HTTPBodyDelimiter)
if len(sp) == 2 {
body := sp[1]
if strings.HasPrefix(body, "[") {
var objmap []*jsoniter.RawMessage
err := jsoniter.Unmarshal([]byte(body), &objmap)
if err != nil {
log.Fatal(err)
}
pprintBody, err = jsoniter.MarshalIndent(objmap, "", " ")
if err != nil {
log.Fatal(err)
}
return sp[0], string(pprintBody)
}
var objmap map[string]*jsoniter.RawMessage
err := jsoniter.Unmarshal([]byte(body), &objmap)
if err != nil {
log.Fatal(err)
}
pprintBody, err = jsoniter.MarshalIndent(objmap, "", " ")
if err != nil {
log.Fatal(err)
}
}
return sp[0], string(pprintBody)
}
func bodyIsJson(h http.Header) bool {
return strings.Contains(h.Get("content-type"), "application/json")
}