use of org.apache.jmeter.protocol.http.control.AuthManager in project jmeter by apache.
the class HTTPHC4Impl method executeRequest.
/**
* Execute request either as is or under PrivilegedAction
* if a Subject is available for url
* @param httpClient the {@link CloseableHttpClient} to be used to execute the httpRequest
* @param httpRequest the {@link HttpRequest} to be executed
* @param localContext th {@link HttpContext} to be used for execution
* @param url the target url (will be used to look up a possible subject for the execution)
* @return the result of the execution of the httpRequest
* @throws IOException
* @throws ClientProtocolException
*/
private CloseableHttpResponse executeRequest(final CloseableHttpClient httpClient, final HttpRequestBase httpRequest, final HttpContext localContext, final URL url) throws IOException, ClientProtocolException {
AuthManager authManager = getAuthManager();
if (authManager != null) {
Subject subject = authManager.getSubjectForUrl(url);
if (subject != null) {
try {
return Subject.doAs(subject, (PrivilegedExceptionAction<CloseableHttpResponse>) () -> httpClient.execute(httpRequest, localContext));
} catch (PrivilegedActionException e) {
log.error("Can't execute httpRequest with subject: {}", subject, e);
throw new RuntimeException("Can't execute httpRequest with subject:" + subject, e);
}
}
if (BASIC_AUTH_PREEMPTIVE) {
Authorization authorization = authManager.getAuthForURL(url);
if (authorization != null && Mechanism.BASIC_DIGEST.equals(authorization.getMechanism())) {
HttpHost target = new HttpHost(url.getHost(), url.getPort(), url.getProtocol());
// Create AuthCache instance
AuthCache authCache = new BasicAuthCache();
// Generate BASIC scheme object and
// add it to the local auth cache
BasicScheme basicAuth = new BasicScheme();
authCache.put(target, basicAuth);
// Add AuthCache to the execution context
localContext.setAttribute(HttpClientContext.AUTH_CACHE, authCache);
}
}
}
return httpClient.execute(httpRequest, localContext);
}
use of org.apache.jmeter.protocol.http.control.AuthManager in project jmeter by apache.
the class AjpSampler method setConnectionHeaders.
private String setConnectionHeaders(URL url, String host, String method) throws IOException {
HeaderManager headers = getHeaderManager();
AuthManager auth = getAuthManager();
StringBuilder hbuf = new StringBuilder();
// Allow Headers to override Host setting
//$NON-NLS-1$
hbuf.append("Host").append(COLON_SPACE).append(host).append(NEWLINE);
//Host
setInt(0xA00b);
setString(host);
if (headers != null) {
for (JMeterProperty jMeterProperty : headers.getHeaders()) {
Header header = (Header) jMeterProperty.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(HTTPConstants.POST)) {
int cl = -1;
HTTPFileArg[] hfa = getHTTPFiles();
if (hfa.length > 0) {
HTTPFileArg fa = hfa[0];
String fn = fa.getPath();
File input = new File(fn);
cl = (int) input.length();
if (body != null) {
JOrphanUtils.closeQuietly(body);
body = null;
}
body = new BufferedInputStream(new FileInputStream(input));
setString(HTTPConstants.HEADER_CONTENT_DISPOSITION);
setString("form-data; name=\"" + encode(fa.getParamName()) + "\"; filename=\"" + encode(fn) + //$NON-NLS-1$ //$NON-NLS-2$
"\"");
String mt = fa.getMimeType();
hbuf.append(HTTPConstants.HEADER_CONTENT_TYPE).append(COLON_SPACE).append(mt).append(NEWLINE);
// content-type
setInt(0xA007);
setString(mt);
} else {
hbuf.append(HTTPConstants.HEADER_CONTENT_TYPE).append(COLON_SPACE).append(HTTPConstants.APPLICATION_X_WWW_FORM_URLENCODED).append(NEWLINE);
// content-type
setInt(0xA007);
setString(HTTPConstants.APPLICATION_X_WWW_FORM_URLENCODED);
StringBuilder sb = new StringBuilder();
boolean first = true;
for (JMeterProperty arg : getArguments()) {
if (first) {
first = false;
} else {
sb.append('&');
}
sb.append(arg.getStringValue());
}
stringBody = sb.toString();
// TODO - charset?
byte[] sbody = stringBody.getBytes();
cl = sbody.length;
body = new ByteArrayInputStream(sbody);
}
hbuf.append(HTTPConstants.HEADER_CONTENT_LENGTH).append(COLON_SPACE).append(String.valueOf(cl)).append(NEWLINE);
// Content-length
setInt(0xA008);
setString(String.valueOf(cl));
}
if (auth != null) {
String authHeader = auth.getAuthHeaderForURL(url);
if (authHeader != null) {
// Authorization
setInt(0xA005);
setString(authHeader);
hbuf.append(HTTPConstants.HEADER_AUTHORIZATION).append(COLON_SPACE).append(authHeader).append(NEWLINE);
}
}
return hbuf.toString();
}
use of org.apache.jmeter.protocol.http.control.AuthManager in project jmeter by apache.
the class AjpSampler method getHeaderSize.
private int getHeaderSize(String method, URL url) {
HeaderManager headers = getHeaderManager();
CookieManager cookies = getCookieManager();
AuthManager auth = getAuthManager();
// Host always
int hsz = 1;
if (method.equals(HTTPConstants.POST)) {
HTTPFileArg[] hfa = getHTTPFiles();
if (hfa.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;
}
Aggregations