github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/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 // 9 // srvfiles [--h=HOST] [--p=PORT] [--d=DIR] 10 // 11 // Options: 12 // 13 // --h: hostname (default: 127.0.0.1) 14 // --p: port number (default: 8080) 15 // --d: directory to serve (default: .) 16 package main 17 18 import ( 19 "flag" 20 "log" 21 "net" 22 "net/http" 23 ) 24 25 var ( 26 host = flag.String("h", "127.0.0.1", "hostname") 27 port = flag.String("p", "8080", "port number") 28 dir = flag.String("d", ".", "directory to serve") 29 ) 30 31 var cacheHeaders = []string{ 32 "ETag", 33 "If-Modified-Since", 34 "If-None-Match", 35 "If-Range", 36 "If-Unmodified-Since", 37 } 38 39 func maxAgeHandler(h http.Handler) http.Handler { 40 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 41 for _, v := range cacheHeaders { 42 if r.Header.Get(v) != "" { 43 r.Header.Del(v) 44 } 45 } 46 47 h.ServeHTTP(w, r) 48 }) 49 } 50 51 func main() { 52 flag.Parse() 53 http.Handle("/", maxAgeHandler(http.FileServer(http.Dir(*dir)))) 54 log.Fatal(http.ListenAndServe(net.JoinHostPort(*host, *port), nil)) 55 }