mirror of https://github.com/gofiber/fiber.git
Add files via upload
parent
c93f21fc8a
commit
700c721c89
|
@ -1,4 +1,7 @@
|
|||
<img src="docs/logo.jpg" width="150" alt="Fiber"><br><br>
|
||||
<p align="center">
|
||||
<img src="https://fenny.github.io/fiber/logo.jpg" width="150" alt="Fiber">
|
||||
<br><br>
|
||||
</p>
|
||||
|
||||
[](https://github.com/fenny/fiber/releases/latest)
|
||||
[](http://godoc.org/github.com/fenny/fiber)
|
||||
|
|
37
context.go
37
context.go
|
@ -1,9 +1,13 @@
|
|||
package fiber
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"mime"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/pquerna/ffjson/ffjson"
|
||||
"github.com/valyala/fasthttp"
|
||||
)
|
||||
|
||||
|
@ -42,6 +46,29 @@ func (ctx *Ctx) Path() string {
|
|||
return b2s(ctx.Fasthttp.URI().Path())
|
||||
}
|
||||
|
||||
// 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 := string(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 {
|
||||
|
@ -148,6 +175,16 @@ func (ctx *Ctx) Get(key string) string {
|
|||
return b2s(ctx.Fasthttp.Request.Header.Peek(key))
|
||||
}
|
||||
|
||||
// Json :
|
||||
func (ctx *Ctx) Json(v interface{}) error {
|
||||
ctx.Set("Content-Type", "application/json")
|
||||
b := bytes.NewBuffer(nil)
|
||||
enc := ffjson.NewEncoder(b)
|
||||
err := enc.Encode(v)
|
||||
ctx.Send(b.Bytes())
|
||||
return err
|
||||
}
|
||||
|
||||
// Redirect :
|
||||
func (ctx *Ctx) Redirect(args ...interface{}) {
|
||||
if len(args) == 1 {
|
||||
|
|
Loading…
Reference in New Issue