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 8, 2025
1 parent 25428ac commit c920563
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
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

0 comments on commit c920563

Please sign in to comment.