From 685954d341b7edb00405ce1648332b316d671c60 Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Wed, 27 Dec 2017 15:38:06 +0200 Subject: [PATCH] add a simple context#YAML function to render yaml data and make sure that the cookie's values are url escaped and unescaped when retrieve from client's headers Former-commit-id: cad1e09679f6b646c12bb6ffdeba62738bdeef5d --- {mvc/ideas/1 => _examples/mvc/basic}/main.go | 4 --- context/context.go | 30 +++++++++++++++----- mvc/ideas/1/TODO.txt | 2 -- 3 files changed, 23 insertions(+), 13 deletions(-) rename {mvc/ideas/1 => _examples/mvc/basic}/main.go (80%) delete mode 100644 mvc/ideas/1/TODO.txt diff --git a/mvc/ideas/1/main.go b/_examples/mvc/basic/main.go similarity index 80% rename from mvc/ideas/1/main.go rename to _examples/mvc/basic/main.go index 70078627..24f81a14 100644 --- a/mvc/ideas/1/main.go +++ b/_examples/mvc/basic/main.go @@ -9,10 +9,6 @@ import ( "github.com/kataras/iris/mvc" ) -// TODO: It's not here but this file is what I'll see before the commit in order to delete it: -// Think a way to simplify the router cycle, I did create it to support any type of router -// but as I see nobody wants to override the iris router's behavior(I'm not speaking about wrapper, this will stay of course because it's useful on security-critical middlewares) because it's the best by far. -// Therefore I should reduce some "freedom of change" for the shake of code maintanability in the core/router files: handler.go | router.go and single change on APIBuilder's field. func main() { app := iris.New() app.Logger().SetLevel("debug") diff --git a/context/context.go b/context/context.go index 0f1f5d37..024353ce 100644 --- a/context/context.go +++ b/context/context.go @@ -27,6 +27,7 @@ import ( "github.com/json-iterator/go" "github.com/microcosm-cc/bluemonday" "github.com/russross/blackfriday" + "gopkg.in/yaml.v2" "github.com/kataras/iris/core/errors" "github.com/kataras/iris/core/memstore" @@ -696,9 +697,10 @@ type Context interface { JSONP(v interface{}, options ...JSONP) (int, error) // XML marshals the given interface object and writes the XML response. XML(v interface{}, options ...XML) (int, error) - // Markdown parses the markdown to html and renders to client. + // Markdown parses the markdown to html and renders its result to the client. Markdown(markdownB []byte, options ...Markdown) (int, error) - + // YAML parses the "v" using the yaml parser and renders its result to the client. + YAML(v interface{}) (int, error) // +------------------------------------------------------------+ // | Serve files | // +------------------------------------------------------------+ @@ -2119,9 +2121,10 @@ const ( ContentTextHeaderValue = "text/plain" // ContentXMLHeaderValue header value for XML data. ContentXMLHeaderValue = "text/xml" - // ContentMarkdownHeaderValue custom key/content type, the real is the text/html. ContentMarkdownHeaderValue = "text/markdown" + // ContentYAMLHeaderValue header value for YAML data. + ContentYAMLHeaderValue = "application/x-yaml" ) // Binary writes out the raw bytes as binary data. @@ -2382,7 +2385,7 @@ func (ctx *context) XML(v interface{}, opts ...XML) (int, error) { return n, err } -// WriteMarkdown parses the markdown to html and renders these contents to the writer. +// WriteMarkdown parses the markdown to html and writes these contents to the writer. func WriteMarkdown(writer io.Writer, markdownB []byte, options Markdown) (int, error) { buf := blackfriday.Run(markdownB) if options.Sanitize { @@ -2395,7 +2398,7 @@ func WriteMarkdown(writer io.Writer, markdownB []byte, options Markdown) (int, e // from `WriteMarkdown` and `ctx.Markdown`. var DefaultMarkdownOptions = Markdown{} -// Markdown parses the markdown to html and renders to the client. +// Markdown parses the markdown to html and renders its result to the client. func (ctx *context) Markdown(markdownB []byte, opts ...Markdown) (int, error) { options := DefaultMarkdownOptions @@ -2414,6 +2417,18 @@ func (ctx *context) Markdown(markdownB []byte, opts ...Markdown) (int, error) { return n, err } +// YAML marshals the "v" using the yaml marshaler and renders its result to the client. +func (ctx *context) YAML(v interface{}) (int, error) { + out, err := yaml.Marshal(v) + if err != nil { + ctx.StatusCode(http.StatusInternalServerError) + return 0, err + } + + ctx.ContentType(ContentYAMLHeaderValue) + return ctx.Write(out) +} + // +------------------------------------------------------------+ // | Serve files | // +------------------------------------------------------------+ @@ -2511,7 +2526,7 @@ var ( func (ctx *context) SetCookieKV(name, value string) { c := &http.Cookie{} c.Name = name - c.Value = value + c.Value = url.QueryEscape(value) c.HttpOnly = true c.Expires = time.Now().Add(SetCookieKVExpiration) c.MaxAge = int(SetCookieKVExpiration.Seconds()) @@ -2525,7 +2540,8 @@ func (ctx *context) GetCookie(name string) string { if err != nil { return "" } - return cookie.Value + value, _ := url.QueryUnescape(cookie.Value) + return value } // RemoveCookie deletes a cookie by it's name. diff --git a/mvc/ideas/1/TODO.txt b/mvc/ideas/1/TODO.txt deleted file mode 100644 index 02e3412f..00000000 --- a/mvc/ideas/1/TODO.txt +++ /dev/null @@ -1,2 +0,0 @@ -Remove the "ideas" folder or move this example somewhere in the _examples/mvc or even make a https://medium.com/@kataras -small tutorial about Iris' new MVC implementation. \ No newline at end of file