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

Log error in StaticFIleServer init if passed a non-directory path #1389

Merged
merged 11 commits into from Jan 22, 2019
21 changes: 19 additions & 2 deletions Sources/Kitura/staticFileServer/StaticFileServer.swift
Expand Up @@ -15,6 +15,7 @@
*/

import Foundation
import LoggerAPI

// MARK: StaticFileServer

Expand Down Expand Up @@ -101,9 +102,25 @@ open class StaticFileServer: RouterMiddleware {
/// - Parameter customResponseHeadersSetter: an object of a class that
/// implements `ResponseHeadersSetter` protocol providing a custom method to set
/// the headers of the response.
public init(path: String = "./public", options: Options = Options(),
public init?(path: String = "./public", options: Options = Options(),
kilnerm marked this conversation as resolved.
Show resolved Hide resolved
customResponseHeadersSetter: ResponseHeadersSetter? = nil) {
absoluteRootPath = StaticFileServer.ResourcePathHandler.getAbsolutePath(for: path)
let rootPathAbsolute = StaticFileServer.ResourcePathHandler.getAbsolutePath(for: path)
absoluteRootPath = rootPathAbsolute
// Check the supplied path is a directory and if not fail initialisation
kilnerm marked this conversation as resolved.
Show resolved Hide resolved
var isDirectory = ObjCBool(false)

let pathExists = FileManager.default.fileExists(atPath: absoluteRootPath, isDirectory: &isDirectory)
#if !os(Linux) || swift(>=4.1)
let isDirectoryBool = isDirectory.boolValue
#else
let isDirectoryBool = isDirectory
#endif
if !pathExists {
kilnerm marked this conversation as resolved.
Show resolved Hide resolved
Log.warning("StaticFileServer being initialised with invalid path: \(rootPathAbsolute)")
} else if !isDirectoryBool {
Log.error("StaticFileServer cannot be intialised with a file")
kilnerm marked this conversation as resolved.
Show resolved Hide resolved
return nil
}

let cacheOptions = options.cacheOptions
let cacheRelatedHeadersSetter =
Expand Down
26 changes: 18 additions & 8 deletions Tests/KituraTests/TestStaticFileServer.swift
Expand Up @@ -171,28 +171,28 @@ class TestStaticFileServer: KituraTest {
let router = Router(enableWelcomePage: enableWelcomePage)

// The route below ensures that the static file server does not prevent all routes being walked
router.all("/", middleware: StaticFileServer())
router.all("/", middleware: StaticFileServer()!)

var cacheOptions = StaticFileServer.CacheOptions(maxAgeCacheControlHeader: 2)
var options = StaticFileServer.Options(possibleExtensions: ["exe", "html"], cacheOptions: cacheOptions)
router.all("/qwer", middleware: StaticFileServer(path: "./Tests/KituraTests/TestStaticFileServer/", options:options, customResponseHeadersSetter: HeaderSetter()))
router.all("/qwer", middleware: StaticFileServer(path: "./Tests/KituraTests/TestStaticFileServer/", options:options, customResponseHeadersSetter: HeaderSetter())!)

cacheOptions = StaticFileServer.CacheOptions(addLastModifiedHeader: false, generateETag: false)
options = StaticFileServer.Options(serveIndexForDirectory: false, cacheOptions: cacheOptions)
router.all("/zxcv", middleware: StaticFileServer(path: "./Tests/KituraTests/TestStaticFileServer/", options:options))
router.all("/zxcv", middleware: StaticFileServer(path: "./Tests/KituraTests/TestStaticFileServer/", options:options)!)

options = StaticFileServer.Options(redirect: false)
let directoryURL = URL(fileURLWithPath: #file + "/../TestStaticFileServer").standardizedFileURL
router.all("/asdf", middleware: StaticFileServer(path: directoryURL.path, options:options))
router.all("/asdf", middleware: StaticFileServer(path: directoryURL.path, options:options)!)

options = StaticFileServer.Options(possibleExtensions: ["exe", "html"], cacheOptions: cacheOptions, acceptRanges: false)
router.all("/tyui", middleware: StaticFileServer(path: "./Tests/KituraTests/TestStaticFileServer/", options:options, customResponseHeadersSetter: HeaderSetter()))
router.all("/tyui", middleware: StaticFileServer(path: "./Tests/KituraTests/TestStaticFileServer/", options:options, customResponseHeadersSetter: HeaderSetter())!)

options = StaticFileServer.Options(serveIndexForDirectory: true, redirect: true, cacheOptions: cacheOptions)
router.route("/ghjk").all(middleware: StaticFileServer(path: "./Tests/KituraTests/TestStaticFileServer/", options: options))
router.route("/ghjk").all(middleware: StaticFileServer(path: "./Tests/KituraTests/TestStaticFileServer/", options: options)!)

options = StaticFileServer.Options(serveIndexForDirectory: true, redirect: true, cacheOptions: cacheOptions)
router.route("/opnm/:parameter").all(middleware: StaticFileServer(path: "./Tests/KituraTests/TestStaticFileServer/subfolder", options: options))
router.route("/opnm/:parameter").all(middleware: StaticFileServer(path: "./Tests/KituraTests/TestStaticFileServer/subfolder", options: options)!)

return router
}
Expand Down Expand Up @@ -271,7 +271,7 @@ class TestStaticFileServer: KituraTest {
}

func testAbsoluteRootPath() {
XCTAssertEqual(StaticFileServer(path: "/").absoluteRootPath, "/", "Absolute root path did not resolve to system root")
XCTAssertEqual(StaticFileServer(path: "/")!.absoluteRootPath, "/", "Absolute root path did not resolve to system root")
}

let indexHtmlContents = "<!DOCTYPE html><html><body><b>Index</b></body></html>" // contents of index.html
Expand Down Expand Up @@ -589,4 +589,14 @@ class TestStaticFileServer: KituraTest {
expectation.fulfill()
}
}

// Test the StaticFileServer can not be initialised when passed a file path rather than folder.
func testRejectFile() {
let filePath = #file
guard let _ = StaticFileServer(path: filePath) else {
return
}
XCTFail("Static file server initialised for file path")
return
}
}