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

Streaming files as partial content #2342

Merged
merged 13 commits into from Jan 7, 2021
Merged
262 changes: 262 additions & 0 deletions Sources/Vapor/HTTP/Headers/HTTPHeaders+ContentRange.swift
@@ -0,0 +1,262 @@
import Foundation

extension HTTPHeaders {

/// The unit in which `ContentRange`s and `Range`s are specified. This is usually `bytes`.
/// See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Range
public enum RangeUnit: Equatable {
case bytes
case custom(value: String)

public func serialize() -> String {
switch self {
case .bytes:
return "bytes"
case .custom(let value):
return value
}
}
}

/// Represents the HTTP `Range` request header.
/// See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Range
public struct Range: Equatable {
public let unit: RangeUnit
public let ranges: [HTTPHeaders.Range.Value]

public init(unit: RangeUnit, ranges: [HTTPHeaders.Range.Value]) {
self.unit = unit
self.ranges = ranges
}

init?(directives: [HTTPHeaders.Directive]) {
let rangeCandidates: [HTTPHeaders.Range.Value] = directives.enumerated().compactMap {
if $0.0 == 0, let parameter = $0.1.parameter {
return HTTPHeaders.Range.Value.from(requestStr: parameter)
}
return HTTPHeaders.Range.Value.from(requestStr: $0.1.value)
}
guard !rangeCandidates.isEmpty else {
return nil
}
self.ranges = rangeCandidates
let lowerCasedUnit = directives[0].value.trimmingCharacters(in: .whitespacesAndNewlines).lowercased()
self.unit = lowerCasedUnit == "bytes"
? RangeUnit.bytes
: RangeUnit.custom(value: lowerCasedUnit)
}

public func serialize() -> String {
return "\(unit.serialize())=\(ranges.map { $0.serialize() }.joined(separator: ", "))"
}
}

/// Represents the HTTP `Content-Range` response header.
///
/// See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Range
public struct ContentRange: Equatable {
public let unit: RangeUnit
public let range: HTTPHeaders.ContentRange.Value

init?(directive: HTTPHeaders.Directive) {
let splitResult = directive.value.split(separator: " ")
guard splitResult.count == 2 else {
return nil
}
let (unitStr, rangeStr) = (splitResult[0], splitResult[1])
let lowerCasedUnit = unitStr.trimmingCharacters(in: .whitespacesAndNewlines).lowercased()
guard let contentRange = HTTPHeaders.ContentRange.Value.from(responseStr: rangeStr) else {
return nil
}
self.unit = lowerCasedUnit == "bytes"
? RangeUnit.bytes
: RangeUnit.custom(value: lowerCasedUnit)
self.range = contentRange
}

public init(unit: RangeUnit, range: HTTPHeaders.ContentRange.Value) {
self.unit = unit
self.range = range
}

init?(directives: [Directive]) {
guard directives.count == 1 else {
return nil
}
self.init(directive: directives[0])
}

public func serialize() -> String {
return "\(unit.serialize()) \(range.serialize())"
}

}

/// Convenience for accessing the Content-Range response header.
///
/// See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Range
public var contentRange: ContentRange? {
get {
return HTTPHeaders.ContentRange(directives: self.parseDirectives(name: .contentRange).flatMap { $0 })
}
set {
if self.contains(name: .contentRange) {
self.remove(name: .contentRange)
}
guard let newValue = newValue else {
return
}
self.add(name: .contentRange, value: newValue.serialize())
}
}

/// Convenience for accessing the `Range` request header.
///
/// See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Range
public var range: Range? {
get {
return HTTPHeaders.Range(directives: self.parseDirectives(name: .range).flatMap { $0 })
}
set {
if self.contains(name: .range) {
self.remove(name: .range)
}
guard let newValue = newValue else {
return
}
self.add(name: .range, value: newValue.serialize())
}
}
}

