Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
| 20 | package common |
| 21 | |
| 22 | import ( |
Jens Geyer | c6b991f | 2015-08-07 23:41:09 +0200 | [diff] [blame] | 23 | "compress/zlib" |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 24 | "crypto/tls" |
| 25 | "flag" |
| 26 | "fmt" |
| 27 | "gen/thrifttest" |
claudemiro | f8ca055 | 2016-01-10 23:31:30 -0200 | [diff] [blame] | 28 | "net/http" |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 29 | "thrift" |
| 30 | ) |
| 31 | |
| 32 | var debugClientProtocol bool |
| 33 | |
| 34 | func init() { |
| 35 | flag.BoolVar(&debugClientProtocol, "debug_client_protocol", false, "turn client protocol trace on") |
| 36 | } |
| 37 | |
| 38 | func StartClient( |
| 39 | host string, |
| 40 | port int64, |
| 41 | domain_socket string, |
| 42 | transport string, |
| 43 | protocol string, |
D. Can Celasun | 4f77ab8 | 2017-09-21 15:21:00 +0200 | [diff] [blame^] | 44 | ssl bool) (client *thrifttest.ThriftTestClient, trans thrift.TTransport, err error) { |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 45 | |
| 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() |
| 58 | default: |
D. Can Celasun | 4f77ab8 | 2017-09-21 15:21:00 +0200 | [diff] [blame^] | 59 | return nil, nil, fmt.Errorf("Invalid protocol specified %s", protocol) |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 60 | } |
| 61 | if debugClientProtocol { |
| 62 | protocolFactory = thrift.NewTDebugProtocolFactory(protocolFactory, "client:") |
| 63 | } |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 64 | if ssl { |
| 65 | trans, err = thrift.NewTSSLSocket(hostPort, &tls.Config{InsecureSkipVerify: true}) |
| 66 | } else { |
| 67 | if domain_socket != "" { |
| 68 | trans, err = thrift.NewTSocket(domain_socket) |
| 69 | } else { |
| 70 | trans, err = thrift.NewTSocket(hostPort) |
| 71 | } |
| 72 | } |
| 73 | if err != nil { |
D. Can Celasun | 4f77ab8 | 2017-09-21 15:21:00 +0200 | [diff] [blame^] | 74 | return nil, nil, err |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 75 | } |
| 76 | switch transport { |
| 77 | case "http": |
claudemiro | f8ca055 | 2016-01-10 23:31:30 -0200 | [diff] [blame] | 78 | if ssl { |
| 79 | tr := &http.Transport{ |
| 80 | TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, |
| 81 | } |
| 82 | client := &http.Client{Transport: tr} |
| 83 | trans, err = thrift.NewTHttpPostClientWithOptions(fmt.Sprintf("https://%s/", hostPort), thrift.THttpClientOptions{Client: client}) |
| 84 | fmt.Println(hostPort) |
| 85 | } else { |
| 86 | trans, err = thrift.NewTHttpPostClient(fmt.Sprintf("http://%s/", hostPort)) |
| 87 | } |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 88 | case "framed": |
| 89 | trans = thrift.NewTFramedTransport(trans) |
| 90 | case "buffered": |
| 91 | trans = thrift.NewTBufferedTransport(trans, 8192) |
Jens Geyer | c6b991f | 2015-08-07 23:41:09 +0200 | [diff] [blame] | 92 | case "zlib": |
| 93 | trans, err = thrift.NewTZlibTransport(trans, zlib.BestCompression) |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 94 | case "": |
| 95 | trans = trans |
| 96 | default: |
D. Can Celasun | 4f77ab8 | 2017-09-21 15:21:00 +0200 | [diff] [blame^] | 97 | return nil, nil, fmt.Errorf("Invalid transport specified %s", transport) |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 98 | } |
D. Can Celasun | 4f77ab8 | 2017-09-21 15:21:00 +0200 | [diff] [blame^] | 99 | if err != nil { |
| 100 | return nil, nil, err |
| 101 | } |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 102 | if err = trans.Open(); err != nil { |
D. Can Celasun | 4f77ab8 | 2017-09-21 15:21:00 +0200 | [diff] [blame^] | 103 | return nil, nil, err |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 104 | } |
D. Can Celasun | 4f77ab8 | 2017-09-21 15:21:00 +0200 | [diff] [blame^] | 105 | iprot := protocolFactory.GetProtocol(trans) |
| 106 | oprot := protocolFactory.GetProtocol(trans) |
| 107 | client = thrifttest.NewThriftTestClient(thrift.NewTStandardClient(iprot, oprot)) |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 108 | return |
| 109 | } |