// Instantiate the sampler
HTTPSamplerBase sampler = HTTPSamplerFactory.newInstance(httpSamplerName);
HttpRequestHdr request = new HttpRequestHdr(sampler);
SampleResult result = null;
HeaderManager headers = null;
try {
// Now, parse only first line
request.parse(new BufferedInputStream(clientSocket.getInputStream()));
outStreamClient = clientSocket.getOutputStream();
if ((request.getMethod().startsWith(HTTPConstants.CONNECT)) && (outStreamClient != null)) {
log.debug("Method CONNECT => SSL");
// write a OK reponse to browser, to engage SSL exchange
outStreamClient.write(("HTTP/1.0 200 OK\r\n\r\n").getBytes()); // $NON-NLS-1$ // TODO charset?
outStreamClient.flush();
// With ssl request, url is host:port (without https:// or path)
String[] param = request.getUrl().split(":"); // $NON-NLS-1$
if (param.length == 2) {
log.debug("Start to negotiate SSL connection, host: " + param[0]);
clientSocket = startSSL(clientSocket, param[0]);
} else {
log.warn("In SSL request, unable to find host and port in CONNECT request");
}
// Re-parse (now it's the http request over SSL)
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 forcedHTTPS = 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);
forcedHTTPS = true;
}
} else {
sampler.setProtocol(HTTPConstants.PROTOCOL_HTTPS);
forcedHTTPS = 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 (httpsSpoof && 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()), forcedHTTPS);
} 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("Probably used https instead of http. " +
"To record https requests, see " +
"<a href=\"http://jakarta.apache.org/jmeter/usermanual/component_reference.html#HTTP_Proxy_Server\">HTTP Proxy Server documentation</a>"));
result = generateErrorResult(result, e); // Generate result (if nec.) and populate it
} catch (IOException ioe) {
log.error("Problem with SSL certificate? Ensure browser is set to accept the JMeter proxy cert: "+ioe.getLocalizedMessage());
// won't work: writeErrorToClient(HttpReplyHdr.formInternalError());
if (result == null) {
result = new SampleResult();
result.setSampleLabel("Sample failed");
}
result.setResponseMessage(ioe.getMessage()+ "\n**ensure browser is set to accept the JMeter proxy certificate**");
} catch (Exception e) {
log.error("Exception when processing sample", e);
writeErrorToClient(HttpReplyHdr.formInternalError());
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(HTTPConstants.HEADER_COOKIE);// Always remove cookies
headers.removeHeaderNamed(HTTPConstants.HEADER_AUTHORIZATION);// Always remove authorization
// 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();