io.undertow.websockets.spi.WebSocketHttpExchange类的使用及代码示例

x33g5p2x  于2022-02-03 转载在 其他  
字(9.3k)|赞(0)|评价(0)|浏览(130)

本文整理了Java中io.undertow.websockets.spi.WebSocketHttpExchange类的一些代码示例,展示了WebSocketHttpExchange类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。WebSocketHttpExchange类的具体详情如下:
包路径:io.undertow.websockets.spi.WebSocketHttpExchange
类名称:WebSocketHttpExchange

WebSocketHttpExchange介绍

[英]An abstraction for a Http exchange. Undertow uses 3 different types of exchanges:

  • async - blocking - servlet

This class provides a way to operate on the underling exchange while providing the correct semantics regardless of the underlying exchange type.

The main use case for this is web sockets. Web sockets should be able to perform a handshake regardless of the nature of the underlying request, while still respecting servlet filters, security etc.
[中]Http交换的抽象。Undertow使用3种不同类型的交换:
-异步阻塞servlet
此类提供了一种在底层交换上操作的方法,同时提供了正确的语义,而不管底层交换类型是什么。
这方面的主要用例是web套接字。Web套接字应该能够执行握手,而不管底层请求的性质如何,同时仍然尊重servlet过滤器、安全性等。

代码示例

代码示例来源:origin: wildfly/wildfly

protected void handshakeInternal(final WebSocketHttpExchange exchange) {
  String origin = exchange.getRequestHeader(Headers.SEC_WEB_SOCKET_ORIGIN_STRING);
  if (origin != null) {
    exchange.setResponseHeader(Headers.SEC_WEB_SOCKET_ORIGIN_STRING, origin);
  }
  selectSubprotocol(exchange);
  selectExtensions(exchange);
  exchange.setResponseHeader(Headers.SEC_WEB_SOCKET_LOCATION_STRING, getWebSocketLocation(exchange));
  final String key = exchange.getRequestHeader(Headers.SEC_WEB_SOCKET_KEY_STRING);
  try {
    final String solution = solve(key);
    exchange.setResponseHeader(Headers.SEC_WEB_SOCKET_ACCEPT_STRING, solution);
    performUpgrade(exchange);
  } catch (NoSuchAlgorithmException e) {
    IoUtils.safeClose(exchange);
    exchange.endExchange();
    return;
  }
}

代码示例来源:origin: wildfly/wildfly

/**
 * Return the full url of the websocket location of the given {@link WebSocketHttpExchange}
 */
protected static String getWebSocketLocation(WebSocketHttpExchange exchange) {
  String scheme;
  if ("https".equals(exchange.getRequestScheme())) {
    scheme = "wss";
  } else {
    scheme = "ws";
  }
  return scheme + "://" + exchange.getRequestHeader(Headers.HOST_STRING) + exchange.getRequestURI();
}

代码示例来源:origin: wildfly/wildfly

/**
   * Create the {@code ExtensionFunction} list associated with the negotiated extensions defined in the exchange's response.
   *
   * @param exchange the exchange used to retrieve negotiated extensions
   * @return         a list of {@code ExtensionFunction} with the implementation of the extensions
   */
  protected final List<ExtensionFunction> initExtensions(final WebSocketHttpExchange exchange) {
    String extHeader = exchange.getResponseHeaders().get(Headers.SEC_WEB_SOCKET_EXTENSIONS_STRING) != null ?
        exchange.getResponseHeaders().get(Headers.SEC_WEB_SOCKET_EXTENSIONS_STRING).get(0) : null;

    List<ExtensionFunction> negotiated = new ArrayList<>();
    if (extHeader != null) {
      List<WebSocketExtension> extensions = WebSocketExtension.parse(extHeader);
      if (extensions != null && !extensions.isEmpty()) {
        for (WebSocketExtension ext : extensions) {
          for (ExtensionHandshake extHandshake : availableExtensions) {
            if (extHandshake.getName().equals(ext.getName())) {
              negotiated.add(extHandshake.create());
            }
          }
        }
      }
    }
    return negotiated;
  }
}

