blob: 85429f0a6b5f3a32f9684cf6a9be9c47c73e8290 [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"
Yuxuan 'fishy' Wangb71f11e2021-03-22 15:01:00 -070027
28 "github.com/apache/thrift/lib/go/thrift"
29 "github.com/apache/thrift/test/go/src/gen/thrifttest"
Jens Geyerf4598682014-05-08 23:18:44 +020030)
31
32var (
33 debugServerProtocol bool
Jens Geyerf4598682014-05-08 23:18:44 +020034)
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,
Yuxuan 'fishy' Wang17373a32021-08-26 11:04:27 -070048 handler thrifttest.ThriftTest,
Yuxuan 'fishy' Wang91565d42024-08-14 09:01:15 -070049) (thrift.TProcessor, thrift.TServerTransport, thrift.TTransportFactory, thrift.TProtocolFactory, string /* addr */, error) {
Jens Geyerf4598682014-05-08 23:18:44 +020050
claudemirof8ca0552016-01-10 23:31:30 -020051 var err error
Yuxuan 'fishy' Wange98d6b12024-08-14 13:57:52 -070052 hostPort := fmt.Sprintf("%s:%d", host, port)
Yuxuan 'fishy' Wang17373a32021-08-26 11:04:27 -070053 var cfg *thrift.TConfiguration = nil
Jens Geyerf4598682014-05-08 23:18:44 +020054
55 var protocolFactory thrift.TProtocolFactory
56 switch protocol {
57 case "compact":
Yuxuan 'fishy' Wang17373a32021-08-26 11:04:27 -070058 protocolFactory = thrift.NewTCompactProtocolFactoryConf(cfg)
Jens Geyerf4598682014-05-08 23:18:44 +020059 case "simplejson":
Yuxuan 'fishy' Wang17373a32021-08-26 11:04:27 -070060 protocolFactory = thrift.NewTSimpleJSONProtocolFactoryConf(cfg)
Jens Geyerf4598682014-05-08 23:18:44 +020061 case "json":
62 protocolFactory = thrift.NewTJSONProtocolFactory()
63 case "binary":
Yuxuan 'fishy' Wang17373a32021-08-26 11:04:27 -070064 protocolFactory = thrift.NewTBinaryProtocolFactoryConf(nil)
Yuxuan 'fishy' Wang4d46c112019-06-07 20:47:18 +080065 case "header":
Yuxuan 'fishy' Wang17373a32021-08-26 11:04:27 -070066 protocolFactory = thrift.NewTHeaderProtocolFactoryConf(nil)
Jens Geyerf4598682014-05-08 23:18:44 +020067 default:
Yuxuan 'fishy' Wang91565d42024-08-14 09:01:15 -070068 return nil, nil, nil, nil, "", fmt.Errorf("invalid protocol specified %s", protocol)
Jens Geyerf4598682014-05-08 23:18:44 +020069 }
70 if debugServerProtocol {
Yuxuan 'fishy' Wang17373a32021-08-26 11:04:27 -070071 protocolFactory = thrift.NewTDebugProtocolFactoryWithLogger(protocolFactory, "server:", thrift.StdLogger(nil))
Jens Geyerf4598682014-05-08 23:18:44 +020072 }
73
74 var serverTransport thrift.TServerTransport
Yuxuan 'fishy' Wang91565d42024-08-14 09:01:15 -070075 var addr string
Yuxuan 'fishy' Wange98d6b12024-08-14 13:57:52 -070076 if transport == "http" {
77 // In cross-test servers, we would call http.ListenAndServe
78 // again on the host:port, so don't use the listen to fill the
79 // addr and just generate it here instead.
80 addr = hostPort
81 if domain_socket != "" {
82 addr = domain_socket
83 }
84 }
Jens Geyerf4598682014-05-08 23:18:44 +020085 if ssl {
86 cfg := new(tls.Config)
87 if cert, err := tls.LoadX509KeyPair(certPath+"/server.crt", certPath+"/server.key"); err != nil {
Yuxuan 'fishy' Wang91565d42024-08-14 09:01:15 -070088 return nil, nil, nil, nil, "", err
Jens Geyerf4598682014-05-08 23:18:44 +020089 } else {
90 cfg.Certificates = append(cfg.Certificates, cert)
91 }
Yuxuan 'fishy' Wange98d6b12024-08-14 13:57:52 -070092 serverSocket, transportErr := thrift.NewTSSLServerSocket(hostPort, cfg)
Yuxuan 'fishy' Wang91565d42024-08-14 09:01:15 -070093 if transportErr == nil {
Yuxuan 'fishy' Wange98d6b12024-08-14 13:57:52 -070094 if transport != "http" {
95 listenErr := serverSocket.Listen()
96 if listenErr == nil {
97 serverTransport = serverSocket
98 addr = serverSocket.Addr().String()
99 } else {
100 err = listenErr
101 }
Yuxuan 'fishy' Wang91565d42024-08-14 09:01:15 -0700102 }
103 } else {
104 err = transportErr
105 }
Jens Geyerf4598682014-05-08 23:18:44 +0200106 } else {
107 if domain_socket != "" {
108 serverTransport, err = thrift.NewTServerSocket(domain_socket)
Yuxuan 'fishy' Wang91565d42024-08-14 09:01:15 -0700109 addr = domain_socket
Jens Geyerf4598682014-05-08 23:18:44 +0200110 } else {
Yuxuan 'fishy' Wange98d6b12024-08-14 13:57:52 -0700111 serverSocket, transportErr := thrift.NewTServerSocket(hostPort)
Yuxuan 'fishy' Wang91565d42024-08-14 09:01:15 -0700112 if transportErr == nil {
Yuxuan 'fishy' Wange98d6b12024-08-14 13:57:52 -0700113 if transport != "http" {
114 listenErr := serverSocket.Listen()
115 if listenErr == nil {
116 serverTransport = serverSocket
117 addr = serverSocket.Addr().String()
118 } else {
119 err = listenErr
120 }
Yuxuan 'fishy' Wang91565d42024-08-14 09:01:15 -0700121 }
122 } else {
123 err = transportErr
124 }
Jens Geyerf4598682014-05-08 23:18:44 +0200125 }
126 }
127 if err != nil {
Yuxuan 'fishy' Wang91565d42024-08-14 09:01:15 -0700128 return nil, nil, nil, nil, "", err
Jens Geyerf4598682014-05-08 23:18:44 +0200129 }
130
131 var transportFactory thrift.TTransportFactory
132
133 switch transport {
134 case "http":
claudemirof8ca0552016-01-10 23:31:30 -0200135 // there is no such factory, and we don't need any
136 transportFactory = nil
Jens Geyerf4598682014-05-08 23:18:44 +0200137 case "framed":
138 transportFactory = thrift.NewTTransportFactory()
Yuxuan 'fishy' Wang17373a32021-08-26 11:04:27 -0700139 transportFactory = thrift.NewTFramedTransportFactoryConf(transportFactory, nil)
Jens Geyerf4598682014-05-08 23:18:44 +0200140 case "buffered":
141 transportFactory = thrift.NewTBufferedTransportFactory(8192)
Jens Geyerc6b991f2015-08-07 23:41:09 +0200142 case "zlib":
143 transportFactory = thrift.NewTZlibTransportFactory(zlib.BestCompression)
Jens Geyerf4598682014-05-08 23:18:44 +0200144 case "":
145 transportFactory = thrift.NewTTransportFactory()
146 default:
Yuxuan 'fishy' Wang91565d42024-08-14 09:01:15 -0700147 return nil, nil, nil, nil, "", fmt.Errorf("invalid transport specified %s", transport)
Jens Geyerf4598682014-05-08 23:18:44 +0200148 }
149 processor := thrifttest.NewThriftTestProcessor(handler)
claudemirof8ca0552016-01-10 23:31:30 -0200150
Yuxuan 'fishy' Wang91565d42024-08-14 09:01:15 -0700151 return processor, serverTransport, transportFactory, protocolFactory, addr, nil
Jens Geyerf4598682014-05-08 23:18:44 +0200152}