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,
|
2019-02-02 16:18:25 +08:00
|
|
|
// 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"
|
2019-07-17 23:24:27 +08:00
|
|
|
"net/http"
|
|
|
|
|
2019-08-19 20:17:13 +08:00
|
|
|
"github.com/gogf/gf/os/gres"
|
|
|
|
|
2019-07-29 21:01:19 +08:00
|
|
|
"github.com/gogf/gf/os/gfile"
|
2017-12-07 14:57:16 +08:00
|
|
|
)
|
|
|
|
|
2019-11-20 12:09:26 +08:00
|
|
|
// Response is the http response manager.
|
2019-10-09 15:26:50 +08:00
|
|
|
// Note that it implements the http.ResponseWriter interface with buffering feature.
|
2018-01-02 16:35:13 +08:00
|
|
|
type Response struct {
|
2019-09-24 20:19:18 +08:00
|
|
|
*ResponseWriter // Underlying ResponseWriter.
|
|
|
|
Server *Server // Parent server.
|
|
|
|
Writer *ResponseWriter // Alias of ResponseWriter.
|
|
|
|
Request *Request // According request.
|
2018-04-20 15:43:02 +08:00
|
|
|
}
|
|
|
|
|
2019-10-09 15:26:50 +08:00
|
|
|
// newResponse creates and returns a new Response object.
|
2018-08-12 12:22:02 +08:00
|
|
|
func newResponse(s *Server, w http.ResponseWriter) *Response {
|
2019-06-19 09:06:52 +08:00
|
|
|
r := &Response{
|
|
|
|
Server: s,
|
2019-09-24 20:19:18 +08:00
|
|
|
ResponseWriter: &ResponseWriter{
|
|
|
|
writer: w,
|
|
|
|
buffer: bytes.NewBuffer(nil),
|
2019-06-19 09:06:52 +08:00
|
|
|
},
|
|
|
|
}
|
2019-09-24 20:19:18 +08:00
|
|
|
r.Writer = r.ResponseWriter
|
2019-06-19 09:06:52 +08:00
|
|
|
return r
|
2018-04-30 22:14:14 +08:00
|
|
|
}
|
|
|
|
|
2019-10-09 15:26:50 +08:00
|
|
|
// ServeFile serves the file to the response.
|
2019-07-24 22:48:43 +08:00
|
|
|
func (r *Response) ServeFile(path string, allowIndex ...bool) {
|
2021-01-19 19:33:21 +08:00
|
|
|
serveFile := (*staticFile)(nil)
|
2019-08-19 20:17:13 +08:00
|
|
|
if file := gres.Get(path); file != nil {
|
2021-01-19 19:33:21 +08:00
|
|
|
serveFile = &staticFile{
|
2020-01-17 21:12:52 +08:00
|
|
|
File: file,
|
|
|
|
IsDir: file.FileInfo().IsDir(),
|
2019-08-15 19:20:39 +08:00
|
|
|
}
|
2019-08-19 20:17:13 +08:00
|
|
|
} else {
|
2019-08-15 19:20:39 +08:00
|
|
|
path = gfile.RealPath(path)
|
|
|
|
if path == "" {
|
|
|
|
r.WriteStatus(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
2021-01-19 19:33:21 +08:00
|
|
|
serveFile = &staticFile{Path: path}
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2019-09-24 20:19:18 +08:00
|
|
|
r.Server.serveFile(r.Request, serveFile, allowIndex...)
|
2018-08-19 13:48:56 +08:00
|
|
|
}
|
|
|
|
|
2019-11-20 12:09:26 +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) {
|
2021-01-19 19:33:21 +08:00
|
|
|
serveFile := (*staticFile)(nil)
|
2019-06-19 09:06:52 +08:00
|
|
|
downloadName := ""
|
|
|
|
if len(name) > 0 {
|
|
|
|
downloadName = name[0]
|
2019-08-15 19:20:39 +08:00
|
|
|
}
|
2019-08-19 20:17:13 +08:00
|
|
|
if file := gres.Get(path); file != nil {
|
2021-01-19 19:33:21 +08:00
|
|
|
serveFile = &staticFile{
|
2020-01-17 21:12:52 +08:00
|
|
|
File: file,
|
|
|
|
IsDir: file.FileInfo().IsDir(),
|
2019-08-15 19:20:39 +08:00
|
|
|
}
|
2019-08-19 20:17:13 +08:00
|
|
|
if downloadName == "" {
|
|
|
|
downloadName = gfile.Basename(file.Name())
|
|
|
|
}
|
|
|
|
} else {
|
2019-08-15 19:20:39 +08:00
|
|
|
path = gfile.RealPath(path)
|
|
|
|
if path == "" {
|
|
|
|
r.WriteStatus(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
2021-01-19 19:33:21 +08:00
|
|
|
serveFile = &staticFile{Path: path}
|
2019-08-15 19:20:39 +08:00
|
|
|
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))
|
2019-09-24 20:19:18 +08:00
|
|
|
r.Server.serveFile(r.Request, serveFile)
|
2018-11-23 09:20:45 +08:00
|
|
|
}
|
|
|
|
|
2020-01-16 21:04:28 +08:00
|
|
|
// RedirectTo redirects 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)
|
2020-01-16 21:04:28 +08:00
|
|
|
if len(code) > 0 {
|
|
|
|
r.WriteHeader(code[0])
|
|
|
|
} else {
|
|
|
|
r.WriteHeader(http.StatusFound)
|
|
|
|
}
|
2019-09-24 20:19:18 +08:00
|
|
|
r.Request.Exit()
|
2018-04-14 01:05:46 +08:00
|
|
|
}
|
2017-12-07 14:57:16 +08:00
|
|
|
|
2020-01-16 21:04:28 +08:00
|
|
|
// RedirectBack redirects 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...)
|
2018-08-24 14:57:49 +08:00
|
|
|
}
|
|
|
|
|
2019-11-20 12:09:26 +08:00
|
|
|
// BufferString 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
|
|
|
}
|
|
|
|
|
2019-11-20 12:09:26 +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()
|
|
|
|
}
|
|
|
|
|
2019-11-20 12:09:26 +08:00
|
|
|
// BufferLength returns the length of the buffered content.
|
2018-05-04 14:35:20 +08:00
|
|
|
func (r *Response) BufferLength() int {
|
2019-06-19 09:06:52 +08:00
|
|
|
return r.buffer.Len()
|
2018-05-04 14:35:20 +08:00
|
|
|
}
|
|
|
|
|
2019-10-09 15:26:50 +08:00
|
|
|
// 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)
|
2018-04-14 01:05:46 +08:00
|
|
|
}
|
|
|
|
|
2019-10-09 15:26:50 +08:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2020-01-05 19:55:17 +08:00
|
|
|
// Output outputs the buffer content to the client and clears the buffer.
|
2020-06-15 23:36:20 +08:00
|
|
|
func (r *Response) Flush() {
|
2019-11-07 11:32:25 +08:00
|
|
|
if r.Server.config.ServerAgent != "" {
|
|
|
|
r.Header().Set("Server", r.Server.config.ServerAgent)
|
|
|
|
}
|
2020-06-15 23:36:20 +08:00
|
|
|
r.Writer.Flush()
|
2019-03-05 17:06:37 +08:00
|
|
|
}
|