代码示例来源:origin: wildfly/wildfly

/**
 * Selects the first matching supported sub protocol and add it the the headers of the exchange.
 *
 */
protected final void selectSubprotocol(final WebSocketHttpExchange exchange) {
  String requestedSubprotocols = exchange.getRequestHeader(Headers.SEC_WEB_SOCKET_PROTOCOL_STRING);
  if (requestedSubprotocols == null) {
    return;
  }
  String[] requestedSubprotocolArray = PATTERN.split(requestedSubprotocols);
  String subProtocol = supportedSubprotols(requestedSubprotocolArray);
  if (subProtocol != null && !subProtocol.isEmpty()) {
    exchange.setResponseHeader(Headers.SEC_WEB_SOCKET_PROTOCOL_STRING, subProtocol);
  }
}

代码示例来源:origin: wildfly/wildfly

@Override
  public WebSocketChannel createChannel(WebSocketHttpExchange exchange, final StreamConnection channel, final ByteBufferPool pool) {
    List<ExtensionFunction> extensionFunctions = initExtensions(exchange);
    return new WebSocket13Channel(channel, pool, getWebSocketLocation(exchange), exchange.getResponseHeader(Headers.SEC_WEB_SOCKET_PROTOCOL_STRING), false, !extensionFunctions.isEmpty(), CompositeExtensionFunction.compose(extensionFunctions), exchange.getPeerConnections(), exchange.getOptions());
  }
}

代码示例来源:origin: io.skullabs.kikaha/kikaha-core

public WebSocketSession(final WebSocketHttpExchange originalExchange, final WebSocketChannel channel, final URLMatcher urlMatcher, Serializer serializer, Unserializer unserializer, ExecutorService executorService) {
  this.originalExchange = originalExchange;
  this.urlMatcher = urlMatcher;
  this.channel = channel;
  this.requestHeaders = originalExchange.getRequestHeaders();
  this.responseHeaders = originalExchange.getResponseHeaders();
  this.userPrincipal = originalExchange.getUserPrincipal();
  this.requestURI = channel.getUrl();
  this.peerConnections = retrievePeerConnectionsForCurrentURLRequest( channel );
  this.requestParameters = extractRequestParameters( channel );
  this.serializer = serializer;
  this.unserializer = unserializer;
  this.executorService = executorService;
}

代码示例来源:origin: syncany/syncany

String originHeader = exchange.getRequestHeader("Origin");
  exchange.close();

代码示例来源:origin: wildfly/wildfly

@Override
public boolean matches(final WebSocketHttpExchange exchange) {
  if (exchange.getRequestHeader(Headers.SEC_WEB_SOCKET_KEY_STRING) != null &&
      exchange.getRequestHeader(Headers.SEC_WEB_SOCKET_VERSION_STRING) != null) {
    return exchange.getRequestHeader(Headers.SEC_WEB_SOCKET_VERSION_STRING)
        .equals(getVersion().toHttpHeaderValue());
  }
  return false;
}

代码示例来源:origin: org.minijax/minijax-undertow-websocket

public MinijaxUndertowWebSocketRequestContext(
    final MinijaxApplication container,
    final WebSocketHttpExchange exchange) {
  super(container);
  this.exchange = exchange;
  uriInfo = new MinijaxUriInfo(URI.create(exchange.getRequestURI() + "?" + exchange.getQueryString()));
}

代码示例来源:origin: wildfly/wildfly

/**
 * Issue the WebSocket upgrade
 *
 * @param exchange The {@link WebSocketHttpExchange} for which the handshake and upgrade should occur.
 */
public final void handshake(final WebSocketHttpExchange exchange) {
  exchange.putAttachment(WebSocketVersion.ATTACHMENT_KEY, version);
  handshakeInternal(exchange);
}

代码示例来源:origin: wildfly/wildfly

protected void upgradeChannel(final WebSocketHttpExchange exchange, final byte[] data) {
  if (data.length > 0) {
    writePayload(exchange, ByteBuffer.wrap(data));
  } else {
    exchange.endExchange();
  }
}

