Advertisement
Guest User

Untitled

a guest
Jan 17th, 2013
665
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.93 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement