📄 httpsampler2.java
字号:
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.jmeter.protocol.http.sampler;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.URL;
import java.net.URLDecoder;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.zip.GZIPInputStream;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpMethodBase;
import org.apache.commons.httpclient.HttpVersion;
import org.apache.commons.httpclient.NTCredentials;
import org.apache.commons.httpclient.ProtocolException;
import org.apache.commons.httpclient.SimpleHttpConnectionManager;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.DeleteMethod;
import org.apache.commons.httpclient.methods.FileRequestEntity;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.HeadMethod;
import org.apache.commons.httpclient.methods.OptionsMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.methods.TraceMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart;
import org.apache.commons.httpclient.params.DefaultHttpParams;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.httpclient.params.HttpParams;
import org.apache.commons.httpclient.protocol.Protocol;
import org.apache.jmeter.JMeter;
import org.apache.jmeter.protocol.http.control.AuthManager;
import org.apache.jmeter.protocol.http.control.Authorization;
import org.apache.jmeter.protocol.http.control.CookieManager;
import org.apache.jmeter.protocol.http.control.HeaderManager;
import org.apache.jmeter.protocol.http.util.EncoderCache;
import org.apache.jmeter.protocol.http.util.HTTPArgument;
import org.apache.jmeter.protocol.http.util.LoopbackHttpClientSocketFactory;
import org.apache.jmeter.protocol.http.util.SlowHttpClientSocketFactory;
import org.apache.jmeter.testelement.property.CollectionProperty;
import org.apache.jmeter.testelement.property.PropertyIterator;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jmeter.util.SSLManager;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.jorphan.util.JOrphanUtils;
import org.apache.log.Logger;
/**
* A sampler which understands all the parts necessary to read statistics about
* HTTP requests, including cookies and authentication.
*
*/
public class HTTPSampler2 extends HTTPSamplerBase {
private static final Logger log = LoggingManager.getLoggerForClass();
private static final String HTTP_AUTHENTICATION_PREEMPTIVE = "http.authentication.preemptive"; // $NON-NLS-1$
private static final boolean canSetPreEmptive; // OK to set pre-emptive auth?
static final String PROXY_HOST =
System.getProperty("http.proxyHost",""); // $NON-NLS-1$
private static final String NONPROXY_HOSTS =
System.getProperty("http.nonProxyHosts",""); // $NON-NLS-1$
static final int PROXY_PORT =
Integer.parseInt(System.getProperty("http.proxyPort","0")); // $NON-NLS-1$
// Have proxy details been provided?
private static final boolean PROXY_DEFINED = PROXY_HOST.length() > 0 && PROXY_PORT > 0;
static final String PROXY_USER =
JMeterUtils.getPropDefault(JMeter.HTTP_PROXY_USER,""); // $NON-NLS-1$
static final String PROXY_PASS =
JMeterUtils.getPropDefault(JMeter.HTTP_PROXY_PASS,""); // $NON-NLS-1$
private static final String PROXY_DOMAIN =
JMeterUtils.getPropDefault("http.proxyDomain",""); // $NON-NLS-1$ $NON-NLS-2$
static final InetAddress localAddress;
private static final String localHost;
/*
* Connection is re-used within the thread if possible
*/
static final ThreadLocal httpClients = new ThreadLocal();
private static final Set nonProxyHostFull = new HashSet();// www.apache.org
private static final List nonProxyHostSuffix = new ArrayList();// .apache.org
private static final int nonProxyHostSuffixSize;
private static boolean isNonProxy(String host){
return nonProxyHostFull.contains(host) || isPartialMatch(host);
}
private static boolean isPartialMatch(String host) {
for (int i=0;i<nonProxyHostSuffixSize;i++){
if (host.endsWith((String)nonProxyHostSuffix.get(i)))
return true;
}
return false;
}
static {
if (NONPROXY_HOSTS.length() > 0){
StringTokenizer s = new StringTokenizer(NONPROXY_HOSTS,"|");// $NON-NLS-1$
while (s.hasMoreTokens()){
String t = s.nextToken();
if (t.indexOf("*") ==0){// e.g. *.apache.org // $NON-NLS-1$
nonProxyHostSuffix.add(t.substring(1));
} else {
nonProxyHostFull.add(t);// e.g. www.apache.org
}
}
}
nonProxyHostSuffixSize=nonProxyHostSuffix.size();
int cps =
JMeterUtils.getPropDefault("httpclient.socket.http.cps", 0); // $NON-NLS-1$
if (cps > 0) {
log.info("Setting up HTTP SlowProtocol, cps="+cps);
Protocol.registerProtocol(PROTOCOL_HTTP,
new Protocol(PROTOCOL_HTTP,new SlowHttpClientSocketFactory(cps),DEFAULT_HTTP_PORT));
}
// Now done in JsseSSLManager (which needs to register the protocol)
// cps =
// JMeterUtils.getPropDefault("httpclient.socket.https.cps", 0); // $NON-NLS-1$
//
// if (cps > 0) {
// log.info("Setting up HTTPS SlowProtocol, cps="+cps);
// Protocol.registerProtocol(PROTOCOL_HTTPS,
// new Protocol(PROTOCOL_HTTPS,new SlowHttpClientSocketFactory(cps),DEFAULT_HTTPS_PORT));
// }
InetAddress inet=null;
String localHostOrIP =
JMeterUtils.getPropDefault("httpclient.localaddress",""); // $NON-NLS-1$
if (localHostOrIP.length() > 0){
try {
inet = InetAddress.getByName(localHostOrIP);
log.info("Using localAddress "+inet.getHostAddress());
} catch (UnknownHostException e) {
log.warn(e.getLocalizedMessage());
}
} else {
try {
InetAddress addr = InetAddress.getLocalHost();
// Get hostname
localHostOrIP = addr.getHostName();
} catch (UnknownHostException e) {
log.warn("Cannot determine localhost name, and httpclient.localaddress was not specified");
}
}
localAddress = inet;
localHost = localHostOrIP;
log.info("Local host = "+localHost);
// Set default parameters as needed
HttpParams params = DefaultHttpParams.getDefaultParams();
// Process httpclient parameters file
String file=JMeterUtils.getProperty("httpclient.parameters.file"); // $NON-NLS-1$
if (file != null) {
HttpClientDefaultParameters.load(file,params);
}
// If the pre-emptive parameter is undefined, then we cans set it as needed
// otherwise we should do what the user requested.
canSetPreEmptive = params.getParameter(HTTP_AUTHENTICATION_PREEMPTIVE) == null;
// Handle old-style JMeter properties
// Default to HTTP version 1.1
String ver=JMeterUtils.getPropDefault("httpclient.version","1.1"); // $NON-NLS-1$ $NON-NLS-2$
try {
params.setParameter(HttpMethodParams.PROTOCOL_VERSION, HttpVersion.parse("HTTP/"+ver));
} catch (ProtocolException e) {
log.warn("Problem setting protocol version "+e.getLocalizedMessage());
}
String to= JMeterUtils.getProperty("httpclient.timeout");
if (to != null){
params.setIntParameter(HttpMethodParams.SO_TIMEOUT, Integer.parseInt(to));
}
// This must be done last, as must not be overridden
params.setParameter(HttpMethodParams.COOKIE_POLICY,CookiePolicy.IGNORE_COOKIES);
// We do our own cookie handling
if (JMeterUtils.getPropDefault("httpclient.loopback",false)){// $NON-NLS-1$
LoopbackHttpClientSocketFactory.setup();
}
}
/**
* Constructor for the HTTPSampler2 object.
*
* Consider using HTTPSamplerFactory.newInstance() instead
*/
public HTTPSampler2() {
}
/*
* Send POST data from <code>Entry</code> to the open connection.
*
* @param connection
* <code>URLConnection</code> where POST data should be sent
* @return a String show what was posted. Will not contain actual file upload content
* @exception IOException
* if an I/O exception occurs
*/
private String sendPostData(PostMethod post) throws IOException {
// Buffer to hold the post body, expect file content
StringBuffer postedBody = new StringBuffer(1000);
// Check if we should do a multipart/form-data or an
// application/x-www-form-urlencoded post request
if(getUseMultipartForPost()) {
// If a content encoding is specified, we use that es the
// encoding of any parameter values
String contentEncoding = getContentEncoding();
if(contentEncoding != null && contentEncoding.length() == 0) {
contentEncoding = null;
}
// Check how many parts we need, one for each parameter and file
int noParts = getArguments().getArgumentCount();
if(hasUploadableFiles())
{
noParts++;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -