]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/lib/rb/lib/thrift/transport/http_client_transport.rb
buildsys: switch source download to quincy
[ceph.git] / ceph / src / jaegertracing / thrift / lib / rb / lib / thrift / transport / http_client_transport.rb
1 # encoding: ascii-8bit
2 #
3 # Licensed to the Apache Software Foundation (ASF) under one
4 # or more contributor license agreements. See the NOTICE file
5 # distributed with this work for additional information
6 # regarding copyright ownership. The ASF licenses this file
7 # to you under the Apache License, Version 2.0 (the
8 # "License"); you may not use this file except in compliance
9 # with the License. You may obtain a copy of the License at
10 #
11 # http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing,
14 # software distributed under the License is distributed on an
15 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 # KIND, either express or implied. See the License for the
17 # specific language governing permissions and limitations
18 # under the License.
19 #
20
21 require 'net/http'
22 require 'net/https'
23 require 'openssl'
24 require 'uri'
25 require 'stringio'
26
27 module Thrift
28 class HTTPClientTransport < BaseTransport
29
30 def initialize(url, opts = {})
31 @url = URI url
32 @headers = {'Content-Type' => 'application/x-thrift'}
33 @outbuf = Bytes.empty_byte_buffer
34 @ssl_verify_mode = opts.fetch(:ssl_verify_mode, OpenSSL::SSL::VERIFY_PEER)
35 end
36
37 def open?; true end
38 def read(sz); @inbuf.read sz end
39 def write(buf); @outbuf << Bytes.force_binary_encoding(buf) end
40
41 def add_headers(headers)
42 @headers = @headers.merge(headers)
43 end
44
45 def flush
46 http = Net::HTTP.new @url.host, @url.port
47 http.use_ssl = @url.scheme == 'https'
48 http.verify_mode = @ssl_verify_mode if @url.scheme == 'https'
49 resp = http.post(@url.request_uri, @outbuf, @headers)
50 data = resp.body
51 data = Bytes.force_binary_encoding(data)
52 @inbuf = StringIO.new data
53 ensure
54 @outbuf = Bytes.empty_byte_buffer
55 end
56
57 def to_s
58 "@{self.url}"
59 end
60 end
61 end