Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support range-requests beyond EOF. Return up to EOF. #2997

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions Sources/Vapor/HTTP/Headers/HTTPHeaders+ContentRange.swift
Expand Up @@ -262,11 +262,12 @@ extension HTTPHeaders.Range.Value {
}
return .withinWithLimit(start: (limit - end), end: limit - 1, limit: limit)
case .within(let start, let end):
guard start >= 0, end >= 0, start < end, start <= limit, end <= limit else {
guard start >= 0, end >= 0, start < end, start <= limit else {
throw Abort(.badRequest)
}

return .withinWithLimit(start: start, end: end, limit: limit)
let myEnd = min(end, limit)
return .withinWithLimit(start: start, end: myEnd, limit: limit)
keniwhat marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
6 changes: 4 additions & 2 deletions Sources/Vapor/Utilities/FileIO.swift
Expand Up @@ -265,6 +265,7 @@ public struct FileIO {
extension HTTPHeaders.Range.Value {

fileprivate func asByteBufferBounds(withMaxSize size: Int, logger: Logger) throws -> (offset: Int64, byteCount: Int) {

switch self {
case .start(let value):
guard value <= size, value >= 0 else {
Expand All @@ -279,7 +280,7 @@ extension HTTPHeaders.Range.Value {
}
return (offset: numericCast(size - value), byteCount: value)
case .within(let start, let end):
guard start >= 0, end >= 0, start < end, start <= size, end <= size else {
guard start >= 0, end >= 0, start < end, start <= size else {
logger.debug("Requested range was invalid: \(start)-\(end)")
throw Abort(.badRequest)
}
Expand All @@ -288,7 +289,8 @@ extension HTTPHeaders.Range.Value {
logger.debug("Requested range was invalid: \(start)-\(end)")
throw Abort(.badRequest)
}
return (offset: numericCast(start), byteCount: byteCount)
let myByteCount = end <= size ? byteCount : byteCount - (end - size)
keniwhat marked this conversation as resolved.
Show resolved Hide resolved
return (offset: numericCast(start), byteCount: myByteCount)
}
}
}