Skip to content

Instantly share code, notes, and snippets.

@kgilmer
Created April 13, 2018 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kgilmer/133453b927423088a14f5f94b8cafa9a to your computer and use it in GitHub Desktop.
Save kgilmer/133453b927423088a14f5f94b8cafa9a to your computer and use it in GitHub Desktop.
import com.sun.net.httpserver.HttpExchange
import com.sun.net.httpserver.HttpServer
import java.io.File
import java.net.InetSocketAddress
import java.net.URI
fun main(args: Array<String>) {
val port = args[0].toIntOrNull() ?: 8080
val basePath = File(args.getOrElse(1, { System.getProperty("user.dir")!! }))
HttpServer.create(InetSocketAddress(port), 0).apply {
createContext("/") { httpExchange ->
println("New client request...")
val localFile = fetchFileOrNull(httpExchange, basePath)
when (localFile) {
null -> httpExchange.sendResponseHeaders(404, 0)
else -> {
httpExchange.sendResponseHeaders(200, localFile.length())
localFile.inputStream().copyTo(httpExchange.responseBody)
}
}
httpExchange.close()
}
start()
}
}
fun fetchFileOrNull(exchange: HttpExchange, basePath: File): File? =
when (validRequest(exchange)) {
true -> {
localFileFromRequestUri(basePath, exchange.requestURI).let { requestedFile ->
if (requestedFile.isFile) requestedFile else null
}
}
false -> null
}
fun localFileFromRequestUri(basePath: File, uri: URI): File {
val basePathElements = basePath.absolutePath.split(File.separator).filter { it.isNotEmpty() }.toMutableSet()
val clientPathElements = uri.path.substring(1).split(File.separatorChar)
val filteredPathElements = clientPathElements.mapNotNull {
if (!basePathElements.remove(it)) {
it
} else {
null
}
}.joinToString(separator = File.separator) { it }
return File(basePath, filteredPathElements)
}
fun validRequest(exchange: HttpExchange): Boolean =
exchange.requestHeaders.containsKey("User-Agent")
&& exchange.requestMethod == "GET"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment