diff options
author | burbon <blazej.cegielka@gmail.com> | 2020-05-11 09:37:59 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-11 11:37:59 +0300 |
commit | d8e6acda8cfe7e823ce49a20a4587b2641a90bf4 (patch) | |
tree | e5e638c32fcdcbde0a20b22c159a225a696eab97 /modules | |
parent | 742e26f5a5dab8768558231cb88801911b9abaa7 (diff) | |
download | gitea-d8e6acda8cfe7e823ce49a20a4587b2641a90bf4.tar.gz gitea-d8e6acda8cfe7e823ce49a20a4587b2641a90bf4.zip |
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 <art27@cantab.net>
Co-authored-by: zeripath <art27@cantab.net>
Diffstat (limited to 'modules')
-rw-r--r-- | modules/lfs/server.go | 20 |
1 files changed, 15 insertions, 5 deletions
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) |