forked from songquanpeng/one-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support gzip decode (songquanpeng#1962)
- Loading branch information
1 parent
6ab87f8
commit 7c8628b
Showing
2 changed files
with
28 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package middleware | ||
|
||
import ( | ||
"compress/gzip" | ||
"github.com/gin-gonic/gin" | ||
"io" | ||
"net/http" | ||
) | ||
|
||
func GzipDecodeMiddleware() gin.HandlerFunc { | ||
return func(c *gin.Context) { | ||
if c.GetHeader("Content-Encoding") == "gzip" { | ||
gzipReader, err := gzip.NewReader(c.Request.Body) | ||
if err != nil { | ||
c.AbortWithStatus(http.StatusBadRequest) | ||
return | ||
} | ||
defer gzipReader.Close() | ||
|
||
// Replace the request body with the decompressed data | ||
c.Request.Body = io.NopCloser(gzipReader) | ||
} | ||
|
||
// Continue processing the request | ||
c.Next() | ||
} | ||
} |
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