mirror of
https://github.com/kataras/iris.git
synced 2025-03-15 17:36:29 +01:00
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
This commit is contained in:
parent
19e9ed330d
commit
685954d341
|
@ -9,10 +9,6 @@ import (
|
||||||
"github.com/kataras/iris/mvc"
|
"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() {
|
func main() {
|
||||||
app := iris.New()
|
app := iris.New()
|
||||||
app.Logger().SetLevel("debug")
|
app.Logger().SetLevel("debug")
|
|
@ -27,6 +27,7 @@ import (
|
||||||
"github.com/json-iterator/go"
|
"github.com/json-iterator/go"
|
||||||
"github.com/microcosm-cc/bluemonday"
|
"github.com/microcosm-cc/bluemonday"
|
||||||
"github.com/russross/blackfriday"
|
"github.com/russross/blackfriday"
|
||||||
|
"gopkg.in/yaml.v2"
|
||||||
|
|
||||||
"github.com/kataras/iris/core/errors"
|
"github.com/kataras/iris/core/errors"
|
||||||
"github.com/kataras/iris/core/memstore"
|
"github.com/kataras/iris/core/memstore"
|
||||||
|
@ -696,9 +697,10 @@ type Context interface {
|
||||||
JSONP(v interface{}, options ...JSONP) (int, error)
|
JSONP(v interface{}, options ...JSONP) (int, error)
|
||||||
// XML marshals the given interface object and writes the XML response.
|
// XML marshals the given interface object and writes the XML response.
|
||||||
XML(v interface{}, options ...XML) (int, error)
|
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)
|
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 |
|
// | Serve files |
|
||||||
// +------------------------------------------------------------+
|
// +------------------------------------------------------------+
|
||||||
|
@ -2119,9 +2121,10 @@ const (
|
||||||
ContentTextHeaderValue = "text/plain"
|
ContentTextHeaderValue = "text/plain"
|
||||||
// ContentXMLHeaderValue header value for XML data.
|
// ContentXMLHeaderValue header value for XML data.
|
||||||
ContentXMLHeaderValue = "text/xml"
|
ContentXMLHeaderValue = "text/xml"
|
||||||
|
|
||||||
// ContentMarkdownHeaderValue custom key/content type, the real is the text/html.
|
// ContentMarkdownHeaderValue custom key/content type, the real is the text/html.
|
||||||
ContentMarkdownHeaderValue = "text/markdown"
|
ContentMarkdownHeaderValue = "text/markdown"
|
||||||
|
// ContentYAMLHeaderValue header value for YAML data.
|
||||||
|
ContentYAMLHeaderValue = "application/x-yaml"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Binary writes out the raw bytes as binary data.
|
// 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
|
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) {
|
func WriteMarkdown(writer io.Writer, markdownB []byte, options Markdown) (int, error) {
|
||||||
buf := blackfriday.Run(markdownB)
|
buf := blackfriday.Run(markdownB)
|
||||||
if options.Sanitize {
|
if options.Sanitize {
|
||||||
|
@ -2395,7 +2398,7 @@ func WriteMarkdown(writer io.Writer, markdownB []byte, options Markdown) (int, e
|
||||||
// from `WriteMarkdown` and `ctx.Markdown`.
|
// from `WriteMarkdown` and `ctx.Markdown`.
|
||||||
var DefaultMarkdownOptions = 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) {
|
func (ctx *context) Markdown(markdownB []byte, opts ...Markdown) (int, error) {
|
||||||
options := DefaultMarkdownOptions
|
options := DefaultMarkdownOptions
|
||||||
|
|
||||||
|
@ -2414,6 +2417,18 @@ func (ctx *context) Markdown(markdownB []byte, opts ...Markdown) (int, error) {
|
||||||
return n, err
|
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 |
|
// | Serve files |
|
||||||
// +------------------------------------------------------------+
|
// +------------------------------------------------------------+
|
||||||
|
@ -2511,7 +2526,7 @@ var (
|
||||||
func (ctx *context) SetCookieKV(name, value string) {
|
func (ctx *context) SetCookieKV(name, value string) {
|
||||||
c := &http.Cookie{}
|
c := &http.Cookie{}
|
||||||
c.Name = name
|
c.Name = name
|
||||||
c.Value = value
|
c.Value = url.QueryEscape(value)
|
||||||
c.HttpOnly = true
|
c.HttpOnly = true
|
||||||
c.Expires = time.Now().Add(SetCookieKVExpiration)
|
c.Expires = time.Now().Add(SetCookieKVExpiration)
|
||||||
c.MaxAge = int(SetCookieKVExpiration.Seconds())
|
c.MaxAge = int(SetCookieKVExpiration.Seconds())
|
||||||
|
@ -2525,7 +2540,8 @@ func (ctx *context) GetCookie(name string) string {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
return cookie.Value
|
value, _ := url.QueryUnescape(cookie.Value)
|
||||||
|
return value
|
||||||
}
|
}
|
||||||
|
|
||||||
// RemoveCookie deletes a cookie by it's name.
|
// RemoveCookie deletes a cookie by it's name.
|
||||||
|
|
|
@ -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.
|
|
Loading…
Reference in New Issue
Block a user