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 all commits
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 @@ -263,11 +263,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 endToRequest = min(end, limit)
return .withinWithLimit(start: start, end: endToRequest, limit: limit)
}
}
}
10 changes: 8 additions & 2 deletions Sources/Vapor/Utilities/FileIO.swift
Expand Up @@ -275,6 +275,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 @@ -289,7 +290,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 @@ -298,7 +299,12 @@ extension HTTPHeaders.Range.Value {
logger.debug("Requested range was invalid: \(start)-\(end)")
throw Abort(.badRequest)
}
return (offset: numericCast(start), byteCount: byteCount)
var byteCountToReqeust = byteCount
// Request past EOF, return up to EOF bytes
if (end > size) {
byteCountToReqeust = byteCount - (end - size)
}
return (offset: numericCast(start), byteCount: byteCountToReqeust)
}
}
}
10 changes: 10 additions & 0 deletions Tests/VaporTests/HTTPHeaderTests.swift
Expand Up @@ -359,6 +359,16 @@ final class HTTPHeaderTests: XCTestCase {
)
}

func testRangeLimit() throws {
let contentRanges = HTTPHeaders.Range(unit: .bytes, ranges: [
.within(start: 200, end: 1000)
])

let firstRange = contentRanges.ranges.first
let accept = try firstRange!.asResponseContentRange(limit: 600)
XCTAssertEqual(accept.serialize(), "200-600/600")
}

func testLinkHeaderParsing() throws {
let headers = HTTPHeaders([
("link", #"<https://localhost/?a=1>; rel="next", <https://localhost/?a=2>; rel="last"; custom1="whatever", </?a=-1>; rel=related, </?a=-2>; rel=related"#)
Expand Down