79 import java.util.HashSet; |
77 import java.util.HashSet; |
80 import java.util.LinkedList; |
78 import java.util.LinkedList; |
81 import java.util.List; |
79 import java.util.List; |
82 import java.util.Random; |
80 import java.util.Random; |
83 import jdk.testlibrary.SimpleSSLContext; |
81 import jdk.testlibrary.SimpleSSLContext; |
84 import static jdk.incubator.http.HttpRequest.BodyProcessor.fromFile; |
82 import static jdk.incubator.http.HttpRequest.BodyPublisher.fromFile; |
85 import static jdk.incubator.http.HttpRequest.BodyProcessor.fromInputStream; |
83 import static jdk.incubator.http.HttpRequest.BodyPublisher.fromInputStream; |
86 import static jdk.incubator.http.HttpRequest.BodyProcessor.fromString; |
84 import static jdk.incubator.http.HttpRequest.BodyPublisher.fromString; |
87 import static jdk.incubator.http.HttpResponse.*; |
85 import static jdk.incubator.http.HttpResponse.*; |
88 import static jdk.incubator.http.HttpResponse.BodyHandler.asFile; |
86 import static jdk.incubator.http.HttpResponse.BodyHandler.asFile; |
89 import static jdk.incubator.http.HttpResponse.BodyHandler.asString; |
87 import static jdk.incubator.http.HttpResponse.BodyHandler.asString; |
90 import java.util.concurrent.CountDownLatch; |
88 import java.util.concurrent.CountDownLatch; |
91 import java.util.logging.ConsoleHandler; |
89 import java.util.logging.ConsoleHandler; |
688 |
687 |
689 proxy = new ProxyServer(0, false); |
688 proxy = new ProxyServer(0, false); |
690 proxyPort = proxy.getPort(); |
689 proxyPort = proxy.getPort(); |
691 System.out.println("Proxy port = " + proxyPort); |
690 System.out.println("Proxy port = " + proxyPort); |
692 } |
691 } |
693 } |
692 |
694 |
693 static class RedirectHandler implements HttpHandler { |
695 class Configurator extends HttpsConfigurator { |
694 private final String root; |
696 public Configurator(SSLContext ctx) { |
695 private volatile int count = 0; |
697 super(ctx); |
696 |
698 } |
697 RedirectHandler(String root) { |
699 |
698 this.root = root; |
700 public void configure (HttpsParameters params) { |
699 } |
701 params.setSSLParameters (getSSLContext().getSupportedSSLParameters()); |
700 |
702 } |
701 @Override |
703 } |
702 public synchronized void handle(HttpExchange t) throws IOException { |
704 |
703 byte[] buf = new byte[2048]; |
705 class UploadServer extends Thread { |
704 try (InputStream is = t.getRequestBody()) { |
706 int statusCode; |
705 while (is.read(buf) != -1) ; |
707 ServerSocket ss; |
706 } |
708 int port; |
707 |
709 int size; |
708 Headers responseHeaders = t.getResponseHeaders(); |
710 Object lock; |
709 |
711 boolean failed = false; |
710 if (count++ < 1) { |
712 |
711 responseHeaders.add("Location", root + "/foo/" + count); |
713 UploadServer(int size) throws IOException { |
712 } else { |
714 this.statusCode = statusCode; |
713 responseHeaders.add("Location", SmokeTest.midSizedFilename); |
715 this.size = size; |
714 } |
716 ss = new ServerSocket(0); |
715 t.sendResponseHeaders(301, -1); |
717 port = ss.getLocalPort(); |
716 t.close(); |
718 lock = new Object(); |
717 } |
719 } |
718 |
720 |
719 int count() { |
721 int port() { |
720 return count; |
722 return port; |
721 } |
723 } |
722 |
724 |
723 void reset() { |
725 int size() { |
724 count = 0; |
726 return size; |
725 } |
727 } |
726 } |
728 |
727 |
729 // wait a sec before calling this |
728 static class RedirectErrorHandler implements HttpHandler { |
730 boolean failed() { |
729 private final String root; |
731 synchronized(lock) { |
730 private volatile int count = 1; |
732 return failed; |
731 |
733 } |
732 RedirectErrorHandler(String root) { |
734 } |
733 this.root = root; |
735 |
734 } |
736 @Override |
735 |
737 public void run () { |
736 synchronized int count() { |
738 int nbytes = 0; |
737 return count; |
739 Socket s = null; |
738 } |
740 |
739 |
741 synchronized(lock) { |
740 synchronized void increment() { |
|
741 count++; |
|
742 } |
|
743 |
|
744 @Override |
|
745 public synchronized void handle(HttpExchange t) throws IOException { |
|
746 try (InputStream is = t.getRequestBody()) { |
|
747 is.readAllBytes(); |
|
748 } |
|
749 |
|
750 Headers map = t.getResponseHeaders(); |
|
751 String redirect = root + "/foo/" + Integer.toString(count); |
|
752 increment(); |
|
753 map.add("Location", redirect); |
|
754 t.sendResponseHeaders(301, -1); |
|
755 t.close(); |
|
756 } |
|
757 } |
|
758 |
|
759 static class DelayHandler implements HttpHandler { |
|
760 |
|
761 CyclicBarrier bar1 = new CyclicBarrier(2); |
|
762 CyclicBarrier bar2 = new CyclicBarrier(2); |
|
763 CyclicBarrier bar3 = new CyclicBarrier(2); |
|
764 |
|
765 CyclicBarrier barrier1() { |
|
766 return bar1; |
|
767 } |
|
768 |
|
769 CyclicBarrier barrier2() { |
|
770 return bar2; |
|
771 } |
|
772 |
|
773 @Override |
|
774 public synchronized void handle(HttpExchange he) throws IOException { |
|
775 he.getRequestBody().readAllBytes(); |
742 try { |
776 try { |
743 s = ss.accept(); |
777 bar1.await(); |
744 |
778 bar2.await(); |
745 InputStream is = s.getInputStream(); |
779 } catch (Exception e) { } |
746 OutputStream os = s.getOutputStream(); |
780 he.sendResponseHeaders(200, -1); // will probably fail |
747 os.write("HTTP/1.1 201 OK\r\nContent-length: 0\r\n\r\n".getBytes()); |
781 he.close(); |
748 int n; |
782 } |
749 byte[] buf = new byte[8000]; |
783 } |
750 while ((n=is.read(buf)) != -1) { |
784 |
751 nbytes += n; |
785 static class Configurator extends HttpsConfigurator { |
752 } |
786 public Configurator(SSLContext ctx) { |
753 } catch (IOException e) { |
787 super(ctx); |
754 System.out.println ("read " + nbytes); |
788 } |
755 System.out.println ("size " + size); |
789 |
756 failed = nbytes >= size; |
790 public void configure (HttpsParameters params) { |
757 } finally { |
791 params.setSSLParameters (getSSLContext().getSupportedSSLParameters()); |
758 try { |
|
759 ss.close(); |
|
760 if (s != null) |
|
761 s.close(); |
|
762 } catch (IOException e) {} |
|
763 } |
|
764 } |
|
765 } |
|
766 } |
|
767 |
|
768 class RedirectHandler implements HttpHandler { |
|
769 String root; |
|
770 volatile int count = 0; |
|
771 |
|
772 RedirectHandler(String root) { |
|
773 this.root = root; |
|
774 } |
|
775 |
|
776 @Override |
|
777 public synchronized void handle(HttpExchange t) |
|
778 throws IOException |
|
779 { |
|
780 byte[] buf = new byte[2048]; |
|
781 try (InputStream is = t.getRequestBody()) { |
|
782 while (is.read(buf) != -1) ; |
|
783 } |
|
784 |
|
785 Headers responseHeaders = t.getResponseHeaders(); |
|
786 |
|
787 if (count++ < 1) { |
|
788 responseHeaders.add("Location", root + "/foo/" + count); |
|
789 } else { |
|
790 responseHeaders.add("Location", SmokeTest.midSizedFilename); |
|
791 } |
|
792 t.sendResponseHeaders(301, -1); |
|
793 t.close(); |
|
794 } |
|
795 |
|
796 int count() { |
|
797 return count; |
|
798 } |
|
799 |
|
800 void reset() { |
|
801 count = 0; |
|
802 } |
|
803 } |
|
804 |
|
805 class RedirectErrorHandler implements HttpHandler { |
|
806 String root; |
|
807 volatile int count = 1; |
|
808 |
|
809 RedirectErrorHandler(String root) { |
|
810 this.root = root; |
|
811 } |
|
812 |
|
813 synchronized int count() { |
|
814 return count; |
|
815 } |
|
816 |
|
817 synchronized void increment() { |
|
818 count++; |
|
819 } |
|
820 |
|
821 @Override |
|
822 public synchronized void handle (HttpExchange t) |
|
823 throws IOException |
|
824 { |
|
825 byte[] buf = new byte[2048]; |
|
826 try (InputStream is = t.getRequestBody()) { |
|
827 while (is.read(buf) != -1) ; |
|
828 } |
|
829 |
|
830 Headers map = t.getResponseHeaders(); |
|
831 String redirect = root + "/foo/" + Integer.toString(count); |
|
832 increment(); |
|
833 map.add("Location", redirect); |
|
834 t.sendResponseHeaders(301, -1); |
|
835 t.close(); |
|
836 } |
|
837 } |
|
838 |
|
839 class Util { |
|
840 static byte[] readAll(InputStream is) throws IOException { |
|
841 byte[] buf = new byte[1024]; |
|
842 byte[] result = new byte[0]; |
|
843 |
|
844 while (true) { |
|
845 int n = is.read(buf); |
|
846 if (n > 0) { |
|
847 byte[] b1 = new byte[result.length + n]; |
|
848 System.arraycopy(result, 0, b1, 0, result.length); |
|
849 System.arraycopy(buf, 0, b1, result.length, n); |
|
850 result = b1; |
|
851 } else if (n == -1) { |
|
852 return result; |
|
853 } |
|
854 } |
792 } |
855 } |
793 } |
856 |
794 |
857 static Path getTempFile(int size) throws IOException { |
795 static Path getTempFile(int size) throws IOException { |
858 File f = File.createTempFile("test", "txt"); |
796 File f = File.createTempFile("test", "txt"); |
859 f.deleteOnExit(); |
797 f.deleteOnExit(); |
860 byte[] buf = new byte[2048]; |
798 byte[] buf = new byte[2048]; |
861 for (int i=0; i<buf.length; i++) |
799 for (int i = 0; i < buf.length; i++) |
862 buf[i] = (byte)i; |
800 buf[i] = (byte) i; |
863 |
801 |
864 FileOutputStream fos = new FileOutputStream(f); |
802 FileOutputStream fos = new FileOutputStream(f); |
865 while (size > 0) { |
803 while (size > 0) { |
866 int amount = Math.min(size, buf.length); |
804 int amount = Math.min(size, buf.length); |
867 fos.write(buf, 0, amount); |
805 fos.write(buf, 0, amount); |
868 size -= amount; |
806 size -= amount; |
869 } |
807 } |
870 fos.close(); |
808 fos.close(); |
871 return f.toPath(); |
809 return f.toPath(); |
872 } |
810 } |
873 } |
|
874 |
|
875 class DelayHandler implements HttpHandler { |
|
876 |
|
877 CyclicBarrier bar1 = new CyclicBarrier(2); |
|
878 CyclicBarrier bar2 = new CyclicBarrier(2); |
|
879 CyclicBarrier bar3 = new CyclicBarrier(2); |
|
880 |
|
881 CyclicBarrier barrier1() { |
|
882 return bar1; |
|
883 } |
|
884 |
|
885 CyclicBarrier barrier2() { |
|
886 return bar2; |
|
887 } |
|
888 |
|
889 @Override |
|
890 public synchronized void handle(HttpExchange he) throws IOException { |
|
891 byte[] buf = Util.readAll(he.getRequestBody()); |
|
892 try { |
|
893 bar1.await(); |
|
894 bar2.await(); |
|
895 } catch (Exception e) {} |
|
896 he.sendResponseHeaders(200, -1); // will probably fail |
|
897 he.close(); |
|
898 } |
|
899 |
|
900 } |
811 } |
901 |
812 |
902 // check for simple hardcoded sequence and use remote address |
813 // check for simple hardcoded sequence and use remote address |
903 // to check. |
814 // to check. |
904 // First 4 requests executed in sequence (should use same connection/address) |
815 // First 4 requests executed in sequence (should use same connection/address) |