This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Jan 17th, 2013  |  syntax: Scala  |  size: 0.93 KB  |  views: 626  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import java.net._
  2. import java.io._
  3. import scala.concurrent._
  4. import java.util.concurrent.Executors
  5.  
  6. object Main {
  7.   implicit val threadpool = ExecutionContext.fromExecutor(Executors.newCachedThreadPool())
  8.  
  9.   def handleConnection(socket: Socket) = {
  10.     val in = io.Source.fromInputStream(socket.getInputStream).getLines
  11.     val out = new OutputStreamWriter(socket.getOutputStream)
  12.     var close = false
  13.     while (!close && in.hasNext) {
  14.       val req = in.takeWhile(_!="").toList
  15.       close = !req.exists(_.equalsIgnoreCase("Connection: Keep-Alive"))
  16.    
  17.       out.write("HTTP/1.0 200 OK\r\n" +
  18.         (if (!close) "Connection: Keep-Alive\r\n" else "")+
  19.         "Content-Length: 0\r\n\r\n")
  20.       out.flush
  21.     }
  22.     socket.close
  23.   }
  24.  
  25.   def main(args: Array[String]) = {
  26.     val server = new ServerSocket(5555)
  27.     while (true) {
  28.       val connection = server.accept
  29.       Future(handleConnection(connection))
  30.     }
  31.   }
  32. }
clone this paste RAW Paste Data