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" |
| 28 | "thrift" |
| 29 | ) |
| 30 | |
| 31 | var ( |
| 32 | debugServerProtocol bool |
| 33 | certPath string |
| 34 | ) |
| 35 | |
| 36 | func init() { |
| 37 | flag.BoolVar(&debugServerProtocol, "debug_server_protocol", false, "turn server protocol trace on") |
| 38 | } |
| 39 | |
claudemiro | f8ca055 | 2016-01-10 23:31:30 -0200 | [diff] [blame] | 40 | func GetServerParams( |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 41 | host string, |
| 42 | port int64, |
| 43 | domain_socket string, |
| 44 | transport string, |
| 45 | protocol string, |
| 46 | ssl bool, |
Roger Meier | 41ad434 | 2015-03-24 22:30:40 +0100 | [diff] [blame] | 47 | certPath string, |
claudemiro | f8ca055 | 2016-01-10 23:31:30 -0200 | [diff] [blame] | 48 | handler thrifttest.ThriftTest) (thrift.TProcessor, thrift.TServerTransport, thrift.TTransportFactory, thrift.TProtocolFactory, error) { |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 49 | |
claudemiro | f8ca055 | 2016-01-10 23:31:30 -0200 | [diff] [blame] | 50 | var err error |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 51 | hostPort := fmt.Sprintf("%s:%d", host, port) |
| 52 | |
| 53 | var protocolFactory thrift.TProtocolFactory |
| 54 | switch protocol { |
| 55 | case "compact": |
| 56 | protocolFactory = thrift.NewTCompactProtocolFactory() |
| 57 | case "simplejson": |
| 58 | protocolFactory = thrift.NewTSimpleJSONProtocolFactory() |
| 59 | case "json": |
| 60 | protocolFactory = thrift.NewTJSONProtocolFactory() |
| 61 | case "binary": |
| 62 | protocolFactory = thrift.NewTBinaryProtocolFactoryDefault() |
Yuxuan 'fishy' Wang | 4d46c11 | 2019-06-07 20:47:18 +0800 | [diff] [blame] | 63 | case "header": |
| 64 | protocolFactory = thrift.NewTHeaderProtocolFactory() |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 65 | default: |
claudemiro | f8ca055 | 2016-01-10 23:31:30 -0200 | [diff] [blame] | 66 | return nil, nil, nil, nil, fmt.Errorf("Invalid protocol specified %s", protocol) |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 67 | } |
| 68 | if debugServerProtocol { |
| 69 | protocolFactory = thrift.NewTDebugProtocolFactory(protocolFactory, "server:") |
| 70 | } |
| 71 | |
| 72 | var serverTransport thrift.TServerTransport |
| 73 | if ssl { |
| 74 | cfg := new(tls.Config) |
| 75 | if cert, err := tls.LoadX509KeyPair(certPath+"/server.crt", certPath+"/server.key"); err != nil { |
claudemiro | f8ca055 | 2016-01-10 23:31:30 -0200 | [diff] [blame] | 76 | return nil, nil, nil, nil, err |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 77 | } else { |
| 78 | cfg.Certificates = append(cfg.Certificates, cert) |
| 79 | } |
| 80 | serverTransport, err = thrift.NewTSSLServerSocket(hostPort, cfg) |
| 81 | } else { |
| 82 | if domain_socket != "" { |
| 83 | serverTransport, err = thrift.NewTServerSocket(domain_socket) |
| 84 | } else { |
| 85 | serverTransport, err = thrift.NewTServerSocket(hostPort) |
| 86 | } |
| 87 | } |
| 88 | if err != nil { |
claudemiro | f8ca055 | 2016-01-10 23:31:30 -0200 | [diff] [blame] | 89 | return nil, nil, nil, nil, err |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | var transportFactory thrift.TTransportFactory |
| 93 | |
| 94 | switch transport { |
| 95 | case "http": |
claudemiro | f8ca055 | 2016-01-10 23:31:30 -0200 | [diff] [blame] | 96 | // there is no such factory, and we don't need any |
| 97 | transportFactory = nil |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 98 | case "framed": |
| 99 | transportFactory = thrift.NewTTransportFactory() |
| 100 | transportFactory = thrift.NewTFramedTransportFactory(transportFactory) |
| 101 | case "buffered": |
| 102 | transportFactory = thrift.NewTBufferedTransportFactory(8192) |
Jens Geyer | c6b991f | 2015-08-07 23:41:09 +0200 | [diff] [blame] | 103 | case "zlib": |
| 104 | transportFactory = thrift.NewTZlibTransportFactory(zlib.BestCompression) |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 105 | case "": |
| 106 | transportFactory = thrift.NewTTransportFactory() |
| 107 | default: |
claudemiro | f8ca055 | 2016-01-10 23:31:30 -0200 | [diff] [blame] | 108 | return nil, nil, nil, nil, fmt.Errorf("Invalid transport specified %s", transport) |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 109 | } |
| 110 | processor := thrifttest.NewThriftTestProcessor(handler) |
claudemiro | f8ca055 | 2016-01-10 23:31:30 -0200 | [diff] [blame] | 111 | |
| 112 | return processor, serverTransport, transportFactory, protocolFactory, nil |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 113 | } |