这是indexloc提供的服务,不要输入任何密码
Skip to content

Range eof #3323

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

Open
wants to merge 8 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Sources/Vapor/HTTP/Headers/HTTPHeaders+ContentRange.swift
Original file line number Diff line number Diff line change
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)
}
}
}
7 changes: 5 additions & 2 deletions Sources/Vapor/Utilities/FileIO.swift
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,7 @@ public struct FileIO: Sendable {
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 @@ -608,11 +609,13 @@ 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)
}
let (byteCount, overflow) = (end - start).addingReportingOverflow(1)
// Request past EOF, return up to EOF bytes
let validEnd = min(end,size-1)
let (byteCount, overflow) = (validEnd - start).addingReportingOverflow(1)
guard !overflow else {
logger.debug("Requested range was invalid: \(start)-\(end)")
throw Abort(.badRequest)
Expand Down
4 changes: 2 additions & 2 deletions Tests/VaporTests/AsyncFileTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ final class AsyncFileTests: XCTestCase, @unchecked Sendable {

headerRequest.range = .init(unit: .bytes, ranges: [.within(start: 10, end: 100000000)])
try await app.testable(method: .running(port: 0)).test(.GET, "/file-stream", headers: headerRequest) { res async in
XCTAssertEqual(res.status, .badRequest)
XCTAssertEqual(res.status, .partialContent)
}
}

Expand Down Expand Up @@ -297,7 +297,7 @@ final class AsyncFileTests: XCTestCase, @unchecked Sendable {
var headers = HTTPHeaders()
headers.replaceOrAdd(name: .range, value: "bytes=0-9223372036854775807")
try await app.testable(method: .running(port: 0)).test(.GET, "/file-stream", headers: headers) { res async in
XCTAssertEqual(res.status, .badRequest)
XCTAssertEqual(res.status, .partialContent)
}

headers.replaceOrAdd(name: .range, value: "bytes=-1-10")
Expand Down
23 changes: 20 additions & 3 deletions Tests/VaporTests/FileTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,26 @@ final class FileTests: XCTestCase {
XCTAssertEqual(res.status, .badRequest)
}

headerRequest.range = .init(unit: .bytes, ranges: [.within(start: 10, end: 100000000)])
let offset = 10
headerRequest.range = .init(unit: .bytes, ranges: [.within(start: offset, end: 100000000)])
try app.testable(method: .running(port: 0)).test(.GET, "/file-stream", headers: headerRequest) { res in
XCTAssertEqual(res.status, .badRequest)
XCTAssertEqual(res.status, .partialContent)
// Test content-range and number of bytes returned
do {
var fileSize = 0
let attr: Dictionary? = try FileManager.default.attributesOfItem(atPath: #file)
if let _attr = attr {
fileSize = _attr[.size] as? Int ?? 0
}
let range = res.headers.first(name: .contentRange)!.split(separator: "/").first!.split(separator: " ").last!
XCTAssertEqual(range, "10-\(fileSize)")

let count = res.body.readableBytes
XCTAssertEqual(count, fileSize-offset)
}
catch {
XCTFail("Checking content-range should have succeeded")
}
}
}

Expand Down Expand Up @@ -453,7 +470,7 @@ final class FileTests: XCTestCase {
}

var headers = HTTPHeaders()
headers.replaceOrAdd(name: .range, value: "bytes=0-9223372036854775807")
headers.replaceOrAdd(name: .range, value: "bytes=-9-9223372036854775807")
try app.testable(method: .running(port: 0)).test(.GET, "/file-stream", headers: headers) { res in
XCTAssertEqual(res.status, .badRequest)
}
Expand Down
10 changes: 10 additions & 0 deletions Tests/VaporTests/HTTPHeaderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,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