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

proposal: net/http: add Listen and ListenTLS to http.Server #12731

Closed
kaleworsley opened this issue Sep 24, 2015 · 7 comments

Comments

Projects
None yet
7 participants
@kaleworsley
Copy link

commented Sep 24, 2015

I'd like to extract part of ListenAndServe and ListenAndServeTLS into separate
Listen and ListenTLS methods, respectively. This would make it possible to call
syscall.Setuid after listening on a privileged port.

For example:

package main

import (
    "fmt"
    "net/http"
    "syscall"
)

func handle(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello! I'm UID 501.")
}

func main() {
    s := http.Server{
        Addr:    ":80",
        Handler: http.HandlerFunc(handle),
    }

    ln, err := s.Listen()

    if err != nil {
        panic(err)
    }

    syscall.Setuid(501)

    s.Serve(ln)
}

I have written a patch, but wanted to check if it's a reasonable idea before submitting it.

@cespare

This comment has been minimized.

Copy link
Contributor

commented Sep 24, 2015

You can easily do this today by making your own net.Listener.

ln, err := net.Listen("tcp", ":80")
if err != nil {
  // ...
}

The only thing ListenAndServe adds beyond this is TCP keepalive, but that's also easy to do yourself.

@kaleworsley

This comment has been minimized.

Copy link
Author

commented Sep 24, 2015

That's true. That's what I've done up until now, though I was hoping to avoid redoing the TCP keepalive code, since it's already there in net/http, unexported.

@minux

This comment has been minimized.

Copy link
Member

commented Sep 24, 2015

@kennylevinsen

This comment has been minimized.

Copy link

commented Sep 24, 2015

I agree that it's a little bit annoying that "ListenAndServe" isn't directly the same as "Serve" with a net.Listener. I think this should definitely be added to the docs to inform that ListenAndServe != Listen + Serve. However, I think that having a special http.Listen, which would be identical to net.Listen except for keep-alive, would be too much, and will just add to the confusion, without really solving that much.

Not only does "Listen" by itself not say anything obvious about keep-alive, but keep-alive is in no way HTTP specific. You should do this in almost all your Accept loops. Instead, you could request for a net.ListenWithKeepAlive, but I'm not sure that request would gain much traction.

@bradfitz

This comment has been minimized.

Copy link
Member

commented Sep 24, 2015

I would rather fix this with documentation (about ListenAndServe doing the keep-alive stuff), and ideally not much documentation. I don't want to add more API surface.

The net and net/http packages are large enough. The small percentage of people needing custom listeners can code up exactly what they want. Once we added some sort of keep-alive-listener type, then the question would be how to configure it. Best to just let people write the thing they want. It's not much code.

/cc @adg since it's a proposal

@adg

This comment has been minimized.

Copy link
Contributor

commented Sep 24, 2015

Agree with @bradfitz that we should just document the status quo.

@kaleworsley

This comment has been minimized.

Copy link
Author

commented Sep 24, 2015

No problem. Thank's for the response.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.