Skip to content

Commit

Permalink
Merge pull request #24 from sysvinit/misc-fixes
Browse files Browse the repository at this point in the history
Misc fixes
  • Loading branch information
ThomasLeister committed Dec 23, 2020
2 parents d2c2650 + ce597ed commit 263c6ff
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions prosody-filer.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
return
}

file, err := os.OpenFile(absFilename, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0755)
file, err := os.OpenFile(absFilename, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0644)
defer file.Close()
if err != nil {
log.Println("Creating new file failed:", err)
Expand All @@ -137,12 +137,16 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
http.Error(w, "403 Forbidden", 403)
return
}
} else if r.Method == "HEAD" {
} else if r.Method == "HEAD" || r.Method == "GET" {
fileinfo, err := os.Stat(absFilename)
if err != nil {
log.Println("Getting file information failed:", err)
http.Error(w, "404 Not Found", 404)
return
} else if fileinfo.IsDir() {
log.Println("Directory listing forbidden!")
http.Error(w, "403 Forbidden", 403)
return
}

/*
Expand All @@ -151,20 +155,18 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
* relying on file extensions.
*/
contentType := mime.TypeByExtension(filepath.Ext(fileStorePath))
w.Header().Set("Content-Length", strconv.FormatInt(fileinfo.Size(), 10))
w.Header().Set("Content-Type", contentType)
} else if r.Method == "GET" {
contentType := mime.TypeByExtension(filepath.Ext(fileStorePath))
if f, err := os.Stat(absFilename); err != nil || f.IsDir() {
log.Println("Directory listing forbidden!")
http.Error(w, "403 Forbidden", 403)
return
}
if contentType == "" {
contentType = "application/octet-stream"
}
http.ServeFile(w, r, absFilename)
w.Header().Set("Content-Type", contentType)

if r.Method == "HEAD" {
w.Header().Set("Content-Length", strconv.FormatInt(fileinfo.Size(), 10))
} else {
http.ServeFile(w, r, absFilename)
}

return
} else if r.Method == "OPTIONS" {
w.Header().Set("Allow", ALLOWED_METHODS)
return
Expand Down

0 comments on commit 263c6ff

Please sign in to comment.