runtime: HttpClient does not respect DefaultRequestVersion with version 3 and DefaultVersionPolicy.RequestVersionOrLower

Description

When you initialize a httpclient with the following configuration

                Client = new HttpClient()
                {
                    DefaultRequestVersion = HttpVersion.Version30,
                    DefaultVersionPolicy = HttpVersionPolicy.RequestVersionOrLower
                };

It will first attempt to make a h2 connection to the target server, if the server advertises h3 via alt-svc subsequent connections will be upgraded to http3.

Reproduction Steps

Initialize a httpclient with the following configuration

                Client = new HttpClient()
                {
                    DefaultRequestVersion = HttpVersion.Version30,
                    DefaultVersionPolicy = HttpVersionPolicy.RequestVersionOrLower
                };

Attempt to connect to a server that supports http3 running php Check the $_SERVER[‘SERVER_PROTOCOL’] and it will be “HTTP/2.0”

Expected behavior

HttpClient should first attempt to connect via quic/http3 as it is the DefaultRequestVersion, not HttpVersion.Version20

Actual behavior

HttpClient connects via h2 and checks for alt-svc to upgrade subsequent connections.

Regression?

No response

Known Workarounds

Use DefaultVersionPolicy.RequestVersionExact

Configuration

No response

Other information

My software operates in censorship prone environments. I’ve been testing the utilization of h3 to help improve connectivity in such environments. While the results have been positive, there does not seem to be a silver bullet that works for all users. I would think that given the provided configuration that a attempt would be made in h3, and if that fails then to try use h2/h1. Currently im shipping using DefaultVersionPolicy.RequestVersionExact but while this improves the connectivity of more users, I can predict some users local firewall or proxy configurations may make h3 requests impossible. In those cases I would like the httpclient to dropdown to standard tcp connections.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 26 (17 by maintainers)

Most upvoted comments

Probably just that H3 isn’t supported when using a proxy since we can’t tunnel UDP?

but for #2 System.Net.Quic.QuicConnection.IsSupported returns true. Is there some other way to tell that quic is unsupported?

This is interesting, I’ll have to test it out. I’d expect to have some inner exception in this case (QuicConnection.IsSupported == true). Although you say you’re running .NET 8 and I’m checking current main (future 10.0, give or take 9.0 code) and there were changes in H/3 connection handling in 9.0. Could you try with .NET 9? If you really want to get to the bottom of it.

I can confirm with .net9, same situation fiddler classic running in the background, QuicConnection.IsSupported returns true, and the h3 connection attempt is met with a VersionNegotiationError with a null inner exception.

The problem with catch (HttpRequestException ex) is that this exception type is also used for all the other errors that might happen when the request is already being sent or the response is being received. For example, you can get any of these wrapped in that. And in many such cases, because the request might have been already (partially or fully) sent, you should not re-send it:

  • it might not be idempotent
  • the request content might not be rewindable

For that reason, I put some details in exception filtering in the example. You should be more selective and make sure that the filter for your exceptions catches only connection problems and nothing else.

Thank you for the extra insight into the error codes

                    catch (HttpRequestException ex) when (ex.HttpRequestError == HttpRequestError.VersionNegotiationError || (HasQuicException(ex, out var qex) && qex.QuicError is QuicError.ConnectionTimeout or QuicError.ConnectionRefused or  QuicError.ConnectionIdle))

how does this filtering look?

Added QuicError.ConnectionIdle, as that error is thrown + HttpRequestError = ConnectionError when UDP is blocked on 443

The problem with catch (HttpRequestException ex) is that this exception type is also used for all the other errors that might happen when the request is already being sent or the response is being received. For example, you can get any of these wrapped in that. And in many such cases, because the request might have been already (partially or fully) sent, you should not re-send it:

  • it might not be idempotent
  • the request content might not be rewindable

For that reason, I put some details in exception filtering in the example. You should be more selective and make sure that the filter for your exceptions catches only connection problems and nothing else.

Thank you for the extra insight into the error codes

                    catch (HttpRequestException ex) when (ex.HttpRequestError == HttpRequestError.VersionNegotiationError || (HasQuicException(ex, out var qex) && qex.QuicError is QuicError.ConnectionTimeout or QuicError.ConnectionRefused or  QuicError.ConnectionIdle))

how does this filtering look?

attempt to connect via udp, and if that fails to drop down to H2 or even H1

How are we to decide which QUIC transport errors to be swallowed and which to be propagated? For comparison, if we fail to establish TCP connection we propagate that error to the user, we don’t attempt anything else.

but they must also wrap their initial requests in a try/catch wrapper to handle HttpRequestException, recreate the httpclient object, and drop it down manually to h2+HttpVersionPolicy.RequestVersionOrLower. This makes everyone usage of the httpclient more difficult .

You could provide them with an extension method, or a custom HttpMessageHandler that does what you’re describing here. There are solutions/workarounds to this without your users doing the hard work and without changing .NET runtime itself.

My software faces some interesting challenges is that we allow 3rd party code to be run as plugins, I’d like to make these 3rd party developers lives as easy as possible, figuring out the difficult stuff. We also operate in a censorship prone environment where normal tls connections are regularly impossible to establish.

What I’m getting there is that your set up is pretty specific. In other words, this is not a common scenario. And for that reason, it’s also unlikely that we would do any changes. But I can keep this open to collect interest from other users to see if this gets any traction, we can then re-evaluate the current behavior.

I think this would be expected e.g. the client would not use the version unless there is confidence it can actually work. I you really want to force H/3 you can use HttpVersionPolicy.RequestVersionExact