Repository: go-kratos/kratos
Pull request #1060 (2021-06-15 03:23:07)

add http transport


Patch:
diff --git a/internal/host/host.go b/internal/host/host.go
index 0014c0a2..193056d6 100644
--- a/internal/host/host.go
+++ b/internal/host/host.go
@@ -1,6 +1,7 @@
 package host
 
 import (
+	"fmt"
 	"net"
 	"strconv"
 )
@@ -27,6 +28,8 @@ func Extract(hostPort string, lis net.Listener) (string, error) {
 	if lis != nil {
 		if p, ok := Port(lis); ok {
 			port = strconv.Itoa(p)
+		} else {
+			return "", fmt.Errorf("failed to extract port: %v", lis.Addr())
 		}
 	}
 	if len(addr) > 0 && (addr != "0.0.0.0" && addr != "[::]" && addr != "::") {
diff --git a/transport/grpc/server.go b/transport/grpc/server.go
index 5710c824..21868f2a 100644
--- a/transport/grpc/server.go
+++ b/transport/grpc/server.go
@@ -138,7 +138,7 @@ func (s *Server) Endpoint() (*url.URL, error) {
 			s.err = err
 			return
 		}
-		addr, err := host.Extract(s.address, s.lis)
+		addr, err := host.Extract(s.address, lis)
 		if err != nil {
 			lis.Close()
 			s.err = err
diff --git a/transport/http/client.go b/transport/http/client.go
index 526150e5..9eb84c51 100644
--- a/transport/http/client.go
+++ b/transport/http/client.go
@@ -199,9 +199,8 @@ func (client *Client) Invoke(ctx context.Context, method, path string, args inte
 	ctx = transport.NewClientContext(ctx, &Transport{
 		endpoint:  client.opts.endpoint,
 		header:    headerCarrier(req.Header),
-		path:      path,
-		method:    method,
 		operation: c.operation,
+		request:   req,
 	})
 	return client.invoke(ctx, req, args, reply, c)
 }
diff --git a/transport/http/server.go b/transport/http/server.go
index 99266ef4..93f31e95 100644
--- a/transport/http/server.go
+++ b/transport/http/server.go
@@ -164,10 +164,9 @@ func (s *Server) filter() mux.MiddlewareFunc {
 			}
 			tr := &Transport{
 				endpoint:  s.endpoint.String(),
-				path:      req.RequestURI,
-				method:    req.Method,
 				operation: req.RequestURI,
 				header:    headerCarrier(req.Header),
+				request:   req,
 			}
 			if r := mux.CurrentRoute(req); r != nil {
 				if path, err := r.GetPathTemplate(); err == nil {
@@ -190,7 +189,7 @@ func (s *Server) Endpoint() (*url.URL, error) {
 			s.err = err
 			return
 		}
-		addr, err := host.Extract(s.address, s.lis)
+		addr, err := host.Extract(s.address, lis)
 		if err != nil {
 			lis.Close()
 			s.err = err
diff --git a/transport/http/transport.go b/transport/http/transport.go
index 1789c737..743cd316 100644
--- a/transport/http/transport.go
+++ b/transport/http/transport.go
@@ -14,10 +14,9 @@ var (
 // Transport is an HTTP transport.
 type Transport struct {
 	endpoint  string
-	path      string
-	method    string
 	operation string
 	header    headerCarrier
+	request   *http.Request
 }
 
 // Kind returns the transport kind.
@@ -40,24 +39,9 @@ func (tr *Transport) Header() transport.Header {
 	return tr.header
 }
 
-// Path returns the Transport path from server context.
-func Path(ctx context.Context) string {
-	if tr, ok := transport.FromServerContext(ctx); ok {
-		if tr, ok := tr.(*Transport); ok {
-			return tr.path
-		}
-	}
-	return ""
-}
-
-// Method returns the Transport method from server context.
-func Method(ctx context.Context) string {
-	if tr, ok := transport.FromServerContext(ctx); ok {
-		if tr, ok := tr.(*Transport); ok {
-			return tr.method
-		}
-	}
-	return ""
+// Request returns the transport request.
+func (tr *Transport) Request() *http.Request {
+	return tr.request
 }
 
 // SetOperation sets the transport operation.


Test patch:
diff --git a/transport/grpc/server_test.go b/transport/grpc/server_test.go
index c2901dc0..1f281adc 100644
--- a/transport/grpc/server_test.go
+++ b/transport/grpc/server_test.go
@@ -2,11 +2,9 @@ package grpc
 
 import (
 	"context"
-	"fmt"
+	"strings"
 	"testing"
 	"time"
-
-	"github.com/go-kratos/kratos/v2/internal/host"
 )
 
 type testKey struct{}
@@ -15,7 +13,8 @@ func TestServer(t *testing.T) {
 	ctx := context.Background()
 	ctx = context.WithValue(ctx, testKey{}, "test")
 	srv := NewServer()
-	if e, err := srv.Endpoint(); err != nil || e == nil {
+
+	if e, err := srv.Endpoint(); err != nil || e == nil || strings.HasSuffix(e.Host, ":0") {
 		t.Fatal(e, err)
 	}
 
@@ -31,13 +30,12 @@ func TestServer(t *testing.T) {
 }
 
 func testClient(t *testing.T, srv *Server) {
-	port, ok := host.Port(srv.lis)
-	if !ok {
-		t.Fatalf("extract port error: %v", srv.lis)
+	u, err := srv.Endpoint()
+	if err != nil {
+		t.Fatal(err)
 	}
-	endpoint := fmt.Sprintf("127.0.0.1:%d", port)
 	// new a gRPC client
-	conn, err := DialInsecure(context.Background(), WithEndpoint(endpoint))
+	conn, err := DialInsecure(context.Background(), WithEndpoint(u.Host))
 	if err != nil {
 		t.Fatal(err)
 	}
diff --git a/transport/http/server_test.go b/transport/http/server_test.go
index 12783bef..e0e1894c 100644
--- a/transport/http/server_test.go
+++ b/transport/http/server_test.go
@@ -6,6 +6,7 @@ import (
 	"fmt"
 	"io/ioutil"
 	"net/http"
+	"strings"
 	"testing"
 	"time"
 
@@ -33,7 +34,7 @@ func TestServer(t *testing.T) {
 	srv := NewServer()
 	srv.HandleFunc("/index", fn)
 
-	if e, err := srv.Endpoint(); err != nil || e == nil {
+	if e, err := srv.Endpoint(); err != nil || e == nil || strings.HasSuffix(e.Host, ":0") {
 		t.Fatal(e, err)
 	}
 
@@ -58,17 +59,17 @@ func testClient(t *testing.T, srv *Server) {
 		{"PATCH", "/index"},
 		{"DELETE", "/index"},
 	}
-	port, ok := host.Port(srv.lis)
-	if !ok {
-		t.Fatalf("extract port error: %v", srv.lis)
+	e, err := srv.Endpoint()
+	if err != nil {
+		t.Fatal(err)
 	}
-	client, err := NewClient(context.Background(), WithEndpoint(fmt.Sprintf("127.0.0.1:%d", port)))
+	client, err := NewClient(context.Background(), WithEndpoint(e.Host))
 	if err != nil {
 		t.Fatal(err)
 	}
 	for _, test := range tests {
 		var res testData
-		url := fmt.Sprintf("http://127.0.0.1:%d%s", port, test.path)
+		url := fmt.Sprintf(e.String() + test.path)
 		req, err := http.NewRequest(test.method, url, nil)
 		if err != nil {
 			t.Fatal(err)