代码示例来源:origin: pippo-java/pippo

private Handshake getHandshake(WebSocketHttpExchange exchange) {
  for (Handshake handshake : handshakes) {
    if (handshake.matches(exchange)) {
      return handshake;
    }
  }
  throw new PippoRuntimeException("No matching Undertow Handshake found: {}", exchange.getRequestHeaders());
}

代码示例来源:origin: wildfly/wildfly

@Override
  public WebSocketChannel createChannel(WebSocketHttpExchange exchange, final StreamConnection channel, final ByteBufferPool pool) {
    List<ExtensionFunction> extensionFunctions = initExtensions(exchange);
    return new WebSocket07Channel(channel, pool, getWebSocketLocation(exchange), exchange.getResponseHeader(Headers.SEC_WEB_SOCKET_PROTOCOL_STRING), false, !extensionFunctions.isEmpty(), CompositeExtensionFunction.compose(extensionFunctions), exchange.getPeerConnections(), exchange.getOptions());
  }
}

代码示例来源:origin: wildfly/wildfly

protected final void selectExtensions(final WebSocketHttpExchange exchange) {
  List<WebSocketExtension> requestedExtensions = WebSocketExtension.parse(exchange.getRequestHeader(Headers.SEC_WEB_SOCKET_EXTENSIONS_STRING));
  List<WebSocketExtension> extensions = selectedExtension(requestedExtensions);
  if (extensions != null && !extensions.isEmpty()) {
    exchange.setResponseHeader(Headers.SEC_WEB_SOCKET_EXTENSIONS_STRING, WebSocketExtension.toExtensionHeader(extensions));
  }
}

代码示例来源:origin: networknt/light

String originHeader = exchange.getRequestHeader("Origin");
boolean allowedOriginHeader = (originHeader == null ||
    LightServer.WEBSOCKET_ALLOWED_ORIGIN_HEADER.matcher(originHeader).matches());
  exchange.close();

代码示例来源:origin: io.undertow/undertow-core

@Override
public boolean matches(final WebSocketHttpExchange exchange) {
  if (exchange.getRequestHeader(Headers.SEC_WEB_SOCKET_KEY_STRING) != null &&
      exchange.getRequestHeader(Headers.SEC_WEB_SOCKET_VERSION_STRING) != null) {
    return exchange.getRequestHeader(Headers.SEC_WEB_SOCKET_VERSION_STRING)
        .equals(getVersion().toHttpHeaderValue());
  }
  return false;
}

代码示例来源:origin: io.undertow/undertow-core

/**
 * Issue the WebSocket upgrade
 *
 * @param exchange The {@link WebSocketHttpExchange} for which the handshake and upgrade should occur.
 */
public final void handshake(final WebSocketHttpExchange exchange) {
  exchange.putAttachment(WebSocketVersion.ATTACHMENT_KEY, version);
  handshakeInternal(exchange);
}

代码示例来源:origin: io.undertow/undertow-core

protected void upgradeChannel(final WebSocketHttpExchange exchange, final byte[] data) {
  if (data.length > 0) {
    writePayload(exchange, ByteBuffer.wrap(data));
  } else {
    exchange.endExchange();
  }
}

代码示例来源:origin: org.minijax/minijax-undertow-websocket

public MinijaxUndertowWebSocketHttpHeaders(final WebSocketHttpExchange exchange) {
  headerMap = exchange.getRequestHeaders();
}

代码示例来源:origin: wildfly/wildfly

@Override
  public WebSocketChannel createChannel(final WebSocketHttpExchange exchange, final StreamConnection channel, final ByteBufferPool pool) {
    List<ExtensionFunction> extensionFunctions = initExtensions(exchange);
    return new WebSocket08Channel(channel, pool, getWebSocketLocation(exchange), exchange.getResponseHeader(Headers.SEC_WEB_SOCKET_PROTOCOL_STRING), false, !extensionFunctions.isEmpty(), CompositeExtensionFunction.compose(extensionFunctions), exchange.getPeerConnections(), exchange.getOptions());

  }
}

相关文章