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

caddyhttp: Make use of http.ResponseController #5654

Merged
merged 7 commits into from
Aug 2, 2023
Merged

Conversation

francislavoie
Copy link
Member

One part of #5288, should fix #5431 (if built with Go 1.21+).

Adds a new enable_full_duplex server global option to opt-in (only effectual for Go 1.21+, with a go:build constraint). I think it needs to be opt-in because it could cause deadlocks if the client doesn't know how to handle a connection in that mode, but it seems necessary to turn on for certain kinds of apps. Maybe this should be an HTTP handler (maybe as a new option for https://caddyserver.com/docs/caddyfile/directives/request_body) which would allow using matchers to conditionally enable it instead of enabling it for all connections to the server 🤔 But I marked this as experimental so we can adjust later if needed.

The other main aspect of this is dropping caddyhttp.HTTPInterfaces because it's now made redundant by http.ResponseController which gives a way to use these optional interfaces without type assertions. Simplifies code in most cases.

Also syncs the reverseproxy implementation with stdlib's which now uses ResponseController as well; see golang/go@2449bbb for the original diff that I ported over.

And finally, minor improvement, io.Copy() already tries io.ReaderFrom so we don't need to do that ourselves in responsewriter.go.

@francislavoie francislavoie added the feature ⚙️ New feature or request label Jul 26, 2023
@francislavoie francislavoie added this to the v2.7.0 milestone Jul 26, 2023
@francislavoie
Copy link
Member Author

Linter doesn't like this new pattern. Opened an issue: timakin/bodyclose#52

Also the Go stdlib code we ported ignores the err returned by m.flush(), so I need to suppress it to keep the code the same 🤷‍♂️

Copy link
Member

@mholt mholt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this definitely looks more elegant than what we were doing before.

I imagine there aren't external repos using our HTTPInterfaces type?

modules/caddyhttp/server.go Show resolved Hide resolved
Copy link
Member

@mholt mholt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM now. Thank you for contributing this!

@francislavoie
Copy link
Member Author

I'd just like @WeidiDeng to give his 👍 on this as well in case I missed something.

@WeidiDeng
Copy link
Member

I'd just like @WeidiDeng to give his 👍 on this as well in case I missed something.

There is something about encode that I don't like, can it be type asserted just like before to avoid wrapping a caddyhttp.ResponseWriterWrapper?

@francislavoie
Copy link
Member Author

Hmm. I'm not sure what we would type assert at all. We already know it's a ResponseWriter. Do we even need wrapping now that response controller lets us call those optional interfaces in a unified way? Does our wrapper add any value, or is it necessary for it to work properly?

If you have a suggestion, feel free to commit it 🤔 I'm not sure I follow.

@WeidiDeng
Copy link
Member

I mean like this:

if rww, ok := wrappedRW.(*caddyhttp.ResponseWriterWrapper); ok {
  rw.ResponseWriter = rww
} else {
  rw.ResponseWriter = &caddyhttp.ResponseWriterWrapper{ResponseWriter: wrappedRW}
}

@francislavoie
Copy link
Member Author

Aaah I see, so don't re-wrap if it's already wrapped. Makes sense.

@francislavoie
Copy link
Member Author

Thanks, pushed that fix. Do you notice anything else?

@mholt mholt enabled auto-merge (squash) August 2, 2023 17:14
@mholt
Copy link
Member

mholt commented Aug 2, 2023

I'll probably merge this in a few minutes to a few hours (before I have to take off for some family stuff today) so I can tag 2.7. Thank you for working on this!

@mholt
Copy link
Member

mholt commented Aug 2, 2023

And @WeidiDeng thank you for your invaluable review and contributions 🙏

@mholt mholt merged commit cd486c2 into master Aug 2, 2023
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature ⚙️ New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Caddy prematurely closes connections to reverse proxies when using HTTP/1
3 participants