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

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

例外発生時は500を返す。
parent 7edb1f5b
...@@ -47,12 +47,23 @@ object DummyHttpServer { ...@@ -47,12 +47,23 @@ 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)
try {
t.getRequestHeaders.entrySet().foreach { ent => t.getRequestHeaders.entrySet().foreach { ent =>
ent.getValue.foreach { v => ent.getValue.foreach { v =>
printf("%s: %s\n", ent.getKey, v) printf("%s: %s\n", ent.getKey, v)
} }
} }
findFile(new File(t.getRequestURI.getPath).getName, new File(basedir)) match { val q = parseQuery(t.getRequestURI.getRawQuery)
if (q.nonEmpty) {
println("-- Query")
q.foreach { case (k, v) =>
printf("%s: %s\n", k, v)
}
}
findFile(new File(t.getRequestURI.getPath).getName, new File(basedir)).orElse {
q.get("Action").flatMap { action => findFile(action + ".xml", new File(basedir)) }
} match {
case Some(file) => case Some(file) =>
t.sendResponseHeaders(200, file.length) t.sendResponseHeaders(200, file.length)
val is = new BufferedInputStream(new FileInputStream(file)) val is = new BufferedInputStream(new FileInputStream(file))
...@@ -65,7 +76,12 @@ object DummyHttpServer { ...@@ -65,7 +76,12 @@ object DummyHttpServer {
printf(" -> 200 (%s)\n", file.getAbsolutePath) printf(" -> 200 (%s)\n", file.getAbsolutePath)
case _ => case _ =>
t.sendResponseHeaders(404, 0) t.sendResponseHeaders(404, 0)
println(" -> 404") 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