square / okhttp

Square’s meticulous HTTP client for the JVM, Android, and GraalVM.
https://square.github.io/okhttp/
Apache License 2.0
45.86k stars 9.16k forks source link

Change user-agent on connect request? #7362

Open regbo opened 2 years ago

regbo commented 2 years ago

Forgive me if there is a solution for this, but I can't seem to find a way to change the User-Agent header on a connect request. It seems like it's hard coded to:

User-Agent: okhttp/4.10.0

I can change it with reflection, but we have a situation where a server needs the user agent to match all requests moving forward. Is there a way to do it? Perhaps it could be acheived with this enhancement?

https://github.com/square/okhttp/issues/7297

swankjesse commented 2 years ago

No way to manipulate this currently. I'm not even sure where we'd add that configuration option in our API.

regbo commented 2 years ago

No way to manipulate this currently. I'm not even sure where we'd add that configuration option in our API.

Perhaps just copy the value sent with the request initiating the connect?

swankjesse commented 2 years ago

That's the most obvious option. The drawback is that the request that creates the tunnel is arbitrary. If you have a connection that carries multiple requests, they may each use different user-agents. In general we're pretty allergic to data leakage like this.

For regular applications it's unlikely to matter but it's possible for OkHttp to be used inside a proxy or gateway where the requests are truly independent.

regbo commented 2 years ago

Right. What about a default user agent tied to the connection pool?

Or somehow allow intercepting on the Real connection?

yschimke commented 2 years ago

Should User-Agent be configurable on OkHttpClient?

The hardcoded default seems like the issue here.

https://github.com/square/okhttp/blob/5c62ed796d05682c969b2636d3419b5bc214eb11/okhttp/src/jvmMain/kotlin/okhttp3/internal/http/BridgeInterceptor.kt#L80

import okhttp3.internal.userAgent

yschimke commented 2 years ago

@swankjesse would you object to allow setting the user agent on the client?