Update Accepts

pull/6/head
Fenny 2020-01-21 10:46:39 +01:00
parent cbdfbe909c
commit 52712b3e4a
2 changed files with 114 additions and 73 deletions

View File

@ -2,26 +2,31 @@
The ctx object represents the HTTP request and response and has methods for the request query string, parameters, body, HTTP headers, and so on. In this documentation and by convention, the context is always referred to as c but its actual name is determined by the parameters to the callback function in which youre working.
#### Accepts
Checks if the specified content types are acceptable, based on the requests Accept HTTP header field.
Checks if the specified content types are acceptable, based on the requests Accept HTTP header field. You can use an extention or content-type format
```go
// Function signature
c.Accepts(ext string) bool
c.Accepts(types ...string) string
// Example
app.Get("/", func(c *fiber.Ctx) {
// Accept: text/html
c.Accepts("html")
// => true
// => "html"
// Accept: text/*, application/json
c.Accepts("text")
// => // => true
c.Accepts("html")
// => true
c.Accepts("text/html")
//=> "text/html"
c.Accepts("json", "text")
// => "json"
c.Accepts("application/json")
// => "application/json"
c.Accepts("json")
// => // => true
c.Accepts("☕")
// => // => false
// Accept: text/*, application/json
c.Accepts("image/png"
c.Accepts("png")
// => ""
})
```
@ -29,19 +34,19 @@ app.Get("/", func(c *fiber.Ctx) {
Returns the first accepted charset of the specified character sets, based on the requests Accept-Charset HTTP header field
```go
// Function signature
c.AcceptsCharsets(charset string) bool
c.AcceptsCharsets(charsets ...string) string
// Example
app.Get("/", func(c *fiber.Ctx) {
// Accept-Charset: utf-8, iso-8859-1;q=0.2, utf-7;q=0.5
c.AcceptsCharsets("utf-8")
// => true
// => "utf-8"
c.AcceptsCharsets("iso-8859-1")
// => true
c.AcceptsCharsets("utf-16", "iso-8859-1")
// => "iso-8859-1"
c.AcceptsCharsets("utf-16")
// => false
// => ""
})
```
@ -50,19 +55,19 @@ app.Get("/", func(c *fiber.Ctx) {
Returns the first accepted encoding of the specified encodings, based on the requests Accept-Encoding HTTP header field.
```go
// Function signature
c.AcceptsEncodings(charset string) bool
c.AcceptsEncodings(encodings ...string) string
// Example
app.Get("/", func(c *fiber.Ctx) {
// Accept-Encoding: gzip, compress;q=0.2
c.AcceptsEncodings("gzip")
// => true
// => "gzip"
c.AcceptsEncodings("compress")
// => true
c.AcceptsEncodings("compress", "br")
// => "compress"
c.AcceptsEncodings("deflate")
// => false
// => ""
})
```
@ -70,19 +75,19 @@ app.Get("/", func(c *fiber.Ctx) {
Returns the first accepted language of the specified languages, based on the requests Accept-Language HTTP header field.
```go
// Function signature
c.AcceptsLanguages(charset string) bool
c.AcceptsLanguages(languages ...string) string
// Example
app.Get("/", func(c *fiber.Ctx) {
// Accept-Language: en;q=0.8, es, pt
c.AcceptsLanguages("en")
// => true
// => "en"
c.AcceptsLanguages("pt")
// => true
c.AcceptsLanguages("pt", "nl")
// => "pt"
c.AcceptsLanguages("fr")
// => false
// => ""
})
```

View File

@ -18,74 +18,110 @@ import (
)
// Accepts : https://gofiber.github.io/fiber/#/context?id=accepts
func (ctx *Ctx) Accepts(types ...string) string {
// No types given, return ""
if len(types) == 0 {
return ""
func (ctx *Ctx) Accepts(offers ...string) string {
if len(offers) == 0 {
panic("You must provide atleast one content type string")
}
// Get accept header
h := ctx.Get("Accept")
if h == "" {
return types[0]
return offers[0]
}
for _, typ := range types {
// match any, return first type
if strings.Contains(h, "*/*") {
return typ
specs := strings.Split(h, ",")
for _, offer := range offers {
mimetype := mime.TypeByExtension("." + offer)
if mimetype != "" {
mimetype = strings.Split(mimetype, ";")[0]
} else {
mimetype = offer
}
// convert typ to mime
if typ[0] != '.' {
typ = "." + typ
}
m := strings.Split(mime.TypeByExtension(typ), ";")[0]
// if header contains mime, return typ
if strings.Contains(h, m) {
return typ
}
// if header contains type/*
if strings.Contains(h, strings.Split(m, "/")[0]+"/*") {
return typ
}
// if header contains */type
if strings.Contains(h, "/"+strings.Split(m, "/")[0]) {
return typ
}
if typ == "html" && strings.Contains(h, "text/*") {
return typ
}
if strings.Contains(h, strings.Split(typ, "/")[0]) {
return typ
for _, spec := range specs {
spec = strings.TrimSpace(spec)
if strings.HasPrefix(spec, "*/*") {
return offer
}
if strings.HasPrefix(spec, mimetype) {
return offer
}
if strings.Contains(spec, "/*") {
if strings.HasPrefix(spec, strings.Split(mimetype, "/")[0]) {
return offer
}
}
}
}
return ""
}
// AcceptsCharsets : https://gofiber.github.io/fiber/#/context?id=acceptscharsets
func (ctx *Ctx) AcceptsCharsets(charset string) bool {
accept := ctx.Get("Accept-Charset")
if strings.Contains(accept, charset) {
return true
func (ctx *Ctx) AcceptsCharsets(offers ...string) string {
if len(offers) == 0 {
panic("You must provide atleast one content type string")
}
return false
h := ctx.Get("Accept-Charset")
if h == "" {
return offers[0]
}
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 ""
}
// AcceptsEncodings : https://gofiber.github.io/fiber/#/context?id=acceptsencodings
func (ctx *Ctx) AcceptsEncodings(encoding string) bool {
accept := ctx.Get("Accept-Encoding")
if strings.Contains(accept, encoding) {
return true
func (ctx *Ctx) AcceptsEncodings(offers ...string) string {
if len(offers) == 0 {
panic("You must provide atleast one content type string")
}
return false
h := ctx.Get("Accept-Encoding")
if h == "" {
return offers[0]
}
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 ""
}
// AcceptsLanguages : https://gofiber.github.io/fiber/#/context?id=acceptslanguages
func (ctx *Ctx) AcceptsLanguages(lang string) bool {
accept := ctx.Get("Accept-Language")
if strings.Contains(accept, lang) {
return true
func (ctx *Ctx) AcceptsLanguages(offers ...string) string {
if len(offers) == 0 {
panic("You must provide atleast one content type string")
}
return false
h := ctx.Get("Accept-Language")
if h == "" {
return offers[0]
}
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 ""
}
// BaseUrl : https://gofiber.github.io/fiber/#/context?id=baseurl