You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@hc.apache.org by "Arturo Bernal (Jira)" <ji...@apache.org> on 2023/02/10 19:43:00 UTC

[jira] [Commented] (HTTPCORE-478) Add "Forwarded" HttpRequestInterceptor

    [ https://issues.apache.org/jira/browse/HTTPCORE-478?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17687253#comment-17687253 ] 

Arturo Bernal commented on HTTPCORE-478:
----------------------------------------

[~olegk] 

 

Just to know if I'm on the right track and I'm not saying/doing anything foolish. Does this comply with the rules of the RFC? I've tried to follow the documentation and reflect everything it says but it's not easy to follow.

 

 

 
{code:java}
@Override
public void process(final HttpRequest request, final EntityDetails entity, final HttpContext context) throws HttpException, IOException {

    Args.notNull(request, "HTTP request");
    Args.notNull(context, "HTTP context");

    final ProtocolVersion ver = context.getProtocolVersion() != null ? context.getProtocolVersion() : HttpVersion.HTTP_1_1;

    final URIAuthority authority = request.getAuthority();
    if (authority == null) {
        throw new ProtocolException("Request authority not specified");
    }

    final int port = authority.getPort();

    final StringBuilder valueBuilder = new StringBuilder();
    valueBuilder.append("host=").append(authority.getHostName());

    if (port != -1) {
        valueBuilder.append(";port=").append(port);
    }

    final String protoValue = ver.getProtocol();
    if (protoValue != null) {
        valueBuilder.append("proto=").append(protoValue);
    }


    final EndpointDetails endpointDetails = (EndpointDetails) context.getAttribute(HttpCoreContext.CONNECTION_ENDPOINT);

    // Add the "for" property
    if (endpointDetails != null) {
        valueBuilder.append(";for=").append(endpointDetails.getRemoteAddress().toString());
    }

    final String byValue = (String) context.getAttribute(X_FORWARDED_HEADER_NAME);
    if (byValue != null) {
        valueBuilder.append(";by=").append(byValue);
    } else {
        valueBuilder.append(";by=unknown");
    }

    final Header header = request.getFirstHeader(X_FORWARDED_HEADER_NAME);
    if (header != null) {
        request.removeHeader(header);
    }
    request.addHeader(new BasicHeader(X_FORWARDED_HEADER_NAME, valueBuilder.toString()));
    request.addHeader(new BasicHeader(FORWARDED_HEADER_NAME, valueBuilder.toString()));
}{code}

> Add "Forwarded" HttpRequestInterceptor
> --------------------------------------
>
>                 Key: HTTPCORE-478
>                 URL: https://issues.apache.org/jira/browse/HTTPCORE-478
>             Project: HttpComponents HttpCore
>          Issue Type: New Feature
>            Reporter: Gary D. Gregory
>            Priority: Major
>
> Add a "Forwarded" HttpRequestInterceptor to add the "Forwarded" HTTP header.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org