Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-pages.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Thomas <nick@gitlab.com>2017-07-03 18:22:16 +0300
committerNick Thomas <nick@gitlab.com>2017-07-03 18:36:16 +0300
commit62409e8cb2dc856134587c3479f0a3da8b3da53b (patch)
tree15afbc63836fb7016df3a2bf5a5a0fb81b20b40c
parentd7de6b25b36ae245c9befdd09162a169b9d28651 (diff)
Rationalise serveFile and serveCustomFile
-rw-r--r--domain.go75
1 files changed, 40 insertions, 35 deletions
diff --git a/domain.go b/domain.go
index 8af0e394..6d24c17a 100644
--- a/domain.go
+++ b/domain.go
@@ -27,29 +27,40 @@ func acceptsGZip(r *http.Request) bool {
if r.Header.Get("Range") != "" {
return false
}
+
offers := []string{"gzip", "identity"}
acceptedEncoding := httputil.NegotiateContentEncoding(r, offers)
return acceptedEncoding == "gzip"
}
-func (d *domain) serveFile(w http.ResponseWriter, r *http.Request, fullPath string) error {
- // Open and serve content of file
- if acceptsGZip(r) {
- _, err := os.Stat(fullPath + ".gz")
- if err == nil {
- // Set the content type based on the non-gzipped extension
- _, haveType := w.Header()["Content-Type"]
- if !haveType {
- ctype := mime.TypeByExtension(filepath.Ext(fullPath))
- w.Header().Set("Content-Type", ctype)
- }
-
- // Serve up the gzipped version
- fullPath += ".gz"
- w.Header().Set("Content-Encoding", "gzip")
- }
+func handleGZip(w http.ResponseWriter, r *http.Request, fullPath string) string {
+ if !acceptsGZip(r) {
+ return fullPath
+ }
+
+ gzipPath := fullPath + ".gz"
+
+ _, err := os.Stat(gzipPath)
+ if err != nil {
+ return fullPath
}
+ w.Header().Set("Content-Encoding", "gzip")
+
+ return gzipPath
+}
+
+func setContentType(w http.ResponseWriter, fullPath string) {
+ ext := filepath.Ext(fullPath)
+ ctype := mime.TypeByExtension(ext)
+ if ctype != "" {
+ w.Header().Set("Content-Type", ctype)
+ }
+}
+
+func (d *domain) serveFile(w http.ResponseWriter, r *http.Request, origPath string) error {
+ fullPath := handleGZip(w, r, origPath)
+
file, err := os.Open(fullPath)
if err != nil {
return err
@@ -62,21 +73,16 @@ func (d *domain) serveFile(w http.ResponseWriter, r *http.Request, fullPath stri
}
fmt.Println("Serving", fullPath, "for", r.URL.Path)
- http.ServeContent(w, r, filepath.Base(file.Name()), fi.ModTime(), file)
+
+ // ServeContent sets Content-Type for us
+ http.ServeContent(w, r, origPath, fi.ModTime(), file)
return nil
}
-func (d *domain) serveCustomFile(w http.ResponseWriter, r *http.Request, code int, fullPath string) error {
+func (d *domain) serveCustomFile(w http.ResponseWriter, r *http.Request, code int, origPath string) error {
+ fullPath := handleGZip(w, r, origPath)
+
// Open and serve content of file
- ext := filepath.Ext(fullPath)
- if acceptsGZip(r) {
- _, err := os.Stat(fullPath + ".gz")
- if err == nil {
- // Serve up the gzipped version
- fullPath += ".gz"
- w.Header().Set("Content-Encoding", "gzip")
- }
- }
file, err := os.Open(fullPath)
if err != nil {
return err
@@ -88,19 +94,18 @@ func (d *domain) serveCustomFile(w http.ResponseWriter, r *http.Request, code in
return err
}
- fmt.Println("Serving", fullPath, "for", r.URL.Path, "with", code)
+ setContentType(w, origPath)
+ w.Header().Set("Content-Length", strconv.FormatInt(fi.Size(), 10))
// Serve the file
- _, haveType := w.Header()["Content-Type"]
- if !haveType {
- ctype := mime.TypeByExtension(ext)
- w.Header().Set("Content-Type", ctype)
- }
- w.Header().Set("Content-Length", strconv.FormatInt(fi.Size(), 10))
+ fmt.Println("Serving", fullPath, "for", r.URL.Path, "with", code)
w.WriteHeader(code)
+
if r.Method != "HEAD" {
- io.CopyN(w, file, fi.Size())
+ _, err := io.CopyN(w, file, fi.Size())
+ return err
}
+
return nil
}