aboutsummaryrefslogtreecommitdiffstats
path: root/cmd/git-lfs-server
diff options
context:
space:
mode:
authorLibravatar Rutger Broekhoff2023-12-29 22:32:57 +0100
committerLibravatar Rutger Broekhoff2023-12-29 22:32:57 +0100
commitf1478c61558bfb520efebc47dc9c97a9d9d0bc9e (patch)
tree2fc6bb90d5f1a36ba7b1eeffad1672a0cbb96878 /cmd/git-lfs-server
parentae7e8cff49148926a4e445c94ac0206c466bb4e8 (diff)
downloadgitolfs3-f1478c61558bfb520efebc47dc9c97a9d9d0bc9e.tar.gz
gitolfs3-f1478c61558bfb520efebc47dc9c97a9d9d0bc9e.zip
Improve handling of MIME types
Diffstat (limited to 'cmd/git-lfs-server')
-rw-r--r--cmd/git-lfs-server/main.go22
1 files changed, 17 insertions, 5 deletions
diff --git a/cmd/git-lfs-server/main.go b/cmd/git-lfs-server/main.go
index f264aa0..0e2ea80 100644
--- a/cmd/git-lfs-server/main.go
+++ b/cmd/git-lfs-server/main.go
@@ -5,6 +5,7 @@ import (
5 "encoding/json" 5 "encoding/json"
6 "errors" 6 "errors"
7 "fmt" 7 "fmt"
8 "mime"
8 "net/http" 9 "net/http"
9 "net/http/cgi" 10 "net/http/cgi"
10 "net/url" 11 "net/url"
@@ -124,7 +125,7 @@ type lfsError struct {
124} 125}
125 126
126func makeRespError(w http.ResponseWriter, message string, code int) { 127func makeRespError(w http.ResponseWriter, message string, code int) {
127 w.Header().Set("Content-Type", lfsMIME) 128 w.Header().Set("Content-Type", lfsMIME+"; charset=utf-8")
128 w.WriteHeader(code) 129 w.WriteHeader(code)
129 json.NewEncoder(w).Encode(lfsError{Message: message}) 130 json.NewEncoder(w).Encode(lfsError{Message: message})
130} 131}
@@ -190,6 +191,17 @@ type parsedBatchObject struct {
190 size uint64 191 size uint64
191} 192}
192 193
194func isLFSMediaType(t string) bool {
195 if mediaType, params, err := mime.ParseMediaType(t); err == nil {
196 if mediaType == lfsMIME {
197 if params["charset"] == "" || strings.ToLower(params["charset"]) == "utf-8" {
198 return true
199 }
200 }
201 }
202 return false
203}
204
193func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { 205func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
194 submatches := re.FindStringSubmatch(r.URL.Path) 206 submatches := re.FindStringSubmatch(r.URL.Path)
195 if len(submatches) != 1 { 207 if len(submatches) != 1 {
@@ -198,12 +210,12 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
198 } 210 }
199 repo := strings.TrimPrefix("/", path.Clean(submatches[0])) 211 repo := strings.TrimPrefix("/", path.Clean(submatches[0]))
200 212
201 if !slices.Contains(r.Header.Values("Accept"), lfsMIME) { 213 if !slices.ContainsFunc(r.Header.Values("Accept"), isLFSMediaType) {
202 makeRespError(w, "Expected "+lfsMIME+" in list of acceptable response media types", http.StatusNotAcceptable) 214 makeRespError(w, "Expected "+lfsMIME+" (with UTF-8 charset) in list of acceptable response media types", http.StatusNotAcceptable)
203 return 215 return
204 } 216 }
205 if r.Header.Get("Content-Type") != lfsMIME { 217 if !isLFSMediaType(r.Header.Get("Content-Type")) {
206 makeRespError(w, "Expected request Content-Type to be "+lfsMIME, http.StatusUnsupportedMediaType) 218 makeRespError(w, "Expected request Content-Type to be "+lfsMIME+" (with UTF-8 charset)", http.StatusUnsupportedMediaType)
207 return 219 return
208 } 220 }
209 221