Skip to content

Commit

Permalink
Merge pull request #2 from xamanu/master
Browse files Browse the repository at this point in the history
Avoid serving overview of main directory
  • Loading branch information
ThomasLeister authored Jul 8, 2018
2 parents 819d418 + 9054a5d commit 45c8da4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", contentType)
} else if r.Method == "GET" {
contentType := mime.TypeByExtension(filepath.Ext(fileStorePath))
if fileStorePath == "" {
http.Error(w, "403 Forbidden", 403)
return
}
if contentType == "" {
contentType = "application/octet-stream"
}
Expand Down
23 changes: 23 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,26 @@ func TestDownloadGet(t *testing.T) {
t.Errorf("handler returned wrong status code: got %v want %v. HTTP body: %s", status, http.StatusOK, rr.Body.String())
}
}

func TestEmptyGet(t *testing.T) {
// Set config
readConfig("config.toml", &conf)

// Create request
req, err := http.NewRequest("GET", "", nil)

if err != nil {
t.Fatal(err)
}

rr := httptest.NewRecorder()
handler := http.HandlerFunc(handleRequest)

// Send request and record response
handler.ServeHTTP(rr, req)

// Check status code
if status := rr.Code; status != http.StatusForbidden {
t.Errorf("handler returned wrong status code: got %v want %v. HTTP body: %s", status, http.StatusForbidden, rr.Body.String())
}
}

0 comments on commit 45c8da4

Please sign in to comment.