gf/net/ghttp/ghttp_response.go

241 lines
6.2 KiB
Go
Raw Normal View History

// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
2017-12-29 16:03:30 +08:00
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
2017-12-29 16:03:30 +08:00
//
2017-12-31 18:19:58 +08:00
2017-12-07 14:57:16 +08:00
package ghttp
import (
2019-06-19 09:06:52 +08:00
"bytes"
2019-09-03 17:18:16 +08:00
"encoding/json"
2019-06-19 09:06:52 +08:00
"fmt"
"net/http"
"github.com/gogf/gf/os/gres"
2019-07-29 21:01:19 +08:00
"github.com/gogf/gf/encoding/gparser"
"github.com/gogf/gf/os/gfile"
"github.com/gogf/gf/util/gconv"
2017-12-07 14:57:16 +08:00
)
// Response is the writer for response buffer.
// Note that it implements the http.ResponseWriter interface with buffering feature.
2018-01-02 16:35:13 +08:00
type Response struct {
*ResponseWriter // Underlying ResponseWriter.
Server *Server // Parent server.
Writer *ResponseWriter // Alias of ResponseWriter.
Request *Request // According request.
2018-04-20 15:43:02 +08:00
}
// newResponse creates and returns a new Response object.
func newResponse(s *Server, w http.ResponseWriter) *Response {
2019-06-19 09:06:52 +08:00
r := &Response{
Server: s,
ResponseWriter: &ResponseWriter{
writer: w,
buffer: bytes.NewBuffer(nil),
2019-06-19 09:06:52 +08:00
},
}
r.Writer = r.ResponseWriter
2019-06-19 09:06:52 +08:00
return r
}
// Write writes <content> to the response buffer.
2019-06-19 09:06:52 +08:00
func (r *Response) Write(content ...interface{}) {
if r.hijacked || len(content) == 0 {
2019-06-19 09:06:52 +08:00
return
}
if r.Status == 0 {
r.Status = http.StatusOK
}
2019-06-19 09:06:52 +08:00
for _, v := range content {
switch value := v.(type) {
case []byte:
r.buffer.Write(value)
case string:
r.buffer.WriteString(value)
default:
r.buffer.WriteString(gconv.String(v))
}
}
2017-12-07 14:57:16 +08:00
}
// WriteOver overwrites the response buffer with <content>.
func (r *Response) WriteOver(content ...interface{}) {
r.ClearBuffer()
r.Write(content...)
}
// Writef writes the response with fmt.Sprintf.
2019-06-19 09:06:52 +08:00
func (r *Response) Writef(format string, params ...interface{}) {
r.Write(fmt.Sprintf(format, params...))
}
// Writef writes the response with <content> and new line.
2019-06-19 09:06:52 +08:00
func (r *Response) Writeln(content ...interface{}) {
if len(content) == 0 {
r.Write("\n")
return
}
r.Write(append(content, "\n")...)
}
// Writefln writes the response with fmt.Sprintf and new line.
2019-06-19 09:06:52 +08:00
func (r *Response) Writefln(format string, params ...interface{}) {
r.Writeln(fmt.Sprintf(format, params...))
}
// WriteJson writes <content> to the response with JSON format.
func (r *Response) WriteJson(content interface{}) error {
2019-09-03 17:18:16 +08:00
if b, err := json.Marshal(content); err != nil {
2019-06-19 09:06:52 +08:00
return err
} else {
r.Header().Set("Content-Type", "application/json")
r.Write(b)
}
return nil
2017-12-08 09:50:11 +08:00
}
// WriteJson writes <content> to the response with JSONP format.
// Note that there should be a "callback" parameter in the request for JSONP format.
func (r *Response) WriteJsonP(content interface{}) error {
2019-09-03 17:18:16 +08:00
if b, err := json.Marshal(content); err != nil {
2019-06-19 09:06:52 +08:00
return err
} else {
//r.Header().Set("Content-Type", "application/json")
if callback := r.Request.GetString("callback"); callback != "" {
2019-06-19 09:06:52 +08:00
buffer := []byte(callback)
buffer = append(buffer, byte('('))
buffer = append(buffer, b...)
buffer = append(buffer, byte(')'))
r.Write(buffer)
} else {
r.Write(b)
}
}
return nil
}
// WriteJson writes <content> to the response with XML format.
2019-06-19 09:06:52 +08:00
func (r *Response) WriteXml(content interface{}, rootTag ...string) error {
if b, err := gparser.VarToXml(content, rootTag...); err != nil {
return err
} else {
r.Header().Set("Content-Type", "application/xml")
r.Write(b)
}
return nil
}
// WriteStatus writes HTTP <status> and <content> to the response.
func (r *Response) WriteStatus(status int, content ...interface{}) {
r.WriteHeader(status)
if len(content) > 0 {
r.Write(content...)
} else {
r.Write(http.StatusText(status))
2019-06-19 09:06:52 +08:00
}
if r.Header().Get("Content-Type") == "" {
r.Header().Set("Content-Type", "text/plain; charset=utf-8")
//r.Header().Set("X-Content-Type-Options", "nosniff")
}
}
// ServeFile serves the file to the response.
2019-07-24 22:48:43 +08:00
func (r *Response) ServeFile(path string, allowIndex ...bool) {
serveFile := (*staticServeFile)(nil)
if file := gres.Get(path); file != nil {
serveFile = &staticServeFile{
file: file,
dir: file.FileInfo().IsDir(),
}
} else {
path = gfile.RealPath(path)
if path == "" {
r.WriteStatus(http.StatusNotFound)
return
}
serveFile = &staticServeFile{path: path}
2019-06-19 09:06:52 +08:00
}
r.Server.serveFile(r.Request, serveFile, allowIndex...)
2018-08-19 13:48:56 +08:00
}
// ServeFileDownload serves file as file downloading to the response.
2019-06-19 09:06:52 +08:00
func (r *Response) ServeFileDownload(path string, name ...string) {
serveFile := (*staticServeFile)(nil)
2019-06-19 09:06:52 +08:00
downloadName := ""
if len(name) > 0 {
downloadName = name[0]
}
if file := gres.Get(path); file != nil {
serveFile = &staticServeFile{
file: file,
dir: file.FileInfo().IsDir(),
}
if downloadName == "" {
downloadName = gfile.Basename(file.Name())
}
} else {
path = gfile.RealPath(path)
if path == "" {
r.WriteStatus(http.StatusNotFound)
return
}
serveFile = &staticServeFile{path: path}
if downloadName == "" {
downloadName = gfile.Basename(path)
}
2019-06-19 09:06:52 +08:00
}
r.Header().Set("Content-Type", "application/force-download")
r.Header().Set("Accept-Ranges", "bytes")
r.Header().Set("Content-Disposition", fmt.Sprintf(`attachment;filename="%s"`, downloadName))
r.Server.serveFile(r.Request, serveFile)
2018-11-23 09:20:45 +08:00
}
// RedirectTo redirects client to another location.
func (r *Response) RedirectTo(location string) {
2019-06-19 09:06:52 +08:00
r.Header().Set("Location", location)
r.WriteHeader(http.StatusFound)
r.Request.Exit()
}
2017-12-07 14:57:16 +08:00
// RedirectBack redirects client back to referer.
func (r *Response) RedirectBack() {
r.RedirectTo(r.Request.GetReferer())
}
// BufferString returns the buffer content as []byte.
2018-01-02 16:35:13 +08:00
func (r *Response) Buffer() []byte {
2019-06-19 09:06:52 +08:00
return r.buffer.Bytes()
2017-12-26 10:13:49 +08:00
}
// BufferString returns the buffer content as string.
2019-09-23 16:21:19 +08:00
func (r *Response) BufferString() string {
return r.buffer.String()
}
// BufferLength returns the length of the buffer content.
func (r *Response) BufferLength() int {
2019-06-19 09:06:52 +08:00
return r.buffer.Len()
}
// SetBuffer overwrites the buffer with <data>.
2018-11-17 11:17:02 +08:00
func (r *Response) SetBuffer(data []byte) {
2019-06-19 09:06:52 +08:00
r.buffer.Reset()
r.buffer.Write(data)
}
// ClearBuffer clears the response buffer.
2018-01-02 16:35:13 +08:00
func (r *Response) ClearBuffer() {
2019-06-19 09:06:52 +08:00
r.buffer.Reset()
2017-12-28 15:21:25 +08:00
}
// Output outputs the buffer content to the client.
func (r *Response) Output() {
2019-06-19 09:06:52 +08:00
r.Header().Set("Server", r.Server.config.ServerAgent)
//r.handleGzip()
r.Writer.OutputBuffer()
}