15
15
16
16
import java .io .IOException ;
17
17
import java .io .InputStream ;
18
- import java .io .PrintStream ;
18
+ import java .io .OutputStream ;
19
19
import java .net .InetSocketAddress ;
20
- import java .net .SocketAddress ;
21
20
22
21
import javax .xml .bind .JAXBContext ;
23
22
import javax .xml .bind .JAXBException ;
26
25
import javax .xml .transform .Source ;
27
26
import javax .xml .transform .sax .SAXSource ;
28
27
28
+ import com .sun .net .httpserver .Headers ;
29
+ import com .sun .net .httpserver .HttpContext ;
30
+ import com .sun .net .httpserver .HttpExchange ;
31
+ import com .sun .net .httpserver .HttpServer ;
32
+ import org .apache .http .protocol .HTTP ;
29
33
import org .apache .logging .log4j .LogManager ;
30
34
import org .apache .logging .log4j .Logger ;
31
- import org .simpleframework .http .Request ;
32
- import org .simpleframework .http .Response ;
33
- import org .simpleframework .http .core .Container ;
34
- import org .simpleframework .http .core .ContainerSocketProcessor ;
35
- import org .simpleframework .transport .SocketProcessor ;
36
- import org .simpleframework .transport .connect .Connection ;
37
- import org .simpleframework .transport .connect .SocketConnection ;
38
35
import org .xml .sax .InputSource ;
39
36
import org .xml .sax .SAXException ;
40
37
41
38
import com .intuit .tank .vm .agent .messages .StandaloneAgentRequest ;
42
- import com .intuit .tank .vm .api .enumerated .WatsAgentCommand ;
39
+ import com .intuit .tank .vm .api .enumerated .AgentCommand ;
43
40
44
- public class CommandListener implements Container {
41
+ public class CommandListener {
45
42
46
43
private static Logger LOG = LogManager .getLogger (CommandListener .class );
47
44
@@ -54,12 +51,11 @@ public synchronized static void startHttpServer(int port, StandaloneAgentStartup
54
51
if (!started ) {
55
52
agentStarter = standaloneAgentStartup ;
56
53
try {
57
- Container container = new CommandListener ( );
58
- SocketProcessor processor = new ContainerSocketProcessor ( container );
59
- Connection connection = new SocketConnection ( processor );
60
- SocketAddress address = new InetSocketAddress ( port );
54
+ HttpServer server = HttpServer . create ( new InetSocketAddress ( PORT ), 0 );
55
+ HttpContext context = server . createContext ( "/" );
56
+ context . setHandler ( CommandListener :: handleRequest );
57
+ server . start ( );
61
58
System .out .println ("Starting httpserver on port " + port );
62
- connection .connect (address );
63
59
started = true ;
64
60
} catch (IOException e ) {
65
61
LOG .error ("Error starting httpServer: " + e , e );
@@ -68,43 +64,38 @@ public synchronized static void startHttpServer(int port, StandaloneAgentStartup
68
64
}
69
65
}
70
66
71
- @ Override
72
- public void handle (Request req , Response response ) {
67
+ private static void handleRequest (HttpExchange exchange ) {
73
68
try {
74
- String msg = "unknown path" ;
69
+ String response = "unknown path" ;
75
70
int code = 200 ;
76
- String path = req . getPath ().getPath ();
77
- if (path .equals (WatsAgentCommand .request .getPath ())) {
78
- msg = "Requesting users " ;
79
- StandaloneAgentRequest agentRequest = getRequest (req . getInputStream ());
71
+ String path = exchange . getRequestURI ().getPath ();
72
+ if (path .equals (AgentCommand .request .getPath ())) {
73
+ response = "Requesting users " ;
74
+ StandaloneAgentRequest agentRequest = getRequest (exchange . getRequestBody ());
80
75
if (agentRequest == null ) {
81
- msg = "Invalid StandaloneAgentRequest." ;
76
+ response = "Invalid StandaloneAgentRequest." ;
82
77
code = 406 ;
83
78
} else if (agentRequest .getJobId () != null && agentRequest .getUsers () > 0 ) {
84
79
// launch the harness with the specified details.
85
80
agentStarter .startTest (agentRequest );
86
81
} else {
87
- msg = "invalid request." ;
82
+ response = "invalid request." ;
88
83
code = 400 ;
89
84
}
90
85
}
91
- long time = System .currentTimeMillis ();
92
- response .setCode (code );
93
- response .setContentType ("text/plain" );
94
- response .setDescription ("Intuit Tank Agent/2.3.0" );
95
- response .setDate ("Date" , time );
96
- response .setDate ("Last-Modified" , time );
97
-
98
- PrintStream body = response .getPrintStream ();
99
- body .println (msg );
100
- body .close ();
101
- } catch (Exception e ) {
86
+ exchange .getResponseHeaders ().set (HTTP .CONTENT_TYPE , "text/plain" );
87
+ exchange .getResponseHeaders ().set (HTTP .SERVER_HEADER ,"Intuit Tank Agent/3.0.1" );
88
+ exchange .sendResponseHeaders (code , response .length ());
89
+ OutputStream os = exchange .getResponseBody ();
90
+ os .write (response .getBytes ());
91
+ os .close ();
92
+ exchange .close ();
93
+ } catch (SAXException | ParserConfigurationException | IOException e ) {
102
94
LOG .error ("error sending response" );
103
- response .setCode (500 );
104
95
}
105
96
}
106
97
107
- private StandaloneAgentRequest getRequest (InputStream inputStream ) throws SAXException , ParserConfigurationException {
98
+ private static StandaloneAgentRequest getRequest (InputStream inputStream ) throws SAXException , ParserConfigurationException {
108
99
try {
109
100
//Source: https://www.owasp.org/index.php/XML_External_Entity_(XXE)_Prevention_Cheat_Sheet#Unmarshaller
110
101
SAXParserFactory spf = SAXParserFactory .newInstance ();
0 commit comments