From d8e6acda8cfe7e823ce49a20a4587b2641a90bf4 Mon Sep 17 00:00:00 2001 From: burbon Date: Mon, 11 May 2020 09:37:59 +0100 Subject: Support Range header end in lfs (#11314) * Initial support of end Range header option in lfs * Allow end range option to be unspecified * Declare toByte for consistency * Factor out content encoding tests from doLfs This is so Range tests could still use doLfs but without repeating not related tests * Add Range header test * implemented extraHeader * parametrized expectedStatus * Add more test cases of Range header * Fix S1030: should use resp.Body.String() * Add more tests for edge cases * Fix tests Signed-off-by: Andrew Thornton Co-authored-by: zeripath --- modules/lfs/server.go | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) (limited to 'modules/lfs') diff --git a/modules/lfs/server.go b/modules/lfs/server.go index fda5ed7452..c39bf40d69 100644 --- a/modules/lfs/server.go +++ b/modules/lfs/server.go @@ -165,15 +165,24 @@ func getContentHandler(ctx *context.Context) { } // Support resume download using Range header - var fromByte int64 + var fromByte, toByte int64 + toByte = meta.Size - 1 statusCode := 200 if rangeHdr := ctx.Req.Header.Get("Range"); rangeHdr != "" { - regex := regexp.MustCompile(`bytes=(\d+)\-.*`) + regex := regexp.MustCompile(`bytes=(\d+)\-(\d*).*`) match := regex.FindStringSubmatch(rangeHdr) if len(match) > 1 { statusCode = 206 fromByte, _ = strconv.ParseInt(match[1], 10, 32) - ctx.Resp.Header().Set("Content-Range", fmt.Sprintf("bytes %d-%d/%d", fromByte, meta.Size-1, meta.Size-fromByte)) + + if match[2] != "" { + _toByte, _ := strconv.ParseInt(match[2], 10, 32) + if _toByte >= fromByte && _toByte < toByte { + toByte = _toByte + } + } + + ctx.Resp.Header().Set("Content-Range", fmt.Sprintf("bytes %d-%d/%d", fromByte, toByte, meta.Size-fromByte)) } } @@ -186,7 +195,8 @@ func getContentHandler(ctx *context.Context) { } defer content.Close() - ctx.Resp.Header().Set("Content-Length", strconv.FormatInt(meta.Size-fromByte, 10)) + contentLength := toByte + 1 - fromByte + ctx.Resp.Header().Set("Content-Length", strconv.FormatInt(contentLength, 10)) ctx.Resp.Header().Set("Content-Type", "application/octet-stream") filename := ctx.Params("filename") @@ -198,7 +208,7 @@ func getContentHandler(ctx *context.Context) { } ctx.Resp.WriteHeader(statusCode) - if written, err := io.Copy(ctx.Resp, content); err != nil { + if written, err := io.CopyN(ctx.Resp, content, contentLength); err != nil { log.Error("Error whilst copying LFS OID[%s] to the response after %d bytes. Error: %v", meta.Oid, written, err) } logRequest(ctx.Req, statusCode) -- cgit v1.2.3