This commit is contained in:
daisyonly 2014-09-01 10:50:24 +08:00
commit 03b99cc27e
14 changed files with 283 additions and 31 deletions

File diff suppressed because one or more lines are too long

View File

@ -216,7 +216,7 @@ public class RequestHandler implements Runnable {
sb.append(changeList2String(headerMap.get(key)) + "\r\n");
continue;
}
if (key.equals("Transfer-Encoding") || key.equals("Content-Encoding")) {
if (key.equals("Transfer-Encoding")) {
continue;
}
sb.append(key + ": " + changeList2String(headerMap.get(key))

View File

@ -344,7 +344,7 @@ public abstract class AbstractCodeGenerator implements IScriptGenerator,
dealWithHtmlResponse(header, responseParser);
doParseRequest(header, requestBody);
response.setResponse(doParseHtmlContent(responseParser.getResponseBody(), header.url));
response.setResponse(responseParser.encodeCoent(doParseHtmlContent(responseParser.getResponseBody(), header.url)));
setStruts(new String(response.getResponse()).toLowerCase().indexOf(
"org.apache.struts.taglib.html.token") > 0);
doEndTransaction();
@ -444,7 +444,7 @@ public abstract class AbstractCodeGenerator implements IScriptGenerator,
* @param rootUrl
* @return new response body
*/
public abstract byte[] doParseHtmlContent(String responseBody, String rootUrl);
public abstract String doParseHtmlContent(String responseBody, String rootUrl);
public abstract void doHeaders(HeaderValue[] paramArrayOfHeaderValue);

View File

@ -2,6 +2,7 @@ package org.bench4q.recorder.httpcapture.generator;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
@ -200,11 +201,11 @@ public class Bench4qCodeGenerator extends AbstractCodeGenerator {
}
@Override
public byte[] doParseHtmlContent(String responseBody, String rootUrl) {
public String doParseHtmlContent(String responseBody, String rootUrl) {
int htmlStart = responseBody.indexOf("<html");
int bodyStart = responseBody.indexOf("<body");
if (htmlStart == -1 || bodyStart == -1) {
return responseBody.getBytes();
return responseBody;
}
int htmlEnd = responseBody.indexOf("</html>");
int bodyEnd = responseBody.indexOf("</body>");
@ -216,7 +217,7 @@ public class Bench4qCodeGenerator extends AbstractCodeGenerator {
responseBody += "</html>";
htmlEnd = responseBody.indexOf("</html>");
}
return parseDocument(rootUrl, responseBody, htmlStart, htmlEnd).getBytes();
return parseDocument(rootUrl, responseBody, htmlStart, htmlEnd);
}
private String parseDocument(String rootUrl, String responseContent,

View File

@ -0,0 +1,25 @@
package org.bench4q.recorder.httpcapture.generator;
import org.apache.log4j.Logger;
public class ContentEncoder {
protected Logger logger = Logger.getLogger(ContentEncoder.class);
protected ContentEncoder() {
}
public static ContentEncoder createEncoder(String encodeType) {
if (encodeType == null) {
return new ContentEncoder();
}
if (encodeType.equalsIgnoreCase("gzip")) {
return new GzipEncoder();
} else {
return new ContentEncoder();
}
}
public byte[] encoderContent(byte[] content){
return content;
}
}

View File

@ -6,6 +6,7 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
public class GzipDecoder extends ContentDecoder {
@ -25,7 +26,6 @@ public class GzipDecoder extends ContentDecoder {
return null;
}
}
public String decodeContent(InputStream inputStream, Charset charset){
InputStream is = inputStream;
GZIPInputStream gzin;

View File

@ -0,0 +1,25 @@
package org.bench4q.recorder.httpcapture.generator;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPOutputStream;
public class GzipEncoder extends ContentEncoder{
public byte[] encoderContent(byte[] content){
try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
GZIPOutputStream gzipOutputStream = new GZIPOutputStream(outputStream);
gzipOutputStream.write(content, 0, content.length);
gzipOutputStream.finish();
gzipOutputStream.flush();
gzipOutputStream.close();
return outputStream.toByteArray();
} catch (IOException e) {
logger.error("decodeContent", e);
return null;
}
}
}

View File

@ -183,4 +183,17 @@ public class ResponseParser {
}
this.setResponseBody(new String(contentBodyAfterDecoded, charset));
}
public byte[] encodeCoent(String responseBody){
ContentEncoder contentEncoder = ContentEncoder.createEncoder(this
.getResponseHeader().getContentEncoding());
Charset charset = null;
try {
charset = Charset.forName(this.getResponseHeader().getCharset());
} catch (Exception e) {
charset = Charset.forName("utf-8");
logger.error(e, e);
}
return contentEncoder.encoderContent(responseBody.getBytes(charset));
}
}

View File

@ -1,6 +1,7 @@
package org.bench4q.recorder;
import java.io.IOException;
import java.nio.charset.Charset;
import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.HttpClient;
@ -52,12 +53,12 @@ public class Test_RequestHandler {
get.addRequestHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
get.addRequestHeader("Accept-Language", "zh-CN,zh;q=0.8");
get.addRequestHeader("Cache-Control", "max-age=0");
// get.addRequestHeader("Accept-Encoding", "gzip,deflate,sdch");
// get.addRequestHeader("Cookie","__zpspc=188.1.1409195668.1409195668.1%234%7C%7C%7C%7C%7C; BDUSS=E45eXUtamxkY2JUbS12ZjFUSmJPbWdmUU13Ri1ZVHlzWWx2S1B3YXRxRVNUU1pVQVFBQUFBJCQAAAAAAAAAAAEAAAAOfOsxd29uYW5ndW8xMQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABLA~lMSwP5Tc; cflag=65535%3A1; BD_UPN=123143; BD_HOME=1; BAIDUID=12B697CB05FD662D2CA4416FF118FCD8:FG=1; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; BD_CK_SAM=1; H_PS_PSSID=8307_5228_1466_7800_8235_8488_8057_6506_6017_8251_7607_7799_8483_8457_8167_8509_8435_8382_8114; BD_HOME=1");
get.addRequestHeader("Accept-Encoding", "gzip,deflate,sdch");
get.addRequestHeader("Cookie","__zpspc=188.1.1409195668.1409195668.1%234%7C%7C%7C%7C%7C; BDUSS=E45eXUtamxkY2JUbS12ZjFUSmJPbWdmUU13Ri1ZVHlzWWx2S1B3YXRxRVNUU1pVQVFBQUFBJCQAAAAAAAAAAAEAAAAOfOsxd29uYW5ndW8xMQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABLA~lMSwP5Tc; cflag=65535%3A1; BD_UPN=123143; BD_HOME=1; BAIDUID=12B697CB05FD662D2CA4416FF118FCD8:FG=1; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; BD_CK_SAM=1; H_PS_PSSID=8307_5228_1466_7800_8235_8488_8057_6506_6017_8251_7607_7799_8483_8457_8167_8509_8435_8382_8114; BD_HOME=1");
int statusCode = this.httpClient.executeMethod(get);
System.out.println(statusCode);
System.out.println(get.getResponseBodyAsString());
System.out.println(new String(get.getResponseBody(),Charset.forName("utf-8")));
} catch (URIException e) {
// TODO Auto-generated catch block
e.printStackTrace();

View File

@ -29,7 +29,7 @@ public class ScriptMessager extends MasterMessager {
public OperateScriptServerResponseModel startRecording(
String accessToken, String port, String fileNameUUID) {
String url = this.getBaseUrl() + "/startRecording";
String url = this.getBaseUrl() + "/startScriptRecording";
Map<String, String> params = new HashMap<String, String>();
params.put("port", port);
params.put("fileNameUUID", fileNameUUID);

View File

@ -207,3 +207,4 @@ testExecuteTime=Execute Time
startServerFail=Fail to start record server:
stopTestPlan=Stop Test
stop=Stop
testProxySettings=testProxySettings

View File

@ -208,3 +208,4 @@ startServer=\u70B9\u51FB\u542F\u52A8\u670D\u52A1\u5F55\u5236\u811A\u672C\u6216\u
testExecuteTime=\u6267\u884C\u65F6\u95F4
stopTestPlan=\u505C\u6B62\u6D4B\u8BD5
stop=\u505C\u6B62
testProxySettings=\u6d4b\u8bd5Proxy\u8bbe\u7f6e

View File

@ -143,20 +143,77 @@ body {
</div>
<div class="modal-body">
<p id="scriptInfo"><fmt:message key="startServer" /></p>
<input autofocus class="input" id="target-url" name="target-url" type="url" value="http://">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary"
onClick="startServer()" id="startServer">
<fmt:message key="startserver" />
<fmt:message key="script_jsp_recordScript" />
</button>
<button type="button" id="uploadScript" class="btn btn-primary">
<fmt:message key="uploadScript" />
</button>
</div>
<div id="fileName" style="display: none" class="modal-footer">
<input class="input-mini" name="scriptname"></input>
<button type="button" class="btn btn-primary"
onClick="saveScript()">
<fmt:message key="savefile" />
</button>
</div>
</div>
<div class="modal hide fade" id="mySecondModal">
<div class="modal-header">
<button type="button" id="myModal-close" class="close"
data-dismiss="modal">×</button>
<h3>
<fmt:message key="settings" />
</h3>
</div>
<div class="modal-body">
<p id="scriptInfo2"></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary"
onClick="testRecordProxy()" id="testRecordProxy">
<fmt:message key="testProxySettings" />
</button>
<button type="button" id="cancel" class="btn btn-primary">
<fmt:message key="plugin_jsp_cancel" />
</button>
</div>
<div id="fileName" style="display: none" class="modal-footer">
<input class="input-mini" name="scriptname"></input>
<button type="button" class="btn btn-primary"
onClick="saveScript()">
<fmt:message key="savefile" />
</button>
</div>
</div>
<div class="modal hide fade" id="myThirdModal">
<div class="modal-header">
<button type="button" id="myModal-close" class="close"
data-dismiss="modal">×</button>
<h3>
<fmt:message key="settings" />
</h3>
</div>
<div class="modal-body">
<p id="scriptInfo3"></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary"
onClick="startRecording()" id="startRecording">
<fmt:message key="startRecording" />
</button>
<button type="button" class="btn btn-primary"
onClick="stopServer()" id="stopServer">
<fmt:message key="stopserver" />
</button>
<button type="button" id="uploadScript" class="btn btn-primary">
<fmt:message key="uploadScript" />
</button>
</div>
<div id="fileName" style="display: none" class="modal-footer">
<input class="input-mini" name="scriptname"></input>

View File

@ -5,24 +5,27 @@ $('.btn-setting').click(function(e) {
e.preventDefault();
$('#myModal').modal('show');
});
$("#mySecondModal #cancel").click(function() {
$('#mySecondModal').modal('hide');
});
function startServer() {
$.post("startRecordServer", {}, function(data) {
if (data.success) {
server = data.server;
$('#scriptInfo').text(
$.i18n.prop("setProxy")+
" IP:133.133.2.100"
+ " ,port: " + server.port
+ $.i18n.prop("recordScript"));
$('#startServer').attr('disabled', true);
$('#stopServer').attr('disabled', false);
$('#myModal').modal('hide');
$('#mySecondModal').modal('show');
$('#scriptInfo2').text(
$.i18n.prop("setProxy")+
" IP:133.133.2.100"
+ " ,port: " + server.port
+ $.i18n.prop("recordScript"));
//$('#startServer').attr('disabled', true);
//$('#stopServer').attr('disabled', false);
} else {
$('#scriptInfo').html(
$.i18n.prop("startServerFail") + data.failedMessage);
}
}, "json");
}
$('#uploadScript')
.click(
@ -60,7 +63,7 @@ function stopServer() {
scriptRecordUUID : server.fileName
}, function(data) {
if (!data.success) {
$('#scriptInfo').text(
$('#scriptInfo3').text(
"stop record server error:" + data.failedMessage);
$('#stopServer').attr('disabled', true);
$('#startServer').attr('disabled', false);
@ -68,13 +71,13 @@ function stopServer() {
else {
$('#fileName').show();
$('#scriptInfo').text(
$('#scriptInfo3').text(
$.i18n.prop("stopRecord")
);
}
}, "json");
} else {
$('#scriptInfo').text("record script server has not started");
$('#scriptInfo3').text("record script server has not started");
}
}
@ -87,11 +90,11 @@ function saveScript() {
scriptRecordUUID : server.fileName
}, function(data) {
if (data.success) {
$("#scriptInfo").text($.i18n.prop("saveScriptSuccess"));
$("#scriptInfo3").text($.i18n.prop("saveScriptSuccess"));
$('#fileName').hide();
server = null;
} else {
$("#scriptInfo").text($.i18n.prop("saveScriptFail") + data.failedMessage);
$("#scriptInfo3").text($.i18n.prop("saveScriptFail") + data.failedMessage);
$('#fileName').hide();
}
loadScript(table, 2);
@ -123,3 +126,41 @@ function getScriptPlugins(scriptId) {
}, "json")
}
function testRecordProxy() {
$.post("testRecordProxy", {}, function(data) {
if (data.success) {
$('#mySecondModal').modal('hide');
$('#myThirdModal').modal('show');
//server = data.server;
$('#scriptInfo3').text(
$.i18n.prop("RecordingScript"));
//$('#startServer').attr('disabled', true);
$('#stopServer').attr('disabled', true);
} else {
$('#scriptInfo2').html(
$.i18n.prop("ProxySettingError") + data.failedMessage);
}
}, "json");
}
function startRecording() {
if (server != null) {
$.post("startRecording", {
port : server.port,
scriptRecordUUID : server.fileName
}, function(data) {
if (!data.success) {
$('#scriptInfo3').text(
"start recording error:" + data.failedMessage);
$('#stopServer').attr('disabled', false);
$('#startRecording').attr('disabled', true);
}
else {
window.open('http://www.baidu.com/')
}
}, "json");
} else {
$('#scriptInfo3').text("record script server has not started");
}
}