mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2025-12-14 18:47:28 -06:00
[feature] request blocking by http headers (#2409)
This commit is contained in:
parent
07bd848028
commit
8ebb7775a3
36 changed files with 2561 additions and 81 deletions
|
|
@ -18,21 +18,22 @@
|
|||
package middleware
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
apiutil "github.com/superseriousbusiness/gotosocial/internal/api/util"
|
||||
)
|
||||
|
||||
// UserAgent returns a gin middleware which aborts requests with
|
||||
// empty user agent strings, returning code 418 - I'm a teapot.
|
||||
func UserAgent() gin.HandlerFunc {
|
||||
// todo: make this configurable
|
||||
var rsp = []byte(`{"error": "I'm a teapot: no user-agent sent with request"}`)
|
||||
return func(c *gin.Context) {
|
||||
if ua := c.Request.UserAgent(); ua == "" {
|
||||
code := http.StatusTeapot
|
||||
err := errors.New(http.StatusText(code) + ": no user-agent sent with request")
|
||||
c.AbortWithStatusJSON(code, gin.H{"error": err.Error()})
|
||||
apiutil.Data(c,
|
||||
http.StatusTeapot, apiutil.AppJSON, rsp)
|
||||
c.Abort()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue