/** * Creates a new HTTP response of the specified {@link HttpStatus} and closes the stream. * * @param mediaType the {@link MediaType} of the response content * @param content the content of the response * @param trailingHeaders the trailing HTTP headers */ static HttpResponse of(HttpStatus status, MediaType mediaType, HttpData content, HttpHeaders trailingHeaders) { requireNonNull(status, "status"); requireNonNull(mediaType, "mediaType"); requireNonNull(content, "content"); final HttpHeaders headers = HttpHeaders.of(status).contentType(mediaType); return of(headers, content, trailingHeaders); }
@Override public String get(AsciiString name, String defaultValue) { return delegate.get(name, defaultValue); }
/** * Returns an {@link HttpHeaders} with the specified {@code location}, the default {@code cache-control} * and the default {@code pragma} headers. */ static HttpHeaders headersWithLocation(String location) { return HttpHeaders.of(HttpStatus.FOUND) .add(HttpHeaderNames.LOCATION, location) .add(HttpHeaderNames.CACHE_CONTROL, DEFAULT_CACHE_CONTROL) .add(HttpHeaderNames.PRAGMA, DEFAULT_PRAGMA); }
private HttpHeaders setHttpStatus(HttpHeaders headers) { if (headers.status() != null) { // Do not overwrite HTTP status. return headers; } final HttpStatus defaultHttpStatus = defaultHttpHeaders.status(); assert defaultHttpStatus != null; return headers.isImmutable() ? HttpHeaders.copyOf(headers).status(defaultHttpStatus) : headers.status(defaultHttpStatus); }
/** * Sends an HTTP GET request. */ default HttpResponse get(String path) { return execute(HttpHeaders.of(HttpMethod.GET, path)); }
@Test public void badContentType() throws Exception { final HttpRequest req = HttpRequest.of( HttpHeaders.of(HttpMethod.POST, "/grpc.testing.TestService.UnaryCall") .contentType(MediaType.JSON_UTF_8)); final ServiceRequestContext ctx = ServiceRequestContext.of(req); final HttpResponse response = grpcService.doPost(ctx, req); assertThat(response.aggregate().get()).isEqualTo(AggregatedHttpMessage.of( HttpHeaders.of(HttpStatus.UNSUPPORTED_MEDIA_TYPE) .contentType(MediaType.PLAIN_TEXT_UTF_8) .setInt(HttpHeaderNames.CONTENT_LENGTH, 39), HttpData.ofUtf8("Missing or invalid Content-Type header."))); }
private static ServiceRequestContext newServiceContext( String path, @Nullable String query) throws Exception { final InetSocketAddress remoteAddress = new InetSocketAddress( InetAddress.getByAddress("client.com", new byte[] { 1, 2, 3, 4 }), 5678); final InetSocketAddress localAddress = new InetSocketAddress( InetAddress.getByAddress("server.com", new byte[] { 5, 6, 7, 8 }), 8080); final String pathAndQuery = path + (query != null ? '?' + query : ""); final HttpRequest req = HttpRequest.of(HttpHeaders.of(HttpMethod.GET, pathAndQuery) .authority("server.com:8080") .set(HttpHeaderNames.USER_AGENT, "some-client")); final ServiceRequestContext ctx = ServiceRequestContextBuilder.of(req) .sslSession(newSslSession()) .remoteAddress(remoteAddress) .localAddress(localAddress) .clientAddress(InetAddress.getByName("9.10.11.12")) .build(); ctx.attr(MY_ATTR).set(new CustomValue("some-attr")); return ctx; }
private void fail(int id, HttpResponseStatus status) { discarding = true; req = null; final HttpData data = HttpData.ofUtf8(status.toString()); final com.linecorp.armeria.common.HttpHeaders headers = com.linecorp.armeria.common.HttpHeaders.of(status.code()); headers.set(HttpHeaderNames.CONNECTION, "close"); headers.setObject(HttpHeaderNames.CONTENT_TYPE, MediaType.PLAIN_TEXT_UTF_8); headers.setInt(HttpHeaderNames.CONTENT_LENGTH, data.length()); writer.writeHeaders(id, 1, headers, false); writer.writeData(id, 1, data, true).addListener(ChannelFutureListener.CLOSE); }
private static HttpHeaders toResponseHeaders(ArmeriaHttpTransport transport) { final MetaData.Response info = transport.info; if (info == null) { throw new IllegalStateException("response metadata unavailable"); } final HttpHeaders headers = HttpHeaders.of(HttpStatus.valueOf(info.getStatus())); info.getFields().forEach(e -> headers.add(HttpHeaderNames.of(e.getName()), e.getValue())); if (transport.method != HttpMethod.HEAD) { headers.setLong(HttpHeaderNames.CONTENT_LENGTH, transport.contentLength); } return headers; }
@Override public MediaType contentType() { return delegate.contentType(); }
@Override public HttpStatus status() { return delegate.status(); }
/** * Returns new HTTP headers with four entries. */ static HttpHeaders of(AsciiString name1, String value1, AsciiString name2, String value2, AsciiString name3, String value3, AsciiString name4, String value4) { return new DefaultHttpHeaders().add(name1, value1).add(name2, value2) .add(name3, value3).add(name4, value4); }
@Test public void single() { final HttpClient client = HttpClient.of(rule.uri("/single")); AggregatedHttpMessage msg; msg = client.get("/string").aggregate().join(); assertThat(msg.headers().contentType()).isEqualTo(MediaType.PLAIN_TEXT_UTF_8); assertThat(msg.content().toStringUtf8()).isEqualTo("a"); msg = client.get("/json").aggregate().join(); assertThat(msg.headers().contentType()).isEqualTo(MediaType.JSON_UTF_8); assertThatJson(msg.content().toStringUtf8()).isStringEqualTo("a"); msg = client.get("/error").aggregate().join(); assertThat(msg.headers().status()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR); }
void setCorsAllowCredentials(HttpHeaders headers) { // The string "*" cannot be used for a resource that supports credentials. // https://www.w3.org/TR/cors/#resource-requests if (credentialsAllowed && !ANY_ORIGIN.equals(headers.get(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN))) { headers.set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_CREDENTIALS, "true"); } }
/** * Sets the keep alive header as per: * - https://www.w3.org/Protocols/HTTP/1.1/draft-ietf-http-v11-spec-01.html#Connection */ private void addKeepAliveHeaders(HttpRequest req, HttpHeaders headers) { if (protocol == H1 || protocol == H1C) { headers.set(HttpHeaderNames.CONNECTION, "keep-alive"); } else { // Do not add the 'connection' header for HTTP/2 responses. // See https://tools.ietf.org/html/rfc7540#section-8.1.2.2 } }
private void prepareHeaders(HttpHeaders headers, Compressor compressor) { if (compressor != Identity.NONE) { headers.set(GrpcHeaderNames.GRPC_ENCODING, compressor.getMessageEncoding()); } if (!advertisedEncodingsHeader.isEmpty()) { headers.add(GrpcHeaderNames.GRPC_ACCEPT_ENCODING, advertisedEncodingsHeader); } headers.add(GrpcHeaderNames.GRPC_TIMEOUT, TimeoutHeaderUtil.toHeaderValue( TimeUnit.MILLISECONDS.toNanos(ctx.responseTimeoutMillis()))); }
final HttpRequest req = HttpRequest.of(HttpHeaders.of(HttpMethod.POST, "/hello/armeria") .authority("foo.com")); final RpcRequest rpcReq = RpcRequest.of(HelloService.Iface.class, "hello", "Armeria"); final HttpResponse res = HttpResponse.of(HttpStatus.OK); ctx.logBuilder().responseHeaders(HttpHeaders.of(HttpStatus.OK)); ctx.logBuilder().responseFirstBytesTransferred(); ctx.logBuilder().responseContent(rpcRes, res);
private HttpHeaders addNegotiatedResponseMediaType(ServiceRequestContext ctx, HttpHeaders headers) { final MediaType negotiatedResponseMediaType = ctx.negotiatedResponseMediaType(); if (negotiatedResponseMediaType == null || headers.contentType() != null) { // Do not overwrite 'content-type'. return headers; } return headers.isImmutable() ? HttpHeaders.copyOf(headers).contentType(negotiatedResponseMediaType) : headers.contentType(negotiatedResponseMediaType); }