Permalink
Browse files

Fix iris-contrib/middleware#13

  • Loading branch information...
1 parent 5e7975d commit 716b5001da6e7ec22a1a58e1833823a45cf56051 @kataras committed Sep 30, 2016
Showing with 147 additions and 224 deletions.
  1. +94 −155 README.md
  2. +1 −1 configuration.go
  3. +2 −2 context.go
  4. +3 −3 context_test.go
  5. +34 −55 http.go
  6. +5 −0 http_test.go
  7. +1 −1 iris.go
  8. BIN logo.jpg
  9. +6 −6 plugin_test.go
  10. +1 −1 ssh.go
View
Oops, something went wrong.
View
@@ -38,7 +38,7 @@ func (o OptionSet) Set(c *Configuration) {
// Configuration the whole configuration for an iris instance ($instance.Config) or global iris instance (iris.Config)
// these can be passed via options also, look at the top of this file(configuration.go)
//
-// Configuration is also implements the OptionSet so it's a valid option itself, this is briliant enough
+// Configuration is also implements the OptionSet so it's a valid option itself, this is brilliant enough
type Configuration struct {
// VHost is the addr or the domain that server listens to, which it's optional
// When to set VHost manually:
View
@@ -463,7 +463,7 @@ func (ctx *Context) SetHeader(k string, v string) {
// first parameter is the url to redirect
// second parameter is the http status should send, default is 302 (StatusFound), you can set it to 301 (Permant redirect), if that's nessecery
func (ctx *Context) Redirect(urlToRedirect string, statusHeader ...int) {
- httpStatus := StatusFound // a 'temporary-redirect-like' wich works better than for our purpose
+ httpStatus := StatusFound // a 'temporary-redirect-like' which works better than for our purpose
if statusHeader != nil && len(statusHeader) > 0 && statusHeader[0] > 0 {
httpStatus = statusHeader[0]
}
@@ -833,7 +833,7 @@ func (ctx *Context) Set(key string, value interface{}) {
// Note: the method ctx.Request.Header.VisitAllCookie by fasthttp, has a strange bug which I cannot solve at the moment.
// This is the reason which this function exists and should be used instead of fasthttp's built'n.
func (ctx *Context) VisitAllCookies(visitor func(key string, value string)) {
- // strange bug, this doesnt works also: cookieHeaderContent := ctx.Request.Header.Peek("Cookie")/User-Agent tested also
+ // strange bug, this doesn't works also: cookieHeaderContent := ctx.Request.Header.Peek("Cookie")/User-Agent tested also
headerbody := string(ctx.Request.Header.Header())
headerlines := strings.Split(headerbody, "\n")
for _, s := range headerlines {
View
@@ -402,7 +402,7 @@ func TestContextFlashMessages(t *testing.T) {
}
})
- // get the first flash, the next should be avaiable to the next requess
+ // get the first flash, the next should be available to the next requess
Get("/get_first_flash", func(ctx *Context) {
for _, v := range values {
val, err := ctx.GetFlash(v.Key)
@@ -421,7 +421,7 @@ func TestContextFlashMessages(t *testing.T) {
Get("/get_no_getflash", func(ctx *Context) {
})
- // get the last flash, the next should be avaiable to the next requess
+ // get the last flash, the next should be available to the next requess
Get("/get_last_flash", func(ctx *Context) {
for i, v := range values {
if i == len(values)-1 {
@@ -572,7 +572,7 @@ func TestContextSessions(t *testing.T) {
es.Request("GET", "/get").Expect().Status(StatusOK).JSON().Object().Equal(values)
}
- // test destory which also clears first
+ // test destroy which also clears first
d := e.GET("/destroy").Expect().Status(StatusOK)
d.JSON().Null()
// This removed: d.Cookies().Empty(). Reason:
View
@@ -824,7 +824,7 @@ func (s bySubdomain) Less(i, j int) bool {
var _ Route = &route{}
func newRoute(method []byte, subdomain string, path string, middleware Middleware) *route {
- r := &route{name: path + subdomain, method: method, subdomain: subdomain, path: path, middleware: middleware}
+ r := &route{name: path + subdomain, method: method, methodStr: string(method), subdomain: subdomain, path: path, middleware: middleware}
r.formatPath()
return r
}
@@ -926,11 +926,10 @@ type (
// ex: mysubdomain.
subdomain string
entry *muxEntry
- next *muxTree
}
serveMux struct {
- tree *muxTree
+ garden []*muxTree
lookups []*route
maxParameters uint8
@@ -1008,16 +1007,14 @@ func (mux *serveMux) fireError(statusCode int, ctx *Context) {
errHandler.Serve(ctx)
}
-func (mux *serveMux) getTree(method []byte, subdomain string) (tree *muxTree) {
- tree = mux.tree
- for tree != nil {
- if bytes.Equal(tree.method, method) && tree.subdomain == subdomain {
- return
+func (mux *serveMux) getTree(method []byte, subdomain string) *muxTree {
+ for i := range mux.garden {
+ t := mux.garden[i]
+ if bytes.Equal(t.method, method) && t.subdomain == subdomain {
+ return t
}
- tree = tree.next
}
- // tree is nil here, return that.
- return
+ return nil
}
func (mux *serveMux) register(method []byte, subdomain string, path string, middleware Middleware) *route {
@@ -1041,30 +1038,30 @@ func (mux *serveMux) register(method []byte, subdomain string, path string, midd
// build collects all routes info and adds them to the registry in order to be served from the request handler
// this happens once when server is setting the mux's handler.
-func (mux *serveMux) build() (func(reqCtx *fasthttp.RequestCtx) string, func([]byte, []byte) bool) {
- mux.tree = nil
+func (mux *serveMux) build() func(reqCtx *fasthttp.RequestCtx) string {
+
+ // check for cors conflicts FIRST in order to put them in OPTIONS tree also
+ for i := range mux.lookups {
+ r := mux.lookups[i]
+ if r.hasCors() {
+ if exists := mux.lookup(r.path + r.subdomain); exists == nil || exists.Method() != MethodOptions {
+ // skip any already registed to OPTIONS, some users maybe do that manually, so we should be careful here, we do not catch custom names but that's fairly enough
+ mux.register(MethodOptionsBytes, r.subdomain, r.path, r.middleware)
+ }
+
+ }
+ }
+
sort.Sort(bySubdomain(mux.lookups))
- for _, r := range mux.lookups {
+
+ for i := range mux.lookups {
+ r := mux.lookups[i]
// add to the registry tree
tree := mux.getTree(r.method, r.subdomain)
if tree == nil {
//first time we register a route to this method with this domain
- tree = &muxTree{method: r.method, subdomain: r.subdomain, entry: &muxEntry{}, next: nil}
-
- if mux.tree == nil {
- // it's the first entry
- mux.tree = tree
- } else {
- // find the last tree and make the .next to the tree we created before
- lastTree := mux.tree
- for lastTree != nil {
- if lastTree.next == nil {
- lastTree.next = tree
- break
- }
- lastTree = lastTree.next
- }
- }
+ tree = &muxTree{method: r.method, subdomain: r.subdomain, entry: &muxEntry{}}
+ mux.garden = append(mux.garden, tree)
}
// I decide that it's better to explicit give subdomain and a path to it than registedPath(mysubdomain./something) now its: subdomain: mysubdomain., path: /something
// we have different tree for each of subdomains, now you can use everything you can use with the normal paths ( before you couldn't set /any/*path)
@@ -1086,20 +1083,7 @@ func (mux *serveMux) build() (func(reqCtx *fasthttp.RequestCtx) string, func([]b
getRequestPath = func(reqCtx *fasthttp.RequestCtx) string { return utils.BytesToString(reqCtx.RequestURI()) }
}
- methodEqual := func(treeMethod []byte, reqMethod []byte) bool {
- return bytes.Equal(treeMethod, reqMethod)
- }
- // check for cors conflicts
- for _, r := range mux.lookups {
- if r.hasCors() {
- methodEqual = func(treeMethod []byte, reqMethod []byte) bool {
- return bytes.Equal(treeMethod, reqMethod) || bytes.Equal(reqMethod, MethodOptionsBytes)
- }
- break
- }
- }
-
- return getRequestPath, methodEqual
+ return getRequestPath
}
@@ -1116,20 +1100,15 @@ func (mux *serveMux) lookup(routeName string) *route {
func (mux *serveMux) BuildHandler() HandlerFunc {
// initialize the router once
- getRequestPath, methodEqual := mux.build()
+ getRequestPath := mux.build()
return func(context *Context) {
routePath := getRequestPath(context.RequestCtx)
- tree := mux.tree
- for tree != nil {
- if !methodEqual(tree.method, context.Method()) {
- // we break any CORS OPTIONS method
- // but for performance reasons if user wants http method OPTIONS to be served
- // then must register it with .Options(...)
- tree = tree.next
+ for i := range mux.garden {
+ tree := mux.garden[i]
+ if !bytes.Equal(tree.method, context.Method()) {
continue
}
- // we have at least one subdomain on the root
if mux.hosts && tree.subdomain != "" {
// context.VirtualHost() is a slow method because it makes string.Replaces but user can understand that if subdomain then server will have some nano/or/milleseconds performance cost
requestHost := context.VirtualHostname()
@@ -1143,17 +1122,17 @@ func (mux *serveMux) BuildHandler() HandlerFunc {
// so the host must be api.iris-go.com:8080
if tree.subdomain+mux.hostname != requestHost {
// go to the next tree, we have a subdomain but it is not the correct
- tree = tree.next
+
continue
}
}
} else {
//("it's subdomain but the request is the same as the listening addr mux.host == requestHost =>" + mux.host + "=" + requestHost + " ____ and tree's subdomain was: " + tree.subdomain)
- tree = tree.next
continue
}
}
+
middleware, params, mustRedirect := tree.entry.get(routePath, context.Params) // pass the parameters here for 0 allocation
if middleware != nil {
// ok we found the correct route, serve it and exit entirely from here
View
@@ -152,6 +152,8 @@ func TestMultiRunningServers_v1_PROXY(t *testing.T) {
defer Close()
host := "localhost" // you have to add it to your hosts file( for windows, as 127.0.0.1 mydomain.com)
hostTLS := "localhost:9999"
+ Close()
+ defer Close()
initDefault()
Default.Config.DisableBanner = true
// create the key and cert files on the fly, and delete them when this test finished
@@ -406,9 +408,12 @@ func TestMuxSimpleParty(t *testing.T) {
}
Default.Config.VHost = "0.0.0.0:8080"
+ //Default.Config.Tester.Debug = true
+ //Default.Config.Tester.ExplicitURL = true
e := Tester(t)
request := func(reqPath string) {
+
e.Request("GET", reqPath).
Expect().
Status(StatusOK).Body().Equal(Default.Config.VHost + reqPath)
View
@@ -79,7 +79,7 @@ import (
const (
// Version is the current version of the Iris web framework
- Version = "4.4.1"
+ Version = "4.4.2"
banner = ` _____ _
|_ _| (_)
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
@@ -176,13 +176,13 @@ func TestPluginEvents(t *testing.T) {
plugins.DoPreClose(nil)
if !prelistenran {
- t.Fatalf("Expected to run PreListen Func but it doesnt!")
+ t.Fatalf("Expected to run PreListen Func but it doesn't!")
}
if !postlistenran {
- t.Fatalf("Expected to run PostListen Func but it doesnt!")
+ t.Fatalf("Expected to run PostListen Func but it doesn't!")
}
if !precloseran {
- t.Fatalf("Expected to run PostListen Func but it doesnt!")
+ t.Fatalf("Expected to run PostListen Func but it doesn't!")
}
if !myplugin.named {
@@ -192,13 +192,13 @@ func TestPluginEvents(t *testing.T) {
t.Fatalf("Plugin should be activated but it's not!")
}
if !myplugin.prelistenran {
- t.Fatalf("Expected to run PreListen Struct but it doesnt!")
+ t.Fatalf("Expected to run PreListen Struct but it doesn't!")
}
if !myplugin.postlistenran {
- t.Fatalf("Expected to run PostListen Struct but it doesnt!")
+ t.Fatalf("Expected to run PostListen Struct but it doesn't!")
}
if !myplugin.precloseran {
- t.Fatalf("Expected to run PostListen Struct but it doesnt!")
+ t.Fatalf("Expected to run PostListen Struct but it doesn't!")
}
}
View
@@ -1,6 +1,6 @@
package iris
-// Minimal managment over SSH for your Iris & Q web server
+// Minimal management over SSH for your Iris & Q web server
//
// Declaration:
//

0 comments on commit 716b500

Please sign in to comment.