encode response with gzip

This commit is contained in:
hmm 2014-09-01 10:42:05 +08:00
parent c0f3ac1daa
commit c52820b1ae
7 changed files with 113 additions and 11 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

@ -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

@ -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();