package net.usebox.gemini.server.handlers import java.net.URI import java.nio.file.{Files, Path} import scala.util.Try import net.usebox.gemini.server.{Response, ServiceConf} abstract class ProtocolHandler(conf: ServiceConf) { private val defaultMimeType = conf.defaultMimeType val vHosts = conf.virtualHosts def guessMimeType(path: Path, params: Option[String]): String = conf.mimeTypes.fold { List(".gmi", ".gemini") .find(path.toString().endsWith(_)) .fold { Try(Option(Files.probeContentType(path))).toOption.flatten match { case Some(mime) => mime case _ => defaultMimeType } }(_ => "text/gemini") } { types => types .find { case (t, exts) => exts.exists(path.toString().endsWith(_)) } .fold(defaultMimeType) { case (t, _) => t } } match { case mime @ "text/gemini" => params.fold(mime)(p => s"$mime; ${p.stripMargin(';').trim()}") case mime => mime } def handle(req: String, uri: URI, remoteAddr: String): Response }