Compare commits

...

8 Commits

Author SHA1 Message Date
ljluestc
20bfe1dd50
Merge 4c81c2a933738dc74934576edc3173eaa688c769 into d1a15347b1e45a8ee816193d3578a93bfd73b70f 2025-12-13 13:24:12 -08:00
OHZEKI Naoki
d1a15347b1
refactor(utils): move util functions to utils.go (#4467)
Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com>
2025-12-12 13:43:25 +08:00
Name
64a6ed9a41
perf(recovery): optimize line reading in stack function (#4466)
Co-authored-by: 1911860538 <alxps1911@gmail.com>
2025-12-12 13:42:03 +08:00
ljluestc
4c81c2a933 fix tests 2025-11-29 14:45:35 -08:00
ljluestc
f6e887d460 docs: remove GetRequestBody documentation changes 2025-11-29 14:28:12 -08:00
ljluestc
b296dbea8b fix context 2025-11-29 14:04:30 -08:00
ljluestc
51137fbe43 docs: add GetRequestBody() method documentation
- Add 'Get Request Body' section to docs/doc.md
- Include usage examples and curl test commands
- Document the new API for reusable request body access
2025-11-25 14:08:01 -08:00
ljluestc
9e193dc13c feat: add GetRequestBody() method for reusable request body access
- Add Context.GetRequestBody() method that caches request body bytes
- Allows multiple reads of request body without consumption
- Integrates with existing ShouldBindBodyWith* caching mechanism
- Includes comprehensive tests and usage examples
- Addresses issue #1974: Wrap Request.Body for reuse by Gin, middlewares and app code
2025-11-25 13:56:26 -08:00
9 changed files with 390 additions and 50 deletions

View File

@ -5,6 +5,7 @@
package gin
import (
"bytes"
"errors"
"fmt"
"io"
@ -55,14 +56,6 @@ const ContextRequestKey ContextKeyType = 0
// abortIndex represents a typical value used in abort functions.
const abortIndex int8 = math.MaxInt8 >> 1
// safeInt8 converts int to int8 safely, capping at math.MaxInt8
func safeInt8(n int) int8 {
if n > math.MaxInt8 {
return math.MaxInt8
}
return int8(n)
}
// Context is the most important part of gin. It allows us to pass variables between middleware,
// manage the flow, validate the JSON of a request and render a JSON response for example.
type Context struct {
@ -914,6 +907,24 @@ func (c *Context) ShouldBindUri(obj any) error {
// ShouldBindWith binds the passed struct pointer using the specified binding engine.
// See the binding package.
func (c *Context) ShouldBindWith(obj any, b binding.Binding) error {
if b.Name() == "form-urlencoded" || b.Name() == "form" {
var body []byte
if cb, ok := c.Get(BodyBytesKey); ok {
if cbb, ok := cb.([]byte); ok {
body = cbb
}
}
if body == nil {
var err error
body, err = io.ReadAll(c.Request.Body)
if err != nil {
return err
}
c.Set(BodyBytesKey, body)
}
c.Request.Body = io.NopCloser(bytes.NewBuffer(body))
}
return b.Bind(c.Request, obj)
}
@ -1077,9 +1088,43 @@ func (c *Context) GetRawData() ([]byte, error) {
if c.Request.Body == nil {
return nil, errors.New("cannot read nil body")
}
if cb, ok := c.Get(BodyBytesKey); ok {
if cbb, ok := cb.([]byte); ok {
return cbb, nil
}
}
return io.ReadAll(c.Request.Body)
}
// GetRequestBody returns the request body as bytes, caching it for reuse.
// This allows the body to be read multiple times without being consumed.
// Returns an error if the body cannot be read or if it's nil.
func (c *Context) GetRequestBody() ([]byte, error) {
if c.Request.Body == nil {
return nil, errors.New("cannot read nil body")
}
// Check if body is already cached
if cb, ok := c.Get(BodyBytesKey); ok {
if cbb, ok := cb.([]byte); ok {
return cbb, nil
}
}
// Read and cache the body
body, err := io.ReadAll(c.Request.Body)
if err != nil {
return nil, err
}
// Cache the body for future use
c.Set(BodyBytesKey, body)
return body, nil
}
// SetSameSite with cookie
func (c *Context) SetSameSite(samesite http.SameSite) {
c.sameSite = samesite

View File

@ -2349,6 +2349,28 @@ func TestContextShouldBindWithQuery(t *testing.T) {
assert.Equal(t, 0, w.Body.Len())
}
func TestContextGetRawDataAfterShouldBind(t *testing.T) {
w := httptest.NewRecorder()
c, _ := CreateTestContext(w)
c.Request, _ = http.NewRequest(http.MethodPost, "/", strings.NewReader("p1=1&p2=2&p3=4"))
c.Request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
var s struct {
P1 string `form:"p1"`
P2 string `form:"p2"`
P3 string `form:"p3"`
}
require.NoError(t, c.ShouldBind(&s))
assert.Equal(t, "1", s.P1)
assert.Equal(t, "2", s.P2)
assert.Equal(t, "4", s.P3)
rawData, err := c.GetRawData()
require.NoError(t, err)
assert.Equal(t, "p1=1&p2=2&p3=4", string(rawData))
}
func TestContextShouldBindWithYAML(t *testing.T) {
w := httptest.NewRecorder()
c, _ := CreateTestContext(w)
@ -2872,6 +2894,105 @@ func TestContextGetRawData(t *testing.T) {
assert.Equal(t, "Fetch binary post data", string(data))
}
func TestContextGetRequestBody(t *testing.T) {
// Test basic functionality
t.Run("basic functionality", func(t *testing.T) {
c, _ := CreateTestContext(httptest.NewRecorder())
bodyContent := "test request body data"
body := strings.NewReader(bodyContent)
c.Request, _ = http.NewRequest(http.MethodPost, "/", body)
data, err := c.GetRequestBody()
require.NoError(t, err)
assert.Equal(t, bodyContent, string(data))
})
// Test multiple calls return cached data
t.Run("multiple calls return cached data", func(t *testing.T) {
c, _ := CreateTestContext(httptest.NewRecorder())
bodyContent := "reusable request body"
body := strings.NewReader(bodyContent)
c.Request, _ = http.NewRequest(http.MethodPost, "/", body)
// First call
data1, err1 := c.GetRequestBody()
require.NoError(t, err1)
assert.Equal(t, bodyContent, string(data1))
// Second call should return cached data
data2, err2 := c.GetRequestBody()
require.NoError(t, err2)
assert.Equal(t, bodyContent, string(data2))
// Third call should also return cached data
data3, err3 := c.GetRequestBody()
require.NoError(t, err3)
assert.Equal(t, bodyContent, string(data3))
// All calls should return the same data
assert.Equal(t, data1, data2)
assert.Equal(t, data2, data3)
})
// Test nil body error
t.Run("nil body error", func(t *testing.T) {
c, _ := CreateTestContext(httptest.NewRecorder())
c.Request, _ = http.NewRequest(http.MethodPost, "/", nil)
c.Request.Body = nil
data, err := c.GetRequestBody()
assert.Error(t, err)
assert.Nil(t, data)
assert.Contains(t, err.Error(), "cannot read nil body")
})
// Test empty body
t.Run("empty body", func(t *testing.T) {
c, _ := CreateTestContext(httptest.NewRecorder())
body := strings.NewReader("")
c.Request, _ = http.NewRequest(http.MethodPost, "/", body)
data, err := c.GetRequestBody()
require.NoError(t, err)
assert.Equal(t, "", string(data))
assert.Equal(t, 0, len(data))
})
// Test large body
t.Run("large body", func(t *testing.T) {
c, _ := CreateTestContext(httptest.NewRecorder())
largeContent := strings.Repeat("large body content ", 1000)
body := strings.NewReader(largeContent)
c.Request, _ = http.NewRequest(http.MethodPost, "/", body)
data, err := c.GetRequestBody()
require.NoError(t, err)
assert.Equal(t, largeContent, string(data))
assert.Equal(t, len(largeContent), len(data))
})
// Test integration with ShouldBindBodyWith
t.Run("integration with ShouldBindBodyWith", func(t *testing.T) {
c, _ := CreateTestContext(httptest.NewRecorder())
jsonBody := `{"name":"test","value":123}`
body := strings.NewReader(jsonBody)
c.Request, _ = http.NewRequest(http.MethodPost, "/", body)
c.Request.Header.Set("Content-Type", "application/json")
// Get body first
bodyData, err := c.GetRequestBody()
require.NoError(t, err)
assert.Equal(t, jsonBody, string(bodyData))
// ShouldBindBodyWith should still work and reuse cached body
var jsonData map[string]interface{}
err = c.ShouldBindBodyWithJSON(&jsonData)
require.NoError(t, err)
assert.Equal(t, "test", jsonData["name"])
assert.Equal(t, float64(123), jsonData["value"])
})
}
func TestContextRenderDataFromReader(t *testing.T) {
w := httptest.NewRecorder()
c, _ := CreateTestContext(w)

View File

@ -0,0 +1,86 @@
package main
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
// Example middleware that reads the request body
r.Use(func(c *gin.Context) {
// Get the request body - this can be called multiple times
body, err := c.GetRequestBody()
if err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
}
// Log the body length for demonstration
fmt.Printf("Middleware: Request body length: %d bytes\n", len(body))
// Store body in context for use by handlers
c.Set("rawBody", body)
c.Next()
})
// Handler that also reads the body
r.POST("/echo", func(c *gin.Context) {
// Get the body again - this will use the cached version
body, err := c.GetRequestBody()
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
// Also get the body that was stored by middleware
storedBody, exists := c.Get("rawBody")
if !exists {
c.JSON(http.StatusInternalServerError, gin.H{"error": "body not found in context"})
return
}
// Both should be identical
if string(body) != string(storedBody.([]byte)) {
c.JSON(http.StatusInternalServerError, gin.H{"error": "bodies don't match"})
return
}
c.JSON(http.StatusOK, gin.H{
"message": "Body received and cached successfully",
"body": string(body),
"length": len(body),
})
})
// Handler that uses binding after GetRequestBody
r.POST("/bind-after-body", func(c *gin.Context) {
// First bind to a struct - this caches the body
var jsonData map[string]interface{}
if err := c.ShouldBindBodyWithJSON(&jsonData); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
// Then get the raw body - this will use the cached version
rawBody, err := c.GetRequestBody()
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{
"raw_body": string(rawBody),
"parsed_data": jsonData,
})
})
fmt.Println("Server starting on :9090")
fmt.Println("Try: curl -X POST -d 'hello world' http://localhost:9090/echo")
fmt.Println("Or: curl -X POST -H 'Content-Type: application/json' -d '{\"name\":\"test\"}' http://localhost:9090/bind-after-body")
r.Run(":9090")
}

View File

@ -16,6 +16,7 @@ import (
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"sync"
"testing"
@ -64,7 +65,16 @@ func testRequest(t *testing.T, params ...string) {
}
func TestRunEmpty(t *testing.T) {
os.Setenv("PORT", "")
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
require.NoError(t, err)
l, err := net.ListenTCP("tcp", addr)
require.NoError(t, err)
port := strconv.Itoa(l.Addr().(*net.TCPAddr).Port)
l.Close()
os.Setenv("PORT", port)
defer os.Unsetenv("PORT")
router := New()
go func() {
router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
@ -75,8 +85,8 @@ func TestRunEmpty(t *testing.T) {
err := waitForServerReady("http://localhost:8080/example", 10)
require.NoError(t, err, "server should start successfully")
require.Error(t, router.Run(":8080"))
testRequest(t, "http://localhost:8080/example")
require.Error(t, router.Run(":"+port))
testRequest(t, "http://localhost:"+port+"/example")
}
func TestBadTrustedCIDRs(t *testing.T) {

View File

@ -5,7 +5,9 @@
package gin
import (
"bufio"
"bytes"
"cmp"
"errors"
"fmt"
"io"
@ -21,9 +23,10 @@ import (
"github.com/gin-gonic/gin/internal/bytesconv"
)
const dunno = "???"
var dunnoBytes = []byte(dunno)
const (
dunno = "???"
stackSkip = 3
)
// RecoveryFunc defines the function passable to CustomRecovery.
type RecoveryFunc func(c *Context, err any)
@ -72,7 +75,6 @@ func CustomRecoveryWithWriter(out io.Writer, handle RecoveryFunc) HandlerFunc {
brokenPipe = true
}
if logger != nil {
const stackSkip = 3
if brokenPipe {
logger.Printf("%s\n%s%s", err, secureRequestDump(c.Request), reset)
} else if IsDebugging() {
@ -120,8 +122,11 @@ func stack(skip int) []byte {
buf := new(bytes.Buffer) // the returned data
// As we loop, we open files and read them. These variables record the currently
// loaded file.
var lines [][]byte
var lastFile string
var (
nLine string
lastFile string
err error
)
for i := skip; ; i++ { // Skip the expected number of frames
pc, file, line, ok := runtime.Caller(i)
if !ok {
@ -130,25 +135,44 @@ func stack(skip int) []byte {
// Print this much at least. If we can't find the source, it won't show.
fmt.Fprintf(buf, "%s:%d (0x%x)\n", file, line, pc)
if file != lastFile {
data, err := os.ReadFile(file)
nLine, err = readNthLine(file, line-1)
if err != nil {
continue
}
lines = bytes.Split(data, []byte{'\n'})
lastFile = file
}
fmt.Fprintf(buf, "\t%s: %s\n", function(pc), source(lines, line))
fmt.Fprintf(buf, "\t%s: %s\n", function(pc), cmp.Or(nLine, dunno))
}
return buf.Bytes()
}
// source returns a space-trimmed slice of the n'th line.
func source(lines [][]byte, n int) []byte {
n-- // in stack trace, lines are 1-indexed but our array is 0-indexed
if n < 0 || n >= len(lines) {
return dunnoBytes
// readNthLine reads the nth line from the file.
// It returns the trimmed content of the line if found,
// or an empty string if the line doesn't exist.
// If there's an error opening the file, it returns the error.
func readNthLine(file string, n int) (string, error) {
if n < 0 {
return "", nil
}
return bytes.TrimSpace(lines[n])
f, err := os.Open(file)
if err != nil {
return "", err
}
defer f.Close()
scanner := bufio.NewScanner(f)
for i := 0; i < n; i++ {
if !scanner.Scan() {
return "", nil
}
}
if scanner.Scan() {
return strings.TrimSpace(scanner.Text()), nil
}
return "", nil
}
// function returns, if possible, the name of the function containing the PC.

View File

@ -88,21 +88,6 @@ func TestPanicWithAbort(t *testing.T) {
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestSource(t *testing.T) {
bs := source(nil, 0)
assert.Equal(t, dunnoBytes, bs)
in := [][]byte{
[]byte("Hello world."),
[]byte("Hi, gin.."),
}
bs = source(in, 10)
assert.Equal(t, dunnoBytes, bs)
bs = source(in, 1)
assert.Equal(t, []byte("Hello world."), bs)
}
func TestFunction(t *testing.T) {
bs := function(1)
assert.Equal(t, dunno, bs)
@ -331,3 +316,53 @@ func TestSecureRequestDump(t *testing.T) {
})
}
}
// TestReadNthLine tests the readNthLine function with various scenarios.
func TestReadNthLine(t *testing.T) {
// Create a temporary test file
testContent := "line 0 \n line 1 \nline 2 \nline 3 \nline 4"
tempFile, err := os.CreateTemp("", "testfile*.txt")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tempFile.Name())
// Write test content to the temporary file
if _, err := tempFile.WriteString(testContent); err != nil {
t.Fatal(err)
}
if err := tempFile.Close(); err != nil {
t.Fatal(err)
}
// Test cases
tests := []struct {
name string
lineNum int
fileName string
want string
wantErr bool
}{
{name: "Read first line", lineNum: 0, fileName: tempFile.Name(), want: "line 0", wantErr: false},
{name: "Read middle line", lineNum: 2, fileName: tempFile.Name(), want: "line 2", wantErr: false},
{name: "Read last line", lineNum: 4, fileName: tempFile.Name(), want: "line 4", wantErr: false},
{name: "Line number exceeds file length", lineNum: 10, fileName: tempFile.Name(), want: "", wantErr: false},
{name: "Negative line number", lineNum: -1, fileName: tempFile.Name(), want: "", wantErr: false},
{name: "Non-existent file", lineNum: 1, fileName: "/non/existent/file.txt", want: "", wantErr: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := readNthLine(tt.fileName, tt.lineNum)
assert.Equal(t, tt.wantErr, err != nil)
assert.Equal(t, tt.want, got)
})
}
}
func BenchmarkStack(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
_ = stack(stackSkip)
}
}

View File

@ -5,7 +5,6 @@
package gin
import (
"math"
"net/url"
"strings"
"unicode"
@ -78,14 +77,6 @@ func (n *node) addChild(child *node) {
}
}
// safeUint16 converts int to uint16 safely, capping at math.MaxUint16
func safeUint16(n int) uint16 {
if n > math.MaxUint16 {
return math.MaxUint16
}
return uint16(n)
}
func countParams(path string) uint16 {
colons := strings.Count(path, ":")
stars := strings.Count(path, "*")

View File

@ -6,6 +6,7 @@ package gin
import (
"encoding/xml"
"math"
"net/http"
"os"
"path"
@ -162,3 +163,19 @@ func isASCII(s string) bool {
}
return true
}
// safeInt8 converts int to int8 safely, capping at math.MaxInt8
func safeInt8(n int) int8 {
if n > math.MaxInt8 {
return math.MaxInt8
}
return int8(n)
}
// safeUint16 converts int to uint16 safely, capping at math.MaxUint16
func safeUint16(n int) uint16 {
if n > math.MaxUint16 {
return math.MaxUint16
}
return uint16(n)
}

View File

@ -8,6 +8,7 @@ import (
"bytes"
"encoding/xml"
"fmt"
"math"
"net/http"
"testing"
@ -148,3 +149,13 @@ func TestIsASCII(t *testing.T) {
assert.True(t, isASCII("test"))
assert.False(t, isASCII("🧡💛💚💙💜"))
}
func TestSafeInt8(t *testing.T) {
assert.Equal(t, int8(100), safeInt8(100))
assert.Equal(t, int8(math.MaxInt8), safeInt8(int(math.MaxInt8)+123))
}
func TestSafeUint16(t *testing.T) {
assert.Equal(t, uint16(100), safeUint16(100))
assert.Equal(t, uint16(math.MaxUint16), safeUint16(int(math.MaxUint16)+123))
}