📄 ajpsampler.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.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.URL;
import org.apache.jmeter.protocol.http.control.AuthManager;
import org.apache.jmeter.protocol.http.control.CookieManager;
import org.apache.jmeter.protocol.http.control.Header;
import org.apache.jmeter.protocol.http.control.HeaderManager;
import org.apache.jmeter.testelement.property.CollectionProperty;
import org.apache.jmeter.testelement.property.JMeterProperty;
import org.apache.jmeter.testelement.property.PropertyIterator;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;
/**
* Selector for the AJP/1.3 protocol
* (i.e. what Tomcat uses with mod_jk)
* It allows you to test Tomcat in AJP mode without
* actually having Apache installed and configured
*
*/
public class AjpSampler extends HTTPSamplerBase {
private static final Logger log= LoggingManager.getLoggerForClass();
private static final char NEWLINE = '\n';
private static final String COLON_SPACE = ": ";//$NON-NLS-1$
/**
* Translates integer codes to request header names
*/
private static final String []headerTransArray = {
"accept", //$NON-NLS-1$
"accept-charset", //$NON-NLS-1$
"accept-encoding", //$NON-NLS-1$
"accept-language", //$NON-NLS-1$
"authorization", //$NON-NLS-1$
"connection", //$NON-NLS-1$
"content-type", //$NON-NLS-1$
"content-length", //$NON-NLS-1$
"cookie", //$NON-NLS-1$
"cookie2", //$NON-NLS-1$
"host", //$NON-NLS-1$
"pragma", //$NON-NLS-1$
"referer", //$NON-NLS-1$
"user-agent" //$NON-NLS-1$
};
/**
* Base value for translated headers
*/
static final int AJP_HEADER_BASE = 0xA000;
static final int MAX_SEND_SIZE = 8*1024 - 4 - 4;
private transient Socket channel = null;
private int lastPort = -1;
private String lastHost = null;
private String localName = null;
private String localAddress = null;
private byte [] inbuf = new byte[8*1024];
private byte [] outbuf = new byte[8*1024];
private transient ByteArrayOutputStream responseData = new ByteArrayOutputStream();
private int inpos = 0;
private int outpos = 0;
private transient InputStream body = null;
public AjpSampler() {
}
protected HTTPSampleResult sample(URL url,
String method,
boolean frd,
int fd) {
HTTPSampleResult res = new HTTPSampleResult();
res.setMonitor(false);
res.setSampleLabel(url.toExternalForm());
res.sampleStart();
try {
setupConnection(url, method, res);
execute(method, res);
res.sampleEnd();
res.setResponseData(responseData.toByteArray());
return res;
} catch(IOException iex) {
res.sampleEnd();
HTTPSampleResult err = errorResult(iex, res);
lastPort = -1; // force reopen on next sample
channel = null;
return err;
}
}
public void threadFinished() {
if(channel != null) {
try {
channel.close();
} catch(IOException iex) {
log.debug("Error closing channel",iex);
}
}
channel = null;
body = null;
}
private void setupConnection(URL url,
String method,
HTTPSampleResult res) throws IOException {
String host = url.getHost();
int port = url.getPort();
if(port <= 0 || port == url.getDefaultPort()) {
port = 8009;
}
String scheme = url.getProtocol();
if(channel == null || !host.equals(lastHost) || port != lastPort) {
if(channel != null) {
channel.close();
}
channel = new Socket(host, port);
int timeout = JMeterUtils.getPropDefault("httpclient.timeout",0);//$NON-NLS-1$
if(timeout > 0) {
channel.setSoTimeout(timeout);
}
localAddress = channel.getLocalAddress().getHostAddress();
localName = channel.getLocalAddress().getHostName();
lastHost = host;
lastPort = port;
}
res.setURL(url);
res.setHTTPMethod(method);
outpos = 4;
setByte((byte)2);
if(method.equals(POST))
setByte((byte)4);
else
setByte((byte)2);
if(JMeterUtils.getPropDefault("httpclient.version","1.1").equals("1.0")) {//$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
setString("HTTP/1.0");//$NON-NLS-1$
} else {
setString(HTTP_1_1);
}
setString(url.getFile());
setString(localAddress);
setString(localName);
setString(host);
setInt(url.getDefaultPort());
setByte(PROTOCOL_HTTPS.equalsIgnoreCase(scheme) ? (byte)1 : (byte)0);
setInt(getHeaderSize(method, url));
String hdr = setConnectionHeaders(url, host, method);
res.setRequestHeaders(hdr);
setConnectionCookies(url, getCookieManager());
setByte((byte)0xff); // Attributes not supported
}
private int getHeaderSize(String method, URL url) {
HeaderManager headers = getHeaderManager();
CookieManager cookies = getCookieManager();
AuthManager auth = getAuthManager();
int hsz = 1; // Host always
if(method.equals(POST)) {
String fn = getFilename();
if(fn != null && fn.trim().length() > 0) {
hsz += 3;
} else {
hsz += 2;
}
}
if(headers != null) {
hsz += headers.size();
}
if(cookies != null) {
hsz += cookies.getCookieCount();
}
if(auth != null) {
String authHeader = auth.getAuthHeaderForURL(url);
if(authHeader != null) {
++hsz;
}
}
return hsz;
}
private String setConnectionHeaders(URL url, String host, String method)
throws IOException {
HeaderManager headers = getHeaderManager();
AuthManager auth = getAuthManager();
StringBuffer hbuf = new StringBuffer();
// Allow Headers to override Host setting
hbuf.append("Host").append(COLON_SPACE).append(host).append(NEWLINE);//$NON-NLS-1$
setInt(0xA00b); //Host
setString(host);
if(headers != null) {
CollectionProperty coll = headers.getHeaders();
PropertyIterator i = coll.iterator();
while(i.hasNext()) {
Header header = (Header)i.next().getObjectValue();
String n = header.getName();
String v = header.getValue();
hbuf.append(n).append(COLON_SPACE).append(v).append(NEWLINE);
int hc = translateHeader(n);
if(hc > 0) {
setInt(hc+AJP_HEADER_BASE);
} else {
setString(n);
}
setString(v);
}
}
if(method.equals(POST)) {
int cl = -1;
String fn = getFilename();
if(fn != null && fn.trim().length() > 0) {
File input = new File(fn);
cl = (int)input.length();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -