Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net/http: add ErrSchemeMismatch #44855

Closed
AkihiroSuda opened this issue Mar 8, 2021 · 16 comments
Closed

net/http: add ErrSchemeMismatch #44855

AkihiroSuda opened this issue Mar 8, 2021 · 16 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Proposal Proposal-Accepted
Milestone

Comments

@AkihiroSuda
Copy link
Contributor

AkihiroSuda commented Mar 8, 2021

What version of Go are you using (go version)?

$ go version
go version go1.16 linux/amd64

Does this issue reproduce with the latest release?

Yes

What did you expect to see?

I'd like to see the http: server gave HTTP response to HTTPS client error in the net/http package to be exposed as a constant that can be compared with errors.Is

err = errors.New("http: server gave HTTP response to HTTPS client")

Context

I'm working on a clone of Docker called nerdctl (contaiNERD ctl).
I want to compare http: server gave HTTP response to HTTPS client with errors.Is, for ease of reimplementing dockerd --insecure-registries, which attempts HTTPS first and then falls back to HTTP.

What did you see instead?

The http: server gave HTTP response to HTTPS client error is not exposed.

@toothrot toothrot added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Mar 8, 2021
@toothrot toothrot added this to the Backlog milestone Mar 8, 2021
@toothrot
Copy link
Contributor

toothrot commented Mar 8, 2021

/cc @bradfitz @rsc @empijei @neild

@neild
Copy link
Contributor

neild commented Mar 8, 2021

This seems reasonable to me, especially given that this error is replacing a more-informative (to programs) tls.RecordHeaderError.

Perhaps the error returned here should wrap the tls.RecordHeaderError rather than replacing it.

@neild
Copy link
Contributor

neild commented Mar 9, 2021

Three concrete possibilities that I can think of:

  1. Expose an ErrHTTPResponse var:

    ErrHTTPResponse = errors.New("http: server gave HTTP response to HTTPS client")

    [+] Simple.
    [-] Inconsistently returns ErrHTTPResponse or tls.RecordHeaderError depending on the response.

  2. Don't expose a var, but make the error wrap the underlying tls.RecordHeaderError. In the case where someone wants to detect this condition, they can use errors.As to recover the underlying error and examine it:

    var tlsErr tls.RecordHeaderError
    if errors.As(err, &tlsErr) && string(tlsErr.RecordHeader[:]) == "HTTP/" {
        // ...
    }

    [+] Consistently return the underlying tls.RecordHeaderError.
    [-] Detecting the error condition is more work for the caller.
    [+] Contract with the caller is clear: We provide the tls.RecordHeaderError and they can decide how to interpret it.

  3. Both: Return an error which errors.Is equivalent to an ErrHTTPResponse sentinel and which also wraps tls.RecordHeaderError.
    [-] Lot of mechanism for a fairly obscure case.

Option (2) requires the least change to the http API surface, but (1) is the simplest. Either of those seems reasonable to me.

@AkihiroSuda
Copy link
Contributor Author

Can we wrap (2) in a function like func IsNotHTTPS(err error) bool?

@bradfitz
Copy link
Contributor

I'm fine with (2). Or (1) if we name it, say, ErrSchemeMismatch ("HTTP" in "ErrHTTPResponse" sounds too generic, as "HTTP" often also means "HTTPS").

I wouldn't want to see a new function. That's too heavy in godoc for how fringe this is.

@nathalie21005
Copy link

Hi @bradfitz @rsc @empijei @neild any solution regarding this error message? @AkihiroSuda did you found the solution?
I tried to post and I got the same error message "http server gave http response to https client"

Thank you

AkihiroSuda added a commit to AkihiroSuda/go that referenced this issue Jan 31, 2022
Expose "http: server gave HTTP response to HTTPS client" error as
ErrSchemeMismatch, so that it can be compared with errors.Is .

Fixes golang#44855

Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
AkihiroSuda added a commit to AkihiroSuda/go that referenced this issue Jan 31, 2022
Expose "http: server gave HTTP response to HTTPS client" error as
ErrSchemeMismatch, so that it can be compared with errors.Is .

