/
webserver.scala
48 lines (40 loc) · 1.45 KB
/
webserver.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package webserver
import java.net.{ServerSocket, Socket}
import java.io.OutputStream
import java.util.Scanner
import scala.util.Try
object html:
def page(body: String): String = //minimal web page
s"""<!DOCTYPE html>
|<html>
|<head><meta charset="UTF-8"><title>Min Sörver</title></head>
|<body>
|$body
|</body>
|</html>
""".stripMargin
def header(length: Int): String = //standardized header of reply
s"HTTP/1.0 200 OK\nContent-length: $length\n" +
"Content-type: text/html\n\n"
object start:
def handleRequest(cmd: String, uri: String, socket: Socket): Unit =
val os = socket.getOutputStream
val response = html.page("Baklänges: " + uri.reverse)
os.write(html.header(response.size).getBytes("UTF-8"))
os.write(response.getBytes("UTF-8"))
os.close
socket.close
end handleRequest
def serverLoop(server: ServerSocket): Unit =
println(s"http://localhost:${server.getLocalPort}/hej")
while true do
Try {
var socket = server.accept // blocks thread until connect
val scan = new Scanner(socket.getInputStream, "UTF-8")
val (cmd, uri) = (scan.next, scan.next)
println(s"Request: $cmd $uri")
handleRequest(cmd, uri, socket)
}.recover{ case e: Throwable => s"Connection failed: $e" }
def main(args: Array[String]) =
val port = Try{ args(0).toInt }.getOrElse(8089)
serverLoop(new ServerSocket(port))