Skip to content
Permalink
Browse files
8273142: Remove dependancy of TestHttpServer, HttpTransaction, HttpCa…
…llback from open/test/jdk/sun/net/www/protocol/http/ tests

Reviewed-by: michaelm
  • Loading branch information
mahendrachhipa authored and Michael-Mc-Mahon committed Sep 30, 2021
1 parent 94e31e5 commit 2f955d6f5ba15fc2c06eaf683d1ffa3ade97521b
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -24,17 +24,30 @@
/*
* @test
* @bug 6296310
* @modules java.base/sun.net.www
* @library ../../httptest/
* @build HttpCallback TestHttpServer HttpTransaction
* @library /test/lib
* @run main/othervm B6296310
* @run main/othervm -Djava.net.preferIPv6Addresses=true B6296310
* @summary REGRESSION: AppletClassLoader.getResourceAsStream() behaviour is wrong in some cases
*/

import java.net.*;
import java.io.*;
import java.util.*;
import java.io.IOException;
import java.io.OutputStream;
import java.net.CacheRequest;
import java.net.CacheResponse;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.ResponseCache;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.util.Map;
import java.util.concurrent.Executors;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

/*
* http server returns 200 and content-length=0
@@ -44,7 +57,7 @@
public class B6296310
{
static SimpleHttpTransaction httpTrans;
static TestHttpServer server;
static HttpServer server;

public static void main(String[] args) throws Exception
{
@@ -56,31 +69,35 @@ public static void main(String[] args) throws Exception
public static void startHttpServer() throws IOException {
httpTrans = new SimpleHttpTransaction();
InetAddress loopback = InetAddress.getLoopbackAddress();
server = new TestHttpServer(httpTrans, 1, 10, loopback, 0);
server = HttpServer.create(new InetSocketAddress(loopback, 0), 10);
server.createContext("/", httpTrans);
server.setExecutor(Executors.newSingleThreadExecutor());
server.start();
}

public static void makeHttpCall() throws IOException {
try {
System.out.println("http server listen on: " + server.getLocalPort());
System.out.println("http server listen on: " + server.getAddress().getPort());
URL url = new URL("http" , InetAddress.getLoopbackAddress().getHostAddress(),
server.getLocalPort(), "/");
server.getAddress().getPort(), "/");
HttpURLConnection uc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
System.out.println(uc.getResponseCode());
} finally {
server.terminate();
server.stop(1);
}
}
}

class SimpleHttpTransaction implements HttpCallback
class SimpleHttpTransaction implements HttpHandler
{
/*
* Our http server which simply retruns a file with no content
*/
public void request(HttpTransaction trans) {
@Override
public void handle(HttpExchange trans) {
try {
trans.setResponseEntityBody("");
trans.sendResponse(200, "OK");
trans.sendResponseHeaders(200, 0);
trans.close();
} catch (Exception e) {
e.printStackTrace();
}
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2002, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -24,20 +24,32 @@
/**
* @test
* @bug 4726087
* @modules java.base/sun.net.www
* @library ../../httptest/
* @build HttpCallback TestHttpServer ClosedChannelList HttpTransaction
* @run main RelativeRedirect
* @library /test/lib
* @run main/othervm RelativeRedirect
* @run main/othervm -Djava.net.preferIPv6Addresses=true RelativeRedirect
* @summary URLConnection cannot handle redirects
*/

import java.io.*;
import java.net.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
import java.net.Proxy;
import java.net.URL;
import java.util.concurrent.Executors;

public class RelativeRedirect implements HttpCallback {
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

public class RelativeRedirect implements HttpHandler {
static int count = 0;
static TestHttpServer server;
static HttpServer server;


static class MyAuthenticator extends Authenticator {
public MyAuthenticator () {
@@ -50,26 +62,29 @@ public PasswordAuthentication getPasswordAuthentication ()
}
}

void firstReply (HttpTransaction req) throws IOException {
req.addResponseHeader ("Connection", "close");
req.addResponseHeader ("Location", "/redirect/file.html");
req.sendResponse (302, "Moved Permamently");
req.orderlyClose();
void firstReply(HttpExchange req) throws IOException {
req.getResponseHeaders().set("Connection", "close");
req.getResponseHeaders().set("Location", "/redirect/file.html");
req.sendResponseHeaders(302, -1);
}

void secondReply (HttpTransaction req) throws IOException {
void secondReply (HttpExchange req) throws IOException {
if (req.getRequestURI().toString().equals("/redirect/file.html") &&
req.getRequestHeader("Host").equals(authority(server.getLocalPort()))) {
req.setResponseEntityBody ("Hello .");
req.sendResponse (200, "Ok");
req.getRequestHeaders().get("Host").get(0).equals(authority(server.getAddress().getPort()))) {
req.sendResponseHeaders(200, 0);
try(PrintWriter pw = new PrintWriter(req.getResponseBody())) {
pw.print("Hello .");
}
} else {
req.setResponseEntityBody (req.getRequestURI().toString());
req.sendResponse (400, "Bad request");
req.sendResponseHeaders(400, 0);
try(PrintWriter pw = new PrintWriter(req.getResponseBody())) {
pw.print(req.getRequestURI().toString());
}
}
req.orderlyClose();

}
public void request (HttpTransaction req) {

@Override
public void handle (HttpExchange req) {
try {
switch (count) {
case 0:
@@ -101,9 +116,12 @@ public static void main (String[] args) throws Exception {
MyAuthenticator auth = new MyAuthenticator ();
Authenticator.setDefault (auth);
try {
server = new TestHttpServer (new RelativeRedirect(), 1, 10, loopback, 0);
System.out.println ("Server: listening on port: " + server.getLocalPort());
URL url = new URL("http://" + authority(server.getLocalPort()));
server = HttpServer.create(new InetSocketAddress(loopback, 0), 10);
server.createContext("/", new RelativeRedirect());
server.setExecutor(Executors.newSingleThreadExecutor());
server.start();
System.out.println ("Server: listening on port: " + server.getAddress().getPort());
URL url = new URL("http://" + authority(server.getAddress().getPort()));
System.out.println ("client opening connection to: " + url);
HttpURLConnection urlc = (HttpURLConnection)url.openConnection (Proxy.NO_PROXY);
InputStream is = urlc.getInputStream ();
@@ -112,7 +130,7 @@ public static void main (String[] args) throws Exception {
throw new RuntimeException(e);
} finally {
if (server != null) {
server.terminate();
server.stop(1);
}
}
}
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -25,33 +25,48 @@
* @test
* @bug 6262486
* @library /test/lib
* @modules java.base/sun.net.www
* @library ../../httptest/
* @build HttpCallback TestHttpServer ClosedChannelList HttpTransaction
* @run main/othervm -Dhttp.keepAlive=false ResponseCacheStream
* @summary COMPATIBILITY: jagex_com - Monkey Puzzle applet fails to load
*/

import java.net.*;
import java.io.*;
import java.util.*;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.CacheRequest;
import java.net.CacheResponse;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ResponseCache;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import jdk.test.lib.net.URIBuilder;

public class ResponseCacheStream implements HttpCallback {
public class ResponseCacheStream implements HttpHandler {

void okReply (HttpTransaction req) throws IOException {
req.setResponseEntityBody ("Hello, This is the response body. Let's make it as long as possible since we need to test the cache mechanism.");
req.sendResponse (200, "Ok");
System.out.println ("Server: sent response");
req.orderlyClose();
void okReply (HttpExchange req) throws IOException {
req.sendResponseHeaders(200, 0);
try(PrintWriter pw = new PrintWriter(req.getResponseBody())) {
pw.print("Hello, This is the response body. Let's make it as long as possible since we need to test the cache mechanism.");
}
System.out.println ("Server: sent response");
}

public void request (HttpTransaction req) {
try {
okReply (req);
} catch (IOException e) {
e.printStackTrace();
}

@Override
public void handle(HttpExchange exchange) throws IOException {
okReply(exchange);
exchange.close();
}

static class MyCacheRequest extends CacheRequest {
@@ -94,19 +109,22 @@ public CacheResponse get(URI uri, String rqstMethod, Map<String, List<String>> r
}
}

static TestHttpServer server;
static HttpServer server;

public static void main(String[] args) throws Exception {
MyResponseCache cache = new MyResponseCache();
try {
InetAddress loopback = InetAddress.getLoopbackAddress();
ResponseCache.setDefault(cache);
server = new TestHttpServer (new ResponseCacheStream(), loopback, 0);
System.out.println ("Server: listening on port: " + server.getLocalPort());
server = HttpServer.create(new InetSocketAddress(loopback, 0), 10);
server.createContext("/", new ResponseCacheStream());
server.setExecutor(Executors.newSingleThreadExecutor());
server.start();
System.out.println("Server: listening on port: " + server.getAddress().getPort());
URL url = URIBuilder.newBuilder()
.scheme("http")
.loopback()
.port(server.getLocalPort())
.port(server.getAddress().getPort())
.path("/")
.toURL();
System.out.println ("Client: connecting to " + url);
@@ -149,10 +167,10 @@ public static void main(String[] args) throws Exception {
}
} catch (Exception e) {
if (server != null) {
server.terminate();
server.stop(1);
}
throw e;
}
server.terminate();
server.stop(1);
}
}
Loading

1 comment on commit 2f955d6

@openjdk-notifier

This comment has been minimized.

Copy link

@openjdk-notifier openjdk-notifier bot commented on 2f955d6 Sep 30, 2021

Please sign in to comment.