You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by je...@apache.org on 2016/01/28 23:05:47 UTC

[2/5] thrift git commit: THRIFT-3251 Add http transport for server to Go lib Client: Go Patch: claudemiro

THRIFT-3251 Add http transport for server to Go lib
Client: Go
Patch: claudemiro <di...@gmail.com>

This closes #785


Project: http://git-wip-us.apache.org/repos/asf/thrift/repo
Commit: http://git-wip-us.apache.org/repos/asf/thrift/commit/f8ca0552
Tree: http://git-wip-us.apache.org/repos/asf/thrift/tree/f8ca0552
Diff: http://git-wip-us.apache.org/repos/asf/thrift/diff/f8ca0552

Branch: refs/heads/master
Commit: f8ca05528e04a24b9f843c82c6600e4de5e42291
Parents: 108fab8
Author: claudemiro <di...@gmail.com>
Authored: Sun Jan 10 23:31:30 2016 -0200
Committer: Jens Geyer <je...@apache.org>
Committed: Thu Jan 28 23:05:02 2016 +0100

----------------------------------------------------------------------
 lib/go/thrift/http_transport.go         | 34 ++++++++++++++++++++++++++++
 test/go/src/bin/testserver/main.go      | 33 ++++++++++++++++++++++++---
 test/go/src/common/client.go            | 14 +++++++++++-
 test/go/src/common/clientserver_test.go | 15 +++++++++---
 test/go/src/common/server.go            | 28 +++++++++--------------
 test/tests.json                         |  3 ++-
 6 files changed, 102 insertions(+), 25 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/thrift/blob/f8ca0552/lib/go/thrift/http_transport.go
----------------------------------------------------------------------
diff --git a/lib/go/thrift/http_transport.go b/lib/go/thrift/http_transport.go
new file mode 100644
index 0000000..f6d7458
--- /dev/null
+++ b/lib/go/thrift/http_transport.go
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package thrift
+
+import "net/http"
+
+// NewThriftHandlerFunc is a function that create a ready to use Apache Thrift Handler function
+func NewThriftHandlerFunc(processor TProcessor,
+	inPfactory, outPfactory TProtocolFactory) func(w http.ResponseWriter, r *http.Request) {
+
+	return func(w http.ResponseWriter, r *http.Request) {
+		w.Header().Add("Content-Type", "application/x-thrift")
+
+		transport := NewStreamTransport(r.Body, w)
+		processor.Process(inPfactory.GetProtocol(transport), outPfactory.GetProtocol(transport))
+	}
+}

http://git-wip-us.apache.org/repos/asf/thrift/blob/f8ca0552/test/go/src/bin/testserver/main.go
----------------------------------------------------------------------
diff --git a/test/go/src/bin/testserver/main.go b/test/go/src/bin/testserver/main.go
index 291dff5..0bf833d 100644
--- a/test/go/src/bin/testserver/main.go
+++ b/test/go/src/bin/testserver/main.go
@@ -22,7 +22,10 @@ package main
 import (
 	"common"
 	"flag"
+	"fmt"
 	"log"
+	"net/http"
+	"thrift"
 )
 
 var host = flag.String("host", "localhost", "Host to connect")
