github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/cmds/exp/srvfiles/srvfiles.go (about)

     1  // Copyright 2014-2017 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Serve files on the network.
     6  //
     7  // Synopsis:
     8  //     srvfiles [--h=HOST] [--p=PORT] [--d=DIR]
     9  //
    10  // Options:
    11  //     --h: hostname (default: 127.0.0.1)
    12  //     --p: port number (default: 8080)
    13  //     --d: directory to serve (default: .)
    14  package main
    15  
    16  import (
    17  	"flag"
    18  	"log"
    19  	"net"
    20  	"net/http"
    21  )
    22  
    23  var (
    24  	host = flag.String("h", "127.0.0.1", "hostname")
    25  	port = flag.String("p", "8080", "port number")
    26  	dir  = flag.String("d", ".", "directory to serve")
    27  )
    28  
    29  var cacheHeaders = []string{
    30  	"ETag",
    31  	"If-Modified-Since",
    32  	"If-None-Match",
    33  	"If-Range",
    34  	"If-Unmodified-Since",
    35  }
    36  
    37  func maxAgeHandler(h http.Handler) http.Handler {
    38  	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    39  
    40  		for _, v := range cacheHeaders {
    41  			if r.Header.Get(v) != "" {
    42  				r.Header.Del(v)
    43  			}
    44  		}
    45  
    46  		h.ServeHTTP(w, r)
    47  	})
    48  }
    49  
    50  func main() {
    51  	flag.Parse()
    52  	http.Handle("/", maxAgeHandler(http.FileServer(http.Dir(*dir))))
    53  	log.Fatal(http.ListenAndServe(net.JoinHostPort(*host, *port), nil))
    54  }