Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misc fixes #24

Merged
merged 2 commits into from
Dec 23, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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