forked from hprose/hprose-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_client.go
145 lines (123 loc) · 3.97 KB
/
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**********************************************************\
| |
| hprose |
| |
| Official WebSite: http://www.hprose.com/ |
| http://www.hprose.org/ |
| |
\**********************************************************/
/**********************************************************\
* *
* hprose/http_client.go *
* *
* hprose http client for Go. *
* *
* LastModified: Feb 8, 2015 *
* Author: Ma Bingyao <andot@hprose.com> *
* *
\**********************************************************/
package hprose
import (
"crypto/tls"
"io"
"io/ioutil"
"net/http"
"net/http/cookiejar"
"net/url"
)
var cookieJar, _ = cookiejar.New(nil)
var DisableGlobalCookie = false
type HttpClient struct {
*BaseClient
}
type HttpTransporter struct {
*http.Client
}
func NewHttpClient(uri string) Client {
client := &HttpClient{NewBaseClient(newHttpTransporter())}
client.Client = client
client.SetUri(uri)
client.SetKeepAlive(true)
return client
}
func (client *HttpClient) Close() {}
func (client *HttpClient) SetUri(uri string) {
if u, err := url.Parse(uri); err == nil {
if u.Scheme != "http" && u.Scheme != "https" {
panic("This client desn't support " + u.Scheme + " scheme.")
}
if u.Scheme == "https" {
client.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
}
}
client.BaseClient.SetUri(uri)
}
func (client *HttpClient) Http() *http.Client {
return client.Transporter.(*HttpTransporter).Client
}
func (client *HttpClient) transport() *http.Transport {
return client.Http().Transport.(*http.Transport)
}
func (client *HttpClient) TLSClientConfig() *tls.Config {
return client.transport().TLSClientConfig
}
func (client *HttpClient) SetTLSClientConfig(config *tls.Config) {
client.transport().TLSClientConfig = config
}
func (client *HttpClient) KeepAlive() bool {
return !client.transport().DisableKeepAlives
}
func (client *HttpClient) SetKeepAlive(enable bool) {
client.transport().DisableKeepAlives = !enable
}
func (client *HttpClient) Compression() bool {
return !client.transport().DisableCompression
}
func (client *HttpClient) SetCompression(enable bool) {
client.transport().DisableCompression = !enable
}
func (client *HttpClient) MaxIdleConnsPerHost() int {
return client.transport().MaxIdleConnsPerHost
}
func (client *HttpClient) SetMaxIdleConnsPerHost(value int) {
client.transport().MaxIdleConnsPerHost = value
}
func newHttpTransporter() *HttpTransporter {
tr := &http.Transport{
DisableCompression: true,
DisableKeepAlives: false,
MaxIdleConnsPerHost: 4}
jar := cookieJar
if DisableGlobalCookie {
jar, _ = cookiejar.New(nil)
}
return &HttpTransporter{&http.Client{Jar: jar, Transport: tr}}
}
func (h *HttpTransporter) readAll(response *http.Response) (data []byte, err error) {
if response.ContentLength > 0 {
data = make([]byte, response.ContentLength)
_, err = io.ReadFull(response.Body, data)
return data, err
}
if response.ContentLength < 0 {
return ioutil.ReadAll(response.Body)
}
return make([]byte, 0), nil
}
func (h *HttpTransporter) SendAndReceive(uri string, data []byte) ([]byte, error) {
req, err := http.NewRequest("POST", uri, NewBytesReader(data))
if err != nil {
return nil, err
}
req.ContentLength = int64(len(data))
req.Header.Set("Content-Type", "application/hprose")
resp, err := h.Do(req)
if err != nil {
return nil, err
}
data, err = ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return data, resp.Body.Close()
}