2019-02-02 16:18:25 +08:00
|
|
|
|
// 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,
|
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"
|
2019-09-03 17:18:16 +08:00
|
|
|
|
"encoding/json"
|
2019-06-19 09:06:52 +08:00
|
|
|
|
"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/encoding/gparser"
|
|
|
|
|
"github.com/gogf/gf/os/gfile"
|
|
|
|
|
"github.com/gogf/gf/util/gconv"
|
2017-12-07 14:57:16 +08:00
|
|
|
|
)
|
|
|
|
|
|
2018-08-21 21:18:56 +08:00
|
|
|
|
// 服务端请求返回对象。
|
|
|
|
|
// 注意该对象并没有实现http.ResponseWriter接口,而是依靠ghttp.ResponseWriter实现。
|
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
|
|
|
|
}
|
|
|
|
|
|
2018-04-30 22:14:14 +08:00
|
|
|
|
// 创建一个ghttp.Response对象指针
|
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
|
|
|
|
}
|
|
|
|
|
|
2018-04-14 01:05:46 +08:00
|
|
|
|
// 返回信息,任何变量自动转换为bytes
|
2019-06-19 09:06:52 +08:00
|
|
|
|
func (r *Response) Write(content ...interface{}) {
|
|
|
|
|
if len(content) == 0 {
|
|
|
|
|
return
|
|
|
|
|
}
|
2019-09-24 20:19:18 +08:00
|
|
|
|
if r.Status == 0 && r.Request.hasServeHandler {
|
2019-09-14 22:53:28 +08:00
|
|
|
|
r.Status = http.StatusOK
|
|
|
|
|
}
|
2019-06-19 09:06:52 +08:00
|
|
|
|
for _, v := range content {
|
|
|
|
|
switch value := v.(type) {
|
|
|
|
|
case []byte:
|
|
|
|
|
r.buffer.Write(value)
|
|
|
|
|
case string:
|
|
|
|
|
r.buffer.WriteString(value)
|
|
|
|
|
default:
|
|
|
|
|
r.buffer.WriteString(gconv.String(v))
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-12-07 14:57:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-06 12:48:15 +08:00
|
|
|
|
// 返回信息,支持自定义format格式
|
2019-06-19 09:06:52 +08:00
|
|
|
|
func (r *Response) Writef(format string, params ...interface{}) {
|
|
|
|
|
r.Write(fmt.Sprintf(format, params...))
|
2018-08-06 12:48:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-15 10:30:59 +08:00
|
|
|
|
// 返回信息,末尾增加换行标识符"\n"
|
2019-06-19 09:06:52 +08:00
|
|
|
|
func (r *Response) Writeln(content ...interface{}) {
|
|
|
|
|
if len(content) == 0 {
|
|
|
|
|
r.Write("\n")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
content = append(content, "\n")
|
|
|
|
|
r.Write(content...)
|
2018-04-15 10:30:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-06 12:48:15 +08:00
|
|
|
|
// 返回信息,末尾增加换行标识符"\n"
|
2019-06-19 09:06:52 +08:00
|
|
|
|
func (r *Response) Writefln(format string, params ...interface{}) {
|
|
|
|
|
r.Writeln(fmt.Sprintf(format, params...))
|
2018-08-06 12:48:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-14 01:05:46 +08:00
|
|
|
|
// 返回JSON
|
|
|
|
|
func (r *Response) WriteJson(content interface{}) error {
|
2019-09-03 17:18:16 +08:00
|
|
|
|
if b, err := json.Marshal(content); err != nil {
|
2019-06-19 09:06:52 +08:00
|
|
|
|
return err
|
|
|
|
|
} else {
|
|
|
|
|
r.Header().Set("Content-Type", "application/json")
|
|
|
|
|
r.Write(b)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
2017-12-08 09:50:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-16 14:33:25 +08:00
|
|
|
|
// 返回JSONP
|
|
|
|
|
func (r *Response) WriteJsonP(content interface{}) error {
|
2019-09-03 17:18:16 +08:00
|
|
|
|
if b, err := json.Marshal(content); err != nil {
|
2019-06-19 09:06:52 +08:00
|
|
|
|
return err
|
|
|
|
|
} else {
|
|
|
|
|
//r.Header().Set("Content-Type", "application/json")
|
2019-09-24 20:19:18 +08:00
|
|
|
|
if callback := r.Request.GetString("callback"); callback != "" {
|
2019-06-19 09:06:52 +08:00
|
|
|
|
buffer := []byte(callback)
|
|
|
|
|
buffer = append(buffer, byte('('))
|
|
|
|
|
buffer = append(buffer, b...)
|
|
|
|
|
buffer = append(buffer, byte(')'))
|
|
|
|
|
r.Write(buffer)
|
|
|
|
|
} else {
|
|
|
|
|
r.Write(b)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
2018-04-16 14:33:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-14 01:05:46 +08:00
|
|
|
|
// 返回XML
|
2019-06-19 09:06:52 +08:00
|
|
|
|
func (r *Response) WriteXml(content interface{}, rootTag ...string) error {
|
|
|
|
|
if b, err := gparser.VarToXml(content, rootTag...); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
} else {
|
|
|
|
|
r.Header().Set("Content-Type", "application/xml")
|
|
|
|
|
r.Write(b)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
2018-04-14 01:05:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 返回HTTP Code状态码
|
2019-07-17 23:24:27 +08:00
|
|
|
|
func (r *Response) WriteStatus(status int, content ...interface{}) {
|
2019-06-19 09:06:52 +08:00
|
|
|
|
if r.buffer.Len() == 0 {
|
|
|
|
|
// 状态码注册回调函数处理
|
|
|
|
|
if status != http.StatusOK {
|
2019-09-24 20:19:18 +08:00
|
|
|
|
if f := r.Request.Server.getStatusHandler(status, r.Request); f != nil {
|
2019-08-03 15:54:12 +08:00
|
|
|
|
niceCallFunc(func() {
|
2019-09-24 20:19:18 +08:00
|
|
|
|
f(r.Request)
|
2019-06-19 09:06:52 +08:00
|
|
|
|
})
|
|
|
|
|
// 防止多次设置(http: multiple response.WriteHeader calls)
|
|
|
|
|
if r.Status == 0 {
|
|
|
|
|
r.WriteHeader(status)
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-17 23:24:27 +08:00
|
|
|
|
if r.Header().Get("Content-Type") == "" {
|
|
|
|
|
r.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
|
|
|
|
//r.Header().Set("X-Content-Type-Options", "nosniff")
|
|
|
|
|
}
|
2019-06-19 09:06:52 +08:00
|
|
|
|
if len(content) > 0 {
|
2019-07-17 23:24:27 +08:00
|
|
|
|
r.Write(content...)
|
2019-06-19 09:06:52 +08:00
|
|
|
|
} else {
|
|
|
|
|
r.Write(http.StatusText(status))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
r.WriteHeader(status)
|
2018-04-14 01:05:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-19 13:48:56 +08:00
|
|
|
|
// 静态文件处理
|
2019-07-24 22:48:43 +08:00
|
|
|
|
func (r *Response) ServeFile(path string, allowIndex ...bool) {
|
2019-08-15 19:20:39 +08:00
|
|
|
|
serveFile := (*staticServeFile)(nil)
|
2019-08-19 20:17:13 +08:00
|
|
|
|
if file := gres.Get(path); file != nil {
|
|
|
|
|
serveFile = &staticServeFile{
|
|
|
|
|
file: file,
|
|
|
|
|
dir: 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
|
|
|
|
|
}
|
|
|
|
|
serveFile = &staticServeFile{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
|
|
|
|
}
|
|
|
|
|
|
2018-11-23 09:20:45 +08:00
|
|
|
|
// 静态文件下载处理
|
2019-06-19 09:06:52 +08:00
|
|
|
|
func (r *Response) ServeFileDownload(path string, name ...string) {
|
2019-08-15 19:20:39 +08:00
|
|
|
|
serveFile := (*staticServeFile)(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 {
|
|
|
|
|
serveFile = &staticServeFile{
|
|
|
|
|
file: file,
|
|
|
|
|
dir: 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
|
|
|
|
|
}
|
|
|
|
|
serveFile = &staticServeFile{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))
|
2019-09-24 20:19:18 +08:00
|
|
|
|
r.Server.serveFile(r.Request, serveFile)
|
2018-11-23 09:20:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-05 17:06:37 +08:00
|
|
|
|
// 返回location标识,引导客户端跳转。
|
|
|
|
|
// 注意这里要先把设置的cookie输出,否则会被忽略。
|
2018-04-14 01:05:46 +08:00
|
|
|
|
func (r *Response) RedirectTo(location string) {
|
2019-06-19 09:06:52 +08:00
|
|
|
|
r.Header().Set("Location", location)
|
|
|
|
|
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
|
|
|
|
|
2018-08-24 14:57:49 +08:00
|
|
|
|
// 返回location标识,引导客户端跳转到来源页面
|
|
|
|
|
func (r *Response) RedirectBack() {
|
2019-09-24 20:19:18 +08:00
|
|
|
|
r.RedirectTo(r.Request.GetReferer())
|
2018-08-24 14:57:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 13:31:07 +08:00
|
|
|
|
// 获取当前缓冲区中的数据
|
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-09-23 16:21:19 +08:00
|
|
|
|
// 获取当前缓冲区中的数据(string)
|
|
|
|
|
func (r *Response) BufferString() string {
|
|
|
|
|
return r.buffer.String()
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2018-04-14 01:05:46 +08:00
|
|
|
|
// 手动设置缓冲区内容
|
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
|
|
|
|
}
|
|
|
|
|
|
2017-12-28 15:21:25 +08:00
|
|
|
|
// 清空缓冲区内容
|
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
|
|
|
|
}
|
|
|
|
|
|
2019-03-05 17:06:37 +08:00
|
|
|
|
// 输出缓冲区数据到客户端.
|
|
|
|
|
func (r *Response) Output() {
|
2019-06-19 09:06:52 +08:00
|
|
|
|
r.Header().Set("Server", r.Server.config.ServerAgent)
|
|
|
|
|
//r.handleGzip()
|
|
|
|
|
r.Writer.OutputBuffer()
|
2019-03-05 17:06:37 +08:00
|
|
|
|
}
|