Skip to content

Commit

Permalink
fix:ISSUE#667
Browse files Browse the repository at this point in the history
This commit has made compatibility processing for the ZLIB data format that may be passed into the GZIP decompression method, and added a new test case for ZLIB compression.
  • Loading branch information
zhenliemao committed Jan 7, 2025
1 parent d970007 commit 25428ac
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 46 deletions.
30 changes: 1 addition & 29 deletions golang/pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,9 @@ package utils
import (
"bytes"
"compress/gzip"
"compress/zlib"
"context"
"encoding/hex"
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"net/url"
Expand Down Expand Up @@ -143,33 +140,8 @@ 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) {
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")
}
reader, err := gzip.NewReader(bytes.NewReader(in))
if err != nil {
var out []byte
return out, err
Expand Down
18 changes: 1 addition & 17 deletions golang/pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ package utils

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

v2 "github.com/apache/rocketmq-clients/golang/v5/protocol/v2"
Expand Down Expand Up @@ -115,7 +113,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 && !strings.Contains(err.Error(), "unknown format") {
if err != gzip.ErrHeader {
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 @@ -127,20 +125,6 @@ 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

0 comments on commit 25428ac

Please sign in to comment.