/** * Creates a new {@link HttpRequest} and closes the stream. */ static HttpRequest of(HttpHeaders headers, HttpData content) { return of(headers, content, HttpHeaders.EMPTY_HEADERS); }
/** * Creates a new HTTP request and closes the stream. * * @param method the HTTP method of the request * @param path the path of the request * @param mediaType the {@link MediaType} of the request content * @param content the content of the request */ static HttpRequest of(HttpMethod method, String path, MediaType mediaType, HttpData content) { return of(method, path, mediaType, content, HttpHeaders.EMPTY_HEADERS); }
/** * Creates a new {@link HttpRequest} with empty content and closes the stream. */ static HttpRequest of(HttpHeaders headers) { return of(headers, HttpData.EMPTY_DATA); }
/** * Converts this message into a new complete {@link HttpRequest}. * * @deprecated Use {@link HttpRequest#of(AggregatedHttpMessage)}. */ @Deprecated default HttpRequest toHttpRequest() { return HttpRequest.of(this); }
/** * Creates a new HTTP request with empty content and closes the stream. * * @param method the HTTP method of the request * @param path the path of the request */ static HttpRequest of(HttpMethod method, String path) { requireNonNull(method, "method"); requireNonNull(path, "path"); return of(HttpHeaders.of(method, path)); }
HttpResponse execute(@Nullable EventLoop eventLoop, AggregatedHttpMessage aggregatedReq) { return execute(eventLoop, HttpRequest.of(aggregatedReq)); } }
/** * Creates a new HTTP request and closes the stream. * * @param method the HTTP method of the request * @param path the path of the request * @param mediaType the {@link MediaType} of the request content * @param content the content of the request */ static HttpRequest of(HttpMethod method, String path, MediaType mediaType, byte[] content) { requireNonNull(content, "content"); return of(method, path, mediaType, HttpData.of(content)); }
/** * Creates a new HTTP request and closes the stream. * * @param method the HTTP method of the request * @param path the path of the request * @param mediaType the {@link MediaType} of the request content * @param content the content of the request */ static HttpRequest of(HttpMethod method, String path, MediaType mediaType, String content) { requireNonNull(content, "content"); requireNonNull(mediaType, "mediaType"); return of(method, path, mediaType, content.getBytes(mediaType.charset().orElse(StandardCharsets.UTF_8))); }
/** * Creates a new HTTP request and closes the stream. * * @param method the HTTP method of the request * @param path the path of the request * @param mediaType the {@link MediaType} of the request content * @param content the content of the request * @param offset the start offset of {@code content} * @param length the length of {@code content} */ static HttpRequest of( HttpMethod method, String path, MediaType mediaType, byte[] content, int offset, int length) { requireNonNull(content, "content"); return of(method, path, mediaType, HttpData.of(content, offset, length)); }
private Mono<Void> write(Flux<? extends DataBuffer> body) { return doCommit(execute(() -> HttpRequest.of(headers, body.map(factoryWrapper::toHttpData)))); }
/** * Creates a new HTTP request and closes the stream. * * @param method the HTTP method of the request * @param path the path of the request * @param mediaType the {@link MediaType} of the request content * @param content the content of the request * @param trailingHeaders the trailing HTTP headers */ static HttpRequest of(HttpMethod method, String path, MediaType mediaType, HttpData content, HttpHeaders trailingHeaders) { requireNonNull(method, "method"); requireNonNull(path, "path"); requireNonNull(mediaType, "mediaType"); return of(HttpHeaders.of(method, path).contentType(mediaType), content, trailingHeaders); }
@Override public Mono<Void> setComplete() { return isCommitted() ? Mono.empty() : doCommit(execute(() -> HttpRequest.of(headers))); }
/** * Converts the {@link AggregatedHttpMessage} into a new {@link HttpRequest} and closes the stream. */ static HttpRequest of(AggregatedHttpMessage message) { return of(message.headers(), message.content(), message.trailingHeaders()); }
private AggregatedHttpMessage sendViaHttpPostBindingProtocol( String path, String paramName, SignableSAMLObject sinableObj) throws Exception { final String encoded = toSignedBase64(sinableObj, idpCredential, signatureAlgorithm); final QueryStringEncoder encoder = new QueryStringEncoder("/"); encoder.addParam(paramName, encoded); final HttpRequest req = HttpRequest.of(HttpMethod.POST, path, MediaType.FORM_DATA, encoder.toUri().getRawQuery()); return client.execute(req).aggregate().join(); }
@Test public void getCookies() { final HttpRequest httpRequest = HttpRequest.of(HttpHeaders.of(HttpMethod.POST, "/") .add(HttpHeaderNames.COOKIE, "a=1;b=2")); final ServiceRequestContext ctx = newRequestContext(httpRequest); final ArmeriaServerHttpRequest req = request(ctx); // Check cached. final MultiValueMap<String, HttpCookie> cookie1 = req.getCookies(); final MultiValueMap<String, HttpCookie> cookie2 = req.getCookies(); assertThat(cookie1 == cookie2).isTrue(); assertThat(cookie1.get("a")).containsExactly(new HttpCookie("a", "1")); assertThat(cookie1.get("b")).containsExactly(new HttpCookie("b", "2")); }
@Test public void missingContentType() throws Exception { final HttpRequest req = HttpRequest.of(HttpMethod.POST, "/grpc.testing.TestService.UnaryCall"); 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 AggregatedHttpMessage sendViaHttpRedirectBindingProtocol( String path, String paramName, SAMLObject samlObject) throws Exception { final QueryStringEncoder encoder = new QueryStringEncoder("/"); encoder.addParam(paramName, toDeflatedBase64(samlObject)); encoder.addParam(SIGNATURE_ALGORITHM, signatureAlgorithm); final String input = encoder.toUri().getRawQuery(); final String output = generateSignature(idpCredential, signatureAlgorithm, input); encoder.addParam(SIGNATURE, output); final HttpRequest req = HttpRequest.of(HttpMethod.POST, path, MediaType.FORM_DATA, encoder.toUri().getRawQuery()); return client.execute(req).aggregate().join(); } }
@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."))); }
@Before public void setUp() { request = HttpRequest.of(HttpMethod.POST, "/armeria.grpc.testing.TestService/EmptyCall", MediaType.JSON_UTF_8, "{}"); ctx = ServiceRequestContextBuilder.of(request).eventLoop(eventLoop.get()).build(); }
@Test public void pathMissingSlash() throws Exception { final HttpRequest req = HttpRequest.of( HttpHeaders.of(HttpMethod.POST, "/grpc.testing.TestService.UnaryCall") .set(HttpHeaderNames.CONTENT_TYPE, "application/grpc+proto")); final PathMappingResult pathMappingResult = PathMappingResult.of("grpc.testing.TestService.UnaryCall"); final ServiceRequestContext ctx = ServiceRequestContextBuilder.of(req) .pathMappingResult(pathMappingResult) .build(); final HttpResponse response = grpcService.doPost(ctx, req); assertThat(response.aggregate().get()).isEqualTo(AggregatedHttpMessage.of( HttpHeaders.of(HttpStatus.BAD_REQUEST) .contentType(MediaType.PLAIN_TEXT_UTF_8) .setInt(HttpHeaderNames.CONTENT_LENGTH, 13), HttpData.ofUtf8("Invalid path."))); }