gf/net/ghttp/ghttp_response.go

146 lines
3.9 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"
"fmt"
"net/http"
"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
)
// 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) {
serveFile := (*StaticFile)(nil)
if file := gres.Get(path); file != nil {
serveFile = &StaticFile{
File: file,
IsDir: file.FileInfo().IsDir(),
}
} else {
path = gfile.RealPath(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) {
serveFile := (*StaticFile)(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 = &StaticFile{
File: file,
IsDir: file.FileInfo().IsDir(),
}
if downloadName == "" {
downloadName = gfile.Basename(file.Name())
}
} else {
path = gfile.RealPath(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"`, downloadName))
r.Server.serveFile(r.Request, serveFile)
2018-11-23 09:20:45 +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)
if len(code) > 0 {
r.WriteHeader(code[0])
} else {
r.WriteHeader(http.StatusFound)
}
r.Request.Exit()
}
2017-12-07 14:57:16 +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...)
}
// 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
}
// 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
}
// Output outputs the buffer content to the client and clears the buffer.
func (r *Response) Output() {
if r.Server.config.ServerAgent != "" {
r.Header().Set("Server", r.Server.config.ServerAgent)
}
2019-06-19 09:06:52 +08:00
r.Writer.OutputBuffer()
}