https://linen.dev logo
#help
Title
# help
ž

Žiga sternad

03/11/2024, 10:09 AM
Hi all! I’m still trying to find a way to keep the
user-agent
header as it is sent from the client, proxied via. Wiremock, to the server. In my case, we need to forward the exact value as it was sent by the client. I have tried to create an extension to remove and re-add the header, with the code below:
Copy code
public class UrlAndHeadersModifyingFilter implements StubRequestFilterV2 {

    @Override
    public RequestFilterAction filter(Request request, ServeEvent serveEvent) {
        String originalUserAgent = "example-user-agent";

        Request wrappedRequest = RequestWrapper.create()
                .removeHeader("User-Agent")
                .addHeader("User-Agent", originalUserAgent)
                .wrap(request);

        return RequestFilterAction.continueWith(wrappedRequest);
    }

    @Override
    public String getName() {
        return "url-and-header-modifier";
    }
}
I can see the value being updated in the http://localhost:9999/__admin/requests logs, but the server receives the updated version of the header:
apache-httpClient/5.3.1 (Java/17.0.9)
I also tried to enable
.notifier(new ConsoleNotifier(true))
and I see all requests/responses in the terminal. All the request logs contain the original User-agent that is coming from the client. Am I missing something? 😐 I would like to know if this is even solvable with the Wiremock, or is it a bug or something that is not supported? Really appreciate some thoughts / feedback on this 🙏 🙇 cc: @Tom
t

Tom

03/11/2024, 11:32 AM
I’m not sure TBH, so I think you’d need to dig into the Apache HTTP client to figure this out. You can definitely set the UA at client setup time, but I don’t know if it supports doing it per request. It is possible to substitute the whole HTTP client via
HttpClientFactory
so that’s the nuclear option if you can’t get the Apache client to play.
ž

Žiga sternad

03/11/2024, 11:54 AM
Thanks for your response, Tom. I just cloned the Wiremock repo and removed the User-agent from the FORBIDDEN_REQUEST_HEADERS list, by changing the line in
com.github.tomakehurst.wiremock.http.client.HttpClient
Copy code
List<String> FORBIDDEN_REQUEST_HEADERS = List.of(TRANSFER_ENCODING, CONTENT_LENGTH, "connection", USER_AGENT);
to
Copy code
List<String> FORBIDDEN_REQUEST_HEADERS = List.of(TRANSFER_ENCODING, CONTENT_LENGTH, "connection");
… and now the proper user-agent is forwarded to the server Do you know, is there a reason why this header is forbidden to be forwarded?
t

Tom

03/11/2024, 11:55 AM
Good spot. I can’t remember whether I did that or what the reason was TBH.
Perhaps we could create a config parameter called
preserveUserAgentProxyHeader
in the same fashion as
preserveHostHeader
I don’t think I’d want to just drop this, as in some cases at least you want the last caller in the chain to present the UA
ž

Žiga sternad

03/11/2024, 12:02 PM
That would be a life-saver! 🙌
t

Tom

03/11/2024, 12:02 PM
Do you feel like creating a PR?
ž

Žiga sternad

03/11/2024, 12:10 PM
I’ll do my best - I’m working on it 🙂
t

Tom

03/11/2024, 12:11 PM
Thanks. Will definitely happen quicker that way.
ž

Žiga sternad

03/11/2024, 12:17 PM
In your opinion, is there a dependency which flag should this preserveUserAgentProxyHeader be used with? e.g.
proxyAll
or
enableBrowserProxying
or none …?
t

Tom

03/11/2024, 12:20 PM
No, because it could just be a proxy stub
1