okhttp icon indicating copy to clipboard operation
okhttp copied to clipboard

Change user-agent on connect request?

Open regbo opened this issue 2 years ago • 6 comments

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

regbo avatar Jun 30 '22 19:06 regbo

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

swankjesse avatar Jul 01 '22 10:07 swankjesse

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?

regbo avatar Jul 01 '22 10:07 regbo

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.

swankjesse avatar Jul 01 '22 11:07 swankjesse

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

Or somehow allow intercepting on the Real connection?

regbo avatar Jul 01 '22 11:07 regbo

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 avatar Jul 01 '22 12:07 yschimke

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

yschimke avatar Jul 16 '22 12:07 yschimke