1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
|
/*
This source file is part of the Swift.org open source project
Copyright (c) 2021-2024 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception
See https://swift.org/LICENSE.txt for license information
See https://swift.org/CONTRIBUTORS.txt for Swift project authors
*/
import Foundation
import SymbolKit
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif
#if os(Windows)
import WinSDK
#endif
fileprivate let slashCharSet = CharacterSet(charactersIn: "/")
/**
FileServer is a struct simulating a web server behavior to serve files.
It is useful to interface a custom schema with `WKWebView` via `WKURLSchemeHandler` or
`WebView` via a custom `URLProtocol`.
*/
public class FileServer {
/// The base URL of the server. Example: `http://www.example.com`.
public let baseURL: URL
/// The list of providers from which files are served.
private var providers: [String: FileServerProvider] = [:]
/**
Initialize a FileServer instance with a base URL.
- parameter baseURL: The base URL to use.
*/
public init(baseURL: URL) {
self.baseURL = baseURL.absoluteURL
}
/// Registers a `FileServerProvider` to a `FileServer` objects which can be used to provide content
/// to a local web page served by local content.
/// - Parameters:
/// - provider: An object conforming to `FileServerProvider`.
/// - subPath: The sub-path in which the `FileServerProvider` will be queried for content.
/// - Returns: A boolean indicating if the registration succeeded or not.
@discardableResult
public func register(provider: FileServerProvider, subPath: String = "/") -> Bool {
guard !subPath.isEmpty else { return false }
let trimmed = subPath.trimmingCharacters(in: slashCharSet)
providers[trimmed] = provider
return true
}
/**
Returns the data for a given URL.
*/
public func data(for url: URL) -> Data? {
let providerKey = providers.keys.sorted { (l, r) -> Bool in
l.count > r.count
}.filter { (path) -> Bool in
return url.path.trimmingCharacters(in: slashCharSet).hasPrefix(path)
}.first ?? "" //in case missing an exact match, get the root one
guard let provider = providers[providerKey] else {
fatalError("A provider has not been passed to a FileServer.")
}
return provider.data(for: url.path.trimmingCharacters(in: slashCharSet).removingPrefix(providerKey))
}
/**
Returns a tuple with a response and the given data.
- Parameter request: The request coming from a web client.
- Returns: The response and data which are going to be served to the client.
*/
public func response(to request: URLRequest) -> (URLResponse, Data?) {
guard let url = request.url else {
return (HTTPURLResponse(url: baseURL, statusCode: 400, httpVersion: "HTTP/1.1", headerFields: nil)!, nil)
}
var data: Data? = nil
let response: URLResponse
let mimeType: String
// We need to make sure that the path extension is for an actual file and not a symbol name which is a false positive
// like: "'...(_:)-6u3ic", that would be recognized as filename with the extension "(_:)-6u3ic". (rdar://71856738)
if url.pathExtension.isAlphanumeric && !url.lastPathComponent.isSwiftEntity {
data = self.data(for: url)
mimeType = FileServer.mimeType(for: url.pathExtension)
} else { // request is for a path, we need to fake a redirect here
if url.pathComponents.isEmpty {
xlog("Tried to load an invalid URL: \(url.absoluteString).\nFalling back to serve index.html.")
}
mimeType = "text/html"
data = self.data(for: baseURL.appendingPathComponent("/index.html"))
}
if let data {
response = URLResponse(url: url, mimeType: mimeType, expectedContentLength: data.count, textEncodingName: nil)
} else {
response = URLResponse(url: url, mimeType: nil, expectedContentLength: 0, textEncodingName: nil)
}
return (response, data)
}
/// Returns the MIME type based on file extension, best guess.
internal static func mimeType(for ext: String) -> String {
// RFC 2046 states in section 4.5.1:
// The "octet-stream" subtype is used to indicate that a body contains arbitrary binary data.
// https://stackoverflow.com/questions/1176022/unknown-file-type-mime
let defaultMimeType = "application/octet-stream"
#if os(macOS)
let unmanagedFileUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, ext as CFString, nil)
guard let fileUTI = unmanagedFileUTI?.takeRetainedValue() else {
return defaultMimeType
}
guard let mimeType = UTTypeCopyPreferredTagWithClass (fileUTI, kUTTagClassMIMEType)?.takeRetainedValue() else {
return defaultMimeType
}
return (mimeType as NSString) as String
#elseif os(Windows)
return ".\(ext)".withCString(encodedAs: UTF16.self) {
var pwszMimeOut: UnsafeMutablePointer<WCHAR>?
guard FindMimeFromData(nil, $0, nil, 0, nil, DWORD(FMFD_URLASFILENAME), &pwszMimeOut, 0) >= 0,
let pwszMimeOut else {
return defaultMimeType
}
defer { CoTaskMemFree(pwszMimeOut) }
return String(decodingCString: pwszMimeOut, as: UTF16.self)
}
#else
let mimeTypes = [
"html": "text/html",
"htm": "text/html",
"css": "text/css",
"png": "image/png",
"jpeg": "image/jpeg",
"jpg": "image/jpeg",
"svg": "image/svg+xml",
"gif": "image/gif",
"js": "application/javascript",
"json": "application/json"]
return mimeTypes[ext] ?? defaultMimeType
#endif
}
}
/**
A protocol used for serving content to a `FileServer`. The data can then come from multiple sources such as:
- disk
- remote source
- in memory storage
This abstraction lets a `FileServer` provide content from multiple types of sources at the same time.
*/
public protocol FileServerProvider {
/**
Retrieve the data linked to a given path based on the `baseURL`.
- parameter path: The path.
- returns: The data matching the url, if possible.
*/
func data(for path: String) -> Data?
}
public class FileSystemServerProvider: FileServerProvider {
private(set) var directoryURL: URL
public init?(directoryPath: String) {
guard FileManager.default.directoryExists(atPath: directoryPath) else {
return nil
}
self.directoryURL = URL(fileURLWithPath: directoryPath)
}
public func data(for path: String) -> Data? {
let finalURL = directoryURL.appendingPathComponent(path)
return try? Data(contentsOf: finalURL)
}
}
public class MemoryFileServerProvider: FileServerProvider {
/// Files to serve based on relative path.
private var files = [String: Data]()
public init() {}
/// Add a file to the file server.
///
/// - Parameters:
/// - path: The path to the file.
/// - data: The data for that file.
/// - Returns: `true` if the file was added successfully.
@discardableResult
public func addFile(path: String, data: Data) -> Bool {
guard !path.isEmpty else { return false }
var trimmed = path.trimmingCharacters(in: slashCharSet)
#if os(Windows)
trimmed = trimmed.replacingOccurrences(of: #"/"#, with: #"\"#)
#endif
files[trimmed] = data
return true
}
/// Retrieve the data that the server serves for the given path.
///
/// - Parameter path: The path to a file served by the server.
/// - Returns: The data for that file, if server by the server. Otherwise, `nil`.
public func data(for path: String) -> Data? {
var trimmed = path.trimmingCharacters(in: slashCharSet)
#if os(Windows)
trimmed = trimmed.replacingOccurrences(of: #"/"#, with: #"\"#)
#endif
return files[trimmed]
}
/// Adds files from the `source` directory to the `destination` directory in the file server.
///
/// - Parameters:
/// - source: The source directory to add files from.
/// - destination: The destination directory in the file server to add the files to.
/// - recursive: Whether or not to recursively add files from the source directory.
public func addFiles(inFolder source: String, inSubPath destination: String = "", recursive: Bool = true) {
var isDirectory: ObjCBool = false
guard FileManager.default.fileExists(atPath: source, isDirectory: &isDirectory) else { return }
guard isDirectory.boolValue else { return }
let trimmedSubPath = destination.trimmingCharacters(in: slashCharSet)
let enumerator = FileManager.default.enumerator(atPath: source)!
for file in enumerator {
guard let file = file as? String else { fatalError("Enumerator returned an unexpected type.") }
guard let data = try? Data(contentsOf: URL(fileURLWithPath: source).appendingPathComponent(file)) else { continue }
if recursive == false && file.contains("/") { continue } // skip if subfolder and recursive is disabled
addFile(path: "/\(trimmedSubPath)/\(file)", data: data)
}
}
/// Remove all files served by the server.
public func removeAllFiles() {
files.removeAll()
}
/// Removes all files served by the server matching a given subpath.
///
/// - Parameter directory: The path to a directory to remove
public func removeAllFiles(in directory: String) {
var trimmed = directory.trimmingCharacters(in: slashCharSet)
#if os(Windows)
trimmed = trimmed.appending(#"\"#)
#else
trimmed = trimmed.appending(#"/"#)
#endif
for key in files.keys where key.hasPrefix(trimmed) {
files.removeValue(forKey: key)
}
}
}
/// Checks whether the given string is a known entity definition which might interfere with the rendering engine while dealing with URLs.
fileprivate func isKnownEntityDefinition(_ identifier: String) -> Bool {
return SymbolGraph.Symbol.KindIdentifier.isKnownIdentifier(identifier)
}
fileprivate extension String {
/// Removes the prefix of a string.
func removingPrefix(_ prefix: String) -> String {
guard hasPrefix(prefix) else { return self }
return String(dropFirst(prefix.count))
}
/// Check that a given string is alphanumeric.
var isAlphanumeric: Bool {
return !self.isEmpty && self.rangeOfCharacter(from: CharacterSet.alphanumerics.inverted) == nil
}
/// Check that a given string is a Swift entity definition.
var isSwiftEntity: Bool {
let swiftEntityPattern = #"(?<=\-)swift\..*"#
if let range = range(of: swiftEntityPattern, options: .regularExpression, range: nil, locale: nil) {
let entityCheck = String(self[range])
return isKnownEntityDefinition(entityCheck)
}
return false
}
}
|