diff --git a/_examples/compression/client-using-iris/main.go b/_examples/compression/client-using-iris/main.go index 59775aab..65d97131 100644 --- a/_examples/compression/client-using-iris/main.go +++ b/_examples/compression/client-using-iris/main.go @@ -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 { diff --git a/_examples/file-server/basic/assets/js/jquery-2.1.1.js b/_examples/file-server/basic/assets/js/main.js similarity index 100% rename from _examples/file-server/basic/assets/js/jquery-2.1.1.js rename to _examples/file-server/basic/assets/js/main.js diff --git a/_examples/file-server/basic/main_test.go b/_examples/file-server/basic/main_test.go index d53b1a68..e67ca695 100644 --- a/_examples/file-server/basic/main_test.go +++ b/_examples/file-server/basic/main_test.go @@ -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", diff --git a/context/compress.go b/context/compress.go index 440e3eba..ea8f3241 100644 --- a/context/compress.go +++ b/context/compress.go @@ -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: diff --git a/iris.go b/iris.go index 94c7f34e..586572c3 100644 --- a/iris.go +++ b/iris.go @@ -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 ( diff --git a/middleware/requestid/requestid.go b/middleware/requestid/requestid.go index 103a68a3..9c242454 100644 --- a/middleware/requestid/requestid.go +++ b/middleware/requestid/requestid.go @@ -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)) +}