github.com/oweisse/u-root@v0.0.0-20181109060735-d005ad25fef1/cmds/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/http"
    20  )
    21  
    22  var (
    23  	host = flag.String("h", "127.0.0.1", "hostname")
    24  	port = flag.String("p", "8080", "port number")
    25  	dir  = flag.String("d", ".", "directory to serve")
    26  )
    27  
    28  var cacheHeaders = []string{
    29  	"ETag",
    30  	"If-Modified-Since",
    31  	"If-None-Match",
    32  	"If-Range",
    33  	"If-Unmodified-Since",
    34  }
    35  
    36  func maxAgeHandler(h http.Handler) http.Handler {
    37  	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    38  
    39  		for _, v := range cacheHeaders {
    40  			if r.Header.Get(v) != "" {
    41  				r.Header.Del(v)
    42  			}
    43  		}
    44  
    45  		h.ServeHTTP(w, r)
    46  	})
    47  }
    48  
    49  func main() {
    50  	flag.Parse()
    51  	http.Handle("/", maxAgeHandler(http.FileServer(http.Dir(*dir))))
    52  	log.Fatal(http.ListenAndServe(*host+":"+*port, nil))
    53  }