public boolean isAjaxRequest() { return HttpHeaderConstants.XML_HTTP_REQUEST.equalsIgnoreCase(headers.get(HttpHeaderConstants.X_REQUESTED_WITH)); }
private String determineScheme(Request request) { Headers headers = request.getHeaders(); String forwardedSsl = headers.get(X_FORWARDED_SSL.toString()); String forwardedProto = headers.get(X_FORWARDED_PROTO.toString()); if (ON.toString().equalsIgnoreCase(forwardedSsl)) { return HTTPS_SCHEME; } return forwardedProto != null ? forwardedProto : defaultScheme; } }
@Nullable @Override public String get(CharSequence name) { return headers.get(name); }
@Override @Nullable public String get(String name) { return headers.get(name); }
public Set<Cookie> getCookies() { if (cookies == null) { String header = headers.get(HttpHeaderNames.COOKIE); if (header == null || header.length() == 0) { cookies = Collections.emptySet(); } else { cookies = ServerCookieDecoder.STRICT.decode(header); } } return cookies; }
public boolean hasValidHeader(Headers headers, String token) { String submittedHeader = headers.get(config.getHeaderName()); return token.equals(submittedHeader); }
@Override public MediaType getContentType() { return DefaultMediaType.get(headers.get(HttpHeaderNames.CONTENT_TYPE)); }
@Override public String getRequestHeader(String name) { return request.getHeaders().get(name); }
private HostAndPort getForwardedHostData(Request request) { Headers headers = request.getHeaders(); String forwardedHostHeader = Strings.emptyToNull(headers.get(X_FORWARDED_HOST.toString())); String hostPortString = forwardedHostHeader != null ? Iterables.getFirst(FORWARDED_HOST_SPLITTER.split(forwardedHostHeader), null) : null; return hostPortString != null ? HostAndPort.fromString(hostPortString) : null; }
private HostAndPort getHostData(Request request) { Headers headers = request.getHeaders(); String hostPortString = Strings.emptyToNull(headers.get(HOST.toString())); return hostPortString != null ? HostAndPort.fromString(hostPortString) : null; }
/** * Returns the request header with the specified name. * <p> * If there is more than value for the specified header, the first value is returned. * <p> * Shorthand for {@code getRequest().getHeaders().get(String)}. * * @param name the case insensitive name of the header to get retrieve the first value of * @return the header value or {@code null} if there is no such header * @since 1.4 */ default Optional<String> header(CharSequence name) { return Optional.ofNullable(getRequest().getHeaders().get(name)); }
/** * Adds a handler to the chain that delegates to the given handler chain if the request has a {@code Host} header that matches the given value exactly. * * <pre class="java-chain-dsl">{@code * chain. * host("foo.com", new Action<Chain>() { * public void execute(Chain hostChain) { * hostChain.all(new Handler() { * public void handle(Context context) { * context.getResponse().send("Host Handler"); * } * }); * } * }); * }</pre> * * @param hostName the name of the HTTP Header to match on * @param action the handler chain to delegate to if the host matches * @return this * @throws Exception any thrown by {@code action} */ default Chain host(String hostName, Action<? super Chain> action) throws Exception { return when(ctx -> Optional.ofNullable(ctx.getRequest().getHeaders().get(HttpHeaderNames.HOST)) .map(s -> s.equals(hostName)) .orElse(false), action ); }
@Override public RequestId generate(Request request) { String value = request.getHeaders().get(headerName); if (value == null) { return fallback.generate(request); } else { return RequestId.of(value); } } }
protected static String getId(Context ctx) { String id = ctx.get(Session.class).getId(); String uuid = ctx.getRequest().getHeaders().get("uuid"); if(uuid != null) { id += "-" + uuid; } logger.trace("id: {}", id); return id; } }
@Override public void handle(Context context) throws Exception { String acceptHeader = context.getRequest().getHeaders().get(HttpHeaderNames.ACCEPT); if (acceptHeader == null || acceptHeader.isEmpty()) { context.clientError(406); } else { String winner = MimeParse.bestMatch(contentTypes, acceptHeader); if (winner == null || winner.isEmpty()) { context.clientError(406); } else { context.next(); } } } }
public static void sendFile(Context context, Path file, BasicFileAttributes attributes) { Date date = new Date(attributes.lastModifiedTime().toMillis()); context.lastModified(date, () -> { final String ifNoneMatch = context.getRequest().getHeaders().get(HttpHeaderNames.IF_NONE_MATCH); Response response = context.getResponse(); if (ifNoneMatch != null && ifNoneMatch.trim().equals("*")) { response.status(NOT_MODIFIED.code()).send(); return; } response.contentTypeIfNotSet(() -> context.get(MimeTypes.class).getContentType(file.getFileName().toString())); response.getHeaders().set(HttpHeaderConstants.CONTENT_LENGTH, Long.toString(attributes.size())); try { response.sendFile(file); } catch (Exception e) { throw Exceptions.uncheck(e); } }); }
SentResponse response = outcome.getResponse(); String responseSize = "-"; String contentLength = response.getHeaders().get(HttpHeaderConstants.CONTENT_LENGTH); if (contentLength != null) { responseSize = contentLength;
@Override public void handle(Context context) throws Exception { if (handlers.isEmpty()) { noMatchHandler.handle(context); return; } String acceptHeader = context.getRequest().getHeaders().get(HttpHeaderNames.ACCEPT); if (Strings.isNullOrEmpty(acceptHeader)) { unspecifiedHandler.handle(context); } else { String winner = MimeParse.bestMatch(reverseKeys, acceptHeader); if (Strings.isNullOrEmpty(winner)) { noMatchHandler.handle(context); } else { String decoratedMimeType = mimeTypeDecorator.apply(winner); context.getResponse().contentType(decoratedMimeType); handlers.get(winner).handle(context); } } }
@SuppressWarnings("deprecation") public static <T> void connect(final Context context, String path, int maxLength, final WebSocketHandler<T> handler) { PublicAddress publicAddress = context.get(PublicAddress.class); URI address = publicAddress.get(context); URI httpPath = address.resolve(path); URI wsPath; try { wsPath = new URI("ws", httpPath.getUserInfo(), httpPath.getHost(), httpPath.getPort(), httpPath.getPath(), httpPath.getQuery(), httpPath.getFragment()); } catch (URISyntaxException e) { throw uncheck(e); } WebSocketServerHandshakerFactory factory = new WebSocketServerHandshakerFactory(wsPath.toString(), null, false, maxLength); Request request = context.getRequest(); HttpMethod method = valueOf(request.getMethod().getName()); FullHttpRequest nettyRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, method, request.getUri()); nettyRequest.headers().add(SEC_WEBSOCKET_VERSION, request.getHeaders().get(SEC_WEBSOCKET_VERSION)); nettyRequest.headers().add(SEC_WEBSOCKET_KEY, request.getHeaders().get(SEC_WEBSOCKET_KEY)); final WebSocketServerHandshaker handshaker = factory.newHandshaker(nettyRequest); final DirectChannelAccess directChannelAccess = context.getDirectChannelAccess(); final Channel channel = directChannelAccess.getChannel(); if (!channel.config().isAutoRead()) { channel.config().setAutoRead(true); } handshaker.handshake(channel, nettyRequest).addListener(new HandshakeFutureListener<>(context, handshaker, handler)); }
protected ReceivedResponse toReceivedResponse(HttpResponse msg, ByteBuf responseBuffer) { responseBuffer.touch(); final Headers headers = new NettyHeadersBackedHeaders(msg.headers()); String contentType = headers.get(HttpHeaderConstants.CONTENT_TYPE); final ByteBufBackedTypedData typedData = new ByteBufBackedTypedData(responseBuffer, DefaultMediaType.get(contentType)); final Status status = new DefaultStatus(msg.status()); return new DefaultReceivedResponse(status, headers, typedData); }