From e46af81197d9510212476685ee4f96e0a7a3ead5 Mon Sep 17 00:00:00 2001 From: Julien Chaumond Date: Fri, 11 Dec 2015 13:27:29 +0100 Subject: [PATCH 1/2] Linux: Range support: get rid of the NSRegularExpression dependency --- Sources/Swifter/HttpHandlers.swift | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/Sources/Swifter/HttpHandlers.swift b/Sources/Swifter/HttpHandlers.swift index 46a0c92c..943ae77b 100644 --- a/Sources/Swifter/HttpHandlers.swift +++ b/Sources/Swifter/HttpHandlers.swift @@ -8,7 +8,7 @@ import Foundation public class HttpHandlers { - private static let rangeExpression = try! NSRegularExpression(pattern: "bytes=(\\d*)-(\\d*)", options: .CaseInsensitive) + private static let rangePrefix = "bytes=" public class func directory(dir: String) -> (HttpRequest -> HttpResponse) { return { request in @@ -25,12 +25,18 @@ public class HttpHandlers { if let rangeHeader = request.headers["range"] { - guard let match = rangeExpression.matchesInString(rangeHeader, options: .Anchored, range: NSRange(location: 0, length: rangeHeader.characters.count)).first where match.numberOfRanges == 3 else { + guard rangeHeader.hasPrefix(HttpHandlers.rangePrefix) else { return HttpResponse.BadRequest } - let startStr = (rangeHeader as NSString).substringWithRange(match.rangeAtIndex(1)) - let endStr = (rangeHeader as NSString).substringWithRange(match.rangeAtIndex(2)) + let rangeString = rangeHeader.substringFromIndex(rangeHeader.startIndex.advancedBy(HttpHandlers.rangePrefix.characters.count)) + let rangeStringExploded = rangeString.split("-") + guard rangeStringExploded.count == 2 else { + return HttpResponse.BadRequest + } + + let startStr = rangeStringExploded[0] + let endStr = rangeStringExploded[1] guard let start = Int(startStr), end = Int(endStr) else { var array = [UInt8](count: fileBody.length, repeatedValue: 0) From 812d50af702da0784bd0ad7d97d746c3b81e9e06 Mon Sep 17 00:00:00 2001 From: Julien Chaumond Date: Fri, 11 Dec 2015 13:33:28 +0100 Subject: [PATCH 2/2] Linux: Range support: get rid of the NSRegularExpression dependency (2) --- Sources/Swifter/HttpHandlers.swift | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Sources/Swifter/HttpHandlers.swift b/Sources/Swifter/HttpHandlers.swift index 943ae77b..3f5a788e 100644 --- a/Sources/Swifter/HttpHandlers.swift +++ b/Sources/Swifter/HttpHandlers.swift @@ -29,7 +29,11 @@ public class HttpHandlers { return HttpResponse.BadRequest } +#if os(Linux) + let rangeString = rangeHeader.substringFromIndex(HttpHandlers.rangePrefix.characters.count) +#else let rangeString = rangeHeader.substringFromIndex(rangeHeader.startIndex.advancedBy(HttpHandlers.rangePrefix.characters.count)) +#endif let rangeStringExploded = rangeString.split("-") guard rangeStringExploded.count == 2 else { return HttpResponse.BadRequest