Actually set the response status correctly

Reading the docs helps.
This commit is contained in:
Bendodroid 2023-11-04 22:30:39 +01:00
parent 6d70d29050
commit 26a9cf4911
Signed by: bendodroid
GPG key ID: 3EEE19A0F73D5FFC

View file

@ -41,8 +41,8 @@ func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet { if r.Method != http.MethodGet {
log.Println("Wrong METHOD from", r.RemoteAddr) log.Println("Wrong METHOD from", r.RemoteAddr)
w.WriteHeader(http.StatusMethodNotAllowed)
w.Header().Set("allow", http.MethodGet) w.Header().Set("allow", http.MethodGet)
w.WriteHeader(http.StatusMethodNotAllowed)
return return
} }
@ -61,15 +61,15 @@ func main() {
username, password, ok := r.BasicAuth() username, password, ok := r.BasicAuth()
if !ok || username != validUsername || password != validPassword { if !ok || username != validUsername || password != validPassword {
log.Println("Unauthorized request from", r.RemoteAddr) log.Println("Unauthorized request from", r.RemoteAddr)
w.WriteHeader(http.StatusUnauthorized)
w.Header().Set("www-authentication", "Basic realm=\"space-api\"") w.Header().Set("www-authentication", "Basic realm=\"space-api\"")
w.WriteHeader(http.StatusUnauthorized)
return return
} }
if r.Method != http.MethodPut { if r.Method != http.MethodPut {
log.Println("Wrong METHOD from", r.RemoteAddr) log.Println("Wrong METHOD from", r.RemoteAddr)
w.WriteHeader(http.StatusMethodNotAllowed)
w.Header().Set("allow", http.MethodPut) w.Header().Set("allow", http.MethodPut)
w.WriteHeader(http.StatusMethodNotAllowed)
return return
} }