test/jdk/java/net/httpclient/http2/BadHeadersTest.java
branchhttp-client-branch
changeset 56104 3420c1bdd254
child 56167 96fa4f49a9ff
equal deleted inserted replaced
56103:d5f70938e399 56104:3420c1bdd254
       
     1 /*
       
     2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    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 
       
    24 /*
       
    25  * @test
       
    26  * @modules java.base/sun.net.www.http
       
    27  *          java.net.http/jdk.internal.net.http.common
       
    28  *          java.net.http/jdk.internal.net.http.frame
       
    29  *          java.net.http/jdk.internal.net.http.hpack
       
    30  * @library /lib/testlibrary server
       
    31  * @build Http2TestServer
       
    32  * @build jdk.testlibrary.SimpleSSLContext
       
    33  * @run testng/othervm BadHeadersTest
       
    34  */
       
    35 
       
    36 import jdk.internal.net.http.common.HttpHeadersImpl;
       
    37 import jdk.internal.net.http.common.Pair;
       
    38 import jdk.internal.net.http.frame.ContinuationFrame;
       
    39 import jdk.internal.net.http.frame.HeaderFrame;
       
    40 import jdk.internal.net.http.frame.HeadersFrame;
       
    41 import jdk.internal.net.http.frame.Http2Frame;
       
    42 import jdk.testlibrary.SimpleSSLContext;
       
    43 import org.testng.annotations.AfterTest;
       
    44 import org.testng.annotations.BeforeTest;
       
    45 import org.testng.annotations.DataProvider;
       
    46 import org.testng.annotations.Test;
       
    47 
       
    48 import javax.net.ssl.SSLContext;
       
    49 import javax.net.ssl.SSLSession;
       
    50 import java.io.IOException;
       
    51 import java.io.InputStream;
       
    52 import java.io.OutputStream;
       
    53 import java.net.URI;
       
    54 import java.net.http.HttpClient;
       
    55 import java.net.http.HttpRequest;
       
    56 import java.nio.ByteBuffer;
       
    57 import java.util.ArrayList;
       
    58 import java.util.List;
       
    59 import java.util.concurrent.CompletionException;
       
    60 import java.util.concurrent.atomic.AtomicInteger;
       
    61 import java.util.function.BiFunction;
       
    62 
       
    63 import static java.net.http.HttpRequest.BodyPublisher.fromString;
       
    64 import static java.net.http.HttpResponse.BodyHandler.asString;
       
    65 import static jdk.internal.net.http.common.Pair.pair;
       
    66 import static org.testng.Assert.assertThrows;
       
    67 
       
    68 // Code copied from ContinuationFrameTest
       
    69 public class BadHeadersTest {
       
    70 
       
    71     private static final List<Pair<String, String>> BAD_HEADERS = List.of(
       
    72             pair(":hello", "GET"),                    // Unknown pseudo-header
       
    73             pair("hell o", "value"),                  // Space in the name
       
    74             pair("hello", "line1\r\n  line2\r\n"),    // Multiline value
       
    75             pair("hello", "DE" + ((char) 0x7F) + "L") // Bad byte in value
       
    76     );
       
    77 
       
    78     SSLContext sslContext;
       
    79     Http2TestServer http2TestServer;   // HTTP/2 ( h2c )
       
    80     Http2TestServer https2TestServer;  // HTTP/2 ( h2  )
       
    81     String http2URI;
       
    82     String https2URI;
       
    83 
       
    84     /**
       
    85      * A function that returns a list of 1) a HEADERS frame ( with an empty
       
    86      * payload ), and 2) a CONTINUATION frame with the actual headers.
       
    87      */
       
    88     static BiFunction<Integer,List<ByteBuffer>,List<Http2Frame>> oneContinuation =
       
    89             (Integer streamid, List<ByteBuffer> encodedHeaders) -> {
       
    90                 List<ByteBuffer> empty =  List.of(ByteBuffer.wrap(new byte[0]));
       
    91                 HeadersFrame hf = new HeadersFrame(streamid, 0, empty);
       
    92                 ContinuationFrame cf = new ContinuationFrame(streamid,
       
    93                                                              HeaderFrame.END_HEADERS,
       
    94                                                              encodedHeaders);
       
    95                 return List.of(hf, cf);
       
    96             };
       
    97 
       
    98     /**
       
    99      * A function that returns a list of a HEADERS frame followed by a number of
       
   100      * CONTINUATION frames. Each frame contains just a single byte of payload.
       
   101      */
       
   102     static BiFunction<Integer,List<ByteBuffer>,List<Http2Frame>> byteAtATime =
       
   103             (Integer streamid, List<ByteBuffer> encodedHeaders) -> {
       
   104                 assert encodedHeaders.get(0).hasRemaining();
       
   105                 List<Http2Frame> frames = new ArrayList<>();
       
   106                 ByteBuffer hb = ByteBuffer.wrap(new byte[] {encodedHeaders.get(0).get()});
       
   107                 HeadersFrame hf = new HeadersFrame(streamid, 0, hb);
       
   108                 frames.add(hf);
       
   109                 for (ByteBuffer bb : encodedHeaders) {
       
   110                     while (bb.hasRemaining()) {
       
   111                         List<ByteBuffer> data = List.of(ByteBuffer.wrap(new byte[] {bb.get()}));
       
   112                         ContinuationFrame cf = new ContinuationFrame(streamid, 0, data);
       
   113                         frames.add(cf);
       
   114                     }
       
   115                 }
       
   116                 frames.get(frames.size() - 1).setFlag(HeaderFrame.END_HEADERS);
       
   117                 return frames;
       
   118             };
       
   119 
       
   120     @DataProvider(name = "variants")
       
   121     public Object[][] variants() {
       
   122         return new Object[][] {
       
   123                 { http2URI,  false, oneContinuation },
       
   124                 { https2URI, false, oneContinuation },
       
   125                 { http2URI,  true,  oneContinuation },
       
   126                 { https2URI, true,  oneContinuation },
       
   127 
       
   128                 { http2URI,  false, byteAtATime },
       
   129                 { https2URI, false, byteAtATime },
       
   130                 { http2URI,  true,  byteAtATime },
       
   131                 { https2URI, true,  byteAtATime },
       
   132         };
       
   133     }
       
   134 
       
   135 
       
   136     @Test(dataProvider = "variants")
       
   137     void test(String uri,
       
   138               boolean sameClient,
       
   139               BiFunction<Integer,List<ByteBuffer>,List<Http2Frame>> headerFramesSupplier)
       
   140             throws Exception
       
   141     {
       
   142         CFTHttp2TestExchange.setHeaderFrameSupplier(headerFramesSupplier);
       
   143 
       
   144         HttpClient client = null;
       
   145         for (int i=0; i< BAD_HEADERS.size(); i++) {
       
   146             if (!sameClient || client == null)
       
   147                 client = HttpClient.newBuilder().sslContext(sslContext).build();
       
   148 
       
   149             HttpRequest request = HttpRequest.newBuilder(URI.create(uri))
       
   150                     .POST(fromString("Hello there!"))
       
   151                     .build();
       
   152             final HttpClient cc = client;
       
   153             if (i % 2 == 0) {
       
   154                 assertThrows(IOException.class, () -> cc.send(request, asString()));
       
   155             } else {
       
   156                 Throwable t = null;
       
   157                 try {
       
   158                     cc.sendAsync(request, asString()).join();
       
   159                 } catch (Throwable t0) {
       
   160                     t = t0;
       
   161                 }
       
   162                 if (t == null) {
       
   163                     throw new AssertionError("An exception was expected");
       
   164                 }
       
   165                 if (t instanceof CompletionException) {
       
   166                     Throwable c = t.getCause();
       
   167                     if (!(c instanceof IOException)) {
       
   168                         throw new AssertionError("Unexpected exception", c);
       
   169                     }
       
   170                 } else if (!(t instanceof IOException)) {
       
   171                     throw new AssertionError("Unexpected exception", t);
       
   172                 }
       
   173             }
       
   174         }
       
   175     }
       
   176 
       
   177     @BeforeTest
       
   178     public void setup() throws Exception {
       
   179         sslContext = new SimpleSSLContext().get();
       
   180         if (sslContext == null)
       
   181             throw new AssertionError("Unexpected null sslContext");
       
   182 
       
   183         http2TestServer = new Http2TestServer("127.0.0.1", false, 0);
       
   184         http2TestServer.addHandler(new Http2EchoHandler(), "/http2/echo");
       
   185         int port = http2TestServer.getAddress().getPort();
       
   186         http2URI = "http://127.0.0.1:" + port + "/http2/echo";
       
   187 
       
   188         https2TestServer = new Http2TestServer("127.0.0.1", true, 0);
       
   189         https2TestServer.addHandler(new Http2EchoHandler(), "/https2/echo");
       
   190         port = https2TestServer.getAddress().getPort();
       
   191         https2URI = "https://127.0.0.1:" + port + "/https2/echo";
       
   192 
       
   193         // Override the default exchange supplier with a custom one to enable
       
   194         // particular test scenarios
       
   195         http2TestServer.setExchangeSupplier(CFTHttp2TestExchange::new);
       
   196         https2TestServer.setExchangeSupplier(CFTHttp2TestExchange::new);
       
   197 
       
   198         http2TestServer.start();
       
   199         https2TestServer.start();
       
   200     }
       
   201 
       
   202     @AfterTest
       
   203     public void teardown() throws Exception {
       
   204         http2TestServer.stop();
       
   205         https2TestServer.stop();
       
   206     }
       
   207 
       
   208     static class Http2EchoHandler implements Http2Handler {
       
   209 
       
   210         private final AtomicInteger requestNo = new AtomicInteger();
       
   211 
       
   212         @Override
       
   213         public void handle(Http2TestExchange t) throws IOException {
       
   214             try (InputStream is = t.getRequestBody();
       
   215                  OutputStream os = t.getResponseBody()) {
       
   216                 byte[] bytes = is.readAllBytes();
       
   217                 int i = requestNo.incrementAndGet();
       
   218                 Pair<String, String> p = BAD_HEADERS.get(i % BAD_HEADERS.size());
       
   219                 t.getResponseHeaders().addHeader(p.first, p.second);
       
   220                 t.sendResponseHeaders(200, bytes.length);
       
   221                 os.write(bytes);
       
   222             }
       
   223         }
       
   224     }
       
   225 
       
   226     // A custom Http2TestExchangeImpl that overrides sendResponseHeaders to
       
   227     // allow headers to be sent with a number of CONTINUATION frames.
       
   228     static class CFTHttp2TestExchange extends Http2TestExchangeImpl {
       
   229         static volatile BiFunction<Integer,List<ByteBuffer>,List<Http2Frame>> headerFrameSupplier;
       
   230 
       
   231         static void setHeaderFrameSupplier(BiFunction<Integer,List<ByteBuffer>,List<Http2Frame>> hfs) {
       
   232             headerFrameSupplier = hfs;
       
   233         }
       
   234 
       
   235         CFTHttp2TestExchange(int streamid, String method, HttpHeadersImpl reqheaders,
       
   236                              HttpHeadersImpl rspheaders, URI uri, InputStream is,
       
   237                              SSLSession sslSession, BodyOutputStream os,
       
   238                              Http2TestServerConnection conn, boolean pushAllowed) {
       
   239             super(streamid, method, reqheaders, rspheaders, uri, is, sslSession,
       
   240                   os, conn, pushAllowed);
       
   241 
       
   242         }
       
   243 
       
   244         @Override
       
   245         public void sendResponseHeaders(int rCode, long responseLength) throws IOException {
       
   246             this.responseLength = responseLength;
       
   247             if (responseLength > 0 || responseLength < 0) {
       
   248                 long clen = responseLength > 0 ? responseLength : 0;
       
   249                 rspheaders.setHeader("Content-length", Long.toString(clen));
       
   250             }
       
   251             rspheaders.setHeader(":status", Integer.toString(rCode));
       
   252 
       
   253             List<ByteBuffer> encodeHeaders = conn.encodeHeaders(rspheaders);
       
   254             List<Http2Frame> headerFrames = headerFrameSupplier.apply(streamid, encodeHeaders);
       
   255             assert headerFrames.size() > 0;  // there must always be at least 1
       
   256 
       
   257             if (responseLength < 0) {
       
   258                 headerFrames.get(headerFrames.size() -1).setFlag(HeadersFrame.END_STREAM);
       
   259                 os.closeInternal();
       
   260             }
       
   261 
       
   262             for (Http2Frame f : headerFrames)
       
   263                 conn.outputQ.put(f);
       
   264 
       
   265             os.goodToGo();
       
   266             System.err.println("Sent response headers " + rCode);
       
   267         }
       
   268     }
       
   269 }