1
0
mirror of https://github.com/gofiber/fiber.git synced 2025-02-24 12:04:02 +00:00
fiber/response.go

306 lines
7.6 KiB
Go
Raw Normal View History

2020-01-20 04:33:55 +01:00
// 🚀 Fiber, Express on Steriods
// 📌 Don't use in production until version 1.0.0
// 🖥 https://github.com/gofiber/fiber
// 🦸 Not all heroes wear capes, thank you to some amazing people
// 💖 @valyala, @dgrr, @erikdubbelboer, @savsgio, @julienschmidt
package fiber
import (
"fmt"
"mime"
"path/filepath"
"strings"
"time"
jsoniter "github.com/json-iterator/go"
"github.com/valyala/fasthttp"
)
// Append : https://gofiber.github.io/fiber/#/context?id=append
func (ctx *Ctx) Append(field string, values ...string) {
newVal := ctx.Get(field)
if len(values) > 0 {
for i := range values {
newVal = newVal + ", " + values[i]
}
}
ctx.Set(field, newVal)
}
// Attachment : https://gofiber.github.io/fiber/#/context?id=attachment
func (ctx *Ctx) Attachment(name ...string) {
if len(name) > 0 {
filename := filepath.Base(name[0])
ctx.Type(filepath.Ext(filename))
ctx.Set("Content-Disposition", `attachment; filename="`+filename+`"`)
return
}
ctx.Set("Content-Disposition", "attachment")
}
// ClearCookie : https://gofiber.github.io/fiber/#/context?id=clearcookie
func (ctx *Ctx) ClearCookie(name ...string) {
if len(name) == 0 {
ctx.Fasthttp.Request.Header.VisitAllCookie(func(k, v []byte) {
fmt.Println(b2s(k), b2s(v))
ctx.Fasthttp.Response.Header.DelClientCookie(b2s(k))
})
} else if len(name) > 0 {
for i := range name {
ctx.Fasthttp.Response.Header.DelClientCookie(name[i])
}
}
}
// Cookie : https://gofiber.github.io/fiber/#/context?id=cookie
func (ctx *Ctx) Cookie(key, value string, options ...interface{}) {
cook := &fasthttp.Cookie{}
cook.SetKey(key)
cook.SetValue(value)
if len(options) > 0 {
switch opt := options[0].(type) {
case *Cookie:
if opt.Expire > 0 {
cook.SetExpire(time.Unix(int64(opt.Expire), 0))
}
if opt.MaxAge > 0 {
cook.SetMaxAge(opt.MaxAge)
}
if opt.Domain != "" {
cook.SetDomain(opt.Domain)
}
if opt.Path != "" {
cook.SetPath(opt.Path)
}
if opt.HttpOnly {
cook.SetHTTPOnly(opt.HttpOnly)
}
if opt.Secure {
cook.SetSecure(opt.Secure)
}
if opt.SameSite != "" {
sameSite := fasthttp.CookieSameSiteDisabled
if strings.EqualFold(opt.SameSite, "lax") {
sameSite = fasthttp.CookieSameSiteLaxMode
} else if strings.EqualFold(opt.SameSite, "strict") {
sameSite = fasthttp.CookieSameSiteStrictMode
} else if strings.EqualFold(opt.SameSite, "none") {
sameSite = fasthttp.CookieSameSiteNoneMode
} else {
sameSite = fasthttp.CookieSameSiteDefaultMode
}
cook.SetSameSite(sameSite)
}
default:
panic("Invalid cookie options")
}
}
ctx.Fasthttp.Response.Header.SetCookie(cook)
}
// Download : https://gofiber.github.io/fiber/#/context?id=download
func (ctx *Ctx) Download(file string, name ...string) {
filename := filepath.Base(file)
if len(name) > 0 {
filename = name[0]
}
ctx.Set("Content-Disposition", "attachment; filename="+filename)
ctx.SendFile(file)
}
// End : https://gofiber.github.io/fiber/#/context?id=end
func (ctx *Ctx) End() {
}
// Format : https://gofiber.github.io/fiber/#/context?id=format
func (ctx *Ctx) Format() {
}
// HeadersSent : https://gofiber.github.io/fiber/#/context?id=headerssent
func (ctx *Ctx) HeadersSent() {
}
// Json : https://gofiber.github.io/fiber/#/context?id=json
func (ctx *Ctx) Json(v interface{}) error {
raw, err := jsoniter.Marshal(&v)
if err != nil {
return err
}
ctx.Set("Content-Type", "application/json")
ctx.Fasthttp.Response.SetBodyString(b2s(raw))
return nil
}
// Jsonp : https://gofiber.github.io/fiber/#/context?id=jsonp
func (ctx *Ctx) Jsonp(v interface{}, cb ...string) error {
raw, err := jsoniter.Marshal(&v)
if err != nil {
return err
}
var builder strings.Builder
if len(cb) > 0 {
builder.Write(s2b(cb[0]))
} else {
builder.Write([]byte("callback"))
}
builder.Write([]byte("("))
builder.Write(raw)
builder.Write([]byte(");"))
ctx.Set("X-Content-Type-Options", "nosniff")
ctx.Set("Content-Type", "application/javascript")
ctx.Fasthttp.Response.SetBodyString(builder.String())
return nil
}
// Links : https://gofiber.github.io/fiber/#/context?id=links
func (ctx *Ctx) Links(link ...string) {
h := ""
for i, l := range link {
if i%2 == 0 {
h += "<" + l + ">"
} else {
h += `; rel="` + l + `",`
}
}
if len(link) > 0 {
h = strings.TrimSuffix(h, ",")
ctx.Set("Link", h)
}
}
// Location : https://gofiber.github.io/fiber/#/context?id=location
func (ctx *Ctx) Location(path string) {
ctx.Set("Location", path)
}
// Next : https://gofiber.github.io/fiber/#/context?id=next
func (ctx *Ctx) Next() {
ctx.next = true
ctx.params = nil
ctx.values = nil
}
// Redirect : https://gofiber.github.io/fiber/#/context?id=redirect
func (ctx *Ctx) Redirect(path string, status ...int) {
ctx.Set("Location", path)
if len(status) > 0 {
ctx.Status(status[0])
} else {
ctx.Status(302)
}
}
// Render : https://gofiber.github.io/fiber/#/context?id=render
func (ctx *Ctx) Render() {
}
// Send : https://gofiber.github.io/fiber/#/context?id=send
func (ctx *Ctx) Send(args ...interface{}) {
// https://github.com/valyala/fasthttp/blob/master/http.go#L490
if len(args) != 1 {
panic("To many arguments!")
}
switch body := args[0].(type) {
case string:
//ctx.Fasthttp.Response.SetBodyRaw(s2b(body))
ctx.Fasthttp.Response.SetBodyString(body)
case []byte:
//ctx.Fasthttp.Response.SetBodyRaw(body)
ctx.Fasthttp.Response.SetBodyString(b2s(body))
default:
panic("body must be a string or []byte")
}
}
// SendBytes : https://gofiber.github.io/fiber/#/context?id=sendbytes
func (ctx *Ctx) SendBytes(body []byte) {
ctx.Fasthttp.Response.SetBodyString(b2s(body))
}
// SendFile : https://gofiber.github.io/fiber/#/context?id=sendfile
func (ctx *Ctx) SendFile(file string, gzip ...bool) {
// Disable gzipping
if len(gzip) > 0 && !gzip[0] {
fasthttp.ServeFileUncompressed(ctx.Fasthttp, file)
}
fasthttp.ServeFile(ctx.Fasthttp, file)
// https://github.com/valyala/fasthttp/blob/master/fs.go#L81
//ctx.Type(filepath.Ext(path))
//ctx.Fasthttp.SendFile(path)
}
// SendStatus : https://gofiber.github.io/fiber/#/context?id=sendstatus
func (ctx *Ctx) SendStatus(status int) {
ctx.Status(status)
// Only set status body when there is no response body
if len(ctx.Fasthttp.Response.Body()) == 0 {
msg := statusMessages[status]
if msg != "" {
ctx.Fasthttp.Response.SetBodyString(msg)
}
}
}
// SendString : https://gofiber.github.io/fiber/#/context?id=sendstring
func (ctx *Ctx) SendString(body string) {
ctx.Fasthttp.Response.SetBodyString(body)
}
// Set : https://gofiber.github.io/fiber/#/context?id=set
func (ctx *Ctx) Set(key string, val string) {
ctx.Fasthttp.Response.Header.SetCanonical(s2b(key), s2b(val))
}
// Status : https://gofiber.github.io/fiber/#/context?id=status
func (ctx *Ctx) Status(status int) *Ctx {
ctx.Fasthttp.Response.SetStatusCode(status)
return ctx
}
// Type : https://gofiber.github.io/fiber/#/context?id=type
func (ctx *Ctx) Type(ext string) *Ctx {
if ext[0] != '.' {
ext = "." + ext
}
m := mime.TypeByExtension(ext)
ctx.Set("Content-Type", m)
return ctx
}
// Vary : https://gofiber.github.io/fiber/#/context?id=vary
func (ctx *Ctx) Vary(field ...string) {
vary := ctx.Get("Vary")
for _, f := range field {
if !strings.Contains(vary, f) {
vary += ", " + f
}
}
if len(field) > 0 {
ctx.Set("Vary", vary)
}
}
// Write : https://gofiber.github.io/fiber/#/context?id=write
func (ctx *Ctx) Write(args ...interface{}) {
if len(args) == 0 {
panic("Missing body")
}
switch body := args[0].(type) {
case string:
ctx.Fasthttp.Response.SetBodyString(body)
case []byte:
ctx.Fasthttp.Response.AppendBodyString(b2s(body))
default:
panic("body must be a string or []byte")
}
}