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, |
| 44 | ssl bool) (client *thrifttest.ThriftTestClient, err error) { |
| 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: |
| 59 | return nil, fmt.Errorf("Invalid protocol specified %s", protocol) |
| 60 | } |
| 61 | if debugClientProtocol { |
| 62 | protocolFactory = thrift.NewTDebugProtocolFactory(protocolFactory, "client:") |
| 63 | } |
| 64 | var trans thrift.TTransport |
| 65 | if ssl { |
| 66 | trans, err = thrift.NewTSSLSocket(hostPort, &tls.Config{InsecureSkipVerify: true}) |
| 67 | } else { |
| 68 | if domain_socket != "" { |
| 69 | trans, err = thrift.NewTSocket(domain_socket) |
| 70 | } else { |
| 71 | trans, err = thrift.NewTSocket(hostPort) |
| 72 | } |
| 73 | } |
| 74 | if err != nil { |
| 75 | return nil, err |
| 76 | } |
| 77 | switch transport { |
| 78 | case "http": |
claudemiro | f8ca055 | 2016-01-10 23:31:30 -0200 | [diff] [blame^] | 79 | if ssl { |
| 80 | tr := &http.Transport{ |
| 81 | TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, |
| 82 | } |
| 83 | client := &http.Client{Transport: tr} |
| 84 | trans, err = thrift.NewTHttpPostClientWithOptions(fmt.Sprintf("https://%s/", hostPort), thrift.THttpClientOptions{Client: client}) |
| 85 | fmt.Println(hostPort) |
| 86 | } else { |
| 87 | trans, err = thrift.NewTHttpPostClient(fmt.Sprintf("http://%s/", hostPort)) |
| 88 | } |
| 89 | |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 90 | if err != nil { |
| 91 | return nil, err |
| 92 | } |
claudemiro | f8ca055 | 2016-01-10 23:31:30 -0200 | [diff] [blame^] | 93 | |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 94 | case "framed": |
| 95 | trans = thrift.NewTFramedTransport(trans) |
| 96 | case "buffered": |
| 97 | trans = thrift.NewTBufferedTransport(trans, 8192) |
Jens Geyer | c6b991f | 2015-08-07 23:41:09 +0200 | [diff] [blame] | 98 | case "zlib": |
| 99 | trans, err = thrift.NewTZlibTransport(trans, zlib.BestCompression) |
| 100 | if err != nil { |
| 101 | return nil, err |
| 102 | } |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 103 | case "": |
| 104 | trans = trans |
| 105 | default: |
| 106 | return nil, fmt.Errorf("Invalid transport specified %s", transport) |
| 107 | } |
| 108 | |
| 109 | if err = trans.Open(); err != nil { |
| 110 | return nil, err |
| 111 | } |
| 112 | client = thrifttest.NewThriftTestClientFactory(trans, protocolFactory) |
| 113 | return |
| 114 | } |