Skip to content

Commit

Permalink
Revert ":sparkles: feature: add SendFileWithConfig method to ctx (#1852
Browse files Browse the repository at this point in the history
…)" (#1861)

This reverts commit f19ef67.
  • Loading branch information
ReneWerner87 committed Apr 15, 2022
1 parent c42af6d commit e9151e8
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 165 deletions.
24 changes: 0 additions & 24 deletions app.go
Expand Up @@ -412,30 +412,6 @@ type Static struct {
Next func(c *Ctx) bool
}

// SendFile defines configuration options when to transfer file with SendFileWithConfig.
type SendFile struct {
// When set to true, the server tries minimizing CPU usage by caching compressed files.
// This works differently than the github.com/gofiber/compression middleware.
// Optional. Default value false
Compress bool `json:"compress"`

// When set to true, enables byte range requests.
// Optional. Default value false
ByteRange bool `json:"byte_range"`

// Expiration duration for inactive file handlers.
// Use a negative time.Duration to disable it.
//
// Optional. Default value 10 * time.Second.
CacheDuration time.Duration `json:"cache_duration"`

// The value for the Cache-Control HTTP-header
// that is set on the file response. MaxAge is defined in seconds.
//
// Optional. Default value 0.
MaxAge int `json:"max_age"`
}

