add FromProxy to request header

add div to response html
This commit is contained in:
hmm 2014-08-27 08:37:25 +08:00
parent 69d6b8b23f
commit e1b064319a
9 changed files with 166 additions and 44 deletions

View File

@ -77,6 +77,11 @@ public class HttpRequestHeader {
* Anything in the header that was unrecognized by this class.
*/
public String unrecognized = new String();
/**
* indicate the request go through proxy
*/
public boolean fromProxy = true;
/**
* Indicates that no cached versions of the requested object are to be sent.
* Usually used to tell proxy not to send a cached copy. This may also
@ -236,7 +241,10 @@ public class HttpRequestHeader {
if (0 != authorization.length())
Request += "Authorization: " + authorization + CR;
//add flag show the request from proxy
Request += "FromProxy:"+fromProxy + CR;
if (sendUnknowen) {
if (0 != unrecognized.length())
Request += unrecognized;

View File

@ -40,7 +40,7 @@ public class ProxyServer implements Runnable {
}
public void processResponse(HttpRequestHeader header, byte[] requestBody,
byte[] response) throws Exception {
ResponseModel response) throws Exception {
for (Enumeration<Observer> e = this.listeners.elements(); e
.hasMoreElements();) {
Observer pl = (Observer) e.nextElement();
@ -82,6 +82,6 @@ public class ProxyServer implements Runnable {
public abstract void processResponse(
HttpRequestHeader paramHttpRequestHeader, byte[] requestBody,
byte[] paramArrayOfByte) throws Exception;
ResponseModel response) throws Exception;
}
}

View File

@ -41,8 +41,10 @@ public class RequestHandler implements Runnable {
try {
this.header = new HttpRequestHeader(this.clientIn);
} catch (IOException e) {
log.error("truncated request from browser: " + this.header.url, e);
System.out.println(this.header.url);
if(this.header != null){
log.error("truncated request from browser: " + this.header.url, e);
System.out.println(this.header.url);
}
throw new Utils.SilentException();
}
if (!this.header.url.startsWith("http"))
@ -159,19 +161,20 @@ public class RequestHandler implements Runnable {
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() + this.buffer.toString());
throw new Utils.SilentException();
}
this.buffer.write(buf, 0, len);
}
log.trace("transferred response len:" + len);
ResponseModel responseModel = new ResponseModel(this.buffer.toByteArray());
this.proxyServer.processResponse(this.header, requestBody,
this.buffer.toByteArray());
responseModel);
try {
this.clientOut.write(responseModel.getResponse());
} catch (SocketException e) {
log.info("browser stopped listening: "
+ e.getMessage() + this.buffer.toString());
throw new Utils.SilentException();
}
log.trace("processed response");
}

View File

@ -19,7 +19,6 @@ import javax.xml.bind.Marshaller;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.bench4q.recorder.httpcapture.ProxyServer;
import org.bench4q.recorder.httpcapture.Action;
import org.bench4q.recorder.httpcapture.Config;
import org.bench4q.recorder.httpcapture.HeaderValue;
@ -27,6 +26,8 @@ import org.bench4q.recorder.httpcapture.HttpRequestHeader;
import org.bench4q.recorder.httpcapture.HttpTestCase;
import org.bench4q.recorder.httpcapture.IScriptAdapter;
import org.bench4q.recorder.httpcapture.Param;
import org.bench4q.recorder.httpcapture.ProxyServer;
import org.bench4q.recorder.httpcapture.ResponseModel;
import org.bench4q.recorder.httpcapture.Utils;
import org.bench4q.recorder.httpcapture.Utils.UserException;
import org.bench4q.share.models.agent.RunScenarioModel;
@ -225,7 +226,7 @@ public abstract class AbstractCodeGenerator implements IScriptGenerator,
doSetData(new String(requestBody, newCharset));
else
doSetData(new String(requestBody));
} else if (newCharset != null) {
} else if (newCharset != null && !newCharset.equals("")) {
setBodyParams(action, new String(requestBody, newCharset));
} else {
setBodyParams(action, new String(requestBody));
@ -327,13 +328,14 @@ public abstract class AbstractCodeGenerator implements IScriptGenerator,
}
public void processResponse(HttpRequestHeader header, byte[] requestBody,
byte[] response) throws Exception {
ResponseModel response) throws Exception {
System.out.println("enter processResponse");
if (this.ignoreNextResponse) {
log.debug("Ignoring response");
this.ignoreNextResponse = false;
return;
}
ResponseParser responseParser = new ResponseParser(response);
ResponseParser responseParser = new ResponseParser(response.getResponse());
ResponseHeader responseHeader = responseParser.getResponseHeader();
doAssertResponse(responseHeader.getRespCode());
if (responseHeader.isValidResponseHeader()) {
@ -342,14 +344,14 @@ public abstract class AbstractCodeGenerator implements IScriptGenerator,
&& hasHtmlTag(responseParser)) {
dealWithHtmlResponse(header, responseParser);
doParseRequest(header, requestBody);
System.out.println("enter doParseHtml");
doParseHtmlContent(responseParser.getResponseBody(), header.url);
setStruts(new String(response).toLowerCase().indexOf(
response.setResponse(doParseHtmlContent(responseParser.getResponseBody(), header.url));
setStruts(new String(response.getResponse()).toLowerCase().indexOf(
"org.apache.struts.taglib.html.token") > 0);
doEndTransaction();
return;
}
setStruts(new String(response).toLowerCase().indexOf(
setStruts(new String(response.getResponse()).toLowerCase().indexOf(
"org.apache.struts.taglib.html.token") > 0);
doEndTransaction();
} else {
@ -438,7 +440,12 @@ public abstract class AbstractCodeGenerator implements IScriptGenerator,
public abstract void doTidyCode(String paramString)
throws Utils.UserException;
public abstract void doParseHtmlContent(String responseBody, String rootUrl);
/**
* @param responseBody
* @param rootUrl
* @return new response body
*/
public abstract byte[] doParseHtmlContent(String responseBody, String rootUrl);
public abstract void doHeaders(HeaderValue[] paramArrayOfHeaderValue);

View File

@ -200,16 +200,16 @@ public class Bench4qCodeGenerator extends AbstractCodeGenerator {
}
@Override
public void doParseHtmlContent(String responseBody, String rootUrl) {
public byte[] doParseHtmlContent(String responseBody, String rootUrl) {
int htmlStart = responseBody.indexOf("<html");
int htmlEnd = responseBody.indexOf("</html>");
if (htmlStart == -1 || htmlEnd == -1) {
return;
return responseBody.getBytes();
}
parseDocument(rootUrl, responseBody, htmlStart, htmlEnd);
return parseDocument(rootUrl, responseBody, htmlStart, htmlEnd).getBytes();
}
private void parseDocument(String rootUrl, String responseContent,
private String parseDocument(String rootUrl, String responseContent,
int htmlStart, int htmlEnd) {
// TODO:reset the childrenUrls in scirptAdapter
String htmlContent = responseContent.substring(htmlStart, htmlEnd + 6);
@ -220,6 +220,9 @@ public class Bench4qCodeGenerator extends AbstractCodeGenerator {
.buildParentChildrenRelationship(this.getScriptAdapter()
.getParentBatchIdWithParentUrl(rootUrl));
this.getScriptAdapter().getChildrenUrls().addAll(childrenUrls);
//add record flag to document
documentParser.appendRecordFlag();
return documentParser.toString();
}
private void saveToFile(String htmlContent) {

View File

@ -12,6 +12,24 @@ import org.jsoup.select.Elements;
public class HtmlDocumentParser {
private Document document;
private static Logger logger = Logger.getLogger(HtmlDocumentParser.class);
private static String recordFlagHtml = "<div id=\"hemeimei\" style=\"cursor: default;vertical-align: top;"
+ "text-decoration: none;display: inline-block;width: 22px;height: 22px;"
+ "margin-top: 8px;border: none;"
+ "url('data:image/png;"
+ "base64,iVBORw0KGgoAAAANSUhEUgAAABYAAAAWCAYAAADEtGw7AAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5"
+ "ccllPAAAA6dJREFUeNrUlV9oW3UUx8/v3iQ3ubk3q0mXm2xZa0oximKXtqNOBhMq60NQoeCzjO1pVAWh4FsJTFAfJqgMffBp"
+ "jz7JGHtYCzoYdC6YqW2Zbik1XZubJQ1dbnJzc//9PL+2YV3/yR724IEvv5vfPb9PTs7vnBNCKYXnYRw8J/Mc9JIQwuMSQvmZZ"
+ "ABBA2jjs7GlOv5i55nACJUQdOR7gPfjhJwWAQZ4Sg87hDxqAfz+kNKfLwD8iH4qwhu7zu+VY0mSlI8BXn9H1789PDTU253JCL"
+ "6eHvB0d4O9tgbm8jJUb9xoV27dWvxJFD/6BuDPRqNRPhCcTCa7JqvVsTds+4cXJyZE6eRJ0i6VwKnXwWk2gRME4INBEI4eBX1"
+ "ujv5z6ZL+C8d98HU0Ol0oFB7vCR4eHvb263rqk3v3Zl7OZqOeWAxa9+8DtW0A5sfkugCOA5QQEFMpcNbX4a9sVv0ilXp7KRD4O"
+ "5fLWbuqQlGUyIVC4dMjmUzYixE183lwNbwuw3iidntTGH0zlwNvJALK6GhkYnFxUpblyJ7lFgqF/EHLejdy5oxHv3sXQNf3hFK"
+ "2miZAqwXa9DREBga8kmm+F4vF/LvA2WzW81axGPceOiRwgQDY5TJQPLgdSnBlopUK2AsLYN6+DQaKcBx4BEF8c2Ulyjg7y83Xs7z"
+ "8ihiP29b8vM8uFICXpI3LoiyvLEIGrdXAtSxgd0Nwn8NcO7gnJRJO/9LSa78CzCHL3g52iSxrFsspHnSxEgAjoSgWEdbrphDIsX1"
+ "c2bW7CCc+H1iGQTGXdba1M8dmc2xswVBVge/qAs7rfQJDcZ0v4PnN584++vGhELRWV33VTGaecZ4CT01NueTUqccYSdnC/Ap9fR"
+ "sHYQd8O5S9Y37MH3h+NTgyojHOrqoYHx9/6PT1fbZy7ZoRHBoCDhvhqai31PnMGiUwOAgr168bZjL5eTgcbhzY0r/J8oySTp9+4"
+ "cQJvjU7C46qAocp2IgSjV0mpyjgHxmB9Tt3HDWfnyE3b55Np9Ml9nrfITSoaaN5v3+hpapJbBa/i/Xs4oygjQZwsgwcNgURRShdvW"
+ "poxeJi8OLFyVQ6Xe1A9424Y3/09n5Jy+UP5f5+GozHBV84zJm1mquXSu36gweEKMpXL83Ofld1nEoikWj953TbbpXLl4+vXbly3CwW"
+ "09BovIoRz3mOHcuHz53LR8+fX0WXWqfEngmMxoa9wBpI0zQPzgN7q6TYwHf2nef/u/+8fwUYAGyMtUnmcOi+AAAAAElFTkSuQmCC') no-repeat top left\"></div>";
private Document getDocument() {
return document;
@ -25,10 +43,18 @@ public class HtmlDocumentParser {
this.setDocument(Jsoup.parse(htmlContent, baseUrl));
}
public String toString(){
return this.getDocument().toString();
}
public void beginParse() {
}
/**
*
*/
public void appendRecordFlag(){
this.getDocument().body().append(recordFlagHtml);
}
public List<ChildrenUrl> buildParentChildrenRelationship(int parentBatchId) {
List<ChildrenUrl> result = new ArrayList<ChildrenUrl>();
if (parentBatchId == -1) {

View File

@ -17,7 +17,8 @@ public class ResponseHeader {
private String charset;
private String contentEncoding;
private int contentLength;
private String transferEncoding;
public String getRespCode() {
return respCode;
}
@ -71,4 +72,12 @@ public class ResponseHeader {
boolean isGoodRequest() {
return this.getRespCode().startsWith("200");
}
public String getTransferEncoding() {
return transferEncoding;
}
public void setTransferEncoding(String transferEncoding) {
this.transferEncoding = transferEncoding;
}
}

View File

@ -1,9 +1,13 @@
package org.bench4q.recorder.httpcapture.generator;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.StringTokenizer;
import org.apache.commons.httpclient.ChunkedInputStream;
import org.apache.commons.httpclient.ChunkedOutputStream;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.log4j.Logger;
public class ResponseParser {
@ -52,6 +56,7 @@ public class ResponseParser {
result.setContentLength(Integer.parseInt(parseContentLength()));
result.setContentType(parseContentType());
result.setRespCode(parseResponseCode());
result.setTransferEncoding(parseTransferEncoding());
return result;
}
@ -145,14 +150,73 @@ public class ResponseParser {
return respCode;
}
private String parseTransferEncoding() {
String response = preprocess(new String(this.getResponse()));
int pos = response.indexOf("transfer-encoding:");
if (pos > -1) {
pos += 19;
int end = response.indexOf("\r\n", pos);
return response.substring(pos, end);
}
return null;
}
private byte[] buildResponseBody() {
String response = preprocess(new String(this.getResponse()));
int pos = response.indexOf("\r\n\r\n");
pos += 4;
String testRes = response.substring(pos);
System.out.println(testRes);
ByteArrayInputStream in = new ByteArrayInputStream(this.getResponse(),
pos, this.getResponse().length - pos);
ChunkedInputStream chunkedIS = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
chunkedIS = new ChunkedInputStream(in);
int tempByte;
while ((tempByte = chunkedIS.read()) != -1){
out.write(tempByte);
}
return out.toByteArray();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (chunkedIS != null) {
chunkedIS.close();
}
if(out != null){
out.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
private void parseResponseBody() {
ContentDecoder contentDecoder = ContentDecoder.createDecoder(this
.getResponseHeader().getContentEncoding());
byte[] contentBodyAfterDecoded = contentDecoder
.decodeContent(new ByteArrayInputStream(this.getResponse(),
this.getResponse().length
- this.getResponseHeader().getContentLength(),
this.getResponseHeader().getContentLength()));
byte[] contentBodyAfterDecoded = null;
if (this.getResponseHeader().getTransferEncoding() != null
&& this.getResponseHeader().getTransferEncoding()
.equals("chunked")) {
contentBodyAfterDecoded = contentDecoder
.decodeContent(new ByteArrayInputStream(buildResponseBody()));
} else {
contentBodyAfterDecoded = contentDecoder
.decodeContent(new ByteArrayInputStream(this.getResponse(),
this.getResponse().length
- this.getResponseHeader()
.getContentLength(), this
.getResponseHeader().getContentLength()));
}
if (contentBodyAfterDecoded == null) {
return;
}

View File

@ -1,6 +1,7 @@
package org.bench4q.recorder;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.ByteArrayInputStream;
import java.io.File;
@ -15,6 +16,7 @@ import org.bench4q.recorder.httpcapture.Bench4qTestScriptAdapter;
import org.bench4q.recorder.httpcapture.HeaderValue;
import org.bench4q.recorder.httpcapture.HttpRequestHeader;
import org.bench4q.recorder.httpcapture.Param;
import org.bench4q.recorder.httpcapture.ResponseModel;
import org.bench4q.recorder.httpcapture.Utils.UserException;
import org.bench4q.recorder.httpcapture.generator.Bench4qCodeGenerator;
import org.bench4q.recorder.httpcapture.generator.ContentDecoder;
@ -38,8 +40,8 @@ public class Test_Bench4QCodeGenerator extends TestRecordBase {
scriptGenerator.processResponse(
makeAHeader("www.makeup.com"),
new byte[0],
FileUtils.readFileToByteArray(new File(dealWithSerie
+ "NullResponseBody.html")));
new ResponseModel(FileUtils.readFileToByteArray(new File(dealWithSerie
+ "NullResponseBody.html"))));
assertEquals(0, this.scriptAdapter.getChildrenUrls().size());
}
@ -50,8 +52,8 @@ public class Test_Bench4QCodeGenerator extends TestRecordBase {
scriptGenerator.processResponse(
makeAHeader("www.makeup.com"),
new byte[0],
FileUtils.readFileToByteArray(new File(dealWithSerie
+ "BlankBody.html")));
new ResponseModel(FileUtils.readFileToByteArray(new File(dealWithSerie
+ "BlankBody.html"))));
assertEquals(0, this.scriptAdapter.getChildrenUrls().size());
}
@ -63,8 +65,8 @@ public class Test_Bench4QCodeGenerator extends TestRecordBase {
scriptGenerator.processResponse(
header,
new byte[0],
FileUtils.readFileToByteArray(new File(dealWithSerie
+ "Response.txt")));
new ResponseModel(FileUtils.readFileToByteArray(new File(dealWithSerie
+ "Response.txt"))));
assertEquals(11, this.scriptAdapter.getChildrenUrls().size());
}
@ -106,8 +108,8 @@ public class Test_Bench4QCodeGenerator extends TestRecordBase {
scriptGenerator.processResponse(
makeAHeader("www.makeup2.com"),
new byte[0],
FileUtils.readFileToByteArray(new File(dealWithSerie
+ "NotHtmlResponse.html")));
new ResponseModel(FileUtils.readFileToByteArray(new File(dealWithSerie
+ "NotHtmlResponse.html"))));
}
@Test