当前仓库属于暂停状态,部分功能使用受限,详情请查阅 仓库状态说明
1 Star 0 Fork 0

Simbory/mego
暂停

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
result.go 3.35 KB
一键复制 编辑 原始数据 按行查看 历史
simbory 提交于 2020-07-03 15:39 . 弃用net/http,采用fasthttp
package mego
import (
"bufio"
"bytes"
"io"
"strings"
"github.com/valyala/fasthttp"
)
// Result the request result interface
type Result interface {
Execute(w *fasthttp.Response, r *fasthttp.Request)
}
// BufResult the buffered result
type BufResult struct {
writer *bufio.Writer
compression bool
ContentType string
Encoding string
headers map[string]string
StatusCode int
}
func (b *BufResult) makeBuf() {
if b.writer == nil {
b.writer = bufio.NewWriter(bytes.NewBuffer(nil))
}
}
// Write write byte array p to the result buffer
func (b *BufResult) Write(p []byte) (n int, err error) {
b.makeBuf()
return b.writer.Write(p)
}
// WriteByte write byte c to the result buffer
func (b *BufResult) WriteByte(c byte) error {
b.makeBuf()
return b.writer.WriteByte(c)
}
// WriteString write string s to the result buffer
func (b *BufResult) WriteString(s string) (n int, err error) {
b.makeBuf()
return b.writer.WriteString(s)
}
// WriteRune write rune r to the result buffer
func (b *BufResult) WriteRune(r rune) (n int, err error) {
b.makeBuf()
return b.writer.WriteRune(r)
}
// ReadFrom read the data from reader r and write to the result buffer
func (b *BufResult) ReadFrom(r io.Reader) (n int64, err error) {
b.makeBuf()
return b.writer.ReadFrom(r)
}
// AddHeader write http header to the result
func (b *BufResult) AddHeader(key, value string) {
notEmpty("key", key)
notEmpty("value", value)
if strings.EqualFold(key, "content-type") {
b.ContentType = value
return
}
if b.headers == nil {
b.headers = make(map[string]string)
}
b.headers[key] = value
}
// Execute write the data in the result buffer to the response original
func (b *BufResult) Execute(w *fasthttp.Response, r *fasthttp.Request) {
if len(b.headers) > 0 {
for key, value := range b.headers {
w.Header.Set(key, value)
}
}
if len(b.Encoding) == 0 {
b.Encoding = "utf-8"
}
if len(b.ContentType) == 0 {
b.ContentType = "text/plain"
}
w.Header.Set("content-type", StrJoin(b.ContentType, "; charset=", b.Encoding))
if b.StatusCode <= 0 {
b.StatusCode = 200
}
w.SetStatusCode(b.StatusCode)
if b.writer == nil {
return
}
if b.compression && acceptGzip(r) {
panicErr(w.WriteGzip(b.writer))
} else if b.compression && acceptDeflate(r) {
panicErr(w.WriteDeflate(b.writer))
} else {
panicErr(w.Write(b.writer))
}
}
// NewBufResult create a new buffer result with default value buf
func NewBufResult(buf *bytes.Buffer, compression bool) *BufResult {
return &BufResult{writer: bufio.NewWriter(buf)}
}
// EmptyResult the empty buffer result
type EmptyResult struct{}
// Execute do nothing
func (er *EmptyResult) Execute(w *fasthttp.Response, r *fasthttp.Request) {
}
// FileResult the file result
type FileResult struct {
ContentType string
FilePath string
}
// Execute execute the result
func (fr *FileResult) Execute(w *fasthttp.Response, r *fasthttp.Request) {
if len(fr.ContentType) > 0 {
w.Header.Set("Content-Type", fr.ContentType)
}
panicErr(w.SendFile(fr.FilePath))
}
// RedirectResult the redirect result
type RedirectResult struct {
RedirectURL string
StatusCode int
}
// Execute execute the redirect result
func (rr *RedirectResult) Execute(w *fasthttp.Response, r *fasthttp.Request) {
var statusCode = 301
if rr.StatusCode != 301 {
statusCode = 302
}
w.SetStatusCode(statusCode)
w.Header.Set("Location", rr.RedirectURL)
panic(&endCtxSignal{})
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/simbory/mego.git
git@gitee.com:simbory/mego.git
simbory
mego
mego
master

搜索帮助