// RouteMessage is some message need to be print when server starts
type RouteMessage struct {
name string
Expand Down
28 changes: 3 additions & 25 deletions ctx.go
Expand Up @@ -1337,17 +1337,6 @@ var (
// The file is not compressed by default, enable this by passing a 'true' argument
// Sets the Content-Type response HTTP header field based on the filenames extension.
func (c *Ctx) SendFile(file string, compress ...bool) error {
if len(compress) > 0 {
return c.SendFileWithConfig(file, SendFile{Compress: compress[0]})
}

return c.SendFileWithConfig(file)
}

// SendFileWithConfig transfers the file from the given path.
// Some config fields are overwritten. You can change them by config parameter.
// Sets the Content-Type response HTTP header field based on the filenames extension.
func (c *Ctx) SendFileWithConfig(file string, config ...SendFile) error {
// Save the filename, we will need it in the error message if the file isn't found
filename := file

Expand All @@ -1371,18 +1360,10 @@ func (c *Ctx) SendFileWithConfig(file string, config ...SendFile) error {
// Keep original path for mutable params
c.pathOriginal = utils.CopyString(c.pathOriginal)
// Disable compression
// Set config if provided
var cacheControlValue string
if len(config) > 0 {
maxAge := config[0].MaxAge
if maxAge > 0 {
cacheControlValue = "public, max-age=" + strconv.Itoa(maxAge)
}
sendFileFS.CacheDuration = config[0].CacheDuration
sendFileFS.Compress = config[0].Compress
sendFileFS.AcceptByteRange = config[0].ByteRange
if len(compress) == 0 || !compress[0] {
// https://github.com/valyala/fasthttp/blob/master/fs.go#L46
c.fasthttp.Request.Header.Del(HeaderAcceptEncoding)
}

// https://github.com/valyala/fasthttp/blob/master/fs.go#L85
if len(file) == 0 || file[0] != '/' {
hasTrailingSlash := len(file) > 0 && file[len(file)-1] == '/'
Expand All @@ -1409,9 +1390,6 @@ func (c *Ctx) SendFileWithConfig(file string, config ...SendFile) error {
if status != fsStatus && status != StatusOK {
c.Status(status)
}
if status != StatusNotFound && status != StatusForbidden && len(cacheControlValue) > 0 {
c.fasthttp.Response.Header.Set(HeaderCacheControl, cacheControlValue)
}
// Check for error
if status != StatusNotFound && fsStatus == StatusNotFound {
return NewError(StatusNotFound, fmt.Sprintf("sendfile: file %s not found", filename))
Expand Down
116 changes: 0 additions & 116 deletions ctx_test.go
Expand Up @@ -1935,122 +1935,6 @@ func Test_Ctx_SendFile_RestoreOriginalURL(t *testing.T) {
utils.AssertEqual(t, nil, err2)
}

// go test -race -run Test_Ctx_SendFileWithConfig
func Test_Ctx_SendFileWithConfig(t *testing.T) {
t.Parallel()
app := New()

// fetch file content
f, err := os.Open("./ctx.go")
utils.AssertEqual(t, nil, err)
defer f.Close()
expectFileContent, err := ioutil.ReadAll(f)
utils.AssertEqual(t, nil, err)
// fetch file info for the not modified test case
fI, err := os.Stat("./ctx.go")
utils.AssertEqual(t, nil, err)

// simple test case
c := app.AcquireCtx(&fasthttp.RequestCtx{})
err = c.SendFileWithConfig("ctx.go")
// check expectation
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, expectFileContent, c.Response().Body())
utils.AssertEqual(t, StatusOK, c.Response().StatusCode())
app.ReleaseCtx(c)

// test with custom error code
c = app.AcquireCtx(&fasthttp.RequestCtx{})
err = c.Status(StatusInternalServerError).SendFileWithConfig("ctx.go")
// check expectation
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, expectFileContent, c.Response().Body())
utils.AssertEqual(t, StatusInternalServerError, c.Response().StatusCode())
app.ReleaseCtx(c)

// test not modified
c = app.AcquireCtx(&fasthttp.RequestCtx{})
c.Request().Header.Set(HeaderIfModifiedSince, fI.ModTime().Format(time.RFC1123))
err = c.SendFileWithConfig("ctx.go")
// check expectation
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, StatusNotModified, c.Response().StatusCode())
utils.AssertEqual(t, []byte(nil), c.Response().Body())
app.ReleaseCtx(c)
}

// go test -race -run Test_Ctx_SendFileWithConfig_404
func Test_Ctx_SendFileWithConfig_404(t *testing.T) {
t.Parallel()
app := New()
app.Get("/", func(c *Ctx) error {
err := c.SendFileWithConfig("./john_dow.go/")
utils.AssertEqual(t, false, err == nil)
return err
})

resp, err := app.Test(httptest.NewRequest("GET", "/", nil))
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, StatusNotFound, resp.StatusCode)
}

// go test -race -run Test_Ctx_SendFileWithConfig_MaxAge
func Test_Ctx_SendFileWithConfig_MaxAge(t *testing.T) {
t.Parallel()
app := New()
app.Get("/", func(c *Ctx) error {
err := c.SendFileWithConfig("./john_dow.go/", SendFile{MaxAge: 100})
utils.AssertEqual(t, false, err == nil)
return err
})

resp, err := app.Test(httptest.NewRequest("GET", "/", nil))
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, StatusNotFound, resp.StatusCode)
utils.AssertEqual(t, "public, max-age=100", resp.Header.Get(HeaderCacheControl), "CacheControl Control")
}

// go test -race -run Test_Ctx_SendFileWithConfig_Immutable
func Test_Ctx_SendFileWithConfig_Immutable(t *testing.T) {
t.Parallel()
app := New()
app.Get("/:file", func(c *Ctx) error {
file := c.Params("file")
if err := c.SendFileWithConfig("./.github/" + file + ".html"); err != nil {
utils.AssertEqual(t, nil, err)
}
utils.AssertEqual(t, "index", file)
return c.SendString(file)
})
// 1st try
resp, err := app.Test(httptest.NewRequest("GET", "/index", nil))
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, StatusOK, resp.StatusCode)
// 2nd try
resp, err = app.Test(httptest.NewRequest("GET", "/index", nil))
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, StatusOK, resp.StatusCode)
}

// go test -race -run Test_Ctx_SendFileWithConfig_RestoreOriginalURL
func Test_Ctx_SendFileWithConfig_RestoreOriginalURL(t *testing.T) {
t.Parallel()
app := New()
app.Get("/", func(c *Ctx) error {
originalURL := utils.CopyString(c.OriginalURL())
err := c.SendFileWithConfig("ctx.go")
utils.AssertEqual(t, originalURL, c.OriginalURL())
return err
})

_, err1 := app.Test(httptest.NewRequest("GET", "/?test=true", nil))
// second request required to confirm with zero allocation
_, err2 := app.Test(httptest.NewRequest("GET", "/?test=true", nil))

utils.AssertEqual(t, nil, err1)
utils.AssertEqual(t, nil, err2)
}

// go test -run Test_Ctx_JSON
func Test_Ctx_JSON(t *testing.T) {
t.Parallel()
Expand Down

0 comments on commit e9151e8

Please sign in to comment.