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
|
|
|
"fmt"
|
2022-12-22 10:25:30 +08:00
|
|
|
"io"
|
2019-07-17 23:24:27 +08:00
|
|
|
"net/http"
|
2022-03-01 18:32:11 +08:00
|
|
|
"net/url"
|
2022-12-22 10:25:30 +08:00
|
|
|
"time"
|
2019-07-17 23:24:27 +08:00
|
|
|
|
2025-10-10 10:32:02 +08:00
|
|
|
"github.com/gogf/gf/v2/internal/utils"
|
2023-02-15 14:13:32 +08:00
|
|
|
"github.com/gogf/gf/v2/net/ghttp/internal/response"
|
2021-12-18 21:54:12 +08:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
|
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 {
|
2024-03-24 21:18:30 +08:00
|
|
|
*response.BufferWriter // Underlying ResponseWriter.
|
|
|
|
|
Server *Server // Parent server.
|
|
|
|
|
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{
|
2024-03-24 21:18:30 +08:00
|
|
|
Server: s,
|
|
|
|
|
BufferWriter: response.NewBufferWriter(w),
|
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-08-16 20:30:58 +08:00
|
|
|
var (
|
|
|
|
|
serveFile *staticFile
|
|
|
|
|
)
|
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 {
|
2021-08-16 20:30:58 +08:00
|
|
|
path, _ = gfile.Search(path)
|
2019-08-15 19:20:39 +08:00
|
|
|
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-08-16 20:30:58 +08:00
|
|
|
var (
|
2021-11-13 23:23:55 +08:00
|
|
|
serveFile *staticFile
|
|
|
|
|
downloadName = ""
|
2021-08-16 20:30:58 +08:00
|
|
|
)
|
2021-11-13 23:23:55 +08:00
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
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 {
|
2021-08-16 20:30:58 +08:00
|
|
|
path, _ = gfile.Search(path)
|
2019-08-15 19:20:39 +08:00
|
|
|
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")
|
2025-10-10 10:32:02 +08:00
|
|
|
if utils.IsASCII(downloadName) {
|
|
|
|
|
r.Header().Set("Content-Disposition", fmt.Sprintf(`attachment;filename=%s`, url.QueryEscape(downloadName)))
|
|
|
|
|
} else {
|
|
|
|
|
r.Header().Set("Content-Disposition", fmt.Sprintf(`attachment;filename*=UTF-8''%s`, url.QueryEscape(downloadName)))
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-28 20:07:56 +08:00
|
|
|
r.Header().Set("Access-Control-Expose-Headers", "Content-Disposition")
|
2019-09-24 20:19:18 +08:00
|
|
|
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.
|
2021-10-21 18:22:47 +08:00
|
|
|
// The optional parameter `code` specifies the http status code for redirecting,
|
2020-01-16 21:04:28 +08:00
|
|
|
// 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
|
|
|
|
2022-03-19 17:58:21 +08:00
|
|
|
// RedirectBack redirects the client back to referer.
|
2021-10-21 18:22:47 +08:00
|
|
|
// The optional parameter `code` specifies the http status code for redirecting,
|
2020-01-16 21:04:28 +08:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
|
2022-12-22 10:25:30 +08:00
|
|
|
// ServeContent replies to the request using the content in the
|
|
|
|
|
// provided ReadSeeker. The main benefit of ServeContent over io.Copy
|
|
|
|
|
// is that it handles Range requests properly, sets the MIME type, and
|
|
|
|
|
// handles If-Match, If-Unmodified-Since, If-None-Match, If-Modified-Since,
|
|
|
|
|
// and If-Range requests.
|
|
|
|
|
//
|
|
|
|
|
// See http.ServeContent
|
|
|
|
|
func (r *Response) ServeContent(name string, modTime time.Time, content io.ReadSeeker) {
|
2024-03-24 21:18:30 +08:00
|
|
|
http.ServeContent(r.RawWriter(), r.Request.Request, name, modTime, content)
|
2022-12-22 10:25:30 +08:00
|
|
|
}
|
|
|
|
|
|
2021-07-13 23:01:31 +08:00
|
|
|
// Flush outputs the buffer content to the client and clears the buffer.
|
2020-06-15 23:36:20 +08:00
|
|
|
func (r *Response) Flush() {
|
2022-11-14 19:57:39 +08:00
|
|
|
r.Header().Set(responseHeaderTraceID, gtrace.GetTraceID(r.Request.Context()))
|
2019-11-07 11:32:25 +08:00
|
|
|
if r.Server.config.ServerAgent != "" {
|
|
|
|
|
r.Header().Set("Server", r.Server.config.ServerAgent)
|
|
|
|
|
}
|
2024-03-24 21:18:30 +08:00
|
|
|
r.BufferWriter.Flush()
|
2019-03-05 17:06:37 +08:00
|
|
|
}
|