Files
gf/net/ghttp/ghttp_response.go

153 lines
4.0 KiB
Go
Raw Permalink Normal View History

2021-01-17 21:46:25 +08:00
// Copyright GoFrame Author(https://goframe.org). 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"
"fmt"
"net/http"
"net/url"
"github.com/gogf/gf/v2/net/gtrace"
2021-10-11 21:41:56 +08:00
"github.com/gogf/gf/v2/os/gfile"
2021-11-13 23:23:55 +08:00
"github.com/gogf/gf/v2/os/gres"
2017-12-07 14:57:16 +08:00
)
// Response is the http response manager.
// 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
}
// ServeFile serves the file to the response.
2019-07-24 22:48:43 +08:00
func (r *Response) ServeFile(path string, allowIndex ...bool) {
var (
serveFile *staticFile
)
if file := gres.Get(path); file != nil {
serveFile = &staticFile{
File: file,
IsDir: file.FileInfo().IsDir(),
}
} else {
path, _ = gfile.Search(path)
if path == "" {
r.WriteStatus(http.StatusNotFound)
return
}
serveFile = &staticFile{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 downloading to the response.
2019-06-19 09:06:52 +08:00
func (r *Response) ServeFileDownload(path string, name ...string) {
var (
2021-11-13 23:23:55 +08:00
serveFile *staticFile
downloadName = ""
)
2021-11-13 23:23:55 +08:00
2019-06-19 09:06:52 +08:00
if len(name) > 0 {
downloadName = name[0]
}
if file := gres.Get(path); file != nil {
serveFile = &staticFile{
File: file,
IsDir: file.FileInfo().IsDir(),
}
if downloadName == "" {
downloadName = gfile.Basename(file.Name())
}
} else {
path, _ = gfile.Search(path)
if path == "" {
r.WriteStatus(http.StatusNotFound)
return
}
serveFile = &staticFile{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`, url.QueryEscape(downloadName)))
r.Server.serveFile(r.Request, serveFile)
2018-11-23 09:20:45 +08:00
}
2022-03-19 17:58:21 +08:00
// RedirectTo redirects the client to another location.
// The optional parameter `code` specifies the http status code for redirecting,
// which commonly can be 301 or 302. It's 302 in default.
func (r *Response) RedirectTo(location string, code ...int) {
2019-06-19 09:06:52 +08:00
r.Header().Set("Location", location)
if len(code) > 0 {
r.WriteHeader(code[0])
} else {
r.WriteHeader(http.StatusFound)
}
r.Request.Exit()
}
2017-12-07 14:57:16 +08:00
2022-03-19 17:58:21 +08:00
// RedirectBack redirects the client back to referer.
// The optional parameter `code` specifies the http status code for redirecting,
// which commonly can be 301 or 302. It's 302 in default.
func (r *Response) RedirectBack(code ...int) {
r.RedirectTo(r.Request.GetReferer(), code...)
}
// Buffer returns the buffered 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 buffered 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 buffered 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
}
// Flush outputs the buffer content to the client and clears the buffer.
func (r *Response) Flush() {
2021-12-19 00:18:18 +08:00
r.Header().Set(responseTraceIDHeader, gtrace.GetTraceID(r.Request.Context()))
if r.Server.config.ServerAgent != "" {
r.Header().Set("Server", r.Server.config.ServerAgent)
}
r.Writer.Flush()
}