From e413843b3b3f13fe87bbb589c31383c38ee948fb Mon Sep 17 00:00:00 2001 From: malakaganga Date: Thu, 5 Dec 2024 16:02:30 +0530 Subject: [PATCH] Fix java.net.URI class does not support underscore in the URI Fixes: https://github.com/wso2/product-micro-integrator/issues/3802 --- .../http/conn/ClientConnFactory.java | 12 ++++++++ .../http/conn/ClientSSLSetupHandler.java | 30 +++++++++++++------ 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/http/conn/ClientConnFactory.java b/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/http/conn/ClientConnFactory.java index 1f27e997f2..095f3713ac 100644 --- a/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/http/conn/ClientConnFactory.java +++ b/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/http/conn/ClientConnFactory.java @@ -33,9 +33,11 @@ import org.apache.http.params.HttpParams; import java.net.InetSocketAddress; +import java.net.MalformedURLException; import java.net.SocketAddress; import java.net.URI; import java.net.URISyntaxException; +import java.net.URL; import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; @@ -195,6 +197,7 @@ private SSLIOSession createClientModeSSLsession(IOSession iosession, SSLContext int port; if (endpoint != null && !endpoint.isEmpty()) { URI endpointURI; + URL endpointURL; try { endpointURI = new URI(endpoint); } catch (URISyntaxException e) { @@ -202,6 +205,15 @@ private SSLIOSession createClientModeSSLsession(IOSession iosession, SSLContext } hostname = endpointURI.getHost(); port = endpointURI.getPort(); + if (hostname == null) { + try { + endpointURL = new URL(endpoint); + } catch (MalformedURLException e) { + throw new IllegalArgumentException("Invalid endpointURL"); + } + hostname = endpointURL.getHost(); + port = endpointURL.getPort(); + } } else { hostname = ((InetSocketAddress) address).getHostName(); port = ((InetSocketAddress) address).getPort(); diff --git a/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/http/conn/ClientSSLSetupHandler.java b/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/http/conn/ClientSSLSetupHandler.java index 9b61eea80d..a0907dba2c 100644 --- a/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/http/conn/ClientSSLSetupHandler.java +++ b/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/http/conn/ClientSSLSetupHandler.java @@ -18,6 +18,18 @@ */ package org.apache.synapse.transport.http.conn; +import java.net.InetSocketAddress; +import java.net.MalformedURLException; +import java.net.SocketAddress; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; +import java.util.Arrays; + +import javax.net.ssl.SSLEngine; +import javax.net.ssl.SSLException; +import javax.net.ssl.SSLSession; + import org.apache.http.conn.ssl.AbstractVerifier; import org.apache.http.conn.ssl.X509HostnameVerifier; import org.apache.http.nio.reactor.IOSession; @@ -25,15 +37,6 @@ import org.apache.synapse.transport.certificatevalidation.CertificateVerificationException; import org.apache.synapse.transport.certificatevalidation.CertificateVerificationManager; -import javax.net.ssl.SSLEngine; -import javax.net.ssl.SSLException; -import javax.net.ssl.SSLSession; -import java.net.InetSocketAddress; -import java.net.SocketAddress; -import java.net.URI; -import java.net.URISyntaxException; -import java.util.Arrays; - public class ClientSSLSetupHandler implements SSLSetupHandler { private final static String[] LOCALHOSTS = {"::1", "127.0.0.1", @@ -167,7 +170,16 @@ public void verify(IOSession iosession, SSLSession sslsession) throws SSLExcepti if (endpoint != null && !endpoint.isEmpty()) { try { URI endpointURI = new URI(endpoint); + URL endpointURL; address = endpointURI.getHost(); + if (address == null) { + try { + endpointURL = new URL(endpoint); + } catch (MalformedURLException e) { + throw new IllegalArgumentException("Invalid endpointURL"); + } + address = endpointURL.getHost(); + } } catch (URISyntaxException e) { throw new IllegalArgumentException("Invalid endpointURI: "+ endpoint, e); }