|
| 1 | +/* |
| 2 | + * ==================================================================== |
| 3 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 4 | + * or more contributor license agreements. See the NOTICE file |
| 5 | + * distributed with this work for additional information |
| 6 | + * regarding copyright ownership. The ASF licenses this file |
| 7 | + * to you under the Apache License, Version 2.0 (the |
| 8 | + * "License"); you may not use this file except in compliance |
| 9 | + * with the License. You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, |
| 14 | + * software distributed under the License is distributed on an |
| 15 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 | + * KIND, either express or implied. See the License for the |
| 17 | + * specific language governing permissions and limitations |
| 18 | + * under the License. |
| 19 | + * ==================================================================== |
| 20 | + * |
| 21 | + * This software consists of voluntary contributions made by many |
| 22 | + * individuals on behalf of the Apache Software Foundation. For more |
| 23 | + * information on the Apache Software Foundation, please see |
| 24 | + * <http://www.apache.org/>. |
| 25 | + * |
| 26 | + */ |
| 27 | +package org.apache.hc.client5.http.impl.async; |
| 28 | + |
| 29 | +import org.apache.hc.client5.http.EarlyHintsListener; |
| 30 | +import org.apache.hc.client5.http.async.AsyncExecCallback; |
| 31 | +import org.apache.hc.client5.http.async.AsyncExecChain; |
| 32 | +import org.apache.hc.client5.http.async.AsyncExecChainHandler; |
| 33 | +import org.apache.hc.core5.http.EntityDetails; |
| 34 | +import org.apache.hc.core5.http.HttpException; |
| 35 | +import org.apache.hc.core5.http.HttpRequest; |
| 36 | +import org.apache.hc.core5.http.HttpResponse; |
| 37 | +import org.apache.hc.core5.http.HttpStatus; |
| 38 | +import org.apache.hc.core5.http.nio.AsyncEntityProducer; |
| 39 | +import org.apache.hc.core5.http.protocol.HttpContext; |
| 40 | + |
| 41 | +/** |
| 42 | + * Execution chain handler that delivers {@code 103 Early Hints} (RFC 8297) |
| 43 | + * informational responses to a user-provided {@link org.apache.hc.client5.http.EarlyHintsListener} |
| 44 | + * without affecting the final (non-1xx) response processing. |
| 45 | + * |
| 46 | + * <p>This handler wraps the downstream {@link org.apache.hc.client5.http.async.AsyncExecCallback} |
| 47 | + * and forwards any informational response with code |
| 48 | + * {@link org.apache.hc.core5.http.HttpStatus#SC_EARLY_HINTS 103} to the listener. |
| 49 | + * All other responses (including the final response) are delegated unchanged.</p> |
| 50 | + * |
| 51 | + * |
| 52 | + * <p>For security and interoperability, applications typically act only on |
| 53 | + * headers considered safe in Early Hints (for example, {@code Link} with |
| 54 | + * {@code rel=preload} or {@code rel=preconnect}).</p> |
| 55 | + * |
| 56 | + * @see org.apache.hc.client5.http.EarlyHintsListener |
| 57 | + * @see org.apache.hc.core5.http.HttpStatus#SC_EARLY_HINTS |
| 58 | + * @see org.apache.hc.core5.http.nio.ResponseChannel#sendInformation(HttpResponse, HttpContext) |
| 59 | + * @since 5.6 |
| 60 | + */ |
| 61 | +public final class EarlyHintsAsyncExec implements AsyncExecChainHandler { |
| 62 | + private final EarlyHintsListener listener; |
| 63 | + |
| 64 | + public EarlyHintsAsyncExec(final EarlyHintsListener listener) { |
| 65 | + this.listener = listener; |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public void execute(final HttpRequest request, |
| 70 | + final AsyncEntityProducer entityProducer, |
| 71 | + final AsyncExecChain.Scope scope, |
| 72 | + final AsyncExecChain chain, |
| 73 | + final AsyncExecCallback callback) throws HttpException, java.io.IOException { |
| 74 | + |
| 75 | + if (listener == null) { |
| 76 | + chain.proceed(request, entityProducer, scope, callback); |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + chain.proceed(request, entityProducer, scope, new AsyncExecCallback() { |
| 81 | + @Override |
| 82 | + public void handleInformationResponse(final HttpResponse response) |
| 83 | + throws HttpException, java.io.IOException { |
| 84 | + if (response.getCode() == HttpStatus.SC_EARLY_HINTS) { |
| 85 | + listener.onEarlyHints(response, scope.clientContext); |
| 86 | + } |
| 87 | + callback.handleInformationResponse(response); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public org.apache.hc.core5.http.nio.AsyncDataConsumer handleResponse( |
| 92 | + final HttpResponse response, final EntityDetails entityDetails) |
| 93 | + throws HttpException, java.io.IOException { |
| 94 | + return callback.handleResponse(response, entityDetails); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public void completed() { |
| 99 | + callback.completed(); |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public void failed(final Exception cause) { |
| 104 | + callback.failed(cause); |
| 105 | + } |
| 106 | + }); |
| 107 | + } |
| 108 | +} |
0 commit comments