import java.net._
import java.io._
import scala.concurrent._
import java.util.concurrent.Executors
object Main {
implicit val threadpool = ExecutionContext.fromExecutor(Executors.newCachedThreadPool())
def handleConnection(socket: Socket) = {
val in = io.Source.fromInputStream(socket.getInputStream).getLines
val out = new OutputStreamWriter(socket.getOutputStream)
var close = false
while (!close && in.hasNext) {
val req = in.takeWhile(_!="").toList
close = !req.exists(_.equalsIgnoreCase("Connection: Keep-Alive"))
out.write("HTTP/1.0 200 OK\r\n" +
(if (!close) "Connection: Keep-Alive\r\n" else "")+
"Content-Length: 0\r\n\r\n")
out.flush
}
socket.close
}
def main(args: Array[String]) = {
val server = new ServerSocket(5555)
while (true) {
val connection = server.accept
Future(handleConnection(connection))
}
}
}