Publish the new version :airplane: | Look description please!
# FAQ
### Looking for free support?
http://support.iris-go.com
https://kataras.rocket.chat/channel/iris
### Looking for previous versions?
https://github.com/kataras/iris#version
### Should I upgrade my Iris?
Developers are not forced to upgrade if they don't really need it. Upgrade whenever you feel ready.
> Iris uses the [vendor directory](https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo) feature, so you get truly reproducible builds, as this method guards against upstream renames and deletes.
**How to upgrade**: Open your command-line and execute this command: `go get -u github.com/kataras/iris`.
For further installation support, please click [here](http://support.iris-go.com/d/16-how-to-install-iris-web-framework).
### About our new home page
http://iris-go.com
Thanks to [Santosh Anand](https://github.com/santoshanand) the http://iris-go.com has been upgraded and it's really awesome!
[Santosh](https://github.com/santoshanand) is a freelancer, he has a great knowledge of nodejs and express js, Android, iOS, React Native, Vue.js etc, if you need a developer to find or create a solution for your problem or task, please contact with him.
The amount of the next two or three donations you'll send they will be immediately transferred to his own account balance, so be generous please!
Read more at https://github.com/kataras/iris/blob/master/HISTORY.md
Former-commit-id: eec2d71bbe011d6b48d2526eb25919e36e5ad94e
2017-06-03 22:22:52 +02:00
|
|
|
package context
|
|
|
|
|
2017-11-04 01:59:21 +01:00
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"runtime"
|
2019-06-21 18:43:25 +02:00
|
|
|
"strings"
|
2017-11-04 01:59:21 +01:00
|
|
|
)
|
|
|
|
|
2017-06-10 02:56:42 +02:00
|
|
|
// A Handler responds to an HTTP request.
|
|
|
|
// It writes reply headers and data to the Context.ResponseWriter() and then return.
|
|
|
|
// Returning signals that the request is finished;
|
|
|
|
// it is not valid to use the Context after or concurrently with the completion of the Handler call.
|
|
|
|
//
|
|
|
|
// Depending on the HTTP client software, HTTP protocol version,
|
2017-07-10 17:32:42 +02:00
|
|
|
// and any intermediaries between the client and the iris server,
|
2017-06-10 02:56:42 +02:00
|
|
|
// it may not be possible to read from the Context.Request().Body after writing to the context.ResponseWriter().
|
|
|
|
// Cautious handlers should read the Context.Request().Body first, and then reply.
|
|
|
|
//
|
|
|
|
// Except for reading the body, handlers should not modify the provided Context.
|
|
|
|
//
|
|
|
|
// If Handler panics, the server (the caller of Handler) assumes that the effect of the panic was isolated to the active request.
|
|
|
|
// It recovers the panic, logs a stack trace to the server error log, and hangs up the connection.
|
Publish the new version :airplane: | Look description please!
# FAQ
### Looking for free support?
http://support.iris-go.com
https://kataras.rocket.chat/channel/iris
### Looking for previous versions?
https://github.com/kataras/iris#version
### Should I upgrade my Iris?
Developers are not forced to upgrade if they don't really need it. Upgrade whenever you feel ready.
> Iris uses the [vendor directory](https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo) feature, so you get truly reproducible builds, as this method guards against upstream renames and deletes.
**How to upgrade**: Open your command-line and execute this command: `go get -u github.com/kataras/iris`.
For further installation support, please click [here](http://support.iris-go.com/d/16-how-to-install-iris-web-framework).
### About our new home page
http://iris-go.com
Thanks to [Santosh Anand](https://github.com/santoshanand) the http://iris-go.com has been upgraded and it's really awesome!
[Santosh](https://github.com/santoshanand) is a freelancer, he has a great knowledge of nodejs and express js, Android, iOS, React Native, Vue.js etc, if you need a developer to find or create a solution for your problem or task, please contact with him.
The amount of the next two or three donations you'll send they will be immediately transferred to his own account balance, so be generous please!
Read more at https://github.com/kataras/iris/blob/master/HISTORY.md
Former-commit-id: eec2d71bbe011d6b48d2526eb25919e36e5ad94e
2017-06-03 22:22:52 +02:00
|
|
|
type Handler func(Context)
|
|
|
|
|
2017-06-10 02:56:42 +02:00
|
|
|
// Handlers is just a type of slice of []Handler.
|
|
|
|
//
|
|
|
|
// See `Handler` for more.
|
Publish the new version :airplane: | Look description please!
# FAQ
### Looking for free support?
http://support.iris-go.com
https://kataras.rocket.chat/channel/iris
### Looking for previous versions?
https://github.com/kataras/iris#version
### Should I upgrade my Iris?
Developers are not forced to upgrade if they don't really need it. Upgrade whenever you feel ready.
> Iris uses the [vendor directory](https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo) feature, so you get truly reproducible builds, as this method guards against upstream renames and deletes.
**How to upgrade**: Open your command-line and execute this command: `go get -u github.com/kataras/iris`.
For further installation support, please click [here](http://support.iris-go.com/d/16-how-to-install-iris-web-framework).
### About our new home page
http://iris-go.com
Thanks to [Santosh Anand](https://github.com/santoshanand) the http://iris-go.com has been upgraded and it's really awesome!
[Santosh](https://github.com/santoshanand) is a freelancer, he has a great knowledge of nodejs and express js, Android, iOS, React Native, Vue.js etc, if you need a developer to find or create a solution for your problem or task, please contact with him.
The amount of the next two or three donations you'll send they will be immediately transferred to his own account balance, so be generous please!
Read more at https://github.com/kataras/iris/blob/master/HISTORY.md
Former-commit-id: eec2d71bbe011d6b48d2526eb25919e36e5ad94e
2017-06-03 22:22:52 +02:00
|
|
|
type Handlers []Handler
|
2017-11-04 01:59:21 +01:00
|
|
|
|
2019-06-21 18:43:25 +02:00
|
|
|
// HandlerName returns the handler's function name.
|
|
|
|
// See `context.HandlerName` to get function name of the current running handler in the chain.
|
2017-11-04 01:59:21 +01:00
|
|
|
func HandlerName(h Handler) string {
|
|
|
|
pc := reflect.ValueOf(h).Pointer()
|
|
|
|
return runtime.FuncForPC(pc).Name()
|
|
|
|
}
|
2019-01-19 22:33:33 +01:00
|
|
|
|
2019-06-21 18:43:25 +02:00
|
|
|
// HandlerFileLine returns the handler's file and line information.
|
|
|
|
// See `context.HandlerFileLine` to get the file, line of the current running handler in the chain.
|
|
|
|
func HandlerFileLine(h Handler) (file string, line int) {
|
|
|
|
pc := reflect.ValueOf(h).Pointer()
|
|
|
|
return runtime.FuncForPC(pc).FileLine(pc)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MainHandlerName tries to find the main handler than end-developer
|
|
|
|
// registered on the provided chain of handlers and returns its function name.
|
|
|
|
func MainHandlerName(handlers Handlers) (name string) {
|
|
|
|
for i := 0; i < len(handlers); i++ {
|
|
|
|
name = HandlerName(handlers[i])
|
|
|
|
if !strings.HasPrefix(name, "github.com/kataras/iris") ||
|
|
|
|
strings.HasPrefix(name, "github.com/kataras/iris/core/router.StripPrefix") ||
|
|
|
|
strings.HasPrefix(name, "github.com/kataras/iris/core/router.FileServer") {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-01-19 22:33:33 +01:00
|
|
|
// Filter is just a type of func(Handler) bool which reports whether an action must be performed
|
|
|
|
// based on the incoming request.
|
|
|
|
//
|
|
|
|
// See `NewConditionalHandler` for more.
|
|
|
|
type Filter func(Context) bool
|
|
|
|
|
|
|
|
// NewConditionalHandler returns a single Handler which can be registered
|
|
|
|
// as a middleware.
|
|
|
|
// Filter is just a type of Handler which returns a boolean.
|
|
|
|
// Handlers here should act like middleware, they should contain `ctx.Next` to proceed
|
2019-01-19 22:34:41 +01:00
|
|
|
// to the next handler of the chain. Those "handlers" are registered to the per-request context.
|
2019-01-19 22:33:33 +01:00
|
|
|
//
|
|
|
|
//
|
|
|
|
// It checks the "filter" and if passed then
|
|
|
|
// it, correctly, executes the "handlers".
|
|
|
|
//
|
|
|
|
// If passed, this function makes sure that the Context's information
|
|
|
|
// about its per-request handler chain based on the new "handlers" is always updated.
|
|
|
|
//
|
|
|
|
// If not passed, then simply the Next handler(if any) is executed and "handlers" are ignored.
|
|
|
|
//
|
|
|
|
// Example can be found at: _examples/routing/conditional-chain.
|
|
|
|
func NewConditionalHandler(filter Filter, handlers ...Handler) Handler {
|
|
|
|
return func(ctx Context) {
|
|
|
|
if filter(ctx) {
|
|
|
|
// Note that we don't want just to fire the incoming handlers, we must make sure
|
|
|
|
// that it won't break any further handler chain
|
|
|
|
// information that may be required for the next handlers.
|
|
|
|
//
|
|
|
|
// The below code makes sure that this conditional handler does not break
|
|
|
|
// the ability that iris provides to its end-devs
|
|
|
|
// to check and modify the per-request handlers chain at runtime.
|
|
|
|
currIdx := ctx.HandlerIndex(-1)
|
|
|
|
currHandlers := ctx.Handlers()
|
|
|
|
if currIdx == len(currHandlers)-1 {
|
|
|
|
// if this is the last handler of the chain
|
|
|
|
// just add to the last the new handlers and call Next to fire those.
|
|
|
|
ctx.AddHandler(handlers...)
|
|
|
|
ctx.Next()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// otherwise insert the new handlers in the middle of the current executed chain and the next chain.
|
|
|
|
newHandlers := append(currHandlers[:currIdx], append(handlers, currHandlers[currIdx+1:]...)...)
|
|
|
|
ctx.SetHandlers(newHandlers)
|
|
|
|
ctx.Next()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// if not pass, then just execute the next.
|
|
|
|
ctx.Next()
|
|
|
|
}
|
|
|
|
}
|