Skip to content

Instantly share code, notes, and snippets.

@hideshi
Last active January 4, 2016 08:09
Show Gist options
  • Save hideshi/8593433 to your computer and use it in GitHub Desktop.
Save hideshi/8593433 to your computer and use it in GitHub Desktop.
CGI library for Scala
package cgilib
import System._
import scala.io.Source._
import scala.collection.JavaConversions._
object CGI {
val GET = "GET"
val POST = "POST"
def envInfo():Map[String, String] = {getenv().toMap}
def params(method:String):Map[String, String] = {
val in = method match {
case m if m == GET => getenv("QUERY_STRING")
case m if m == POST => stdin.getLines.mkString("\n")
case _ => throw new IllegalArgumentException()
}
val list = in.split("&").toList.map(_.split("="))
(for(item <- list) yield {
item match {
case i if i.length == 1 => (i(0), "")
case i if i.length == 2 => (i(0), i(1))
}
}).toMap
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment