2017-12-29 16:03:30 +08:00
|
|
|
|
// Copyright 2017 gf Author(https://gitee.com/johng/gf). All Rights Reserved.
|
|
|
|
|
//
|
|
|
|
|
// 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://gitee.com/johng/gf.
|
|
|
|
|
//
|
2017-12-31 18:19:58 +08:00
|
|
|
|
|
2017-12-07 14:57:16 +08:00
|
|
|
|
package ghttp
|
|
|
|
|
|
|
|
|
|
import (
|
2017-12-13 11:36:29 +08:00
|
|
|
|
"sync"
|
2018-01-23 15:02:42 +08:00
|
|
|
|
"net/http"
|
2018-04-14 01:05:46 +08:00
|
|
|
|
"gitee.com/johng/gf/g/util/gconv"
|
|
|
|
|
"gitee.com/johng/gf/g/encoding/gparser"
|
2018-04-16 14:33:25 +08:00
|
|
|
|
"strconv"
|
2018-08-06 12:48:15 +08:00
|
|
|
|
"fmt"
|
2017-12-07 14:57:16 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// 服务端请求返回对象
|
2018-01-02 16:35:13 +08:00
|
|
|
|
type Response struct {
|
2018-04-20 15:43:02 +08:00
|
|
|
|
ResponseWriter
|
2018-04-30 22:14:14 +08:00
|
|
|
|
Writer *ResponseWriter // io.Writer
|
|
|
|
|
mu sync.RWMutex // 缓冲区互斥锁
|
|
|
|
|
buffer []byte // 每个请求的返回数据缓冲区
|
|
|
|
|
request *Request // 关联的Request请求对象
|
2018-04-20 15:43:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 自定义的ResponseWriter,用于写入流的控制
|
|
|
|
|
type ResponseWriter struct {
|
|
|
|
|
http.ResponseWriter
|
2018-04-20 18:52:04 +08:00
|
|
|
|
Status int // http status
|
|
|
|
|
Length int // response length
|
2018-04-20 15:43:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-30 22:14:14 +08:00
|
|
|
|
// 创建一个ghttp.Response对象指针
|
|
|
|
|
func newResponse(w http.ResponseWriter) *Response {
|
|
|
|
|
r := &Response {
|
|
|
|
|
ResponseWriter : ResponseWriter{w, http.StatusOK, 0},
|
|
|
|
|
}
|
|
|
|
|
r.Writer = &r.ResponseWriter
|
|
|
|
|
return r
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-20 15:43:02 +08:00
|
|
|
|
// 覆盖父级的WriteHeader方法
|
|
|
|
|
func (w *ResponseWriter) Write(buffer []byte) (int, error) {
|
|
|
|
|
n, e := w.ResponseWriter.Write(buffer)
|
2018-04-20 18:52:04 +08:00
|
|
|
|
w.Length += n
|
2018-04-20 15:43:02 +08:00
|
|
|
|
return n, e
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 覆盖父级的WriteHeader方法
|
|
|
|
|
func (w *ResponseWriter) WriteHeader(code int) {
|
2018-04-20 18:52:04 +08:00
|
|
|
|
w.Status = code
|
2018-04-20 15:43:02 +08:00
|
|
|
|
w.ResponseWriter.WriteHeader(code)
|
2017-12-07 14:57:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-14 01:05:46 +08:00
|
|
|
|
// 返回信息,任何变量自动转换为bytes
|
2018-04-15 10:30:59 +08:00
|
|
|
|
func (r *Response) Write(content ... interface{}) {
|
|
|
|
|
if len(content) == 0 {
|
|
|
|
|
return
|
|
|
|
|
}
|
2018-04-20 15:43:02 +08:00
|
|
|
|
r.mu.Lock()
|
2018-04-15 10:30:59 +08:00
|
|
|
|
for _, v := range content {
|
2018-04-30 22:34:28 +08:00
|
|
|
|
switch v.(type) {
|
|
|
|
|
case []byte:
|
|
|
|
|
// 如果是二进制数据,那么返回二进制数据
|
|
|
|
|
r.buffer = append(r.buffer, gconv.Bytes(v)...)
|
|
|
|
|
default:
|
|
|
|
|
// 否则一律按照可显示的字符串进行转换
|
|
|
|
|
r.buffer = append(r.buffer, gconv.String(v)...)
|
|
|
|
|
}
|
2018-04-15 10:30:59 +08:00
|
|
|
|
}
|
2018-04-20 15:43:02 +08:00
|
|
|
|
r.mu.Unlock()
|
2017-12-07 14:57:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-06 12:48:15 +08:00
|
|
|
|
// 返回信息,支持自定义format格式
|
|
|
|
|
func (r *Response) Writef(format string, params ... interface{}) {
|
|
|
|
|
r.Write(fmt.Sprintf(format, params...))
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-15 10:30:59 +08:00
|
|
|
|
// 返回信息,末尾增加换行标识符"\n"
|
|
|
|
|
func (r *Response) Writeln(content ... interface{}) {
|
|
|
|
|
if len(content) == 0 {
|
2018-07-24 23:43:00 +08:00
|
|
|
|
r.Write("\n")
|
2018-04-15 10:30:59 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
content = append(content, "\n")
|
|
|
|
|
r.Write(content...)
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-06 12:48:15 +08:00
|
|
|
|
// 返回信息,末尾增加换行标识符"\n"
|
|
|
|
|
func (r *Response) Writefln(format string, params ... interface{}) {
|
|
|
|
|
r.Writeln(fmt.Sprintf(format, params...))
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-14 01:05:46 +08:00
|
|
|
|
// 返回JSON
|
|
|
|
|
func (r *Response) WriteJson(content interface{}) error {
|
|
|
|
|
if b, err := gparser.VarToJson(content); err != nil {
|
|
|
|
|
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 {
|
|
|
|
|
if b, err := gparser.VarToJson(content); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
} else {
|
2018-04-16 17:15:06 +08:00
|
|
|
|
//r.Header().Set("Content-Type", "application/json")
|
2018-04-16 14:33:25 +08:00
|
|
|
|
if callback := r.request.Get("callback"); callback != "" {
|
|
|
|
|
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-14 01:05:46 +08:00
|
|
|
|
// 返回XML
|
|
|
|
|
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-16 14:33:25 +08:00
|
|
|
|
// 允许AJAX跨域访问
|
|
|
|
|
func (r *Response) SetAllowCrossDomainRequest(allowOrigin string, allowMethods string, maxAge...int) {
|
|
|
|
|
age := 3628800
|
|
|
|
|
if len(maxAge) > 0 {
|
|
|
|
|
age = maxAge[0]
|
|
|
|
|
}
|
|
|
|
|
r.Header().Set("Access-Control-Allow-Origin", allowOrigin);
|
|
|
|
|
r.Header().Set("Access-Control-Allow-Methods", allowMethods);
|
|
|
|
|
r.Header().Set("Access-Control-Max-Age", strconv.Itoa(age));
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-14 01:05:46 +08:00
|
|
|
|
// 返回HTTP Code状态码
|
2018-05-04 14:35:20 +08:00
|
|
|
|
func (r *Response) WriteStatus(status int, content...string) {
|
|
|
|
|
if len(r.buffer) == 0 {
|
2018-05-04 16:07:50 +08:00
|
|
|
|
// 状态码注册回调函数处理
|
|
|
|
|
if status != http.StatusOK {
|
|
|
|
|
if f := r.request.Server.getStatusHandler(status, r.request); f != nil {
|
|
|
|
|
f(r.request)
|
2018-05-04 16:59:00 +08:00
|
|
|
|
// 如果是http.StatusOK那么表示回调函数内部没有设置header status,
|
|
|
|
|
// 那么这里就可以设置status,防止多次设置(http: multiple response.WriteHeader calls)
|
|
|
|
|
if r.Status == http.StatusOK {
|
|
|
|
|
r.WriteHeader(status)
|
|
|
|
|
}
|
2018-05-04 16:07:50 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-05-04 14:35:20 +08:00
|
|
|
|
r.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
|
|
|
|
r.Header().Set("X-Content-Type-Options", "nosniff")
|
|
|
|
|
if len(content) > 0 {
|
|
|
|
|
r.Write(content[0])
|
|
|
|
|
} else {
|
|
|
|
|
r.Write(http.StatusText(status))
|
|
|
|
|
}
|
2018-04-14 01:05:46 +08:00
|
|
|
|
}
|
2018-05-04 14:35:20 +08:00
|
|
|
|
r.WriteHeader(status)
|
2018-04-14 01:05:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 返回location标识,引导客户端跳转
|
|
|
|
|
func (r *Response) RedirectTo(location string) {
|
|
|
|
|
r.Header().Set("Location", location)
|
|
|
|
|
r.WriteHeader(http.StatusFound)
|
|
|
|
|
}
|
2017-12-07 14:57:16 +08:00
|
|
|
|
|
2017-12-26 13:31:07 +08:00
|
|
|
|
// 获取当前缓冲区中的数据
|
2018-01-02 16:35:13 +08:00
|
|
|
|
func (r *Response) Buffer() []byte {
|
2018-04-20 15:43:02 +08:00
|
|
|
|
r.mu.RLock()
|
|
|
|
|
defer r.mu.RUnlock()
|
2017-12-26 10:13:49 +08:00
|
|
|
|
return r.buffer
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-04 14:35:20 +08:00
|
|
|
|
// 获取当前缓冲区中的数据大小
|
|
|
|
|
func (r *Response) BufferLength() int {
|
|
|
|
|
r.mu.RLock()
|
|
|
|
|
defer r.mu.RUnlock()
|
|
|
|
|
return len(r.buffer)
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-14 01:05:46 +08:00
|
|
|
|
// 手动设置缓冲区内容
|
|
|
|
|
func (r *Response) SetBuffer(buffer []byte) {
|
2018-04-20 15:43:02 +08:00
|
|
|
|
r.mu.Lock()
|
2018-04-14 01:05:46 +08:00
|
|
|
|
r.buffer = buffer
|
2018-04-20 15:43:02 +08:00
|
|
|
|
r.mu.Unlock()
|
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() {
|
2018-04-20 15:43:02 +08:00
|
|
|
|
r.mu.Lock()
|
2017-12-28 15:21:25 +08:00
|
|
|
|
r.buffer = make([]byte, 0)
|
2018-04-20 15:43:02 +08:00
|
|
|
|
r.mu.Unlock()
|
2017-12-28 15:21:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-13 11:36:29 +08:00
|
|
|
|
// 输出缓冲区数据到客户端
|
2018-01-02 16:35:13 +08:00
|
|
|
|
func (r *Response) OutputBuffer() {
|
2017-12-26 13:31:07 +08:00
|
|
|
|
if len(r.buffer) > 0 {
|
2018-05-04 16:07:50 +08:00
|
|
|
|
r.mu.Lock()
|
2017-12-26 13:31:07 +08:00
|
|
|
|
r.ResponseWriter.Write(r.buffer)
|
|
|
|
|
r.buffer = make([]byte, 0)
|
2018-05-04 16:07:50 +08:00
|
|
|
|
r.mu.Unlock()
|
2017-12-26 13:31:07 +08:00
|
|
|
|
}
|
2018-05-04 16:07:50 +08:00
|
|
|
|
|
2017-12-13 11:36:29 +08:00
|
|
|
|
}
|