mirror of
https://github.com/kataras/iris.git
synced 2025-02-13 12:36:20 +01:00
add HashGenerator on requestid middleware
Former-commit-id: 8f59a46a3fecc538d6d3e624fc36f2c314c20bb6
This commit is contained in:
parent
4f1af5d135
commit
38eec57e46
|
@ -101,7 +101,7 @@ func postExample() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
defer cr.Close() // Closes the request body too.
|
defer cr.Close()
|
||||||
|
|
||||||
body, err := ioutil.ReadAll(cr)
|
body, err := ioutil.ReadAll(cr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -59,7 +59,7 @@ func (r resource) loadFromBase(dir string, strip string) string {
|
||||||
func TestFileServerBasic(t *testing.T) {
|
func TestFileServerBasic(t *testing.T) {
|
||||||
urls := []resource{
|
urls := []resource{
|
||||||
"/v1/static/css/main.css",
|
"/v1/static/css/main.css",
|
||||||
"/v1/static/js/jquery-2.1.1.js",
|
"/v1/static/js/main.js",
|
||||||
"/v1/static/favicon.ico",
|
"/v1/static/favicon.ico",
|
||||||
"/v1/static/app2",
|
"/v1/static/app2",
|
||||||
"/v1/static/app2/app2app3",
|
"/v1/static/app2/app2app3",
|
||||||
|
|
|
@ -123,7 +123,7 @@ func NewCompressReader(src io.Reader, encoding string) (*CompressReader, error)
|
||||||
case GZIP:
|
case GZIP:
|
||||||
rc, err = gzip.NewReader(src)
|
rc, err = gzip.NewReader(src)
|
||||||
case DEFLATE:
|
case DEFLATE:
|
||||||
rc = &noOpReadCloser{flate.NewReader(src)}
|
rc = flate.NewReader(src)
|
||||||
case BROTLI:
|
case BROTLI:
|
||||||
rc = &noOpReadCloser{brotli.NewReader(src)}
|
rc = &noOpReadCloser{brotli.NewReader(src)}
|
||||||
case SNAPPY:
|
case SNAPPY:
|
||||||
|
|
2
iris.go
2
iris.go
|
@ -28,7 +28,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// Version is the current version number of the Iris Web Framework.
|
// Version is the current version number of the Iris Web Framework.
|
||||||
const Version = "12.1.8"
|
const Version = "12.2.0"
|
||||||
|
|
||||||
// Byte unit helpers.
|
// Byte unit helpers.
|
||||||
const (
|
const (
|
||||||
|
|
|
@ -1,6 +1,10 @@
|
||||||
package requestid
|
package requestid
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/sha1"
|
||||||
|
"encoding/hex"
|
||||||
|
"net/http/httputil"
|
||||||
|
|
||||||
"github.com/kataras/iris/v12/context"
|
"github.com/kataras/iris/v12/context"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
@ -10,7 +14,7 @@ func init() {
|
||||||
context.SetHandlerName("iris/middleware/requestid.*", "iris.request.id")
|
context.SetHandlerName("iris/middleware/requestid.*", "iris.request.id")
|
||||||
}
|
}
|
||||||
|
|
||||||
const xRequestIDHeaderValue = "X-Request-Id"
|
const xRequestIDHeaderKey = "X-Request-Id"
|
||||||
|
|
||||||
// Generator defines the function which should extract or generate
|
// Generator defines the function which should extract or generate
|
||||||
// a Request ID. See `DefaultGenerator` and `New` package-level functions.
|
// a Request ID. See `DefaultGenerator` and `New` package-level functions.
|
||||||
|
@ -23,8 +27,12 @@ type Generator func(ctx *context.Context) string
|
||||||
//
|
//
|
||||||
// See `Get` package-level function too.
|
// See `Get` package-level function too.
|
||||||
var DefaultGenerator Generator = func(ctx *context.Context) string {
|
var DefaultGenerator Generator = func(ctx *context.Context) string {
|
||||||
id := ctx.GetHeader(xRequestIDHeaderValue)
|
id := ctx.ResponseWriter().Header().Get(xRequestIDHeaderKey)
|
||||||
|
if id != "" {
|
||||||
|
return id
|
||||||
|
}
|
||||||
|
|
||||||
|
id = ctx.GetHeader(xRequestIDHeaderKey)
|
||||||
if id == "" {
|
if id == "" {
|
||||||
uid, err := uuid.NewRandom()
|
uid, err := uuid.NewRandom()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -33,12 +41,21 @@ var DefaultGenerator Generator = func(ctx *context.Context) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
id = uid.String()
|
id = uid.String()
|
||||||
ctx.Header(xRequestIDHeaderValue, id)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ctx.Header(xRequestIDHeaderKey, id)
|
||||||
return id
|
return id
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// HashGenerator uses the request's hash to generate a fixed-length Request ID.
|
||||||
|
// Note that one or many requests may contain the same ID, so it's not unique.
|
||||||
|
func HashGenerator(includeBody bool) Generator {
|
||||||
|
return func(ctx *context.Context) string {
|
||||||
|
ctx.Header(xRequestIDHeaderKey, Hash(ctx, includeBody))
|
||||||
|
return DefaultGenerator(ctx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// New returns a new request id middleware.
|
// New returns a new request id middleware.
|
||||||
// It optionally accepts an ID Generator.
|
// It optionally accepts an ID Generator.
|
||||||
// The Generator can stop the handlers chain with an error or
|
// The Generator can stop the handlers chain with an error or
|
||||||
|
@ -81,3 +98,15 @@ func Get(ctx *context.Context) string {
|
||||||
|
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Hash returns the sha1 hash of the request.
|
||||||
|
// It does not capture error, instead it returns an empty string.
|
||||||
|
func Hash(ctx *context.Context, includeBody bool) string {
|
||||||
|
h := sha1.New()
|
||||||
|
b, err := httputil.DumpRequest(ctx.Request(), includeBody)
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
h.Write(b)
|
||||||
|
return hex.EncodeToString(h.Sum(nil))
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user