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

More Linux work #74

Merged
merged 3 commits into from
Dec 11, 2015
Merged
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
3 changes: 2 additions & 1 deletion Sources/Swifter/DemoServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public func demoServer(publicDir: String?) -> HttpServer {
}

server["/json"] = { request in
return .OK(.Json(["posts": [["id": 1, "message": "hello world"], ["id": 2, "message": "sample message"]], "new_updates": false]))
let jsonObject: NSDictionary = [NSString(string: "foo"): NSNumber(int: 3), NSString(string: "bar"): NSString(string: "baz")]
return .OK(.Json(jsonObject))
}

server["/redirect"] = { request in
Expand Down
10 changes: 1 addition & 9 deletions Sources/Swifter/HttpHandlers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ public class HttpHandlers {

private static let rangeExpression = try! NSRegularExpression(pattern: "bytes=(\\d*)-(\\d*)", options: .CaseInsensitive)

private static let cache = NSCache()

public class func directory(dir: String) -> (HttpRequest -> HttpResponse) {
return { request in

Expand All @@ -21,16 +19,10 @@ public class HttpHandlers {

let filesPath = dir + "/" + localPath.1

let cachedBody = cache.objectForKey(filesPath) as? NSData

guard let fileBody = cachedBody ?? NSData(contentsOfFile: filesPath) else {
guard let fileBody = NSData(contentsOfFile: filesPath) else {
return HttpResponse.NotFound
}

if cachedBody == nil {
cache.setObject(fileBody, forKey: filesPath)
}

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 {
Expand Down
4 changes: 4 additions & 0 deletions Sources/Swifter/HttpRouter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ public class HttpRouter {
}
}
if patternToken.characters.first == ":" {
#if os(Linux)
params[patternToken.substringFromIndex(1)] = valueToken
#else
params[patternToken.substringFromIndex(patternToken.characters.startIndex.successor())] = valueToken
#endif
} else {
if patternToken != valueToken {
return nil
Expand Down