< prev index next >

test/jdk/java/net/httpclient/lib/jdk/httpclient/test/lib/common/HttpServerAdapters.java

Print this page

 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 package jdk.httpclient.test.lib.common;
 24 
 25 import com.sun.net.httpserver.Authenticator;
 26 import com.sun.net.httpserver.BasicAuthenticator;
 27 import com.sun.net.httpserver.Filter;
 28 import com.sun.net.httpserver.Headers;
 29 import com.sun.net.httpserver.HttpContext;
 30 import com.sun.net.httpserver.HttpExchange;
 31 import com.sun.net.httpserver.HttpHandler;
 32 import com.sun.net.httpserver.HttpServer;
 33 import com.sun.net.httpserver.HttpsConfigurator;
 34 import com.sun.net.httpserver.HttpsServer;
 35 import jdk.httpclient.test.lib.http2.Http2Handler;
 36 import jdk.httpclient.test.lib.http2.Http2TestExchange;
 37 import jdk.httpclient.test.lib.http2.Http2TestServer;
 38 import jdk.internal.net.http.common.HttpHeadersBuilder;
 39 
 40 import java.net.InetAddress;
 41 import java.io.ByteArrayInputStream;
 42 import java.net.http.HttpClient.Version;
 43 import java.io.ByteArrayOutputStream;
 44 import java.io.IOException;
 45 import java.io.InputStream;
 46 import java.io.OutputStream;
 47 import java.io.PrintStream;
 48 import java.io.UncheckedIOException;
 49 import java.math.BigInteger;
 50 import java.net.InetSocketAddress;
 51 import java.net.URI;
 52 import java.net.http.HttpHeaders;
 53 import java.util.Base64;

781          * @throws IOException if any exception occurs during the server creation
782          */
783         public static HttpTestServer create(Version serverVersion, SSLContext sslContext,
784                                             ExecutorService executor) throws IOException {
785             Objects.requireNonNull(serverVersion);
786             switch (serverVersion) {
787                 case HTTP_2 -> {
788                     Http2TestServer underlying;
789                     try {
790                         underlying = sslContext == null
791                                 ? new Http2TestServer("localhost", false, 0, executor, null) // HTTP
792                                 : new Http2TestServer("localhost", true, 0, executor, sslContext); // HTTPS
793                     } catch (IOException ioe) {
794                         throw ioe;
795                     } catch (Exception e) {
796                         throw new IOException(e);
797                     }
798                     return HttpTestServer.of(underlying);
799                 }
800                 case HTTP_1_1 ->  {
801                     InetSocketAddress sa = new InetSocketAddress(
802                             InetAddress.getLoopbackAddress(), 0);
803                     HttpServer underlying;
804                     if (sslContext == null) {
805                         underlying = HttpServer.create(sa, 0); // HTTP
806                     } else {
807                         HttpsServer https = HttpsServer.create(sa, 0); // HTTPS
808                         https.setHttpsConfigurator(new HttpsConfigurator(sslContext));
809                         underlying = https;
810                     }
811                     if (executor != null) {
812                         underlying.setExecutor(executor);
813                     }
814                     return HttpTestServer.of(underlying);
815                 }
816                 default -> throw new IllegalArgumentException("Unsupported HTTP version "
817                         + serverVersion);
818             }
819         }
820 
821         private static class Http1TestServer extends  HttpTestServer {
822             private final HttpServer impl;
823             private final ExecutorService executor;
824             Http1TestServer(HttpServer server) {
825                 this(server, null);
826             }
827             Http1TestServer(HttpServer server, ExecutorService executor) {
828                 if (executor != null) server.setExecutor(executor);

 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 package jdk.httpclient.test.lib.common;
 24 
 25 import com.sun.net.httpserver.Authenticator;
 26 import com.sun.net.httpserver.BasicAuthenticator;
 27 import com.sun.net.httpserver.Filter;
 28 import com.sun.net.httpserver.Headers;
 29 import com.sun.net.httpserver.HttpContext;
 30 import com.sun.net.httpserver.HttpExchange;
 31 import com.sun.net.httpserver.HttpHandler;
 32 import com.sun.net.httpserver.HttpServer;

 33 import com.sun.net.httpserver.HttpsServer;
 34 import jdk.httpclient.test.lib.http2.Http2Handler;
 35 import jdk.httpclient.test.lib.http2.Http2TestExchange;
 36 import jdk.httpclient.test.lib.http2.Http2TestServer;
 37 import jdk.internal.net.http.common.HttpHeadersBuilder;
 38 
 39 import java.net.InetAddress;
 40 import java.io.ByteArrayInputStream;
 41 import java.net.http.HttpClient.Version;
 42 import java.io.ByteArrayOutputStream;
 43 import java.io.IOException;
 44 import java.io.InputStream;
 45 import java.io.OutputStream;
 46 import java.io.PrintStream;
 47 import java.io.UncheckedIOException;
 48 import java.math.BigInteger;
 49 import java.net.InetSocketAddress;
 50 import java.net.URI;
 51 import java.net.http.HttpHeaders;
 52 import java.util.Base64;

780          * @throws IOException if any exception occurs during the server creation
781          */
782         public static HttpTestServer create(Version serverVersion, SSLContext sslContext,
783                                             ExecutorService executor) throws IOException {
784             Objects.requireNonNull(serverVersion);
785             switch (serverVersion) {
786                 case HTTP_2 -> {
787                     Http2TestServer underlying;
788                     try {
789                         underlying = sslContext == null
790                                 ? new Http2TestServer("localhost", false, 0, executor, null) // HTTP
791                                 : new Http2TestServer("localhost", true, 0, executor, sslContext); // HTTPS
792                     } catch (IOException ioe) {
793                         throw ioe;
794                     } catch (Exception e) {
795                         throw new IOException(e);
796                     }
797                     return HttpTestServer.of(underlying);
798                 }
799                 case HTTP_1_1 ->  {
800                     InetAddress loopback = InetAddress.getLoopbackAddress();
801                     InetSocketAddress sa = new InetSocketAddress(loopback, 0);
802                     HttpServer underlying;
803                     if (sslContext == null) {
804                         underlying = HttpServer.create(sa, 0); // HTTP
805                     } else {
806                         HttpsServer https = HttpsServer.create(sa, 0); // HTTPS
807                         https.setHttpsConfigurator(new TestServerConfigurator(loopback, sslContext));
808                         underlying = https;
809                     }
810                     if (executor != null) {
811                         underlying.setExecutor(executor);
812                     }
813                     return HttpTestServer.of(underlying);
814                 }
815                 default -> throw new IllegalArgumentException("Unsupported HTTP version "
816                         + serverVersion);
817             }
818         }
819 
820         private static class Http1TestServer extends  HttpTestServer {
821             private final HttpServer impl;
822             private final ExecutorService executor;
823             Http1TestServer(HttpServer server) {
824                 this(server, null);
825             }
826             Http1TestServer(HttpServer server, ExecutorService executor) {
827                 if (executor != null) server.setExecutor(executor);
< prev index next >