add HashGenerator on requestid middleware

Former-commit-id: 8f59a46a3fecc538d6d3e624fc36f2c314c20bb6
This commit is contained in:
Gerasimos (Makis) Maropoulos 2020-07-15 11:18:40 +03:00
parent 4f1af5d135
commit 38eec57e46
6 changed files with 36 additions and 7 deletions

View File

@ -101,7 +101,7 @@ func postExample() {
if err != nil {
panic(err)
}
defer cr.Close() // Closes the request body too.
defer cr.Close()
body, err := ioutil.ReadAll(cr)
if err != nil {

View File

@ -59,7 +59,7 @@ func (r resource) loadFromBase(dir string, strip string) string {
func TestFileServerBasic(t *testing.T) {
urls := []resource{
"/v1/static/css/main.css",
"/v1/static/js/jquery-2.1.1.js",
"/v1/static/js/main.js",
"/v1/static/favicon.ico",
"/v1/static/app2",
"/v1/static/app2/app2app3",

View File

@ -123,7 +123,7 @@ func NewCompressReader(src io.Reader, encoding string) (*CompressReader, error)
case GZIP:
rc, err = gzip.NewReader(src)
case DEFLATE:
rc = &noOpReadCloser{flate.NewReader(src)}
rc = flate.NewReader(src)
case BROTLI:
rc = &noOpReadCloser{brotli.NewReader(src)}
case SNAPPY:

View File

@ -28,7 +28,7 @@ import (
)
// Version is the current version number of the Iris Web Framework.
const Version = "12.1.8"
const Version = "12.2.0"
// Byte unit helpers.
const (

View File

@ -1,6 +1,10 @@
package requestid
import (
"crypto/sha1"
"encoding/hex"
"net/http/httputil"
"github.com/kataras/iris/v12/context"
"github.com/google/uuid"
@ -10,7 +14,7 @@ func init() {
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
// 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.
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 == "" {
uid, err := uuid.NewRandom()
if err != nil {
@ -33,12 +41,21 @@ var DefaultGenerator Generator = func(ctx *context.Context) string {
}
id = uid.String()
ctx.Header(xRequestIDHeaderValue, id)
}
ctx.Header(xRequestIDHeaderKey, 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.
// It optionally accepts an ID Generator.
// The Generator can stop the handlers chain with an error or
@ -81,3 +98,15 @@ func Get(ctx *context.Context) string {
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))
}