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