mirror of
https://github.com/gin-gonic/gin.git
synced 2026-06-06 12:08:20 +08:00
Compare commits
5 Commits
52241aa97d
...
5bd9e3061b
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5bd9e3061b | ||
|
|
5f4f964325 | ||
|
|
8596512f4f | ||
|
|
18d33bd738 | ||
|
|
12ed1b1715 |
2
.github/workflows/gin.yml
vendored
2
.github/workflows/gin.yml
vendored
@ -78,6 +78,6 @@ jobs:
|
|||||||
run: make test
|
run: make test
|
||||||
|
|
||||||
- name: Upload coverage to Codecov
|
- name: Upload coverage to Codecov
|
||||||
uses: codecov/codecov-action@v5
|
uses: codecov/codecov-action@v6
|
||||||
with:
|
with:
|
||||||
flags: ${{ matrix.os }},go-${{ matrix.go }},${{ matrix.test-tags }}
|
flags: ${{ matrix.os }},go-${{ matrix.go }},${{ matrix.test-tags }}
|
||||||
|
|||||||
4
.github/workflows/trivy-scan.yml
vendored
4
.github/workflows/trivy-scan.yml
vendored
@ -27,7 +27,7 @@ jobs:
|
|||||||
fetch-depth: 0
|
fetch-depth: 0
|
||||||
|
|
||||||
- name: Run Trivy vulnerability scanner (source code)
|
- name: Run Trivy vulnerability scanner (source code)
|
||||||
uses: aquasecurity/trivy-action@0.35.0
|
uses: aquasecurity/trivy-action@v0.36.0
|
||||||
with:
|
with:
|
||||||
scan-type: "fs"
|
scan-type: "fs"
|
||||||
scan-ref: "."
|
scan-ref: "."
|
||||||
@ -44,7 +44,7 @@ jobs:
|
|||||||
sarif_file: "trivy-results.sarif"
|
sarif_file: "trivy-results.sarif"
|
||||||
|
|
||||||
- name: Run Trivy scanner (table output for logs)
|
- name: Run Trivy scanner (table output for logs)
|
||||||
uses: aquasecurity/trivy-action@0.35.0
|
uses: aquasecurity/trivy-action@v0.36.0
|
||||||
if: always()
|
if: always()
|
||||||
with:
|
with:
|
||||||
scan-type: "fs"
|
scan-type: "fs"
|
||||||
|
|||||||
42
gin.go
42
gin.go
@ -101,6 +101,8 @@ type Engine struct {
|
|||||||
// For example if /foo/ is requested but a route only exists for /foo, the
|
// For example if /foo/ is requested but a route only exists for /foo, the
|
||||||
// client is redirected to /foo with http status code 301 for GET requests
|
// client is redirected to /foo with http status code 301 for GET requests
|
||||||
// and 307 for all other request methods.
|
// and 307 for all other request methods.
|
||||||
|
// If this field is false then /foo and /foo/ are assumed the same and no redirection will happen,
|
||||||
|
// both endpoints will use the same handlers.
|
||||||
RedirectTrailingSlash bool
|
RedirectTrailingSlash bool
|
||||||
|
|
||||||
// RedirectFixedPath if enabled, the router tries to fix the current request path, if no
|
// RedirectFixedPath if enabled, the router tries to fix the current request path, if no
|
||||||
@ -712,21 +714,39 @@ func (engine *Engine) handleHTTPRequest(c *Context) {
|
|||||||
}
|
}
|
||||||
root := t[i].root
|
root := t[i].root
|
||||||
// Find route in tree
|
// Find route in tree
|
||||||
value := root.getValue(rPath, c.params, c.skippedNodes, unescape)
|
isRouteFound := func(value nodeValue) bool {
|
||||||
if value.params != nil {
|
if value.params != nil {
|
||||||
c.Params = *value.params
|
c.Params = *value.params
|
||||||
|
}
|
||||||
|
if value.handlers != nil {
|
||||||
|
c.handlers = value.handlers
|
||||||
|
c.fullPath = value.fullPath
|
||||||
|
c.Next()
|
||||||
|
c.writermem.WriteHeaderNow()
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
if value.handlers != nil {
|
|
||||||
c.handlers = value.handlers
|
value := root.getValue(rPath, c.params, c.skippedNodes, unescape)
|
||||||
c.fullPath = value.fullPath
|
if isRouteFound(value) {
|
||||||
c.Next()
|
|
||||||
c.writermem.WriteHeaderNow()
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if httpMethod != http.MethodConnect && rPath != "/" {
|
if httpMethod != http.MethodConnect && rPath != "/" {
|
||||||
if value.tsr && engine.RedirectTrailingSlash {
|
if value.tsr {
|
||||||
redirectTrailingSlash(c)
|
if engine.engine.RedirectTrailingSlash {
|
||||||
return
|
redirectTrailingSlash(c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
newRPath := rPath + "/"
|
||||||
|
if rPath[len(rPath)-1] == '/' {
|
||||||
|
newRPath = rPath[:len(rPath)-1]
|
||||||
|
}
|
||||||
|
value = root.getValue(newRPath, c.params, c.skippedNodes, unescape)
|
||||||
|
if isRouteFound(value) {
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if engine.RedirectFixedPath && redirectFixedPath(c, root, engine.RedirectFixedPath) {
|
if engine.RedirectFixedPath && redirectFixedPath(c, root, engine.RedirectFixedPath) {
|
||||||
return
|
return
|
||||||
|
|||||||
@ -237,13 +237,13 @@ func TestRouteRedirectTrailingSlash(t *testing.T) {
|
|||||||
router.RedirectTrailingSlash = false
|
router.RedirectTrailingSlash = false
|
||||||
|
|
||||||
w = PerformRequest(router, http.MethodGet, "/path/")
|
w = PerformRequest(router, http.MethodGet, "/path/")
|
||||||
assert.Equal(t, http.StatusNotFound, w.Code)
|
assert.Equal(t, http.StatusOK, w.Code)
|
||||||
w = PerformRequest(router, http.MethodGet, "/path2")
|
w = PerformRequest(router, http.MethodGet, "/path2")
|
||||||
assert.Equal(t, http.StatusNotFound, w.Code)
|
assert.Equal(t, http.StatusOK, w.Code)
|
||||||
w = PerformRequest(router, http.MethodPost, "/path3/")
|
w = PerformRequest(router, http.MethodPost, "/path3/")
|
||||||
assert.Equal(t, http.StatusNotFound, w.Code)
|
assert.Equal(t, http.StatusOK, w.Code)
|
||||||
w = PerformRequest(router, http.MethodPut, "/path4")
|
w = PerformRequest(router, http.MethodPut, "/path4")
|
||||||
assert.Equal(t, http.StatusNotFound, w.Code)
|
assert.Equal(t, http.StatusOK, w.Code)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRouteRedirectFixedPath(t *testing.T) {
|
func TestRouteRedirectFixedPath(t *testing.T) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user