// Check which HTTPSampler class we should use
String httpSamplerName = target.getSamplerTypeName();
HttpRequestHdr request = new HttpRequestHdr(httpSamplerName);
SampleResult result = null;
HeaderManager headers = null;
HTTPSamplerBase sampler = 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(SampleResult.DEFAULT_HTTP_ENCODING)); // $NON-NLS-1$
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()));
}
SamplerCreator samplerCreator = factory.getSamplerCreator(request, pageEncodings, formEncodings);
sampler = samplerCreator.createAndPopulateSampler(request, 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);
sampler.threadStarted(); // Needed for HTTPSampler2
result = sampler.sample();
// 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()));
samplerCreator.postProcessSampler(sampler, result);
} 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://jmeter.apache.org/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(), ioe);
// 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()) {
if(sampler != null) {
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(String hdr : headersToRemove){
headers.removeHeaderNamed(hdr);
}
}
if(sampler != null) {
target.deliverSampler(sampler, new TestElement[] { captureHttpHeaders ? headers : null }, result);
}