2021-01-05 13:05:40 +00:00
|
|
|
package render
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"encoding/xml"
|
|
|
|
"html/template"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Engine is the generic interface for all responses.
|
|
|
|
type Engine interface {
|
|
|
|
Render(io.Writer, interface{}) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Head defines the basic ContentType and Status fields.
|
|
|
|
type Head struct {
|
|
|
|
ContentType string
|
|
|
|
Status int
|
|
|
|
}
|
|
|
|
|
|
|
|
// Data built-in renderer.
|
|
|
|
type Data struct {
|
|
|
|
Head
|
|
|
|
}
|
|
|
|
|
|
|
|
// HTML built-in renderer.
|
|
|
|
type HTML struct {
|
|
|
|
Head
|
|
|
|
Name string
|
|
|
|
Templates *template.Template
|
2021-04-22 09:54:27 +00:00
|
|
|
|
|
|
|
bp GenericBufferPool
|
2021-01-05 13:05:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// JSON built-in renderer.
|
|
|
|
type JSON struct {
|
|
|
|
Head
|
|
|
|
Indent bool
|
|
|
|
UnEscapeHTML bool
|
|
|
|
Prefix []byte
|
|
|
|
StreamingJSON bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// JSONP built-in renderer.
|
|
|
|
type JSONP struct {
|
|
|
|
Head
|
|
|
|
Indent bool
|
|
|
|
Callback string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Text built-in renderer.
|
|
|
|
type Text struct {
|
|
|
|
Head
|
|
|
|
}
|
|
|
|
|
|
|
|
// XML built-in renderer.
|
|
|
|
type XML struct {
|
|
|
|
Head
|
|
|
|
Indent bool
|
|
|
|
Prefix []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write outputs the header content.
|
|
|
|
func (h Head) Write(w http.ResponseWriter) {
|
|
|
|
w.Header().Set(ContentType, h.ContentType)
|
|
|
|
w.WriteHeader(h.Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render a data response.
|
|
|
|
func (d Data) Render(w io.Writer, v interface{}) error {
|
|
|
|
if hw, ok := w.(http.ResponseWriter); ok {
|
|
|
|
c := hw.Header().Get(ContentType)
|
|
|
|
if c != "" {
|
|
|
|
d.Head.ContentType = c
|
|
|
|
}
|
|
|
|
d.Head.Write(hw)
|
|
|
|
}
|
|
|
|
|
2021-06-10 14:44:25 +00:00
|
|
|
_, _ = w.Write(v.([]byte))
|
2021-01-05 13:05:40 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render a HTML response.
|
|
|
|
func (h HTML) Render(w io.Writer, binding interface{}) error {
|
2021-04-22 09:54:27 +00:00
|
|
|
var buf *bytes.Buffer
|
|
|
|
if h.bp != nil {
|
|
|
|
// If we have a bufferpool, allocate from it
|
|
|
|
buf = h.bp.Get()
|
|
|
|
defer h.bp.Put(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := h.Templates.ExecuteTemplate(buf, h.Name, binding)
|
2021-01-05 13:05:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if hw, ok := w.(http.ResponseWriter); ok {
|
|
|
|
h.Head.Write(hw)
|
|
|
|
}
|
2021-06-10 14:44:25 +00:00
|
|
|
_, _ = buf.WriteTo(w)
|
2021-01-05 13:05:40 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render a JSON response.
|
|
|
|
func (j JSON) Render(w io.Writer, v interface{}) error {
|
|
|
|
if j.StreamingJSON {
|
|
|
|
return j.renderStreamingJSON(w, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
var result []byte
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if j.Indent {
|
|
|
|
result, err = json.MarshalIndent(v, "", " ")
|
|
|
|
result = append(result, '\n')
|
|
|
|
} else {
|
|
|
|
result, err = json.Marshal(v)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unescape HTML if needed.
|
|
|
|
if j.UnEscapeHTML {
|
2021-06-10 14:44:25 +00:00
|
|
|
result = bytes.ReplaceAll(result, []byte("\\u003c"), []byte("<"))
|
|
|
|
result = bytes.ReplaceAll(result, []byte("\\u003e"), []byte(">"))
|
|
|
|
result = bytes.ReplaceAll(result, []byte("\\u0026"), []byte("&"))
|
2021-01-05 13:05:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// JSON marshaled fine, write out the result.
|
|
|
|
if hw, ok := w.(http.ResponseWriter); ok {
|
|
|
|
j.Head.Write(hw)
|
|
|
|
}
|
|
|
|
if len(j.Prefix) > 0 {
|
2021-06-10 14:44:25 +00:00
|
|
|
_, _ = w.Write(j.Prefix)
|
2021-01-05 13:05:40 +00:00
|
|
|
}
|
2021-06-10 14:44:25 +00:00
|
|
|
_, _ = w.Write(result)
|
2021-01-05 13:05:40 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j JSON) renderStreamingJSON(w io.Writer, v interface{}) error {
|
|
|
|
if hw, ok := w.(http.ResponseWriter); ok {
|
|
|
|
j.Head.Write(hw)
|
|
|
|
}
|
|
|
|
if len(j.Prefix) > 0 {
|
2021-06-10 14:44:25 +00:00
|
|
|
_, _ = w.Write(j.Prefix)
|
2021-01-05 13:05:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return json.NewEncoder(w).Encode(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render a JSONP response.
|
|
|
|
func (j JSONP) Render(w io.Writer, v interface{}) error {
|
|
|
|
var result []byte
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if j.Indent {
|
|
|
|
result, err = json.MarshalIndent(v, "", " ")
|
|
|
|
} else {
|
|
|
|
result, err = json.Marshal(v)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// JSON marshaled fine, write out the result.
|
|
|
|
if hw, ok := w.(http.ResponseWriter); ok {
|
|
|
|
j.Head.Write(hw)
|
|
|
|
}
|
2021-06-10 14:44:25 +00:00
|
|
|
_, _ = w.Write([]byte(j.Callback + "("))
|
|
|
|
_, _ = w.Write(result)
|
|
|
|
_, _ = w.Write([]byte(");"))
|
2021-01-05 13:05:40 +00:00
|
|
|
|
|
|
|
// If indenting, append a new line.
|
|
|
|
if j.Indent {
|
2021-06-10 14:44:25 +00:00
|
|
|
_, _ = w.Write([]byte("\n"))
|
2021-01-05 13:05:40 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render a text response.
|
|
|
|
func (t Text) Render(w io.Writer, v interface{}) error {
|
|
|
|
if hw, ok := w.(http.ResponseWriter); ok {
|
|
|
|
c := hw.Header().Get(ContentType)
|
|
|
|
if c != "" {
|
|
|
|
t.Head.ContentType = c
|
|
|
|
}
|
|
|
|
t.Head.Write(hw)
|
|
|
|
}
|
|
|
|
|
2021-06-10 14:44:25 +00:00
|
|
|
_, _ = w.Write([]byte(v.(string)))
|
2021-01-05 13:05:40 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render an XML response.
|
|
|
|
func (x XML) Render(w io.Writer, v interface{}) error {
|
|
|
|
var result []byte
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if x.Indent {
|
|
|
|
result, err = xml.MarshalIndent(v, "", " ")
|
|
|
|
result = append(result, '\n')
|
|
|
|
} else {
|
|
|
|
result, err = xml.Marshal(v)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// XML marshaled fine, write out the result.
|
|
|
|
if hw, ok := w.(http.ResponseWriter); ok {
|
|
|
|
x.Head.Write(hw)
|
|
|
|
}
|
|
|
|
if len(x.Prefix) > 0 {
|
2021-06-10 14:44:25 +00:00
|
|
|
_, _ = w.Write(x.Prefix)
|
2021-01-05 13:05:40 +00:00
|
|
|
}
|
2021-06-10 14:44:25 +00:00
|
|
|
_, _ = w.Write(result)
|
2021-01-05 13:05:40 +00:00
|
|
|
return nil
|
|
|
|
}
|