Skip to content

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

@kaleworsley

Description

@kaleworsley

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions