forked from yaitoo/xun
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviewer_xml.go
More file actions
41 lines (34 loc) · 961 Bytes
/
viewer_xml.go
File metadata and controls
41 lines (34 loc) · 961 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package xun
import (
"encoding/xml"
"net/http"
)
// XmlViewer is a viewer that writes the given data as xml to the http.ResponseWriter.
//
// It sets the Content-Type header to "application/xml".
type XmlViewer struct {
}
var XmlViewerMime = &MimeType{Type: "text", SubType: "xml"}
// MimeType returns the MIME type of the xml content.
//
// It returns "text/xml".
func (*XmlViewer) MimeType() *MimeType {
return XmlViewerMime
}
// Render renders the given data as xml to the http.ResponseWriter.
//
// It sets the Content-Type header to "text/xml; charset=utf-8".
func (*XmlViewer) Render(ctx *Context, data any) error { // skipcq: RVV-B0012
var err error
ctx.Response.Header().Set("Content-Type", "text/xml; charset=utf-8")
if ctx.Request.Method != http.MethodHead {
buf := BufPool.Get()
defer BufPool.Put(buf)
err = xml.NewEncoder(buf).Encode(data)
if err != nil {
return err
}
_, err = buf.WriteTo(ctx.Response)
}
return err
}