Files
gf/net/ghttp/ghttp_response.go

142 lines
4.1 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
"fmt"
"io"
"net/http"
"net/url"
"time"
"github.com/gogf/gf/v2/internal/utils"
2023-02-15 14:13:32 +08:00
"github.com/gogf/gf/v2/net/ghttp/internal/response"
"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 {
*response.BufferWriter // Underlying ResponseWriter.
Server *Server // Parent server.
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,
BufferWriter: response.NewBufferWriter(w),
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")
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)))
}
r.Header().Set("Access-Control-Expose-Headers", "Content-Disposition")
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...)
}
// 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) {
http.ServeContent(r.RawWriter(), r.Request.Request, name, modTime, content)
}
// Flush outputs the buffer content to the client and clears the buffer.
func (r *Response) Flush() {
r.Header().Set(responseHeaderTraceID, gtrace.GetTraceID(r.Request.Context()))
if r.Server.config.ServerAgent != "" {
r.Header().Set("Server", r.Server.config.ServerAgent)
}
r.BufferWriter.Flush()
}