Skip to content

Commit

Permalink
Fix #285 by making send-authorization work again
Browse files Browse the repository at this point in the history
  • Loading branch information
ndw committed Dec 21, 2018
1 parent 31fd85c commit c574db2
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions src/main/java/com/xmlcalabash/library/HttpRequest.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import org.apache.http.NameValuePair; import org.apache.http.NameValuePair;
import org.apache.http.auth.AuthScope; import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
import org.apache.http.client.CookieStore; import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient; import org.apache.http.client.HttpClient;
import org.apache.http.client.config.AuthSchemes; import org.apache.http.client.config.AuthSchemes;
Expand All @@ -61,21 +62,22 @@
import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpHead; import org.apache.http.client.methods.HttpHead;
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut; import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.cookie.Cookie; import org.apache.http.cookie.Cookie;
import org.apache.http.entity.ByteArrayEntity; import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.ContentType; import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity; import org.apache.http.entity.StringEntity;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.BasicCookieStore; import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.StandardHttpRequestRetryHandler; import org.apache.http.impl.client.StandardHttpRequestRetryHandler;
import org.apache.http.message.BasicHeader; import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpCoreContext; import org.apache.http.protocol.HttpCoreContext;
import org.apache.http.util.EntityUtils; import org.apache.http.util.EntityUtils;
import org.json.JSONTokener; import org.json.JSONTokener;
Expand Down Expand Up @@ -129,6 +131,7 @@ public class HttpRequest extends DefaultStep {
private static final int bufSize = 912 * 8; // A multiple of 3, 4, and 75 for base64 line breaking private static final int bufSize = 912 * 8; // A multiple of 3, 4, and 75 for base64 line breaking


private boolean detailed = false; private boolean detailed = false;
private boolean sendAuthorization = false;
private URI requestURI = null; private URI requestURI = null;
private Vector<Header> headers = new Vector<Header> (); private Vector<Header> headers = new Vector<Header> ();
private String overrideContentType = null; private String overrideContentType = null;
Expand Down Expand Up @@ -193,6 +196,7 @@ public void run() throws SaxonApiException {
boolean statusOnly = "true".equals(start.getAttributeValue(_status_only)); boolean statusOnly = "true".equals(start.getAttributeValue(_status_only));
String method = start.getAttributeValue(_method); String method = start.getAttributeValue(_method);
detailed = "true".equals(start.getAttributeValue(_detailed)); detailed = "true".equals(start.getAttributeValue(_detailed));
sendAuthorization = "true".equals(start.getAttributeValue(_send_authorization));
overrideContentType = start.getAttributeValue(_override_content_type); overrideContentType = start.getAttributeValue(_override_content_type);


if (method == null) { if (method == null) {
Expand All @@ -218,7 +222,7 @@ public void run() throws SaxonApiException {
RequestConfig.Builder rqbuilder = RequestConfig.custom(); RequestConfig.Builder rqbuilder = RequestConfig.custom();
rqbuilder.setCookieSpec(CookieSpecs.DEFAULT); rqbuilder.setCookieSpec(CookieSpecs.DEFAULT);


HttpContext localContext = new BasicHttpContext(); HttpClientContext localContext = HttpClientContext.create();


// What about cookies // What about cookies
String saveCookieKey = step.getExtensionAttribute(cx_save_cookies); String saveCookieKey = step.getExtensionAttribute(cx_save_cookies);
Expand Down Expand Up @@ -257,24 +261,32 @@ public void run() throws SaxonApiException {
String pass = start.getAttributeValue(_password); String pass = start.getAttributeValue(_password);
String meth = start.getAttributeValue(_auth_method); String meth = start.getAttributeValue(_auth_method);


String host = requestURI.getHost();
int port = requestURI.getPort();
AuthScope scope = new AuthScope(host,port); // Or this? new AuthScope(null, AuthScope.ANY_PORT)
BasicCredentialsProvider bCredsProvider = new BasicCredentialsProvider();
bCredsProvider.setCredentials(scope, new UsernamePasswordCredentials(user, pass));

List<String> authpref; List<String> authpref;
if ("basic".equalsIgnoreCase(meth)) { if ("basic".equalsIgnoreCase(meth)) {
authpref = Collections.singletonList(AuthSchemes.BASIC); authpref = Collections.singletonList(AuthSchemes.BASIC);

if (sendAuthorization) {
// See https://stackoverflow.com/questions/20914311/httpclientbuilder-basic-auth
AuthCache authCache = new BasicAuthCache();
BasicScheme basicAuth = new BasicScheme();
authCache.put(new HttpHost(host, port), basicAuth);

localContext.setCredentialsProvider(bCredsProvider);
localContext.setAuthCache(authCache);
}
} else if ("digest".equalsIgnoreCase(meth)) { } else if ("digest".equalsIgnoreCase(meth)) {
authpref = Collections.singletonList(AuthSchemes.DIGEST); authpref = Collections.singletonList(AuthSchemes.DIGEST);
} else { } else {
throw XProcException.stepError(3, "Unsupported auth-method: " + meth); throw XProcException.stepError(3, "Unsupported auth-method: " + meth);
} }


rqbuilder.setProxyPreferredAuthSchemes(authpref); rqbuilder.setProxyPreferredAuthSchemes(authpref);

String host = requestURI.getHost();
int port = requestURI.getPort();
AuthScope scope = new AuthScope(host,port);
// Or this? new AuthScope(null, AuthScope.ANY_PORT)

BasicCredentialsProvider bCredsProvider = new BasicCredentialsProvider();
bCredsProvider.setCredentials(scope, new UsernamePasswordCredentials(user, pass));
builder.setDefaultCredentialsProvider(bCredsProvider); builder.setDefaultCredentialsProvider(bCredsProvider);
} }


Expand Down

0 comments on commit c574db2

Please sign in to comment.