Skip to content

Commit 6e9fae8

Browse files
authored
Merge pull request #436 from Fenny/master
Hot fix Fasthttp file server bug
2 parents 961fda2 + f44b1fd commit 6e9fae8

File tree

5 files changed

+125
-13
lines changed

5 files changed

+125
-13
lines changed

app.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
)
2727

2828
// Version of current package
29-
const Version = "1.10.4"
29+
const Version = "1.10.5"
3030

3131
// Map is a shortcut for map[string]interface{}, useful for JSON returns
3232
type Map map[string]interface{}
@@ -49,6 +49,14 @@ type App struct {
4949

5050
// Settings holds is a struct holding the server settings
5151
type Settings struct {
52+
// Possible feature for v1.11.x
53+
// // ErrorHandler is executed when you pass an error in the Next(err) method
54+
// // This function is also executed when a panic occurs somewhere in the stack
55+
// // Default: func(err error, ctx *fiber.Ctx) {
56+
// // ctx.Status(500).Send(err.Error())
57+
// // }
58+
// ErrorHandler func(*Ctx, error)
59+
5260
// Enables the "Server: value" HTTP header.
5361
// Default: ""
5462
ServerHeader string
@@ -181,6 +189,10 @@ func New(settings ...*Settings) *App {
181189
Prefork: utils.GetArgument("-prefork"),
182190
BodyLimit: 4 * 1024 * 1024,
183191
Concurrency: 256 * 1024,
192+
// Possible feature for v1.11.x
193+
// ErrorHandler: func(ctx *Ctx, err error) {
194+
// ctx.Status(500).SendString(err.Error())
195+
// },
184196
},
185197
}
186198
// Overwrite settings if provided
@@ -200,6 +212,12 @@ func New(settings ...*Settings) *App {
200212
getBytes = getBytesImmutable
201213
getString = getStringImmutable
202214
}
215+
// Possible feature for v1.11.x
216+
// if app.Settings.ErrorHandler == nil {
217+
// app.Settings.ErrorHandler = func(ctx *Ctx, err error) {
218+
// ctx.Status(500).SendString(err.Error())
219+
// }
220+
// }
203221
}
204222
// Initialize app
205223
return app.init()
@@ -491,6 +509,10 @@ func (app *App) init() *App {
491509
Logger: &disableLogger{},
492510
LogAllErrors: false,
493511
ErrorHandler: func(fctx *fasthttp.RequestCtx, err error) {
512+
// Possible feature for v1.11.x
513+
// ctx := app.AcquireCtx(fctx)
514+
// app.Settings.ErrorHandler(ctx, err)
515+
// app.ReleaseCtx(ctx)
494516
if _, ok := err.(*fasthttp.ErrSmallBuffer); ok {
495517
fctx.Response.SetStatusCode(StatusRequestHeaderFieldsTooLarge)
496518
fctx.Response.SetBodyString("Request Header Fields Too Large")

app_test.go

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,43 @@ func testStatus200(t *testing.T, app *App, url string, method string) {
2828

2929
// }
3030

31+
// func Test_App_ErrorHandler(t *testing.T) {
32+
// app := New()
33+
34+
// app.Get("/", func(c *Ctx) {
35+
// c.Next(errors.New("Hi, I'm an error!"))
36+
// })
37+
38+
// resp, err := app.Test(httptest.NewRequest("GET", "/", nil))
39+
// utils.AssertEqual(t, nil, err, "app.Test(req)")
40+
// utils.AssertEqual(t, 500, resp.StatusCode, "Status code")
41+
42+
// body, err := ioutil.ReadAll(resp.Body)
43+
// utils.AssertEqual(t, nil, err)
44+
// utils.AssertEqual(t, "Hi, I'm an error!", string(body))
45+
46+
// }
47+
48+
// func Test_App_ErrorHandler_Custom(t *testing.T) {
49+
// app := New(&Settings{
50+
// ErrorHandler: func(ctx *Ctx, err error) {
51+
// ctx.Status(200).SendString("Hi, I'm an custom error!")
52+
// },
53+
// })
54+
55+
// app.Get("/", func(c *Ctx) {
56+
// c.Next(errors.New("Hi, I'm an error!"))
57+
// })
58+
59+
// resp, err := app.Test(httptest.NewRequest("GET", "/", nil))
60+
// utils.AssertEqual(t, nil, err, "app.Test(req)")
61+
// utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
62+
63+
// body, err := ioutil.ReadAll(resp.Body)
64+
// utils.AssertEqual(t, nil, err)
65+
// utils.AssertEqual(t, "Hi, I'm an custom error!", string(body))
66+
// }
67+
3168
func Test_App_Nested_Params(t *testing.T) {
3269
app := New()
3370

@@ -204,8 +241,8 @@ func Test_App_Shutdown(t *testing.T) {
204241
_ = app.Shutdown()
205242
}
206243

207-
// go test -run Test_App_Static
208-
func Test_App_Static_Index(t *testing.T) {
244+
// go test -run Test_App_Static_Index_Default
245+
func Test_App_Static_Index_Default(t *testing.T) {
209246
app := New()
210247

211248
app.Static("/prefix", "./.github/workflows")
@@ -222,6 +259,33 @@ func Test_App_Static_Index(t *testing.T) {
222259
utils.AssertEqual(t, true, strings.Contains(string(body), "Hello, World!"))
223260

224261
}
262+
263+
// go test -run Test_App_Static_Index
264+
func Test_App_Static_Direct(t *testing.T) {
265+
app := New()
266+
267+
app.Static("/", "./.github")
268+
269+
resp, err := app.Test(httptest.NewRequest("GET", "/index.html", nil))
270+
utils.AssertEqual(t, nil, err, "app.Test(req)")
271+
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
272+
utils.AssertEqual(t, false, resp.Header.Get("Content-Length") == "")
273+
utils.AssertEqual(t, "text/html; charset=utf-8", resp.Header.Get("Content-Type"))
274+
275+
body, err := ioutil.ReadAll(resp.Body)
276+
utils.AssertEqual(t, nil, err)
277+
utils.AssertEqual(t, true, strings.Contains(string(body), "Hello, World!"))
278+
279+
resp, err = app.Test(httptest.NewRequest("GET", "/FUNDING.yml", nil))
280+
utils.AssertEqual(t, nil, err, "app.Test(req)")
281+
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
282+
utils.AssertEqual(t, false, resp.Header.Get("Content-Length") == "")
283+
utils.AssertEqual(t, "text/plain; charset=utf-8", resp.Header.Get("Content-Type"))
284+
285+
body, err = ioutil.ReadAll(resp.Body)
286+
utils.AssertEqual(t, nil, err)
287+
utils.AssertEqual(t, true, strings.Contains(string(body), "buymeacoffee"))
288+
}
225289
func Test_App_Static_Group(t *testing.T) {
226290
app := New()
227291

@@ -264,6 +328,10 @@ func Test_App_Static_Wildcard(t *testing.T) {
264328
utils.AssertEqual(t, false, resp.Header.Get("Content-Length") == "")
265329
utils.AssertEqual(t, "text/plain; charset=utf-8", resp.Header.Get("Content-Type"))
266330

331+
body, err := ioutil.ReadAll(resp.Body)
332+
utils.AssertEqual(t, nil, err)
333+
utils.AssertEqual(t, true, strings.Contains(string(body), "buymeacoffee"))
334+
267335
}
268336

269337
func Test_App_Static_Prefix_Wildcard(t *testing.T) {
@@ -277,6 +345,18 @@ func Test_App_Static_Prefix_Wildcard(t *testing.T) {
277345
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
278346
utils.AssertEqual(t, false, resp.Header.Get("Content-Length") == "")
279347
utils.AssertEqual(t, "text/plain; charset=utf-8", resp.Header.Get("Content-Type"))
348+
349+
app.Static("/my/nameisjohn*", "./.github/FUNDING.yml")
350+
351+
resp, err = app.Test(httptest.NewRequest("GET", "/my/nameisjohn/no/its/not", nil))
352+
utils.AssertEqual(t, nil, err, "app.Test(req)")
353+
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
354+
utils.AssertEqual(t, false, resp.Header.Get("Content-Length") == "")
355+
utils.AssertEqual(t, "text/plain; charset=utf-8", resp.Header.Get("Content-Type"))
356+
357+
body, err := ioutil.ReadAll(resp.Body)
358+
utils.AssertEqual(t, nil, err)
359+
utils.AssertEqual(t, true, strings.Contains(string(body), "buymeacoffee"))
280360
}
281361

282362
func Test_App_Static_Prefix(t *testing.T) {

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ module github.com/gofiber/fiber
33
go 1.11
44

55
require (
6-
github.com/gofiber/utils v0.0.3
6+
github.com/gofiber/utils v0.0.4
7+
github.com/google/uuid v1.1.1 // indirect
78
github.com/gorilla/schema v1.1.0
89
github.com/valyala/bytebufferpool v1.0.0
910
github.com/valyala/fasthttp v1.14.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
github.com/andybalholm/brotli v1.0.0 h1:7UCwP93aiSfvWpapti8g88vVVGp2qqtGyePsSuDafo4=
22
github.com/andybalholm/brotli v1.0.0/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
3-
github.com/gofiber/utils v0.0.3 h1:nNQKfZbZAGmOHqTOYplJwwOvX1Mg/NsTjfFO4/wTGrU=
4-
github.com/gofiber/utils v0.0.3/go.mod h1:pacRFtghAE3UoknMOUiXh2Io/nLWSUHtQCi/3QASsOc=
3+
github.com/gofiber/utils v0.0.4 h1:gcOb+WZDQ319bELqAp5ePDYwxostDDzWh4pY7S0KiMI=
4+
github.com/gofiber/utils v0.0.4/go.mod h1:pacRFtghAE3UoknMOUiXh2Io/nLWSUHtQCi/3QASsOc=
55
github.com/gorilla/schema v1.1.0 h1:CamqUDOFUBqzrvxuz2vEwo8+SUdwsluFh7IlzJh30LY=
66
github.com/gorilla/schema v1.1.0/go.mod h1:kgLaKoK1FELgZqMAVxx/5cbj0kT+57qxUrAlIO2eleU=
77
github.com/klauspost/compress v1.10.4 h1:jFzIFaf586tquEB5EhzQG0HwGNSlgAJpG53G6Ss11wc=

router.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,19 @@ func (app *App) next(ctx *Ctx) bool {
9696
func (app *App) handler(rctx *fasthttp.RequestCtx) {
9797
// Acquire Ctx with fasthttp request from pool
9898
ctx := app.AcquireCtx(rctx)
99+
// // Possible feature for v1.12
100+
// // Add recover by default
101+
// defer func() {
102+
// if r := recover(); r != nil {
103+
// err, ok := r.(error)
104+
// if !ok {
105+
// err = fmt.Errorf("%v", r)
106+
// }
107+
// app.Settings.ErrorHandler(ctx, err)
108+
// app.ReleaseCtx(ctx)
109+
// return
110+
// }
111+
// }()
99112
// Prettify path
100113
ctx.prettifyPath()
101114
// Find match in stack
@@ -233,7 +246,8 @@ func (app *App) registerStatic(prefix, root string, config ...Static) *Route {
233246
path = path[prefixLen:]
234247
}
235248
}
236-
return append(path, '/')
249+
path = append([]byte("/"), path...)
250+
return path
237251
},
238252
PathNotFound: func(ctx *fasthttp.RequestCtx) {
239253
ctx.Response.SetStatusCode(404)
@@ -262,12 +276,7 @@ func (app *App) registerStatic(prefix, root string, config ...Static) *Route {
262276
c.Fasthttp.Response.SetStatusCode(200)
263277
c.Fasthttp.Response.SetBodyString("")
264278
// Next middleware
265-
match := c.app.next(c)
266-
// If no other route is executed return 404 Not Found
267-
if !match {
268-
c.Fasthttp.Response.SetStatusCode(404)
269-
c.Fasthttp.Response.SetBodyString("Not Found")
270-
}
279+
c.Next()
271280
}
272281
route := &Route{
273282
use: true,

0 commit comments

Comments
 (0)