mstover1 2003/02/05 10:41:57
Modified: bin jmeter.properties
bin/testfiles AssertionTestPlan.jmx AuthManagerTestPlan.jmx
HeaderManagerTestPlan.jmx InterleaveTestPlan.jmx
InterleaveTestPlan2.jmx LoopTestPlan.jmx
Modification Manager.jmx OnceOnlyTestPlan.jmx
SimpleTestPlan.jmx
src/components/org/apache/jmeter/visualizers GraphModel.java
src/protocol/http/org/apache/jmeter/protocol/http/sampler
HTTPSampler.java
Log:
Fixing calculation of standard deviation (Jeremy Arnold)
Fixing spaces in URL Paths (Tom Wiedenhoft)
Revision Changes Path
1.54 +2 -2 jakarta-jmeter/bin/jmeter.properties
Index: jmeter.properties
===================================================================
RCS file: /home/cvs/jakarta-jmeter/bin/jmeter.properties,v
retrieving revision 1.53
retrieving revision 1.54
diff -u -r1.53 -r1.54
--- jmeter.properties 3 Feb 2003 16:08:35 -0000 1.53
+++ jmeter.properties 5 Feb 2003 18:41:55 -0000 1.54
@@ -69,11 +69,11 @@
#Logging levels for the logging categories in JMeter. Correct values are
FATAL_ERROR, ERROR, WARN, INFO, and DEBUG
log_level.jmeter=WARN
log_level.jmeter.engine=WARN
-log_level.jmeter.gui=DEBUG
+log_level.jmeter.gui=WARN
log_level.jmeter.elements=WARN
log_level.jmeter.util=WARN
log_level.jmeter.util.classfinder=WARN
-log_level.jmeter.test=DEBUG
+log_level.jmeter.test=WARN
log_level.jmeter.protocol.http=WARN
log_level.jmeter.protocol.ftp=WARN
log_level.jmeter.protocol.jdbc=WARN
1.3 +1 -0 jakarta-jmeter/bin/testfiles/AssertionTestPlan.jmx
Index: AssertionTestPlan.jmx
===================================================================
RCS file: /home/cvs/jakarta-jmeter/bin/testfiles/AssertionTestPlan.jmx,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- AssertionTestPlan.jmx 29 Dec 2002 14:20:54 -0000 1.2
+++ AssertionTestPlan.jmx 5 Feb 2003 18:41:55 -0000 1.3
@@ -33,6 +33,7 @@
<property xml:space="preserve" name="HTTPSampler.mimetype"/>
<property xml:space="preserve"
name="TestElement.gui_class">org.apache.jmeter.protocol.http.control.gui.HttpTestSampleGui</property>
<property xml:space="preserve" name="HTTPSampler.path">/</property>
+<property xml:space="preserve" name="HTTPSampler.encoded_path">/</property>
<collection class="java.util.ArrayList" name="AbstractSampler.assertions"/>
<property xml:space="preserve" name="HTTPSampler.FILE_FIELD"/>
<property xml:space="preserve"
name="TestElement.test_class">org.apache.jmeter.protocol.http.sampler.HTTPSampler</property>
1.3 +3 -0 jakarta-jmeter/bin/testfiles/AuthManagerTestPlan.jmx
Index: AuthManagerTestPlan.jmx
===================================================================
RCS file: /home/cvs/jakarta-jmeter/bin/testfiles/AuthManagerTestPlan.jmx,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- AuthManagerTestPlan.jmx 29 Dec 2002 14:20:55 -0000 1.2
+++ AuthManagerTestPlan.jmx 5 Feb 2003 18:41:55 -0000 1.3
@@ -63,6 +63,7 @@
<property xml:space="preserve" name="HTTPSampler.mimetype"/>
<property xml:space="preserve"
name="TestElement.gui_class">org.apache.jmeter.protocol.http.control.gui.HttpTestSampleGui</property>
<property xml:space="preserve" name="HTTPSampler.path">/secret/index.html</property>
+<property xml:space="preserve"
name="HTTPSampler.encoded_path">/secret/index.html</property>
<collection class="java.util.ArrayList" name="AbstractSampler.assertions"/>
<property xml:space="preserve" name="HTTPSampler.FILE_FIELD"/>
<property xml:space="preserve"
name="TestElement.test_class">org.apache.jmeter.protocol.http.sampler.HTTPSampler</property>
@@ -87,6 +88,7 @@
<property xml:space="preserve" name="HTTPSampler.mimetype"/>
<property xml:space="preserve"
name="TestElement.gui_class">org.apache.jmeter.protocol.http.control.gui.HttpTestSampleGui</property>
<property xml:space="preserve"
name="HTTPSampler.path">/secret/index2.html</property>
+<property xml:space="preserve"
name="HTTPSampler.encoded_path">/secret/index2.html</property>
<collection class="java.util.ArrayList" name="AbstractSampler.assertions"/>
<property xml:space="preserve" name="HTTPSampler.FILE_FIELD"/>
<property xml:space="preserve"
name="TestElement.test_class">org.apache.jmeter.protocol.http.sampler.HTTPSampler</property>
@@ -111,6 +113,7 @@
<property xml:space="preserve" name="HTTPSampler.mimetype"/>
<property xml:space="preserve"
name="TestElement.gui_class">org.apache.jmeter.protocol.http.control.gui.HttpTestSampleGui</property>
<property xml:space="preserve" name="HTTPSampler.path">/index.html</property>
+<property xml:space="preserve"
name="HTTPSampler.encoded_path">/index.html</property>
<collection class="java.util.ArrayList" name="AbstractSampler.assertions"/>
<property xml:space="preserve" name="HTTPSampler.FILE_FIELD"/>
<property xml:space="preserve"
name="TestElement.test_class">org.apache.jmeter.protocol.http.sampler.HTTPSampler</property>
1.3 +1 -0 jakarta-jmeter/bin/testfiles/HeaderManagerTestPlan.jmx
Index: HeaderManagerTestPlan.jmx
===================================================================
RCS file: /home/cvs/jakarta-jmeter/bin/testfiles/HeaderManagerTestPlan.jmx,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- HeaderManagerTestPlan.jmx 29 Dec 2002 14:20:55 -0000 1.2
+++ HeaderManagerTestPlan.jmx 5 Feb 2003 18:41:55 -0000 1.3
@@ -46,6 +46,7 @@
<property xml:space="preserve" name="HTTPSampler.mimetype"/>
<property xml:space="preserve"
name="TestElement.gui_class">org.apache.jmeter.protocol.http.control.gui.HttpTestSampleGui</property>
<property xml:space="preserve" name="HTTPSampler.path">/</property>
+<property xml:space="preserve" name="HTTPSampler.encoded_path">/</property>
<collection class="java.util.ArrayList" name="AbstractSampler.assertions"/>
<property xml:space="preserve" name="HTTPSampler.FILE_FIELD"/>
<property xml:space="preserve"
name="TestElement.test_class">org.apache.jmeter.protocol.http.sampler.HTTPSampler</property>
1.3 +3 -0 jakarta-jmeter/bin/testfiles/InterleaveTestPlan.jmx
Index: InterleaveTestPlan.jmx
===================================================================
RCS file: /home/cvs/jakarta-jmeter/bin/testfiles/InterleaveTestPlan.jmx,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- InterleaveTestPlan.jmx 29 Dec 2002 14:20:55 -0000 1.2
+++ InterleaveTestPlan.jmx 5 Feb 2003 18:41:55 -0000 1.3
@@ -56,6 +56,7 @@
<property xml:space="preserve" name="HTTPSampler.mimetype"/>
<property xml:space="preserve"
name="TestElement.gui_class">org.apache.jmeter.protocol.http.control.gui.HttpTestSampleGui</property>
<property xml:space="preserve" name="HTTPSampler.path">/site/news.html</property>
+<property xml:space="preserve"
name="HTTPSampler.encoded_path">/site/news.html</property>
<collection class="java.util.ArrayList" name="AbstractSampler.assertions"/>
<property xml:space="preserve" name="HTTPSampler.FILE_FIELD"/>
<property xml:space="preserve"
name="TestElement.test_class">org.apache.jmeter.protocol.http.sampler.HTTPSampler</property>
@@ -80,6 +81,7 @@
<property xml:space="preserve" name="HTTPSampler.mimetype"/>
<property xml:space="preserve"
name="TestElement.gui_class">org.apache.jmeter.protocol.http.control.gui.HttpTestSampleGui</property>
<property xml:space="preserve" name="HTTPSampler.path">/site/faqs.html</property>
+<property xml:space="preserve"
name="HTTPSampler.encoded_path">/site/faqs.html</property>
<collection class="java.util.ArrayList" name="AbstractSampler.assertions"/>
<property xml:space="preserve" name="HTTPSampler.FILE_FIELD"/>
<property xml:space="preserve"
name="TestElement.test_class">org.apache.jmeter.protocol.http.sampler.HTTPSampler</property>
@@ -104,6 +106,7 @@
<property xml:space="preserve" name="HTTPSampler.mimetype"/>
<property xml:space="preserve"
name="TestElement.gui_class">org.apache.jmeter.protocol.http.control.gui.HttpTestSampleGui</property>
<property xml:space="preserve" name="HTTPSampler.path">/gump</property>
+<property xml:space="preserve" name="HTTPSampler.encoded_path">/gump</property>
<collection class="java.util.ArrayList" name="AbstractSampler.assertions"/>
<property xml:space="preserve" name="HTTPSampler.FILE_FIELD"/>
<property xml:space="preserve"
name="TestElement.test_class">org.apache.jmeter.protocol.http.sampler.HTTPSampler</property>
1.3 +4 -0 jakarta-jmeter/bin/testfiles/InterleaveTestPlan2.jmx
Index: InterleaveTestPlan2.jmx
===================================================================
RCS file: /home/cvs/jakarta-jmeter/bin/testfiles/InterleaveTestPlan2.jmx,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- InterleaveTestPlan2.jmx 29 Dec 2002 14:20:55 -0000 1.2
+++ InterleaveTestPlan2.jmx 5 Feb 2003 18:41:55 -0000 1.3
@@ -63,6 +63,7 @@
<property xml:space="preserve" name="HTTPSampler.mimetype"/>
<property xml:space="preserve"
name="TestElement.gui_class">org.apache.jmeter.protocol.http.control.gui.HttpTestSampleGui</property>
<property xml:space="preserve" name="HTTPSampler.path">/</property>
+<property xml:space="preserve" name="HTTPSampler.encoded_path">/</property>
<collection class="java.util.ArrayList" name="AbstractSampler.assertions"/>
<property xml:space="preserve" name="HTTPSampler.FILE_FIELD"/>
<property xml:space="preserve"
name="TestElement.test_class">org.apache.jmeter.protocol.http.sampler.HTTPSampler</property>
@@ -87,6 +88,7 @@
<property xml:space="preserve" name="HTTPSampler.mimetype"/>
<property xml:space="preserve"
name="TestElement.gui_class">org.apache.jmeter.protocol.http.control.gui.HttpTestSampleGui</property>
<property xml:space="preserve"
name="HTTPSampler.path">/site/cvsindex.html</property>
+<property xml:space="preserve"
name="HTTPSampler.encoded_path">/site/cvsindex.html</property>
<collection class="java.util.ArrayList" name="AbstractSampler.assertions"/>
<property xml:space="preserve" name="HTTPSampler.FILE_FIELD"/>
<property xml:space="preserve"
name="TestElement.test_class">org.apache.jmeter.protocol.http.sampler.HTTPSampler</property>
@@ -119,6 +121,7 @@
<property xml:space="preserve" name="HTTPSampler.mimetype"/>
<property xml:space="preserve"
name="TestElement.gui_class">org.apache.jmeter.protocol.http.control.gui.HttpTestSampleGui</property>
<property xml:space="preserve" name="HTTPSampler.path">/site/bugs.html</property>
+<property xml:space="preserve"
name="HTTPSampler.encoded_path">/site/bugs.html</property>
<collection class="java.util.ArrayList" name="AbstractSampler.assertions"/>
<property xml:space="preserve" name="HTTPSampler.FILE_FIELD"/>
<property xml:space="preserve"
name="TestElement.test_class">org.apache.jmeter.protocol.http.sampler.HTTPSampler</property>
@@ -143,6 +146,7 @@
<property xml:space="preserve" name="HTTPSampler.mimetype"/>
<property xml:space="preserve"
name="TestElement.gui_class">org.apache.jmeter.protocol.http.control.gui.HttpTestSampleGui</property>
<property xml:space="preserve" name="HTTPSampler.path">/site/faqs.html</property>
+<property xml:space="preserve"
name="HTTPSampler.encoded_path">/site/faqs.html</property>
<collection class="java.util.ArrayList" name="AbstractSampler.assertions"/>
<property xml:space="preserve" name="HTTPSampler.FILE_FIELD"/>
<property xml:space="preserve"
name="TestElement.test_class">org.apache.jmeter.protocol.http.sampler.HTTPSampler</property>
1.3 +2 -0 jakarta-jmeter/bin/testfiles/LoopTestPlan.jmx
Index: LoopTestPlan.jmx
===================================================================
RCS file: /home/cvs/jakarta-jmeter/bin/testfiles/LoopTestPlan.jmx,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- LoopTestPlan.jmx 29 Dec 2002 14:20:55 -0000 1.2
+++ LoopTestPlan.jmx 5 Feb 2003 18:41:55 -0000 1.3
@@ -49,6 +49,7 @@
<property xml:space="preserve" name="HTTPSampler.mimetype"/>
<property xml:space="preserve"
name="TestElement.gui_class">org.apache.jmeter.protocol.http.control.gui.HttpTestSampleGui</property>
<property xml:space="preserve" name="HTTPSampler.path">/</property>
+<property xml:space="preserve" name="HTTPSampler.encoded_path">/</property>
<collection class="java.util.ArrayList" name="AbstractSampler.assertions"/>
<property xml:space="preserve" name="HTTPSampler.FILE_FIELD"/>
<property xml:space="preserve"
name="TestElement.test_class">org.apache.jmeter.protocol.http.sampler.HTTPSampler</property>
@@ -81,6 +82,7 @@
<property xml:space="preserve" name="HTTPSampler.mimetype"/>
<property xml:space="preserve"
name="TestElement.gui_class">org.apache.jmeter.protocol.http.control.gui.HttpTestSampleGui</property>
<property xml:space="preserve" name="HTTPSampler.path">/site/news.html</property>
+<property xml:space="preserve"
name="HTTPSampler.encoded_path">/site/news.html</property>
<collection class="java.util.ArrayList" name="AbstractSampler.assertions"/>
<property xml:space="preserve" name="HTTPSampler.FILE_FIELD"/>
<property xml:space="preserve"
name="TestElement.test_class">org.apache.jmeter.protocol.http.sampler.HTTPSampler</property>
No revision
No revision
1.3 +2 -0 jakarta-jmeter/bin/testfiles/OnceOnlyTestPlan.jmx
Index: OnceOnlyTestPlan.jmx
===================================================================
RCS file: /home/cvs/jakarta-jmeter/bin/testfiles/OnceOnlyTestPlan.jmx,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- OnceOnlyTestPlan.jmx 29 Dec 2002 14:20:55 -0000 1.2
+++ OnceOnlyTestPlan.jmx 5 Feb 2003 18:41:55 -0000 1.3
@@ -55,6 +55,7 @@
<property xml:space="preserve" name="HTTPSampler.mimetype"/>
<property xml:space="preserve"
name="TestElement.gui_class">org.apache.jmeter.protocol.http.control.gui.HttpTestSampleGui</property>
<property xml:space="preserve" name="HTTPSampler.path">/</property>
+<property xml:space="preserve" name="HTTPSampler.encoded_path">/</property>
<collection class="java.util.ArrayList" name="AbstractSampler.assertions"/>
<property xml:space="preserve" name="HTTPSampler.FILE_FIELD"/>
<property xml:space="preserve"
name="TestElement.test_class">org.apache.jmeter.protocol.http.sampler.HTTPSampler</property>
@@ -80,6 +81,7 @@
<property xml:space="preserve" name="HTTPSampler.mimetype"/>
<property xml:space="preserve"
name="TestElement.gui_class">org.apache.jmeter.protocol.http.control.gui.HttpTestSampleGui</property>
<property xml:space="preserve" name="HTTPSampler.path">/site/bugs.html</property>
+<property xml:space="preserve"
name="HTTPSampler.encoded_path">/site/bugs.html</property>
<collection class="java.util.ArrayList" name="AbstractSampler.assertions"/>
<property xml:space="preserve" name="HTTPSampler.FILE_FIELD"/>
<property xml:space="preserve"
name="TestElement.test_class">org.apache.jmeter.protocol.http.sampler.HTTPSampler</property>
1.3 +4 -0 jakarta-jmeter/bin/testfiles/SimpleTestPlan.jmx
Index: SimpleTestPlan.jmx
===================================================================
RCS file: /home/cvs/jakarta-jmeter/bin/testfiles/SimpleTestPlan.jmx,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- SimpleTestPlan.jmx 29 Dec 2002 14:20:55 -0000 1.2
+++ SimpleTestPlan.jmx 5 Feb 2003 18:41:56 -0000 1.3
@@ -55,6 +55,7 @@
<property xml:space="preserve" name="HTTPSampler.mimetype"/>
<property xml:space="preserve"
name="TestElement.gui_class">org.apache.jmeter.protocol.http.control.gui.HttpTestSampleGui</property>
<property xml:space="preserve" name="HTTPSampler.path">/ant/index.html</property>
+<property xml:space="preserve"
name="HTTPSampler.encoded_path">/ant/index.html</property>
<collection class="java.util.ArrayList" name="AbstractSampler.assertions"/>
<property xml:space="preserve" name="HTTPSampler.FILE_FIELD"/>
<property xml:space="preserve"
name="TestElement.test_class">org.apache.jmeter.protocol.http.sampler.HTTPSampler</property>
@@ -79,6 +80,7 @@
<property xml:space="preserve" name="HTTPSampler.mimetype"/>
<property xml:space="preserve"
name="TestElement.gui_class">org.apache.jmeter.protocol.http.control.gui.HttpTestSampleGui</property>
<property xml:space="preserve" name="HTTPSampler.path">/ant/antnews.html</property>
+<property xml:space="preserve"
name="HTTPSampler.encoded_path">/ant/antnews.html</property>
<collection class="java.util.ArrayList" name="AbstractSampler.assertions"/>
<property xml:space="preserve" name="HTTPSampler.FILE_FIELD"/>
<property xml:space="preserve"
name="TestElement.test_class">org.apache.jmeter.protocol.http.sampler.HTTPSampler</property>
@@ -110,6 +112,7 @@
<property xml:space="preserve" name="HTTPSampler.mimetype"/>
<property xml:space="preserve"
name="TestElement.gui_class">org.apache.jmeter.protocol.http.control.gui.HttpTestSampleGui</property>
<property xml:space="preserve" name="HTTPSampler.path">/log4j/index.html</property>
+<property xml:space="preserve"
name="HTTPSampler.encoded_path">/log4j/index.html</property>
<collection class="java.util.ArrayList" name="AbstractSampler.assertions"/>
<property xml:space="preserve" name="HTTPSampler.FILE_FIELD"/>
<property xml:space="preserve"
name="TestElement.test_class">org.apache.jmeter.protocol.http.sampler.HTTPSampler</property>
@@ -134,6 +137,7 @@
<property xml:space="preserve" name="HTTPSampler.mimetype"/>
<property xml:space="preserve"
name="TestElement.gui_class">org.apache.jmeter.protocol.http.control.gui.HttpTestSampleGui</property>
<property xml:space="preserve"
name="HTTPSampler.path">/log4j/docs/history.html</property>
+<property xml:space="preserve"
name="HTTPSampler.encoded_path">/log4j/docs/history.html</property>
<collection class="java.util.ArrayList" name="AbstractSampler.assertions"/>
<property xml:space="preserve" name="HTTPSampler.FILE_FIELD"/>
<property xml:space="preserve"
name="TestElement.test_class">org.apache.jmeter.protocol.http.sampler.HTTPSampler</property>
1.4 +19 -11
jakarta-jmeter/src/components/org/apache/jmeter/visualizers/GraphModel.java
Index: GraphModel.java
===================================================================
RCS file:
/home/cvs/jakarta-jmeter/src/components/org/apache/jmeter/visualizers/GraphModel.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- GraphModel.java 3 Feb 2003 14:29:00 -0000 1.3
+++ GraphModel.java 5 Feb 2003 18:41:56 -0000 1.4
@@ -2,7 +2,7 @@
* ====================================================================
* The Apache Software License, Version 1.1
*
- * Copyright (c) 2001 The Apache Software Foundation. All rights
+ * Copyright (c) 2001,2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -81,8 +81,8 @@
private String name;
private List samples;
private List listeners;
- private long averageSum = 0;
- private long variationSum = 0;
+ private long sum = 0;
+ private long sumOfSquares = 0;
private long counter = 0;
private long previous = 0;
private long max = 1;
@@ -247,8 +247,8 @@
public void clear()
{
samples.clear();
- averageSum = 0;
- variationSum = 0;
+ sum = 0;
+ sumOfSquares = 0;
counter = 0;
previous = 0;
max = 1;
@@ -310,11 +310,18 @@
{
max = sample;
}
- averageSum += sample;
- long average = averageSum / ++counter;
- variationSum += Math.pow(sample - average, 2);
- long deviation = (long) Math.pow(variationSum / counter, 0.5);
+ counter++;
+
+ sum += sample;
+ float average = ((float)sum) / counter;
+
+ sumOfSquares += sample * sample;
+ // Standard deviation is the mean of the squares minus the square
+ // of the mean
+ long deviation = (long)Math.sqrt(
+ (sumOfSquares / counter) - average * average);
+
float throughput = 0;
if (timeStamp - startTime > 0)
@@ -330,14 +337,15 @@
if (average > graphMax)
{
bigChange = true;
- graphMax = average * 3;
+ graphMax = (long)average * 3;
}
if (deviation > graphMax)
{
bigChange = true;
graphMax = deviation * 3;
}
- Sample s = new Sample(sample, average, deviation, throughput, !success);
+ Sample s = new Sample(sample, (long)average, deviation,
+ throughput, !success);
previous = sample;
current = s;
1.21 +1055
-1072jakarta-jmeter/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPSampler.java
Index: HTTPSampler.java
===================================================================
RCS file:
/home/cvs/jakarta-jmeter/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPSampler.java,v
retrieving revision 1.20
retrieving revision 1.21
diff -u -r1.20 -r1.21
--- HTTPSampler.java 5 Feb 2003 05:12:10 -0000 1.20
+++ HTTPSampler.java 5 Feb 2003 18:41:56 -0000 1.21
@@ -77,11 +77,16 @@
import org.apache.jmeter.samplers.Entry;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.testelement.TestElement;
-import org.apache.jmeter.util.JMeterUtils;
import org.apache.jmeter.util.SSLManager;
import org.apache.jorphan.util.JOrphanUtils;
import org.apache.log.Hierarchy;
import org.apache.log.Logger;
+import org.apache.oro.text.PatternCacheLRU;
+import org.apache.oro.text.regex.Perl5Compiler;
+import org.apache.oro.text.regex.Perl5Matcher;
+import org.apache.oro.text.regex.StringSubstitution;
+import org.apache.oro.text.regex.Substitution;
+import org.apache.oro.text.regex.Util;
/****************************************
* A sampler which understands all the parts necessary to read statistics about
* HTTP requests, including cookies and authentication.
@@ -92,1081 +97,1059 @@
***************************************/
public class HTTPSampler extends AbstractSampler
{
- public final static String HEADERS = "headers";
- public final static String HEADER = "header";
- public final static String ARGUMENTS = "HTTPsampler.Arguments";
- public final static String AUTH_MANAGER = "HTTPSampler.auth_manager";
- public final static String COOKIE_MANAGER = "HTTPSampler.cookie_manager";
- public final static String HEADER_MANAGER = "HTTPSampler.header_manager";
- public final static String MIMETYPE = "HTTPSampler.mimetype";
- public final static String DOMAIN = "HTTPSampler.domain";
- public final static String PORT = "HTTPSampler.port";
- public final static String METHOD = "HTTPSampler.method";
- public final static String PATH = "HTTPSampler.path";
- public final static String FOLLOW_REDIRECTS = "HTTPSampler.follow_redirects";
- public final static String PROTOCOL = "HTTPSampler.protocol";
- public final static String DEFAULT_PROTOCOL = "http";
- public final static String URL = "HTTPSampler.URL";
- public final static String POST = "POST";
- public final static String GET = "GET";
- public final static String USE_KEEPALIVE = "HTTPSampler.use_keepalive";
- public final static String FILE_NAME = "HTTPSampler.FILE_NAME";
- public final static String FILE_FIELD = "HTTPSampler.FILE_FIELD";
- public final static String FILE_DATA = "HTTPSampler.FILE_DATA";
- public final static String FILE_MIMETYPE = "HTTPSampler.FILE_MIMETYPE";
- public final static String CONTENT_TYPE = "HTTPSampler.CONTENT_TYPE";
- public final static String NORMAL_FORM = "normal_form";
- public final static String MULTIPART_FORM = "multipart_form";
- private static final int MAX_REDIRECTS=10;
- protected static String encoding = "iso-8859-1";
- private static final PostWriter postWriter = new PostWriter();
- transient protected HttpURLConnection conn;
- private int connectionTries = 0;
- public void setFileField(String value)
- {
- setProperty(FILE_FIELD, value);
- }
- public String getFileField()
- {
- return getPropertyAsString(FILE_FIELD);
- }
- public void setFilename(String value)
- {
- setProperty(FILE_NAME, value);
- }
- public String getFilename()
- {
- return getPropertyAsString(FILE_NAME);
- }
- public void setProtocol(String value)
- {
- setProperty(PROTOCOL, value);
- }
- public String getProtocol()
- {
- String protocol= getPropertyAsString(PROTOCOL);
- if (protocol==null || protocol.equals(""))
- {
- return DEFAULT_PROTOCOL;
- }
- else return protocol;
- }
- /**
- * Sets the Path attribute of the UrlConfig object
- *
- *@param path The new Path value
- */
- public void setPath(String path)
- {
- if (GET.equals(getMethod()))
- {
- int index = path.indexOf("?");
- if (index > -1)
- {
- setProperty(PATH, path.substring(0, index));
- parseArguments(path.substring(index + 1));
- }
- else
- {
- setProperty(PATH, path);
- }
- }
- else
- {
- setProperty(PATH, path);
- }
- }
- public String getPath()
- {
- return getPropertyAsString(PATH);
- }
- public void setFollowRedirects(boolean value)
- {
- setProperty(FOLLOW_REDIRECTS, new Boolean(value));
- }
- public boolean getFollowRedirects()
- {
- return getPropertyAsBoolean(FOLLOW_REDIRECTS);
- }
- public void setMethod(String value)
- {
- setProperty(METHOD, value);
- }
- public String getMethod()
- {
- return getPropertyAsString(METHOD);
- }
- public void setUseKeepAlive(boolean value)
- {
- setProperty(USE_KEEPALIVE, new Boolean(value));
- }
- public boolean getUseKeepAlive()
- {
- return getPropertyAsBoolean(USE_KEEPALIVE);
- }
- public void addEncodedArgument(String name, String value, String metaData)
- {
- Arguments args = getArguments();
- HTTPArgument arg = new HTTPArgument(name, value, metaData, true);
- if (arg.getName().equals(arg.getEncodedName())
- && arg.getValue().equals(arg.getEncodedValue()))
- {
- arg.setAlwaysEncode(false);
- }
- args.addArgument(arg);
- }
- public void addArgument(String name, String value)
- {
- Arguments args = this.getArguments();
- args.addArgument(new HTTPArgument(name, value));
- }
- public void addArgument(String name, String value, String metadata)
- {
- Arguments args = this.getArguments();
- args.addArgument(new HTTPArgument(name, value, metadata));
- }
- public void setPort(int value)
- {
- setProperty(PORT, new Integer(value));
- }
- public int getPort()
- {
- int port = getPropertyAsInt(PORT);
- if (port == 0)
- {
- if("https".equalsIgnoreCase(getProtocol()))
- {
- return 443;
- }
- return 80;
- }
- return port;
- }
- public void setDomain(String value)
- {
- setProperty(DOMAIN, value);
- }
- public String getDomain()
- {
- return (String) getProperty(DOMAIN);
- }
- public void setArguments(Arguments value)
- {
- setProperty(ARGUMENTS, value);
- }
- public Arguments getArguments()
- {
- return (Arguments) getProperty(ARGUMENTS);
- }
- public void setAuthManager(AuthManager value)
- {
- setProperty(AUTH_MANAGER, value);
- }
- public AuthManager getAuthManager()
- {
- return (AuthManager) getProperty(AUTH_MANAGER);
- }
- public void setHeaderManager(HeaderManager value)
- {
- setProperty(HEADER_MANAGER, value);
- }
- public HeaderManager getHeaderManager()
- {
- return (HeaderManager) getProperty(HEADER_MANAGER);
- }
- public void setCookieManager(CookieManager value)
- {
- setProperty(COOKIE_MANAGER, value);
- }
- public CookieManager getCookieManager()
- {
- return (CookieManager) getProperty(COOKIE_MANAGER);
- }
- public void setMimetype(String value)
- {
- setProperty(MIMETYPE, value);
- }
- public String getMimetype()
- {
- return (String) getProperty(MIMETYPE);
- }
- protected void addCustomTestElement(TestElement element)
- {
- if (element instanceof Arguments)
- {
- if (getProperty(ARGUMENTS) != null)
- {
- ((Arguments)
getProperty(ARGUMENTS)).addTestElement(element);
- }
- else
- {
- setProperty(ARGUMENTS, element);
- }
- }
- else if (element instanceof AuthManager)
- {
- if (getProperty(AUTH_MANAGER) != null)
- {
- ((TestElement)
getProperty(AUTH_MANAGER)).addTestElement(element);
- }
- else
- {
- setProperty(AUTH_MANAGER, element);
- }
- }
- else if (element instanceof CookieManager)
- {
- if (getProperty(COOKIE_MANAGER) != null)
- {
- ((TestElement)
getProperty(COOKIE_MANAGER)).addTestElement(element);
- }
- else
- {
- setProperty(COOKIE_MANAGER, element);
- }
- }
- else if (element instanceof HeaderManager)
- {
- if (getProperty(HEADER_MANAGER) != null)
- {
- ((TestElement)
getProperty(HEADER_MANAGER)).addTestElement(element);
- }
- else
- {
- setProperty(HEADER_MANAGER, element);
- }
- }
- else if (
- element instanceof HTTPSampler || element instanceof
ConfigTestElement)
- {
- this.mergeIn(element);
- }
- }
- /****************************************
- * !ToDo (Field description)
- ***************************************/
- protected final static String NON_HTTP_RESPONSE_CODE =
- "Non HTTP response code";
- /****************************************
- * !ToDo (Field description)
- ***************************************/
- protected final static String NON_HTTP_RESPONSE_MESSAGE =
- "Non HTTP response message";
- transient private static Logger log =
- Hierarchy.getDefaultHierarchy().getLoggerFor("jmeter.protocol.http");
- /****************************************
- * Holds a list of URLs sampled - so we're not flooding stdout with debug
- * information
- ***************************************/
- private ArrayList m_sampledURLs = new ArrayList();
- /****************************************
- * Constructor for the HTTPSampler object
- ***************************************/
- public HTTPSampler()
- {
- setArguments(new Arguments());
- }
- public HTTPSampler(URL u)
- {
- setMethod(GET);
- setDomain(u.getHost());
- setPath(u.getPath());
- setPort(u.getPort());
- setProtocol(u.getProtocol());
- parseArguments(u.getQuery());
- setFollowRedirects(true);
- setUseKeepAlive(true);
- setArguments(new Arguments());
- }
- /****************************************
- * Do a sampling and return its results.
- *
- *@param e <code>Entry</code> to be sampled
- *@return results of the sampling
- *@see
org.apache.jmeter.protocol.http.sampler.HTTPSampler.sample(org.apache.jmeter.samplers.Entry,
- * int)
- ***************************************/
- public SampleResult sample(Entry e)
- {
- return sample(0);
- }
- public SampleResult sample()
- {
- return sample(0);
- }
- /**
- * !ToDoo (Method description)
- *
- *@return !ToDo (Return description)
- *@exception MalformedURLException !ToDo (Exception description)
- */
- public URL getUrl() throws MalformedURLException
- {
- String pathAndQuery = null;
- if (this.getMethod().equals(HTTPSampler.GET)
- && getQueryString().length() > 0)
- {
- if (this.getPath().indexOf("?") > -1)
- {
- pathAndQuery = this.getPath() + "&" + getQueryString();
- }
- else
- {
- pathAndQuery = this.getPath() + "?" + getQueryString();
- }
- }
- else
- {
- pathAndQuery = this.getPath();
- }
- if (!pathAndQuery.startsWith("/"))
- {
- pathAndQuery = "/" + pathAndQuery;
- }
- if (getPort() == 0)
- {
- return new URL(getProtocol(), getDomain(), pathAndQuery);
- }
- else
- {
- return new URL(
- getProtocol(),
- getPropertyAsString(HTTPSampler.DOMAIN),
- getPort(),
- pathAndQuery);
- }
- }
- /**
- * Gets the QueryString attribute of the UrlConfig object
- *
- *@return The QueryString value
- */
- public String getQueryString()
- {
- StringBuffer buf = new StringBuffer();
- Iterator iter = getArguments().iterator();
- boolean first = true;
- while (iter.hasNext())
- {
- HTTPArgument item = (HTTPArgument) iter.next();
- if (!first)
- {
- buf.append("&");
- }
- else
- {
- first = false;
- }
- buf.append(item.getEncodedName());
- if (item.getMetaData() == null)
- {
- buf.append("=");
- }
- else
- {
- buf.append(item.getMetaData());
- }
- buf.append(item.getEncodedValue());
- }
- return buf.toString();
- }
+ public final static String HEADERS = "headers";
+ public final static String HEADER = "header";
+ public final static String ARGUMENTS = "HTTPsampler.Arguments";
+ public final static String AUTH_MANAGER = "HTTPSampler.auth_manager";
+ public final static String COOKIE_MANAGER = "HTTPSampler.cookie_manager";
+ public final static String HEADER_MANAGER = "HTTPSampler.header_manager";
+ public final static String MIMETYPE = "HTTPSampler.mimetype";
+ public final static String DOMAIN = "HTTPSampler.domain";
+ public final static String PORT = "HTTPSampler.port";
+ public final static String METHOD = "HTTPSampler.method";
+ public final static String PATH = "HTTPSampler.path";
+ public final static String FOLLOW_REDIRECTS = "HTTPSampler.follow_redirects";
+ public final static String PROTOCOL = "HTTPSampler.protocol";
+ public final static String DEFAULT_PROTOCOL = "http";
+ public final static String URL = "HTTPSampler.URL";
+ public final static String POST = "POST";
+ public final static String GET = "GET";
+ public final static String USE_KEEPALIVE = "HTTPSampler.use_keepalive";
+ public final static String FILE_NAME = "HTTPSampler.FILE_NAME";
+ public final static String FILE_FIELD = "HTTPSampler.FILE_FIELD";
+ public final static String FILE_DATA = "HTTPSampler.FILE_DATA";
+ public final static String FILE_MIMETYPE = "HTTPSampler.FILE_MIMETYPE";
+ public final static String CONTENT_TYPE = "HTTPSampler.CONTENT_TYPE";
+ public final static String NORMAL_FORM = "normal_form";
+ public final static String MULTIPART_FORM = "multipart_form";
+ public final static String ENCODED_PATH = "HTTPSampler.encoded_path";
+ private static final int MAX_REDIRECTS = 10;
+ protected static String encoding = "iso-8859-1";
+ private static final PostWriter postWriter = new PostWriter();
+ transient protected HttpURLConnection conn;
- /****************************************
- * Set request headers in preparation to opening a connection
- *
- *@param connection <code>URLConnection</code> to set headers on
- *@exception IOException if an I/O exception occurs
- ***************************************/
- public void setPostHeaders(URLConnection conn) throws IOException
- {
- postWriter.setHeaders(conn, this);
- }
+ private static PatternCacheLRU patternCache = new PatternCacheLRU(1000, new
Perl5Compiler());
- /****************************************
- * Send POST data from <code>Entry</code> to the open connection.
- *
- *@param connection <code>URLConnection</code> of where POST data should
- * be sent
- *@param url contains the query string for POST
- *@exception IOException if an I/O exception occurs
- ***************************************/
- public void sendPostData(URLConnection connection) throws IOException
- {
- postWriter.sendPostData(connection, this);
- }
+ private static ThreadLocal localMatcher = new ThreadLocal()
+ {
+ protected synchronized Object initialValue()
+ {
+ return new Perl5Matcher();
+ }
+ };
- /****************************************
- * Returns a <code>HttpURLConnection</code> with request method(GET or POST),
- * headers, cookies, authorization properly set for the URL request
- *
- *@param u <code>URL</code> of the URL request
- *@param url <code>UrlConfig</code> of the URL request
- *@return <code>HttpURLConnection</code> of the URL request
- *@exception IOException if an I/O Exception occurs
- ***************************************/
- protected HttpURLConnection setupConnection(URL u, String method)
- throws IOException
- {
- HttpURLConnection conn;
- // [Jordi <[EMAIL PROTECTED]>]
- // I've not been able to find out why we're not using this
- // feature of HttpURLConnections and we're doing redirection
- // by hand instead. Everything would be so much simpler...
- // [/Jordi]
- // Mike: answer - it didn't work. Maybe in JDK1.4 it works, but
honestly,
- // it doesn't seem like they're working on this.
- // My longer term plan is to use Apache's home grown HTTP Client, or
- // maybe even HTTPUnit's classes. I'm sure both would be better than
Sun's
- HttpURLConnection.setFollowRedirects(false);
- conn = (HttpURLConnection) u.openConnection();
- // delegate SSL specific stuff to SSLManager so that compilation still
works otherwise.
- if ("https".equals(u.getProtocol()))
- {
- try
- {
- SSLManager.getInstance().setContext(conn);
- }
- catch (Exception e)
- {
- log.warn(
- "You may have forgotten to set the
ssl.provider property in jmeter.properties",
- e);
- }
- }
- // a well-bahaved browser is supposed to send 'Connection: close'
- // with the last request to an HTTP server. Instead, most browsers
- // leave it to the server to close the connection after their
- // timeout period. Leave it to the JMeter user to decide.
- if (getUseKeepAlive())
- {
- conn.setRequestProperty("Connection", "keep-alive");
- }
- else
- {
- conn.setRequestProperty("Connection", "close");
- }
- conn.setRequestMethod(method);
- setConnectionHeaders(conn, u, getHeaderManager());
- setConnectionCookie(conn, u, getCookieManager());
- setConnectionAuthorization(conn, u, getAuthManager());
- return conn;
- }
- //Mark Walsh 2002-08-03, modified to also parse a parameter name value
- //string, where string contains only the parameter name and no equal sign.
- /**
- * This method allows a proxy server to send over the raw text from a browser's
- * output stream to be parsed and stored correctly into the UrlConfig object.
- */
- public void parseArguments(String queryString)
- {
- String[] args = JOrphanUtils.split(queryString, "&");
- for (int i = 0; i < args.length; i++)
- {
- // need to handle four cases: string contains name=value
- // string contains name=
- // string contains name
- // empty string
- // find end of parameter name
- int endOfNameIndex = 0;
- String metaData = ""; // records the existance of an equal sign
- if (args[i].indexOf("=") != -1)
- {
- endOfNameIndex = args[i].indexOf("="); // case of
name=value, name=
- metaData = "=";
- }
- else
- {
- metaData = "";
- if (args[i].length() > 0)
- {
- endOfNameIndex = args[i].length(); // case name
- }
- else
- {
- endOfNameIndex = 0; //case where name value
string is empty
- }
- }
- // parse name
- String name = ""; // for empty string
- if (args[i].length() > 0)
- {
- name = args[i].substring(0, endOfNameIndex); //for non
empty string
- }
- // parse value
- String value = "";
- if ((endOfNameIndex + 1) < args[i].length())
- {
- value = args[i].substring(endOfNameIndex + 1,
args[i].length());
- }
- if (name.length() > 0)
- {
- // In JDK 1.2, the decode() method has a throws clause:
- // "throws Exception". In JDK 1.3, the method does not
have
- // a throws clause. So, in order to be JDK 1.2
compliant,
- // we need to add a try/catch around the method call.
- try
- {
- addEncodedArgument(name, value, metaData);
- }
- catch (Exception e)
- {
- log.error(
- "UrlConfig:parseArguments(): Unable to
parse argument=["
- + value
- + "]");
- log.error(
- "UrlConfig:parseArguments():
queryString=[" + queryString + "]",
- e);
- }
- }
- }
- }
- /****************************************
- * Reads the response from the URL connection
- *
- *@param conn URL from which to read response
- *@return response in <code>String</code>
- *@exception IOException if an I/O exception occurs
- ***************************************/
- protected byte[] readResponse(HttpURLConnection conn) throws IOException
- {
- byte[] buffer = new byte[4096];
- BufferedInputStream in;
- try
- {
- in = new BufferedInputStream(conn.getInputStream());
- }
- catch (Exception e)
- {
- in = new BufferedInputStream(conn.getErrorStream());
- }
- java.io.ByteArrayOutputStream w = new ByteArrayOutputStream();
- int x = 0;
- while ((x = in.read(buffer)) > -1)
- {
- w.write(buffer, 0, x);
- }
- in.close();
- w.flush();
- w.close();
- return w.toByteArray();
- }
- /****************************************
- * Gets the ResponseHeaders from the URLConnection, save them to the
- * SampleResults object.
- *
- *@param conn connection from which the headers are read
- *@param res where the headers read are stored
- ***************************************/
- protected byte[] getResponseHeaders(HttpURLConnection conn, SampleResult res)
- throws IOException
- {
- StringBuffer headerBuf = new StringBuffer();
- headerBuf.append(conn.getHeaderField(0).substring(0, 8));
- headerBuf.append(" ");
- headerBuf.append(conn.getResponseCode());
- headerBuf.append(" ");
- headerBuf.append(conn.getResponseMessage());
- headerBuf.append("\n");
- for (int i = 1; conn.getHeaderFieldKey(i) != null; i++)
- {
- if
(!conn.getHeaderFieldKey(i).equalsIgnoreCase("transfer-encoding"))
- {
- headerBuf.append(conn.getHeaderFieldKey(i));
- headerBuf.append(": ");
- headerBuf.append(conn.getHeaderField(i));
- headerBuf.append("\n");
- }
- }
- headerBuf.append("\n");
- return headerBuf.toString().getBytes("8859_1");
- }
- /****************************************
- * Extracts all the required cookies for that particular URL request and set
- * them in the <code>HttpURLConnection</code> passed in
- *
- *@param conn <code>HttpUrlConnection</code> which represents the URL
- * request
- *@param u <code>URL</code> of the URL request
- *@param cookieManager the <code>CookieManager</code> containing all the
- * cookies for this <code>UrlConfig</code>
- ***************************************/
- private void setConnectionCookie(
- HttpURLConnection conn,
- URL u,
- CookieManager cookieManager)
- {
- if (cookieManager != null)
- {
- String cookieHeader = cookieManager.getCookieHeaderForURL(u);
- if (cookieHeader != null)
- {
- conn.setRequestProperty("Cookie", cookieHeader);
- }
- }
- }
- /****************************************
- * Extracts all the required headers for that particular URL request and set
- * them in the <code>HttpURLConnection</code> passed in
- *
- *@param conn <code>HttpUrlConnection</code> which represents the URL
- * request
- *@param u <code>URL</code> of the URL request
- *@param headerManager the <code>HeaderManager</code> containing all the
- * cookies for this <code>UrlConfig</code>
- ***************************************/
- private void setConnectionHeaders(
- HttpURLConnection conn,
- URL u,
- HeaderManager headerManager)
- {
- if (headerManager != null)
- {
- Collection headers = headerManager.getHeaders();
- if (headers != null)
- {
- Iterator i = headers.iterator();
- while (i.hasNext())
- {
- Header header = (Header) i.next();
- conn.setRequestProperty(header.getName(),
header.getValue());
- }
- }
- }
- }
- /****************************************
- * Extracts all the required authorization for that particular URL request and
- * set them in the <code>HttpURLConnection</code> passed in
- *
- *@param conn <code>HttpUrlConnection</code> which represents the URL
- * request
- *@param u <code>URL</code> of the URL request
- *@param authManager the <code>AuthManager</code> containing all the cookies
- * for this <code>UrlConfig</code>
- ***************************************/
- private void setConnectionAuthorization(
- HttpURLConnection conn,
- URL u,
- AuthManager authManager)
- {
- if (authManager != null)
- {
- String authHeader = authManager.getAuthHeaderForURL(u);
- if (authHeader != null)
- {
- conn.setRequestProperty("Authorization", authHeader);
- }
- }
- }
- /****************************************
- * Get the response code of the URL connection and divide it by 100 thus
- * returning 2(for 2xx response codes), 3(for 3xx reponse codes), etc
- *
- *@param conn <code>HttpURLConnection</code> of URL request
- *@param res where all results of sampling will be stored
- *@param time time when the URL request was first started
- *@return HTTP response code divided by 100
- ***************************************/
- private int getErrorLevel(
- HttpURLConnection conn,
- SampleResult res,
- long time)
- throws IOException
- {
- int errorLevel = 200;
- String message = null;
- errorLevel = ((HttpURLConnection) conn).getResponseCode();
- message = ((HttpURLConnection) conn).getResponseMessage();
- res.setResponseCode(String.valueOf(errorLevel));
- res.setResponseMessage(message);
- return errorLevel;
- }
- public void removeArguments()
- {
- setProperty(HTTPSampler.ARGUMENTS, new Arguments());
- }
- /****************************************
- * Follow redirection manually. Normally if the web server does a redirection
- * the intermediate page is not returned. Only the resultant page and the
- * response code for the page will be returned. With redirection turned off,
- * the response code of 3xx will be returned together with a "Location"
- * header-value pair to indicate that the "Location" value needs to be followed
- * to get the resultant page.
- *
- *@param conn connection
- *@param u
- *@param urlConfig !ToDo (Parameter description)
- *@exception MalformedURLException if URL is not understood
- ***************************************/
- private void redirectUrl(HttpURLConnection conn, URL u)
- throws MalformedURLException
- {
- String loc = conn.getHeaderField("Location");
- if (loc != null)
- {
- if (loc.indexOf("http") == -1)
- {
- String tempURL = u.toString();
- if (loc.startsWith("/"))
- {
- int ind = tempURL.indexOf("//") + 2;
- loc =
- tempURL.substring(0,
tempURL.indexOf("/", ind) + 1)
- + loc.substring(1);
- }
- else
- {
- loc =
- u.toString().substring(0,
u.toString().lastIndexOf('/') + 1) + loc;
- }
- }
- }
- URL newUrl = new URL(loc);
- setMethod(GET);
- setProtocol(newUrl.getProtocol());
- setDomain(newUrl.getHost());
- setPort(newUrl.getPort());
- setPath(newUrl.getFile());
- removeArguments();
- parseArguments(newUrl.getQuery());
- }
+ private static Substitution spaceSub = new StringSubstitution("%20");
- protected long connect() throws IOException
- {
- long time = System.currentTimeMillis();
- try
- {
- conn.connect();
- }
- catch(BindException e)
- {
- log.debug("Bind exception, try again");
- if(connectionTries++ == 10)
- {
- log.error("Can't connect",e);
- throw e;
- }
- conn.disconnect();
- conn = null;
- System.gc();
- Runtime.getRuntime().runFinalization();
- this.setUseKeepAlive(false);
- conn = setupConnection(getUrl(),getMethod());
- if(getMethod().equals(HTTPSampler.POST))
- {
- setPostHeaders(conn);
- }
- time = connect();
- }
- catch(IOException e)
- {
- log.debug("Connection failed, giving up");
- conn.disconnect();
- conn = null;
- System.gc();
- Runtime.getRuntime().runFinalization();
- throw e;
- }
- return time;
- }
+ private int connectionTries = 0;
+ public void setFileField(String value)
+ {
+ setProperty(FILE_FIELD, value);
+ }
+ public String getFileField()
+ {
+ return getPropertyAsString(FILE_FIELD);
+ }
+ public void setFilename(String value)
+ {
+ setProperty(FILE_NAME, value);
+ }
+ public String getFilename()
+ {
+ return getPropertyAsString(FILE_NAME);
+ }
+ public void setProtocol(String value)
+ {
+ setProperty(PROTOCOL, value);
+ }
+ public String getProtocol()
+ {
+ String protocol = getPropertyAsString(PROTOCOL);
+ if (protocol == null || protocol.equals(""))
+ {
+ return DEFAULT_PROTOCOL;
+ }
+ else
+ return protocol;
+ }
+ /**
+ * Sets the Path attribute of the UrlConfig object
+ *
+ *@param path The new Path value
+ */
+ public void setPath(String path)
+ {
+ if (GET.equals(getMethod()))
+ {
+ int index = path.indexOf("?");
+ if (index > -1)
+ {
+ setProperty(PATH, path.substring(0, index));
+ parseArguments(path.substring(index + 1));
+ }
+ else
+ {
+ setProperty(PATH, path);
+ }
+ }
+ else
+ {
+ setProperty(PATH, path);
+ }
+ }
- /****************************************
- * Samples <code>Entry</code> passed in and stores the result in
<code>SampleResult</code>
- *
- *@param e <code>Entry</code> to be sampled
- *@param redirects the level of redirection we're processing (0 means
- * original request) -- just used to prevent
- * an infinite loop.
- *@return results of the sampling
- ***************************************/
- private SampleResult sample(int redirects)
- {
- log.debug("Start : sample2");
- long time = System.currentTimeMillis();
- SampleResult res = new SampleResult();
- URL u = null;
- try
- {
- u = getUrl();
- res.setSampleLabel(getName());
- // specify the data to the result.
- res.setSamplerData(this);
- /****************************************
- * END - cached logging hack
- ***************************************/
- if (log.isDebugEnabled())
- {
- log.debug("sample2 : sampling url - " + u);
- }
- conn = setupConnection(u, getMethod());
- if (getProperty(HTTPSampler.METHOD).equals(HTTPSampler.POST))
- {
- setPostHeaders(conn);
- time = connect();
- sendPostData(conn);
- }
- else time = connect();
- saveConnectionCookies(conn, u, getCookieManager());
- int errorLevel = 0;
- try
- {
- errorLevel = getErrorLevel(conn, res, time);
- }
- catch (IOException e)
- {
- time = bundleResponseInResult(time, res, conn);
- res.setSuccessful(false);
- res.setTime(time);
- return res;
- }
- if (errorLevel / 100 == 2 || errorLevel == 304)
- {
- time = bundleResponseInResult(time, res, conn);
- }
- else if (errorLevel / 100 == 3)
- {
- if (redirects >= MAX_REDIRECTS)
- {
- throw new IOException("Maximum number of
redirects exceeded");
- }
+ public void setEncodedPath(String path)
+ {
+ path =
+ Util.substitute(
+ (Perl5Matcher) localMatcher.get(),
+ patternCache.getPattern(" ", Perl5Compiler.READ_ONLY_MASK &
Perl5Compiler.SINGLELINE_MASK),
+ spaceSub,
+ path,
+ Util.SUBSTITUTE_ALL);
+ setProperty(ENCODED_PATH, path);
+ }
- if (!getFollowRedirects())
- {
- time = bundleResponseInResult(time, res, conn);
- }
- else
- {
- time = System.currentTimeMillis() - time;
+ public String getEncodedPath()
+ {
+ return getPropertyAsString(ENCODED_PATH);
+ }
- HTTPSampler redirect=
(HTTPSampler)this.clone();
- redirect.redirectUrl(conn, u);
- SampleResult redirectResult=
redirect.sample(redirects+1);
- res.addSubResult(redirectResult);
-
res.setResponseData(redirectResult.getResponseData());
- time += redirectResult.getTime();
- }
- }
- else
- {
- // Could not sample the URL
- time = bundleResponseInResult(time, res, conn);
- res.setSuccessful(false);
- }
- res.setTime(time);
- log.debug("End : sample2");
- return res;
- }
- catch (IOException ex)
- {
- log.warn(ex.getMessage(), ex);
- res.setDataType(res.TEXT);
- res.setResponseData(ex.toString().getBytes());
- res.setResponseCode(NON_HTTP_RESPONSE_CODE);
- res.setResponseMessage(NON_HTTP_RESPONSE_MESSAGE);
- res.setTime(System.currentTimeMillis() - time);
- res.setSuccessful(false);
- }
- finally
- {
- try
- {
- // calling disconnect doesn't close the connection
immediately, but
- // indicates we're through with it. The JVM should
close it when
- // necessary.
- String connection = conn.getHeaderField("Connection");
- if (connection == null ||
connection.equalsIgnoreCase("close"))
- conn.disconnect();
- }
- catch (Exception e)
- {
- }
+ public void setProperty(String key, Object prop)
+ {
+ super.setProperty(key, prop);
+ if (PATH.equals(key))
+ {
+ setEncodedPath(prop.toString());
+ }
+ }
- }
- log.debug("End : sample2");
- return res;
- }
- private long bundleResponseInResult(
- long time,
- SampleResult res,
- HttpURLConnection conn)
- throws IOException, FileNotFoundException
- {
- res.setDataType(res.TEXT);
- byte[] ret = readResponse(conn);
- byte[] head = getResponseHeaders(conn, res);
- time = System.currentTimeMillis() - time;
- byte[] complete = new byte[ret.length + head.length];
- System.arraycopy(head, 0, complete, 0, head.length);
- System.arraycopy(ret, 0, complete, head.length, ret.length);
- res.setResponseData(complete);
- res.setSuccessful(true);
- return time;
- }
- /****************************************
- * From the <code>HttpURLConnection</code>, store all the "set-cookie" key-pair
- * values in the cookieManager of the <code>UrlConfig</code>
- *
- *@param conn <code>HttpUrlConnection</code> which represents the URL
- * request
- *@param u <code>URL</code> of the URL request
- *@param cookieManager the <code>CookieManager</code> containing all the
- * cookies for this <code>UrlConfig</code>
- ***************************************/
- private void saveConnectionCookies(
- HttpURLConnection conn,
- URL u,
- CookieManager cookieManager)
- {
- if (cookieManager != null)
- {
- for (int i = 1; conn.getHeaderFieldKey(i) != null; i++)
- {
- if
(conn.getHeaderFieldKey(i).equalsIgnoreCase("set-cookie"))
- {
-
cookieManager.addCookieFromHeader(conn.getHeaderField(i), u);
- }
- }
- }
- }
- public String toString()
- {
- try
- {
- return this.getUrl().toString()
- + ((POST.equals(getMethod())) ? "\nQuery Data: " +
getQueryString() : "");
- }
- catch (MalformedURLException e)
- {
- return "";
- }
- }
- public static class Test extends junit.framework.TestCase
- {
- public Test(String name)
- {
- super(name);
- }
- public void testMakingUrl() throws Exception
- {
- HTTPSampler config = new HTTPSampler();
- config.setProtocol("http");
- config.setMethod(HTTPSampler.GET);
- config.addArgument("param1", "value1");
- config.setPath("/index.html");
- config.setDomain("www.apache.org");
- assertEquals(
- "http://www.apache.org:80/index.html?param1=value1",
- config.getUrl().toString());
- }
- public void testMakingUrl2() throws Exception
- {
- HTTPSampler config = new HTTPSampler();
- config.setProtocol("http");
- config.setMethod(HTTPSampler.GET);
- config.addArgument("param1", "value1");
- config.setPath("/index.html?p1=p2");
- config.setDomain("www.apache.org");
- assertEquals(
-
"http://www.apache.org:80/index.html?param1=value1&p1=p2",
- config.getUrl().toString());
- }
- public void testMakingUrl3() throws Exception
- {
- HTTPSampler config = new HTTPSampler();
- config.setProtocol("http");
- config.setMethod(HTTPSampler.POST);
- config.addArgument("param1", "value1");
- config.setPath("/index.html?p1=p2");
- config.setDomain("www.apache.org");
- assertEquals(
- "http://www.apache.org:80/index.html?p1=p2",
- config.getUrl().toString());
- }
- // test cases for making Url, and exercise method addArgument(String
name,String value,String metadata)
- public void testMakingUrl4() throws Exception
- {
- HTTPSampler config = new HTTPSampler();
- config.setProtocol("http");
- config.setMethod(HTTPSampler.GET);
- config.addArgument("param1", "value1", "=");
- config.setPath("/index.html");
- config.setDomain("www.apache.org");
- assertEquals(
- "http://www.apache.org:80/index.html?param1=value1",
- config.getUrl().toString());
- }
- public void testMakingUrl5() throws Exception
- {
- HTTPSampler config = new HTTPSampler();
- config.setProtocol("http");
- config.setMethod(HTTPSampler.GET);
- config.addArgument("param1", "", "=");
- config.setPath("/index.html");
- config.setDomain("www.apache.org");
- assertEquals(
- "http://www.apache.org:80/index.html?param1=",
- config.getUrl().toString());
- }
- public void testMakingUrl6() throws Exception
- {
- HTTPSampler config = new HTTPSampler();
- config.setProtocol("http");
- config.setMethod(HTTPSampler.GET);
- config.addArgument("param1", "", "");
- config.setPath("/index.html");
- config.setDomain("www.apache.org");
- assertEquals(
- "http://www.apache.org:80/index.html?param1",
- config.getUrl().toString());
- }
- // test cases for making Url, and exercise method
parseArguments(String queryString)
- public void testMakingUrl7() throws Exception
- {
- HTTPSampler config = new HTTPSampler();
- config.setProtocol("http");
- config.setMethod(HTTPSampler.GET);
- config.parseArguments("param1=value1");
- config.setPath("/index.html");
- config.setDomain("www.apache.org");
- assertEquals(
- "http://www.apache.org:80/index.html?param1=value1",
- config.getUrl().toString());
- }
- public void testMakingUrl8() throws Exception
- {
- HTTPSampler config = new HTTPSampler();
- config.setProtocol("http");
- config.setMethod(HTTPSampler.GET);
- config.parseArguments("param1=");
- config.setPath("/index.html");
- config.setDomain("www.apache.org");
- assertEquals(
- "http://www.apache.org:80/index.html?param1=",
- config.getUrl().toString());
- }
- public void testMakingUrl9() throws Exception
- {
- HTTPSampler config = new HTTPSampler();
- config.setProtocol("http");
- config.setMethod(HTTPSampler.GET);
- config.parseArguments("param1");
- config.setPath("/index.html");
- config.setDomain("www.apache.org");
- assertEquals(
- "http://www.apache.org:80/index.html?param1",
- config.getUrl().toString());
- }
- public void testMakingUrl10() throws Exception
- {
- HTTPSampler config = new HTTPSampler();
- config.setProtocol("http");
- config.setMethod(HTTPSampler.GET);
- config.parseArguments("");
- config.setPath("/index.html");
- config.setDomain("www.apache.org");
- assertEquals(
- "http://www.apache.org:80/index.html",
- config.getUrl().toString());
- }
- }
+ public String getPath()
+ {
+ return getPropertyAsString(PATH);
+ }
+ public void setFollowRedirects(boolean value)
+ {
+ setProperty(FOLLOW_REDIRECTS, new Boolean(value));
+ }
+ public boolean getFollowRedirects()
+ {
+ return getPropertyAsBoolean(FOLLOW_REDIRECTS);
+ }
+ public void setMethod(String value)
+ {
+ setProperty(METHOD, value);
+ }
+ public String getMethod()
+ {
+ return getPropertyAsString(METHOD);
+ }
+ public void setUseKeepAlive(boolean value)
+ {
+ setProperty(USE_KEEPALIVE, new Boolean(value));
+ }
+ public boolean getUseKeepAlive()
+ {
+ return getPropertyAsBoolean(USE_KEEPALIVE);
+ }
+ public void addEncodedArgument(String name, String value, String metaData)
+ {
+ Arguments args = getArguments();
+ HTTPArgument arg = new HTTPArgument(name, value, metaData, true);
+ if (arg.getName().equals(arg.getEncodedName()) &&
arg.getValue().equals(arg.getEncodedValue()))
+ {
+ arg.setAlwaysEncode(false);
+ }
+ args.addArgument(arg);
+ }
+ public void addArgument(String name, String value)
+ {
+ Arguments args = this.getArguments();
+ args.addArgument(new HTTPArgument(name, value));
+ }
+ public void addArgument(String name, String value, String metadata)
+ {
+ Arguments args = this.getArguments();
+ args.addArgument(new HTTPArgument(name, value, metadata));
+ }
+ public void setPort(int value)
+ {
+ setProperty(PORT, new Integer(value));
+ }
+ public int getPort()
+ {
+ int port = getPropertyAsInt(PORT);
+ if (port == 0)
+ {
+ if ("https".equalsIgnoreCase(getProtocol()))
+ {
+ return 443;
+ }
+ return 80;
+ }
+ return port;
+ }
+ public void setDomain(String value)
+ {
+ setProperty(DOMAIN, value);
+ }
+ public String getDomain()
+ {
+ return (String) getProperty(DOMAIN);
+ }
+ public void setArguments(Arguments value)
+ {
+ setProperty(ARGUMENTS, value);
+ }
+ public Arguments getArguments()
+ {
+ return (Arguments) getProperty(ARGUMENTS);
+ }
+ public void setAuthManager(AuthManager value)
+ {
+ setProperty(AUTH_MANAGER, value);
+ }
+ public AuthManager getAuthManager()
+ {
+ return (AuthManager) getProperty(AUTH_MANAGER);
+ }
+ public void setHeaderManager(HeaderManager value)
+ {
+ setProperty(HEADER_MANAGER, value);
+ }
+ public HeaderManager getHeaderManager()
+ {
+ return (HeaderManager) getProperty(HEADER_MANAGER);
+ }
+ public void setCookieManager(CookieManager value)
+ {
+ setProperty(COOKIE_MANAGER, value);
+ }
+ public CookieManager getCookieManager()
+ {
+ return (CookieManager) getProperty(COOKIE_MANAGER);
+ }
+ public void setMimetype(String value)
+ {
+ setProperty(MIMETYPE, value);
+ }
+ public String getMimetype()
+ {
+ return (String) getProperty(MIMETYPE);
+ }
+ protected void addCustomTestElement(TestElement element)
+ {
+ if (element instanceof Arguments)
+ {
+ if (getProperty(ARGUMENTS) != null)
+ {
+ ((Arguments) getProperty(ARGUMENTS)).addTestElement(element);
+ }
+ else
+ {
+ setProperty(ARGUMENTS, element);
+ }
+ }
+ else if (element instanceof AuthManager)
+ {
+ if (getProperty(AUTH_MANAGER) != null)
+ {
+ ((TestElement) getProperty(AUTH_MANAGER)).addTestElement(element);
+ }
+ else
+ {
+ setProperty(AUTH_MANAGER, element);
+ }
+ }
+ else if (element instanceof CookieManager)
+ {
+ if (getProperty(COOKIE_MANAGER) != null)
+ {
+ ((TestElement) getProperty(COOKIE_MANAGER)).addTestElement(element);
+ }
+ else
+ {
+ setProperty(COOKIE_MANAGER, element);
+ }
+ }
+ else if (element instanceof HeaderManager)
+ {
+ if (getProperty(HEADER_MANAGER) != null)
+ {
+ ((TestElement) getProperty(HEADER_MANAGER)).addTestElement(element);
+ }
+ else
+ {
+ setProperty(HEADER_MANAGER, element);
+ }
+ }
+ else if (element instanceof HTTPSampler || element instanceof
ConfigTestElement)
+ {
+ this.mergeIn(element);
+ }
+ }
+ /****************************************
+ * !ToDo (Field description)
+ ***************************************/
+ protected final static String NON_HTTP_RESPONSE_CODE = "Non HTTP response code";
+ /****************************************
+ * !ToDo (Field description)
+ ***************************************/
+ protected final static String NON_HTTP_RESPONSE_MESSAGE = "Non HTTP response
message";
+ transient private static Logger log =
Hierarchy.getDefaultHierarchy().getLoggerFor("jmeter.protocol.http");
+ /****************************************
+ * Holds a list of URLs sampled - so we're not flooding stdout with debug
+ * information
+ ***************************************/
+ private ArrayList m_sampledURLs = new ArrayList();
+ /****************************************
+ * Constructor for the HTTPSampler object
+ ***************************************/
+ public HTTPSampler()
+ {
+ setArguments(new Arguments());
+ }
+ public HTTPSampler(URL u)
+ {
+ setMethod(GET);
+ setDomain(u.getHost());
+ setPath(u.getPath());
+ setPort(u.getPort());
+ setProtocol(u.getProtocol());
+ parseArguments(u.getQuery());
+ setFollowRedirects(true);
+ setUseKeepAlive(true);
+ setArguments(new Arguments());
+ }
+ /****************************************
+ * Do a sampling and return its results.
+ *
+ *@param e <code>Entry</code> to be sampled
+ *@return results of the sampling
+ *@see
org.apache.jmeter.protocol.http.sampler.HTTPSampler.sample(org.apache.jmeter.samplers.Entry,
+ * int)
+ ***************************************/
+ public SampleResult sample(Entry e)
+ {
+ return sample(0);
+ }
+ public SampleResult sample()
+ {
+ return sample(0);
+ }
+ /**
+ * !ToDoo (Method description)
+ *
+ *@return !ToDo (Return description)
+ *@exception MalformedURLException !ToDo (Exception description)
+ */
+ public URL getUrl() throws MalformedURLException
+ {
+ String pathAndQuery = null;
+ if (this.getMethod().equals(HTTPSampler.GET) && getQueryString().length() >
0)
+ {
+ if (this.getEncodedPath().indexOf("?") > -1)
+ {
+ pathAndQuery = this.getEncodedPath() + "&" + getQueryString();
+ }
+ else
+ {
+ pathAndQuery = this.getEncodedPath() + "?" + getQueryString();
+ }
+ }
+ else
+ {
+ pathAndQuery = this.getEncodedPath();
+ }
+ if (!pathAndQuery.startsWith("/"))
+ {
+ pathAndQuery = "/" + pathAndQuery;
+ }
+ if (getPort() == 0)
+ {
+ return new URL(getProtocol(), getDomain(), pathAndQuery);
+ }
+ else
+ {
+ return new URL(getProtocol(), getPropertyAsString(HTTPSampler.DOMAIN),
getPort(), pathAndQuery);
+ }
+ }
+ /**
+ * Gets the QueryString attribute of the UrlConfig object
+ *
+ *@return The QueryString value
+ */
+ public String getQueryString()
+ {
+ StringBuffer buf = new StringBuffer();
+ Iterator iter = getArguments().iterator();
+ boolean first = true;
+ while (iter.hasNext())
+ {
+ HTTPArgument item = (HTTPArgument) iter.next();
+ if (!first)
+ {
+ buf.append("&");
+ }
+ else
+ {
+ first = false;
+ }
+ buf.append(item.getEncodedName());
+ if (item.getMetaData() == null)
+ {
+ buf.append("=");
+ }
+ else
+ {
+ buf.append(item.getMetaData());
+ }
+ buf.append(item.getEncodedValue());
+ }
+ return buf.toString();
+ }
+
+ /****************************************
+ * Set request headers in preparation to opening a connection
+ *
+ *@param connection <code>URLConnection</code> to set headers on
+ *@exception IOException if an I/O exception occurs
+ ***************************************/
+ public void setPostHeaders(URLConnection conn) throws IOException
+ {
+ postWriter.setHeaders(conn, this);
+ }
+
+ /****************************************
+ * Send POST data from <code>Entry</code> to the open connection.
+ *
+ *@param connection <code>URLConnection</code> of where POST data should
+ * be sent
+ *@param url contains the query string for POST
+ *@exception IOException if an I/O exception occurs
+ ***************************************/
+ public void sendPostData(URLConnection connection) throws IOException
+ {
+ postWriter.sendPostData(connection, this);
+ }
+
+ /****************************************
+ * Returns a <code>HttpURLConnection</code> with request method(GET or POST),
+ * headers, cookies, authorization properly set for the URL request
+ *
+ *@param u <code>URL</code> of the URL request
+ *@param url <code>UrlConfig</code> of the URL request
+ *@return <code>HttpURLConnection</code> of the URL request
+ *@exception IOException if an I/O Exception occurs
+ ***************************************/
+ protected HttpURLConnection setupConnection(URL u, String method) throws
IOException
+ {
+ HttpURLConnection conn;
+ // [Jordi <[EMAIL PROTECTED]>]
+ // I've not been able to find out why we're not using this
+ // feature of HttpURLConnections and we're doing redirection
+ // by hand instead. Everything would be so much simpler...
+ // [/Jordi]
+ // Mike: answer - it didn't work. Maybe in JDK1.4 it works, but honestly,
+ // it doesn't seem like they're working on this.
+ // My longer term plan is to use Apache's home grown HTTP Client, or
+ // maybe even HTTPUnit's classes. I'm sure both would be better than Sun's
+ HttpURLConnection.setFollowRedirects(false);
+ conn = (HttpURLConnection) u.openConnection();
+ // delegate SSL specific stuff to SSLManager so that compilation still
works otherwise.
+ if ("https".equals(u.getProtocol()))
+ {
+ try
+ {
+ SSLManager.getInstance().setContext(conn);
+ }
+ catch (Exception e)
+ {
+ log.warn("You may have forgotten to set the ssl.provider property
in jmeter.properties", e);
+ }
+ }
+ // a well-bahaved browser is supposed to send 'Connection: close'
+ // with the last request to an HTTP server. Instead, most browsers
+ // leave it to the server to close the connection after their
+ // timeout period. Leave it to the JMeter user to decide.
+ if (getUseKeepAlive())
+ {
+ conn.setRequestProperty("Connection", "keep-alive");
+ }
+ else
+ {
+ conn.setRequestProperty("Connection", "close");
+ }
+ conn.setRequestMethod(method);
+ setConnectionHeaders(conn, u, getHeaderManager());
+ setConnectionCookie(conn, u, getCookieManager());
+ setConnectionAuthorization(conn, u, getAuthManager());
+ return conn;
+ }
+ //Mark Walsh 2002-08-03, modified to also parse a parameter name value
+ //string, where string contains only the parameter name and no equal sign.
+ /**
+ * This method allows a proxy server to send over the raw text from a browser's
+ * output stream to be parsed and stored correctly into the UrlConfig object.
+ */
+ public void parseArguments(String queryString)
+ {
+ String[] args = JOrphanUtils.split(queryString, "&");
+ for (int i = 0; i < args.length; i++)
+ {
+ // need to handle four cases: string contains name=value
+ // string contains name=
+ // string contains name
+ // empty string
+ // find end of parameter name
+ int endOfNameIndex = 0;
+ String metaData = ""; // records the existance of an equal sign
+ if (args[i].indexOf("=") != -1)
+ {
+ endOfNameIndex = args[i].indexOf("="); // case of name=value, name=
+ metaData = "=";
+ }
+ else
+ {
+ metaData = "";
+ if (args[i].length() > 0)
+ {
+ endOfNameIndex = args[i].length(); // case name
+ }
+ else
+ {
+ endOfNameIndex = 0; //case where name value string is empty
+ }
+ }
+ // parse name
+ String name = ""; // for empty string
+ if (args[i].length() > 0)
+ {
+ name = args[i].substring(0, endOfNameIndex); //for non empty string
+ }
+ // parse value
+ String value = "";
+ if ((endOfNameIndex + 1) < args[i].length())
+ {
+ value = args[i].substring(endOfNameIndex + 1, args[i].length());
+ }
+ if (name.length() > 0)
+ {
+ // In JDK 1.2, the decode() method has a throws clause:
+ // "throws Exception". In JDK 1.3, the method does not have
+ // a throws clause. So, in order to be JDK 1.2 compliant,
+ // we need to add a try/catch around the method call.
+ try
+ {
+ addEncodedArgument(name, value, metaData);
+ }
+ catch (Exception e)
+ {
+ log.error("UrlConfig:parseArguments(): Unable to parse
argument=[" + value + "]");
+ log.error("UrlConfig:parseArguments(): queryString=[" +
queryString + "]", e);
+ }
+ }
+ }
+ }
+ /****************************************
+ * Reads the response from the URL connection
+ *
+ *@param conn URL from which to read response
+ *@return response in <code>String</code>
+ *@exception IOException if an I/O exception occurs
+ ***************************************/
+ protected byte[] readResponse(HttpURLConnection conn) throws IOException
+ {
+ byte[] buffer = new byte[4096];
+ BufferedInputStream in;
+ try
+ {
+ in = new BufferedInputStream(conn.getInputStream());
+ }
+ catch (Exception e)
+ {
+ in = new BufferedInputStream(conn.getErrorStream());
+ }
+ java.io.ByteArrayOutputStream w = new ByteArrayOutputStream();
+ int x = 0;
+ while ((x = in.read(buffer)) > -1)
+ {
+ w.write(buffer, 0, x);
+ }
+ in.close();
+ w.flush();
+ w.close();
+ return w.toByteArray();
+ }
+ /****************************************
+ * Gets the ResponseHeaders from the URLConnection, save them to the
+ * SampleResults object.
+ *
+ *@param conn connection from which the headers are read
+ *@param res where the headers read are stored
+ ***************************************/
+ protected byte[] getResponseHeaders(HttpURLConnection conn, SampleResult res)
throws IOException
+ {
+ StringBuffer headerBuf = new StringBuffer();
+ headerBuf.append(conn.getHeaderField(0).substring(0, 8));
+ headerBuf.append(" ");
+ headerBuf.append(conn.getResponseCode());
+ headerBuf.append(" ");
+ headerBuf.append(conn.getResponseMessage());
+ headerBuf.append("\n");
+ for (int i = 1; conn.getHeaderFieldKey(i) != null; i++)
+ {
+ if (!conn.getHeaderFieldKey(i).equalsIgnoreCase("transfer-encoding"))
+ {
+ headerBuf.append(conn.getHeaderFieldKey(i));
+ headerBuf.append(": ");
+ headerBuf.append(conn.getHeaderField(i));
+ headerBuf.append("\n");
+ }
+ }
+ headerBuf.append("\n");
+ return headerBuf.toString().getBytes("8859_1");
+ }
+ /****************************************
+ * Extracts all the required cookies for that particular URL request and set
+ * them in the <code>HttpURLConnection</code> passed in
+ *
+ *@param conn <code>HttpUrlConnection</code> which represents the URL
+ * request
+ *@param u <code>URL</code> of the URL request
+ *@param cookieManager the <code>CookieManager</code> containing all the
+ * cookies for this <code>UrlConfig</code>
+ ***************************************/
+ private void setConnectionCookie(HttpURLConnection conn, URL u, CookieManager
cookieManager)
+ {
+ if (cookieManager != null)
+ {
+ String cookieHeader = cookieManager.getCookieHeaderForURL(u);
+ if (cookieHeader != null)
+ {
+ conn.setRequestProperty("Cookie", cookieHeader);
+ }
+ }
+ }
+ /****************************************
+ * Extracts all the required headers for that particular URL request and set
+ * them in the <code>HttpURLConnection</code> passed in
+ *
+ *@param conn <code>HttpUrlConnection</code> which represents the URL
+ * request
+ *@param u <code>URL</code> of the URL request
+ *@param headerManager the <code>HeaderManager</code> containing all the
+ * cookies for this <code>UrlConfig</code>
+ ***************************************/
+ private void setConnectionHeaders(HttpURLConnection conn, URL u, HeaderManager
headerManager)
+ {
+ if (headerManager != null)
+ {
+ Collection headers = headerManager.getHeaders();
+ if (headers != null)
+ {
+ Iterator i = headers.iterator();
+ while (i.hasNext())
+ {
+ Header header = (Header) i.next();
+ conn.setRequestProperty(header.getName(), header.getValue());
+ }
+ }
+ }
+ }
+ /****************************************
+ * Extracts all the required authorization for that particular URL request and
+ * set them in the <code>HttpURLConnection</code> passed in
+ *
+ *@param conn <code>HttpUrlConnection</code> which represents the URL
+ * request
+ *@param u <code>URL</code> of the URL request
+ *@param authManager the <code>AuthManager</code> containing all the cookies
+ * for this <code>UrlConfig</code>
+ ***************************************/
+ private void setConnectionAuthorization(HttpURLConnection conn, URL u,
AuthManager authManager)
+ {
+ if (authManager != null)
+ {
+ String authHeader = authManager.getAuthHeaderForURL(u);
+ if (authHeader != null)
+ {
+ conn.setRequestProperty("Authorization", authHeader);
+ }
+ }
+ }
+ /****************************************
+ * Get the response code of the URL connection and divide it by 100 thus
+ * returning 2(for 2xx response codes), 3(for 3xx reponse codes), etc
+ *
+ *@param conn <code>HttpURLConnection</code> of URL request
+ *@param res where all results of sampling will be stored
+ *@param time time when the URL request was first started
+ *@return HTTP response code divided by 100
+ ***************************************/
+ private int getErrorLevel(HttpURLConnection conn, SampleResult res, long time)
throws IOException
+ {
+ int errorLevel = 200;
+ String message = null;
+ errorLevel = ((HttpURLConnection) conn).getResponseCode();
+ message = ((HttpURLConnection) conn).getResponseMessage();
+ res.setResponseCode(String.valueOf(errorLevel));
+ res.setResponseMessage(message);
+ return errorLevel;
+ }
+ public void removeArguments()
+ {
+ setProperty(HTTPSampler.ARGUMENTS, new Arguments());
+ }
+ /****************************************
+ * Follow redirection manually. Normally if the web server does a redirection
+ * the intermediate page is not returned. Only the resultant page and the
+ * response code for the page will be returned. With redirection turned off,
+ * the response code of 3xx will be returned together with a "Location"
+ * header-value pair to indicate that the "Location" value needs to be followed
+ * to get the resultant page.
+ *
+ *@param conn connection
+ *@param u
+ *@param urlConfig !ToDo (Parameter description)
+ *@exception MalformedURLException if URL is not understood
+ ***************************************/
+ private void redirectUrl(HttpURLConnection conn, URL u) throws
MalformedURLException
+ {
+ String loc = conn.getHeaderField("Location");
+ if (loc != null)
+ {
+ if (loc.indexOf("http") == -1)
+ {
+ String tempURL = u.toString();
+ if (loc.startsWith("/"))
+ {
+ int ind = tempURL.indexOf("//") + 2;
+ loc = tempURL.substring(0, tempURL.indexOf("/", ind) + 1) +
loc.substring(1);
+ }
+ else
+ {
+ loc = u.toString().substring(0, u.toString().lastIndexOf('/') +
1) + loc;
+ }
+ }
+ }
+ URL newUrl = new URL(loc);
+ setMethod(GET);
+ setProtocol(newUrl.getProtocol());
+ setDomain(newUrl.getHost());
+ setPort(newUrl.getPort());
+ setPath(newUrl.getFile());
+ removeArguments();
+ parseArguments(newUrl.getQuery());
+ }
+
+ protected long connect() throws IOException
+ {
+ long time = System.currentTimeMillis();
+ try
+ {
+ conn.connect();
+ }
+ catch (BindException e)
+ {
+ log.debug("Bind exception, try again");
+ if (connectionTries++ == 10)
+ {
+ log.error("Can't connect", e);
+ throw e;
+ }
+ conn.disconnect();
+ conn = null;
+ System.gc();
+ Runtime.getRuntime().runFinalization();
+ this.setUseKeepAlive(false);
+ conn = setupConnection(getUrl(), getMethod());
+ if (getMethod().equals(HTTPSampler.POST))
+ {
+ setPostHeaders(conn);
+ }
+ time = connect();
+ }
+ catch (IOException e)
+ {
+ log.debug("Connection failed, giving up");
+ conn.disconnect();
+ conn = null;
+ System.gc();
+ Runtime.getRuntime().runFinalization();
+ throw e;
+ }
+ return time;
+ }
+
+ /****************************************
+ * Samples <code>Entry</code> passed in and stores the result in
<code>SampleResult</code>
+ *
+ *@param e <code>Entry</code> to be sampled
+ *@param redirects the level of redirection we're processing (0 means
+ * original request) -- just used to prevent
+ * an infinite loop.
+ *@return results of the sampling
+ ***************************************/
+ private SampleResult sample(int redirects)
+ {
+ log.debug("Start : sample2");
+ long time = System.currentTimeMillis();
+ SampleResult res = new SampleResult();
+ URL u = null;
+ try
+ {
+ u = getUrl();
+ res.setSampleLabel(getName());
+ // specify the data to the result.
+ res.setSamplerData(this);
+ /****************************************
+ * END - cached logging hack
+ ***************************************/
+ if (log.isDebugEnabled())
+ {
+ log.debug("sample2 : sampling url - " + u);
+ }
+ conn = setupConnection(u, getMethod());
+ if (getProperty(HTTPSampler.METHOD).equals(HTTPSampler.POST))
+ {
+ setPostHeaders(conn);
+ time = connect();
+ sendPostData(conn);
+ }
+ else
+ time = connect();
+ saveConnectionCookies(conn, u, getCookieManager());
+ int errorLevel = 0;
+ try
+ {
+ errorLevel = getErrorLevel(conn, res, time);
+ }
+ catch (IOException e)
+ {
+ time = bundleResponseInResult(time, res, conn);
+ res.setSuccessful(false);
+ res.setTime(time);
+ return res;
+ }
+ if (errorLevel / 100 == 2 || errorLevel == 304)
+ {
+ time = bundleResponseInResult(time, res, conn);
+ }
+ else if (errorLevel / 100 == 3)
+ {
+ if (redirects >= MAX_REDIRECTS)
+ {
+ throw new IOException("Maximum number of redirects exceeded");
+ }
+
+ if (!getFollowRedirects())
+ {
+ time = bundleResponseInResult(time, res, conn);
+ }
+ else
+ {
+ time = System.currentTimeMillis() - time;
+
+ HTTPSampler redirect = (HTTPSampler) this.clone();
+ redirect.redirectUrl(conn, u);
+ SampleResult redirectResult = redirect.sample(redirects + 1);
+ res.addSubResult(redirectResult);
+ res.setResponseData(redirectResult.getResponseData());
+ time += redirectResult.getTime();
+ }
+ }
+ else
+ {
+ // Could not sample the URL
+ time = bundleResponseInResult(time, res, conn);
+ res.setSuccessful(false);
+ }
+ res.setTime(time);
+ log.debug("End : sample2");
+ return res;
+ }
+ catch (IOException ex)
+ {
+ log.warn(ex.getMessage(), ex);
+ res.setDataType(res.TEXT);
+ res.setResponseData(ex.toString().getBytes());
+ res.setResponseCode(NON_HTTP_RESPONSE_CODE);
+ res.setResponseMessage(NON_HTTP_RESPONSE_MESSAGE);
+ res.setTime(System.currentTimeMillis() - time);
+ res.setSuccessful(false);
+ }
+ finally
+ {
+ try
+ {
+ // calling disconnect doesn't close the connection immediately, but
+ // indicates we're through with it. The JVM should close it when
+ // necessary.
+ String connection = conn.getHeaderField("Connection");
+ if (connection == null || connection.equalsIgnoreCase("close"))
+ conn.disconnect();
+ }
+ catch (Exception e)
+ {}
+
+ }
+ log.debug("End : sample2");
+ return res;
+ }
+ private long bundleResponseInResult(long time, SampleResult res,
HttpURLConnection conn) throws IOException, FileNotFoundException
+ {
+ res.setDataType(res.TEXT);
+ byte[] ret = readResponse(conn);
+ byte[] head = getResponseHeaders(conn, res);
+ time = System.currentTimeMillis() - time;
+ byte[] complete = new byte[ret.length + head.length];
+ System.arraycopy(head, 0, complete, 0, head.length);
+ System.arraycopy(ret, 0, complete, head.length, ret.length);
+ res.setResponseData(complete);
+ res.setSuccessful(true);
+ return time;
+ }
+ /****************************************
+ * From the <code>HttpURLConnection</code>, store all the "set-cookie" key-pair
+ * values in the cookieManager of the <code>UrlConfig</code>
+ *
+ *@param conn <code>HttpUrlConnection</code> which represents the URL
+ * request
+ *@param u <code>URL</code> of the URL request
+ *@param cookieManager the <code>CookieManager</code> containing all the
+ * cookies for this <code>UrlConfig</code>
+ ***************************************/
+ private void saveConnectionCookies(HttpURLConnection conn, URL u, CookieManager
cookieManager)
+ {
+ if (cookieManager != null)
+ {
+ for (int i = 1; conn.getHeaderFieldKey(i) != null; i++)
+ {
+ if (conn.getHeaderFieldKey(i).equalsIgnoreCase("set-cookie"))
+ {
+ cookieManager.addCookieFromHeader(conn.getHeaderField(i), u);
+ }
+ }
+ }
+ }
+ public String toString()
+ {
+ try
+ {
+ return this.getUrl().toString() + ((POST.equals(getMethod())) ?
"\nQuery Data: " + getQueryString() : "");
+ }
+ catch (MalformedURLException e)
+ {
+ return "";
+ }
+ }
+ public static class Test extends junit.framework.TestCase
+ {
+ public Test(String name)
+ {
+ super(name);
+ }
+ public void testMakingUrl() throws Exception
+ {
+ HTTPSampler config = new HTTPSampler();
+ config.setProtocol("http");
+ config.setMethod(HTTPSampler.GET);
+ config.addArgument("param1", "value1");
+ config.setPath("/index.html");
+ config.setDomain("www.apache.org");
+ assertEquals("http://www.apache.org:80/index.html?param1=value1",
config.getUrl().toString());
+ }
+ public void testMakingUrl2() throws Exception
+ {
+ HTTPSampler config = new HTTPSampler();
+ config.setProtocol("http");
+ config.setMethod(HTTPSampler.GET);
+ config.addArgument("param1", "value1");
+ config.setPath("/index.html?p1=p2");
+ config.setDomain("www.apache.org");
+ assertEquals("http://www.apache.org:80/index.html?param1=value1&p1=p2",
config.getUrl().toString());
+ }
+ public void testMakingUrl3() throws Exception
+ {
+ HTTPSampler config = new HTTPSampler();
+ config.setProtocol("http");
+ config.setMethod(HTTPSampler.POST);
+ config.addArgument("param1", "value1");
+ config.setPath("/index.html?p1=p2");
+ config.setDomain("www.apache.org");
+ assertEquals("http://www.apache.org:80/index.html?p1=p2",
config.getUrl().toString());
+ }
+ // test cases for making Url, and exercise method addArgument(String
name,String value,String metadata)
+ public void testMakingUrl4() throws Exception
+ {
+ HTTPSampler config = new HTTPSampler();
+ config.setProtocol("http");
+ config.setMethod(HTTPSampler.GET);
+ config.addArgument("param1", "value1", "=");
+ config.setPath("/index.html");
+ config.setDomain("www.apache.org");
+ assertEquals("http://www.apache.org:80/index.html?param1=value1",
config.getUrl().toString());
+ }
+ public void testMakingUrl5() throws Exception
+ {
+ HTTPSampler config = new HTTPSampler();
+ config.setProtocol("http");
+ config.setMethod(HTTPSampler.GET);
+ config.addArgument("param1", "", "=");
+ config.setPath("/index.html");
+ config.setDomain("www.apache.org");
+ assertEquals("http://www.apache.org:80/index.html?param1=",
config.getUrl().toString());
+ }
+ public void testMakingUrl6() throws Exception
+ {
+ HTTPSampler config = new HTTPSampler();
+ config.setProtocol("http");
+ config.setMethod(HTTPSampler.GET);
+ config.addArgument("param1", "", "");
+ config.setPath("/index.html");
+ config.setDomain("www.apache.org");
+ assertEquals("http://www.apache.org:80/index.html?param1",
config.getUrl().toString());
+ }
+ // test cases for making Url, and exercise method parseArguments(String
queryString)
+ public void testMakingUrl7() throws Exception
+ {
+ HTTPSampler config = new HTTPSampler();
+ config.setProtocol("http");
+ config.setMethod(HTTPSampler.GET);
+ config.parseArguments("param1=value1");
+ config.setPath("/index.html");
+ config.setDomain("www.apache.org");
+ assertEquals("http://www.apache.org:80/index.html?param1=value1",
config.getUrl().toString());
+ }
+ public void testMakingUrl8() throws Exception
+ {
+ HTTPSampler config = new HTTPSampler();
+ config.setProtocol("http");
+ config.setMethod(HTTPSampler.GET);
+ config.parseArguments("param1=");
+ config.setPath("/index.html");
+ config.setDomain("www.apache.org");
+ assertEquals("http://www.apache.org:80/index.html?param1=",
config.getUrl().toString());
+ }
+ public void testMakingUrl9() throws Exception
+ {
+ HTTPSampler config = new HTTPSampler();
+ config.setProtocol("http");
+ config.setMethod(HTTPSampler.GET);
+ config.parseArguments("param1");
+ config.setPath("/index.html");
+ config.setDomain("www.apache.org");
+ assertEquals("http://www.apache.org:80/index.html?param1",
config.getUrl().toString());
+ }
+ public void testMakingUrl10() throws Exception
+ {
+ HTTPSampler config = new HTTPSampler();
+ config.setProtocol("http");
+ config.setMethod(HTTPSampler.GET);
+ config.parseArguments("");
+ config.setPath("/index.html");
+ config.setDomain("www.apache.org");
+ assertEquals("http://www.apache.org:80/index.html",
config.getUrl().toString());
+ }
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]