Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE#667] Java client Bug gzip&zlib compliance #909

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion golang/pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ package utils
import (
"bytes"
"compress/gzip"
"compress/zlib"
"context"
"encoding/hex"
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"net/url"
Expand Down Expand Up @@ -140,8 +143,33 @@ func MatchMessageType(mq *v2.MessageQueue, messageType v2.MessageType) bool {
return false
}

func detectCompressionFormat(in []byte) (string, error) {
if len(in) < 2 {
return "", errors.New("body too short")
}
if in[0] == 0x1f && in[1] == 0x8b {
return "GZIP", nil
} else if in[0] == 0x78 {
return "ZLIB", nil
} else {
return "", errors.New("unknown format")
}
}

func GZIPDecode(in []byte) ([]byte, error) {
reader, err := gzip.NewReader(bytes.NewReader(in))
format, err := detectCompressionFormat(in)
if err != nil {
return nil, err
}
var reader io.ReadCloser
switch format {
case "GZIP":
reader, err = gzip.NewReader(bytes.NewReader(in))
case "ZLIB":
reader, err = zlib.NewReader(bytes.NewReader(in))
default:
return nil, errors.New("unsupported compression format")
}
if err != nil {
var out []byte
return out, err
Expand Down
18 changes: 17 additions & 1 deletion golang/pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package utils

import (
"compress/gzip"
"compress/zlib"
"strings"
"testing"

v2 "github.com/apache/rocketmq-clients/golang/v5/protocol/v2"
Expand Down Expand Up @@ -113,7 +115,7 @@ func TestMatchMessageType(t *testing.T) {

func TestGZIPDecode(t *testing.T) {
_, err := GZIPDecode([]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9})
if err != gzip.ErrHeader {
if err != gzip.ErrHeader && !strings.Contains(err.Error(), "unknown format") {
t.Error()
}
bytes, err := GZIPDecode([]byte{31, 139, 8, 0, 0, 0, 0, 0, 0, 255, 42, 202, 79, 206, 78, 45, 201, 45, 212, 77, 206, 201, 76, 205, 43, 209, 77, 207, 7, 0, 0, 0, 255, 255, 1, 0, 0, 255, 255, 97, 36, 132, 114, 18, 0, 0, 0})
Expand All @@ -125,6 +127,20 @@ func TestGZIPDecode(t *testing.T) {
}
}

func TestZLIBDecode(t *testing.T) {
_, err := GZIPDecode([]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9})
if err != zlib.ErrHeader && !strings.Contains(err.Error(), "unknown format") {
t.Error()
}
bytes, err := GZIPDecode([]byte{120, 156, 42, 202, 79, 206, 78, 45, 201, 45, 212, 77, 206, 201, 76, 205, 43, 209, 77, 207, 7, 4, 0, 0, 255, 255, 68, 223, 7, 22})
if err != nil {
t.Error()
}
if string(bytes) != "rocketmq-client-go" {
t.Error()
}
}

func TestSelectAnAddress(t *testing.T) {
if SelectAnAddress(nil) != nil {
t.Error()
Expand Down