blob: ed820aeafd4751b169d91dd5ab69ce104a49e9fa [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"
27 "gen/thrifttest"
claudemirof8ca0552016-01-10 23:31:30 -020028 "net/http"
Jens Geyerf4598682014-05-08 23:18:44 +020029 "thrift"
30)
31
32var debugClientProtocol bool
33
34func init() {
35 flag.BoolVar(&debugClientProtocol, "debug_client_protocol", false, "turn client protocol trace on")
36}
37
38func StartClient(
39 host string,
40 port int64,
41 domain_socket string,
42 transport string,
43 protocol string,
D. Can Celasun4f77ab82017-09-21 15:21:00 +020044 ssl bool) (client *thrifttest.ThriftTestClient, trans thrift.TTransport, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +020045
46 hostPort := fmt.Sprintf("%s:%d", host, port)
47
48 var protocolFactory thrift.TProtocolFactory
49 switch protocol {
50 case "compact":
51 protocolFactory = thrift.NewTCompactProtocolFactory()
52 case "simplejson":
53 protocolFactory = thrift.NewTSimpleJSONProtocolFactory()
54 case "json":
55 protocolFactory = thrift.NewTJSONProtocolFactory()
56 case "binary":
57 protocolFactory = thrift.NewTBinaryProtocolFactoryDefault()
Yuxuan 'fishy' Wang4d46c112019-06-07 20:47:18 +080058 case "header":
59 protocolFactory = thrift.NewTHeaderProtocolFactory()
Jens Geyerf4598682014-05-08 23:18:44 +020060 default:
D. Can Celasun4f77ab82017-09-21 15:21:00 +020061 return nil, nil, fmt.Errorf("Invalid protocol specified %s", protocol)
Jens Geyerf4598682014-05-08 23:18:44 +020062 }
63 if debugClientProtocol {
64 protocolFactory = thrift.NewTDebugProtocolFactory(protocolFactory, "client:")
65 }
Jens Geyerf4598682014-05-08 23:18:44 +020066 if ssl {
67 trans, err = thrift.NewTSSLSocket(hostPort, &tls.Config{InsecureSkipVerify: true})
68 } else {
69 if domain_socket != "" {
70 trans, err = thrift.NewTSocket(domain_socket)
71 } else {
72 trans, err = thrift.NewTSocket(hostPort)
73 }
74 }
75 if err != nil {
D. Can Celasun4f77ab82017-09-21 15:21:00 +020076 return nil, nil, err
Jens Geyerf4598682014-05-08 23:18:44 +020077 }
78 switch transport {
79 case "http":
claudemirof8ca0552016-01-10 23:31:30 -020080 if ssl {
81 tr := &http.Transport{
82 TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
83 }
84 client := &http.Client{Transport: tr}
85 trans, err = thrift.NewTHttpPostClientWithOptions(fmt.Sprintf("https://%s/", hostPort), thrift.THttpClientOptions{Client: client})
86 fmt.Println(hostPort)
87 } else {
88 trans, err = thrift.NewTHttpPostClient(fmt.Sprintf("http://%s/", hostPort))
89 }
Jens Geyerf4598682014-05-08 23:18:44 +020090 case "framed":
91 trans = thrift.NewTFramedTransport(trans)
92 case "buffered":
93 trans = thrift.NewTBufferedTransport(trans, 8192)
Jens Geyerc6b991f2015-08-07 23:41:09 +020094 case "zlib":
95 trans, err = thrift.NewTZlibTransport(trans, zlib.BestCompression)
Jens Geyerf4598682014-05-08 23:18:44 +020096 case "":
97 trans = trans
98 default:
D. Can Celasun4f77ab82017-09-21 15:21:00 +020099 return nil, nil, fmt.Errorf("Invalid transport specified %s", transport)
Jens Geyerf4598682014-05-08 23:18:44 +0200100 }
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200101 if err != nil {
102 return nil, nil, err
103 }
Jens Geyerf4598682014-05-08 23:18:44 +0200104 if err = trans.Open(); err != nil {
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200105 return nil, nil, err
Jens Geyerf4598682014-05-08 23:18:44 +0200106 }
D. Can Celasun4f77ab82017-09-21 15:21:00 +0200107 iprot := protocolFactory.GetProtocol(trans)
108 oprot := protocolFactory.GetProtocol(trans)
109 client = thrifttest.NewThriftTestClient(thrift.NewTStandardClient(iprot, oprot))
Jens Geyerf4598682014-05-08 23:18:44 +0200110 return
111}