Skip to content

Commit

Permalink
Fixes response status code
Browse files Browse the repository at this point in the history
  • Loading branch information
aldor007 committed Mar 9, 2018
1 parent 8449073 commit bd661ff
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
27 changes: 18 additions & 9 deletions pkg/processor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (r *RequestProcessor) replyWithError(obj *object.FileObject, sc int, err er
}

key := r.serverConfig.Placeholder + strconv.FormatUint(obj.Transforms.Hash().Sum64(), 16)
if cacheRes := r.fetchResponseFromCache(key); cacheRes != nil {
if cacheRes := r.fetchResponseFromCache(key, true); cacheRes != nil {
cacheRes.StatusCode = sc
return cacheRes
}
Expand Down Expand Up @@ -131,7 +131,7 @@ func (r *RequestProcessor) replyWithError(obj *object.FileObject, sc int, err er
case <-timer.C:
return response.NewError(sc, err)
default:
if cacheRes := r.fetchResponseFromCache(key); cacheRes != nil {
if cacheRes := r.fetchResponseFromCache(key, false); cacheRes != nil {
lockResult.Cancel <- true
return cacheRes
}
Expand Down Expand Up @@ -197,7 +197,7 @@ func (r *RequestProcessor) collapseGET(req *http.Request, obj *object.FileObject
lockResult.Cancel <- true
return r.replyWithError(obj, 504, ErrTimeout)
default:
if cacheRes := r.fetchResponseFromCache(obj.Key); cacheRes != nil {
if cacheRes := r.fetchResponseFromCache(obj.Key, true); cacheRes != nil {
lockResult.Cancel <- true
return cacheRes
}
Expand All @@ -206,7 +206,7 @@ func (r *RequestProcessor) collapseGET(req *http.Request, obj *object.FileObject

}

func (r *RequestProcessor) fetchResponseFromCache(key string) *response.Response {
func (r *RequestProcessor) fetchResponseFromCache(key string, allowExpired bool) *response.Response {
cacheValue := r.cache.Get(key)
if cacheValue != nil {
if cacheValue.Expired() == false {
Expand All @@ -222,8 +222,15 @@ func (r *RequestProcessor) fetchResponseFromCache(key string) *response.Response
monitoring.Log().Info("Handle Get cache", zap.String("cache", "expired"), zap.String("obj.Key", key))
monitoring.Report().Inc("cache_ratio;status:expired")
res := cacheValue.Value().(*response.Response)
res.Close()
r.cache.Delete(key)
if allowExpired {
resCp, err := res.Copy()
if err == nil {
return resCp
}
} else {
res.Close()
r.cache.Delete(key)
}
}
}

Expand All @@ -232,7 +239,7 @@ func (r *RequestProcessor) fetchResponseFromCache(key string) *response.Response
}

func (r *RequestProcessor) handleGET(req *http.Request, obj *object.FileObject) *response.Response {
if cacheRes := r.fetchResponseFromCache(obj.Key); cacheRes != nil {
if cacheRes := r.fetchResponseFromCache(obj.Key, false); cacheRes != nil {
return cacheRes
}

Expand Down Expand Up @@ -269,7 +276,6 @@ func (r *RequestProcessor) handleGET(req *http.Request, obj *object.FileObject)
}(parentObj)
}

resLoop:
for {
select {
case <-ctx.Done():
Expand All @@ -290,7 +296,7 @@ resLoop:
}

if res.StatusCode == 404 {
break resLoop
return r.handleNotFound(ctx, obj, parentObj, transformsTab, parentRes, res)
} else {
return res
}
Expand All @@ -304,6 +310,9 @@ resLoop:
}
}

}

func (r *RequestProcessor) handleNotFound(ctx context.Context, obj, parentObj *object.FileObject, transformsTab []transforms.Transforms, parentRes, res *response.Response) *response.Response {
if parentObj != nil {
if !obj.CheckParent {
parentRes = storage.Head(parentObj)
Expand Down
3 changes: 2 additions & 1 deletion pkg/response/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,8 @@ func (r *Response) Send(w http.ResponseWriter) error {
// SendContent use http.ServeContent to return response to client
// It can handle range and condition requests
func (r *Response) SendContent(req *http.Request, w http.ResponseWriter) error {
if r.bodySeeker == nil || isRangeOrCondition(req) == false {
// ServerContent will modified status code so to it we should pass only 200 response
if r.StatusCode != 200 || r.bodySeeker == nil || isRangeOrCondition(req) == false {
return r.Send(w)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func Head(obj *object.FileObject) *response.Response {
if err != nil {
if err == stow.ErrNotFound {
monitoring.Logs().Infow("Storage/Head item response", zap.String("obj.Key", obj.Key), zap.String("obj.Bucket", obj.Bucket), zap.Int("sc", 404))
return response.NewString(404, obj.Key)
return response.NewString(404, notFound)
}

monitoring.Logs().Infow("Storage/Head item response", zap.String("obj.Key", obj.Key), zap.String("obj.Bucket", obj.Bucket), zap.Error(err))
Expand Down

0 comments on commit bd661ff

Please sign in to comment.