Files
skopeo/tests/serve.go
Nalin Dahyabhai 19a7165783 Apply Last-Modified time as mtime when saving URLs
When saving the contents of a URL to a local file, attempt to set mtime
based on the response's Last-Modified header, if there is one.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>

Closes: #58
Approved by: nalind
2017-03-29 19:38:20 +00:00

41 lines
918 B
Go

package main
import (
"log"
"net/http"
"os"
"path/filepath"
)
func sendThatFile(basepath string) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
filename := filepath.Join(basepath, filepath.Clean(string([]rune{filepath.Separator})+r.URL.Path))
f, err := os.Open(filename)
if err != nil {
if os.IsNotExist(err) {
http.NotFound(w, r)
return
}
http.Error(w, "whoops", http.StatusInternalServerError)
return
}
finfo, err := f.Stat()
if err != nil {
http.Error(w, "whoops", http.StatusInternalServerError)
return
}
http.ServeContent(w, r, filename, finfo.ModTime(), f)
}
}
func main() {
args := os.Args
if len(args) < 3 {
log.Fatal("requires listening port and subdirectory path")
}
port := args[1]
basedir := args[2]
http.HandleFunc("/", sendThatFile(basedir))
log.Fatal(http.ListenAndServe(":"+port, nil))
}