@Override public MutableHeaders setDate(CharSequence name, Date value) { wrapped.set(name, value); return this; }
@Override public Response contentType(CharSequence contentType) { headers.set(CONTENT_TYPE, contentType); return this; }
@Override public Response noCompress() { headers.set(HttpHeaderNames.CONTENT_ENCODING, HttpHeaderValues.IDENTITY); return this; }
@Override public MutableHeaders set(CharSequence name, Object value) { if (!contentTypeSet && (name == CONTENT_TYPE || name.toString().equalsIgnoreCase(CONTENT_TYPE.toString()))) { contentTypeSet = true; } wrapped.set(name, value); return this; }
@Override public MutableHeaders set(CharSequence name, Iterable<?> values) { if (!contentTypeSet && (name == CONTENT_TYPE || name.toString().equalsIgnoreCase(CONTENT_TYPE.toString()))) { contentTypeSet = true; } wrapped.set(name, values); return this; }
private void maybeSetContentType(CharSequence s) { if (!headers.contains(HttpHeaderConstants.CONTENT_TYPE.toString())) { headers.set(HttpHeaderConstants.CONTENT_TYPE, s); } } }
@Override public void setResponseHeader(String name, String value) { response.getHeaders().set(name, value); }
@Override public Body type(CharSequence contentType) { getHeaders().set(HttpHeaderConstants.CONTENT_TYPE, contentType); return this; }
private void finalizeHeaders() { if (requestConfig.headers.get(HttpHeaderConstants.HOST) == null) { HostAndPort hostAndPort = HostAndPort.fromParts(channelKey.host, channelKey.port); requestConfig.headers.set(HttpHeaderConstants.HOST, hostAndPort.toString()); } if (client.getPoolSize() == 0) { requestConfig.headers.set(HttpHeaderConstants.CONNECTION, HttpHeaderValues.CLOSE); } int contentLength = requestConfig.body.readableBytes(); if (contentLength > 0) { requestConfig.headers.set(HttpHeaderConstants.CONTENT_LENGTH, Integer.toString(contentLength)); } }
/** * Forces the closing of the current connection, even if the client requested it to be kept alive. * <p> * This method can be used when it is desirable to force the client's connection to close, defeating HTTP keep alive. * This can be desirable in some networking environments where rate limiting or throttling is performed via edge routers or similar. * <p> * This method simply calls {@code getHeaders().set("Connection", "close")}, which has the same effect. * * @return {@code this} * @since 1.1 */ default Response forceCloseConnection() { getHeaders().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE); return this; } }
/** * Adds the appropriate header for HTTP Basic authentication with the given username and password. * <p> * This will replace any previous value set for the {@code "Authorization"} header. * * @param username the username * @param password the password * @return {@code this} */ default RequestSpec basicAuth(String username, String password) { byte[] bytes = (username + ":" + password).getBytes(StandardCharsets.ISO_8859_1); byte[] encodedBytes = Base64.getEncoder().encode(bytes); getHeaders().set(HttpHeaderNames.AUTHORIZATION, "Basic " + new String(encodedBytes, StandardCharsets.ISO_8859_1)); return this; }
/** * Sets a response header. * <p> * Any previously set values for the header will be removed. * <p> * Shorthand for {@code getResponse().getHeaders().set(CharSequence, Iterable)}. * * @param name the name of the header to set * @param values the header values * @return {@code this} * @since 1.4 * @see MutableHeaders#set(CharSequence, Iterable) */ default Context header(CharSequence name, Object... values) { getResponse().getHeaders().set(name, Arrays.asList(values)); return this; }
private void commit(ByteBuf buffer) { int readableBytes = buffer.readableBytes(); if (readableBytes > 0 || !mustNotHaveBody()) { headers.set(HttpHeaderNames.CONTENT_LENGTH, readableBytes); } finalizeResponse(() -> { setCookieHeader(); responseTransmitter.transmit(status.getNettyStatus(), buffer); }); }
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); } }); }
@Override public void redirect(Context context, int code, Object to) { String stringValue; if (to instanceof URI) { stringValue = ((URI) to).toASCIIString(); } else { stringValue = to.toString(); } context.getResponse().status(code); String normalizedLocation = generateRedirectLocation(context, context.getRequest(), stringValue); context.getResponse().getHeaders().set(HttpHeaderConstants.LOCATION, normalizedLocation); context.getResponse().send(); }
/** * Adds the number of milliseconds of elapsed time between {@link Request#getTimestamp()} and when the response is ready to be sent. * <p> * The timer is stopped, and the header added, by {@link Response#beforeSend(ratpack.func.Action)}. * This means that the time value is the elapsed time, commonly referred to as wall clock time, and not CPU time. * Similarly, it does not include the time to actually start sending data out over the socket. * It effectively times the application processing. * <p> * The value is in milliseconds, accurate to 5 decimal places. * * @param ctx the handling context. */ @Override public void handle(Context ctx) { Response response = ctx.getResponse(); response.beforeSend(m -> { Clock clock = ctx.get(Clock.class); Instant start = ctx.getRequest().getTimestamp(); long nanos = start.until(Instant.now(clock), ChronoUnit.NANOS); BigDecimal diffNanos = new BigDecimal(nanos); BigDecimal diffMillis = diffNanos.divide(NANOS_IN_MILLIS, 5, RoundingMode.UP); m.getHeaders().set(HEADER_NAME, diffMillis.toString()); }); ctx.next(); } }
response.getHeaders().set(HttpHeaderConstants.CONTENT_LENGTH, body.readableBytes()); responseTransmitter.transmit(HttpResponseStatus.INTERNAL_SERVER_ERROR, body);
/** * {@inheritDoc} */ @Override public void render(Context context) throws Exception { Response response = context.getResponse(); response.getHeaders().add(HttpHeaderConstants.TRANSFER_ENCODING, HttpHeaderConstants.CHUNKED); response.getHeaders().set(HttpHeaderConstants.CONTENT_TYPE, getContentType()); Publisher<? extends ByteBuf> publisher = publisher(context.get(ByteBufAllocator.class)); response.sendStream(publisher); }