]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/lib/go/thrift/http_client.go
buildsys: switch source download to quincy
[ceph.git] / ceph / src / jaegertracing / thrift / lib / go / thrift / http_client.go
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20 package thrift
21
22 import (
23 "bytes"
24 "context"
25 "io"
26 "io/ioutil"
27 "net/http"
28 "net/url"
29 "strconv"
30 )
31
32 // Default to using the shared http client. Library users are
33 // free to change this global client or specify one through
34 // THttpClientOptions.
35 var DefaultHttpClient *http.Client = http.DefaultClient
36
37 type THttpClient struct {
38 client *http.Client
39 response *http.Response
40 url *url.URL
41 requestBuffer *bytes.Buffer
42 header http.Header
43 nsecConnectTimeout int64
44 nsecReadTimeout int64
45 }
46
47 type THttpClientTransportFactory struct {
48 options THttpClientOptions
49 url string
50 }
51
52 func (p *THttpClientTransportFactory) GetTransport(trans TTransport) (TTransport, error) {
53 if trans != nil {
54 t, ok := trans.(*THttpClient)
55 if ok && t.url != nil {
56 return NewTHttpClientWithOptions(t.url.String(), p.options)
57 }
58 }
59 return NewTHttpClientWithOptions(p.url, p.options)
60 }
61
62 type THttpClientOptions struct {
63 // If nil, DefaultHttpClient is used
64 Client *http.Client
65 }
66
67 func NewTHttpClientTransportFactory(url string) *THttpClientTransportFactory {
68 return NewTHttpClientTransportFactoryWithOptions(url, THttpClientOptions{})
69 }
70
71 func NewTHttpClientTransportFactoryWithOptions(url string, options THttpClientOptions) *THttpClientTransportFactory {
72 return &THttpClientTransportFactory{url: url, options: options}
73 }
74
75 func NewTHttpClientWithOptions(urlstr string, options THttpClientOptions) (TTransport, error) {
76 parsedURL, err := url.Parse(urlstr)
77 if err != nil {
78 return nil, err
79 }
80 buf := make([]byte, 0, 1024)
81 client := options.Client
82 if client == nil {
83 client = DefaultHttpClient
84 }
85 httpHeader := map[string][]string{"Content-Type": {"application/x-thrift"}}
86 return &THttpClient{client: client, url: parsedURL, requestBuffer: bytes.NewBuffer(buf), header: httpHeader}, nil
87 }
88
89 func NewTHttpClient(urlstr string) (TTransport, error) {
90 return NewTHttpClientWithOptions(urlstr, THttpClientOptions{})
91 }
92
93 // Set the HTTP Header for this specific Thrift Transport
94 // It is important that you first assert the TTransport as a THttpClient type
95 // like so:
96 //
97 // httpTrans := trans.(THttpClient)
98 // httpTrans.SetHeader("User-Agent","Thrift Client 1.0")
99 func (p *THttpClient) SetHeader(key string, value string) {
100 p.header.Add(key, value)
101 }
102
103 // Get the HTTP Header represented by the supplied Header Key for this specific Thrift Transport
104 // It is important that you first assert the TTransport as a THttpClient type
105 // like so:
106 //
107 // httpTrans := trans.(THttpClient)
108 // hdrValue := httpTrans.GetHeader("User-Agent")
109 func (p *THttpClient) GetHeader(key string) string {
110 return p.header.Get(key)
111 }
112
113 // Deletes the HTTP Header given a Header Key for this specific Thrift Transport
114 // It is important that you first assert the TTransport as a THttpClient type
115 // like so:
116 //
117 // httpTrans := trans.(THttpClient)
118 // httpTrans.DelHeader("User-Agent")
119 func (p *THttpClient) DelHeader(key string) {
120 p.header.Del(key)
121 }
122
123 func (p *THttpClient) Open() error {
124 // do nothing
125 return nil
126 }
127
128 func (p *THttpClient) IsOpen() bool {
129 return p.response != nil || p.requestBuffer != nil
130 }
131
132 func (p *THttpClient) closeResponse() error {
133 var err error
134 if p.response != nil && p.response.Body != nil {
135 // The docs specify that if keepalive is enabled and the response body is not
136 // read to completion the connection will never be returned to the pool and
137 // reused. Errors are being ignored here because if the connection is invalid
138 // and this fails for some reason, the Close() method will do any remaining
139 // cleanup.
140 io.Copy(ioutil.Discard, p.response.Body)
141
142 err = p.response.Body.Close()
143 }
144
145 p.response = nil
146 return err
147 }
148
149 func (p *THttpClient) Close() error {
150 if p.requestBuffer != nil {
151 p.requestBuffer.Reset()
152 p.requestBuffer = nil
153 }
154 return p.closeResponse()
155 }
156
157 func (p *THttpClient) Read(buf []byte) (int, error) {
158 if p.response == nil {
159 return 0, NewTTransportException(NOT_OPEN, "Response buffer is empty, no request.")
160 }
161 n, err := p.response.Body.Read(buf)
162 if n > 0 && (err == nil || err == io.EOF) {
163 return n, nil
164 }
165 return n, NewTTransportExceptionFromError(err)
166 }
167
168 func (p *THttpClient) ReadByte() (c byte, err error) {
169 return readByte(p.response.Body)
170 }
171
172 func (p *THttpClient) Write(buf []byte) (int, error) {
173 n, err := p.requestBuffer.Write(buf)
174 return n, err
175 }
176
177 func (p *THttpClient) WriteByte(c byte) error {
178 return p.requestBuffer.WriteByte(c)
179 }
180
181 func (p *THttpClient) WriteString(s string) (n int, err error) {
182 return p.requestBuffer.WriteString(s)
183 }
184
185 func (p *THttpClient) Flush(ctx context.Context) error {
186 // Close any previous response body to avoid leaking connections.
187 p.closeResponse()
188
189 req, err := http.NewRequest("POST", p.url.String(), p.requestBuffer)
190 if err != nil {
191 return NewTTransportExceptionFromError(err)
192 }
193 req.Header = p.header
194 if ctx != nil {
195 req = req.WithContext(ctx)
196 }
197 response, err := p.client.Do(req)
198 if err != nil {
199 return NewTTransportExceptionFromError(err)
200 }
201 if response.StatusCode != http.StatusOK {
202 // Close the response to avoid leaking file descriptors. closeResponse does
203 // more than just call Close(), so temporarily assign it and reuse the logic.
204 p.response = response
205 p.closeResponse()
206
207 // TODO(pomack) log bad response
208 return NewTTransportException(UNKNOWN_TRANSPORT_EXCEPTION, "HTTP Response code: "+strconv.Itoa(response.StatusCode))
209 }
210 p.response = response
211 return nil
212 }
213
214 func (p *THttpClient) RemainingBytes() (num_bytes uint64) {
215 len := p.response.ContentLength
216 if len >= 0 {
217 return uint64(len)
218 }
219
220 const maxSize = ^uint64(0)
221 return maxSize // the thruth is, we just don't know unless framed is used
222 }
223
224 // Deprecated: Use NewTHttpClientTransportFactory instead.
225 func NewTHttpPostClientTransportFactory(url string) *THttpClientTransportFactory {
226 return NewTHttpClientTransportFactoryWithOptions(url, THttpClientOptions{})
227 }
228
229 // Deprecated: Use NewTHttpClientTransportFactoryWithOptions instead.
230 func NewTHttpPostClientTransportFactoryWithOptions(url string, options THttpClientOptions) *THttpClientTransportFactory {
231 return NewTHttpClientTransportFactoryWithOptions(url, options)
232 }
233
234 // Deprecated: Use NewTHttpClientWithOptions instead.
235 func NewTHttpPostClientWithOptions(urlstr string, options THttpClientOptions) (TTransport, error) {
236 return NewTHttpClientWithOptions(urlstr, options)
237 }
238
239 // Deprecated: Use NewTHttpClient instead.
240 func NewTHttpPostClient(urlstr string) (TTransport, error) {
241 return NewTHttpClientWithOptions(urlstr, THttpClientOptions{})
242 }