From 9a668ddf5e8f19c0758c8ff29f58f3de72523f45 Mon Sep 17 00:00:00 2001 From: hmm Date: Tue, 2 Sep 2014 14:36:43 +0800 Subject: [PATCH] add chunked encoding to response --- .../RecordScriptTestCase/baidu.html | 103 +++------------- .../recorder/httpcapture/RequestHandler.java | 61 +++++++--- .../httpcapture/generator/ResponseParser.java | 61 +--------- .../bench4q/recorder/Test_RequestHandler.java | 110 +++++++++--------- 4 files changed, 120 insertions(+), 215 deletions(-) diff --git a/Bench4Q-Recorder/RecordScriptTestCase/baidu.html b/Bench4Q-Recorder/RecordScriptTestCase/baidu.html index 5f21f00a..373ced8a 100644 --- a/Bench4Q-Recorder/RecordScriptTestCase/baidu.html +++ b/Bench4Q-Recorder/RecordScriptTestCase/baidu.html @@ -1,88 +1,17 @@ -百度一下,你就知道

显示卡片宝箱换肤消息 设为首页 wonanguo11

加载中,精彩马上呈现
©2014 Baidu 使用百度前必读 京ICP证030173号
+ + + +> headerMap, - int responseLen) { + ResponseModel responseModel) { StringBuilder sb = new StringBuilder(); for (String key : headerMap.keySet()) { @@ -216,20 +218,45 @@ public class RequestHandler implements Runnable { sb.append(changeList2String(headerMap.get(key)) + "\r\n"); continue; } - if (key.equals("Transfer-Encoding")) { + if(key.equals("Content-Length") && headerMap.get(key).get(0).equals("-1")){ + sb.append("Content-Length: "+responseModel.getResponse().length +"\r\n"); continue; } + if (key.equals("Transfer-Encoding") && headerMap.get(key).get(0).equals("chunked")) { + responseModel.setResponse(buildResponseBodyToChunked(responseModel.getResponse())); + } sb.append(key + ": " + changeList2String(headerMap.get(key)) + "\r\n"); } - // change response length - if (headerMap.get("Content-Length") == null) { - sb.append("Content-Length: " + responseLen).append("\r\n"); - } sb.append("\r\n"); return sb.toString().getBytes(); } + private byte[] buildResponseBodyToChunked(byte[] responseBytes) { + ChunkedOutputStream chunkedOutputStream = null; + try { + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + chunkedOutputStream = new ChunkedOutputStream(byteArrayOutputStream); + chunkedOutputStream.write(responseBytes,0,responseBytes.length); + chunkedOutputStream.finish(); + chunkedOutputStream.flush(); + + return byteArrayOutputStream.toByteArray(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } finally{ + if(chunkedOutputStream != null){ + try { + chunkedOutputStream.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } + return responseBytes; + } private String changeList2String(List headers) { if (headers == null) return ""; 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 856a8109..b9bd8f58 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 @@ -8,6 +8,7 @@ import java.util.List; import java.util.Map; import org.apache.commons.httpclient.ChunkedInputStream; +import org.apache.commons.httpclient.ChunkedOutputStream; import org.apache.commons.io.output.ByteArrayOutputStream; import org.apache.log4j.Logger; import org.bench4q.recorder.httpcapture.ResponseModel; @@ -71,29 +72,6 @@ public class ResponseParser { return result; } - private String preprocess(String respString) { - return respString.toLowerCase(); - } - - private String parseContentLength(Map> map) { - - List values = map.get("Content-Length:"); - if (values != null) { - return values.get(0); - } - return "-1"; - } - - private String parseContentEncoding(Map> map) { - - String ret = ""; - List values = map.get("Content-Encoding:"); - if (values != null) { - return values.get(0); - } - return null; - } - private String parseCharset(HttpURLConnection httpURLConnection) { String ret = null; @@ -129,39 +107,7 @@ public class ResponseParser { return httpURLConnection.getHeaderField("Transfer-Encoding"); } - private byte[] buildResponseBody() { - - ByteArrayInputStream in = new ByteArrayInputStream(this.getResponse(), - 0, this.getResponse().length); - 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 @@ -194,6 +140,9 @@ public class ResponseParser { charset = Charset.forName("utf-8"); logger.error(e, e); } - return contentEncoder.encoderContent(responseBody.getBytes(charset)); + byte[] responseBytes = responseBody.getBytes(charset); + responseBytes = contentEncoder.encoderContent(responseBytes); + + return responseBytes; } } diff --git a/Bench4Q-Recorder/src/test/java/org/bench4q/recorder/Test_RequestHandler.java b/Bench4Q-Recorder/src/test/java/org/bench4q/recorder/Test_RequestHandler.java index 6a5ea156..20018858 100644 --- a/Bench4Q-Recorder/src/test/java/org/bench4q/recorder/Test_RequestHandler.java +++ b/Bench4Q-Recorder/src/test/java/org/bench4q/recorder/Test_RequestHandler.java @@ -18,59 +18,59 @@ import org.junit.Test; public class Test_RequestHandler { private static final int PORT = 8910; - @Test - public void test() { - try { - ProxyServer proxyServer = new ProxyServer(PORT); - Bench4qCodeGenerator observer = new Bench4qCodeGenerator( - new Bench4qTestScriptAdapter(new RunScenarioModel())); - proxyServer.addObserver(observer); - RequestHandler requestHandler = new RequestHandler(proxyServer, - proxyServer.getServerSocket().accept()); - requestHandler.run(); - } catch (IOException e) { - e.printStackTrace(); - } - } - - @Test - public void test2() { - new SendRequest().run(); - } - - private static class SendRequest implements Runnable { - private HttpClient httpClient = new HttpClient(); - - @SuppressWarnings("deprecation") - public void run() { - HostConfiguration hostConfiguration = new HostConfiguration(); - hostConfiguration.setProxy("127.0.0.1", PORT); - httpClient.setHostConfiguration(hostConfiguration); -// CustomGetMethod get = new CustomGetMethod("http://www.baidu.com"); - GetMethod get = new GetMethod("http://www.baidu.com"); - - try { - 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"); - - int statusCode = this.httpClient.executeMethod(get); - System.out.println(statusCode); - System.out.println(new String(get.getResponseBody(),Charset.forName("utf-8"))); - } catch (URIException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (HttpException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - } - - } +// @Test +// public void test() { +// try { +// ProxyServer proxyServer = new ProxyServer(PORT); +// Bench4qCodeGenerator observer = new Bench4qCodeGenerator( +// new Bench4qTestScriptAdapter(new RunScenarioModel())); +// proxyServer.addObserver(observer); +// RequestHandler requestHandler = new RequestHandler(proxyServer, +// proxyServer.getServerSocket().accept()); +// requestHandler.run(); +// } catch (IOException e) { +// e.printStackTrace(); +// } +// } +// +// @Test +// public void test2() { +// new SendRequest().run(); +// } +// +// private static class SendRequest implements Runnable { +// private HttpClient httpClient = new HttpClient(); +// +// @SuppressWarnings("deprecation") +// public void run() { +// HostConfiguration hostConfiguration = new HostConfiguration(); +// hostConfiguration.setProxy("127.0.0.1", PORT); +// httpClient.setHostConfiguration(hostConfiguration); +//// CustomGetMethod get = new CustomGetMethod("http://www.baidu.com"); +// GetMethod get = new GetMethod("http://www.baidu.com"); +// +// try { +// 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"); +// +// int statusCode = this.httpClient.executeMethod(get); +// System.out.println(statusCode); +// System.out.println(new String(get.getResponseBody(),Charset.forName("utf-8"))); +// } catch (URIException e) { +// // TODO Auto-generated catch block +// e.printStackTrace(); +// } catch (HttpException e) { +// // TODO Auto-generated catch block +// e.printStackTrace(); +// } catch (IOException e) { +// // TODO Auto-generated catch block +// e.printStackTrace(); +// } +// +// } +// +// } }