/** * Returns the scheme of this request. This method is a shortcut of {@code headers().scheme()}. */ default String scheme() { return headers().scheme(); }
/** * Sets the method of this request. This method is a shortcut of {@code headers().method(...)}. * * @return {@code this} */ default HttpRequest method(HttpMethod method) { headers().method(method); return this; }
/** * Sets the authority of this request. This method is a shortcut of {@code headers().authority(...)}. * * @return {@code this} */ default HttpRequest authority(String authority) { headers().authority(authority); return this; }
/** * Returns the method of this request. This method is a shortcut of {@code headers().method()}. */ default HttpMethod method() { return headers().method(); }
/** * Returns the path of this request. This method is a shortcut of {@code headers().path()}. */ default String path() { return headers().path(); }
/** * Sets the scheme of this request. This method is a shortcut of {@code headers().scheme(...)}. * * @return {@code this} */ default HttpRequest scheme(String scheme) { headers().scheme(scheme); return this; }
/** * Sets the path of this request. This method is a shortcut of {@code headers().path(...)}. * * @return {@code this} */ default HttpRequest path(String path) { headers().path(path); return this; }
/** * Returns the authority of this request. This method is a shortcut of {@code headers().authority()}. */ default String authority() { return headers().authority(); }
/** * Returns whether the request should be aggregated. */ static boolean aggregationRequired(AggregationStrategy strategy, HttpRequest req) { requireNonNull(strategy, "strategy"); switch (strategy) { case ALWAYS: return true; case FOR_FORM_DATA: return isFormData(req.headers().contentType()); } return false; }
@Override protected AggregatedHttpMessage onSuccess(HttpData content) { return AggregatedHttpMessage.of(request.headers(), content, trailingHeaders); }
/** * Returns {@code true} if the specified {@code request} is a CORS preflight request. */ public static boolean isCorsPreflightRequest(com.linecorp.armeria.common.HttpRequest request) { requireNonNull(request, "request"); return request.method() == HttpMethod.OPTIONS && request.headers().contains(HttpHeaderNames.ORIGIN) && request.headers().contains(HttpHeaderNames.ACCESS_CONTROL_REQUEST_METHOD); }
@Nullable static HttpEncodingType getWrapperForRequest(HttpRequest request) { final String acceptEncoding = request.headers().get(HttpHeaderNames.ACCEPT_ENCODING); if (acceptEncoding == null) { return null; } return determineEncoding(acceptEncoding); }
@Override protected MultiValueMap<String, HttpCookie> initCookies() { final MultiValueMap<String, HttpCookie> cookies = new LinkedMultiValueMap<>(); final List<String> values = req.headers().getAll(HttpHeaderNames.COOKIE); values.stream() .map(ServerCookieDecoder.LAX::decode) .flatMap(Collection::stream) .forEach(c -> cookies.add(c.name(), new HttpCookie(c.name(), c.value()))); return cookies; }
private static boolean validateAcceptHeaders(HttpRequest req, SerializationFormat serializationFormat) { // If accept header is present, make sure it is sane. Currently, we do not support accept // headers with a different format than the content type header. final List<String> acceptHeaders = req.headers().getAll(HttpHeaderNames.ACCEPT); if (!acceptHeaders.isEmpty() && !serializationFormat.mediaTypes().matchHeaders(acceptHeaders).isPresent()) { return false; } return true; }
@Override protected HttpResponse doExecute(ClientRequestContext ctx, HttpRequest req) throws Exception { final boolean hasInitialAuthority = !isNullOrEmpty(req.headers().authority()); final CompletableFuture<HttpResponse> responseFuture = new CompletableFuture<>(); final HttpResponse res = HttpResponse.from(responseFuture); final HttpRequestDuplicator reqDuplicator = new HttpRequestDuplicator(req, 0, ctx.eventLoop()); doExecute0(ctx, reqDuplicator, req, res, responseFuture, hasInitialAuthority); return res; }
@Override public HttpResponse serve(ServiceRequestContext ctx, HttpRequest req) throws Exception { final HttpEncodingType encodingType = HttpEncoders.getWrapperForRequest(req); final HttpResponse delegateResponse = delegate().serve(ctx, req); if (encodingType == null || !encodableRequestHeadersPredicate.test(req.headers())) { return delegateResponse; } return new HttpEncodedResponse( delegateResponse, encodingType, encodableContentTypePredicate, minBytesToForceChunkedAndEncoding); }
ArmeriaServerHttpRequest(ServiceRequestContext ctx, HttpRequest req, DataBufferFactoryWrapper<?> factoryWrapper) { super(URI.create(requireNonNull(req, "req").path()), null, fromArmeriaHttpHeaders(req.headers())); this.ctx = requireNonNull(ctx, "ctx"); this.req = req; body = Flux.from(req).cast(HttpData.class).map(factoryWrapper::toDataBuffer) // Guarantee that the context is accessible from a controller method // when a user specify @RequestBody in order to convert a request body into an object. .publishOn(Schedulers.fromExecutor(ctx.contextAwareExecutor())); }
HttpParameters httpParameters() { HttpParameters result = httpParameters; if (result == null) { synchronized (this) { result = httpParameters; if (result == null) { httpParameters = result = httpParametersOf(context.query(), request.headers().contentType(), message); } } } return result; }
@Override public HttpResponse execute(ClientRequestContext ctx, HttpRequest req) throws Exception { if (req.headers().contains(HttpHeaderNames.ACCEPT_ENCODING)) { // Client specified encoding, so we don't do anything automatically. return delegate().execute(ctx, req); } req.headers().set(HttpHeaderNames.ACCEPT_ENCODING, acceptEncodingHeader); final HttpResponse res = delegate().execute(ctx, req); return new HttpDecodedResponse(res, decoderFactories); } }
@Override public CompletionStage<Boolean> authorize(ServiceRequestContext ctx, HttpRequest req) { final String value = req.headers().get(HttpHeaderNames.COOKIE); if (value == null) { return CompletableFuture.completedFuture(false); } // Authentication will be succeeded only if both the specified cookie name and value are matched. final Set<Cookie> cookies = ServerCookieDecoder.STRICT.decode(value); final boolean result = cookies.stream().anyMatch( cookie -> cookieName.equals(cookie.name()) && cookieValue.equals(cookie.value())); return CompletableFuture.completedFuture(result); } }