diff --git a/README.md b/README.md index f9678011..b80792c2 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@
-Releases +Releases Examples @@ -871,7 +871,7 @@ I recommend writing your API tests using this new library, [httpexpect](https:// Versioning ------------ -Current: **v4.5.2** +Current: **v4.5.3** > 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 [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 -[Release Widget]: https://img.shields.io/badge/release-4.5.2%20-blue.svg?style=flat-square +[Release Widget]: https://img.shields.io/badge/release-4.5.3%20-blue.svg?style=flat-square [Release]: https://github.com/kataras/iris/releases [Chat Widget]: https://img.shields.io/badge/community-chat%20-00BCD4.svg?style=flat-square [Chat]: https://kataras.rocket.chat/channel/iris diff --git a/context.go b/context.go index 5b12c6af..03c5f113 100644 --- a/context.go +++ b/context.go @@ -20,7 +20,6 @@ import ( "github.com/kataras/go-errors" "github.com/kataras/go-fs" "github.com/kataras/go-sessions" - "github.com/kataras/iris/utils" "github.com/valyala/fasthttp" ) @@ -202,12 +201,12 @@ func (ctx *Context) URLParamInt64(key string) (int64, error) { // MethodString returns the HTTP Method func (ctx *Context) MethodString() string { - return utils.BytesToString(ctx.Method()) + return string(ctx.Method()) } // HostString returns the Host of the request( the url as string ) func (ctx *Context) HostString() string { - return utils.BytesToString(ctx.Host()) + return string(ctx.Host()) } // VirtualHostname returns the hostname that user registers, host path maybe differs from the real which is HostString, which taken from a net.listener @@ -246,9 +245,9 @@ func (ctx *Context) PathString() string { func (ctx *Context) RequestPath(escape bool) string { if escape { // return utils.BytesToString(ctx.RequestCtx.Path()) - return utils.BytesToString(ctx.RequestCtx.URI().PathOriginal()) + return string(ctx.RequestCtx.URI().PathOriginal()) } - return utils.BytesToString(ctx.RequestCtx.RequestURI()) + return string(ctx.RequestCtx.RequestURI()) } // RequestIP gets just the Remote Address from the client. @@ -283,7 +282,7 @@ func (ctx *Context) RemoteAddr() string { // accepts one parameter, the key of the header (string) // returns string func (ctx *Context) RequestHeader(k string) string { - return utils.BytesToString(ctx.RequestCtx.Request.Header.Peek(k)) + return string(ctx.RequestCtx.Request.Header.Peek(k)) } // IsAjax returns true if this request is an 'ajax request'( XMLHttpRequest) diff --git a/http.go b/http.go index 2ca8b645..c929ce5d 100644 --- a/http.go +++ b/http.go @@ -15,7 +15,6 @@ import ( "github.com/iris-contrib/letsencrypt" "github.com/kataras/go-errors" - "github.com/kataras/iris/utils" "github.com/valyala/fasthttp" "github.com/valyala/fasthttp/fasthttpadaptor" ) @@ -434,6 +433,14 @@ func getParamsLen(path string) uint8 { return uint8(n) } +// findLower returns the smaller number between a and b +func findLower(a, b int) int { + if a <= b { + return a + } + return b +} + // add adds a muxEntry to the existing muxEntry or to the tree if no muxEntry has the prefix of func (e *muxEntry) add(path string, middleware Middleware) error { fullPath := path @@ -448,7 +455,7 @@ func (e *muxEntry) add(path string, middleware Middleware) error { } i := 0 - max := utils.FindLower(len(path), len(e.part)) + max := findLower(len(path), len(e.part)) for i < max && path[i] == e.part[i] { i++ } @@ -1073,11 +1080,11 @@ func (mux *serveMux) build() (getRequestPath func(*fasthttp.RequestCtx) string, // optimize this once once, we could do that: context.RequestPath(mux.escapePath), but we lose some nanoseconds on if :) getRequestPath = func(reqCtx *fasthttp.RequestCtx) string { - return utils.BytesToString(reqCtx.Path()) //string(ctx.Path()[:]) // a little bit of memory allocation, old method used: BytesToString, If I see the benchmarks get low I will change it back to old, but this way is safer. + return string(reqCtx.Path()) } if !mux.escapePath { - getRequestPath = func(reqCtx *fasthttp.RequestCtx) string { return utils.BytesToString(reqCtx.RequestURI()) } + getRequestPath = func(reqCtx *fasthttp.RequestCtx) string { return string(reqCtx.RequestURI()) } } methodEqual = func(reqMethod []byte, treeMethod []byte) bool { @@ -1109,6 +1116,22 @@ func (mux *serveMux) lookup(routeName string) *route { return nil } +//THESE ARE FROM Go Authors +var htmlReplacer = strings.NewReplacer( + "&", "&", + "<", "<", + ">", ">", + // """ is shorter than """. + `"`, """, + // "'" is shorter than "'" and apos was not in HTML until HTML5. + "'", "'", +) + +// HTMLEscape returns a string which has no valid html code +func HTMLEscape(s string) string { + return htmlReplacer.Replace(s) +} + // BuildHandler the default Iris router when iris.Handler is nil func (mux *serveMux) BuildHandler() HandlerFunc { @@ -1168,7 +1191,7 @@ func (mux *serveMux) BuildHandler() HandlerFunc { } context.Request.URI().SetPath(reqPath) - urlToRedirect := utils.BytesToString(context.Request.RequestURI()) + urlToRedirect := string(context.Request.RequestURI()) statisForRedirect := StatusMovedPermanently // StatusMovedPermanently, this document is obselte, clients caches this. if bytes.Equal(tree.method, MethodPostBytes) || @@ -1182,7 +1205,7 @@ func (mux *serveMux) BuildHandler() HandlerFunc { // response because older user agents may not understand 301/307. // Shouldn't send the response for POST or HEAD; that leaves GET. if bytes.Equal(tree.method, MethodGetBytes) { - note := "Moved Permanently.\n" + note := "Moved Permanently.\n" context.Write(note) } return diff --git a/iris.go b/iris.go index 28c98338..2f94540f 100644 --- a/iris.go +++ b/iris.go @@ -79,7 +79,7 @@ import ( const ( // Version is the current version of the Iris web framework - Version = "4.5.2" + Version = "4.5.3" banner = ` _____ _ |_ _| (_) diff --git a/utils/strings.go b/utils/strings.go deleted file mode 100644 index ac56fd41..00000000 --- a/utils/strings.go +++ /dev/null @@ -1,120 +0,0 @@ -package utils - -import ( - "bytes" - "encoding/base64" - "encoding/gob" - "math/rand" - "reflect" - "strings" - "time" - "unsafe" -) - -//THESE ARE FROM Go Authors -var htmlReplacer = strings.NewReplacer( - "&", "&", - "<", "<", - ">", ">", - // """ is shorter than """. - `"`, """, - // "'" is shorter than "'" and apos was not in HTML until HTML5. - "'", "'", -) - -// HTMLEscape returns a string which has no valid html code -func HTMLEscape(s string) string { - return htmlReplacer.Replace(s) -} - -// FindLower returns the smaller number between a and b -func FindLower(a, b int) int { - if a <= b { - return a - } - return b -} - -// BytesToString accepts bytes and returns their string presentation -// instead of string() this method doesn't generate memory allocations, -// BUT it is not safe to use anywhere because it points -// this helps on 0 memory allocations -func BytesToString(b []byte) string { - bh := (*reflect.SliceHeader)(unsafe.Pointer(&b)) - sh := reflect.StringHeader{bh.Data, bh.Len} - return *(*string)(unsafe.Pointer(&sh)) -} - -// StringToBytes accepts string and returns their []byte presentation -// instead of byte() this method doesn't generate memory allocations, -// BUT it is not safe to use anywhere because it points -// this helps on 0 memory allocations -func StringToBytes(s string) []byte { - sh := (*reflect.StringHeader)(unsafe.Pointer(&s)) - bh := reflect.SliceHeader{sh.Data, sh.Len, 0} - return *(*[]byte)(unsafe.Pointer(&bh)) -} - -// -const ( - letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" - letterIdxBits = 6 // 6 bits to represent a letter index - letterIdxMask = 1<= 0; { - if remain == 0 { - cache, remain = src.Int63(), letterIdxMax - } - if idx := int(cache & letterIdxMask); idx < len(letterBytes) { - b[i] = letterBytes[idx] - i-- - } - cache >>= letterIdxBits - remain-- - } - - return b -} - -// RandomString accepts a number(10 for example) and returns a random string using simple but fairly safe random algorithm -func RandomString(n int) string { - return string(Random(n)) -} - -// Serialize serialize any type to gob bytes and after returns its the base64 encoded string -func Serialize(m interface{}) (string, error) { - b := bytes.Buffer{} - encoder := gob.NewEncoder(&b) - err := encoder.Encode(m) - if err != nil { - return "", err - } - return base64.StdEncoding.EncodeToString(b.Bytes()), nil -} - -// Deserialize accepts an encoded string and a data struct which will be filled with the desierialized string -// using gob decoder -func Deserialize(str string, m interface{}) error { - by, err := base64.StdEncoding.DecodeString(str) - if err != nil { - return err - } - b := bytes.Buffer{} - b.Write(by) - d := gob.NewDecoder(&b) - // d := gob.NewDecoder(bytes.NewBufferString(str)) - err = d.Decode(&m) - if err != nil { - return err - } - return nil -}