Repository: vapor/vapor
File: Sources/Vapor/HTTP/Headers/HTTPHeaders+ContentRange.swift (Swift)

Code before:
        let splitResult = header.split(separator: " ", omittingEmptySubsequences: false)
        guard splitResult.count == 2 else {
            return nil
        }
        let (unitStr, rangeStr) = (splitResult[0], splitResult[1])
        let lowerCasedUnit = unitStr.trimmingCharacters(in: .whitespacesAndNewlines).lowercased()
        guard let contentRange = ResponseContentRange.from(responseStr: rangeStr) else {
            return nil
        }
        self.unit = lowerCasedUnit == "bytes"
            ? RangeUnit.bytes
            : RangeUnit.custom(value: lowerCasedUnit)
        self.range = contentRange
    }
    
    func serialize() -> String {
        return "\(unit.serialize()) \(range.serialize())"
    }
}

public enum ResponseContentRange : Equatable {
    case within(start: Int, end: Int)
    case withinWithLimit(start: Int, end: Int, limit: Int)
    case any(size: Int)
    
    public static func from<T>(responseStr: T) -> ResponseContentRange? where T : StringProtocol {
        let ranges = responseStr.split(separator: "-", omittingEmptySubsequences: false)
            .map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }
        guard ranges.count <= 2 else {
            return nil
        }
        if ranges.count == 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)
        }
        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 {

Review comment (bug):
This should be using the `parseDirectives` methods. This takes care of a lot of weird edge cases in HTTP header values automatically. 

Again HTTPHeaders+ContentDisposition is a good example to follow here: https://github.com/vapor/vapor/blob/master/Sources/Vapor/HTTP/Headers/HTTPHeaders%2BContentDisposition.swift#L20

Code after:
        
        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())
        }