1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-07 20:17:05 +00:00

New: gRPC MVC features, new WithLowercaseRouting option and add some new context methods

read HISTORY.md


Former-commit-id: 30a16cceb11f754aa32923058abeda1e736350e7
This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-04-25 02:30:19 +03:00
parent 0cf5d5a4a3
commit 5d3c96947c
21 changed files with 566 additions and 185 deletions

View File

@@ -25,7 +25,8 @@ type ConfigurationReadOnly interface {
GetDisablePathCorrection() bool
// GetDisablePathCorrectionRedirection returns the Configuration#DisablePathCorrectionRedirection field.
// If DisablePathCorrectionRedirection set to true then it will fire the handler of the matching route without
// If DisablePathCorrectionRedirection set to true then it will handle paths as they are.
// it will fire the handler of the matching route without
// the last slash ("/") instead of send a redirection status.
GetDisablePathCorrectionRedirection() bool
@@ -33,6 +34,9 @@ type ConfigurationReadOnly interface {
// returns true when its escapes the path, the named parameters (if any).
GetEnablePathEscape() bool
// GetForceLowercaseRouting returns the value of the `ForceLowercaseRouting` setting.
GetForceLowercaseRouting() bool
// GetEnableOptimizations returns whether
// the application has performance optimizations enabled.
GetEnableOptimizations() bool

View File

@@ -165,7 +165,6 @@ type Context interface {
// Router is calling this function to add the route's handler.
// If AddHandler called then the handlers will be inserted
// to the end of the already-defined route's handler.
//
AddHandler(...Handler)
// SetHandlers replaces all handlers with the new.
SetHandlers(Handlers)
@@ -387,6 +386,11 @@ type Context interface {
IsMobile() bool
// IsScript reports whether a client is a script.
IsScript() bool
// IsHTTP2 reports whether the protocol version for incoming request was HTTP/2.
// The client code always uses either HTTP/1.1 or HTTP/2.
IsHTTP2() bool
// IsGRPC reports whether the request came from a gRPC client.
IsGRPC() bool
// GetReferrer extracts and returns the information from the "Referer" header as specified
// in https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referrer-Policy
// or by the URL query parameter "referer".
@@ -1668,6 +1672,7 @@ func (ctx *context) RequestPath(escape bool) string {
if escape {
return ctx.request.URL.EscapedPath() // DecodeQuery(ctx.request.URL.EscapedPath())
}
return ctx.request.URL.Path // RawPath returns empty, requesturi can be used instead also.
}
@@ -1826,6 +1831,17 @@ func (ctx *context) IsScript() bool {
return isScriptRegex.MatchString(s)
}
// IsHTTP2 reports whether the protocol version for incoming request was HTTP/2.
// The client code always uses either HTTP/1.1 or HTTP/2.
func (ctx *context) IsHTTP2() bool {
return ctx.Request().ProtoMajor == 2
}
// IsGRPC reports whether the request came from a gRPC client.
func (ctx *context) IsGRPC() bool {
return ctx.IsHTTP2() && ctx.GetContentTypeRequested() == ContentGRPCHeaderValue
}
type (
// Referrer contains the extracted information from the `GetReferrer`
//
@@ -3316,6 +3332,8 @@ const (
ContentFormHeaderValue = "application/x-www-form-urlencoded"
// ContentFormMultipartHeaderValue header value for post multipart form data.
ContentFormMultipartHeaderValue = "multipart/form-data"
// ContentGRPCHeaderValue Content-Type header value for gRPC.
ContentGRPCHeaderValue = "application/grpc"
)
// Binary writes out the raw bytes as binary data.

View File

@@ -1,6 +1,7 @@
package context
import (
"path/filepath"
"reflect"
"runtime"
"strings"
@@ -27,20 +28,38 @@ type Handler func(Context)
// See `Handler` for more.
type Handlers []Handler
func valueOf(v interface{}) reflect.Value {
if val, ok := v.(reflect.Value); ok {
return val
}
return reflect.ValueOf(v)
}
// HandlerName returns the handler's function name.
// See `context.HandlerName` to get function name of the current running handler in the chain.
func HandlerName(h Handler) string {
pc := reflect.ValueOf(h).Pointer()
func HandlerName(h interface{}) string {
pc := valueOf(h).Pointer()
return runtime.FuncForPC(pc).Name()
}
// 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()
func HandlerFileLine(h interface{}) (file string, line int) {
pc := valueOf(h).Pointer()
return runtime.FuncForPC(pc).FileLine(pc)
}
// HandlerFileLineRel same as `HandlerFileLine` but it returns the path as relative to the "workingDir".
func HandlerFileLineRel(h interface{}, workingDir string) (string, int) {
file, line := HandlerFileLine(h)
if relFile, err := filepath.Rel(workingDir, file); err == nil {
file = "./" + relFile
}
return file, line
}
// 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) {