1。script name can't be null

2.get the charset from html
3.close record server when web page closed
This commit is contained in:
hmm 2014-09-05 14:11:21 +08:00
parent 86a52df435
commit ed62f1d8f0
6 changed files with 62 additions and 14 deletions

View File

@ -1,5 +1,6 @@
package org.bench4q.recorder.httpcapture.generator; package org.bench4q.recorder.httpcapture.generator;
import java.nio.charset.Charset;
import java.util.HashMap; import java.util.HashMap;
public class ResponseHeader { public class ResponseHeader {
@ -18,6 +19,7 @@ public class ResponseHeader {
private String contentEncoding; private String contentEncoding;
private int contentLength; private int contentLength;
private String transferEncoding; private String transferEncoding;
private Charset contentCharset;
public int getRespCode() { public int getRespCode() {
return respCode; return respCode;
@ -81,4 +83,12 @@ public class ResponseHeader {
public void setTransferEncoding(String transferEncoding) { public void setTransferEncoding(String transferEncoding) {
this.transferEncoding = transferEncoding; this.transferEncoding = transferEncoding;
} }
public Charset getContentCharset() {
return contentCharset;
}
public void setContentCharset(Charset charset) {
this.contentCharset = charset;
}
} }

View File

@ -7,6 +7,7 @@ import java.nio.charset.Charset;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.bench4q.recorder.httpcapture.ResponseModel; import org.bench4q.recorder.httpcapture.ResponseModel;
import org.jsoup.Jsoup;
public class ResponseParser { public class ResponseParser {
private Logger logger = Logger.getLogger(ResponseParser.class); private Logger logger = Logger.getLogger(ResponseParser.class);
@ -96,14 +97,11 @@ public class ResponseParser {
return ret; return ret;
} }
private String parseTransferEncoding(HttpURLConnection httpURLConnection) { private String parseTransferEncoding(HttpURLConnection httpURLConnection) {
return httpURLConnection.getHeaderField("Transfer-Encoding"); return httpURLConnection.getHeaderField("Transfer-Encoding");
} }
private void parseResponseBody() { private void parseResponseBody() {
ContentDecoder contentDecoder = ContentDecoder.createDecoder(this ContentDecoder contentDecoder = ContentDecoder.createDecoder(this
.getResponseHeader().getContentEncoding()); .getResponseHeader().getContentEncoding());
@ -119,20 +117,49 @@ public class ResponseParser {
try { try {
charset = Charset.forName(this.getResponseHeader().getCharset()); charset = Charset.forName(this.getResponseHeader().getCharset());
} catch (Exception e) { } catch (Exception e) {
charset = Charset.forName("utf-8"); charset = getCharsetFromContent(contentBodyAfterDecoded);
if(charset == null){
charset = Charset.forName("utf-8");
}
this.getResponseHeader().setContentCharset(charset);
logger.error(e, e); logger.error(e, e);
} }
this.setResponseBody(new String(contentBodyAfterDecoded, charset)); this.setResponseBody(new String(contentBodyAfterDecoded, charset));
} }
public byte[] encodeCoent(String responseBody){ private Charset getCharsetFromContent(byte[] content) {
String contentString = new String(content,
Charset.forName("iso-8859-1")).toLowerCase();
int pos = contentString.indexOf("charset=");
if (pos == -1)
return null;
pos += 8;
char tempChar = contentString.charAt(pos);
StringBuilder sBuilder = new StringBuilder();
if (tempChar == '"') {
pos++;
}
while ((tempChar = contentString.charAt(pos)) != '"') {
sBuilder.append(tempChar);
pos++;
}
try {
return Charset.forName(sBuilder.toString());
} catch (Exception e) {
return null;
}
}
public byte[] encodeCoent(String responseBody) {
ContentEncoder contentEncoder = ContentEncoder.createEncoder(this ContentEncoder contentEncoder = ContentEncoder.createEncoder(this
.getResponseHeader().getContentEncoding()); .getResponseHeader().getContentEncoding());
Charset charset = null; Charset charset = null;
try { try {
charset = Charset.forName(this.getResponseHeader().getCharset()); charset = Charset.forName(this.getResponseHeader().getCharset());
} catch (Exception e) { } catch (Exception e) {
charset = Charset.forName("utf-8"); charset = this.getResponseHeader().getContentCharset();
if(charset == null)
charset = Charset.forName("utf-8");
logger.error(e, e); logger.error(e, e);
} }
byte[] responseBytes = responseBody.getBytes(charset); byte[] responseBytes = responseBody.getBytes(charset);

View File

@ -118,9 +118,9 @@ body {
<tr> <tr>
<td> <td>
<button id="createNewScript" <button id="createNewScript"
class="btn btn-primary" onclick="startServer()" type="submit"> class="btn btn-primary" type="submit" onclick="startServer()">
<fmt:message key="script_jsp_recordScript" /> <fmt:message key="script_jsp_recordScript" />
</button> <a href="createScript.jsp"><button id="createScript" </button><a href="createScript.jsp"><button id="createScript"
class="btn btn-primary" onclick="#" type="submit"> class="btn btn-primary" onclick="#" type="submit">
<fmt:message key="home-createScript" /> <fmt:message key="home-createScript" />
</button> </a> </button> </a>

View File

@ -112,6 +112,10 @@ function testRecordProxy() {
function saveScript() { function saveScript() {
var scriptName = document.getElementsByName("scriptname")[0].value; var scriptName = document.getElementsByName("scriptname")[0].value;
if (scriptName == undefined || scriptName == "") {
information("need a script name!");
return;
}
$.post("saveScriptRecorded", { $.post("saveScriptRecorded", {
scriptName : scriptName, scriptName : scriptName,
port : server.port, port : server.port,
@ -126,5 +130,7 @@ function saveScript() {
$('#fileName').hide(); $('#fileName').hide();
} }
loadScript(table, 2); loadScript(table, 2);
}).error(function(){
information("failed to connect server");
}); });
} }

View File

@ -2,7 +2,12 @@ $('.btn-setting').click(function(e) {
e.preventDefault(); e.preventDefault();
$('#myModal').modal('show'); $('#myModal').modal('show');
}); });
window.onbeforeunload = function() {
if(startRecordingSuccess){
stopRecording();
}else if(startServerSuccess)
stopServer();
}
function dismiss(container){ function dismiss(container){
$(container).modal('hide'); $(container).modal('hide');
if(startRecordingSuccess){ if(startRecordingSuccess){

View File

@ -33,7 +33,7 @@ function submitScript(pages,usePlugins,formData) {
var scriptName = $("#scriptName").val(); var scriptName = $("#scriptName").val();
if (scriptName == undefined || scriptName == "") { if (scriptName == undefined || scriptName == "") {
information("need a script name!") information("need a script name!");
return; return;
} }
var runScenarioModel = new RunScenarioModel(0,usePlugins,new Array(),pages); var runScenarioModel = new RunScenarioModel(0,usePlugins,new Array(),pages);