there is a bug in httpcapture

This commit is contained in:
coderfengyun 2013-07-19 21:27:30 +08:00
parent b1422b22a1
commit e569cb0a0f
5 changed files with 164 additions and 181 deletions

View File

@ -1,6 +1,5 @@
package org.bench4q.master; package org.bench4q.master;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
MasterServer masterServer = new MasterServer(8080); MasterServer masterServer = new MasterServer(8080);

View File

@ -57,7 +57,7 @@ public class MasterServer {
"classpath*:org/bench4q/master/config/application-context.xml"); "classpath*:org/bench4q/master/config/application-context.xml");
this.getServer().setHandler(servletContextHandler); this.getServer().setHandler(servletContextHandler);
this.getServer().start(); this.getServer().start();
this.getAgentPoolService().loadAgentPoolFromDB(); //this.getAgentPoolService().loadAgentPoolFromDB();
return true; return true;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();

View File

@ -48,10 +48,9 @@ public class RecordPortController extends BaseController {
LocalPort localPort = new LocalPort(); LocalPort localPort = new LocalPort();
localPort.setPort(port); localPort.setPort(port);
localPort.setInUse(false); localPort.setInUse(false);
if (this.getPortPoolService().getScriptPortPool().put(port, localPort) == null) {
return setOrganizePortResponseModel(false, this.getPortPoolService().getScriptPortPool().put(port, localPort);
"add to local pool fails 1");
}
if (!this.getPortPoolService().addPortToDBPool(port)) { if (!this.getPortPoolService().addPortToDBPool(port)) {
return setOrganizePortResponseModel(false, "add to DB pool fails 2"); return setOrganizePortResponseModel(false, "add to DB pool fails 2");
} }

View File

@ -67,6 +67,9 @@ public class RecordScriptController extends BaseController {
synchronized (PORT_LOCK) { synchronized (PORT_LOCK) {
// TODO: // TODO:
LocalPort localPort = this.getPortPoolService().getAPortNotInUse(); LocalPort localPort = this.getPortPoolService().getAPortNotInUse();
if (localPort == null) {
return returnResponseModel(false, "port is in use!");
}
this.setPortForRecord(localPort.getPort()); this.setPortForRecord(localPort.getPort());
localPort.setInUse(true); localPort.setInUse(true);
} }

View File

@ -14,201 +14,183 @@ import java.net.URL;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; 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 static final Log log = LogFactory.getLog(RequestHandler.class);
private Config config = Config.getConfig(); private Config config = Config.getConfig();
private InputStream clientIn; private InputStream clientIn;
private InputStream serverIn; private InputStream serverIn;
private OutputStream clientOut; private OutputStream clientOut;
private OutputStream serverOut; private OutputStream serverOut;
private HttpRequestHeader header; private HttpRequestHeader header;
private ProxyServer proxyServer; private ProxyServer proxyServer;
private Socket clientSocket; private Socket clientSocket;
private Socket serverSocket; private Socket serverSocket;
private ByteArrayOutputStream buffer; private ByteArrayOutputStream buffer;
private static Object mutex = new Object(); private static Object mutex = new Object();
RequestHandler(ProxyServer proxyServer, Socket s) RequestHandler(ProxyServer proxyServer, Socket s) {
{ assert (s != null);
assert (s != null); this.clientSocket = s;
this.clientSocket = s; this.buffer = new ByteArrayOutputStream();
this.buffer = new ByteArrayOutputStream(); this.proxyServer = proxyServer;
this.proxyServer = proxyServer; }
}
private void initClientServerConnections(Socket s) private void initClientServerConnections(Socket s) throws Throwable {
throws Throwable this.clientIn = new BufferedInputStream(s.getInputStream());
{ this.clientOut = s.getOutputStream();
this.clientIn = new BufferedInputStream(s.getInputStream()); try {
this.clientOut = s.getOutputStream(); this.header = new HttpRequestHeader(this.clientIn);
try { } catch (IOException e) {
this.header = new HttpRequestHeader(this.clientIn); log.info("truncated request from browser: " + e.getMessage());
} throw new Utils.SilentException();
catch (IOException e) }
{ if (!this.header.url.startsWith("http"))
log.info("truncated request from browser: " + e.getMessage()); throw new Utils.UserException(
throw new Utils.SilentException(); "MaxQ only supports the HTTP protocol.");
} URL url = new URL(this.header.url);
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(); Config.ProxySettings proxy = this.config.getProxySettings();
int port; int port;
String host; String host;
if (proxy != null) { if (proxy != null) {
host = proxy.host; host = proxy.host;
port = proxy.port; port = proxy.port;
} else { } else {
host = url.getHost(); host = url.getHost();
port = url.getPort(); port = url.getPort();
} }
if (port < 1) if (port < 1)
port = 80; port = 80;
try { try {
this.serverSocket = new Socket(InetAddress.getByName(host), port); this.serverSocket = new Socket(InetAddress.getByName(host), port);
} catch (ConnectException e) { } catch (ConnectException e) {
String msg = "Cannot connect to "; String msg = "Cannot connect to ";
if (proxy != null) if (proxy != null)
msg = msg + "proxy server "; msg = msg + "proxy server ";
msg = msg + host; msg = msg + host;
if (port != 80) if (port != 80)
msg = msg + " on port " + Integer.toString(port); msg = msg + " on port " + Integer.toString(port);
throw new Utils.UserException(msg + "."); throw new Utils.UserException(msg + ".");
} }
try { try {
this.serverIn = this.serverSocket.getInputStream(); this.serverIn = this.serverSocket.getInputStream();
this.serverOut = this.serverSocket.getOutputStream(); this.serverOut = this.serverSocket.getOutputStream();
} catch (Throwable e) { } catch (Throwable e) {
this.serverSocket.close(); this.serverSocket.close();
this.serverSocket = null; this.serverSocket = null;
throw e; throw e;
} }
} }
private String stripProxyInfoFromRequestHeader() private String stripProxyInfoFromRequestHeader()
throws MalformedURLException throws MalformedURLException {
{ String res = "";
String res = ""; String origUrl = this.header.url;
String origUrl = this.header.url; URL url = new URL(origUrl);
URL url = new URL(origUrl); this.header.url = url.getFile();
this.header.url = url.getFile(); res = this.header.toString();
res = this.header.toString(); this.header.url = origUrl;
this.header.url = origUrl; return res;
return res; }
}
public void run() public void run() {
{ try {
try try {
{ int rs = this.clientSocket.getReceiveBufferSize();
try int ss = this.clientSocket.getSendBufferSize();
{ int BUF_SIZE = rs < ss ? ss : rs;
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); initClientServerConnections(this.clientSocket);
String headerStr; String headerStr;
if (this.config.getProxySettings() == null) if (this.config.getProxySettings() == null)
headerStr = stripProxyInfoFromRequestHeader(); headerStr = stripProxyInfoFromRequestHeader();
else else
headerStr = this.header.toString(); headerStr = this.header.toString();
log.trace("read request header"); log.trace("read request header");
byte[] bytes = headerStr.getBytes(); byte[] bytes = headerStr.getBytes();
this.serverOut.write(bytes, 0, bytes.length); this.serverOut.write(bytes, 0, bytes.length);
log.trace("wrote request header"); log.trace("wrote request header");
byte[] requestBody; byte[] requestBody;
if (this.header.contentLength > 0) { if (this.header.contentLength > 0) {
this.buffer.reset(); this.buffer.reset();
int len = 0; int len = 0;
int num = 0; int num = 0;
while (num < this.header.contentLength) { while (num < this.header.contentLength) {
try { try {
len = this.clientIn.read(buf, 0, buf.length); len = this.clientIn.read(buf, 0, buf.length);
} } catch (SocketException e) {
catch (SocketException e) log.info("truncated request from browser: "
{ + e.getMessage());
log.info("truncated request from browser: " + throw new Utils.SilentException();
e.getMessage()); }
throw new Utils.SilentException(); if (len == 0)
} break;
if (len == 0) log.trace("read " + Integer.toString(len) + " bytes");
break; this.serverOut.write(buf, 0, len);
log.trace("read " + Integer.toString(len) + " bytes"); this.buffer.write(buf, 0, len);
this.serverOut.write(buf, 0, len); log.trace("wrote " + Integer.toString(len) + " bytes");
this.buffer.write(buf, 0, len); num += len;
log.trace("wrote " + Integer.toString(len) + " bytes"); }
num += len; requestBody = this.buffer.toByteArray();
} log.trace("transferred rest of request body");
requestBody = this.buffer.toByteArray(); } else {
log.trace("transferred rest of request body"); requestBody = new byte[0];
} else { log.trace("no request body");
requestBody = new byte[0]; }
log.trace("no request body");
}
this.clientSocket.shutdownInput(); this.clientSocket.shutdownInput();
this.serverSocket.shutdownOutput(); this.serverSocket.shutdownOutput();
synchronized (mutex) synchronized (mutex) {
{ this.proxyServer.processRequest(this.header, requestBody);
this.proxyServer.processRequest(this.header, requestBody); log.trace("processed request");
log.trace("processed request");
this.buffer.reset(); this.buffer.reset();
int len; int len;
while ((len = this.serverIn.read(buf, 0, buf.length)) > 0) while ((len = this.serverIn.read(buf, 0, buf.length)) > 0) {
{ log.trace("read " + Integer.toString(len));
log.trace("read " + Integer.toString(len)); try {
try { this.clientOut.write(buf, 0, len);
this.clientOut.write(buf, 0, len); } catch (SocketException e) {
} log.info("browser stopped listening: "
catch (SocketException e) + e.getMessage());
{ throw new Utils.SilentException();
log.info("browser stopped listening: " + }
e.getMessage()); this.buffer.write(buf, 0, len);
throw new Utils.SilentException(); }
} log.trace("transferred response");
this.buffer.write(buf, 0, len);
}
log.trace("transferred response");
this.proxyServer.processResponse(this.header, this.buffer.toByteArray()); this.proxyServer.processResponse(this.header,
log.trace("processed response"); this.buffer.toByteArray());
} log.trace("processed response");
}
} } finally {
finally if (this.serverSocket != null) {
{ this.serverSocket.close();
if (this.serverSocket != null) { log.trace("closed server socket");
this.serverSocket.close(); }
log.trace("closed server socket");
}
this.clientSocket.close(); this.clientSocket.close();
log.trace("closed client socket"); log.trace("closed client socket");
} }
if (this.serverSocket != null) { if (this.serverSocket != null) {
this.serverSocket.close(); this.serverSocket.close();
log.trace("closed server socket"); log.trace("closed server socket");
} }
this.clientSocket.close(); this.clientSocket.close();
log.trace("closed client socket"); log.trace("closed client socket");
} } catch (Throwable localThrowable) {
catch (Throwable localThrowable) }
{ }
}
}
} }