there is a bug in httpcapture
This commit is contained in:
parent
b1422b22a1
commit
e569cb0a0f
|
@ -1,6 +1,5 @@
|
|||
package org.bench4q.master;
|
||||
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
MasterServer masterServer = new MasterServer(8080);
|
||||
|
|
|
@ -57,7 +57,7 @@ public class MasterServer {
|
|||
"classpath*:org/bench4q/master/config/application-context.xml");
|
||||
this.getServer().setHandler(servletContextHandler);
|
||||
this.getServer().start();
|
||||
this.getAgentPoolService().loadAgentPoolFromDB();
|
||||
//this.getAgentPoolService().loadAgentPoolFromDB();
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
|
|
|
@ -48,10 +48,9 @@ public class RecordPortController extends BaseController {
|
|||
LocalPort localPort = new LocalPort();
|
||||
localPort.setPort(port);
|
||||
localPort.setInUse(false);
|
||||
if (this.getPortPoolService().getScriptPortPool().put(port, localPort) == null) {
|
||||
return setOrganizePortResponseModel(false,
|
||||
"add to local pool fails 1");
|
||||
}
|
||||
|
||||
this.getPortPoolService().getScriptPortPool().put(port, localPort);
|
||||
|
||||
if (!this.getPortPoolService().addPortToDBPool(port)) {
|
||||
return setOrganizePortResponseModel(false, "add to DB pool fails 2");
|
||||
}
|
||||
|
|
|
@ -67,6 +67,9 @@ public class RecordScriptController extends BaseController {
|
|||
synchronized (PORT_LOCK) {
|
||||
// TODO:
|
||||
LocalPort localPort = this.getPortPoolService().getAPortNotInUse();
|
||||
if (localPort == null) {
|
||||
return returnResponseModel(false, "port is in use!");
|
||||
}
|
||||
this.setPortForRecord(localPort.getPort());
|
||||
localPort.setInUse(true);
|
||||
}
|
||||
|
|
|
@ -14,201 +14,183 @@ import java.net.URL;
|
|||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
|
||||
public class RequestHandler implements Runnable{
|
||||
public class RequestHandler implements Runnable {
|
||||
|
||||
private static final Log log = LogFactory.getLog(RequestHandler.class);
|
||||
|
||||
private Config config = Config.getConfig();
|
||||
private InputStream clientIn;
|
||||
private InputStream serverIn;
|
||||
private OutputStream clientOut;
|
||||
private OutputStream serverOut;
|
||||
private HttpRequestHeader header;
|
||||
private ProxyServer proxyServer;
|
||||
private Socket clientSocket;
|
||||
private Socket serverSocket;
|
||||
private ByteArrayOutputStream buffer;
|
||||
private static Object mutex = new Object();
|
||||
private Config config = Config.getConfig();
|
||||
private InputStream clientIn;
|
||||
private InputStream serverIn;
|
||||
private OutputStream clientOut;
|
||||
private OutputStream serverOut;
|
||||
private HttpRequestHeader header;
|
||||
private ProxyServer proxyServer;
|
||||
private Socket clientSocket;
|
||||
private Socket serverSocket;
|
||||
private ByteArrayOutputStream buffer;
|
||||
private static Object mutex = new Object();
|
||||
|
||||
RequestHandler(ProxyServer proxyServer, Socket s)
|
||||
{
|
||||
assert (s != null);
|
||||
this.clientSocket = s;
|
||||
this.buffer = new ByteArrayOutputStream();
|
||||
this.proxyServer = proxyServer;
|
||||
}
|
||||
RequestHandler(ProxyServer proxyServer, Socket s) {
|
||||
assert (s != null);
|
||||
this.clientSocket = s;
|
||||
this.buffer = new ByteArrayOutputStream();
|
||||
this.proxyServer = proxyServer;
|
||||
}
|
||||
|
||||
private void initClientServerConnections(Socket s)
|
||||
throws Throwable
|
||||
{
|
||||
this.clientIn = new BufferedInputStream(s.getInputStream());
|
||||
this.clientOut = s.getOutputStream();
|
||||
try {
|
||||
this.header = new HttpRequestHeader(this.clientIn);
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
log.info("truncated request from browser: " + e.getMessage());
|
||||
throw new Utils.SilentException();
|
||||
}
|
||||
if (!this.header.url.startsWith("http"))
|
||||
throw new Utils.UserException("MaxQ only supports the HTTP protocol.");
|
||||
URL url = new URL(this.header.url);
|
||||
private void initClientServerConnections(Socket s) throws Throwable {
|
||||
this.clientIn = new BufferedInputStream(s.getInputStream());
|
||||
this.clientOut = s.getOutputStream();
|
||||
try {
|
||||
this.header = new HttpRequestHeader(this.clientIn);
|
||||
} catch (IOException e) {
|
||||
log.info("truncated request from browser: " + e.getMessage());
|
||||
throw new Utils.SilentException();
|
||||
}
|
||||
if (!this.header.url.startsWith("http"))
|
||||
throw new Utils.UserException(
|
||||
"MaxQ only supports the HTTP protocol.");
|
||||
URL url = new URL(this.header.url);
|
||||
|
||||
Config.ProxySettings proxy = this.config.getProxySettings();
|
||||
int port;
|
||||
String host;
|
||||
if (proxy != null) {
|
||||
host = proxy.host;
|
||||
port = proxy.port;
|
||||
} else {
|
||||
host = url.getHost();
|
||||
port = url.getPort();
|
||||
}
|
||||
Config.ProxySettings proxy = this.config.getProxySettings();
|
||||
int port;
|
||||
String host;
|
||||
if (proxy != null) {
|
||||
host = proxy.host;
|
||||
port = proxy.port;
|
||||
} else {
|
||||
host = url.getHost();
|
||||
port = url.getPort();
|
||||
}
|
||||
|
||||
if (port < 1)
|
||||
port = 80;
|
||||
try {
|
||||
this.serverSocket = new Socket(InetAddress.getByName(host), port);
|
||||
} catch (ConnectException e) {
|
||||
String msg = "Cannot connect to ";
|
||||
if (proxy != null)
|
||||
msg = msg + "proxy server ";
|
||||
msg = msg + host;
|
||||
if (port != 80)
|
||||
msg = msg + " on port " + Integer.toString(port);
|
||||
throw new Utils.UserException(msg + ".");
|
||||
}
|
||||
try {
|
||||
this.serverIn = this.serverSocket.getInputStream();
|
||||
this.serverOut = this.serverSocket.getOutputStream();
|
||||
} catch (Throwable e) {
|
||||
this.serverSocket.close();
|
||||
this.serverSocket = null;
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
if (port < 1)
|
||||
port = 80;
|
||||
try {
|
||||
this.serverSocket = new Socket(InetAddress.getByName(host), port);
|
||||
} catch (ConnectException e) {
|
||||
String msg = "Cannot connect to ";
|
||||
if (proxy != null)
|
||||
msg = msg + "proxy server ";
|
||||
msg = msg + host;
|
||||
if (port != 80)
|
||||
msg = msg + " on port " + Integer.toString(port);
|
||||
throw new Utils.UserException(msg + ".");
|
||||
}
|
||||
try {
|
||||
this.serverIn = this.serverSocket.getInputStream();
|
||||
this.serverOut = this.serverSocket.getOutputStream();
|
||||
} catch (Throwable e) {
|
||||
this.serverSocket.close();
|
||||
this.serverSocket = null;
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
private String stripProxyInfoFromRequestHeader()
|
||||
throws MalformedURLException
|
||||
{
|
||||
String res = "";
|
||||
String origUrl = this.header.url;
|
||||
URL url = new URL(origUrl);
|
||||
this.header.url = url.getFile();
|
||||
res = this.header.toString();
|
||||
this.header.url = origUrl;
|
||||
return res;
|
||||
}
|
||||
private String stripProxyInfoFromRequestHeader()
|
||||
throws MalformedURLException {
|
||||
String res = "";
|
||||
String origUrl = this.header.url;
|
||||
URL url = new URL(origUrl);
|
||||
this.header.url = url.getFile();
|
||||
res = this.header.toString();
|
||||
this.header.url = origUrl;
|
||||
return res;
|
||||
}
|
||||
|
||||
public void run()
|
||||
{
|
||||
try
|
||||
{
|
||||
try
|
||||
{
|
||||
int rs = this.clientSocket.getReceiveBufferSize();
|
||||
int ss = this.clientSocket.getSendBufferSize();
|
||||
int BUF_SIZE = rs < ss ? ss : rs;
|
||||
public void run() {
|
||||
try {
|
||||
try {
|
||||
int rs = this.clientSocket.getReceiveBufferSize();
|
||||
int ss = this.clientSocket.getSendBufferSize();
|
||||
int BUF_SIZE = rs < ss ? ss : rs;
|
||||
|
||||
byte[] buf = new byte[BUF_SIZE];
|
||||
byte[] buf = new byte[BUF_SIZE];
|
||||
|
||||
initClientServerConnections(this.clientSocket);
|
||||
String headerStr;
|
||||
if (this.config.getProxySettings() == null)
|
||||
headerStr = stripProxyInfoFromRequestHeader();
|
||||
else
|
||||
headerStr = this.header.toString();
|
||||
log.trace("read request header");
|
||||
initClientServerConnections(this.clientSocket);
|
||||
String headerStr;
|
||||
if (this.config.getProxySettings() == null)
|
||||
headerStr = stripProxyInfoFromRequestHeader();
|
||||
else
|
||||
headerStr = this.header.toString();
|
||||
log.trace("read request header");
|
||||
|
||||
byte[] bytes = headerStr.getBytes();
|
||||
this.serverOut.write(bytes, 0, bytes.length);
|
||||
byte[] bytes = headerStr.getBytes();
|
||||
this.serverOut.write(bytes, 0, bytes.length);
|
||||
|
||||
log.trace("wrote request header");
|
||||
byte[] requestBody;
|
||||
if (this.header.contentLength > 0) {
|
||||
this.buffer.reset();
|
||||
int len = 0;
|
||||
int num = 0;
|
||||
while (num < this.header.contentLength) {
|
||||
try {
|
||||
len = this.clientIn.read(buf, 0, buf.length);
|
||||
}
|
||||
catch (SocketException e)
|
||||
{
|
||||
log.info("truncated request from browser: " +
|
||||
e.getMessage());
|
||||
throw new Utils.SilentException();
|
||||
}
|
||||
if (len == 0)
|
||||
break;
|
||||
log.trace("read " + Integer.toString(len) + " bytes");
|
||||
this.serverOut.write(buf, 0, len);
|
||||
this.buffer.write(buf, 0, len);
|
||||
log.trace("wrote " + Integer.toString(len) + " bytes");
|
||||
num += len;
|
||||
}
|
||||
requestBody = this.buffer.toByteArray();
|
||||
log.trace("transferred rest of request body");
|
||||
} else {
|
||||
requestBody = new byte[0];
|
||||
log.trace("no request body");
|
||||
}
|
||||
log.trace("wrote request header");
|
||||
byte[] requestBody;
|
||||
if (this.header.contentLength > 0) {
|
||||
this.buffer.reset();
|
||||
int len = 0;
|
||||
int num = 0;
|
||||
while (num < this.header.contentLength) {
|
||||
try {
|
||||
len = this.clientIn.read(buf, 0, buf.length);
|
||||
} catch (SocketException e) {
|
||||
log.info("truncated request from browser: "
|
||||
+ e.getMessage());
|
||||
throw new Utils.SilentException();
|
||||
}
|
||||
if (len == 0)
|
||||
break;
|
||||
log.trace("read " + Integer.toString(len) + " bytes");
|
||||
this.serverOut.write(buf, 0, len);
|
||||
this.buffer.write(buf, 0, len);
|
||||
log.trace("wrote " + Integer.toString(len) + " bytes");
|
||||
num += len;
|
||||
}
|
||||
requestBody = this.buffer.toByteArray();
|
||||
log.trace("transferred rest of request body");
|
||||
} else {
|
||||
requestBody = new byte[0];
|
||||
log.trace("no request body");
|
||||
}
|
||||
|
||||
this.clientSocket.shutdownInput();
|
||||
this.clientSocket.shutdownInput();
|
||||
|
||||
this.serverSocket.shutdownOutput();
|
||||
this.serverSocket.shutdownOutput();
|
||||
|
||||
synchronized (mutex)
|
||||
{
|
||||
this.proxyServer.processRequest(this.header, requestBody);
|
||||
log.trace("processed request");
|
||||
synchronized (mutex) {
|
||||
this.proxyServer.processRequest(this.header, requestBody);
|
||||
log.trace("processed request");
|
||||
|
||||
this.buffer.reset();
|
||||
int len;
|
||||
while ((len = this.serverIn.read(buf, 0, buf.length)) > 0)
|
||||
{
|
||||
log.trace("read " + Integer.toString(len));
|
||||
try {
|
||||
this.clientOut.write(buf, 0, len);
|
||||
}
|
||||
catch (SocketException e)
|
||||
{
|
||||
log.info("browser stopped listening: " +
|
||||
e.getMessage());
|
||||
throw new Utils.SilentException();
|
||||
}
|
||||
this.buffer.write(buf, 0, len);
|
||||
}
|
||||
log.trace("transferred response");
|
||||
this.buffer.reset();
|
||||
int len;
|
||||
while ((len = this.serverIn.read(buf, 0, buf.length)) > 0) {
|
||||
log.trace("read " + Integer.toString(len));
|
||||
try {
|
||||
this.clientOut.write(buf, 0, len);
|
||||
} catch (SocketException e) {
|
||||
log.info("browser stopped listening: "
|
||||
+ e.getMessage());
|
||||
throw new Utils.SilentException();
|
||||
}
|
||||
this.buffer.write(buf, 0, len);
|
||||
}
|
||||
log.trace("transferred response");
|
||||
|
||||
this.proxyServer.processResponse(this.header, this.buffer.toByteArray());
|
||||
log.trace("processed response");
|
||||
}
|
||||
this.proxyServer.processResponse(this.header,
|
||||
this.buffer.toByteArray());
|
||||
log.trace("processed response");
|
||||
}
|
||||
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (this.serverSocket != null) {
|
||||
this.serverSocket.close();
|
||||
log.trace("closed server socket");
|
||||
}
|
||||
} finally {
|
||||
if (this.serverSocket != null) {
|
||||
this.serverSocket.close();
|
||||
log.trace("closed server socket");
|
||||
}
|
||||
|
||||
this.clientSocket.close();
|
||||
log.trace("closed client socket");
|
||||
}
|
||||
if (this.serverSocket != null) {
|
||||
this.serverSocket.close();
|
||||
log.trace("closed server socket");
|
||||
}
|
||||
this.clientSocket.close();
|
||||
log.trace("closed client socket");
|
||||
}
|
||||
if (this.serverSocket != null) {
|
||||
this.serverSocket.close();
|
||||
log.trace("closed server socket");
|
||||
}
|
||||
|
||||
this.clientSocket.close();
|
||||
log.trace("closed client socket");
|
||||
}
|
||||
catch (Throwable localThrowable)
|
||||
{
|
||||
}
|
||||
}
|
||||
this.clientSocket.close();
|
||||
log.trace("closed client socket");
|
||||
} catch (Throwable localThrowable) {
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue