aboutsummaryrefslogtreecommitdiff
path: root/Sources/swift-gopher
diff options
context:
space:
mode:
Diffstat (limited to 'Sources/swift-gopher')
-rw-r--r--Sources/swift-gopher/gopherHandler.swift75
-rw-r--r--Sources/swift-gopher/helpers.swift10
-rw-r--r--Sources/swift-gopher/server.swift2
3 files changed, 45 insertions, 42 deletions
diff --git a/Sources/swift-gopher/gopherHandler.swift b/Sources/swift-gopher/gopherHandler.swift
index be06b97..cd150e3 100644
--- a/Sources/swift-gopher/gopherHandler.swift
+++ b/Sources/swift-gopher/gopherHandler.swift
@@ -1,8 +1,8 @@
import ArgumentParser
import Foundation
+import GopherHelpers
import Logging
import NIO
-import GopherHelpers
final class GopherHandler: ChannelInboundHandler {
typealias InboundIn = ByteBuffer
@@ -37,9 +37,11 @@ final class GopherHandler: ChannelInboundHandler {
}
if let remoteAddress = context.remoteAddress {
- logger.info("Received request from \(remoteAddress) for '\(requestString.replacingOccurrences(of: "\r\n", with: "<GopherSequence>").replacingOccurrences(of: "\n", with: "<Linebreak>"))'")
+ logger.info(
+ "Received request from \(remoteAddress) for '\(requestString.replacingOccurrences(of: "\r\n", with: "<GopherSequence>").replacingOccurrences(of: "\n", with: "<Linebreak>"))'"
+ )
} else {
- logger.warning("Unable to retrieve remote address")
+ logger.warning("Unable to retrieve remote address")
}
let response = processGopherRequest(requestString)
@@ -110,7 +112,7 @@ final class GopherHandler: ChannelInboundHandler {
// Now check if there is still a prefix
if sanitizedPath.hasPrefix("/") {
- sanitizedPath = String(sanitizedPath.dropFirst())
+ sanitizedPath = String(sanitizedPath.dropFirst())
}
let full_path = base_dir.appendingPathComponent(sanitizedPath)
@@ -135,26 +137,26 @@ final class GopherHandler: ChannelInboundHandler {
var basePath = URL(fileURLWithPath: gopherdata_dir).path
if basePath.hasSuffix("/") {
- basePath = String(basePath.dropLast())
+ basePath = String(basePath.dropLast())
}
let fm = FileManager.default
do {
- print("Reading directory: \(path.path)")
- let itemsInDirectory = try fm.contentsOfDirectory(at: path, includingPropertiesForKeys: nil)
- for item in itemsInDirectory {
- let isDirectory = try item.resourceValues(forKeys: [.isDirectoryKey]).isDirectory ?? false
- let name = item.lastPathComponent
- if isDirectory {
- items.append(generateGopherItem(item_name: "1\(name)", item_path: item))
- } else {
- let fileType = getFileType(fileExtension: item.pathExtension)
- let gopherFileType = fileTypeToGopherItem(fileType: fileType)
- items.append(generateGopherItem(item_name: "\(gopherFileType)\(name)", item_path: item))
- }
+ print("Reading directory: \(path.path)")
+ let itemsInDirectory = try fm.contentsOfDirectory(at: path, includingPropertiesForKeys: nil)
+ for item in itemsInDirectory {
+ let isDirectory = try item.resourceValues(forKeys: [.isDirectoryKey]).isDirectory ?? false
+ let name = item.lastPathComponent
+ if isDirectory {
+ items.append(generateGopherItem(item_name: "1\(name)", item_path: item))
+ } else {
+ let fileType = getFileType(fileExtension: item.pathExtension)
+ let gopherFileType = fileTypeToGopherItem(fileType: fileType)
+ items.append(generateGopherItem(item_name: "\(gopherFileType)\(name)", item_path: item))
}
+ }
} catch {
- print("Error reading directory: \(path.path)")
+ print("Error reading directory: \(path.path)")
}
return items
}
@@ -171,14 +173,14 @@ final class GopherHandler: ChannelInboundHandler {
let gophermap_lines = gophermap_contents.components(separatedBy: "\n")
for originalLine in gophermap_lines {
// Only keep first 80 characters
- var line = String(originalLine)//.prefix(80)
- if "0123456789+gIT:;<dhprsPXi".contains(line.prefix(1)) && line.count > 1 {
+ var line = String(originalLine) //.prefix(80)
+ if "0123456789+gIT:;<dhprsPXi".contains(line.prefix(1)) && line.count > 1 {
if line.hasSuffix("\n") {
line = String(line.dropLast())
}
if line.prefix(1) == "i" {
gopherResponse.append("\(line)\t\terror.host\t1\r\n")
- continue
+ continue
}
let regex = try! NSRegularExpression(pattern: "\\t+| {2,}")
@@ -201,7 +203,7 @@ final class GopherHandler: ChannelInboundHandler {
}
if components.count < 3 {
- continue
+ continue
}
let item_name = components[0]
@@ -227,18 +229,17 @@ final class GopherHandler: ChannelInboundHandler {
// Append Search
if enableSearch {
- let search_line = "7Search Server\t/search\t\(gopherdata_host)\t\(gopherdata_port)\r\n"
- gopherResponse.append(search_line)
+ let search_line = "7Search Server\t/search\t\(gopherdata_host)\t\(gopherdata_port)\r\n"
+ gopherResponse.append(search_line)
}
// Append Server Info
gopherResponse.append(buildVersionStringResponse())
-
return gopherResponse.joined(separator: "")
}
- // TODO: Refactor
+ // TODO: Refactor
func performSearch(query: String) -> String {
// Really basic search implementation
@@ -326,28 +327,28 @@ final class GopherHandler: ChannelInboundHandler {
// Fix for "Gopher" (iOS) client sending an extra \n
if request.hasSuffix("\n\n") {
- request = String(request.dropLast())
+ request = String(request.dropLast())
}
- if request == "\r\n" { // Empty request
+ if request == "\r\n" { // Empty request
return .string(prepareGopherMenu(path: preparePath()))
}
// Again, fix for the iOS client. Might as well make my own client
if request.hasSuffix("\n") {
- request = String(request.dropLast())
+ request = String(request.dropLast())
}
if request.contains("\t") {
- if enableSearch {
- var searchQuery = request.components(separatedBy: "\t")[1]
- searchQuery = searchQuery.replacingOccurrences(of: "\r\n", with: "")
- return .string(performSearch(query: searchQuery.lowercased()))
- } else {
- return .string("3Search is disabled on this server.\r\n")
- }
+ if enableSearch {
+ var searchQuery = request.components(separatedBy: "\t")[1]
+ searchQuery = searchQuery.replacingOccurrences(of: "\r\n", with: "")
+ return .string(performSearch(query: searchQuery.lowercased()))
+ } else {
+ return .string("3Search is disabled on this server.\r\n")
+ }
}
-
+
//TODO: Potential Bug in Gopher implementation? curl gopher://localhost:8080/new_folder/ does not work but curl gopher://localhost:8080//new_folder/ works (tested with gopher://gopher.meulie.net//EFFector/ as well)
return requestHandler(path: preparePath(path: request))
}
diff --git a/Sources/swift-gopher/helpers.swift b/Sources/swift-gopher/helpers.swift
index f054d17..3c70b51 100644
--- a/Sources/swift-gopher/helpers.swift
+++ b/Sources/swift-gopher/helpers.swift
@@ -1,11 +1,13 @@
import Foundation
-let versionString = "generated and served by swift-gopher/1.0.0" // TODO: Handle automatic versioning
+let versionString = "generated and served by swift-gopher/1.0.0" // TODO: Handle automatic versioning
func buildVersionStringResponse() -> String {
- let repeatedString = "i" + String(repeating: "-", count: 80) + "\t\terror.host\t1\r\n"
- let versionResponseString = "i" + String(repeating: " ", count: 80 - versionString.count) + versionString + "\t\terror.host\t1\r\n"
- return "\(repeatedString)\(versionResponseString)"
+ let repeatedString = "i" + String(repeating: "-", count: 80) + "\t\terror.host\t1\r\n"
+ let versionResponseString =
+ "i" + String(repeating: " ", count: 80 - versionString.count) + versionString
+ + "\t\terror.host\t1\r\n"
+ return "\(repeatedString)\(versionResponseString)"
}
enum ResponseType {
diff --git a/Sources/swift-gopher/server.swift b/Sources/swift-gopher/server.swift
index c8cf97b..2f4a863 100644
--- a/Sources/swift-gopher/server.swift
+++ b/Sources/swift-gopher/server.swift
@@ -16,7 +16,7 @@ struct swiftGopher: ParsableCommand {
var port: Int = 8080
@Option(name: [.customShort("d"), .long], help: "Data directory to map")
var gopherDataDir: String = "./example-gopherdata"
- @Flag(help: "Disable full-text search feature")
+ @Flag(help: "Disable full-text search feature")
var disableSearch: Bool = false
@Flag(help: "Disable reading gophermap files to override automatic generation")
var disableGophermap: Bool = false