tsweb: add a Server, which serves HTTP/HTTPS with various good defaults.

Updates #3797

Signed-off-by: David Anderson <danderson@tailscale.com>
This commit is contained in:
David Anderson
2022-02-01 20:09:12 -08:00
parent fa612c28cf
commit 754a6d0514
3 changed files with 402 additions and 1 deletions

View File

@@ -280,6 +280,7 @@ func (h retHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// response code that gets sent, if any.
type loggingResponseWriter struct {
http.ResponseWriter
ctx context.Context
code int
bytes int
hijacked bool
@@ -330,6 +331,20 @@ func (l loggingResponseWriter) Flush() {
f.Flush()
}
func (l *loggingResponseWriter) httpCode() int {
switch {
case l.code != 0:
return l.code
case l.hijacked:
return http.StatusSwitchingProtocols
case l.ctx.Err() == context.Canceled:
return 499 // nginx convention: Client Closed Request
default:
// Handler didn't write a body or send a header, that means 200.
return 200
}
}
// HTTPError is an error with embedded HTTP response information.
//
// It is the error type to be (optionally) used by Handler.ServeHTTPReturn.