io.netty.handler.codec.http.HttpRequest.headers()方法的使用及代码示例

x33g5p2x  于2022-01-20 转载在 其他  
字(10.1k)|赞(0)|评价(0)|浏览(185)

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

HttpRequest.headers介绍

暂无

代码示例

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

/**
 * Determines whether or not the message is an HTTP upgrade request.
 */
private static boolean isUpgradeRequest(HttpObject msg) {
  return msg instanceof HttpRequest && ((HttpRequest) msg).headers().get(HttpHeaderNames.UPGRADE) != null;
}

代码示例来源:origin: jooby-project/jooby

@SuppressWarnings("unchecked")
@Override
public <T> T upgrade(final Class<T> type) {
 if (type == NativeWebSocket.class) {
  String protocol = ifSecure("wss", "ws");
  String webSocketURL = protocol + "://" + req.headers().get(HttpHeaderNames.HOST) + path;
  WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(
    webSocketURL, null, true, wsMaxMessageSize);
  WebSocketServerHandshaker handshaker = wsFactory.newHandshaker(req);
  NettyWebSocket result = new NettyWebSocket(ctx, handshaker, (ws) -> {
   handshaker.handshake(ctx.channel(), (FullHttpRequest) req)
     .addListener(FIRE_EXCEPTION_ON_FAILURE)
     .addListener(payload -> ws.connect())
     .addListener(FIRE_EXCEPTION_ON_FAILURE);
  });
  ctx.channel().attr(NettyWebSocket.KEY).set(result);
  return (T) result;
 } else if (type == Sse.class) {
  NettySse sse = new NettySse(ctx, responseHeaders);
  return (T) sse;
 } else if (type == NativePushPromise.class) {
  return (T) new NettyPush(ctx,
    req.headers().getInt(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text()),
    header("host").orElse(ip()), ifSecure("https", "http"));
 }
 throw new UnsupportedOperationException("Not Supported: " + type);
}

代码示例来源:origin: AsyncHttpClient/async-http-client

HttpHeaders headers = nettyRequest.getHttpRequest().headers();
 String userDefinedAcceptEncoding = headers.get(ACCEPT_ENCODING);
 if (userDefinedAcceptEncoding != null) {

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

private static String getWebSocketLocation(ChannelPipeline cp, HttpRequest req, String path) {
    String protocol = "ws";
    if (cp.get(SslHandler.class) != null) {
      // SSL in use so use Secure WebSockets
      protocol = "wss";
    }
    String host = req.headers().get(HttpHeaderNames.HOST);
    return protocol + "://" + host + path;
  }
}

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

