// Instantiate the sampler
HTTPSamplerBase sampler = HTTPSamplerFactory.newInstance(httpSamplerName);
HttpRequestHdr request = new HttpRequestHdr(sampler);
SampleResult result = null;
HeaderManager headers = null;
try {
request.parse(new BufferedInputStream(clientSocket.getInputStream()));
// Populate the sampler. It is the same sampler as we sent into
// the constructor of the HttpRequestHdr instance above
request.getSampler(pageEncodings, formEncodings);
/*
* Create a Header Manager to ensure that the browsers headers are
* captured and sent to the server
*/
headers = request.getHeaderManager();
sampler.setHeaderManager(headers);
/*
* If we are trying to spoof https, change the protocol
*/
boolean forcedHTTP = false; // so we know when to revert
if (httpsSpoof) {
if (httpsSpoofMatch.length() > 0){
String url = request.getUrl();
if (url.matches(httpsSpoofMatch)){
sampler.setProtocol(HTTPConstants.PROTOCOL_HTTPS);
forcedHTTP = true;
}
} else {
sampler.setProtocol(HTTPConstants.PROTOCOL_HTTPS);
forcedHTTP = true;
}
}
sampler.threadStarted(); // Needed for HTTPSampler2
result = sampler.sample();
/*
* If we're dealing with text data, and if we're spoofing https,
* replace all occurences of "https://" with "http://" for the client.
* TODO - also check the match string to restrict the changes further?
*/
if (forcedHTTP && SampleResult.TEXT.equals(result.getDataType()))
{
final String enc = result.getDataEncodingWithDefault();
String noHttpsResult = new String(result.getResponseData(),enc);
final String HTTPS_HOST = // match https://host[:port]/ and drop default port if present
"https://([^:/]+)(:"+HTTPConstants.DEFAULT_HTTPS_PORT_STRING+")?"; // $NON-NLS-1$ $NON-NLS-2$
noHttpsResult = noHttpsResult.replaceAll(HTTPS_HOST, "http://$1"); // $NON-NLS-1$
result.setResponseData(noHttpsResult.getBytes(enc));
}
// Find the page encoding and possibly encodings for forms in the page
// in the response from the web server
String pageEncoding = addPageEncoding(result);
addFormEncodings(result, pageEncoding);
writeToClient(result, new BufferedOutputStream(clientSocket.getOutputStream()));
} catch (UnknownHostException uhe) {
log.warn("Server Not Found.", uhe);
writeErrorToClient(HttpReplyHdr.formServerNotFound());
result = generateErrorResult(result, uhe); // Generate result (if nec.) and populate it
} catch (IllegalArgumentException e) {
log.error("Not implemented (probably used https)", e);
writeErrorToClient(HttpReplyHdr.formNotImplemented());
result = generateErrorResult(result, e); // Generate result (if nec.) and populate it
} catch (Exception e) {
log.error("Exception when processing sample", e);
writeErrorToClient(HttpReplyHdr.formTimeout());
result = generateErrorResult(result, e); // Generate result (if nec.) and populate it
} finally {
if (log.isDebugEnabled()) {
log.debug("Will deliver sample " + sampler.getName());
}
/*
* We don't want to store any cookies in the generated test plan
*/
if (headers != null) {
headers.removeHeaderNamed("cookie");// Always remove cookies // $NON-NLS-1$
headers.removeHeaderNamed("Authorization");// Always remove authorization // $NON-NLS-1$
// Remove additional headers
for(int i=0; i < headersToRemove.length; i++){
headers.removeHeaderNamed(headersToRemove[i]);
}
}
target.deliverSampler(sampler, new TestElement[] { captureHttpHeaders ? headers : null }, result);
try {
clientSocket.close();