Fixes golang#44855

Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
@AkihiroSuda
Copy link
Contributor Author

AkihiroSuda commented Jan 31, 2022

PR: #50939 (ErrSchemeMismatch)

@gopherbot
Copy link

Change https://golang.org/cl/382117 mentions this issue: net/http: expose "http: server gave HTTP response to HTTPS client" error

@AkihiroSuda
Copy link
Contributor Author

@bradfitz Any chance to get the PR #50939 merged? 🙏

@neild neild changed the title net/http: Expose "http: server gave HTTP response to HTTPS client" error proposal: net/http: add ErrSchemeMismatch Mar 7, 2023
@neild neild added the Proposal label Mar 7, 2023
@neild
Copy link
Contributor

neild commented Mar 7, 2023

This is a proposed API change, so adding to the proposal process.

Specific proposal:

Add http.ErrSchemeMismatch, a distinguishable error that indicates an HTTPS server received what looks like an HTTP request. This converts an existing error to an exported, distinguishable symbol. See https://go.dev/cl/382117.

@seankhliao seankhliao modified the milestones: Backlog, Proposal Mar 8, 2023
@rsc
Copy link
Contributor

rsc commented Mar 8, 2023

This proposal has been added to the active column of the proposals project
and will now be reviewed at the weekly proposal review meetings.
— rsc for the proposal review group

@rsc
Copy link
Contributor

rsc commented Mar 15, 2023

Sounds like everyone is in favor of this. Have all concerns been addressed?

@AkihiroSuda
Copy link
Contributor Author

Sounds like everyone is in favor of this. Have all concerns been addressed?

I don't see any remaining concern

@rsc
Copy link
Contributor

rsc commented Mar 29, 2023

Based on the discussion above, this proposal seems like a likely accept.
— rsc for the proposal review group

@rsc
Copy link
Contributor

rsc commented Apr 6, 2023

No change in consensus, so accepted. 🎉
This issue now tracks the work of implementing the proposal.
— rsc for the proposal review group

@rsc rsc changed the title proposal: net/http: add ErrSchemeMismatch net/http: add ErrSchemeMismatch Apr 6, 2023
@rsc rsc modified the milestones: Proposal, Backlog Apr 6, 2023
AkihiroSuda added a commit to AkihiroSuda/go that referenced this issue Apr 7, 2023
Expose "http: server gave HTTP response to HTTPS client" error as
ErrSchemeMismatch, so that it can be compared with errors.Is .

Fixes golang#44855

Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
@gopherbot
Copy link

Change https://go.dev/cl/496795 mentions this issue: doc: fill out net/http.ErrSchemeMismatch note

@dmitshur dmitshur modified the milestones: Backlog, Go1.21 May 26, 2023
gopherbot pushed a commit that referenced this issue May 26, 2023
For #44855

Change-Id: I517394b227ea948ed3e1f9ffdaab2bb2676863c8
Reviewed-on: https://go-review.googlesource.com/c/go/+/496795
TryBot-Bypass: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Run-TryBot: Ian Lance Taylor <iant@google.com>
Desiki-high added a commit to Desiki-high/nydus that referenced this issue Dec 22, 2023
ref: golang/go#44855

Signed-off-by: Yadong Ding <ding_yadong@foxmail.com>
Desiki-high added a commit to Desiki-high/nydus that referenced this issue Dec 22, 2023
ref: golang/go#44855

Signed-off-by: Yadong Ding <ding_yadong@foxmail.com>
imeoer pushed a commit to dragonflyoss/nydus that referenced this issue Dec 25, 2023
ref: golang/go#44855

Signed-off-by: Yadong Ding <ding_yadong@foxmail.com>
Desiki-high added a commit to Desiki-high/nydus that referenced this issue Dec 27, 2023
ref: golang/go#44855

Signed-off-by: Yadong Ding <ding_yadong@foxmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Proposal Proposal-Accepted
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants