Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
D
dummyhttpserver
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Yoshiharu Hirose
dummyhttpserver
Commits
3c4706cd
Commit
3c4706cd
authored
Apr 06, 2016
by
Yoshiharu Hirose
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ファイルがなく、Actionが指定されていたら(Action).xmlを返す。
例外発生時は500を返す。
parent
7edb1f5b
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
54 additions
and
18 deletions
+54
-18
DummyHttpServer.scala
src/main/scala/DummyHttpServer.scala
+54
-18
No files found.
src/main/scala/DummyHttpServer.scala
View file @
3c4706cd
...
...
@@ -47,25 +47,41 @@ object DummyHttpServer {
def
handle
(
t
:
HttpExchange
)
:
Unit
=
{
println
(
"------------------------------------------------------------"
)
printf
(
"%s %s\n"
,
t
.
getRequestMethod
,
t
.
getRequestURI
)
t
.
getRequestHeaders
.
entrySet
().
foreach
{
ent
=>
ent
.
getValue
.
foreach
{
v
=>
printf
(
"%s: %s\n"
,
ent
.
getKey
,
v
)
try
{
t
.
getRequestHeaders
.
entrySet
().
foreach
{
ent
=>
ent
.
getValue
.
foreach
{
v
=>
printf
(
"%s: %s\n"
,
ent
.
getKey
,
v
)
}
}
}
findFile
(
new
File
(
t
.
getRequestURI
.
getPath
).
getName
,
new
File
(
basedir
))
match
{
case
Some
(
file
)
=>
t
.
sendResponseHeaders
(
200
,
file
.
length
)
val
is
=
new
BufferedInputStream
(
new
FileInputStream
(
file
))
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
)
println
(
" -> 404"
)
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
)
=>
t
.
sendResponseHeaders
(
200
,
file
.
length
)
val
is
=
new
BufferedInputStream
(
new
FileInputStream
(
file
))
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
()
}
...
...
@@ -90,5 +106,25 @@ object DummyHttpServer {
}
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
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment