From ed62f1d8f05b8d6bb3d73f93c518a4ebb675d736 Mon Sep 17 00:00:00 2001 From: hmm Date: Fri, 5 Sep 2014 14:11:21 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=82script=20name=20can't=20be=20null=202?= =?UTF-8?q?.get=20the=20charset=20from=20html=203.close=20record=20server?= =?UTF-8?q?=20when=20web=20page=20closed?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../httpcapture/generator/ResponseHeader.java | 10 ++++ .../httpcapture/generator/ResponseParser.java | 47 +++++++++++++++---- Bench4Q-Web/src/main/webapp/script.jsp | 4 +- .../script/RecordScript/RecordScriptServer.js | 6 +++ .../script/RecordScript/RecordScriptUI.js | 7 ++- .../script/scriptManager/submitScript.js | 2 +- 6 files changed, 62 insertions(+), 14 deletions(-) diff --git a/Bench4Q-Recorder/src/main/java/org/bench4q/recorder/httpcapture/generator/ResponseHeader.java b/Bench4Q-Recorder/src/main/java/org/bench4q/recorder/httpcapture/generator/ResponseHeader.java index 567e527c..bce7d1fb 100644 --- a/Bench4Q-Recorder/src/main/java/org/bench4q/recorder/httpcapture/generator/ResponseHeader.java +++ b/Bench4Q-Recorder/src/main/java/org/bench4q/recorder/httpcapture/generator/ResponseHeader.java @@ -1,5 +1,6 @@ package org.bench4q.recorder.httpcapture.generator; +import java.nio.charset.Charset; import java.util.HashMap; public class ResponseHeader { @@ -18,6 +19,7 @@ public class ResponseHeader { private String contentEncoding; private int contentLength; private String transferEncoding; + private Charset contentCharset; public int getRespCode() { return respCode; @@ -81,4 +83,12 @@ public class ResponseHeader { public void setTransferEncoding(String transferEncoding) { this.transferEncoding = transferEncoding; } + + public Charset getContentCharset() { + return contentCharset; + } + + public void setContentCharset(Charset charset) { + this.contentCharset = charset; + } } diff --git a/Bench4Q-Recorder/src/main/java/org/bench4q/recorder/httpcapture/generator/ResponseParser.java b/Bench4Q-Recorder/src/main/java/org/bench4q/recorder/httpcapture/generator/ResponseParser.java index 1abd5026..af2d6b02 100644 --- a/Bench4Q-Recorder/src/main/java/org/bench4q/recorder/httpcapture/generator/ResponseParser.java +++ b/Bench4Q-Recorder/src/main/java/org/bench4q/recorder/httpcapture/generator/ResponseParser.java @@ -7,6 +7,7 @@ import java.nio.charset.Charset; import org.apache.log4j.Logger; import org.bench4q.recorder.httpcapture.ResponseModel; +import org.jsoup.Jsoup; public class ResponseParser { private Logger logger = Logger.getLogger(ResponseParser.class); @@ -80,7 +81,7 @@ public class ResponseParser { } private String parseContentType(HttpURLConnection httpURLConnection) { - + String ret = null; String value = httpURLConnection.getHeaderField("Content-Type"); int pos; @@ -96,14 +97,11 @@ public class ResponseParser { return ret; } - private String parseTransferEncoding(HttpURLConnection httpURLConnection) { - + return httpURLConnection.getHeaderField("Transfer-Encoding"); } - - private void parseResponseBody() { ContentDecoder contentDecoder = ContentDecoder.createDecoder(this .getResponseHeader().getContentEncoding()); @@ -119,25 +117,54 @@ public class ResponseParser { try { charset = Charset.forName(this.getResponseHeader().getCharset()); } 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); } 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 .getResponseHeader().getContentEncoding()); Charset charset = null; try { charset = Charset.forName(this.getResponseHeader().getCharset()); } catch (Exception e) { - charset = Charset.forName("utf-8"); + charset = this.getResponseHeader().getContentCharset(); + if(charset == null) + charset = Charset.forName("utf-8"); logger.error(e, e); } byte[] responseBytes = responseBody.getBytes(charset); responseBytes = contentEncoder.encoderContent(responseBytes); - + return responseBytes; } } diff --git a/Bench4Q-Web/src/main/webapp/script.jsp b/Bench4Q-Web/src/main/webapp/script.jsp index 8ded5155..dae8fe94 100644 --- a/Bench4Q-Web/src/main/webapp/script.jsp +++ b/Bench4Q-Web/src/main/webapp/script.jsp @@ -118,9 +118,9 @@ body { diff --git a/Bench4Q-Web/src/main/webapp/script/RecordScript/RecordScriptServer.js b/Bench4Q-Web/src/main/webapp/script/RecordScript/RecordScriptServer.js index 99f40acf..fd11022d 100644 --- a/Bench4Q-Web/src/main/webapp/script/RecordScript/RecordScriptServer.js +++ b/Bench4Q-Web/src/main/webapp/script/RecordScript/RecordScriptServer.js @@ -112,6 +112,10 @@ function testRecordProxy() { function saveScript() { var scriptName = document.getElementsByName("scriptname")[0].value; + if (scriptName == undefined || scriptName == "") { + information("need a script name!"); + return; + } $.post("saveScriptRecorded", { scriptName : scriptName, port : server.port, @@ -126,5 +130,7 @@ function saveScript() { $('#fileName').hide(); } loadScript(table, 2); + }).error(function(){ + information("failed to connect server"); }); } diff --git a/Bench4Q-Web/src/main/webapp/script/RecordScript/RecordScriptUI.js b/Bench4Q-Web/src/main/webapp/script/RecordScript/RecordScriptUI.js index 9a91300d..c018059d 100644 --- a/Bench4Q-Web/src/main/webapp/script/RecordScript/RecordScriptUI.js +++ b/Bench4Q-Web/src/main/webapp/script/RecordScript/RecordScriptUI.js @@ -2,7 +2,12 @@ $('.btn-setting').click(function(e) { e.preventDefault(); $('#myModal').modal('show'); }); - +window.onbeforeunload = function() { + if(startRecordingSuccess){ + stopRecording(); + }else if(startServerSuccess) + stopServer(); +} function dismiss(container){ $(container).modal('hide'); if(startRecordingSuccess){ diff --git a/Bench4Q-Web/src/main/webapp/script/scriptManager/submitScript.js b/Bench4Q-Web/src/main/webapp/script/scriptManager/submitScript.js index 67316d6d..39bf3940 100644 --- a/Bench4Q-Web/src/main/webapp/script/scriptManager/submitScript.js +++ b/Bench4Q-Web/src/main/webapp/script/scriptManager/submitScript.js @@ -33,7 +33,7 @@ function submitScript(pages,usePlugins,formData) { var scriptName = $("#scriptName").val(); if (scriptName == undefined || scriptName == "") { - information("need a script name!") + information("need a script name!"); return; } var runScenarioModel = new RunScenarioModel(0,usePlugins,new Array(),pages);