-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
130 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,83 @@ | ||
package traffic | ||
|
||
import ( | ||
"bytes" | ||
"compress/gzip" | ||
"io" | ||
"net/http" | ||
"net/url" | ||
) | ||
|
||
// GetBodyReader checks if the request Content-Encoding or request query parameter indicates gzip compression. | ||
func GetContentEncoding(request *http.Request) (string, error) { | ||
queryParams, err := url.ParseQuery(request.URL.RawQuery) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
encoding := queryParams.Get("ContentEncoding") | ||
if encoding == "" { | ||
encoding = request.Header.Get("Content-Encoding") | ||
} | ||
return encoding, nil | ||
} | ||
|
||
// WrapReader checks if the request Content-Encoding or request query parameter indicates gzip compression. | ||
// If so, it returns a gzip.Reader that decompresses the content. | ||
func GetBodyReader(request *http.Request) (io.ReadCloser, error) { | ||
func WrapReader(request *http.Request, encoding string) (io.ReadCloser, error) { | ||
if request.Body == nil || request.Body == http.NoBody { | ||
return nil, nil | ||
} | ||
|
||
queryParams, err := url.ParseQuery(request.URL.RawQuery) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if request.Header.Get("Content-Encoding") == "gzip" || queryParams.Get("ContentEncoding") == "gzip" { | ||
switch encoding { | ||
case "gzip": | ||
// Create a new gzip.Reader to decompress the request body | ||
return gzip.NewReader(request.Body) | ||
default: | ||
// If the content is not gzip-compressed, return the original request body | ||
return request.Body, nil | ||
} | ||
} | ||
|
||
func EncodeData(data []byte, encoding string) ([]byte, error) { | ||
switch encoding { | ||
case "gzip": | ||
var buf bytes.Buffer | ||
gz := gzip.NewWriter(&buf) | ||
|
||
_, err := gz.Write(data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = gz.Close() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
compressedData := buf.Bytes() | ||
return compressedData, nil | ||
default: | ||
// identity encoding | ||
return data, nil | ||
} | ||
} | ||
|
||
func DecodeData(data []byte, encoding string) ([]byte, error) { | ||
switch encoding { | ||
case "gzip": | ||
reader, err := gzip.NewReader(bytes.NewReader(data)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// If the content is not gzip-compressed, return the original request body | ||
return request.Body, nil | ||
decodedData, err := io.ReadAll(reader) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return decodedData, nil | ||
default: | ||
// identity encoding | ||
return data, nil | ||
} | ||
} |