-
Notifications
You must be signed in to change notification settings - Fork 606
/
http_client.go
204 lines (175 loc) · 5.43 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
/*
* 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
type HTTPClient struct {
client *http.Client
response *http.Response
url *url.URL
requestBuffer *bytes.Buffer
responseBuffer bytes.Buffer
header http.Header
nsecConnectTimeout int64
nsecReadTimeout int64
}
type httpClientOptions struct {
// If nil, DefaultHTTPClient is used
Client *http.Client
}
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}, 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{}}, 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()
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())
}