@SuppressWarnings("deprecation")
private SpdySynStreamFrame createSynStreamFrame(HttpRequest httpRequest) throws Exception {
  final HttpHeaders httpHeaders = httpRequest.headers();
  int streamId = httpHeaders.getInt(SpdyHttpHeaders.Names.STREAM_ID);
  int associatedToStreamId = httpHeaders.getInt(SpdyHttpHeaders.Names.ASSOCIATED_TO_STREAM_ID, 0);
  byte priority = (byte) httpHeaders.getInt(SpdyHttpHeaders.Names.PRIORITY, 0);
  CharSequence scheme = httpHeaders.get(SpdyHttpHeaders.Names.SCHEME);
  httpHeaders.remove(SpdyHttpHeaders.Names.STREAM_ID);
  httpHeaders.remove(SpdyHttpHeaders.Names.ASSOCIATED_TO_STREAM_ID);
  CharSequence host = httpHeaders.get(HttpHeaderNames.HOST);
  httpHeaders.remove(HttpHeaderNames.HOST);
  frameHeaders.set(SpdyHeaders.HttpNames.HOST, host);

代码示例来源:origin: openzipkin/brave

@Override public String requestHeader(HttpRequest request, String name) {
 return request.headers().get(name);
}

代码示例来源:origin: jooby-project/jooby

@Override
public Optional<String> header(final String name) {
 String value = req.headers().get(name);
 return Optional.ofNullable(value);
}

代码示例来源:origin: eclipse-vertx/vert.x

static String getWebSocketLocation(HttpRequest req, boolean ssl) throws Exception {
 String prefix;
 if (ssl) {
  prefix = "ws://";
 } else {
  prefix = "wss://";
 }
 URI uri = new URI(req.uri());
 String path = uri.getRawPath();
 String loc = prefix + req.headers().get(HttpHeaderNames.HOST) + path;
 String query = uri.getRawQuery();
 if (query != null) {
  loc += "?" + query;
 }
 return loc;
}

代码示例来源:origin: mrniko/netty-socketio

private String getWebSocketLocation(HttpRequest req) {
  String protocol = "ws://";
  if (isSsl) {
    protocol = "wss://";
  }
  return protocol + req.headers().get(HttpHeaderNames.HOST) + req.uri();
}

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

/**
 * Check if the given request is a multipart request
 * @return True if the request is a Multipart request
 */
public static boolean isMultipart(HttpRequest request) {
  if (request.headers().contains(HttpHeaderNames.CONTENT_TYPE)) {
    return getMultipartDataBoundary(request.headers().get(HttpHeaderNames.CONTENT_TYPE)) != null;
  } else {
    return false;
  }
}

代码示例来源:origin: eclipse-vertx/vert.x

private void prepareRequestHeaders(HttpRequest request, String hostHeader, boolean chunked) {
 HttpHeaders headers = request.headers();
 headers.remove(TRANSFER_ENCODING);
 if (!headers.contains(HOST)) {
  request.headers().set(HOST, hostHeader);
 }
 if (chunked) {
  HttpUtil.setTransferEncodingChunked(request, true);
 }
 if (conn.options.isTryUseCompression() && request.headers().get(ACCEPT_ENCODING) == null) {
  // if compression should be used but nothing is specified by the user support deflate and gzip.
  request.headers().set(ACCEPT_ENCODING, DEFLATE_GZIP);
 }
 if (!conn.options.isKeepAlive() && conn.options.getProtocolVersion() == io.vertx.core.http.HttpVersion.HTTP_1_1) {
  request.headers().set(CONNECTION, CLOSE);
 } else if (conn.options.isKeepAlive() && conn.options.getProtocolVersion() == io.vertx.core.http.HttpVersion.HTTP_1_0) {
  request.headers().set(CONNECTION, KEEP_ALIVE);
 }
}

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

private void echoRequestOrigin(final HttpResponse response) {
  setOrigin(response, request.headers().get(HttpHeaderNames.ORIGIN));
}

代码示例来源:origin: jooby-project/jooby

@Override
public List<org.jooby.Cookie> cookies() {
 if (this.cookies == null) {
  String cookieString = req.headers().get(HttpHeaderNames.COOKIE);
  if (cookieString != null) {
   this.cookies = ServerCookieDecoder.STRICT.decode(cookieString).stream()
     .map(this::cookie)
     .collect(Collectors.toList());
  } else {
   this.cookies = Collections.emptyList();
  }
 }
 return this.cookies;
}

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

@Override
protected void decode(ChannelHandlerContext ctx, HttpRequest msg, List<Object> out)
    throws Exception {
  CharSequence acceptedEncoding = msg.headers().get(HttpHeaderNames.ACCEPT_ENCODING);
  if (acceptedEncoding == null) {
    acceptedEncoding = HttpContentDecoder.IDENTITY;
  }
  HttpMethod meth = msg.method();
  if (meth == HttpMethod.HEAD) {
    acceptedEncoding = ZERO_LENGTH_HEAD;
  } else if (meth == HttpMethod.CONNECT) {
    acceptedEncoding = ZERO_LENGTH_CONNECT;
  }
  acceptEncodingQueue.add(acceptedEncoding);
  out.add(ReferenceCountUtil.retain(msg));
}

代码示例来源:origin: blynkkk/blynk-server

public static User validateAuth(UserDao userDao, HttpRequest req) throws IllegalAccessException {
    String auth = req.headers().get(HttpHeaderNames.AUTHORIZATION);
    if (auth != null) {
      try {
        String encodedAuth = auth.substring("Basic ".length());
        String decoded = new String(java.util.Base64.getDecoder().decode(encodedAuth));
        String[] userAndPass = decoded.split(":");
        String user = userAndPass[0].toLowerCase();
        String pass = userAndPass[1];

        User superUser = userDao.getSuperAdmin();
        String passHash = SHA256Util.makeHash(pass, user);

        log.info("Header auth attempt. User: {}, pass: {}", user, pass);
        if (superUser != null && superUser.email.equals(user) && superUser.pass.equals(passHash)) {
          return superUser;
        } else {
          throw new IllegalAccessException("Authentication failed.");
        }
      } catch (IllegalAccessException iae) {
        log.error("Error invoking OTA handler. {}", iae.getMessage());
        throw iae;
      } catch (Exception e) {
        log.error("Error invoking OTA handler.");
      }
    }
    return null;
  }
}

