Add files via upload

pull/6/head
Fenny 2020-01-07 09:30:09 -05:00 committed by GitHub
parent c93f21fc8a
commit 700c721c89
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 1 deletions

View File

@ -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>
[![Latest Release](https://img.shields.io/github/release/fenny/fiber.svg)](https://github.com/fenny/fiber/releases/latest)
[![GoDoc](https://godoc.org/github.com/fenny/fiber?status.svg)](http://godoc.org/github.com/fenny/fiber)

View File

@ -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 {