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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
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
37 type THttpClient struct {
39 response *http.Response
41 requestBuffer *bytes.Buffer
43 nsecConnectTimeout int64
47 type THttpClientTransportFactory struct {
48 options THttpClientOptions
52 func (p *THttpClientTransportFactory) GetTransport(trans TTransport) (TTransport, error) {
54 t, ok := trans.(*THttpClient)
55 if ok && t.url != nil {
56 return NewTHttpClientWithOptions(t.url.String(), p.options)
59 return NewTHttpClientWithOptions(p.url, p.options)
62 type THttpClientOptions struct {
63 // If nil, DefaultHttpClient is used
67 func NewTHttpClientTransportFactory(url string) *THttpClientTransportFactory {
68 return NewTHttpClientTransportFactoryWithOptions(url, THttpClientOptions{})
71 func NewTHttpClientTransportFactoryWithOptions(url string, options THttpClientOptions) *THttpClientTransportFactory {
72 return &THttpClientTransportFactory{url: url, options: options}
75 func NewTHttpClientWithOptions(urlstr string, options THttpClientOptions) (TTransport, error) {
76 parsedURL, err := url.Parse(urlstr)
80 buf := make([]byte, 0, 1024)
81 client := options.Client
83 client = DefaultHttpClient
85 httpHeader := map[string][]string{"Content-Type": {"application/x-thrift"}}
86 return &THttpClient{client: client, url: parsedURL, requestBuffer: bytes.NewBuffer(buf), header: httpHeader}, nil
89 func NewTHttpClient(urlstr string) (TTransport, error) {
90 return NewTHttpClientWithOptions(urlstr, THttpClientOptions{})
93 // Set the HTTP Header for this specific Thrift Transport
94 // It is important that you first assert the TTransport as a THttpClient type
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)
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
107 // httpTrans := trans.(THttpClient)
108 // hdrValue := httpTrans.GetHeader("User-Agent")
109 func (p *THttpClient) GetHeader(key string) string {
110 return p.header.Get(key)
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
117 // httpTrans := trans.(THttpClient)
118 // httpTrans.DelHeader("User-Agent")
119 func (p *THttpClient) DelHeader(key string) {
123 func (p *THttpClient) Open() error {
128 func (p *THttpClient) IsOpen() bool {
129 return p.response != nil || p.requestBuffer != nil
132 func (p *THttpClient) closeResponse() 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
140 io.Copy(ioutil.Discard, p.response.Body)
142 err = p.response.Body.Close()
149 func (p *THttpClient) Close() error {
150 if p.requestBuffer != nil {
151 p.requestBuffer.Reset()
152 p.requestBuffer = nil
154 return p.closeResponse()
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.")
161 n, err := p.response.Body.Read(buf)
162 if n > 0 && (err == nil || err == io.EOF) {
165 return n, NewTTransportExceptionFromError(err)
168 func (p *THttpClient) ReadByte() (c byte, err error) {
169 return readByte(p.response.Body)
172 func (p *THttpClient) Write(buf []byte) (int, error) {
173 n, err := p.requestBuffer.Write(buf)
177 func (p *THttpClient) WriteByte(c byte) error {
178 return p.requestBuffer.WriteByte(c)
181 func (p *THttpClient) WriteString(s string) (n int, err error) {
182 return p.requestBuffer.WriteString(s)
185 func (p *THttpClient) Flush(ctx context.Context) error {
186 // Close any previous response body to avoid leaking connections.
189 req, err := http.NewRequest("POST", p.url.String(), p.requestBuffer)
191 return NewTTransportExceptionFromError(err)
193 req.Header = p.header
195 req = req.WithContext(ctx)
197 response, err := p.client.Do(req)
199 return NewTTransportExceptionFromError(err)
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
207 // TODO(pomack) log bad response
208 return NewTTransportException(UNKNOWN_TRANSPORT_EXCEPTION, "HTTP Response code: "+strconv.Itoa(response.StatusCode))
210 p.response = response
214 func (p *THttpClient) RemainingBytes() (num_bytes uint64) {
215 len := p.response.ContentLength
220 const maxSize = ^uint64(0)
221 return maxSize // the thruth is, we just don't know unless framed is used
224 // Deprecated: Use NewTHttpClientTransportFactory instead.
225 func NewTHttpPostClientTransportFactory(url string) *THttpClientTransportFactory {
226 return NewTHttpClientTransportFactoryWithOptions(url, THttpClientOptions{})
229 // Deprecated: Use NewTHttpClientTransportFactoryWithOptions instead.
230 func NewTHttpPostClientTransportFactoryWithOptions(url string, options THttpClientOptions) *THttpClientTransportFactory {
231 return NewTHttpClientTransportFactoryWithOptions(url, options)
234 // Deprecated: Use NewTHttpClientWithOptions instead.
235 func NewTHttpPostClientWithOptions(urlstr string, options THttpClientOptions) (TTransport, error) {
236 return NewTHttpClientWithOptions(urlstr, options)
239 // Deprecated: Use NewTHttpClient instead.
240 func NewTHttpPostClient(urlstr string) (TTransport, error) {
241 return NewTHttpClientWithOptions(urlstr, THttpClientOptions{})