diff --git a/context.go b/context.go index 2b4b05de..870590de 100644 --- a/context.go +++ b/context.go @@ -327,23 +327,6 @@ func (ctx *Context) Subdomain() (subdomain string) { return } -// URLEncode returns the path encoded as url -// useful when you want to pass something to a database and be valid to retrieve it via context.Param -// use it only for special cases, when the default behavior doesn't suits you. -// -// http://www.blooberry.com/indexdot/html/topics/urlencoding.htm -/* Credits to Manish Singh @kryptodev for URLEncode by post issue share code */ -func URLEncode(path string) string { - if path == "" { - return "" - } - u := fasthttp.AcquireURI() - u.SetPath(path) - encodedPath := u.String()[8:] - fasthttp.ReleaseURI(u) - return encodedPath -} - // ReadJSON reads JSON from request's body func (ctx *Context) ReadJSON(jsonObject interface{}) error { data := ctx.RequestCtx.Request.Body() diff --git a/http_test.go b/http_test.go index 8f9dbd9e..e25e7056 100644 --- a/http_test.go +++ b/http_test.go @@ -10,7 +10,6 @@ import ( "fmt" "io/ioutil" "os" - "strconv" "testing" "time" @@ -243,12 +242,13 @@ func TestMultiRunningServers_v2(t *testing.T) { } const ( - testEnableSubdomain = false - testSubdomain = "mysubdomain.com" + testEnableSubdomain = true + testSubdomain = "mysubdomain" ) func testSubdomainHost() string { - return testSubdomain + strconv.Itoa(Servers.Main().Port()) + s := testSubdomain + "." + Servers.Main().Host() + return s } func testSubdomainURL() (subdomainURL string) { @@ -433,6 +433,20 @@ func TestMuxPathEscape(t *testing.T) { Expect().Status(StatusOK).Body().Equal("name=Sakamoto desu ga,highlight=text") } +func TestMuxEncodeURL(t *testing.T) { + initDefault() + + Get("/encoding/:url", func(ctx *Context) { + url := URLEncode(ctx.Param("url")) + ctx.SetStatusCode(StatusOK) + ctx.Write(url) + }) + + e := Tester(t) + + e.GET("/encoding/http%3A%2F%2Fsome-url.com").Expect().Status(StatusOK).Body().Equal("http://some-url.com") +} + func TestMuxCustomErrors(t *testing.T) { var ( notFoundMessage = "Iris custom message for 404 not found" diff --git a/iris.go b/iris.go index e1a1d30f..e015edab 100644 --- a/iris.go +++ b/iris.go @@ -54,6 +54,7 @@ import ( "fmt" "io" "net/http" + "net/url" "os" "path" "reflect" @@ -753,6 +754,39 @@ func (s *Framework) Path(routeName string, args ...interface{}) string { return fmt.Sprintf(r.formattedPath, arguments...) } +// URLEncode returns the path encoded as url +// useful when you want to pass something to a database and be valid to retrieve it via context.Param +// use it only for special cases, when the default behavior doesn't suits you. +// +// http://www.blooberry.com/indexdot/html/topics/urlencoding.htm +// it uses just the url.QueryUnescape +func URLEncode(path string) string { + if path == "" { + return "" + } + encodedPath, _ := url.QueryUnescape(path) + return encodedPath +} + +// URLEncode returns the path encoded as url +// useful when you want to pass something to a database and be valid to retrieve it via context.Param +// use it only for special cases, when the default behavior doesn't suits you. +// +// http://www.blooberry.com/indexdot/html/topics/urlencoding.htm +/* Credits to Manish Singh @kryptodev for URLEncode by post issue share code */ +// simple things, if URLEncode doesn't gives you the results you waited, use this function +// I know it is not the best way to describe it, but I don't think you will ever need this, it is here for ANY CASE +func URLEncodeFasthttp(path string) string { + if path == "" { + return "" + } + u := fasthttp.AcquireURI() + u.SetPath(path) + encodedPath := u.String()[8:] + fasthttp.ReleaseURI(u) + return encodedPath +} + // URL returns the subdomain+ host + Path(...optional named parameters if route is dynamic) // returns an empty string if parse is failed func URL(routeName string, args ...interface{}) (url string) {