From 22c41c77e62bf06eeee5291d410490733b9f4a39 Mon Sep 17 00:00:00 2001 From: Menghan Li Date: Wed, 27 Jul 2016 15:46:27 -0700 Subject: [PATCH] Change function names --- transport/handler_server.go | 4 ++-- transport/handler_server_test.go | 4 ++-- transport/http2_client.go | 2 +- transport/http2_server.go | 2 +- transport/http_util.go | 24 ++++++++++++------------ transport/http_util_test.go | 16 ++++++++-------- 6 files changed, 26 insertions(+), 26 deletions(-) diff --git a/transport/handler_server.go b/transport/handler_server.go index 5288657c..30e21ac0 100644 --- a/transport/handler_server.go +++ b/transport/handler_server.go @@ -83,7 +83,7 @@ func NewServerHandlerTransport(w http.ResponseWriter, r *http.Request) (ServerTr } if v := r.Header.Get("grpc-timeout"); v != "" { - to, err := timeoutDecode(v) + to, err := decodeTimeout(v) if err != nil { return nil, StreamErrorf(codes.Internal, "malformed time-out: %v", err) } @@ -194,7 +194,7 @@ func (ht *serverHandlerTransport) WriteStatus(s *Stream, statusCode codes.Code, h := ht.rw.Header() h.Set("Grpc-Status", fmt.Sprintf("%d", statusCode)) if statusDesc != "" { - h.Set("Grpc-Message", grpcMessageEncode(statusDesc)) + h.Set("Grpc-Message", encodeGrpcMessage(statusDesc)) } if md := s.Trailer(); len(md) > 0 { for k, vv := range md { diff --git a/transport/handler_server_test.go b/transport/handler_server_test.go index 0711d012..84fc917f 100644 --- a/transport/handler_server_test.go +++ b/transport/handler_server_test.go @@ -333,7 +333,7 @@ func handleStreamCloseBodyTest(t *testing.T, statusCode codes.Code, msg string) "Content-Type": {"application/grpc"}, "Trailer": {"Grpc-Status", "Grpc-Message"}, "Grpc-Status": {fmt.Sprint(uint32(statusCode))}, - "Grpc-Message": {grpcMessageEncode(msg)}, + "Grpc-Message": {encodeGrpcMessage(msg)}, } if !reflect.DeepEqual(st.rw.HeaderMap, wantHeader) { t.Errorf("Header+Trailer mismatch.\n got: %#v\nwant: %#v", st.rw.HeaderMap, wantHeader) @@ -381,7 +381,7 @@ func TestHandlerTransport_HandleStreams_Timeout(t *testing.T) { "Content-Type": {"application/grpc"}, "Trailer": {"Grpc-Status", "Grpc-Message"}, "Grpc-Status": {"4"}, - "Grpc-Message": {grpcMessageEncode("too slow")}, + "Grpc-Message": {encodeGrpcMessage("too slow")}, } if !reflect.DeepEqual(rw.HeaderMap, wantHeader) { t.Errorf("Header+Trailer Map mismatch.\n got: %#v\nwant: %#v", rw.HeaderMap, wantHeader) diff --git a/transport/http2_client.go b/transport/http2_client.go index 51cf1792..41c0acf0 100644 --- a/transport/http2_client.go +++ b/transport/http2_client.go @@ -341,7 +341,7 @@ func (t *http2Client) NewStream(ctx context.Context, callHdr *CallHdr) (_ *Strea t.hEnc.WriteField(hpack.HeaderField{Name: "grpc-encoding", Value: callHdr.SendCompress}) } if timeout > 0 { - t.hEnc.WriteField(hpack.HeaderField{Name: "grpc-timeout", Value: timeoutEncode(timeout)}) + t.hEnc.WriteField(hpack.HeaderField{Name: "grpc-timeout", Value: encodeTimeout(timeout)}) } for k, v := range authData { // Capital header names are illegal in HTTP/2. diff --git a/transport/http2_server.go b/transport/http2_server.go index 2322c938..31ffee0c 100644 --- a/transport/http2_server.go +++ b/transport/http2_server.go @@ -507,7 +507,7 @@ func (t *http2Server) WriteStatus(s *Stream, statusCode codes.Code, statusDesc s Name: "grpc-status", Value: strconv.Itoa(int(statusCode)), }) - t.hEnc.WriteField(hpack.HeaderField{Name: "grpc-message", Value: grpcMessageEncode(statusDesc)}) + t.hEnc.WriteField(hpack.HeaderField{Name: "grpc-message", Value: encodeGrpcMessage(statusDesc)}) // Attach the trailer metadata. for k, v := range s.trailer { // Clients don't tolerate reading restricted headers after some non restricted ones were sent. diff --git a/transport/http_util.go b/transport/http_util.go index ea363d93..3e16e4df 100644 --- a/transport/http_util.go +++ b/transport/http_util.go @@ -175,11 +175,11 @@ func (d *decodeState) processHeaderField(f hpack.HeaderField) { } d.statusCode = codes.Code(code) case "grpc-message": - d.statusDesc = grpcMessageDecode(f.Value) + d.statusDesc = decodeGrpcMessage(f.Value) case "grpc-timeout": d.timeoutSet = true var err error - d.timeout, err = timeoutDecode(f.Value) + d.timeout, err = decodeTimeout(f.Value) if err != nil { d.setErr(StreamErrorf(codes.Internal, "transport: malformed time-out: %v", err)) return @@ -252,7 +252,7 @@ func div(d, r time.Duration) int64 { } // TODO(zhaoq): It is the simplistic and not bandwidth efficient. Improve it. -func timeoutEncode(t time.Duration) string { +func encodeTimeout(t time.Duration) string { if d := div(t, time.Nanosecond); d <= maxTimeoutValue { return strconv.FormatInt(d, 10) + "n" } @@ -272,7 +272,7 @@ func timeoutEncode(t time.Duration) string { return strconv.FormatInt(div(t, time.Hour), 10) + "H" } -func timeoutDecode(s string) (time.Duration, error) { +func decodeTimeout(s string) (time.Duration, error) { size := len(s) if size < 2 { return 0, fmt.Errorf("transport: timeout string is too short: %q", s) @@ -295,13 +295,13 @@ const ( percentByte = '%' ) -// grpcMessageEncode is used to encode status code in header field +// encodeGrpcMessage is used to encode status code in header field // "grpc-message". // It checks to see if each individual byte in msg is an // allowable byte, and then either percent encoding or passing it through. // When percent encoding, the byte is converted into hexadecimal notation // with a '%' prepended. -func grpcMessageEncode(msg string) string { +func encodeGrpcMessage(msg string) string { if msg == "" { return "" } @@ -309,13 +309,13 @@ func grpcMessageEncode(msg string) string { for i := 0; i < lenMsg; i++ { c := msg[i] if !(c >= spaceByte && c < tildaByte && c != percentByte) { - return grpcMessageEncodeUnchecked(msg) + return encodeGrpcMessageUnchecked(msg) } } return msg } -func grpcMessageEncodeUnchecked(msg string) string { +func encodeGrpcMessageUnchecked(msg string) string { var buf bytes.Buffer lenMsg := len(msg) for i := 0; i < lenMsg; i++ { @@ -329,21 +329,21 @@ func grpcMessageEncodeUnchecked(msg string) string { return buf.String() } -// grpcMessageDecode decodes the msg encoded by grpcMessageEncode. -func grpcMessageDecode(msg string) string { +// decodeGrpcMessage decodes the msg encoded by encodeGrpcMessage. +func decodeGrpcMessage(msg string) string { if msg == "" { return "" } lenMsg := len(msg) for i := 0; i < lenMsg; i++ { if msg[i] == percentByte && i+2 < lenMsg { - return grpcMessageDecodeUnchecked(msg) + return decodeGrpcMessageUnchecked(msg) } } return msg } -func grpcMessageDecodeUnchecked(msg string) string { +func decodeGrpcMessageUnchecked(msg string) string { var buf bytes.Buffer lenMsg := len(msg) for i := 0; i < lenMsg; i++ { diff --git a/transport/http_util_test.go b/transport/http_util_test.go index 9967161b..41bf5477 100644 --- a/transport/http_util_test.go +++ b/transport/http_util_test.go @@ -59,7 +59,7 @@ func TestTimeoutEncode(t *testing.T) { if err != nil { t.Fatalf("failed to parse duration string %s: %v", test.in, err) } - out := timeoutEncode(d) + out := encodeTimeout(d) if out != test.out { t.Fatalf("timeoutEncode(%s) = %s, want %s", test.in, out, test.out) } @@ -79,7 +79,7 @@ func TestTimeoutDecode(t *testing.T) { {"1", 0, fmt.Errorf("transport: timeout string is too short: %q", "1")}, {"", 0, fmt.Errorf("transport: timeout string is too short: %q", "")}, } { - d, err := timeoutDecode(test.s) + d, err := decodeTimeout(test.s) if d != test.d || fmt.Sprint(err) != fmt.Sprint(test.err) { t.Fatalf("timeoutDecode(%q) = %d, %v, want %d, %v", test.s, int64(d), err, int64(test.d), test.err) } @@ -108,7 +108,7 @@ func TestValidContentType(t *testing.T) { } } -func TestGrpcMessageEncode(t *testing.T) { +func TestEncodeGrpcMessage(t *testing.T) { for _, tt := range []struct { input string expected string @@ -118,14 +118,14 @@ func TestGrpcMessageEncode(t *testing.T) { {"my favorite character is \u0000", "my favorite character is %00"}, {"my favorite character is %", "my favorite character is %25"}, } { - actual := grpcMessageEncode(tt.input) + actual := encodeGrpcMessage(tt.input) if tt.expected != actual { - t.Errorf("grpcMessageEncode(%v) = %v, want %v", tt.input, actual, tt.expected) + t.Errorf("encodeGrpcMessage(%v) = %v, want %v", tt.input, actual, tt.expected) } } } -func TestGrpcMessageDecode(t *testing.T) { +func TestDecodeGrpcMessage(t *testing.T) { for _, tt := range []struct { input string expected string @@ -136,9 +136,9 @@ func TestGrpcMessageDecode(t *testing.T) { {"H%6", "H%6"}, {"%G0", "%G0"}, } { - actual := grpcMessageDecode(tt.input) + actual := decodeGrpcMessage(tt.input) if tt.expected != actual { - t.Errorf("grpcMessageDncode(%v) = %v, want %v", tt.input, actual, tt.expected) + t.Errorf("dncodeGrpcMessage(%v) = %v, want %v", tt.input, actual, tt.expected) } } }