PageRenderTime 37ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshakerFactory.java

https://gitlab.com/taichu/netty
Java | 132 lines | 44 code | 9 blank | 79 comment | 8 complexity | afca31951f0776e41a19a7b695344f73 MD5 | raw file
  1. /*
  2. * Copyright 2012 The Netty Project
  3. *
  4. * The Netty Project licenses this file to you under the Apache License,
  5. * version 2.0 (the "License"); you may not use this file except in compliance
  6. * with the License. You may obtain a copy of the License at:
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations
  14. * under the License.
  15. */
  16. package io.netty.handler.codec.http.websocketx;
  17. import io.netty.handler.codec.http.HttpHeaders;
  18. import java.net.URI;
  19. import static io.netty.handler.codec.http.websocketx.WebSocketVersion.*;
  20. /**
  21. * Creates a new {@link WebSocketClientHandshaker} of desired protocol version.
  22. */
  23. public final class WebSocketClientHandshakerFactory {
  24. /**
  25. * Private constructor so this static class cannot be instanced.
  26. */
  27. private WebSocketClientHandshakerFactory() {
  28. }
  29. /**
  30. * Creates a new handshaker.
  31. *
  32. * @param webSocketURL
  33. * URL for web socket communications. e.g "ws://myhost.com/mypath".
  34. * Subsequent web socket frames will be sent to this URL.
  35. * @param version
  36. * Version of web socket specification to use to connect to the server
  37. * @param subprotocol
  38. * Sub protocol request sent to the server. Null if no sub-protocol support is required.
  39. * @param allowExtensions
  40. * Allow extensions to be used in the reserved bits of the web socket frame
  41. * @param customHeaders
  42. * Custom HTTP headers to send during the handshake
  43. */
  44. public static WebSocketClientHandshaker newHandshaker(
  45. URI webSocketURL, WebSocketVersion version, String subprotocol,
  46. boolean allowExtensions, HttpHeaders customHeaders) {
  47. return newHandshaker(webSocketURL, version, subprotocol, allowExtensions, customHeaders, 65536);
  48. }
  49. /**
  50. * Creates a new handshaker.
  51. *
  52. * @param webSocketURL
  53. * URL for web socket communications. e.g "ws://myhost.com/mypath".
  54. * Subsequent web socket frames will be sent to this URL.
  55. * @param version
  56. * Version of web socket specification to use to connect to the server
  57. * @param subprotocol
  58. * Sub protocol request sent to the server. Null if no sub-protocol support is required.
  59. * @param allowExtensions
  60. * Allow extensions to be used in the reserved bits of the web socket frame
  61. * @param customHeaders
  62. * Custom HTTP headers to send during the handshake
  63. * @param maxFramePayloadLength
  64. * Maximum allowable frame payload length. Setting this value to your application's
  65. * requirement may reduce denial of service attacks using long data frames.
  66. */
  67. public static WebSocketClientHandshaker newHandshaker(
  68. URI webSocketURL, WebSocketVersion version, String subprotocol,
  69. boolean allowExtensions, HttpHeaders customHeaders, int maxFramePayloadLength) {
  70. return newHandshaker(webSocketURL, version, subprotocol, allowExtensions, customHeaders,
  71. maxFramePayloadLength, true, false);
  72. }
  73. /**
  74. * Creates a new handshaker.
  75. *
  76. * @param webSocketURL
  77. * URL for web socket communications. e.g "ws://myhost.com/mypath".
  78. * Subsequent web socket frames will be sent to this URL.
  79. * @param version
  80. * Version of web socket specification to use to connect to the server
  81. * @param subprotocol
  82. * Sub protocol request sent to the server. Null if no sub-protocol support is required.
  83. * @param allowExtensions
  84. * Allow extensions to be used in the reserved bits of the web socket frame
  85. * @param customHeaders
  86. * Custom HTTP headers to send during the handshake
  87. * @param maxFramePayloadLength
  88. * Maximum allowable frame payload length. Setting this value to your application's
  89. * requirement may reduce denial of service attacks using long data frames.
  90. * @param performMasking
  91. * Whether to mask all written websocket frames. This must be set to true in order to be fully compatible
  92. * with the websocket specifications. Client applications that communicate with a non-standard server
  93. * which doesn't require masking might set this to false to achieve a higher performance.
  94. * @param allowMaskMismatch
  95. * Allows to loosen the masking requirement on received frames. When this is set to false then also
  96. * frames which are not masked properly according to the standard will still be accepted.
  97. */
  98. public static WebSocketClientHandshaker newHandshaker(
  99. URI webSocketURL, WebSocketVersion version, String subprotocol,
  100. boolean allowExtensions, HttpHeaders customHeaders, int maxFramePayloadLength,
  101. boolean performMasking, boolean allowMaskMismatch) {
  102. if (version == V13) {
  103. return new WebSocketClientHandshaker13(
  104. webSocketURL, V13, subprotocol, allowExtensions, customHeaders,
  105. maxFramePayloadLength, performMasking, allowMaskMismatch);
  106. }
  107. if (version == V08) {
  108. return new WebSocketClientHandshaker08(
  109. webSocketURL, V08, subprotocol, allowExtensions, customHeaders,
  110. maxFramePayloadLength, performMasking, allowMaskMismatch);
  111. }
  112. if (version == V07) {
  113. return new WebSocketClientHandshaker07(
  114. webSocketURL, V07, subprotocol, allowExtensions, customHeaders,
  115. maxFramePayloadLength, performMasking, allowMaskMismatch);
  116. }
  117. if (version == V00) {
  118. return new WebSocketClientHandshaker00(
  119. webSocketURL, V00, subprotocol, customHeaders, maxFramePayloadLength);
  120. }
  121. throw new WebSocketHandshakeException("Protocol version " + version + " not supported.");
  122. }
  123. }