mirror of
https://github.com/containers/skopeo.git
synced 2026-02-22 07:06:32 +00:00
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
41 lines
918 B
Go
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))
|
|
}
|