mirror of
https://github.com/kataras/iris.git
synced 2025-02-02 15:30:36 +01:00
Update to 4.5.2
This commit is contained in:
parent
40b000c20f
commit
9bce4e846a
33
HISTORY.md
33
HISTORY.md
|
@ -2,6 +2,39 @@
|
||||||
|
|
||||||
**How to upgrade**: remove your `$GOPATH/src/github.com/kataras` folder, open your command-line and execute this command: `go get -u github.com/kataras/iris/iris`.
|
**How to upgrade**: remove your `$GOPATH/src/github.com/kataras` folder, open your command-line and execute this command: `go get -u github.com/kataras/iris/iris`.
|
||||||
|
|
||||||
|
|
||||||
|
## 4.5.1 -> 4.5.2
|
||||||
|
|
||||||
|
- **Feature request**: I never though that it will be easier for users to catch 405 instead of simple 404, I though that will make your life harder, but it's requested by the Community [here](https://github.com/kataras/iris/issues/469), so I did my duty. Enable firing Status Method Not Allowed (405) with a simple configuration field: `iris.Config.FireMethodNotAllowed=true` or `iris.Set(iris.OptionFireMethodNotAllowed(true))` or `app := iris.New(iris.Configuration{FireMethodNotAllowed:true})`, a test example:
|
||||||
|
|
||||||
|
```go
|
||||||
|
|
||||||
|
func TestMuxFireMethodNotAllowed(t *testing.T) {
|
||||||
|
|
||||||
|
iris.Config.FireMethodNotAllowed = true // enable catching 405 errors
|
||||||
|
|
||||||
|
h := func(ctx *iris.Context) {
|
||||||
|
ctx.Write("%s", ctx.MethodString())
|
||||||
|
}
|
||||||
|
|
||||||
|
Iris.OnError(iris.StatusMethodNotAllowed, func(ctx *iris.Context) {
|
||||||
|
ctx.Write("Hello from my custom 405 page")
|
||||||
|
})
|
||||||
|
|
||||||
|
iris.Get("/mypath", h)
|
||||||
|
iris.Put("/mypath", h)
|
||||||
|
|
||||||
|
e := iris.Tester(t)
|
||||||
|
|
||||||
|
e.GET("/mypath").Expect().Status(StatusOK).Body().Equal("GET")
|
||||||
|
e.PUT("/mypath").Expect().Status(StatusOK).Body().Equal("PUT")
|
||||||
|
// this should fail with 405 and catch by the custom http error
|
||||||
|
|
||||||
|
e.POST("/mypath").Expect().Status(StatusMethodNotAllowed).Body().Equal("Hello from my custom 405 page")
|
||||||
|
iris.Close()
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
## 4.5.0 -> 4.5.1
|
## 4.5.0 -> 4.5.1
|
||||||
|
|
||||||
- **NEW**: `PreBuild` plugin type, raises before `.Build`. Used by third-party plugins to register any runtime routes or make any changes to the iris main configuration, example of this usage is the [OAuth/OAuth2 Plugin](https://github.com/iris-contrib/plugin/tree/master/oauth).
|
- **NEW**: `PreBuild` plugin type, raises before `.Build`. Used by third-party plugins to register any runtime routes or make any changes to the iris main configuration, example of this usage is the [OAuth/OAuth2 Plugin](https://github.com/iris-contrib/plugin/tree/master/oauth).
|
||||||
|
|
|
@ -19,7 +19,7 @@
|
||||||
<br/>
|
<br/>
|
||||||
|
|
||||||
|
|
||||||
<a href="https://github.com/kataras/iris/releases"><img src="https://img.shields.io/badge/%20version%20-%204.5.1%20-blue.svg?style=flat-square" alt="Releases"></a>
|
<a href="https://github.com/kataras/iris/releases"><img src="https://img.shields.io/badge/%20version%20-%204.5.2%20-blue.svg?style=flat-square" alt="Releases"></a>
|
||||||
|
|
||||||
<a href="https://github.com/iris-contrib/examples"><img src="https://img.shields.io/badge/%20examples-repository-3362c2.svg?style=flat-square" alt="Examples"></a>
|
<a href="https://github.com/iris-contrib/examples"><img src="https://img.shields.io/badge/%20examples-repository-3362c2.svg?style=flat-square" alt="Examples"></a>
|
||||||
|
|
||||||
|
@ -871,7 +871,7 @@ I recommend writing your API tests using this new library, [httpexpect](https://
|
||||||
Versioning
|
Versioning
|
||||||
------------
|
------------
|
||||||
|
|
||||||
Current: **v4.5.1**
|
Current: **v4.5.2**
|
||||||
|
|
||||||
> Iris is an active project
|
> Iris is an active project
|
||||||
|
|
||||||
|
@ -907,7 +907,7 @@ This project is licensed under the [MIT License](LICENSE), Copyright (c) 2016 Ge
|
||||||
[Travis]: http://travis-ci.org/kataras/iris
|
[Travis]: http://travis-ci.org/kataras/iris
|
||||||
[License Widget]: https://img.shields.io/badge/license-MIT%20%20License%20-E91E63.svg?style=flat-square
|
[License Widget]: https://img.shields.io/badge/license-MIT%20%20License%20-E91E63.svg?style=flat-square
|
||||||
[License]: https://github.com/kataras/iris/blob/master/LICENSE
|
[License]: https://github.com/kataras/iris/blob/master/LICENSE
|
||||||
[Release Widget]: https://img.shields.io/badge/release-4.5.1%20-blue.svg?style=flat-square
|
[Release Widget]: https://img.shields.io/badge/release-4.5.2%20-blue.svg?style=flat-square
|
||||||
[Release]: https://github.com/kataras/iris/releases
|
[Release]: https://github.com/kataras/iris/releases
|
||||||
[Chat Widget]: https://img.shields.io/badge/community-chat%20-00BCD4.svg?style=flat-square
|
[Chat Widget]: https://img.shields.io/badge/community-chat%20-00BCD4.svg?style=flat-square
|
||||||
[Chat]: https://kataras.rocket.chat/channel/iris
|
[Chat]: https://kataras.rocket.chat/channel/iris
|
||||||
|
|
|
@ -1,15 +1,16 @@
|
||||||
package iris
|
package iris
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/imdario/mergo"
|
|
||||||
"github.com/kataras/go-options"
|
|
||||||
"github.com/kataras/go-sessions"
|
|
||||||
"github.com/valyala/fasthttp"
|
|
||||||
"io"
|
"io"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/imdario/mergo"
|
||||||
|
"github.com/kataras/go-options"
|
||||||
|
"github.com/kataras/go-sessions"
|
||||||
|
"github.com/valyala/fasthttp"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
|
@ -160,6 +161,10 @@ type Configuration struct {
|
||||||
// Default is false
|
// Default is false
|
||||||
DisablePathEscape bool
|
DisablePathEscape bool
|
||||||
|
|
||||||
|
// FireMethodNotAllowed if it's true router checks for StatusMethodNotAllowed(405) and fires the 405 error instead of 404
|
||||||
|
// Default is false
|
||||||
|
FireMethodNotAllowed bool
|
||||||
|
|
||||||
// DisableBanner outputs the iris banner at startup
|
// DisableBanner outputs the iris banner at startup
|
||||||
//
|
//
|
||||||
// Default is false
|
// Default is false
|
||||||
|
@ -381,6 +386,14 @@ var (
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FireMethodNotAllowed if it's true router checks for StatusMethodNotAllowed(405) and fires the 405 error instead of 404
|
||||||
|
// Default is false
|
||||||
|
OptionFireMethodNotAllowed = func(val bool) OptionSet {
|
||||||
|
return func(c *Configuration) {
|
||||||
|
c.FireMethodNotAllowed = val
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// OptionDisableBanner outputs the iris banner at startup
|
// OptionDisableBanner outputs the iris banner at startup
|
||||||
//
|
//
|
||||||
// Default is false
|
// Default is false
|
||||||
|
@ -523,6 +536,7 @@ func DefaultConfiguration() Configuration {
|
||||||
CheckForUpdatesSync: false,
|
CheckForUpdatesSync: false,
|
||||||
DisablePathCorrection: DefaultDisablePathCorrection,
|
DisablePathCorrection: DefaultDisablePathCorrection,
|
||||||
DisablePathEscape: DefaultDisablePathEscape,
|
DisablePathEscape: DefaultDisablePathEscape,
|
||||||
|
FireMethodNotAllowed: false,
|
||||||
DisableBanner: false,
|
DisableBanner: false,
|
||||||
LoggerOut: DefaultLoggerOut,
|
LoggerOut: DefaultLoggerOut,
|
||||||
LoggerPreffix: DefaultLoggerPreffix,
|
LoggerPreffix: DefaultLoggerPreffix,
|
||||||
|
|
15
context.go
15
context.go
|
@ -147,16 +147,31 @@ func (ctx *Context) GetHandlerName() string {
|
||||||
/* Request */
|
/* Request */
|
||||||
|
|
||||||
// Param returns the string representation of the key's path named parameter's value
|
// Param returns the string representation of the key's path named parameter's value
|
||||||
|
//
|
||||||
|
// Return value should be never stored directly, instead store it to a local variable,
|
||||||
|
// for example
|
||||||
|
// instead of: context.Session().Set("name", ctx.Param("user"))
|
||||||
|
// do this: username:= ctx.Param("user");ctx.Session().Set("name", username)
|
||||||
func (ctx *Context) Param(key string) string {
|
func (ctx *Context) Param(key string) string {
|
||||||
return ctx.Params.Get(key)
|
return ctx.Params.Get(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParamInt returns the int representation of the key's path named parameter's value
|
// ParamInt returns the int representation of the key's path named parameter's value
|
||||||
|
//
|
||||||
|
// Return value should be never stored directly, instead store it to a local variable,
|
||||||
|
// for example
|
||||||
|
// instead of: context.Session().Set("age", ctx.Param("age"))
|
||||||
|
// do this: age:= ctx.Param("age");ctx.Session().Set("age", age)
|
||||||
func (ctx *Context) ParamInt(key string) (int, error) {
|
func (ctx *Context) ParamInt(key string) (int, error) {
|
||||||
return strconv.Atoi(ctx.Param(key))
|
return strconv.Atoi(ctx.Param(key))
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParamInt64 returns the int64 representation of the key's path named parameter's value
|
// ParamInt64 returns the int64 representation of the key's path named parameter's value
|
||||||
|
//
|
||||||
|
// Return value should be never stored directly, instead store it to a local variable,
|
||||||
|
// for example
|
||||||
|
// instead of: context.Session().Set("ms", ctx.ParamInt64("ms"))
|
||||||
|
// do this: ms:= ctx.ParamInt64("ms");ctx.Session().Set("ms", ms)
|
||||||
func (ctx *Context) ParamInt64(key string) (int64, error) {
|
func (ctx *Context) ParamInt64(key string) (int64, error) {
|
||||||
return strconv.ParseInt(ctx.Param(key), 10, 64)
|
return strconv.ParseInt(ctx.Param(key), 10, 64)
|
||||||
}
|
}
|
||||||
|
|
19
http.go
19
http.go
|
@ -949,6 +949,9 @@ type (
|
||||||
// if false then the /something it's not the same as /something/
|
// if false then the /something it's not the same as /something/
|
||||||
// defaults to true
|
// defaults to true
|
||||||
correctPath bool
|
correctPath bool
|
||||||
|
// if enabled then the router checks and fires an error for 405 http status method not allowed too if no method compatible method was found
|
||||||
|
// by default is false
|
||||||
|
fireMethodNotAllowed bool
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
@ -960,6 +963,7 @@ func newServeMux(logger *log.Logger) *serveMux {
|
||||||
hostname: DefaultServerHostname, // these are changing when the server is up
|
hostname: DefaultServerHostname, // these are changing when the server is up
|
||||||
escapePath: !DefaultDisablePathEscape,
|
escapePath: !DefaultDisablePathEscape,
|
||||||
correctPath: !DefaultDisablePathCorrection,
|
correctPath: !DefaultDisablePathCorrection,
|
||||||
|
fireMethodNotAllowed: false,
|
||||||
logger: logger,
|
logger: logger,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -978,6 +982,10 @@ func (mux *serveMux) setCorrectPath(b bool) {
|
||||||
mux.correctPath = b
|
mux.correctPath = b
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (mux *serveMux) setFireMethodNotAllowed(b bool) {
|
||||||
|
mux.fireMethodNotAllowed = b
|
||||||
|
}
|
||||||
|
|
||||||
// registerError registers a handler to a http status
|
// registerError registers a handler to a http status
|
||||||
func (mux *serveMux) registerError(statusCode int, handler Handler) {
|
func (mux *serveMux) registerError(statusCode int, handler Handler) {
|
||||||
mux.mu.Lock()
|
mux.mu.Lock()
|
||||||
|
@ -1183,6 +1191,17 @@ func (mux *serveMux) BuildHandler() HandlerFunc {
|
||||||
// not found
|
// not found
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
// https://github.com/kataras/iris/issues/469
|
||||||
|
if mux.fireMethodNotAllowed {
|
||||||
|
for i := range mux.garden {
|
||||||
|
tree := mux.garden[i]
|
||||||
|
if !methodEqual(context.Method(), tree.method) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
mux.fireError(StatusMethodNotAllowed, context)
|
||||||
|
return
|
||||||
|
}
|
||||||
mux.fireError(StatusNotFound, context)
|
mux.fireError(StatusNotFound, context)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
24
http_test.go
24
http_test.go
|
@ -665,3 +665,27 @@ func TestMuxCustomHandler(t *testing.T) {
|
||||||
expectedData4.DynamicPathParameter = param4
|
expectedData4.DynamicPathParameter = param4
|
||||||
e.GET("/custom_handler_2/" + param4).Expect().Status(StatusOK).JSON().Equal(expectedData4)
|
e.GET("/custom_handler_2/" + param4).Expect().Status(StatusOK).JSON().Equal(expectedData4)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMuxFireMethodNotAllowed(t *testing.T) {
|
||||||
|
initDefault()
|
||||||
|
Default.Config.FireMethodNotAllowed = true
|
||||||
|
h := func(ctx *Context) {
|
||||||
|
ctx.Write("%s", ctx.MethodString())
|
||||||
|
}
|
||||||
|
|
||||||
|
Default.OnError(StatusMethodNotAllowed, func(ctx *Context) {
|
||||||
|
ctx.Write("Hello from my custom 405 page")
|
||||||
|
})
|
||||||
|
|
||||||
|
Get("/mypath", h)
|
||||||
|
Put("/mypath", h)
|
||||||
|
|
||||||
|
e := Tester(t)
|
||||||
|
|
||||||
|
e.GET("/mypath").Expect().Status(StatusOK).Body().Equal("GET")
|
||||||
|
e.PUT("/mypath").Expect().Status(StatusOK).Body().Equal("PUT")
|
||||||
|
// this should fail with 405 and catch by the custom http error
|
||||||
|
|
||||||
|
e.POST("/mypath").Expect().Status(StatusMethodNotAllowed).Body().Equal("Hello from my custom 405 page")
|
||||||
|
Close()
|
||||||
|
}
|
||||||
|
|
3
iris.go
3
iris.go
|
@ -79,7 +79,7 @@ import (
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// Version is the current version of the Iris web framework
|
// Version is the current version of the Iris web framework
|
||||||
Version = "4.5.1"
|
Version = "4.5.2"
|
||||||
|
|
||||||
banner = ` _____ _
|
banner = ` _____ _
|
||||||
|_ _| (_)
|
|_ _| (_)
|
||||||
|
@ -353,6 +353,7 @@ func (s *Framework) Build() {
|
||||||
// prepare the mux runtime fields again, for any case
|
// prepare the mux runtime fields again, for any case
|
||||||
s.mux.setCorrectPath(!s.Config.DisablePathCorrection)
|
s.mux.setCorrectPath(!s.Config.DisablePathCorrection)
|
||||||
s.mux.setEscapePath(!s.Config.DisablePathEscape)
|
s.mux.setEscapePath(!s.Config.DisablePathEscape)
|
||||||
|
s.mux.setFireMethodNotAllowed(s.Config.FireMethodNotAllowed)
|
||||||
|
|
||||||
// prepare the server's handler, we do that check because iris supports
|
// prepare the server's handler, we do that check because iris supports
|
||||||
// custom routers (you can take the routes registed by iris using iris.Lookups function)
|
// custom routers (you can take the routes registed by iris using iris.Lookups function)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user