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
9 changes: 7 additions & 2 deletions Sources/Kitura/staticFileServer/StaticFileServer.swift
Expand Up @@ -15,6 +15,7 @@
*/

import Foundation
import LoggerAPI

// MARK: StaticFileServer

Expand Down Expand Up @@ -103,7 +104,8 @@ open class StaticFileServer: RouterMiddleware {
/// the headers of the response.
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)

Expand All @@ -113,7 +115,10 @@ open class StaticFileServer: RouterMiddleware {
#else
let isDirectoryBool = isDirectory
#endif
guard pathExists, isDirectoryBool else {
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
}

Expand Down