Commit 3c4706cd authored by Yoshiharu Hirose's avatar Yoshiharu Hirose

ファイルがなく、Actionが指定されていたら(Action).xmlを返す。

例外発生時は500を返す。
parent 7edb1f5b
...@@ -47,25 +47,41 @@ object DummyHttpServer { ...@@ -47,25 +47,41 @@ object DummyHttpServer {
def handle(t: HttpExchange): Unit = { def handle(t: HttpExchange): Unit = {
println("------------------------------------------------------------") println("------------------------------------------------------------")
printf("%s %s\n", t.getRequestMethod, t.getRequestURI) printf("%s %s\n", t.getRequestMethod, t.getRequestURI)
t.getRequestHeaders.entrySet().foreach { ent => try {
ent.getValue.foreach { v => t.getRequestHeaders.entrySet().foreach { ent =>
printf("%s: %s\n", ent.getKey, v) ent.getValue.foreach { v =>
printf("%s: %s\n", ent.getKey, v)
}
} }
} val q = parseQuery(t.getRequestURI.getRawQuery)
findFile(new File(t.getRequestURI.getPath).getName, new File(basedir)) match { if (q.nonEmpty) {
case Some(file) => println("-- Query")
t.sendResponseHeaders(200, file.length) q.foreach { case (k, v) =>
val is = new BufferedInputStream(new FileInputStream(file)) printf("%s: %s\n", k, v)
val os = t.getResponseBody }
val buf = new Array[Byte](file.length.toInt) }
is.read(buf)
os.write(buf) findFile(new File(t.getRequestURI.getPath).getName, new File(basedir)).orElse {
os.close() q.get("Action").flatMap { action => findFile(action + ".xml", new File(basedir)) }
is.close() } match {
printf(" -> 200 (%s)\n", file.getAbsolutePath) case Some(file) =>
case _ => t.sendResponseHeaders(200, file.length)
t.sendResponseHeaders(404, 0) val is = new BufferedInputStream(new FileInputStream(file))
println(" -> 404") val os = t.getResponseBody
val buf = new Array[Byte](file.length.toInt)
is.read(buf)
os.write(buf)
os.close()
is.close()
printf(" -> 200 (%s)\n", file.getAbsolutePath)
case _ =>
t.sendResponseHeaders(404, 0)
printf(" -> 404\n")
}
} catch {
case e: Throwable =>
t.sendResponseHeaders(500, 0)
printf(" -> 500 (%s)\n", e.toString)
} }
t.close() t.close()
} }
...@@ -90,5 +106,25 @@ object DummyHttpServer { ...@@ -90,5 +106,25 @@ object DummyHttpServer {
} }
None None
} }
/**
* クエリ文字列解析 (簡易)
*
* @param query クエリ文字列
* @return Map
*/
def parseQuery(query: String): Map[String, String] = {
val m = Map.newBuilder[String, String]
if (query != null) {
query.split("&").foreach {
_.split("=") match {
case Array(a) => m += a -> ""
case Array(a, b) => m += a -> b
}
}
}
m.result
}
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment