Skip to content

Commit

Permalink
feat: Add way to redirect with trailing slash when getting a file giv…
Browse files Browse the repository at this point in the history
…es a not found error
  • Loading branch information
oxyno-zeta committed Jan 18, 2021
1 parent e43bc99 commit c441329
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
3 changes: 2 additions & 1 deletion pkg/s3-proxy/bucket/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ type ErrorHandlers struct {
// nolint:whitespace
func NewClient(
tgt *config.TargetConfig, tplConfig *config.TemplateConfig, logger log.Logger,
mountPath string, httpRW http.ResponseWriter,
mountPath string, httpRW http.ResponseWriter, httpReq *http.Request,
metricsCtx metrics.Client,
errorHandlers *ErrorHandlers,
parentTrace tracing.Trace,
Expand All @@ -69,6 +69,7 @@ func NewClient(
targetCfg: tgt,
mountPath: mountPath,
httpRW: httpRW,
httpReq: httpReq,
tplConfig: tplConfig,
errorsHandlers: errorHandlers,
}, nil
Expand Down
20 changes: 20 additions & 0 deletions pkg/s3-proxy/bucket/requestContext.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type requestContext struct {
tplConfig *config.TemplateConfig
mountPath string
httpRW http.ResponseWriter
httpReq *http.Request
errorsHandlers *ErrorHandlers
}

Expand Down Expand Up @@ -182,6 +183,25 @@ func (rctx *requestContext) Get(requestPath string) {
if err != nil {
// Check if error is a not found error
if err == s3client.ErrNotFound {
// Test that redirect with trailing slash isn't asked and possible on this request
if rctx.targetCfg.Actions != nil && rctx.targetCfg.Actions.GET != nil &&
rctx.targetCfg.Actions.GET.RedirectWithTrailingSlashForNotFoundFile &&
!strings.HasSuffix(requestPath, "/") {
// Get path
p := rctx.httpReq.URL.Path
// Check if path doesn't start with /
if !strings.HasPrefix(p, "/") {
p = "/" + p
}
// Check if path doesn't end with /
if !strings.HasSuffix(p, "/") {
p += "/"
}
// Redirect
http.Redirect(rctx.httpRW, rctx.httpReq, p, http.StatusFound)

return
}
// Not found
rctx.HandleNotFound(requestPath)
// Stop
Expand Down
3 changes: 2 additions & 1 deletion pkg/s3-proxy/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,8 @@ type PutActionConfigConfig struct {

// GetActionConfig Get action configuration
type GetActionConfig struct {
Enabled bool `mapstructure:"enabled"`
Enabled bool `mapstructure:"enabled"`
RedirectWithTrailingSlashForNotFoundFile bool `mapstructure:"redirectWithTrailingSlashForNotFoundFile"`
}

// Resource Resource
Expand Down
2 changes: 1 addition & 1 deletion pkg/s3-proxy/server/middlewares/bucket-request-context.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func BucketRequestContext(
// Get request trace
trace := tracing.GetTraceFromRequest(req)
// Generate new bucket client
brctx, err := bucket.NewClient(tgt, tplConfig, logEntry, path, rw, metricsCli, errorhandlers, trace)
brctx, err := bucket.NewClient(tgt, tplConfig, logEntry, path, rw, req, metricsCli, errorhandlers, trace)
if err != nil {
logEntry.Error(err)
utils.HandleInternalServerError(logEntry, rw, tplConfig, requestURI, err)
Expand Down

0 comments on commit c441329

Please # to comment.