Fixed host selectors by provising a -host option and passing this through to server and handlers
This commit is contained in:
parent
91ba32dc29
commit
3cea3ab217
@ -19,6 +19,7 @@ func cwd() string {
|
|||||||
func main() {
|
func main() {
|
||||||
var (
|
var (
|
||||||
bind = flag.String("bind", ":70", "port to listen on")
|
bind = flag.String("bind", ":70", "port to listen on")
|
||||||
|
host = flag.String("host", "localhost", "fqdn hostname")
|
||||||
root = flag.String("root", cwd(), "root directory to serve")
|
root = flag.String("root", cwd(), "root directory to serve")
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -26,5 +27,6 @@ func main() {
|
|||||||
|
|
||||||
gopher.Handle("/", gopher.FileServer(gopher.Dir(*root)))
|
gopher.Handle("/", gopher.FileServer(gopher.Dir(*root)))
|
||||||
|
|
||||||
log.Fatal(gopher.ListenAndServe(*bind, nil))
|
server := gopher.Server{Addr: *bind, Hostname: *host}
|
||||||
|
log.Fatal(server.ListenAndServe())
|
||||||
}
|
}
|
||||||
|
13
gopher.go
13
gopher.go
@ -531,6 +531,8 @@ type Server struct {
|
|||||||
Addr string // TCP address to listen on, ":gopher" if empty
|
Addr string // TCP address to listen on, ":gopher" if empty
|
||||||
Handler Handler // handler to invoke, gopher.DefaultServeMux if nil
|
Handler Handler // handler to invoke, gopher.DefaultServeMux if nil
|
||||||
|
|
||||||
|
Hostname string // FQDN Hostname to reach this server on
|
||||||
|
|
||||||
// ErrorLog specifies an optional logger for errors accepting
|
// ErrorLog specifies an optional logger for errors accepting
|
||||||
// connections and unexpected behavior from handlers.
|
// connections and unexpected behavior from handlers.
|
||||||
// If nil, logging goes to os.Stderr via the log package's
|
// If nil, logging goes to os.Stderr via the log package's
|
||||||
@ -558,7 +560,7 @@ func (sh serverHandler) ServeGopher(rw ResponseWriter, req *Request) {
|
|||||||
//
|
//
|
||||||
// If the address is not a FQDN, LocalHost as passed to the Handler
|
// If the address is not a FQDN, LocalHost as passed to the Handler
|
||||||
// may not be accessible to clients, so links may not work.
|
// may not be accessible to clients, so links may not work.
|
||||||
func (s Server) ListenAndServe() error {
|
func (s *Server) ListenAndServe() error {
|
||||||
addr := s.Addr
|
addr := s.Addr
|
||||||
if addr == "" {
|
if addr == "" {
|
||||||
addr = ":70"
|
addr = ":70"
|
||||||
@ -608,7 +610,7 @@ func (s *Server) ListenAndServeTLS(certFile, keyFile string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Serve ...
|
// Serve ...
|
||||||
func (s Server) Serve(l net.Listener) error {
|
func (s *Server) Serve(l net.Listener) error {
|
||||||
defer l.Close()
|
defer l.Close()
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
@ -731,8 +733,15 @@ func (c *conn) readRequest(ctx context.Context) (w *response, err error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
server := ctx.Value(ServerContextKey).(*Server)
|
||||||
|
if server.Hostname == "" {
|
||||||
req.LocalHost = host
|
req.LocalHost = host
|
||||||
req.LocalPort = int(n)
|
req.LocalPort = int(n)
|
||||||
|
} else {
|
||||||
|
req.LocalHost = server.Hostname
|
||||||
|
// TODO: Parse this from -bind option
|
||||||
|
req.LocalPort = int(n)
|
||||||
|
}
|
||||||
|
|
||||||
w = &response{
|
w = &response{
|
||||||
conn: c,
|
conn: c,
|
||||||
|
Loading…
Reference in New Issue
Block a user