Просмотр исходного кода

server: fix "multiple response.WriteHeader calls".

The server no does nothing for errors other than ErrNotFound (for which
it calls http.NotFound). This is becase when getting error on writing,
we already wrote headers, so can't respond with status 500.

Possibly log errors in future?
Dmitry Chestnykh 15 лет назад
Родитель
Сommit
1f1174743d
1 измененных файлов с 6 добавлено и 10 удалено
  1. 6 10
      server.go

+ 6 - 10
server.go

@@ -43,10 +43,6 @@ func (h *captchaHandler) serve(w http.ResponseWriter, id, ext string, download b
 		}
 		}
 		return WriteImage(w, id, h.imgWidth, h.imgHeight)
 		return WriteImage(w, id, h.imgWidth, h.imgHeight)
 	case ".wav":
 	case ".wav":
-		if !download {
-			w.Header().Set("Content-Type", "audio/x-wav")
-		}
-		//return WriteAudio(w, id)
 		//XXX(dchest) Workaround for Chrome: it wants content-length,
 		//XXX(dchest) Workaround for Chrome: it wants content-length,
 		//or else will start playing NOT from the beginning.
 		//or else will start playing NOT from the beginning.
 		//Filed issue: http://code.google.com/p/chromium/issues/detail?id=80565
 		//Filed issue: http://code.google.com/p/chromium/issues/detail?id=80565
@@ -55,6 +51,9 @@ func (h *captchaHandler) serve(w http.ResponseWriter, id, ext string, download b
 			return ErrNotFound
 			return ErrNotFound
 		}
 		}
 		a := NewAudio(d)
 		a := NewAudio(d)
+		if !download {
+			w.Header().Set("Content-Type", "audio/x-wav")
+		}
 		w.Header().Set("Content-Length", strconv.Itoa(a.EncodedLen()))
 		w.Header().Set("Content-Length", strconv.Itoa(a.EncodedLen()))
 		_, err := a.WriteTo(w)
 		_, err := a.WriteTo(w)
 		return err
 		return err
@@ -74,11 +73,8 @@ func (h *captchaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 		Reload(id)
 		Reload(id)
 	}
 	}
 	download := path.Base(dir) == "download"
 	download := path.Base(dir) == "download"
-	if err := h.serve(w, id, ext, download); err != nil {
-		if err == ErrNotFound {
-			http.NotFound(w, r)
-			return
-		}
-		http.Error(w, "error serving captcha", http.StatusInternalServerError)
+	if h.serve(w, id, ext, download) == ErrNotFound {
+		http.NotFound(w, r)
 	}
 	}
+	// Ignore other errors.
 }
 }