blob: 15973d82c373f1e31d1e2b2760ce8ac48c18de52 [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 {
65 protocolFactory = thrift.NewTDebugProtocolFactory(protocolFactory, "client:")
66 }
Jens Geyerf4598682014-05-08 23:18:44 +020067 if ssl {
68 trans, err = thrift.NewTSSLSocket(hostPort, &tls.Config{InsecureSkipVerify: true})
69 } else {
70 if domain_socket != "" {
71 trans, err = thrift.NewTSocket(domain_socket)
72 } else {
73 trans, err = thrift.NewTSocket(hostPort)
74 }
75 }
76 if err != nil {
D. Can Celasun4f77ab82017-09-21 15:21:00 +020077 return nil, nil, err
Jens Geyerf4598682014-05-08 23:18:44 +020078 }
79 switch transport {
80 case "http":
claudemirof8ca0552016-01-10 23:31:30 -020081 if ssl {
82 tr := &http.Transport{
83 TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
84 }
85 client := &http.Client{Transport: tr}
86 trans, err = thrift.NewTHttpPostClientWithOptions(fmt.Sprintf("https://%s/", hostPort), thrift.THttpClientOptions{Client: client})
87 fmt.Println(hostPort)
88 } else {
89 trans, err = thrift.NewTHttpPostClient(fmt.Sprintf("http://%s/", hostPort))
90 }
Jens Geyerf4598682014-05-08 23:18:44 +020091 case "framed":
92 trans = thrift.NewTFramedTransport(trans)
93 case "buffered":
94 trans = thrift.NewTBufferedTransport(trans, 8192)
Jens Geyerc6b991f2015-08-07 23:41:09 +020095 case "zlib":
96 trans, err = thrift.NewTZlibTransport(trans, zlib.BestCompression)
Jens Geyerf4598682014-05-08 23:18:44 +020097 case "":
98 trans = trans
99 default:
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200100 return nil, nil, fmt.Errorf("Invalid transport specified %s", transport)
Jens Geyerf4598682014-05-08 23:18:44 +0200101 }
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200102 if err != nil {
103 return nil, nil, err
104 }
Jens Geyerf4598682014-05-08 23:18:44 +0200105 if err = trans.Open(); err != nil {
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200106 return nil, nil, err
Jens Geyerf4598682014-05-08 23:18:44 +0200107 }
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200108 iprot := protocolFactory.GetProtocol(trans)
109 oprot := protocolFactory.GetProtocol(trans)
110 client = thrifttest.NewThriftTestClient(thrift.NewTStandardClient(iprot, oprot))
Jens Geyerf4598682014-05-08 23:18:44 +0200111 return
112}