/*
* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache JMeter" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* "Apache JMeter", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.jmeter.protocol.http.sampler;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.http.config.UrlConfig;
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.samplers.Entry;
import org.apache.jmeter.samplers.HttpSampleResult;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.samplers.Sampler;
import org.apache.jmeter.util.SSLManager;
import org.apache.log4j.Category;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
/**
* A sampler which understands all the parts necessary to read statistics about
* HTTP requests, including cookies and authentication.
*
*@author Michael Stover
*@created $Date: 2002/02/16 03:59:11 $
*@version $Revision: 1.33 $
*/
public class HTTPSampler implements Sampler
{
/**
* Description of the Field
*/
public final static String ARGUMENTS = "httpsampler.Arguments";
/**
* Description of the Field
*/
public final static String URL = "httpsampler.URL";
/**
* Description of the Field
*/
public final static String POST = "httpsampler.POST";
/**
* Description of the Field
*/
public final static String GET = "httpsampler.GET";
/**
* Description of the Field
*/
public final static String FILE_NAME = "httpsampler.FILE_NAME";
/**
* Description of the Field
*/
public final static String FILE_FIELD = "httpsampler.FILE_FIELD";
/**
* Description of the Field
*/
public final static String FILE_DATA = "httpsampler.FILE_DATA";
/**
* Description of the Field
*/
public final static String FILE_MIMETYPE = "httpsampler.FILE_MIMETYPE";
/**
* Description of the Field
*/
public final static String CONTENT_TYPE = "httpsampler.CONTENT_TYPE";
/**
* Description of the Field
*/
public final static String NORMAL_FORM = "normal_form";
/**
* Description of the Field
*/
public final static String MULTIPART_FORM = "multipart_form";
protected static String encoding = "iso-8859-1";
protected static final String NON_HTTP_RESPONSE_CODE =
"Non HTTP response code";
protected static final String NON_HTTP_RESPONSE_MESSAGE =
"Non HTTP response message";
private static Category catClass = Category.getInstance(
HTTPSampler.class.getName());
/**
* Constructor for the HTTPSampler object
*/
public HTTPSampler()
{
}
/**
* A convenience method to call <code>sample</code> with no redirection
*
* @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, boolean)
*/
public SampleResult sample(Entry e)
{
return sample(e, false);
}
/**
* 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, UrlConfig url)
throws IOException
{
((HttpURLConnection) connection).setRequestMethod("POST");
String postData = url.getQueryString();
connection.setRequestProperty("Content-length", "" + postData.length());
connection.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
connection.setDoOutput(true);
PrintWriter out = new PrintWriter(connection.getOutputStream());
out.print(postData);
out.close();
}
/**
* 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
* @param e <code>Entry</code> from which all other info can
* be derived from e.g. cookies, header, authorization
* @return <code>HttpURLConnection</code> of the URL request
* @exception IOException if an I/O Exception occurs
*/
protected HttpURLConnection setupConnection(URL u, UrlConfig url, Entry e) throws IOException
{
HttpURLConnection conn;
conn = (HttpURLConnection) u.openConnection();
// delegate SSL specific stuff to SSLManager so that compilation still works otherwise.
if ("https".equals(u.getProtocol())) {
SSLManager.getInstance().setContext(conn);
}
conn.setFollowRedirects(false);
conn.setRequestMethod((String) url.getProperty(UrlConfig.METHOD));
setConnectionHeaders(conn, u, (HeaderManager) e.getConfigElement(HeaderManager.class));
setConnectionCookie(conn, u, (CookieManager) e.getConfigElement(CookieManager.class));
setConnectionAuthorization(conn, u, (AuthManager) e.getConfigElement(AuthManager.class));
return conn;
}
/**
* Gets the UrlConfig attribute of the HTTPSampler object
*
* @param e Description of Parameter
* @return The UrlConfig value
*/
public UrlConfig getUrlConfig(Entry e)
{
UrlConfig urlConfig = (UrlConfig)e.getConfigElement(UrlConfig.class);
catClass.debug("getUrlConfig1 : Returning urlConfig - " + urlConfig);
return urlConfig;
}
/**
* This method will setup <code>HttpURLConnection</code> to handle post using
* <code>sendPostData</code> method if the URL request is actually a html form
* that needs to be posted
*
* @param redirected does <code>HttpURLConnection</code> allow redirection
* @param url contains query string for POST
* @param conn <HttpURLConnection> of the URL request
* @exception IOException if
*/
protected void writeToStream(boolean redirected, UrlConfig url, HttpURLConnection conn) throws IOException
{
if (!redirected && url.getProperty(UrlConfig.METHOD).equals(UrlConfig.POST))
{
sendPostData(conn, url);
}
}
/*
* Uploading a file - put in separate sampler
* else if (contentType.equals(MULTIPART_FORM))
* {
*
* }
* }
*/
/**
* 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)
{
int errorLevel = 2;
int responseCode = 0;
String message = null;
try
{
responseCode = ((HttpURLConnection) conn).getResponseCode();
errorLevel = responseCode / 100;
message = ((HttpURLConnection) conn).getResponseMessage();
res.putValue(this.RESPONSE_CODE,
String.valueOf(responseCode));
res.putValue(this.RESPONSE_MESSAGE,
message);
}
catch (Exception e2)
{
res.putValue(SampleResult.TEXT_RESPONSE, e2.toString().getBytes());
res.putValue(this.RESPONSE_CODE,
NON_HTTP_RESPONSE_CODE);
res.putValue(this.RESPONSE_MESSAGE,
NON_HTTP_RESPONSE_MESSAGE);
res.setTime(System.currentTimeMillis() - time);
res.putValue(SampleResult.SUCCESS, new Boolean(false));
}
if(catClass.isDebugEnabled())
{
catClass.debug("getErrorLevel : Returning errorLevel - " + errorLevel);
}
return errorLevel;
}
/**
* 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
* @exception MalformedURLException if URL is not understood
*/
private void redirectUrl(HttpURLConnection conn, URL u, UrlConfig urlConfig) 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);
urlConfig.putProperty(UrlConfig.DOMAIN, newUrl.getHost());
urlConfig.putProperty(UrlConfig.PATH, newUrl.getFile());
urlConfig.putProperty(UrlConfig.ARGUMENTS,new Arguments());
}
/**
* Samples <code>Entry</code> passed in and stores the result in
* <code>SampleResult</code>
*
* @param e <code>Entry</code> to be sampled
* @param redirected whether redirection is turned on
* @return results of the sampling
*/
private SampleResult sample(Entry e, boolean redirected)
{
catClass.debug("Start : sample2");
long time;
SampleResult res = new HttpSampleResult();
UrlConfig url = getUrlConfig(e);
if(redirected)
{
url.removeArguments();
}
URL u = null;
try
{
u = url.getUrl();
res.putValue(SampleResult.SAMPLE_LABEL, u.toString());
res.putValue(SampleResult.DISPLAY_NAME, u.toString());
res.putValue(HTTPSampler.URL,u);
// specify the data to the result.
res.putValue(HttpSampleResult.DATA, url);
System.out.println("Sampling url: " + u);
if(catClass.isDebugEnabled())
{
catClass.debug("sample2 : sampling url - " + u);
}
HttpURLConnection conn = setupConnection(u, url, e);
writeToStream(redirected, url, conn);
time = System.currentTimeMillis();
conn.connect();
saveConnectionCookies(conn, u, (CookieManager) e.getConfigElement(CookieManager.class));
int errorLevel = getErrorLevel(conn, res, time);
if (errorLevel == 2)
{
byte[] ret = readResponse(conn);
time = System.currentTimeMillis() - time;
res.putValue(SampleResult.TEXT_RESPONSE, ret);
res.putValue(SampleResult.SUCCESS, new Boolean(true));
getResponseHeaders(conn, res);
}
else if (errorLevel == 3)
{
redirectUrl(conn, u, url);
time = System.currentTimeMillis() - time;
res = sample(e, true);
time += res.getTime();
}
else
{
// Could not sample the URL
System.out.println("URL = " + u);
throw new IOException();
}
res.setTime(time);
catClass.debug("End : sample2");
return res;
}
catch (IOException ex)
{
ex.printStackTrace();
res.setTime((long) 0);
res.putValue(this.RESPONSE_CODE,
NON_HTTP_RESPONSE_CODE);
res.putValue(this.RESPONSE_MESSAGE,
NON_HTTP_RESPONSE_MESSAGE);
res.putValue(SampleResult.TEXT_RESPONSE, ex.toString().getBytes());
res.putValue(SampleResult.SUCCESS, new Boolean(false));
}
catClass.debug("End : sample2");
return res;
}
/**
* 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);
}
}
}
}
/**
* 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 = new BufferedInputStream(conn.getInputStream());
java.io.ByteArrayOutputStream w = new ByteArrayOutputStream();
int x = 0;
while ((x = in.read(buffer)) != -1)
{
w.write(buffer, 0, x);
}
in.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 void getResponseHeaders(HttpURLConnection conn, SampleResult res)
{
HashMap hValues = new HashMap(20);
for (int i = 1; conn.getHeaderFieldKey(i) != null; i++)
{
hValues.put(conn.getHeaderFieldKey(i), conn.getHeaderField(i));
}
res.putValue(Sampler.HEADER, hValues);
}
}