Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
C
CTF
Project
Project
Details
Activity
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
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Grzegorz
CTF
Commits
c900bd03
Commit
c900bd03
authored
Apr 10, 2016
by
Grzegorz Pietrusza
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
forward post body
parent
0a612e65
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
42 additions
and
13 deletions
+42
-13
TaskRequestContextBuilder.java
...rakyournet/ctf/beans/tasks/TaskRequestContextBuilder.java
+10
-1
WebTask.java
.../com/telephoners/krakyournet/ctf/beans/tasks/WebTask.java
+32
-12
No files found.
service/src/main/java/com/telephoners/krakyournet/ctf/beans/tasks/TaskRequestContextBuilder.java
View file @
c900bd03
...
...
@@ -5,6 +5,7 @@ import com.google.inject.Singleton;
import
com.telephoners.krakyournet.ctf.beans.User
;
import
com.telephoners.krakyournet.ctf.core.ApplicationConfiguration
;
import
com.telephoners.krakyournet.ctf.helpers.StreamUtils
;
import
org.eclipse.jetty.http.HttpMethod
;
import
org.glassfish.jersey.server.ContainerRequest
;
import
javax.ws.rs.container.ContainerRequestContext
;
...
...
@@ -55,10 +56,18 @@ public class TaskRequestContextBuilder
}
taskRequestContext
.
withPath
(
fullPath
);
if
(
httpMethod
.
equals
(
"POST"
))
{
if
(
httpMethod
.
equals
(
"GET"
))
{
taskRequestContext
.
withHttpMethod
(
HttpMethod
.
GET
);
}
else
if
(
httpMethod
.
equals
(
"POST"
))
{
taskRequestContext
.
withHttpMethod
(
HttpMethod
.
POST
);
String
body
=
StreamUtils
.
readStream
(
containerRequestContext
.
getEntityStream
());
taskRequestContext
.
withBody
(
body
);
}
else
{
throw
new
IllegalStateException
(
"Invalid http method"
);
}
Map
<
String
,
String
>
proxiedHeaders
=
extractProxiedHeaders
(
containerRequestContext
.
getHeaders
());
taskRequestContext
.
withHeaders
(
proxiedHeaders
);
...
...
service/src/main/java/com/telephoners/krakyournet/ctf/beans/tasks/WebTask.java
View file @
c900bd03
...
...
@@ -4,11 +4,17 @@ import com.fasterxml.jackson.annotation.JsonInclude;
import
com.telephoners.krakyournet.ctf.beans.Flag
;
import
com.telephoners.krakyournet.ctf.helpers.StreamUtils
;
import
org.apache.http.Header
;
import
org.apache.http.client.entity.UrlEncodedFormEntity
;
import
org.apache.http.client.methods.CloseableHttpResponse
;
import
org.apache.http.client.methods.HttpGet
;
import
org.apache.http.client.methods.HttpPost
;
import
org.apache.http.entity.BasicHttpEntity
;
import
org.apache.http.entity.ByteArrayEntity
;
import
org.apache.http.entity.HttpEntityWrapper
;
import
org.apache.http.impl.client.CloseableHttpClient
;
import
org.apache.http.impl.client.HttpClients
;
import
org.apache.http.message.BasicHeader
;
import
org.eclipse.jetty.http.HttpMethod
;
import
org.mongodb.morphia.annotations.Entity
;
import
java.io.IOException
;
...
...
@@ -34,15 +40,17 @@ public class WebTask extends Task
}
//todo: avoid converting in both ways
private
Map
<
String
,
String
>
buildHeadersMap
(
Header
[]
headers
)
{
return
Stream
.
of
(
headers
)
.
collect
(
Collectors
.
toMap
(
Header:
:
getName
,
Header:
:
getValue
));
private
Map
<
String
,
String
>
buildHeadersMap
(
Header
[]
headers
)
{
return
Stream
.
of
(
headers
)
.
collect
(
Collectors
.
toMap
(
Header:
:
getName
,
Header:
:
getValue
));
}
private
Header
[]
buildHeadersArray
(
Map
<
String
,
String
>
headers
)
{
private
Header
[]
buildHeadersArray
(
Map
<
String
,
String
>
headers
)
{
List
<
BasicHeader
>
headersList
=
headers
.
entrySet
().
stream
()
//todo: dirty casting
.
map
(
headerEntry
->
new
BasicHeader
(
headerEntry
.
getKey
(),
headerEntry
.
getValue
()))
.
collect
(
Collectors
.
toList
());
...
...
@@ -54,7 +62,7 @@ public class WebTask extends Task
public
TaskResponse
getTaskResponse
(
TaskRequestContext
taskRequestContext
)
throws
IOException
{
String
url
=
getUrl
()
+
taskRequestContext
.
getPath
();
CloseableHttpResponse
response
=
proxyRequest
(
url
,
taskRequestContext
.
getHeaders
()
);
CloseableHttpResponse
response
=
proxyRequest
(
url
,
taskRequestContext
);
String
text
=
StreamUtils
.
readStream
(
response
.
getEntity
().
getContent
());
return
new
TaskResponse
(
text
,
buildHeadersMap
(
response
.
getAllHeaders
()));
}
...
...
@@ -69,11 +77,23 @@ public class WebTask extends Task
this
.
url
=
url
;
}
private
CloseableHttpResponse
proxyRequest
(
String
url
,
Map
<
String
,
String
>
headers
)
throws
IOException
private
CloseableHttpResponse
proxyRequest
(
String
url
,
TaskRequestContext
taskRequestContext
)
throws
IOException
{
CloseableHttpClient
httpClient
=
HttpClients
.
createDefault
();
HttpGet
httpget
=
new
HttpGet
(
url
);
httpget
.
setHeaders
(
buildHeadersArray
(
headers
));
return
httpClient
.
execute
(
httpget
);
HttpMethod
httpMethod
=
taskRequestContext
.
getHttpMethod
();
if
(
httpMethod
.
equals
(
HttpMethod
.
GET
))
{
HttpGet
httpget
=
new
HttpGet
(
url
);
httpget
.
setHeaders
(
buildHeadersArray
(
taskRequestContext
.
getHeaders
()));
return
httpClient
.
execute
(
httpget
);
}
else
if
(
httpMethod
.
equals
(
HttpMethod
.
POST
))
{
HttpPost
httpPost
=
new
HttpPost
(
url
);
httpPost
.
setHeaders
(
buildHeadersArray
(
taskRequestContext
.
getHeaders
()));
httpPost
.
setEntity
(
new
ByteArrayEntity
(
taskRequestContext
.
getBody
().
getBytes
(
"UTF-8"
)));
return
httpClient
.
execute
(
httpPost
);
}
else
{
throw
new
IllegalStateException
(
"Invalid http method"
);
}
}
}
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