blob: 201503538cc435cc6f778dcd9166add0e8aca4af [file] [log] [blame]
Jens Geyerf4598682014-05-08 23:18:44 +02001/*
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
20package common
21
22import (
Jens Geyerc6b991f2015-08-07 23:41:09 +020023 "compress/zlib"
Jens Geyerf4598682014-05-08 23:18:44 +020024 "crypto/tls"
25 "flag"
26 "fmt"
claudemirof8ca0552016-01-10 23:31:30 -020027 "net/http"
Yuxuan 'fishy' Wangb71f11e2021-03-22 15:01:00 -070028
29 "github.com/apache/thrift/lib/go/thrift"
30 "github.com/apache/thrift/test/go/src/gen/thrifttest"
Jens Geyerf4598682014-05-08 23:18:44 +020031)
32
33var debugClientProtocol bool
34
35func init() {
36 flag.BoolVar(&debugClientProtocol, "debug_client_protocol", false, "turn client protocol trace on")
37}
38
39func StartClient(
40 host string,
41 port int64,
42 domain_socket string,
43 transport string,
44 protocol string,
D. Can Celasun4f77ab82017-09-21 15:21:00 +020045 ssl bool) (client *thrifttest.ThriftTestClient, trans thrift.TTransport, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +020046
47 hostPort := fmt.Sprintf("%s:%d", host, port)
48
49 var protocolFactory thrift.TProtocolFactory
50 switch protocol {
51 case "compact":
52 protocolFactory = thrift.NewTCompactProtocolFactory()
53 case "simplejson":
54 protocolFactory = thrift.NewTSimpleJSONProtocolFactory()
55 case "json":
56 protocolFactory = thrift.NewTJSONProtocolFactory()
57 case "binary":
58 protocolFactory = thrift.NewTBinaryProtocolFactoryDefault()
Yuxuan 'fishy' Wang4d46c112019-06-07 20:47:18 +080059 case "header":
60 protocolFactory = thrift.NewTHeaderProtocolFactory()
Jens Geyerf4598682014-05-08 23:18:44 +020061 default:
D. Can Celasun4f77ab82017-09-21 15:21:00 +020062 return nil, nil, fmt.Errorf("Invalid protocol specified %s", protocol)
Jens Geyerf4598682014-05-08 23:18:44 +020063 }
64 if debugClientProtocol {
Yuxuan 'fishy' Wangfb539ae2021-08-09 14:27:48 -070065 protocolFactory = thrift.NewTDebugProtocolFactoryWithLogger(protocolFactory, "client:", thrift.StdLogger(nil))
Jens Geyerf4598682014-05-08 23:18:44 +020066 }
Jens Geyerf4598682014-05-08 23:18:44 +020067 if ssl {
Yuxuan 'fishy' Wangfb539ae2021-08-09 14:27:48 -070068 trans, err = thrift.NewTSSLSocketConf(hostPort, &thrift.TConfiguration{
69 TLSConfig: &tls.Config{InsecureSkipVerify: true},
70 })
Jens Geyerf4598682014-05-08 23:18:44 +020071 } else {
72 if domain_socket != "" {
Yuxuan 'fishy' Wangfb539ae2021-08-09 14:27:48 -070073 trans = thrift.NewTSocketConf(domain_socket, nil)
Jens Geyerf4598682014-05-08 23:18:44 +020074 } else {
Yuxuan 'fishy' Wangfb539ae2021-08-09 14:27:48 -070075 trans = thrift.NewTSocketConf(hostPort, nil)
Jens Geyerf4598682014-05-08 23:18:44 +020076 }
77 }
78 if err != nil {
D. Can Celasun4f77ab82017-09-21 15:21:00 +020079 return nil, nil, err
Jens Geyerf4598682014-05-08 23:18:44 +020080 }
81 switch transport {
82 case "http":
claudemirof8ca0552016-01-10 23:31:30 -020083 if ssl {
84 tr := &http.Transport{
85 TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
86 }
87 client := &http.Client{Transport: tr}
88 trans, err = thrift.NewTHttpPostClientWithOptions(fmt.Sprintf("https://%s/", hostPort), thrift.THttpClientOptions{Client: client})
89 fmt.Println(hostPort)
90 } else {
91 trans, err = thrift.NewTHttpPostClient(fmt.Sprintf("http://%s/", hostPort))
92 }
Jens Geyerf4598682014-05-08 23:18:44 +020093 case "framed":
94 trans = thrift.NewTFramedTransport(trans)
95 case "buffered":
96 trans = thrift.NewTBufferedTransport(trans, 8192)
Jens Geyerc6b991f2015-08-07 23:41:09 +020097 case "zlib":
98 trans, err = thrift.NewTZlibTransport(trans, zlib.BestCompression)
Jens Geyerf4598682014-05-08 23:18:44 +020099 case "":
100 trans = trans
101 default:
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200102 return nil, nil, fmt.Errorf("Invalid transport specified %s", transport)
Jens Geyerf4598682014-05-08 23:18:44 +0200103 }
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200104 if err != nil {
105 return nil, nil, err
106 }
Jens Geyerf4598682014-05-08 23:18:44 +0200107 if err = trans.Open(); err != nil {
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200108 return nil, nil, err
Jens Geyerf4598682014-05-08 23:18:44 +0200109 }
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200110 iprot := protocolFactory.GetProtocol(trans)
111 oprot := protocolFactory.GetProtocol(trans)
112 client = thrifttest.NewThriftTestClient(thrift.NewTStandardClient(iprot, oprot))
Jens Geyerf4598682014-05-08 23:18:44 +0200113 return
114}