1
0
mirror of https://github.com/gofiber/fiber.git synced 2025-02-24 08:43:43 +00:00

Merge pull request #936 from imxyb/cache-baseuri

improve base uri for ctx
This commit is contained in:
Fenny 2020-10-19 14:49:31 -07:00 committed by GitHub
commit d2c0d0f8c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 2 deletions

9
ctx.go
View File

@ -39,6 +39,7 @@ type Ctx struct {
indexHandler int // Index of the current handler
method string // HTTP method
methodINT int // HTTP method INT equivalent
baseURI string // HTTP base uri
path string // Prettified HTTP path -> string copy from pathBuffer
pathBuffer []byte // Prettified HTTP path buffer
treePath string // Path for the search in the tree
@ -94,6 +95,8 @@ func (app *App) AcquireCtx(fctx *fasthttp.RequestCtx) *Ctx {
c.methodINT = methodInt(c.method)
// Attach *fasthttp.RequestCtx to ctx
c.fasthttp = fctx
// reset base uri
c.baseURI = ""
// Prettify path
c.prettifyPath()
return c
@ -206,7 +209,11 @@ func (c *Ctx) Attachment(filename ...string) {
func (c *Ctx) BaseURL() string {
// TODO: Could be improved: 53.8 ns/op 32 B/op 1 allocs/op
// Should work like https://codeigniter.com/user_guide/helpers/url_helper.html
return c.Protocol() + "://" + c.Hostname()
if c.baseURI != "" {
return c.baseURI
}
c.baseURI = c.Protocol() + "://" + c.Hostname()
return c.baseURI
}
// Body contains the raw body submitted in a POST request.

View File

@ -259,7 +259,7 @@ func Test_Ctx_BaseURL(t *testing.T) {
utils.AssertEqual(t, "http://google.com", c.BaseURL())
}
// go test -v -run=^$ -bench=Benchmark_Ctx_Append -benchmem -count=4
// go test -v -run=^$ -bench=Benchmark_Ctx_BaseURL -benchmem
func Benchmark_Ctx_BaseURL(b *testing.B) {
app := New()
c := app.AcquireCtx(&fasthttp.RequestCtx{})