extension HTTPHeaders.Range {
/// Represents one value of the `Range` request header.
///
/// See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Range
public enum Value: Equatable {
///Integer with single trailing dash, e.g. `25-`
case start(value: Int)
///Integer with single leading dash, e.g. `-25`
case tail(value: Int)
///Two integers with single dash in between, e.g. `20-25`
case within(start: Int, end: Int)

///Parses a string representing a requested range in one of the following formats:
///
///- `<range-start>-<range-end>`
///- `-<range-end>`
///- `<range-start>-`
///
/// - parameters:
/// - requestStr: String representing a requested range
/// - returns: A `HTTPHeaders.Range.Value` if the `requestStr` is valid, `nil` otherwise.
public static func from<T>(requestStr: T) -> HTTPHeaders.Range.Value? where T: StringProtocol {
let ranges = requestStr.split(separator: "-", omittingEmptySubsequences: false)
.map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }
let count = ranges.count
guard count <= 2 else { return nil }

switch (count > 0 ? Int(ranges[0]) : nil, count > 1 ? Int(ranges[1]) : nil) {
case (nil, nil):
return nil
case let (.some(start), nil):
return .start(value: start)
case let (nil, .some(tail)):
return .tail(value: tail)
case let (.some(start), .some(end)):
return .within(start: start, end: end)
}
}

///Serializes `HTTPHeaders.Range.Value` to a string for use within the HTTP `Range` header.
public func serialize() -> String {
switch self {
case .start(let value):
return "\(value)-"
case .tail(let value):
return "-\(value)"
case .within(let start, let end):
return "\(start)-\(end)"
}
}
}
}

extension HTTPHeaders.ContentRange {
/// Represents the value of the `Content-Range` request header.
///
/// See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Range
public enum Value : Equatable {
case within(start: Int, end: Int)
case withinWithLimit(start: Int, end: Int, limit: Int)
case any(size: Int)

///Parses a string representing a response range in one of the following formats:
///
///- `<range-start>-<range-end>/<size>`
///- `<range-start>-<range-end>/*`
///- `*/<size>`
///
/// - parameters:
/// - requestStr: String representing the response range
/// - returns: A `HTTPHeaders.ContentRange.Value` if the `responseStr` is valid, `nil` otherwise.
public static func from<T>(responseStr: T) -> HTTPHeaders.ContentRange.Value? where T : StringProtocol {
let ranges = responseStr.split(separator: "-", omittingEmptySubsequences: false)
.map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }

switch ranges.count {
case 1:
let anyRangeOfSize = ranges[0].split(separator: "/", omittingEmptySubsequences: false)
guard anyRangeOfSize.count == 2,
anyRangeOfSize[0] == "*",
let size = Int(anyRangeOfSize[1]) else {
return nil
}
return .any(size: size)
case 2:
guard let start = Int(ranges[0]) else {
return nil
}
let limits = ranges[1].split(separator: "/", omittingEmptySubsequences: false)
guard limits.count == 2, let end = Int(limits[0]) else {
return nil
}
if limits[1] == "*" {
return .within(start: start, end: end)
}
guard let limit = Int(limits[1]) else {
return nil
}
return .withinWithLimit(start: start, end: end, limit: limit)
default: return nil
}
}

///Serializes `HTTPHeaders.Range.Value` to a string for use within the HTTP `Content-Range` header.
public func serialize() -> String {
switch self {
case .any(let size):
return "*/\(size)"
case .within(let start, let end):
return "\(start)-\(end)/*"
case .withinWithLimit(let start, let end, let limit):
return "\(start)-\(end)/\(limit)"
}
}
}
}

extension HTTPHeaders.Range.Value {

///Converts this `HTTPHeaders.Range.Value` to a `HTTPHeaders.ContentRange.Value` with the given `limit`.
public func asResponseContentRange(limit: Int) -> HTTPHeaders.ContentRange.Value {
switch self {
case .start(let start):
return .withinWithLimit(start: start, end: limit - 1, limit: limit)
case .tail(let end):
return .withinWithLimit(start: end, end: limit - 1, limit: limit)
case .within(let start, let end):
return .withinWithLimit(start: start, end: end, limit: limit)
}
}
}
70 changes: 61 additions & 9 deletions Sources/Vapor/Utilities/FileIO.swift
Expand Up @@ -92,7 +92,14 @@ public struct FileIO {
else {
return self.request.eventLoop.makeFailedFuture(Abort(.internalServerError))
}
return self.read(path: path, fileSize: fileSize.intValue, chunkSize: chunkSize, onRead: onRead)
return self.read(
path: path,
fromOffset: 0,
byteCount:
fileSize.intValue,
Comment on lines +98 to +99
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: extra newline?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is finally ready - @siemensikkema @gwynne if you want to do a final review.

@Akazm is there anything else that needs addressing before we merge?

Hello 0xTim,
please don't ask me for this. I've been in trouble since last year, I'm unfortunately occupied since my last activity here (since Corona gave our company many oppurtinities).

Nevertheless: I'm happy to see that there's some progress here made by other people since this is my first contribution to Open Source ever!

chunkSize: chunkSize,
onRead: onRead
)
}

