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>
This commit is contained in:
parent
742e26f5a5
commit
d8e6acda8c
2 changed files with 119 additions and 18 deletions
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue