< prev index next >

src/java.net.http/share/classes/jdk/internal/net/http/websocket/OpeningHandshake.java

Print this page


   1 /*
   2  * Copyright (c) 2015, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 126             if (ILLEGAL_HEADERS.contains(p.first)) {
 127                 throw illegal("Illegal header: " + p.first);
 128             }
 129             requestBuilder.header(p.first, p.second);
 130         }
 131         this.subprotocols = createRequestSubprotocols(b.getSubprotocols());
 132         if (!this.subprotocols.isEmpty()) {
 133             String p = String.join(", ", this.subprotocols);
 134             requestBuilder.header(HEADER_PROTOCOL, p);
 135         }
 136         requestBuilder.header(HEADER_VERSION, VERSION);
 137         this.nonce = createNonce();
 138         requestBuilder.header(HEADER_KEY, this.nonce);
 139         // Setting request version to HTTP/1.1 forcibly, since it's not possible
 140         // to upgrade from HTTP/2 to WebSocket (as of August 2016):
 141         //
 142         //     https://tools.ietf.org/html/draft-hirano-httpbis-websocket-over-http2-00
 143         requestBuilder.version(Version.HTTP_1_1).GET();
 144         request = requestBuilder.buildForWebSocket();
 145         request.isWebSocket(true);
 146         request.setSystemHeader(HEADER_UPGRADE, "websocket");
 147         request.setSystemHeader(HEADER_CONNECTION, "Upgrade");
 148         request.setProxy(proxy);
 149     }
 150 
 151     private static Collection<String> createRequestSubprotocols(
 152             Collection<String> subprotocols)
 153     {
 154         LinkedHashSet<String> sp = new LinkedHashSet<>(subprotocols.size(), 1);
 155         for (String s : subprotocols) {
 156             if (s.trim().isEmpty() || !isValidName(s)) {
 157                 throw illegal("Bad subprotocol syntax: " + s);
 158             }
 159             if (!sp.add(s)) {
 160                 throw illegal("Duplicating subprotocol: " + s);
 161             }
 162         }
 163         return Collections.unmodifiableCollection(sp);
 164     }
 165 
 166     /*
 167      * Checks the given URI for being a WebSocket URI and translates it into a


   1 /*
   2  * Copyright (c) 2015, 2019, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 126             if (ILLEGAL_HEADERS.contains(p.first)) {
 127                 throw illegal("Illegal header: " + p.first);
 128             }
 129             requestBuilder.header(p.first, p.second);
 130         }
 131         this.subprotocols = createRequestSubprotocols(b.getSubprotocols());
 132         if (!this.subprotocols.isEmpty()) {
 133             String p = String.join(", ", this.subprotocols);
 134             requestBuilder.header(HEADER_PROTOCOL, p);
 135         }
 136         requestBuilder.header(HEADER_VERSION, VERSION);
 137         this.nonce = createNonce();
 138         requestBuilder.header(HEADER_KEY, this.nonce);
 139         // Setting request version to HTTP/1.1 forcibly, since it's not possible
 140         // to upgrade from HTTP/2 to WebSocket (as of August 2016):
 141         //
 142         //     https://tools.ietf.org/html/draft-hirano-httpbis-websocket-over-http2-00
 143         requestBuilder.version(Version.HTTP_1_1).GET();
 144         request = requestBuilder.buildForWebSocket();
 145         request.isWebSocket(true);
 146         Utils.setWebSocketUpgradeHeaders(request);

 147         request.setProxy(proxy);
 148     }
 149 
 150     private static Collection<String> createRequestSubprotocols(
 151             Collection<String> subprotocols)
 152     {
 153         LinkedHashSet<String> sp = new LinkedHashSet<>(subprotocols.size(), 1);
 154         for (String s : subprotocols) {
 155             if (s.trim().isEmpty() || !isValidName(s)) {
 156                 throw illegal("Bad subprotocol syntax: " + s);
 157             }
 158             if (!sp.add(s)) {
 159                 throw illegal("Duplicating subprotocol: " + s);
 160             }
 161         }
 162         return Collections.unmodifiableCollection(sp);
 163     }
 164 
 165     /*
 166      * Checks the given URI for being a WebSocket URI and translates it into a


< prev index next >