1
0
mirror of https://github.com/gofiber/fiber.git synced 2025-02-24 16:44:15 +00:00
fiber/request.go

398 lines
9.4 KiB
Go
Raw Normal View History

// 🔌 Fiber is an Express.js inspired web framework build on 🚀 Fasthttp.
2020-01-22 05:42:37 +01:00
// 📌 Please open an issue if you got suggestions or found a bug!
2020-01-20 04:33:55 +01:00
// 🖥 https://github.com/gofiber/fiber
// 🦸 Not all heroes wear capes, thank you to some amazing people
// 💖 @valyala, @dgrr, @erikdubbelboer, @savsgio, @julienschmidt
package fiber
import (
"encoding/base64"
"fmt"
2020-02-01 22:38:22 +03:00
"log"
2020-01-20 04:33:55 +01:00
"mime"
"mime/multipart"
"strings"
2020-01-21 08:58:11 +01:00
"github.com/valyala/fasthttp"
2020-01-20 04:33:55 +01:00
)
// Accepts : https://gofiber.github.io/fiber/#/context?id=accepts
2020-01-21 10:46:39 +01:00
func (ctx *Ctx) Accepts(offers ...string) string {
if len(offers) == 0 {
panic("You must provide atleast one content type string")
2020-01-20 04:33:55 +01:00
}
2020-01-30 23:17:25 -05:00
h := ctx.Get(fasthttp.HeaderAccept)
2020-01-21 08:58:11 +01:00
if h == "" {
2020-01-21 10:46:39 +01:00
return offers[0]
2020-01-21 08:58:11 +01:00
}
2020-01-21 10:46:39 +01:00
specs := strings.Split(h, ",")
for _, offer := range offers {
2020-01-30 23:17:25 -05:00
mimetype := getType(offer)
// if mimetype != "" {
// mimetype = strings.Split(mimetype, ";")[0]
// } else {
// mimetype = offer
// }
2020-01-21 10:46:39 +01:00
for _, spec := range specs {
spec = strings.TrimSpace(spec)
if strings.HasPrefix(spec, "*/*") {
return offer
}
2020-01-21 10:46:39 +01:00
if strings.HasPrefix(spec, mimetype) {
return offer
}
2020-01-21 10:46:39 +01:00
if strings.Contains(spec, "/*") {
if strings.HasPrefix(spec, strings.Split(mimetype, "/")[0]) {
return offer
}
}
2020-01-21 08:58:11 +01:00
}
}
return ""
2020-01-20 04:33:55 +01:00
}
// AcceptsCharsets : https://gofiber.github.io/fiber/#/context?id=acceptscharsets
2020-01-21 10:46:39 +01:00
func (ctx *Ctx) AcceptsCharsets(offers ...string) string {
if len(offers) == 0 {
panic("You must provide atleast one content type string")
2020-01-20 04:33:55 +01:00
}
2020-01-30 23:17:25 -05:00
h := ctx.Get(fasthttp.HeaderAcceptCharset)
2020-01-21 10:46:39 +01:00
if h == "" {
return offers[0]
}
2020-01-21 10:46:39 +01:00
specs := strings.Split(h, ",")
for _, offer := range offers {
for _, spec := range specs {
spec = strings.TrimSpace(spec)
if strings.HasPrefix(spec, "*") {
return offer
}
if strings.HasPrefix(spec, offer) {
return offer
}
}
}
return ""
2020-01-20 04:33:55 +01:00
}
// AcceptsEncodings : https://gofiber.github.io/fiber/#/context?id=acceptsencodings
2020-01-21 10:46:39 +01:00
func (ctx *Ctx) AcceptsEncodings(offers ...string) string {
if len(offers) == 0 {
panic("You must provide atleast one content type string")
2020-01-20 04:33:55 +01:00
}
2020-01-30 23:17:25 -05:00
h := ctx.Get(fasthttp.HeaderAcceptEncoding)
2020-01-21 10:46:39 +01:00
if h == "" {
return offers[0]
}
2020-01-21 10:46:39 +01:00
specs := strings.Split(h, ",")
for _, offer := range offers {
for _, spec := range specs {
spec = strings.TrimSpace(spec)
if strings.HasPrefix(spec, "*") {
return offer
}
if strings.HasPrefix(spec, offer) {
return offer
}
}
}
return ""
2020-01-20 04:33:55 +01:00
}
// AcceptsLanguages : https://gofiber.github.io/fiber/#/context?id=acceptslanguages
2020-01-21 10:46:39 +01:00
func (ctx *Ctx) AcceptsLanguages(offers ...string) string {
if len(offers) == 0 {
panic("You must provide atleast one content type string")
2020-01-20 04:33:55 +01:00
}
2020-01-30 23:17:25 -05:00
h := ctx.Get(fasthttp.HeaderAcceptLanguage)
2020-01-21 10:46:39 +01:00
if h == "" {
return offers[0]
}
2020-01-21 10:46:39 +01:00
specs := strings.Split(h, ",")
for _, offer := range offers {
for _, spec := range specs {
spec = strings.TrimSpace(spec)
if strings.HasPrefix(spec, "*") {
return offer
}
if strings.HasPrefix(spec, offer) {
return offer
}
}
}
return ""
2020-01-20 04:33:55 +01:00
}
// BaseURL : https://gofiber.github.io/fiber/#/context?id=baseurl
func (ctx *Ctx) BaseURL() string {
2020-01-20 04:33:55 +01:00
return ctx.Protocol() + "://" + ctx.Hostname()
}
// BasicAuth : https://gofiber.github.io/fiber/#/context?id=basicauth
func (ctx *Ctx) BasicAuth() (user, pass string, ok bool) {
2020-01-30 23:17:25 -05:00
auth := ctx.Get(fasthttp.HeaderAuthorization)
2020-01-20 04:33:55 +01:00
if auth == "" {
return
}
2020-01-20 04:33:55 +01:00
const prefix = "Basic "
2020-01-20 04:33:55 +01:00
// Case insensitive prefix match.
if len(auth) < len(prefix) || !strings.EqualFold(auth[:len(prefix)], prefix) {
return
}
2020-01-20 04:33:55 +01:00
c, err := base64.StdEncoding.DecodeString(auth[len(prefix):])
if err != nil {
return
}
2020-01-30 23:17:25 -05:00
cs := getString(c)
2020-01-20 04:33:55 +01:00
s := strings.IndexByte(cs, ':')
if s < 0 {
return
}
2020-01-20 04:33:55 +01:00
return cs[:s], cs[s+1:], true
}
// Body : https://gofiber.github.io/fiber/#/context?id=body
func (ctx *Ctx) Body(args ...interface{}) string {
if len(args) == 0 {
2020-01-30 23:17:25 -05:00
return getString(ctx.Fasthttp.Request.Body())
2020-01-20 04:33:55 +01:00
}
2020-01-20 04:33:55 +01:00
if len(args) == 1 {
switch arg := args[0].(type) {
case string:
2020-01-30 23:17:25 -05:00
return getString(ctx.Fasthttp.Request.PostArgs().Peek(arg))
2020-01-20 04:33:55 +01:00
case func(string, string):
ctx.Fasthttp.Request.PostArgs().VisitAll(func(k []byte, v []byte) {
2020-01-30 23:17:25 -05:00
arg(getString(k), getString(v))
2020-01-20 04:33:55 +01:00
})
default:
2020-01-30 23:17:25 -05:00
return getString(ctx.Fasthttp.Request.Body())
2020-01-20 04:33:55 +01:00
}
}
return ""
}
// Cookies : https://gofiber.github.io/fiber/#/context?id=cookies
func (ctx *Ctx) Cookies(args ...interface{}) string {
if len(args) == 0 {
2020-01-30 23:17:25 -05:00
return ctx.Get(fasthttp.HeaderCookie)
2020-01-20 04:33:55 +01:00
}
2020-01-20 04:33:55 +01:00
switch arg := args[0].(type) {
case string:
2020-01-30 23:17:25 -05:00
return getString(ctx.Fasthttp.Request.Header.Cookie(arg))
case []byte:
return getString(ctx.Fasthttp.Request.Header.CookieBytes(arg))
2020-01-20 04:33:55 +01:00
case func(string, string):
ctx.Fasthttp.Request.Header.VisitAllCookie(func(k, v []byte) {
2020-01-30 23:17:25 -05:00
arg(getString(k), getString(v))
2020-01-20 04:33:55 +01:00
})
default:
2020-01-30 23:17:25 -05:00
return ctx.Get(fasthttp.HeaderCookie)
2020-01-20 04:33:55 +01:00
}
2020-01-20 04:33:55 +01:00
return ""
}
// FormFile : https://gofiber.github.io/fiber/#/context?id=formfile
func (ctx *Ctx) FormFile(key string) (*multipart.FileHeader, error) {
return ctx.Fasthttp.FormFile(key)
}
// FormValue : https://gofiber.github.io/fiber/#/context?id=formvalue
func (ctx *Ctx) FormValue(key string) string {
2020-01-30 23:17:25 -05:00
return getString(ctx.Fasthttp.FormValue(key))
2020-01-20 04:33:55 +01:00
}
// Fresh : https://gofiber.github.io/fiber/#/context?id=fresh
func (ctx *Ctx) Fresh() bool {
return true
}
// Get : https://gofiber.github.io/fiber/#/context?id=get
func (ctx *Ctx) Get(key string) string {
// https://en.wikipedia.org/wiki/HTTP_referer
if key == "referrer" {
key = "referer"
}
2020-01-30 23:17:25 -05:00
return getString(ctx.Fasthttp.Request.Header.Peek(key))
2020-01-20 04:33:55 +01:00
}
// Hostname : https://gofiber.github.io/fiber/#/context?id=hostname
func (ctx *Ctx) Hostname() string {
2020-01-30 23:17:25 -05:00
return getString(ctx.Fasthttp.URI().Host())
2020-01-20 04:33:55 +01:00
}
// Ip : DEPRECATED
func (ctx *Ctx) Ip() string { // NOLINT
fmt.Println("Fiber deprecated c.Ip(): Use c.IP() instead")
return ctx.IP()
}
// IP : https://gofiber.github.io/fiber/#/context?id=Ip
func (ctx *Ctx) IP() string {
2020-01-20 04:33:55 +01:00
return ctx.Fasthttp.RemoteIP().String()
}
// Ips : DEPRECATED
func (ctx *Ctx) Ips() []string { // NOLINT
fmt.Println("Fiber deprecated c.Ips(): Use c.IPs() instead")
return ctx.IPs()
}
// IPs : https://gofiber.github.io/fiber/#/context?id=ips
func (ctx *Ctx) IPs() []string {
2020-01-30 23:17:25 -05:00
ips := strings.Split(ctx.Get(fasthttp.HeaderXForwardedFor), ",")
2020-01-20 04:33:55 +01:00
for i := range ips {
ips[i] = strings.TrimSpace(ips[i])
}
return ips
}
// Is : https://gofiber.github.io/fiber/#/context?id=is
func (ctx *Ctx) Is(ext string) bool {
if ext[0] != '.' {
ext = "." + ext
}
2020-01-30 23:17:25 -05:00
exts, _ := mime.ExtensionsByType(ctx.Get(fasthttp.HeaderContentType))
2020-01-20 04:33:55 +01:00
if len(exts) > 0 {
for _, item := range exts {
if item == ext {
return true
}
}
}
return false
}
// Locals : https://gofiber.github.io/fiber/#/context?id=locals
func (ctx *Ctx) Locals(key string, val ...interface{}) interface{} {
if len(val) == 0 {
return ctx.Fasthttp.UserValue(key)
}
ctx.Fasthttp.SetUserValue(key, val[0])
2020-01-20 04:33:55 +01:00
return nil
}
// Method : https://gofiber.github.io/fiber/#/context?id=method
func (ctx *Ctx) Method() string {
2020-01-30 23:17:25 -05:00
return getString(ctx.Fasthttp.Request.Header.Method())
2020-01-20 04:33:55 +01:00
}
// MultipartForm : https://gofiber.github.io/fiber/#/context?id=multipartform
func (ctx *Ctx) MultipartForm() (*multipart.Form, error) {
return ctx.Fasthttp.MultipartForm()
}
// OriginalUrl : DEPRECATED
func (ctx *Ctx) OriginalUrl() string {
fmt.Println("Fiber deprecated c.OriginalUrl(): Use c.OriginalURL() instead")
return ctx.OriginalURL()
}
// OriginalURL : https://gofiber.github.io/fiber/#/context?id=originalurl
func (ctx *Ctx) OriginalURL() string {
2020-01-30 23:17:25 -05:00
return getString(ctx.Fasthttp.Request.Header.RequestURI())
2020-01-20 04:33:55 +01:00
}
// Params : https://gofiber.github.io/fiber/#/context?id=params
func (ctx *Ctx) Params(key string) string {
if ctx.params == nil {
return ""
}
2020-01-20 04:33:55 +01:00
for i := 0; i < len(*ctx.params); i++ {
if (*ctx.params)[i] == key {
return ctx.values[i]
}
}
return ""
}
// Path : https://gofiber.github.io/fiber/#/context?id=path
func (ctx *Ctx) Path() string {
2020-01-30 23:17:25 -05:00
return getString(ctx.Fasthttp.URI().Path())
2020-01-20 04:33:55 +01:00
}
// Protocol : https://gofiber.github.io/fiber/#/context?id=protocol
func (ctx *Ctx) Protocol() string {
if ctx.Fasthttp.IsTLS() {
return "https"
}
return "http"
}
// Query : https://gofiber.github.io/fiber/#/context?id=query
func (ctx *Ctx) Query(key string) string {
2020-01-30 23:17:25 -05:00
return getString(ctx.Fasthttp.QueryArgs().Peek(key))
2020-01-20 04:33:55 +01:00
}
// Range : https://gofiber.github.io/fiber/#/context?id=range
func (ctx *Ctx) Range() {
}
// Route : https://gofiber.github.io/fiber/#/context?id=route
func (ctx *Ctx) Route() *Route {
2020-01-28 14:28:09 -05:00
return ctx.route
2020-01-20 04:33:55 +01:00
}
2020-01-21 08:58:11 +01:00
// SaveFile : https://gofiber.github.io/fiber/#/context?id=secure
func (ctx *Ctx) SaveFile(fh *multipart.FileHeader, path string) {
2020-02-01 22:38:22 +03:00
if err := fasthttp.SaveMultipartFile(fh, path); err != nil {
log.Fatal(err)
}
2020-01-21 08:58:11 +01:00
}
2020-01-20 04:33:55 +01:00
// Secure : https://gofiber.github.io/fiber/#/context?id=secure
func (ctx *Ctx) Secure() bool {
return ctx.Fasthttp.IsTLS()
}
// SignedCookies : https://gofiber.github.io/fiber/#/context?id=signedcookies
func (ctx *Ctx) SignedCookies() {
}
// Stale : https://gofiber.github.io/fiber/#/context?id=stale
func (ctx *Ctx) Stale() bool {
return true
}
// Subdomains : https://gofiber.github.io/fiber/#/context?id=subdomains
func (ctx *Ctx) Subdomains() (subs []string) {
subs = strings.Split(ctx.Hostname(), ".")
subs = subs[:len(subs)-2]
return subs
}
// Xhr : DEPRECATED
func (ctx *Ctx) Xhr() bool {
fmt.Println("Fiber deprecated c.Xhr(): Use c.XHR() instead")
return ctx.XHR()
}
// XHR : https://gofiber.github.io/fiber/#/context?id=xhr
func (ctx *Ctx) XHR() bool {
2020-01-20 04:33:55 +01:00
return ctx.Get("X-Requested-With") == "XMLHttpRequest"
}