blob: c6674ae75979ee758d9a5a00637f8cc1f3d9c1ee [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"
28 "thrift"
29)
30
31var (
32 debugServerProtocol bool
33 certPath string
34)
35
36func init() {
37 flag.BoolVar(&debugServerProtocol, "debug_server_protocol", false, "turn server protocol trace on")
38}
39
claudemirof8ca0552016-01-10 23:31:30 -020040func GetServerParams(
Jens Geyerf4598682014-05-08 23:18:44 +020041 host string,
42 port int64,
43 domain_socket string,
44 transport string,
45 protocol string,
46 ssl bool,
Roger Meier41ad4342015-03-24 22:30:40 +010047 certPath string,
claudemirof8ca0552016-01-10 23:31:30 -020048 handler thrifttest.ThriftTest) (thrift.TProcessor, thrift.TServerTransport, thrift.TTransportFactory, thrift.TProtocolFactory, error) {
Jens Geyerf4598682014-05-08 23:18:44 +020049
claudemirof8ca0552016-01-10 23:31:30 -020050 var err error
Jens Geyerf4598682014-05-08 23:18:44 +020051 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' Wang4d46c112019-06-07 20:47:18 +080063 case "header":
64 protocolFactory = thrift.NewTHeaderProtocolFactory()
Jens Geyerf4598682014-05-08 23:18:44 +020065 default:
claudemirof8ca0552016-01-10 23:31:30 -020066 return nil, nil, nil, nil, fmt.Errorf("Invalid protocol specified %s", protocol)
Jens Geyerf4598682014-05-08 23:18:44 +020067 }
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 {
claudemirof8ca0552016-01-10 23:31:30 -020076 return nil, nil, nil, nil, err
Jens Geyerf4598682014-05-08 23:18:44 +020077 } 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 {
claudemirof8ca0552016-01-10 23:31:30 -020089 return nil, nil, nil, nil, err
Jens Geyerf4598682014-05-08 23:18:44 +020090 }
91
92 var transportFactory thrift.TTransportFactory
93
94 switch transport {
95 case "http":
claudemirof8ca0552016-01-10 23:31:30 -020096 // there is no such factory, and we don't need any
97 transportFactory = nil
Jens Geyerf4598682014-05-08 23:18:44 +020098 case "framed":
99 transportFactory = thrift.NewTTransportFactory()
100 transportFactory = thrift.NewTFramedTransportFactory(transportFactory)
101 case "buffered":
102 transportFactory = thrift.NewTBufferedTransportFactory(8192)
Jens Geyerc6b991f2015-08-07 23:41:09 +0200103 case "zlib":
104 transportFactory = thrift.NewTZlibTransportFactory(zlib.BestCompression)
Jens Geyerf4598682014-05-08 23:18:44 +0200105 case "":
106 transportFactory = thrift.NewTTransportFactory()
107 default:
claudemirof8ca0552016-01-10 23:31:30 -0200108 return nil, nil, nil, nil, fmt.Errorf("Invalid transport specified %s", transport)
Jens Geyerf4598682014-05-08 23:18:44 +0200109 }
110 processor := thrifttest.NewThriftTestProcessor(handler)
claudemirof8ca0552016-01-10 23:31:30 -0200111
112 return processor, serverTransport, transportFactory, protocolFactory, nil
Jens Geyerf4598682014-05-08 23:18:44 +0200113}