/// Generates a chunked `HTTPResponse` for the specified file. This method respects values in
Expand All @@ -109,7 +116,11 @@ public struct FileIO {
/// - req: `HTTPRequest` to parse `"If-None-Match"` header from.
/// - chunkSize: Maximum size for the file data chunks.
/// - returns: A `200 OK` response containing the file stream and appropriate headers.
public func streamFile(at path: String, chunkSize: Int = NonBlockingFileIO.defaultChunkSize) -> Response {
public func streamFile(
at path: String,
chunkSize: Int = NonBlockingFileIO.defaultChunkSize,
mediaType: HTTPMediaType? = nil
) -> Response {
// Get file attributes for this file.
guard
let attributes = try? FileManager.default.attributesOfItem(atPath: path),
Expand All @@ -119,6 +130,16 @@ public struct FileIO {
return Response(status: .internalServerError)
}

let contentRange: HTTPHeaders.Range?
if let rangeFromHeaders = request.headers.range {
if rangeFromHeaders.unit == .bytes && rangeFromHeaders.ranges.count == 1 {
contentRange = rangeFromHeaders
} else {
contentRange = nil
}
} else {
contentRange = nil
}
// Create empty headers array.
var headers: HTTPHeaders = [:]

Expand All @@ -133,18 +154,33 @@ public struct FileIO {

// Create the HTTP response.
let response = Response(status: .ok, headers: headers)

let offset: Int64
let byteCount: Int
if let contentRange = contentRange {
Akazm marked this conversation as resolved.
Show resolved Hide resolved
response.status = .partialContent
response.headers.add(name: .accept, value: contentRange.unit.serialize())
if let firstRange = contentRange.ranges.first {
let range = firstRange.asResponseContentRange(limit: fileSize)
response.headers.contentRange = HTTPHeaders.ContentRange(unit: contentRange.unit, range: range)
(offset, byteCount) = firstRange.asByteBufferBounds(withMaxSize: fileSize)
} else {
offset = 0
byteCount = fileSize
}
} else {
offset = 0
byteCount = fileSize
}
// Set Content-Type header based on the media type
// Only set Content-Type if file not modified and returned above.
if
let fileExtension = path.components(separatedBy: ".").last,
let type = HTTPMediaType.fileExtension(fileExtension)
let type = mediaType ?? HTTPMediaType.fileExtension(fileExtension)
{
response.headers.contentType = type
}

response.body = .init(stream: { stream in
self.read(path: path, fileSize: fileSize, chunkSize: chunkSize) { chunk in
self.read(path: path, fromOffset: offset, byteCount: byteCount, chunkSize: chunkSize) { chunk in
return stream.write(.buffer(chunk))
}.whenComplete { result in
switch result {
Expand All @@ -154,7 +190,7 @@ public struct FileIO {
stream.write(.end, promise: nil)
}
}
}, count: fileSize)
}, count: byteCount)

return response
}
Expand All @@ -163,15 +199,17 @@ public struct FileIO {
/// There may be use in publicizing this in the future for reads that must be async.
private func read(
path: String,
fileSize: Int,
fromOffset offset: Int64,
byteCount: Int,
chunkSize: Int,
onRead: @escaping (ByteBuffer) -> EventLoopFuture<Void>
) -> EventLoopFuture<Void> {
do {
let fd = try NIOFileHandle(path: path)
let done = self.io.readChunked(
fileHandle: fd,
byteCount: fileSize,
fromOffset: offset,
byteCount: byteCount,
chunkSize: chunkSize,
allocator: allocator,
eventLoop: self.request.eventLoop
Expand Down Expand Up @@ -209,3 +247,17 @@ public struct FileIO {
}
}
}

extension HTTPHeaders.Range.Value {

fileprivate func asByteBufferBounds(withMaxSize size: Int) -> (offset: Int64, byteCount: Int) {
switch self {
case .start(let value):
return (offset: numericCast(value), byteCount: size - value)
case .tail(let value):
return (offset: numericCast(size - value), byteCount: value)
case .within(let start, let end):
return (offset: numericCast(start), byteCount: end + 1)
}
}
}