@@ -35,9 +38,33 @@ var certPath = flag.String("certPath", "keys", "Directory that contains SSL cert
 
 func main() {
 	flag.Parse()
-	server, err := common.StartServer(*host, *port, *domain_socket, *transport, *protocol, *ssl, *certPath, common.PrintingHandler)
+
+	processor, serverTransport, transportFactory, protocolFactory, err := common.GetServerParams(*host, *port, *domain_socket, *transport, *protocol, *ssl, *certPath, common.PrintingHandler)
+
 	if err != nil {
-		log.Fatalf("Unable to start server: ", err)
+		log.Fatalf("Unable to process server params: ", err)
+	}
+
+	if *transport == "http" {
+		http.HandleFunc("/", thrift.NewThriftHandlerFunc(processor, protocolFactory, protocolFactory))
+
+		if *ssl {
+			err := http.ListenAndServeTLS(fmt.Sprintf(":%d", *port),
+				*certPath+"/server.pem", *certPath+"/server.key", nil)
+
+			if err != nil {
+				fmt.Println(err)
+				return
+			}
+		} else {
+			http.ListenAndServe(fmt.Sprintf(":%d", *port), nil)
+		}
+	} else {
+		server := thrift.NewTSimpleServer4(processor, serverTransport, transportFactory, protocolFactory)
+		if err = server.Listen(); err != nil {
+			return
+		}
+		go server.AcceptLoop()
+		server.Serve()
 	}
-	server.Serve()
 }

http://git-wip-us.apache.org/repos/asf/thrift/blob/f8ca0552/test/go/src/common/client.go
----------------------------------------------------------------------
diff --git a/test/go/src/common/client.go b/test/go/src/common/client.go
index e55dc6d..4251d91 100644
--- a/test/go/src/common/client.go
+++ b/test/go/src/common/client.go
@@ -25,6 +25,7 @@ import (
 	"flag"
 	"fmt"
 	"gen/thrifttest"
+	"net/http"
 	"thrift"
 )
 
@@ -75,10 +76,21 @@ func StartClient(
 	}
 	switch transport {
 	case "http":
-		trans, err = thrift.NewTHttpClient(fmt.Sprintf("http://%s/service", hostPort))
+		if ssl {
+			tr := &http.Transport{
+				TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
+			}
+			client := &http.Client{Transport: tr}
+			trans, err = thrift.NewTHttpPostClientWithOptions(fmt.Sprintf("https://%s/", hostPort), thrift.THttpClientOptions{Client: client})
+			fmt.Println(hostPort)
+		} else {
+			trans, err = thrift.NewTHttpPostClient(fmt.Sprintf("http://%s/", hostPort))
+		}
+
 		if err != nil {
 			return nil, err
 		}
+
 	case "framed":
 		trans = thrift.NewTFramedTransport(trans)
 	case "buffered":

http://git-wip-us.apache.org/repos/asf/thrift/blob/f8ca0552/test/go/src/common/clientserver_test.go
----------------------------------------------------------------------
diff --git a/test/go/src/common/clientserver_test.go b/test/go/src/common/clientserver_test.go
index 5c8915a..26fa7af 100644
--- a/test/go/src/common/clientserver_test.go
+++ b/test/go/src/common/clientserver_test.go
@@ -20,12 +20,13 @@
 package common
 
 import (
-	"github.com/golang/mock/gomock"
 	"errors"
 	"gen/thrifttest"
 	"reflect"
 	"testing"
 	"thrift"
+
+	"github.com/golang/mock/gomock"
 )
 
 type test_unit struct {
@@ -56,7 +57,15 @@ func doUnit(t *testing.T, unit *test_unit) {
 	ctrl := gomock.NewController(t)
 	defer ctrl.Finish()
 	handler := NewMockThriftTest(ctrl)
-	server, err := StartServer(unit.host, unit.port, unit.domain_socket, unit.transport, unit.protocol, unit.ssl, "../../../keys", handler)
+
+	processor, serverTransport, transportFactory, protocolFactory, err := GetServerParams(unit.host, unit.port, unit.domain_socket, unit.transport, unit.protocol, unit.ssl, "../../../keys", handler)
+
+	server := thrift.NewTSimpleServer4(processor, serverTransport, transportFactory, protocolFactory)
+	if err = server.Listen(); err != nil {
+		return
+	}
+	go server.AcceptLoop()
+	server.Serve()
 	if err != nil {
 		t.Errorf("Unable to start server", err)
 		t.FailNow()
@@ -175,7 +184,7 @@ func callEverythingWithMock(t *testing.T, client *thrifttest.ThriftTestClient, h
 	}
 
 	// TODO: add TestBinary() call
-	
+
 	xs := thrifttest.NewXtruct()
 	xs.StringThing = "thing"
 	xs.ByteThing = 42

http://git-wip-us.apache.org/repos/asf/thrift/blob/f8ca0552/test/go/src/common/server.go
----------------------------------------------------------------------
diff --git a/test/go/src/common/server.go b/test/go/src/common/server.go
index dc380b2..5ac4400 100644
--- a/test/go/src/common/server.go
+++ b/test/go/src/common/server.go
@@ -37,7 +37,7 @@ func init() {
 	flag.BoolVar(&debugServerProtocol, "debug_server_protocol", false, "turn server protocol trace on")
 }
 
-func StartServer(
+func GetServerParams(
 	host string,
 	port int64,
 	domain_socket string,
@@ -45,8 +45,9 @@ func StartServer(
 	protocol string,
 	ssl bool,
 	certPath string,
-	handler thrifttest.ThriftTest) (srv *thrift.TSimpleServer, err error) {
+	handler thrifttest.ThriftTest) (thrift.TProcessor, thrift.TServerTransport, thrift.TTransportFactory, thrift.TProtocolFactory, error) {
 
+	var err error
 	hostPort := fmt.Sprintf("%s:%d", host, port)
 
 	var protocolFactory thrift.TProtocolFactory
@@ -60,7 +61,7 @@ func StartServer(
 	case "binary":
 		protocolFactory = thrift.NewTBinaryProtocolFactoryDefault()
 	default:
-		return nil, fmt.Errorf("Invalid protocol specified %s", protocol)
+		return nil, nil, nil, nil, fmt.Errorf("Invalid protocol specified %s", protocol)
 	}
 	if debugServerProtocol {
 		protocolFactory = thrift.NewTDebugProtocolFactory(protocolFactory, "server:")
@@ -70,7 +71,7 @@ func StartServer(
 	if ssl {
 		cfg := new(tls.Config)
 		if cert, err := tls.LoadX509KeyPair(certPath+"/server.crt", certPath+"/server.key"); err != nil {
-			return nil, err
+			return nil, nil, nil, nil, err
 		} else {
 			cfg.Certificates = append(cfg.Certificates, cert)
 		}
@@ -83,18 +84,15 @@ func StartServer(
 		}
 	}
 	if err != nil {
-		return nil, err
+		return nil, nil, nil, nil, err
 	}
 
 	var transportFactory thrift.TTransportFactory
 
 	switch transport {
 	case "http":
-		return nil, fmt.Errorf("Http server transport is not supported")
-		// trans, err = thrift.NewTHttpClient(fmt.Sprintf("http://%s/service", hostPort))
-		// if err != nil {
-		// 	return nil, err
-		// }
+		// there is no such factory, and we don't need any
+		transportFactory = nil
 	case "framed":
 		transportFactory = thrift.NewTTransportFactory()
 		transportFactory = thrift.NewTFramedTransportFactory(transportFactory)
@@ -105,13 +103,9 @@ func StartServer(
 	case "":
 		transportFactory = thrift.NewTTransportFactory()
 	default:
-		return nil, fmt.Errorf("Invalid transport specified %s", transport)
+		return nil, nil, nil, nil, fmt.Errorf("Invalid transport specified %s", transport)
 	}
 	processor := thrifttest.NewThriftTestProcessor(handler)
-	server := thrift.NewTSimpleServer4(processor, serverTransport, transportFactory, protocolFactory)
-	if err = server.Listen(); err != nil {
-		return
-	}
-	go server.AcceptLoop()
-	return server, nil
+
+	return processor, serverTransport, transportFactory, protocolFactory, nil
 }

http://git-wip-us.apache.org/repos/asf/thrift/blob/f8ca0552/test/tests.json
----------------------------------------------------------------------
diff --git a/test/tests.json b/test/tests.json
index 8ba6186..6df5d41 100644
--- a/test/tests.json
+++ b/test/tests.json
@@ -45,7 +45,8 @@
     },
     "transports": [
       "buffered",
-      "framed"
+      "framed",
+      "http"
     ],
     "sockets": [
       "ip",