mirror of
https://github.com/gofiber/fiber.git
synced 2025-02-22 06:12:55 +00:00
512 lines
9.6 KiB
Go
512 lines
9.6 KiB
Go
package fiber
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"mime"
|
|
"mime/multipart"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync"
|
|
|
|
jsoniter "github.com/json-iterator/go"
|
|
"github.com/valyala/fasthttp"
|
|
)
|
|
|
|
// Ctx struct
|
|
type Ctx struct {
|
|
noCopy noCopy
|
|
next bool
|
|
params *[]string
|
|
values []string
|
|
Fasthttp *fasthttp.RequestCtx
|
|
}
|
|
|
|
// Ctx pool
|
|
var ctxPool = sync.Pool{
|
|
New: func() interface{} {
|
|
return new(Ctx)
|
|
},
|
|
}
|
|
|
|
// Get new Ctx from pool
|
|
func acquireCtx(fctx *fasthttp.RequestCtx) *Ctx {
|
|
ctx := ctxPool.Get().(*Ctx)
|
|
ctx.Fasthttp = fctx
|
|
return ctx
|
|
}
|
|
|
|
// Return Context to pool
|
|
func releaseCtx(ctx *Ctx) {
|
|
ctx.next = false
|
|
ctx.params = nil
|
|
ctx.values = nil
|
|
ctx.Fasthttp = nil
|
|
ctxPool.Put(ctx)
|
|
}
|
|
|
|
// Accepts :
|
|
func (ctx *Ctx) Accepts(ext string) bool {
|
|
accept := ctx.Get("Accept")
|
|
if ext[0] != '.' {
|
|
ext = "." + ext
|
|
}
|
|
// Accept: text/*, application/json
|
|
// n = text/html => no match
|
|
m := mime.TypeByExtension(ext)
|
|
if strings.Contains(accept, m) {
|
|
return true
|
|
}
|
|
// Accept: text/*, application/json
|
|
// n = text/* => match
|
|
m = strings.Split(m, "/")[0]
|
|
if strings.Contains(accept, m+"/*") {
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
// AcceptsCharsets TODO
|
|
func (ctx *Ctx) AcceptsCharsets() {
|
|
|
|
}
|
|
|
|
// AcceptsCharsets TODO
|
|
func (ctx *Ctx) AcceptsEncodings() {
|
|
|
|
}
|
|
|
|
// AcceptsCharsets TODO
|
|
func (ctx *Ctx) AcceptsLanguages() {
|
|
|
|
}
|
|
|
|
// Append :
|
|
func (ctx *Ctx) Append(field, val string) {
|
|
prev := ctx.Get(field)
|
|
value := val
|
|
if prev != "" {
|
|
value = prev + "; " + val
|
|
}
|
|
ctx.Set(field, value)
|
|
}
|
|
|
|
// 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")
|
|
}
|
|
|
|
// BaseUrl TODO
|
|
func (ctx *Ctx) BaseUrl() {
|
|
|
|
}
|
|
|
|
// BasicAuth :
|
|
func (ctx *Ctx) BasicAuth() (user, pass string, ok bool) {
|
|
auth := ctx.Get("Authorization")
|
|
if auth == "" {
|
|
return
|
|
}
|
|
const prefix = "Basic "
|
|
// Case insensitive prefix match.
|
|
if len(auth) < len(prefix) || !strings.EqualFold(auth[:len(prefix)], prefix) {
|
|
return
|
|
}
|
|
c, err := base64.StdEncoding.DecodeString(auth[len(prefix):])
|
|
if err != nil {
|
|
return
|
|
}
|
|
cs := b2s(c)
|
|
s := strings.IndexByte(cs, ':')
|
|
if s < 0 {
|
|
return
|
|
}
|
|
return cs[:s], cs[s+1:], true
|
|
}
|
|
|
|
// Body :
|
|
func (ctx *Ctx) Body(args ...interface{}) string {
|
|
if len(args) == 0 {
|
|
return b2s(ctx.Fasthttp.Request.Body())
|
|
}
|
|
if len(args) == 1 {
|
|
switch arg := args[0].(type) {
|
|
case string:
|
|
return b2s(ctx.Fasthttp.Request.PostArgs().Peek(arg))
|
|
case func(string, string):
|
|
ctx.Fasthttp.Request.PostArgs().VisitAll(func(k []byte, v []byte) {
|
|
arg(b2s(k), b2s(v))
|
|
})
|
|
default:
|
|
return b2s(ctx.Fasthttp.Request.Body())
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// ClearCookie :
|
|
func (ctx *Ctx) ClearCookie(name ...string) {
|
|
if len(name) == 0 {
|
|
ctx.Fasthttp.Request.Header.VisitAllCookie(func(k, v []byte) {
|
|
ctx.Fasthttp.Response.Header.DelClientCookie(b2s(k))
|
|
})
|
|
}
|
|
if len(name) > 0 {
|
|
ctx.Fasthttp.Response.Header.DelClientCookie(name[0])
|
|
}
|
|
}
|
|
|
|
// Cookie :
|
|
func (ctx *Ctx) Cookie(name, value string, options ...interface{}) {
|
|
cook := &fasthttp.Cookie{}
|
|
if len(options) > 0 {
|
|
// options
|
|
}
|
|
cook.SetKey(name)
|
|
cook.SetValue(value)
|
|
ctx.Fasthttp.Response.Header.SetCookie(cook)
|
|
}
|
|
|
|
// Cookies :
|
|
func (ctx *Ctx) Cookies(args ...interface{}) string {
|
|
if len(args) == 0 {
|
|
return ctx.Get("Cookie")
|
|
}
|
|
switch arg := args[0].(type) {
|
|
case string:
|
|
return b2s(ctx.Fasthttp.Request.Header.Cookie(arg))
|
|
case func(string, string):
|
|
ctx.Fasthttp.Request.Header.VisitAllCookie(func(k, v []byte) {
|
|
arg(b2s(k), b2s(v))
|
|
})
|
|
default:
|
|
panic("Argument must be a string or func(string, string)")
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// 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 TODO
|
|
func (ctx *Ctx) End() {
|
|
|
|
}
|
|
|
|
// Format TODO
|
|
func (ctx *Ctx) Format() {
|
|
|
|
}
|
|
|
|
// FormFile :
|
|
func (ctx *Ctx) FormFile(key string) (*multipart.FileHeader, error) {
|
|
return ctx.Fasthttp.FormFile(key)
|
|
}
|
|
|
|
// FormValue :
|
|
func (ctx *Ctx) FormValue(key string) string {
|
|
return b2s(ctx.Fasthttp.FormValue(key))
|
|
}
|
|
|
|
// Fresh TODO https://expressjs.com/en/4x/api.html#req.fresh
|
|
func (ctx *Ctx) Fresh() bool {
|
|
return true
|
|
}
|
|
|
|
// Get :
|
|
func (ctx *Ctx) Get(key string) string {
|
|
// https://en.wikipedia.org/wiki/HTTP_referer
|
|
if key == "referrer" {
|
|
key = "referer"
|
|
}
|
|
return b2s(ctx.Fasthttp.Request.Header.Peek(key))
|
|
}
|
|
|
|
// HeadersSent TODO
|
|
func (ctx *Ctx) HeadersSent() {
|
|
|
|
}
|
|
|
|
// Hostname :
|
|
func (ctx *Ctx) Hostname() string {
|
|
return b2s(ctx.Fasthttp.URI().Host())
|
|
}
|
|
|
|
// Ip :
|
|
func (ctx *Ctx) Ip() string {
|
|
return ctx.Fasthttp.RemoteIP().String()
|
|
}
|
|
|
|
// Ips https://expressjs.com/en/4x/api.html#req.ips
|
|
func (ctx *Ctx) Ips() []string {
|
|
ips := strings.Split(ctx.Get("X-Forwarded-For"), ",")
|
|
for i := range ips {
|
|
ips[i] = strings.TrimSpace(ips[i])
|
|
}
|
|
return ips
|
|
}
|
|
|
|
// Is :
|
|
func (ctx *Ctx) Is(ext string) bool {
|
|
if ext[0] != '.' {
|
|
ext = "." + ext
|
|
}
|
|
exts, _ := mime.ExtensionsByType(ctx.Get("Content-Type"))
|
|
if len(exts) > 0 {
|
|
for _, item := range exts {
|
|
if item == ext {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// 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 :
|
|
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(");"))
|
|
|
|
// Create buffer with length of json + cbname + ( );
|
|
// buf := make([]byte, len(raw)+len(cbName)+3)
|
|
//
|
|
// count := 0
|
|
// count += copy(buf[count:], cbName)
|
|
// count += copy(buf[count:], "(")
|
|
// count += copy(buf[count:], raw)
|
|
// count += copy(buf[count:], ");")
|
|
|
|
ctx.Set("X-Content-Type-Options", "nosniff")
|
|
ctx.Set("Content-Type", "application/javascript")
|
|
ctx.Fasthttp.Response.SetBodyString(builder.String())
|
|
return nil
|
|
}
|
|
|
|
// Links TODO
|
|
func (ctx *Ctx) Links() {
|
|
|
|
}
|
|
|
|
// Locals TODO
|
|
func (ctx *Ctx) Locals() {
|
|
|
|
}
|
|
|
|
// Location :
|
|
func (ctx *Ctx) Location(path string) {
|
|
ctx.Set("Location", path)
|
|
}
|
|
|
|
// Method :
|
|
func (ctx *Ctx) Method() string {
|
|
return b2s(ctx.Fasthttp.Request.Header.Method())
|
|
}
|
|
|
|
// MultipartForm :
|
|
func (ctx *Ctx) MultipartForm() (*multipart.Form, error) {
|
|
return ctx.Fasthttp.MultipartForm()
|
|
}
|
|
|
|
// Next :
|
|
func (ctx *Ctx) Next() {
|
|
ctx.next = true
|
|
ctx.params = nil
|
|
ctx.values = nil
|
|
}
|
|
|
|
// OriginalUrl :
|
|
func (ctx *Ctx) OriginalUrl() string {
|
|
return b2s(ctx.Fasthttp.Request.Header.RequestURI())
|
|
}
|
|
|
|
// Params :
|
|
func (ctx *Ctx) Params(key string) string {
|
|
if ctx.params == nil {
|
|
return ""
|
|
}
|
|
for i := 0; i < len(*ctx.params); i++ {
|
|
if (*ctx.params)[i] == key {
|
|
return ctx.values[i]
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// Path :
|
|
func (ctx *Ctx) Path() string {
|
|
return b2s(ctx.Fasthttp.URI().Path())
|
|
}
|
|
|
|
// Protocol :
|
|
func (ctx *Ctx) Protocol() string {
|
|
if ctx.Fasthttp.IsTLS() {
|
|
return "https"
|
|
}
|
|
return "http"
|
|
}
|
|
|
|
// Query :
|
|
func (ctx *Ctx) Query(key string) string {
|
|
return b2s(ctx.Fasthttp.QueryArgs().Peek(key))
|
|
}
|
|
|
|
// Range TODO
|
|
func (ctx *Ctx) Range() {
|
|
|
|
}
|
|
|
|
// 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 TODO https://expressjs.com/en/4x/api.html#res.render
|
|
func (ctx *Ctx) Render() {
|
|
|
|
}
|
|
|
|
// Route TODO https://expressjs.com/en/4x/api.html#res.render
|
|
func (ctx *Ctx) Route() {
|
|
|
|
}
|
|
|
|
// Secure :
|
|
func (ctx *Ctx) Secure() bool {
|
|
return ctx.Fasthttp.IsTLS()
|
|
}
|
|
|
|
// 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")
|
|
}
|
|
}
|
|
|
|
// SendFile :
|
|
func (ctx *Ctx) SendFile(file string) {
|
|
// https://github.com/valyala/fasthttp/blob/master/fs.go#L81
|
|
fasthttp.ServeFile(ctx.Fasthttp, file)
|
|
//ctx.Type(filepath.Ext(path))
|
|
//ctx.Fasthttp.SendFile(path)
|
|
}
|
|
|
|
// SendStatus :
|
|
func (ctx *Ctx) SendStatus() {
|
|
|
|
}
|
|
|
|
// Set :
|
|
func (ctx *Ctx) Set(key string, val string) {
|
|
ctx.Fasthttp.Response.Header.SetCanonical(s2b(key), s2b(val))
|
|
}
|
|
|
|
// SignedCookies TODO
|
|
func (ctx *Ctx) SignedCookies() {
|
|
|
|
}
|
|
|
|
// Stale TODO https://expressjs.com/en/4x/api.html#req.fresh
|
|
func (ctx *Ctx) Stale() bool {
|
|
return true
|
|
}
|
|
|
|
// Status :
|
|
func (ctx *Ctx) Status(status int) *Ctx {
|
|
ctx.Fasthttp.Response.SetStatusCode(status)
|
|
return ctx
|
|
}
|
|
|
|
// Subdomains :
|
|
func (ctx *Ctx) Subdomains() (subs []string) {
|
|
subs = strings.Split(ctx.Hostname(), ".")
|
|
subs = subs[:len(subs)-2]
|
|
return subs
|
|
}
|
|
|
|
// 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 TODO
|
|
func (ctx *Ctx) Vary() {
|
|
|
|
}
|
|
|
|
// 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")
|
|
}
|
|
}
|
|
|
|
// Xhr :
|
|
func (ctx *Ctx) Xhr() bool {
|
|
return ctx.Get("X-Requested-With") == "XMLHttpRequest"
|
|
}
|