Skip to content

Commit

Permalink
fix: use UTF-8 as a default encoding in HTTP sampler
Browse files Browse the repository at this point in the history
In PR 5987 HTTP sampler encoded filenames with percent encoding,
however, it should not encode all the characters.

Fixes #6005
This is a fixup to #5987
  • Loading branch information
vlsi committed Jun 29, 2023
1 parent 143ac96 commit 7450e4b
Show file tree
Hide file tree
Showing 17 changed files with 583 additions and 301 deletions.
16 changes: 8 additions & 8 deletions bin/testfiles/HTMLParserTestFile_2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<queryString class="java.lang.String"></queryString>
<java.net.URL>file:testfiles/HTMLParserTestFile_2.html</java.net.URL>
</httpSample>
<httpSample s="true" lb="file:testfiles/HTMLParserTestFile_2.html-1" rc="200" rm="OK" tn="Thread Group 1-1" dt="text" de="" by="1321" sc="1" ec="0" ng="1" na="1">
<httpSample s="true" lb="file:testfiles/HTMLParserTestFile_2.html-1" rc="200" rm="OK" tn="Thread Group 1-1" dt="text" de="UTF-8" by="1321" sc="1" ec="0" ng="1" na="1">
<responseHeader class="java.lang.String"></responseHeader>
<requestHeader class="java.lang.String"></requestHeader>
<responseFile class="java.lang.String"></responseFile>
Expand All @@ -29,7 +29,7 @@
<queryString class="java.lang.String"></queryString>
<java.net.URL>file:testfiles/HTMLParserTestFile_2_files/halfbanner.htm</java.net.URL>
</httpSample>
<httpSample s="true" lb="file:testfiles/HTMLParserTestFile_2_files/halfbanner.htm-1" rc="200" rm="OK" tn="Thread Group 1-1" dt="text" de="" by="5421" sc="1" ec="0" ng="0" na="0">
<httpSample s="true" lb="file:testfiles/HTMLParserTestFile_2_files/halfbanner.htm-1" rc="200" rm="OK" tn="Thread Group 1-1" dt="text" de="UTF-8" by="5421" sc="1" ec="0" ng="0" na="0">
<responseHeader class="java.lang.String"></responseHeader>
<requestHeader class="java.lang.String"></requestHeader>
<responseFile class="java.lang.String"></responseFile>
Expand All @@ -46,7 +46,7 @@
<queryString class="java.lang.String"></queryString>
<java.net.URL>file:testfiles/HTMLParserTestFile_2_files/halfbanner.htm</java.net.URL>
</httpSample>
<httpSample s="true" lb="file:testfiles/HTMLParserTestFile_2.html-3" rc="200" rm="OK" tn="Thread Group 1-1" dt="text" de="" by="8584" sc="1" ec="0" ng="1" na="1">
<httpSample s="true" lb="file:testfiles/HTMLParserTestFile_2.html-3" rc="200" rm="OK" tn="Thread Group 1-1" dt="text" de="UTF-8" by="8584" sc="1" ec="0" ng="1" na="1">
<responseHeader class="java.lang.String"></responseHeader>
<requestHeader class="java.lang.String"></requestHeader>
<responseFile class="java.lang.String"></responseFile>
Expand All @@ -55,7 +55,7 @@
<queryString class="java.lang.String"></queryString>
<java.net.URL>file:testfiles/HTMLParserTestFile_2_files/jakarta-logo.gif</java.net.URL>
</httpSample>
<httpSample s="true" lb="file:testfiles/HTMLParserTestFile_2.html-4" rc="200" rm="OK" tn="Thread Group 1-1" dt="text" de="" by="8886" sc="1" ec="0" ng="1" na="1">
<httpSample s="true" lb="file:testfiles/HTMLParserTestFile_2.html-4" rc="200" rm="OK" tn="Thread Group 1-1" dt="text" de="UTF-8" by="8886" sc="1" ec="0" ng="1" na="1">
<responseHeader class="java.lang.String"></responseHeader>
<requestHeader class="java.lang.String"></requestHeader>
<responseFile class="java.lang.String"></responseFile>
Expand All @@ -64,7 +64,7 @@
<queryString class="java.lang.String"></queryString>
<java.net.URL>file:testfiles/HTMLParserTestFile_2_files/logo.jpg</java.net.URL>
</httpSample>
<httpSample s="true" lb="file:testfiles/HTMLParserTestFile_2.html-5" rc="200" rm="OK" tn="Thread Group 1-1" dt="text" de="" by="3064" sc="1" ec="0" ng="1" na="1">
<httpSample s="true" lb="file:testfiles/HTMLParserTestFile_2.html-5" rc="200" rm="OK" tn="Thread Group 1-1" dt="text" de="UTF-8" by="3064" sc="1" ec="0" ng="1" na="1">
<responseHeader class="java.lang.String"></responseHeader>
<requestHeader class="java.lang.String"></requestHeader>
<responseFile class="java.lang.String"></responseFile>
Expand All @@ -73,7 +73,7 @@
<queryString class="java.lang.String"></queryString>
<java.net.URL>file:testfiles/HTMLParserTestFile_2_files/http-config-example.png</java.net.URL>
</httpSample>
<httpSample s="true" lb="file:testfiles/HTMLParserTestFile_2.html-6" rc="200" rm="OK" tn="Thread Group 1-1" dt="text" de="" by="2395" sc="1" ec="0" ng="1" na="1">
<httpSample s="true" lb="file:testfiles/HTMLParserTestFile_2.html-6" rc="200" rm="OK" tn="Thread Group 1-1" dt="text" de="UTF-8" by="2395" sc="1" ec="0" ng="1" na="1">
<responseHeader class="java.lang.String"></responseHeader>
<requestHeader class="java.lang.String"></requestHeader>
<responseFile class="java.lang.String"></responseFile>
Expand All @@ -82,7 +82,7 @@
<queryString class="java.lang.String"></queryString>
<java.net.URL>file:testfiles/HTMLParserTestFile_2_files/scoping1.png</java.net.URL>
</httpSample>
<httpSample s="true" lb="file:testfiles/HTMLParserTestFile_2.html-7" rc="200" rm="OK" tn="Thread Group 1-1" dt="text" de="" by="2641" sc="1" ec="0" ng="1" na="1">
<httpSample s="true" lb="file:testfiles/HTMLParserTestFile_2.html-7" rc="200" rm="OK" tn="Thread Group 1-1" dt="text" de="UTF-8" by="2641" sc="1" ec="0" ng="1" na="1">
<responseHeader class="java.lang.String"></responseHeader>
<requestHeader class="java.lang.String"></requestHeader>
<responseFile class="java.lang.String"></responseFile>
Expand All @@ -91,7 +91,7 @@
<queryString class="java.lang.String"></queryString>
<java.net.URL>file:testfiles/HTMLParserTestFile_2_files/scoping2.png</java.net.URL>
</httpSample>
<httpSample s="true" lb="file:testfiles/HTMLParserTestFile_2.html-8" rc="200" rm="OK" tn="Thread Group 1-1" dt="text" de="" by="3260" sc="1" ec="0" ng="1" na="1">
<httpSample s="true" lb="file:testfiles/HTMLParserTestFile_2.html-8" rc="200" rm="OK" tn="Thread Group 1-1" dt="text" de="UTF-8" by="3260" sc="1" ec="0" ng="1" na="1">
<responseHeader class="java.lang.String"></responseHeader>
<requestHeader class="java.lang.String"></requestHeader>
<responseFile class="java.lang.String"></responseFile>
Expand Down
4 changes: 2 additions & 2 deletions bin/testfiles/TEST_HTTP.jmx
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,7 @@ mirrorServer.start();</stringProp>
<stringProp name="cacheKey">true</stringProp>
<stringProp name="filename"></stringProp>
<stringProp name="parameters"></stringProp>
<stringProp name="script">String textToCheck = &apos;Content-Disposition: form-data; name=&quot;?_param&quot;&apos;;
<stringProp name="script">String textToCheck = &apos;Content-Disposition: form-data; name=&quot;安_param&quot;&apos;;
if(prev.getSamplerData().indexOf(textToCheck) &lt; 0) {
AssertionResult.setFailure(true);
AssertionResult.setFailureMessage(&quot;Request does not contains &apos;&quot;+textToCheck+&quot;&apos;&quot;);
Expand Down Expand Up @@ -1005,7 +1005,7 @@ if(prev.getSamplerData().indexOf(textToCheck) &lt; 0) {
<ResponseAssertion guiclass="AssertionGui" testclass="ResponseAssertion" testname="Response Assertion" enabled="true">
<collectionProp name="Asserion.test_strings">
<stringProp name="-1049865380">nv_contentType</stringProp>
<stringProp name="817335912">text/plain</stringProp>
<stringProp name="1947100436">text/plain; charset=UTF-8</stringProp>
</collectionProp>
<stringProp name="Assertion.test_field">Assertion.response_data</stringProp>
<boolProp name="Assertion.assume_success">false</boolProp>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public class SampleResult implements Serializable, Cloneable, Searchable {
* The default encoding to be used if not overridden.
* The value is ISO-8859-1.
*/
public static final String DEFAULT_HTTP_ENCODING = StandardCharsets.ISO_8859_1.name();
public static final String DEFAULT_HTTP_ENCODING = StandardCharsets.UTF_8.name();

private static final String OK_CODE = Integer.toString(HttpURLConnection.HTTP_OK);
private static final String OK_MSG = "OK"; // $NON-NLS-1$
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import org.apache.jmeter.protocol.http.control.gui.HttpTestSampleGui;
import org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase;
import org.apache.jmeter.protocol.http.sampler.HTTPSamplerFactory;
import org.apache.jmeter.protocol.http.sampler.PostWriter;
import org.apache.jmeter.protocol.http.util.ConversionUtils;
import org.apache.jmeter.protocol.http.util.GraphQLRequestParamUtils;
import org.apache.jmeter.protocol.http.util.HTTPConstants;
Expand Down Expand Up @@ -218,24 +217,10 @@ protected void computeFromPostBody(HTTPSamplerBase sampler,
final String contentType = request.getContentType();
MultipartUrlConfig urlConfig = request.getMultipartConfig(contentType);
String contentEncoding = sampler.getContentEncoding();
// Get the post data using the content encoding of the request
String postData = null;
if (log.isDebugEnabled()) {
if(!StringUtils.isEmpty(contentEncoding)) {
log.debug("Using encoding {} for request body", contentEncoding);
}
else {
log.debug("No encoding found, using JRE default encoding for request body");
}
}

log.debug("Using encoding {} for request body", contentEncoding);

if (!StringUtils.isEmpty(contentEncoding)) {
postData = new String(request.getRawPostData(), contentEncoding);
} else {
// Use default encoding
postData = new String(request.getRawPostData(), PostWriter.ENCODING);
}
// Get the post data using the content encoding of the request
String postData = new String(request.getRawPostData(), contentEncoding);

if (urlConfig != null) {
urlConfig.parseArguments(postData);
Expand Down Expand Up @@ -436,16 +421,7 @@ private static String getNumberedFormat(int httpSampleNameMode) {
* @param request {@link HttpRequestHdr}
*/
protected void computePath(HTTPSamplerBase sampler, HttpRequestHdr request) {
if(sampler.getContentEncoding() != null) {
sampler.setPath(request.getPath(), sampler.getContentEncoding());
}
else {
// Although the spec says UTF-8 should be used for encoding URL parameters,
// most browser use ISO-8859-1 for default if encoding is not known.
// We use null for contentEncoding, then the url parameters will be added
// with the value in the URL, and the "encode?" flag set to false
sampler.setPath(request.getPath(), null);
}
sampler.setPath(request.getPath(), sampler.getContentEncoding());
if (log.isDebugEnabled()) {
log.debug("Proxy: finished setting path: {}", sampler.getPath());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public byte[] parse(InputStream in) throws IOException {
inHeaders = false;
firstLine = false; // cannot be first line either
}
final String reqLine = line.toString(StandardCharsets.ISO_8859_1.name());
final String reqLine = line.toString(StandardCharsets.UTF_8.name());
if (firstLine) {
parseFirstLine(reqLine);
firstLine = false;
Expand Down
Loading

0 comments on commit 7450e4b

Please sign in to comment.