use of java.net.InetAddress in project okhttp by square.
the class URLConnectionTest method testConnectViaSocketFactory.
public void testConnectViaSocketFactory(boolean useHttps) throws IOException {
SocketFactory uselessSocketFactory = new SocketFactory() {
public Socket createSocket() {
throw new IllegalArgumentException("useless");
}
public Socket createSocket(InetAddress host, int port) {
return null;
}
public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) {
return null;
}
public Socket createSocket(String host, int port) {
return null;
}
public Socket createSocket(String host, int port, InetAddress localHost, int localPort) {
return null;
}
};
if (useHttps) {
server.useHttps(sslClient.socketFactory, false);
urlFactory.setClient(urlFactory.client().newBuilder().sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).hostnameVerifier(new RecordingHostnameVerifier()).build());
}
server.enqueue(new MockResponse().setStatus("HTTP/1.1 200 OK"));
urlFactory.setClient(urlFactory.client().newBuilder().socketFactory(uselessSocketFactory).build());
connection = urlFactory.open(server.url("/").url());
try {
connection.getResponseCode();
fail();
} catch (IllegalArgumentException expected) {
}
urlFactory.setClient(urlFactory.client().newBuilder().socketFactory(SocketFactory.getDefault()).build());
connection = urlFactory.open(server.url("/").url());
assertEquals(200, connection.getResponseCode());
}
use of java.net.InetAddress in project rest.li by linkedin.
the class AbstractNettyStreamClient method writeRequest.
private void writeRequest(final Request request, final RequestContext requestContext, final TimeoutTransportCallback<StreamResponse> callback) {
State state = _state.get();
if (state != State.RUNNING) {
errorResponse(callback, new IllegalStateException("Client is " + state));
return;
}
URI uri = request.getURI();
String scheme = uri.getScheme();
if (!"http".equalsIgnoreCase(scheme) && !"https".equalsIgnoreCase(scheme)) {
errorResponse(callback, new IllegalArgumentException("Unknown scheme: " + scheme + " (only http/https is supported)"));
return;
}
String host = uri.getHost();
int port = uri.getPort();
if (port == -1) {
port = "http".equalsIgnoreCase(scheme) ? HTTP_DEFAULT_PORT : HTTPS_DEFAULT_PORT;
}
final SocketAddress address;
try {
// TODO investigate DNS resolution and timing
InetAddress inetAddress = InetAddress.getByName(host);
address = new InetSocketAddress(inetAddress, port);
requestContext.putLocalAttr(R2Constants.REMOTE_SERVER_ADDR, inetAddress.getHostAddress());
} catch (UnknownHostException e) {
errorResponse(callback, e);
return;
}
doWriteRequest(request, requestContext, address, callback);
}
use of java.net.InetAddress in project robovm by robovm.
the class DefaultClientConnectionOperator method openConnection.
// non-javadoc, see interface ClientConnectionOperator
public void openConnection(OperatedClientConnection conn, HttpHost target, InetAddress local, HttpContext context, HttpParams params) throws IOException {
if (conn == null) {
throw new IllegalArgumentException("Connection must not be null.");
}
if (target == null) {
throw new IllegalArgumentException("Target host must not be null.");
}
//@@@ is context allowed to be null?
if (params == null) {
throw new IllegalArgumentException("Parameters must not be null.");
}
if (conn.isOpen()) {
throw new IllegalArgumentException("Connection must not be open.");
}
final Scheme schm = schemeRegistry.getScheme(target.getSchemeName());
final SocketFactory sf = schm.getSocketFactory();
final SocketFactory plain_sf;
final LayeredSocketFactory layered_sf;
if (sf instanceof LayeredSocketFactory) {
plain_sf = staticPlainSocketFactory;
layered_sf = (LayeredSocketFactory) sf;
} else {
plain_sf = sf;
layered_sf = null;
}
InetAddress[] addresses = InetAddress.getAllByName(target.getHostName());
for (int i = 0; i < addresses.length; ++i) {
Socket sock = plain_sf.createSocket();
conn.opening(sock, target);
try {
Socket connsock = plain_sf.connectSocket(sock, addresses[i].getHostAddress(), schm.resolvePort(target.getPort()), local, 0, params);
if (sock != connsock) {
sock = connsock;
conn.opening(sock, target);
}
/*
* prepareSocket is called on the just connected
* socket before the creation of the layered socket to
* ensure that desired socket options such as
* TCP_NODELAY, SO_RCVTIMEO, SO_LINGER will be set
* before any I/O is performed on the socket. This
* happens in the common case as
* SSLSocketFactory.createSocket performs hostname
* verification which requires that SSL handshaking be
* performed.
*/
prepareSocket(sock, context, params);
if (layered_sf != null) {
Socket layeredsock = layered_sf.createSocket(sock, target.getHostName(), schm.resolvePort(target.getPort()), true);
if (layeredsock != sock) {
conn.opening(layeredsock, target);
}
conn.openCompleted(sf.isSecure(layeredsock), params);
} else {
conn.openCompleted(sf.isSecure(sock), params);
}
break;
// BEGIN android-changed
// catch SocketException to cover any kind of connect failure
} catch (SocketException ex) {
if (i == addresses.length - 1) {
ConnectException cause = ex instanceof ConnectException ? (ConnectException) ex : new ConnectException(ex.getMessage(), ex);
throw new HttpHostConnectException(target, cause);
}
// END android-changed
} catch (ConnectTimeoutException ex) {
if (i == addresses.length - 1) {
throw ex;
}
}
}
}
use of java.net.InetAddress in project robovm by robovm.
the class DefaultHttpRoutePlanner method determineRoute.
// non-javadoc, see interface HttpRoutePlanner
public HttpRoute determineRoute(HttpHost target, HttpRequest request, HttpContext context) throws HttpException {
if (request == null) {
throw new IllegalStateException("Request must not be null.");
}
// If we have a forced route, we can do without a target.
HttpRoute route = ConnRouteParams.getForcedRoute(request.getParams());
if (route != null)
return route;
if (target == null) {
throw new IllegalStateException("Target host must not be null.");
}
final InetAddress local = ConnRouteParams.getLocalAddress(request.getParams());
final HttpHost proxy = ConnRouteParams.getDefaultProxy(request.getParams());
final Scheme schm = schemeRegistry.getScheme(target.getSchemeName());
// as it is typically used for TLS/SSL, we assume that
// a layered scheme implies a secure connection
final boolean secure = schm.isLayered();
if (proxy == null) {
route = new HttpRoute(target, local, secure);
} else {
route = new HttpRoute(target, local, proxy, secure);
}
return route;
}
use of java.net.InetAddress in project robovm by robovm.
the class ProxySelectorRoutePlanner method determineRoute.
// non-javadoc, see interface HttpRoutePlanner
public HttpRoute determineRoute(HttpHost target, HttpRequest request, HttpContext context) throws HttpException {
if (request == null) {
throw new IllegalStateException("Request must not be null.");
}
// If we have a forced route, we can do without a target.
HttpRoute route = ConnRouteParams.getForcedRoute(request.getParams());
if (route != null)
return route;
if (target == null) {
throw new IllegalStateException("Target host must not be null.");
}
final InetAddress local = ConnRouteParams.getLocalAddress(request.getParams());
// BEGIN android-changed
// If the client or request explicitly specifies a proxy (or no
// proxy), prefer that over the ProxySelector's VM-wide default.
HttpHost proxy = (HttpHost) request.getParams().getParameter(ConnRoutePNames.DEFAULT_PROXY);
if (proxy == null) {
proxy = determineProxy(target, request, context);
} else if (ConnRouteParams.NO_HOST.equals(proxy)) {
// value is explicitly unset
proxy = null;
}
// END android-changed
final Scheme schm = this.schemeRegistry.getScheme(target.getSchemeName());
// as it is typically used for TLS/SSL, we assume that
// a layered scheme implies a secure connection
final boolean secure = schm.isLayered();
if (proxy == null) {
route = new HttpRoute(target, local, secure);
} else {
route = new HttpRoute(target, local, proxy, secure);
}
return route;
}
Aggregations