代码示例来源:origin: lets-blade/blade

private boolean isWebSocketRequest(HttpRequest req){
  return req != null
      && (this.handler = this.blade.routeMatcher().getWebSocket(req.uri())) != null
      && req.decoderResult().isSuccess()
      && "websocket".equals(req.headers().get("Upgrade"));
}

代码示例来源:origin: lets-blade/blade

private boolean isWebSocketRequest(HttpRequest req){
  return req != null
      && (this.handler = this.blade.routeMatcher().getWebSocket(req.uri())) != null
      && req.decoderResult().isSuccess()
      && "websocket".equals(req.headers().get("Upgrade"));
}

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

@Override
public void channelRead(final ChannelHandlerContext ctx, final Object msg) throws Exception {
  if (msg instanceof HttpRequest) {
    request = (HttpRequest) msg;
    final String origin = request.headers().get(HttpHeaderNames.ORIGIN);
    config = getForOrigin(origin);
    if (isPreflightRequest(request)) {
      handlePreflight(ctx, request);
      return;
    }
    if (isShortCircuit && !(origin == null || config != null)) {
      forbidden(ctx, request);
      return;
    }
  }
  ctx.fireChannelRead(msg);
}

代码示例来源:origin: eclipse-vertx/vert.x

if (msg instanceof HttpRequest) {
 HttpRequest request = (HttpRequest) msg;
 if (request.headers().contains(io.vertx.core.http.HttpHeaders.UPGRADE, Http2CodecUtil.HTTP_UPGRADE_PROTOCOL_NAME, true)) {
  String connection = request.headers().get(io.vertx.core.http.HttpHeaders.CONNECTION);
  int found = 0;
  if (connection != null && connection.length() > 0) {
   String settingsHeader = request.headers().get(Http2CodecUtil.HTTP_UPGRADE_SETTINGS_HEADER);
   if (settingsHeader != null) {
    Http2Settings settings = HttpUtils.decodeSettings(settingsHeader);
      headers.method(request.method().name());
      headers.path(request.uri());
      headers.authority(request.headers().get("host"));
      headers.scheme("http");
      request.headers().remove("http2-settings");
      request.headers().remove("host");
      request.headers().forEach(header -> headers.set(header.getKey().toLowerCase(), header.getValue()));
      ctx.fireChannelRead(new DefaultHttp2HeadersFrame(headers, false));
     } else {

代码示例来源:origin: jooby-project/jooby

@Override
public void channelRead0(final ChannelHandlerContext ctx, final Object msg) {
 if (msg instanceof HttpRequest) {
  ctx.channel().attr(NettyRequest.NEED_FLUSH).set(true);
  HttpRequest req = (HttpRequest) msg;
  ctx.channel().attr(PATH).set(req.method().name() + " " + req.uri());
  if (HttpUtil.is100ContinueExpected(req)) {
   ctx.write(new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.CONTINUE));
  }
  boolean keepAlive = HttpUtil.isKeepAlive(req);
  try {
   String streamId = req.headers().get(STREAM_ID);
   HttpHeaders headers = new DefaultHttpHeaders();
   handler.handle(
     new NettyRequest(ctx, req, headers, tmpdir, wsMaxMessageSize),
     new NettyResponse(ctx, headers, bufferSize, keepAlive, streamId));
  } catch (Throwable ex) {
   exceptionCaught(ctx, ex);
  }
 } else if (msg instanceof WebSocketFrame) {
  Attribute<NettyWebSocket> ws = ctx.channel().attr(NettyWebSocket.KEY);
  ws.get().handle(msg);
 }
}

相关文章