forked from vesoft-inc/fbthrift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_client.go
268 lines (231 loc) · 7.29 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package thrift
import (
"bytes"
"io"
"net/http"
"net/url"
"strconv"
)
// Default to using the shared http client. Library users are
// free to change this global client or specify one through
// HTTPClientOptions.
var DefaultHTTPClient *http.Client = http.DefaultClient
// Default to 16MB response buffer limit.
// If the response is larger than this, the buffer will be renewed.
var DefaultHTTPResponseBufferLimit int64 = 16 * 1024 * 1024
type HTTPClient struct {
client *http.Client
response *http.Response
url *url.URL
requestBuffer *bytes.Buffer
responseBuffer bytes.Buffer
responseBufferLimit int64
header http.Header
nsecConnectTimeout int64
nsecReadTimeout int64
}
type HTTPClientTransportFactory struct {
options HTTPClientOptions
url string
isPost bool
}
func (p *HTTPClientTransportFactory) GetTransport(trans Transport) Transport {
if trans != nil {
t, ok := trans.(*HTTPClient)
if ok && t.url != nil {
if t.requestBuffer != nil {
t2, _ := NewHTTPPostClientWithOptions(t.url.String(), p.options)
return t2
}
t2, _ := NewHTTPClientWithOptions(t.url.String(), p.options)
return t2
}
}
if p.isPost {
s, _ := NewHTTPPostClientWithOptions(p.url, p.options)
return s
}
s, _ := NewHTTPClientWithOptions(p.url, p.options)
return s
}
type HTTPClientOptions struct {
// If nil, DefaultHTTPClient is used
Client *http.Client
}
func NewHTTPClientTransportFactory(url string) *HTTPClientTransportFactory {
return NewHTTPClientTransportFactoryWithOptions(url, HTTPClientOptions{})
}
func NewHTTPClientTransportFactoryWithOptions(url string, options HTTPClientOptions) *HTTPClientTransportFactory {
return &HTTPClientTransportFactory{url: url, isPost: false, options: options}
}
func NewHTTPPostClientTransportFactory(url string) *HTTPClientTransportFactory {
return NewHTTPPostClientTransportFactoryWithOptions(url, HTTPClientOptions{})
}
func NewHTTPPostClientTransportFactoryWithOptions(url string, options HTTPClientOptions) *HTTPClientTransportFactory {
return &HTTPClientTransportFactory{url: url, isPost: true, options: options}
}
func NewHTTPClientWithOptions(urlstr string, options HTTPClientOptions) (Transport, error) {
parsedURL, err := url.Parse(urlstr)
if err != nil {
return nil, err
}
response, err := http.Get(urlstr)
if err != nil {
return nil, err
}
client := options.Client
if client == nil {
client = DefaultHTTPClient
}
return &HTTPClient{
client: client,
response: response,
url: parsedURL,
responseBufferLimit: DefaultHTTPResponseBufferLimit,
}, nil
}
func NewHTTPClient(urlstr string) (Transport, error) {
return NewHTTPClientWithOptions(urlstr, HTTPClientOptions{})
}
func NewHTTPPostClientWithOptions(urlstr string, options HTTPClientOptions) (Transport, error) {
parsedURL, err := url.Parse(urlstr)
if err != nil {
return nil, err
}
buf := make([]byte, 0, 1024)
client := options.Client
if client == nil {
client = DefaultHTTPClient
}
return &HTTPClient{
client: client, url: parsedURL,
requestBuffer: bytes.NewBuffer(buf),
header: http.Header{},
responseBufferLimit: DefaultHTTPResponseBufferLimit,
}, nil
}
func NewHTTPPostClient(urlstr string) (Transport, error) {
return NewHTTPPostClientWithOptions(urlstr, HTTPClientOptions{})
}
// Set the HTTP Header for this specific Thrift Transport
// It is important that you first assert the Transport as a HTTPClient type
// like so:
//
// httpTrans := trans.(HTTPClient)
// httpTrans.SetHeader("User-Agent","Thrift Client 1.0")
func (p *HTTPClient) SetHeader(key string, value string) {
p.header.Add(key, value)
}
// Get the HTTP Header represented by the supplied Header Key for this specific Thrift Transport
// It is important that you first assert the Transport as a HTTPClient type
// like so:
//
// httpTrans := trans.(HTTPClient)
// hdrValue := httpTrans.GetHeader("User-Agent")
func (p *HTTPClient) GetHeader(key string) string {
return p.header.Get(key)
}
// Deletes the HTTP Header given a Header Key for this specific Thrift Transport
// It is important that you first assert the Transport as a HTTPClient type
// like so:
//
// httpTrans := trans.(HTTPClient)
// httpTrans.DelHeader("User-Agent")
func (p *HTTPClient) DelHeader(key string) {
p.header.Del(key)
}
func (p *HTTPClient) Open() error {
// do nothing
return nil
}
func (p *HTTPClient) IsOpen() bool {
return p.response != nil || p.requestBuffer != nil
}
func (p *HTTPClient) closeResponse() error {
p.response = nil
p.responseBuffer.Reset()
if p.responseBuffer.Cap() > int(p.responseBufferLimit) {
p.responseBuffer = bytes.Buffer{}
}
return nil
}
func (p *HTTPClient) Close() error {
if p.requestBuffer != nil {
p.requestBuffer.Reset()
p.requestBuffer = nil
}
return p.closeResponse()
}
func (p *HTTPClient) Read(buf []byte) (int, error) {
if p.response == nil {
return 0, NewTransportException(NOT_OPEN, "Response buffer is empty, no request.")
}
n, err := p.responseBuffer.Read(buf)
if n > 0 && (err == nil || err == io.EOF) {
return n, nil
}
return n, NewTransportExceptionFromError(err)
}
func (p *HTTPClient) ReadByte() (c byte, err error) {
return readByte(&p.responseBuffer)
}
func (p *HTTPClient) Write(buf []byte) (int, error) {
n, err := p.requestBuffer.Write(buf)
return n, err
}
func (p *HTTPClient) WriteByte(c byte) error {
return p.requestBuffer.WriteByte(c)
}
func (p *HTTPClient) WriteString(s string) (n int, err error) {
return p.requestBuffer.WriteString(s)
}
func (p *HTTPClient) Flush() error {
// Close any previous response body to avoid leaking connections.
p.closeResponse()
req, err := http.NewRequest("POST", p.url.String(), p.requestBuffer)
if err != nil {
return NewTransportExceptionFromError(err)
}
p.header.Set("Content-Type", "application/x-thrift")
req.Header = p.header
response, err := p.client.Do(req)
if err != nil {
return NewTransportExceptionFromError(err)
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
// Close the response to avoid leaking file descriptors. closeResponse does
// more than just call Close(), so temporarily assign it and reuse the logic.
p.response = response
p.closeResponse()
// TODO(pomack) log bad response
return NewTransportException(UNKNOWN_TRANSPORT_EXCEPTION, "HTTP Response code: "+strconv.Itoa(response.StatusCode))
}
_, err = io.Copy(&p.responseBuffer, response.Body)
if err != nil {
return NewTransportExceptionFromError(err)
}
p.response = response
return nil
}
func (p *HTTPClient) RemainingBytes() (num_bytes uint64) {
return uint64(p.responseBuffer.Len())
}
func (p *HTTPClient) SetResponseBufferLimit(limit int64) {
p.responseBufferLimit = limit
}